1
0
Fork 0
mirror of synced 2024-10-05 12:34:50 +13:00

Use bson library to parse to json

This commit is contained in:
Adria Navarro 2024-02-07 16:14:56 +01:00
parent 405de459ce
commit 0ea7a515de
3 changed files with 9 additions and 6 deletions

File diff suppressed because one or more lines are too long

View file

@ -1 +1,4 @@
import { EJSON } from "bson"
export { deserialize } from "bson"
export const toJson = EJSON.deserialize

View file

@ -26,8 +26,8 @@ class ScriptRunner {
// 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 and parse result in order to convert the result from BSON to json
script = `return JSON.parse(JSON.stringify((function(){data=deserialize(data).data;${script}})()));`
// 4. Stringify the result in order to convert the result from BSON to json
script = `return toJson((function(){data=deserialize(data, { validation: { utf8: true } }).data;${script}})());`
}
this.vm.code = script
@ -93,7 +93,7 @@ class IsolatedVM {
set code(code: string) {
code = `const fn=function(){${code}};results.out=fn();`
if (this.#bsonModule) {
code = `import {deserialize} from "compiled_module";${code}`
code = `import {deserialize, toJson} from "compiled_module";${code}`
}
this.#script = this.#isolate.compileModuleSync(code)
}