Skip to content

Commit a91d8c7

Browse files
committed
[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> DiffTrain build for [ef8bdbe](ef8bdbe)
1 parent 2d9d96b commit a91d8c7

10 files changed

+1538
-1521
lines changed

compiled/facebook-www/REVISION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
d1ad984db1591b131d16739a24dee4ba44886a09
1+
ef8bdbecb6dbb9743b895c2e867e5a5264dd6651

compiled/facebook-www/ReactFlightDOMRelayClient-dev.classic.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ function parseModel(response, json) {
7272
return parseModelRecursively(response, dummy, "", json);
7373
}
7474

75+
var knownServerReferences = new WeakMap();
76+
7577
// ATTENTION
7678
// When adding new symbols to this file,
7779
// Please consider also adding to 'react-devtools-shared/src/backend/ReactSymbols'
@@ -487,6 +489,7 @@ function createServerReferenceProxy(response, metaData) {
487489
});
488490
};
489491

492+
knownServerReferences.set(proxy, metaData);
490493
return proxy;
491494
}
492495

compiled/facebook-www/ReactFlightDOMRelayClient-dev.modern.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ function parseModel(response, json) {
7272
return parseModelRecursively(response, dummy, "", json);
7373
}
7474

75+
var knownServerReferences = new WeakMap();
76+
7577
// ATTENTION
7678
// When adding new symbols to this file,
7779
// Please consider also adding to 'react-devtools-shared/src/backend/ReactSymbols'
@@ -487,6 +489,7 @@ function createServerReferenceProxy(response, metaData) {
487489
});
488490
};
489491

492+
knownServerReferences.set(proxy, metaData);
490493
return proxy;
491494
}
492495

compiled/facebook-www/ReactFlightDOMRelayClient-prod.classic.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ function parseModelRecursively(response, parentObj, key, value) {
6969
return value;
7070
}
7171
var dummy = {},
72+
knownServerReferences = new WeakMap(),
7273
REACT_ELEMENT_TYPE = Symbol.for("react.element"),
7374
REACT_LAZY_TYPE = Symbol.for("react.lazy"),
7475
REACT_SERVER_CONTEXT_DEFAULT_VALUE_NOT_LOADED = Symbol.for(
@@ -227,8 +228,7 @@ function createModelReject(chunk) {
227228
};
228229
}
229230
function createServerReferenceProxy(response, metaData) {
230-
var callServer = response._callServer;
231-
return function () {
231+
function proxy() {
232232
var args = Array.prototype.slice.call(arguments),
233233
p = metaData.bound;
234234
return p
@@ -238,7 +238,10 @@ function createServerReferenceProxy(response, metaData) {
238238
return callServer(metaData.id, bound.concat(args));
239239
})
240240
: callServer(metaData.id, args);
241-
};
241+
}
242+
var callServer = response._callServer;
243+
knownServerReferences.set(proxy, metaData);
244+
return proxy;
242245
}
243246
function parseModelString(response, parentObject, key, value) {
244247
if ("$" === value[0]) {

compiled/facebook-www/ReactFlightDOMRelayClient-prod.modern.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ function parseModelRecursively(response, parentObj, key, value) {
6969
return value;
7070
}
7171
var dummy = {},
72+
knownServerReferences = new WeakMap(),
7273
REACT_ELEMENT_TYPE = Symbol.for("react.element"),
7374
REACT_LAZY_TYPE = Symbol.for("react.lazy"),
7475
REACT_SERVER_CONTEXT_DEFAULT_VALUE_NOT_LOADED = Symbol.for(
@@ -227,8 +228,7 @@ function createModelReject(chunk) {
227228
};
228229
}
229230
function createServerReferenceProxy(response, metaData) {
230-
var callServer = response._callServer;
231-
return function () {
231+
function proxy() {
232232
var args = Array.prototype.slice.call(arguments),
233233
p = metaData.bound;
234234
return p
@@ -238,7 +238,10 @@ function createServerReferenceProxy(response, metaData) {
238238
return callServer(metaData.id, bound.concat(args));
239239
})
240240
: callServer(metaData.id, args);
241-
};
241+
}
242+
var callServer = response._callServer;
243+
knownServerReferences.set(proxy, metaData);
244+
return proxy;
242245
}
243246
function parseModelString(response, parentObject, key, value) {
244247
if ("$" === value[0]) {

0 commit comments

Comments
 (0)