2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-05-04 11:00:00 -07:00
|
|
|
CodeExecutionMode,
|
|
|
|
CodeNodeEditorLanguage,
|
2022-10-13 05:28:02 -07:00
|
|
|
IExecuteFunctions,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
2023-05-04 11:00:00 -07:00
|
|
|
import { javascriptCodeDescription } from './descriptions/JavascriptCodeDescription';
|
|
|
|
import { pythonCodeDescription } from './descriptions/PythonCodeDescription';
|
|
|
|
import { JavaScriptSandbox } from './JavaScriptSandbox';
|
|
|
|
import { PythonSandbox } from './PythonSandbox';
|
|
|
|
import { getSandboxContext } from './Sandbox';
|
2022-10-13 05:28:02 -07:00
|
|
|
import { standardizeOutput } from './utils';
|
|
|
|
|
2023-08-25 05:23:23 -07:00
|
|
|
const { CODE_ENABLE_STDOUT } = process.env;
|
|
|
|
|
2022-10-13 05:28:02 -07:00
|
|
|
export class Code implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Code',
|
|
|
|
name: 'code',
|
2024-02-21 08:04:57 -08:00
|
|
|
icon: 'file:code.svg',
|
2022-10-13 05:28:02 -07:00
|
|
|
group: ['transform'],
|
2023-05-04 11:00:00 -07:00
|
|
|
version: [1, 2],
|
2023-06-02 04:10:19 -07:00
|
|
|
defaultVersion: 2,
|
2023-08-10 03:23:58 -07:00
|
|
|
description: 'Run custom JavaScript or Python code',
|
2022-10-13 05:28:02 -07:00
|
|
|
defaults: {
|
|
|
|
name: 'Code',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
parameterPane: 'wide',
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Mode',
|
|
|
|
name: 'mode',
|
|
|
|
type: 'options',
|
|
|
|
noDataExpression: true,
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Run Once for All Items',
|
|
|
|
value: 'runOnceForAllItems',
|
|
|
|
description: 'Run this code only once, no matter how many input items there are',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Run Once for Each Item',
|
|
|
|
value: 'runOnceForEachItem',
|
|
|
|
description: 'Run this code as many times as there are input items',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'runOnceForAllItems',
|
|
|
|
},
|
|
|
|
{
|
2023-05-04 11:00:00 -07:00
|
|
|
displayName: 'Language',
|
|
|
|
name: 'language',
|
|
|
|
type: 'options',
|
2022-10-13 05:28:02 -07:00
|
|
|
noDataExpression: true,
|
2023-05-04 11:00:00 -07:00
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
'@version': [2],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'JavaScript',
|
|
|
|
value: 'javaScript',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Python (Beta)',
|
|
|
|
value: 'python',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'javaScript',
|
2022-10-13 05:28:02 -07:00
|
|
|
},
|
2023-05-11 07:44:43 -07:00
|
|
|
{
|
|
|
|
displayName: 'Language',
|
|
|
|
name: 'language',
|
|
|
|
type: 'hidden',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
'@version': [1],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
default: 'javaScript',
|
|
|
|
},
|
2023-05-04 11:00:00 -07:00
|
|
|
|
|
|
|
...javascriptCodeDescription,
|
|
|
|
...pythonCodeDescription,
|
2022-10-13 05:28:02 -07:00
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions) {
|
2023-05-05 01:22:49 -07:00
|
|
|
const nodeMode = this.getNodeParameter('mode', 0) as CodeExecutionMode;
|
2022-10-13 05:28:02 -07:00
|
|
|
const workflowMode = this.getMode();
|
|
|
|
|
2023-08-01 08:47:43 -07:00
|
|
|
const node = this.getNode();
|
2023-05-04 11:00:00 -07:00
|
|
|
const language: CodeNodeEditorLanguage =
|
2023-08-01 08:47:43 -07:00
|
|
|
node.typeVersion === 2
|
2023-05-05 01:22:49 -07:00
|
|
|
? (this.getNodeParameter('language', 0) as CodeNodeEditorLanguage)
|
|
|
|
: 'javaScript';
|
2023-05-04 11:00:00 -07:00
|
|
|
const codeParameterName = language === 'python' ? 'pythonCode' : 'jsCode';
|
|
|
|
|
|
|
|
const getSandbox = (index = 0) => {
|
2023-05-05 01:22:49 -07:00
|
|
|
const code = this.getNodeParameter(codeParameterName, index) as string;
|
2023-05-04 11:00:00 -07:00
|
|
|
const context = getSandboxContext.call(this, index);
|
2023-05-30 03:56:55 -07:00
|
|
|
if (nodeMode === 'runOnceForAllItems') {
|
|
|
|
context.items = context.$input.all();
|
|
|
|
} else {
|
|
|
|
context.item = context.$input.item;
|
|
|
|
}
|
|
|
|
|
2023-08-01 08:47:43 -07:00
|
|
|
const Sandbox = language === 'python' ? PythonSandbox : JavaScriptSandbox;
|
|
|
|
const sandbox = new Sandbox(context, code, index, this.helpers);
|
|
|
|
sandbox.on(
|
|
|
|
'output',
|
|
|
|
workflowMode === 'manual'
|
|
|
|
? this.sendMessageToUI
|
2023-08-25 05:23:23 -07:00
|
|
|
: CODE_ENABLE_STDOUT === 'true'
|
2023-11-23 02:55:02 -08:00
|
|
|
? (...args) =>
|
|
|
|
console.log(`[Workflow "${this.getWorkflow().id}"][Node "${node.name}"]`, ...args)
|
|
|
|
: () => {},
|
2023-08-01 08:47:43 -07:00
|
|
|
);
|
|
|
|
return sandbox;
|
2023-05-04 11:00:00 -07:00
|
|
|
};
|
|
|
|
|
2022-10-13 05:28:02 -07:00
|
|
|
// ----------------------------------
|
|
|
|
// runOnceForAllItems
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
if (nodeMode === 'runOnceForAllItems') {
|
2023-05-04 11:00:00 -07:00
|
|
|
const sandbox = getSandbox();
|
|
|
|
let items: INodeExecutionData[];
|
2022-10-13 05:28:02 -07:00
|
|
|
try {
|
2023-11-29 03:13:55 -08:00
|
|
|
items = (await sandbox.runCodeAllItems()) as INodeExecutionData[];
|
2022-10-13 05:28:02 -07:00
|
|
|
} catch (error) {
|
2023-04-24 04:17:08 -07:00
|
|
|
if (!this.continueOnFail()) throw error;
|
2023-05-04 11:00:00 -07:00
|
|
|
items = [{ json: { error: error.message } }];
|
2022-10-13 05:28:02 -07:00
|
|
|
}
|
|
|
|
|
2023-05-04 11:00:00 -07:00
|
|
|
for (const item of items) {
|
2022-10-13 05:28:02 -07:00
|
|
|
standardizeOutput(item.json);
|
|
|
|
}
|
|
|
|
|
2023-09-05 03:59:02 -07:00
|
|
|
return [items];
|
2022-10-13 05:28:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------
|
|
|
|
// runOnceForEachItem
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
const returnData: INodeExecutionData[] = [];
|
|
|
|
|
2023-04-19 04:09:46 -07:00
|
|
|
const items = this.getInputData();
|
2022-10-13 05:28:02 -07:00
|
|
|
|
2023-04-19 04:09:46 -07:00
|
|
|
for (let index = 0; index < items.length; index++) {
|
2023-05-04 11:00:00 -07:00
|
|
|
const sandbox = getSandbox(index);
|
2023-04-19 04:09:46 -07:00
|
|
|
let result: INodeExecutionData | undefined;
|
2022-10-13 05:28:02 -07:00
|
|
|
try {
|
2023-05-04 11:00:00 -07:00
|
|
|
result = await sandbox.runCodeEachItem();
|
2022-10-13 05:28:02 -07:00
|
|
|
} catch (error) {
|
2023-04-24 04:17:08 -07:00
|
|
|
if (!this.continueOnFail()) throw error;
|
2023-10-30 10:42:47 -07:00
|
|
|
returnData.push({
|
|
|
|
json: { error: error.message },
|
|
|
|
pairedItem: {
|
|
|
|
item: index,
|
|
|
|
},
|
|
|
|
});
|
2022-10-13 05:28:02 -07:00
|
|
|
}
|
|
|
|
|
2023-04-19 04:09:46 -07:00
|
|
|
if (result) {
|
2022-10-13 05:28:02 -07:00
|
|
|
returnData.push({
|
2023-04-19 04:09:46 -07:00
|
|
|
json: standardizeOutput(result.json),
|
2022-10-13 05:28:02 -07:00
|
|
|
pairedItem: { item: index },
|
2023-04-19 04:09:46 -07:00
|
|
|
...(result.binary && { binary: result.binary }),
|
2022-10-13 05:28:02 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-05 03:59:02 -07:00
|
|
|
return [returnData];
|
2022-10-13 05:28:02 -07:00
|
|
|
}
|
|
|
|
}
|