Skip to content

fix(solid-js): harden streaming wrapper - #804

Merged
harlan-zw merged 4 commits into
mainfrom
fix/solid-streaming-wrap-stream
Jun 29, 2026
Merged

harlan-zw merged 4 commits into
mainfrom
fix/solid-streaming-wrap-stream

Conversation

@harlan-zw

@harlan-zw harlan-zw commented Jun 29, 2026 •

Copy link
Copy Markdown
Collaborator

🔗 Linked issue

Related to #789

❓ Type of change

  • 📖 Documentation
  • 🐞 Bug fix
  • 👌 Enhancement
  • ✨ New feature
  • 🧹 Chore
  • ⚠️ Breaking change

📚 Description

Splits the Solid streaming wrapper work from the broader streaming contracts branch into a focused @unhead/solid-js PR.

wrapStream now reads the app stream as soon as the wrapper starts, buffers app chunks until onCompleteShell, forwards inner stream errors before and after shell completion, cancels the inner reader when the outer stream is cancelled, and releases the inner reader on completion. The Solid streaming tests cover those cases, and the existing Solid e2e streaming test now imports the Solid integration instead of the core stream helpers.

Summary by CodeRabbit

  • Bug Fixes
    • Improved streaming head rendering by starting output earlier and flushing buffered chunks in order once the head shell is ready.
    • Strengthened coordination for errors, cancellation, and normal completion to avoid hangs and ensure correct end-of-stream behavior.
  • Tests
    • Added local streaming helpers and expanded wrapStream coverage for inner errors (before/after shell readiness), cancellation propagation, and completion behavior.
    • Improved streaming e2e test stability for XSS title assertions and test setup order.
  • Examples
    • Updated the SSR streaming example to mount into #root and adjust JSON-LD injection behavior.

@coderabbitai

coderabbitai Bot commented Jun 29, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 35d3d743-058a-4f16-9229-ac878ce215b4

📥 Commits

Reviewing files that changed from the base of the PR and between f0e4930 and 9b04ec8.

📒 Files selected for processing (1)
  • examples/vite-ssr-solidjs-streaming/src/components/Reviews.tsx
🚧 Files skipped from review as they are similar to previous changes (1)
  • examples/vite-ssr-solidjs-streaming/src/components/Reviews.tsx

📝 Walkthrough

Walkthrough

wrapStream now reads the inner stream immediately, buffers output until shell readiness, and flushes shell, buffered chunks, and the end marker in order. The Solid streaming example now targets #root, emits review JSON-LD unconditionally, and uses the updated Unhead streaming plugin.

Changes

wrapStream rewrite and streaming setup

Layer / File(s) Summary
wrapStream core implementation
packages/solid-js/src/stream/server.ts
Replaces the linear start flow with buffered output state, concurrent shell readiness handling, explicit failure and close helpers, and cancellation that stops output, cancels the inner reader, and waits for the pump to finish.
Streaming tests and e2e assertion updates
packages/solid-js/test/streaming.test.ts, packages/solid-js/test/e2e-streaming.test.ts
Adds shared stream test helpers, new wrapStream coverage for error, cancellation, and completion paths, and updates the e2e XSS assertion to inspect the rendered title content.
Solid streaming example wiring
examples/vite-ssr-solidjs-streaming/index.html, examples/vite-ssr-solidjs-streaming/src/components/Reviews.tsx, examples/vite-ssr-solidjs-streaming/src/entry-client.tsx, examples/vite-ssr-solidjs-streaming/vite.config.ts
Switches the example to a #root mount point, always injects the review JSON-LD script, and updates the Vite Unhead plugin import and invocation for streaming.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~40 minutes

Poem

🐇 I nibbled the stream as it softly unwound,
Shell then the carrots came marching around.
Buffered and buffered, then pop! out they flow,
#root got the bunny-bright review-show.
Hop, cancel, stream — all tidy and neat!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise, conventional, and accurately summarizes the main Solid streaming wrapper hardening change.
Description check ✅ Passed The description matches the template with a linked issue, change type, and a detailed summary of the bug fix.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/solid-streaming-wrap-stream

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

github-actions Bot commented Jun 29, 2026 •

Copy link
Copy Markdown
Contributor

📦 Bundle Size

✅ No notable changes

All bundles (12)
Bundle Gzipped Raw
Core
Client (Minimal) 4.7 kB 11.7 kB ✅
Client (Full) 7.9 kB 20.6 kB ✅
Server (Minimal) 4.5 kB 11.4 kB ✅
Vue
Vue Client (Minimal) 5.2 kB 12.8 kB ✅
Vue Client (Full) 8.7 kB 22.5 kB ✅
Vue Server (Minimal) 5 kB 12.3 kB ✅
React
React Client (Minimal) 5.1 kB 12.7 kB ✅
React Client (Full) 8.5 kB 22.1 kB ✅
React Server (Minimal) 4.8 kB 12 kB ✅
Schema.org
Schema.org (Minimal) 9.5 kB 26.3 kB ✅
Schema.org Imports 0.1 kB 0.1 kB ✅
Schema.org Vue Meta 0.4 kB 0.8 kB ✅

⚡ Performance (directional)

✅ No significant change (within CI noise)

All benchmarks (6)
Benchmark PR Δ RME
SSR render (CPU) 0.441 ms ~ noise ±9.1%
SSR render (wall) 0.285 ms ~ noise ±4.6%
SSR allocated / render 317.8 KiB ~ noise —
CSR DOM mutations / nav 38 ~ noise —
CSR re-render (CPU) 0.736 ms ~ noise ±5.0%
CSR re-render (wall) 0.423 ms ~ noise ±2.7%

Baseline: main @ 49ccade · 2026-06-28 · gzipped is the headline size metric · perf is directional (shared-runner, gated)

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
packages/solid-js/src/stream/server.ts (1)

143-145: 🚀 Performance & Scalability | 🔵 Trivial | ⚡ Quick win

Avoid quadratic flushing of buffered chunks.

Repeated shift() reindexes the array for every buffered chunk. Iterate and clear the buffer instead.

Suggested change
-              while (bufferedChunks.length) {
-                controller.enqueue(bufferedChunks.shift()!)
-              }
+              for (const chunk of bufferedChunks) {
+                controller.enqueue(chunk)
+              }
+              bufferedChunks.length = 0
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/solid-js/src/stream/server.ts` around lines 143 - 145, The buffered
chunk flush in server streaming is doing repeated array reindexing via
bufferedChunks.shift(), which can become quadratic. Update the flush logic in
the streaming path around the bufferedChunks loop to iterate through the
existing items and then clear the buffer afterward, using the existing
controller.enqueue flow without mutating the array per item.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/solid-js/src/stream/server.ts`:
- Around line 190-192: The catch block in the server stream path currently calls
fail(error) immediately, which can discard already buffered chunks from the
ReadableStream. Update the error handling in the stream logic around the catch
path so that buffered shell/app output is allowed to drain before the stream is
failed, or explicitly make this behavior intentional with a test. Use the
surrounding stream flow in server.ts and the relevant ReadableStream
controller/fail handling to locate the fix.

In `@packages/solid-js/test/e2e-streaming.test.ts`:
- Line 9: Tighten the XSS check by broadening XSS_RE in e2e-streaming.test.ts so
it matches any unexpected script tag, not just a specific
<title>...<script>alert... pattern. Update the assertion logic around the XSS
test cases to detect script tags with attributes, line breaks, or scripts
emitted outside the title, using the XSS_RE constant and the related test
expectations near the affected assertions.

---

Nitpick comments:
In `@packages/solid-js/src/stream/server.ts`:
- Around line 143-145: The buffered chunk flush in server streaming is doing
repeated array reindexing via bufferedChunks.shift(), which can become
quadratic. Update the flush logic in the streaming path around the
bufferedChunks loop to iterate through the existing items and then clear the
buffer afterward, using the existing controller.enqueue flow without mutating
the array per item.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 92d82874-deed-4ea2-b7bb-49f823427508

📥 Commits

Reviewing files that changed from the base of the PR and between 49ccade and 5afe03f.

📒 Files selected for processing (3)
  • packages/solid-js/src/stream/server.ts
  • packages/solid-js/test/e2e-streaming.test.ts
  • packages/solid-js/test/streaming.test.ts

Comment thread packages/solid-js/src/stream/server.ts Resolved

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
examples/vite-ssr-solidjs-streaming/src/components/Reviews.tsx (1)

25-34: 🔒 Security & Privacy | 🔵 Trivial | ⚡ Quick win

Use textContent for the JSON-LD script.

Line 28 uses innerHTML on an application/ld+json entry. That works with the current numeric payload, but it skips Unhead’s safer JSON-LD serialization path and makes this example easy to cargo-cult into an XSS bug once string fields get added later.

Suggested change
     script: [
       {
         type: 'application/ld+json',
-        innerHTML: JSON.stringify({
+        textContent: JSON.stringify({
           '`@context`': 'https://schema.org',
           '`@type`': 'AggregateRating',
           ratingValue: DATA.avgRating,
           reviewCount: DATA.reviews.length,
         }),
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@examples/vite-ssr-solidjs-streaming/src/components/Reviews.tsx` around lines
25 - 34, The JSON-LD script in Reviews should not use innerHTML for the
application/ld+json entry. Update the script object to use textContent instead
so the Unhead JSON-LD path is used safely, and keep the change in the Reviews
component where the schema.org AggregateRating object is built.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@examples/vite-ssr-solidjs-streaming/src/components/Reviews.tsx`:
- Around line 25-34: The JSON-LD script in Reviews should not use innerHTML for
the application/ld+json entry. Update the script object to use textContent
instead so the Unhead JSON-LD path is used safely, and keep the change in the
Reviews component where the schema.org AggregateRating object is built.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c316fa61-e35c-442b-bd88-f319a646181e

📥 Commits

Reviewing files that changed from the base of the PR and between 2a5f9d4 and f0e4930.

📒 Files selected for processing (4)
  • examples/vite-ssr-solidjs-streaming/index.html
  • examples/vite-ssr-solidjs-streaming/src/components/Reviews.tsx
  • examples/vite-ssr-solidjs-streaming/src/entry-client.tsx
  • examples/vite-ssr-solidjs-streaming/vite.config.ts
✅ Files skipped from review due to trivial changes (1)
  • examples/vite-ssr-solidjs-streaming/index.html

@harlan-zw
harlan-zw merged commit 9b38328 into main Jun 29, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant