Callbacks
This commit is contained in:
parent
0d0171fa08
commit
c44119b3f9
|
@ -54,37 +54,23 @@ export class IsolatedVM implements VM {
|
||||||
}
|
}
|
||||||
|
|
||||||
withHelpers() {
|
withHelpers() {
|
||||||
const injectedRequire = `
|
const urlModule = this.#registerCallbacks({
|
||||||
const require = function(val){
|
resolve: url.resolve,
|
||||||
switch (val) {
|
parse: url.parse,
|
||||||
case "url":
|
})
|
||||||
return {
|
|
||||||
resolve: (...params) => urlResolveCb(...params),
|
|
||||||
parse: (...params) => urlParseCb(...params),
|
|
||||||
}
|
|
||||||
case "querystring":
|
|
||||||
return {
|
|
||||||
escape: (...params) => querystringEscapeCb(...params),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};`
|
|
||||||
|
|
||||||
const helpersSource = loadBundle(BundleType.HELPERS)
|
const querystringModule = this.#registerCallbacks({
|
||||||
const helpersModule = this.#isolate.compileModuleSync(
|
escape: querystring.escape,
|
||||||
`${injectedRequire};${helpersSource}`
|
})
|
||||||
)
|
|
||||||
|
const injectedRequire = `const require=function req(val) {
|
||||||
|
switch (val) {
|
||||||
|
case "url": return ${urlModule};
|
||||||
|
case "querystring": return ${querystringModule};
|
||||||
|
}
|
||||||
|
}`
|
||||||
|
|
||||||
this.#addToContext({
|
this.#addToContext({
|
||||||
urlResolveCb: new ivm.Callback(
|
|
||||||
(...params: Parameters<typeof url.resolve>) => url.resolve(...params)
|
|
||||||
),
|
|
||||||
urlParseCb: new ivm.Callback((...params: Parameters<typeof url.parse>) =>
|
|
||||||
url.parse(...params)
|
|
||||||
),
|
|
||||||
querystringEscapeCb: new ivm.Callback(
|
|
||||||
(...params: Parameters<typeof querystring.escape>) =>
|
|
||||||
querystring.escape(...params)
|
|
||||||
),
|
|
||||||
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
|
||||||
parsed.protocol = ""
|
parsed.protocol = ""
|
||||||
|
@ -92,24 +78,24 @@ export class IsolatedVM implements VM {
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
|
|
||||||
const cryptoModule = this.#isolate.compileModuleSync(
|
const helpersSource = loadBundle(BundleType.HELPERS)
|
||||||
`export default { randomUUID: cryptoRandomUUIDCb }`
|
const helpersModule = this.#isolate.compileModuleSync(
|
||||||
|
`${injectedRequire};${helpersSource}`
|
||||||
)
|
)
|
||||||
cryptoModule.instantiateSync(this.#vm, specifier => {
|
|
||||||
throw new Error(`No imports allowed. Required: ${specifier}`)
|
|
||||||
})
|
|
||||||
|
|
||||||
this.#addToContext({
|
const cryptoModule = this.#registerCallbacks({
|
||||||
cryptoRandomUUIDCb: new ivm.Callback(
|
randomUUID: crypto.randomUUID,
|
||||||
(...params: Parameters<typeof crypto.randomUUID>) => {
|
|
||||||
return crypto.randomUUID(...params)
|
|
||||||
}
|
|
||||||
),
|
|
||||||
})
|
})
|
||||||
|
|
||||||
helpersModule.instantiateSync(this.#vm, specifier => {
|
helpersModule.instantiateSync(this.#vm, specifier => {
|
||||||
if (specifier === "crypto") {
|
if (specifier === "crypto") {
|
||||||
return cryptoModule
|
const module = this.#isolate.compileModuleSync(
|
||||||
|
`export default ${cryptoModule}`
|
||||||
|
)
|
||||||
|
module.instantiateSync(this.#vm, specifier => {
|
||||||
|
throw new Error(`No imports allowed. Required: ${specifier}`)
|
||||||
|
})
|
||||||
|
return module
|
||||||
}
|
}
|
||||||
throw new Error(`No imports allowed. Required: ${specifier}`)
|
throw new Error(`No imports allowed. Required: ${specifier}`)
|
||||||
})
|
})
|
||||||
|
@ -154,6 +140,28 @@ export class IsolatedVM implements VM {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#registerCallbacks(functions: Record<string, any>) {
|
||||||
|
const libId = crypto.randomUUID().replace(/-/g, "")
|
||||||
|
|
||||||
|
const x: Record<string, string> = {}
|
||||||
|
for (const [funcName, func] of Object.entries(functions)) {
|
||||||
|
const key = `f${libId}${funcName}cb`
|
||||||
|
x[funcName] = key
|
||||||
|
|
||||||
|
this.#addToContext({
|
||||||
|
[key]: new ivm.Callback((...params: any[]) => (func as any)(...params)),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const mod =
|
||||||
|
`{` +
|
||||||
|
Object.entries(x)
|
||||||
|
.map(([key, func]) => `${key}: ${func}`)
|
||||||
|
.join() +
|
||||||
|
"}"
|
||||||
|
return mod
|
||||||
|
}
|
||||||
|
|
||||||
#addToContext(context: Record<string, any>) {
|
#addToContext(context: Record<string, any>) {
|
||||||
for (let key in context) {
|
for (let key in context) {
|
||||||
this.#jail.setSync(
|
this.#jail.setSync(
|
||||||
|
|
Loading…
Reference in New Issue