[Pkg-javascript-commits] [node-unpipe] 01/02: Imported Upstream version 1.0.0

Thorsten Alteholz alteholz at moszumanska.debian.org
Sat Feb 27 14:10:13 UTC 2016


This is an automated email from the git hooks/post-receive script.

alteholz pushed a commit to branch master
in repository node-unpipe.

commit 2520769edef12cebd7984f9d844f035441915cd6
Author: Thorsten Alteholz <debian at alteholz.de>
Date:   Sat Feb 27 15:10:06 2016 +0100

    Imported Upstream version 1.0.0
---
 .gitignore   |   3 +
 .travis.yml  |  19 ++++++
 HISTORY.md   |   4 ++
 LICENSE      |  22 +++++++
 README.md    |  43 ++++++++++++++
 index.js     |  69 ++++++++++++++++++++++
 package.json |  27 +++++++++
 test/test.js | 187 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 8 files changed, 374 insertions(+)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..3cd27af
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+coverage/
+node_modules/
+npm-debug.log
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..d03ff37
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,19 @@
+language: node_js
+node_js:
+  - "0.8"
+  - "0.10"
+  - "0.12"
+  - "1.0"
+  - "1.8"
+  - "2.0"
+  - "2.3"
+sudo: false
+before_install:
+  # Setup Node.js version-specific dependencies
+  - "test $TRAVIS_NODE_VERSION != '0.8' || npm rm --save-dev istanbul"
+script:
+  # Run test script, depending on istanbul install
+  - "test -n $(npm -ps ls istanbul) || npm test"
+  - "test -z $(npm -ps ls istanbul) || npm run-script test-travis"
+after_script:
+  - "test -e ./coverage/lcov.info && npm install coveralls at 2 && cat ./coverage/lcov.info | coveralls"
diff --git a/HISTORY.md b/HISTORY.md
new file mode 100644
index 0000000..85e0f8d
--- /dev/null
+++ b/HISTORY.md
@@ -0,0 +1,4 @@
+1.0.0 / 2015-06-14
+==================
+
+  * Initial release
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..aed0138
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,22 @@
+(The MIT License)
+
+Copyright (c) 2015 Douglas Christopher Wilson <doug at somethingdoug.com>
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..e536ad2
--- /dev/null
+++ b/README.md
@@ -0,0 +1,43 @@
+# unpipe
+
+[![NPM Version][npm-image]][npm-url]
+[![NPM Downloads][downloads-image]][downloads-url]
+[![Node.js Version][node-image]][node-url]
+[![Build Status][travis-image]][travis-url]
+[![Test Coverage][coveralls-image]][coveralls-url]
+
+Unpipe a stream from all destinations.
+
+## Installation
+
+```sh
+$ npm install unpipe
+```
+
+## API
+
+```js
+var unpipe = require('unpipe')
+```
+
+### unpipe(stream)
+
+Unpipes all destinations from a given stream. With stream 2+, this is
+equivalent to `stream.unpipe()`. When used with streams 1 style streams
+(typically Node.js 0.8 and below), this module attempts to undo the
+actions done in `stream.pipe(dest)`.
+
+## License
+
+[MIT](LICENSE)
+
+[npm-image]: https://img.shields.io/npm/v/unpipe.svg
+[npm-url]: https://npmjs.org/package/unpipe
+[node-image]: https://img.shields.io/node/v/unpipe.svg
+[node-url]: http://nodejs.org/download/
+[travis-image]: https://img.shields.io/travis/stream-utils/unpipe.svg
+[travis-url]: https://travis-ci.org/stream-utils/unpipe
+[coveralls-image]: https://img.shields.io/coveralls/stream-utils/unpipe.svg
+[coveralls-url]: https://coveralls.io/r/stream-utils/unpipe?branch=master
+[downloads-image]: https://img.shields.io/npm/dm/unpipe.svg
+[downloads-url]: https://npmjs.org/package/unpipe
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..15c3d97
--- /dev/null
+++ b/index.js
@@ -0,0 +1,69 @@
+/*!
+ * unpipe
+ * Copyright(c) 2015 Douglas Christopher Wilson
+ * MIT Licensed
+ */
+
+'use strict'
+
+/**
+ * Module exports.
+ * @public
+ */
+
+module.exports = unpipe
+
+/**
+ * Determine if there are Node.js pipe-like data listeners.
+ * @private
+ */
+
+function hasPipeDataListeners(stream) {
+  var listeners = stream.listeners('data')
+
+  for (var i = 0; i < listeners.length; i++) {
+    if (listeners[i].name === 'ondata') {
+      return true
+    }
+  }
+
+  return false
+}
+
+/**
+ * Unpipe a stream from all destinations.
+ *
+ * @param {object} stream
+ * @public
+ */
+
+function unpipe(stream) {
+  if (!stream) {
+    throw new TypeError('argument stream is required')
+  }
+
+  if (typeof stream.unpipe === 'function') {
+    // new-style
+    stream.unpipe()
+    return
+  }
+
+  // Node.js 0.8 hack
+  if (!hasPipeDataListeners(stream)) {
+    return
+  }
+
+  var listener
+  var listeners = stream.listeners('close')
+
+  for (var i = 0; i < listeners.length; i++) {
+    listener = listeners[i]
+
+    if (listener.name !== 'cleanup' && listener.name !== 'onclose') {
+      continue
+    }
+
+    // invoke the listener
+    listener.call(stream)
+  }
+}
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..a2b7358
--- /dev/null
+++ b/package.json
@@ -0,0 +1,27 @@
+{
+  "name": "unpipe",
+  "description": "Unpipe a stream from all destinations",
+  "version": "1.0.0",
+  "author": "Douglas Christopher Wilson <doug at somethingdoug.com>",
+  "license": "MIT",
+  "repository": "stream-utils/unpipe",
+  "devDependencies": {
+    "istanbul": "0.3.15",
+    "mocha": "2.2.5",
+    "readable-stream": "1.1.13"
+  },
+  "files": [
+    "HISTORY.md",
+    "LICENSE",
+    "README.md",
+    "index.js"
+  ],
+  "engines": {
+    "node": ">= 0.8"
+  },
+  "scripts": {
+    "test": "mocha --reporter spec --bail --check-leaks test/",
+    "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/",
+    "test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/"
+  }
+}
diff --git a/test/test.js b/test/test.js
new file mode 100644
index 0000000..a5e5aa0
--- /dev/null
+++ b/test/test.js
@@ -0,0 +1,187 @@
+
+var assert = require('assert')
+var stream = require('readable-stream')
+var unpipe = require('..')
+var util = require('util')
+
+describe('unpipe(stream)', function () {
+  describe('arguments', function () {
+    describe('stream', function () {
+      it('should be required', function () {
+        assert.throws(unpipe, /argument stream is required/)
+      })
+    })
+  })
+
+  describe('stream with unpipe support', function () {
+    it('should unpipe no destinations', function (done) {
+      var stream = new SlowReadStream()
+
+      process.nextTick(function () {
+        unpipe(stream)
+        done()
+      })
+    })
+
+    it('should unpipe single destination', function (done) {
+      var pipes = 0
+      var stream = new SlowReadStream()
+      var dest = new SlowWriteStream()
+
+      dest.on('pipe', function () {
+        pipes++
+      })
+
+      stream.on('resume', function () {
+        assert.equal(pipes, 1)
+
+        dest.on('unpipe', function (src) {
+          assert.equal(src, stream)
+          done()
+        })
+
+        unpipe(stream)
+      })
+
+      stream.pipe(dest)
+    })
+
+    it('should not remove custom close events', function (done) {
+      var pipes = 0
+      var stream = new SlowReadStream()
+      var dest = new SlowWriteStream()
+
+      dest.on('pipe', function () {
+        pipes++
+      })
+
+      stream.on('resume', function () {
+        assert.equal(pipes, 1)
+
+        stream.on('close', done)
+
+        unpipe(stream)
+        stream.emit('close')
+      })
+
+      stream.pipe(dest)
+    })
+  })
+
+  describe('stream without unpipe support', function () {
+    it('should unpipe no destinations', function (done) {
+      var stream = new SlowOldStream()
+
+      process.nextTick(function () {
+        unpipe(stream)
+        done()
+      })
+    })
+
+    it('should unpipe single destination', function (done) {
+      var pipes = 0
+      var stream = new SlowOldStream()
+      var dest = new SlowWriteStream()
+
+      dest.on('pipe', function () {
+        pipes++
+      })
+
+      dest.once('write', function () {
+        assert.equal(pipes, 1)
+        dest.on('write', function () {
+          throw new Error('unexpected write event')
+        })
+
+        unpipe(stream)
+        stream.emit('data', 'pong')
+        process.nextTick(done)
+      })
+
+      stream.pipe(dest)
+      stream.emit('data', 'ping')
+    })
+
+    it('should not remove custom data events', function (done) {
+      var pipes = 0
+      var stream = new SlowOldStream()
+      var dest = new SlowWriteStream()
+
+      stream.on('data', function (chunk) {
+        if (chunk === 'pong') {
+          done()
+        }
+      })
+
+      dest.on('pipe', function () {
+        pipes++
+      })
+
+      dest.once('write', function () {
+        assert.equal(pipes, 1)
+        dest.on('write', function () {
+          throw new Error('unexpected write event')
+        })
+
+        unpipe(stream)
+        stream.emit('data', 'pong')
+      })
+
+      stream.pipe(dest)
+      stream.emit('data', 'ping')
+    })
+
+    it('should not remove custom close events', function (done) {
+      var pipes = 0
+      var stream = new SlowOldStream()
+      var dest = new SlowWriteStream()
+
+      dest.on('pipe', function () {
+        pipes++
+      })
+
+      dest.once('write', function () {
+        assert.equal(pipes, 1)
+        dest.on('write', function () {
+          throw new Error('unexpected write event')
+        })
+
+        stream.on('close', done)
+
+        unpipe(stream)
+        stream.emit('data', 'pong')
+        stream.emit('close')
+      })
+
+      stream.pipe(dest)
+      stream.emit('data', 'ping')
+    })
+  })
+})
+
+function SlowOldStream() {
+  stream.Stream.call(this)
+}
+
+util.inherits(SlowOldStream, stream.Stream)
+
+function SlowReadStream() {
+  stream.Readable.call(this)
+}
+
+util.inherits(SlowReadStream, stream.Readable)
+
+SlowReadStream.prototype._read = function _read() {
+  setTimeout(this.push.bind(this, '.'), 1000)
+}
+
+function SlowWriteStream() {
+  stream.Writable.call(this)
+}
+
+util.inherits(SlowWriteStream, stream.Writable)
+
+SlowWriteStream.prototype._write = function _write(chunk, encoding, callback) {
+  this.emit('write')
+  setTimeout(callback, 1000)
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-unpipe.git



More information about the Pkg-javascript-commits mailing list