Skip to content

Commit b4455a6

Browse files
authored
[react-dom] Include all Node.js APIs in Bun entrypoint for /server (facebook#34193)
1 parent 17b3765 commit b4455a6

File tree

7 files changed

+65
-4
lines changed

7 files changed

+65
-4
lines changed

packages/react-dom/npm/server.bun.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ if (process.env.NODE_ENV === 'production') {
1212

1313
exports.version = b.version;
1414
exports.renderToReadableStream = b.renderToReadableStream;
15+
exports.renderToPipeableStream = b.renderToPipeableStream;
16+
exports.resumeToPipeableStream = b.resumeToPipeableStream;
1517
exports.resume = b.resume;
1618
exports.renderToString = l.renderToString;
1719
exports.renderToStaticMarkup = l.renderToStaticMarkup;

packages/react-dom/server.bun.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,17 @@ export function resume() {
3838
arguments,
3939
);
4040
}
41+
42+
export function renderToPipeableStream() {
43+
return require('./src/server/react-dom-server.bun').renderToPipeableStream.apply(
44+
this,
45+
arguments,
46+
);
47+
}
48+
49+
export function resumeToPipeableStream() {
50+
return require('./src/server/react-dom-server.bun').resumeToPipeableStream.apply(
51+
this,
52+
arguments,
53+
);
54+
}

packages/react-dom/src/server/react-dom-server.bun.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,14 @@
88
*/
99

1010
export * from './ReactDOMFizzServerBun.js';
11+
export {
12+
renderToPipeableStream,
13+
resumeToPipeableStream,
14+
resume,
15+
} from './ReactDOMFizzServerNode.js';
16+
export {
17+
prerenderToNodeStream,
18+
prerender,
19+
resumeAndPrerenderToNodeStream,
20+
resumeAndPrerender,
21+
} from './ReactDOMFizzStaticNode.js';

packages/react-dom/src/server/react-dom-server.bun.stable.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,14 @@
88
*/
99

1010
export {renderToReadableStream, version} from './ReactDOMFizzServerBun.js';
11+
export {
12+
renderToPipeableStream,
13+
resume,
14+
resumeToPipeableStream,
15+
} from './ReactDOMFizzServerNode.js';
16+
export {
17+
prerenderToNodeStream,
18+
prerender,
19+
resumeAndPrerenderToNodeStream,
20+
resumeAndPrerender,
21+
} from './ReactDOMFizzStaticNode.js';

packages/react-server/src/ReactServerStreamConfigBun.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,22 @@
99

1010
/* global Bun */
1111

12+
import type {Writable} from 'stream';
13+
1214
type BunReadableStreamController = ReadableStreamController & {
1315
end(): mixed,
1416
write(data: Chunk | BinaryChunk): void,
1517
error(error: Error): void,
1618
flush?: () => void,
1719
};
18-
export type Destination = BunReadableStreamController;
20+
21+
interface MightBeFlushable {
22+
flush?: () => void;
23+
}
24+
25+
export type Destination =
26+
| BunReadableStreamController
27+
| (Writable & MightBeFlushable);
1928

2029
export type PrecomputedChunk = string;
2130
export opaque type Chunk = string;
@@ -46,13 +55,15 @@ export function writeChunk(
4655
return;
4756
}
4857

58+
// $FlowFixMe[incompatible-call]: write() is compatible with both types in Bun
4959
destination.write(chunk);
5060
}
5161

5262
export function writeChunkAndReturn(
5363
destination: Destination,
5464
chunk: PrecomputedChunk | Chunk | BinaryChunk,
5565
): boolean {
66+
// $FlowFixMe[incompatible-call]: write() is compatible with both types in Bun
5667
return !!destination.write(chunk);
5768
}
5869

@@ -86,11 +97,21 @@ export function byteLengthOfBinaryChunk(chunk: BinaryChunk): number {
8697
}
8798

8899
export function closeWithError(destination: Destination, error: mixed): void {
100+
// $FlowFixMe[incompatible-use]
89101
// $FlowFixMe[method-unbinding]
90102
if (typeof destination.error === 'function') {
91103
// $FlowFixMe[incompatible-call]: This is an Error object or the destination accepts other types.
92104
destination.error(error);
93-
} else {
105+
106+
// $FlowFixMe[incompatible-use]
107+
// $FlowFixMe[method-unbinding]
108+
} else if (typeof destination.destroy === 'function') {
109+
// $FlowFixMe[incompatible-call]: This is an Error object or the destination accepts other types.
110+
destination.destroy(error);
111+
112+
// $FlowFixMe[incompatible-use]
113+
// $FlowFixMe[method-unbinding]
114+
} else if (typeof destination.close === 'function') {
94115
// Earlier implementations doesn't support this method. In that environment you're
95116
// supposed to throw from a promise returned but we don't return a promise in our
96117
// 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 {
101122
}
102123
}
103124

104-
export function createFastHash(input: string): string | number {
125+
export function createFastHash(input: string): number {
105126
return Bun.hash(input);
106127
}
107128

scripts/rollup/bundles.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ const bundles = [
405405
global: 'ReactDOMServer',
406406
minifyWithProdErrorCodes: false,
407407
wrapWithModuleBoundaries: false,
408-
externals: ['react', 'react-dom'],
408+
externals: ['react', 'react-dom', 'crypto', 'stream', 'util'],
409409
},
410410

411411
/******* React DOM Fizz Server External Runtime *******/

scripts/shared/inlinedHostConfigs.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ module.exports = [
250250
'react-dom/server.bun',
251251
'react-dom/src/server/react-dom-server.bun',
252252
'react-dom/src/server/ReactDOMFizzServerBun.js',
253+
'react-dom/src/server/ReactDOMFizzServerNode.js',
254+
'react-dom/src/server/ReactDOMFizzStaticNode.js',
253255
'react-dom-bindings',
254256
'react-dom-bindings/src/server/ReactDOMFlightServerHostDispatcher.js',
255257
'react-dom-bindings/src/server/ReactFlightServerConfigDOM.js',

0 commit comments

Comments
 (0)