-
Notifications
You must be signed in to change notification settings - Fork 48.8k
Add Bun streaming server renderer #25597
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
fc96903
Add fizz server for Bun
8a207cb
Add additional bun plugins, remove comments
4923cb6
Remove logs
cfdff5d
Remove .min from file name
c1148aa
Fix build
598cc1c
Change bun target to cjs, use Closure compiler for prod build
e6c39fd
Clean up
d7078c6
Add error codes, export renderToNodeStream/renderToStaticNodeStream
bc000af
Fix imports
07d5fef
Remove esm from package.json.files
9ed1e91
Merge branch 'main' into add-bun-fizz-server
gnoff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
packages/react-client/src/forks/ReactFlightClientHostConfig.bun.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
export * from 'react-client/src/ReactFlightClientHostConfigBrowser'; | ||
export * from 'react-client/src/ReactFlightClientHostConfigStream'; | ||
export * from 'react-server-dom-webpack/src/ReactFlightClientWebpackBundlerConfig'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
'use strict'; | ||
|
||
var b; | ||
var l; | ||
if (process.env.NODE_ENV === 'production') { | ||
b = require('./cjs/react-dom-server.bun.production.min.js'); | ||
l = require('./cjs/react-dom-server-legacy.browser.production.min.js'); | ||
} else { | ||
b = require('./cjs/react-dom-server.bun.development.js'); | ||
l = require('./cjs/react-dom-server-legacy.browser.development.js'); | ||
} | ||
|
||
exports.version = b.version; | ||
exports.renderToReadableStream = b.renderToReadableStream; | ||
exports.renderToNodeStream = b.renderToNodeStream; | ||
exports.renderToStaticNodeStream = b.renderToStaticNodeStream; | ||
exports.renderToString = l.renderToString; | ||
exports.renderToStaticMarkup = l.renderToStaticMarkup; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
// This file is only used for tests. | ||
// It lazily loads the implementation so that we get the correct set of host configs. | ||
|
||
import ReactVersion from 'shared/ReactVersion'; | ||
export {ReactVersion as version}; | ||
|
||
export function renderToReadableStream() { | ||
return require('./src/server/ReactDOMFizzServerBun').renderToReadableStream.apply( | ||
this, | ||
arguments, | ||
); | ||
} | ||
|
||
export function renderToNodeStream() { | ||
return require('./src/server/ReactDOMFizzServerBun').renderToNodeStream.apply( | ||
this, | ||
arguments, | ||
); | ||
} | ||
|
||
export function renderToStaticNodeStream() { | ||
return require('./src/server/ReactDOMFizzServerBun').renderToStaticNodeStream.apply( | ||
this, | ||
arguments, | ||
); | ||
} | ||
|
||
export function renderToString() { | ||
return require('./src/server/ReactDOMLegacyServerBrowser').renderToString.apply( | ||
this, | ||
arguments, | ||
); | ||
} | ||
|
||
export function renderToStaticMarkup() { | ||
return require('./src/server/ReactDOMLegacyServerBrowser').renderToStaticMarkup.apply( | ||
this, | ||
arguments, | ||
); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import type {ReactNodeList} from 'shared/ReactTypes'; | ||
import type {BootstrapScriptDescriptor} from 'react-dom-bindings/src/server/ReactDOMServerFormatConfig'; | ||
|
||
import ReactVersion from 'shared/ReactVersion'; | ||
|
||
import { | ||
createRequest, | ||
startWork, | ||
startFlowing, | ||
abort, | ||
} from 'react-server/src/ReactFizzServer'; | ||
|
||
import { | ||
createResponseState, | ||
createRootFormatContext, | ||
} from 'react-dom-bindings/src/server/ReactDOMServerFormatConfig'; | ||
|
||
type Options = { | ||
identifierPrefix?: string, | ||
namespaceURI?: string, | ||
nonce?: string, | ||
bootstrapScriptContent?: string, | ||
bootstrapScripts?: Array<string | BootstrapScriptDescriptor>, | ||
bootstrapModules?: Array<string | BootstrapScriptDescriptor>, | ||
progressiveChunkSize?: number, | ||
signal?: AbortSignal, | ||
onError?: (error: mixed) => ?string, | ||
unstable_externalRuntimeSrc?: string | BootstrapScriptDescriptor, | ||
}; | ||
|
||
// TODO: Move to sub-classing ReadableStream. | ||
type ReactDOMServerReadableStream = ReadableStream & { | ||
allReady: Promise<void>, | ||
}; | ||
|
||
function renderToReadableStream( | ||
children: ReactNodeList, | ||
options?: Options, | ||
): Promise<ReactDOMServerReadableStream> { | ||
return new Promise((resolve, reject) => { | ||
let onFatalError; | ||
let onAllReady; | ||
const allReady = new Promise((res, rej) => { | ||
onAllReady = res; | ||
onFatalError = rej; | ||
}); | ||
|
||
function onShellReady() { | ||
const stream: ReactDOMServerReadableStream = (new ReadableStream( | ||
{ | ||
type: 'direct', | ||
pull: (controller): ?Promise<void> => { | ||
// $FlowIgnore | ||
startFlowing(request, controller); | ||
}, | ||
cancel: (reason): ?Promise<void> => { | ||
abort(request); | ||
}, | ||
}, | ||
// $FlowFixMe size() methods are not allowed on byte streams. | ||
{highWaterMark: 2048}, | ||
): any); | ||
// TODO: Move to sub-classing ReadableStream. | ||
stream.allReady = allReady; | ||
resolve(stream); | ||
} | ||
function onShellError(error: mixed) { | ||
// If the shell errors the caller of `renderToReadableStream` won't have access to `allReady`. | ||
// However, `allReady` will be rejected by `onFatalError` as well. | ||
// So we need to catch the duplicate, uncatchable fatal error in `allReady` to prevent a `UnhandledPromiseRejection`. | ||
allReady.catch(() => {}); | ||
reject(error); | ||
} | ||
const request = createRequest( | ||
children, | ||
createResponseState( | ||
options ? options.identifierPrefix : undefined, | ||
options ? options.nonce : undefined, | ||
options ? options.bootstrapScriptContent : undefined, | ||
options ? options.bootstrapScripts : undefined, | ||
options ? options.bootstrapModules : undefined, | ||
options ? options.unstable_externalRuntimeSrc : undefined, | ||
), | ||
createRootFormatContext(options ? options.namespaceURI : undefined), | ||
options ? options.progressiveChunkSize : undefined, | ||
options ? options.onError : undefined, | ||
onAllReady, | ||
onShellReady, | ||
onShellError, | ||
onFatalError, | ||
); | ||
if (options && options.signal) { | ||
const signal = options.signal; | ||
if (signal.aborted) { | ||
abort(request, (signal: any).reason); | ||
} else { | ||
const listener = () => { | ||
abort(request, (signal: any).reason); | ||
signal.removeEventListener('abort', listener); | ||
}; | ||
signal.addEventListener('abort', listener); | ||
} | ||
} | ||
startWork(request); | ||
}); | ||
} | ||
|
||
function renderToNodeStream() { | ||
throw new Error( | ||
'ReactDOMServer.renderToNodeStream(): The Node Stream API is not available ' + | ||
'in Bun. Use ReactDOMServer.renderToReadableStream() instead.', | ||
); | ||
} | ||
|
||
function renderToStaticNodeStream() { | ||
throw new Error( | ||
'ReactDOMServer.renderToStaticNodeStream(): The Node Stream API is not available ' + | ||
'in Bun. Use ReactDOMServer.renderToReadableStream() instead.', | ||
); | ||
} | ||
|
||
export { | ||
renderToReadableStream, | ||
renderToNodeStream, | ||
renderToStaticNodeStream, | ||
ReactVersion as version, | ||
}; |
10 changes: 10 additions & 0 deletions
10
packages/react-reconciler/src/forks/ReactFiberHostConfig.bun.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
export * from 'react-dom-bindings/src/client/ReactDOMHostConfig'; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
type BunReadableStreamController = ReadableStreamController & { | ||
end(): mixed, | ||
write(data: Chunk): void, | ||
error(error: Error): void, | ||
}; | ||
export type Destination = BunReadableStreamController; | ||
|
||
export type PrecomputedChunk = string; | ||
export opaque type Chunk = string; | ||
|
||
export function scheduleWork(callback: () => void) { | ||
callback(); | ||
} | ||
|
||
export function flushBuffered(destination: Destination) { | ||
// WHATWG Streams do not yet have a way to flush the underlying | ||
// transform streams. https://github.com/whatwg/streams/issues/960 | ||
} | ||
|
||
// AsyncLocalStorage is not available in bun | ||
export const supportsRequestStorage = false; | ||
export const requestStorage = (null: any); | ||
|
||
export function beginWriting(destination: Destination) {} | ||
|
||
export function writeChunk( | ||
destination: Destination, | ||
chunk: PrecomputedChunk | Chunk, | ||
): void { | ||
if (chunk.length === 0) { | ||
return; | ||
} | ||
|
||
destination.write(chunk); | ||
} | ||
|
||
export function writeChunkAndReturn( | ||
destination: Destination, | ||
chunk: PrecomputedChunk | Chunk, | ||
): boolean { | ||
return !!destination.write(chunk); | ||
} | ||
|
||
export function completeWriting(destination: Destination) {} | ||
|
||
export function close(destination: Destination) { | ||
destination.end(); | ||
} | ||
|
||
export function stringToChunk(content: string): Chunk { | ||
return content; | ||
} | ||
|
||
export function stringToPrecomputedChunk(content: string): PrecomputedChunk { | ||
return content; | ||
} | ||
|
||
export function closeWithError(destination: Destination, error: mixed): void { | ||
// $FlowFixMe[method-unbinding] | ||
if (typeof destination.error === 'function') { | ||
// $FlowFixMe: This is an Error object or the destination accepts other types. | ||
destination.error(error); | ||
} else { | ||
// 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 | ||
// case to begin with. It's even less common to run this in an older environment. | ||
// Even then, this is not where errors are supposed to happen and they get reported | ||
// to a global callback in addition to this anyway. So it's fine just to close this. | ||
destination.close(); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/react-server/src/forks/ReactFlightServerConfig.bun.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
export * from '../ReactFlightServerConfigStream'; | ||
export * from 'react-server-dom-webpack/src/ReactFlightServerWebpackBundlerConfig'; |
10 changes: 10 additions & 0 deletions
10
packages/react-server/src/forks/ReactServerFormatConfig.bun.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
export * from 'react-dom-bindings/src/server/ReactDOMServerFormatConfig'; |
10 changes: 10 additions & 0 deletions
10
packages/react-server/src/forks/ReactServerStreamConfig.bun.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
export * from '../ReactServerStreamConfigBun'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.