mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
Prevent RED.node.registerNode from overriding a constructor's prototype (#865)
* prevent registry.registerNodeConstructor from overriding a constructors protoype * fix for node < v5.0.0 * exercise another code path * altering __proto__ for node < v0.12 * move inheritance code to helper function
This commit is contained in:
committed by
Nick O'Leary
parent
e1d09349ff
commit
b909e32201
@@ -326,6 +326,27 @@ function getCaller(){
|
||||
return stack[0].getFileName();
|
||||
}
|
||||
|
||||
function inheritNode(constructor) {
|
||||
if(Object.getPrototypeOf(constructor.prototype) === Object.prototype) {
|
||||
util.inherits(constructor,Node);
|
||||
} else {
|
||||
var proto = constructor.prototype;
|
||||
while(Object.getPrototypeOf(proto) !== Object.prototype) {
|
||||
proto = Object.getPrototypeOf(proto);
|
||||
}
|
||||
//TODO: This is a partial implementation of util.inherits >= node v5.0.0
|
||||
// which should be changed when support for node < v5.0.0 is dropped
|
||||
// see: https://github.com/nodejs/node/pull/3455
|
||||
proto.constructor.super_ = Node;
|
||||
if(Object.setPrototypeOf) {
|
||||
Object.setPrototypeOf(proto, Node.prototype);
|
||||
} else {
|
||||
// hack for node v0.10
|
||||
proto.__proto__ = Node.prototype;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function registerNodeConstructor(type,constructor) {
|
||||
if (nodeConstructors[type]) {
|
||||
throw new Error(type+" already registered");
|
||||
@@ -333,7 +354,10 @@ function registerNodeConstructor(type,constructor) {
|
||||
//TODO: Ensure type is known - but doing so will break some tests
|
||||
// that don't have a way to register a node template ahead
|
||||
// of registering the constructor
|
||||
util.inherits(constructor,Node);
|
||||
if(!(constructor.prototype instanceof Node)) {
|
||||
inheritNode(constructor);
|
||||
}
|
||||
|
||||
nodeConstructors[type] = constructor;
|
||||
events.emit("type-registered",type);
|
||||
}
|
||||
|
Reference in New Issue
Block a user