feat(Code Node): Add Python support (#4295)

This commit is contained in:
Jan Oberhauser
2023-05-04 20:00:00 +02:00
committed by GitHub
parent 1e6a75f341
commit 35c8510ab6
25 changed files with 962 additions and 591 deletions

View File

@@ -11,7 +11,7 @@
}
]
},
"alias": ["cpde", "Javascript", "JS", "Script", "Custom Code", "Function"],
"alias": ["cpde", "Javascript", "JS", "Python", "Script", "Custom Code", "Function"],
"subcategories": {
"Core Nodes": ["Data Transformation"]
}

View File

@@ -1,12 +1,17 @@
import type {
CodeExecutionMode,
CodeNodeEditorLanguage,
IExecuteFunctions,
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import { getSandboxContext, Sandbox } from './Sandbox';
import { javascriptCodeDescription } from './descriptions/JavascriptCodeDescription';
import { pythonCodeDescription } from './descriptions/PythonCodeDescription';
import { JavaScriptSandbox } from './JavaScriptSandbox';
import { PythonSandbox } from './PythonSandbox';
import { getSandboxContext } from './Sandbox';
import { standardizeOutput } from './utils';
import type { CodeNodeMode } from './utils';
export class Code implements INodeType {
description: INodeTypeDescription = {
@@ -14,7 +19,8 @@ export class Code implements INodeType {
name: 'code',
icon: 'fa:code',
group: ['transform'],
version: 1,
version: [1, 2],
defaultVersion: 1,
description: 'Run custom JavaScript code',
defaults: {
name: 'Code',
@@ -44,59 +50,78 @@ export class Code implements INodeType {
default: 'runOnceForAllItems',
},
{
displayName: 'JavaScript',
name: 'jsCode',
typeOptions: {
editor: 'codeNodeEditor',
},
type: 'string',
default: '', // set by component
description:
'JavaScript code to execute.<br><br>Tip: You can use luxon vars like <code>$today</code> for dates and <code>$jmespath</code> for querying JSON structures. <a href="https://docs.n8n.io/nodes/n8n-nodes-base.function">Learn more</a>.',
displayName: 'Language',
name: 'language',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
'@version': [2],
},
},
options: [
{
name: 'JavaScript',
value: 'javaScript',
},
{
name: 'Python (Beta)',
value: 'python',
},
],
default: 'javaScript',
},
{
displayName:
'Type <code>$</code> for a list of <a target="_blank" href="https://docs.n8n.io/code-examples/methods-variables-reference/">special vars/methods</a>. Debug by using <code>console.log()</code> statements and viewing their output in the browser console.',
name: 'notice',
type: 'notice',
default: '',
},
...javascriptCodeDescription,
...pythonCodeDescription,
],
};
async execute(this: IExecuteFunctions) {
const nodeMode = this.getNodeParameter('mode', 0) as CodeNodeMode;
const nodeMode = this.getNodeParameter<CodeExecutionMode>('mode', 0);
const workflowMode = this.getMode();
const language: CodeNodeEditorLanguage =
this.getNode()?.typeVersion === 2 ? this.getNodeParameter('language', 0) : 'javaScript';
const codeParameterName = language === 'python' ? 'pythonCode' : 'jsCode';
const getSandbox = (index = 0) => {
const code = this.getNodeParameter<string>(codeParameterName, index);
const context = getSandboxContext.call(this, index);
if (language === 'python') {
const modules = this.getNodeParameter<string>('modules', index);
const moduleImports: string[] = modules ? modules.split(',').map((m) => m.trim()) : [];
context.printOverwrite = workflowMode === 'manual' ? this.sendMessageToUI : null;
return new PythonSandbox(context, code, moduleImports, index, this.helpers);
} else {
context.items = context.$input.all();
const sandbox = new JavaScriptSandbox(context, code, index, workflowMode, this.helpers);
if (workflowMode === 'manual') {
sandbox.vm.on('console.log', this.sendMessageToUI);
}
return sandbox;
}
};
// ----------------------------------
// runOnceForAllItems
// ----------------------------------
if (nodeMode === 'runOnceForAllItems') {
const jsCodeAllItems = this.getNodeParameter('jsCode', 0) as string;
const context = getSandboxContext.call(this);
context.items = context.$input.all();
const sandbox = new Sandbox(context, jsCodeAllItems, workflowMode, this.helpers);
if (workflowMode === 'manual') {
sandbox.on('console.log', this.sendMessageToUI);
}
let result: INodeExecutionData[];
const sandbox = getSandbox();
let items: INodeExecutionData[];
try {
result = await sandbox.runCodeAllItems();
items = await sandbox.runCodeAllItems();
} catch (error) {
if (!this.continueOnFail()) throw error;
result = [{ json: { error: error.message } }];
items = [{ json: { error: error.message } }];
}
for (const item of result) {
for (const item of items) {
standardizeOutput(item.json);
}
return this.prepareOutputData(result);
return this.prepareOutputData(items);
}
// ----------------------------------
@@ -108,19 +133,10 @@ export class Code implements INodeType {
const items = this.getInputData();
for (let index = 0; index < items.length; index++) {
const jsCodeEachItem = this.getNodeParameter('jsCode', index) as string;
const context = getSandboxContext.call(this, index);
context.item = context.$input.item;
const sandbox = new Sandbox(context, jsCodeEachItem, workflowMode, this.helpers);
if (workflowMode === 'manual') {
sandbox.on('console.log', this.sendMessageToUI);
}
const sandbox = getSandbox(index);
let result: INodeExecutionData | undefined;
try {
result = await sandbox.runCodeEachItem(index);
result = await sandbox.runCodeEachItem();
} catch (error) {
if (!this.continueOnFail()) throw error;
returnData.push({ json: { error: error.message } });

View File

@@ -0,0 +1,115 @@
import type { NodeVMOptions } from 'vm2';
import { NodeVM } from 'vm2';
import type { IExecuteFunctions, INodeExecutionData, WorkflowExecuteMode } from 'n8n-workflow';
import { ValidationError } from './ValidationError';
import { ExecutionError } from './ExecutionError';
import type { SandboxContext } from './Sandbox';
import { Sandbox } from './Sandbox';
const { NODE_FUNCTION_ALLOW_BUILTIN: builtIn, NODE_FUNCTION_ALLOW_EXTERNAL: external } =
process.env;
const getSandboxOptions = (
context: SandboxContext,
workflowMode: WorkflowExecuteMode,
): NodeVMOptions => ({
console: workflowMode === 'manual' ? 'redirect' : 'inherit',
sandbox: context,
require: {
builtin: builtIn ? builtIn.split(',') : [],
external: external ? { modules: external.split(','), transitive: false } : false,
},
});
export class JavaScriptSandbox extends Sandbox {
readonly vm: NodeVM;
constructor(
context: SandboxContext,
private jsCode: string,
itemIndex: number | undefined,
workflowMode: WorkflowExecuteMode,
helpers: IExecuteFunctions['helpers'],
) {
super(
{
object: {
singular: 'object',
plural: 'objects',
},
},
itemIndex,
helpers,
);
this.vm = new NodeVM(getSandboxOptions(context, workflowMode));
}
async runCodeAllItems(): Promise<INodeExecutionData[]> {
const script = `module.exports = async function() {${this.jsCode}\n}()`;
let executionResult: INodeExecutionData | INodeExecutionData[];
try {
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()`?';
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
throw new ExecutionError(error);
}
if (executionResult === null) return [];
return this.validateRunCodeAllItems(executionResult);
}
async runCodeEachItem(): 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,
});
}
}
let executionResult: INodeExecutionData;
try {
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`?';
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
throw new ExecutionError(error, this.itemIndex);
}
if (executionResult === null) return;
return this.validateRunCodeEachItem(executionResult);
}
}

View File

@@ -0,0 +1,28 @@
import type { PyodideInterface } from 'pyodide';
let pyodideInstance: PyodideInterface | undefined;
export async function LoadPyodide(): Promise<PyodideInterface> {
if (pyodideInstance === undefined) {
// TODO: Find better way to suppress warnings
//@ts-ignore
globalThis.Blob = (await import('node:buffer')).Blob;
// From: https://github.com/nodejs/node/issues/30810
const { emitWarning } = process;
process.emitWarning = (warning, ...args) => {
if (args[0] === 'ExperimentalWarning') {
return;
}
if (args[0] && typeof args[0] === 'object' && args[0].type === 'ExperimentalWarning') {
return;
}
return emitWarning(warning, ...(args as string[]));
};
const { loadPyodide } = await import('pyodide');
pyodideInstance = await loadPyodide();
}
return pyodideInstance;
}

View File

@@ -0,0 +1,119 @@
import type { IExecuteFunctions, INodeExecutionData } from 'n8n-workflow';
import type { PyProxyDict } from 'pyodide';
import { LoadPyodide } from './Pyodide';
import type { SandboxContext } from './Sandbox';
import { Sandbox } from './Sandbox';
type PythonSandboxContext = {
[K in keyof SandboxContext as K extends `$${infer I}` ? `_${I}` : K]: SandboxContext[K];
};
type PyodideError = Error & { type: string };
const envAccessBlocked = process.env.N8N_BLOCK_ENV_ACCESS_IN_NODE === 'true';
export class PythonSandbox extends Sandbox {
private readonly context: PythonSandboxContext;
constructor(
context: SandboxContext,
private pythonCode: string,
private moduleImports: string[],
itemIndex: number | undefined,
helpers: IExecuteFunctions['helpers'],
) {
super(
{
object: {
singular: 'dictionary',
plural: 'dictionaries',
},
},
itemIndex,
helpers,
);
// Since python doesn't allow variable names starting with `$`,
// rename them to all to start with `_` instead
this.context = Object.keys(context).reduce((acc, key) => {
acc[key.startsWith('$') ? key.replace(/^\$/, '_') : key] = context[key];
return acc;
}, {} as PythonSandboxContext);
}
async runCodeAllItems() {
const executionResult = await this.runCodeInPython<INodeExecutionData[]>();
return this.validateRunCodeAllItems(executionResult);
}
async runCodeEachItem() {
const executionResult = await this.runCodeInPython<INodeExecutionData>();
return this.validateRunCodeEachItem(executionResult);
}
private async runCodeInPython<T>() {
// Below workaround from here:
// https://github.com/pyodide/pyodide/discussions/3537#discussioncomment-4864345
const runCode = `
from _pyodide_core import jsproxy_typedict
from js import Object
jsproxy_typedict[0] = type(Object.new().as_object_map())
if printOverwrite:
print = printOverwrite
async def __main():
${this.pythonCode
.split('\n')
.map((line) => ' ' + line)
.join('\n')}
await __main()
`;
const pyodide = await LoadPyodide();
const moduleImportsFiltered = this.moduleImports.filter(
(importModule) => !['asyncio', 'pyodide', 'math'].includes(importModule),
);
if (moduleImportsFiltered.length) {
await pyodide.loadPackage('micropip');
const micropip = pyodide.pyimport('micropip');
await Promise.all(
moduleImportsFiltered.map((importModule) => micropip.install(importModule)),
);
}
let executionResult;
try {
const dict = pyodide.globals.get('dict');
const globalsDict: PyProxyDict = dict();
for (const key of Object.keys(this.context)) {
if ((key === '_env' && envAccessBlocked) || key === '_node') continue;
const value = this.context[key];
globalsDict.set(key, value);
}
executionResult = await pyodide.runPythonAsync(runCode, { globals: globalsDict });
globalsDict.destroy();
} catch (error) {
throw this.getPrettyError(error as PyodideError);
}
if (executionResult?.toJs) {
return executionResult.toJs({
dict_converter: Object.fromEntries,
create_proxies: false,
}) as T;
}
return executionResult as T;
}
private getPrettyError(error: PyodideError): Error {
const errorTypeIndex = error.message.indexOf(error.type);
if (errorTypeIndex !== -1) {
return new Error(error.message.slice(errorTypeIndex));
}
return error;
}
}

View File

@@ -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),
};
}

View File

@@ -0,0 +1,76 @@
import type { INodeProperties } from 'n8n-workflow';
const commonDescription: INodeProperties = {
displayName: 'JavaScript',
name: 'jsCode',
type: 'string',
typeOptions: {
editor: 'codeNodeEditor',
editorLanguage: 'javaScript',
},
default: '',
description:
'JavaScript code to execute.<br><br>Tip: You can use luxon vars like <code>$today</code> for dates and <code>$jmespath</code> for querying JSON structures. <a href="https://docs.n8n.io/nodes/n8n-nodes-base.function">Learn more</a>.',
noDataExpression: true,
};
const v1Properties: INodeProperties[] = [
{
...commonDescription,
displayOptions: {
show: {
'@version': [1],
mode: ['runOnceForAllItems'],
},
},
},
{
...commonDescription,
displayOptions: {
show: {
'@version': [1],
mode: ['runOnceForEachItem'],
},
},
},
];
const v2Properties: INodeProperties[] = [
{
...commonDescription,
displayOptions: {
show: {
'@version': [2],
language: ['javaScript'],
mode: ['runOnceForAllItems'],
},
},
},
{
...commonDescription,
displayOptions: {
show: {
'@version': [2],
language: ['javaScript'],
mode: ['runOnceForEachItem'],
},
},
},
];
export const javascriptCodeDescription: INodeProperties[] = [
...v1Properties,
...v2Properties,
{
displayName:
'Type <code>$</code> for a list of <a target="_blank" href="https://docs.n8n.io/code-examples/methods-variables-reference/">special vars/methods</a>. Debug by using <code>console.log()</code> statements and viewing their output in the browser console.',
name: 'notice',
type: 'notice',
displayOptions: {
show: {
language: ['javaScript'],
},
},
default: '',
},
];

View File

@@ -0,0 +1,63 @@
import type { INodeProperties } from 'n8n-workflow';
const commonDescription: INodeProperties = {
displayName: 'Python',
name: 'pythonCode',
type: 'string',
typeOptions: {
editor: 'codeNodeEditor',
editorLanguage: 'python',
},
default: '',
description:
'Python code to execute.<br><br>Tip: You can use luxon vars like <code>_today</code> for dates and <code>$_mespath</code> for querying JSON structures. <a href="https://docs.n8n.io/nodes/n8n-nodes-base.function">Learn more</a>.',
noDataExpression: true,
};
export const pythonCodeDescription: INodeProperties[] = [
{
...commonDescription,
displayOptions: {
show: {
language: ['python'],
mode: ['runOnceForAllItems'],
},
},
},
{
...commonDescription,
displayOptions: {
show: {
language: ['python'],
mode: ['runOnceForEachItem'],
},
},
},
{
displayName:
'Debug by using <code>print()</code> statements and viewing their output in the browser console.',
name: 'notice',
type: 'notice',
displayOptions: {
show: {
language: ['python'],
},
},
default: '',
},
{
displayName: 'Python Modules',
name: 'modules',
displayOptions: {
show: {
language: ['python'],
},
},
type: 'string',
default: '',
placeholder: 'opencv-python',
description:
'Comma-separated list of Python modules to load. They have to be installed to be able to be loaded and imported.',
noDataExpression: true,
},
];

View File

@@ -1,15 +1,25 @@
import { anyNumber, mock } from 'jest-mock-extended';
import { NodeVM } from 'vm2';
import type { IExecuteFunctions, IWorkflowDataProxyData } from 'n8n-workflow';
import { NodeHelpers } from 'n8n-workflow';
import { normalizeItems } from 'n8n-core';
import { testWorkflows, getWorkflowFilenames } from '../../../test/nodes/Helpers';
import {
testWorkflows,
getWorkflowFilenames,
initBinaryDataManager,
} from '../../../test/nodes/Helpers';
import { Code } from '../Code.node';
import { Sandbox } from '../Sandbox';
import { ValidationError } from '../ValidationError';
const workflows = getWorkflowFilenames(__dirname);
describe('Test Code Node', () => {
const workflows = getWorkflowFilenames(__dirname);
describe('Test Code Node', () => testWorkflows(workflows));
beforeAll(async () => {
await initBinaryDataManager();
});
testWorkflows(workflows);
});
describe('Code Node unit test', () => {
const node = new Code();
@@ -48,7 +58,7 @@ describe('Code Node unit test', () => {
Object.entries(tests).forEach(([title, [input, expected]]) =>
test(title, async () => {
jest.spyOn(Sandbox.prototype, 'run').mockResolvedValueOnce(input);
jest.spyOn(NodeVM.prototype, 'run').mockResolvedValueOnce(input);
const output = await node.execute.call(thisArg);
expect(output).toEqual([expected]);
@@ -68,14 +78,14 @@ describe('Code Node unit test', () => {
Object.entries(tests).forEach(([title, returnData]) =>
test(`return error if \`.json\` is ${title}`, async () => {
jest.spyOn(Sandbox.prototype, 'run').mockResolvedValueOnce([{ json: returnData }]);
jest.spyOn(NodeVM.prototype, 'run').mockResolvedValueOnce([{ json: returnData }]);
try {
await node.execute.call(thisArg);
throw new Error("Validation error wasn't thrown");
} catch (error) {
expect(error).toBeInstanceOf(ValidationError);
expect(error.message).toEqual("A 'json' property isn't an object");
expect(error.message).toEqual("A 'json' property isn't an object [item 0]");
}
}),
);
@@ -100,7 +110,7 @@ describe('Code Node unit test', () => {
Object.entries(tests).forEach(([title, [input, expected]]) =>
test(title, async () => {
jest.spyOn(Sandbox.prototype, 'run').mockResolvedValueOnce(input);
jest.spyOn(NodeVM.prototype, 'run').mockResolvedValueOnce(input);
const output = await node.execute.call(thisArg);
expect(output).toEqual([[{ json: expected?.json, pairedItem: { item: 0 } }]]);
@@ -120,7 +130,7 @@ describe('Code Node unit test', () => {
Object.entries(tests).forEach(([title, returnData]) =>
test(`return error if \`.json\` is ${title}`, async () => {
jest.spyOn(Sandbox.prototype, 'run').mockResolvedValueOnce({ json: returnData });
jest.spyOn(NodeVM.prototype, 'run').mockResolvedValueOnce({ json: returnData });
try {
await node.execute.call(thisArg);

View File

@@ -36,7 +36,3 @@ export function standardizeOutput(output: IDataObject) {
standardizeOutputRecursive(output);
return output;
}
export type CodeNodeMode = 'runOnceForAllItems' | 'runOnceForEachItem';
export const REQUIRED_N8N_ITEM_KEYS = new Set(['json', 'binary', 'pairedItem']);