Registry Mirror
Skip to content

File tree

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

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,6 @@ type Response = {
355355
_encodeFormAction: void | EncodeFormActionCallback,
356356
_nonce: ?string,
357357
_chunks: Map<number, SomeChunk<any>>,
358-
_fromJSON: (key: string, value: JSONValue) => any,
359358
_stringDecoder: StringDecoder,
360359
_closed: boolean,
361360
_closedReason: mixed,
@@ -2302,6 +2301,11 @@ function defineLazyGetter<T>(
23022301
// TODO: We should ideally throw here to indicate a difference.
23032302
return OMITTED_PROP_ERROR;
23042303
},
2304+
// no-op: the walk function may try to reassign this property after
2305+
// parseModelString returns. With the JSON.parse reviver, the engine's
2306+
// internal CreateDataProperty silently failed. We use a no-op setter
2307+
// to match that behavior in strict mode.
2308+
set: function () {},
23052309
enumerable: true,
23062310
configurable: false,
23072311
});
@@ -2622,6 +2626,11 @@ function parseModelString(
26222626
// TODO: We should ideally throw here to indicate a difference.
26232627
return OMITTED_PROP_ERROR;
26242628
},
2629+
// no-op: the walk function may try to reassign this property
2630+
// after parseModelString returns. With the JSON.parse reviver,
2631+
// the engine's internal CreateDataProperty silently failed.
2632+
// We use a no-op setter to match that behavior in strict mode.
2633+
set: function () {},
26252634
enumerable: true,
26262635
configurable: false,
26272636
});
@@ -2699,7 +2708,6 @@ function ResponseInstance(
26992708
this._nonce = nonce;
27002709
this._chunks = chunks;
27012710
this._stringDecoder = createStringDecoder();
2702-
this._fromJSON = (null: any);
27032711
this._closed = false;
27042712
this._closedReason = null;
27052713
this._allowPartialStream = allowPartialStream;
@@ -2783,9 +2791,6 @@ function ResponseInstance(
27832791
markAllTracksInOrder();
27842792
}
27852793
}
2786-
2787-
// Don't inline this call because it causes closure to outline the call above.
2788-
this._fromJSON = createFromJSONCallback(this);
27892794
}
27902795

27912796
export function createResponse(
@@ -5259,24 +5264,52 @@ export function processStringChunk(
52595264
}
52605265

52615266
function parseModel<T>(response: Response, json: UninitializedModel): T {
5262-
return JSON.parse(json, response._fromJSON);
5267+
const rawModel = JSON.parse(json);
5268+
// Pass a wrapper object as parentObject to match the original JSON.parse
5269+
// reviver behavior, where the root value's reviver receives {"": rootValue}
5270+
// as `this`. This ensures parentObject is never null when accessed downstream.
5271+
return reviveModel(response, rawModel, {'': rawModel}, '');
52635272
}
52645273

5265-
function createFromJSONCallback(response: Response) {
5266-
// $FlowFixMe[missing-this-annot]
5267-
return function (key: string, value: JSONValue) {
5268-
if (key === __PROTO__) {
5269-
return undefined;
5274+
function reviveModel(
5275+
response: Response,
5276+
value: JSONValue,
5277+
parentObject: Object,
5278+
key: string,
5279+
): any {
5280+
if (typeof value === 'string') {
5281+
if (value[0] === '$') {
5282+
return parseModelString(response, parentObject, key, value);
52705283
}
5271-
if (typeof value === 'string') {
5272-
// We can't use .bind here because we need the "this" value.
5273-
return parseModelString(response, this, key, value);
5284+
return value;
5285+
}
5286+
if (typeof value !== 'object' || value === null) {
5287+
return value;
5288+
}
5289+
if (isArray(value)) {
5290+
for (let i = 0; i < value.length; i++) {
5291+
(value: any)[i] = reviveModel(response, value[i], value, '' + i);
52745292
}
5275-
if (typeof value === 'object' && value !== null) {
5293+
if (value[0] === REACT_ELEMENT_TYPE) {
5294+
// React element tuple
52765295
return parseModelTuple(response, value);
52775296
}
52785297
return value;
5279-
};
5298+
}
5299+
// Plain object
5300+
for (const k in value) {
5301+
if (k === __PROTO__) {
5302+
delete (value: any)[k];
5303+
} else {
5304+
const walked = reviveModel(response, (value: any)[k], value, k);
5305+
if (walked !== undefined) {
5306+
(value: any)[k] = walked;
5307+
} else {
5308+
delete (value: any)[k];
5309+
}
5310+
}
5311+
}
5312+
return value;
52805313
}
52815314

52825315
export function close(weakResponse: WeakResponse): void {

‎packages/react-noop-renderer/src/ReactNoopFlightClient.js‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ const {createResponse, createStreamState, processBinaryChunk, getRoot, close} =
4343
requireModule(idx: string) {
4444
return readModule(idx);
4545
},
46-
parseModel(response: Response, json) {
47-
return JSON.parse(json, response._fromJSON);
48-
},
4946
bindToConsole(methodName, args, badgeName) {
5047
return Function.prototype.bind.apply(
5148
// eslint-disable-next-line react-internal/no-production-logging

0 commit comments

Comments
 (0)