Skip to content

Commit

Permalink
fix: don't copy httpAgent/httpsAgent [EXT-5676] (#526)
Browse files Browse the repository at this point in the history
Users face issues employing custom `httpsAgent`s that utilize private
fields. Since these fields are not copyable, they must be excluded 
from the deep copy that is utilized to ensure we avoid mutating things 
unnecessarily.
  • Loading branch information
jjolton-contentful authored Nov 20, 2024
1 parent 0addd64 commit f923fc4
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/create-http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ import type { AxiosInstance, CreateHttpClientParams, DefaultOptions } from './ty
// Also enforces toplevel domain specified, no spaces and no protocol
const HOST_REGEX = /^(?!\w+:\/\/)([^\s:]+\.?[^\s:]+)(?::(\d+))?(?!:)$/

function copyHttpClientParams(options: CreateHttpClientParams): CreateHttpClientParams {
const copiedOptions = copy(options)
// httpAgent and httpsAgent cannot be copied because they can contain private fields
copiedOptions.httpAgent = options.httpAgent
copiedOptions.httpsAgent = options.httpsAgent
return copiedOptions
}

/**
* Create pre-configured axios instance
* @private
Expand Down Expand Up @@ -124,7 +132,7 @@ export default function createHttpClient(
newParams: Partial<CreateHttpClientParams>,
): AxiosInstance {
return createHttpClient(axios, {
...copy(options),
...copyHttpClientParams(options),
...newParams,
})
}
Expand Down

0 comments on commit f923fc4

Please sign in to comment.