Skip to content

Commit

Permalink
Use util.inspect instead of JSON.parse
Browse files Browse the repository at this point in the history
  • Loading branch information
tomi committed Dec 30, 2024
1 parent d6f449a commit 67cd4ec
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,27 @@ describe('JsTaskRunner', () => {
}),
).resolves.toBeDefined();
});

it('should log the context object as [[ExecutionContext]]', async () => {
const rpcCallSpy = jest.spyOn(defaultTaskRunner, 'makeRpcCall').mockResolvedValue(undefined);

const task = newTaskWithSettings({
code: `
console.log(this);
return {json: {}}
`,
nodeMode: 'runOnceForAllItems',
});

await execTaskWithParams({
task,
taskData: newDataRequestResponse([wrapIntoJson({})]),
});

expect(rpcCallSpy).toHaveBeenCalledWith(task.taskId, 'logNodeOutput', [
'[[ExecutionContext]]',
]);
});
});

describe('built-in methods and variables available in the context', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
IWorkflowDataProxyData,
} from 'n8n-workflow';
import * as a from 'node:assert';
import { inspect } from 'node:util';
import { runInNewContext, type Context } from 'node:vm';

import type { MainConfig } from '@/config/main-config';
Expand Down Expand Up @@ -438,7 +439,7 @@ export class JsTaskRunner extends TaskRunner {
private buildCustomConsole(taskId: string): CustomConsole {
const stringifyArg = (arg: unknown) => {
try {
return typeof arg === 'object' && arg !== null ? JSON.stringify(arg) : arg;
return typeof arg === 'object' && arg !== null ? inspect(arg) : arg;
} catch (e) {
const error = ensureError(e);
console.warn('Failed to stringify console.log argument:', error.message);
Expand Down Expand Up @@ -479,6 +480,7 @@ export class JsTaskRunner extends TaskRunner {
additionalProperties: Record<string, unknown> = {},
): Context {
const context: Context = {
[inspect.custom]: () => '[[ExecutionContext]]',
require: this.requireResolver,
module: {},
console: this.buildCustomConsole(taskId),
Expand Down

0 comments on commit 67cd4ec

Please sign in to comment.