@naturalcycles/nodejs-lib 15.130.1 → 15.132.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/dist/fs/fs2.d.ts +8 -8
- package/dist/fs/fs2.js +6 -8
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/secret/secrets-encrypt.util.js +4 -7
- package/dist/security/crypto.util.d.ts +6 -6
- package/dist/security/hash.util.d.ts +12 -12
- package/dist/security/hash.util.js +12 -4
- package/dist/stream/index.d.ts +0 -1
- package/dist/stream/index.js +0 -1
- package/dist/stream/pipeline.d.ts +2 -7
- package/dist/stream/pipeline.js +0 -8
- package/dist/stream/transform/transformChunk.d.ts +2 -2
- package/dist/stream/transform/transformChunk.js +1 -0
- package/dist/validation/timezones.js +2 -2
- package/dist/zip/zip2.d.ts +25 -25
- package/dist/zip/zip2.js +2 -2
- package/dist/zip/zipWriter.d.ts +3 -3
- package/dist/zip/zipWriter.js +1 -1
- package/package.json +2 -2
- package/src/fs/fs2.ts +12 -15
- package/src/index.ts +0 -1
- package/src/secret/secrets-encrypt.util.ts +4 -7
- package/src/security/crypto.util.ts +19 -7
- package/src/security/hash.util.ts +25 -16
- package/src/stream/index.ts +0 -1
- package/src/stream/pipeline.ts +2 -11
- package/src/stream/transform/transformChunk.ts +3 -2
- package/src/validation/timezones.ts +2 -2
- package/src/zip/zip2.ts +30 -28
- package/src/zip/zipWriter.ts +9 -5
- package/dist/buffer/buffer.util.d.ts +0 -4
- package/dist/buffer/buffer.util.js +0 -10
- package/dist/stream/transform/transformWarmup.d.ts +0 -24
- package/dist/stream/transform/transformWarmup.js +0 -79
- package/src/buffer/buffer.util.ts +0 -12
- package/src/stream/transform/transformWarmup.ts +0 -105
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
import { Transform } from 'node:stream'
|
|
2
|
-
import { createCommonLoggerAtLevel } from '@naturalcycles/js-lib/log'
|
|
3
|
-
import type { DeferredPromise } from '@naturalcycles/js-lib/promise'
|
|
4
|
-
import { pDefer } from '@naturalcycles/js-lib/promise/pDefer.js'
|
|
5
|
-
import type { NumberOfSeconds, PositiveInteger } from '@naturalcycles/js-lib/types'
|
|
6
|
-
import type { TransformOptions, TransformTyped } from '../stream.model.js'
|
|
7
|
-
|
|
8
|
-
export interface TransformWarmupOptions extends TransformOptions {
|
|
9
|
-
/**
|
|
10
|
-
* Target concurrency after warmup completes.
|
|
11
|
-
*/
|
|
12
|
-
concurrency: PositiveInteger
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Time in seconds to gradually increase concurrency from 1 to `concurrency`.
|
|
16
|
-
* Set to 0 to disable warmup (pass-through mode from the start).
|
|
17
|
-
*/
|
|
18
|
-
warmupSeconds: NumberOfSeconds
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Transform that gradually increases concurrency from 1 to the configured maximum
|
|
23
|
-
* over a warmup period. Useful for scenarios where you want to avoid overwhelming
|
|
24
|
-
* a system at startup (e.g., database connections, API rate limits).
|
|
25
|
-
*
|
|
26
|
-
* During warmup: limits concurrent items based on elapsed time.
|
|
27
|
-
* After warmup: passes items through immediately with zero overhead.
|
|
28
|
-
*
|
|
29
|
-
* @experimental
|
|
30
|
-
*/
|
|
31
|
-
export function transformWarmup<T>(opt: TransformWarmupOptions): TransformTyped<T, T> {
|
|
32
|
-
const { concurrency, warmupSeconds, objectMode = true, highWaterMark = 1 } = opt
|
|
33
|
-
const warmupMs = warmupSeconds * 1000
|
|
34
|
-
const logger = createCommonLoggerAtLevel(opt.logger, opt.logLevel)
|
|
35
|
-
|
|
36
|
-
let startTime = 0
|
|
37
|
-
let warmupComplete = warmupSeconds <= 0 || concurrency <= 1
|
|
38
|
-
let inFlight = 0
|
|
39
|
-
const waiters: DeferredPromise[] = []
|
|
40
|
-
|
|
41
|
-
return new Transform({
|
|
42
|
-
objectMode,
|
|
43
|
-
highWaterMark,
|
|
44
|
-
async transform(item: T, _, cb) {
|
|
45
|
-
// Initialize start time on first item
|
|
46
|
-
if (startTime === 0) {
|
|
47
|
-
startTime = Date.now()
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// Fast-path: after warmup, just pass through with zero overhead
|
|
51
|
-
if (warmupComplete) {
|
|
52
|
-
cb(null, item)
|
|
53
|
-
return
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const currentConcurrency = getCurrentConcurrency()
|
|
57
|
-
|
|
58
|
-
if (inFlight < currentConcurrency) {
|
|
59
|
-
// Have room, proceed immediately
|
|
60
|
-
inFlight++
|
|
61
|
-
logger.debug(`inFlight++ ${inFlight}/${currentConcurrency}, waiters ${waiters.length}`)
|
|
62
|
-
} else {
|
|
63
|
-
// Wait for a slot
|
|
64
|
-
const waiter = pDefer()
|
|
65
|
-
waiters.push(waiter)
|
|
66
|
-
logger.debug(`inFlight ${inFlight}/${currentConcurrency}, waiters++ ${waiters.length}`)
|
|
67
|
-
await waiter
|
|
68
|
-
logger.debug(`waiter resolved, inFlight ${inFlight}/${getCurrentConcurrency()}`)
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// Push the item
|
|
72
|
-
cb(null, item)
|
|
73
|
-
|
|
74
|
-
// Release slot on next microtask - essential for concurrency control.
|
|
75
|
-
// Without this, the slot would be freed immediately and items would
|
|
76
|
-
// flow through without any limiting effect.
|
|
77
|
-
queueMicrotask(release)
|
|
78
|
-
},
|
|
79
|
-
})
|
|
80
|
-
|
|
81
|
-
function getCurrentConcurrency(): number {
|
|
82
|
-
if (warmupComplete) return concurrency
|
|
83
|
-
|
|
84
|
-
const elapsed = Date.now() - startTime
|
|
85
|
-
if (elapsed >= warmupMs) {
|
|
86
|
-
warmupComplete = true
|
|
87
|
-
logger.debug('warmup complete')
|
|
88
|
-
return concurrency
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// Linear interpolation from 1 to concurrency
|
|
92
|
-
const progress = elapsed / warmupMs
|
|
93
|
-
return Math.max(1, Math.floor(1 + (concurrency - 1) * progress))
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
function release(): void {
|
|
97
|
-
inFlight--
|
|
98
|
-
// Wake up waiters based on current concurrency (may have increased)
|
|
99
|
-
const currentConcurrency = getCurrentConcurrency()
|
|
100
|
-
while (waiters.length && inFlight < currentConcurrency) {
|
|
101
|
-
inFlight++
|
|
102
|
-
waiters.shift()!.resolve()
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
}
|