fix(core): Fix serialization of circular json with task runner (#12288)

This commit is contained in:
Tomi Turtiainen 2024-12-19 10:55:50 +02:00 committed by GitHub
parent e0dc385f8b
commit a99d726f42
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 26 additions and 2 deletions

View file

@ -58,4 +58,28 @@ describe('TaskRunnerWsServer', () => {
expect(clearIntervalSpy).toHaveBeenCalled(); 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]"}}',
);
});
});
}); });

View file

@ -1,6 +1,6 @@
import { TaskRunnersConfig } from '@n8n/config'; import { TaskRunnersConfig } from '@n8n/config';
import type { BrokerMessage, RunnerMessage } from '@n8n/task-runner'; import type { BrokerMessage, RunnerMessage } from '@n8n/task-runner';
import { ApplicationError } from 'n8n-workflow'; import { ApplicationError, jsonStringify } from 'n8n-workflow';
import { Service } from 'typedi'; import { Service } from 'typedi';
import type WebSocket from 'ws'; import type WebSocket from 'ws';
@ -83,7 +83,7 @@ export class TaskRunnerWsServer {
} }
sendMessage(id: TaskRunner['id'], message: BrokerMessage.ToRunner.All) { 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) { add(id: TaskRunner['id'], connection: WebSocket) {