feat(Code Node): Add Python support (#4295)

This commit is contained in:
Jan Oberhauser 2023-05-04 20:00:00 +02:00 committed by GitHub
parent 1e6a75f341
commit 35c8510ab6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 962 additions and 591 deletions

View file

@ -30,6 +30,7 @@
"@codemirror/commands": "^6.1.0",
"@codemirror/lang-javascript": "^6.1.2",
"@codemirror/lang-json": "^6.0.1",
"@codemirror/lang-python": "^6.1.2",
"@codemirror/lang-sql": "^6.4.1",
"@codemirror/language": "^6.2.1",
"@codemirror/lint": "^6.0.0",

View file

@ -1,6 +1,6 @@
<template>
<div
:class="['code-node-editor', $style['code-node-editor-container']]"
:class="['code-node-editor', $style['code-node-editor-container'], language]"
@mouseover="onMouseOver"
@mouseout="onMouseOut"
ref="codeNodeEditorContainer"
@ -23,50 +23,42 @@ import type { PropType } from 'vue';
import { mapStores } from 'pinia';
import mixins from 'vue-typed-mixins';
import type { LanguageSupport } from '@codemirror/language';
import type { Extension } from '@codemirror/state';
import { Compartment, EditorState } from '@codemirror/state';
import type { ViewUpdate } from '@codemirror/view';
import { EditorView } from '@codemirror/view';
import { javascript } from '@codemirror/lang-javascript';
import { json } from '@codemirror/lang-json';
import { python } from '@codemirror/lang-python';
import type { CodeExecutionMode, CodeNodeEditorLanguage } from 'n8n-workflow';
import { CODE_EXECUTION_MODES, CODE_LANGUAGES } from 'n8n-workflow';
import { readOnlyEditorExtensions, writableEditorExtensions } from './baseExtensions';
import { linterExtension } from './linter';
import { completerExtension } from './completer';
import { codeNodeEditorTheme } from './theme';
import { workflowHelpers } from '@/mixins/workflowHelpers'; // for json field completions
import { ASK_AI_MODAL_KEY, CODE_NODE_TYPE } from '@/constants';
import { codeNodeEditorEventBus } from '@/event-bus';
import {
ALL_ITEMS_PLACEHOLDER,
CODE_LANGUAGES,
CODE_MODES,
EACH_ITEM_PLACEHOLDER,
} from './constants';
import { useRootStore } from '@/stores/n8nRootStore';
import Modal from '../Modal.vue';
import { useSettingsStore } from '@/stores/settings';
import type { CodeLanguage, CodeMode } from './types';
import Modal from '@/components/Modal.vue';
const placeholders: Partial<Record<CodeLanguage, Record<CodeMode, string>>> = {
javaScript: {
runOnceForAllItems: ALL_ITEMS_PLACEHOLDER,
runOnceForEachItem: EACH_ITEM_PLACEHOLDER,
},
};
import { readOnlyEditorExtensions, writableEditorExtensions } from './baseExtensions';
import { CODE_PLACEHOLDERS } from './constants';
import { linterExtension } from './linter';
import { completerExtension } from './completer';
import { codeNodeEditorTheme } from './theme';
export default mixins(linterExtension, completerExtension, workflowHelpers).extend({
name: 'code-node-editor',
components: { Modal },
props: {
mode: {
type: String as PropType<CodeMode>,
validator: (value: CodeMode): boolean => CODE_MODES.includes(value),
type: String as PropType<CodeExecutionMode>,
validator: (value: CodeExecutionMode): boolean => CODE_EXECUTION_MODES.includes(value),
},
language: {
type: String as PropType<CodeLanguage>,
default: 'javaScript' as CodeLanguage,
validator: (value: CodeLanguage): boolean => CODE_LANGUAGES.includes(value),
type: String as PropType<CodeNodeEditorLanguage>,
default: 'javaScript' as CodeNodeEditorLanguage,
validator: (value: CodeNodeEditorLanguage): boolean => CODE_LANGUAGES.includes(value),
},
isReadOnly: {
type: Boolean,
@ -79,19 +71,30 @@ export default mixins(linterExtension, completerExtension, workflowHelpers).exte
data() {
return {
editor: null as EditorView | null,
languageCompartment: new Compartment(),
linterCompartment: new Compartment(),
isEditorHovered: false,
isEditorFocused: false,
};
},
watch: {
mode(newMode, previousMode: CodeMode) {
mode(newMode, previousMode: CodeExecutionMode) {
this.reloadLinter();
if (this.content.trim() === placeholders[this.language]?.[previousMode]) {
if (this.content.trim() === CODE_PLACEHOLDERS[this.language]?.[previousMode]) {
this.refreshPlaceholder();
}
},
language(newLanguage, previousLanguage: CodeNodeEditorLanguage) {
if (this.content.trim() === CODE_PLACEHOLDERS[previousLanguage]?.[this.mode]) {
this.refreshPlaceholder();
}
const [languageSupport] = this.languageExtensions;
this.editor?.dispatch({
effects: this.languageCompartment.reconfigure(languageSupport),
});
},
},
computed: {
...mapStores(useRootStore),
@ -104,7 +107,17 @@ export default mixins(linterExtension, completerExtension, workflowHelpers).exte
return this.editor.state.doc.toString();
},
placeholder(): string {
return placeholders[this.language]?.[this.mode] ?? '';
return CODE_PLACEHOLDERS[this.language]?.[this.mode] ?? '';
},
languageExtensions(): [LanguageSupport, ...Extension[]] {
switch (this.language) {
case 'json':
return [json()];
case 'javaScript':
return [javascript(), this.autocompletionExtension('javaScript')];
case 'python':
return [python(), this.autocompletionExtension('python')];
}
},
},
methods: {
@ -178,6 +191,7 @@ export default mixins(linterExtension, completerExtension, workflowHelpers).exte
insertedText = full.slice(lastDotIndex + 1);
}
// TODO: Still has to get updated for Python and JSON
this.$telemetry.track('User autocompleted code', {
instance_id: this.rootStore.instanceId,
node_type: CODE_NODE_TYPE,
@ -234,14 +248,8 @@ export default mixins(linterExtension, completerExtension, workflowHelpers).exte
);
}
switch (language) {
case 'json':
extensions.push(json());
break;
case 'javaScript':
extensions.push(javascript(), this.autocompletionExtension());
break;
}
const [languageSupport, ...otherExtensions] = this.languageExtensions;
extensions.push(this.languageCompartment.of(languageSupport), ...otherExtensions);
const state = EditorState.create({
doc: this.value || this.placeholder,

View file

@ -33,7 +33,12 @@ export const completerExtension = mixins(
jsonFieldCompletions,
).extend({
methods: {
autocompletionExtension(): Extension {
autocompletionExtension(language: 'javaScript' | 'python'): Extension {
const completions = [];
if (language === 'javaScript') {
completions.push(jsSnippets, localCompletionSource);
}
return autocompletion({
compareCompletions: (a: Completion, b: Completion) => {
if (/\.json$|id$|id['"]\]$/.test(a.label)) return 0;
@ -41,8 +46,7 @@ export const completerExtension = mixins(
return a.label.localeCompare(b.label);
},
override: [
jsSnippets,
localCompletionSource,
...completions,
// core
this.itemCompletions,

View file

@ -7,7 +7,7 @@ import type { CodeNodeEditorMixin } from '../types';
import { mapStores } from 'pinia';
import { useWorkflowsStore } from '@/stores/workflows';
function getAutocompletableNodeNames(nodes: INodeUi[]) {
function getAutoCompletableNodeNames(nodes: INodeUi[]) {
return nodes
.filter((node: INodeUi) => !NODE_TYPES_EXCLUDED_FROM_AUTOCOMPLETION.includes(node.type))
.map((node: INodeUi) => node.name);
@ -49,54 +49,55 @@ export const baseCompletions = (Vue as CodeNodeEditorMixin).extend({
* - Complete `$` to `$json $binary $itemIndex` in single-item mode.
*/
baseCompletions(context: CompletionContext): CompletionResult | null {
const preCursor = context.matchBefore(/\$\w*/);
const prefix = this.language === 'python' ? '_' : '$';
const preCursor = context.matchBefore(new RegExp(`\\${prefix}\\w*`));
if (!preCursor || (preCursor.from === preCursor.to && !context.explicit)) return null;
const TOP_LEVEL_COMPLETIONS_IN_BOTH_MODES: Completion[] = [
{
label: '$execution',
label: `${prefix}execution`,
info: this.$locale.baseText('codeNodeEditor.completer.$execution'),
},
{ label: '$input', info: this.$locale.baseText('codeNodeEditor.completer.$input') },
{ label: `${prefix}input`, info: this.$locale.baseText('codeNodeEditor.completer.$input') },
{
label: '$prevNode',
label: `${prefix}prevNode`,
info: this.$locale.baseText('codeNodeEditor.completer.$prevNode'),
},
{
label: '$workflow',
label: `${prefix}workflow`,
info: this.$locale.baseText('codeNodeEditor.completer.$workflow'),
},
{
label: '$vars',
label: `${prefix}vars`,
info: this.$locale.baseText('codeNodeEditor.completer.$vars'),
},
{
label: '$now',
label: `${prefix}now`,
info: this.$locale.baseText('codeNodeEditor.completer.$now'),
},
{
label: '$today',
label: `${prefix}today`,
info: this.$locale.baseText('codeNodeEditor.completer.$today'),
},
{
label: '$jmespath()',
label: `${prefix}jmespath()`,
info: this.$locale.baseText('codeNodeEditor.completer.$jmespath'),
},
{
label: '$if()',
label: `${prefix}if()`,
info: this.$locale.baseText('codeNodeEditor.completer.$if'),
},
{
label: '$min()',
label: `${prefix}min()`,
info: this.$locale.baseText('codeNodeEditor.completer.$min'),
},
{
label: '$max()',
label: `${prefix}max()`,
info: this.$locale.baseText('codeNodeEditor.completer.$max'),
},
{
label: '$runIndex',
label: `${prefix}runIndex`,
info: this.$locale.baseText('codeNodeEditor.completer.$runIndex'),
},
];
@ -104,9 +105,9 @@ export const baseCompletions = (Vue as CodeNodeEditorMixin).extend({
const options: Completion[] = TOP_LEVEL_COMPLETIONS_IN_BOTH_MODES.map(addVarType);
options.push(
...getAutocompletableNodeNames(this.workflowsStore.allNodes).map((nodeName) => {
...getAutoCompletableNodeNames(this.workflowsStore.allNodes).map((nodeName) => {
return {
label: `$('${nodeName}')`,
label: `${prefix}('${nodeName}')`,
type: 'variable',
info: this.$locale.baseText('codeNodeEditor.completer.$()', {
interpolate: { nodeName },
@ -117,10 +118,10 @@ export const baseCompletions = (Vue as CodeNodeEditorMixin).extend({
if (this.mode === 'runOnceForEachItem') {
const TOP_LEVEL_COMPLETIONS_IN_SINGLE_ITEM_MODE = [
{ label: '$json' },
{ label: '$binary' },
{ label: `${prefix}json` },
{ label: `${prefix}binary` },
{
label: '$itemIndex',
label: `${prefix}itemIndex`,
info: this.$locale.baseText('codeNodeEditor.completer.$itemIndex'),
},
];
@ -138,14 +139,15 @@ export const baseCompletions = (Vue as CodeNodeEditorMixin).extend({
* Complete `$(` to `$('nodeName')`.
*/
nodeSelectorCompletions(context: CompletionContext): CompletionResult | null {
const preCursor = context.matchBefore(/\$\(.*/);
const prefix = this.language === 'python' ? '_' : '$';
const preCursor = context.matchBefore(new RegExp(`\\${prefix}\\(.*`));
if (!preCursor || (preCursor.from === preCursor.to && !context.explicit)) return null;
const options: Completion[] = getAutocompletableNodeNames(this.workflowsStore.allNodes).map(
const options: Completion[] = getAutoCompletableNodeNames(this.workflowsStore.allNodes).map(
(nodeName) => {
return {
label: `$('${nodeName}')`,
label: `${prefix}('${nodeName}')`,
type: 'variable',
info: this.$locale.baseText('codeNodeEditor.completer.$()', {
interpolate: { nodeName },

View file

@ -50,10 +50,11 @@ export const itemFieldCompletions = (Vue as CodeNodeEditorMixin).extend({
* - Complete `$input.item.` to `.json .binary`.
*/
inputMethodCompletions(context: CompletionContext): CompletionResult | null {
const prefix = this.language === 'python' ? '_' : '$';
const patterns = {
first: /\$input\.first\(\)\..*/,
last: /\$input\.last\(\)\..*/,
item: /\$input\.item\..*/,
first: new RegExp(`\\${prefix}input\\.first\\(\\)\\..*`),
last: new RegExp(`\\${prefix}input\\.last\\(\\)\\..*`),
item: new RegExp(`\\${prefix}item\\.first\\(\\)\\..*`),
all: /\$input\.all\(\)\[(?<index>\w+)\]\..*/,
};
@ -64,11 +65,11 @@ export const itemFieldCompletions = (Vue as CodeNodeEditorMixin).extend({
let replacementBase = '';
if (name === 'item') replacementBase = '$input.item';
if (name === 'item') replacementBase = `${prefix}input.item`;
if (name === 'first') replacementBase = '$input.first()';
if (name === 'first') replacementBase = `${prefix}input.first()`;
if (name === 'last') replacementBase = '$input.last()';
if (name === 'last') replacementBase = `${prefix}input.last()`;
if (name === 'all') {
const match = preCursor.text.match(regex);
@ -77,7 +78,7 @@ export const itemFieldCompletions = (Vue as CodeNodeEditorMixin).extend({
const { index } = match.groups;
replacementBase = `$input.all()[${index}]`;
replacementBase = `${prefix}input.all()[${index}]`;
}
const options: Completion[] = [

View file

@ -1,5 +1,5 @@
import Vue from 'vue';
import { AUTOCOMPLETABLE_BUILT_IN_MODULES } from '../constants';
import { AUTOCOMPLETABLE_BUILT_IN_MODULES_JS } from '../constants';
import type { Completion, CompletionContext, CompletionResult } from '@codemirror/autocomplete';
import type { CodeNodeEditorMixin } from '../types';
import { useSettingsStore } from '@/stores/settings';
@ -25,7 +25,7 @@ export const requireCompletions = (Vue as CodeNodeEditorMixin).extend({
if (allowedModules.builtIn) {
if (allowedModules.builtIn.includes('*')) {
options.push(...AUTOCOMPLETABLE_BUILT_IN_MODULES.map(toOption));
options.push(...AUTOCOMPLETABLE_BUILT_IN_MODULES_JS.map(toOption));
} else if (allowedModules?.builtIn?.length > 0) {
options.push(...allowedModules.builtIn.map(toOption));
}

View file

@ -1,9 +1,10 @@
import { STICKY_NODE_TYPE } from '@/constants';
import type { Diagnostic } from '@codemirror/lint';
import type { CodeExecutionMode, CodeNodeEditorLanguage } from 'n8n-workflow';
export const NODE_TYPES_EXCLUDED_FROM_AUTOCOMPLETION = [STICKY_NODE_TYPE];
export const AUTOCOMPLETABLE_BUILT_IN_MODULES = [
export const AUTOCOMPLETABLE_BUILT_IN_MODULES_JS = [
'console',
'constants',
'crypto',
@ -34,23 +35,32 @@ export const DEFAULT_LINTER_DELAY_IN_MS = 300;
*/
export const OFFSET_FOR_SCRIPT_WRAPPER = 'module.exports = async function() {'.length;
export const ALL_ITEMS_PLACEHOLDER = `
// Loop over input items and add a new field
// called 'myNewField' to the JSON of each one
export const CODE_PLACEHOLDERS: Partial<
Record<CodeNodeEditorLanguage, Record<CodeExecutionMode, string>>
> = {
javaScript: {
runOnceForAllItems: `
// Loop over input items and add a new field called 'myNewField' to the JSON of each one
for (const item of $input.all()) {
item.json.myNewField = 1;
}
return $input.all();
`.trim();
export const EACH_ITEM_PLACEHOLDER = `
// Add a new field called 'myNewField' to the
// JSON of the item
return $input.all();`.trim(),
runOnceForEachItem: `
// Add a new field called 'myNewField' to the JSON of the item
$input.item.json.myNewField = 1;
return $input.item;
`.trim();
export const CODE_LANGUAGES = ['javaScript', 'json'] as const;
export const CODE_MODES = ['runOnceForAllItems', 'runOnceForEachItem'] as const;
return $input.item;`.trim(),
},
python: {
runOnceForAllItems: `
# Loop over input items and add a new field called 'myNewField' to the JSON of each one
for item in _input.all():
item.json.myNewField = 1
return _input.all()`.trim(),
runOnceForEachItem: `
# Add a new field called 'myNewField' to the JSON of the item
_input.item.json.myNewField = 1
return _input.item`.trim(),
},
};

View file

@ -2,22 +2,19 @@ import Vue from 'vue';
import type { Diagnostic } from '@codemirror/lint';
import { linter as createLinter } from '@codemirror/lint';
import { jsonParseLinter } from '@codemirror/lang-json';
import * as esprima from 'esprima-next';
import {
DEFAULT_LINTER_DELAY_IN_MS,
DEFAULT_LINTER_SEVERITY,
OFFSET_FOR_SCRIPT_WRAPPER,
} from './constants';
import { walk } from './utils';
import type { EditorView } from '@codemirror/view';
import * as esprima from 'esprima-next';
import type { Node } from 'estree';
import type { CodeLanguage, CodeNodeEditorMixin, RangeNode } from './types';
import type { CodeNodeEditorLanguage } from 'n8n-workflow';
import { DEFAULT_LINTER_DELAY_IN_MS, DEFAULT_LINTER_SEVERITY } from './constants';
import { OFFSET_FOR_SCRIPT_WRAPPER } from './constants';
import { walk } from './utils';
import type { CodeNodeEditorMixin, RangeNode } from './types';
export const linterExtension = (Vue as CodeNodeEditorMixin).extend({
methods: {
createLinter(language: CodeLanguage) {
createLinter(language: CodeNodeEditorLanguage) {
switch (language) {
case 'javaScript':
return createLinter(this.lintSource, { delay: DEFAULT_LINTER_DELAY_IN_MS });

View file

@ -1,19 +1,16 @@
import type { EditorView } from '@codemirror/view';
import type { I18nClass } from '@/plugins/i18n';
import type { Workflow } from 'n8n-workflow';
import type { Workflow, CodeExecutionMode, CodeNodeEditorLanguage } from 'n8n-workflow';
import type { Node } from 'estree';
import type { CODE_LANGUAGES, CODE_MODES } from './constants';
export type CodeNodeEditorMixin = Vue.VueConstructor<
Vue & {
$locale: I18nClass;
editor: EditorView | null;
mode: 'runOnceForAllItems' | 'runOnceForEachItem';
mode: CodeExecutionMode;
language: CodeNodeEditorLanguage;
getCurrentWorkflow(): Workflow;
}
>;
export type RangeNode = Node & { range: [number, number] };
export type CodeLanguage = (typeof CODE_LANGUAGES)[number];
export type CodeMode = (typeof CODE_MODES)[number];

View file

@ -744,7 +744,7 @@ export default mixins(
},
editorLanguage(): CodeNodeEditorLanguage {
if (this.editorType === 'json' || this.parameter.type === 'json') return 'json';
return 'javaScript';
return (this.getArgument('editorLanguage') as CodeNodeEditorLanguage) ?? 'javaScript';
},
parameterOptions():
| Array<INodePropertyOptions | INodeProperties | INodePropertyCollection>

View file

@ -1672,13 +1672,19 @@ export default mixins(
},
async getNewNodeWithDefaultCredential(nodeTypeData: INodeTypeDescription) {
let nodeVersion = nodeTypeData.defaultVersion;
if (nodeVersion === undefined) {
nodeVersion = Array.isArray(nodeTypeData.version)
? nodeTypeData.version.slice(-1)[0]
: nodeTypeData.version;
}
const newNodeData: INodeUi = {
id: uuid(),
name: nodeTypeData.defaults.name as string,
type: nodeTypeData.name,
typeVersion: Array.isArray(nodeTypeData.version)
? nodeTypeData.version.slice(-1)[0]
: nodeTypeData.version,
typeVersion: nodeVersion,
position: [0, 0],
parameters: {},
};

View file

@ -11,7 +11,7 @@
}
]
},
"alias": ["cpde", "Javascript", "JS", "Script", "Custom Code", "Function"],
"alias": ["cpde", "Javascript", "JS", "Python", "Script", "Custom Code", "Function"],
"subcategories": {
"Core Nodes": ["Data Transformation"]
}

View file

@ -1,12 +1,17 @@
import type {
CodeExecutionMode,
CodeNodeEditorLanguage,
IExecuteFunctions,
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import { getSandboxContext, Sandbox } from './Sandbox';
import { javascriptCodeDescription } from './descriptions/JavascriptCodeDescription';
import { pythonCodeDescription } from './descriptions/PythonCodeDescription';
import { JavaScriptSandbox } from './JavaScriptSandbox';
import { PythonSandbox } from './PythonSandbox';
import { getSandboxContext } from './Sandbox';
import { standardizeOutput } from './utils';
import type { CodeNodeMode } from './utils';
export class Code implements INodeType {
description: INodeTypeDescription = {
@ -14,7 +19,8 @@ export class Code implements INodeType {
name: 'code',
icon: 'fa:code',
group: ['transform'],
version: 1,
version: [1, 2],
defaultVersion: 1,
description: 'Run custom JavaScript code',
defaults: {
name: 'Code',
@ -44,59 +50,78 @@ export class Code implements INodeType {
default: 'runOnceForAllItems',
},
{
displayName: 'JavaScript',
name: 'jsCode',
typeOptions: {
editor: 'codeNodeEditor',
},
type: 'string',
default: '', // set by component
description:
'JavaScript code to execute.<br><br>Tip: You can use luxon vars like <code>$today</code> for dates and <code>$jmespath</code> for querying JSON structures. <a href="https://docs.n8n.io/nodes/n8n-nodes-base.function">Learn more</a>.',
displayName: 'Language',
name: 'language',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
'@version': [2],
},
},
options: [
{
name: 'JavaScript',
value: 'javaScript',
},
{
name: 'Python (Beta)',
value: 'python',
},
],
default: 'javaScript',
},
{
displayName:
'Type <code>$</code> for a list of <a target="_blank" href="https://docs.n8n.io/code-examples/methods-variables-reference/">special vars/methods</a>. Debug by using <code>console.log()</code> statements and viewing their output in the browser console.',
name: 'notice',
type: 'notice',
default: '',
},
...javascriptCodeDescription,
...pythonCodeDescription,
],
};
async execute(this: IExecuteFunctions) {
const nodeMode = this.getNodeParameter('mode', 0) as CodeNodeMode;
const nodeMode = this.getNodeParameter<CodeExecutionMode>('mode', 0);
const workflowMode = this.getMode();
const language: CodeNodeEditorLanguage =
this.getNode()?.typeVersion === 2 ? this.getNodeParameter('language', 0) : 'javaScript';
const codeParameterName = language === 'python' ? 'pythonCode' : 'jsCode';
const getSandbox = (index = 0) => {
const code = this.getNodeParameter<string>(codeParameterName, index);
const context = getSandboxContext.call(this, index);
if (language === 'python') {
const modules = this.getNodeParameter<string>('modules', index);
const moduleImports: string[] = modules ? modules.split(',').map((m) => m.trim()) : [];
context.printOverwrite = workflowMode === 'manual' ? this.sendMessageToUI : null;
return new PythonSandbox(context, code, moduleImports, index, this.helpers);
} else {
context.items = context.$input.all();
const sandbox = new JavaScriptSandbox(context, code, index, workflowMode, this.helpers);
if (workflowMode === 'manual') {
sandbox.vm.on('console.log', this.sendMessageToUI);
}
return sandbox;
}
};
// ----------------------------------
// runOnceForAllItems
// ----------------------------------
if (nodeMode === 'runOnceForAllItems') {
const jsCodeAllItems = this.getNodeParameter('jsCode', 0) as string;
const context = getSandboxContext.call(this);
context.items = context.$input.all();
const sandbox = new Sandbox(context, jsCodeAllItems, workflowMode, this.helpers);
if (workflowMode === 'manual') {
sandbox.on('console.log', this.sendMessageToUI);
}
let result: INodeExecutionData[];
const sandbox = getSandbox();
let items: INodeExecutionData[];
try {
result = await sandbox.runCodeAllItems();
items = await sandbox.runCodeAllItems();
} catch (error) {
if (!this.continueOnFail()) throw error;
result = [{ json: { error: error.message } }];
items = [{ json: { error: error.message } }];
}
for (const item of result) {
for (const item of items) {
standardizeOutput(item.json);
}
return this.prepareOutputData(result);
return this.prepareOutputData(items);
}
// ----------------------------------
@ -108,19 +133,10 @@ export class Code implements INodeType {
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, jsCodeEachItem, workflowMode, this.helpers);
if (workflowMode === 'manual') {
sandbox.on('console.log', this.sendMessageToUI);
}
const sandbox = getSandbox(index);
let result: INodeExecutionData | undefined;
try {
result = await sandbox.runCodeEachItem(index);
result = await sandbox.runCodeEachItem();
} catch (error) {
if (!this.continueOnFail()) throw error;
returnData.push({ json: { error: error.message } });

View file

@ -0,0 +1,115 @@
import type { NodeVMOptions } from 'vm2';
import { NodeVM } from 'vm2';
import type { IExecuteFunctions, INodeExecutionData, WorkflowExecuteMode } from 'n8n-workflow';
import { ValidationError } from './ValidationError';
import { ExecutionError } from './ExecutionError';
import type { SandboxContext } from './Sandbox';
import { Sandbox } from './Sandbox';
const { NODE_FUNCTION_ALLOW_BUILTIN: builtIn, NODE_FUNCTION_ALLOW_EXTERNAL: external } =
process.env;
const getSandboxOptions = (
context: SandboxContext,
workflowMode: WorkflowExecuteMode,
): NodeVMOptions => ({
console: workflowMode === 'manual' ? 'redirect' : 'inherit',
sandbox: context,
require: {
builtin: builtIn ? builtIn.split(',') : [],
external: external ? { modules: external.split(','), transitive: false } : false,
},
});
export class JavaScriptSandbox extends Sandbox {
readonly vm: NodeVM;
constructor(
context: SandboxContext,
private jsCode: string,
itemIndex: number | undefined,
workflowMode: WorkflowExecuteMode,
helpers: IExecuteFunctions['helpers'],
) {
super(
{
object: {
singular: 'object',
plural: 'objects',
},
},
itemIndex,
helpers,
);
this.vm = new NodeVM(getSandboxOptions(context, workflowMode));
}
async runCodeAllItems(): Promise<INodeExecutionData[]> {
const script = `module.exports = async function() {${this.jsCode}\n}()`;
let executionResult: INodeExecutionData | INodeExecutionData[];
try {
executionResult = await this.vm.run(script, __dirname);
} catch (error) {
// anticipate user expecting `items` to pre-exist as in Function Item node
if (error.message === 'items is not defined' && !/(let|const|var) items =/.test(script)) {
const quoted = error.message.replace('items', '`items`');
error.message = (quoted as string) + '. Did you mean `$input.all()`?';
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
throw new ExecutionError(error);
}
if (executionResult === null) return [];
return this.validateRunCodeAllItems(executionResult);
}
async runCodeEachItem(): Promise<INodeExecutionData | undefined> {
const script = `module.exports = async function() {${this.jsCode}\n}()`;
const match = this.jsCode.match(/\$input\.(?<disallowedMethod>first|last|all|itemMatching)/);
if (match?.groups?.disallowedMethod) {
const { disallowedMethod } = match.groups;
const lineNumber =
this.jsCode.split('\n').findIndex((line) => {
return line.includes(disallowedMethod) && !line.startsWith('//') && !line.startsWith('*');
}) + 1;
const disallowedMethodFound = lineNumber !== 0;
if (disallowedMethodFound) {
throw new ValidationError({
message: `Can't use .${disallowedMethod}() here`,
description: "This is only available in 'Run Once for All Items' mode",
itemIndex: this.itemIndex,
lineNumber,
});
}
}
let executionResult: INodeExecutionData;
try {
executionResult = await this.vm.run(script, __dirname);
} catch (error) {
// anticipate user expecting `item` to pre-exist as in Function Item node
if (error.message === 'item is not defined' && !/(let|const|var) item =/.test(script)) {
const quoted = error.message.replace('item', '`item`');
error.message = (quoted as string) + '. Did you mean `$input.item.json`?';
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
throw new ExecutionError(error, this.itemIndex);
}
if (executionResult === null) return;
return this.validateRunCodeEachItem(executionResult);
}
}

View file

@ -0,0 +1,28 @@
import type { PyodideInterface } from 'pyodide';
let pyodideInstance: PyodideInterface | undefined;
export async function LoadPyodide(): Promise<PyodideInterface> {
if (pyodideInstance === undefined) {
// TODO: Find better way to suppress warnings
//@ts-ignore
globalThis.Blob = (await import('node:buffer')).Blob;
// From: https://github.com/nodejs/node/issues/30810
const { emitWarning } = process;
process.emitWarning = (warning, ...args) => {
if (args[0] === 'ExperimentalWarning') {
return;
}
if (args[0] && typeof args[0] === 'object' && args[0].type === 'ExperimentalWarning') {
return;
}
return emitWarning(warning, ...(args as string[]));
};
const { loadPyodide } = await import('pyodide');
pyodideInstance = await loadPyodide();
}
return pyodideInstance;
}

View file

@ -0,0 +1,119 @@
import type { IExecuteFunctions, INodeExecutionData } from 'n8n-workflow';
import type { PyProxyDict } from 'pyodide';
import { LoadPyodide } from './Pyodide';
import type { SandboxContext } from './Sandbox';
import { Sandbox } from './Sandbox';
type PythonSandboxContext = {
[K in keyof SandboxContext as K extends `$${infer I}` ? `_${I}` : K]: SandboxContext[K];
};
type PyodideError = Error & { type: string };
const envAccessBlocked = process.env.N8N_BLOCK_ENV_ACCESS_IN_NODE === 'true';
export class PythonSandbox extends Sandbox {
private readonly context: PythonSandboxContext;
constructor(
context: SandboxContext,
private pythonCode: string,
private moduleImports: string[],
itemIndex: number | undefined,
helpers: IExecuteFunctions['helpers'],
) {
super(
{
object: {
singular: 'dictionary',
plural: 'dictionaries',
},
},
itemIndex,
helpers,
);
// Since python doesn't allow variable names starting with `$`,
// rename them to all to start with `_` instead
this.context = Object.keys(context).reduce((acc, key) => {
acc[key.startsWith('$') ? key.replace(/^\$/, '_') : key] = context[key];
return acc;
}, {} as PythonSandboxContext);
}
async runCodeAllItems() {
const executionResult = await this.runCodeInPython<INodeExecutionData[]>();
return this.validateRunCodeAllItems(executionResult);
}
async runCodeEachItem() {
const executionResult = await this.runCodeInPython<INodeExecutionData>();
return this.validateRunCodeEachItem(executionResult);
}
private async runCodeInPython<T>() {
// Below workaround from here:
// https://github.com/pyodide/pyodide/discussions/3537#discussioncomment-4864345
const runCode = `
from _pyodide_core import jsproxy_typedict
from js import Object
jsproxy_typedict[0] = type(Object.new().as_object_map())
if printOverwrite:
print = printOverwrite
async def __main():
${this.pythonCode
.split('\n')
.map((line) => ' ' + line)
.join('\n')}
await __main()
`;
const pyodide = await LoadPyodide();
const moduleImportsFiltered = this.moduleImports.filter(
(importModule) => !['asyncio', 'pyodide', 'math'].includes(importModule),
);
if (moduleImportsFiltered.length) {
await pyodide.loadPackage('micropip');
const micropip = pyodide.pyimport('micropip');
await Promise.all(
moduleImportsFiltered.map((importModule) => micropip.install(importModule)),
);
}
let executionResult;
try {
const dict = pyodide.globals.get('dict');
const globalsDict: PyProxyDict = dict();
for (const key of Object.keys(this.context)) {
if ((key === '_env' && envAccessBlocked) || key === '_node') continue;
const value = this.context[key];
globalsDict.set(key, value);
}
executionResult = await pyodide.runPythonAsync(runCode, { globals: globalsDict });
globalsDict.destroy();
} catch (error) {
throw this.getPrettyError(error as PyodideError);
}
if (executionResult?.toJs) {
return executionResult.toJs({
dict_converter: Object.fromEntries,
create_proxies: false,
}) as T;
}
return executionResult as T;
}
private getPrettyError(error: PyodideError): Error {
const errorTypeIndex = error.message.indexOf(error.type);
if (errorTypeIndex !== -1) {
return new Error(error.message.slice(errorTypeIndex));
}
return error;
}
}

View file

@ -1,70 +1,93 @@
import { NodeVM } from 'vm2';
import { ValidationError } from './ValidationError';
import { ExecutionError } from './ExecutionError';
import { isObject, REQUIRED_N8N_ITEM_KEYS } from './utils';
import { isObject } from './utils';
import type {
IDataObject,
IExecuteFunctions,
INodeExecutionData,
IWorkflowDataProxyData,
WorkflowExecuteMode,
} from 'n8n-workflow';
import type { IExecuteFunctions, INodeExecutionData, IWorkflowDataProxyData } from 'n8n-workflow';
interface SandboxContext extends IWorkflowDataProxyData {
interface SandboxTextKeys {
object: {
singular: string;
plural: string;
};
}
export 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 const REQUIRED_N8N_ITEM_KEYS = new Set(['json', 'binary', 'pairedItem']);
export class Sandbox extends NodeVM {
private itemIndex: number | undefined = undefined;
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
// $node, $items(), $parameter, $json, $env, etc.
...this.getWorkflowDataProxy(index),
};
}
export abstract class Sandbox {
constructor(
context: SandboxContext,
private jsCode: string,
workflowMode: WorkflowExecuteMode,
private textKeys: SandboxTextKeys,
protected itemIndex: number | undefined,
private helpers: IExecuteFunctions['helpers'],
) {
super({
console: workflowMode === 'manual' ? 'redirect' : 'inherit',
sandbox: context,
require: {
builtin: builtIn ? builtIn.split(',') : [],
external: external ? { modules: external.split(','), transitive: false } : false,
},
});
}
) {}
async runCodeAllItems(): Promise<INodeExecutionData[]> {
const script = `module.exports = async function() {${this.jsCode}\n}()`;
abstract runCodeAllItems(): Promise<INodeExecutionData[]>;
let executionResult: INodeExecutionData | INodeExecutionData[];
abstract runCodeEachItem(): Promise<INodeExecutionData | undefined>;
try {
executionResult = await this.run(script, __dirname);
} catch (error) {
// anticipate user expecting `items` to pre-exist as in Function Item node
if (error.message === 'items is not defined' && !/(let|const|var) items =/.test(script)) {
const quoted = error.message.replace('items', '`items`');
error.message = (quoted as string) + '. Did you mean `$input.all()`?';
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
throw new ExecutionError(error);
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,
});
}
if (executionResult === null) return [];
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,
});
}
if (executionResult === undefined || typeof executionResult !== 'object') {
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);
return returnData;
}
validateRunCodeAllItems(
executionResult: INodeExecutionData | INodeExecutionData[] | undefined,
itemIndex?: number,
): INodeExecutionData[] {
if (typeof executionResult !== 'object') {
throw new ValidationError({
message: "Code doesn't return items properly",
description:
'Please return an array of objects, one for each item you would like to output',
itemIndex: this.itemIndex,
description: `Please return an array of ${this.getTextKey('object', {
plural: true,
})}, one for each item you would like to output.`,
itemIndex,
});
}
@ -77,109 +100,54 @@ export class Sandbox extends NodeVM {
* item keys to be wrapped in `json` when normalizing items below.
*/
const mustHaveTopLevelN8nKey = executionResult.some((item) =>
Object.keys(item as IDataObject).find((key) => REQUIRED_N8N_ITEM_KEYS.has(key)),
Object.keys(item).find((key) => REQUIRED_N8N_ITEM_KEYS.has(key)),
);
for (const item of executionResult) {
if (mustHaveTopLevelN8nKey) {
if (mustHaveTopLevelN8nKey) {
for (const item of executionResult) {
this.validateTopLevelKeys(item);
}
}
}
const returnData = this.helpers.normalizeItems(executionResult);
returnData.forEach((item) => this.validateResult(item));
returnData.forEach((item) => this.validateItem(item));
return returnData;
}
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)/);
if (match?.groups?.disallowedMethod) {
const { disallowedMethod } = match.groups;
const lineNumber =
this.jsCode.split('\n').findIndex((line) => {
return line.includes(disallowedMethod) && !line.startsWith('//') && !line.startsWith('*');
}) + 1;
const disallowedMethodFound = lineNumber !== 0;
if (disallowedMethodFound) {
throw new ValidationError({
message: `Can't use .${disallowedMethod}() here`,
description: "This is only available in 'Run Once for All Items' mode",
itemIndex: this.itemIndex,
lineNumber,
});
}
private getTextKey(
key: keyof SandboxTextKeys,
options?: { includeArticle?: boolean; plural?: boolean },
) {
const response = this.textKeys[key][options?.plural ? 'plural' : 'singular'];
if (!options?.includeArticle) {
return response;
}
let executionResult: INodeExecutionData;
try {
executionResult = await this.run(script, __dirname);
} catch (error) {
// anticipate user expecting `item` to pre-exist as in Function Item node
if (error.message === 'item is not defined' && !/(let|const|var) item =/.test(script)) {
const quoted = error.message.replace('item', '`item`');
error.message = (quoted as string) + '. Did you mean `$input.item.json`?';
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
throw new ExecutionError(error, this.itemIndex);
if (['a', 'e', 'i', 'o', 'u'].some((value) => response.startsWith(value))) {
return `an ${response}`;
}
if (executionResult === null) return;
if (executionResult === undefined || typeof executionResult !== 'object') {
throw new ValidationError({
message: "Code doesn't return an object",
description: `Please return an object representing the output item. ('${executionResult}' was returned instead.)`,
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,
});
}
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;
return `a ${response}`;
}
private validateResult({ json, binary }: INodeExecutionData) {
private validateItem({ 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",
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 },
)}.`,
itemIndex: this.itemIndex,
});
}
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.",
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 },
)}.`,
itemIndex: this.itemIndex,
});
}
@ -196,15 +164,3 @@ export class Sandbox extends NodeVM {
});
}
}
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
...this.getWorkflowDataProxy(index ?? 0),
};
}

View file

@ -0,0 +1,76 @@
import type { INodeProperties } from 'n8n-workflow';
const commonDescription: INodeProperties = {
displayName: 'JavaScript',
name: 'jsCode',
type: 'string',
typeOptions: {
editor: 'codeNodeEditor',
editorLanguage: 'javaScript',
},
default: '',
description:
'JavaScript code to execute.<br><br>Tip: You can use luxon vars like <code>$today</code> for dates and <code>$jmespath</code> for querying JSON structures. <a href="https://docs.n8n.io/nodes/n8n-nodes-base.function">Learn more</a>.',
noDataExpression: true,
};
const v1Properties: INodeProperties[] = [
{
...commonDescription,
displayOptions: {
show: {
'@version': [1],
mode: ['runOnceForAllItems'],
},
},
},
{
...commonDescription,
displayOptions: {
show: {
'@version': [1],
mode: ['runOnceForEachItem'],
},
},
},
];
const v2Properties: INodeProperties[] = [
{
...commonDescription,
displayOptions: {
show: {
'@version': [2],
language: ['javaScript'],
mode: ['runOnceForAllItems'],
},
},
},
{
...commonDescription,
displayOptions: {
show: {
'@version': [2],
language: ['javaScript'],
mode: ['runOnceForEachItem'],
},
},
},
];
export const javascriptCodeDescription: INodeProperties[] = [
...v1Properties,
...v2Properties,
{
displayName:
'Type <code>$</code> for a list of <a target="_blank" href="https://docs.n8n.io/code-examples/methods-variables-reference/">special vars/methods</a>. Debug by using <code>console.log()</code> statements and viewing their output in the browser console.',
name: 'notice',
type: 'notice',
displayOptions: {
show: {
language: ['javaScript'],
},
},
default: '',
},
];

View file

@ -0,0 +1,63 @@
import type { INodeProperties } from 'n8n-workflow';
const commonDescription: INodeProperties = {
displayName: 'Python',
name: 'pythonCode',
type: 'string',
typeOptions: {
editor: 'codeNodeEditor',
editorLanguage: 'python',
},
default: '',
description:
'Python code to execute.<br><br>Tip: You can use luxon vars like <code>_today</code> for dates and <code>$_mespath</code> for querying JSON structures. <a href="https://docs.n8n.io/nodes/n8n-nodes-base.function">Learn more</a>.',
noDataExpression: true,
};
export const pythonCodeDescription: INodeProperties[] = [
{
...commonDescription,
displayOptions: {
show: {
language: ['python'],
mode: ['runOnceForAllItems'],
},
},
},
{
...commonDescription,
displayOptions: {
show: {
language: ['python'],
mode: ['runOnceForEachItem'],
},
},
},
{
displayName:
'Debug by using <code>print()</code> statements and viewing their output in the browser console.',
name: 'notice',
type: 'notice',
displayOptions: {
show: {
language: ['python'],
},
},
default: '',
},
{
displayName: 'Python Modules',
name: 'modules',
displayOptions: {
show: {
language: ['python'],
},
},
type: 'string',
default: '',
placeholder: 'opencv-python',
description:
'Comma-separated list of Python modules to load. They have to be installed to be able to be loaded and imported.',
noDataExpression: true,
},
];

View file

@ -1,15 +1,25 @@
import { anyNumber, mock } from 'jest-mock-extended';
import { NodeVM } from 'vm2';
import type { IExecuteFunctions, IWorkflowDataProxyData } from 'n8n-workflow';
import { NodeHelpers } from 'n8n-workflow';
import { normalizeItems } from 'n8n-core';
import { testWorkflows, getWorkflowFilenames } from '../../../test/nodes/Helpers';
import {
testWorkflows,
getWorkflowFilenames,
initBinaryDataManager,
} from '../../../test/nodes/Helpers';
import { Code } from '../Code.node';
import { Sandbox } from '../Sandbox';
import { ValidationError } from '../ValidationError';
const workflows = getWorkflowFilenames(__dirname);
describe('Test Code Node', () => {
const workflows = getWorkflowFilenames(__dirname);
describe('Test Code Node', () => testWorkflows(workflows));
beforeAll(async () => {
await initBinaryDataManager();
});
testWorkflows(workflows);
});
describe('Code Node unit test', () => {
const node = new Code();
@ -48,7 +58,7 @@ describe('Code Node unit test', () => {
Object.entries(tests).forEach(([title, [input, expected]]) =>
test(title, async () => {
jest.spyOn(Sandbox.prototype, 'run').mockResolvedValueOnce(input);
jest.spyOn(NodeVM.prototype, 'run').mockResolvedValueOnce(input);
const output = await node.execute.call(thisArg);
expect(output).toEqual([expected]);
@ -68,14 +78,14 @@ describe('Code Node unit test', () => {
Object.entries(tests).forEach(([title, returnData]) =>
test(`return error if \`.json\` is ${title}`, async () => {
jest.spyOn(Sandbox.prototype, 'run').mockResolvedValueOnce([{ json: returnData }]);
jest.spyOn(NodeVM.prototype, 'run').mockResolvedValueOnce([{ json: returnData }]);
try {
await node.execute.call(thisArg);
throw new Error("Validation error wasn't thrown");
} catch (error) {
expect(error).toBeInstanceOf(ValidationError);
expect(error.message).toEqual("A 'json' property isn't an object");
expect(error.message).toEqual("A 'json' property isn't an object [item 0]");
}
}),
);
@ -100,7 +110,7 @@ describe('Code Node unit test', () => {
Object.entries(tests).forEach(([title, [input, expected]]) =>
test(title, async () => {
jest.spyOn(Sandbox.prototype, 'run').mockResolvedValueOnce(input);
jest.spyOn(NodeVM.prototype, 'run').mockResolvedValueOnce(input);
const output = await node.execute.call(thisArg);
expect(output).toEqual([[{ json: expected?.json, pairedItem: { item: 0 } }]]);
@ -120,7 +130,7 @@ describe('Code Node unit test', () => {
Object.entries(tests).forEach(([title, returnData]) =>
test(`return error if \`.json\` is ${title}`, async () => {
jest.spyOn(Sandbox.prototype, 'run').mockResolvedValueOnce({ json: returnData });
jest.spyOn(NodeVM.prototype, 'run').mockResolvedValueOnce({ json: returnData });
try {
await node.execute.call(thisArg);

View file

@ -36,7 +36,3 @@ export function standardizeOutput(output: IDataObject) {
standardizeOutputRecursive(output);
return output;
}
export type CodeNodeMode = 'runOnceForAllItems' | 'runOnceForEachItem';
export const REQUIRED_N8N_ITEM_KEYS = new Set(['json', 'binary', 'pairedItem']);

View file

@ -891,6 +891,7 @@
"pg-promise": "^10.5.8",
"pretty-bytes": "^5.6.0",
"promise-ftp": "^1.3.5",
"pyodide": "^0.22.1",
"redis": "^3.1.1",
"request": "^2.88.2",
"rhea": "^1.0.11",

View file

@ -2,6 +2,9 @@
export const BINARY_ENCODING = 'base64';
export const WAIT_TIME_UNLIMITED = '3000-01-01T00:00:00.000Z';
export const CODE_LANGUAGES = ['javaScript', 'json', 'python'] as const;
export const CODE_EXECUTION_MODES = ['runOnceForAllItems', 'runOnceForEachItem'] as const;
/**
* Nodes whose parameter values may refer to other nodes without expressions.
* Their content may need to be updated when the referenced node is renamed.

View file

@ -7,7 +7,9 @@ import type { Readable } from 'stream';
import type { URLSearchParams } from 'url';
import type { OptionsWithUri, OptionsWithUrl } from 'request';
import type { RequestPromiseOptions, RequestPromiseAPI } from 'request-promise-native';
import type { PathLike } from 'fs';
import type { CODE_EXECUTION_MODES, CODE_LANGUAGES } from './Constants';
import type { IDeferredPromise } from './DeferredPromise';
import type { Workflow } from './Workflow';
import type { WorkflowHooks } from './WorkflowHooks';
@ -15,7 +17,6 @@ import type { WorkflowActivationError } from './WorkflowActivationError';
import type { WorkflowOperationError } from './WorkflowErrors';
import type { NodeApiError, NodeOperationError } from './NodeErrors';
import type { ExpressionError } from './ExpressionError';
import type { PathLike } from 'fs';
import type { ExecutionStatus } from './ExecutionStatus';
import type { AuthenticationMethod } from './Authentication';
@ -635,12 +636,12 @@ namespace ExecuteFunctions {
fallbackValue?: number,
options?: IGetNodeParameterOptions,
): number;
getNodeParameter(
getNodeParameter<T = NodeParameterValueType | object>(
parameterName: string,
itemIndex: number,
fallbackValue?: any,
options?: IGetNodeParameterOptions,
): NodeParameterValueType | object;
): T;
};
}
@ -1020,7 +1021,8 @@ export type NodePropertyTypes =
export type CodeAutocompleteTypes = 'function' | 'functionItem';
export type EditorType = 'code' | 'codeNodeEditor' | 'htmlEditor' | 'sqlEditor' | 'json';
export type CodeNodeEditorLanguage = 'javaScript' | 'json'; //| 'python' | 'sql';
export type CodeNodeEditorLanguage = (typeof CODE_LANGUAGES)[number];
export type CodeExecutionMode = (typeof CODE_EXECUTION_MODES)[number];
export type SQLDialect = 'mssql' | 'mysql' | 'postgres';
export interface ILoadOptions {

File diff suppressed because it is too large Load diff