@ix-xs/node-comfort 1.2.0 → 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.
Files changed (75) hide show
  1. package/CHANGELOG.md +190 -0
  2. package/README.md +136 -549
  3. package/index.js +80 -35
  4. package/package.json +160 -33
  5. package/src/Arr.js +887 -419
  6. package/src/Async.js +585 -0
  7. package/src/Cache.js +329 -0
  8. package/src/Checker.js +971 -539
  9. package/src/Cli.js +1034 -0
  10. package/src/Color.js +415 -0
  11. package/src/Crypto.js +707 -0
  12. package/src/Emitter.js +289 -0
  13. package/src/Env.js +555 -0
  14. package/src/FS.js +1483 -894
  15. package/src/Func.js +670 -353
  16. package/src/Http.js +538 -0
  17. package/src/Id.js +384 -201
  18. package/src/Logger.js +739 -378
  19. package/src/Num.js +844 -361
  20. package/src/Obj.js +855 -372
  21. package/src/SQLite.js +1308 -1005
  22. package/src/Schema.js +1586 -0
  23. package/src/Str.js +983 -436
  24. package/src/Sys.js +511 -0
  25. package/src/Time.js +1260 -422
  26. package/src/Utils.js +321 -325
  27. package/src/errors.js +292 -0
  28. package/types/index.d.ts +693 -526
  29. package/types/src/Arr.d.ts +455 -145
  30. package/types/src/Arr.d.ts.map +1 -1
  31. package/types/src/Async.d.ts +321 -0
  32. package/types/src/Async.d.ts.map +1 -0
  33. package/types/src/Cache.d.ts +195 -0
  34. package/types/src/Cache.d.ts.map +1 -0
  35. package/types/src/Checker.d.ts +501 -235
  36. package/types/src/Checker.d.ts.map +1 -1
  37. package/types/src/Cli.d.ts +582 -0
  38. package/types/src/Cli.d.ts.map +1 -0
  39. package/types/src/Color.d.ts +294 -0
  40. package/types/src/Color.d.ts.map +1 -0
  41. package/types/src/Crypto.d.ts +534 -0
  42. package/types/src/Crypto.d.ts.map +1 -0
  43. package/types/src/Emitter.d.ts +170 -0
  44. package/types/src/Emitter.d.ts.map +1 -0
  45. package/types/src/Env.d.ts +382 -0
  46. package/types/src/Env.d.ts.map +1 -0
  47. package/types/src/FS.d.ts +686 -238
  48. package/types/src/FS.d.ts.map +1 -1
  49. package/types/src/Func.d.ts +380 -135
  50. package/types/src/Func.d.ts.map +1 -1
  51. package/types/src/Http.d.ts +374 -0
  52. package/types/src/Http.d.ts.map +1 -0
  53. package/types/src/Id.d.ts +149 -68
  54. package/types/src/Id.d.ts.map +1 -1
  55. package/types/src/Logger.d.ts +363 -46
  56. package/types/src/Logger.d.ts.map +1 -1
  57. package/types/src/Num.d.ts +473 -137
  58. package/types/src/Num.d.ts.map +1 -1
  59. package/types/src/Obj.d.ts +390 -104
  60. package/types/src/Obj.d.ts.map +1 -1
  61. package/types/src/SQLite.d.ts +646 -309
  62. package/types/src/SQLite.d.ts.map +1 -1
  63. package/types/src/Schema.d.ts +940 -0
  64. package/types/src/Schema.d.ts.map +1 -0
  65. package/types/src/Str.d.ts +569 -166
  66. package/types/src/Str.d.ts.map +1 -1
  67. package/types/src/Sys.d.ts +269 -0
  68. package/types/src/Sys.d.ts.map +1 -0
  69. package/types/src/Time.d.ts +663 -204
  70. package/types/src/Time.d.ts.map +1 -1
  71. package/types/src/Utils.d.ts +154 -102
  72. package/types/src/Utils.d.ts.map +1 -1
  73. package/types/src/errors.d.ts +292 -0
  74. package/types/src/errors.d.ts.map +1 -0
  75. package/types/index.d.ts.map +0 -1
@@ -0,0 +1,374 @@
1
+ declare namespace _exports {
2
+ export { HttpBody, HttpMethod, QueryParams, HttpRetryOptions, HttpRequestConfig, HttpHooks, HttpOptions, HttpResponse, HttpClient };
3
+ }
4
+ declare namespace _exports {
5
+ export { request };
6
+ export { get };
7
+ export { head };
8
+ export { post };
9
+ export { put };
10
+ export { patch };
11
+ export { del as delete };
12
+ export { download };
13
+ export { create };
14
+ export { HttpError };
15
+ }
16
+ export = _exports;
17
+ /**
18
+ * A request body.
19
+ */
20
+ type HttpBody = string | Buffer | Uint8Array | ArrayBuffer | URLSearchParams | FormData | Blob | ReadableStream | import("node:stream").Readable;
21
+ /**
22
+ * An HTTP method.
23
+ */
24
+ type HttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS" | (string & {});
25
+ /**
26
+ * Query string values. `null` and `undefined` are skipped, arrays repeat the key.
27
+ */
28
+ type QueryParams = Record<string, string | number | boolean | null | undefined | Array<string | number | boolean>>;
29
+ /**
30
+ * Retry settings.
31
+ */
32
+ type HttpRetryOptions = {
33
+ /**
34
+ * Total attempts, the first included. Defaults to `3` for methods that are safe to repeat, `1` for the others.
35
+ */
36
+ attempts?: number | undefined;
37
+ /**
38
+ * Wait before the first retry, in ms. Defaults to `300`.
39
+ */
40
+ delay?: number | undefined;
41
+ /**
42
+ * Multiplies the wait after each attempt. Defaults to `2`.
43
+ */
44
+ backoff?: number | undefined;
45
+ /**
46
+ * Longest wait, `Retry-After` included. Defaults to 30 seconds.
47
+ */
48
+ maxDelay?: number | undefined;
49
+ /**
50
+ * Statuses worth retrying. Defaults to 408, 413, 429, 500, 502, 503 and 504.
51
+ */
52
+ statuses?: number[] | undefined;
53
+ /**
54
+ * Methods allowed to retry. POST and PATCH aren't, by default, so nothing gets created twice.
55
+ */
56
+ methods?: HttpMethod[] | undefined;
57
+ };
58
+ /**
59
+ * A request as hooks see it.
60
+ */
61
+ type HttpRequestConfig = {
62
+ /**
63
+ * The final URL, base URL and query included.
64
+ */
65
+ url: string;
66
+ method: HttpMethod;
67
+ /**
68
+ * Lower-cased names.
69
+ */
70
+ headers: Record<string, string>;
71
+ body: HttpBody | undefined;
72
+ /**
73
+ * 1 for the first try.
74
+ */
75
+ attempt: number;
76
+ };
77
+ /**
78
+ * Hooks to watch or change requests.
79
+ */
80
+ type HttpHooks = {
81
+ /**
82
+ * Runs before each attempt. It can change the URL, headers or body.
83
+ */
84
+ beforeRequest?: ((config: HttpRequestConfig) => void | Promise<void>)[] | undefined;
85
+ /**
86
+ * Runs after each response, before errors are thrown. It can return another response.
87
+ */
88
+ afterResponse?: ((response: HttpResponse<any>, config: HttpRequestConfig) => void | HttpResponse<any> | Promise<void | HttpResponse<any>>)[] | undefined;
89
+ /**
90
+ * Runs before waiting for a retry.
91
+ */
92
+ beforeRetry?: ((error: unknown, attempt: number, delay: number) => void | Promise<void>)[] | undefined;
93
+ };
94
+ /**
95
+ * Request options.
96
+ */
97
+ type HttpOptions = {
98
+ /**
99
+ * Defaults to `"GET"`.
100
+ */
101
+ method?: HttpMethod | undefined;
102
+ /**
103
+ * Prefix for relative URLs.
104
+ */
105
+ baseURL?: string | undefined;
106
+ /**
107
+ * Set a header to `undefined` to remove a default one.
108
+ */
109
+ headers?: Record<string, string | undefined> | undefined;
110
+ /**
111
+ * Added to the URL's own query string.
112
+ */
113
+ query?: URLSearchParams | QueryParams | undefined;
114
+ /**
115
+ * Sent as JSON.
116
+ */
117
+ json?: unknown;
118
+ /**
119
+ * Sent as a URL-encoded form.
120
+ */
121
+ form?: Record<string, string | number | boolean> | undefined;
122
+ /**
123
+ * Sent as is: string, Buffer, `FormData`, stream...
124
+ */
125
+ body?: HttpBody | undefined;
126
+ /**
127
+ * Sets the `authorization` header.
128
+ */
129
+ auth?: {
130
+ bearer: string;
131
+ } | {
132
+ username: string;
133
+ password: string;
134
+ } | undefined;
135
+ /**
136
+ * Time limit per attempt, like `"10s"`. `false` turns it off. Defaults to 30 seconds.
137
+ */
138
+ timeout?: string | number | false | undefined;
139
+ /**
140
+ * Number of attempts, `false`, or retry settings.
141
+ */
142
+ retry?: number | false | HttpRetryOptions | undefined;
143
+ /**
144
+ * How to read the body. `"auto"` reads JSON or text based on the content type, a Buffer otherwise.
145
+ */
146
+ responseType?: "auto" | "json" | "buffer" | "text" | "stream" | undefined;
147
+ /**
148
+ * Throw an `HttpError` when the status isn't 2xx. Defaults to `true`.
149
+ */
150
+ throwHttpErrors?: boolean | undefined;
151
+ /**
152
+ * Cancels the request.
153
+ */
154
+ signal?: AbortSignal | undefined;
155
+ hooks?: HttpHooks | undefined;
156
+ /**
157
+ * Called while the body downloads.
158
+ */
159
+ onDownloadProgress?: ((progress: {
160
+ loaded: number;
161
+ total: number | undefined;
162
+ percent: number | undefined;
163
+ }) => void) | undefined;
164
+ /**
165
+ * Defaults to `"follow"`.
166
+ */
167
+ redirect?: "error" | "follow" | "manual" | undefined;
168
+ };
169
+ /**
170
+ * A response.
171
+ */
172
+ type HttpResponse<T = any> = {
173
+ status: number;
174
+ statusText: string;
175
+ /**
176
+ * `true` for 2xx statuses.
177
+ */
178
+ ok: boolean;
179
+ /**
180
+ * Lower-cased names.
181
+ */
182
+ headers: Record<string, string>;
183
+ /**
184
+ * The body, already read (see `responseType`).
185
+ */
186
+ data: T;
187
+ /**
188
+ * The final URL, after redirects.
189
+ */
190
+ url: string;
191
+ /**
192
+ * In ms, retries included.
193
+ */
194
+ duration: number;
195
+ /**
196
+ * How many attempts it took.
197
+ */
198
+ attempts: number;
199
+ };
200
+ /**
201
+ * A client with its own defaults, from `create()`.
202
+ */
203
+ type HttpClient = {
204
+ /**
205
+ * Sends a request.
206
+ */
207
+ request: <T = any>(url: string | URL, options?: HttpOptions) => Promise<HttpResponse<T>>;
208
+ /**
209
+ * Sends a GET request.
210
+ */
211
+ get: <T = any>(url: string | URL, options?: HttpOptions) => Promise<HttpResponse<T>>;
212
+ /**
213
+ * Sends a HEAD request.
214
+ */
215
+ head: <T = any>(url: string | URL, options?: HttpOptions) => Promise<HttpResponse<T>>;
216
+ /**
217
+ * Sends a DELETE request.
218
+ */
219
+ delete: <T = any>(url: string | URL, options?: HttpOptions) => Promise<HttpResponse<T>>;
220
+ /**
221
+ * Sends a POST request. Objects and arrays go as JSON.
222
+ */
223
+ post: <T = any>(url: string | URL, data?: unknown, options?: HttpOptions) => Promise<HttpResponse<T>>;
224
+ /**
225
+ * Sends a PUT request. Objects and arrays go as JSON.
226
+ */
227
+ put: <T = any>(url: string | URL, data?: unknown, options?: HttpOptions) => Promise<HttpResponse<T>>;
228
+ /**
229
+ * Sends a PATCH request. Objects and arrays go as JSON.
230
+ */
231
+ patch: <T = any>(url: string | URL, data?: unknown, options?: HttpOptions) => Promise<HttpResponse<T>>;
232
+ /**
233
+ * Saves a response to a file.
234
+ */
235
+ download: (url: string | URL, destination: string, options?: HttpOptions) => Promise<{
236
+ path: string;
237
+ size: number;
238
+ }>;
239
+ /**
240
+ * A new client with these defaults added.
241
+ */
242
+ extend: (defaults: HttpOptions) => HttpClient;
243
+ defaults: Readonly<HttpOptions>;
244
+ };
245
+ /**
246
+ * Sends a request and resolves with the response, body already read.
247
+ * Throws an `HttpError` for error statuses, a `TimeoutError` when it's too
248
+ * slow and an `AbortError` when cancelled.
249
+ *
250
+ * @example
251
+ * const res = await http.request("https://api.example.com/items/42", {
252
+ * method: "PUT",
253
+ * json: { name: "Lamp" },
254
+ * timeout: "10s",
255
+ * retry: { attempts: 5, statuses: [429, 503] },
256
+ * });
257
+ * res.data; // parsed JSON
258
+ *
259
+ * @template [T=any]
260
+ * @param {string | URL} url An absolute URL, or a path when `baseURL` is set.
261
+ * @param {HttpOptions} [options]
262
+ * @returns {Promise<HttpResponse<T>>}
263
+ * @throws {HttpError | TimeoutError | AbortError}
264
+ */
265
+ declare function request<T = any>(url: string | URL, options?: HttpOptions): Promise<HttpResponse<T>>;
266
+ /**
267
+ * Sends a GET request.
268
+ *
269
+ * @example
270
+ * const { data } = await http.get("https://api.example.com/users", { query: { page: 2, tags: ["a", "b"] } });
271
+ *
272
+ * @template [T=any]
273
+ * @param {string | URL} url
274
+ * @param {HttpOptions} [options]
275
+ * @returns {Promise<HttpResponse<T>>}
276
+ */
277
+ declare function get<T = any>(url: string | URL, options?: HttpOptions): Promise<HttpResponse<T>>;
278
+ /**
279
+ * Sends a HEAD request: headers only, no body.
280
+ *
281
+ * @example
282
+ * const { headers } = await http.head("https://example.com/big.zip");
283
+ * headers["content-length"];
284
+ *
285
+ * @template [T=undefined]
286
+ * @param {string | URL} url
287
+ * @param {HttpOptions} [options]
288
+ * @returns {Promise<HttpResponse<T>>}
289
+ */
290
+ declare function head<T = undefined>(url: string | URL, options?: HttpOptions): Promise<HttpResponse<T>>;
291
+ /**
292
+ * Sends a POST request. Objects, arrays, numbers and booleans go as JSON;
293
+ * strings, Buffers, `FormData`, Blobs and streams go as they are.
294
+ *
295
+ * @example
296
+ * const { data: user } = await http.post("https://api.example.com/users", { name: "Ada" });
297
+ * await http.post(uploadUrl, formData);
298
+ *
299
+ * @template [T=any]
300
+ * @param {string | URL} url
301
+ * @param {unknown} [data]
302
+ * @param {HttpOptions} [options]
303
+ * @returns {Promise<HttpResponse<T>>}
304
+ */
305
+ declare function post<T = any>(url: string | URL, data?: unknown, options?: HttpOptions): Promise<HttpResponse<T>>;
306
+ /**
307
+ * Sends a PUT request. The body works like in `post()`.
308
+ *
309
+ * @template [T=any]
310
+ * @param {string | URL} url
311
+ * @param {unknown} [data]
312
+ * @param {HttpOptions} [options]
313
+ * @returns {Promise<HttpResponse<T>>}
314
+ */
315
+ declare function put<T = any>(url: string | URL, data?: unknown, options?: HttpOptions): Promise<HttpResponse<T>>;
316
+ /**
317
+ * Sends a PATCH request. The body works like in `post()`.
318
+ *
319
+ * @template [T=any]
320
+ * @param {string | URL} url
321
+ * @param {unknown} [data]
322
+ * @param {HttpOptions} [options]
323
+ * @returns {Promise<HttpResponse<T>>}
324
+ */
325
+ declare function patch<T = any>(url: string | URL, data?: unknown, options?: HttpOptions): Promise<HttpResponse<T>>;
326
+ /**
327
+ * Sends a DELETE request.
328
+ *
329
+ * @example
330
+ * await http.delete(`https://api.example.com/users/${id}`);
331
+ *
332
+ * @template [T=any]
333
+ * @param {string | URL} url
334
+ * @param {HttpOptions} [options]
335
+ * @returns {Promise<HttpResponse<T>>}
336
+ */
337
+ declare function del<T = any>(url: string | URL, options?: HttpOptions): Promise<HttpResponse<T>>;
338
+ /**
339
+ * Downloads to a file as a stream, so memory stays flat whatever the size.
340
+ * Folders are created, and a partial file is removed if it fails.
341
+ *
342
+ * @example
343
+ * await http.download("https://example.com/video.mp4", "./downloads/video.mp4", {
344
+ * onDownloadProgress: ({ percent }) => bar.update(percent ?? 0),
345
+ * });
346
+ *
347
+ * @param {string | URL} url
348
+ * @param {string} destination
349
+ * @param {HttpOptions} [options]
350
+ * @returns {Promise<{ path: string, size: number }>} The absolute path and size in bytes.
351
+ * @throws {HttpError | TimeoutError | AbortError}
352
+ */
353
+ declare function download(url: string | URL, destination: string, options?: HttpOptions): Promise<{
354
+ path: string;
355
+ size: number;
356
+ }>;
357
+ /**
358
+ * Creates a client with default options. Options passed to each call are
359
+ * merged on top.
360
+ *
361
+ * @example
362
+ * const github = http.create({
363
+ * baseURL: "https://api.github.com",
364
+ * auth: { bearer: process.env.GITHUB_TOKEN },
365
+ * hooks: { afterResponse: [(res) => nc.debug(res.status, res.url)] },
366
+ * });
367
+ * const { data: repo } = await github.get("/repos/nodejs/node");
368
+ *
369
+ * @param {HttpOptions} [defaults]
370
+ * @returns {HttpClient}
371
+ */
372
+ declare function create(defaults?: HttpOptions): HttpClient;
373
+ import { HttpError } from "./errors.js";
374
+ //# sourceMappingURL=Http.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Http.d.ts","sourceRoot":"","sources":["../../src/Http.js"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;gBAsBa,MAAM,GAAG,MAAM,GAAG,UAAU,GAAG,WAAW,GAAG,eAAe,GAAG,QAAQ,GAAG,IAAI,GAAG,cAAc,GAAG,OAAO,aAAa,EAAE,QAAQ;;;;kBAKhI,KAAK,GAAG,MAAM,GAAG,KAAK,GAAG,OAAO,GAAG,QAAQ,GAAG,MAAM,GAAG,SAAS,GAAG,CAAC,MAAM,GAAG,EAAE,CAAC;;;;mBAKhF,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,SAAS,GAAG,KAAK,CAAC,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;SAiB9F,MAAM;YACN,UAAU;;;;a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YAAY,CAAC,CAAC,CAAC,CAAC,CAIpC;AAED;;;;;;;;;;;GAWG;AACH,sBALc,CAAC,mBACJ,MAAM,GAAG,GAAG,YACZ,WAAW,GACT,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAIpC;AAED;;;;;;;;;;;;;GAaG;AACH,sBANc,CAAC,aACJ,MAAM,GAAG,GAAG,SACZ,OAAO,YACP,WAAW,GACT,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAIpC;AAED;;;;;;;;GAQG;AACH,qBANc,CAAC,aACJ,MAAM,GAAG,GAAG,SACZ,OAAO,YACP,WAAW,GACT,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAIpC;AAED;;;;;;;;GAQG;AACH,uBANc,CAAC,aACJ,MAAM,GAAG,GAAG,SACZ,OAAO,YACP,WAAW,GACT,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAIpC;AAED;;;;;;;;;;GAUG;AACH,qBALc,CAAC,aACJ,MAAM,GAAG,GAAG,YACZ,WAAW,GACT,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAIpC;AAED;;;;;;;;;;;;;;GAcG;AACH,+BANW,MAAM,GAAG,GAAG,eACZ,MAAM,YACN,WAAW,GACT,OAAO,CAAC;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC,CAwBnD;AAED;;;;;;;;;;;;;;GAcG;AACH,mCAHW,WAAW,GACT,UAAU,CAItB"}
package/types/src/Id.d.ts CHANGED
@@ -1,119 +1,200 @@
1
1
  /**
2
- * Generates a random UUID v4.
2
+ * Encodings for `token()`.
3
+ */
4
+ export type TokenEncoding = "hex" | "base64" | "base64url" | "base58" | "base32";
5
+ /**
6
+ * Options for `snowflake()` and `parseSnowflake()`.
7
+ */
8
+ export type SnowflakeOptions = {
9
+ /**
10
+ * Start of time, in ms. Defaults to Discord's (2015-01-01). Twitter uses `1288834974657`.
11
+ */
12
+ epoch?: number | undefined;
13
+ /**
14
+ * 0 to 31. Defaults to `0`.
15
+ */
16
+ workerId?: number | undefined;
17
+ /**
18
+ * 0 to 31. Defaults to `process.pid % 32`.
19
+ */
20
+ processId?: number | undefined;
21
+ };
22
+ /**
23
+ * A decoded Snowflake.
24
+ */
25
+ export type ParsedSnowflake = {
26
+ /**
27
+ * When it was created.
28
+ */
29
+ date: Date;
30
+ /**
31
+ * When it was created, in ms.
32
+ */
33
+ timestamp: number;
34
+ workerId: number;
35
+ processId: number;
36
+ /**
37
+ * Sequence number within the millisecond, 0 to 4095.
38
+ */
39
+ increment: number;
40
+ };
41
+ /**
42
+ * A random UUID (version 4). For ids that sort by date, see `uuidv7()`.
3
43
  *
4
44
  * @example
5
- * nodeComfort.id.uuid(); // "3f2504e0-4f89-41d3-9a0c-0305e82c3301"
45
+ * id.uuid(); // "3f2504e0-4f89-41d3-9a0c-0305e82c3301"
6
46
  *
7
- * @returns {string} A UUID v4 string.
47
+ * @returns {string}
8
48
  */
9
49
  export function uuid(): string;
10
50
  /**
11
- * Generates a URL-safe, collision-resistant ID (nanoid-compatible alphabet).
12
- *
13
- * Uses secure randomness with unbiased rejection sampling.
51
+ * A UUID v7: it starts with a timestamp, so ids sort by creation time.
52
+ * That keeps database indexes compact, which makes them great primary
53
+ * keys. Ids from the same process always increase, even within a
54
+ * millisecond.
14
55
  *
15
56
  * @example
16
- * nodeComfort.id.nano(); // "V1StGXR8_Z5jdHi6B-myT"
17
- * nodeComfort.id.nano(10); // "IRFa-VaY2b"
57
+ * id.uuidv7(); // "0190a5c4-7c1e-7b2a-9f3e-1c2d3e4f5a6b"
58
+ * id.timestamp(id.uuidv7()); // when it was created
18
59
  *
19
- * @param {number} [size=21] - Desired length.
20
- * @param {string} [alphabet] - Custom alphabet.
21
- * @returns {string} The generated ID.
60
+ * @returns {string}
22
61
  */
23
- export function nano(size?: number, alphabet?: string): string;
62
+ export function uuidv7(): string;
24
63
  /**
25
- * Generates a ULID • a lexicographically-sortable, timestamp-prefixed ID.
26
- *
27
- * ULIDs sort by creation time and are a great primary-key alternative to UUIDs.
64
+ * A ULID: 26 URL-safe characters that sort by creation time. Ids made in
65
+ * the same millisecond still increase.
28
66
  *
29
67
  * @example
30
- * nodeComfort.id.ulid(); // "01ARZ3NDEKTSV4RRFFQ69G5FAV"
68
+ * id.ulid(); // "01HQ3Z8K9V3N5W7Y2XJ4T6R8PB"
69
+ * id.ulid(Date.UTC(2024, 0, 1)); // for a given time
31
70
  *
32
- * @param {number} [time=Date.now()] - Timestamp in ms to encode.
33
- * @returns {string} A 26-character ULID.
71
+ * @param {number} [time] Timestamp to encode, in ms. Defaults to now.
72
+ * @returns {string}
34
73
  */
35
74
  export function ulid(time?: number): string;
36
75
  /**
37
- * Generates a cryptographically-secure random token.
76
+ * A short, URL-safe random id, like `nanoid`. At 21 characters, collisions
77
+ * are as unlikely as with a UUID.
38
78
  *
39
79
  * @example
40
- * nodeComfort.id.token(); // 32-byte hex string
41
- * nodeComfort.id.token(16, "base64url"); // 16 bytes, URL-safe base64
80
+ * id.nano(); // "V1StGXR8_Z5jdHi6B-myT"
81
+ * id.nano(10); // "IRFa-VaY2b"
82
+ * id.nano(8, "0123456789abcdef"); // "4f90d13a"
42
83
  *
43
- * @param {number} [bytes=32] - Number of random bytes.
44
- * @param {"hex"|"base64"|"base64url"|"base58"} [encoding="hex"] - Output encoding.
45
- * @returns {string} The token.
84
+ * @param {number} [size=21]
85
+ * @param {string} [alphabet] 2 to 256 characters. Defaults to letters, digits, `_` and `-`.
86
+ * @returns {string}
46
87
  */
47
- export function token(bytes?: number, encoding?: "hex" | "base64" | "base64url" | "base58"): string;
88
+ export function nano(size?: number, alphabet?: string): string;
48
89
  /**
49
- * Generates a short, human-friendly random code (uppercase + digits by default).
50
- *
51
- * Ambiguous characters (`0/O`, `1/I`) are excluded, making it suitable for
52
- * coupons, OTP-like codes and invite links.
90
+ * Makes an id generator with your own alphabet and length.
53
91
  *
54
92
  * @example
55
- * nodeComfort.id.code(); // "K7QF9X"
56
- * nodeComfort.id.code(4, "1234"); // "3142"
93
+ * const orderId = id.customAlphabet("0123456789ABCDEF", 12);
94
+ * orderId(); // "4F1A09C2BB7E"
57
95
  *
58
- * @param {number} [length=6] - Number of characters.
59
- * @param {string} [alphabet="ABCDEFGHJKLMNPQRSTUVWXYZ23456789"] - Characters to pick from.
60
- * @returns {string} The random code.
96
+ * @param {string} alphabet
97
+ * @param {number} [size=21]
98
+ * @returns {(size?: number) => string}
61
99
  */
62
- export function code(length?: number, alphabet?: string): string;
100
+ export function customAlphabet(alphabet: string, size?: number): (size?: number) => string;
63
101
  /**
64
- * Hashes a value with a chosen algorithm.
102
+ * A random token for API keys, session ids, reset links...
65
103
  *
66
104
  * @example
67
- * nodeComfort.id.hash("hello"); // sha256 hex
68
- * nodeComfort.id.hash("hello", { algorithm: "md5" });
105
+ * id.token(); // 64 hex characters
106
+ * id.token(16, "base64url"); // 22 URL-safe characters
107
+ * id.token(16, "base58"); // no look-alike characters
69
108
  *
70
- * @param {string|Buffer} value - Data to hash.
71
- * @param {{ algorithm?: string, encoding?: "hex"|"base64"|"base64url" }} [options] - Hash options.
72
- * @returns {string} The digest.
109
+ * @param {number} [bytes=32] How much randomness, in bytes.
110
+ * @param {TokenEncoding} [encoding="hex"]
111
+ * @returns {string}
73
112
  */
74
- export function hash(value: string | Buffer, options?: {
75
- algorithm?: string;
76
- encoding?: "hex" | "base64" | "base64url";
77
- }): string;
113
+ export function token(bytes?: number, encoding?: TokenEncoding): string;
78
114
  /**
79
- * Computes an HMAC signature for a value using a secret key.
115
+ * A short code that's easy to read aloud and type: no `0/O` or `1/I/L`
116
+ * mix-ups. For invites, coupons and verification codes.
80
117
  *
81
118
  * @example
82
- * nodeComfort.id.hmac("payload", "secret"); // sha256 HMAC hex
119
+ * id.code(); // "K7QF9X"
120
+ * id.code(4, "0123456789"); // "3920"
83
121
  *
84
- * @param {string|Buffer} value - Data to sign.
85
- * @param {string|Buffer} secret - Secret key.
86
- * @param {{ algorithm?: string, encoding?: "hex"|"base64"|"base64url" }} [options] - HMAC options.
87
- * @returns {string} The signature.
122
+ * @param {number} [length=6]
123
+ * @param {string} [alphabet="ABCDEFGHJKMNPQRSTUVWXYZ23456789"]
124
+ * @returns {string}
88
125
  */
89
- export function hmac(value: string | Buffer, secret: string | Buffer, options?: {
90
- algorithm?: string;
91
- encoding?: "hex" | "base64" | "base64url";
92
- }): string;
126
+ export function code(length?: number, alphabet?: string): string;
93
127
  /**
94
- * Compares two strings/buffers in constant time to prevent timing attacks.
128
+ * A Snowflake id, the 64-bit format used by Discord and Twitter. Returned
129
+ * as a string because it doesn't fit in a JavaScript number.
130
+ *
131
+ * @example
132
+ * id.snowflake(); // "1215361932810932224"
95
133
  *
96
- * Use this when comparing secrets, tokens, or signatures.
134
+ * @param {SnowflakeOptions} [options]
135
+ * @returns {string}
136
+ */
137
+ export function snowflake(options?: SnowflakeOptions): string;
138
+ /**
139
+ * Decodes a Snowflake. With the default epoch, it reads Discord ids.
97
140
  *
98
141
  * @example
99
- * if (nodeComfort.id.safeEqual(provided, expected)) { ... }
142
+ * id.parseSnowflake("175928847299117063").date; // 2016-04-30T11:18:25.796Z
100
143
  *
101
- * @param {string|Buffer} a - First value.
102
- * @param {string|Buffer} b - Second value.
103
- * @returns {boolean} True if equal.
144
+ * @param {string | bigint} id
145
+ * @param {SnowflakeOptions} [options] Use the epoch it was created with.
146
+ * @returns {ParsedSnowflake}
104
147
  */
105
- export function safeEqual(a: string | Buffer, b: string | Buffer): boolean;
148
+ export function parseSnowflake(id: string | bigint, options?: SnowflakeOptions): ParsedSnowflake;
106
149
  /**
107
- * Returns a monotonically-increasing counter ID prefixed with an optional label.
150
+ * When a ULID or a UUID v7 was created.
151
+ *
152
+ * @example
153
+ * id.timestamp(id.ulid()); // now
154
+ * id.timestamp(id.uuid()); // undefined, v4 has no time in it
108
155
  *
109
- * Useful for readable, unique keys within a single process run.
156
+ * @param {string} value
157
+ * @returns {Date | undefined}
158
+ */
159
+ export function timestamp(value: string): Date | undefined;
160
+ /**
161
+ * Readable sequential ids like `"user-1"`, `"user-2"`, counted per prefix.
162
+ * Only unique within the current process; handy in logs and tests.
110
163
  *
111
164
  * @example
112
- * nodeComfort.id.seq("user"); // "user-1"
113
- * nodeComfort.id.seq("user"); // "user-2"
165
+ * id.seq("user"); // "user-1"
166
+ * id.seq("user"); // "user-2"
114
167
  *
115
- * @param {string} [prefix=""] - Optional prefix.
116
- * @returns {string} The sequential ID.
168
+ * @param {string} [prefix=""]
169
+ * @returns {string}
117
170
  */
118
171
  export function seq(prefix?: string): string;
172
+ /**
173
+ * Hashes a value.
174
+ *
175
+ * @deprecated Use `nc.crypto.hash()`. This alias goes away in 3.0.
176
+ * @param {string | Buffer | Uint8Array} value
177
+ * @param {import("./Crypto").HashOptions} [options]
178
+ * @returns {string}
179
+ */
180
+ export function hash(value: string | Buffer | Uint8Array, options?: import("./Crypto").HashOptions): string;
181
+ /**
182
+ * Signs a value with HMAC.
183
+ *
184
+ * @deprecated Use `nc.crypto.hmac()`. This alias goes away in 3.0.
185
+ * @param {string | Buffer | Uint8Array} value
186
+ * @param {string | Buffer | Uint8Array} secret
187
+ * @param {import("./Crypto").HashOptions} [options]
188
+ * @returns {string}
189
+ */
190
+ export function hmac(value: string | Buffer | Uint8Array, secret: string | Buffer | Uint8Array, options?: import("./Crypto").HashOptions): string;
191
+ /**
192
+ * Compares two secrets in constant time.
193
+ *
194
+ * @deprecated Use `nc.crypto.safeEqual()`. This alias goes away in 3.0.
195
+ * @param {string | Buffer | Uint8Array} a
196
+ * @param {string | Buffer | Uint8Array} b
197
+ * @returns {boolean}
198
+ */
199
+ export function safeEqual(a: string | Buffer | Uint8Array, b: string | Buffer | Uint8Array): boolean;
119
200
  //# sourceMappingURL=Id.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Id.d.ts","sourceRoot":"","sources":["../../src/Id.js"],"names":[],"mappings":"AAyDE;;;;;;;GA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