Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bonniernews/wichita
Run your es6 modules in a sandbox with the experimental vm.SourceTextModule
https://github.com/bonniernews/wichita
nodejs sourcetextmodule vm
Last synced: about 8 hours ago
JSON representation
Run your es6 modules in a sandbox with the experimental vm.SourceTextModule
- Host: GitHub
- URL: https://github.com/bonniernews/wichita
- Owner: BonnierNews
- License: mit
- Created: 2019-01-16T16:29:43.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2022-09-06T13:39:02.000Z (about 2 years ago)
- Last Synced: 2024-11-08T13:21:47.550Z (10 days ago)
- Topics: nodejs, sourcetextmodule, vm
- Language: JavaScript
- Homepage:
- Size: 103 KB
- Stars: 3
- Watchers: 59
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
Wichita - Tallahassee sidekick
==============================[![Run tests](https://github.com/BonnierNews/wichita/actions/workflows/run-tests.yml/badge.svg?branch=master)](https://github.com/BonnierNews/wichita/actions/workflows/run-tests.yml)
Run your es6 modules in a sandbox with the experimental `vm.SourceTextModule`.
The following node options are required to run this module
> --experimental-vm-modules --no-warningsIf running tests with mocha you have a couple of alternatives:
```js
// .mocharc.js
module.exports = {
"node-options": ["experimental-vm-modules", "no-warnings"],
}
``````bash
NODE_OPTIONS="--experimental-vm-modules --no-warnings" mocha -R dot
```# Api
Wichita takes one required argument:
- `sourcePath`: required script path, relative from calling file
- `options`: optional vm context options, passed to `vm.createContext`
- `moduleRoute`: route that will be used when importing modules (optional)
- `fileCache`: optional Map, file content cacheand returns an api:
- `path`: absolute path to file
- `caller`: absolute path to calling file
- `run(sandbox)`: run es6 module
- `sandbox`: required object that will be contextified and used as global context
- `exports(sandbox)`: expose module export functions
- `sandbox`: required object
- `execute(sandbox, fn)`: execute function
- `sandbox`: required object
- `fn`: function that returns module as argument, `fn(es6module)`Run script:
```js
const source = new Script("./resources/main");
await source.run({
setTimeout() {},
console,
window: {},
})
```Exports:
```js
const source = new Script("./resources/lib/module");
const {default: defaultExport, justReturn} = await source.exports({
setTimeout() {},
console,
window: {},
}defaultExport(1);
justReturn(2);
```Execute:
```js
const source = new Script("./resources/lib/module");
await source.execute({
setTimeout() {},
console,
window: {},
}, (module) => {
module.default(1);
module.justReturn(2);
})
```### Example
```js
"use strict";const Script = require("@bonniernews/wichita");
const assert = require("assert");describe("script", () => {
it("executes scripts in passed context", async () => {
const source = new Script("./resources/main");const context = {
window: {
root: true,
},
};
await source.run(context);assert.ok(context.window.broker);
assert.ok(context.window.setByModule);
assert.equal(context.window.count, 1);
assert.ok(context.window.setByQueue);
});it("and again", async () => {
const source = new Script("./resources/main");const context = {
window: {
root: true,
count: 2,
},
};
await source.run(context);assert.ok(context.window.broker);
assert.ok(context.window.setByModule);
assert.equal(context.window.count, 3);
assert.ok(context.window.setByQueue);
});it("get module exports", async () => {
const source = new Script("./resources/lib/module");const context = {
window: {
root: true,
},
console,
};const {justReturn} = await source.exports(context);
assert.equal(justReturn(1), 1);
});it("execute module function", async () => {
const source = new Script("./resources/lib/module");const context = {
window: {
root: true,
},
console,
};let called;
await source.execute(context, (module) => {
called = true;
assert.equal(module.justReturn(1), 1);
});assert.ok(called);
});
});
```# Imports
JSON file import are imported as default:
```js
import data from "./resources/assets/data.json";
````is exported as:
```js
export default { content_of_data_json: true };
````