UNPKG

942 BPlain TextView Raw
1import { configure } from 'safe-stable-stringify';
2
3const safeStringify = configure({
4 deterministic: false,
5 // The reason for the following values is because it is fairly easy
6 // to accidentally pass astronomically large objects to the logger.
7 // For context, we were debugging a UI slowdown that was caused by
8 // unknowingly trying to pass 5MB worth of data to the logger context.
9 //
10 // I am starting with hard limits for now to assess the impact of the changes,
11 // but we may want to make these configurable in the future.
12 maximumBreadth: 20,
13 maximumDepth: 10,
14 strict: false,
15});
16
17export const stringify = (value: unknown): string => {
18 try {
19 return safeStringify(value) ?? '';
20 } catch (error) {
21 // The only time I've seen this happen is when the value was excessively large.
22 // eslint-disable-next-line no-console
23 console.error('[roarr] could not serialize value', value);
24
25 throw error;
26 }
27};