2019-06-23 03:35:23 -07:00
|
|
|
import { IExecuteFunctions } from 'n8n-core';
|
|
|
|
import {
|
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
2021-04-16 09:33:36 -07:00
|
|
|
NodeOperationError,
|
2019-06-23 03:35:23 -07:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
const { NodeVM } = require('vm2');
|
|
|
|
|
|
|
|
export class Function implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Function',
|
|
|
|
name: 'function',
|
|
|
|
icon: 'fa:code',
|
|
|
|
group: ['transform'],
|
|
|
|
version: 1,
|
2021-07-03 05:40:16 -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: [
|
|
|
|
{
|
2020-10-13 02:05:56 -07:00
|
|
|
displayName: 'JavaScript Code',
|
2019-06-23 03:35:23 -07:00
|
|
|
name: 'functionCode',
|
|
|
|
typeOptions: {
|
|
|
|
alwaysOpenEditWindow: true,
|
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.
|
|
|
|
// More info and help: https://docs.n8n.io/nodes/n8n-nodes-base.function
|
|
|
|
|
|
|
|
// 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;`,
|
2019-06-23 03:35:23 -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
|
|
|
|
items = JSON.parse(JSON.stringify(items));
|
|
|
|
|
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),
|
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();
|
|
|
|
|
2019-12-13 09:23:30 -08:00
|
|
|
const options = {
|
2021-05-29 11:41:25 -07:00
|
|
|
console: (mode === 'manual') ? 'redirect' : 'inherit',
|
2019-06-23 03:35:23 -07:00
|
|
|
sandbox,
|
|
|
|
require: {
|
2019-12-26 18:49:48 -08:00
|
|
|
external: false as boolean | { modules: string[] },
|
2019-12-13 09:23:30 -08:00
|
|
|
builtin: [] as string[],
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2019-12-13 09:23:30 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
if (process.env.NODE_FUNCTION_ALLOW_BUILTIN) {
|
|
|
|
options.require.builtin = process.env.NODE_FUNCTION_ALLOW_BUILTIN.split(',');
|
|
|
|
}
|
|
|
|
|
2019-12-26 18:49:48 -08:00
|
|
|
if (process.env.NODE_FUNCTION_ALLOW_EXTERNAL) {
|
|
|
|
options.require.external = { modules: process.env.NODE_FUNCTION_ALLOW_EXTERNAL.split(',') };
|
|
|
|
}
|
|
|
|
|
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
|
2020-01-25 13:21:59 -08:00
|
|
|
items = (await vm.run(`module.exports = async function() {${functionCode}}()`, __dirname));
|
2021-07-19 23:58:54 -07:00
|
|
|
// Do very basic validation of the data
|
|
|
|
if (items === undefined) {
|
|
|
|
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)) {
|
|
|
|
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) {
|
|
|
|
throw new NodeOperationError(this.getNode(), 'All returned items have to contain a property named "json"!');
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (item.binary !== undefined) {
|
|
|
|
if (Array.isArray(item.binary) || typeof item.binary !== 'object') {
|
|
|
|
throw new NodeOperationError(this.getNode(), 'The binary-property has to be an object!');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
|
|
|
items=[{json:{ error: error.message }}];
|
|
|
|
} else {
|
|
|
|
return Promise.reject(error);
|
2019-12-23 12:54:27 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
return this.prepareOutputData(items);
|
|
|
|
}
|
|
|
|
}
|