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 = '\x1b[0m\x1b[7m%c%s\x1b[0m%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
+const padLength = 1;
22
+
23
+// This mutates the args to remove any badges that was added by a FlightClient and
24
+// returns the name in the badge. This is used when a FlightClient replays inside
25
+// a FlightServer and we capture those replays.
26
+export function unbadgeConsole(
27
+ methodName: string,
28
+ args: Array<any>,
29
+): null | string {
30
+ let offset = 0;
31
+ switch (methodName) {
32
+ case 'dir':
33
+ case 'dirxml':
34
+ case 'groupEnd':
35
+ case 'table': {
36
+ // These methods cannot be colorized because they don't take a formatting string.
37
+ // So we wouldn't have added any badge in the first place.
38
+ // $FlowFixMe
39
+ return null;
40
+ }
41
+ case 'assert': {
42
+ // assert takes formatting options as the second argument.
43
+ offset = 1;
44
+ }
45
+ }
46
+ const format = args[offset];
47
+ const style = args[offset + 1];
48
+ const badge = args[offset + 2];
49
+ if (
50
+ typeof format === 'string' &&
51
+ format.startsWith(badgeFormat) &&
52
+ style === badgeStyle &&
53
+ typeof badge === 'string'
54
+ ) {
55
+ // Remove our badging from the arguments.
56
+ args.splice(offset, 4, format.slice(badgeFormat.length));
57
+ return badge.slice(padLength, badge.length - padLength);
58
+ }
59
+ return null;
60
+}