2023-01-27 03:22:44 -08:00
|
|
|
|
import type { NodeVMOptions } from 'vm2';
|
|
|
|
|
import { NodeVM } from 'vm2';
|
|
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
|
IExecuteFunctions,
|
2022-09-11 07:42:09 -07:00
|
|
|
|
IBinaryKeyData,
|
2022-01-02 01:33:15 -08:00
|
|
|
|
IDataObject,
|
2019-06-23 03:35:23 -07:00
|
|
|
|
INodeExecutionData,
|
|
|
|
|
INodeType,
|
|
|
|
|
INodeTypeDescription,
|
|
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
|
import { deepCopy, NodeOperationError } from 'n8n-workflow';
|
2023-05-25 03:55:53 -07:00
|
|
|
|
import { vmResolver } from '../Code/JavaScriptSandbox';
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
|
|
export class Function implements INodeType {
|
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
|
displayName: 'Function',
|
|
|
|
|
name: 'function',
|
2022-10-13 05:28:02 -07:00
|
|
|
|
hidden: true,
|
2019-06-23 03:35:23 -07:00
|
|
|
|
icon: 'fa:code',
|
|
|
|
|
group: ['transform'],
|
|
|
|
|
version: 1,
|
2022-08-01 13:47:55 -07:00
|
|
|
|
description:
|
|
|
|
|
'Run custom function code which gets executed once and allows you to add, remove, change and replace items',
|
2019-06-23 03:35:23 -07:00
|
|
|
|
defaults: {
|
|
|
|
|
name: 'Function',
|
|
|
|
|
color: '#FF9922',
|
|
|
|
|
},
|
|
|
|
|
inputs: ['main'],
|
|
|
|
|
outputs: ['main'],
|
|
|
|
|
properties: [
|
2022-10-13 05:28:02 -07:00
|
|
|
|
{
|
|
|
|
|
displayName: 'A newer version of this node type is available, called the ‘Code’ node',
|
|
|
|
|
name: 'notice',
|
|
|
|
|
type: 'notice',
|
|
|
|
|
default: '',
|
|
|
|
|
},
|
2019-06-23 03:35:23 -07:00
|
|
|
|
{
|
2020-10-13 02:05:56 -07:00
|
|
|
|
displayName: 'JavaScript Code',
|
2019-06-23 03:35:23 -07:00
|
|
|
|
name: 'functionCode',
|
|
|
|
|
typeOptions: {
|
|
|
|
|
alwaysOpenEditWindow: true,
|
2021-12-23 02:41:46 -08:00
|
|
|
|
codeAutocomplete: 'function',
|
2019-09-04 09:22:06 -07:00
|
|
|
|
editor: 'code',
|
2019-06-23 03:35:23 -07:00
|
|
|
|
rows: 10,
|
|
|
|
|
},
|
|
|
|
|
type: 'string',
|
2021-05-29 12:03:59 -07:00
|
|
|
|
default: `// Code here will run only once, no matter how many input items there are.
|
2022-09-29 03:33:16 -07:00
|
|
|
|
// More info and help:https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.function/
|
2022-03-13 01:34:44 -08:00
|
|
|
|
// Tip: You can use luxon for dates and $jmespath for querying JSON structures
|
2021-05-29 12:03:59 -07:00
|
|
|
|
|
|
|
|
|
// Loop over inputs and add a new field called 'myNewField' to the JSON of each one
|
|
|
|
|
for (item of items) {
|
|
|
|
|
item.json.myNewField = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// You can write logs to the browser console
|
|
|
|
|
console.log('Done!');
|
|
|
|
|
|
|
|
|
|
return items;`,
|
2022-05-06 14:01:25 -07:00
|
|
|
|
description: 'The JavaScript code to execute',
|
2019-09-04 09:22:06 -07:00
|
|
|
|
noDataExpression: true,
|
2019-06-23 03:35:23 -07:00
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
|
// const item = this.getInputData();
|
|
|
|
|
let items = this.getInputData();
|
|
|
|
|
|
2019-08-01 13:55:33 -07:00
|
|
|
|
// Copy the items as they may get changed in the functions
|
2022-10-21 08:24:58 -07:00
|
|
|
|
items = deepCopy(items);
|
2019-08-01 13:55:33 -07:00
|
|
|
|
|
2022-09-11 07:42:09 -07:00
|
|
|
|
// Assign item indexes
|
|
|
|
|
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
|
|
|
|
|
items[itemIndex].index = itemIndex;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-02 01:33:15 -08:00
|
|
|
|
const cleanupData = (inputData: IDataObject): IDataObject => {
|
2022-08-01 13:47:55 -07:00
|
|
|
|
Object.keys(inputData).map((key) => {
|
2022-01-02 01:33:15 -08:00
|
|
|
|
if (inputData[key] !== null && typeof inputData[key] === 'object') {
|
|
|
|
|
if (inputData[key]!.constructor.name === 'Object') {
|
|
|
|
|
// Is regular node.js object so check its data
|
|
|
|
|
inputData[key] = cleanupData(inputData[key] as IDataObject);
|
|
|
|
|
} else {
|
|
|
|
|
// Is some special object like a Date so stringify
|
2022-10-21 08:24:58 -07:00
|
|
|
|
inputData[key] = deepCopy(inputData[key]);
|
2022-01-02 01:33:15 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return inputData;
|
|
|
|
|
};
|
|
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
|
// Define the global objects for the custom function
|
|
|
|
|
const sandbox = {
|
|
|
|
|
getNodeParameter: this.getNodeParameter,
|
2019-10-14 22:37:49 -07:00
|
|
|
|
getWorkflowStaticData: this.getWorkflowStaticData,
|
2019-06-23 03:35:23 -07:00
|
|
|
|
helpers: this.helpers,
|
|
|
|
|
items,
|
2019-09-04 05:53:39 -07:00
|
|
|
|
// To be able to access data of other items
|
|
|
|
|
$item: (index: number) => this.getWorkflowDataProxy(index),
|
2022-09-11 07:42:09 -07:00
|
|
|
|
getBinaryDataAsync: async (item: INodeExecutionData): Promise<IBinaryKeyData | undefined> => {
|
|
|
|
|
// Fetch Binary Data, if available. Cannot check item with `if (item?.index)`, as index may be 0.
|
|
|
|
|
if (item?.binary && item?.index !== undefined && item?.index !== null) {
|
|
|
|
|
for (const binaryPropertyName of Object.keys(item.binary)) {
|
|
|
|
|
item.binary[binaryPropertyName].data = (
|
2022-12-02 12:54:28 -08:00
|
|
|
|
await this.helpers.getBinaryDataBuffer(item.index, binaryPropertyName)
|
2022-09-11 07:42:09 -07:00
|
|
|
|
)?.toString('base64');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Return Data
|
|
|
|
|
return item.binary;
|
|
|
|
|
},
|
|
|
|
|
setBinaryDataAsync: async (item: INodeExecutionData, data: IBinaryKeyData) => {
|
|
|
|
|
// Ensure item is provided, else return a friendly error.
|
|
|
|
|
if (!item) {
|
|
|
|
|
throw new NodeOperationError(
|
|
|
|
|
this.getNode(),
|
|
|
|
|
'No item was provided to setBinaryDataAsync (item: INodeExecutionData, data: IBinaryKeyData).',
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ensure data is provided, else return a friendly error.
|
|
|
|
|
if (!data) {
|
|
|
|
|
throw new NodeOperationError(
|
|
|
|
|
this.getNode(),
|
|
|
|
|
'No data was provided to setBinaryDataAsync (item: INodeExecutionData, data: IBinaryKeyData).',
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set Binary Data
|
|
|
|
|
for (const binaryPropertyName of Object.keys(data)) {
|
|
|
|
|
const binaryItem = data[binaryPropertyName];
|
|
|
|
|
data[binaryPropertyName] = await this.helpers.setBinaryDataBuffer(
|
|
|
|
|
binaryItem,
|
|
|
|
|
Buffer.from(binaryItem.data, 'base64'),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set Item Reference
|
|
|
|
|
item.binary = data;
|
|
|
|
|
},
|
2019-06-23 03:35:23 -07:00
|
|
|
|
};
|
|
|
|
|
|
2019-09-04 05:53:39 -07:00
|
|
|
|
// Make it possible to access data via $node, $parameter, ...
|
|
|
|
|
// By default use data from first item
|
|
|
|
|
Object.assign(sandbox, sandbox.$item(0));
|
|
|
|
|
|
2021-05-29 11:41:25 -07:00
|
|
|
|
const mode = this.getMode();
|
|
|
|
|
|
2023-01-13 09:11:56 -08:00
|
|
|
|
const options: NodeVMOptions = {
|
2022-08-01 13:47:55 -07:00
|
|
|
|
console: mode === 'manual' ? 'redirect' : 'inherit',
|
2019-06-23 03:35:23 -07:00
|
|
|
|
sandbox,
|
2023-05-25 03:55:53 -07:00
|
|
|
|
require: vmResolver,
|
2019-12-13 09:23:30 -08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const vm = new NodeVM(options);
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
2021-05-29 11:41:25 -07:00
|
|
|
|
if (mode === 'manual') {
|
|
|
|
|
vm.on('console.log', this.sendMessageToUI);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
|
// Get the code to execute
|
|
|
|
|
const functionCode = this.getNodeParameter('functionCode', 0) as string;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// Execute the function code
|
2022-08-01 13:47:55 -07:00
|
|
|
|
items = await vm.run(`module.exports = async function() {${functionCode}\n}()`, __dirname);
|
2022-03-07 00:54:52 -08:00
|
|
|
|
items = this.helpers.normalizeItems(items);
|
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
|
// Do very basic validation of the data
|
|
|
|
|
if (items === undefined) {
|
2022-08-01 13:47:55 -07:00
|
|
|
|
throw new NodeOperationError(
|
|
|
|
|
this.getNode(),
|
|
|
|
|
'No data got returned. Always return an Array of items!',
|
|
|
|
|
);
|
2019-12-23 12:54:27 -08:00
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
|
if (!Array.isArray(items)) {
|
2022-08-01 13:47:55 -07:00
|
|
|
|
throw new NodeOperationError(
|
|
|
|
|
this.getNode(),
|
|
|
|
|
'Always an Array of items has to be returned!',
|
|
|
|
|
);
|
2019-12-23 12:54:27 -08:00
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
|
for (const item of items) {
|
|
|
|
|
if (item.json === undefined) {
|
2022-08-01 13:47:55 -07:00
|
|
|
|
throw new NodeOperationError(
|
|
|
|
|
this.getNode(),
|
|
|
|
|
'All returned items have to contain a property named "json"!',
|
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
|
}
|
|
|
|
|
if (typeof item.json !== 'object') {
|
|
|
|
|
throw new NodeOperationError(this.getNode(), 'The json-property has to be an object!');
|
2019-12-23 12:54:27 -08:00
|
|
|
|
}
|
2022-01-02 01:33:15 -08:00
|
|
|
|
|
|
|
|
|
item.json = cleanupData(item.json);
|
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
|
if (item.binary !== undefined) {
|
|
|
|
|
if (Array.isArray(item.binary) || typeof item.binary !== 'object') {
|
2022-08-01 13:47:55 -07:00
|
|
|
|
throw new NodeOperationError(
|
|
|
|
|
this.getNode(),
|
|
|
|
|
'The binary-property has to be an object!',
|
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-01 13:47:55 -07:00
|
|
|
|
items = [{ json: { error: error.message } }];
|
2021-07-19 23:58:54 -07:00
|
|
|
|
} else {
|
2021-12-23 12:00:08 -08:00
|
|
|
|
// Try to find the line number which contains the error and attach to error message
|
2021-12-23 06:16:42 -08:00
|
|
|
|
const stackLines = error.stack.split('\n');
|
|
|
|
|
if (stackLines.length > 0) {
|
2022-03-13 01:34:44 -08:00
|
|
|
|
stackLines.shift();
|
|
|
|
|
const lineParts = stackLines.find((line: string) => line.includes('Function')).split(':');
|
2021-12-23 06:16:42 -08:00
|
|
|
|
if (lineParts.length > 2) {
|
|
|
|
|
const lineNumber = lineParts.splice(-2, 1);
|
2023-02-27 19:39:43 -08:00
|
|
|
|
if (!isNaN(lineNumber as number)) {
|
2021-12-23 06:16:42 -08:00
|
|
|
|
error.message = `${error.message} [Line ${lineNumber}]`;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-24 04:17:08 -07:00
|
|
|
|
throw error;
|
2019-12-23 12:54:27 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
|
return this.prepareOutputData(items);
|
|
|
|
|
}
|
|
|
|
|
}
|