@vercel/connect 2.9.4 → 2.9.5
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/token.d.ts +9 -4
- package/dist/token.js +50 -9
- package/package.json +1 -1
package/dist/token.d.ts
CHANGED
|
@@ -140,9 +140,14 @@ export declare function revokeToken(connector: string, params: {
|
|
|
140
140
|
* round trip on every call the way {@link ConnectOptions.forceRefresh}
|
|
141
141
|
* does.
|
|
142
142
|
*
|
|
143
|
-
* The cache key includes `connector`, every field of `params`,
|
|
144
|
-
* team/org scope
|
|
145
|
-
*
|
|
143
|
+
* The cache key includes `connector`, every field of `params`, the
|
|
144
|
+
* team/org scope, and the explicit {@link ConnectOptions.vercelToken} when
|
|
145
|
+
* one was passed. Pass the same `params` and scope options used for the
|
|
146
|
+
* original {@link getTokenResponse} call. Pass `options.vercelToken` to drop
|
|
147
|
+
* only the entry fetched with that credential; omit it to drop the matching
|
|
148
|
+
* entry for every credential. No-op when no matching entry exists.
|
|
146
149
|
*/
|
|
147
|
-
export declare function deleteTokenCacheEntry(connector: string, params: ConnectTokenParams, options?:
|
|
150
|
+
export declare function deleteTokenCacheEntry(connector: string, params: ConnectTokenParams, options?: TokenCacheKeyOptions): void;
|
|
151
|
+
type TokenCacheKeyOptions = ConnectRequestScope & Pick<ConnectOptions, 'vercelToken'>;
|
|
148
152
|
export declare function createConnectErrorFromResponse(response: Response, fallbackMessage: string): Promise<ConnectError>;
|
|
153
|
+
export {};
|
package/dist/token.js
CHANGED
|
@@ -74,7 +74,7 @@ export async function getTokenResponse(connector, params, options) {
|
|
|
74
74
|
if (!response.ok) {
|
|
75
75
|
throw await createConnectErrorFromResponse(response, 'Failed to get token');
|
|
76
76
|
}
|
|
77
|
-
const data = await response
|
|
77
|
+
const data = await readTokenResponse(response);
|
|
78
78
|
if (cacheable) {
|
|
79
79
|
if (cache.size >= MAX_CACHE_SIZE) {
|
|
80
80
|
evictLru();
|
|
@@ -117,28 +117,69 @@ export async function revokeToken(connector, params, options) {
|
|
|
117
117
|
* round trip on every call the way {@link ConnectOptions.forceRefresh}
|
|
118
118
|
* does.
|
|
119
119
|
*
|
|
120
|
-
* The cache key includes `connector`, every field of `params`,
|
|
121
|
-
* team/org scope
|
|
122
|
-
*
|
|
120
|
+
* The cache key includes `connector`, every field of `params`, the
|
|
121
|
+
* team/org scope, and the explicit {@link ConnectOptions.vercelToken} when
|
|
122
|
+
* one was passed. Pass the same `params` and scope options used for the
|
|
123
|
+
* original {@link getTokenResponse} call. Pass `options.vercelToken` to drop
|
|
124
|
+
* only the entry fetched with that credential; omit it to drop the matching
|
|
125
|
+
* entry for every credential. No-op when no matching entry exists.
|
|
123
126
|
*/
|
|
124
127
|
export function deleteTokenCacheEntry(connector, params, options) {
|
|
125
|
-
|
|
128
|
+
const requestParams = normalizeTokenParams(params);
|
|
129
|
+
if (options?.vercelToken !== undefined) {
|
|
130
|
+
cache.delete(tokenCacheKey(connector, requestParams, options));
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
const prefix = tokenRequestKey(connector, requestParams, options);
|
|
134
|
+
for (const key of cache.keys()) {
|
|
135
|
+
if (key.startsWith(prefix)) {
|
|
136
|
+
cache.delete(key);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
126
139
|
}
|
|
127
140
|
const DEFAULT_VALIDITY_BUFFER_MS = 30_000;
|
|
128
141
|
const MAX_CACHE_SIZE = 100;
|
|
129
142
|
const cache = new Map();
|
|
130
143
|
/**
|
|
131
|
-
* Cache key for a `(connector, params)`
|
|
132
|
-
* equal arguments so {@link getTokenResponse} and
|
|
144
|
+
* Cache key for a `(connector, params, scope, credential)` tuple. Stable
|
|
145
|
+
* across calls with equal arguments so {@link getTokenResponse} and
|
|
133
146
|
* {@link deleteTokenCacheEntry} address the same entry.
|
|
147
|
+
*
|
|
148
|
+
* An explicit `vercelToken` is part of the key: one process can call
|
|
149
|
+
* Connect for several teams or projects, and a connector UID such as
|
|
150
|
+
* `oauth/linear` with the same subject resolves to a different grant for
|
|
151
|
+
* each of them, even within one team scope. Calls that use the ambient OIDC
|
|
152
|
+
* token share one partition, since a deployment's OIDC identity does not
|
|
153
|
+
* change between calls.
|
|
134
154
|
*/
|
|
135
155
|
function tokenCacheKey(connector, params, options) {
|
|
136
|
-
return
|
|
156
|
+
return (tokenRequestKey(connector, params, options) +
|
|
157
|
+
JSON.stringify({ vercelToken: options?.vercelToken ?? null }));
|
|
158
|
+
}
|
|
159
|
+
/** The part of {@link tokenCacheKey} derived from the request and scope. */
|
|
160
|
+
function tokenRequestKey(connector, params, options) {
|
|
161
|
+
return `${JSON.stringify({
|
|
137
162
|
connector,
|
|
138
163
|
...params,
|
|
139
164
|
teamId: options?.teamId,
|
|
140
165
|
orgId: options?.orgId,
|
|
141
|
-
})
|
|
166
|
+
})}\n`;
|
|
167
|
+
}
|
|
168
|
+
async function readTokenResponse(response) {
|
|
169
|
+
let data;
|
|
170
|
+
try {
|
|
171
|
+
data = await response.json();
|
|
172
|
+
}
|
|
173
|
+
catch {
|
|
174
|
+
throw new ConnectError(`Failed to get token: ${response.status} response is not valid JSON`, { status: response.status, statusText: response.statusText });
|
|
175
|
+
}
|
|
176
|
+
if (!isRecord(data) ||
|
|
177
|
+
typeof data.token !== 'string' ||
|
|
178
|
+
typeof data.expiresAt !== 'number' ||
|
|
179
|
+
!Number.isFinite(data.expiresAt)) {
|
|
180
|
+
throw new ConnectError(`Failed to get token: ${response.status} response is missing token or expiresAt`, { status: response.status, statusText: response.statusText });
|
|
181
|
+
}
|
|
182
|
+
return data;
|
|
142
183
|
}
|
|
143
184
|
function evictLru() {
|
|
144
185
|
let oldestKey;
|