Skip to content

Commit

Permalink
refactor(Code Node): Constently handle various kinds of data returned…
Browse files Browse the repository at this point in the history
… by user code (#6002)
  • Loading branch information
netroy authored Apr 19, 2023
1 parent fe058aa commit f9b3aea
Show file tree
Hide file tree
Showing 6 changed files with 293 additions and 204 deletions.
28 changes: 14 additions & 14 deletions packages/nodes-base/nodes/Code/Code.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ export class Code implements INodeType {
};

async execute(this: IExecuteFunctions) {
let items = this.getInputData();

const nodeMode = this.getNodeParameter('mode', 0) as CodeNodeMode;
const workflowMode = this.getMode();

Expand All @@ -80,24 +78,25 @@ export class Code implements INodeType {

const context = getSandboxContext.call(this);
context.items = context.$input.all();
const sandbox = new Sandbox(context, workflowMode, nodeMode, this.helpers);
const sandbox = new Sandbox(context, jsCodeAllItems, workflowMode, this.helpers);

if (workflowMode === 'manual') {
sandbox.on('console.log', this.sendMessageToUI);
}

let result: INodeExecutionData[];
try {
items = await sandbox.runCode(jsCodeAllItems);
result = await sandbox.runCodeAllItems();
} catch (error) {
if (!this.continueOnFail()) return Promise.reject(error);
items = [{ json: { error: error.message } }];
result = [{ json: { error: error.message } }];
}

for (const item of items) {
for (const item of result) {
standardizeOutput(item.json);
}

return this.prepareOutputData(items);
return this.prepareOutputData(result);
}

// ----------------------------------
Expand All @@ -106,31 +105,32 @@ export class Code implements INodeType {

const returnData: INodeExecutionData[] = [];

for (let index = 0; index < items.length; index++) {
let item = items[index];
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, workflowMode, nodeMode, this.helpers);
const sandbox = new Sandbox(context, jsCodeEachItem, workflowMode, this.helpers);

if (workflowMode === 'manual') {
sandbox.on('console.log', this.sendMessageToUI);
}

let result: INodeExecutionData | undefined;
try {
item = await sandbox.runCode(jsCodeEachItem, index);
result = await sandbox.runCodeEachItem(index);
} catch (error) {
if (!this.continueOnFail()) return Promise.reject(error);
returnData.push({ json: { error: error.message } });
}

if (item) {
if (result) {
returnData.push({
json: standardizeOutput(item.json),
json: standardizeOutput(result.json),
pairedItem: { item: index },
...(item.binary && { binary: item.binary }),
...(result.binary && { binary: result.binary }),
});
}
}
Expand Down
160 changes: 55 additions & 105 deletions packages/nodes-base/nodes/Code/Sandbox.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { NodeVMOptions } from 'vm2';
import { NodeVM } from 'vm2';
import { ValidationError } from './ValidationError';
import { ExecutionError } from './ExecutionError';
import type { CodeNodeMode } from './utils';
import { isObject, REQUIRED_N8N_ITEM_KEYS } from './utils';

import type {
Expand All @@ -13,48 +11,38 @@ import type {
WorkflowExecuteMode,
} from 'n8n-workflow';

export class Sandbox extends NodeVM {
private jsCode = '';
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 class Sandbox extends NodeVM {
private itemIndex: number | undefined = undefined;

constructor(
context: ReturnType<typeof getSandboxContext>,
context: SandboxContext,
private jsCode: string,
workflowMode: WorkflowExecuteMode,
private nodeMode: CodeNodeMode,
private helpers: IExecuteFunctions['helpers'],
) {
super(Sandbox.getSandboxOptions(context, workflowMode));
}

static getSandboxOptions(
context: ReturnType<typeof getSandboxContext>,
workflowMode: WorkflowExecuteMode,
): NodeVMOptions {
const { NODE_FUNCTION_ALLOW_BUILTIN: builtIn, NODE_FUNCTION_ALLOW_EXTERNAL: external } =
process.env;

return {
super({
console: workflowMode === 'manual' ? 'redirect' : 'inherit',
sandbox: context,
require: {
builtin: builtIn ? builtIn.split(',') : [],
external: external ? { modules: external.split(','), transitive: false } : false,
},
};
}

async runCode(jsCode: string, itemIndex?: number) {
this.jsCode = jsCode;
this.itemIndex = itemIndex;

return this.nodeMode === 'runOnceForAllItems' ? this.runCodeAllItems() : this.runCodeEachItem();
});
}

private async runCodeAllItems() {
async runCodeAllItems(): Promise<INodeExecutionData[]> {
const script = `module.exports = async function() {${this.jsCode}\n}()`;

let executionResult;
let executionResult: INodeExecutionData | INodeExecutionData[];

try {
executionResult = await this.run(script, __dirname);
Expand Down Expand Up @@ -93,55 +81,19 @@ export class Sandbox extends NodeVM {
);

for (const item of executionResult) {
if (item.json !== undefined && !isObject(item.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",
itemIndex: this.itemIndex,
});
}

if (mustHaveTopLevelN8nKey) {
Object.keys(item as IDataObject).forEach((key) => {
if (REQUIRED_N8N_ITEM_KEYS.has(key)) return;
throw new ValidationError({
message: `Unknown top-level item key: ${key}`,
description: 'Access the properties of an item under `.json`, e.g. `item.json`',
itemIndex: this.itemIndex,
});
});
}

if (item.binary !== undefined && !isObject(item.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.",
itemIndex: this.itemIndex,
});
this.validateTopLevelKeys(item);
}
}
} else {
if (executionResult.json !== undefined && !isObject(executionResult.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",
itemIndex: this.itemIndex,
});
}

if (executionResult.binary !== undefined && !isObject(executionResult.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.",
itemIndex: this.itemIndex,
});
}
}

return this.helpers.normalizeItems(executionResult as INodeExecutionData[]);
const returnData = this.helpers.normalizeItems(executionResult);
returnData.forEach((item) => this.validateResult(item));
return returnData;
}

private async runCodeEachItem() {
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)/);
Expand All @@ -166,7 +118,7 @@ export class Sandbox extends NodeVM {
}
}

let executionResult;
let executionResult: INodeExecutionData;

try {
executionResult = await this.run(script, __dirname);
Expand All @@ -191,70 +143,68 @@ export class Sandbox extends NodeVM {
});
}

if (executionResult.json !== undefined && !isObject(executionResult.json)) {
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;
}

private validateResult({ 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",
itemIndex: this.itemIndex,
});
}

if (executionResult.binary !== undefined && !isObject(executionResult.binary)) {
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.",
itemIndex: this.itemIndex,
});
}
}

// 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

Object.keys(executionResult as IDataObject).forEach((key) => {
private validateTopLevelKeys(item: INodeExecutionData) {
Object.keys(item).forEach((key) => {
if (REQUIRED_N8N_ITEM_KEYS.has(key)) return;

throw new ValidationError({
message: `Unknown top-level item key: ${key}`,
description: 'Access the properties of an item under `.json`, e.g. `item.json`',
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,
});
}

return executionResult.json ? executionResult : { json: executionResult };
}
}

export function getSandboxContext(this: IExecuteFunctions, index?: number) {
const sandboxContext: Record<string, unknown> & {
$item: (i: number) => IWorkflowDataProxyData;
$input: any;
} = {
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
$item: this.getWorkflowDataProxy,
$input: null,
...this.getWorkflowDataProxy(index ?? 0),
};

// $node, $items(), $parameter, $json, $env, etc.
Object.assign(sandboxContext, sandboxContext.$item(index ?? 0));

return sandboxContext;
}
Loading

0 comments on commit f9b3aea

Please sign in to comment.