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.
@@ -0,0 +1,121 @@
1
+ /** Any value JSON can represent. serialize() always returns one. */
2
+ export type JsonValue = string | number | boolean | null | JsonValue[] | { [key: string]: JsonValue };
3
+
4
+ /**
5
+ * The envelope serialize() writes for an error. Fields other than `$error`
6
+ * and `name` are present only when the original error had them.
7
+ */
8
+ export interface SerializedError {
9
+ /** Registry key of the class used to rebuild the error, such as "TypeError". */
10
+ $error: string;
11
+ /** The value of `error.name` (own or inherited), encoded. */
12
+ name: JsonValue;
13
+ /** Own non-enumerable `message` (always present for DOMException). */
14
+ message?: JsonValue;
15
+ /** The stack trace, unless `stack: false` was passed. */
16
+ stack?: JsonValue;
17
+ /** DOMException legacy code, for readability only. */
18
+ code?: number;
19
+ /** Own non-enumerable `cause`. */
20
+ cause?: JsonValue;
21
+ /** Own non-enumerable `errors` (AggregateError). */
22
+ errors?: JsonValue;
23
+ /** Own non-enumerable `error` (SuppressedError). */
24
+ error?: JsonValue;
25
+ /** Own non-enumerable `suppressed` (SuppressedError). */
26
+ suppressed?: JsonValue;
27
+ /** Own enumerable properties, or the result of `toJSON()`. */
28
+ props?: { [key: string]: JsonValue };
29
+ }
30
+
31
+ /** Any class whose instances are errors, such as `class HttpError extends Error {}`. */
32
+ export type ErrorClass = abstract new (...args: any[]) => unknown;
33
+
34
+ export interface SerializeOptions {
35
+ /**
36
+ * Nesting limit. Deeper containers become "[Object]", "[Array]", "[Map]"
37
+ * or "[Set]" and deeper errors keep only class, name and message.
38
+ * @default 32
39
+ */
40
+ maxDepth?: number;
41
+ /**
42
+ * Total number of containers and errors copied before the same
43
+ * truncation applies.
44
+ * @default 10000
45
+ */
46
+ maxNodes?: number;
47
+ /**
48
+ * Longest string kept. Longer strings are cut and end with "...".
49
+ * @default Infinity
50
+ */
51
+ maxLength?: number;
52
+ /**
53
+ * Most array items, object keys and Map or Set entries copied per container.
54
+ * @default Infinity
55
+ */
56
+ maxItems?: number;
57
+ /**
58
+ * Include stack traces. Pass false before sending errors to untrusted clients.
59
+ * @default true
60
+ */
61
+ stack?: boolean;
62
+ /**
63
+ * Honor `toJSON()` methods. On an error, the returned object replaces the
64
+ * own enumerable properties.
65
+ * @default true
66
+ */
67
+ toJSON?: boolean;
68
+ /** Extra error classes recognized for this call only. */
69
+ classes?: readonly ErrorClass[];
70
+ }
71
+
72
+ export interface DeserializeOptions {
73
+ /**
74
+ * Nesting limit. Deeper values become undefined. Keep it at least as high
75
+ * as the serializer's maxDepth.
76
+ * @default 256
77
+ */
78
+ maxDepth?: number;
79
+ /** Extra error classes recognized for this call only. */
80
+ classes?: readonly ErrorClass[];
81
+ }
82
+
83
+ /**
84
+ * Converts any value, usually something that was thrown, into a plain value
85
+ * that JSON.stringify, postMessage and structuredClone accept. Never throws
86
+ * on hostile input.
87
+ * @throws {RangeError} when a numeric option is negative or not a number
88
+ * @throws {TypeError} when `classes` contains something that is not a named class
89
+ */
90
+ export declare function serialize(value: unknown, options?: SerializeOptions): JsonValue;
91
+
92
+ /**
93
+ * Rebuilds a value produced by serialize(). Errors come back as real error
94
+ * objects with the right prototype. Unknown classes fall back to Error with
95
+ * the original name. Never throws on hostile input.
96
+ * @throws {RangeError} when `maxDepth` is negative or not a number
97
+ * @throws {TypeError} when `classes` contains something that is not a named class
98
+ */
99
+ export declare function deserialize<T = unknown>(data: unknown, options?: DeserializeOptions): T;
100
+
101
+ /**
102
+ * Registers a custom error class under `name` (default: the class name) so
103
+ * deserialize() can rebuild its instances. Constructors are never called.
104
+ * Returns the class.
105
+ * @throws {TypeError} when the class is not a function, has no name, or the
106
+ * name belongs to a built in error
107
+ */
108
+ export declare function register<T extends ErrorClass>(ErrorClass: T, name?: string): T;
109
+
110
+ /**
111
+ * Removes a class registered with register(), by class or by key.
112
+ * Returns true when something was removed. Built in errors stay.
113
+ */
114
+ export declare function unregister(classOrName: ErrorClass | string): boolean;
115
+
116
+ /**
117
+ * True for real error objects (with the internal [[ErrorData]] slot),
118
+ * including errors from other realms such as node:vm contexts and iframes.
119
+ * Uses the native Error.isError when present.
120
+ */
121
+ export declare function isError(value: unknown): value is Error;
package/index.d.ts ADDED
@@ -0,0 +1,121 @@
1
+ /** Any value JSON can represent. serialize() always returns one. */
2
+ export type JsonValue = string | number | boolean | null | JsonValue[] | { [key: string]: JsonValue };
3
+
4
+ /**
5
+ * The envelope serialize() writes for an error. Fields other than `$error`
6
+ * and `name` are present only when the original error had them.
7
+ */
8
+ export interface SerializedError {
9
+ /** Registry key of the class used to rebuild the error, such as "TypeError". */
10
+ $error: string;
11
+ /** The value of `error.name` (own or inherited), encoded. */
12
+ name: JsonValue;
13
+ /** Own non-enumerable `message` (always present for DOMException). */
14
+ message?: JsonValue;
15
+ /** The stack trace, unless `stack: false` was passed. */
16
+ stack?: JsonValue;
17
+ /** DOMException legacy code, for readability only. */
18
+ code?: number;
19
+ /** Own non-enumerable `cause`. */
20
+ cause?: JsonValue;
21
+ /** Own non-enumerable `errors` (AggregateError). */
22
+ errors?: JsonValue;
23
+ /** Own non-enumerable `error` (SuppressedError). */
24
+ error?: JsonValue;
25
+ /** Own non-enumerable `suppressed` (SuppressedError). */
26
+ suppressed?: JsonValue;
27
+ /** Own enumerable properties, or the result of `toJSON()`. */
28
+ props?: { [key: string]: JsonValue };
29
+ }
30
+
31
+ /** Any class whose instances are errors, such as `class HttpError extends Error {}`. */
32
+ export type ErrorClass = abstract new (...args: any[]) => unknown;
33
+
34
+ export interface SerializeOptions {
35
+ /**
36
+ * Nesting limit. Deeper containers become "[Object]", "[Array]", "[Map]"
37
+ * or "[Set]" and deeper errors keep only class, name and message.
38
+ * @default 32
39
+ */
40
+ maxDepth?: number;
41
+ /**
42
+ * Total number of containers and errors copied before the same
43
+ * truncation applies.
44
+ * @default 10000
45
+ */
46
+ maxNodes?: number;
47
+ /**
48
+ * Longest string kept. Longer strings are cut and end with "...".
49
+ * @default Infinity
50
+ */
51
+ maxLength?: number;
52
+ /**
53
+ * Most array items, object keys and Map or Set entries copied per container.
54
+ * @default Infinity
55
+ */
56
+ maxItems?: number;
57
+ /**
58
+ * Include stack traces. Pass false before sending errors to untrusted clients.
59
+ * @default true
60
+ */
61
+ stack?: boolean;
62
+ /**
63
+ * Honor `toJSON()` methods. On an error, the returned object replaces the
64
+ * own enumerable properties.
65
+ * @default true
66
+ */
67
+ toJSON?: boolean;
68
+ /** Extra error classes recognized for this call only. */
69
+ classes?: readonly ErrorClass[];
70
+ }
71
+
72
+ export interface DeserializeOptions {
73
+ /**
74
+ * Nesting limit. Deeper values become undefined. Keep it at least as high
75
+ * as the serializer's maxDepth.
76
+ * @default 256
77
+ */
78
+ maxDepth?: number;
79
+ /** Extra error classes recognized for this call only. */
80
+ classes?: readonly ErrorClass[];
81
+ }
82
+
83
+ /**
84
+ * Converts any value, usually something that was thrown, into a plain value
85
+ * that JSON.stringify, postMessage and structuredClone accept. Never throws
86
+ * on hostile input.
87
+ * @throws {RangeError} when a numeric option is negative or not a number
88
+ * @throws {TypeError} when `classes` contains something that is not a named class
89
+ */
90
+ export declare function serialize(value: unknown, options?: SerializeOptions): JsonValue;
91
+
92
+ /**
93
+ * Rebuilds a value produced by serialize(). Errors come back as real error
94
+ * objects with the right prototype. Unknown classes fall back to Error with
95
+ * the original name. Never throws on hostile input.
96
+ * @throws {RangeError} when `maxDepth` is negative or not a number
97
+ * @throws {TypeError} when `classes` contains something that is not a named class
98
+ */
99
+ export declare function deserialize<T = unknown>(data: unknown, options?: DeserializeOptions): T;
100
+
101
+ /**
102
+ * Registers a custom error class under `name` (default: the class name) so
103
+ * deserialize() can rebuild its instances. Constructors are never called.
104
+ * Returns the class.
105
+ * @throws {TypeError} when the class is not a function, has no name, or the
106
+ * name belongs to a built in error
107
+ */
108
+ export declare function register<T extends ErrorClass>(ErrorClass: T, name?: string): T;
109
+
110
+ /**
111
+ * Removes a class registered with register(), by class or by key.
112
+ * Returns true when something was removed. Built in errors stay.
113
+ */
114
+ export declare function unregister(classOrName: ErrorClass | string): boolean;
115
+
116
+ /**
117
+ * True for real error objects (with the internal [[ErrorData]] slot),
118
+ * including errors from other realms such as node:vm contexts and iframes.
119
+ * Uses the native Error.isError when present.
120
+ */
121
+ export declare function isError(value: unknown): value is Error;
package/package.json CHANGED
@@ -1,6 +1,67 @@
1
1
  {
2
2
  "name": "faultferry",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Serialize JavaScript errors to plain JSON and back, exactly: cause chains with cycles, AggregateError, SuppressedError, DOMException, custom classes and thrown non errors.",
5
+ "keywords": [
6
+ "error",
7
+ "error-serialization",
8
+ "serialize",
9
+ "deserialize",
10
+ "json",
11
+ "cause",
12
+ "aggregateerror",
13
+ "domexception",
14
+ "is-error",
15
+ "worker",
16
+ "ipc",
17
+ "structured-clone"
18
+ ],
19
+ "homepage": "https://kwy404.github.io/faultferry/",
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/kwy404/faultferry.git"
23
+ },
24
+ "bugs": {
25
+ "url": "https://github.com/kwy404/faultferry/issues"
26
+ },
27
+ "license": "MIT",
28
+ "author": "Alexandre Kohler (https://github.com/kwy404)",
29
+ "type": "module",
30
+ "sideEffects": false,
31
+ "exports": {
32
+ ".": {
33
+ "import": {
34
+ "types": "./index.d.ts",
35
+ "default": "./src/index.js"
36
+ },
37
+ "require": {
38
+ "types": "./dist/index.d.cts",
39
+ "default": "./dist/index.cjs"
40
+ }
41
+ },
42
+ "./package.json": "./package.json"
43
+ },
44
+ "main": "./dist/index.cjs",
45
+ "module": "./src/index.js",
46
+ "types": "./index.d.ts",
47
+ "files": [
48
+ "dist",
49
+ "src",
50
+ "index.d.ts",
51
+ "README.md",
52
+ "LICENSE"
53
+ ],
54
+ "engines": {
55
+ "node": ">=18"
56
+ },
57
+ "scripts": {
58
+ "build": "node scripts/build.mjs",
59
+ "test": "node scripts/build.mjs && node --test",
60
+ "coverage": "node scripts/build.mjs && node --test --experimental-test-coverage --test-coverage-include=src/**",
61
+ "bench": "node bench/index.mjs",
62
+ "size": "node scripts/size.mjs",
63
+ "site": "node scripts/build-site.mjs && node scripts/check-site.mjs",
64
+ "serve": "node scripts/serve.mjs",
65
+ "prepublishOnly": "npm run build && npm test"
66
+ }
67
+ }