mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
feat: Add once for each item support for JS task runner (no-changelog) (#11109)
This commit is contained in:
@@ -5,6 +5,11 @@ import { ValidationError } from './ValidationError';
|
||||
import { ExecutionError } from './ExecutionError';
|
||||
import type { SandboxContext } from './Sandbox';
|
||||
import { Sandbox } from './Sandbox';
|
||||
import {
|
||||
mapItemNotDefinedErrorIfNeededForRunForEach,
|
||||
mapItemsNotDefinedErrorIfNeededForRunForAll,
|
||||
validateNoDisallowedMethodsInRunForEach,
|
||||
} from './JsCodeValidator';
|
||||
|
||||
const { NODE_FUNCTION_ALLOW_BUILTIN: builtIn, NODE_FUNCTION_ALLOW_EXTERNAL: external } =
|
||||
process.env;
|
||||
@@ -25,7 +30,6 @@ export class JavaScriptSandbox extends Sandbox {
|
||||
constructor(
|
||||
context: SandboxContext,
|
||||
private jsCode: string,
|
||||
itemIndex: number | undefined,
|
||||
helpers: IExecuteFunctions['helpers'],
|
||||
options?: { resolver?: Resolver },
|
||||
) {
|
||||
@@ -36,7 +40,6 @@ export class JavaScriptSandbox extends Sandbox {
|
||||
plural: 'objects',
|
||||
},
|
||||
},
|
||||
itemIndex,
|
||||
helpers,
|
||||
);
|
||||
this.vm = new NodeVM({
|
||||
@@ -49,10 +52,10 @@ export class JavaScriptSandbox extends Sandbox {
|
||||
this.vm.on('console.log', (...args: unknown[]) => this.emit('output', ...args));
|
||||
}
|
||||
|
||||
async runCode(): Promise<unknown> {
|
||||
async runCode<T = unknown>(): Promise<T> {
|
||||
const script = `module.exports = async function() {${this.jsCode}\n}()`;
|
||||
try {
|
||||
const executionResult = await this.vm.run(script, __dirname);
|
||||
const executionResult = (await this.vm.run(script, __dirname)) as T;
|
||||
return executionResult;
|
||||
} catch (error) {
|
||||
throw new ExecutionError(error);
|
||||
@@ -70,10 +73,7 @@ export class JavaScriptSandbox extends Sandbox {
|
||||
executionResult = await this.vm.run(script, __dirname);
|
||||
} catch (error) {
|
||||
// anticipate user expecting `items` to pre-exist as in Function Item node
|
||||
if (error.message === 'items is not defined' && !/(let|const|var) items =/.test(script)) {
|
||||
const quoted = error.message.replace('items', '`items`');
|
||||
error.message = (quoted as string) + '. Did you mean `$input.all()`?';
|
||||
}
|
||||
mapItemsNotDefinedErrorIfNeededForRunForAll(this.jsCode, error);
|
||||
|
||||
throw new ExecutionError(error);
|
||||
}
|
||||
@@ -87,7 +87,6 @@ export class JavaScriptSandbox extends Sandbox {
|
||||
message: "The code doesn't return an array of arrays",
|
||||
description:
|
||||
'Please return an array of arrays. One array for the different outputs and one for the different items that get returned.',
|
||||
itemIndex: this.itemIndex,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -101,30 +100,10 @@ export class JavaScriptSandbox extends Sandbox {
|
||||
);
|
||||
}
|
||||
|
||||
async runCodeEachItem(): Promise<INodeExecutionData | undefined> {
|
||||
async runCodeEachItem(itemIndex: number): Promise<INodeExecutionData | undefined> {
|
||||
const script = `module.exports = async function() {${this.jsCode}\n}()`;
|
||||
|
||||
const match = this.jsCode.match(/\$input\.(?<disallowedMethod>first|last|all|itemMatching)/);
|
||||
|
||||
if (match?.groups?.disallowedMethod) {
|
||||
const { disallowedMethod } = match.groups;
|
||||
|
||||
const lineNumber =
|
||||
this.jsCode.split('\n').findIndex((line) => {
|
||||
return line.includes(disallowedMethod) && !line.startsWith('//') && !line.startsWith('*');
|
||||
}) + 1;
|
||||
|
||||
const disallowedMethodFound = lineNumber !== 0;
|
||||
|
||||
if (disallowedMethodFound) {
|
||||
throw new ValidationError({
|
||||
message: `Can't use .${disallowedMethod}() here`,
|
||||
description: "This is only available in 'Run Once for All Items' mode",
|
||||
itemIndex: this.itemIndex,
|
||||
lineNumber,
|
||||
});
|
||||
}
|
||||
}
|
||||
validateNoDisallowedMethodsInRunForEach(this.jsCode, itemIndex);
|
||||
|
||||
let executionResult: INodeExecutionData;
|
||||
|
||||
@@ -132,16 +111,13 @@ export class JavaScriptSandbox extends Sandbox {
|
||||
executionResult = await this.vm.run(script, __dirname);
|
||||
} catch (error) {
|
||||
// anticipate user expecting `item` to pre-exist as in Function Item node
|
||||
if (error.message === 'item is not defined' && !/(let|const|var) item =/.test(script)) {
|
||||
const quoted = error.message.replace('item', '`item`');
|
||||
error.message = (quoted as string) + '. Did you mean `$input.item.json`?';
|
||||
}
|
||||
mapItemNotDefinedErrorIfNeededForRunForEach(this.jsCode, error);
|
||||
|
||||
throw new ExecutionError(error, this.itemIndex);
|
||||
throw new ExecutionError(error, itemIndex);
|
||||
}
|
||||
|
||||
if (executionResult === null) return;
|
||||
if (executionResult === null) return undefined;
|
||||
|
||||
return this.validateRunCodeEachItem(executionResult);
|
||||
return this.validateRunCodeEachItem(executionResult, itemIndex);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user