mirror of
https://github.com/vatesfr/xen-orchestra.git
synced 2026-09-10 22:14:48 -05:00
19 lines
542 B
JavaScript
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
|
|
}
|
|
}
|