@ix-xs/node-comfort 1.1.2 → 1.1.31
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/README.md +255 -52
- package/package.json +6 -9
- package/src/SQLite.js +719 -174
- package/tsconfig.json +18 -0
- package/types/index.d.ts +120 -0
- package/types/index.d.ts.map +1 -0
- package/types/src/Checker.d.ts +145 -0
- package/types/src/Checker.d.ts.map +1 -0
- package/types/src/FS.d.ts +243 -0
- package/types/src/FS.d.ts.map +1 -0
- package/types/src/Logger.d.ts +35 -0
- package/types/src/Logger.d.ts.map +1 -0
- package/types/src/SQLite.d.ts +391 -0
- package/types/src/SQLite.d.ts.map +1 -0
- package/types/src/Utils.d.ts +124 -0
- package/types/src/Utils.d.ts.map +1 -0
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
export = SQLite;
|
|
2
|
+
declare class SQLite {
|
|
3
|
+
/**
|
|
4
|
+
* Creates a new SQLite database connection.
|
|
5
|
+
*
|
|
6
|
+
* The database file and its parent folders are created if necessary.
|
|
7
|
+
* WAL journal mode and foreign key support are enabled automatically.
|
|
8
|
+
*
|
|
9
|
+
* @param {string} [path] - Path to the SQLite database file.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const db = new SQLite();
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* const db = new SQLite("data/my-app.sqlite");
|
|
16
|
+
*/
|
|
17
|
+
constructor(path?: string);
|
|
18
|
+
/**
|
|
19
|
+
* Executes a raw SQL statement using `run()`.
|
|
20
|
+
*
|
|
21
|
+
* Use this for custom SQL that is not covered by the helper methods.
|
|
22
|
+
* This method is intended for statements such as:
|
|
23
|
+
* - `INSERT`
|
|
24
|
+
* - `UPDATE`
|
|
25
|
+
* - `DELETE`
|
|
26
|
+
* - `CREATE INDEX`
|
|
27
|
+
* - PRAGMA writes
|
|
28
|
+
*
|
|
29
|
+
* @param {string} sql - SQL statement.
|
|
30
|
+
* @param {any[]|any} [params=[]] - Parameters to bind.
|
|
31
|
+
* @returns {Promise<{ changes: number, lastInsertRowid: bigint|number } | { error: string }>}
|
|
32
|
+
*/
|
|
33
|
+
exec(sql: string, params?: any[] | any): Promise<{
|
|
34
|
+
changes: number;
|
|
35
|
+
lastInsertRowid: bigint | number;
|
|
36
|
+
} | {
|
|
37
|
+
error: string;
|
|
38
|
+
}>;
|
|
39
|
+
/**
|
|
40
|
+
* Executes a raw SQL query and returns the first row.
|
|
41
|
+
*
|
|
42
|
+
* @param {string} sql - SQL query.
|
|
43
|
+
* @param {any[]|any} [params=[]] - Parameters to bind.
|
|
44
|
+
* @returns {Promise<object|undefined|{ error: string }>}
|
|
45
|
+
*/
|
|
46
|
+
queryOne(sql: string, params?: any[] | any): Promise<object | undefined | {
|
|
47
|
+
error: string;
|
|
48
|
+
}>;
|
|
49
|
+
/**
|
|
50
|
+
* Executes a raw SQL query and returns all rows.
|
|
51
|
+
*
|
|
52
|
+
* @param {string} sql - SQL query.
|
|
53
|
+
* @param {any[]|any} [params=[]] - Parameters to bind.
|
|
54
|
+
* @returns {Promise<object[]|{ error: string }>}
|
|
55
|
+
*/
|
|
56
|
+
queryAll(sql: string, params?: any[] | any): Promise<object[] | {
|
|
57
|
+
error: string;
|
|
58
|
+
}>;
|
|
59
|
+
/**
|
|
60
|
+
* Creates a table and stores its schema in memory.
|
|
61
|
+
*
|
|
62
|
+
* This method supports both the new `columns` property and the legacy
|
|
63
|
+
* `options` property for backward compatibility.
|
|
64
|
+
*
|
|
65
|
+
* @param {{
|
|
66
|
+
* name: string,
|
|
67
|
+
* columns?: Record<string, {
|
|
68
|
+
* type: "INTEGER"|"TEXT"|"REAL"|"BLOB"|"NUMERIC",
|
|
69
|
+
* primaryKey?: boolean,
|
|
70
|
+
* autoincrement?: boolean,
|
|
71
|
+
* notNull?: boolean,
|
|
72
|
+
* unique?: boolean,
|
|
73
|
+
* defaultValue?: any,
|
|
74
|
+
* values?: any[],
|
|
75
|
+
* references?: { table: string, column: string },
|
|
76
|
+
* onDelete?: string,
|
|
77
|
+
* onUpdate?: string
|
|
78
|
+
* }>,
|
|
79
|
+
* options?: Record<string, any>,
|
|
80
|
+
* constraints?: Array<
|
|
81
|
+
* | { type: "unique", columns: string[] }
|
|
82
|
+
* | { type: "primaryKey", columns: string[] }
|
|
83
|
+
* | { type: "check", expression: string }
|
|
84
|
+
* | {
|
|
85
|
+
* type: "foreignKey",
|
|
86
|
+
* columns: string[],
|
|
87
|
+
* references: { table: string, columns: string[] },
|
|
88
|
+
* onDelete?: string,
|
|
89
|
+
* onUpdate?: string
|
|
90
|
+
* }
|
|
91
|
+
* >,
|
|
92
|
+
* indexes?: Array<{
|
|
93
|
+
* name?: string,
|
|
94
|
+
* columns: string[],
|
|
95
|
+
* unique?: boolean
|
|
96
|
+
* }>
|
|
97
|
+
* }} table - Table definition.
|
|
98
|
+
* @returns {Promise<{ ok: true } | { error: string }>}
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* await db.createTable({
|
|
102
|
+
* name: "Post",
|
|
103
|
+
* columns: {
|
|
104
|
+
* id: { type: "INTEGER", primaryKey: true, autoincrement: true },
|
|
105
|
+
* userId: {
|
|
106
|
+
* type: "INTEGER",
|
|
107
|
+
* notNull: true,
|
|
108
|
+
* references: { table: "User", column: "id" },
|
|
109
|
+
* onDelete: "CASCADE"
|
|
110
|
+
* },
|
|
111
|
+
* title: { type: "TEXT", notNull: true }
|
|
112
|
+
* },
|
|
113
|
+
* indexes: [{ columns: ["userId"] }]
|
|
114
|
+
* });
|
|
115
|
+
*/
|
|
116
|
+
createTable(table: {
|
|
117
|
+
name: string;
|
|
118
|
+
columns?: Record<string, {
|
|
119
|
+
type: "INTEGER" | "TEXT" | "REAL" | "BLOB" | "NUMERIC";
|
|
120
|
+
primaryKey?: boolean;
|
|
121
|
+
autoincrement?: boolean;
|
|
122
|
+
notNull?: boolean;
|
|
123
|
+
unique?: boolean;
|
|
124
|
+
defaultValue?: any;
|
|
125
|
+
values?: any[];
|
|
126
|
+
references?: {
|
|
127
|
+
table: string;
|
|
128
|
+
column: string;
|
|
129
|
+
};
|
|
130
|
+
onDelete?: string;
|
|
131
|
+
onUpdate?: string;
|
|
132
|
+
}>;
|
|
133
|
+
options?: Record<string, any>;
|
|
134
|
+
constraints?: Array<{
|
|
135
|
+
type: "unique";
|
|
136
|
+
columns: string[];
|
|
137
|
+
} | {
|
|
138
|
+
type: "primaryKey";
|
|
139
|
+
columns: string[];
|
|
140
|
+
} | {
|
|
141
|
+
type: "check";
|
|
142
|
+
expression: string;
|
|
143
|
+
} | {
|
|
144
|
+
type: "foreignKey";
|
|
145
|
+
columns: string[];
|
|
146
|
+
references: {
|
|
147
|
+
table: string;
|
|
148
|
+
columns: string[];
|
|
149
|
+
};
|
|
150
|
+
onDelete?: string;
|
|
151
|
+
onUpdate?: string;
|
|
152
|
+
}>;
|
|
153
|
+
indexes?: Array<{
|
|
154
|
+
name?: string;
|
|
155
|
+
columns: string[];
|
|
156
|
+
unique?: boolean;
|
|
157
|
+
}>;
|
|
158
|
+
}): Promise<{
|
|
159
|
+
ok: true;
|
|
160
|
+
} | {
|
|
161
|
+
error: string;
|
|
162
|
+
}>;
|
|
163
|
+
/**
|
|
164
|
+
* Creates an index on an existing table.
|
|
165
|
+
*
|
|
166
|
+
* Composite indexes are supported by passing multiple columns.
|
|
167
|
+
*
|
|
168
|
+
* @param {string} tableName - Table name.
|
|
169
|
+
* @param {{
|
|
170
|
+
* name?: string,
|
|
171
|
+
* columns: string[],
|
|
172
|
+
* unique?: boolean
|
|
173
|
+
* }} index - Index definition.
|
|
174
|
+
* @returns {Promise<{ ok: true } | { error: string }>}
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* await db.createIndex("UserAuthMethod", {
|
|
178
|
+
* columns: ["provider", "providerUserId"],
|
|
179
|
+
* unique: true
|
|
180
|
+
* });
|
|
181
|
+
*/
|
|
182
|
+
createIndex(tableName: string, index: {
|
|
183
|
+
name?: string;
|
|
184
|
+
columns: string[];
|
|
185
|
+
unique?: boolean;
|
|
186
|
+
}): Promise<{
|
|
187
|
+
ok: true;
|
|
188
|
+
} | {
|
|
189
|
+
error: string;
|
|
190
|
+
}>;
|
|
191
|
+
/**
|
|
192
|
+
* Drops a table and removes its model definition.
|
|
193
|
+
*
|
|
194
|
+
* @param {string} tableName - Table name.
|
|
195
|
+
* @returns {Promise<{ ok: true } | { error: string }>}
|
|
196
|
+
*/
|
|
197
|
+
deleteTable(tableName: string): Promise<{
|
|
198
|
+
ok: true;
|
|
199
|
+
} | {
|
|
200
|
+
error: string;
|
|
201
|
+
}>;
|
|
202
|
+
/**
|
|
203
|
+
* Deletes all rows from a table.
|
|
204
|
+
*
|
|
205
|
+
* @param {string} tableName - Table name.
|
|
206
|
+
* @returns {Promise<{ ok: true } | { error: string }>}
|
|
207
|
+
*/
|
|
208
|
+
clearTable(tableName: string): Promise<{
|
|
209
|
+
ok: true;
|
|
210
|
+
} | {
|
|
211
|
+
error: string;
|
|
212
|
+
}>;
|
|
213
|
+
/**
|
|
214
|
+
* Returns a single row matching a plain-object WHERE clause.
|
|
215
|
+
*
|
|
216
|
+
* @param {string} tableName - Table name.
|
|
217
|
+
* @param {Record<string, any>} [where={}] - Conditions.
|
|
218
|
+
* @returns {Promise<object|undefined|{ error: string }>}
|
|
219
|
+
*/
|
|
220
|
+
get(tableName: string, where?: Record<string, any>): Promise<object | undefined | {
|
|
221
|
+
error: string;
|
|
222
|
+
}>;
|
|
223
|
+
/**
|
|
224
|
+
* Returns multiple rows from a table.
|
|
225
|
+
*
|
|
226
|
+
* Supports:
|
|
227
|
+
* - exact-match filters via `where`
|
|
228
|
+
* - custom ordering
|
|
229
|
+
* - pagination via `limit` and `offset`
|
|
230
|
+
*
|
|
231
|
+
* @param {string} tableName - Table name.
|
|
232
|
+
* @param {Record<string, any>} [where={}] - Exact-match conditions.
|
|
233
|
+
* @param {{
|
|
234
|
+
* limit?: number,
|
|
235
|
+
* offset?: number,
|
|
236
|
+
* orderBy?: string,
|
|
237
|
+
* direction?: "ASC"|"DESC"
|
|
238
|
+
* }} [options={}] - Query options.
|
|
239
|
+
* @returns {Promise<object[] | { error: string }>}
|
|
240
|
+
*
|
|
241
|
+
* @example
|
|
242
|
+
* const posts = await db.getAll("Post", { userId: 1 }, {
|
|
243
|
+
* limit: 20,
|
|
244
|
+
* offset: 0,
|
|
245
|
+
* orderBy: "createdAt",
|
|
246
|
+
* direction: "DESC"
|
|
247
|
+
* });
|
|
248
|
+
*/
|
|
249
|
+
getAll(tableName: string, where?: Record<string, any>, options?: {
|
|
250
|
+
limit?: number;
|
|
251
|
+
offset?: number;
|
|
252
|
+
orderBy?: string;
|
|
253
|
+
direction?: "ASC" | "DESC";
|
|
254
|
+
}): Promise<object[] | {
|
|
255
|
+
error: string;
|
|
256
|
+
}>;
|
|
257
|
+
/**
|
|
258
|
+
* Inserts a new row into a table.
|
|
259
|
+
*
|
|
260
|
+
* This method performs a plain `INSERT`.
|
|
261
|
+
* If a unique constraint is violated, SQLite will throw an error.
|
|
262
|
+
*
|
|
263
|
+
* @param {string} tableName - Table name.
|
|
264
|
+
* @param {Record<string, any>} data - Row to insert.
|
|
265
|
+
* @returns {Promise<{ ok: true, lastInsertRowid: bigint|number, changes: number } | { error: string }>}
|
|
266
|
+
*/
|
|
267
|
+
insert(tableName: string, data: Record<string, any>): Promise<{
|
|
268
|
+
ok: true;
|
|
269
|
+
lastInsertRowid: bigint | number;
|
|
270
|
+
changes: number;
|
|
271
|
+
} | {
|
|
272
|
+
error: string;
|
|
273
|
+
}>;
|
|
274
|
+
/**
|
|
275
|
+
* Updates one or more rows matching a WHERE clause.
|
|
276
|
+
*
|
|
277
|
+
* This method performs a plain `UPDATE`.
|
|
278
|
+
*
|
|
279
|
+
* @param {string} tableName - Table name.
|
|
280
|
+
* @param {Record<string, any>} data - Fields to update.
|
|
281
|
+
* @param {Record<string, any>} where - Conditions used to select rows.
|
|
282
|
+
* @returns {Promise<{ ok: true, changes: number } | { error: string }>}
|
|
283
|
+
*/
|
|
284
|
+
update(tableName: string, data: Record<string, any>, where: Record<string, any>): Promise<{
|
|
285
|
+
ok: true;
|
|
286
|
+
changes: number;
|
|
287
|
+
} | {
|
|
288
|
+
error: string;
|
|
289
|
+
}>;
|
|
290
|
+
/**
|
|
291
|
+
* Inserts or updates a row using SQLite `ON CONFLICT DO UPDATE`.
|
|
292
|
+
*
|
|
293
|
+
* This is safer than `INSERT OR REPLACE` because it does not delete
|
|
294
|
+
* and recreate the row.
|
|
295
|
+
*
|
|
296
|
+
* @param {string} tableName - Table name.
|
|
297
|
+
* @param {Record<string, any>} data - Row data.
|
|
298
|
+
* @param {string[]} conflict - Columns that define the conflict target.
|
|
299
|
+
* @returns {Promise<{ ok: true, changes: number, lastInsertRowid: bigint|number } | { error: string }>}
|
|
300
|
+
*
|
|
301
|
+
* @example
|
|
302
|
+
* await db.upsert("UserAuthMethod", {
|
|
303
|
+
* userId: 1,
|
|
304
|
+
* provider: "google",
|
|
305
|
+
* providerUserId: "123"
|
|
306
|
+
* }, ["provider", "providerUserId"]);
|
|
307
|
+
*/
|
|
308
|
+
upsert(tableName: string, data: Record<string, any>, conflict?: string[]): Promise<{
|
|
309
|
+
ok: true;
|
|
310
|
+
changes: number;
|
|
311
|
+
lastInsertRowid: bigint | number;
|
|
312
|
+
} | {
|
|
313
|
+
error: string;
|
|
314
|
+
}>;
|
|
315
|
+
/**
|
|
316
|
+
* Backward-compatible helper that inserts or updates a row.
|
|
317
|
+
*
|
|
318
|
+
* Behavior:
|
|
319
|
+
* - If `where` is provided, the existing row is loaded and merged with `data`,
|
|
320
|
+
* then updated if found or inserted if not found.
|
|
321
|
+
* - If `where` is not provided but `data.id` exists, lookup is performed on `id`.
|
|
322
|
+
* - If no existing row is found, a new row is inserted.
|
|
323
|
+
*
|
|
324
|
+
* This method is intentionally simple, but for production-grade control,
|
|
325
|
+
* prefer using `insert()`, `update()`, or `upsert()`.
|
|
326
|
+
*
|
|
327
|
+
* @param {string} tableName - Table name.
|
|
328
|
+
* @param {Record<string, any>} data - Row data.
|
|
329
|
+
* @param {Record<string, any>|null} [where=null] - Lookup condition.
|
|
330
|
+
* @returns {Promise<{ ok: true } | { error: string }>}
|
|
331
|
+
*/
|
|
332
|
+
set(tableName: string, data: Record<string, any>, where?: Record<string, any> | null): Promise<{
|
|
333
|
+
ok: true;
|
|
334
|
+
} | {
|
|
335
|
+
error: string;
|
|
336
|
+
}>;
|
|
337
|
+
/**
|
|
338
|
+
* Deletes rows matching a WHERE clause.
|
|
339
|
+
*
|
|
340
|
+
* @param {string} tableName - Table name.
|
|
341
|
+
* @param {Record<string, any>} where - Conditions.
|
|
342
|
+
* @returns {Promise<{ ok: true, changes: number } | { error: string }>}
|
|
343
|
+
*/
|
|
344
|
+
delete(tableName: string, where: Record<string, any>): Promise<{
|
|
345
|
+
ok: true;
|
|
346
|
+
changes: number;
|
|
347
|
+
} | {
|
|
348
|
+
error: string;
|
|
349
|
+
}>;
|
|
350
|
+
/**
|
|
351
|
+
* Counts rows matching a WHERE clause.
|
|
352
|
+
*
|
|
353
|
+
* @param {string} tableName - Table name.
|
|
354
|
+
* @param {Record<string, any>} [where={}] - Conditions.
|
|
355
|
+
* @returns {Promise<number | { error: string }>}
|
|
356
|
+
*/
|
|
357
|
+
count(tableName: string, where?: Record<string, any>): Promise<number | {
|
|
358
|
+
error: string;
|
|
359
|
+
}>;
|
|
360
|
+
/**
|
|
361
|
+
* Runs multiple operations inside a transaction.
|
|
362
|
+
*
|
|
363
|
+
* If the callback throws, the transaction is rolled back.
|
|
364
|
+
* If the callback completes successfully, the transaction is committed.
|
|
365
|
+
*
|
|
366
|
+
* The callback may be synchronous or asynchronous.
|
|
367
|
+
*
|
|
368
|
+
* @template T
|
|
369
|
+
* @param {() => T|Promise<T>} callback - Function to execute inside the transaction.
|
|
370
|
+
* @returns {Promise<T | { error: string }>}
|
|
371
|
+
*
|
|
372
|
+
* @example
|
|
373
|
+
* const result = await db.transaction(async () => {
|
|
374
|
+
* await db.insert("User", { email: "a@b.com", createdAt: Date.now() });
|
|
375
|
+
* await db.insert("Profile", { userId: 1, nickname: "John" });
|
|
376
|
+
* return { ok: true };
|
|
377
|
+
* });
|
|
378
|
+
*/
|
|
379
|
+
transaction<T>(callback: () => T | Promise<T>): Promise<T | {
|
|
380
|
+
error: string;
|
|
381
|
+
}>;
|
|
382
|
+
/**
|
|
383
|
+
* Closes the underlying SQLite connection.
|
|
384
|
+
*
|
|
385
|
+
* @example
|
|
386
|
+
* db.close();
|
|
387
|
+
*/
|
|
388
|
+
close(): void;
|
|
389
|
+
#private;
|
|
390
|
+
}
|
|
391
|
+
//# sourceMappingURL=SQLite.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SQLite.d.ts","sourceRoot":"","sources":["../../src/SQLite.js"],"names":[],"mappings":";AA0DiB;IAef;;;;;;;;;;;;;OAaG;IACH,mBARW,MAAM,EAmBhB;IA8TD;;;;;;;;;;;;;;OAcG;IACH,UAJW,MAAM,WACN,GAAG,EAAE,GAAC,GAAG,GACP,OAAO,CAAC;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,eAAe,EAAE,MAAM,GAAC,MAAM,CAAA;KAAE,GAAG;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC,CAQ5F;IAED;;;;;;OAMG;IACH,cAJW,MAAM,WACN,GAAG,EAAE,GAAC,GAAG,GACP,OAAO,CAAC,MAAM,GAAC,SAAS,GAAC;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC,CAUvD;IAED;;;;;;OAMG;IACH,cAJW,MAAM,WACN,GAAG,EAAE,GAAC,GAAG,GACP,OAAO,CAAC,MAAM,EAAE,GAAC;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC,CAS/C;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAwDG;IACH,mBAnDW;QACN,IA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,OAAO,EAAE,MAAM,CAAA;KAAE,GAAG;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC,CAsBtE;IAED;;;;;;OAMG;IACH,iBAJW,MAAM,UACN,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GACjB,OAAO,CAAC,MAAM,GAAG;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC,CAmB/C;IAED;;;;;;;;;;;;;;;;;;OAkBG;IACH,YAXa,CAAC,YACH,MAAM,CAAC,GAAC,OAAO,CAAC,CAAC,CAAC,GAChB,OAAO,CAAC,CAAC,GAAG;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC,CAwB1C;IAED;;;;;OAKG;IACH,cAIC;;CACF"}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Waits for a given number of milliseconds.
|
|
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.
|
|
9
|
+
*/
|
|
10
|
+
export function wait(ms: number): Promise<void>;
|
|
11
|
+
/**
|
|
12
|
+
* Polls a predicate at a given interval and emits events when it matches or times out.
|
|
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`.
|
|
67
|
+
*/
|
|
68
|
+
export function when(predicate: boolean | Promise<boolean> | (() => boolean | Promise<boolean>), payload?: any, options?: {
|
|
69
|
+
interval?: number | undefined;
|
|
70
|
+
timeout?: number | null | undefined;
|
|
71
|
+
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
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* Installs process-level safety nets for errors, exits and signals.
|
|
79
|
+
*
|
|
80
|
+
* By default, it:
|
|
81
|
+
* - logs uncaught exceptions and unhandled rejections,
|
|
82
|
+
* - logs process exit codes,
|
|
83
|
+
* - logs SIGINT/SIGTERM/SIGQUIT and exits with code 0.
|
|
84
|
+
*
|
|
85
|
+
* You can override each handler with `.on(event, handler)`.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* // Use defaults:
|
|
89
|
+
* nodeComfort.dontCrash();
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* // Custom error handler:
|
|
93
|
+
* nodeComfort.dontCrash().on("error", (err) => {
|
|
94
|
+
* console.error("Global error:", err);
|
|
95
|
+
* process.exit(1);
|
|
96
|
+
* });
|
|
97
|
+
*
|
|
98
|
+
* @returns {{
|
|
99
|
+
* on: <E extends "error"|"exit"|"sig"|"beforeExit">(event: E, handler?: (
|
|
100
|
+
* E extends "error" ? (error: Error|unknown) => void|Promise<void> :
|
|
101
|
+
* E extends "exit" ? (code: number) => void|Promise<void> :
|
|
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.
|
|
106
|
+
*/
|
|
107
|
+
export function dontCrash(): {
|
|
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
|
+
};
|
|
110
|
+
/**
|
|
111
|
+
* Safely stringifies a value to pretty-printed JSON (4 spaces indent).
|
|
112
|
+
*
|
|
113
|
+
* @param {*} value - Value to stringify.
|
|
114
|
+
* @returns {string} JSON string representation.
|
|
115
|
+
*/
|
|
116
|
+
export function JSONString(value: any): string;
|
|
117
|
+
/**
|
|
118
|
+
* Parses a JSON string.
|
|
119
|
+
*
|
|
120
|
+
* @param {string} value - JSON string to parse.
|
|
121
|
+
* @returns {*} Parsed value.
|
|
122
|
+
*/
|
|
123
|
+
export function JSONParse(value: string): any;
|
|
124
|
+
//# sourceMappingURL=Utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Utils.d.ts","sourceRoot":"","sources":["../../src/Utils.js"],"names":[],"mappings":"AAME;;;;;;;;GAQG;AACH,yBAHW,MAAM,GACJ,OAAO,CAAC,IAAI,CAAC,CAIzB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwDG;AACH,gCA7CW,OAAO,GAAC,OAAO,CAAC,OAAO,CAAC,GAAC,CAAC,MAAM,OAAO,GAAC,OAAO,CAAC,OAAO,CAAC,CAAC,YAEzD,GAAC,YAGT;IAAyB,QAAQ;IAGH,OAAO;IAGP,GAAG;CAIjC,GAsBU;IACR,KAAK,EAAE,MAAM,GAAG,CAAC;IACjB,IAAI,EAAE,MAAM,GAAG,CAAC;IAChB,EAAE,EAAE,CAAC,CAAC,SAAS,OAAO,GAAC,SAAS,GAAC,SAAS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