diff --git a/packages/server/src/jsRunner/bundles/index.ts b/packages/server/src/jsRunner/bundles/index.ts new file mode 100644 index 0000000000..7fe37604bc --- /dev/null +++ b/packages/server/src/jsRunner/bundles/index.ts @@ -0,0 +1,24 @@ +import { utils } from "@budibase/shared-core" +import environment from "../../environment" +import fs from "fs" + +export enum BundleType { + HELPERS = "helpers", +} + +const bundleSourceCode = { + [BundleType.HELPERS]: "../bundles/index-helpers.ivm.bundle.js", +} + +export function loadBundle(type: BundleType) { + if (environment.isJest()) { + return fs.readFileSync(require.resolve(bundleSourceCode[type]), "utf-8") + } + + switch (type) { + case BundleType.HELPERS: + return require("../bundles/index-helpers.ivm.bundle.js") + default: + utils.unreachable(type) + } +} diff --git a/packages/server/src/jsRunner/index.ts b/packages/server/src/jsRunner/index.ts index aaf8ccce65..9c54779567 100644 --- a/packages/server/src/jsRunner/index.ts +++ b/packages/server/src/jsRunner/index.ts @@ -6,6 +6,7 @@ import tracer from "dd-trace" import url from "url" import crypto from "crypto" import querystring from "querystring" +import { BundleType, loadBundle } from "./bundles" class ExecutionTimeoutError extends Error { constructor(message: string) { @@ -15,6 +16,7 @@ class ExecutionTimeoutError extends Error { } export function init() { + const helpersSource = loadBundle(BundleType.HELPERS) setJSRunner((js: string, ctx: Record) => { return tracer.trace("runJS", {}, span => { try { @@ -74,7 +76,6 @@ export function init() { }) ) - const helpersSource = require("./bundles/index-helpers.ivm.bundle.js") const helpersModule = jsIsolate.compileModuleSync( `${injectedRequire};${helpersSource}` )