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

stream: finished waits for 'close' on OutgoingMessage #36648

Closed
wants to merge 2 commits into from
Closed
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
12 changes: 11 additions & 1 deletion lib/internal/streams/end-of-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ function isRequest(stream) {
return stream.setHeader && typeof stream.abort === 'function';
}

function isServerResponse(stream) {
return (
typeof stream._sent100 === 'boolean' &&
typeof stream._removedConnection === 'boolean' &&
typeof stream._removedContLen === 'boolean' &&
typeof stream._removedTE === 'boolean' &&
typeof stream._closed === 'boolean'
);
}

function isReadable(stream) {
return typeof stream.readable === 'boolean' ||
typeof stream.readableEnded === 'boolean' ||
Expand Down Expand Up @@ -80,7 +90,7 @@ function eos(stream, options, callback) {
// TODO (ronag): Improve soft detection to include core modules and
// common ecosystem modules that do properly emit 'close' but fail
// this generic check.
let willEmitClose = (
let willEmitClose = isServerResponse(stream) || (
state &&
state.autoDestroy &&
(state.emitClose || isSocket(stream)) &&
Expand Down
32 changes: 32 additions & 0 deletions test/parallel/test-http-outgoing-finished.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';
const common = require('../common');
const { finished } = require('stream');

const http = require('http');
const assert = require('assert');

const server = http.createServer(function(req, res) {
let closed = false;
res
.on('close', common.mustCall(() => {
closed = true;
finished(res, common.mustCall(() => {
server.close();
}));
}))
.end();
finished(res, common.mustCall(() => {
assert.strictEqual(closed, true);
}));

}).listen(0, function() {
http
.request({
port: this.address().port,
method: 'GET'
})
.on('response', function(res) {
res.resume();
})
.end();
});