feat: Add once for each item support for JS task runner (no-changelog) (#11109)

This commit is contained in:
Tomi Turtiainen
2024-10-07 21:18:32 +03:00
committed by GitHub
parent 1146c4e98d
commit 2bb1996738
23 changed files with 1104 additions and 142 deletions

View File

@@ -1,3 +1,5 @@
import { TaskRunnersConfig } from '@n8n/config';
import set from 'lodash/set';
import {
NodeConnectionType,
type CodeExecutionMode,
@@ -7,13 +9,12 @@ import {
type INodeType,
type INodeTypeDescription,
} from 'n8n-workflow';
import set from 'lodash/set';
import Container from 'typedi';
import { TaskRunnersConfig } from '@n8n/config';
import { javascriptCodeDescription } from './descriptions/JavascriptCodeDescription';
import { pythonCodeDescription } from './descriptions/PythonCodeDescription';
import { JavaScriptSandbox } from './JavaScriptSandbox';
import { JsTaskRunnerSandbox } from './JsTaskRunnerSandbox';
import { PythonSandbox } from './PythonSandbox';
import { getSandboxContext } from './Sandbox';
import { standardizeOutput } from './utils';
@@ -108,23 +109,17 @@ export class Code implements INodeType {
const codeParameterName = language === 'python' ? 'pythonCode' : 'jsCode';
if (!runnersConfig.disabled && language === 'javaScript') {
// TODO: once per item
const code = this.getNodeParameter(codeParameterName, 0) as string;
const items = await this.startJob<INodeExecutionData[]>(
{ javaScript: 'javascript', python: 'python' }[language] ?? language,
{
code,
nodeMode,
workflowMode,
},
0,
);
const sandbox = new JsTaskRunnerSandbox(code, nodeMode, workflowMode, this);
return [items];
return nodeMode === 'runOnceForAllItems'
? [await sandbox.runCodeAllItems()]
: [await sandbox.runCodeForEachItem()];
}
const getSandbox = (index = 0) => {
const code = this.getNodeParameter(codeParameterName, index) as string;
const context = getSandboxContext.call(this, index);
if (nodeMode === 'runOnceForAllItems') {
context.items = context.$input.all();
@@ -133,7 +128,7 @@ export class Code implements INodeType {
}
const Sandbox = language === 'python' ? PythonSandbox : JavaScriptSandbox;
const sandbox = new Sandbox(context, code, index, this.helpers);
const sandbox = new Sandbox(context, code, this.helpers);
sandbox.on(
'output',
workflowMode === 'manual'
@@ -182,7 +177,7 @@ export class Code implements INodeType {
const sandbox = getSandbox(index);
let result: INodeExecutionData | undefined;
try {
result = await sandbox.runCodeEachItem();
result = await sandbox.runCodeEachItem(index);
} catch (error) {
if (!this.continueOnFail()) {
set(error, 'node', node);