Skip to content

Commit

Permalink
fixup! unit test errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Lxxyx committed Jan 12, 2021
1 parent 2a0ecd6 commit bbafac6
Showing 1 changed file with 40 additions and 7 deletions.
47 changes: 40 additions & 7 deletions lib/_http_outgoing.js
Expand Up @@ -71,6 +71,10 @@ const {
} = require('internal/errors');
const { validateString } = require('internal/validators');
const { isUint8Array } = require('internal/util/types');
const {
defaultTriggerAsyncIdScope,
symbols: { async_id_symbol }
} = require('internal/async_hooks');

const HIGH_WATER_MARK = getDefaultHighWaterMark();
const { CRLF, debug } = common;
Expand Down Expand Up @@ -308,7 +312,6 @@ OutgoingMessage.prototype.destroy = function destroy(error) {
return this;
};


// This abstract either writing directly to the socket or buffering it.
OutgoingMessage.prototype._send = function _send(data, encoding, callback) {
// This is a shameful hack to get the headers and first body chunk onto
Expand Down Expand Up @@ -713,7 +716,11 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
}

if (err) {
process.nextTick(callback, err);
if (!msg.destroyed) {
onError(msg, err, callback);
} else {
process.nextTick(callback, err);
}
msg.destroy(err);
return false;
}
Expand Down Expand Up @@ -789,6 +796,23 @@ function onFinish(err) {
this.emit('finish');
}

function onError(msg, err, callback) {
const triggerAsyncId = msg.socket ? msg.socket[async_id_symbol] : undefined;
defaultTriggerAsyncIdScope(triggerAsyncId,
process.nextTick,
emitErrorNt,
msg,
err,
callback);
}

function emitErrorNt(msg, err, callback) {
callback?.(err);
if (typeof msg.emit === 'function' && !msg._closed) {
msg.emit('error', err);
}
}

OutgoingMessage.prototype.end = function end(chunk, encoding, cb) {
if (typeof chunk === 'function') {
cb = chunk;
Expand All @@ -799,16 +823,25 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, cb) {
encoding = null;
}

if (this.socket) {
this.socket.cork();
}
if (chunk !== null && chunk !== undefined) {
if (this.finished) {
onError(this, new ERR_STREAM_WRITE_AFTER_END(), cb);
return this;
}

if (chunk !== null && chunk !== undefined)
this.write(chunk, encoding);
if (this.socket) {
this.socket?.cork();
}

write_(this, chunk, encoding, null, true);
}

let err;
if (!this.finished) {
if (!this._header) {
if (this.socket) {
this.socket.cork();
}
this._contentLength = 0;
this._implicitHeader();
}
Expand Down

0 comments on commit bbafac6

Please sign in to comment.