main
js 57 lines 1.67 KB
Raw
1 /**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @flow
8 */
9
10 // Keep in sync with ReactClientConsoleConfig
11 const badgeFormat = '[%s]';
12 const padLength = 1;
13 const pad = ' ';
14
15 // This mutates the args to remove any badges that was added by a FlightClient and
16 // returns the name in the badge. This is used when a FlightClient replays inside
17 // a FlightServer and we capture those replays.
18 export function unbadgeConsole(
19 methodName: string,
20 args: Array<any>,
21 ): null | string {
22 let offset = 0;
23 switch (methodName) {
24 case 'dir':
25 case 'dirxml':
26 case 'groupEnd':
27 case 'table': {
28 // These methods cannot be colorized because they don't take a formatting string.
29 // So we wouldn't have added any badge in the first place.
30 // $FlowFixMe[incompatible-type]
31 return null;
32 }
33 case 'assert': {
34 // assert takes formatting options as the second argument.
35 offset = 1;
36 }
37 }
38 const format = args[offset];
39 const badge = args[offset + 1];
40 if (
41 typeof format === 'string' &&
42 format.startsWith(badgeFormat) &&
43 typeof badge === 'string' &&
44 badge.startsWith(pad) &&
45 badge.endsWith(pad)
46 ) {
47 // Remove our badging from the arguments.
48 let unbadgedFormat = format.slice(badgeFormat.length);
49 if (unbadgedFormat[0] === ' ') {
50 // Spacing added on the Client if the original argument was a string.
51 unbadgedFormat = unbadgedFormat.slice(1);
52 }
53 args.splice(offset, 4, unbadgedFormat);
54 return badge.slice(padLength, badge.length - padLength);
55 }
56 return null;
57 }