Skip to content

Commit 3d06b40

Browse files
eps1lonunstubbableclaude
authored
[19.2.x][Flight] Improve transport protocol (#37844)
Co-authored-by: Hendrik Liebau <mail@hendrik-liebau.de> Co-authored-by: Claude Code (kimi-k3[1m]) <noreply@anthropic.com>
1 parent 1dd4ecb commit 3d06b40

32 files changed

Lines changed: 1583 additions & 252 deletions

‎.eslintrc.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,10 +613,12 @@ module.exports = {
613613
React$Portal: 'readonly',
614614
React$Ref: 'readonly',
615615
React$RefSetter: 'readonly',
616+
ReadableByteStreamController: 'readonly',
616617
ReadableStreamController: 'readonly',
617618
ReadableStreamReader: 'readonly',
618619
RequestInfo: 'readonly',
619620
RequestOptions: 'readonly',
621+
SharedArrayBuffer: 'readonly',
620622
StoreAsGlobal: 'readonly',
621623
symbol: 'readonly',
622624
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.2.8
10+
// 19.2.9
1111
//
1212
// The @canary channel appends additional information, with the scheme
1313
// <version>-<label>-<commit_sha>, e.g.:
1414
//
15-
// 19.2.8-canary-a1c2d3e4
15+
// 19.2.9-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.2.8';
21+
const ReactVersion = '19.2.9';
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.2.8",
4+
"version": "19.2.9",
55
"main": "index.js",
66
"repository": {
77
"type": "git",
@@ -27,7 +27,7 @@
2727
"scheduler": "^0.27.0"
2828
},
2929
"peerDependencies": {
30-
"react": "^19.2.8"
30+
"react": "^19.2.9"
3131
},
3232
"files": [
3333
"LICENSE",

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

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ const HALTED = 'halted'; // DEV-only. Means it never resolves even if connection
167167

168168
const __PROTO__ = '__proto__';
169169

170+
type RESPONSE_SYMBOL_TYPE = 'RESPONSE_SYMBOL'; // Fake symbol type.
171+
const RESPONSE_SYMBOL: RESPONSE_SYMBOL_TYPE = Symbol() as any;
172+
170173
type PendingChunk<T> = {
171174
status: 'pending',
172175
value: null | Array<InitializationReference | (T => mixed)>,
@@ -188,7 +191,7 @@ type BlockedChunk<T> = {
188191
type ResolvedModelChunk<T> = {
189192
status: 'resolved_model',
190193
value: UninitializedModel,
191-
reason: Response,
194+
reason: {[RESPONSE_SYMBOL_TYPE]: Response},
192195
_children: Array<SomeChunk<any>> | ProfilingResult, // Profiling-only
193196
_debugChunk: null | SomeChunk<ReactDebugInfoEntry>, // DEV-only
194197
_debugInfo: ReactDebugInfo, // DEV-only
@@ -736,7 +739,7 @@ function createResolvedModelChunk<T>(
736739
value: UninitializedModel,
737740
): ResolvedModelChunk<T> {
738741
// $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
739-
return new ReactPromise(RESOLVED_MODEL, value, response);
742+
return new ReactPromise(RESOLVED_MODEL, value, {[RESPONSE_SYMBOL]: response});
740743
}
741744

742745
function createResolvedModuleChunk<T>(
@@ -794,7 +797,9 @@ function createResolvedIteratorResultChunk<T>(
794797
const iteratorResultJSON =
795798
(done ? '{"done":true,"value":' : '{"done":false,"value":') + value + '}';
796799
// $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
797-
return new ReactPromise(RESOLVED_MODEL, iteratorResultJSON, response);
800+
return new ReactPromise(RESOLVED_MODEL, iteratorResultJSON, {
801+
[RESPONSE_SYMBOL]: response,
802+
});
798803
}
799804

800805
function resolveIteratorResultChunk<T>(
@@ -828,7 +833,7 @@ function resolveModelChunk<T>(
828833
const resolvedChunk: ResolvedModelChunk<T> = (chunk: any);
829834
resolvedChunk.status = RESOLVED_MODEL;
830835
resolvedChunk.value = value;
831-
resolvedChunk.reason = response;
836+
resolvedChunk.reason = {[RESPONSE_SYMBOL]: response};
832837
if (resolveListeners !== null) {
833838
// This is unfortunate that we're reading this eagerly if
834839
// we already have listeners attached since they might no
@@ -975,7 +980,7 @@ function initializeModelChunk<T>(chunk: ResolvedModelChunk<T>): void {
975980
initializingHandler = null;
976981

977982
const resolvedModel = chunk.value;
978-
const response = chunk.reason;
983+
const response = chunk.reason[RESPONSE_SYMBOL];
979984

980985
// We go to the BLOCKED state until we've fully resolved this.
981986
// We do this before parsing in case we try to initialize the same chunk
@@ -1747,6 +1752,15 @@ function loadServerReference<A: Iterable<any>, T>(
17471752
parentObject: Object,
17481753
key: string,
17491754
): (...A) => Promise<T> {
1755+
if (key === 'then') {
1756+
// This should never happen because we always serialize objects with then-functions
1757+
// as "thenable" which reduces to a chunk with no other fields.
1758+
return null as any;
1759+
}
1760+
const id = metaData.id;
1761+
if (typeof id !== 'string') {
1762+
return null as any;
1763+
}
17501764
if (!response._serverReferenceConfig) {
17511765
// In the normal case, we can't load this Server Reference in the current environment and
17521766
// we just return a proxy to it.
@@ -1759,27 +1773,27 @@ function loadServerReference<A: Iterable<any>, T>(
17591773
}
17601774
// If we have a module mapping we can load the real version of this Server Reference.
17611775
const serverReference: ClientReference<T> =
1762-
resolveServerReference<$FlowFixMe>(
1763-
response._serverReferenceConfig,
1764-
metaData.id,
1765-
);
1776+
resolveServerReference<$FlowFixMe>(response._serverReferenceConfig, id);
17661777

1778+
const bound = metaData.bound;
17671779
let promise: null | Thenable<any> = preloadModule(serverReference);
17681780
if (!promise) {
1769-
if (!metaData.bound) {
1770-
const resolvedValue = (requireModule(serverReference): any);
1781+
if (bound instanceof ReactPromise) {
1782+
promise = Promise.resolve(bound);
1783+
} else {
1784+
// We only await the bound arguments when they are one of our own chunks.
1785+
// Anything else could be a foreign thenable whose `then` we must not call.
1786+
const resolvedValue = requireModule(serverReference) as any;
17711787
registerBoundServerReference(
17721788
resolvedValue,
1773-
metaData.id,
1774-
metaData.bound,
1789+
id,
1790+
bound,
17751791
response._encodeFormAction,
17761792
);
17771793
return resolvedValue;
1778-
} else {
1779-
promise = Promise.resolve(metaData.bound);
17801794
}
1781-
} else if (metaData.bound) {
1782-
promise = Promise.all([promise, metaData.bound]);
1795+
} else if (bound instanceof ReactPromise) {
1796+
promise = Promise.all([promise, bound]);
17831797
}
17841798

17851799
let handler: InitializationHandler;
@@ -1800,17 +1814,20 @@ function loadServerReference<A: Iterable<any>, T>(
18001814
function fulfill(): void {
18011815
let resolvedValue = (requireModule(serverReference): any);
18021816

1803-
if (metaData.bound) {
1804-
// This promise is coming from us and should have initilialized by now.
1805-
const boundArgs: Array<any> = (metaData.bound: any).value.slice(0);
1817+
if (bound) {
1818+
// This promise is coming from us and should have initialized by now.
1819+
const promiseValue = (bound as any).value;
1820+
const boundArgs: Array<any> = isArray(promiseValue)
1821+
? promiseValue.slice(0)
1822+
: [];
18061823
boundArgs.unshift(null); // this
18071824
resolvedValue = resolvedValue.bind.apply(resolvedValue, boundArgs);
18081825
}
18091826

18101827
registerBoundServerReference(
18111828
resolvedValue,
1812-
metaData.id,
1813-
metaData.bound,
1829+
id,
1830+
bound,
18141831
response._encodeFormAction,
18151832
);
18161833

@@ -2227,7 +2244,17 @@ function extractIterator(response: Response, model: Array<any>): Iterator<any> {
22272244
return model[Symbol.iterator]();
22282245
}
22292246

2230-
function createModel(response: Response, model: any): any {
2247+
function createModel(
2248+
response: Response,
2249+
model: any,
2250+
parentObject: Object,
2251+
key: string,
2252+
): any {
2253+
if (key === 'then' && typeof model === 'function') {
2254+
// This should never happen because we always serialize objects with then-functions
2255+
// as "thenable" which reduces to a chunk with no other fields.
2256+
return null;
2257+
}
22312258
return model;
22322259
}
22332260

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

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

224224
const buffer = [];
225+
// Stream chunks use a dedicated namespace so that the server can tell
226+
// them apart from one-shot chunks without scanning.
227+
const streamPrefix = formFieldPrefix + 'S' + streamId;
225228

226229
function progress(entry: {done: boolean, value: ReactServerValue, ...}) {
227230
if (entry.done) {
228231
const blobId = nextPartId++;
229232
data.append(formFieldPrefix + blobId, new Blob(buffer));
230-
data.append(
231-
formFieldPrefix + streamId,
232-
'"$o' + blobId.toString(16) + '"',
233-
);
234-
data.append(formFieldPrefix + streamId, 'C'); // Close signal
233+
data.append(streamPrefix, '"$o' + blobId.toString(16) + '"');
234+
data.append(streamPrefix, 'C'); // Close signal
235235
pendingParts--;
236236
if (pendingParts === 0) {
237237
resolve(data);
@@ -256,9 +256,13 @@ export function processReply(
256256
pendingParts++;
257257
const streamId = nextPartId++;
258258

259+
// Stream chunks use a dedicated namespace so that the server can tell
260+
// them apart from one-shot chunks without scanning.
261+
const streamPrefix = formFieldPrefix + 'S' + streamId;
262+
259263
function progress(entry: {done: boolean, value: ReactServerValue, ...}) {
260264
if (entry.done) {
261-
data.append(formFieldPrefix + streamId, 'C'); // Close signal
265+
data.append(streamPrefix, 'C'); // Close signal
262266
pendingParts--;
263267
if (pendingParts === 0) {
264268
resolve(data);
@@ -267,7 +271,7 @@ export function processReply(
267271
try {
268272
// $FlowFixMe[incompatible-type]: While plain JSON can return undefined we never do here.
269273
const partJSON: string = JSON.stringify(entry.value, resolveToJSON);
270-
data.append(formFieldPrefix + streamId, partJSON);
274+
data.append(streamPrefix, partJSON);
271275
reader.read().then(progress, reject);
272276
} catch (x) {
273277
reject(x);
@@ -311,6 +315,10 @@ export function processReply(
311315
// iterated more than once on the receiving server.
312316
const isIterator = iterable === iterator;
313317

318+
// Stream chunks use a dedicated namespace so that the server can tell
319+
// them apart from one-shot chunks without scanning.
320+
const streamPrefix = formFieldPrefix + 'S' + streamId;
321+
314322
// There's a race condition between when the stream is aborted and when the promise
315323
// resolves so we track whether we already aborted it to avoid writing twice.
316324
function progress(
@@ -320,14 +328,14 @@ export function processReply(
320328
) {
321329
if (entry.done) {
322330
if (entry.value === undefined) {
323-
data.append(formFieldPrefix + streamId, 'C'); // Close signal
331+
data.append(streamPrefix, 'C'); // Close signal
324332
} else {
325333
// Unlike streams, the last value may not be undefined. If it's not
326334
// we outline it and encode a reference to it in the closing instruction.
327335
try {
328336
// $FlowFixMe[incompatible-type]: While plain JSON can return undefined we never do here.
329337
const partJSON: string = JSON.stringify(entry.value, resolveToJSON);
330-
data.append(formFieldPrefix + streamId, 'C' + partJSON); // Close signal
338+
data.append(streamPrefix, 'C' + partJSON); // Close signal
331339
} catch (x) {
332340
reject(x);
333341
return;
@@ -341,7 +349,7 @@ export function processReply(
341349
try {
342350
// $FlowFixMe[incompatible-type]: While plain JSON can return undefined we never do here.
343351
const partJSON: string = JSON.stringify(entry.value, resolveToJSON);
344-
data.append(formFieldPrefix + streamId, partJSON);
352+
data.append(streamPrefix, partJSON);
345353
iterator.next().then(progress, reject);
346354
} catch (x) {
347355
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.2.8",
4+
"version": "19.2.9",
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.2.8",
3+
"version": "19.2.9",
44
"description": "React package for working with the DOM.",
55
"main": "index.js",
66
"repository": {
@@ -20,7 +20,7 @@
2020
"scheduler": "^0.27.0"
2121
},
2222
"peerDependencies": {
23-
"react": "^19.2.8"
23+
"react": "^19.2.9"
2424
},
2525
"files": [
2626
"LICENSE",

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

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

738748
const barField = container.querySelector('[name=bar]');
739749

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

Lines changed: 13 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');
@@ -411,6 +414,16 @@ describe('ReactDOMFizzServerBrowser', () => {
411414
// eslint-disable-next-line jest/no-interpolation-in-snapshots
412415
`"<div><span></span></div><div>${str492}</div><div>${str492}</div>"`,
413416
);
417+
if (!ArrayBuffer.prototype.hasOwnProperty('detached')) {
418+
assertConsoleErrorDev(
419+
[
420+
'React encountered an empty buffer when writing in an environment that does not support ArrayBuffer.prototype.detached. ' +
421+
'This could be a security issue if the buffer was not actually empty. ' +
422+
'Please use a modern browser that supports ArrayBuffer.prototype.detached.',
423+
],
424+
{withoutStack: true},
425+
);
426+
}
414427

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

0 commit comments

Comments
 (0)
⌂ 换网址