-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update rendering from
renderToString
to `renderToReadableStre…
…am` (#233) Co-authored-by: Marco Pasqualetti <[email protected]>
- Loading branch information
1 parent
21ff3c8
commit 3e4e7ff
Showing
5 changed files
with
98 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// react ReadableStream type is an empty interface so we are using the one from | ||
// node which match the runtime value | ||
import type { ReadableStream } from 'node:stream/web' | ||
|
||
function concatArrayBuffers(chunks: Array<Uint8Array>): Uint8Array { | ||
const result = new Uint8Array(chunks.reduce((a, c) => a + c.length, 0)) | ||
let offset = 0 | ||
for (const chunk of chunks) { | ||
result.set(chunk, offset) | ||
offset += chunk.length | ||
} | ||
return result | ||
} | ||
|
||
async function streamToArrayBuffer( | ||
stream: ReadableStream<Uint8Array>, | ||
): Promise<Uint8Array> { | ||
const chunks: Array<Uint8Array> = [] | ||
|
||
for await (const chunk of stream) { | ||
chunks.push(chunk) | ||
} | ||
|
||
return concatArrayBuffers(chunks) | ||
} | ||
|
||
/** | ||
* This function awaits for the whole stream before returning the string. | ||
* | ||
* NOTE: we should improve the bond between the custom V8 runtime and the | ||
* renderToReadableStream React function to return a stream directly to the client. | ||
*/ | ||
export async function streamToString( | ||
stream: ReadableStream<Uint8Array>, | ||
): Promise<string> { | ||
const buffer = await streamToArrayBuffer(stream) | ||
return new TextDecoder().decode(buffer) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.