summaryrefslogtreecommitdiff
path: root/lib/internal/streams/legacy.js
diff options
context:
space:
mode:
authoryorkie <yorkiefixer@gmail.com>2016-08-09 23:34:23 +0800
committerMatteo Collina <hello@matteocollina.com>2017-02-01 09:58:51 +0100
commit1b30df10037d24224a555829754f4423d35bbf88 (patch)
treead946f2ef26524f61f908426f4f5d646d8c9b319 /lib/internal/streams/legacy.js
parent9db89732e6461fca5c07e1b6bbe168618f2b494f (diff)
downloadandroid-node-v8-1b30df10037d24224a555829754f4423d35bbf88.tar.gz
android-node-v8-1b30df10037d24224a555829754f4423d35bbf88.tar.bz2
android-node-v8-1b30df10037d24224a555829754f4423d35bbf88.zip
stream: move legacy to lib/internal dir
Improve readability of lib/stream.js by moving the legacy abstract Stream into lib/internal/streams/legacy.js. PR-URL: https://github.com/nodejs/node/pull/8197 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Diffstat (limited to 'lib/internal/streams/legacy.js')
-rw-r--r--lib/internal/streams/legacy.js93
1 files changed, 93 insertions, 0 deletions
diff --git a/lib/internal/streams/legacy.js b/lib/internal/streams/legacy.js
new file mode 100644
index 0000000000..3242b15eab
--- /dev/null
+++ b/lib/internal/streams/legacy.js
@@ -0,0 +1,93 @@
+'use strict';
+
+const EE = require('events');
+const util = require('util');
+
+function Stream() {
+ EE.call(this);
+}
+util.inherits(Stream, EE);
+
+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 the 'end' option is not supplied, dest.end() will be called when
+ // source gets the 'end' or 'close' events. Only dest.end() once.
+ 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();
+ }
+
+ // don't leave dangling pipes when there are errors.
+ function onerror(er) {
+ cleanup();
+ if (EE.listenerCount(this, 'error') === 0) {
+ throw er; // Unhandled stream error in pipe.
+ }
+ }
+
+ source.on('error', onerror);
+ dest.on('error', onerror);
+
+ // remove all the event listeners that were added.
+ 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);
+
+ // Allow for unix-like usage: A.pipe(B).pipe(C)
+ return dest;
+};
+
+module.exports = Stream;