pax_global_header00006660000000000000000000000064150240160110014500gustar00rootroot0000000000000052 comment=cea89de8165f24e183603e63839522ec22af58df mafintosh-end-of-stream-7980283/000077500000000000000000000000001502401601100163355ustar00rootroot00000000000000mafintosh-end-of-stream-7980283/.gitignore000066400000000000000000000000151502401601100203210ustar00rootroot00000000000000node_modules mafintosh-end-of-stream-7980283/.travis.yml000066400000000000000000000001141502401601100204420ustar00rootroot00000000000000language: node_js node_js: - '10' - '8' - '6' - '4' arch: - amd64 - ppc64le mafintosh-end-of-stream-7980283/LICENSE000066400000000000000000000020661502401601100173460ustar00rootroot00000000000000The 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.mafintosh-end-of-stream-7980283/README.md000066400000000000000000000032451502401601100176200ustar00rootroot00000000000000# 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 [![Build status](https://travis-ci.org/mafintosh/end-of-stream.svg?branch=master)](https://travis-ci.org/mafintosh/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. mafintosh-end-of-stream-7980283/index.js000066400000000000000000000052631502401601100200100ustar00rootroot00000000000000var once = require('once'); var noop = function() {}; var qnt = global.Bare ? queueMicrotask : process.nextTick.bind(process); 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 cancelled = false; 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 onerror = function(err) { callback.call(stream, err); }; var onclose = function() { qnt(onclosenexttick); }; var onclosenexttick = function() { if (cancelled) return; if (readable && !(rs && (rs.ended && !rs.destroyed))) return callback.call(stream, new Error('premature close')); if (writable && !(ws && (ws.ended && !ws.destroyed))) 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', onerror); stream.on('close', onclose); return function() { cancelled = true; 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', onerror); stream.removeListener('close', onclose); }; }; module.exports = eos; mafintosh-end-of-stream-7980283/package.json000066400000000000000000000014111502401601100206200ustar00rootroot00000000000000{ "name": "end-of-stream", "version": "1.4.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.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", "devDependencies": { "tape": "^4.11.0" } } mafintosh-end-of-stream-7980283/test.js000066400000000000000000000046621502401601100176620ustar00rootroot00000000000000var tape = require('tape') var eos = require('./index') var fs = require('fs') var cp = require('child_process') var net = require('net') var http = require('http') var stream = require('stream') tape('fs writestream destory', function (t) { var ws = fs.createWriteStream('/dev/null'); eos(ws, function(err) { t.ok(!!err) t.ok(this === ws) t.end() }) ws.destroy(); }) tape('fs readstream destroy', function (t) { var rs1 = fs.createReadStream('/dev/urandom'); eos(rs1, function(err) { t.ok(!!err) t.ok(this === rs1) t.end() }) rs1.destroy() }) tape('fs readstream pipe', function (t) { var rs2 = fs.createReadStream(__filename) eos(rs2, function(err) { t.ifError(err) t.ok(this === rs2) t.end() }) rs2.pipe(fs.createWriteStream('/dev/null')) }) tape('fs readstream cancel', function (t) { var rs3 = fs.createReadStream(__filename) eos(rs3, function(err) { t.fail('should not enter') })() rs3.pipe(fs.createWriteStream('/dev/null')) rs3.on('end', function () { t.end() }) }) tape('exec', function (t) { var exec = cp.exec('echo hello world') eos(exec, function (err) { t.ifError(err) t.ok(this === exec) t.end() }) }) tape('spawn', function (t) { var spawn = cp.spawn('echo', ['hello world']); eos(spawn, function (err) { t.ifError(err) t.ok(this === spawn) t.end() }) }) tape('tcp socket', function (t) { t.plan(5) var socket = net.connect(50000) eos(socket, function(err) { t.ok(!!err) t.ok(this === socket) }) var server = net.createServer(function (socket) { eos(socket, function(err) { t.ok(!!err) t.ok(this === socket) }) socket.destroy() }).listen(30000, function () { var socket = net.connect(30000) eos(socket, function() { t.ok(this === socket) server.close() }) }) }) tape('http', function (t) { t.plan(2) var server2 = http.createServer(function(req, res) { eos(res, function(err) { t.ifError(err) }) res.end() }).listen(function() { var port = server2.address().port http.get('http://localhost:' + port, function(res) { eos(res, function(err) { t.ifError(err) server2.close() }) res.resume() }) }) }) tape('end() and emit(close)', function (t) { if (!stream.Writable) return t.end() var ws = new stream.Writable() ws._write = function (data, enc, cb) { process.nextTick(cb) } eos(ws, function (err) { t.error(err, 'no error') t.end() }) ws.write('hi') ws.end() ws.emit('close') })