-
Notifications
You must be signed in to change notification settings - Fork 10
/
stream-template.js
168 lines (157 loc) · 4.67 KB
/
stream-template.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
"use strict";
const eos = require("end-of-stream");
const stream = require("readable-stream");
// No destructuring until Node 6
const PassThrough = stream.PassThrough;
const Readable = stream.Readable;
// Use template literals to allow a template where the variables are streams,
// the output as a whole is also a stream.
function makeForEncoding(encoding) {
return function StreamTemplate(strings /*, ...interpolations*/) {
// No rest params until Node 6
const interpolations = Array.prototype.slice.call(arguments, 1);
let queue = [],
stringBuffer = [],
destroyed = false,
awaitingPromise = false,
currentStream = null,
wantsData = false,
currentStreamHasData = false,
readable = null;
function forwardDestroy(streamOrPromise) {
if (isStream(streamOrPromise)) {
eos(streamOrPromise, err => {
if (err) {
readable.destroy(err);
}
});
} else {
streamOrPromise.catch(err => {
readable.destroy(err);
});
}
}
function read() {
if (destroyed) {
return;
}
wantsData = true;
if (currentStream) {
if (currentStreamHasData) {
currentStreamHasData = false;
const chunk = currentStream.read();
if (chunk != null) {
if (readable.push(chunk) === false) {
wantsData = false;
}
}
}
return;
}
if (awaitingPromise) {
return;
}
while (!destroyed) {
if (queue.length === 0) {
if (stringBuffer.length) {
const toWrite = Buffer.concat(stringBuffer);
stringBuffer.length = 0;
if (readable.push(toWrite) === false) {
return;
}
}
readable.push(null);
break;
}
let item = queue.shift();
if (item != null) {
if (typeof item === "string") {
// Combine plain strings together to avoid extra chunks
// Buffer.from() available in Node 6
stringBuffer.push(new Buffer(`${item}`, encoding));
} else if (Array.isArray(item)) {
queue = item.concat(queue);
} else if (Buffer.isBuffer(item)) {
stringBuffer.push(item);
// stream or Promise
} else {
if (stringBuffer.length) {
queue.unshift(item);
const toWrite = Buffer.concat(stringBuffer);
stringBuffer.length = 0;
if (readable.push(toWrite) !== false) {
read();
}
return;
}
if (isStream(item)) {
currentStream = new PassThrough();
currentStreamHasData = false;
item.pipe(currentStream);
currentStream.once("end", () => {
currentStream = null;
if (wantsData) {
read();
}
});
currentStream.on("readable", () => {
currentStreamHasData = true;
if (wantsData) {
read();
}
});
currentStreamHasData = true;
read();
} else {
// Promise!
awaitingPromise = true;
item.then(result => {
awaitingPromise = false;
queue.unshift(result);
read();
});
}
// Exit out of this loop (we'll have called read again if needed)
return;
}
}
}
}
function destroy(err) {
if (destroyed) {
return;
}
destroyed = true;
for (let i = 0; i < interpolations.length; i++) {
const interpolation = interpolations[i];
if (isStream(interpolation)) {
interpolation.destroy();
}
}
if (err) {
readable.emit("error", err);
}
readable.emit("close");
}
queue.push(strings[0]);
for (let i = 0; i < interpolations.length; i++) {
const interpolation = interpolations[i];
// If is stream or Promise, error handle right away
if (isStream(interpolation) || isPromise(interpolation)) {
forwardDestroy(interpolation);
}
queue.push(interpolation);
queue.push(strings[i + 1]);
}
readable = new Readable({ read, destroy });
return readable;
};
}
function isStream(stream) {
return stream != null && stream.pipe != null;
}
function isPromise(promise) {
return promise != null && promise.then != null;
}
module.exports = makeForEncoding("utf8");
module.exports.encoding = makeForEncoding;