Skip to content

Commit

Permalink
fix: only pass documented options to 'http.request()'
Browse files Browse the repository at this point in the history
https://nodejs.org/api/http.html#httprequestoptions-callback
Passing in the extra fields -- in particular 'href' and 'origin' --
makes node v20 believe the options object is a `URL` instance.
For a brief period, this broke with some node v20 nightlies.

Fixes: #59
  • Loading branch information
trentm committed Mar 30, 2023
1 parent dcf57ba commit 49735be
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/connection/HttpConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,17 +332,14 @@ export default class HttpConnection extends BaseConnection {

buildRequestObject (params: ConnectionRequestParams, options: ConnectionRequestOptions): http.ClientRequestArgs {
const url = this.url
let search = url.search
let pathname = url.pathname
const request = {
protocol: url.protocol,
hostname: url.hostname[0] === '['
? url.hostname.slice(1, -1)
: url.hostname,
hash: url.hash,
search: url.search,
pathname: url.pathname,
path: '',
href: url.href,
origin: url.origin,
// https://github.com/elastic/elasticsearch-js/issues/843
port: url.port !== '' ? url.port : undefined,
headers: this.headers,
Expand All @@ -354,12 +351,12 @@ export default class HttpConnection extends BaseConnection {
for (let i = 0, len = paramsKeys.length; i < len; i++) {
const key = paramsKeys[i]
if (key === 'path') {
request.pathname = resolve(request.pathname, params[key])
pathname = resolve(pathname, params[key])
} else if (key === 'querystring' && Boolean(params[key])) {
if (request.search === '') {
request.search = `?${params[key] as string}`
if (search === '') {
search = `?${params[key] as string}`
} else {
request.search += `&${params[key] as string}`
search += `&${params[key] as string}`
}
} else if (key === 'headers') {
request.headers = Object.assign({}, request.headers, params.headers)
Expand All @@ -369,7 +366,7 @@ export default class HttpConnection extends BaseConnection {
}
}

request.path = request.pathname + request.search
request.path = pathname + search

return request
}
Expand Down

0 comments on commit 49735be

Please sign in to comment.