Unverified Commit 5903c030 authored by Webdesign29's avatar Webdesign29 Committed by GitHub

perf: use Buffer.from for base64 encoding of binary Flight data (#91221)

## Summary

Replace `btoa(String.fromCodePoint(...chunk))` with
`Buffer.from().toString('base64')` for encoding binary Flight data
chunks in `writeFlightDataInstruction`.

The spread operator `...chunk` converts the entire Uint8Array into
individual arguments on the call stack. For a 64KB binary chunk, this
creates 65,536 arguments — causing:
- Significant call stack pressure (V8's argument limit is ~65K)
- Temporary JS string allocation from `String.fromCodePoint`
- The entire chunk must be converted to a JS string before base64
encoding

`Buffer.from().toString('base64')` performs base64 encoding natively in
C++ without any intermediate string allocation or argument spreading.

**Edge runtime compatibility**: Falls back to the original `btoa` path
where `Buffer` is unavailable.

## Test plan

- [x] TypeScript compilation passes
- [x] Prettier and ESLint pass (pre-commit hooks)
- [x] Produces identical base64 output
- [x] Edge runtime falls back to original path (no `Buffer` available)
- [x] Node.js runtime uses `Buffer.from` for native encoding


🤖 Generated with [Claude Code](https://claude.com/claude-code)
parent 2accf8bc
...@@ -241,7 +241,14 @@ function writeFlightDataInstruction( ...@@ -241,7 +241,14 @@ function writeFlightDataInstruction(
// Instead let's inline it in base64. // Instead let's inline it in base64.
// Credits to Devon Govett (devongovett) for the technique. // Credits to Devon Govett (devongovett) for the technique.
// https://github.com/devongovett/rsc-html-stream // https://github.com/devongovett/rsc-html-stream
const base64 = btoa(String.fromCodePoint(...chunk)) const base64 =
typeof Buffer !== 'undefined'
? Buffer.from(
chunk.buffer,
chunk.byteOffset,
chunk.byteLength
).toString('base64')
: btoa(String.fromCodePoint(...chunk))
htmlInlinedData = htmlEscapeJsonString( htmlInlinedData = htmlEscapeJsonString(
JSON.stringify([INLINE_FLIGHT_PAYLOAD_BINARY, base64]) JSON.stringify([INLINE_FLIGHT_PAYLOAD_BINARY, base64])
) )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment