@cfworker/jwt 4.0.0 → 4.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/jwks.js CHANGED
@@ -32,11 +32,18 @@ export async function importKey(iss, jwk) {
32
32
  importedKeys[iss] = importedKeys[iss] || {};
33
33
  importedKeys[iss][jwk.kid ?? 'default'] = key;
34
34
  }
35
+ const jwksImports = {};
35
36
  export async function getKey(decoded) {
36
- let { header: { kid = 'default' }, payload: { iss } } = decoded;
37
+ const { header: { kid = 'default' }, payload: { iss } } = decoded;
37
38
  if (!importedKeys[iss]) {
38
- const jwks = await getJwks(iss);
39
- await Promise.all(jwks.keys.map(jwk => importKey(iss, jwk)));
39
+ if (!jwksImports[iss]) {
40
+ jwksImports[iss] = getJwks(iss)
41
+ .then(jwks => Promise.all(jwks.keys.map(jwk => importKey(iss, jwk))))
42
+ .finally(() => {
43
+ delete jwksImports[iss];
44
+ });
45
+ }
46
+ await jwksImports[iss];
40
47
  }
41
48
  const key = importedKeys[iss][kid];
42
49
  if (!key) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cfworker/jwt",
3
- "version": "4.0.0",
3
+ "version": "4.0.1",
4
4
  "description": "Tiny lib for decoding JWTs and verifying signatures, using native crypto APIs",
5
5
  "keywords": [
6
6
  "jwt",
package/src/jwks.ts CHANGED
@@ -65,18 +65,26 @@ export async function importKey(iss: string, jwk: JsonWebKey) {
65
65
  importedKeys[iss][jwk.kid ?? 'default'] = key;
66
66
  }
67
67
 
68
+ const jwksImports: Record<string, Promise<unknown>> = {};
69
+
68
70
  /**
69
71
  * Get the CryptoKey associated with the JWT's issuer.
70
72
  */
71
73
  export async function getKey(decoded: DecodedJwt): Promise<CryptoKey> {
72
- let {
74
+ const {
73
75
  header: { kid = 'default' },
74
76
  payload: { iss }
75
77
  } = decoded;
76
78
 
77
79
  if (!importedKeys[iss]) {
78
- const jwks = await getJwks(iss);
79
- await Promise.all(jwks.keys.map(jwk => importKey(iss, jwk)));
80
+ if (!jwksImports[iss]) {
81
+ jwksImports[iss] = getJwks(iss)
82
+ .then(jwks => Promise.all(jwks.keys.map(jwk => importKey(iss, jwk))))
83
+ .finally(() => {
84
+ delete jwksImports[iss];
85
+ });
86
+ }
87
+ await jwksImports[iss];
80
88
  }
81
89
 
82
90
  const key = importedKeys[iss][kid];