Skip to content

Commit 5f64386

Browse files
use buffer for received incomplete chunks
1 parent ee9464f commit 5f64386

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

packages/react-on-rails-pro-node-renderer/tests/htmlStreaming.test.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const makeRequest = async (options = {}) => {
3434
const jsonChunks = [];
3535
let firstByteTime;
3636
let status;
37+
let buffer = '';
3738
const decoder = new TextDecoder();
3839

3940
request.on('response', (headers) => {
@@ -44,10 +45,17 @@ const makeRequest = async (options = {}) => {
4445
// Sometimes, multiple chunks are merged into one.
4546
// So, the server uses \n as a delimiter between chunks.
4647
const decodedData = typeof data === 'string' ? data : decoder.decode(data, { stream: false });
47-
const decodedChunksFromData = decodedData
48+
const decodedChunksFromData = (buffer + decodedData)
4849
.split('\n')
4950
.map((chunk) => chunk.trim())
5051
.filter((chunk) => chunk.length > 0);
52+
53+
if (!decodedData.endsWith('\n')) {
54+
buffer = decodedChunksFromData.pop() ?? '';
55+
} else {
56+
buffer = '';
57+
}
58+
5159
chunks.push(...decodedChunksFromData);
5260
jsonChunks.push(
5361
...decodedChunksFromData.map((chunk) => {

0 commit comments

Comments
 (0)