@ix-xs/node-comfort 1.1.31 → 2.0.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/CHANGELOG.md +190 -0
- package/LICENSE +21 -0
- package/README.md +210 -748
- package/index.js +84 -13
- package/package.json +201 -21
- package/src/Arr.js +901 -0
- package/src/Async.js +585 -0
- package/src/Cache.js +329 -0
- package/src/Checker.js +979 -188
- package/src/Cli.js +1034 -0
- package/src/Color.js +415 -0
- package/src/Crypto.js +707 -0
- package/src/Emitter.js +289 -0
- package/src/Env.js +555 -0
- package/src/FS.js +1483 -768
- package/src/Func.js +692 -0
- package/src/Http.js +538 -0
- package/src/Id.js +418 -0
- package/src/Logger.js +739 -307
- package/src/Num.js +849 -0
- package/src/Obj.js +904 -0
- package/src/SQLite.js +1308 -994
- package/src/Schema.js +1586 -0
- package/src/Str.js +999 -0
- package/src/Sys.js +511 -0
- package/src/Time.js +1323 -0
- package/src/Utils.js +321 -325
- package/src/errors.js +292 -0
- package/types/index.d.ts +693 -120
- package/types/src/Arr.d.ts +562 -0
- package/types/src/Arr.d.ts.map +1 -0
- package/types/src/Async.d.ts +321 -0
- package/types/src/Async.d.ts.map +1 -0
- package/types/src/Cache.d.ts +195 -0
- package/types/src/Cache.d.ts.map +1 -0
- package/types/src/Checker.d.ts +609 -81
- package/types/src/Checker.d.ts.map +1 -1
- package/types/src/Cli.d.ts +582 -0
- package/types/src/Cli.d.ts.map +1 -0
- package/types/src/Color.d.ts +294 -0
- package/types/src/Color.d.ts.map +1 -0
- package/types/src/Crypto.d.ts +534 -0
- package/types/src/Crypto.d.ts.map +1 -0
- package/types/src/Emitter.d.ts +170 -0
- package/types/src/Emitter.d.ts.map +1 -0
- package/types/src/Env.d.ts +382 -0
- package/types/src/Env.d.ts.map +1 -0
- package/types/src/FS.d.ts +709 -201
- package/types/src/FS.d.ts.map +1 -1
- package/types/src/Func.d.ts +455 -0
- package/types/src/Func.d.ts.map +1 -0
- package/types/src/Http.d.ts +374 -0
- package/types/src/Http.d.ts.map +1 -0
- package/types/src/Id.d.ts +200 -0
- package/types/src/Id.d.ts.map +1 -0
- package/types/src/Logger.d.ts +374 -24
- package/types/src/Logger.d.ts.map +1 -1
- package/types/src/Num.d.ts +578 -0
- package/types/src/Num.d.ts.map +1 -0
- package/types/src/Obj.d.ts +475 -0
- package/types/src/Obj.d.ts.map +1 -0
- package/types/src/SQLite.d.ts +647 -305
- package/types/src/SQLite.d.ts.map +1 -1
- package/types/src/Schema.d.ts +940 -0
- package/types/src/Schema.d.ts.map +1 -0
- package/types/src/Str.d.ts +696 -0
- package/types/src/Str.d.ts.map +1 -0
- package/types/src/Sys.d.ts +269 -0
- package/types/src/Sys.d.ts.map +1 -0
- package/types/src/Time.d.ts +664 -0
- package/types/src/Time.d.ts.map +1 -0
- package/types/src/Utils.d.ts +154 -102
- package/types/src/Utils.d.ts.map +1 -1
- package/types/src/errors.d.ts +292 -0
- package/types/src/errors.d.ts.map +1 -0
- package/tsconfig.json +0 -18
- package/types/index.d.ts.map +0 -1
package/types/src/Utils.d.ts
CHANGED
|
@@ -1,124 +1,176 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* @example
|
|
5
|
-
* await nodeComfort.wait(500); // waits 500 ms
|
|
6
|
-
*
|
|
7
|
-
* @param {number} ms - Delay in milliseconds.
|
|
8
|
-
* @returns {Promise<void>} A promise that resolves after the delay.
|
|
2
|
+
* Options for `wait()`.
|
|
9
3
|
*/
|
|
10
|
-
export
|
|
4
|
+
export type WaitOptions = {
|
|
5
|
+
/**
|
|
6
|
+
* Cancels the wait; the promise rejects with an `AbortError`.
|
|
7
|
+
*/
|
|
8
|
+
signal?: AbortSignal | undefined;
|
|
9
|
+
/**
|
|
10
|
+
* Let the process exit even if the timer is still pending.
|
|
11
|
+
*/
|
|
12
|
+
unref?: boolean | undefined;
|
|
13
|
+
};
|
|
11
14
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* The predicate is evaluated every `options.interval` milliseconds (default: 50 ms).
|
|
15
|
-
* Be careful with the default interval: 50 ms is very frequent polling and can be
|
|
16
|
-
* unnecessarily CPU‑intensive if your predicate or trigger handlers are heavy.
|
|
17
|
-
* When `options.max` is `null`, the predicate can match indefinitely (infinite triggers),
|
|
18
|
-
* so you should choose an interval that makes sense for your use‑case.
|
|
19
|
-
*
|
|
20
|
-
* A `when(...)` task is one‑shot: once it has been stopped (manually or via `max`/`timeout`),
|
|
21
|
-
* calling `.start()` again on the same task will not restart it.
|
|
22
|
-
*
|
|
23
|
-
* @param {boolean|Promise<boolean>|(() => boolean|Promise<boolean>)} predicate
|
|
24
|
-
* Condition to evaluate at each tick, or a function returning it.
|
|
25
|
-
* @param {*} [payload={}]
|
|
26
|
-
* Value passed to listeners when events are emitted (e.g. an object or message).
|
|
27
|
-
* @param {object} [options]
|
|
28
|
-
* @param {number} [options.interval=50]
|
|
29
|
-
* Polling interval in milliseconds. Increase this value if your predicate is
|
|
30
|
-
* expensive or you expect many triggers without a `max` limit.
|
|
31
|
-
* @param {number|null} [options.timeout=null]
|
|
32
|
-
* Maximum time in milliseconds before emitting a "timeout" event and stopping.
|
|
33
|
-
* If `null`, no timeout is applied.
|
|
34
|
-
* @param {number|null} [options.max=null]
|
|
35
|
-
* Maximum number of successful predicate evaluations ("trigger" events) before
|
|
36
|
-
* automatically stopping. If `null`, triggers are infinite until `stop()` is called.
|
|
37
|
-
*
|
|
38
|
-
* @example
|
|
39
|
-
* // Basic usage: infinite triggers every 50 ms (default interval) – use with caution.
|
|
40
|
-
* const task = nodeComfort.when(() => true, { message: "Ok" });
|
|
41
|
-
*
|
|
42
|
-
* task
|
|
43
|
-
* .on("trigger", (payload) => {
|
|
44
|
-
* console.log(payload.message);
|
|
45
|
-
* // task.stop(); // stop when you’re done
|
|
46
|
-
* })
|
|
47
|
-
* .start();
|
|
48
|
-
*
|
|
49
|
-
* @example
|
|
50
|
-
* // Safer usage: limit triggers and/or lower polling frequency.
|
|
51
|
-
* nodeComfort.when(() => true, { message: "Ok" }, { max: 1, interval: 500 })
|
|
52
|
-
* .on("trigger", (payload) => {
|
|
53
|
-
* console.log(payload.message);
|
|
54
|
-
* })
|
|
55
|
-
* .on("timeout", (payload) => {
|
|
56
|
-
* console.log("Timed out", payload);
|
|
57
|
-
* })
|
|
58
|
-
* .start();
|
|
59
|
-
*
|
|
60
|
-
* @returns {{
|
|
61
|
-
* start: () => any,
|
|
62
|
-
* stop: () => any,
|
|
63
|
-
* on: <E extends "error"|"trigger"|"timeout">(event: E, handler: E extends "error"
|
|
64
|
-
* ? (error: unknown) => void|Promise<void>
|
|
65
|
-
* : (payload: any) => void|Promise<void>) => any
|
|
66
|
-
* }} A small task controller with `start`, `stop` and `on`.
|
|
15
|
+
* Options for `when()`.
|
|
67
16
|
*/
|
|
68
|
-
export
|
|
17
|
+
export type WhenOptions = {
|
|
18
|
+
/**
|
|
19
|
+
* Milliseconds between two checks. Defaults to `50`.
|
|
20
|
+
*/
|
|
69
21
|
interval?: number | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* Give up after this many milliseconds and emit `"timeout"`.
|
|
24
|
+
*/
|
|
70
25
|
timeout?: number | null | undefined;
|
|
26
|
+
/**
|
|
27
|
+
* Stop after this many triggers.
|
|
28
|
+
*/
|
|
71
29
|
max?: number | null | undefined;
|
|
72
|
-
}): {
|
|
73
|
-
start: () => any;
|
|
74
|
-
stop: () => any;
|
|
75
|
-
on: <E extends "error" | "trigger" | "timeout">(event: E, handler: E extends "error" ? (error: unknown) => void | Promise<void> : (payload: any) => void | Promise<void>) => any;
|
|
76
30
|
};
|
|
77
31
|
/**
|
|
78
|
-
*
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
32
|
+
* The task returned by `when()`. Every method returns the task, so calls chain.
|
|
33
|
+
*/
|
|
34
|
+
export type WhenTask = {
|
|
35
|
+
/**
|
|
36
|
+
* Starts checking. A stopped task can't be restarted.
|
|
37
|
+
*/
|
|
38
|
+
start: () => WhenTask;
|
|
39
|
+
/**
|
|
40
|
+
* Stops checking and clears the timers.
|
|
41
|
+
*/
|
|
42
|
+
stop: () => WhenTask;
|
|
43
|
+
/**
|
|
44
|
+
* Listens to `"trigger"` (the condition is true), `"error"` (it threw) or `"timeout"`.
|
|
45
|
+
*/
|
|
46
|
+
on: <E extends "trigger" | "error" | "timeout">(event: E, handler: E extends "error" ? (error: unknown) => void : (payload: any) => void) => WhenTask;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Events you can customize on `dontCrash()`.
|
|
50
|
+
*/
|
|
51
|
+
export type DontCrashEvent = "error" | "exit" | "sig" | "beforeExit";
|
|
52
|
+
/**
|
|
53
|
+
* Returned by `dontCrash()`.
|
|
54
|
+
*/
|
|
55
|
+
export type DontCrashController = {
|
|
56
|
+
/**
|
|
57
|
+
* Replaces the handler for an event. Call it without a handler to restore the default one.
|
|
58
|
+
*/
|
|
59
|
+
on: <E extends DontCrashEvent>(event: E, handler?: E extends "error" ? (error: unknown) => void : E extends "sig" ? (signal: NodeJS.Signals) => void : (code: number) => void) => DontCrashController;
|
|
60
|
+
/**
|
|
61
|
+
* Removes everything `dontCrash()` installed.
|
|
62
|
+
*/
|
|
63
|
+
dispose: () => void;
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* Options for `wait()`.
|
|
67
|
+
* @typedef {object} WaitOptions
|
|
68
|
+
* @property {AbortSignal} [signal] Cancels the wait; the promise rejects with an `AbortError`.
|
|
69
|
+
* @property {boolean} [unref] Let the process exit even if the timer is still pending.
|
|
70
|
+
*/
|
|
71
|
+
/**
|
|
72
|
+
* Options for `when()`.
|
|
73
|
+
* @typedef {object} WhenOptions
|
|
74
|
+
* @property {number} [interval] Milliseconds between two checks. Defaults to `50`.
|
|
75
|
+
* @property {number | null} [timeout] Give up after this many milliseconds and emit `"timeout"`.
|
|
76
|
+
* @property {number | null} [max] Stop after this many triggers.
|
|
77
|
+
*/
|
|
78
|
+
/**
|
|
79
|
+
* The task returned by `when()`. Every method returns the task, so calls chain.
|
|
80
|
+
* @typedef {object} WhenTask
|
|
81
|
+
* @property {() => WhenTask} start Starts checking. A stopped task can't be restarted.
|
|
82
|
+
* @property {() => WhenTask} stop Stops checking and clears the timers.
|
|
83
|
+
* @property {<E extends "trigger" | "error" | "timeout">(event: E, handler: E extends "error" ? (error: unknown) => void : (payload: any) => void) => WhenTask} on Listens to `"trigger"` (the condition is true), `"error"` (it threw) or `"timeout"`.
|
|
84
|
+
*/
|
|
85
|
+
/**
|
|
86
|
+
* Events you can customize on `dontCrash()`.
|
|
87
|
+
* @typedef {"error" | "exit" | "sig" | "beforeExit"} DontCrashEvent
|
|
88
|
+
*/
|
|
89
|
+
/**
|
|
90
|
+
* Returned by `dontCrash()`.
|
|
91
|
+
* @typedef {object} DontCrashController
|
|
92
|
+
* @property {<E extends DontCrashEvent>(event: E, handler?: E extends "error" ? (error: unknown) => void : E extends "sig" ? (signal: NodeJS.Signals) => void : (code: number) => void) => DontCrashController} on Replaces the handler for an event. Call it without a handler to restore the default one.
|
|
93
|
+
* @property {() => void} dispose Removes everything `dontCrash()` installed.
|
|
94
|
+
*/
|
|
95
|
+
/**
|
|
96
|
+
* Resolves after a delay, given in milliseconds or as a duration string.
|
|
86
97
|
*
|
|
87
98
|
* @example
|
|
88
|
-
*
|
|
89
|
-
*
|
|
99
|
+
* await nc.wait(500);
|
|
100
|
+
* await nc.wait("2s");
|
|
101
|
+
* await nc.wait("1m", { signal: controller.signal });
|
|
102
|
+
*
|
|
103
|
+
* @param {number | string} duration Milliseconds, or a string like `"250ms"`, `"2s"`, `"1m30s"`.
|
|
104
|
+
* @param {WaitOptions} [options]
|
|
105
|
+
* @returns {Promise<void>}
|
|
106
|
+
* @throws {AbortError} If the signal is aborted.
|
|
107
|
+
* @throws {TypeError} If the duration can't be parsed.
|
|
108
|
+
*/
|
|
109
|
+
export function wait(duration: number | string, options?: WaitOptions): Promise<void>;
|
|
110
|
+
/**
|
|
111
|
+
* Checks a condition at a regular interval and tells you when it's true.
|
|
112
|
+
* Call `.start()` once your listeners are in place. If you just want to
|
|
113
|
+
* await a condition, `nc.async.poll()` is simpler.
|
|
90
114
|
*
|
|
91
115
|
* @example
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
* });
|
|
116
|
+
* nc.when(() => queue.length > 0, { reason: "jobs waiting" }, { interval: 500, max: 1, timeout: 10_000 })
|
|
117
|
+
* .on("trigger", (payload) => nc.info(payload.reason))
|
|
118
|
+
* .on("timeout", () => nc.warn("Nothing happened in 10s"))
|
|
119
|
+
* .start();
|
|
97
120
|
*
|
|
98
|
-
* @returns
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
* E extends "sig" ? (signal: "SIGINT"|"SIGTERM"|"SIGQUIT") => void|Promise<void> :
|
|
103
|
-
* (code: number) => void|Promise<void>
|
|
104
|
-
* )) => any
|
|
105
|
-
* }} A small controller to configure global handlers.
|
|
121
|
+
* @param {boolean | PromiseLike<boolean> | (() => unknown)} predicate The condition, or a function (sync or async) that returns it.
|
|
122
|
+
* @param {any} [payload={}] Passed to the `"trigger"` and `"timeout"` listeners.
|
|
123
|
+
* @param {WhenOptions} [options]
|
|
124
|
+
* @returns {WhenTask}
|
|
106
125
|
*/
|
|
107
|
-
export function
|
|
108
|
-
on: <E extends "error" | "exit" | "sig" | "beforeExit">(event: E, handler?: (E extends "error" ? (error: Error | unknown) => void | Promise<void> : E extends "exit" ? (code: number) => void | Promise<void> : E extends "sig" ? (signal: "SIGINT" | "SIGTERM" | "SIGQUIT") => void | Promise<void> : (code: number) => void | Promise<void>)) => any;
|
|
109
|
-
};
|
|
126
|
+
export function when(predicate: boolean | PromiseLike<boolean> | (() => unknown), payload?: any, options?: WhenOptions): WhenTask;
|
|
110
127
|
/**
|
|
111
|
-
*
|
|
128
|
+
* Keeps the process alive when something goes wrong. Uncaught exceptions and
|
|
129
|
+
* unhandled rejections are logged instead of crashing, and `SIGINT`/`SIGTERM`
|
|
130
|
+
* are logged before a clean exit. Customize any of it with `.on()`.
|
|
112
131
|
*
|
|
113
|
-
*
|
|
114
|
-
*
|
|
132
|
+
* Only its own handlers are ever removed; listeners added by your code or by
|
|
133
|
+
* other libraries are left alone.
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* nc.dontCrash()
|
|
137
|
+
* .on("error", (error) => sentry.captureException(error))
|
|
138
|
+
* .on("sig", async () => {
|
|
139
|
+
* await server.close();
|
|
140
|
+
* process.exit(0);
|
|
141
|
+
* });
|
|
142
|
+
*
|
|
143
|
+
* @returns {DontCrashController}
|
|
115
144
|
*/
|
|
116
|
-
export function
|
|
145
|
+
export function dontCrash(): DontCrashController;
|
|
117
146
|
/**
|
|
118
|
-
*
|
|
147
|
+
* `JSON.stringify` that never throws. Circular references become
|
|
148
|
+
* `"[Circular]"`, bigints become strings, Maps become objects and Sets become
|
|
149
|
+
* arrays.
|
|
119
150
|
*
|
|
120
|
-
* @
|
|
121
|
-
*
|
|
151
|
+
* @example
|
|
152
|
+
* nc.JSONString({ a: 1 }); // '{\n "a": 1\n}'
|
|
153
|
+
* nc.JSONString({ id: 10n, tags: new Set(["x"]) }, 0); // '{"id":"10","tags":["x"]}'
|
|
154
|
+
*
|
|
155
|
+
* @param {unknown} value
|
|
156
|
+
* @param {number} [spaces=4] Indentation. Use `0` for a single line.
|
|
157
|
+
* @returns {string} The JSON text, or `"undefined"` for values JSON can't represent.
|
|
158
|
+
*/
|
|
159
|
+
export function JSONString(value: unknown, spaces?: number): string;
|
|
160
|
+
/**
|
|
161
|
+
* `JSON.parse` with an optional fallback. With a fallback, invalid input
|
|
162
|
+
* returns it instead of throwing.
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* nc.JSONParse('{"a":1}'); // { a: 1 }
|
|
166
|
+
* nc.JSONParse("oops", null); // null
|
|
167
|
+
* nc.JSONParse("oops"); // throws SyntaxError
|
|
168
|
+
*
|
|
169
|
+
* @template [T=any]
|
|
170
|
+
* @param {string} text
|
|
171
|
+
* @param {T} [fallback] Returned when `text` isn't valid JSON.
|
|
172
|
+
* @returns {T}
|
|
173
|
+
* @throws {SyntaxError} If `text` is invalid and there's no fallback.
|
|
122
174
|
*/
|
|
123
|
-
export function JSONParse(
|
|
175
|
+
export function JSONParse<T = any>(text: string, fallback?: T, ...args: any[]): T;
|
|
124
176
|
//# sourceMappingURL=Utils.d.ts.map
|
package/types/src/Utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Utils.d.ts","sourceRoot":"","sources":["../../src/Utils.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Utils.d.ts","sourceRoot":"","sources":["../../src/Utils.js"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;WAgCc,MAAM,QAAQ;;;;UACd,MAAM,QAAQ;;;;QACd,CAAC,CAAC,SAAS,SAAS,GAAG,OAAO,GAAG,SAAS,EAAE,KAAK,EAAE,CAAC,EAAE,OAAO,EAAE,CAAC,SAAS,OAAO,GAAG,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,GAAG,CAAC,OAAO,EAAE,GAAG,KAAK,IAAI,KAAK,QAAQ;;;;;6BAKlJ,OAAO,GAAG,MAAM,GAAG,KAAK,GAAG,YAAY;;;;;;;;QAMtC,CAAC,CAAC,SAAS,cAAc,EAAE,KAAK,EAAE,CAAC,EAAE,OAAO,CAAC,EAAE,CAAC,SAAS,OAAO,GAAG,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,GAAG,CAAC,SAAS,KAAK,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,OAAO,KAAK,IAAI,GAAG,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,KAAK,mBAAmB;;;;aACjM,MAAM,IAAI;;AAhCxB;;;;;GAKG;AAEH;;;;;;GAMG;AAEH;;;;;;GAMG;AAEH;;;GAGG;AAEH;;;;;GAKG;AAEH;;;;;;;;;;;;;GAaG;AACH,+BANW,MAAM,GAAG,MAAM,YACf,WAAW,GACT,OAAO,CAAC,IAAI,CAAC,CAqBzB;AAED;;;;;;;;;;;;;;;GAeG;AACH,gCALW,OAAO,GAAG,WAAW,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,OAAO,CAAC,YAChD,GAAG,YACH,WAAW,GACT,QAAQ,CAuEpB;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,6BAFa,mBAAmB,CAoE/B;AAKD;;;;;;;;;;;;GAYG;AACH,kCAJW,OAAO,WACP,MAAM,GACJ,MAAM,CAmBlB;AAED;;;;;;;;;;;;;;GAcG;AACH,0BANc,CAAC,cACJ,MAAM,aACN,CAAC,mBACC,CAAC,CAUb"}
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options shared by every error constructor.
|
|
3
|
+
*/
|
|
4
|
+
export type NodeComfortErrorOptions = {
|
|
5
|
+
/**
|
|
6
|
+
* Machine-readable code, like `"ETIMEDOUT"`.
|
|
7
|
+
*/
|
|
8
|
+
code?: string | undefined;
|
|
9
|
+
/**
|
|
10
|
+
* The error that caused this one, kept as `error.cause`.
|
|
11
|
+
*/
|
|
12
|
+
cause?: unknown;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* One problem found while validating data.
|
|
16
|
+
*/
|
|
17
|
+
export type ValidationIssue = {
|
|
18
|
+
/**
|
|
19
|
+
* Where the problem is, like `["user", "emails", 0]`. Empty for the root value.
|
|
20
|
+
*/
|
|
21
|
+
path: Array<string | number>;
|
|
22
|
+
/**
|
|
23
|
+
* What went wrong: `"invalid_type"`, `"too_small"`, `"invalid_string"`, `"custom"`...
|
|
24
|
+
*/
|
|
25
|
+
code: string;
|
|
26
|
+
/**
|
|
27
|
+
* A message you can show to users.
|
|
28
|
+
*/
|
|
29
|
+
message: string;
|
|
30
|
+
/**
|
|
31
|
+
* The expected type, for type errors.
|
|
32
|
+
*/
|
|
33
|
+
expected?: string | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* The received type, for type errors.
|
|
36
|
+
*/
|
|
37
|
+
received?: string | undefined;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* The errors node-comfort throws. They all extend `NodeComfortError` (itself
|
|
41
|
+
* a regular `Error`) and carry a stable `code`, so you can branch on the
|
|
42
|
+
* kind of failure without parsing messages.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* try {
|
|
46
|
+
* await nc.http.get("https://api.example.com/users/42");
|
|
47
|
+
* } catch (error) {
|
|
48
|
+
* if (error instanceof nc.errors.HttpError && error.status === 404) return null;
|
|
49
|
+
* throw error;
|
|
50
|
+
* }
|
|
51
|
+
*/
|
|
52
|
+
/**
|
|
53
|
+
* Options shared by every error constructor.
|
|
54
|
+
* @typedef {object} NodeComfortErrorOptions
|
|
55
|
+
* @property {string} [code] Machine-readable code, like `"ETIMEDOUT"`.
|
|
56
|
+
* @property {unknown} [cause] The error that caused this one, kept as `error.cause`.
|
|
57
|
+
*/
|
|
58
|
+
/**
|
|
59
|
+
* Base class of every node-comfort error. `name` is the class name and
|
|
60
|
+
* `code` a stable identifier.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* if (error instanceof nc.errors.NodeComfortError) logger.warn(error.code);
|
|
64
|
+
*/
|
|
65
|
+
export class NodeComfortError extends Error {
|
|
66
|
+
/**
|
|
67
|
+
* @param {string} message
|
|
68
|
+
* @param {NodeComfortErrorOptions} [options]
|
|
69
|
+
*/
|
|
70
|
+
constructor(message: string, options?: NodeComfortErrorOptions);
|
|
71
|
+
/** Stable, machine-readable code. */
|
|
72
|
+
code: string;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Thrown by `nc.assert()` and `nc.assertType()`.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* nc.assert(user, "User is required");
|
|
79
|
+
*/
|
|
80
|
+
export class AssertionError extends NodeComfortError {
|
|
81
|
+
/**
|
|
82
|
+
* @param {string} [message="Assertion failed"]
|
|
83
|
+
* @param {NodeComfortErrorOptions} [options]
|
|
84
|
+
*/
|
|
85
|
+
constructor(message?: string, options?: NodeComfortErrorOptions);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Thrown when something takes too long: `func.timeout`, `async.poll`, HTTP
|
|
89
|
+
* requests, `emitter.waitFor`...
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* try {
|
|
93
|
+
* await nc.func.timeout(slowTask(), 1000);
|
|
94
|
+
* } catch (error) {
|
|
95
|
+
* if (error instanceof nc.errors.TimeoutError) console.log(`Gave up after ${error.timeout}ms`);
|
|
96
|
+
* }
|
|
97
|
+
*/
|
|
98
|
+
export class TimeoutError extends NodeComfortError {
|
|
99
|
+
/**
|
|
100
|
+
* @param {string} [message="Operation timed out"]
|
|
101
|
+
* @param {NodeComfortErrorOptions & { timeout?: number }} [options] Can include the time limit in milliseconds.
|
|
102
|
+
*/
|
|
103
|
+
constructor(message?: string, options?: NodeComfortErrorOptions & {
|
|
104
|
+
timeout?: number;
|
|
105
|
+
});
|
|
106
|
+
/** The time limit in milliseconds, when known. */
|
|
107
|
+
timeout: number | undefined;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Thrown when an operation is cancelled with an `AbortSignal`. Its name is
|
|
111
|
+
* `"AbortError"`, like native abort errors, so existing checks keep working.
|
|
112
|
+
*/
|
|
113
|
+
export class AbortError extends NodeComfortError {
|
|
114
|
+
/**
|
|
115
|
+
* @param {string} [message="The operation was aborted"]
|
|
116
|
+
* @param {NodeComfortErrorOptions} [options] Usually `{ cause: signal.reason }`.
|
|
117
|
+
*/
|
|
118
|
+
constructor(message?: string, options?: NodeComfortErrorOptions);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* One problem found while validating data.
|
|
122
|
+
* @typedef {object} ValidationIssue
|
|
123
|
+
* @property {Array<string|number>} path Where the problem is, like `["user", "emails", 0]`. Empty for the root value.
|
|
124
|
+
* @property {string} code What went wrong: `"invalid_type"`, `"too_small"`, `"invalid_string"`, `"custom"`...
|
|
125
|
+
* @property {string} message A message you can show to users.
|
|
126
|
+
* @property {string} [expected] The expected type, for type errors.
|
|
127
|
+
* @property {string} [received] The received type, for type errors.
|
|
128
|
+
*/
|
|
129
|
+
/**
|
|
130
|
+
* Thrown when data doesn't match a schema or when environment variables are
|
|
131
|
+
* invalid. `issues` lists every problem, not just the first one.
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* try {
|
|
135
|
+
* User.parse(body);
|
|
136
|
+
* } catch (error) {
|
|
137
|
+
* if (error instanceof nc.errors.ValidationError) return res.status(400).json(error.flatten());
|
|
138
|
+
* throw error;
|
|
139
|
+
* }
|
|
140
|
+
*/
|
|
141
|
+
export class ValidationError extends NodeComfortError {
|
|
142
|
+
/**
|
|
143
|
+
* Turns issues into a readable message, one line per issue.
|
|
144
|
+
*
|
|
145
|
+
* @param {ValidationIssue[]} issues
|
|
146
|
+
* @returns {string}
|
|
147
|
+
*/
|
|
148
|
+
static format(issues: ValidationIssue[]): string;
|
|
149
|
+
/**
|
|
150
|
+
* @param {ValidationIssue[]} issues
|
|
151
|
+
* @param {NodeComfortErrorOptions & { message?: string }} [options]
|
|
152
|
+
*/
|
|
153
|
+
constructor(issues: ValidationIssue[], options?: NodeComfortErrorOptions & {
|
|
154
|
+
message?: string;
|
|
155
|
+
});
|
|
156
|
+
/** Every problem found, in order. */
|
|
157
|
+
issues: ValidationIssue[];
|
|
158
|
+
/**
|
|
159
|
+
* Groups messages by path, which is handy for forms.
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* error.flatten(); // { email: ["Invalid email address"], age: ["Must be at least 18"] }
|
|
163
|
+
*
|
|
164
|
+
* @returns {Record<string, string[]>} Messages by dotted path (`""` for the root).
|
|
165
|
+
*/
|
|
166
|
+
flatten(): Record<string, string[]>;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Thrown by `nc.http` when the response status isn't 2xx (unless you pass
|
|
170
|
+
* `throwHttpErrors: false`), and for network failures (`status` 0).
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
* try {
|
|
174
|
+
* await nc.http.get(url);
|
|
175
|
+
* } catch (error) {
|
|
176
|
+
* if (error instanceof nc.errors.HttpError) console.log(error.status, error.data);
|
|
177
|
+
* }
|
|
178
|
+
*/
|
|
179
|
+
export class HttpError extends NodeComfortError {
|
|
180
|
+
/**
|
|
181
|
+
* @param {string} message
|
|
182
|
+
* @param {NodeComfortErrorOptions & { status?: number, statusText?: string, url?: string, method?: string, headers?: Record<string, string>, data?: unknown }} [options]
|
|
183
|
+
*/
|
|
184
|
+
constructor(message: string, options?: NodeComfortErrorOptions & {
|
|
185
|
+
status?: number;
|
|
186
|
+
statusText?: string;
|
|
187
|
+
url?: string;
|
|
188
|
+
method?: string;
|
|
189
|
+
headers?: Record<string, string>;
|
|
190
|
+
data?: unknown;
|
|
191
|
+
});
|
|
192
|
+
/** HTTP status, like `404`. `0` when no response came back. */
|
|
193
|
+
status: number;
|
|
194
|
+
/** HTTP status text, like `"Not Found"`. */
|
|
195
|
+
statusText: string;
|
|
196
|
+
/** The requested URL. */
|
|
197
|
+
url: string;
|
|
198
|
+
/** The HTTP method, like `"GET"`. */
|
|
199
|
+
method: string;
|
|
200
|
+
/** Response headers, with lower-cased names. */
|
|
201
|
+
headers: Record<string, string>;
|
|
202
|
+
/** The response body: parsed JSON when possible, text otherwise. */
|
|
203
|
+
data: unknown;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Thrown by `nc.crypto.verifyJWT()`. The `code` tells you why:
|
|
207
|
+
* `ERR_JWT_MALFORMED`, `ERR_JWT_ALGORITHM`, `ERR_JWT_SIGNATURE`,
|
|
208
|
+
* `ERR_JWT_EXPIRED`, `ERR_JWT_NOT_BEFORE` or `ERR_JWT_CLAIM`.
|
|
209
|
+
*
|
|
210
|
+
* @example
|
|
211
|
+
* try {
|
|
212
|
+
* return nc.crypto.verifyJWT(token, secret);
|
|
213
|
+
* } catch (error) {
|
|
214
|
+
* if (error.code === "ERR_JWT_EXPIRED") return refreshSession();
|
|
215
|
+
* throw error;
|
|
216
|
+
* }
|
|
217
|
+
*/
|
|
218
|
+
export class JWTError extends NodeComfortError {
|
|
219
|
+
/**
|
|
220
|
+
* @param {string} message
|
|
221
|
+
* @param {NodeComfortErrorOptions & { expiredAt?: Date }} [options]
|
|
222
|
+
*/
|
|
223
|
+
constructor(message: string, options?: NodeComfortErrorOptions & {
|
|
224
|
+
expiredAt?: Date;
|
|
225
|
+
});
|
|
226
|
+
/** When the token expired. Only set for `ERR_JWT_EXPIRED`. */
|
|
227
|
+
expiredAt: Date | undefined;
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Thrown by `nc.sys.run()` and `nc.sys.exec()` when a command fails, is
|
|
231
|
+
* killed or times out. Its output is kept on the error.
|
|
232
|
+
*
|
|
233
|
+
* @example
|
|
234
|
+
* try {
|
|
235
|
+
* await nc.sys.run("git", ["push"]);
|
|
236
|
+
* } catch (error) {
|
|
237
|
+
* console.error(error.exitCode, error.stderr);
|
|
238
|
+
* }
|
|
239
|
+
*/
|
|
240
|
+
export class ProcessError extends NodeComfortError {
|
|
241
|
+
/**
|
|
242
|
+
* @param {string} message
|
|
243
|
+
* @param {NodeComfortErrorOptions & { command?: string, exitCode?: number | null, signal?: string | null, stdout?: string, stderr?: string, timedOut?: boolean }} [options]
|
|
244
|
+
*/
|
|
245
|
+
constructor(message: string, options?: NodeComfortErrorOptions & {
|
|
246
|
+
command?: string;
|
|
247
|
+
exitCode?: number | null;
|
|
248
|
+
signal?: string | null;
|
|
249
|
+
stdout?: string;
|
|
250
|
+
stderr?: string;
|
|
251
|
+
timedOut?: boolean;
|
|
252
|
+
});
|
|
253
|
+
/** The command that ran. */
|
|
254
|
+
command: string;
|
|
255
|
+
/** Exit code, or `null` if a signal killed the process. */
|
|
256
|
+
exitCode: number | null;
|
|
257
|
+
/** The signal that killed the process, like `"SIGTERM"`. */
|
|
258
|
+
signal: string | null;
|
|
259
|
+
/** Everything written to stdout. */
|
|
260
|
+
stdout: string;
|
|
261
|
+
/** Everything written to stderr. */
|
|
262
|
+
stderr: string;
|
|
263
|
+
/** `true` if the process was killed for exceeding its `timeout`. */
|
|
264
|
+
timedOut: boolean;
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Thrown by `nc.SQLite` when a statement fails. Check `sqliteCode` to handle
|
|
268
|
+
* specific cases like a duplicate value.
|
|
269
|
+
*
|
|
270
|
+
* @example
|
|
271
|
+
* try {
|
|
272
|
+
* db.insert("users", { email });
|
|
273
|
+
* } catch (error) {
|
|
274
|
+
* if (error.sqliteCode === "SQLITE_CONSTRAINT_UNIQUE") return "Email already taken";
|
|
275
|
+
* throw error;
|
|
276
|
+
* }
|
|
277
|
+
*/
|
|
278
|
+
export class SQLiteError extends NodeComfortError {
|
|
279
|
+
/**
|
|
280
|
+
* @param {string} message
|
|
281
|
+
* @param {NodeComfortErrorOptions & { sql?: string, sqliteCode?: string }} [options]
|
|
282
|
+
*/
|
|
283
|
+
constructor(message: string, options?: NodeComfortErrorOptions & {
|
|
284
|
+
sql?: string;
|
|
285
|
+
sqliteCode?: string;
|
|
286
|
+
});
|
|
287
|
+
/** The SQL that failed, when there is one. */
|
|
288
|
+
sql: string | undefined;
|
|
289
|
+
/** SQLite's error code, like `"SQLITE_CONSTRAINT_UNIQUE"`, when known. */
|
|
290
|
+
sqliteCode: string | undefined;
|
|
291
|
+
}
|
|
292
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/errors.js"],"names":[],"mappings":";;;;;;;;;;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odenext",
|
|
5
|
-
"allowJs": true,
|
|
6
|
-
"declaration": true,
|
|
7
|
-
"emitDeclarationOnly": true,
|
|
8
|
-
"outDir": "types",
|
|
9
|
-
"declarationMap": true,
|
|
10
|
-
"skipLibCheck": true,
|
|
11
|
-
"forceConsistentCasingInFileNames": true,
|
|
12
|
-
"moduleResolution": "nodenext",
|
|
13
|
-
"esModuleInterop": true,
|
|
14
|
-
"resolveJsonModule": true
|
|
15
|
-
},
|
|
16
|
-
"include": ["**/*"],
|
|
17
|
-
"exclude": ["node_modules", "types"]
|
|
18
|
-
}
|
package/types/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.js"],"names":[],"mappings":";;;;;;;;;;;;;;;iFAYi3N,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;kCAAn0C,MAAM;;;;QAAuZ,MAAM;uBAAiX,MAAM;0BAAq/B,MAAM;gBAA0T,MAAM"}
|