Commit 2c96039
Replace NDJSON envelope with length-prefixed streaming protocol (#2615)
## Summary
Closes #2614
- Replace `JSON.stringify` wrapping of HTML content in the Node→Ruby
streaming protocol with a length-prefixed format that sends raw content
bytes without escaping
- Auto-detect format on Ruby side for backward compatibility with older
node renderers
- Refactor console replay logging into a shared method to handle both
Hash and String chunks
## Problem
The current NDJSON protocol wraps every HTML chunk in a JSON envelope:
```json
{"html":"<div>...escaped HTML...</div>","consoleReplayScript":"","hasErrors":false,"isShellReady":true}\n
```
`JSON.stringify` escapes every `"`, `\`, and `\n` in the HTML content —
adding **~30% overhead** for typical payloads. Ruby then `JSON.parse`s
each line, undoing all the escaping. This serialize→deserialize
round-trip is pure waste for the bulk content (HTML is 99.9% of each
chunk by size).
## Solution
New wire format per chunk:
```
<metadata JSON>\t<content byte length hex>\n<raw content bytes>
```
Example:
```
{"consoleReplayScript":"","hasErrors":false,"isShellReady":true}\t00028A3C\n<div>...(raw HTML, zero escaping)...
```
- **Metadata** (~80 bytes): small JSON without the `html` field —
negligible escaping cost
- **Content** (bulk): raw bytes with length prefix — **zero** escaping
overhead
- **Ruby parser**: reads header line (until `\n`), extracts content
length, reads exactly that many raw bytes
- **Auto-detection**: if the header line contains `\t`, it's
length-prefixed; otherwise legacy NDJSON (backward compatible)
## Changes
| File | Change |
|------|--------|
| `streamingUtils.ts` | Write `metadata\tcontent_length\nraw_content`
instead of `JSON.stringify(envelope)\n` |
| `stream_request.rb` | Replace `loop_response_lines` with
`loop_response_chunks` (length-prefixed parser with auto-detection) |
| `ruby_embedded_java_script.rb` | Handle both Hash (new) and String
(legacy) chunks; extract shared `replay_console_to_rails_logger` |
| `streamServerRenderedReactComponent.test.jsx` | Update
`expectStreamChunk` to parse length-prefixed format |
| `removeRSCChunkStack.ts` | Update to parse length-prefixed format with
NDJSON fallback |
## Test plan
- [x] `streamServerRenderedReactComponent.test.jsx` — 16/16 pass (core
streaming tests)
- [x] `concurrentRSCPayloadGeneration.rsc.test.tsx` — pass (parallel RSC
rendering)
- [x] `RSCSerialization.rsc.test.tsx` — pass
- [x] `ReactOnRailsRSC.rsc.test.tsx` — pass
- [x] `serverRenderRSCReactComponent.rsc.test.tsx` — pass
- [x] `injectRSCPayload.test.ts` — 9/9 pass
- [ ] Integration tests with real node renderer
- [ ] E2E tests with dummy app
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **Improvements**
* Switched streaming to a length‑prefixed protocol for atomic chunk
delivery, correct multibyte handling, and compatibility with legacy
newline‑delimited streams.
* More reliable stream parsing with improved server-side console
replay/logging, clearer error signaling, and robust end‑of‑stream
handling.
* **Tests**
* Added tests validating length‑prefixed parsing, including UTF‑8
multibyte content.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]>
Co-authored-by: Justin Gordon <[email protected]>1 parent da37b46 commit 2c96039
39 files changed
Lines changed: 864 additions & 498 deletions
File tree
- packages
- react-on-rails-pro-node-renderer
- src/worker
- tests
- react-on-rails-pro
- src
- tests
- utils
- react-on-rails
- src
- base
- types
- tests
- react_on_rails_pro
- app
- helpers
- views/react_on_rails_pro
- lib/react_on_rails_pro
- concerns
- spec
- dummy/spec
- helpers
- requests
- react_on_rails_pro
- react_on_rails
- lib/react_on_rails
- server_rendering_pool
- spec/dummy/spec
- helpers
- requests
Lines changed: 4 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
415 | 415 | | |
416 | 416 | | |
417 | 417 | | |
418 | | - | |
419 | | - | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
420 | 422 | | |
421 | 423 | | |
422 | 424 | | |
| |||
Lines changed: 23 additions & 25 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
6 | | - | |
7 | 5 | | |
8 | 6 | | |
9 | 7 | | |
| 8 | + | |
10 | 9 | | |
11 | 10 | | |
12 | 11 | | |
| |||
32 | 31 | | |
33 | 32 | | |
34 | 33 | | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
40 | 41 | | |
41 | 42 | | |
42 | 43 | | |
| |||
55 | 56 | | |
56 | 57 | | |
57 | 58 | | |
58 | | - | |
| 59 | + | |
59 | 60 | | |
60 | 61 | | |
61 | 62 | | |
62 | 63 | | |
63 | 64 | | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
| 65 | + | |
68 | 66 | | |
69 | 67 | | |
70 | 68 | | |
| |||
92 | 90 | | |
93 | 91 | | |
94 | 92 | | |
| 93 | + | |
95 | 94 | | |
96 | 95 | | |
97 | 96 | | |
98 | 97 | | |
99 | | - | |
100 | 98 | | |
101 | 99 | | |
102 | | - | |
| 100 | + | |
103 | 101 | | |
104 | 102 | | |
105 | 103 | | |
106 | 104 | | |
107 | | - | |
| 105 | + | |
108 | 106 | | |
109 | 107 | | |
110 | 108 | | |
111 | 109 | | |
112 | | - | |
| 110 | + | |
113 | 111 | | |
114 | 112 | | |
115 | 113 | | |
116 | 114 | | |
117 | | - | |
| 115 | + | |
118 | 116 | | |
119 | 117 | | |
120 | 118 | | |
121 | 119 | | |
122 | | - | |
| 120 | + | |
123 | 121 | | |
124 | 122 | | |
125 | 123 | | |
126 | 124 | | |
127 | | - | |
| 125 | + | |
128 | 126 | | |
129 | 127 | | |
130 | 128 | | |
131 | 129 | | |
132 | 130 | | |
133 | 131 | | |
134 | | - | |
| 132 | + | |
135 | 133 | | |
136 | | - | |
| 134 | + | |
137 | 135 | | |
138 | | - | |
| 136 | + | |
139 | 137 | | |
140 | | - | |
| 138 | + | |
141 | 139 | | |
142 | | - | |
| 140 | + | |
143 | 141 | | |
144 | | - | |
| 142 | + | |
145 | 143 | | |
146 | 144 | | |
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
405 | 405 | | |
406 | 406 | | |
407 | 407 | | |
408 | | - | |
| 408 | + | |
409 | 409 | | |
410 | 410 | | |
411 | 411 | | |
| |||
Lines changed: 4 additions & 20 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
30 | 31 | | |
31 | 32 | | |
32 | 33 | | |
33 | | - | |
34 | | - | |
| 34 | + | |
35 | 35 | | |
36 | 36 | | |
37 | | - | |
38 | 37 | | |
39 | 38 | | |
40 | 39 | | |
41 | 40 | | |
42 | 41 | | |
43 | 42 | | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
| 43 | + | |
61 | 44 | | |
62 | 45 | | |
63 | 46 | | |
| |||
80 | 63 | | |
81 | 64 | | |
82 | 65 | | |
| 66 | + | |
83 | 67 | | |
84 | 68 | | |
85 | 69 | | |
| |||
Lines changed: 4 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
122 | 122 | | |
123 | 123 | | |
124 | 124 | | |
125 | | - | |
126 | | - | |
127 | | - | |
| 125 | + | |
128 | 126 | | |
129 | 127 | | |
130 | 128 | | |
| |||
142 | 140 | | |
143 | 141 | | |
144 | 142 | | |
145 | | - | |
| 143 | + | |
146 | 144 | | |
147 | 145 | | |
148 | 146 | | |
149 | 147 | | |
150 | 148 | | |
151 | 149 | | |
152 | | - | |
153 | | - | |
| 150 | + | |
| 151 | + | |
154 | 152 | | |
155 | 153 | | |
156 | 154 | | |
| |||
Lines changed: 34 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
Lines changed: 21 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
| |||
69 | 70 | | |
70 | 71 | | |
71 | 72 | | |
72 | | - | |
| 73 | + | |
73 | 74 | | |
74 | 75 | | |
75 | 76 | | |
| |||
78 | 79 | | |
79 | 80 | | |
80 | 81 | | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
| 82 | + | |
86 | 83 | | |
87 | 84 | | |
88 | 85 | | |
| |||
92 | 89 | | |
93 | 90 | | |
94 | 91 | | |
95 | | - | |
| 92 | + | |
96 | 93 | | |
97 | 94 | | |
98 | 95 | | |
| |||
112 | 109 | | |
113 | 110 | | |
114 | 111 | | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
115 | 127 | | |
116 | 128 | | |
117 | 129 | | |
118 | 130 | | |
119 | 131 | | |
120 | | - | |
| 132 | + | |
121 | 133 | | |
122 | 134 | | |
123 | 135 | | |
| |||
138 | 150 | | |
139 | 151 | | |
140 | 152 | | |
141 | | - | |
| 153 | + | |
142 | 154 | | |
143 | 155 | | |
144 | 156 | | |
| |||
0 commit comments