Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow "premature close" for non-core streams #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ var eos = function(stream, opts, callback) {
};

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'));
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() {
Expand Down
20 changes: 18 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var assert = require('assert');
var eos = require('./index');

var expected = 8;
var expected = 9;
var fs = require('fs');
var cp = require('child_process');
var net = require('net');
Expand Down Expand Up @@ -80,7 +80,23 @@ var server = net.createServer(function(socket) {
});
});

setTimeout(function() {
var EE = require('events').EventEmitter;
var customStream = new EE();
customStream.readable = true;
eos(customStream, function(er) {
if (er) {
throw er;
}
expected--;
if (!expected) process.exit(0);
});
customStream.emit('close');
customStream.emit('end');

var timeout = setTimeout(function() {
assert(expected === 0);
process.exit(0);
}, 1000);

// don't keep the process open unnecessarily
if (typeof timeout.unref === 'function') timeout.unref();