Skip to content

Commit

Permalink
fix(core): Fix serialization of circular json with task runner
Browse files Browse the repository at this point in the history
  • Loading branch information
tomi committed Dec 18, 2024
1 parent 7ce4e8d commit 52bfd49
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
24 changes: 24 additions & 0 deletions packages/cli/src/runners/__tests__/task-runner-ws-server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,28 @@ describe('TaskRunnerWsServer', () => {
expect(clearIntervalSpy).toHaveBeenCalled();
});
});

describe('sendMessage', () => {
it('should work with a message containing circular references', () => {
const server = new TaskRunnerWsServer(mock(), mock(), mock(), mock(), mock());
const ws = mock<WebSocket>();
server.runnerConnections.set('test-runner', ws);

const messageData: Record<string, unknown> = {};
messageData.circular = messageData;

expect(() =>
server.sendMessage('test-runner', {
type: 'broker:taskdataresponse',
taskId: 'taskId',
requestId: 'requestId',
data: messageData,
}),
).not.toThrow();

expect(ws.send).toHaveBeenCalledWith(
'{"type":"broker:taskdataresponse","taskId":"taskId","requestId":"requestId","data":{"circular":"[Circular Reference]"}}',
);
});
});
});
4 changes: 2 additions & 2 deletions packages/cli/src/runners/runner-ws-server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TaskRunnersConfig } from '@n8n/config';
import type { BrokerMessage, RunnerMessage } from '@n8n/task-runner';
import { ApplicationError } from 'n8n-workflow';
import { ApplicationError, jsonStringify } from 'n8n-workflow';
import { Service } from 'typedi';
import type WebSocket from 'ws';

Expand Down Expand Up @@ -83,7 +83,7 @@ export class TaskRunnerWsServer {
}

sendMessage(id: TaskRunner['id'], message: BrokerMessage.ToRunner.All) {
this.runnerConnections.get(id)?.send(JSON.stringify(message));
this.runnerConnections.get(id)?.send(jsonStringify(message, { replaceCircularRefs: true }));
}

add(id: TaskRunner['id'], connection: WebSocket) {
Expand Down

0 comments on commit 52bfd49

Please sign in to comment.