[Pkg-javascript-commits] [node-end-of-stream] 01/02: Imported Upstream version 0.1.5
Andrew Kelley
andrewrk-guest at moszumanska.debian.org
Sun Jun 29 19:19:03 UTC 2014
This is an automated email from the git hooks/post-receive script.
andrewrk-guest pushed a commit to branch master
in repository node-end-of-stream.
commit 89a56a0554bfeb7b790f0e0d0f53a2cf7b356eae
Author: Andrew Kelley <superjoe30 at gmail.com>
Date: Sun Jun 29 19:13:38 2014 +0000
Imported Upstream version 0.1.5
---
.gitignore | 1 +
README.md | 47 ++++++++++++++++++++++++++++++++++++++++++++++
index.js | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package.json | 31 ++++++++++++++++++++++++++++++
test.js | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 199 insertions(+)
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..3c3629e
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+node_modules
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..df800c1
--- /dev/null
+++ b/README.md
@@ -0,0 +1,47 @@
+# end-of-stream
+
+A node module that calls a callback when a readable/writable/duplex stream has completed or failed.
+
+ npm install end-of-stream
+
+## Usage
+
+Simply pass a stream and a callback to the `eos`.
+Both legacy streams and streams2 are supported.
+
+``` js
+var eos = require('end-of-stream');
+
+eos(readableStream, function(err) {
+ if (err) return console.log('stream had an error or closed early');
+ console.log('stream has ended');
+});
+
+eos(writableStream, function(err) {
+ if (err) return console.log('stream had an error or closed early');
+ console.log('stream has finished');
+});
+
+eos(duplexStream, function(err) {
+ if (err) return console.log('stream had an error or closed early');
+ console.log('stream has ended and finished');
+});
+
+eos(duplexStream, {readable:false}, function(err) {
+ if (err) return console.log('stream had an error or closed early');
+ console.log('stream has ended but might still be writable');
+});
+
+eos(duplexStream, {writable:false}, function(err) {
+ if (err) return console.log('stream had an error or closed early');
+ console.log('stream has ended but might still be readable');
+});
+
+eos(readableStream, {error:false}, function(err) {
+ // do not treat emit('error', err) as a end-of-stream
+});
+```
+
+## License
+
+MIT
\ No newline at end of file
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..b9fbec0
--- /dev/null
+++ b/index.js
@@ -0,0 +1,61 @@
+var once = require('once');
+
+var noop = function() {};
+
+var isRequest = function(stream) {
+ return stream.setHeader && typeof stream.abort === 'function';
+};
+
+var eos = function(stream, opts, callback) {
+ if (typeof opts === 'function') return eos(stream, null, opts);
+ if (!opts) opts = {};
+
+ callback = once(callback || noop);
+
+ var ws = stream._writableState;
+ var rs = stream._readableState;
+ var readable = opts.readable || (opts.readable !== false && stream.readable);
+ var writable = opts.writable || (opts.writable !== false && stream.writable);
+
+ var onlegacyfinish = function() {
+ if (!stream.writable) onfinish();
+ };
+
+ var onfinish = function() {
+ writable = false;
+ if (!readable) callback();
+ };
+
+ var onend = function() {
+ readable = false;
+ if (!writable) callback();
+ };
+
+ var onclose = function() {
+ if (readable && !(rs && rs.ended)) return callback(new Error('premature close'));
+ if (writable && !(ws && ws.ended)) return callback(new Error('premature close'));
+ };
+
+ var onrequest = function() {
+ stream.req.on('finish', onfinish);
+ };
+
+ if (isRequest(stream)) {
+ stream.on('complete', onfinish);
+ stream.on('abort', onclose);
+ if (stream.req) onrequest();
+ else stream.on('request', onrequest);
+ } else if (writable && !ws) { // legacy streams
+ stream.on('end', onlegacyfinish);
+ stream.on('close', onlegacyfinish);
+ }
+
+ stream.on('end', onend);
+ stream.on('finish', onfinish);
+ if (opts.error !== false) stream.on('error', callback);
+ stream.on('close', onclose);
+
+ return stream;
+};
+
+module.exports = eos;
\ No newline at end of file
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..1f64886
--- /dev/null
+++ b/package.json
@@ -0,0 +1,31 @@
+{
+ "name": "end-of-stream",
+ "version": "0.1.5",
+ "description": "Call a callback when a readable/writable/duplex stream has completed or failed.",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/mafintosh/end-of-stream.git"
+ },
+ "dependencies": {
+ "once": "~1.3.0"
+ },
+ "scripts": {
+ "test": "node test.js"
+ },
+ "keywords": [
+ "stream",
+ "streams",
+ "callback",
+ "finish",
+ "close",
+ "end",
+ "wait"
+ ],
+ "bugs": {
+ "url": "https://github.com/mafintosh/end-of-stream/issues"
+ },
+ "homepage": "https://github.com/mafintosh/end-of-stream",
+ "main": "index.js",
+ "author": "Mathias Buus <mathiasbuus at gmail.com>",
+ "license": "MIT"
+}
diff --git a/test.js b/test.js
new file mode 100644
index 0000000..277f1ce
--- /dev/null
+++ b/test.js
@@ -0,0 +1,59 @@
+var assert = require('assert');
+var eos = require('./index');
+
+var expected = 6;
+var fs = require('fs');
+var net = require('net');
+
+var ws = fs.createWriteStream('/dev/null');
+eos(ws, function(err) {
+ expected--;
+ assert(!!err);
+ if (!expected) process.exit(0);
+});
+ws.close();
+
+var rs = fs.createReadStream('/dev/random');
+eos(rs, function(err) {
+ expected--;
+ assert(!!err);
+ if (!expected) process.exit(0);
+});
+rs.close();
+
+var rs = fs.createReadStream(__filename);
+eos(rs, function(err) {
+ expected--;
+ assert(!err);
+ if (!expected) process.exit(0);
+});
+rs.pipe(fs.createWriteStream('/dev/null'));
+
+var socket = net.connect(50000);
+eos(socket, function(err) {
+ expected--;
+ assert(!!err);
+ if (!expected) process.exit(0);
+});
+
+
+var server = net.createServer(function(socket) {
+ eos(socket, function() {
+ expected--;
+ if (!expected) process.exit(0);
+ });
+ socket.destroy();
+}).listen(30000, function() {
+ var socket = net.connect(30000);
+ eos(socket, function() {
+ expected--;
+ if (!expected) process.exit(0);
+ });
+});
+
+
+
+setTimeout(function() {
+ assert(expected === 0);
+ process.exit(0);
+}, 1000);
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-end-of-stream.git
More information about the Pkg-javascript-commits
mailing list