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 7, 2022
1 parent 3d2a2b8 commit e44cd18
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/jwks/remote.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
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'
Expand Down Expand Up @@ -89,6 +90,20 @@ class RemoteJWKSet extends LocalJWKSet {
}

async reload() {
// see https://github.com/panva/jose/issues/355
if (this._pendingFetch && isCloudflareWorkers()) {
return new Promise<void>((resolve) => {
const isDone = () => {
if (this._pendingFetch === undefined) {
resolve()
} 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 e44cd18

Please sign in to comment.