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/commands": "^6.1.0",
"@codemirror/lang-javascript": "^6.1.2", "@codemirror/lang-javascript": "^6.1.2",
"@codemirror/lang-json": "^6.0.1", "@codemirror/lang-json": "^6.0.1",
"@codemirror/lang-python": "^6.1.2",
"@codemirror/lang-sql": "^6.4.1", "@codemirror/lang-sql": "^6.4.1",
"@codemirror/language": "^6.2.1", "@codemirror/language": "^6.2.1",
"@codemirror/lint": "^6.0.0", "@codemirror/lint": "^6.0.0",

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -1,9 +1,10 @@
import { STICKY_NODE_TYPE } from '@/constants'; import { STICKY_NODE_TYPE } from '@/constants';
import type { Diagnostic } from '@codemirror/lint'; 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 NODE_TYPES_EXCLUDED_FROM_AUTOCOMPLETION = [STICKY_NODE_TYPE];
export const AUTOCOMPLETABLE_BUILT_IN_MODULES = [ export const AUTOCOMPLETABLE_BUILT_IN_MODULES_JS = [
'console', 'console',
'constants', 'constants',
'crypto', '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 OFFSET_FOR_SCRIPT_WRAPPER = 'module.exports = async function() {'.length;
export const ALL_ITEMS_PLACEHOLDER = ` export const CODE_PLACEHOLDERS: Partial<
// Loop over input items and add a new field Record<CodeNodeEditorLanguage, Record<CodeExecutionMode, string>>
// called 'myNewField' to the JSON of each one > = {
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()) { for (const item of $input.all()) {
item.json.myNewField = 1; item.json.myNewField = 1;
} }
return $input.all(); return $input.all();`.trim(),
`.trim(); runOnceForEachItem: `
// Add a new field called 'myNewField' to the JSON of the item
export const EACH_ITEM_PLACEHOLDER = `
// Add a new field called 'myNewField' to the
// JSON of the item
$input.item.json.myNewField = 1; $input.item.json.myNewField = 1;
return $input.item; return $input.item;`.trim(),
`.trim(); },
python: {
export const CODE_LANGUAGES = ['javaScript', 'json'] as const; runOnceForAllItems: `
export const CODE_MODES = ['runOnceForAllItems', 'runOnceForEachItem'] as const; # 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 type { Diagnostic } from '@codemirror/lint';
import { linter as createLinter } from '@codemirror/lint'; import { linter as createLinter } from '@codemirror/lint';
import { jsonParseLinter } from '@codemirror/lang-json'; 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 type { EditorView } from '@codemirror/view';
import * as esprima from 'esprima-next';
import type { Node } from 'estree'; 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({ export const linterExtension = (Vue as CodeNodeEditorMixin).extend({
methods: { methods: {
createLinter(language: CodeLanguage) { createLinter(language: CodeNodeEditorLanguage) {
switch (language) { switch (language) {
case 'javaScript': case 'javaScript':
return createLinter(this.lintSource, { delay: DEFAULT_LINTER_DELAY_IN_MS }); return createLinter(this.lintSource, { delay: DEFAULT_LINTER_DELAY_IN_MS });

View file

@ -1,19 +1,16 @@
import type { EditorView } from '@codemirror/view'; import type { EditorView } from '@codemirror/view';
import type { I18nClass } from '@/plugins/i18n'; 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 { Node } from 'estree';
import type { CODE_LANGUAGES, CODE_MODES } from './constants';
export type CodeNodeEditorMixin = Vue.VueConstructor< export type CodeNodeEditorMixin = Vue.VueConstructor<
Vue & { Vue & {
$locale: I18nClass; $locale: I18nClass;
editor: EditorView | null; editor: EditorView | null;
mode: 'runOnceForAllItems' | 'runOnceForEachItem'; mode: CodeExecutionMode;
language: CodeNodeEditorLanguage;
getCurrentWorkflow(): Workflow; getCurrentWorkflow(): Workflow;
} }
>; >;
export type RangeNode = Node & { range: [number, number] }; 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 { editorLanguage(): CodeNodeEditorLanguage {
if (this.editorType === 'json' || this.parameter.type === 'json') return 'json'; if (this.editorType === 'json' || this.parameter.type === 'json') return 'json';
return 'javaScript'; return (this.getArgument('editorLanguage') as CodeNodeEditorLanguage) ?? 'javaScript';
}, },
parameterOptions(): parameterOptions():
| Array<INodePropertyOptions | INodeProperties | INodePropertyCollection> | Array<INodePropertyOptions | INodeProperties | INodePropertyCollection>

View file

@ -1672,13 +1672,19 @@ export default mixins(
}, },
async getNewNodeWithDefaultCredential(nodeTypeData: INodeTypeDescription) { 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 = { const newNodeData: INodeUi = {
id: uuid(), id: uuid(),
name: nodeTypeData.defaults.name as string, name: nodeTypeData.defaults.name as string,
type: nodeTypeData.name, type: nodeTypeData.name,
typeVersion: Array.isArray(nodeTypeData.version) typeVersion: nodeVersion,
? nodeTypeData.version.slice(-1)[0]
: nodeTypeData.version,
position: [0, 0], position: [0, 0],
parameters: {}, 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": { "subcategories": {
"Core Nodes": ["Data Transformation"] "Core Nodes": ["Data Transformation"]
} }

View file

@ -1,12 +1,17 @@
import type { import type {
CodeExecutionMode,
CodeNodeEditorLanguage,
IExecuteFunctions, IExecuteFunctions,
INodeExecutionData, INodeExecutionData,
INodeType, INodeType,
INodeTypeDescription, INodeTypeDescription,
} from 'n8n-workflow'; } 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 { standardizeOutput } from './utils';
import type { CodeNodeMode } from './utils';
export class Code implements INodeType { export class Code implements INodeType {
description: INodeTypeDescription = { description: INodeTypeDescription = {
@ -14,7 +19,8 @@ export class Code implements INodeType {
name: 'code', name: 'code',
icon: 'fa:code', icon: 'fa:code',
group: ['transform'], group: ['transform'],
version: 1, version: [1, 2],
defaultVersion: 1,
description: 'Run custom JavaScript code', description: 'Run custom JavaScript code',
defaults: { defaults: {
name: 'Code', name: 'Code',
@ -44,59 +50,78 @@ export class Code implements INodeType {
default: 'runOnceForAllItems', default: 'runOnceForAllItems',
}, },
{ {
displayName: 'JavaScript', displayName: 'Language',
name: 'jsCode', name: 'language',
typeOptions: { type: 'options',
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>.',
noDataExpression: true, noDataExpression: true,
displayOptions: {
show: {
'@version': [2],
},
},
options: [
{
name: 'JavaScript',
value: 'javaScript',
}, },
{ {
displayName: name: 'Python (Beta)',
'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.', value: 'python',
name: 'notice',
type: 'notice',
default: '',
}, },
], ],
default: 'javaScript',
},
...javascriptCodeDescription,
...pythonCodeDescription,
],
}; };
async execute(this: IExecuteFunctions) { async execute(this: IExecuteFunctions) {
const nodeMode = this.getNodeParameter('mode', 0) as CodeNodeMode; const nodeMode = this.getNodeParameter<CodeExecutionMode>('mode', 0);
const workflowMode = this.getMode(); 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 // runOnceForAllItems
// ---------------------------------- // ----------------------------------
if (nodeMode === 'runOnceForAllItems') { if (nodeMode === 'runOnceForAllItems') {
const jsCodeAllItems = this.getNodeParameter('jsCode', 0) as string; const sandbox = getSandbox();
let items: INodeExecutionData[];
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[];
try { try {
result = await sandbox.runCodeAllItems(); items = await sandbox.runCodeAllItems();
} catch (error) { } catch (error) {
if (!this.continueOnFail()) throw 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); standardizeOutput(item.json);
} }
return this.prepareOutputData(result); return this.prepareOutputData(items);
} }
// ---------------------------------- // ----------------------------------
@ -108,19 +133,10 @@ export class Code implements INodeType {
const items = this.getInputData(); const items = this.getInputData();
for (let index = 0; index < items.length; index++) { for (let index = 0; index < items.length; index++) {
const jsCodeEachItem = this.getNodeParameter('jsCode', index) as string; const sandbox = getSandbox(index);
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);
}
let result: INodeExecutionData | undefined; let result: INodeExecutionData | undefined;
try { try {
result = await sandbox.runCodeEachItem(index); result = await sandbox.runCodeEachItem();
} catch (error) { } catch (error) {
if (!this.continueOnFail()) throw error; if (!this.continueOnFail()) throw error;
returnData.push({ json: { error: error.message } }); 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 { ValidationError } from './ValidationError';
import { ExecutionError } from './ExecutionError'; import { isObject } from './utils';
import { isObject, REQUIRED_N8N_ITEM_KEYS } from './utils';
import type { import type { IExecuteFunctions, INodeExecutionData, IWorkflowDataProxyData } from 'n8n-workflow';
IDataObject,
IExecuteFunctions,
INodeExecutionData,
IWorkflowDataProxyData,
WorkflowExecuteMode,
} from 'n8n-workflow';
interface SandboxContext extends IWorkflowDataProxyData { interface SandboxTextKeys {
object: {
singular: string;
plural: string;
};
}
export interface SandboxContext extends IWorkflowDataProxyData {
$getNodeParameter: IExecuteFunctions['getNodeParameter']; $getNodeParameter: IExecuteFunctions['getNodeParameter'];
$getWorkflowStaticData: IExecuteFunctions['getWorkflowStaticData']; $getWorkflowStaticData: IExecuteFunctions['getWorkflowStaticData'];
helpers: IExecuteFunctions['helpers']; helpers: IExecuteFunctions['helpers'];
} }
const { NODE_FUNCTION_ALLOW_BUILTIN: builtIn, NODE_FUNCTION_ALLOW_EXTERNAL: external } = export const REQUIRED_N8N_ITEM_KEYS = new Set(['json', 'binary', 'pairedItem']);
process.env;
export class Sandbox extends NodeVM { export function getSandboxContext(this: IExecuteFunctions, index: number): SandboxContext {
private itemIndex: number | undefined = undefined; 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( constructor(
context: SandboxContext, private textKeys: SandboxTextKeys,
private jsCode: string, protected itemIndex: number | undefined,
workflowMode: WorkflowExecuteMode,
private helpers: IExecuteFunctions['helpers'], private helpers: IExecuteFunctions['helpers'],
) { ) {}
super({
console: workflowMode === 'manual' ? 'redirect' : 'inherit', abstract runCodeAllItems(): Promise<INodeExecutionData[]>;
sandbox: context,
require: { abstract runCodeEachItem(): Promise<INodeExecutionData | undefined>;
builtin: builtIn ? builtIn.split(',') : [],
external: external ? { modules: external.split(','), transitive: false } : false, 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,
}); });
} }
async runCodeAllItems(): Promise<INodeExecutionData[]> { if (Array.isArray(executionResult)) {
const script = `module.exports = async function() {${this.jsCode}\n}()`; const firstSentence =
executionResult.length > 0
let executionResult: INodeExecutionData | INodeExecutionData[]; ? `An array of ${typeof executionResult[0]}s was returned.`
: 'An empty array was returned.';
try { throw new ValidationError({
executionResult = await this.run(script, __dirname); message: `Code doesn't return a single ${this.getTextKey('object')}`,
} catch (error) { description: `${firstSentence} If you need to output multiple items, please use the 'Run Once for All Items' mode instead.`,
// anticipate user expecting `items` to pre-exist as in Function Item node itemIndex: this.itemIndex,
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 const [returnData] = this.helpers.normalizeItems([executionResult]);
throw new ExecutionError(error);
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;
} }
if (executionResult === null) return []; validateRunCodeAllItems(
executionResult: INodeExecutionData | INodeExecutionData[] | undefined,
if (executionResult === undefined || typeof executionResult !== 'object') { itemIndex?: number,
): INodeExecutionData[] {
if (typeof executionResult !== 'object') {
throw new ValidationError({ throw new ValidationError({
message: "Code doesn't return items properly", message: "Code doesn't return items properly",
description: description: `Please return an array of ${this.getTextKey('object', {
'Please return an array of objects, one for each item you would like to output', plural: true,
itemIndex: this.itemIndex, })}, 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. * item keys to be wrapped in `json` when normalizing items below.
*/ */
const mustHaveTopLevelN8nKey = executionResult.some((item) => 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); this.validateTopLevelKeys(item);
} }
} }
} }
const returnData = this.helpers.normalizeItems(executionResult); const returnData = this.helpers.normalizeItems(executionResult);
returnData.forEach((item) => this.validateResult(item)); returnData.forEach((item) => this.validateItem(item));
return returnData; return returnData;
} }
async runCodeEachItem(itemIndex: number): Promise<INodeExecutionData | undefined> { private getTextKey(
this.itemIndex = itemIndex; key: keyof SandboxTextKeys,
const script = `module.exports = async function() {${this.jsCode}\n}()`; options?: { includeArticle?: boolean; plural?: boolean },
) {
const match = this.jsCode.match(/\$input\.(?<disallowedMethod>first|last|all|itemMatching)/); const response = this.textKeys[key][options?.plural ? 'plural' : 'singular'];
if (!options?.includeArticle) {
if (match?.groups?.disallowedMethod) { return response;
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,
});
} }
if (['a', 'e', 'i', 'o', 'u'].some((value) => response.startsWith(value))) {
return `an ${response}`;
}
return `a ${response}`;
} }
let executionResult: INodeExecutionData; private validateItem({ json, binary }: 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 (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;
}
private validateResult({ json, binary }: INodeExecutionData) {
if (json === undefined || !isObject(json)) { if (json === undefined || !isObject(json)) {
throw new ValidationError({ throw new ValidationError({
message: "A 'json' property isn't 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 an object", description: `In the returned data, every key named 'json' must point to ${this.getTextKey(
'object',
{ includeArticle: true },
)}.`,
itemIndex: this.itemIndex, itemIndex: this.itemIndex,
}); });
} }
if (binary !== undefined && !isObject(binary)) { if (binary !== undefined && !isObject(binary)) {
throw new ValidationError({ throw new ValidationError({
message: "A 'binary' property isn't 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 an object.", description: `In the returned data, every key named 'binary must point to ${this.getTextKey(
'object',
{ includeArticle: true },
)}.`,
itemIndex: this.itemIndex, 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 { anyNumber, mock } from 'jest-mock-extended';
import { NodeVM } from 'vm2';
import type { IExecuteFunctions, IWorkflowDataProxyData } from 'n8n-workflow'; import type { IExecuteFunctions, IWorkflowDataProxyData } from 'n8n-workflow';
import { NodeHelpers } from 'n8n-workflow'; import { NodeHelpers } from 'n8n-workflow';
import { normalizeItems } from 'n8n-core'; 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 { Code } from '../Code.node';
import { Sandbox } from '../Sandbox';
import { ValidationError } from '../ValidationError'; import { ValidationError } from '../ValidationError';
describe('Test Code Node', () => {
const workflows = getWorkflowFilenames(__dirname); const workflows = getWorkflowFilenames(__dirname);
describe('Test Code Node', () => testWorkflows(workflows)); beforeAll(async () => {
await initBinaryDataManager();
});
testWorkflows(workflows);
});
describe('Code Node unit test', () => { describe('Code Node unit test', () => {
const node = new Code(); const node = new Code();
@ -48,7 +58,7 @@ describe('Code Node unit test', () => {
Object.entries(tests).forEach(([title, [input, expected]]) => Object.entries(tests).forEach(([title, [input, expected]]) =>
test(title, async () => { test(title, async () => {
jest.spyOn(Sandbox.prototype, 'run').mockResolvedValueOnce(input); jest.spyOn(NodeVM.prototype, 'run').mockResolvedValueOnce(input);
const output = await node.execute.call(thisArg); const output = await node.execute.call(thisArg);
expect(output).toEqual([expected]); expect(output).toEqual([expected]);
@ -68,14 +78,14 @@ describe('Code Node unit test', () => {
Object.entries(tests).forEach(([title, returnData]) => Object.entries(tests).forEach(([title, returnData]) =>
test(`return error if \`.json\` is ${title}`, async () => { 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 { try {
await node.execute.call(thisArg); await node.execute.call(thisArg);
throw new Error("Validation error wasn't thrown"); throw new Error("Validation error wasn't thrown");
} catch (error) { } catch (error) {
expect(error).toBeInstanceOf(ValidationError); 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]]) => Object.entries(tests).forEach(([title, [input, expected]]) =>
test(title, async () => { test(title, async () => {
jest.spyOn(Sandbox.prototype, 'run').mockResolvedValueOnce(input); jest.spyOn(NodeVM.prototype, 'run').mockResolvedValueOnce(input);
const output = await node.execute.call(thisArg); const output = await node.execute.call(thisArg);
expect(output).toEqual([[{ json: expected?.json, pairedItem: { item: 0 } }]]); expect(output).toEqual([[{ json: expected?.json, pairedItem: { item: 0 } }]]);
@ -120,7 +130,7 @@ describe('Code Node unit test', () => {
Object.entries(tests).forEach(([title, returnData]) => Object.entries(tests).forEach(([title, returnData]) =>
test(`return error if \`.json\` is ${title}`, async () => { 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 { try {
await node.execute.call(thisArg); await node.execute.call(thisArg);

View file

@ -36,7 +36,3 @@ export function standardizeOutput(output: IDataObject) {
standardizeOutputRecursive(output); standardizeOutputRecursive(output);
return 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", "pg-promise": "^10.5.8",
"pretty-bytes": "^5.6.0", "pretty-bytes": "^5.6.0",
"promise-ftp": "^1.3.5", "promise-ftp": "^1.3.5",
"pyodide": "^0.22.1",
"redis": "^3.1.1", "redis": "^3.1.1",
"request": "^2.88.2", "request": "^2.88.2",
"rhea": "^1.0.11", "rhea": "^1.0.11",

View file

@ -2,6 +2,9 @@
export const BINARY_ENCODING = 'base64'; export const BINARY_ENCODING = 'base64';
export const WAIT_TIME_UNLIMITED = '3000-01-01T00:00:00.000Z'; 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. * Nodes whose parameter values may refer to other nodes without expressions.
* Their content may need to be updated when the referenced node is renamed. * 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 { URLSearchParams } from 'url';
import type { OptionsWithUri, OptionsWithUrl } from 'request'; import type { OptionsWithUri, OptionsWithUrl } from 'request';
import type { RequestPromiseOptions, RequestPromiseAPI } from 'request-promise-native'; 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 { IDeferredPromise } from './DeferredPromise';
import type { Workflow } from './Workflow'; import type { Workflow } from './Workflow';
import type { WorkflowHooks } from './WorkflowHooks'; import type { WorkflowHooks } from './WorkflowHooks';
@ -15,7 +17,6 @@ import type { WorkflowActivationError } from './WorkflowActivationError';
import type { WorkflowOperationError } from './WorkflowErrors'; import type { WorkflowOperationError } from './WorkflowErrors';
import type { NodeApiError, NodeOperationError } from './NodeErrors'; import type { NodeApiError, NodeOperationError } from './NodeErrors';
import type { ExpressionError } from './ExpressionError'; import type { ExpressionError } from './ExpressionError';
import type { PathLike } from 'fs';
import type { ExecutionStatus } from './ExecutionStatus'; import type { ExecutionStatus } from './ExecutionStatus';
import type { AuthenticationMethod } from './Authentication'; import type { AuthenticationMethod } from './Authentication';
@ -635,12 +636,12 @@ namespace ExecuteFunctions {
fallbackValue?: number, fallbackValue?: number,
options?: IGetNodeParameterOptions, options?: IGetNodeParameterOptions,
): number; ): number;
getNodeParameter( getNodeParameter<T = NodeParameterValueType | object>(
parameterName: string, parameterName: string,
itemIndex: number, itemIndex: number,
fallbackValue?: any, fallbackValue?: any,
options?: IGetNodeParameterOptions, options?: IGetNodeParameterOptions,
): NodeParameterValueType | object; ): T;
}; };
} }
@ -1020,7 +1021,8 @@ export type NodePropertyTypes =
export type CodeAutocompleteTypes = 'function' | 'functionItem'; export type CodeAutocompleteTypes = 'function' | 'functionItem';
export type EditorType = 'code' | 'codeNodeEditor' | 'htmlEditor' | 'sqlEditor' | 'json'; 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 type SQLDialect = 'mssql' | 'mysql' | 'postgres';
export interface ILoadOptions { export interface ILoadOptions {

File diff suppressed because it is too large Load diff