Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/presets/bun/runtime/bun.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,36 @@
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.
const _OriginalReadableStream = globalThis.ReadableStream;
Comment thread
harshagarwalnyu marked this conversation as resolved.
const _PatchedReadableStream = function ReadableStream(
underlyingSource?: UnderlyingDefaultSource | UnderlyingByteSource,
strategy?: QueuingStrategy,
) {
if (
underlyingSource &&
(underlyingSource as Record<string, unknown>).type === "direct"
) {
const { type: _type, ...rest } = underlyingSource as Record<string, unknown>;
return new _OriginalReadableStream(
rest as UnderlyingDefaultSource,
strategy,
);
}
return new _OriginalReadableStream(
underlyingSource as UnderlyingDefaultSource,
strategy,
);
} as unknown as typeof ReadableStream;

Object.setPrototypeOf(_PatchedReadableStream, _OriginalReadableStream);
_PatchedReadableStream.prototype = _OriginalReadableStream.prototype;

// @ts-expect-error -- intentional global override for compat
globalThis.ReadableStream = _PatchedReadableStream;

import type { ServerRequest } from "srvx";
import { serve } from "srvx/bun";
import wsAdapter from "crossws/adapters/bun";
Expand Down
15 changes: 15 additions & 0 deletions test/fixture/server/routes/bun-direct-stream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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,
};
};
7 changes: 6 additions & 1 deletion test/presets/bun.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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;
Expand All @@ -25,5 +25,10 @@ describe.runIf(hasBun)("nitro:preset:bun", async () => {
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);
});
});
});