Fix imports
This commit is contained in:
parent
c44119b3f9
commit
7693a1fc69
|
@ -21,13 +21,11 @@ export class IsolatedVM implements VM {
|
||||||
#timeout: number
|
#timeout: number
|
||||||
#perRequestLimit?: number
|
#perRequestLimit?: number
|
||||||
|
|
||||||
#modules: Record<
|
#modules: {
|
||||||
string,
|
import: string
|
||||||
{
|
moduleKey: string
|
||||||
headCode: string
|
module: ivm.Module
|
||||||
module: ivm.Module
|
}[] = []
|
||||||
}
|
|
||||||
> = {}
|
|
||||||
|
|
||||||
readonly #resultKey = "results"
|
readonly #resultKey = "results"
|
||||||
|
|
||||||
|
@ -63,13 +61,6 @@ export class IsolatedVM implements VM {
|
||||||
escape: querystring.escape,
|
escape: querystring.escape,
|
||||||
})
|
})
|
||||||
|
|
||||||
const injectedRequire = `const require=function req(val) {
|
|
||||||
switch (val) {
|
|
||||||
case "url": return ${urlModule};
|
|
||||||
case "querystring": return ${querystringModule};
|
|
||||||
}
|
|
||||||
}`
|
|
||||||
|
|
||||||
this.#addToContext({
|
this.#addToContext({
|
||||||
helpersStripProtocol: new ivm.Callback((str: string) => {
|
helpersStripProtocol: new ivm.Callback((str: string) => {
|
||||||
var parsed = url.parse(str) as any
|
var parsed = url.parse(str) as any
|
||||||
|
@ -78,17 +69,22 @@ export class IsolatedVM implements VM {
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const injectedRequire = `const require=function req(val) {
|
||||||
|
switch (val) {
|
||||||
|
case "url": return ${urlModule};
|
||||||
|
case "querystring": return ${querystringModule};
|
||||||
|
}
|
||||||
|
}`
|
||||||
const helpersSource = loadBundle(BundleType.HELPERS)
|
const helpersSource = loadBundle(BundleType.HELPERS)
|
||||||
const helpersModule = this.#isolate.compileModuleSync(
|
const helpersModule = this.#isolate.compileModuleSync(
|
||||||
`${injectedRequire};${helpersSource}`
|
`${injectedRequire};${helpersSource}`
|
||||||
)
|
)
|
||||||
|
|
||||||
const cryptoModule = this.#registerCallbacks({
|
|
||||||
randomUUID: crypto.randomUUID,
|
|
||||||
})
|
|
||||||
|
|
||||||
helpersModule.instantiateSync(this.#vm, specifier => {
|
helpersModule.instantiateSync(this.#vm, specifier => {
|
||||||
if (specifier === "crypto") {
|
if (specifier === "crypto") {
|
||||||
|
const cryptoModule = this.#registerCallbacks({
|
||||||
|
randomUUID: crypto.randomUUID,
|
||||||
|
})
|
||||||
const module = this.#isolate.compileModuleSync(
|
const module = this.#isolate.compileModuleSync(
|
||||||
`export default ${cryptoModule}`
|
`export default ${cryptoModule}`
|
||||||
)
|
)
|
||||||
|
@ -100,10 +96,11 @@ export class IsolatedVM implements VM {
|
||||||
throw new Error(`No imports allowed. Required: ${specifier}`)
|
throw new Error(`No imports allowed. Required: ${specifier}`)
|
||||||
})
|
})
|
||||||
|
|
||||||
this.#modules["compiled_module"] = {
|
this.#modules.push({
|
||||||
headCode: 'import helpers from "compiled_module"',
|
import: "helpers",
|
||||||
|
moduleKey: `i${crypto.randomUUID().replace(/-/g, "")}`,
|
||||||
module: helpersModule,
|
module: helpersModule,
|
||||||
}
|
})
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,15 +117,16 @@ export class IsolatedVM implements VM {
|
||||||
}
|
}
|
||||||
|
|
||||||
code = [
|
code = [
|
||||||
...Object.values(this.#modules).map(m => m.headCode),
|
...this.#modules.map(m => `import ${m.import} from "${m.moduleKey}"`),
|
||||||
`results.out=${code};`,
|
`results.out=${code};`,
|
||||||
].join(";")
|
].join(";")
|
||||||
|
|
||||||
const script = this.#isolate.compileModuleSync(code)
|
const script = this.#isolate.compileModuleSync(code)
|
||||||
|
|
||||||
script.instantiateSync(this.#vm, specifier => {
|
script.instantiateSync(this.#vm, specifier => {
|
||||||
if (specifier === "compiled_module") {
|
const module = this.#modules.find(m => m.moduleKey === specifier)
|
||||||
return this.#modules[specifier].module
|
if (module) {
|
||||||
|
return module.module
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error(`"${specifier}" import not allowed`)
|
throw new Error(`"${specifier}" import not allowed`)
|
||||||
|
|
Loading…
Reference in New Issue