Skip to content

Commit 38b6fd5

Browse files
committed
Set up FB integration with React Flight Server
1 parent d1727fb commit 38b6fd5

21 files changed

Lines changed: 1971 additions & 0 deletions
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
export {default as rendererVersion} from 'shared/ReactVersion';
11+
export const rendererPackageName = 'react-flight-server-fb';
12+
13+
export * from 'react-client/src/ReactFlightClientStreamConfigWeb';
14+
export * from 'react-client/src/ReactClientConsoleConfigBrowser';
15+
export * from 'react-client/src/ReactClientDebugConfigBrowser';
16+
export * from 'react-flight-server-fb/src/client/ReactFlightClientConfigBundlerFB';
17+
export * from 'react-flight-server-fb/src/client/ReactFlightClientConfigTargetFBBrowser';
18+
export * from 'react-flight-server-fb/src/client/ReactFlightClientConfigDOMFB';
19+
export const usedWithSSR = false;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
export {default as rendererVersion} from 'shared/ReactVersion';
11+
export const rendererPackageName = 'react-flight-server-fb';
12+
13+
export * from 'react-client/src/ReactFlightClientStreamConfigWeb';
14+
export * from 'react-client/src/ReactClientConsoleConfigServer';
15+
export * from 'react-client/src/ReactClientDebugConfigNode';
16+
export * from 'react-flight-server-fb/src/client/ReactFlightClientConfigBundlerFB';
17+
export * from 'react-flight-server-fb/src/client/ReactFlightClientConfigTargetFBBrowser';
18+
export * from 'react-flight-server-fb/src/client/ReactFlightClientConfigDOMFB';
19+
export const usedWithSSR = false;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# react-flight-server-fb
2+
3+
React Flight bindings for DOM using Meta's internal bundler.
4+
5+
**Use it at your own risk.**
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
type TemporaryReferenceSet = mixed;
11+
12+
type ReactServerValue = mixed;
13+
14+
type CallServerCallback = <A, T>(string, args: A) => Promise<T>;
15+
16+
type FindSourceMapURLCallback = (
17+
fileName: string,
18+
environment: string,
19+
) => ?string;
20+
21+
type Options = {
22+
callServer?: CallServerCallback,
23+
debugChannel?: {writable?: WritableStream, readable?: ReadableStream, ...},
24+
temporaryReferences?: TemporaryReferenceSet,
25+
findSourceMapURL?: FindSourceMapURLCallback,
26+
replayConsoleLogs?: boolean,
27+
environmentName?: string,
28+
startTime?: number,
29+
endTime?: number,
30+
};
31+
32+
declare export function createFromFetch<T>(
33+
promiseForResponse: Promise<Response>,
34+
options?: Options,
35+
): Promise<T>;
36+
37+
declare export function createFromReadableStream<T>(
38+
stream: ReadableStream,
39+
options?: Options,
40+
): Promise<T>;
41+
42+
declare export function createServerReference<A: Iterable<any>, T>(
43+
id: string,
44+
callServer: CallServerCallback,
45+
encodeFormAction?: mixed,
46+
findSourceMapURL?: FindSourceMapURLCallback,
47+
functionName?: string,
48+
): (...A) => Promise<T>;
49+
50+
declare export function createTemporaryReferenceSet(): TemporaryReferenceSet;
51+
52+
declare export function encodeReply(
53+
value: ReactServerValue,
54+
options?: {temporaryReferences?: TemporaryReferenceSet, signal?: AbortSignal},
55+
): Promise<string | URLSearchParams | FormData>;
56+
57+
declare export function registerServerReference<T: Function>(
58+
reference: T,
59+
id: string,
60+
): T;
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
import type {Readable, Writable, Duplex} from 'stream';
11+
12+
type TemporaryReferenceSet = mixed;
13+
14+
type ReactClientValue = mixed;
15+
16+
type ClientReference<T> = {
17+
$$typeof: symbol,
18+
$$id: string,
19+
$$hblp: mixed,
20+
};
21+
22+
type Options = {
23+
debugChannel?: Readable | Writable | Duplex | WebSocket,
24+
environmentName?: string | (() => string),
25+
filterStackFrame?: (url: string, functionName: string) => boolean,
26+
onError?: (error: mixed) => void,
27+
identifierPrefix?: string,
28+
temporaryReferences?: TemporaryReferenceSet,
29+
};
30+
31+
type PipeableStream = {
32+
abort(reason: mixed): void,
33+
pipe<T: Writable>(destination: T): T,
34+
};
35+
36+
type PrerenderOptions = {
37+
environmentName?: string | (() => string),
38+
filterStackFrame?: (url: string, functionName: string) => boolean,
39+
onError?: (error: mixed) => void,
40+
identifierPrefix?: string,
41+
temporaryReferences?: TemporaryReferenceSet,
42+
signal?: AbortSignal,
43+
startTime?: number,
44+
};
45+
46+
declare export function renderToPipeableStream(
47+
model: ReactClientValue,
48+
options?: Options,
49+
): PipeableStream;
50+
51+
declare export function renderToString(
52+
model: ReactClientValue,
53+
options?: PrerenderOptions,
54+
): Promise<string>;
55+
56+
declare export function registerClientReference<T>(
57+
proxyImplementation: any,
58+
id: string,
59+
hblp: mixed,
60+
): ClientReference<T>;
61+
62+
type ServerReference<T: Function> = T & {
63+
$$typeof: symbol,
64+
$$id: string,
65+
$$bound: null | Array<ReactClientValue>,
66+
$$location?: Error,
67+
};
68+
69+
declare export function registerServerReference<T: Function>(
70+
reference: T,
71+
id: string,
72+
): ServerReference<T>;
73+
74+
declare export function createTemporaryReferenceSet(): TemporaryReferenceSet;
75+
76+
declare export function decodeReply<T>(
77+
body: string | FormData,
78+
options?: {
79+
temporaryReferences?: TemporaryReferenceSet,
80+
arraySizeLimit?: number,
81+
},
82+
): Promise<T>;
83+
84+
declare export function decodeAction<T>(
85+
body: FormData,
86+
serverManifest: mixed,
87+
): Promise<() => T> | null;
88+
89+
declare export function decodeFormState<S>(
90+
actionResult: S,
91+
body: FormData,
92+
serverManifest: mixed,
93+
): Promise<[S, string, string, number] | null>;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
export * from './src/client/ReactFlightDOMClientBrowser';
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "react-flight-server-fb",
3+
"description": "React Server Components bindings for DOM using Meta's internal bundler. It is not intended to be imported directly.",
4+
"version": "19.3.0",
5+
"keywords": [
6+
"react"
7+
],
8+
"homepage": "https://react.dev/",
9+
"bugs": "https://github.com/facebook/react/issues",
10+
"license": "MIT",
11+
"files": [
12+
"LICENSE",
13+
"README.md",
14+
"client.browser.js"
15+
],
16+
"exports": {
17+
"./client": "./client.browser.js",
18+
"./client.browser": "./client.browser.js",
19+
"./src/*": "./src/*.js",
20+
"./package.json": "./package.json"
21+
},
22+
"repository": {
23+
"type" : "git",
24+
"url" : "https://github.com/facebook/react.git",
25+
"directory": "packages/react-flight-server-fb"
26+
},
27+
"engines": {
28+
"node": ">=0.10.0"
29+
},
30+
"peerDependencies": {
31+
"react": "^19.0.0",
32+
"react-dom": "^19.0.0"
33+
}
34+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
import type {ReactClientValue} from 'react-server/src/ReactFlightServer';
11+
12+
export type ServerReference<T: Function> = T & {
13+
$$typeof: symbol,
14+
$$id: string,
15+
$$bound: null | Array<ReactClientValue>,
16+
$$location?: Error,
17+
};
18+
19+
// eslint-disable-next-line no-unused-vars
20+
export type ClientReference<T> = {
21+
$$typeof: symbol,
22+
$$id: string,
23+
$$hblp: mixed,
24+
};
25+
26+
const CLIENT_REFERENCE_TAG = Symbol.for('react.client.reference');
27+
const SERVER_REFERENCE_TAG = Symbol.for('react.server.reference');
28+
29+
export function isClientReference(reference: Object): boolean {
30+
return reference.$$typeof === CLIENT_REFERENCE_TAG;
31+
}
32+
33+
export function isServerReference(reference: Object): boolean {
34+
return reference.$$typeof === SERVER_REFERENCE_TAG;
35+
}
36+
37+
export function registerClientReference<T>(
38+
proxyImplementation: any,
39+
id: string,
40+
hblp: mixed,
41+
): ClientReference<T> {
42+
return Object.defineProperties(proxyImplementation, {
43+
$$typeof: {value: CLIENT_REFERENCE_TAG},
44+
$$id: {value: id},
45+
$$hblp: {value: hblp},
46+
});
47+
}
48+
49+
// $FlowFixMe[method-unbinding]
50+
const FunctionBind = Function.prototype.bind;
51+
// $FlowFixMe[method-unbinding]
52+
const ArraySlice = Array.prototype.slice;
53+
function bind(this: ServerReference<any>): any {
54+
// $FlowFixMe[incompatible-call]
55+
const newFn = FunctionBind.apply(this, arguments);
56+
if (this.$$typeof === SERVER_REFERENCE_TAG) {
57+
if (__DEV__) {
58+
const thisBind = arguments[0];
59+
if (thisBind != null) {
60+
console.error(
61+
'Cannot bind "this" of a Server Action. Pass null or undefined as the first argument to .bind().',
62+
);
63+
}
64+
}
65+
const args = ArraySlice.call(arguments, 1);
66+
const $$typeof = {value: SERVER_REFERENCE_TAG};
67+
const $$id = {value: this.$$id};
68+
const $$bound = {value: this.$$bound ? this.$$bound.concat(args) : args};
69+
return Object.defineProperties(
70+
(newFn: any),
71+
(__DEV__
72+
? {
73+
$$typeof,
74+
$$id,
75+
$$bound,
76+
$$location: {
77+
value: this.$$location,
78+
configurable: true,
79+
},
80+
bind: {value: bind, configurable: true},
81+
}
82+
: {
83+
$$typeof,
84+
$$id,
85+
$$bound,
86+
bind: {value: bind, configurable: true},
87+
}) as PropertyDescriptorMap,
88+
);
89+
}
90+
return newFn;
91+
}
92+
93+
const serverReferenceToString = {
94+
value: () => 'function () { [omitted code] }',
95+
configurable: true,
96+
writable: true,
97+
};
98+
99+
export function registerServerReference<T: Function>(
100+
reference: T,
101+
id: string,
102+
): ServerReference<T> {
103+
const $$typeof = {value: SERVER_REFERENCE_TAG};
104+
const $$id = {
105+
value: id,
106+
configurable: true,
107+
};
108+
const $$bound = {value: null, configurable: true};
109+
return Object.defineProperties(
110+
(reference: any),
111+
(__DEV__
112+
? {
113+
$$typeof,
114+
$$id,
115+
$$bound,
116+
$$location: {
117+
value: Error('react-stack-top-frame'),
118+
configurable: true,
119+
},
120+
bind: {value: bind, configurable: true},
121+
toString: serverReferenceToString,
122+
}
123+
: {
124+
$$typeof,
125+
$$id,
126+
$$bound,
127+
bind: {value: bind, configurable: true},
128+
toString: serverReferenceToString,
129+
}) as PropertyDescriptorMap,
130+
);
131+
}

0 commit comments

Comments
 (0)