@sudoo/marked
Version:
JavaScript & TypeScript code runner in JavaScript, safe with marked territory, asynchronous
53 lines (52 loc) • 2.58 kB
JavaScript
;
/**
* @author WMXPY
* @namespace Marked_Static
* @description Run Script
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.markedRunScriptStaticMethod = void 0;
const error_code_1 = require("../../declare/error-code");
const error_1 = require("../../util/error/error");
const sandbox_1 = require("../sandbox");
const declare_1 = require("./declare");
const markedRunScriptStaticMethod = (script, options) => __awaiter(void 0, void 0, void 0, function* () {
if (!script) {
throw (0, error_1.error)(error_code_1.ERROR_CODE.SCRIPT_CANNOT_BE_NULL_OR_UNDEFINED);
}
const fixedOptions = Object.assign(Object.assign({}, declare_1.defaultMarkedRunScriptOptions), options);
const sandbox = sandbox_1.Sandbox.fromAllEvaluators(fixedOptions.language);
if (typeof fixedOptions.mixins !== "undefined") {
for (const mixin of fixedOptions.mixins) {
sandbox.use(mixin);
}
}
if (typeof fixedOptions.injects !== "undefined") {
Object.keys(fixedOptions.injects).forEach((key) => sandbox.inject(key, fixedOptions.injects[key]));
}
if (typeof fixedOptions.provides !== "undefined") {
Object.keys(fixedOptions.provides).forEach((key) => sandbox.provide(key, fixedOptions.provides[key]));
}
if (typeof fixedOptions.resolvers !== "undefined") {
for (const resolver of fixedOptions.resolvers) {
sandbox.resolver(resolver);
}
}
if (typeof fixedOptions.sandbox !== "undefined") {
Object.keys(fixedOptions.sandbox).forEach((key) => sandbox.setOption(key, fixedOptions.sandbox[key]));
}
if (typeof fixedOptions.debugInterceptor !== "undefined") {
sandbox.setDebugInterceptor(fixedOptions.debugInterceptor);
}
return yield sandbox.evaluate(script);
});
exports.markedRunScriptStaticMethod = markedRunScriptStaticMethod;