@miniflare/runner-vm
Advanced tools
Comparing version 2.0.0-rc.1 to 2.0.0-rc.2
@@ -12,3 +12,3 @@ /// <reference types="node" /> | ||
export declare const proxiedGlobals: { | ||
export declare function makeProxiedGlobals(blockCodeGeneration?: boolean): { | ||
Object: ObjectConstructor; | ||
@@ -30,3 +30,4 @@ Array: ArrayConstructor; | ||
private context?; | ||
constructor(context?: vm.Context | undefined); | ||
private blockCodeGeneration; | ||
constructor(context?: vm.Context | undefined, blockCodeGeneration?: boolean); | ||
private runAsScript; | ||
@@ -33,0 +34,0 @@ private runAsModule; |
@@ -18,2 +18,3 @@ // packages/runner-vm/src/index.ts | ||
} from "@miniflare/shared"; | ||
var SUGGEST_BUNDLE = "If you're trying to import an npm package, you'll need to bundle your Worker first."; | ||
var ModuleLinker = class { | ||
@@ -60,3 +61,4 @@ constructor(moduleRules, additionalModules) { | ||
if (rule === void 0) { | ||
throw new VMScriptRunnerError("ERR_MODULE_RULE", `${errorBase}: no matching module rules`); | ||
throw new VMScriptRunnerError("ERR_MODULE_RULE", `${errorBase}: no matching module rules. | ||
${SUGGEST_BUNDLE}`); | ||
} | ||
@@ -110,3 +112,4 @@ const data = await fs.readFile(identifier); | ||
if (rule === void 0) { | ||
throw new VMScriptRunnerError("ERR_MODULE_RULE", `${errorBase}: no matching module rules`); | ||
throw new VMScriptRunnerError("ERR_MODULE_RULE", `${errorBase}: no matching module rules. | ||
${SUGGEST_BUNDLE}`); | ||
} | ||
@@ -188,25 +191,30 @@ this.#cjsModuleCache.set(identifier, module); | ||
} | ||
var proxiedGlobals = { | ||
Object: proxyHasInstance(Object, isObject), | ||
Array: proxyHasInstance(Array, Array.isArray), | ||
Promise: proxyHasInstance(Promise, types.isPromise), | ||
RegExp: proxyHasInstance(RegExp, types.isRegExp), | ||
Error: proxyHasInstance(Error, isError(Error)), | ||
EvalError: proxyHasInstance(EvalError, isError(EvalError)), | ||
RangeError: proxyHasInstance(RangeError, isError(RangeError)), | ||
ReferenceError: proxyHasInstance(ReferenceError, isError(ReferenceError)), | ||
SyntaxError: proxyHasInstance(SyntaxError, isError(SyntaxError)), | ||
TypeError: proxyHasInstance(TypeError, isError(TypeError)), | ||
URIError: proxyHasInstance(URIError, isError(URIError)), | ||
Function: proxyHasInstance(Function, isFunction, { | ||
construct() { | ||
throw new EvalError("Code generation from strings disallowed for this context"); | ||
} | ||
}) | ||
}; | ||
function makeProxiedGlobals(blockCodeGeneration) { | ||
if (!blockCodeGeneration) | ||
blockCodeGeneration = void 0; | ||
return { | ||
Object: proxyHasInstance(Object, isObject), | ||
Array: proxyHasInstance(Array, Array.isArray), | ||
Promise: proxyHasInstance(Promise, types.isPromise), | ||
RegExp: proxyHasInstance(RegExp, types.isRegExp), | ||
Error: proxyHasInstance(Error, isError(Error)), | ||
EvalError: proxyHasInstance(EvalError, isError(EvalError)), | ||
RangeError: proxyHasInstance(RangeError, isError(RangeError)), | ||
ReferenceError: proxyHasInstance(ReferenceError, isError(ReferenceError)), | ||
SyntaxError: proxyHasInstance(SyntaxError, isError(SyntaxError)), | ||
TypeError: proxyHasInstance(TypeError, isError(TypeError)), | ||
URIError: proxyHasInstance(URIError, isError(URIError)), | ||
Function: proxyHasInstance(Function, isFunction, blockCodeGeneration && { | ||
construct() { | ||
throw new EvalError("Code generation from strings disallowed for this context"); | ||
} | ||
}) | ||
}; | ||
} | ||
// packages/runner-vm/src/index.ts | ||
var VMScriptRunner = class { | ||
constructor(context) { | ||
constructor(context, blockCodeGeneration = true) { | ||
this.context = context; | ||
this.blockCodeGeneration = blockCodeGeneration; | ||
} | ||
@@ -233,3 +241,3 @@ runAsScript(context, blueprint) { | ||
const linker = modulesRules && new ModuleLinker(modulesRules, additionalModules ?? {}); | ||
Object.assign(globalScope, proxiedGlobals); | ||
Object.assign(globalScope, makeProxiedGlobals(this.blockCodeGeneration)); | ||
let context = this.context; | ||
@@ -239,4 +247,5 @@ if (context) { | ||
} else { | ||
const allow = !this.blockCodeGeneration; | ||
context = vm2.createContext(globalScope, { | ||
codeGeneration: { strings: false, wasm: false } | ||
codeGeneration: { strings: allow, wasm: allow } | ||
}); | ||
@@ -261,4 +270,4 @@ } | ||
VMScriptRunnerError, | ||
proxiedGlobals | ||
makeProxiedGlobals | ||
}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@miniflare/runner-vm", | ||
"version": "2.0.0-rc.1", | ||
"version": "2.0.0-rc.2", | ||
"description": "VM script runner module for Miniflare: a fun, full-featured, fully-local simulator for Cloudflare Workers", | ||
@@ -39,7 +39,7 @@ "keywords": [ | ||
"dependencies": { | ||
"@miniflare/shared": "2.0.0-rc.1" | ||
"@miniflare/shared": "2.0.0-rc.2" | ||
}, | ||
"devDependencies": { | ||
"@miniflare/shared-test": "2.0.0-rc.1" | ||
"@miniflare/shared-test": "2.0.0-rc.2" | ||
} | ||
} |
@@ -5,2 +5,38 @@ # `@miniflare/runner-vm` | ||
[Miniflare](https://github.com/cloudflare/miniflare): a fun, full-featured, | ||
fully-local simulator for Cloudflare Workers | ||
fully-local simulator for Cloudflare Workers. | ||
## Example | ||
```js | ||
import { VMScriptRunner } from "@miniflare/runner-vm"; | ||
const runner = new VMScriptRunner(); | ||
// Pass `console` into sandbox | ||
const globalScope = { console }; | ||
// Run regular script | ||
const blueprint1 = { | ||
code: 'console.log("hello")', | ||
filePath: "test.js", | ||
}; | ||
await runner.run(globalScope, blueprint1); // hello | ||
// Run module script | ||
const blueprint2 = { | ||
code: 'import thing from "./thing.js"; console.log(thing);', | ||
filePath: "test.mjs", | ||
}; | ||
const moduleRules = [{ type: "ESModule", include: /\.js$/ }]; | ||
// Assuming thing.js contains `"export default "thing";`... | ||
await runner.run(globalScope, blueprint2, moduleRules); // thing | ||
// Run module script with additional module | ||
const blueprint3 = { | ||
code: `import additional from "__ADDITIONAL"; console.log(additional);`, | ||
filePath: "test.mjs", | ||
}; | ||
const modules = { | ||
__ADDITIONAL: { default: "stuff" }, | ||
}; | ||
await runner.run(globalScope, blueprint3, moduleRules, modules); // stuff | ||
``` |
Sorry, the diff of this file is not supported yet
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
18739
297
42
+ Added@miniflare/shared@2.0.0-rc.2(transitive)
- Removed@miniflare/shared@2.0.0-rc.1(transitive)
Updated@miniflare/shared@2.0.0-rc.2