feat: return UTF-8 buffers from Node render#402
Conversation
There was a problem hiding this comment.
Pull request overview
This PR extends WebUI’s Node runtime rendering surface with a buffered API (Protocol.renderBuffer()) that returns UTF-8 output as a Node.js Buffer, enabling HTTP responses to write bytes directly without creating a JavaScript string. It reuses the existing protocol decode/index and render pipeline, keeps the existing string and streaming APIs unchanged, and updates docs/examples/benchmarks to validate equivalence and measure wins.
Changes:
- Add Node-facing
Protocol.renderBuffer()(TS wrapper + native addon export) returning aBuffer. - Add equivalence tests (UTF-8 buffer vs string) and extend the Node addon benchmark runner/README to include the new render path.
- Update docs (
DESIGN.md, package README, integrations guide, and examples) to document the new API and recommended usage.
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| packages/webui/test/integration.test.ts | Adds an integration test asserting renderBuffer() returns UTF-8 bytes equivalent to render(). |
| packages/webui/src/index.ts | Adds the public Protocol.renderBuffer() wrapper and factors JSON serialization into serializeState(). |
| packages/webui/README.md | Documents protocol.renderBuffer() and updates wording around protocol ownership/byte-per-request APIs. |
| examples/integration/node/index.js | Switches the example to emit a Buffer via renderBuffer() to stdout. |
| examples/integration/node-addon-bench/README.md | Documents the new benchmark row and updates output-equivalence validation description. |
| examples/integration/node-addon-bench/bench.mjs | Adds equivalence checks for buffer output and includes a new benchmark case for renderBuffer(). |
| docs/guide/integrations/node.md | Updates Node integration docs to recommend renderBuffer() for buffered HTTP writes and adds it to the API table/example. |
| DESIGN.md | Updates the Node package API contract to include Protocol.renderBuffer() semantics. |
| crates/webui-node/src/lib.rs | Implements the native render_buffer N-API method and adds a Rust-side equivalence test. |
| BENCHMARKS.md | Updates node-addon benchmark description to include string vs buffer render paths. |
684f2ee to
8e81627
Compare
|
Thanks for measuring this. I’m fine with a breaking change here, and I’d prefer the API to have one canonical buffered render method rather than both Please change Please update the docs, examples, tests, and benchmark naming to reflect that API. |
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
8e81627 to
476d40f
Compare
Summary
Protocol.render()return the canonical UTF-8 NodeBuffer; remove the separaterenderBuffer()API.renderStream()remains unchanged.Call
.toString("utf8")explicitly when JavaScript string operations are required.Local benchmark
The old String and new Buffer implementations were measured in the same release run on Linux x64 with Node 24.18.0. Canonical
render()now uses the measured Buffer path.The Buffer path avoids creating a V8 JavaScript string and lets napi-rs expose the rendered Rust allocation as an external
Buffer, so the benefit grows with output size.Output bytes are identical across buffered and streaming paths.
Validation
cargo xtask checkcargo xtask bench node-addon --baseline before-opt