-
-
Notifications
You must be signed in to change notification settings - Fork 833
fix(bun): polyfill ReadableStream to strip unsupported type direct #4265
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
base: main
Are you sure you want to change the base?
Changes from all commits
56edebf
62f94cf
d100d4c
dcc6be3
1618f28
fe38d54
d069445
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,28 @@ | ||
| import "#nitro/virtual/polyfills"; | ||
|
|
||
| // React 19's server.edge.js uses ReadableStream({ type: "direct", ... }), a | ||
| // Cloudflare Workers extension. Bun follows the web spec strictly and throws | ||
| // ERR_INVALID_ARG_VALUE for unknown `type` values. Strip it before it reaches | ||
| // Bun's constructor so prerendering works without switching to the node preset. | ||
| // Using class extends preserves the prototype chain so instanceof checks work correctly. | ||
| const _OriginalReadableStream = globalThis.ReadableStream; | ||
| // @ts-expect-error -- TypeScript cannot resolve overloaded ReadableStream constructor generics via class extends | ||
| class _PatchedReadableStream extends _OriginalReadableStream { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please check if there is already an upstream tracker issue we can link? (or we can make a minimal report for Bun)
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I filed one upstream: oven-sh/bun#30966. The Bun team closed it as That is really the case for handling it here: React 19's |
||
| constructor( | ||
| underlyingSource?: UnderlyingDefaultSource | UnderlyingByteSource, | ||
| strategy?: QueuingStrategy | ||
| ) { | ||
| if (underlyingSource && (underlyingSource as Record<string, unknown>).type === "direct") { | ||
| const { type: _type, ...rest } = underlyingSource as Record<string, unknown>; | ||
| super(rest as UnderlyingDefaultSource, strategy); | ||
| } else { | ||
| super(underlyingSource as UnderlyingDefaultSource, strategy); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| globalThis.ReadableStream = _PatchedReadableStream; | ||
|
|
||
| import type { ServerRequest } from "srvx"; | ||
| import { serve } from "srvx/bun"; | ||
| import wsAdapter from "crossws/adapters/bun"; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| export default () => { | ||
| const encoder = new TextEncoder(); | ||
| const stream = new ReadableStream({ | ||
| // @ts-expect-error - direct is a Cloudflare extension | ||
| type: "direct", | ||
| start(controller) { | ||
| controller.enqueue(encoder.encode("bun-direct")); | ||
| controller.close(); | ||
| }, | ||
| }); | ||
|
|
||
| return { | ||
| isStream: stream instanceof ReadableStream, | ||
| hasCorrectPrototype: Object.getPrototypeOf(stream) === ReadableStream.prototype, | ||
| }; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,39 @@ | ||
| import { execa, execaCommandSync } from "execa"; | ||
| import { getRandomPort, waitForPort } from "get-port-please"; | ||
| import { resolve } from "pathe"; | ||
| import { describe } from "vitest"; | ||
| import { describe, it, expect } from "vitest"; | ||
| import { setupTest, testNitro } from "../tests.ts"; | ||
|
|
||
| const hasBun = execaCommandSync("bun --version", { stdio: "ignore", reject: false }).exitCode === 0; | ||
|
|
||
| describe.runIf(hasBun)("nitro:preset:bun", async () => { | ||
| const ctx = await setupTest("bun"); | ||
| testNitro(ctx, async () => { | ||
| const port = await getRandomPort(); | ||
| process.env.PORT = String(port); | ||
| execa("bun", [resolve(ctx.outDir, "server/index.mjs")], { | ||
| stdio: "inherit", | ||
| }); | ||
| ctx.server = { | ||
| url: `http://127.0.0.1:${port}`, | ||
| close: () => { | ||
| // p.kill() | ||
| }, | ||
| } as any; | ||
| await waitForPort(port); | ||
| return async ({ url, ...opts }) => { | ||
| const res = await ctx.fetch(url, opts); | ||
| return res; | ||
| }; | ||
| }); | ||
| testNitro( | ||
| ctx, | ||
| async () => { | ||
| const port = await getRandomPort(); | ||
| process.env.PORT = String(port); | ||
| execa("bun", [resolve(ctx.outDir, "server/index.mjs")], { | ||
| stdio: "inherit", | ||
| }); | ||
| ctx.server = { | ||
| url: `http://127.0.0.1:${port}`, | ||
| close: () => { | ||
| // p.kill() | ||
| }, | ||
| } as any; | ||
| await waitForPort(port); | ||
| return async ({ url, ...opts }) => { | ||
| const res = await ctx.fetch(url, opts); | ||
| return res; | ||
| }; | ||
| }, | ||
| (ctx, callHandler) => { | ||
| it("bun: ReadableStream polyfill works", async () => { | ||
| const { data } = await callHandler({ url: "/bun-direct-stream" }); | ||
| expect(data.isStream).toBe(true); | ||
| expect(data.hasCorrectPrototype).toBe(true); | ||
| }); | ||
| } | ||
| ); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.