Comparing version 1.3.0 to 1.4.0
'use strict'; | ||
module.exports = require('./lib/script'); | ||
module.exports.require = require('./lib/reader'); | ||
module.exports.from = require('./lib/reader'); |
@@ -25,3 +25,3 @@ 'use strict'; | ||
const path = join(dir, file.name); | ||
if (!checkAccess(path, options.access, 'internal')) return acc; | ||
if (!checkAccess(path, options.access, 'reader')) return acc; | ||
if (file.isDirectory() && !deep) return acc; | ||
@@ -28,0 +28,0 @@ return acc.push(loader(file, path)), acc; |
@@ -15,3 +15,3 @@ 'use strict'; | ||
const name = !npm ? resolve(dir, module) : module; | ||
const lib = checkAccess(name, access, 'sandbox'); | ||
const lib = checkAccess(name, access, 'realm'); | ||
@@ -18,0 +18,0 @@ if (lib instanceof Object) return lib; |
@@ -29,2 +29,3 @@ 'use strict'; | ||
if (!ctx) return Script.sandbox.EMPTY; | ||
if (isContext(ctx)) return ctx; | ||
const options = { ...Script.sandbox.OPTIONS, preventEscape: mode ? 'afterEvaluate' : '' }; | ||
@@ -31,0 +32,0 @@ return createContext(ctx, options); |
@@ -16,5 +16,5 @@ 'use strict'; | ||
if (typeof access === 'function') return access(module, type); | ||
return type !== 'sandbox'; | ||
return type !== 'realm'; //? By default reader have full access and realm no access | ||
}; | ||
module.exports = { exec, wrap, checkAccess }; |
{ | ||
"license": "MIT", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"type": "commonjs", | ||
@@ -21,2 +21,4 @@ "name": "isolation", | ||
"isolation", | ||
"realm", | ||
"pollution-control", | ||
"javascript", | ||
@@ -31,3 +33,3 @@ "script-loader", | ||
"readmeFilename": "README.md", | ||
"engines": { "node": "18 || 19 || 20" }, | ||
"engines": { "node": ">= 18" }, | ||
"browser": {}, | ||
@@ -34,0 +36,0 @@ "files": ["/lib", "/types"], |
@@ -143,6 +143,6 @@ <h1 align="center">Isolation</h1> | ||
```javascript | ||
const { require: read } = require('isolation'); | ||
read('./path/to/script.js').then(console.log); // Output: result of script execution | ||
read('./path/to').then(console.log); // Output: { script: any } | ||
read('./path/to', { prepare: true }).then(console.log); // Output: { script: Script {} } | ||
const Realm = require('isolation'); | ||
Realm.from('./path/to/script.js').then(console.log); // Output: result of script execution | ||
Realm.from('./path/to').then(console.log); // Output: { script: any } | ||
Realm.from('./path/to', { prepare: true }).then(console.log); // Output: { script: Script {} } | ||
``` | ||
@@ -153,4 +153,4 @@ | ||
```js | ||
const { require: read } = require('isolation'); | ||
read('./path/to', {}, false); | ||
const Realm = require('isolation'); | ||
Realm.from('./path/to', {}, false); | ||
``` | ||
@@ -161,5 +161,5 @@ | ||
```javascript | ||
const { require: read } = require('isolation'); | ||
read.script('./path/to/script.js').then(console.log); // Output: result of script execution | ||
read.script('./path/to/script.js', { prepare: true }).then(console.log); // Output: Script {} | ||
const Realm = require('isolation'); | ||
Realm.from.script('./path/to/script.js').then(console.log); // Output: result of script execution | ||
Realm.from.script('./path/to/script.js', { prepare: true }).then(console.log); // Output: Script {} | ||
``` | ||
@@ -170,6 +170,6 @@ | ||
```javascript | ||
const { require: read } = require('isolation'); | ||
read.script('./path/to').then(console.log); // Output: { script: any, deep: { script: any } } | ||
read.script('./path/to', { prepare: true }).then(console.log); Output: { script: Script {} } | ||
read.script('./path/to', {}, false).then(console.log); // Output: { script: any } | ||
const Realm = require('isolation'); | ||
Realm.from.script('./path/to').then(console.log); // Output: { script: any, deep: { script: any } } | ||
Realm.from.script('./path/to', { prepare: true }).then(console.log); Output: { script: Script {} } | ||
Realm.from.script('./path/to', {}, false).then(console.log); // Output: { script: any } | ||
``` | ||
@@ -193,3 +193,3 @@ | ||
```js | ||
const { execute: exec } = require('isolation'); | ||
const Realm = require('isolation'); | ||
const src = ` | ||
@@ -199,3 +199,3 @@ const fs = require('fs'); | ||
`; | ||
const result = exec(src, { | ||
const result = Realm.execute(src, { | ||
access: { | ||
@@ -202,0 +202,0 @@ sandbox: module => ({ fs: { readFile: (filename) => filename + ' Works !' } })[module]; |
/** | ||
* @example <caption>Sandbox usage example</caption> | ||
* const ctx = Astroctx.sandbox({ console, a: 1000, b: 10 }); | ||
* const prepared = Astroctx.prepare(`a - b`); | ||
* prepared.execute(ctx); // Output: 990 | ||
* prepared.execute({ ...ctx, b: 7 })); // Output: 993 | ||
* const realm = new Realm(`a - b`); | ||
* realm.execute({ a: 1000, b: 10 }); // Output: 990 | ||
* realm.execute({ a: 1000, b: 20 }); // Output: 980 | ||
*/ | ||
@@ -36,5 +35,5 @@ export type TSandbox = { | ||
* @example <caption>You can create custom context</caption> | ||
* const ctx = Astroctx.sandbox({ console, a: 1000, b: 10 }); | ||
* const ctx = Realm.sandbox({ console, a: 1000, b: 10 }); | ||
**/ | ||
(ctx?: Context | Object, preventEscape?: boolean): Context; | ||
}; |
@@ -15,18 +15,4 @@ import type { Context, Script, ScriptOptions, BaseOptions } from 'node:vm'; | ||
/** | ||
* Astroctx - VM Container for Commonjs | ||
* @example <caption>Basics</caption> | ||
* const Astroctx = require('astroctx'); | ||
* console.log(new Astroctx(`({ field: 'value' });`).execute()); // Output: { field: 'value' } | ||
* console.log(Astroctx.execute(`(a, b) => a + b;`)(2 + 2)); // Output: 4 | ||
* Astroctx.execute(`async (a, b) => a + b;`)(2 + 2).then(console.log); // Output: 4 | ||
* @example <caption>CTX & Delay execution example</caption> | ||
* const ctx = Astroctx.sandbox({ console, a: 1000, b: 10 }); | ||
* const prepared = Astroctx.prepare(`a - b`, { ctx }); | ||
* prepared.execute(); // Output: 990 | ||
* prepared.execute(Astroctx.sandbox({ ...ctx, b: 7 })); // Output: 993 | ||
* @example <caption>Read Api</caption> | ||
* Astroctx.require('./path/to/script.js').then(console.log); // Output: result of script execution | ||
* Astroctx.require('./path/to').then(console.log); // Output: { script: any } | ||
* Astroctx.require('./path/to', { prepare: true }).then(console.log); // Output: { script: Script {} } | ||
* Astroctx.require('./path/to', { deep: true }).then(console.log); // Output: { script: any, deep: { script: any } } | ||
* Isolation | ||
* @description Isolate your code in custom realms / contexts | ||
*/ | ||
@@ -43,8 +29,39 @@ export = class Script { | ||
static require: TRead; | ||
/** | ||
* @example <caption>Read Api</caption> | ||
* Realm.from('./path/to/script.js').then(console.log); // Output: result of script execution | ||
* Realm.from('./path/to').then(console.log); // Output: { script: any } | ||
* Realm.from('./path/to', { prepare: true }).then(console.log); // Output: { script: Script {} } | ||
* Realm.from('./path/to', { deep: true }).then(console.log); // Output: { script: any, deep: { script: any } } | ||
*/ | ||
static from: TRead; | ||
/** | ||
* @example <caption>Functional initialization</caption> | ||
* const Realm = require('isolation'); | ||
* console.log(Realm.from(`({ field: 'value' });`).execute()); // Output: { field: 'value' } | ||
*/ | ||
static prepare: (src: string, options?: TOptions) => Script; | ||
/** | ||
* @example <caption>Skip init process</caption> | ||
* console.log(Realm.execute(`(a, b) => a + b;`)(2 + 2)); // Output: 4 | ||
* Realm.execute(`async (a, b) => a + b;`)(2 + 2).then(console.log); // Output: 4 | ||
*/ | ||
static execute: (src: string, options?: TOptions) => unknown; | ||
static require: (path: string, options?: TOptionsReader) => Promise<unknown | Script>; | ||
/** | ||
* @example <caption>Custom sandboxes</caption> | ||
* const ctx = { a: 1000, b: 10 } | ||
* const realm = new Realm(`a - b`, { ctx }); | ||
* realm.execute(); // Output: 990 | ||
* realm.execute({ ...ctx, b: 7 }); // Output: 993 | ||
*/ | ||
static sandbox: TSandbox; | ||
/** | ||
* @example <caption>Constructor initialization</caption> | ||
* const Realm = require('isolation'); | ||
* console.log(new Realm(`({ field: 'value' });`).execute()); // Output: { field: 'value' } | ||
*/ | ||
constructor(src: string, options?: TOptions): Script; | ||
@@ -51,0 +68,0 @@ |
@@ -1,4 +0,15 @@ | ||
type TAccess = (path, type?: string) => boolean | object; | ||
type TAccess = (path: string, type?: 'reader' | 'realm') => boolean | object; | ||
type TSpecific = <RES>(path: string) => RES; | ||
export interface TOptions extends BaseOptions { | ||
/** | ||
* @example | ||
* ({ | ||
* dir: '/tests', //? __dirname variable, internal require startpoint | ||
* filename: 'index.js', //? __filename variable | ||
* npmIsolation: true, //? Intenal dependencies will be loaded with isolation, default is false | ||
* ctx: { console, A: 5, B: 'Hello world' }, //? Inject global variables, default {} | ||
* access: (name, type) => true, //? Controll access to Realm submodules or reader API | ||
* }) | ||
*/ | ||
export interface TOptions { | ||
dir?: string; | ||
@@ -8,3 +19,3 @@ filename?: string; | ||
access?: { sandbox?: TAccess; internal?: TAccess } | TAccess; | ||
access?: { sandbox?: TSpecific<boolean | object>; internal?: TSpecific<boolean> } | TAccess; | ||
ctx?: Context | { [key: string]: unknown }; | ||
@@ -11,0 +22,0 @@ |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
21133
262
1