Skip to content

Commit

Permalink
replaced usage of Object.assign with spread operator and bumped brows…
Browse files Browse the repository at this point in the history
…erify-istanbul
  • Loading branch information
parthverma1 committed Jul 22, 2024
1 parent 80f23eb commit d1c19cc
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 30 deletions.
31 changes: 19 additions & 12 deletions lib/autohttp/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -96,9 +99,10 @@ class AutoHttp2Agent extends EventEmitter {
return
}

const http1RequestOptions = Object.assign({}, options, {
const http1RequestOptions = {
...options,
agent: this.httpsAgent
})
}

let request
try {
Expand All @@ -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) => {
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions lib/autohttp/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 12 additions & 7 deletions lib/http2/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
19 changes: 11 additions & 8 deletions lib/http2/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit d1c19cc

Please sign in to comment.