Skip to content

Commit 35f6509

Browse files
committedFeb 25, 2025
fix: use [customFetch] when provided to createRemoteJWKSet
closes #760
1 parent cd06359 commit 35f6509

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed
 

‎src/jwks/remote.ts

+27-25
Original file line numberDiff line numberDiff line change
@@ -157,17 +157,15 @@ export type FetchImplementation = (
157157

158158
async function fetchJwks(
159159
url: string,
160-
options: {
161-
headers: Headers
162-
signal: AbortSignal
163-
[customFetch]?: FetchImplementation
164-
},
160+
headers: Headers,
161+
signal: AbortSignal,
162+
fetchImpl: FetchImplementation = fetch,
165163
) {
166-
const response = await (options?.[customFetch] || fetch)(url, {
164+
const response = await fetchImpl(url, {
167165
method: 'GET',
168-
signal: options.signal,
166+
signal,
169167
redirect: 'manual',
170-
headers: options.headers,
168+
headers,
171169
}).catch((err) => {
172170
if (err.name === 'TimeoutError') {
173171
throw new JWKSTimeout()
@@ -315,7 +313,9 @@ class RemoteJWKSet {
315313

316314
private _pendingFetch?: Promise<unknown>
317315

318-
private _options: Pick<RemoteJWKSetOptions, 'headers'>
316+
private _headers: Headers
317+
318+
private [customFetch]?: FetchImplementation
319319

320320
private _local!: ReturnType<typeof createLocalJWKSet>
321321

@@ -326,12 +326,23 @@ class RemoteJWKSet {
326326
throw new TypeError('url must be an instance of URL')
327327
}
328328
this._url = new URL(url.href)
329-
this._options = { headers: options?.headers }
329+
330330
this._timeoutDuration =
331331
typeof options?.timeoutDuration === 'number' ? options?.timeoutDuration : 5000
332332
this._cooldownDuration =
333333
typeof options?.cooldownDuration === 'number' ? options?.cooldownDuration : 30000
334334
this._cacheMaxAge = typeof options?.cacheMaxAge === 'number' ? options?.cacheMaxAge : 600000
335+
this._headers = new Headers(options?.headers)
336+
if (USER_AGENT && !this._headers.has('User-Agent')) {
337+
this._headers.set('User-Agent', USER_AGENT)
338+
}
339+
340+
if (!this._headers.has('accept')) {
341+
this._headers.set('accept', 'application/json')
342+
this._headers.append('accept', 'application/jwk-set+json')
343+
}
344+
345+
this[customFetch] = options?.[customFetch]
335346

336347
if (options?.[jwksCache] !== undefined) {
337348
this._cache = options?.[jwksCache]
@@ -382,21 +393,12 @@ class RemoteJWKSet {
382393
this._pendingFetch = undefined
383394
}
384395

385-
const headers = new Headers(this._options.headers)
386-
if (USER_AGENT && !headers.has('User-Agent')) {
387-
headers.set('User-Agent', USER_AGENT)
388-
this._options.headers = Object.fromEntries(headers.entries())
389-
}
390-
391-
if (!headers.has('accept')) {
392-
headers.set('accept', 'application/json')
393-
headers.append('accept', 'application/jwk-set+json')
394-
}
395-
396-
this._pendingFetch ||= fetchJwks(this._url.href, {
397-
headers,
398-
signal: AbortSignal.timeout(this._timeoutDuration),
399-
})
396+
this._pendingFetch ||= fetchJwks(
397+
this._url.href,
398+
this._headers,
399+
AbortSignal.timeout(this._timeoutDuration),
400+
this[customFetch],
401+
)
400402
.then((json) => {
401403
this._local = createLocalJWKSet(json as unknown as types.JSONWebKeySet)
402404
if (this._cache) {

0 commit comments

Comments
 (0)
Please sign in to comment.