plot-plan-designer
Version:
Design Editor Tools with React.js + ant.design + fabric.js
57 lines (56 loc) • 1.74 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const UnsafetyWordException_1 = __importDefault(require("../exception/UnsafetyWordException"));
const NoExistWordException_1 = __importDefault(require("../exception/NoExistWordException"));
const excludeWords = [
'window',
'Window',
'alert',
'console',
'this',
'eval',
'new',
'function',
'Function',
'document',
];
const includeWords = ['return'];
const parameters = ['value', 'animations', 'styles', 'userProperty'];
class SandBox {
/**
*Creates an instance of SandBox.
* @param {{ excludeWords: string[], includeWords: string[] }} params
* @memberof SandBox
*/
constructor(params = {}) {
this.excludeWords = params.excludeWords || excludeWords;
this.includeWords = params.includeWords || includeWords;
}
verify(code) {
const newCode = code.toString();
if (this.excludeWords.some((word) => code.includes(word))) {
throw new UnsafetyWordException_1.default();
}
if (!this.includeWords.some((word) => code.includes(word))) {
throw new NoExistWordException_1.default();
}
return new Function(parameters, `"use strict"; ${newCode}`);
}
compile(code) {
try {
return this.verify(code);
}
catch (error) {
if (error.toString) {
console.error(error.toString());
}
else {
console.error(error.message);
}
}
}
}
exports.default = SandBox;