Remove bson knowledge from execute

This commit is contained in:
Adria Navarro 2024-02-08 13:13:09 +01:00
parent abe09509f0
commit 2bcf7f5fd7
1 changed files with 19 additions and 18 deletions

View File

@ -52,7 +52,8 @@ export class IsolatedVM implements VM {
#timeout: number #timeout: number
#perRequestLimit?: number #perRequestLimit?: number
#parseBson?: boolean // By default the wrapper returns itself
#codeWrapper: (code: string) => string = code => code
#moduleHandler = new ModuleHandler() #moduleHandler = new ModuleHandler()
@ -136,10 +137,8 @@ export class IsolatedVM implements VM {
} }
withParsingBson(data: any) { withParsingBson(data: any) {
this.#parseBson = true
this.#addToContext({ this.#addToContext({
bsonData: bson.BSON.serialize(data), bsonData: bson.BSON.serialize({ data }),
}) })
const bsonSource = loadBundle(BundleType.BSON) const bsonSource = loadBundle(BundleType.BSON)
@ -149,6 +148,19 @@ export class IsolatedVM implements VM {
}) })
this.#moduleHandler.registerModule(bsonModule, "{deserialize, toJson}") this.#moduleHandler.registerModule(bsonModule, "{deserialize, toJson}")
// If we need to parse bson, we follow the next steps:
// 1. Serialise the data from potential BSON to buffer before passing it to the isolate
// 2. Deserialise the data within the isolate, to get the original data
// 3. Process script
// 4. Stringify the result in order to convert the result from BSON to json
this.#codeWrapper = code =>
`(function(){
const data = deserialize(bsonData, { validation: { utf8: false } }).data;
const result = ${code}
return toJson(result);
})();`
return this return this
} }
@ -164,20 +176,9 @@ export class IsolatedVM implements VM {
} }
} }
if (this.#parseBson) { code = `${this.#moduleHandler.generateImports()};results.out=${this.#codeWrapper(
// If we need to parse bson, we follow the next steps: code
// 1. Serialise the data from potential BSON to buffer before passing it to the isolate )};`
// 2. Deserialise the data within the isolate, to get the original data
// 3. Process script
// 4. Stringify the result in order to convert the result from BSON to json
code = `(function(){
const data= deserialize(bsonData);
const result = ${code}
return toJson(result);
})();`
}
code = `${this.#moduleHandler.generateImports()};results.out=${code};`
const script = this.#isolate.compileModuleSync(code) const script = this.#isolate.compileModuleSync(code)