diff --git a/lib/autohttp/agent.js b/lib/autohttp/agent.js index 7c7315b11..75402c00b 100644 --- a/lib/autohttp/agent.js +++ b/lib/autohttp/agent.js @@ -57,10 +57,12 @@ class AutoHttp2Agent extends EventEmitter { cb, socketCb ) { - const options = Object.assign({}, reqOptions, this.options, { + const options = { + ...reqOptions, + ...this.options, port: Number(reqOptions.port || this.options.port || this.defaultPort), host: reqOptions.hostname || reqOptions.host || 'localhost' - }) + } // check if ALPN is cached const name = getSocketName(options) @@ -76,9 +78,10 @@ class AutoHttp2Agent extends EventEmitter { // No need to pass the cachedSocket since the respective protocol's agents will reuse the socket that was initially // passed during ALPN Negotiation if (protocol === 'h2') { - const http2Options = Object.assign({}, options, { + const http2Options = { + ...options, path: options.socketPath - }) + } let connection try { @@ -96,9 +99,10 @@ class AutoHttp2Agent extends EventEmitter { return } - const http1RequestOptions = Object.assign({}, options, { + const http1RequestOptions = { + ...options, agent: this.httpsAgent - }) + } let request try { @@ -116,11 +120,12 @@ class AutoHttp2Agent extends EventEmitter { const uri = options.uri const name = getSocketName(options) - const socket = tls.connect(Object.assign({}, options, { + const socket = tls.connect({ + ...options, path: options.socketPath, ALPNProtocols: supportedProtocols, servername: options.servername || calculateServerName(options) - })) + }) socketCb(socket) const socketConnectionErrorHandler = (e) => { @@ -153,9 +158,10 @@ class AutoHttp2Agent extends EventEmitter { }) if (protocol === 'h2') { - const http2Options = Object.assign({}, options, { + const http2Options = { + ...options, path: options.socketPath - }) + } try { const connection = this.http2Agent.createConnection( req, @@ -182,9 +188,10 @@ class AutoHttp2Agent extends EventEmitter { return socket } - const http1RequestOptions = Object.assign({}, options, { + const http1RequestOptions = { + ...options, agent: this.httpsAgent - }) + } let request try { request = https.request(http1RequestOptions) diff --git a/lib/autohttp/request.js b/lib/autohttp/request.js index 788ecc93c..8b12466b9 100644 --- a/lib/autohttp/request.js +++ b/lib/autohttp/request.js @@ -36,11 +36,12 @@ class MultiProtocolRequest extends EventEmitter { } onHttp2 (connection) { - const options = Object.assign({}, this.options, { + const options = { + ...this.options, agent: { createConnection: () => connection } - }) + } let req try { diff --git a/lib/http2/agent.js b/lib/http2/agent.js index 91c74f234..464f5f720 100644 --- a/lib/http2/agent.js +++ b/lib/http2/agent.js @@ -10,17 +10,22 @@ class Http2Agent extends EventEmitter { } createConnection (req, uri, options, socket) { - const _options = Object.assign({}, options, this.options) + const _options = { + ...options, + ...this.options + } + const name = getConnectionName(_options) let connection = this.connections[name] if (!connection || connection.destroyed || connection.closed) { - const connectionOptions = Object.assign({}, _options, - { - port: _options.port || 443, - settings: { - enablePush: false - } }) + const connectionOptions = { + ..._options, + port: _options.port || 443, + settings: { + enablePush: false + } + } // check if a socket is supplied if (socket) { diff --git a/lib/http2/request.js b/lib/http2/request.js index b742cdd2f..1568b0822 100644 --- a/lib/http2/request.js +++ b/lib/http2/request.js @@ -42,14 +42,13 @@ class Http2Request extends EventEmitter { this._flushHeaders = this._flushHeaders.bind(this) this[kHeadersFlushed] = false - const headers = options.headers || {} - const uri = httpOptionsToUri(options) - const _options = Object.assign({}, options, { + const _options = { + ...options, port: Number(options.port || 443), path: undefined, host: options.hostname || options.host || 'localhost' - }) + } if (options.socketPath) { _options.path = options.socketPath @@ -59,19 +58,23 @@ class Http2Request extends EventEmitter { this._client = agent.createConnection(this, uri, _options) - this.requestHeaders = Object.assign(headers, { + const headers = options.headers || {} + + this.requestHeaders = { + ...headers, [http2.constants.HTTP2_HEADER_PATH]: options.path || '/', [http2.constants.HTTP2_HEADER_METHOD]: _options.method, [http2.constants.HTTP2_HEADER_AUTHORITY]: headers['host'] || _options.host - }) + } if (options.uri.isUnix || headers['host'] === 'unix' || _options.host === 'unix') { // The authority field needs to be set to 'localhost' when using unix sockets. // The default URL parser supplies the isUnix flag when the host is 'unix'. Added other checks incase using a different parser like WHATWG URL (new URL()). // See: https://github.com/nodejs/node/issues/32326 - this.requestHeaders = Object.assign(this.requestHeaders, { + this.requestHeaders = { + ...this.requestHeaders, [http2.constants.HTTP2_HEADER_AUTHORITY]: 'localhost' - }) + } } this.socket = this._client.socket diff --git a/package.json b/package.json index bb8b3b561..41b9a250e 100644 --- a/package.json +++ b/package.json @@ -56,7 +56,7 @@ "devDependencies": { "bluebird": "^3.2.1", "browserify": "^16.5.2", - "browserify-istanbul": "^2.0.0", + "browserify-istanbul": "^3.0.0", "buffer-equal": "^1.0.1", "codecov": "^3.0.4", "coveralls": "^3.0.2",