@bonniernews/wichita
Advanced tools
Comparing version 0.1.0 to 0.2.0
50
index.js
@@ -6,5 +6,5 @@ "use strict"; | ||
const {name, version} = require("./package.json"); | ||
const {dirname, extname, join, resolve: resolvePath} = require("path"); | ||
const {dirname, extname, join, resolve: resolvePath, isAbsolute} = require("path"); | ||
module.exports = function Scripts(sourcePath) { | ||
module.exports = function Scripts(sourcePath, options) { | ||
if (!("SourceTextModule" in vm)) throw new Error("No SourceTextModule in vm, try using node --experimental-vm-modules flag"); | ||
@@ -18,5 +18,15 @@ | ||
calledFrom, | ||
run(browser) { | ||
return runScripts(browser, fullPath); | ||
run(globalContext) { | ||
return runScripts(globalContext, fullPath, options); | ||
}, | ||
execute(globalContext, testFn) { | ||
return runScripts(globalContext, fullPath, {...options, initializeImportMeta}, ` | ||
import * as Module from "${fullPath}"; | ||
import.meta.export(Module) | ||
`); | ||
function initializeImportMeta(meta) { | ||
meta.export = testFn; | ||
} | ||
}, | ||
}; | ||
@@ -26,2 +36,3 @@ }; | ||
function getFullPath(sourcePath, calledFrom) { | ||
if (isAbsolute(sourcePath)) return sourcePath; | ||
const isRelativePath = /^\.{1,2}[/\\]?/.test(sourcePath); | ||
@@ -55,16 +66,35 @@ const resolvedPath = !isRelativePath && getModulePath(sourcePath); | ||
async function runScripts(globalContext, mainPath, options = {}) { | ||
async function runScripts(globalContext, mainPath, options = {}, testScript) { | ||
const cache = {}; | ||
const {initializeImportMeta} = options; | ||
const vmContext = vm.createContext(globalContext, { | ||
name: `${name} v${version}`, | ||
...options | ||
...options, | ||
}); | ||
const main = await loadScript(mainPath, vmContext); | ||
let mainModule; | ||
if (testScript) { | ||
mainModule = new vm.SourceTextModule(testScript, { | ||
url: `file://${mainPath}`, | ||
context: vmContext, | ||
initializeImportMeta, | ||
}); | ||
await main.instantiate(); | ||
await mainModule.link(linker); | ||
return main.evaluate(); | ||
} else { | ||
mainModule = await loadScript(mainPath, vmContext); | ||
} | ||
await mainModule.instantiate(); | ||
return mainModule.evaluate().then((result) => { | ||
return { | ||
...result, | ||
module: mainModule, | ||
context: vmContext, | ||
}; | ||
}); | ||
async function loadScript(scriptPath, context) { | ||
@@ -76,2 +106,3 @@ const scriptSource = await readScript(scriptPath); | ||
context, | ||
initializeImportMeta | ||
}); | ||
@@ -88,2 +119,3 @@ | ||
if (cache[scriptPath]) { | ||
@@ -90,0 +122,0 @@ return cache[scriptPath]; |
{ | ||
"name": "@bonniernews/wichita", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Run your es6 modules with imports in a vm sandbox", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -27,3 +27,7 @@ Wichita - Tallahassee sidekick | ||
- `globalContext`: required object that will be converted into a sandbox | ||
- `execute(globalContext, callback)`: execute module function | ||
- `globalContext`: required object that will be converted into a sandbox | ||
- `callback`: function that returns module as argument, `callback(es6module)` | ||
Run script: | ||
```js | ||
@@ -38,2 +42,15 @@ const source = Script("./resources/main"); | ||
Execute module: | ||
```js | ||
const source = Script("./resources/lib/module"); | ||
source.execute({ | ||
setTimeout() {}, | ||
console, | ||
window: {}, | ||
}, (module) => { | ||
module.default(1); | ||
module.justReturn(2); | ||
}) | ||
``` | ||
### Example | ||
@@ -79,3 +96,23 @@ | ||
}); | ||
it("executes module function", async () => { | ||
const source = 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); | ||
}); | ||
}); | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8448
114
116