faultferry 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.es.md +220 -0
- package/README.ja.md +220 -0
- package/README.md +219 -2
- package/README.pt.md +220 -0
- package/dist/index.cjs +875 -0
- package/dist/index.d.cts +121 -0
- package/index.d.ts +121 -0
- package/package.json +65 -4
- package/src/index.js +868 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,875 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// faultferry: send errors across processes and bring them back whole.
|
|
3
|
+
//
|
|
4
|
+
// serialize() turns any thrown value into a plain JSON safe value and
|
|
5
|
+
// deserialize() rebuilds it. Errors travel as an envelope tagged with the
|
|
6
|
+
// "$error" key; values JSON cannot hold travel as "$value" records; shared
|
|
7
|
+
// and circular references travel as "$ref" records that point at the
|
|
8
|
+
// pre-order index of an earlier container; a plain object that happens to
|
|
9
|
+
// use one of those keys is wrapped in "$escape". Both directions walk the
|
|
10
|
+
// fields in the same fixed order, so reference indexes always line up.
|
|
11
|
+
|
|
12
|
+
const OBJECT_PROTO = Object.prototype;
|
|
13
|
+
const ARRAY_PROTO = Array.prototype;
|
|
14
|
+
const objectToString = OBJECT_PROTO.toString;
|
|
15
|
+
const getOwnDescriptor = Object.getOwnPropertyDescriptor;
|
|
16
|
+
const defineProperty = Object.defineProperty;
|
|
17
|
+
const getPrototypeOf = Object.getPrototypeOf;
|
|
18
|
+
const fnToString = Function.prototype.toString;
|
|
19
|
+
|
|
20
|
+
const TAGS = ['$error', '$value', '$ref', '$escape'];
|
|
21
|
+
// Own properties of an error that travel as dedicated envelope fields when
|
|
22
|
+
// they are non-enumerable (the way the constructors create them).
|
|
23
|
+
const SPECIAL = ['cause', 'errors', 'error', 'suppressed'];
|
|
24
|
+
const NATIVE_NAMES = [
|
|
25
|
+
'Error',
|
|
26
|
+
'TypeError',
|
|
27
|
+
'RangeError',
|
|
28
|
+
'ReferenceError',
|
|
29
|
+
'SyntaxError',
|
|
30
|
+
'EvalError',
|
|
31
|
+
'URIError',
|
|
32
|
+
'AggregateError',
|
|
33
|
+
'SuppressedError',
|
|
34
|
+
'DOMException',
|
|
35
|
+
];
|
|
36
|
+
|
|
37
|
+
/** @type {Map<string, Function>} name to native constructor of this realm */
|
|
38
|
+
const natives = new Map();
|
|
39
|
+
for (const name of NATIVE_NAMES) {
|
|
40
|
+
const ctor = globalThis[name];
|
|
41
|
+
if (typeof ctor === 'function') natives.set(name, ctor);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** @type {Map<string, Function>} key to class, natives included */
|
|
45
|
+
const registry = new Map(natives);
|
|
46
|
+
/** @type {Map<Function, string>} class to key, natives included */
|
|
47
|
+
const registryKeys = new Map();
|
|
48
|
+
for (const [name, ctor] of natives) registryKeys.set(ctor, name);
|
|
49
|
+
|
|
50
|
+
/** Cache of the native base constructor used to build a registered class. */
|
|
51
|
+
const baseCache = new WeakMap();
|
|
52
|
+
|
|
53
|
+
function hasOwn(object, key) {
|
|
54
|
+
try {
|
|
55
|
+
return getOwnDescriptor(object, key) !== undefined;
|
|
56
|
+
} catch {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function protoOf(value) {
|
|
62
|
+
try {
|
|
63
|
+
return getPrototypeOf(value);
|
|
64
|
+
} catch {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function isObjectLike(value) {
|
|
70
|
+
return value !== null && (typeof value === 'object' || typeof value === 'function');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function fallbackIsError(value) {
|
|
74
|
+
if (!isObjectLike(value)) return false;
|
|
75
|
+
let tag;
|
|
76
|
+
try {
|
|
77
|
+
tag = value[Symbol.toStringTag];
|
|
78
|
+
} catch {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
if (typeof tag !== 'string') {
|
|
82
|
+
// Without a string tag, Object.prototype.toString reports the internal
|
|
83
|
+
// [[ErrorData]] slot directly, in any realm.
|
|
84
|
+
try {
|
|
85
|
+
return objectToString.call(value) === '[object Error]';
|
|
86
|
+
} catch {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
// A string tag hides the slot from toString. The structured clone
|
|
91
|
+
// algorithm still sees it: only objects with [[ErrorData]] clone into
|
|
92
|
+
// errors, everything else clones into a plain object or throws.
|
|
93
|
+
if (typeof structuredClone !== 'function') return value instanceof Error;
|
|
94
|
+
// DOMException is a platform object that clones into a DOMException; the
|
|
95
|
+
// HTML standard gives it [[ErrorData]], so it counts as an error.
|
|
96
|
+
try {
|
|
97
|
+
const copy = structuredClone(value);
|
|
98
|
+
return objectToString.call(copy) === '[object Error]' || isDomInstance(copy);
|
|
99
|
+
} catch {
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function isDomInstance(value) {
|
|
105
|
+
const Dom = natives.get('DOMException');
|
|
106
|
+
return Dom !== undefined && value instanceof Dom;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Tells whether a value is a real error object (it has the internal
|
|
111
|
+
* [[ErrorData]] slot), including errors from other realms such as iframes
|
|
112
|
+
* and node:vm contexts. Uses the native Error.isError when available.
|
|
113
|
+
* @param {unknown} value
|
|
114
|
+
* @returns {boolean}
|
|
115
|
+
*/
|
|
116
|
+
function isError(value) {
|
|
117
|
+
const native = Error.isError;
|
|
118
|
+
if (typeof native === 'function') return native(value);
|
|
119
|
+
return fallbackIsError(value);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function isDomException(value) {
|
|
123
|
+
try {
|
|
124
|
+
if (isDomInstance(value)) return true;
|
|
125
|
+
} catch {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
try {
|
|
129
|
+
return objectToString.call(value) === '[object DOMException]';
|
|
130
|
+
} catch {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function inheritsError(value) {
|
|
136
|
+
let proto = protoOf(value);
|
|
137
|
+
for (let i = 0; proto !== null && i < 100; i++) {
|
|
138
|
+
if (proto === Error.prototype) return true;
|
|
139
|
+
proto = protoOf(proto);
|
|
140
|
+
}
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
function functionName(fn) {
|
|
146
|
+
try {
|
|
147
|
+
const name = fn.name;
|
|
148
|
+
return typeof name === 'string' ? name : '';
|
|
149
|
+
} catch {
|
|
150
|
+
return '';
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Function.prototype.toString never throws for callables, proxies included.
|
|
155
|
+
function isNativeFunction(fn) {
|
|
156
|
+
return fnToString.call(fn).endsWith('{ [native code] }');
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Finds the registry key of an error by walking its prototype chain. */
|
|
160
|
+
function classKeyOf(error, local) {
|
|
161
|
+
let proto = protoOf(error);
|
|
162
|
+
for (let i = 0; proto !== null && i < 100; i++) {
|
|
163
|
+
let ctor;
|
|
164
|
+
try {
|
|
165
|
+
const desc = getOwnDescriptor(proto, 'constructor');
|
|
166
|
+
ctor = desc && desc.value;
|
|
167
|
+
} catch {
|
|
168
|
+
ctor = undefined;
|
|
169
|
+
}
|
|
170
|
+
if (typeof ctor === 'function') {
|
|
171
|
+
const key = (local && local.keys.get(ctor)) || registryKeys.get(ctor);
|
|
172
|
+
if (key !== undefined) return key;
|
|
173
|
+
// A native constructor from another realm (iframe, node:vm context).
|
|
174
|
+
const name = functionName(ctor);
|
|
175
|
+
if (natives.has(name) && (name === 'DOMException' || isNativeFunction(ctor))) return name;
|
|
176
|
+
}
|
|
177
|
+
proto = protoOf(proto);
|
|
178
|
+
}
|
|
179
|
+
return isDomException(error) ? 'DOMException' : 'Error';
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function checkClass(ErrorClass, name, where) {
|
|
183
|
+
if (typeof ErrorClass !== 'function' || !isObjectLike(ErrorClass.prototype)) {
|
|
184
|
+
throw new TypeError(`${where}: expected an error class, got ${typeof ErrorClass}`);
|
|
185
|
+
}
|
|
186
|
+
const key = name === undefined ? functionName(ErrorClass) : name;
|
|
187
|
+
if (typeof key !== 'string' || key === '') {
|
|
188
|
+
throw new TypeError(`${where}: the class needs a non empty name`);
|
|
189
|
+
}
|
|
190
|
+
if (natives.has(key) && natives.get(key) !== ErrorClass) {
|
|
191
|
+
throw new TypeError(`${where}: "${key}" is the name of a built in error`);
|
|
192
|
+
}
|
|
193
|
+
return key;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Registers a custom error class so deserialize() can rebuild its
|
|
198
|
+
* instances with the right prototype. Register the same classes on both
|
|
199
|
+
* sides. Returns the class, so it also works as a decorator style helper.
|
|
200
|
+
* @template {Function} T
|
|
201
|
+
* @param {T} ErrorClass
|
|
202
|
+
* @param {string} [name] key used on the wire, defaults to ErrorClass.name
|
|
203
|
+
* @returns {T}
|
|
204
|
+
*/
|
|
205
|
+
function register(ErrorClass, name) {
|
|
206
|
+
const key = checkClass(ErrorClass, name, 'register');
|
|
207
|
+
if (natives.has(key)) return ErrorClass;
|
|
208
|
+
const previous = registry.get(key);
|
|
209
|
+
if (previous !== undefined) registryKeys.delete(previous);
|
|
210
|
+
const previousKey = registryKeys.get(ErrorClass);
|
|
211
|
+
if (previousKey !== undefined) registry.delete(previousKey);
|
|
212
|
+
registry.set(key, ErrorClass);
|
|
213
|
+
registryKeys.set(ErrorClass, key);
|
|
214
|
+
return ErrorClass;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Removes a class registered with register(), by class or by key.
|
|
219
|
+
* Built in errors cannot be removed.
|
|
220
|
+
* @param {Function | string} classOrName
|
|
221
|
+
* @returns {boolean} true when something was removed
|
|
222
|
+
*/
|
|
223
|
+
function unregister(classOrName) {
|
|
224
|
+
const key = typeof classOrName === 'string' ? classOrName : registryKeys.get(classOrName);
|
|
225
|
+
if (key === undefined || natives.has(key) || !registry.has(key)) return false;
|
|
226
|
+
registryKeys.delete(registry.get(key));
|
|
227
|
+
registry.delete(key);
|
|
228
|
+
return true;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function localRegistry(classes, where) {
|
|
232
|
+
if (classes === undefined || classes === null) return undefined;
|
|
233
|
+
const local = { byKey: new Map(), keys: new Map() };
|
|
234
|
+
for (const entry of classes) {
|
|
235
|
+
const key = checkClass(entry, undefined, where);
|
|
236
|
+
local.byKey.set(key, entry);
|
|
237
|
+
local.keys.set(entry, key);
|
|
238
|
+
}
|
|
239
|
+
return local;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function limit(value, fallback, name, where) {
|
|
243
|
+
if (value === undefined) return fallback;
|
|
244
|
+
if (typeof value !== 'number' || Number.isNaN(value) || value < 0) {
|
|
245
|
+
throw new RangeError(`${where}: options.${name} must be a non negative number`);
|
|
246
|
+
}
|
|
247
|
+
return value;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function setKey(target, key, value) {
|
|
251
|
+
if (key === '__proto__') {
|
|
252
|
+
defineProperty(target, key, { value, writable: true, enumerable: true, configurable: true });
|
|
253
|
+
} else {
|
|
254
|
+
target[key] = value;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function readDescriptor(object, desc) {
|
|
259
|
+
if ('value' in desc) return desc.value;
|
|
260
|
+
try {
|
|
261
|
+
return desc.get === undefined ? undefined : desc.get.call(object);
|
|
262
|
+
} catch {
|
|
263
|
+
return undefined;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function ownKeys(object) {
|
|
268
|
+
try {
|
|
269
|
+
return Object.keys(object);
|
|
270
|
+
} catch {
|
|
271
|
+
return [];
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const sizeGetter = (Ctor) => {
|
|
276
|
+
const desc = Ctor && getOwnDescriptor(Ctor.prototype, 'size');
|
|
277
|
+
return desc && desc.get;
|
|
278
|
+
};
|
|
279
|
+
const mapSize = sizeGetter(globalThis.Map);
|
|
280
|
+
const setSize = sizeGetter(globalThis.Set);
|
|
281
|
+
const dateTime = Date.prototype.getTime;
|
|
282
|
+
const regexpSource = getOwnDescriptor(RegExp.prototype, 'source').get;
|
|
283
|
+
|
|
284
|
+
function brand(getter, value) {
|
|
285
|
+
try {
|
|
286
|
+
getter.call(value);
|
|
287
|
+
return true;
|
|
288
|
+
} catch {
|
|
289
|
+
return false;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
class Encoder {
|
|
294
|
+
constructor(options) {
|
|
295
|
+
const where = 'serialize';
|
|
296
|
+
const o = options == null ? {} : options;
|
|
297
|
+
this.maxDepth = limit(o.maxDepth, 32, 'maxDepth', where);
|
|
298
|
+
this.maxNodes = limit(o.maxNodes, 10000, 'maxNodes', where);
|
|
299
|
+
this.maxLength = limit(o.maxLength, Infinity, 'maxLength', where);
|
|
300
|
+
this.maxItems = limit(o.maxItems, Infinity, 'maxItems', where);
|
|
301
|
+
this.stack = o.stack !== false;
|
|
302
|
+
this.useToJSON = o.toJSON !== false;
|
|
303
|
+
this.local = localRegistry(o.classes, where);
|
|
304
|
+
this.seen = new Map();
|
|
305
|
+
this.ids = 0;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
string(value) {
|
|
309
|
+
if (value.length <= this.maxLength) return value;
|
|
310
|
+
return value.slice(0, this.maxLength) + '...';
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
open(value, depth) {
|
|
314
|
+
if (depth >= this.maxDepth || this.ids >= this.maxNodes) return false;
|
|
315
|
+
this.seen.set(value, this.ids++);
|
|
316
|
+
return true;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
value(value, depth, skipToJSON) {
|
|
320
|
+
switch (typeof value) {
|
|
321
|
+
case 'string':
|
|
322
|
+
return this.string(value);
|
|
323
|
+
case 'number':
|
|
324
|
+
if (value === 0 && 1 / value < 0) return { $value: '-0' };
|
|
325
|
+
return Number.isFinite(value) ? value : { $value: String(value) };
|
|
326
|
+
case 'boolean':
|
|
327
|
+
return value;
|
|
328
|
+
case 'undefined':
|
|
329
|
+
return { $value: 'undefined' };
|
|
330
|
+
case 'bigint':
|
|
331
|
+
return { $value: 'bigint', data: value.toString() };
|
|
332
|
+
case 'symbol':
|
|
333
|
+
return this.string(value.toString());
|
|
334
|
+
case 'function':
|
|
335
|
+
return `[Function: ${functionName(value) || 'anonymous'}]`;
|
|
336
|
+
}
|
|
337
|
+
if (value === null) return null;
|
|
338
|
+
const ref = this.seen.get(value);
|
|
339
|
+
if (ref !== undefined) return { $ref: ref };
|
|
340
|
+
if (isError(value)) return this.error(value, depth);
|
|
341
|
+
const proto = protoOf(value);
|
|
342
|
+
if (proto !== OBJECT_PROTO && proto !== ARRAY_PROTO && proto !== null) {
|
|
343
|
+
if (inheritsError(value) || isDomException(value)) return this.error(value, depth);
|
|
344
|
+
}
|
|
345
|
+
let tag;
|
|
346
|
+
try {
|
|
347
|
+
tag = objectToString.call(value);
|
|
348
|
+
} catch {
|
|
349
|
+
tag = '';
|
|
350
|
+
}
|
|
351
|
+
if (tag === '[object Date]' && brand(dateTime, value)) {
|
|
352
|
+
const time = dateTime.call(value);
|
|
353
|
+
return { $value: 'Date', data: Number.isNaN(time) ? null : new Date(time).toISOString() };
|
|
354
|
+
}
|
|
355
|
+
if (tag === '[object RegExp]' && brand(regexpSource, value)) {
|
|
356
|
+
return { $value: 'RegExp', source: value.source, flags: value.flags };
|
|
357
|
+
}
|
|
358
|
+
if (tag === '[object Map]' && mapSize && brand(mapSize, value)) return this.collection(value, depth, 'Map');
|
|
359
|
+
if (tag === '[object Set]' && setSize && brand(setSize, value)) return this.collection(value, depth, 'Set');
|
|
360
|
+
let array;
|
|
361
|
+
try {
|
|
362
|
+
array = Array.isArray(value);
|
|
363
|
+
} catch {
|
|
364
|
+
array = false;
|
|
365
|
+
}
|
|
366
|
+
if (array) return this.array(value, depth);
|
|
367
|
+
if (this.useToJSON && !skipToJSON) {
|
|
368
|
+
let toJSON;
|
|
369
|
+
try {
|
|
370
|
+
toJSON = value.toJSON;
|
|
371
|
+
} catch {
|
|
372
|
+
toJSON = undefined;
|
|
373
|
+
}
|
|
374
|
+
if (typeof toJSON === 'function') {
|
|
375
|
+
let result;
|
|
376
|
+
try {
|
|
377
|
+
result = toJSON.call(value, '');
|
|
378
|
+
} catch {
|
|
379
|
+
return this.object(value, depth);
|
|
380
|
+
}
|
|
381
|
+
return this.value(result, depth, true);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
return this.object(value, depth);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
array(value, depth) {
|
|
388
|
+
if (!this.open(value, depth)) return '[Array]';
|
|
389
|
+
let length;
|
|
390
|
+
try {
|
|
391
|
+
length = value.length >>> 0;
|
|
392
|
+
} catch {
|
|
393
|
+
length = 0;
|
|
394
|
+
}
|
|
395
|
+
const count = Math.min(length, this.maxItems);
|
|
396
|
+
const out = [];
|
|
397
|
+
for (let i = 0; i < count; i++) {
|
|
398
|
+
let item;
|
|
399
|
+
try {
|
|
400
|
+
item = value[i];
|
|
401
|
+
} catch {
|
|
402
|
+
item = undefined;
|
|
403
|
+
}
|
|
404
|
+
out.push(this.value(item, depth + 1));
|
|
405
|
+
}
|
|
406
|
+
return out;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
object(value, depth) {
|
|
410
|
+
if (!this.open(value, depth)) return '[Object]';
|
|
411
|
+
const out = {};
|
|
412
|
+
const keys = ownKeys(value);
|
|
413
|
+
const count = Math.min(keys.length, this.maxItems);
|
|
414
|
+
let escape = false;
|
|
415
|
+
for (let i = 0; i < count; i++) {
|
|
416
|
+
const key = keys[i];
|
|
417
|
+
let item;
|
|
418
|
+
try {
|
|
419
|
+
item = value[key];
|
|
420
|
+
} catch {
|
|
421
|
+
continue;
|
|
422
|
+
}
|
|
423
|
+
if (!escape && key.charCodeAt(0) === 36 && TAGS.includes(key)) escape = true;
|
|
424
|
+
setKey(out, key, this.value(item, depth + 1));
|
|
425
|
+
}
|
|
426
|
+
return escape ? { $escape: out } : out;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
collection(value, depth, kind) {
|
|
430
|
+
if (!this.open(value, depth)) return `[${kind}]`;
|
|
431
|
+
const data = [];
|
|
432
|
+
let count = 0;
|
|
433
|
+
try {
|
|
434
|
+
for (const entry of value) {
|
|
435
|
+
if (count++ >= this.maxItems) break;
|
|
436
|
+
data.push(
|
|
437
|
+
kind === 'Map'
|
|
438
|
+
? [this.value(entry[0], depth + 1), this.value(entry[1], depth + 1)]
|
|
439
|
+
: this.value(entry, depth + 1),
|
|
440
|
+
);
|
|
441
|
+
}
|
|
442
|
+
} catch {
|
|
443
|
+
// A hostile iterator stops the copy, what was read so far is kept.
|
|
444
|
+
}
|
|
445
|
+
return { $value: kind, data };
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
read(error, key) {
|
|
449
|
+
try {
|
|
450
|
+
return error[key];
|
|
451
|
+
} catch {
|
|
452
|
+
return undefined;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
error(error, depth) {
|
|
457
|
+
const key = classKeyOf(error, this.local);
|
|
458
|
+
const shallow = !this.open(error, depth) && this.seen.set(error, this.ids++);
|
|
459
|
+
const dom = key === 'DOMException' || isDomException(error);
|
|
460
|
+
const out = { $error: key };
|
|
461
|
+
const name = this.read(error, 'name');
|
|
462
|
+
out.name = this.value(name === undefined ? 'Error' : name, depth + 1);
|
|
463
|
+
let desc;
|
|
464
|
+
try {
|
|
465
|
+
desc = getOwnDescriptor(error, 'message');
|
|
466
|
+
} catch {
|
|
467
|
+
desc = undefined;
|
|
468
|
+
}
|
|
469
|
+
if (dom) out.message = this.value(this.read(error, 'message'), depth + 1);
|
|
470
|
+
else if (desc !== undefined && !desc.enumerable) out.message = this.value(readDescriptor(error, desc), depth + 1);
|
|
471
|
+
if (shallow) return out;
|
|
472
|
+
|
|
473
|
+
if (this.stack) {
|
|
474
|
+
let own;
|
|
475
|
+
try {
|
|
476
|
+
own = getOwnDescriptor(error, 'stack');
|
|
477
|
+
} catch {
|
|
478
|
+
own = undefined;
|
|
479
|
+
}
|
|
480
|
+
if (own === undefined || !own.enumerable) {
|
|
481
|
+
const stack = own === undefined ? this.read(error, 'stack') : readDescriptor(error, own);
|
|
482
|
+
if (stack !== undefined) out.stack = this.value(stack, depth + 1);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
if (dom) {
|
|
486
|
+
const code = this.read(error, 'code');
|
|
487
|
+
if (typeof code === 'number') out.code = code;
|
|
488
|
+
}
|
|
489
|
+
for (const special of SPECIAL) {
|
|
490
|
+
let own;
|
|
491
|
+
try {
|
|
492
|
+
own = getOwnDescriptor(error, special);
|
|
493
|
+
} catch {
|
|
494
|
+
own = undefined;
|
|
495
|
+
}
|
|
496
|
+
if (own !== undefined && !own.enumerable) out[special] = this.value(readDescriptor(error, own), depth + 1);
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
let source = error;
|
|
500
|
+
if (this.useToJSON) {
|
|
501
|
+
const toJSON = this.read(error, 'toJSON');
|
|
502
|
+
if (typeof toJSON === 'function') {
|
|
503
|
+
try {
|
|
504
|
+
const result = toJSON.call(error, '');
|
|
505
|
+
if (isObjectLike(result) && result !== error) source = result;
|
|
506
|
+
} catch {
|
|
507
|
+
// Fall back to the own properties.
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
const keys = ownKeys(source);
|
|
512
|
+
const count = Math.min(keys.length, this.maxItems);
|
|
513
|
+
let props;
|
|
514
|
+
for (let i = 0; i < count; i++) {
|
|
515
|
+
const prop = keys[i];
|
|
516
|
+
if (prop === 'stack' && !this.stack) continue;
|
|
517
|
+
let item;
|
|
518
|
+
try {
|
|
519
|
+
item = source[prop];
|
|
520
|
+
} catch {
|
|
521
|
+
continue;
|
|
522
|
+
}
|
|
523
|
+
if (props === undefined) props = {};
|
|
524
|
+
setKey(props, prop, this.value(item, depth + 1));
|
|
525
|
+
}
|
|
526
|
+
if (props !== undefined) out.props = props;
|
|
527
|
+
return out;
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
/**
|
|
532
|
+
* @typedef {object} SerializeOptions
|
|
533
|
+
* @property {number} [maxDepth] nesting limit, deeper containers become
|
|
534
|
+
* "[Object]", "[Array]", "[Map]" or "[Set]" and deeper errors keep only
|
|
535
|
+
* class, name and message (default 32)
|
|
536
|
+
* @property {number} [maxNodes] total number of containers and errors
|
|
537
|
+
* copied before the same truncation applies (default 10000)
|
|
538
|
+
* @property {number} [maxLength] longest string kept, longer strings are cut
|
|
539
|
+
* and end with "..." (default Infinity)
|
|
540
|
+
* @property {number} [maxItems] most array items, object keys and
|
|
541
|
+
* collection entries copied per container (default Infinity)
|
|
542
|
+
* @property {boolean} [stack] include stack traces (default true)
|
|
543
|
+
* @property {boolean} [toJSON] honor toJSON() methods (default true)
|
|
544
|
+
* @property {Function[]} [classes] extra error classes for this call only
|
|
545
|
+
*/
|
|
546
|
+
|
|
547
|
+
/**
|
|
548
|
+
* Converts any value (usually something that was thrown) into a plain value
|
|
549
|
+
* that JSON.stringify, postMessage and structuredClone accept, keeping
|
|
550
|
+
* everything deserialize() needs to rebuild it. Never throws on hostile
|
|
551
|
+
* input; it only throws a RangeError or TypeError for invalid options.
|
|
552
|
+
* @param {unknown} value
|
|
553
|
+
* @param {SerializeOptions} [options]
|
|
554
|
+
* @returns {unknown}
|
|
555
|
+
*/
|
|
556
|
+
function serialize(value, options) {
|
|
557
|
+
const encoder = new Encoder(options);
|
|
558
|
+
try {
|
|
559
|
+
return encoder.value(value, 0);
|
|
560
|
+
/* node:coverage disable */
|
|
561
|
+
} catch {
|
|
562
|
+
// Last resort guard, every read above is already protected.
|
|
563
|
+
return { $error: 'Error', name: 'Error', message: 'faultferry: value could not be serialized' };
|
|
564
|
+
}
|
|
565
|
+
/* node:coverage enable */
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
function isPlainRecord(value) {
|
|
569
|
+
if (!isObjectLike(value) || typeof value === 'function') return false;
|
|
570
|
+
try {
|
|
571
|
+
return !Array.isArray(value);
|
|
572
|
+
} catch {
|
|
573
|
+
return false;
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
function field(record, key) {
|
|
578
|
+
try {
|
|
579
|
+
const desc = getOwnDescriptor(record, key);
|
|
580
|
+
return desc === undefined ? undefined : desc;
|
|
581
|
+
} catch {
|
|
582
|
+
return undefined;
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
function nativeBase(Class) {
|
|
587
|
+
const cached = baseCache.get(Class);
|
|
588
|
+
if (cached !== undefined) return cached;
|
|
589
|
+
let base = Error;
|
|
590
|
+
let current = Class;
|
|
591
|
+
for (let i = 0; typeof current === 'function' && i < 100; i++) {
|
|
592
|
+
const key = registryKeys.get(current);
|
|
593
|
+
if (key !== undefined && natives.get(key) === current) {
|
|
594
|
+
base = current;
|
|
595
|
+
break;
|
|
596
|
+
}
|
|
597
|
+
current = protoOf(current);
|
|
598
|
+
}
|
|
599
|
+
baseCache.set(Class, base);
|
|
600
|
+
return base;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
function define(target, key, value) {
|
|
604
|
+
try {
|
|
605
|
+
defineProperty(target, key, { value, writable: true, enumerable: false, configurable: true });
|
|
606
|
+
/* node:coverage disable */
|
|
607
|
+
} catch {
|
|
608
|
+
// Non configurable property on an exotic error, keep what is there.
|
|
609
|
+
}
|
|
610
|
+
/* node:coverage enable */
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
class Decoder {
|
|
614
|
+
constructor(options) {
|
|
615
|
+
const where = 'deserialize';
|
|
616
|
+
const o = options == null ? {} : options;
|
|
617
|
+
this.maxDepth = limit(o.maxDepth, 256, 'maxDepth', where);
|
|
618
|
+
this.local = localRegistry(o.classes, where);
|
|
619
|
+
this.refs = [];
|
|
620
|
+
this.inputs = new Map();
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
value(data, depth) {
|
|
624
|
+
if (!isObjectLike(data)) return data;
|
|
625
|
+
if (typeof data === 'function') return undefined;
|
|
626
|
+
const known = this.inputs.get(data);
|
|
627
|
+
if (known !== undefined) return known;
|
|
628
|
+
if (depth > this.maxDepth) return undefined;
|
|
629
|
+
let array;
|
|
630
|
+
try {
|
|
631
|
+
array = Array.isArray(data);
|
|
632
|
+
} catch {
|
|
633
|
+
return undefined;
|
|
634
|
+
}
|
|
635
|
+
if (array) {
|
|
636
|
+
const out = [];
|
|
637
|
+
this.refs.push(out);
|
|
638
|
+
this.inputs.set(data, out);
|
|
639
|
+
let length;
|
|
640
|
+
try {
|
|
641
|
+
length = data.length;
|
|
642
|
+
} catch {
|
|
643
|
+
length = 0;
|
|
644
|
+
}
|
|
645
|
+
for (let i = 0; i < length; i++) {
|
|
646
|
+
let item;
|
|
647
|
+
try {
|
|
648
|
+
item = data[i];
|
|
649
|
+
} catch {
|
|
650
|
+
item = undefined;
|
|
651
|
+
}
|
|
652
|
+
out.push(this.value(item, depth + 1));
|
|
653
|
+
}
|
|
654
|
+
return out;
|
|
655
|
+
}
|
|
656
|
+
let tag;
|
|
657
|
+
if ((tag = field(data, '$ref'))) {
|
|
658
|
+
const index = tag.value;
|
|
659
|
+
return Number.isInteger(index) && index >= 0 && index < this.refs.length ? this.refs[index] : undefined;
|
|
660
|
+
}
|
|
661
|
+
if ((tag = field(data, '$error'))) return this.error(data, tag.value, depth);
|
|
662
|
+
if ((tag = field(data, '$value'))) return this.special(data, tag.value, depth);
|
|
663
|
+
if ((tag = field(data, '$escape'))) {
|
|
664
|
+
return isPlainRecord(tag.value) ? this.object(tag.value, depth) : undefined;
|
|
665
|
+
}
|
|
666
|
+
return this.object(data, depth);
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
object(data, depth) {
|
|
670
|
+
const out = {};
|
|
671
|
+
this.refs.push(out);
|
|
672
|
+
this.inputs.set(data, out);
|
|
673
|
+
for (const key of ownKeys(data)) {
|
|
674
|
+
let item;
|
|
675
|
+
try {
|
|
676
|
+
item = data[key];
|
|
677
|
+
} catch {
|
|
678
|
+
continue;
|
|
679
|
+
}
|
|
680
|
+
setKey(out, key, this.value(item, depth + 1));
|
|
681
|
+
}
|
|
682
|
+
return out;
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
special(data, kind, depth) {
|
|
686
|
+
const payload = field(data, 'data');
|
|
687
|
+
const raw = payload && payload.value;
|
|
688
|
+
switch (kind) {
|
|
689
|
+
case 'undefined':
|
|
690
|
+
return undefined;
|
|
691
|
+
case 'NaN':
|
|
692
|
+
return NaN;
|
|
693
|
+
case 'Infinity':
|
|
694
|
+
return Infinity;
|
|
695
|
+
case '-Infinity':
|
|
696
|
+
return -Infinity;
|
|
697
|
+
case '-0':
|
|
698
|
+
return -0;
|
|
699
|
+
case 'bigint':
|
|
700
|
+
try {
|
|
701
|
+
return BigInt(raw);
|
|
702
|
+
} catch {
|
|
703
|
+
return undefined;
|
|
704
|
+
}
|
|
705
|
+
case 'Date':
|
|
706
|
+
return new Date(typeof raw === 'string' ? raw : NaN);
|
|
707
|
+
case 'RegExp': {
|
|
708
|
+
const source = field(data, 'source');
|
|
709
|
+
const flags = field(data, 'flags');
|
|
710
|
+
try {
|
|
711
|
+
return new RegExp(String(source && source.value), flags && typeof flags.value === 'string' ? flags.value : '');
|
|
712
|
+
} catch {
|
|
713
|
+
return undefined;
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
case 'Map':
|
|
717
|
+
case 'Set': {
|
|
718
|
+
const out = kind === 'Map' ? new Map() : new Set();
|
|
719
|
+
this.refs.push(out);
|
|
720
|
+
this.inputs.set(data, out);
|
|
721
|
+
let items;
|
|
722
|
+
try {
|
|
723
|
+
items = Array.isArray(raw) ? raw : [];
|
|
724
|
+
} catch {
|
|
725
|
+
items = [];
|
|
726
|
+
}
|
|
727
|
+
for (const item of items) {
|
|
728
|
+
if (kind === 'Set') {
|
|
729
|
+
out.add(this.value(item, depth + 1));
|
|
730
|
+
} else if (Array.isArray(item)) {
|
|
731
|
+
const k = this.value(item[0], depth + 1);
|
|
732
|
+
out.set(k, this.value(item[1], depth + 1));
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
return out;
|
|
736
|
+
}
|
|
737
|
+
default:
|
|
738
|
+
return undefined;
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
lookup(key) {
|
|
743
|
+
if (typeof key === 'string') {
|
|
744
|
+
const local = this.local && this.local.byKey.get(key);
|
|
745
|
+
if (local !== undefined) return local;
|
|
746
|
+
const known = registry.get(key);
|
|
747
|
+
if (known !== undefined) return known;
|
|
748
|
+
}
|
|
749
|
+
return Error;
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
error(data, key, depth) {
|
|
753
|
+
const Class = this.lookup(key);
|
|
754
|
+
const id = this.refs.length;
|
|
755
|
+
this.refs.push(undefined);
|
|
756
|
+
const nameField = field(data, 'name');
|
|
757
|
+
const name = nameField && this.value(nameField.value, depth + 1);
|
|
758
|
+
const messageField = field(data, 'message');
|
|
759
|
+
const message = messageField && this.value(messageField.value, depth + 1);
|
|
760
|
+
const base = nativeBase(Class);
|
|
761
|
+
const textMessage = typeof message === 'string' ? message : undefined;
|
|
762
|
+
let error;
|
|
763
|
+
// The restored error gets its stack from the data, so skip capturing a
|
|
764
|
+
// fresh one (the most expensive part of constructing an error in V8).
|
|
765
|
+
const limit = Error.stackTraceLimit;
|
|
766
|
+
if (typeof limit === 'number') Error.stackTraceLimit = 0;
|
|
767
|
+
try {
|
|
768
|
+
if (base === natives.get('AggregateError')) error = Reflect.construct(base, [[], textMessage], Class);
|
|
769
|
+
else if (base === natives.get('SuppressedError')) {
|
|
770
|
+
error = Reflect.construct(base, [undefined, undefined, textMessage], Class);
|
|
771
|
+
} else if (base === natives.get('DOMException')) {
|
|
772
|
+
error = Reflect.construct(base, [textMessage, typeof name === 'string' ? name : 'Error'], Class);
|
|
773
|
+
} else error = Reflect.construct(base, [textMessage], Class);
|
|
774
|
+
} catch {
|
|
775
|
+
error = new Error(textMessage);
|
|
776
|
+
} finally {
|
|
777
|
+
if (typeof limit === 'number') Error.stackTraceLimit = limit;
|
|
778
|
+
}
|
|
779
|
+
this.refs[id] = error;
|
|
780
|
+
this.inputs.set(data, error);
|
|
781
|
+
if (messageField && textMessage === undefined) define(error, 'message', message);
|
|
782
|
+
|
|
783
|
+
const propsField = field(data, 'props');
|
|
784
|
+
const props = propsField && isPlainRecord(propsField.value) ? propsField.value : undefined;
|
|
785
|
+
if (nameField && !(props && hasOwn(props, 'name'))) {
|
|
786
|
+
let current;
|
|
787
|
+
try {
|
|
788
|
+
current = error.name;
|
|
789
|
+
} catch {
|
|
790
|
+
current = undefined;
|
|
791
|
+
}
|
|
792
|
+
if (current !== name) define(error, 'name', name);
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
const stackField = field(data, 'stack');
|
|
796
|
+
if (stackField) define(error, 'stack', this.value(stackField.value, depth + 1));
|
|
797
|
+
else {
|
|
798
|
+
try {
|
|
799
|
+
delete error.stack;
|
|
800
|
+
/* node:coverage disable */
|
|
801
|
+
} catch {
|
|
802
|
+
// Ignore, some engines keep a non configurable stack.
|
|
803
|
+
}
|
|
804
|
+
/* node:coverage enable */
|
|
805
|
+
}
|
|
806
|
+
for (const special of SPECIAL) {
|
|
807
|
+
const entry = field(data, special);
|
|
808
|
+
if (entry) define(error, special, this.value(entry.value, depth + 1));
|
|
809
|
+
else if (hasOwn(error, special)) {
|
|
810
|
+
try {
|
|
811
|
+
delete error[special];
|
|
812
|
+
/* node:coverage disable */
|
|
813
|
+
} catch {
|
|
814
|
+
// Constructors only create configurable properties.
|
|
815
|
+
}
|
|
816
|
+
/* node:coverage enable */
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
if (props) {
|
|
820
|
+
for (const prop of ownKeys(props)) {
|
|
821
|
+
let item;
|
|
822
|
+
try {
|
|
823
|
+
item = props[prop];
|
|
824
|
+
} catch {
|
|
825
|
+
continue;
|
|
826
|
+
}
|
|
827
|
+
try {
|
|
828
|
+
defineProperty(error, prop, {
|
|
829
|
+
value: this.value(item, depth + 1),
|
|
830
|
+
writable: true,
|
|
831
|
+
enumerable: true,
|
|
832
|
+
configurable: true,
|
|
833
|
+
});
|
|
834
|
+
} catch {
|
|
835
|
+
// Non configurable property, leave it.
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
return error;
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
/**
|
|
844
|
+
* @typedef {object} DeserializeOptions
|
|
845
|
+
* @property {number} [maxDepth] nesting limit, deeper values become
|
|
846
|
+
* undefined (default 256)
|
|
847
|
+
* @property {Function[]} [classes] extra error classes for this call only
|
|
848
|
+
*/
|
|
849
|
+
|
|
850
|
+
/**
|
|
851
|
+
* Rebuilds a value produced by serialize(): errors come back as real error
|
|
852
|
+
* objects with the right prototype, name, message, stack, cause, errors and
|
|
853
|
+
* own properties. Unknown error classes fall back to Error with the original
|
|
854
|
+
* name. Never throws on hostile input.
|
|
855
|
+
* @param {unknown} data
|
|
856
|
+
* @param {DeserializeOptions} [options]
|
|
857
|
+
* @returns {unknown}
|
|
858
|
+
*/
|
|
859
|
+
function deserialize(data, options) {
|
|
860
|
+
const decoder = new Decoder(options);
|
|
861
|
+
try {
|
|
862
|
+
return decoder.value(data, 0);
|
|
863
|
+
/* node:coverage disable */
|
|
864
|
+
} catch {
|
|
865
|
+
// Last resort guard, every read above is already protected.
|
|
866
|
+
return undefined;
|
|
867
|
+
}
|
|
868
|
+
/* node:coverage enable */
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
exports.isError = isError;
|
|
872
|
+
exports.register = register;
|
|
873
|
+
exports.unregister = unregister;
|
|
874
|
+
exports.serialize = serialize;
|
|
875
|
+
exports.deserialize = deserialize;
|