@@ -103,36 +103,26 @@ class RemoteJWKSet<T extends KeyLike = KeyLike> extends LocalJWKSet<T> {
103
103
}
104
104
105
105
async reload ( ) {
106
- // see https://github.com/panva/jose/issues/355
106
+ // Do not assume a fetch created in another request reliably resolves
107
+ // see https://github.com/panva/jose/issues/355 and https://github.com/panva/jose/issues/509
107
108
if ( this . _pendingFetch && isCloudflareWorkers ( ) ) {
108
- return new Promise < void > ( ( resolve ) => {
109
- const isDone = ( ) => {
110
- if ( this . _pendingFetch === undefined ) {
111
- resolve ( )
112
- } else {
113
- setTimeout ( isDone , 5 )
114
- }
115
- }
116
- isDone ( )
117
- } )
109
+ this . _pendingFetch = undefined
118
110
}
119
111
120
- if ( ! this . _pendingFetch ) {
121
- this . _pendingFetch = fetchJwks ( this . _url , this . _timeoutDuration , this . _options )
122
- . then ( ( json ) => {
123
- if ( ! isJWKSLike ( json ) ) {
124
- throw new JWKSInvalid ( 'JSON Web Key Set malformed' )
125
- }
126
-
127
- this . _jwks = { keys : json . keys }
128
- this . _jwksTimestamp = Date . now ( )
129
- this . _pendingFetch = undefined
130
- } )
131
- . catch ( ( err : Error ) => {
132
- this . _pendingFetch = undefined
133
- throw err
134
- } )
135
- }
112
+ this . _pendingFetch ||= fetchJwks ( this . _url , this . _timeoutDuration , this . _options )
113
+ . then ( ( json ) => {
114
+ if ( ! isJWKSLike ( json ) ) {
115
+ throw new JWKSInvalid ( 'JSON Web Key Set malformed' )
116
+ }
117
+
118
+ this . _jwks = { keys : json . keys }
119
+ this . _jwksTimestamp = Date . now ( )
120
+ this . _pendingFetch = undefined
121
+ } )
122
+ . catch ( ( err : Error ) => {
123
+ this . _pendingFetch = undefined
124
+ throw err
125
+ } )
136
126
137
127
await this . _pendingFetch
138
128
}
0 commit comments