| import EE from 'events'; |
| import {inherits} from 'util'; |
|
|
| import {Duplex} from './readable-stream/duplex.js'; |
| import {Readable} from './readable-stream/readable.js'; |
| import {Writable} from './readable-stream/writable.js'; |
| import {Transform} from './readable-stream/transform.js'; |
| import {PassThrough} from './readable-stream/passthrough.js'; |
| inherits(Stream, EE); |
| Stream.Readable = Readable; |
| Stream.Writable = Writable; |
| Stream.Duplex = Duplex; |
| Stream.Transform = Transform; |
| Stream.PassThrough = PassThrough; |
|
|
| |
| Stream.Stream = Stream; |
|
|
| export default Stream; |
| export {Readable,Writable,Duplex,Transform,PassThrough,Stream} |
|
|
| |
| |
|
|
| function Stream() { |
| EE.call(this); |
| } |
|
|
| Stream.prototype.pipe = function(dest, options) { |
| var source = this; |
|
|
| function ondata(chunk) { |
| if (dest.writable) { |
| if (false === dest.write(chunk) && source.pause) { |
| source.pause(); |
| } |
| } |
| } |
|
|
| source.on('data', ondata); |
|
|
| function ondrain() { |
| if (source.readable && source.resume) { |
| source.resume(); |
| } |
| } |
|
|
| dest.on('drain', ondrain); |
|
|
| |
| |
| if (!dest._isStdio && (!options || options.end !== false)) { |
| source.on('end', onend); |
| source.on('close', onclose); |
| } |
|
|
| var didOnEnd = false; |
| function onend() { |
| if (didOnEnd) return; |
| didOnEnd = true; |
|
|
| dest.end(); |
| } |
|
|
|
|
| function onclose() { |
| if (didOnEnd) return; |
| didOnEnd = true; |
|
|
| if (typeof dest.destroy === 'function') dest.destroy(); |
| } |
|
|
| |
| function onerror(er) { |
| cleanup(); |
| if (EE.listenerCount(this, 'error') === 0) { |
| throw er; |
| } |
| } |
|
|
| source.on('error', onerror); |
| dest.on('error', onerror); |
|
|
| |
| function cleanup() { |
| source.removeListener('data', ondata); |
| dest.removeListener('drain', ondrain); |
|
|
| source.removeListener('end', onend); |
| source.removeListener('close', onclose); |
|
|
| source.removeListener('error', onerror); |
| dest.removeListener('error', onerror); |
|
|
| source.removeListener('end', cleanup); |
| source.removeListener('close', cleanup); |
|
|
| dest.removeListener('close', cleanup); |
| } |
|
|
| source.on('end', cleanup); |
| source.on('close', cleanup); |
|
|
| dest.on('close', cleanup); |
|
|
| dest.emit('pipe', source); |
|
|
| |
| return dest; |
| }; |
|
|