Skip to content
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
20 changes: 15 additions & 5 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,18 +370,28 @@ OutgoingMessage.prototype._send = function _send(data, encoding, callback, byteL

OutgoingMessage.prototype._writeRaw = _writeRaw;
function _writeRaw(data, encoding, callback, size) {
if (typeof encoding === 'function') {
callback = encoding;
encoding = null;
}

if (this.destroyed) {
if (typeof callback === 'function') {
process.nextTick(callback, new ERR_STREAM_DESTROYED('write'));
}
return false;
}

const conn = this[kSocket];
if (conn?.destroyed) {
// The socket was destroyed. If we're still trying to write to it,
// then we haven't gotten the 'close' event yet.
if (typeof callback === 'function') {
process.nextTick(callback, new ERR_STREAM_DESTROYED('write'));
}
return false;
}

if (typeof encoding === 'function') {
callback = encoding;
encoding = null;
}

if (conn && conn._httpMessage === this && conn.writable) {
// There might be pending data in the this.output buffer.
if (this.outputData.length) {
Expand Down
20 changes: 20 additions & 0 deletions test/parallel/test-http-outgoing-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,23 @@ const OutgoingMessage = http.OutgoingMessage;
}));
msg.on('error', common.mustNotCall());
}

{
const msg = new OutgoingMessage();
msg.destroy();

assert.strictEqual(msg._writeRaw('asd', common.mustCall((err) => {
assert.strictEqual(err.code, 'ERR_STREAM_DESTROYED');
})), false);
msg.on('error', common.mustNotCall());
}

{
const msg = new OutgoingMessage();
msg.socket = { destroyed: true };

assert.strictEqual(msg._writeRaw('asd', common.mustCall((err) => {
assert.strictEqual(err.code, 'ERR_STREAM_DESTROYED');
})), false);
msg.on('error', common.mustNotCall());
}
Loading