Skip to content

Commit

Permalink
Merge pull request #14227 from Budibase/fix/buffer-in-backend-js
Browse files Browse the repository at this point in the history
Polyfill for Buffer in isolated-vm
  • Loading branch information
mike12345567 authored Jul 24, 2024
2 parents fd9296c + 26fe887 commit a170d78
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 28 deletions.
4 changes: 3 additions & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"build:isolated-vm-lib:snippets": "esbuild --minify --bundle src/jsRunner/bundles/snippets.ts --outfile=src/jsRunner/bundles/snippets.ivm.bundle.js --platform=node --format=iife --global-name=snippets",
"build:isolated-vm-lib:string-templates": "esbuild --minify --bundle src/jsRunner/bundles/index-helpers.ts --outfile=src/jsRunner/bundles/index-helpers.ivm.bundle.js --platform=node --format=iife --external:handlebars --global-name=helpers",
"build:isolated-vm-lib:bson": "esbuild --minify --bundle src/jsRunner/bundles/bsonPackage.ts --outfile=src/jsRunner/bundles/bson.ivm.bundle.js --platform=node --format=iife --global-name=bson",
"build:isolated-vm-libs": "yarn build:isolated-vm-lib:string-templates && yarn build:isolated-vm-lib:bson && yarn build:isolated-vm-lib:snippets",
"build:isolated-vm-lib:buffer": "esbuild --minify --bundle src/jsRunner/bundles/buffer.ts --outfile=src/jsRunner/bundles/buffer.ivm.bundle.js --platform=node --format=iife --global-name=buffer",
"build:isolated-vm-libs": "yarn build:isolated-vm-lib:string-templates && yarn build:isolated-vm-lib:bson && yarn build:isolated-vm-lib:snippets && yarn build:isolated-vm-lib:buffer",
"build:dev": "yarn prebuild && tsc --build --watch --preserveWatchOutput",
"debug": "yarn build && node --expose-gc --inspect=9222 dist/index.js",
"jest": "NODE_OPTIONS=\"--no-node-snapshot $NODE_OPTIONS\" jest",
Expand Down Expand Up @@ -68,6 +69,7 @@
"aws-sdk": "2.1030.0",
"bcrypt": "5.1.0",
"bcryptjs": "2.4.3",
"buffer": "6.0.3",
"bull": "4.10.1",
"chokidar": "3.5.3",
"content-disposition": "^0.5.4",
Expand Down
14 changes: 7 additions & 7 deletions packages/server/src/jsRunner/bundles/bson.ivm.bundle.js

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions packages/server/src/jsRunner/bundles/buffer.ivm.bundle.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions packages/server/src/jsRunner/bundles/buffer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const Buffer = require("buffer/").Buffer

export default Buffer
2 changes: 2 additions & 0 deletions packages/server/src/jsRunner/bundles/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ export const enum BundleType {
HELPERS = "helpers",
BSON = "bson",
SNIPPETS = "snippets",
BUFFER = "buffer",
}

const bundleSourceFile: Record<BundleType, string> = {
[BundleType.HELPERS]: "./index-helpers.ivm.bundle.js",
[BundleType.BSON]: "./bson.ivm.bundle.js",
[BundleType.SNIPPETS]: "./snippets.ivm.bundle.js",
[BundleType.BUFFER]: "./buffer.ivm.bundle.js",
}
const bundleSourceCode: Partial<Record<BundleType, string>> = {}

Expand Down
1 change: 1 addition & 0 deletions packages/server/src/jsRunner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function init() {
isolateAccumulatedTimeout: env.JS_PER_REQUEST_TIMEOUT_MS,
})
.withHelpers()
.withBuffer()
.withSnippets(bbCtx?.snippets)

// Persist isolate in context so we can reuse it
Expand Down
11 changes: 11 additions & 0 deletions packages/server/src/jsRunner/tests/jsRunner.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ describe("jsRunner (using isolated-vm)", () => {
expect(result).toBeLessThanOrEqual(max)
})
})

describe("buffer", () => {
it("handle a buffer", async () => {
const base64 = Buffer.from("hello").toString("base64")
const result = await processJS(
`return Buffer.from("${base64}", "base64").toString("utf8")`
)
expect(result).toBeDefined()
expect(result).toEqual("hello")
})
})
})

// the test cases here were extracted from templates/real world examples of JS in Budibase
Expand Down
14 changes: 14 additions & 0 deletions packages/server/src/jsRunner/vm/isolated-vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export class IsolatedVM implements VM {
}
}`
const helpersSource = loadBundle(BundleType.HELPERS)

const script = this.isolate.compileScriptSync(
`${injectedRequire};${helpersSource};helpers=helpers.default`
)
Expand Down Expand Up @@ -118,6 +119,19 @@ export class IsolatedVM implements VM {
return this
}

withBuffer() {
const bufferSource = loadBundle(BundleType.BUFFER)
const script = this.isolate.compileScriptSync(`
${bufferSource};
const Buffer = buffer.default;
`)
script.runSync(this.vm, { timeout: this.invocationTimeout, release: false })
new Promise(() => {
script.release()
})
return this
}

withContext<T>(context: Record<string, any>, executeWithContext: () => T) {
this.addToContext(context)

Expand Down
40 changes: 20 additions & 20 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2038,7 +2038,7 @@
resolved "https://registry.yarnpkg.com/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz#75a2e8b51cb758a7553d6804a5932d7aace75c39"
integrity sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==

"@budibase/[email protected].22":
"@budibase/[email protected].24":
version "0.0.0"
dependencies:
"@budibase/nano" "10.1.5"
Expand Down Expand Up @@ -2119,14 +2119,14 @@
through2 "^2.0.0"

"@budibase/pro@npm:@budibase/pro@latest":
version "2.29.22"
resolved "https://registry.yarnpkg.com/@budibase/pro/-/pro-2.29.22.tgz#2608b2a76be0426879068e5a61100d6b8dde6f3a"
integrity sha512-flMVIpWQb9w3f4aiBSM73aLcYmfoPIf+kP8JXgRWO0k3nGrUGaMQNBKzXwC7soTkTrJCZjBh8uaY75AxTP2RdA==
dependencies:
"@budibase/backend-core" "2.29.22"
"@budibase/shared-core" "2.29.22"
"@budibase/string-templates" "2.29.22"
"@budibase/types" "2.29.22"
version "2.29.24"
resolved "https://registry.yarnpkg.com/@budibase/pro/-/pro-2.29.24.tgz#2dbd4c6c0f757aab7e17c413c6d6e4520086f9ac"
integrity sha512-m1v24UD6O21Vbrfsuo5kC5oeg7FzjWO2w8TQMw1VvPKmdIqqclaKDPTPytxwllTMkapMDRNzM5cQzqnQ3yHf6A==
dependencies:
"@budibase/backend-core" "2.29.24"
"@budibase/shared-core" "2.29.24"
"@budibase/string-templates" "2.29.24"
"@budibase/types" "2.29.24"
"@koa/router" "8.0.8"
bull "4.10.1"
joi "17.6.0"
Expand All @@ -2137,21 +2137,21 @@
scim-patch "^0.8.1"
scim2-parse-filter "^0.2.8"

"@budibase/[email protected].22":
"@budibase/[email protected].24":
version "0.0.0"
dependencies:
"@budibase/types" "0.0.0"
cron-validate "1.4.5"

"@budibase/[email protected].22":
"@budibase/[email protected].24":
version "0.0.0"
dependencies:
"@budibase/handlebars-helpers" "^0.13.2"
dayjs "^1.10.8"
handlebars "^4.7.8"
lodash.clonedeep "^4.5.0"

"@budibase/[email protected].22":
"@budibase/[email protected].24":
version "0.0.0"
dependencies:
scim-patch "^0.8.1"
Expand Down Expand Up @@ -7770,6 +7770,14 @@ [email protected]:
ieee754 "^1.1.4"
isarray "^1.0.0"

[email protected], buffer@^6.0.3:
version "6.0.3"
resolved "https://registry.yarnpkg.com/buffer/-/buffer-6.0.3.tgz#2ace578459cc8fbe2a70aaa8f52ee63b6a74c6c6"
integrity sha512-FTiCpNxtwiZZHEZbcbTIcZjERVICn9yq/pDFkTl95/AxzD1naBctN7YO68riM/gLSDY7sdrMby8hofADYuuqOA==
dependencies:
base64-js "^1.3.1"
ieee754 "^1.2.1"

buffer@^5.1.0, buffer@^5.2.0, buffer@^5.2.1, buffer@^5.5.0, buffer@^5.6.0:
version "5.7.1"
resolved "https://registry.yarnpkg.com/buffer/-/buffer-5.7.1.tgz#ba62e7c13133053582197160851a8f648e99eed0"
Expand All @@ -7778,14 +7786,6 @@ buffer@^5.1.0, buffer@^5.2.0, buffer@^5.2.1, buffer@^5.5.0, buffer@^5.6.0:
base64-js "^1.3.1"
ieee754 "^1.1.13"

buffer@^6.0.3:
version "6.0.3"
resolved "https://registry.yarnpkg.com/buffer/-/buffer-6.0.3.tgz#2ace578459cc8fbe2a70aaa8f52ee63b6a74c6c6"
integrity sha512-FTiCpNxtwiZZHEZbcbTIcZjERVICn9yq/pDFkTl95/AxzD1naBctN7YO68riM/gLSDY7sdrMby8hofADYuuqOA==
dependencies:
base64-js "^1.3.1"
ieee754 "^1.2.1"

bufferutil@^4.0.1:
version "4.0.7"
resolved "https://registry.yarnpkg.com/bufferutil/-/bufferutil-4.0.7.tgz#60c0d19ba2c992dd8273d3f73772ffc894c153ad"
Expand Down

0 comments on commit a170d78

Please sign in to comment.