@@ -157,17 +157,15 @@ export type FetchImplementation = (
157
157
158
158
async function fetchJwks (
159
159
url : string ,
160
- options : {
161
- headers : Headers
162
- signal : AbortSignal
163
- [ customFetch ] ?: FetchImplementation
164
- } ,
160
+ headers : Headers ,
161
+ signal : AbortSignal ,
162
+ fetchImpl : FetchImplementation = fetch ,
165
163
) {
166
- const response = await ( options ?. [ customFetch ] || fetch ) ( url , {
164
+ const response = await fetchImpl ( url , {
167
165
method : 'GET' ,
168
- signal : options . signal ,
166
+ signal,
169
167
redirect : 'manual' ,
170
- headers : options . headers ,
168
+ headers,
171
169
} ) . catch ( ( err ) => {
172
170
if ( err . name === 'TimeoutError' ) {
173
171
throw new JWKSTimeout ( )
@@ -315,7 +313,9 @@ class RemoteJWKSet {
315
313
316
314
private _pendingFetch ?: Promise < unknown >
317
315
318
- private _options : Pick < RemoteJWKSetOptions , 'headers' >
316
+ private _headers : Headers
317
+
318
+ private [ customFetch ] ?: FetchImplementation
319
319
320
320
private _local ! : ReturnType < typeof createLocalJWKSet >
321
321
@@ -326,12 +326,23 @@ class RemoteJWKSet {
326
326
throw new TypeError ( 'url must be an instance of URL' )
327
327
}
328
328
this . _url = new URL ( url . href )
329
- this . _options = { headers : options ?. headers }
329
+
330
330
this . _timeoutDuration =
331
331
typeof options ?. timeoutDuration === 'number' ? options ?. timeoutDuration : 5000
332
332
this . _cooldownDuration =
333
333
typeof options ?. cooldownDuration === 'number' ? options ?. cooldownDuration : 30000
334
334
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 ]
335
346
336
347
if ( options ?. [ jwksCache ] !== undefined ) {
337
348
this . _cache = options ?. [ jwksCache ]
@@ -382,21 +393,12 @@ class RemoteJWKSet {
382
393
this . _pendingFetch = undefined
383
394
}
384
395
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
+ )
400
402
. then ( ( json ) => {
401
403
this . _local = createLocalJWKSet ( json as unknown as types . JSONWebKeySet )
402
404
if ( this . _cache ) {
0 commit comments