-
Notifications
You must be signed in to change notification settings - Fork 46.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Flight] Allow String Chunks to Passthrough in Node streams and renderToMarkup #30131
Conversation
…chunks It can be efficient to accept raw string chunks to pass through a stream instead of encoding them into a binary copy first. However, to avoid having to deal with encoding them back to binary from the parser itself, this helper is limited to dealing with unsplit and unconcattened strings so that we can make some assumptions. This is mainly intended for use for pass-through within the same memory.
Also, let Node accept string chunks as long as they're following our expected constraints. This lets us test the mixed protocol using pass-throughs.
This lets us avoid the dependency on TextDecoder/TextEncoder.
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
@@ -75,12 +75,10 @@ export function renderToMarkup( | |||
options?: MarkupOptions, | |||
): Promise<string> { | |||
return new Promise((resolve, reject) => { | |||
const textEncoder = new TextEncoder(); | |||
const flightDestination = { | |||
push(chunk: string | null): boolean { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We currently only encode strings in these streams because there's no reason to send any binary data to the "client". However, if we enabled Blobs to be used inside e.g. <img src>
as planned, then there would be reason to do that and we can enable the stream config to allow pushing raw binary chunks and receive them here using processBinaryChunk.
It can be efficient to accept raw string chunks to pass through a stream instead of encoding them into a binary copy first.
Previously our Flight parsers didn't accept receiving string chunks. That's partly because we sometimes need to encode binary chunks anyway so string only transport isn't enough but some chunks can be strings. This adds a partial ability for chunks to be received as strings.
However, accepting strings comes with some downsides. E.g. if the strings are split up we need to buffer it which compromises the perf for the common case. If the chunk represents binary data, then we'd need to encode it back into a typed array which would require a TextEncoder dependency in the parser. If the string chunk represents a byte length encoded string we don't know how many unicode characters to read without measuring them in terms of binary - also requiring a TextEncoder.
This PR is mainly intended for use for pass-through within the same memory. We can simplify the implementation by assuming that any string chunk is passed as the original chunk. This requires that the server stream config doesn't arbitrarily concatenate strings (e.g. large strings should not be concatenated which is probably a good heuristic anyway). It also means that this is not suitable to be used with for example receiving string chunks on the client by passing them through SSR hydration data - except if the encoding that way was only used with chunks that were already encoded as strings by Flight.
Web streams mostly just work on binary data anyway so they can't use this.
In Node.js streams we concatenate precomputed and small strings into larger buffers. It might make sense to do that using string ropes instead. However, in the meantime we can at least pass large strings that are outside our buffer view size as raw strings. There's no benefit to us eagerly encoding those.
Also, let Node accept string chunks as long as they're following our expected constraints. This lets us test the mixed protocol using pass-throughs. This can also be useful when the RSC server is in the same environment as the SSR server as they don't have to go from strings to typed arrays back to strings.
Now we can also use this in the pass-through used in renderToMarkup. This lets us avoid the dependency on TextDecoder/TextEncoder in that package.