Skip to content

Commit

Permalink
Merge pull request #3 from astrohelm/dev
Browse files Browse the repository at this point in the history
Astroctx V1.2.0
  • Loading branch information
shuritch authored Oct 25, 2023
2 parents 17f7751 + 2e8e94b commit c60c169
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 43 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## [Unreleased][unreleased]

## [1.2.0][] - 2023-10-25

- Fix type definitions
- New default global variables
- Tests

## [1.1.0][] - 2023-10-01

### Major updates
Expand Down
17 changes: 11 additions & 6 deletions lib/ctx.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

const { createContext } = require('node:vm');

const NODE = { global, console, process };
const TIME = { setTimeout, setImmediate, setInterval, clearTimeout, clearImmediate, clearInterval };
const BUFF = { Buffer, TextDecoder, TextEncoder };
const URI = { URL, URLSearchParams };
const EVENT = { Event, EventTarget, queueMicrotask };
const MESSAGE = { MessageChannel, MessageEvent, MessagePort };
const DEFAULT = { AbortController, ...EVENT, ...BUFF, ...URI, ...TIME, ...MESSAGE };
const MSG = { MessageChannel, MessageEvent, MessagePort, BroadcastChannel };
const CRUD = { fetch, FormData, Response, Request, Headers, Buffer, Blob };
const TXT = { TextDecoder, TextEncoder, TextDecoderStream, TextEncoderStream };
const TIME = { setTimeout, setImmediate, setInterval, clearTimeout, clearImmediate, clearInterval };
const EVT = { AbortController, AbortSignal, Event, EventTarget };
const STRAT = { ByteLengthQueuingStrategy, CountQueuingStrategy };
const STREAM = { CompressionStream, DecompressionStream };
const OTHER = { queueMicrotask, WebAssembly };

const NODE = { global, console, process };
const DEFAULT = { ...OTHER, ...STRAT, ...STREAM, ...URI, ...MSG, ...TXT, ...CRUD, ...TIME, ...EVT };

const CTX_OPTS = { codeGeneration: { strings: false, wasm: false } };

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"license": "MIT",
"version": "1.1.0",
"version": "1.2.0",
"type": "commonjs",
"name": "astroctx",
"homepage": "https://astrohelm.ru",
Expand Down
22 changes: 11 additions & 11 deletions tests/core/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const Script = require('../..');
const { sandbox, require: read } = Script;
const target = name => path.join(__dirname, name);

test('[Core] Script executor', async () => {
test('[CORE] Script executor', async () => {
const script = new Script(`module.exports = ({field: 'value'});`);
assert.strictEqual(script.name, 'Astro');
assert.strictEqual(script.dir, process.cwd());
Expand All @@ -16,7 +16,7 @@ test('[Core] Script executor', async () => {
assert.strictEqual(result.field, 'value');
});

test('[Core] Script executor extended', async () => {
test('[CORE] Script executor extended', async () => {
const script = Script.prepare(`module.exports = a + b`);
assert.strictEqual(script.name, 'Astro');
assert.strictEqual(script.dir, process.cwd());
Expand All @@ -25,7 +25,7 @@ test('[Core] Script executor extended', async () => {
assert.strictEqual(script.execute({ a: 2, b: 3 }), 5);
});

test('[Core] Npm isolation check', async () => {
test('[CORE] Npm isolation check', async () => {
const access = () => true;
const script = Script.prepare(`module.exports = require('chalk')`, {
access,
Expand All @@ -35,7 +35,7 @@ test('[Core] Npm isolation check', async () => {
assert.strictEqual(typeof script.execute(), 'function');
});

test('[Reader] Script loader', async () => {
test('[READER] Script loader', async () => {
const simple = await read.script(target('examples/simple.js'));

assert.deepStrictEqual(Object.keys(simple), ['field', 'sub', 'add']);
Expand All @@ -44,7 +44,7 @@ test('[Reader] Script loader', async () => {
assert.strictEqual(simple.sub(2, 3), -1);
});

test('[Reader] Access control', async () => {
test('[READER] Access control', async () => {
const access = { internal: path => !path.endsWith('simple.js') && !path.endsWith('.json') };
const scripts = await read(target('examples'), { access });
const { deep } = scripts;
Expand All @@ -59,7 +59,7 @@ test('[Reader] Access control', async () => {
assert.strictEqual(arrow(-1, 1), 0);
});

test('[Reader] Folder loader', async () => {
test('[READER] Folder loader', async () => {
const access = { internal: path => !path.endsWith('.json') };
const scripts = await read(target('examples'), { access });
const { deep, simple } = scripts;
Expand All @@ -79,7 +79,7 @@ test('[Reader] Folder loader', async () => {
assert.strictEqual(arrow(-1, 1), 0);
});

test('[Reader] Universal loader', async () => {
test('[READER] Universal loader', async () => {
const access = path => !path.endsWith('.json');
const scripts = await read(target('examples'), { access });
const { deep, simple } = scripts;
Expand All @@ -99,7 +99,7 @@ test('[Reader] Universal loader', async () => {
assert.strictEqual(arrow(-1, 1), 0);
});

test('[Reader] Deep option', async () => {
test('[READER] Deep option', async () => {
const access = path => !path.endsWith('.json');
const scripts = await read(target('examples'), { access }, false);
const { simple } = scripts;
Expand All @@ -113,7 +113,7 @@ test('[Reader] Deep option', async () => {
assert.strictEqual(simple.sub(2, 3), -1);
});

test('[Reader] prepare option', async () => {
test('[READER] prepare option', async () => {
const access = path => !path.endsWith('.json');
const scripts = await read.dir(target('examples'), { prepare: true, access });
const { deep } = scripts;
Expand Down Expand Up @@ -167,7 +167,7 @@ test('[CTX] Custom', async () => {
assert.strictEqual(ctx.global, context);
});

test('[Sanbox access] Internal', async () => {
test('[SANDBOX] Internal', async () => {
try {
const result = Script.execute(`const fs = require('fs');`);
assert.strictEqual(result, undefined);
Expand All @@ -176,7 +176,7 @@ test('[Sanbox access] Internal', async () => {
}
});

test('[Sanbox access] Non-existent', async () => {
test('[SANDBOX] Non-existent', async () => {
try {
const src = `const notExist = require('nothing');`;
const result = Script.execute(src);
Expand Down
16 changes: 8 additions & 8 deletions tests/errors/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const exec = Script.execute;

const target = name => path.join(__dirname, 'examples', name);

test('[Core] Eval error ', async () => {
test('[CORE] Eval error ', async () => {
try {
exec(`module.exports = eval('100 * 2');`, { type: 'cjs' });
assert.fail(new Error('Should throw an error.'));
Expand All @@ -18,7 +18,7 @@ test('[Core] Eval error ', async () => {
}
});

test('[Core] Eval error', async () => {
test('[CORE] Eval error', async () => {
try {
exec(`eval('100 * 2')`);
assert.fail(new Error('Should throw an error.'));
Expand All @@ -27,7 +27,7 @@ test('[Core] Eval error', async () => {
}
});

test('[Core] Error.notfound.js', async () => {
test('[CORE] Error.notfound.js', async () => {
let ms;
try {
ms = await read(target('error.notfound.js'));
Expand All @@ -38,7 +38,7 @@ test('[Core] Error.notfound.js', async () => {
assert.strictEqual(ms, undefined);
});

test('[Core] Error.syntax.js', async () => {
test('[CORE] Error.syntax.js', async () => {
try {
await read(target('error.syntax'));
assert.fail(new Error('Should throw an error.'));
Expand All @@ -47,7 +47,7 @@ test('[Core] Error.syntax.js', async () => {
}
});

test('[Core] Error.reference.js', async () => {
test('[CORE] Error.reference.js', async () => {
try {
const script = await read(target('error.reference.js'));
await script();
Expand All @@ -58,7 +58,7 @@ test('[Core] Error.reference.js', async () => {
}
});

test('[Core] Call undefined as a function', async () => {
test('[CORE] Call undefined as a function', async () => {
try {
const script = await read(target('error.undef.js'));
await script();
Expand All @@ -68,7 +68,7 @@ test('[Core] Call undefined as a function', async () => {
}
});

test('[Core] Error.reference.js Error.reference.cjs (line number)', async () => {
test('[CORE] Error.reference.js Error.reference.cjs (line number)', async () => {
try {
const script = await read(target('error.reference.js'));
await script();
Expand All @@ -91,7 +91,7 @@ test('[Core] Error.reference.js Error.reference.cjs (line number)', async () =>
}
});

test('[Core] Error.empty.js', async () => {
test('[CORE] Error.empty.js', async () => {
try {
await read(target('error.empty.js'));
assert.fail(new Error('Should throw an error.'));
Expand Down
14 changes: 7 additions & 7 deletions tests/modules/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ const exec = Script.execute;

const target = name => path.join(__dirname, 'examples', name);

test('[Sanbox access] For node internal module', async () => {
test('[SANDBOX] For node internal module', async () => {
const context = {};
context.global = context;
const src = `module.exports = { fs: require('fs') };`;
const ctx = sandbox(Object.freeze(context));
exec(src, { ctx, access: module => module === 'fs', type: 'cjs' });
});

test('[Sanbox access] non-existent but granted', async () => {
test('[SANDBOX] non-existent but granted', async () => {
try {
const ms = exec(`module.exports = require('astroctx');`, {
access: module => module === 'astroctx',
Expand All @@ -28,7 +28,7 @@ test('[Sanbox access] non-existent but granted', async () => {
}
});

test('[Sanbox access] Stub', async () => {
test('[SANDBOX] Stub', async () => {
const src = `
const fs = require('fs');
module.exports = {
Expand Down Expand Up @@ -57,7 +57,7 @@ test('[Sanbox access] Stub', async () => {
assert.strictEqual(res, 'stub-content');
});

test('[Sanbox access] Nested', async () => {
test('[SANDBOX] Nested', async () => {
const context = { console };
context.global = sandbox;
const src = `module.exports = require('./module.cjs');`;
Expand All @@ -76,7 +76,7 @@ test('[Sanbox access] Nested', async () => {
assert.strictEqual(ms.nested.value, 2);
});

test('[Sanbox access] Access with reader', async () => {
test('[SANDBOX] Access with reader', async () => {
const ms = await read.script(target('module.cjs'), {
dir: path.join(__dirname, 'examples'),
access: filepath => filepath === path.join(__dirname, 'examples', 'module.nested.js'),
Expand All @@ -85,7 +85,7 @@ test('[Sanbox access] Access with reader', async () => {
assert.strictEqual(ms.nested.value, 2);
});

test('[Sanbox access] Nested not permitted', async () => {
test('[SANDBOX] Nested not permitted', async () => {
try {
const src = `module.exports = require('./module.cjs');`;
exec(src, {
Expand All @@ -98,7 +98,7 @@ test('[Sanbox access] Nested not permitted', async () => {
}
});

test('[Sandbox access] nested npm', async () => {
test('[SANDBOX] nested npm', async () => {
const src = `module.exports = require('node:test');`;
const ms = exec(src, {
access: module => module === 'node:test',
Expand Down
14 changes: 7 additions & 7 deletions tests/scripts/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { require: read, sandbox } = require('../..');

const target = name => path.join(__dirname, 'examples', name);

test('[Core] Simple.js', async () => {
test('[CORE] Simple.js', async () => {
const ms = await read.script(target('simple.js'));

assert.deepStrictEqual(Object.keys(ms), ['field', 'add', 'sub']);
Expand All @@ -16,7 +16,7 @@ test('[Core] Simple.js', async () => {
assert.strictEqual(ms.sub(2, 3), -1);
});

test('[Core] Simple (from non extension file)', async () => {
test('[CORE] Simple (from non extension file)', async () => {
const ms = await read.script(target('simple'));

assert.deepStrictEqual(Object.keys(ms), ['field', 'add', 'sub']);
Expand All @@ -25,7 +25,7 @@ test('[Core] Simple (from non extension file)', async () => {
assert.strictEqual(ms.sub(2, 3), -1);
});

test('[Core] Complex.js', async () => {
test('[CORE] Complex.js', async () => {
const ctx = sandbox({ setTimeout });
const options = { filename: 'CUSTOM FILE NAME', ctx };
const ms = await read.script(target('complex.js'), options);
Expand All @@ -38,15 +38,15 @@ test('[Core] Complex.js', async () => {
});
});

test('[Core] Function.js', async () => {
test('[CORE] Function.js', async () => {
const ms = await read.script(target('function.js'));

assert.strictEqual(typeof ms, 'function');
assert.strictEqual(ms(2, 3), 6);
assert.strictEqual(ms.bind(null, 3)(4), 12);
});

test('[Core] Arrow.js', async () => {
test('[CORE] Arrow.js', async () => {
const ms = await read.script(target('arrow.js'));

assert.strictEqual(typeof ms, 'function');
Expand All @@ -55,7 +55,7 @@ test('[Core] Arrow.js', async () => {
assert.strictEqual(ms(-1, 1), 0);
});

test('[Core] Async.js', async () => {
test('[CORE] Async.js', async () => {
const ms = await read.script(target('async.js'));
const result = await ms('str', { field: 'value' });

Expand All @@ -65,7 +65,7 @@ test('[Core] Async.js', async () => {
assert.rejects(ms('', { field: 'value' }));
});

test('[Core] Local.js', async () => {
test('[CORE] Local.js', async () => {
const ms = await read.script(target('local.js'));
const result = await ms('str');

Expand Down
2 changes: 1 addition & 1 deletion types/context.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export type TSandbox = {

/**
* @example <caption>Nodejs global variables</caption>
* const NODE = { global, console, process };
* const NODE = { global, console, process, ...COMMON };
*/
NODE: Context;

Expand Down
4 changes: 2 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type TRead = {
* 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 } }
*/
export class Script {
export = class Script {
/**
* @description Equivalent to __filename
*/
Expand All @@ -51,4 +51,4 @@ export class Script {
* @description Run prepared scripts
*/
execute: (ctx?: Context) => unknown;
}
};

0 comments on commit c60c169

Please sign in to comment.