@isdk/util 0.3.2 → 0.3.3

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 (55) hide show
  1. package/dist/index.d.mts +464 -1
  2. package/dist/index.d.ts +464 -1
  3. package/dist/index.js +1 -1
  4. package/dist/index.mjs +1 -1
  5. package/docs/classes/BinarySemaphore.md +418 -0
  6. package/docs/classes/ConfigFile.md +6 -6
  7. package/docs/classes/Deque.md +1912 -0
  8. package/docs/classes/IntSet.md +216 -0
  9. package/docs/classes/Semaphore.md +538 -0
  10. package/docs/classes/SignalGate.md +184 -0
  11. package/docs/functions/RateLimit.md +37 -0
  12. package/docs/functions/arrayHasAll.md +1 -1
  13. package/docs/functions/extNameLevel.md +1 -1
  14. package/docs/functions/filenameReservedRegex.md +1 -1
  15. package/docs/functions/findPort.md +25 -0
  16. package/docs/functions/getMultiLevelExtname.md +1 -1
  17. package/docs/functions/glob.md +1 -1
  18. package/docs/functions/isStringIn.md +1 -1
  19. package/docs/functions/isValidFilename.md +1 -1
  20. package/docs/functions/isValidFilepath.md +1 -1
  21. package/docs/functions/normalizeIncludeFiles.md +1 -1
  22. package/docs/functions/parseFrontMatter.md +1 -1
  23. package/docs/functions/parseYaml.md +1 -1
  24. package/docs/functions/reControlCharsRegex.md +1 -1
  25. package/docs/functions/registerYamlTag.md +2 -2
  26. package/docs/functions/removeLeadingEmptyLines.md +1 -1
  27. package/docs/functions/sanitizeFilename.md +1 -1
  28. package/docs/functions/sanitizeFilepath.md +1 -1
  29. package/docs/functions/sleep.md +36 -0
  30. package/docs/functions/stringifyYaml.md +1 -1
  31. package/docs/functions/toCamelCase.md +1 -1
  32. package/docs/functions/toCapitalCase.md +1 -1
  33. package/docs/functions/toPascalCase.md +1 -1
  34. package/docs/functions/traverseFolder.md +1 -1
  35. package/docs/functions/traverseFolderSync.md +1 -1
  36. package/docs/functions/yieldExec.md +27 -0
  37. package/docs/globals.md +17 -0
  38. package/docs/interfaces/BinarySemaphoreAcquireOptions.md +25 -0
  39. package/docs/interfaces/BinarySemaphoreOptions.md +57 -0
  40. package/docs/interfaces/BinarySemaphoreReleaseOptions.md +25 -0
  41. package/docs/interfaces/BinarySemaphoreReleaserFunc.md +37 -0
  42. package/docs/interfaces/IncludeFiles.md +3 -3
  43. package/docs/interfaces/LoadConfigFileOptions.md +3 -3
  44. package/docs/interfaces/SanitizeFilenameOptions.md +3 -3
  45. package/docs/interfaces/SemaphoreOptions.md +89 -0
  46. package/docs/interfaces/SemaphoreTaskItem.md +73 -0
  47. package/docs/type-aliases/SemaphoreIsReadyFuncType.md +15 -0
  48. package/docs/type-aliases/StringifyFunc.md +1 -1
  49. package/docs/type-aliases/TraverseFolderHandler.md +1 -1
  50. package/docs/type-aliases/TraverseFolderSyncHandler.md +1 -1
  51. package/docs/variables/DefaultAllTextFiles.md +1 -1
  52. package/docs/variables/DefaultAsyncSemaphoreCapacity.md +11 -0
  53. package/docs/variables/FilenameReservedRegex.md +1 -1
  54. package/docs/variables/WindowsReservedNameRegex.md +1 -1
  55. package/package.json +3 -1
@@ -0,0 +1,418 @@
1
+ [**@isdk/util**](../README.md)
2
+
3
+ ***
4
+
5
+ [@isdk/util](../globals.md) / BinarySemaphore
6
+
7
+ # Class: BinarySemaphore
8
+
9
+ Defined in: [src/async-semaphore.ts:88](https://github.com/isdk/util.js/blob/f6ac1e1b241d01211870dd55d000c1e9d4daa404/src/async-semaphore.ts#L88)
10
+
11
+ A binary semaphore implementation for managing concurrency in asynchronous operations.
12
+ Unlike a general semaphore, a binary semaphore allows only one caller to acquire the semaphore at a time.
13
+ It provides methods to acquire, release, and manage waiting tasks efficiently.
14
+
15
+ Example usage:
16
+
17
+ ```typescript
18
+ const semaphore = new Semaphore(5); // Allows 5 concurrent operations.
19
+
20
+ const semaphore = new Semaphore(
21
+ 4, // Allow 4 concurrent async calls
22
+ {
23
+ capacity: 100 // Prealloc space for 100 tokens
24
+ }
25
+ );
26
+
27
+ async function fetchData(x) {
28
+ await semaphore.acquire()
29
+ try {
30
+ console.log(semaphore.pendingCount + ' calls to fetch are waiting')
31
+ // ... do some async stuff with x
32
+ } finally {
33
+ semaphore.release();
34
+ }
35
+ }
36
+
37
+ const data = await Promise.all(array.map(fetchData));
38
+ ```
39
+
40
+ ## Extended by
41
+
42
+ - [`Semaphore`](Semaphore.md)
43
+
44
+ ## Constructors
45
+
46
+ ### Constructor
47
+
48
+ > **new BinarySemaphore**(`options`): `BinarySemaphore`
49
+
50
+ Defined in: [src/async-semaphore.ts:145](https://github.com/isdk/util.js/blob/f6ac1e1b241d01211870dd55d000c1e9d4daa404/src/async-semaphore.ts#L145)
51
+
52
+ Creates a binary semaphore object for managing concurrency in asynchronous operations.
53
+
54
+ #### Parameters
55
+
56
+ ##### options
57
+
58
+ [`BinarySemaphoreOptions`](../interfaces/BinarySemaphoreOptions.md) = `{}`
59
+
60
+ #### Returns
61
+
62
+ `BinarySemaphore`
63
+
64
+ ## Properties
65
+
66
+ ### \_activeCount
67
+
68
+ > `protected` **\_activeCount**: `number`
69
+
70
+ Defined in: [src/async-semaphore.ts:97](https://github.com/isdk/util.js/blob/f6ac1e1b241d01211870dd55d000c1e9d4daa404/src/async-semaphore.ts#L97)
71
+
72
+ ***
73
+
74
+ ### emitter
75
+
76
+ > `protected` **emitter**: `EventEmitter`
77
+
78
+ Defined in: [src/async-semaphore.ts:91](https://github.com/isdk/util.js/blob/f6ac1e1b241d01211870dd55d000c1e9d4daa404/src/async-semaphore.ts#L91)
79
+
80
+ ***
81
+
82
+ ### free
83
+
84
+ > `protected` **free**: `any`
85
+
86
+ Defined in: [src/async-semaphore.ts:90](https://github.com/isdk/util.js/blob/f6ac1e1b241d01211870dd55d000c1e9d4daa404/src/async-semaphore.ts#L90)
87
+
88
+ ***
89
+
90
+ ### initTokenFn()
91
+
92
+ > `protected` **initTokenFn**: (`token`?) => `void`
93
+
94
+ Defined in: [src/async-semaphore.ts:95](https://github.com/isdk/util.js/blob/f6ac1e1b241d01211870dd55d000c1e9d4daa404/src/async-semaphore.ts#L95)
95
+
96
+ #### Parameters
97
+
98
+ ##### token?
99
+
100
+ `any`
101
+
102
+ #### Returns
103
+
104
+ `void`
105
+
106
+ ***
107
+
108
+ ### paused
109
+
110
+ > `protected` **paused**: `boolean`
111
+
112
+ Defined in: [src/async-semaphore.ts:96](https://github.com/isdk/util.js/blob/f6ac1e1b241d01211870dd55d000c1e9d4daa404/src/async-semaphore.ts#L96)
113
+
114
+ ***
115
+
116
+ ### pauseFn()?
117
+
118
+ > `protected` `optional` **pauseFn**: () => `void`
119
+
120
+ Defined in: [src/async-semaphore.ts:93](https://github.com/isdk/util.js/blob/f6ac1e1b241d01211870dd55d000c1e9d4daa404/src/async-semaphore.ts#L93)
121
+
122
+ #### Returns
123
+
124
+ `void`
125
+
126
+ ***
127
+
128
+ ### resumeFn()?
129
+
130
+ > `protected` `optional` **resumeFn**: () => `void`
131
+
132
+ Defined in: [src/async-semaphore.ts:94](https://github.com/isdk/util.js/blob/f6ac1e1b241d01211870dd55d000c1e9d4daa404/src/async-semaphore.ts#L94)
133
+
134
+ #### Returns
135
+
136
+ `void`
137
+
138
+ ***
139
+
140
+ ### useDefaultTokens
141
+
142
+ > `protected` **useDefaultTokens**: `boolean`
143
+
144
+ Defined in: [src/async-semaphore.ts:92](https://github.com/isdk/util.js/blob/f6ac1e1b241d01211870dd55d000c1e9d4daa404/src/async-semaphore.ts#L92)
145
+
146
+ ***
147
+
148
+ ### waiting
149
+
150
+ > `readonly` **waiting**: [`Deque`](Deque.md)\<`undefined` \| [`SemaphoreTaskItem`](../interfaces/SemaphoreTaskItem.md)\>
151
+
152
+ Defined in: [src/async-semaphore.ts:89](https://github.com/isdk/util.js/blob/f6ac1e1b241d01211870dd55d000c1e9d4daa404/src/async-semaphore.ts#L89)
153
+
154
+ ## Accessors
155
+
156
+ ### activeCount
157
+
158
+ #### Get Signature
159
+
160
+ > **get** **activeCount**(): `number`
161
+
162
+ Defined in: [src/async-semaphore.ts:318](https://github.com/isdk/util.js/blob/f6ac1e1b241d01211870dd55d000c1e9d4daa404/src/async-semaphore.ts#L318)
163
+
164
+ Get the total count of all active operations.
165
+
166
+ This method returns the number of operations that are either:
167
+ - Waiting in the queue to acquire the semaphore (`pendingCount`).
168
+ - Already acquired the semaphore but have not yet released it.
169
+
170
+ ##### Returns
171
+
172
+ `number`
173
+
174
+ The total count of active operations, including both waiting and ongoing tasks.
175
+
176
+ ***
177
+
178
+ ### pendingCount
179
+
180
+ #### Get Signature
181
+
182
+ > **get** **pendingCount**(): `number`
183
+
184
+ Defined in: [src/async-semaphore.ts:327](https://github.com/isdk/util.js/blob/f6ac1e1b241d01211870dd55d000c1e9d4daa404/src/async-semaphore.ts#L327)
185
+
186
+ Get the number of callers waiting on the semaphore, i.e. the number of pending promises.
187
+
188
+ ##### Returns
189
+
190
+ `number`
191
+
192
+ The number of waiters in the waiting list.
193
+
194
+ ## Methods
195
+
196
+ ### \_dispatchTask()
197
+
198
+ > **\_dispatchTask**(`task`, `options`?): `void`
199
+
200
+ Defined in: [src/async-semaphore.ts:212](https://github.com/isdk/util.js/blob/f6ac1e1b241d01211870dd55d000c1e9d4daa404/src/async-semaphore.ts#L212)
201
+
202
+ #### Parameters
203
+
204
+ ##### task
205
+
206
+ [`SemaphoreTaskItem`](../interfaces/SemaphoreTaskItem.md)
207
+
208
+ ##### options?
209
+
210
+ [`BinarySemaphoreReleaseOptions`](../interfaces/BinarySemaphoreReleaseOptions.md)
211
+
212
+ #### Returns
213
+
214
+ `void`
215
+
216
+ ***
217
+
218
+ ### \_newReleaser()
219
+
220
+ > **\_newReleaser**(`options`?): [`BinarySemaphoreReleaserFunc`](../interfaces/BinarySemaphoreReleaserFunc.md)
221
+
222
+ Defined in: [src/async-semaphore.ts:199](https://github.com/isdk/util.js/blob/f6ac1e1b241d01211870dd55d000c1e9d4daa404/src/async-semaphore.ts#L199)
223
+
224
+ #### Parameters
225
+
226
+ ##### options?
227
+
228
+ [`BinarySemaphoreReleaseOptions`](../interfaces/BinarySemaphoreReleaseOptions.md)
229
+
230
+ #### Returns
231
+
232
+ [`BinarySemaphoreReleaserFunc`](../interfaces/BinarySemaphoreReleaserFunc.md)
233
+
234
+ ***
235
+
236
+ ### abort()
237
+
238
+ > **abort**(`reason`?): `void`
239
+
240
+ Defined in: [src/async-semaphore.ts:301](https://github.com/isdk/util.js/blob/f6ac1e1b241d01211870dd55d000c1e9d4daa404/src/async-semaphore.ts#L301)
241
+
242
+ #### Parameters
243
+
244
+ ##### reason?
245
+
246
+ `any`
247
+
248
+ #### Returns
249
+
250
+ `void`
251
+
252
+ ***
253
+
254
+ ### acquire()
255
+
256
+ > **acquire**(`options`?): `Promise`\<[`BinarySemaphoreReleaserFunc`](../interfaces/BinarySemaphoreReleaserFunc.md)\>
257
+
258
+ Defined in: [src/async-semaphore.ts:243](https://github.com/isdk/util.js/blob/f6ac1e1b241d01211870dd55d000c1e9d4daa404/src/async-semaphore.ts#L243)
259
+
260
+ Acquire a token from the semaphore, thus decrement the number of available execution slots. If initFn is not used then the return value of the function can be discarded.
261
+
262
+ #### Parameters
263
+
264
+ ##### options?
265
+
266
+ [`BinarySemaphoreAcquireOptions`](../interfaces/BinarySemaphoreAcquireOptions.md)
267
+
268
+ #### Returns
269
+
270
+ `Promise`\<[`BinarySemaphoreReleaserFunc`](../interfaces/BinarySemaphoreReleaserFunc.md)\>
271
+
272
+ A promise that resolves to a release function when a token is acquired. If the semaphore is full, the caller will be added to a waiting queue.
273
+
274
+ ***
275
+
276
+ ### drain()
277
+
278
+ > **drain**(): `Promise`\<`any`[]\>
279
+
280
+ Defined in: [src/async-semaphore.ts:296](https://github.com/isdk/util.js/blob/f6ac1e1b241d01211870dd55d000c1e9d4daa404/src/async-semaphore.ts#L296)
281
+
282
+ Drains the semaphore and returns all the initialized tokens in an array. Draining is an ideal way to ensure there are no pending async tasks, for example before a process will terminate.
283
+
284
+ #### Returns
285
+
286
+ `Promise`\<`any`[]\>
287
+
288
+ ***
289
+
290
+ ### init()
291
+
292
+ > **init**(`options`): `void`
293
+
294
+ Defined in: [src/async-semaphore.ts:193](https://github.com/isdk/util.js/blob/f6ac1e1b241d01211870dd55d000c1e9d4daa404/src/async-semaphore.ts#L193)
295
+
296
+ #### Parameters
297
+
298
+ ##### options
299
+
300
+ [`BinarySemaphoreOptions`](../interfaces/BinarySemaphoreOptions.md)
301
+
302
+ #### Returns
303
+
304
+ `void`
305
+
306
+ ***
307
+
308
+ ### initFree()
309
+
310
+ > **initFree**(`options`?): `void`
311
+
312
+ Defined in: [src/async-semaphore.ts:173](https://github.com/isdk/util.js/blob/f6ac1e1b241d01211870dd55d000c1e9d4daa404/src/async-semaphore.ts#L173)
313
+
314
+ #### Parameters
315
+
316
+ ##### options?
317
+
318
+ [`BinarySemaphoreOptions`](../interfaces/BinarySemaphoreOptions.md)
319
+
320
+ #### Returns
321
+
322
+ `void`
323
+
324
+ ***
325
+
326
+ ### lock()
327
+
328
+ > **lock**(`options`?): `any`
329
+
330
+ Defined in: [src/async-semaphore.ts:217](https://github.com/isdk/util.js/blob/f6ac1e1b241d01211870dd55d000c1e9d4daa404/src/async-semaphore.ts#L217)
331
+
332
+ #### Parameters
333
+
334
+ ##### options?
335
+
336
+ [`BinarySemaphoreAcquireOptions`](../interfaces/BinarySemaphoreAcquireOptions.md)
337
+
338
+ #### Returns
339
+
340
+ `any`
341
+
342
+ ***
343
+
344
+ ### onReleased()
345
+
346
+ > **onReleased**(`options`?): `void`
347
+
348
+ Defined in: [src/async-semaphore.ts:177](https://github.com/isdk/util.js/blob/f6ac1e1b241d01211870dd55d000c1e9d4daa404/src/async-semaphore.ts#L177)
349
+
350
+ #### Parameters
351
+
352
+ ##### options?
353
+
354
+ [`BinarySemaphoreReleaseOptions`](../interfaces/BinarySemaphoreReleaseOptions.md)
355
+
356
+ #### Returns
357
+
358
+ `void`
359
+
360
+ ***
361
+
362
+ ### release()
363
+
364
+ > **release**(`options`?): `void`
365
+
366
+ Defined in: [src/async-semaphore.ts:288](https://github.com/isdk/util.js/blob/f6ac1e1b241d01211870dd55d000c1e9d4daa404/src/async-semaphore.ts#L288)
367
+
368
+ Releases the semaphore, incrementing the number of free execution slots. If there are tasks in the waiting queue, the next task will be dispatched.
369
+
370
+ #### Parameters
371
+
372
+ ##### options?
373
+
374
+ [`BinarySemaphoreReleaseOptions`](../interfaces/BinarySemaphoreReleaseOptions.md)
375
+
376
+ #### Returns
377
+
378
+ `void`
379
+
380
+ ***
381
+
382
+ ### tryAcquire()
383
+
384
+ > **tryAcquire**(`options`?): `any`
385
+
386
+ Defined in: [src/async-semaphore.ts:234](https://github.com/isdk/util.js/blob/f6ac1e1b241d01211870dd55d000c1e9d4daa404/src/async-semaphore.ts#L234)
387
+
388
+ Attempt to acquire a token from the semaphore, if one is available immediately. Otherwise, return undefined.
389
+
390
+ #### Parameters
391
+
392
+ ##### options?
393
+
394
+ [`BinarySemaphoreAcquireOptions`](../interfaces/BinarySemaphoreAcquireOptions.md)
395
+
396
+ #### Returns
397
+
398
+ `any`
399
+
400
+ Returns a token if the semaphore is not full; otherwise, returns `undefined`.
401
+
402
+ ***
403
+
404
+ ### unlock()
405
+
406
+ > **unlock**(`token`?): `void`
407
+
408
+ Defined in: [src/async-semaphore.ts:225](https://github.com/isdk/util.js/blob/f6ac1e1b241d01211870dd55d000c1e9d4daa404/src/async-semaphore.ts#L225)
409
+
410
+ #### Parameters
411
+
412
+ ##### token?
413
+
414
+ `any`
415
+
416
+ #### Returns
417
+
418
+ `void`
@@ -6,7 +6,7 @@
6
6
 
7
7
  # Class: ConfigFile
8
8
 
9
- Defined in: [config-file.ts:46](https://github.com/isdk/util.js/blob/e52ad0627fc33dea09d8db6ef431d619770364c0/src/config-file.ts#L46)
9
+ Defined in: [src/config-file.ts:46](https://github.com/isdk/util.js/blob/f6ac1e1b241d01211870dd55d000c1e9d4daa404/src/config-file.ts#L46)
10
10
 
11
11
  Represents a configuration file utility class that provides methods to load and save configuration files.
12
12
  It supports multiple file formats such as YAML, JSON, etc., by registering corresponding parsers and stringifiers.
@@ -45,7 +45,7 @@ console.log(config); // Output: { key: 'value' }
45
45
 
46
46
  > `static` **stringifys**: `Record`\<`string`, [`StringifyFunc`](../type-aliases/StringifyFunc.md)\> = `{}`
47
47
 
48
- Defined in: [config-file.ts:50](https://github.com/isdk/util.js/blob/e52ad0627fc33dea09d8db6ef431d619770364c0/src/config-file.ts#L50)
48
+ Defined in: [src/config-file.ts:50](https://github.com/isdk/util.js/blob/f6ac1e1b241d01211870dd55d000c1e9d4daa404/src/config-file.ts#L50)
49
49
 
50
50
  A record of registered stringify functions for different file extensions.
51
51
 
@@ -55,7 +55,7 @@ A record of registered stringify functions for different file extensions.
55
55
 
56
56
  > `static` **existsSync**(`filename`, `options`?): `boolean`
57
57
 
58
- Defined in: [config-file.ts:132](https://github.com/isdk/util.js/blob/e52ad0627fc33dea09d8db6ef431d619770364c0/src/config-file.ts#L132)
58
+ Defined in: [src/config-file.ts:132](https://github.com/isdk/util.js/blob/f6ac1e1b241d01211870dd55d000c1e9d4daa404/src/config-file.ts#L132)
59
59
 
60
60
  Checks if a configuration file exists at the specified path.
61
61
 
@@ -104,7 +104,7 @@ console.log(existsWithExt); // true or false
104
104
 
105
105
  > `static` **loadSync**(`filename`, `options`?): `any`
106
106
 
107
- Defined in: [config-file.ts:85](https://github.com/isdk/util.js/blob/e52ad0627fc33dea09d8db6ef431d619770364c0/src/config-file.ts#L85)
107
+ Defined in: [src/config-file.ts:85](https://github.com/isdk/util.js/blob/f6ac1e1b241d01211870dd55d000c1e9d4daa404/src/config-file.ts#L85)
108
108
 
109
109
  Loads a configuration file based on the provided filename and options.
110
110
 
@@ -141,7 +141,7 @@ console.log(config); // Output: { key: 'value' }
141
141
 
142
142
  > `static` **register**(`extname`, `parser`, `stringify`): `void`
143
143
 
144
- Defined in: [config-file.ts:64](https://github.com/isdk/util.js/blob/e52ad0627fc33dea09d8db6ef431d619770364c0/src/config-file.ts#L64)
144
+ Defined in: [src/config-file.ts:64](https://github.com/isdk/util.js/blob/f6ac1e1b241d01211870dd55d000c1e9d4daa404/src/config-file.ts#L64)
145
145
 
146
146
  Registers a parser and stringifier for specific file extensions.
147
147
 
@@ -181,7 +181,7 @@ ConfigFile.register(['.json'], JSON.parse, (obj) => JSON.stringify(obj, null, 2)
181
181
 
182
182
  > `static` **saveSync**(`filename`, `config`, `options`?): `string`
183
183
 
184
- Defined in: [config-file.ts:102](https://github.com/isdk/util.js/blob/e52ad0627fc33dea09d8db6ef431d619770364c0/src/config-file.ts#L102)
184
+ Defined in: [src/config-file.ts:102](https://github.com/isdk/util.js/blob/f6ac1e1b241d01211870dd55d000c1e9d4daa404/src/config-file.ts#L102)
185
185
 
186
186
  Saves a configuration object to a file with the specified filename and options.
187
187