Files
react/packages/react-server/src/ReactServerConsoleConfigPlain.js
Sebastian Markbåge ef8b6fa257 [Flight] Don't double badge consoles that are replayed from a third party (#33685)
If a FlightClient runs inside a FlightServer like fetching from a third
party and that logs, then we currently double badge them since we just
add on another badge. The issue is that this might be unnecessarily
noisy but we also transfer the original format of the current server
into the second badge.

This extracts our own badge and then adds the environment name as
structured data which lets the client decide how to format it.

Before:

<img width="599" alt="Screenshot 2025-07-02 at 2 30 07 PM"
src="https://github.com/user-attachments/assets/4bf26a29-b3a8-4024-8eb9-a3f90dbff97a"
/>

After:

<img width="590" alt="Screenshot 2025-07-02 at 2 32 56 PM"
src="https://github.com/user-attachments/assets/f06bbb6d-fbb1-4ae6-b0e3-775849fe3c53"
/>
2025-07-02 18:22:14 -04:00

53 lines
1.4 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
// Keep in sync with ReactClientConsoleConfig
const badgeFormat = '[%s] ';
const padLength = 1;
const pad = ' ';
// This mutates the args to remove any badges that was added by a FlightClient and
// returns the name in the badge. This is used when a FlightClient replays inside
// a FlightServer and we capture those replays.
export function unbadgeConsole(
methodName: string,
args: Array<any>,
): null | string {
let offset = 0;
switch (methodName) {
case 'dir':
case 'dirxml':
case 'groupEnd':
case 'table': {
// These methods cannot be colorized because they don't take a formatting string.
// So we wouldn't have added any badge in the first place.
// $FlowFixMe
return null;
}
case 'assert': {
// assert takes formatting options as the second argument.
offset = 1;
}
}
const format = args[offset];
const badge = args[offset + 1];
if (
typeof format === 'string' &&
format.startsWith(badgeFormat) &&
typeof badge === 'string' &&
badge.startsWith(pad) &&
badge.endsWith(pad)
) {
// Remove our badging from the arguments.
args.splice(offset, 2, format.slice(badgeFormat.length));
return badge.slice(padLength, badge.length - padLength);
}
return null;
}