Restructure download function
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
import { Transform } from 'stream'
|
||||
|
||||
/**
|
||||
* A pass-through Transform that destroys itself if no data flows through for
|
||||
* `idleMs`. The timer is set when the transform is created and reset on every
|
||||
* chunk, so a slow-but-steady download (large video over a slow link) keeps
|
||||
* going, while a genuinely stalled connection still fails fast.
|
||||
*/
|
||||
export function createIdleTimeoutStream (idleMs: number): Transform {
|
||||
let timer: NodeJS.Timeout | undefined
|
||||
const transform: Transform = new Transform({
|
||||
transform (chunk, _, cb) {
|
||||
if (timer) clearTimeout(timer)
|
||||
timer = setTimeout(() => transform.destroy(new Error(`No data received for ${idleMs}ms`)), idleMs)
|
||||
cb(null, chunk)
|
||||
},
|
||||
flush (cb) {
|
||||
if (timer) clearTimeout(timer)
|
||||
cb()
|
||||
}
|
||||
})
|
||||
// Arm the timer immediately so a response that returns headers but never
|
||||
// sends a body also times out.
|
||||
timer = setTimeout(() => transform.destroy(new Error(`No data received for ${idleMs}ms`)), idleMs)
|
||||
return transform
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* Returns a function that runs at most `limit` async tasks concurrently.
|
||||
* Tasks queue when the limit is reached and resume in FIFO order.
|
||||
*/
|
||||
export function createLimiter (limit: number) {
|
||||
let active = 0
|
||||
const queue: Array<() => void> = []
|
||||
return async function run<T> (fn: () => Promise<T>): Promise<T> {
|
||||
if (active >= limit) {
|
||||
await new Promise<void>(resolve => queue.push(resolve))
|
||||
}
|
||||
active++
|
||||
try {
|
||||
return await fn()
|
||||
} finally {
|
||||
active--
|
||||
const next = queue.shift()
|
||||
if (next) next()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user