Inject only on init
This commit is contained in:
parent
a6c50500fe
commit
7d6e49f8ca
|
@ -15,18 +15,6 @@ class ExecutionTimeoutError extends Error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ModuleHandler {
|
|
||||||
private modules: string[] = []
|
|
||||||
|
|
||||||
registerModule(code: string) {
|
|
||||||
this.modules.push(code)
|
|
||||||
}
|
|
||||||
|
|
||||||
generateImports() {
|
|
||||||
return this.modules.join(";")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class IsolatedVM implements VM {
|
export class IsolatedVM implements VM {
|
||||||
private isolate: ivm.Isolate
|
private isolate: ivm.Isolate
|
||||||
private vm: ivm.Context
|
private vm: ivm.Context
|
||||||
|
@ -37,8 +25,6 @@ export class IsolatedVM implements VM {
|
||||||
// By default the wrapper returns itself
|
// By default the wrapper returns itself
|
||||||
private codeWrapper: (code: string) => string = code => code
|
private codeWrapper: (code: string) => string = code => code
|
||||||
|
|
||||||
private moduleHandler = new ModuleHandler()
|
|
||||||
|
|
||||||
private readonly resultKey = "results"
|
private readonly resultKey = "results"
|
||||||
|
|
||||||
constructor({
|
constructor({
|
||||||
|
@ -93,9 +79,11 @@ export class IsolatedVM implements VM {
|
||||||
}
|
}
|
||||||
}`
|
}`
|
||||||
const helpersSource = loadBundle(BundleType.HELPERS)
|
const helpersSource = loadBundle(BundleType.HELPERS)
|
||||||
this.moduleHandler.registerModule(
|
const script = this.isolate.compileScriptSync(
|
||||||
`${injectedRequire};${helpersSource};helpers=helpers.default`
|
`${injectedRequire};${helpersSource};helpers=helpers.default`
|
||||||
)
|
)
|
||||||
|
script.runSync(this.vm, { timeout: this.invocationTimeout, release: true })
|
||||||
|
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -157,7 +145,10 @@ export class IsolatedVM implements VM {
|
||||||
.toString()
|
.toString()
|
||||||
.replace(/TextDecoderMock/, "TextDecoder")
|
.replace(/TextDecoderMock/, "TextDecoder")
|
||||||
|
|
||||||
this.moduleHandler.registerModule(`${textDecoderPolyfill};${bsonSource}`)
|
const script = this.isolate.compileScriptSync(
|
||||||
|
`${textDecoderPolyfill};${bsonSource}`
|
||||||
|
)
|
||||||
|
script.runSync(this.vm, { timeout: this.invocationTimeout, release: true })
|
||||||
|
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
@ -172,9 +163,7 @@ export class IsolatedVM implements VM {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
code = `${this.moduleHandler.generateImports()};results.out=${this.codeWrapper(
|
code = `results.out=${this.codeWrapper(code)}`
|
||||||
code
|
|
||||||
)}`
|
|
||||||
|
|
||||||
const script = this.isolate.compileScriptSync(code)
|
const script = this.isolate.compileScriptSync(code)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue