forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombine.js
More file actions
146 lines (119 loc) · 2.9 KB
/
Copy pathcombine.js
File metadata and controls
146 lines (119 loc) · 2.9 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
'use strict';
const { Readable } = require('stream');
const {
toStreamable, isPromise, isBuffer, isString
} = require('./helpers');
const current = Symbol('current');
const queue = Symbol('queue');
const forwarding = Symbol('forwarding');
const next = Symbol('next');
const forward = Symbol('forward');
const nextStream = Symbol('nextStream');
class CombinedStream extends Readable {
constructor() {
super();
this.destroyed = false;
this._drained = false;
this[forwarding] = false;
this[current] = null;
this[queue] = [];
}
/**
* List of symbols
* @returns {Object}
* @static
*/
static get symbols() {
return {
current, queue, forwarding, next, forward, nextStream,
};
}
/**
* Appends data to queue
* @param {...Any} data
* @returns {Void}
*/
_append(...data) {
this[queue].push(...data.map(value => toStreamable(value)));
}
/**
* Readable stream _read implementation
* @returns {Void}
*/
_read() {
this._drained = true;
this[forward]();
}
[forward]() {
if (this[forwarding] || !this._drained || !this[current]) {
return;
}
this[forwarding] = true;
let chunk = this[current].read();
while (chunk !== null) {
this._drained = this.push(chunk);
chunk = this[current].read();
}
this[forwarding] = false;
}
[next]() {
this[current] = null;
const stream = this[queue].shift();
if (isPromise(stream)) {
return stream.then(res => this[nextStream](toStreamable(res))).catch(e => this.destroy(e));
}
if (isString(stream) || isBuffer(stream)) {
this._drained = this.push(stream);
return this[next]();
}
this[nextStream](stream);
}
[nextStream](stream) {
if (!stream) {
this.push(null);
return this.destroy();
}
this[current] = stream;
this[forward]();
const onReadable = () => this[forward]();
const onError = e => this.destroy(e);
const onClose = () => {
if (!stream._readableState.ended) {
onEnd(); // eslint-disable-line
this.destroy();
}
};
const onEnd = () => {
this[current] = null;
stream.removeListener('readable', onReadable);
stream.removeListener('end', onEnd);
stream.removeListener('error', onError);
stream.removeListener('close', onClose);
this[next]();
};
stream.on('readable', onReadable);
stream.on('error', onError);
stream.on('close', onClose);
stream.on('end', onEnd);
}
/**
* Destroys stream
* @param {Error} [e]
* @returns {Void}
*/
destroy(e) {
if (this.destroyed) {
return;
}
this.destroyed = true;
if (this[current] && this[current].destroy) {
this[current].destroy();
}
this[queue].map(stream => stream.destroy && stream.destroy());
if (e) {
this.emit('error', e);
}
this.emit('close');
}
}
module.exports = CombinedStream;