-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
index.js
209 lines (176 loc) · 4.1 KB
/
index.js
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import errorConstructors from './error-constructors.js';
export class NonError extends Error {
name = 'NonError';
constructor(message) {
super(NonError._prepareSuperMessage(message));
}
static _prepareSuperMessage(message) {
try {
return JSON.stringify(message);
} catch {
return String(message);
}
}
}
const commonProperties = [
{
property: 'name',
enumerable: false,
},
{
property: 'message',
enumerable: false,
},
{
property: 'stack',
enumerable: false,
},
{
property: 'code',
enumerable: true,
},
{
property: 'cause',
enumerable: false,
},
];
const toJsonWasCalled = new WeakSet();
const toJSON = from => {
toJsonWasCalled.add(from);
const json = from.toJSON();
toJsonWasCalled.delete(from);
return json;
};
const getErrorConstructor = name => errorConstructors.get(name) ?? Error;
// eslint-disable-next-line complexity
const destroyCircular = ({
from,
seen,
to,
forceEnumerable,
maxDepth,
depth,
useToJSON,
serialize,
}) => {
if (!to) {
if (Array.isArray(from)) {
to = [];
} else if (!serialize && isErrorLike(from)) {
const Error = getErrorConstructor(from.name);
to = new Error();
} else {
to = {};
}
}
seen.push(from);
if (depth >= maxDepth) {
return to;
}
if (useToJSON && typeof from.toJSON === 'function' && !toJsonWasCalled.has(from)) {
return toJSON(from);
}
const continueDestroyCircular = value => destroyCircular({
from: value,
seen: [...seen],
forceEnumerable,
maxDepth,
depth,
useToJSON,
serialize,
});
for (const [key, value] of Object.entries(from)) {
if (value && value instanceof Uint8Array && value.constructor.name === 'Buffer') {
to[key] = '[object Buffer]';
continue;
}
// TODO: Use `stream.isReadable()` when targeting Node.js 18.
if (value !== null && typeof value === 'object' && typeof value.pipe === 'function') {
to[key] = '[object Stream]';
continue;
}
if (typeof value === 'function') {
continue;
}
if (!value || typeof value !== 'object') {
// Gracefully handle non-configurable errors like `DOMException`.
try {
to[key] = value;
} catch {}
continue;
}
if (!seen.includes(from[key])) {
depth++;
to[key] = continueDestroyCircular(from[key]);
continue;
}
to[key] = '[Circular]';
}
for (const {property, enumerable} of commonProperties) {
if (from[property] !== undefined && from[property] !== null) {
Object.defineProperty(to, property, {
value: isErrorLike(from[property]) ? continueDestroyCircular(from[property]) : from[property],
enumerable: forceEnumerable ? true : enumerable,
configurable: true,
writable: true,
});
}
}
return to;
};
export function serializeError(value, options = {}) {
const {
maxDepth = Number.POSITIVE_INFINITY,
useToJSON = true,
} = options;
if (typeof value === 'object' && value !== null) {
return destroyCircular({
from: value,
seen: [],
forceEnumerable: true,
maxDepth,
depth: 0,
useToJSON,
serialize: true,
});
}
// People sometimes throw things besides Error objects…
if (typeof value === 'function') {
// `JSON.stringify()` discards functions. We do too, unless a function is thrown directly.
// We intentionally use `||` because `.name` is an empty string for anonymous functions.
return `[Function: ${value.name || 'anonymous'}]`;
}
return value;
}
export function deserializeError(value, options = {}) {
const {maxDepth = Number.POSITIVE_INFINITY} = options;
if (value instanceof Error) {
return value;
}
if (isMinimumViableSerializedError(value)) {
const Error = getErrorConstructor(value.name);
return destroyCircular({
from: value,
seen: [],
to: new Error(),
maxDepth,
depth: 0,
serialize: false,
});
}
return new NonError(value);
}
export function isErrorLike(value) {
return Boolean(value)
&& typeof value === 'object'
&& 'name' in value
&& 'message' in value
&& 'stack' in value;
}
function isMinimumViableSerializedError(value) {
return Boolean(value)
&& typeof value === 'object'
&& 'message' in value
&& !Array.isArray(value);
}
export {default as errorConstructors} from './error-constructors.js';