-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconcat.js
68 lines (54 loc) · 1.21 KB
/
concat.js
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { finished, Readable } from 'readable-stream'
class ConcatStream extends Readable {
/**
* @param {(import('stream').Duplex)[]} streams
* @param {{
* objectMode?: boolean
* }} [options]
*/
constructor(streams, { objectMode = false } = {}) {
super({ objectMode })
this.streams = streams
this.current = null
this.next()
}
/**
* @return {void|boolean|unknown}
*/
_read() {
if (!this.current) {
return this.push(null)
}
const chunk = this.current.read()
if (!chunk) {
return setTimeout(() => this._read(), 0)
}
if (this.push(chunk)) {
this._read()
}
}
next() {
this.current = this.streams.shift()
if (this.current) {
this.current.on('error', err => this.destroy(err))
finished(this.current, () => this.next())
}
}
}
/**
* @param {(import('stream').Duplex)[]} streams
* @return {Readable}
*/
function factory(...streams) {
return new ConcatStream(streams)
}
/**
* @param {(import('stream').Duplex)[]} streams
* @return {Readable}
*/
const object = (...streams) => {
return new ConcatStream(streams, { objectMode: true })
}
factory.object = object
export default factory
export { object }