mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
feat(Code Node): Add Python support (#4295)
This commit is contained in:
@@ -1,70 +1,93 @@
|
||||
import { NodeVM } from 'vm2';
|
||||
import { ValidationError } from './ValidationError';
|
||||
import { ExecutionError } from './ExecutionError';
|
||||
import { isObject, REQUIRED_N8N_ITEM_KEYS } from './utils';
|
||||
import { isObject } from './utils';
|
||||
|
||||
import type {
|
||||
IDataObject,
|
||||
IExecuteFunctions,
|
||||
INodeExecutionData,
|
||||
IWorkflowDataProxyData,
|
||||
WorkflowExecuteMode,
|
||||
} from 'n8n-workflow';
|
||||
import type { IExecuteFunctions, INodeExecutionData, IWorkflowDataProxyData } from 'n8n-workflow';
|
||||
|
||||
interface SandboxContext extends IWorkflowDataProxyData {
|
||||
interface SandboxTextKeys {
|
||||
object: {
|
||||
singular: string;
|
||||
plural: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface SandboxContext extends IWorkflowDataProxyData {
|
||||
$getNodeParameter: IExecuteFunctions['getNodeParameter'];
|
||||
$getWorkflowStaticData: IExecuteFunctions['getWorkflowStaticData'];
|
||||
helpers: IExecuteFunctions['helpers'];
|
||||
}
|
||||
|
||||
const { NODE_FUNCTION_ALLOW_BUILTIN: builtIn, NODE_FUNCTION_ALLOW_EXTERNAL: external } =
|
||||
process.env;
|
||||
export const REQUIRED_N8N_ITEM_KEYS = new Set(['json', 'binary', 'pairedItem']);
|
||||
|
||||
export class Sandbox extends NodeVM {
|
||||
private itemIndex: number | undefined = undefined;
|
||||
export function getSandboxContext(this: IExecuteFunctions, index: number): SandboxContext {
|
||||
return {
|
||||
// from NodeExecuteFunctions
|
||||
$getNodeParameter: this.getNodeParameter,
|
||||
$getWorkflowStaticData: this.getWorkflowStaticData,
|
||||
helpers: this.helpers,
|
||||
|
||||
// to bring in all $-prefixed vars and methods from WorkflowDataProxy
|
||||
// $node, $items(), $parameter, $json, $env, etc.
|
||||
...this.getWorkflowDataProxy(index),
|
||||
};
|
||||
}
|
||||
|
||||
export abstract class Sandbox {
|
||||
constructor(
|
||||
context: SandboxContext,
|
||||
private jsCode: string,
|
||||
workflowMode: WorkflowExecuteMode,
|
||||
private textKeys: SandboxTextKeys,
|
||||
protected itemIndex: number | undefined,
|
||||
private helpers: IExecuteFunctions['helpers'],
|
||||
) {
|
||||
super({
|
||||
console: workflowMode === 'manual' ? 'redirect' : 'inherit',
|
||||
sandbox: context,
|
||||
require: {
|
||||
builtin: builtIn ? builtIn.split(',') : [],
|
||||
external: external ? { modules: external.split(','), transitive: false } : false,
|
||||
},
|
||||
});
|
||||
}
|
||||
) {}
|
||||
|
||||
async runCodeAllItems(): Promise<INodeExecutionData[]> {
|
||||
const script = `module.exports = async function() {${this.jsCode}\n}()`;
|
||||
abstract runCodeAllItems(): Promise<INodeExecutionData[]>;
|
||||
|
||||
let executionResult: INodeExecutionData | INodeExecutionData[];
|
||||
abstract runCodeEachItem(): Promise<INodeExecutionData | undefined>;
|
||||
|
||||
try {
|
||||
executionResult = await this.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()`?';
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
||||
throw new ExecutionError(error);
|
||||
validateRunCodeEachItem(executionResult: INodeExecutionData | undefined): INodeExecutionData {
|
||||
if (typeof executionResult !== 'object') {
|
||||
throw new ValidationError({
|
||||
message: `Code doesn't return ${this.getTextKey('object', { includeArticle: true })}`,
|
||||
description: `Please return ${this.getTextKey('object', {
|
||||
includeArticle: true,
|
||||
})} representing the output item. ('${executionResult}' was returned instead.)`,
|
||||
itemIndex: this.itemIndex,
|
||||
});
|
||||
}
|
||||
|
||||
if (executionResult === null) return [];
|
||||
if (Array.isArray(executionResult)) {
|
||||
const firstSentence =
|
||||
executionResult.length > 0
|
||||
? `An array of ${typeof executionResult[0]}s was returned.`
|
||||
: 'An empty array was returned.';
|
||||
throw new ValidationError({
|
||||
message: `Code doesn't return a single ${this.getTextKey('object')}`,
|
||||
description: `${firstSentence} If you need to output multiple items, please use the 'Run Once for All Items' mode instead.`,
|
||||
itemIndex: this.itemIndex,
|
||||
});
|
||||
}
|
||||
|
||||
if (executionResult === undefined || typeof executionResult !== 'object') {
|
||||
const [returnData] = this.helpers.normalizeItems([executionResult]);
|
||||
|
||||
this.validateItem(returnData);
|
||||
|
||||
// If at least one top-level key is a supported item key (`json`, `binary`, etc.),
|
||||
// and another top-level key is unrecognized, then the user mis-added a property
|
||||
// directly on the item, when they intended to add it on the `json` property
|
||||
this.validateTopLevelKeys(returnData);
|
||||
|
||||
return returnData;
|
||||
}
|
||||
|
||||
validateRunCodeAllItems(
|
||||
executionResult: INodeExecutionData | INodeExecutionData[] | undefined,
|
||||
itemIndex?: number,
|
||||
): INodeExecutionData[] {
|
||||
if (typeof executionResult !== 'object') {
|
||||
throw new ValidationError({
|
||||
message: "Code doesn't return items properly",
|
||||
description:
|
||||
'Please return an array of objects, one for each item you would like to output',
|
||||
itemIndex: this.itemIndex,
|
||||
description: `Please return an array of ${this.getTextKey('object', {
|
||||
plural: true,
|
||||
})}, one for each item you would like to output.`,
|
||||
itemIndex,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -77,109 +100,54 @@ export class Sandbox extends NodeVM {
|
||||
* item keys to be wrapped in `json` when normalizing items below.
|
||||
*/
|
||||
const mustHaveTopLevelN8nKey = executionResult.some((item) =>
|
||||
Object.keys(item as IDataObject).find((key) => REQUIRED_N8N_ITEM_KEYS.has(key)),
|
||||
Object.keys(item).find((key) => REQUIRED_N8N_ITEM_KEYS.has(key)),
|
||||
);
|
||||
|
||||
for (const item of executionResult) {
|
||||
if (mustHaveTopLevelN8nKey) {
|
||||
if (mustHaveTopLevelN8nKey) {
|
||||
for (const item of executionResult) {
|
||||
this.validateTopLevelKeys(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const returnData = this.helpers.normalizeItems(executionResult);
|
||||
returnData.forEach((item) => this.validateResult(item));
|
||||
returnData.forEach((item) => this.validateItem(item));
|
||||
return returnData;
|
||||
}
|
||||
|
||||
async runCodeEachItem(itemIndex: number): Promise<INodeExecutionData | undefined> {
|
||||
this.itemIndex = itemIndex;
|
||||
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,
|
||||
});
|
||||
}
|
||||
private getTextKey(
|
||||
key: keyof SandboxTextKeys,
|
||||
options?: { includeArticle?: boolean; plural?: boolean },
|
||||
) {
|
||||
const response = this.textKeys[key][options?.plural ? 'plural' : 'singular'];
|
||||
if (!options?.includeArticle) {
|
||||
return response;
|
||||
}
|
||||
|
||||
let executionResult: INodeExecutionData;
|
||||
|
||||
try {
|
||||
executionResult = await this.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`?';
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
||||
throw new ExecutionError(error, this.itemIndex);
|
||||
if (['a', 'e', 'i', 'o', 'u'].some((value) => response.startsWith(value))) {
|
||||
return `an ${response}`;
|
||||
}
|
||||
|
||||
if (executionResult === null) return;
|
||||
|
||||
if (executionResult === undefined || typeof executionResult !== 'object') {
|
||||
throw new ValidationError({
|
||||
message: "Code doesn't return an object",
|
||||
description: `Please return an object representing the output item. ('${executionResult}' was returned instead.)`,
|
||||
itemIndex: this.itemIndex,
|
||||
});
|
||||
}
|
||||
|
||||
if (Array.isArray(executionResult)) {
|
||||
const firstSentence =
|
||||
executionResult.length > 0
|
||||
? `An array of ${typeof executionResult[0]}s was returned.`
|
||||
: 'An empty array was returned.';
|
||||
|
||||
throw new ValidationError({
|
||||
message: "Code doesn't return a single object",
|
||||
description: `${firstSentence} If you need to output multiple items, please use the 'Run Once for All Items' mode instead`,
|
||||
itemIndex: this.itemIndex,
|
||||
});
|
||||
}
|
||||
|
||||
const [returnData] = this.helpers.normalizeItems([executionResult]);
|
||||
this.validateResult(returnData);
|
||||
|
||||
// If at least one top-level key is a supported item key (`json`, `binary`, etc.),
|
||||
// and another top-level key is unrecognized, then the user mis-added a property
|
||||
// directly on the item, when they intended to add it on the `json` property
|
||||
this.validateTopLevelKeys(returnData);
|
||||
|
||||
return returnData;
|
||||
return `a ${response}`;
|
||||
}
|
||||
|
||||
private validateResult({ json, binary }: INodeExecutionData) {
|
||||
private validateItem({ json, binary }: INodeExecutionData) {
|
||||
if (json === undefined || !isObject(json)) {
|
||||
throw new ValidationError({
|
||||
message: "A 'json' property isn't an object",
|
||||
description: "In the returned data, every key named 'json' must point to an object",
|
||||
message: `A 'json' property isn't ${this.getTextKey('object', { includeArticle: true })}`,
|
||||
description: `In the returned data, every key named 'json' must point to ${this.getTextKey(
|
||||
'object',
|
||||
{ includeArticle: true },
|
||||
)}.`,
|
||||
itemIndex: this.itemIndex,
|
||||
});
|
||||
}
|
||||
|
||||
if (binary !== undefined && !isObject(binary)) {
|
||||
throw new ValidationError({
|
||||
message: "A 'binary' property isn't an object",
|
||||
description: "In the returned data, every key named 'binary’ must point to an object.",
|
||||
message: `A 'binary' property isn't ${this.getTextKey('object', { includeArticle: true })}`,
|
||||
description: `In the returned data, every key named 'binary’ must point to ${this.getTextKey(
|
||||
'object',
|
||||
{ includeArticle: true },
|
||||
)}.`,
|
||||
itemIndex: this.itemIndex,
|
||||
});
|
||||
}
|
||||
@@ -196,15 +164,3 @@ export class Sandbox extends NodeVM {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export function getSandboxContext(this: IExecuteFunctions, index?: number): SandboxContext {
|
||||
return {
|
||||
// from NodeExecuteFunctions
|
||||
$getNodeParameter: this.getNodeParameter,
|
||||
$getWorkflowStaticData: this.getWorkflowStaticData,
|
||||
helpers: this.helpers,
|
||||
|
||||
// to bring in all $-prefixed vars and methods from WorkflowDataProxy
|
||||
...this.getWorkflowDataProxy(index ?? 0),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user