mirror of
https://github.com/n8n-io/n8n.git
synced 2025-02-02 07:01:30 -08:00
perf(Code Node): Improve n8n item key validation performance (#4669)
⚡ Improve validator performance
This commit is contained in:
parent
7b00d6e731
commit
740513b424
|
@ -2,7 +2,7 @@ import { normalizeItems } from 'n8n-core';
|
||||||
import { NodeVM, NodeVMOptions } from 'vm2';
|
import { NodeVM, NodeVMOptions } from 'vm2';
|
||||||
import { ValidationError } from './ValidationError';
|
import { ValidationError } from './ValidationError';
|
||||||
import { ExecutionError } from './ExecutionError';
|
import { ExecutionError } from './ExecutionError';
|
||||||
import { CodeNodeMode, isObject, SUPPORTED_ITEM_KEYS } from './utils';
|
import { CodeNodeMode, isObject, N8N_ITEM_KEYS } from './utils';
|
||||||
|
|
||||||
import type { IExecuteFunctions, IWorkflowDataProxyData, WorkflowExecuteMode } from 'n8n-workflow';
|
import type { IExecuteFunctions, IWorkflowDataProxyData, WorkflowExecuteMode } from 'n8n-workflow';
|
||||||
|
|
||||||
|
@ -95,6 +95,17 @@ export class Sandbox extends NodeVM {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(executionResult)) {
|
if (Array.isArray(executionResult)) {
|
||||||
|
/**
|
||||||
|
* 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) =>
|
||||||
|
Object.keys(item).find((key) => N8N_ITEM_KEYS.has(key)),
|
||||||
|
);
|
||||||
|
|
||||||
for (const item of executionResult) {
|
for (const item of executionResult) {
|
||||||
if (item.json !== undefined && !isObject(item.json)) {
|
if (item.json !== undefined && !isObject(item.json)) {
|
||||||
throw new ValidationError({
|
throw new ValidationError({
|
||||||
|
@ -104,18 +115,9 @@ export class Sandbox extends NodeVM {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// If at least one top-level key is a supported item key (`json`, `binary`, etc.),
|
if (mustHaveTopLevelN8nKey) {
|
||||||
// then validate all keys to be a supported item key, else allow user keys
|
|
||||||
// to be wrapped in `json` when normalizing items below.
|
|
||||||
|
|
||||||
if (
|
|
||||||
executionResult.some((item) =>
|
|
||||||
Object.keys(item).find((key) => SUPPORTED_ITEM_KEYS.has(key)),
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
Object.keys(item).forEach((key) => {
|
Object.keys(item).forEach((key) => {
|
||||||
if (SUPPORTED_ITEM_KEYS.has(key)) return;
|
if (N8N_ITEM_KEYS.has(key)) return;
|
||||||
|
|
||||||
throw new ValidationError({
|
throw new ValidationError({
|
||||||
message: `Unknown top-level item key: ${key}`,
|
message: `Unknown top-level item key: ${key}`,
|
||||||
description: 'Access the properties of an item under `.json`, e.g. `item.json`',
|
description: 'Access the properties of an item under `.json`, e.g. `item.json`',
|
||||||
|
@ -223,7 +225,7 @@ export class Sandbox extends NodeVM {
|
||||||
// directly on the item, when they intended to add it on the `json` property
|
// directly on the item, when they intended to add it on the `json` property
|
||||||
|
|
||||||
Object.keys(executionResult).forEach((key) => {
|
Object.keys(executionResult).forEach((key) => {
|
||||||
if (SUPPORTED_ITEM_KEYS.has(key)) return;
|
if (N8N_ITEM_KEYS.has(key)) return;
|
||||||
|
|
||||||
throw new ValidationError({
|
throw new ValidationError({
|
||||||
message: `Unknown top-level item key: ${key}`,
|
message: `Unknown top-level item key: ${key}`,
|
||||||
|
|
|
@ -26,4 +26,4 @@ function isTraversable(maybe: unknown): maybe is IDataObject {
|
||||||
|
|
||||||
export type CodeNodeMode = 'runOnceForAllItems' | 'runOnceForEachItem';
|
export type CodeNodeMode = 'runOnceForAllItems' | 'runOnceForEachItem';
|
||||||
|
|
||||||
export const SUPPORTED_ITEM_KEYS = new Set(['json', 'binary', 'error', 'pairedItem', 'index']);
|
export const N8N_ITEM_KEYS = new Set(['json', 'binary', 'error', 'pairedItem', 'index']);
|
||||||
|
|
Loading…
Reference in a new issue