@miniflare/runner-vm
Advanced tools
Comparing version 2.0.0-next.3 to 2.0.0-rc.1
@@ -0,1 +1,4 @@ | ||
/// <reference types="node" /> | ||
import { AdditionalModules } from '@miniflare/shared'; | ||
import { Context } from '@miniflare/shared'; | ||
@@ -7,7 +10,25 @@ import { MiniflareError } from '@miniflare/shared'; | ||
import { ScriptRunnerResult } from '@miniflare/shared'; | ||
import vm from 'vm'; | ||
export declare const proxiedGlobals: { | ||
Object: ObjectConstructor; | ||
Array: ArrayConstructor; | ||
Promise: PromiseConstructor; | ||
RegExp: RegExpConstructor; | ||
Error: ErrorConstructor; | ||
EvalError: EvalErrorConstructor; | ||
RangeError: RangeErrorConstructor; | ||
ReferenceError: ReferenceErrorConstructor; | ||
SyntaxError: SyntaxErrorConstructor; | ||
TypeError: TypeErrorConstructor; | ||
URIError: URIErrorConstructor; | ||
Function: FunctionConstructor; | ||
}; | ||
export declare class VMScriptRunner implements ScriptRunner { | ||
private context?; | ||
constructor(context?: vm.Context | undefined); | ||
private runAsScript; | ||
private runAsModule; | ||
run(globalScope: Context, blueprint: ScriptBlueprint, modulesRules?: ProcessedModuleRule[]): Promise<ScriptRunnerResult>; | ||
run(globalScope: Context, blueprint: ScriptBlueprint, modulesRules?: ProcessedModuleRule[], additionalModules?: AdditionalModules): Promise<ScriptRunnerResult>; | ||
} | ||
@@ -14,0 +35,0 @@ |
@@ -19,4 +19,5 @@ // packages/runner-vm/src/index.ts | ||
var ModuleLinker = class { | ||
constructor(moduleRules) { | ||
constructor(moduleRules, additionalModules) { | ||
this.moduleRules = moduleRules; | ||
this.additionalModules = additionalModules; | ||
this.linker = this.linker.bind(this); | ||
@@ -40,6 +41,18 @@ } | ||
} | ||
const identifier = path.resolve(path.dirname(referencing.identifier), spec); | ||
const additionalModule = this.additionalModules[spec]; | ||
const identifier = additionalModule ? spec : path.resolve(path.dirname(referencing.identifier), spec); | ||
const cached = this.#moduleCache.get(identifier); | ||
if (cached) | ||
return cached; | ||
const moduleOptions = { identifier, context: referencing.context }; | ||
let module; | ||
if (additionalModule) { | ||
module = new vm.SyntheticModule(Object.keys(additionalModule), function() { | ||
for (const [key, value] of Object.entries(additionalModule)) { | ||
this.setExport(key, value); | ||
} | ||
}, moduleOptions); | ||
this.#moduleCache.set(identifier, module); | ||
return module; | ||
} | ||
const relativeIdentifier = path.relative("", identifier); | ||
@@ -52,4 +65,2 @@ const rule = this.moduleRules.find((rule2) => rule2.include.test(relativeIdentifier)); | ||
this.#referencedPathSizes.set(identifier, data.byteLength); | ||
const moduleOptions = { identifier, context: referencing.context }; | ||
let module; | ||
switch (rule.type) { | ||
@@ -60,3 +71,3 @@ case "ESModule": | ||
case "CommonJS": | ||
const exports = this.loadCommonJSModule(errorBase, identifier, referencing.context); | ||
const exports = this.loadCommonJSModule(errorBase, identifier, spec, referencing.context); | ||
module = new vm.SyntheticModule(["default"], function() { | ||
@@ -87,6 +98,13 @@ this.setExport("default", exports); | ||
} | ||
loadCommonJSModule(errorBase, identifier, context) { | ||
loadCommonJSModule(errorBase, identifier, spec, context) { | ||
const cached = this.#cjsModuleCache.get(identifier); | ||
if (cached) | ||
return cached.exports; | ||
const additionalModule = this.additionalModules[spec]; | ||
const module = { exports: {} }; | ||
if (additionalModule) { | ||
module.exports.default = additionalModule.default; | ||
this.#cjsModuleCache.set(identifier, module); | ||
return module.exports; | ||
} | ||
const relativeIdentifier = path.relative("", identifier); | ||
@@ -97,3 +115,2 @@ const rule = this.moduleRules.find((rule2) => rule2.include.test(relativeIdentifier)); | ||
} | ||
const module = { exports: {} }; | ||
this.#cjsModuleCache.set(identifier, module); | ||
@@ -138,3 +155,3 @@ const data = readFileSync(identifier); | ||
const identifier = path.resolve(referencingDirname, spec); | ||
return this.loadCommonJSModule(errorBase, identifier, context); | ||
return this.loadCommonJSModule(errorBase, identifier, spec, context); | ||
}; | ||
@@ -196,2 +213,5 @@ } | ||
var VMScriptRunner = class { | ||
constructor(context) { | ||
this.context = context; | ||
} | ||
runAsScript(context, blueprint) { | ||
@@ -212,11 +232,16 @@ const script = new vm2.Script(blueprint.code, { | ||
} | ||
async run(globalScope, blueprint, modulesRules) { | ||
async run(globalScope, blueprint, modulesRules, additionalModules) { | ||
if (modulesRules && !("SourceTextModule" in vm2)) { | ||
throw new VMScriptRunnerError("ERR_MODULE_DISABLED", "Modules support requires the --experimental-vm-modules flag"); | ||
} | ||
const linker = modulesRules && new ModuleLinker(modulesRules); | ||
const linker = modulesRules && new ModuleLinker(modulesRules, additionalModules ?? {}); | ||
Object.assign(globalScope, proxiedGlobals); | ||
const context = vm2.createContext(globalScope, { | ||
codeGeneration: { strings: false, wasm: false } | ||
}); | ||
let context = this.context; | ||
if (context) { | ||
Object.assign(context, globalScope); | ||
} else { | ||
context = vm2.createContext(globalScope, { | ||
codeGeneration: { strings: false, wasm: false } | ||
}); | ||
} | ||
let exports = {}; | ||
@@ -238,4 +263,5 @@ let bundleSize = 0; | ||
VMScriptRunner, | ||
VMScriptRunnerError | ||
VMScriptRunnerError, | ||
proxiedGlobals | ||
}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@miniflare/runner-vm", | ||
"version": "2.0.0-next.3", | ||
"version": "2.0.0-rc.1", | ||
"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-next.3" | ||
"@miniflare/shared": "2.0.0-rc.1" | ||
}, | ||
"devDependencies": { | ||
"@miniflare/shared-test": "2.0.0-next.3" | ||
"@miniflare/shared-test": "2.0.0-rc.1" | ||
} | ||
} |
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
17068
287
+ Added@miniflare/shared@2.0.0-rc.1(transitive)
- Removed@miniflare/shared@2.0.0-next.3(transitive)
Updated@miniflare/shared@2.0.0-rc.1