Skip to content

Commit

Permalink
Merge pull request #250 from XmiliaH/cleanup
Browse files Browse the repository at this point in the history
Added allot of jsdoc.
  • Loading branch information
patriksimek committed Mar 20, 2020
2 parents ed7215f + e8dd146 commit d71d647
Show file tree
Hide file tree
Showing 5 changed files with 1,003 additions and 423 deletions.
98 changes: 69 additions & 29 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,44 @@ export interface NodeVMOptions extends VMOptions {
/** `commonjs` (default) to wrap script into CommonJS wrapper, `none` to retrieve value returned by the script. */
wrapper?: "commonjs" | "none";
/** File extensions that the internal module resolver should accept. */
sourceExtensions?: string[]
sourceExtensions?: string[];
}

/**
* A VM with behavior more similar to running inside Node.
* VM is a simple sandbox, without `require` feature, to synchronously run an untrusted code.
* Only JavaScript built-in objects + Buffer are available. Scheduling functions
* (`setInterval`, `setTimeout` and `setImmediate`) are not available by default.
*/
export class NodeVM extends EventEmitter {
constructor(options?: NodeVMOptions);
export class VM {
constructor(options?: VMOptions);
/** Direct access to the global sandbox object */
readonly sandbox: any;
/** Timeout to use for the run methods */
timeout?: number;
/** Runs the code */
run(js: string, path: string): any;
run(js: string, path?: string): any;
/** Runs the VMScript object */
run(script: VMScript, path?: string): any;

run(script: VMScript): any;
/** Runs the code in the specific file */
runFile(filename: string): any;
/** Loads all the values into the global object with the same names */
setGlobals(values: any): this;
/** Make a object visible as a global with a specific name */
setGlobal(name: string, value: any): this;
/** Get the global object with the specific name */
getGlobal(name: string): any;
/** Freezes the object inside VM making it read-only. Not available for primitive values. */
freeze(object: any, name: string): any;
/** Protects the object inside VM making impossible to set functions as it's properties. Not available for primitive values. */
protect(object: any, name: string): any;
freeze(object: any, name?: string): any;
/** Protects the object inside VM making impossible to set functions as it's properties. Not available for primitive values */
protect(object: any, name?: string): any;
}

/**
* A VM with behavior more similar to running inside Node.
*/
export class NodeVM extends EventEmitter implements VM {
constructor(options?: NodeVMOptions);

/** Require a module in VM and return it's exports. */
require(module: string): any;

Expand All @@ -96,32 +117,36 @@ export class NodeVM extends EventEmitter {
* @param {string} [filename] File name (used in stack traces only).
* @param {Object} [options] VM options.
*/
static code(script: string, filename: string, options: NodeVMOptions): NodeVM;
static code(script: string, filename?: string, options?: NodeVMOptions): any;

/**
* Create NodeVM and run script from file inside it.
*
* @param {string} [filename] File name (used in stack traces only).
* @param {Object} [options] VM options.
*/
static file(filename: string, options: NodeVMOptions): NodeVM
}
static file(filename: string, options?: NodeVMOptions): any;

/**
* VM is a simple sandbox, without `require` feature, to synchronously run an untrusted code.
* Only JavaScript built-in objects + Buffer are available. Scheduling functions
* (`setInterval`, `setTimeout` and `setImmediate`) are not available by default.
*/
export class VM {
constructor(options?: VMOptions);
/** Direct access to the global sandbox object */
readonly sandbox: any;
/** Only here because of implements VM. Does nothing. */
timeout?: number;
/** Runs the code */
run(js: string): any;
run(js: string, path?: string): any;
/** Runs the VMScript object */
run(script: VMScript): any;
/** Runs the code in the specific file */
runFile(filename: string): any;
/** Loads all the values into the global object with the same names */
setGlobals(values: any): this;
/** Make a object visible as a global with a specific name */
setGlobal(name: string, value: any): this;
/** Get the global object with the specific name */
getGlobal(name: string): any;
/** Freezes the object inside VM making it read-only. Not available for primitive values. */
freeze(object: any, name: string): any;
freeze(object: any, name?: string): any;
/** Protects the object inside VM making impossible to set functions as it's properties. Not available for primitive values */
protect(object: any, name: string): any;
protect(object: any, name?: string): any;
}

/**
Expand All @@ -130,14 +155,29 @@ export class VM {
* to any VM (context); rather, it is bound before each run, just for that run.
*/
export class VMScript {
constructor(code: string, path?: string, options?: {
lineOffset: number;
columnOffset: number;
constructor(code: string, path: string, options?: {
lineOffset?: number;
columnOffset?: number;
compiler?: "javascript" | "coffeescript" | CompilerFunction;
});
constructor(code: string, options?: {
filename?: string,
lineOffset?: number;
columnOffset?: number;
compiler?: "javascript" | "coffeescript" | CompilerFunction;
});
/** Wraps the code */
wrap(prefix: string, postfix: string): VMScript;
readonly code: string;
readonly filename: string;
readonly lineOffset: number;
readonly columnOffset: number;
readonly compiler: "javascript" | "coffeescript" | CompilerFunction;
/**
* Wraps the code
* @deprecated
*/
wrap(prefix: string, postfix: string): this;
/** Compiles the code. If called multiple times, the code is only compiled once. */
compile(): any;
compile(): this;
}

/** Custom Error class */
Expand Down
31 changes: 23 additions & 8 deletions lib/contextify.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,9 +544,9 @@ Decontextify.value = (value, traps, deepTraps, flags, mock) => {
case 'object':
if (value === null) {
return null;
} else if (instanceOf(value, Number)) { return host.Number(value);
} else if (instanceOf(value, String)) { return host.String(value);
} else if (instanceOf(value, Boolean)) { return host.Boolean(value);
} else if (instanceOf(value, Number)) { return Decontextify.instance(value, host.Number, deepTraps, flags, 'Number');
} else if (instanceOf(value, String)) { return Decontextify.instance(value, host.String, deepTraps, flags, 'String');
} else if (instanceOf(value, Boolean)) { return Decontextify.instance(value, host.Boolean, deepTraps, flags, 'Boolean');
} else if (instanceOf(value, Date)) { return Decontextify.instance(value, host.Date, deepTraps, flags, 'Date');
} else if (instanceOf(value, RangeError)) { return Decontextify.instance(value, host.RangeError, deepTraps, flags, 'Error');
} else if (instanceOf(value, ReferenceError)) { return Decontextify.instance(value, host.ReferenceError, deepTraps, flags, 'Error');
Expand Down Expand Up @@ -871,9 +871,9 @@ Contextify.value = (value, traps, deepTraps, flags, mock) => {
case 'object':
if (value === null) {
return null;
} else if (instanceOf(value, host.Number)) { return host.Number(value);
} else if (instanceOf(value, host.String)) { return host.String(value);
} else if (instanceOf(value, host.Boolean)) { return host.Boolean(value);
} else if (instanceOf(value, host.Number)) { return Contextify.instance(value, Number, deepTraps, flags, 'Number');
} else if (instanceOf(value, host.String)) { return Contextify.instance(value, String, deepTraps, flags, 'String');
} else if (instanceOf(value, host.Boolean)) { return Contextify.instance(value, Boolean, deepTraps, flags, 'Boolean');
} else if (instanceOf(value, host.Date)) { return Contextify.instance(value, Date, deepTraps, flags, 'Date');
} else if (instanceOf(value, host.RangeError)) { return Contextify.instance(value, RangeError, deepTraps, flags, 'Error');
} else if (instanceOf(value, host.ReferenceError)) { return Contextify.instance(value, ReferenceError, deepTraps, flags, 'Error');
Expand Down Expand Up @@ -910,8 +910,21 @@ Contextify.value = (value, traps, deepTraps, flags, mock) => {
return null;
}
};
Contextify.globalValue = (value, name) => {
return (global[name] = Contextify.value(value));
Contextify.setGlobal = (name, value) => {
const prop = Contextify.value(name);
try {
global[prop] = Contextify.value(value);
} catch (e) {
throw Decontextify.value(e);
}
};
Contextify.getGlobal = (name) => {
const prop = Contextify.value(name);
try {
return Decontextify.value(global[prop]);
} catch (e) {
throw Decontextify.value(e);
}
};
Contextify.readonly = (value, mock) => {
return Contextify.value(value, null, FROZEN_TRAPS, null, mock);
Expand All @@ -923,6 +936,7 @@ Contextify.connect = (outer, inner) => {
Decontextified.set(outer, inner);
Contextified.set(inner, outer);
};
Contextify.makeModule = ()=>({exports: {}});

const BufferMock = host.Object.create(null);
BufferMock.allocUnsafe = function allocUnsafe(size) {
Expand All @@ -949,5 +963,6 @@ const exportsMap = host.Object.create(null);
exportsMap.Contextify = Contextify;
exportsMap.Decontextify = Decontextify;
exportsMap.Buffer = LocalBuffer;
exportsMap.sandbox = Decontextify.value(global);

return exportsMap;
Loading

0 comments on commit d71d647

Please sign in to comment.