AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents
TypeScript ai-agentai-agent-toolsai-agentsai-agents-frameworkmcpmcp-servermcp-toolsmcpsn8n-alternativeno-code-automationworkflowworkflow-automationworkflows
fix(engine): run legacy CommonJS code steps under Deno via require fallback (#15877)
Chaker Atallah11c1a882a7f3
diff --git a/packages/server/engine/src/lib/core/code/deno-code-sandbox.ts b/packages/server/engine/src/lib/core/code/deno-code-sandbox.tsindex 21c3ba5ed5d..e88817cde4a 100644--- a/packages/server/engine/src/lib/core/code/deno-code-sandbox.ts+++ b/packages/server/engine/src/lib/core/code/deno-code-sandbox.ts@@ -20,8 +20,35 @@ export function denoCodeSandbox(permissions: DenoPermission[]): CodeSandbox {return deno.run({body: `const { createRequire } = await import('node:module');+ const { readFileSync } = await import('node:fs');globalThis.require = createRequire(${JSON.stringify(entryUrl)});- const mod = await import(${JSON.stringify(entryUrl)});+ const source = readFileSync(${JSON.stringify(realCodePath)}, 'utf8');+ const hasEsmSyntax = /^[ \\t]*(import|export)\\s/m.test(source);+ const hasCjsExports = /\\b(module\\.exports|exports\\.[$A-Za-z_]|exports\\[)/.test(source);+ let mod;+ if (!hasEsmSyntax && hasCjsExports) {+ try {+ mod = globalThis.require(${JSON.stringify(realCodePath)});+ }+ catch (error) {+ if (error?.code !== 'ERR_REQUIRE_ASYNC_MODULE') {+ throw error;+ }+ mod = await import(${JSON.stringify(entryUrl)});+ }+ }+ else {+ try {+ mod = await import(${JSON.stringify(entryUrl)});+ }+ catch (error) {+ const isCommonJsSignature = error instanceof ReferenceError && /\\b(exports|module) is not defined\\b/.test(String(error));+ if (!isCommonJsSignature) {+ throw error;+ }+ mod = globalThis.require(${JSON.stringify(realCodePath)});+ }+ }if (typeof mod.code !== 'function') {throw new Error('Code step must export a "code" function');}diff --git a/packages/server/engine/test/core/code/deno-code-sandbox.test.ts b/packages/server/engine/test/core/code/deno-code-sandbox.test.tsindex df6c0c0aa0c..c9d3e8dd88c 100644--- a/packages/server/engine/test/core/code/deno-code-sandbox.test.ts+++ b/packages/server/engine/test/core/code/deno-code-sandbox.test.ts@@ -188,6 +188,50 @@ describe('denoCodeSandbox permission boundary', () => {})})+ describe('runs legacy CommonJS code steps', () => {+ it('loads exports.code style modules through the require fallback', async () => {+ const result = await runModule(`exports.code = async (inputs: { a: number }) => inputs.a + 1`, { a: 41 })+ expect(result).toBe(42)+ })++ it('loads module.exports = { code } style modules', async () => {+ const result = await runModule(`module.exports = { code: async () => 'cjs-object' }`)+ expect(result).toBe('cjs-object')+ })++ it('supports require of node builtins inside CJS modules', async () => {+ const result = await runModule(`const crypto = require('node:crypto');\nexports.code = async () => crypto.createHash('sha256').update('ap').digest('hex')`)+ expect(result).toMatch(/^[0-9a-f]{64}$/)+ })++ it('rejects a CJS module that does not export a code function', async () => {+ await expectRejection(runModule(`exports.notCode = 1`), /must export a "code" function/)+ })++ it('evaluates module-scope statements of a CJS step exactly once', async () => {+ const result = await runModule(`globalThis.__apRuns = (globalThis.__apRuns ?? 0) + 1;\nexports.code = async () => globalThis.__apRuns`)+ expect(result).toBe(1)+ })++ it('loads a top-level-await ESM step even when it looks like CJS to the source sniff', async () => {+ const result = await runModule(`const note = "module.exports is legacy"; export const code = async () => note + '-' + top;\nconst top = await Promise.resolve('tla');`)+ expect(result).toBe('module.exports is legacy-tla')+ })++ it('resolves an installed npm package via require inside a CJS step', async () => {+ const pkgDir = path.join(stepDir, 'node_modules', 'cjs-req-fixture')+ await mkdir(pkgDir, { recursive: true })+ await writeFile(path.join(pkgDir, 'package.json'), JSON.stringify({ name: 'cjs-req-fixture', version: '1.0.0', main: 'index.js' }))+ await writeFile(path.join(pkgDir, 'index.js'), `module.exports = { greet: (name) => 'hello ' + name }`)+ const result = await runModule(`const { greet } = require('cjs-req-fixture');\nexports.code = async () => greet('cjs')`)+ expect(result).toBe('hello cjs')+ })++ it('does not swallow genuine module-scope errors from ESM modules', async () => {+ await expectRejection(runModule(`throw new Error('boom at module scope');\nexport const code = async () => 1`), /boom at module scope/)+ })+ })+describe('resolves imports', () => {it('supports bare require of node builtins (legacy user code)', async () => {const result = await runModule(`const crypto = require('node:crypto'); export const code = async () => crypto.createHash('sha256').update('ap').digest('hex')`)