2022-10-13 05:28:02 -07:00
|
|
|
|
import { ValidationError } from './ValidationError';
|
2023-05-04 11:00:00 -07:00
|
|
|
|
import { isObject } from './utils';
|
|
|
|
|
|
|
|
|
|
import type { IExecuteFunctions, INodeExecutionData, IWorkflowDataProxyData } from 'n8n-workflow';
|
|
|
|
|
|
|
|
|
|
interface SandboxTextKeys {
|
|
|
|
|
object: {
|
|
|
|
|
singular: string;
|
|
|
|
|
plural: string;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface SandboxContext extends IWorkflowDataProxyData {
|
2023-04-19 04:09:46 -07:00
|
|
|
|
$getNodeParameter: IExecuteFunctions['getNodeParameter'];
|
|
|
|
|
$getWorkflowStaticData: IExecuteFunctions['getWorkflowStaticData'];
|
|
|
|
|
helpers: IExecuteFunctions['helpers'];
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-04 11:00:00 -07:00
|
|
|
|
export const REQUIRED_N8N_ITEM_KEYS = new Set(['json', 'binary', 'pairedItem']);
|
|
|
|
|
|
|
|
|
|
export function getSandboxContext(this: IExecuteFunctions, index: number): SandboxContext {
|
|
|
|
|
return {
|
|
|
|
|
// from NodeExecuteFunctions
|
|
|
|
|
$getNodeParameter: this.getNodeParameter,
|
|
|
|
|
$getWorkflowStaticData: this.getWorkflowStaticData,
|
|
|
|
|
helpers: this.helpers,
|
2022-12-02 12:54:28 -08:00
|
|
|
|
|
2023-05-04 11:00:00 -07:00
|
|
|
|
// to bring in all $-prefixed vars and methods from WorkflowDataProxy
|
|
|
|
|
// $node, $items(), $parameter, $json, $env, etc.
|
|
|
|
|
...this.getWorkflowDataProxy(index),
|
|
|
|
|
};
|
|
|
|
|
}
|
2022-10-13 05:28:02 -07:00
|
|
|
|
|
2023-05-04 11:00:00 -07:00
|
|
|
|
export abstract class Sandbox {
|
2022-10-13 05:28:02 -07:00
|
|
|
|
constructor(
|
2023-05-04 11:00:00 -07:00
|
|
|
|
private textKeys: SandboxTextKeys,
|
|
|
|
|
protected itemIndex: number | undefined,
|
2023-03-09 09:13:15 -08:00
|
|
|
|
private helpers: IExecuteFunctions['helpers'],
|
2023-05-04 11:00:00 -07:00
|
|
|
|
) {}
|
2022-10-13 05:28:02 -07:00
|
|
|
|
|
2023-05-04 11:00:00 -07:00
|
|
|
|
abstract runCodeAllItems(): Promise<INodeExecutionData[]>;
|
2022-10-13 05:28:02 -07:00
|
|
|
|
|
2023-05-04 11:00:00 -07:00
|
|
|
|
abstract runCodeEachItem(): Promise<INodeExecutionData | undefined>;
|
2022-10-13 05:28:02 -07:00
|
|
|
|
|
2023-05-04 11:00:00 -07:00
|
|
|
|
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,
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-10-13 05:28:02 -07:00
|
|
|
|
|
2023-05-04 11:00:00 -07:00
|
|
|
|
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,
|
|
|
|
|
});
|
2022-10-13 05:28:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-04 11:00:00 -07:00
|
|
|
|
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);
|
2022-10-13 05:28:02 -07:00
|
|
|
|
|
2023-05-04 11:00:00 -07:00
|
|
|
|
return returnData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
validateRunCodeAllItems(
|
|
|
|
|
executionResult: INodeExecutionData | INodeExecutionData[] | undefined,
|
|
|
|
|
itemIndex?: number,
|
|
|
|
|
): INodeExecutionData[] {
|
|
|
|
|
if (typeof executionResult !== 'object') {
|
2022-10-13 05:28:02 -07:00
|
|
|
|
throw new ValidationError({
|
|
|
|
|
message: "Code doesn't return items properly",
|
2023-05-04 11:00:00 -07:00
|
|
|
|
description: `Please return an array of ${this.getTextKey('object', {
|
|
|
|
|
plural: true,
|
|
|
|
|
})}, one for each item you would like to output.`,
|
|
|
|
|
itemIndex,
|
2022-10-13 05:28:02 -07:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Array.isArray(executionResult)) {
|
2022-11-22 05:49:13 -08:00
|
|
|
|
/**
|
|
|
|
|
* If at least one top-level key is an n8n item key (`json`, `binary`, etc.),
|
|
|
|
|
* then require all item keys to be an n8n item key.
|
|
|
|
|
*
|
|
|
|
|
* If no top-level key is an n8n key, then skip this check, allowing non-n8n
|
|
|
|
|
* item keys to be wrapped in `json` when normalizing items below.
|
|
|
|
|
*/
|
|
|
|
|
const mustHaveTopLevelN8nKey = executionResult.some((item) =>
|
2023-05-04 11:00:00 -07:00
|
|
|
|
Object.keys(item).find((key) => REQUIRED_N8N_ITEM_KEYS.has(key)),
|
2022-11-22 05:49:13 -08:00
|
|
|
|
);
|
|
|
|
|
|
2023-05-04 11:00:00 -07:00
|
|
|
|
if (mustHaveTopLevelN8nKey) {
|
|
|
|
|
for (const item of executionResult) {
|
2023-04-19 04:09:46 -07:00
|
|
|
|
this.validateTopLevelKeys(item);
|
2022-10-13 05:28:02 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-19 04:09:46 -07:00
|
|
|
|
const returnData = this.helpers.normalizeItems(executionResult);
|
2023-05-04 11:00:00 -07:00
|
|
|
|
returnData.forEach((item) => this.validateItem(item));
|
2023-04-19 04:09:46 -07:00
|
|
|
|
return returnData;
|
2022-10-13 05:28:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-04 11:00:00 -07:00
|
|
|
|
private getTextKey(
|
|
|
|
|
key: keyof SandboxTextKeys,
|
|
|
|
|
options?: { includeArticle?: boolean; plural?: boolean },
|
|
|
|
|
) {
|
|
|
|
|
const response = this.textKeys[key][options?.plural ? 'plural' : 'singular'];
|
|
|
|
|
if (!options?.includeArticle) {
|
|
|
|
|
return response;
|
2022-10-13 05:28:02 -07:00
|
|
|
|
}
|
2023-05-04 11:00:00 -07:00
|
|
|
|
if (['a', 'e', 'i', 'o', 'u'].some((value) => response.startsWith(value))) {
|
|
|
|
|
return `an ${response}`;
|
2023-04-19 04:09:46 -07:00
|
|
|
|
}
|
2023-05-04 11:00:00 -07:00
|
|
|
|
return `a ${response}`;
|
2023-04-19 04:09:46 -07:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-04 11:00:00 -07:00
|
|
|
|
private validateItem({ json, binary }: INodeExecutionData) {
|
2023-04-19 04:09:46 -07:00
|
|
|
|
if (json === undefined || !isObject(json)) {
|
2022-10-13 05:28:02 -07:00
|
|
|
|
throw new ValidationError({
|
2023-05-04 11:00:00 -07:00
|
|
|
|
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 },
|
|
|
|
|
)}.`,
|
2022-10-13 05:28:02 -07:00
|
|
|
|
itemIndex: this.itemIndex,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-19 04:09:46 -07:00
|
|
|
|
if (binary !== undefined && !isObject(binary)) {
|
2022-10-13 05:28:02 -07:00
|
|
|
|
throw new ValidationError({
|
2023-05-04 11:00:00 -07:00
|
|
|
|
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 },
|
|
|
|
|
)}.`,
|
2022-10-13 05:28:02 -07:00
|
|
|
|
itemIndex: this.itemIndex,
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-04-19 04:09:46 -07:00
|
|
|
|
}
|
2022-10-13 05:28:02 -07:00
|
|
|
|
|
2023-04-19 04:09:46 -07:00
|
|
|
|
private validateTopLevelKeys(item: INodeExecutionData) {
|
|
|
|
|
Object.keys(item).forEach((key) => {
|
2022-12-05 01:59:26 -08:00
|
|
|
|
if (REQUIRED_N8N_ITEM_KEYS.has(key)) return;
|
2022-10-13 05:28:02 -07:00
|
|
|
|
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,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|