From 16749ec3dd5024d88e11fcd3e63f4be81039d23f Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Thu, 1 Feb 2024 16:30:34 +0100 Subject: [PATCH] Revert "Migrate from `vm` to `isolated-vm`" --- hosting/single/Dockerfile | 2 - packages/backend-core/package.json | 2 +- packages/backend-core/src/context/types.ts | 8 +- packages/backend-core/src/timers/timers.ts | 38 ++++ .../bbui/src/Form/Core/EnvDropdown.svelte | 4 +- packages/server/Dockerfile | 2 - packages/server/nodemon.json | 4 +- packages/server/package.json | 22 +-- packages/server/scripts/test.sh | 4 +- .../src/api/controllers/static/index.ts | 2 +- .../server/src/api/routes/tests/table.spec.ts | 2 +- packages/server/src/db/newid.ts | 2 +- packages/server/src/environment.ts | 2 - packages/server/src/jsRunner.ts | 61 +++++++ packages/server/src/jsRunner/index.ts | 167 ------------------ .../src/jsRunner/tests/jsRunner.spec.ts | 75 -------- .../src/utilities/fileSystem/filesystem.ts | 2 +- packages/server/src/utilities/scriptRunner.ts | 65 ++----- packages/string-templates/package.json | 10 +- packages/string-templates/src/errors.js | 11 -- .../src/helpers/javascript.js | 2 +- packages/string-templates/src/helpers/list.js | 33 ++-- .../string-templates/src/index-helpers.js | 9 - packages/string-templates/src/index.cjs | 5 - packages/string-templates/src/index.js | 5 - packages/string-templates/src/index.mjs | 2 - .../string-templates/test/manifest.spec.js | 103 ++++++++++- packages/string-templates/test/utils.js | 111 ------------ packages/worker/Dockerfile | 2 - packages/worker/nodemon.json | 4 +- yarn.lock | 137 ++------------ 31 files changed, 260 insertions(+), 638 deletions(-) create mode 100644 packages/server/src/jsRunner.ts delete mode 100644 packages/server/src/jsRunner/index.ts delete mode 100644 packages/server/src/jsRunner/tests/jsRunner.spec.ts delete mode 100644 packages/string-templates/src/errors.js delete mode 100644 packages/string-templates/src/index-helpers.js delete mode 100644 packages/string-templates/test/utils.js diff --git a/hosting/single/Dockerfile b/hosting/single/Dockerfile index f9044cd124..67ac677984 100644 --- a/hosting/single/Dockerfile +++ b/hosting/single/Dockerfile @@ -124,8 +124,6 @@ HEALTHCHECK --interval=15s --timeout=15s --start-period=45s CMD "/healthcheck.sh # must set this just before running ENV NODE_ENV=production -# this is required for isolated-vm to work on Node 20+ -ENV NODE_OPTIONS="--no-node-snapshot" WORKDIR / CMD ["./runner.sh"] diff --git a/packages/backend-core/package.json b/packages/backend-core/package.json index 13bc7011b5..85644488f5 100644 --- a/packages/backend-core/package.json +++ b/packages/backend-core/package.json @@ -54,7 +54,7 @@ "sanitize-s3-objectkey": "0.0.1", "semver": "7.3.7", "tar-fs": "2.1.1", - "uuid": "^8.3.2" + "uuid": "8.3.2" }, "devDependencies": { "@shopify/jest-koa-mocks": "5.1.1", diff --git a/packages/backend-core/src/context/types.ts b/packages/backend-core/src/context/types.ts index cc052ca505..f73dc9f5c7 100644 --- a/packages/backend-core/src/context/types.ts +++ b/packages/backend-core/src/context/types.ts @@ -1,5 +1,5 @@ import { IdentityContext } from "@budibase/types" -import { Isolate, Context, Module } from "isolated-vm" +import { ExecutionTimeTracker } from "../timers" // keep this out of Budibase types, don't want to expose context info export type ContextMap = { @@ -10,9 +10,5 @@ export type ContextMap = { isScim?: boolean automationId?: string isMigrating?: boolean - isolateRefs?: { - jsIsolate: Isolate - jsContext: Context - helpersModule: Module - } + jsExecutionTracker?: ExecutionTimeTracker } diff --git a/packages/backend-core/src/timers/timers.ts b/packages/backend-core/src/timers/timers.ts index 000be74821..9121c576cd 100644 --- a/packages/backend-core/src/timers/timers.ts +++ b/packages/backend-core/src/timers/timers.ts @@ -20,3 +20,41 @@ export function cleanup() { } intervals = [] } + +export class ExecutionTimeoutError extends Error { + public readonly name = "ExecutionTimeoutError" +} + +export class ExecutionTimeTracker { + static withLimit(limitMs: number) { + return new ExecutionTimeTracker(limitMs) + } + + constructor(readonly limitMs: number) {} + + private totalTimeMs = 0 + + track(f: () => T): T { + this.checkLimit() + const start = process.hrtime.bigint() + try { + return f() + } finally { + const end = process.hrtime.bigint() + this.totalTimeMs += Number(end - start) / 1e6 + this.checkLimit() + } + } + + get elapsedMS() { + return this.totalTimeMs + } + + checkLimit() { + if (this.totalTimeMs > this.limitMs) { + throw new ExecutionTimeoutError( + `Execution time limit of ${this.limitMs}ms exceeded: ${this.totalTimeMs}ms` + ) + } + } +} diff --git a/packages/bbui/src/Form/Core/EnvDropdown.svelte b/packages/bbui/src/Form/Core/EnvDropdown.svelte index c690ffbc6b..2edf8a5f9d 100644 --- a/packages/bbui/src/Form/Core/EnvDropdown.svelte +++ b/packages/bbui/src/Form/Core/EnvDropdown.svelte @@ -184,7 +184,7 @@ {#if environmentVariablesEnabled}
showModal()} class="add-variable">
handleUpgradePanel()} class="add-variable">