I'm currently working on a node.js project that requires generating and altering objects in memory at and during runtime. I know this sounds confusing, so here's an example.
Monday 10am:
var PI = Math.PI;
exports.area = function (r) {
return PI * r * r;
};
Monday 11am:
var PI = Math.PI;
var minSize = 10;
exports.area = function (r) {
if (r >= minSize) {
return PI * r * r;
} else {
return "Error: Minimum size not met.";
}
};
Monday 12noon:
var PI = Math.PI;
var minSize = 10;
var msg = "Error: Minimum size of " + minSize + "not met.";
exports.area = function (r) {
if (r >= minSize) {
return PI * r * r;
} else {
return msg;
}
};
Tuesday 10am:
var PI = Math.PI;
var minSize = 15;
var error['eng'] = "Error: Minimum size of " + minSize + "not met.";
var error['chn'] = "錯誤:不符合最小尺寸:" + minSize + "。"
exports.area = function (r) {
if (r >= minSize) {
return PI * r * r;
} else {
return error;
}
};
exports.circumference = function (r) {
if (r >= minSize) {
return 2 * PI * r;
} else {
return error;
}
};
This is a bit of a contrived example, because most of the changes could avoided with proper planning, I know, but it sort of goes to illustrate the point. This module kept changing in both variables and functions, expanding while the server is running.
Here are the restrictions I have:
I've thought of two possible strategies:
Using .prototype, continue to add/change functions and variables.
Editing the files directly and reloading them through something like node-supervisor.
That's all I can think of right now. Would appreciate any opinions or options in this area.
For future reference:
Conclusion: It's not impossible to store functions and execute them safely within the confines of a sandbox, it's just not very practical.
Alternate Solution: If the functions themselves are not too complex, it's not impossible to store the calls in the form of a DSL and parse them back out after.