TS写的,差不多意思
const stream2buffer = (stream: NodeJS.ReadableStream): Promise<Buffer> => {
return new Promise((resolve, reject) => {
if (!stream.readable) return resolve(Buffer.concat([]))
const bufs: any = []
const onData = (d: any) => bufs.push(d)
const onEnd = (error: any) => {
error ? reject(error) : resolve(Buffer.concat(bufs))
clean()
}
const onClose = () => {
resolve(Buffer.concat(bufs))
clean()
}
const clean = () => {
stream.removeListener('data', onData)
stream.removeListener('end', onEnd)
stream.removeListener('error', onEnd)
stream.removeListener('close', onClose)
}
stream.on('data', onData)
stream.on('end', onEnd)
stream.on('error', onEnd)
stream.on('close', onClose)
})
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…