@vercel/connect 2.9.3 → 2.9.4-snapshot-92f4d50bf3fb

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.
@@ -1,4 +1,9 @@
1
1
  export function normalizeTokenSubject(subject) {
2
+ // Untyped callers can omit the subject; Connect then resolves the caller, so
3
+ // the request goes out without one instead of throwing here.
4
+ if (!subject) {
5
+ return subject;
6
+ }
2
7
  if ((subject.type === undefined || subject.type === 'token') &&
3
8
  typeof subject.token === 'string') {
4
9
  return { type: 'token', token: subject.token };
package/dist/token.js CHANGED
@@ -42,10 +42,13 @@ export async function getTokenResponse(connector, params, options) {
42
42
  const requestParams = normalizeTokenParams(params);
43
43
  const bufferMs = requestParams.validityBufferMs ?? DEFAULT_VALIDITY_BUFFER_MS;
44
44
  const cacheKey = tokenCacheKey(connector, requestParams, options);
45
- if (options?.forceRefresh) {
45
+ // Without a subject, Connect resolves it from the caller's credential, which the
46
+ // cache key doesn't include, so another caller could be served this token.
47
+ const cacheable = requestParams.subject !== undefined;
48
+ if (cacheable && options?.forceRefresh) {
46
49
  cache.delete(cacheKey);
47
50
  }
48
- else {
51
+ else if (cacheable) {
49
52
  const cached = cache.get(cacheKey);
50
53
  if (cached) {
51
54
  const now = Date.now();
@@ -72,10 +75,12 @@ export async function getTokenResponse(connector, params, options) {
72
75
  throw await createConnectErrorFromResponse(response, 'Failed to get token');
73
76
  }
74
77
  const data = await response.json();
75
- if (cache.size >= MAX_CACHE_SIZE) {
76
- evictLru();
78
+ if (cacheable) {
79
+ if (cache.size >= MAX_CACHE_SIZE) {
80
+ evictLru();
81
+ }
82
+ cache.set(cacheKey, { response: data, lastUsed: Date.now() });
77
83
  }
78
- cache.set(cacheKey, { response: data, lastUsed: Date.now() });
79
84
  return data;
80
85
  }
81
86
  export async function revokeToken(connector, params, options) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vercel/connect",
3
- "version": "2.9.3",
3
+ "version": "2.9.4-snapshot-92f4d50bf3fb",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "repository": {