Skip to content

Commit 61aa00d

Browse files
eps1lonunstubbableclaude
authored
[19.3.x][Flight] Improve transport protocol (#37845)
Co-authored-by: Hendrik Liebau <[email protected]> Co-authored-by: Claude Code (kimi-k3[1m]) <[email protected]>
1 parent 1d34f91 commit 61aa00d

31 files changed

Lines changed: 1525 additions & 226 deletions

‎.eslintrc.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,11 +608,13 @@ module.exports = {
608608
React$Portal: 'readonly',
609609
React$Ref: 'readonly',
610610
React$RefSetter: 'readonly',
611+
ReadableByteStreamController: 'readonly',
611612
ReadableStreamController: 'readonly',
612613
ReadableStreamReader: 'readonly',
613614
RequestInfo: 'readonly',
614615
RequestOptions: 'readonly',
615616
Required: 'readonly',
617+
SharedArrayBuffer: 'readonly',
616618
StoreAsGlobal: 'readonly',
617619
symbol: 'readonly',
618620
SyntheticEvent: 'readonly',

‎ReactVersions.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77
//
88
// The @latest channel uses the version as-is, e.g.:
99
//
10-
// 19.3.0
10+
// 19.3.1
1111
//
1212
// The @canary channel appends additional information, with the scheme
1313
// <version>-<label>-<commit_sha>, e.g.:
1414
//
15-
// 19.3.0-canary-a1c2d3e4
15+
// 19.3.1-canary-a1c2d3e4
1616
//
1717
// The @experimental channel doesn't include a version, only a date and a sha, e.g.:
1818
//
1919
// 0.0.0-experimental-241c4467e-20200129
2020

21-
const ReactVersion = '19.3.0';
21+
const ReactVersion = '19.3.1';
2222

2323
// The label used by the @canary channel. Represents the upcoming release's
2424
// stability. Most of the time, this will be "canary", but we may temporarily

‎packages/react-art/package.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "react-art",
33
"description": "React ART is a JavaScript library for drawing vector graphics using React. It provides declarative and reactive bindings to the ART library. Using the same declarative API you can render the output to either Canvas, SVG or VML (IE8).",
4-
"version": "19.3.0",
4+
"version": "19.3.1",
55
"main": "index.js",
66
"repository": {
77
"type": "git",
@@ -27,7 +27,7 @@
2727
"scheduler": "^0.28.0"
2828
},
2929
"peerDependencies": {
30-
"react": "^19.3.0"
30+
"react": "^19.3.1"
3131
},
3232
"files": [
3333
"LICENSE",

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

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ const __PROTO__ = '__proto__';
172172
const ObjectPrototype = Object.prototype;
173173
const ArrayPrototype = Array.prototype;
174174

175+
type RESPONSE_SYMBOL_TYPE = 'RESPONSE_SYMBOL'; // Fake symbol type.
176+
const RESPONSE_SYMBOL: RESPONSE_SYMBOL_TYPE = Symbol() as any;
177+
175178
type PendingChunk<T> = {
176179
status: 'pending',
177180
value: null | Array<InitializationReference | (T => mixed)>,
@@ -203,7 +206,7 @@ type BlockedChunk<T> = {
203206
type ResolvedModelChunk<T> = {
204207
status: 'resolved_model',
205208
value: UninitializedModel,
206-
reason: Response,
209+
reason: {[RESPONSE_SYMBOL_TYPE]: Response},
207210
_children: Array<SomeChunk<any>> | ProfilingResult, // Profiling-only
208211
_debugChunk: null | SomeChunk<ReactDebugInfoEntry>, // DEV-only
209212
_debugInfo: ReactDebugInfo, // DEV-only
@@ -865,7 +868,7 @@ function createResolvedModelChunk<T>(
865868
value: UninitializedModel,
866869
): ResolvedModelChunk<T> {
867870
// $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
868-
return new ReactPromise(RESOLVED_MODEL, value, response);
871+
return new ReactPromise(RESOLVED_MODEL, value, {[RESPONSE_SYMBOL]: response});
869872
}
870873

871874
function createResolvedModuleChunk<T>(
@@ -929,7 +932,9 @@ function createResolvedIteratorResultChunk<T>(
929932
const iteratorResultJSON =
930933
(done ? '{"done":true,"value":' : '{"done":false,"value":') + value + '}';
931934
// $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
932-
return new ReactPromise(RESOLVED_MODEL, iteratorResultJSON, response);
935+
return new ReactPromise(RESOLVED_MODEL, iteratorResultJSON, {
936+
[RESPONSE_SYMBOL]: response,
937+
});
933938
}
934939

935940
function resolveIteratorResultChunk<T>(
@@ -963,7 +968,7 @@ function resolveModelChunk<T>(
963968
const resolvedChunk: ResolvedModelChunk<T> = chunk as any;
964969
resolvedChunk.status = RESOLVED_MODEL;
965970
resolvedChunk.value = value;
966-
resolvedChunk.reason = response;
971+
resolvedChunk.reason = {[RESPONSE_SYMBOL]: response};
967972
if (resolveListeners !== null) {
968973
// This is unfortunate that we're reading this eagerly if
969974
// we already have listeners attached since they might no
@@ -1134,7 +1139,7 @@ function initializeModelChunk<T>(chunk: ResolvedModelChunk<T>): void {
11341139
initializingHandler = null;
11351140

11361141
const resolvedModel = chunk.value;
1137-
const response = chunk.reason;
1142+
const response = chunk.reason[RESPONSE_SYMBOL];
11381143

11391144
// We go to the BLOCKED state until we've fully resolved this.
11401145
// We do this before parsing in case we try to initialize the same chunk
@@ -2070,6 +2075,15 @@ function loadServerReference<A: Iterable<any>, T>(
20702075
parentObject: Object,
20712076
key: string,
20722077
): (...A) => Promise<T> {
2078+
if (key === 'then') {
2079+
// This should never happen because we always serialize objects with then-functions
2080+
// as "thenable" which reduces to a chunk with no other fields.
2081+
return null as any;
2082+
}
2083+
const id = metaData.id;
2084+
if (typeof id !== 'string') {
2085+
return null as any;
2086+
}
20732087
if (!response._serverReferenceConfig) {
20742088
// In the normal case, we can't load this Server Reference in the current environment and
20752089
// we just return a proxy to it.
@@ -2082,27 +2096,27 @@ function loadServerReference<A: Iterable<any>, T>(
20822096
}
20832097
// If we have a module mapping we can load the real version of this Server Reference.
20842098
const serverReference: ClientReference<T> =
2085-
resolveServerReference<$FlowFixMe>(
2086-
response._serverReferenceConfig,
2087-
metaData.id,
2088-
);
2099+
resolveServerReference<$FlowFixMe>(response._serverReferenceConfig, id);
20892100

2101+
const bound = metaData.bound;
20902102
let promise: null | Thenable<any> = preloadModule(serverReference);
20912103
if (!promise) {
2092-
if (!metaData.bound) {
2104+
if (bound instanceof ReactPromise) {
2105+
promise = Promise.resolve(bound);
2106+
} else {
2107+
// We only await the bound arguments when they are one of our own chunks.
2108+
// Anything else could be a foreign thenable whose `then` we must not call.
20932109
const resolvedValue = requireModule(serverReference) as any;
20942110
registerBoundServerReference(
20952111
resolvedValue,
2096-
metaData.id,
2097-
metaData.bound,
2112+
id,
2113+
bound,
20982114
response._encodeFormAction,
20992115
);
21002116
return resolvedValue;
2101-
} else {
2102-
promise = Promise.resolve(metaData.bound);
21032117
}
2104-
} else if (metaData.bound) {
2105-
promise = Promise.all([promise, metaData.bound]);
2118+
} else if (bound instanceof ReactPromise) {
2119+
promise = Promise.all([promise, bound]);
21062120
}
21072121

21082122
let handler: InitializationHandler;
@@ -2123,17 +2137,20 @@ function loadServerReference<A: Iterable<any>, T>(
21232137
function fulfill(): void {
21242138
let resolvedValue = requireModule(serverReference) as any;
21252139

2126-
if (metaData.bound) {
2127-
// This promise is coming from us and should have initilialized by now.
2128-
const boundArgs: Array<any> = (metaData.bound as any).value.slice(0);
2140+
if (bound) {
2141+
// This promise is coming from us and should have initialized by now.
2142+
const promiseValue = (bound as any).value;
2143+
const boundArgs: Array<any> = isArray(promiseValue)
2144+
? promiseValue.slice(0)
2145+
: [];
21292146
boundArgs.unshift(null); // this
21302147
resolvedValue = resolvedValue.bind.apply(resolvedValue, boundArgs);
21312148
}
21322149

21332150
registerBoundServerReference(
21342151
resolvedValue,
2135-
metaData.id,
2136-
metaData.bound,
2152+
id,
2153+
bound,
21372154
response._encodeFormAction,
21382155
);
21392156

@@ -2584,7 +2601,17 @@ function extractIterator(response: Response, model: Array<any>): Iterator<any> {
25842601
return model[Symbol.iterator]();
25852602
}
25862603

2587-
function createModel(response: Response, model: any): any {
2604+
function createModel(
2605+
response: Response,
2606+
model: any,
2607+
parentObject: Object,
2608+
key: string,
2609+
): any {
2610+
if (key === 'then' && typeof model === 'function') {
2611+
// This should never happen because we always serialize objects with then-functions
2612+
// as "thenable" which reduces to a chunk with no other fields.
2613+
return null;
2614+
}
25882615
return model;
25892616
}
25902617

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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -283,16 +283,16 @@ export function processReply(
283283
const streamId = nextPartId++;
284284

285285
const buffer = [];
286+
// Stream chunks use a dedicated namespace so that the server can tell
287+
// them apart from one-shot chunks without scanning.
288+
const streamPrefix = formFieldPrefix + 'S' + streamId;
286289

287290
function progress(entry: {done: boolean, value: ReactServerValue, ...}) {
288291
if (entry.done) {
289292
const blobId = nextPartId++;
290293
data.append(formFieldPrefix + blobId, new Blob(buffer));
291-
data.append(
292-
formFieldPrefix + streamId,
293-
'"$o' + blobId.toString(16) + '"',
294-
);
295-
data.append(formFieldPrefix + streamId, 'C'); // Close signal
294+
data.append(streamPrefix, '"$o' + blobId.toString(16) + '"');
295+
data.append(streamPrefix, 'C'); // Close signal
296296
pendingParts--;
297297
if (pendingParts === 0) {
298298
resolve(data);
@@ -317,9 +317,13 @@ export function processReply(
317317
pendingParts++;
318318
const streamId = nextPartId++;
319319

320+
// Stream chunks use a dedicated namespace so that the server can tell
321+
// them apart from one-shot chunks without scanning.
322+
const streamPrefix = formFieldPrefix + 'S' + streamId;
323+
320324
function progress(entry: {done: boolean, value: ReactServerValue, ...}) {
321325
if (entry.done) {
322-
data.append(formFieldPrefix + streamId, 'C'); // Close signal
326+
data.append(streamPrefix, 'C'); // Close signal
323327
pendingParts--;
324328
if (pendingParts === 0) {
325329
resolve(data);
@@ -328,7 +332,7 @@ export function processReply(
328332
try {
329333
// $FlowFixMe[incompatible-type]: While plain JSON can return undefined we never do here.
330334
const partJSON: string = JSON.stringify(entry.value, resolveToJSON);
331-
data.append(formFieldPrefix + streamId, partJSON);
335+
data.append(streamPrefix, partJSON);
332336
reader.read().then(progress, reject);
333337
} catch (x) {
334338
reject(x);
@@ -372,6 +376,10 @@ export function processReply(
372376
// iterated more than once on the receiving server.
373377
const isIterator = iterable === iterator;
374378

379+
// Stream chunks use a dedicated namespace so that the server can tell
380+
// them apart from one-shot chunks without scanning.
381+
const streamPrefix = formFieldPrefix + 'S' + streamId;
382+
375383
// There's a race condition between when the stream is aborted and when the promise
376384
// resolves so we track whether we already aborted it to avoid writing twice.
377385
function progress(
@@ -381,14 +389,14 @@ export function processReply(
381389
) {
382390
if (entry.done) {
383391
if (entry.value === undefined) {
384-
data.append(formFieldPrefix + streamId, 'C'); // Close signal
392+
data.append(streamPrefix, 'C'); // Close signal
385393
} else {
386394
// Unlike streams, the last value may not be undefined. If it's not
387395
// we outline it and encode a reference to it in the closing instruction.
388396
try {
389397
// $FlowFixMe[incompatible-type]: While plain JSON can return undefined we never do here.
390398
const partJSON: string = JSON.stringify(entry.value, resolveToJSON);
391-
data.append(formFieldPrefix + streamId, 'C' + partJSON); // Close signal
399+
data.append(streamPrefix, 'C' + partJSON); // Close signal
392400
} catch (x) {
393401
reject(x);
394402
return;
@@ -402,7 +410,7 @@ export function processReply(
402410
try {
403411
// $FlowFixMe[incompatible-type]: While plain JSON can return undefined we never do here.
404412
const partJSON: string = JSON.stringify(entry.value, resolveToJSON);
405-
data.append(formFieldPrefix + streamId, partJSON);
413+
data.append(streamPrefix, partJSON);
406414
iterator.next().then(progress, reject);
407415
} catch (x) {
408416
reject(x);

‎packages/react-dom-bindings/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "react-dom-bindings",
33
"description": "React implementation details for react-dom.",
4-
"version": "19.3.0",
4+
"version": "19.3.1",
55
"private": true,
66
"main": "index.js",
77
"repository": {

‎packages/react-dom/package.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-dom",
3-
"version": "19.3.0",
3+
"version": "19.3.1",
44
"description": "React package for working with the DOM.",
55
"main": "index.js",
66
"repository": {
@@ -20,7 +20,7 @@
2020
"scheduler": "^0.28.0"
2121
},
2222
"peerDependencies": {
23-
"react": "^19.3.0"
23+
"react": "^19.3.1"
2424
},
2525
"files": [
2626
"LICENSE",

‎packages/react-dom/src/__tests__/ReactDOMFizzForm-test.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,13 @@ describe('ReactDOMFizzForm', () => {
734734
ReactDOMServer.renderToReadableStream(<App />),
735735
);
736736
await readIntoContainer(stream);
737+
if (!ArrayBuffer.prototype.hasOwnProperty('detached')) {
738+
assertConsoleErrorDev([
739+
'React encountered an empty buffer when writing in an environment that does not support ArrayBuffer.prototype.detached. ' +
740+
'This could be a security issue if the buffer was not actually empty. ' +
741+
'Please use a modern browser that supports ArrayBuffer.prototype.detached.',
742+
]);
743+
}
737744

738745
const barField = container.querySelector('[name=bar]');
739746

‎packages/react-dom/src/__tests__/ReactDOMFizzServerBrowser-test.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@ let React;
2020
let ReactDOMFizzServer;
2121
let Suspense;
2222
let serverAct;
23+
let assertConsoleErrorDev;
2324

2425
describe('ReactDOMFizzServerBrowser', () => {
2526
beforeEach(() => {
2627
jest.resetModules();
2728

2829
patchMessageChannel();
2930
serverAct = require('internal-test-utils').serverAct;
31+
assertConsoleErrorDev =
32+
require('internal-test-utils').assertConsoleErrorDev;
3033

3134
React = require('react');
3235
ReactDOMFizzServer = require('react-dom/server.browser');
@@ -415,6 +418,13 @@ describe('ReactDOMFizzServerBrowser', () => {
415418
// eslint-disable-next-line jest/no-interpolation-in-snapshots
416419
`"<div><span></span></div><div>${str492}</div><div>${str492}</div>"`,
417420
);
421+
if (!ArrayBuffer.prototype.hasOwnProperty('detached')) {
422+
assertConsoleErrorDev([
423+
'React encountered an empty buffer when writing in an environment that does not support ArrayBuffer.prototype.detached. ' +
424+
'This could be a security issue if the buffer was not actually empty. ' +
425+
'Please use a modern browser that supports ArrayBuffer.prototype.detached.',
426+
]);
427+
}
418428

419429
// this size 2049 was chosen to be a couple base 2 orders larger than the current view
420430
// size. if the size changes in the future hopefully this will still exercise

0 commit comments

Comments
 (0)