Files
xen-orchestra/@xen-orchestra/backups/_forkStreamUnpipe.js
2022-02-22 12:34:41 +01:00

31 lines
737 B
JavaScript

'use strict'
const eos = require('end-of-stream')
const { PassThrough } = require('stream')
// create a new readable stream from an existing one which may be piped later
//
// in case of error in the new readable stream, it will simply be unpiped
// from the original one
exports.forkStreamUnpipe = function forkStreamUnpipe(stream) {
const { forks = 0 } = stream
stream.forks = forks + 1
const proxy = new PassThrough()
stream.pipe(proxy)
eos(stream, error => {
if (error !== undefined) {
proxy.destroy(error)
}
})
eos(proxy, _ => {
stream.forks--
stream.unpipe(proxy)
if (stream.forks === 0) {
stream.destroy(new Error('no more consumers for this stream'))
}
})
return proxy
}