mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-10 14:44:05 -08:00
87def60979
Github issue / Community forum post (link here to close automatically): https://community.n8n.io/t/langchain-memory-chat/23733 --------- Signed-off-by: Oleg Ivaniv <me@olegivaniv.com> Co-authored-by: Oleg Ivaniv <me@olegivaniv.com> Co-authored-by: Val <68596159+valya@users.noreply.github.com> Co-authored-by: Alex Grozav <alex@grozav.com> Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in> Co-authored-by: Deborah <deborah@starfallprojects.co.uk> Co-authored-by: Jesper Bylund <mail@jesperbylund.com> Co-authored-by: Jon <jonathan.bennetts@gmail.com> Co-authored-by: Michael Kret <88898367+michael-radency@users.noreply.github.com> Co-authored-by: Giulio Andreini <andreini@netseven.it> Co-authored-by: Mason Geloso <Mason.geloso@gmail.com> Co-authored-by: Mason Geloso <hone@Masons-Mac-mini.local> Co-authored-by: Mutasem Aldmour <mutasem@n8n.io>
148 lines
4.3 KiB
TypeScript
148 lines
4.3 KiB
TypeScript
import { NodeVM, makeResolverFromLegacyOptions, type Resolver } from '@n8n/vm2';
|
|
import type { IExecuteFunctions, INodeExecutionData } 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;
|
|
|
|
export const vmResolver = makeResolverFromLegacyOptions({
|
|
external: external
|
|
? {
|
|
modules: external.split(','),
|
|
transitive: false,
|
|
}
|
|
: false,
|
|
builtin: builtIn?.split(',') ?? [],
|
|
});
|
|
|
|
export class JavaScriptSandbox extends Sandbox {
|
|
private readonly vm: NodeVM;
|
|
|
|
constructor(
|
|
context: SandboxContext,
|
|
private jsCode: string,
|
|
itemIndex: number | undefined,
|
|
helpers: IExecuteFunctions['helpers'],
|
|
options?: { resolver?: Resolver },
|
|
) {
|
|
super(
|
|
{
|
|
object: {
|
|
singular: 'object',
|
|
plural: 'objects',
|
|
},
|
|
},
|
|
itemIndex,
|
|
helpers,
|
|
);
|
|
this.vm = new NodeVM({
|
|
console: 'redirect',
|
|
sandbox: context,
|
|
require: options?.resolver ?? vmResolver,
|
|
wasm: false,
|
|
});
|
|
|
|
this.vm.on('console.log', (...args: unknown[]) => this.emit('output', ...args));
|
|
}
|
|
|
|
async runCode(): Promise<unknown> {
|
|
const script = `module.exports = async function() {${this.jsCode}\n}()`;
|
|
try {
|
|
const executionResult = await this.vm.run(script, __dirname);
|
|
return executionResult;
|
|
} catch (error) {
|
|
throw new ExecutionError(error);
|
|
}
|
|
}
|
|
|
|
async runCodeAllItems(options?: {
|
|
multiOutput?: boolean;
|
|
}): Promise<INodeExecutionData[] | INodeExecutionData[][]> {
|
|
const script = `module.exports = async function() {${this.jsCode}\n}()`;
|
|
|
|
let executionResult: INodeExecutionData | 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()`?';
|
|
}
|
|
|
|
throw new ExecutionError(error);
|
|
}
|
|
|
|
if (executionResult === null) return [];
|
|
|
|
if (options?.multiOutput === true) {
|
|
// Check if executionResult is an array of arrays
|
|
if (!Array.isArray(executionResult) || executionResult.some((item) => !Array.isArray(item))) {
|
|
throw new ValidationError({
|
|
message: "The code doesn't return an array of arrays",
|
|
description:
|
|
'Please return an array of arrays. One array for the different outputs and one for the different items that get returned.',
|
|
itemIndex: this.itemIndex,
|
|
});
|
|
}
|
|
|
|
return executionResult.map((data) => {
|
|
return this.validateRunCodeAllItems(data);
|
|
});
|
|
}
|
|
|
|
return this.validateRunCodeAllItems(
|
|
executionResult as INodeExecutionData | INodeExecutionData[],
|
|
);
|
|
}
|
|
|
|
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`?';
|
|
}
|
|
|
|
throw new ExecutionError(error, this.itemIndex);
|
|
}
|
|
|
|
if (executionResult === null) return;
|
|
|
|
return this.validateRunCodeEachItem(executionResult);
|
|
}
|
|
}
|