isolation
Version:
How often do you see libraries which mutates global variables Or how often do you check libraries actions ? This library provides script isolation in custom contexts to solve this kind of issues.
24 lines (20 loc) • 717 B
JavaScript
;
const GLOBAL = require('./global');
const { createContext, isContext } = require('node:vm');
const CTX_OPTS = { codeGeneration: { strings: false, wasm: false } };
// prettier-ignore
var contextify = module.exports = (ctx, mode = false) => {
if (!ctx) return contextify.EMPTY;
if (isContext(ctx)) return ctx;
return createContext(ctx, {
...contextify.OPTIONS,
microtaskMode: mode ? 'afterEvaluate' : undefined,
});
};
const { freeze, assign } = Object;
assign(contextify, {
OPTIONS: CTX_OPTS,
EMPTY: createContext(freeze({}), CTX_OPTS),
COMMON: createContext(freeze({ ...GLOBAL }), CTX_OPTS),
NODE: createContext(freeze({ ...GLOBAL, global, console, process }), CTX_OPTS),
});