Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed May 4, 2021
1 parent 607439b commit 63ecaf0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
9 changes: 0 additions & 9 deletions lib/_http_incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const {
} = primordials;

const { Readable, finished } = require('stream');
const { kDestroy, destroy } = require('internal/stream')

const kHeaders = Symbol('kHeaders');
const kHeadersCount = Symbol('kHeadersCount');
Expand Down Expand Up @@ -173,14 +172,6 @@ IncomingMessage.prototype._read = function _read(n) {
readStart(this.socket);
};

IncomingMessage.prototype[kDestroy] = function (err) {
if (!err && !this.res.destroyed) {
this.socket = null
}

this.destroy(err)
}

// It's possible that the socket will be destroyed, and removed from
// any messages, before ever calling this. In that case, just skip
// it, since something else is destroying this connection anyway.
Expand Down
24 changes: 22 additions & 2 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ const {
const { IncomingMessage } = require('_http_incoming');
const {
connResetException,
codes
codes,
AbortError
} = require('internal/errors');
const { kDestroy } = require('internal/stream');
const {
ERR_HTTP_REQUEST_TIMEOUT,
ERR_HTTP_HEADERS_SENT,
Expand Down Expand Up @@ -361,6 +363,23 @@ function writeHead(statusCode, reason, obj) {
// Docs-only deprecated: DEP0063
ServerResponse.prototype.writeHeader = ServerResponse.prototype.writeHead;

class ServerRequest extends IncomingMessage {
[kDestroy] (err) {
if (!this.res.destroyed) {
this._destroy = (err, cb) => {
if (!this.readableEnded || !this.complete) {
this.aborted = true;
this.emit('aborted');
}

cb(err)
};
}

this.destroy(err);
}
}

function Server(options, requestListener) {
if (!(this instanceof Server)) return new Server(options, requestListener);

Expand All @@ -373,7 +392,7 @@ function Server(options, requestListener) {
throw new ERR_INVALID_ARG_TYPE('options', 'object', options);
}

this[kIncomingMessage] = options.IncomingMessage || IncomingMessage;
this[kIncomingMessage] = options.IncomingMessage || ServerRequest;
this[kServerResponse] = options.ServerResponse || ServerResponse;

const maxHeaderSize = options.maxHeaderSize;
Expand Down Expand Up @@ -997,6 +1016,7 @@ module.exports = {
STATUS_CODES,
Server,
ServerResponse,
ServerRequest,
_connectionListener: connectionListener,
kServerResponse
};
8 changes: 7 additions & 1 deletion lib/internal/streams/destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {
codes: {
ERR_MULTIPLE_CALLBACK,
},
AbortError,
} = require('internal/errors');
const {
FunctionPrototypeCall,
Expand Down Expand Up @@ -374,8 +375,12 @@ function destroyer(stream, err) {
return
}

if (!err && (stream.readable || stream.writable)) {
err = new AbortError();
}

if (typeof stream[kDestroy] === 'function') {
stream[kDestroy]();
stream[kDestroy](err);
} else if (isRequest(stream)) {
stream.abort();
} else if (isRequest(stream.req)) {
Expand Down Expand Up @@ -406,6 +411,7 @@ module.exports = {
kDestroy,
construct,
destroyer,
destroy2,
destroy,
undestroy,
errorOrDestroy
Expand Down

0 comments on commit 63ecaf0

Please sign in to comment.