main
js 66 lines 2 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 = '%c%s%c';
12 // Same badge styling as DevTools.
13 const badgeStyle =
14 // We use a fixed background if light-dark is not supported, otherwise
15 // we use a transparent background.
16 'background: #e6e6e6;' +
17 'background: light-dark(rgba(0,0,0,0.1), rgba(255,255,255,0.25));' +
18 'color: #000000;' +
19 'color: light-dark(#000000, #ffffff);' +
20 'border-radius: 2px';
21
22 const padLength = 1;
23
24 // This mutates the args to remove any badges that was added by a FlightClient and
25 // returns the name in the badge. This is used when a FlightClient replays inside
26 // a FlightServer and we capture those replays.
27 export function unbadgeConsole(
28 methodName: string,
29 args: Array<any>,
30 ): null | string {
31 let offset = 0;
32 switch (methodName) {
33 case 'dir':
34 case 'dirxml':
35 case 'groupEnd':
36 case 'table': {
37 // These methods cannot be colorized because they don't take a formatting string.
38 // So we wouldn't have added any badge in the first place.
39 // $FlowFixMe[incompatible-type]
40 return null;
41 }
42 case 'assert': {
43 // assert takes formatting options as the second argument.
44 offset = 1;
45 }
46 }
47 const format = args[offset];
48 const style = args[offset + 1];
49 const badge = args[offset + 2];
50 if (
51 typeof format === 'string' &&
52 format.startsWith(badgeFormat) &&
53 style === badgeStyle &&
54 typeof badge === 'string'
55 ) {
56 // Remove our badging from the arguments.
57 let unbadgedFormat = format.slice(badgeFormat.length);
58 if (unbadgedFormat[0] === ' ') {
59 // Spacing added on the Client if the original argument was a string.
60 unbadgedFormat = unbadgedFormat.slice(1);
61 }
62 args.splice(offset, 4, unbadgedFormat);
63 return badge.slice(padLength, badge.length - padLength);
64 }
65 return null;
66 }