forked from n8n-io/n8n
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(editor): Buffer json chunks in stream response (n8n-io#10439)
- Loading branch information
Showing
4 changed files
with
147 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
packages/editor-ui/src/utils/__tests__/apiUtils.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { STREAM_SEPERATOR, streamRequest } from '../apiUtils'; | ||
|
||
describe('streamRequest', () => { | ||
it('should stream data from the API endpoint', async () => { | ||
const encoder = new TextEncoder(); | ||
const mockResponse = new ReadableStream({ | ||
start(controller) { | ||
controller.enqueue(encoder.encode(`${JSON.stringify({ chunk: 1 })}${STREAM_SEPERATOR}`)); | ||
controller.enqueue(encoder.encode(`${JSON.stringify({ chunk: 2 })}${STREAM_SEPERATOR}`)); | ||
controller.enqueue(encoder.encode(`${JSON.stringify({ chunk: 3 })}${STREAM_SEPERATOR}`)); | ||
controller.close(); | ||
}, | ||
}); | ||
|
||
const mockFetch = vi.fn().mockResolvedValue({ | ||
ok: true, | ||
body: mockResponse, | ||
}); | ||
|
||
global.fetch = mockFetch; | ||
|
||
const onChunkMock = vi.fn(); | ||
const onDoneMock = vi.fn(); | ||
const onErrorMock = vi.fn(); | ||
|
||
await streamRequest( | ||
{ | ||
baseUrl: 'https://api.example.com', | ||
pushRef: '', | ||
}, | ||
'/data', | ||
{ key: 'value' }, | ||
onChunkMock, | ||
onDoneMock, | ||
onErrorMock, | ||
); | ||
|
||
expect(mockFetch).toHaveBeenCalledWith('https://api.example.com/data', { | ||
method: 'POST', | ||
body: JSON.stringify({ key: 'value' }), | ||
credentials: 'include', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'browser-id': expect.stringContaining('-'), | ||
}, | ||
}); | ||
|
||
expect(onChunkMock).toHaveBeenCalledTimes(3); | ||
expect(onChunkMock).toHaveBeenNthCalledWith(1, { chunk: 1 }); | ||
expect(onChunkMock).toHaveBeenNthCalledWith(2, { chunk: 2 }); | ||
expect(onChunkMock).toHaveBeenNthCalledWith(3, { chunk: 3 }); | ||
|
||
expect(onDoneMock).toHaveBeenCalledTimes(1); | ||
expect(onErrorMock).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should handle broken stream data', async () => { | ||
const encoder = new TextEncoder(); | ||
const mockResponse = new ReadableStream({ | ||
start(controller) { | ||
controller.enqueue( | ||
encoder.encode(`${JSON.stringify({ chunk: 1 })}${STREAM_SEPERATOR}{"chunk": `), | ||
); | ||
controller.enqueue(encoder.encode(`2}${STREAM_SEPERATOR}{"ch`)); | ||
controller.enqueue(encoder.encode('unk":')); | ||
controller.enqueue(encoder.encode(`3}${STREAM_SEPERATOR}`)); | ||
controller.close(); | ||
}, | ||
}); | ||
|
||
const mockFetch = vi.fn().mockResolvedValue({ | ||
ok: true, | ||
body: mockResponse, | ||
}); | ||
|
||
global.fetch = mockFetch; | ||
|
||
const onChunkMock = vi.fn(); | ||
const onDoneMock = vi.fn(); | ||
const onErrorMock = vi.fn(); | ||
|
||
await streamRequest( | ||
{ | ||
baseUrl: 'https://api.example.com', | ||
pushRef: '', | ||
}, | ||
'/data', | ||
{ key: 'value' }, | ||
onChunkMock, | ||
onDoneMock, | ||
onErrorMock, | ||
); | ||
|
||
expect(mockFetch).toHaveBeenCalledWith('https://api.example.com/data', { | ||
method: 'POST', | ||
body: JSON.stringify({ key: 'value' }), | ||
credentials: 'include', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'browser-id': expect.stringContaining('-'), | ||
}, | ||
}); | ||
|
||
expect(onChunkMock).toHaveBeenCalledTimes(3); | ||
expect(onChunkMock).toHaveBeenNthCalledWith(1, { chunk: 1 }); | ||
expect(onChunkMock).toHaveBeenNthCalledWith(2, { chunk: 2 }); | ||
expect(onChunkMock).toHaveBeenNthCalledWith(3, { chunk: 3 }); | ||
|
||
expect(onDoneMock).toHaveBeenCalledTimes(1); | ||
expect(onErrorMock).not.toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters