Skip to content

Commit

Permalink
refactor(router): pass socket to response constructor. (#1743)
Browse files Browse the repository at this point in the history
Passing the socket from the request to the `IncomingMessage` is what
happens in native Node. Using a dummy `EventEmitter` then tacking the
socket onto the response later is kludge from historical tech-debt that
can now safely be cleaned up.
  • Loading branch information
mastermatt committed Oct 15, 2019
1 parent f4a208b commit d687179
Showing 1 changed file with 2 additions and 6 deletions.
8 changes: 2 additions & 6 deletions lib/intercepted_request_router.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const debug = require('debug')('nock.request_overrider')
const { EventEmitter } = require('events')
const {
IncomingMessage,
ClientRequest,
Expand Down Expand Up @@ -32,8 +31,8 @@ class InterceptedRequestRouter {
}
this.interceptors = interceptors

this.response = new IncomingMessage(new EventEmitter())
this.socket = new Socket({ proto: options.proto })
this.response = new IncomingMessage(this.socket)
this.playbackStarted = false
this.requestBodyBuffers = []

Expand Down Expand Up @@ -63,11 +62,8 @@ class InterceptedRequestRouter {
// ClientRequest.connection is an alias for ClientRequest.socket
// https://nodejs.org/api/http.html#http_request_socket
// https://github.com/nodejs/node/blob/b0f75818f39ed4e6bd80eb7c4010c1daf5823ef7/lib/_http_client.js#L640-L641
// IncomingMessage.connection & IncomingMessage.client are aliases for IncomingMessage.socket
// https://nodejs.org/api/http.html#http_response_socket
// https://github.com/nodejs/node/blob/b0f75818f39ed4e6bd80eb7c4010c1daf5823ef7/lib/_http_incoming.js#L44-L69
// The same Socket is shared between the request and response to mimic native behavior.
req.socket = req.connection = response.socket = response.client = response.connection = socket
req.socket = req.connection = socket

propagate(['error', 'timeout'], req.socket, req)

Expand Down

0 comments on commit d687179

Please sign in to comment.