-
-
Notifications
You must be signed in to change notification settings - Fork 632
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stream rsc payload in json objects like streamed react components
- Loading branch information
1 parent
a761735
commit bf548a2
Showing
9 changed files
with
149 additions
and
80 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
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
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
40 changes: 40 additions & 0 deletions
40
node_package/src/transformRSCStreamAndReplayConsoleLogs.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,40 @@ | ||
export default function transformRSCStreamAndReplayConsoleLogs(stream: ReadableStream) { | ||
return new ReadableStream({ | ||
async start(controller) { | ||
const reader = stream.getReader(); | ||
const decoder = new TextDecoder(); | ||
const encoder = new TextEncoder(); | ||
|
||
let { value, done } = await reader.read(); | ||
while (!done) { | ||
const decodedValue = decoder.decode(value); | ||
const jsonChunks = decodedValue.split('\n') | ||
.filter(line => line.trim() !== '') | ||
.map((line) => { | ||
try { | ||
return JSON.parse(line); | ||
} catch (error) { | ||
console.error('Error parsing JSON:', line, error); | ||
throw error; | ||
} | ||
}); | ||
|
||
for (const jsonChunk of jsonChunks) { | ||
const { html, consoleReplayScript } = jsonChunk; | ||
controller.enqueue(encoder.encode(html)); | ||
|
||
const replayConsoleCode = consoleReplayScript?.trim().replace(/^<script.*>/, '').replace(/<\/script>$/, ''); | ||
if (replayConsoleCode?.trim() !== '') { | ||
const scriptElement = document.createElement('script'); | ||
scriptElement.textContent = replayConsoleCode; | ||
document.body.appendChild(scriptElement); | ||
} | ||
} | ||
|
||
// eslint-disable-next-line no-await-in-loop | ||
({ value, done } = await reader.read()); | ||
} | ||
controller.close(); | ||
} | ||
}); | ||
} |
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