@meistrari/data-token 0.0.9 → 0.0.11

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.
@@ -23,4 +23,4 @@ export type DataTokenContext = {
23
23
  * app.get('/', c => c.json({ data: c.get('authData') }))
24
24
  * ```
25
25
  */
26
- export declare function createDataTokenMiddleware<Env extends DataTokenContext>(url: string, skipOnError?: boolean, getToken?: (c: Context<Env>) => string): import("hono").MiddlewareHandler<Env, string, {}>;
26
+ export declare function createDataTokenMiddleware<Env extends DataTokenContext>(url: string | ((c: Context<Env>) => string), skipOnError?: boolean, getToken?: (c: Context<Env>) => string): import("hono").MiddlewareHandler<Env, string, {}>;
@@ -20,7 +20,8 @@ import { getValidator } from '..';
20
20
  */
21
21
  export function createDataTokenMiddleware(url, skipOnError = false, getToken = c => c.req.header('x-data-token') ?? '') {
22
22
  return createMiddleware(async (c, next) => {
23
- const validator = getValidator(url);
23
+ const jwksUrl = typeof url === 'function' ? url(c) : url;
24
+ const validator = getValidator(jwksUrl);
24
25
  const token = getToken(c);
25
26
  const data = await validator(token).catch(() => {
26
27
  if (skipOnError) {
package/dist/index.js CHANGED
@@ -1,9 +1,12 @@
1
- import { createRemoteJWKSet, jwtVerify } from 'jose';
1
+ import { ExponentialBackoff, handleType, retry } from 'cockatiel';
2
+ import { createRemoteJWKSet, errors, jwtVerify } from 'jose';
2
3
  import { DataToken } from './types';
4
+ const retryJWKS = retry(handleType(errors.JWKSTimeout)
5
+ .orWhen(err => err.message.includes('ECONNRESET')), { maxAttempts: 3, backoff: new ExponentialBackoff() });
3
6
  export function getValidator(jwksUrl, { algorithms } = { algorithms: ['RS256'] }) {
4
- const JWKS = createRemoteJWKSet(new URL(jwksUrl));
7
+ const JWKS = createRemoteJWKSet(new URL(jwksUrl), { cacheMaxAge: 0, headers: { 'cache-control': 'no-cache', 'pragma': 'no-cache' } });
5
8
  return async (token) => {
6
- const { payload } = await jwtVerify(token, JWKS, { algorithms });
9
+ const { payload } = await retryJWKS.execute(async () => jwtVerify(token, JWKS, { algorithms }));
7
10
  return DataToken.parse(payload);
8
11
  };
9
12
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@meistrari/data-token",
3
3
  "type": "module",
4
- "version": "0.0.9",
4
+ "version": "0.0.11",
5
5
  "description": "Internal library to verify and decode the API Gateway data token",
6
6
  "author": "Roz <roz@meistrari.com>",
7
7
  "license": "UNLICENSED",
@@ -28,6 +28,7 @@
28
28
  "prepack": "bun run build"
29
29
  },
30
30
  "dependencies": {
31
+ "cockatiel": "^3.2.1",
31
32
  "jose": "^5.8.0",
32
33
  "zod": "^3.23.8"
33
34
  },