Files
Xen-Orchestra-xen-orchestra…/@xen-orchestra/backups/_runners/_createStreamThrottle.mjs

19 lines
542 B
JavaScript

import { pipeline } from 'node:stream'
import { ThrottleGroup } from '@kldzj/stream-throttle'
import identity from 'lodash/identity.js'
const noop = Function.prototype
export default function createStreamThrottle(rate) {
if (rate === 0) {
return identity
}
const group = new ThrottleGroup({ rate })
return function throttleStream(stream) {
const throttled = pipeline(stream, group.createThrottle(), noop)
throttled.length = stream.length
throttled.maxStreamLength = stream.maxStreamLength
return throttled
}
}