Skip to content

Commit ef8bdbe

Browse files
sebmarkbagegnoff
andauthored
[Flight Reply] Add Reply Encoding (#26360)
This adds `encodeReply` to the Flight Client and `decodeReply` to the Flight Server. Basically, it's a reverse Flight. It serializes values passed from the client to the server. I call this a "Reply". The tradeoffs and implementation details are a bit different so it requires its own implementation but is basically a clone of the Flight Server/Client but in reverse. Either through callServer or ServerContext. The goal of this project is to provide the equivalent serialization as passing props through RSC to client. Except React Elements and Components and such. So that you can pass a value to the client and back and it should have the same serialization constraints so when we add features in one direction we should mostly add it in the other. Browser support for streaming request bodies are currently very limited in that only Chrome supports it. So this doesn't produce a ReadableStream. Instead `encodeReply` produces either a JSON string or FormData. It uses a JSON string if it's a simple enough payload. For advanced features it uses FormData. This will also let the browser stream things like File objects (even though they're not yet supported since it follows the same rules as the other Flight). On the server side, you can either consume this by blocking on generating a FormData object or you can stream in the `multipart/form-data`. Even if the client isn't streaming data, the network does. On Node.js busboy seems to be the canonical library for this, so I exposed a `decodeReplyFromBusboy` in the Node build. However, if there's ever a web-standard way to stream form data, or if a library wins in that space we can support it. We can also just build a multipart parser that takes a ReadableStream built-in. On the server, server references passed as arguments are loaded from Node or Webpack just like the client or SSR does. This means that you can create higher order functions on the client or server. This can be tokenized when done from a server components but this is a security implication as it might be tempting to think that these are not fungible but you can swap one function for another on the client. So you have to basically treat an incoming argument as insecure, even if it's a function. I'm not too happy with the naming parity: Encode `server.renderToReadableStream` Decode: `client.createFromFetch` Decode `client.encodeReply` Decode: `server.decodeReply` This is mainly an implementation details of frameworks but it's annoying nonetheless. This comes from that `renderToReadableStream` does do some "rendering" by unwrapping server components etc. The `create` part comes from the parity with Fizz/Fiber where you `render` on the server and `create` a root on the client. Open to bike-shedding this some more. --------- Co-authored-by: Josh Story <josh.c.story@gmail.com>
1 parent a8875ea commit ef8bdbe

27 files changed

Lines changed: 1535 additions & 413 deletions

‎fixtures/flight/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"babel-preset-react-app": "^10.0.1",
1818
"body-parser": "^1.20.1",
1919
"browserslist": "^4.18.1",
20+
"busboy": "^1.6.0",
2021
"camelcase": "^6.2.1",
2122
"case-sensitive-paths-webpack-plugin": "^2.4.0",
2223
"compression": "^1.7.4",

‎fixtures/flight/server/region.js‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ if (typeof fetch === 'undefined') {
3333

3434
constexpress=require('express');
3535
constbodyParser=require('body-parser');
36+
constbusboy=require('busboy');
3637
constapp=express();
3738
constcompress=require('compression');
3839

@@ -95,9 +96,8 @@ app.get('/', async function (req, res) {
9596
});
9697

9798
app.post('/',bodyParser.text(),asyncfunction(req,res){
98-
const{renderToPipeableStream}=awaitimport(
99-
'react-server-dom-webpack/server'
100-
);
99+
const{renderToPipeableStream, decodeReply, decodeReplyFromBusboy}=
100+
awaitimport('react-server-dom-webpack/server');
101101
constserverReference=req.get('rsc-action');
102102
const[filepath,name]=serverReference.split('#');
103103
constaction=(awaitimport(filepath))[name];
@@ -108,9 +108,18 @@ app.post('/', bodyParser.text(), async function (req, res) {
108108
thrownewError('Invalid action');
109109
}
110110

111-
constargs=JSON.parse(req.body);
112-
constresult=action.apply(null,args);
111+
letargs;
112+
if(req.is('multipart/form-data')){
113+
// Use busboy to streamingly parse the reply from form-data.
114+
constbb=busboy({headers: req.headers});
115+
constreply=decodeReplyFromBusboy(bb);
116+
req.pipe(bb);
117+
args=awaitreply;
118+
}else{
119+
args=awaitdecodeReply(req.body);
120+
}
113121

122+
constresult=action.apply(null,args);
114123
const{pipe}=renderToPipeableStream(result,{});
115124
pipe(res);
116125
});

‎fixtures/flight/src/actions.js‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
'use server';
22

33
exportasyncfunctionlike(){
4-
returnnewPromise((resolve,reject)=>
5-
setTimeout(
6-
()=>
7-
Math.random()>0.5
8-
? resolve('Liked')
9-
: reject(newError('Failed to like')),
10-
500
11-
)
12-
);
4+
returnnewPromise((resolve,reject)=>resolve('Liked'));
135
}

‎fixtures/flight/src/index.js‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
import*asReactfrom'react';
22
import{Suspense}from'react';
33
importReactDOMfrom'react-dom/client';
4-
importReactServerDOMReaderfrom'react-server-dom-webpack/client';
4+
import{createFromFetch,encodeReply}from'react-server-dom-webpack/client';
55

66
// TODO: This should be a dependency of the App but we haven't implemented CSS in Node yet.
77
import'./style.css';
88

9-
letdata=ReactServerDOMReader.createFromFetch(
9+
letdata=createFromFetch(
1010
fetch('/',{
1111
headers: {
1212
Accept: 'text/x-component',
1313
},
1414
}),
1515
{
16-
callServer(id,args){
16+
asynccallServer(id,args){
1717
constresponse=fetch('/',{
1818
method: 'POST',
1919
headers: {
2020
Accept: 'text/x-component',
2121
'rsc-action': id,
2222
},
23-
body: JSON.stringify(args),
23+
body: awaitencodeReply(args),
2424
});
25-
returnReactServerDOMReader.createFromFetch(response);
25+
returncreateFromFetch(response);
2626
},
2727
}
2828
);

‎packages/react-client/src/ReactFlightClient.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import {
2525
parseModel,
2626
}from'./ReactFlightClientHostConfig';
2727

28+
import{knownServerReferences}from'./ReactFlightServerReferenceRegistry';
29+
2830
import{REACT_LAZY_TYPE,REACT_ELEMENT_TYPE}from'shared/ReactSymbols';
2931

3032
import{getOrCreateServerContext}from'shared/ReactServerContextRegistry';
@@ -495,6 +497,7 @@ function createServerReferenceProxy<A: Iterable<any>, T>(
495497
returncallServer(metaData.id,bound.concat(args));
496498
});
497499
};
500+
knownServerReferences.set(proxy, metaData);
498501
return proxy;
499502
}
500503

Lines changed: 278 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,278 @@
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+
importtype{Thenable}from'shared/ReactTypes';
11+
12+
import{knownServerReferences}from'./ReactFlightServerReferenceRegistry';
13+
14+
import{
15+
REACT_ELEMENT_TYPE,
16+
REACT_LAZY_TYPE,
17+
REACT_PROVIDER_TYPE,
18+
}from'shared/ReactSymbols';
19+
20+
import{
21+
describeObjectForErrorMessage,
22+
isSimpleObject,
23+
objectName,
24+
}from'shared/ReactSerializationErrors';
25+
26+
importisArrayfrom'shared/isArray';
27+
28+
typeReactJSONValue=
29+
|string
30+
|boolean
31+
|number
32+
|null
33+
|$ReadOnlyArray<ReactJSONValue>
34+
|ReactServerObject;
35+
36+
exportopaquetypeServerReference<T>=T;
37+
38+
// Serializable values
39+
exporttypeReactServerValue=
40+
// References are passed by their value
41+
|ServerReference<any>
42+
// The rest are passed as is. Sub-types can be passed in but lose their
43+
// subtype, so the receiver can only accept once of these.
44+
|string
45+
|boolean
46+
|number
47+
|symbol
48+
|null
49+
|Iterable<ReactServerValue>
50+
|Array<ReactServerValue>
51+
|ReactServerObject
52+
|Promise<ReactServerValue>;// Thenable<ReactServerValue>
53+
54+
type ReactServerObject ={+[key: string]: ReactServerValue};
55+
56+
// function serializeByValueID(id: number): string {
57+
// return '$' + id.toString(16);
58+
// }
59+
60+
functionserializePromiseID(id: number): string{
61+
return'$@'+id.toString(16);
62+
}
63+
64+
functionserializeServerReferenceID(id: number): string{
65+
return'$F'+id.toString(16);
66+
}
67+
68+
functionserializeSymbolReference(name: string): string{
69+
return'$S'+name;
70+
}
71+
72+
functionescapeStringValue(value: string): string{
73+
if(value[0]==='$'){
74+
// We need to escape $ prefixed strings since we use those to encode
75+
// references to IDs and as special symbol values.
76+
return'$'+value;
77+
}else{
78+
returnvalue;
79+
}
80+
}
81+
82+
exportfunctionprocessReply(
83+
root: ReactServerValue,
84+
resolve: (string|FormData)=>void,
85+
reject: (error: mixed)=>void,
86+
): void{
87+
letnextPartId=1;
88+
letpendingParts=0;
89+
letformData: null|FormData=null;
90+
91+
functionresolveToJSON(
92+
this:
93+
|{+[key: string|number]: ReactServerValue}
94+
|$ReadOnlyArray<ReactServerValue>,
95+
key: string,
96+
value: ReactServerValue,
97+
): ReactJSONValue{
98+
constparent=this;
99+
if(__DEV__){
100+
// $FlowFixMe
101+
constoriginalValue=this[key];
102+
if(typeoforiginalValue==='object'&&originalValue!==value){
103+
if(objectName(originalValue)!=='Object'){
104+
console.error(
105+
'Only plain objects can be passed to Server Functions from the Client. '+
106+
'%s objects are not supported.%s',
107+
objectName(originalValue),
108+
describeObjectForErrorMessage(parent,key),
109+
);
110+
}else{
111+
console.error(
112+
'Only plain objects can be passed to Server Functions from the Client. '+
113+
'Objects with toJSON methods are not supported. Convert it manually '+
114+
'to a simple value before passing it to props.%s',
115+
describeObjectForErrorMessage(parent,key),
116+
);
117+
}
118+
}
119+
}
120+
121+
if(value===null){
122+
returnnull;
123+
}
124+
125+
if(typeofvalue==='object'){
126+
// $FlowFixMe[method-unbinding]
127+
if(typeofvalue.then==='function'){
128+
// We assume that any object with a .then property is a "Thenable" type,
129+
// or a Promise type. Either of which can be represented by a Promise.
130+
if(formData===null){
131+
// Upgrade to use FormData to allow us to stream this value.
132+
formData=newFormData();
133+
}
134+
pendingParts++;
135+
constpromiseId=nextPartId++;
136+
constthenable: Thenable<any> = (value: any);
137+
thenable.then(
138+
partValue =>{
139+
constpartJSON=JSON.stringify(partValue,resolveToJSON);
140+
// $FlowFixMe[incompatible-type] We know it's not null because we assigned it above.
141+
constdata: FormData=formData;
142+
// eslint-disable-next-line react-internal/safe-string-coercion
143+
data.append(''+promiseId,partJSON);
144+
pendingParts--;
145+
if(pendingParts===0){
146+
resolve(data);
147+
}
148+
},
149+
reason =>{
150+
// In the future we could consider serializing this as an error
151+
// that throws on the server instead.
152+
reject(reason);
153+
},
154+
);
155+
return serializePromiseID(promiseId);
156+
}
157+
158+
if(__DEV__){
159+
if(value!==null&&!isArray(value)){
160+
// Verify that this is a simple plain object.
161+
if((value: any).$$typeof===REACT_ELEMENT_TYPE){
162+
console.error(
163+
'React Element cannot be passed to Server Functions from the Client.%s',
164+
describeObjectForErrorMessage(parent,key),
165+
);
166+
} else if ((value: any).$$typeof === REACT_LAZY_TYPE) {
167+
console.error(
168+
'React Lazy cannot be passed to Server Functions from the Client.%s',
169+
describeObjectForErrorMessage(parent,key),
170+
);
171+
} else if ((value: any).$$typeof === REACT_PROVIDER_TYPE) {
172+
console.error(
173+
'React Context Providers cannot be passed to Server Functions from the Client.%s',
174+
describeObjectForErrorMessage(parent,key),
175+
);
176+
} else if (objectName(value) !== 'Object') {
177+
console.error(
178+
'Only plain objects can be passed to Client Components from Server Components. '+
179+
'%s objects are not supported.%s',
180+
objectName(value),
181+
describeObjectForErrorMessage(parent,key),
182+
);
183+
} else if (!isSimpleObject(value)) {
184+
console.error(
185+
'Only plain objects can be passed to Client Components from Server Components. '+
186+
'Classes or other objects with methods are not supported.%s',
187+
describeObjectForErrorMessage(parent,key),
188+
);
189+
} else if (Object.getOwnPropertySymbols) {
190+
constsymbols=Object.getOwnPropertySymbols(value);
191+
if(symbols.length>0){
192+
console.error(
193+
'Only plain objects can be passed to Client Components from Server Components. '+
194+
'Objects with symbol properties like %s are not supported.%s',
195+
symbols[0].description,
196+
describeObjectForErrorMessage(parent,key),
197+
);
198+
}
199+
}
200+
}
201+
}
202+
203+
// $FlowFixMe
204+
returnvalue;
205+
}
206+
207+
if(typeofvalue=== 'string'){
208+
returnescapeStringValue(value);
209+
}
210+
211+
if (
212+
typeof value === 'boolean' ||
213+
typeof value === 'number' ||
214+
typeof value === 'undefined'
215+
) {
216+
returnvalue;
217+
}
218+
219+
if (typeof value === 'function') {
220+
constmetaData=knownServerReferences.get(value);
221+
if(metaData!==undefined){
222+
const metaDataJSON =JSON.stringify(metaData,resolveToJSON);
223+
if(formData===null){
224+
// Upgrade to use FormData to allow us to stream this value.
225+
formData =newFormData();
226+
}
227+
// The reference to this function came from the same client so we can pass it back.
228+
constrefId=nextPartId++;
229+
// eslint-disable-next-line react-internal/safe-string-coercion
230+
formData.set(''+refId,metaDataJSON);
231+
returnserializeServerReferenceID(refId);
232+
}
233+
thrownewError(
234+
'Client Functions cannot be passed directly to Server Functions. '+
235+
'Only Functions passed from the Server can be passed back again.',
236+
);
237+
}
238+
239+
if (typeof value === 'symbol') {
240+
// $FlowFixMe `description` might be undefined
241+
constname: string=value.description;
242+
if(Symbol.for(name)!==value){
243+
thrownewError(
244+
'Only global symbols received from Symbol.for(...) can be passed to Server Functions. '+
245+
`The symbol Symbol.for(${
246+
// $FlowFixMe `description` might be undefined
247+
value.description
248+
}) cannot be found among global symbols.`,
249+
);
250+
}
251+
returnserializeSymbolReference(name);
252+
}
253+
254+
if(typeofvalue==='bigint'){
255+
thrownewError(
256+
`BigInt (${value}) is not yet supported as an argument to a Server Function.`,
257+
);
258+
}
259+
260+
thrownewError(
261+
`Type ${typeofvalue} is not supported as an argument to a Server Function.`,
262+
);
263+
}
264+
265+
// $FlowFixMe[incompatible-type] it's not going to be undefined because we'll encode it.
266+
constjson: string=JSON.stringify(root,resolveToJSON);
267+
if(formData===null){
268+
// If it's a simple data structure, we just use plain JSON.
269+
resolve(json);
270+
}else{
271+
// Otherwise, we use FormData to let us stream in the result.
272+
formData.set('0',json);
273+
if(pendingParts===0){
274+
// $FlowFixMe[incompatible-call] this has already been refined.
275+
resolve(formData);
276+
}
277+
}
278+
}

0 commit comments

Comments
 (0)