[react-dom] Include all Node.js APIs in Bun entrypoint for /server (#34193)

This commit is contained in:
Michael H 2025-10-28 09:06:45 +11:00 committed by GitHub
parent 17b3765244
commit b4455a6ee6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 65 additions and 4 deletions

View File

@ -12,6 +12,8 @@ if (process.env.NODE_ENV === 'production') {
exports.version = b.version;
exports.renderToReadableStream = b.renderToReadableStream;
exports.renderToPipeableStream = b.renderToPipeableStream;
exports.resumeToPipeableStream = b.resumeToPipeableStream;
exports.resume = b.resume;
exports.renderToString = l.renderToString;
exports.renderToStaticMarkup = l.renderToStaticMarkup;

View File

@ -38,3 +38,17 @@ export function resume() {
arguments,
);
}
export function renderToPipeableStream() {
return require('./src/server/react-dom-server.bun').renderToPipeableStream.apply(
this,
arguments,
);
}
export function resumeToPipeableStream() {
return require('./src/server/react-dom-server.bun').resumeToPipeableStream.apply(
this,
arguments,
);
}

View File

@ -8,3 +8,14 @@
*/
export * from './ReactDOMFizzServerBun.js';
export {
renderToPipeableStream,
resumeToPipeableStream,
resume,
} from './ReactDOMFizzServerNode.js';
export {
prerenderToNodeStream,
prerender,
resumeAndPrerenderToNodeStream,
resumeAndPrerender,
} from './ReactDOMFizzStaticNode.js';

View File

@ -8,3 +8,14 @@
*/
export {renderToReadableStream, version} from './ReactDOMFizzServerBun.js';
export {
renderToPipeableStream,
resume,
resumeToPipeableStream,
} from './ReactDOMFizzServerNode.js';
export {
prerenderToNodeStream,
prerender,
resumeAndPrerenderToNodeStream,
resumeAndPrerender,
} from './ReactDOMFizzStaticNode.js';

View File

@ -9,13 +9,22 @@
/* global Bun */
import type {Writable} from 'stream';
type BunReadableStreamController = ReadableStreamController & {
end(): mixed,
write(data: Chunk | BinaryChunk): void,
error(error: Error): void,
flush?: () => void,
};
export type Destination = BunReadableStreamController;
interface MightBeFlushable {
flush?: () => void;
}
export type Destination =
| BunReadableStreamController
| (Writable & MightBeFlushable);
export type PrecomputedChunk = string;
export opaque type Chunk = string;
@ -46,6 +55,7 @@ export function writeChunk(
return;
}
// $FlowFixMe[incompatible-call]: write() is compatible with both types in Bun
destination.write(chunk);
}
@ -53,6 +63,7 @@ export function writeChunkAndReturn(
destination: Destination,
chunk: PrecomputedChunk | Chunk | BinaryChunk,
): boolean {
// $FlowFixMe[incompatible-call]: write() is compatible with both types in Bun
return !!destination.write(chunk);
}
@ -86,11 +97,21 @@ export function byteLengthOfBinaryChunk(chunk: BinaryChunk): number {
}
export function closeWithError(destination: Destination, error: mixed): void {
// $FlowFixMe[incompatible-use]
// $FlowFixMe[method-unbinding]
if (typeof destination.error === 'function') {
// $FlowFixMe[incompatible-call]: This is an Error object or the destination accepts other types.
destination.error(error);
} else {
// $FlowFixMe[incompatible-use]
// $FlowFixMe[method-unbinding]
} else if (typeof destination.destroy === 'function') {
// $FlowFixMe[incompatible-call]: This is an Error object or the destination accepts other types.
destination.destroy(error);
// $FlowFixMe[incompatible-use]
// $FlowFixMe[method-unbinding]
} else if (typeof destination.close === 'function') {
// Earlier implementations doesn't support this method. In that environment you're
// supposed to throw from a promise returned but we don't return a promise in our
// approach. We could fork this implementation but this is environment is an edge
@ -101,7 +122,7 @@ export function closeWithError(destination: Destination, error: mixed): void {
}
}
export function createFastHash(input: string): string | number {
export function createFastHash(input: string): number {
return Bun.hash(input);
}

View File

@ -405,7 +405,7 @@ const bundles = [
global: 'ReactDOMServer',
minifyWithProdErrorCodes: false,
wrapWithModuleBoundaries: false,
externals: ['react', 'react-dom'],
externals: ['react', 'react-dom', 'crypto', 'stream', 'util'],
},
/******* React DOM Fizz Server External Runtime *******/

View File

@ -250,6 +250,8 @@ module.exports = [
'react-dom/server.bun',
'react-dom/src/server/react-dom-server.bun',
'react-dom/src/server/ReactDOMFizzServerBun.js',
'react-dom/src/server/ReactDOMFizzServerNode.js',
'react-dom/src/server/ReactDOMFizzStaticNode.js',
'react-dom-bindings',
'react-dom-bindings/src/server/ReactDOMFlightServerHostDispatcher.js',
'react-dom-bindings/src/server/ReactFlightServerConfigDOM.js',