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

refactor(router): pass socket to IncomingMessage constructor. #1743

Merged
merged 1 commit into from
Oct 15, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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