Repository: facebook/react
File: packages/react-server/src/ReactFlightServer.js (JavaScript)

Code before:
  if (value instanceof Float64Array) {
    // double
    emitTypedArrayChunk(request, id, 'g', value);
    return;
  }
  if (value instanceof BigInt64Array) {
    // number
    emitTypedArrayChunk(request, id, 'M', value);
    return;
  }
  if (value instanceof BigUint64Array) {
    // unsigned number
    // We use "m" instead of "n" since JSON can start with "null"
    emitTypedArrayChunk(request, id, 'm', value);
    return;
  }
  if (value instanceof DataView) {
    emitTypedArrayChunk(request, id, 'V', value);
    return;
  }
  // For anything else we need to try to serialize it using JSON.
  // We stash the outer parent size so we can restore it when we exit.
  // We don't reset the serialized size counter from reentry because that indicates that we
  // are outlining a model and we actually want to include that size into the parent since
  // it will still block the parent row. It only restores to zero at the top of the stack.
  const parentSerializedSize = 0;
  try {
    // $FlowFixMe[incompatible-type] stringify can return null for undefined but we never do
    const json: string = stringify(value, task.toJSON);
    emitModelChunk(request, task.id, json);
  } finally {
    serializedSize = parentSerializedSize;
  }
}

function erroredTask(request: Request, task: Task, error: mixed): void {
  if (enableProfilerTimer && enableComponentPerformanceTrack) {
    if (task.timed) {
      emitTimingChunk(request, task.id, performance.now());
    }
  }
  task.status = ERRORED;
  if (
    enablePostpone &&
    typeof error === 'object' &&
    error !== null &&
    error.$$typeof === REACT_POSTPONE_TYPE
  ) {
    const postponeInstance: Postpone = (error: any);
    logPostpone(request, postponeInstance.message, task);
    emitPostponeChunk(request, task.id, postponeInstance);

Review comment (bug):
I'm probably misinterpreting this comment, but to me "We stash the outer parent size so we can restore it when we exit" sounds like `const parentSerializedSize = serializedSize` here, along with `serializedSize = parentSerializedSize` in `finally`. But then there would need to be `serializedSize = 0` somewhere else to match "It only restores to zero at the top of the stack". Which is wrong, a) the comment, b) the code, or c) my interpretation?

Code after:
  if (value instanceof Float64Array) {
    // double
    emitTypedArrayChunk(request, id, 'g', value);
    return;
  }
  if (value instanceof BigInt64Array) {
    // number
    emitTypedArrayChunk(request, id, 'M', value);
    return;
  }
  if (value instanceof BigUint64Array) {
    // unsigned number
    // We use "m" instead of "n" since JSON can start with "null"
    emitTypedArrayChunk(request, id, 'm', value);
    return;
  }
  if (value instanceof DataView) {
    emitTypedArrayChunk(request, id, 'V', value);
    return;
  }
  // For anything else we need to try to serialize it using JSON.
  // We stash the outer parent size so we can restore it when we exit.
  const parentSerializedSize = serializedSize;
  // We don't reset the serialized size counter from reentry because that indicates that we
  // are outlining a model and we actually want to include that size into the parent since
  // it will still block the parent row. It only restores to zero at the top of the stack.
  try {
    // $FlowFixMe[incompatible-type] stringify can return null for undefined but we never do
    const json: string = stringify(value, task.toJSON);
    emitModelChunk(request, task.id, json);
  } finally {
    serializedSize = parentSerializedSize;
  }
}

function erroredTask(request: Request, task: Task, error: mixed): void {
  if (enableProfilerTimer && enableComponentPerformanceTrack) {
    if (task.timed) {
      emitTimingChunk(request, task.id, performance.now());
    }
  }
  task.status = ERRORED;
  if (
    enablePostpone &&
    typeof error === 'object' &&
    error !== null &&
    error.$$typeof === REACT_POSTPONE_TYPE
  ) {
    const postponeInstance: Postpone = (error: any);
    logPostpone(request, postponeInstance.message, task);
    emitPostponeChunk(request, task.id, postponeInstance);