Repository: facebook/react
File: packages/react-client/src/ReactFlightClient.js (JavaScript)

Code before:
      // this use case.
      throw new Error(
        'String chunks need to be passed in their original shape. ' +
          'Not split into smaller string chunks. ' +
          'This is a bug in the wiring of the React streams.',
      );
    }
  }
  streamState._rowState = rowState;
  streamState._rowID = rowID;
  streamState._rowTag = rowTag;
  streamState._rowLength = rowLength;
}

function parseModel<T>(response: Response, json: UninitializedModel): T {
  return response._walkJSON(JSON.parse(json), null, '');
}

function createWalkParsedJSON(response: Response) {
  function walk(
    value: JSONValue,
    parentObject: Object | null,
    key: string,
  ): any {
    if (typeof value === 'string') {
      if (value[0] === '$') {
        return parseModelString(response, parentObject, key, value);
      }
      return value;
    }
    if (typeof value !== 'object' || value === null) {
      return value;
    }
    if (Array.isArray(value)) {
      if (value[0] === REACT_ELEMENT_TYPE) {
        // React element tuple
        return parseModelTuple(response, value);
      }
      for (let i = 0; i < value.length; i++) {
        (value: any)[i] = walk(value[i], value, '' + i);
      }
      return value;
    }
    // Plain object
    for (const k in value) {
      if (k === __PROTO__) {
        delete (value: any)[k];
      } else {
        const walked = walk((value: any)[k], value, k);
        if (walked !== undefined) {
          (value: any)[k] = walked;

Review comment (suggestion):
I don't think this check is an optimization tbh. it's checked in `parseModelString` and will just return the value there.

Code after:
      buffer.length = 0;
    } else if (chunk.length !== i) {
      // The rest of this row is in a future chunk. We only support passing the
      // string from chunks in their entirety. Not split up into smaller string chunks.
      // We could support this by buffering them but we shouldn't need to for
      // this use case.
      throw new Error(
        'String chunks need to be passed in their original shape. ' +
          'Not split into smaller string chunks. ' +
          'This is a bug in the wiring of the React streams.',
      );
    }
  }
  streamState._rowState = rowState;
  streamState._rowID = rowID;
  streamState._rowTag = rowTag;
  streamState._rowLength = rowLength;
}

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

function reviveModel(
  response: Response,
  value: JSONValue,
  parentObject: Object,
  key: string,
): any {
  if (typeof value === 'string') {
    if (value[0] === '$') {
      return parseModelString(response, parentObject, key, value);
    }
    return value;
  }
  if (typeof value !== 'object' || value === null) {
    return value;
  }
  if (isArray(value)) {
    for (let i = 0; i < value.length; i++) {
      (value: any)[i] = reviveModel(response, value[i], value, '' + i);
    }
    if (value[0] === REACT_ELEMENT_TYPE) {
      // React element tuple
      return parseModelTuple(response, value);
    }
    return value;