Skip to content

Commit

Permalink
Fix RSC stream parsing to handle incomplete chunks
Browse files Browse the repository at this point in the history
- Introduce `lastIncompleteChunk` to preserve partial JSON data between stream reads
- Ensure complete JSON chunks are processed by splitting on newlines
- Handle cases where the last chunk is not terminated with a newline
  • Loading branch information
AbanoubGhadban committed Feb 6, 2025
1 parent 44dee52 commit bdc72ed
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions node_package/src/transformRSCStreamAndReplayConsoleLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@ export default function transformRSCStreamAndReplayConsoleLogs(stream: ReadableS
const decoder = new TextDecoder();
const encoder = new TextEncoder();

let lastIncompleteChunk = '';
let { value, done } = await reader.read();
while (!done) {
const decodedValue = decoder.decode(value);
const jsonChunks = decodedValue.split('\n')
const decodedValue = lastIncompleteChunk + decoder.decode(value);
const chunks = decodedValue.split('\n');
if (!decodedValue.endsWith('\n')) {
lastIncompleteChunk = chunks.pop() ?? '';
} else {
lastIncompleteChunk = '';
}

const jsonChunks = chunks
.filter(line => line.trim() !== '')
.map((line) => {
try {
Expand Down

0 comments on commit bdc72ed

Please sign in to comment.