From 5b98f8711f28454ccbeae11e38ea22b4de9ee700 Mon Sep 17 00:00:00 2001 From: Val <68596159+valya@users.noreply.github.com> Date: Wed, 23 Oct 2024 10:41:10 +0100 Subject: [PATCH] fix: Add missing Node.js natives to task runners (no-changelog) (#11362) --- .../__tests__/js-task-runner.test.ts | 18 ++++++++-- .../src/js-task-runner/js-task-runner.ts | 34 ++++++++++++------- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/packages/@n8n/task-runner/src/js-task-runner/__tests__/js-task-runner.test.ts b/packages/@n8n/task-runner/src/js-task-runner/__tests__/js-task-runner.test.ts index 9635e7eae6..d555412c59 100644 --- a/packages/@n8n/task-runner/src/js-task-runner/__tests__/js-task-runner.test.ts +++ b/packages/@n8n/task-runner/src/js-task-runner/__tests__/js-task-runner.test.ts @@ -283,7 +283,7 @@ describe('JsTaskRunner', () => { }); it('should allow access to Node.js Buffers', async () => { - const outcome = await execTaskWithParams({ + const outcomeAll = await execTaskWithParams({ task: newTaskWithSettings({ code: 'return { val: Buffer.from("test-buffer").toString() }', nodeMode: 'runOnceForAllItems', @@ -293,7 +293,21 @@ describe('JsTaskRunner', () => { }), }); - expect(outcome.result).toEqual([wrapIntoJson({ val: 'test-buffer' })]); + expect(outcomeAll.result).toEqual([wrapIntoJson({ val: 'test-buffer' })]); + + const outcomePer = await execTaskWithParams({ + task: newTaskWithSettings({ + code: 'return { val: Buffer.from("test-buffer").toString() }', + nodeMode: 'runOnceForEachItem', + }), + taskData: newAllCodeTaskData(inputItems.map(wrapIntoJson), { + envProviderState: undefined, + }), + }); + + expect(outcomePer.result).toEqual([ + { ...wrapIntoJson({ val: 'test-buffer' }), pairedItem: { item: 0 } }, + ]); }); }); diff --git a/packages/@n8n/task-runner/src/js-task-runner/js-task-runner.ts b/packages/@n8n/task-runner/src/js-task-runner/js-task-runner.ts index 8693fcc4cd..9e793a2dc5 100644 --- a/packages/@n8n/task-runner/src/js-task-runner/js-task-runner.ts +++ b/packages/@n8n/task-runner/src/js-task-runner/js-task-runner.ts @@ -153,6 +153,25 @@ export class JsTaskRunner extends TaskRunner { }; } + private getNativeVariables() { + return { + // Exposed Node.js globals in vm2 + Buffer, + Function, + eval, + setTimeout, + setInterval, + setImmediate, + clearTimeout, + clearInterval, + clearImmediate, + + // Missing JS natives + btoa, + atob, + }; + } + /** * Executes the requested code for all items in a single run */ @@ -170,19 +189,9 @@ export class JsTaskRunner extends TaskRunner { require: this.requireResolver, module: {}, console: customConsole, - - // Exposed Node.js globals in vm2 - Buffer, - Function, - eval, - setTimeout, - setInterval, - setImmediate, - clearTimeout, - clearInterval, - clearImmediate, - items: inputItems, + + ...this.getNativeVariables(), ...dataProxy, ...this.buildRpcCallObject(taskId), }; @@ -232,6 +241,7 @@ export class JsTaskRunner extends TaskRunner { console: customConsole, item, + ...this.getNativeVariables(), ...dataProxy, ...this.buildRpcCallObject(taskId), };