-
Notifications
You must be signed in to change notification settings - Fork 1
/
readStream.mjs
37 lines (32 loc) · 1.11 KB
/
readStream.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import stream from 'stream'
/** Read up to *max* bytes from the readable *stream*
* @param {stream.Readable} stream
* @returns {Promise<Buffer>}
*/
export default function readStream(rstr, max = Infinity) {
if (!(rstr instanceof stream.Readable)) return Promise.reject(new TypeError('Argument must be a readable stream.'))
if (!rstr.readable || rstr.closed || rstr.destroyed) return Promise.resolve()
return new Promise((resolve, reject) => {
const bufferArray = []
let currentLength = 0
function onData(chunk) {
bufferArray.push(chunk.slice(0, max - currentLength))
currentLength += chunk.byteLength
if (currentLength > max) rstr.destroy(new RangeError('Too Much Data.'))
}
function onceEnd() {
rstr.removeListener('data', onData)
rstr.removeListener('error', onceError)
resolve(Buffer.concat(bufferArray))
}
function onceError(err) {
rstr.removeListener('data', onData)
rstr.removeListener('end', onceEnd)
reject(err)
}
rstr.on('data', onData)
rstr.once('end', onceEnd)
rstr.once('error', onceError)
rstr.resume()
})
}