forked from shakacode/react_on_rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildConsoleReplay.ts
44 lines (36 loc) · 1.16 KB
/
buildConsoleReplay.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import RenderUtils from './RenderUtils';
import scriptSanitizedVal from './scriptSanitizedVal';
/* eslint-disable @typescript-eslint/no-explicit-any */
declare global {
interface Console {
history?: {
arguments: Array<string | Record<string, string>>; level: "error" | "log" | "debug";
}[];
}
}
export function consoleReplay(): string {
// console.history is a global polyfill used in server rendering.
// $FlowFixMe
if (!(console.history instanceof Array)) {
return '';
}
const lines = console.history.map(msg => {
const stringifiedList = msg.arguments.map(arg => {
let val;
try {
val = (typeof arg === 'string' || arg instanceof String) ? arg : JSON.stringify(arg);
if (val === undefined) {
val = 'undefined';
}
} catch (e: any) {
val = `${e.message}: ${arg}`;
}
return scriptSanitizedVal(val as string);
});
return `console.${msg.level}.apply(console, ${JSON.stringify(stringifiedList)});`;
});
return lines.join('\n');
}
export default function buildConsoleReplay(): string {
return RenderUtils.wrapInScriptTags('consoleReplayLog', consoleReplay());
}