index.js
6.19 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
var Readable = require('stream').Readable;
var Writable = require('stream').Writable;
var Duplex = require('stream').Duplex;
var hashish = require('hashish');
/**
* Proxy some events from underlying readable and writable streams.
*/
var SOURCE_EVENTS = ['error', 'close'];
var DEST_EVENTS = ['drain', 'close'];
/**
* Creates property to use with `Object.create()`
*
* @param {Object} value
* @return {Object}
*/
function prop(value) {
return {
writable: true,
enumerable: true,
configurable: true,
value: value
};
}
/**
* @constructor
* @param {Object} options
* {Object} superCtor
* {Boolean} readable
* {Boolean} writable
* @return {Duplex}
*/
var Streamify = module.exports = function Streamify(options) {
options = options || {};
options.readable = typeof options.readable !== 'undefined' ?
options.readable : true;
options.writable = typeof options.writable !== 'undefined' ?
options.writable : true;
var superCtor = getConstructor(options);
// Add `Streamify.prototype` methods.
var properties = hashish.map(Streamify.prototype, prop);
var o = Object.create(superCtor.prototype, properties);
superCtor.call(o);
o.readable = options.readable;
o.writable = options.writable;
o.__options = options;
o._onevent = function onevent() {
};
o._destWritten = [];
if (options.writable) {
o.once('finish', function() {
if (o._dest) {
o._dest.stream.end();
}
});
}
return o;
};
/**
* Required implementation by streaming API.
*
* @param {Number} size
*/
Streamify.prototype._read = function(size) {
if (this._source) {
var self = this;
var onreadable = this._source.onreadable = function onreadable() {
if (!self._source) { return; }
var data = self._source.stream.read(size);
if (data) {
self.push(data);
} else {
self._source.stream.once('readable', onreadable);
}
};
onreadable();
} else {
this._sourceRead = size;
}
};
/**
* Required implementation by streaming API.
*
* @param {Buffer|String} chunk
* @param {!String} encoding
* @param {Function(!Error)} callback
*/
Streamify.prototype._write = function(chunk, encoding, callback) {
if (this._dest) {
this._dest.stream.write(chunk, encoding, callback);
} else {
this._destWritten.push(arguments);
}
};
/**
* Add a stream to be the readable stream source.
*
* @param {Readable|Stream} stream
*/
Streamify.prototype.addSource = function(stream) {
if (this._source) {
throw Error('A source stream has already been added.');
}
stream = getReadable(stream);
this._source = { stream: stream, listeners: {}, onend: onend };
var self = this;
SOURCE_EVENTS.forEach(function(event) {
var onevent = self._source.listeners[event] = function onevent() {
var args = Array.prototype.slice.call(arguments);
self.emit.apply(self, [event].concat(args));
};
stream.on(event, onevent);
});
// Listen for `end` event to signal for end of data.
function onend() {
self.push(null);
}
stream.on('end', onend);
// Check if `Readable#_read()` has already been called.
this._read(this._sourceRead);
};
/**
* Remove a stream from being the source.
*/
Streamify.prototype.removeSource = function() {
if (!this._source) {
throw Error('A source stream has not been added.');
}
var source = this._source;
SOURCE_EVENTS.forEach(function(event) {
source.stream.removeListener(event, source.listeners[event]);
});
source.stream.removeListener('readable', source.onreadable);
source.stream.removeListener('end', source.onend);
delete this._source;
};
/**
* Add a stream to be the writable stream destination.
*
* @param {Writable|Stream} stream
*/
Streamify.prototype.addDest = function(stream) {
if (this._dest) {
throw Error('A destination stream has already been added.');
}
this._dest = { stream: stream, listeners: {} };
var self = this;
DEST_EVENTS.forEach(function(event) {
var onevent = self._dest.listeners[event] = function onevent() {
var args = Array.prototype.slice.call(arguments);
self.emit.apply(self, [event].concat(args));
};
stream.on(event, onevent);
});
if (this._destWritten.length) {
this._destWritten.forEach(function(args) {
stream.write.apply(stream, args);
});
}
};
/**
* Remove a stream from being the destination.
*/
Streamify.prototype.removeDest = function() {
if (!this._dest) {
throw Error('A destination stream has not been added.');
}
var dest = this._dest;
DEST_EVENTS.forEach(function(event) {
dest.stream.removeListener(event, dest.listeners[event]);
});
delete this._dest;
this._destWritten = [];
};
/**
* Begins fueling data from actual stream into Streamify instance.
*
* @param {Readable|Writable|Duplex|Stream} stream
*/
Streamify.prototype.resolve = function(stream) {
if (this.__options.readable && stream.readable) {
this.addSource(stream);
}
if (this.__options.writable && stream.writable) {
this.addDest(stream);
}
};
/**
* Removes a stream from this, possibly because another is replacing it.
*/
Streamify.prototype.unresolve = function() {
if (this._source) {
this.removeSource();
}
if (this._dest) {
this.removeDest();
}
};
/**
* Returns a readable new stream API stream if the stream is using the
* old API. Otherwise it returns the same stream.
*
* @param {Readable|Stream} stream
* @return {Readable}
*/
function getReadable(stream) {
if (isOldStyleStream(stream)) {
var readable = new Readable();
readable.wrap(stream);
return readable;
} else {
return stream;
}
}
/**
* Returns true if a stream is an old style API stream.
*
* @param {Readable|Stream} stream
* @return {Boolean}
*/
function isOldStyleStream(stream) {
return typeof stream.read !== 'function' ||
typeof stream._read !== 'function' ||
typeof stream.push !== 'function' ||
typeof stream.unshift !== 'function' ||
typeof stream.wrap !== 'function';
}
function getConstructor (options) {
var superCtor = Duplex;
if (options.readable && !options.writable) {
superCtor = Readable;
}
if (options.writable && !options.readable) {
superCtor = Writable;
}
return superCtor;
}