forked from mafintosh/end-of-stream
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 876066a
Showing
5 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# end-of-stream | ||
|
||
A node module that calls a callback when a readable/writable/duplex stream has completed. | ||
|
||
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'); | ||
}); | ||
``` | ||
|
||
## License | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
var once = require('once'); | ||
|
||
var noop = function() {}; | ||
|
||
var patch = function(stream, fn) { // ensure a 'finish' event - even for 0.8 streams | ||
var end = stream.end; | ||
stream.end = function() { | ||
fn(); | ||
end.apply(this, arguments); | ||
}; | ||
}; | ||
|
||
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 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')); | ||
}; | ||
|
||
if (writable && !ws) patch(stream, onfinish); | ||
|
||
stream.on('end', onend); | ||
stream.on('finish', onfinish); | ||
stream.on('error', callback); | ||
stream.on('close', onclose); | ||
|
||
stream.on('abort', onclose); // not really a stream event but needed for request compat... | ||
|
||
return stream; | ||
}; | ||
|
||
module.exports = eos; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "end-of-stream", | ||
"version": "0.1.0", | ||
"description": "wait for a readable/writable to finish or fail", | ||
"repository": "git://github.com/mafintosh/end-of-stream.git", | ||
"dependencies": { | ||
"once": "~1.3.0" | ||
}, | ||
"scripts": { | ||
"test": "node test.js" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |