pax_global_header00006660000000000000000000000064130617442360014520gustar00rootroot0000000000000052 comment=0ae1658b8167596fafbb9195363ada3bc5a3eaf2 end-of-stream-1.4.0/000077500000000000000000000000001306174423600141635ustar00rootroot00000000000000end-of-stream-1.4.0/.gitignore000066400000000000000000000000151306174423600161470ustar00rootroot00000000000000node_modules end-of-stream-1.4.0/LICENSE000066400000000000000000000020661306174423600151740ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2014 Mathias Buus 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.end-of-stream-1.4.0/README.md000066400000000000000000000030421306174423600154410ustar00rootroot00000000000000# 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, streams2 and stream3 are supported. ``` js var eos = require('end-of-stream'); eos(readableStream, function(err) { // this will be set to the stream instance if (err) return console.log('stream had an error or closed early'); console.log('stream has ended', this === readableStream); }); eos(writableStream, function(err) { if (err) return console.log('stream had an error or closed early'); console.log('stream has finished', this === writableStream); }); eos(duplexStream, function(err) { if (err) return console.log('stream had an error or closed early'); console.log('stream has ended and finished', this === duplexStream); }); eos(duplexStream, {readable:false}, function(err) { if (err) return console.log('stream had an error or closed early'); console.log('stream has finished but might still be readable'); }); 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 writable'); }); eos(readableStream, {error:false}, function(err) { // do not treat emit('error', err) as a end-of-stream }); ``` ## License MIT ## Related `end-of-stream` is part of the [mississippi stream utility collection](https://github.com/maxogden/mississippi) which includes more useful stream modules similar to this one. end-of-stream-1.4.0/index.js000066400000000000000000000045751306174423600156430ustar00rootroot00000000000000var once = require('once'); var noop = function() {}; var isRequest = function(stream) { return stream.setHeader && typeof stream.abort === 'function'; }; var isChildProcess = function(stream) { return stream.stdio && Array.isArray(stream.stdio) && stream.stdio.length === 3 }; 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.call(stream); }; var onend = function() { readable = false; if (!writable) callback.call(stream); }; var onexit = function(exitCode) { callback.call(stream, exitCode ? new Error('exited with error code: ' + exitCode) : null); }; var onclose = function() { if (readable && !(rs && rs.ended)) return callback.call(stream, new Error('premature close')); if (writable && !(ws && ws.ended)) return callback.call(stream, 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); } if (isChildProcess(stream)) stream.on('exit', onexit); stream.on('end', onend); stream.on('finish', onfinish); if (opts.error !== false) stream.on('error', callback); stream.on('close', onclose); return function() { stream.removeListener('complete', onfinish); stream.removeListener('abort', onclose); stream.removeListener('request', onrequest); if (stream.req) stream.req.removeListener('finish', onfinish); stream.removeListener('end', onlegacyfinish); stream.removeListener('close', onlegacyfinish); stream.removeListener('finish', onfinish); stream.removeListener('exit', onexit); stream.removeListener('end', onend); stream.removeListener('error', callback); stream.removeListener('close', onclose); }; }; module.exports = eos; end-of-stream-1.4.0/package.json000066400000000000000000000013271306174423600164540ustar00rootroot00000000000000{ "name": "end-of-stream", "version": "1.4.0", "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.4.0" }, "scripts": { "test": "node test.js" }, "files": [ "index.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 ", "license": "MIT" } end-of-stream-1.4.0/test.js000066400000000000000000000033661306174423600155100ustar00rootroot00000000000000var assert = require('assert'); var eos = require('./index'); var expected = 8; var fs = require('fs'); var cp = require('child_process'); var net = require('net'); var ws = fs.createWriteStream('/dev/null'); eos(ws, function(err) { expected--; assert(!!err); assert(this === ws); if (!expected) process.exit(0); }); ws.close(); var rs1 = fs.createReadStream('/dev/random'); eos(rs1, function(err) { expected--; assert(!!err); assert(this === rs1); if (!expected) process.exit(0); }); rs1.close(); var rs2 = fs.createReadStream(__filename); eos(rs2, function(err) { expected--; assert(!err); assert(this === rs2); if (!expected) process.exit(0); }); rs2.pipe(fs.createWriteStream('/dev/null')); var rs3 = fs.createReadStream(__filename); eos(rs3, function(err) { assert(this === rs); throw new Error('no go'); })(); rs3.pipe(fs.createWriteStream('/dev/null')); var exec = cp.exec('echo hello world'); eos(exec, function(err) { expected--; assert(!err); assert(this === exec); if (!expected) process.exit(0); }); var spawn = cp.spawn('echo', ['hello world']); eos(spawn, function(err) { expected--; assert(!err); assert(this === spawn); if (!expected) process.exit(0); }); var socket = net.connect(50000); eos(socket, function(err) { expected--; assert(!!err); assert(this === socket); if (!expected) process.exit(0); }); var server = net.createServer(function(socket) { eos(socket, function() { expected--; assert(this === socket); if (!expected) process.exit(0); }); socket.destroy(); }).listen(30000, function() { var socket = net.connect(30000); eos(socket, function() { expected--; assert(this === socket); if (!expected) process.exit(0); }); }); setTimeout(function() { assert(expected === 0); process.exit(0); }, 1000);