Skip to content

Commit

Permalink
fix: concurrent fetch await in cloudflare
Browse files Browse the repository at this point in the history
fixes #355
  • Loading branch information
panva committed Feb 4, 2022
1 parent 7835489 commit 41ad2d6
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/jwks/remote.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import fetchJwks from '../runtime/fetch_jwks.js'
import { isCloudflareWorkers } from '../runtime/env.js'

import type { KeyLike, JWSHeaderParameters, FlattenedJWSInput, GetKeyFunction } from '../types.d'
import { JWKSInvalid, JWKSNoMatchingKey } from '../util/errors.js'
import { JWKSInvalid, JWKSNoMatchingKey, JWKSTimeout } from '../util/errors.js'

import { isJWKSLike, LocalJWKSet } from './local.js'

Expand Down Expand Up @@ -89,6 +90,24 @@ class RemoteJWKSet extends LocalJWKSet {
}

async reload() {
// see https://github.com/panva/jose/issues/355
if (this._pendingFetch && isCloudflareWorkers()) {
return new Promise<void>((resolve, reject) => {
let timedout = false
setTimeout(() => (timedout = true), this._timeoutDuration)
const isDone = () => {
if (this._pendingFetch === undefined) {
resolve()
} else if (timedout) {
reject(new JWKSTimeout())
} else {
setTimeout(isDone, 5)
}
}
isDone()
})
}

if (!this._pendingFetch) {
this._pendingFetch = fetchJwks(this._url, this._timeoutDuration, this._options)
.then((json) => {
Expand Down

0 comments on commit 41ad2d6

Please sign in to comment.