@north-light/identity 0.3.414 → 0.3.416
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/index.d.ts +29 -2
- package/dist/index.js +81 -12
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -27,7 +27,31 @@ export type Scope = 'crtr:llm' | 'crtr:act' | 'crtr:schedule' | `crtr:memory:${'
|
|
|
27
27
|
export declare const ENFORCED: ReadonlySet<string>;
|
|
28
28
|
export declare function parseScope(value: string, grantee?: string): Scope;
|
|
29
29
|
export declare function covers(held: string, requested: string): boolean;
|
|
30
|
-
/**
|
|
30
|
+
/** A directory fetch ran past its deadline (or was aborted by its caller before that). `code` is `deadline_exceeded`;
|
|
31
|
+
* `deadlineAt` is the instant (epoch ms) the operation had to finish by. The `@north-light/sdk` maps it to its
|
|
32
|
+
* `DeadlineExceededError`. */
|
|
33
|
+
export declare class FetchDeadlineError extends Error {
|
|
34
|
+
readonly deadlineAt: number;
|
|
35
|
+
readonly url: string;
|
|
36
|
+
readonly code = "deadline_exceeded";
|
|
37
|
+
constructor(deadlineAt: number, url: string);
|
|
38
|
+
}
|
|
39
|
+
/** The default deadline for one `JwksCache.load`, covering its discovery read and its key-set read together. */
|
|
40
|
+
export declare const DEFAULT_JWKS_TIMEOUT_MS = 10000;
|
|
41
|
+
/**
|
|
42
|
+
* One `fetch` plus the reading of its answer (`read`, e.g. `response.json()`), bounded by one absolute deadline. The
|
|
43
|
+
* request is aborted when the deadline passes or `signal` fires, and the returned promise rejects at that moment
|
|
44
|
+
* even when `fetcher` ignores its signal, so a directory that accepts the connection and never answers cannot hold a
|
|
45
|
+
* caller (or a lock the caller holds) past `deadlineAt`. Rejects `FetchDeadlineError` at the deadline and the signal's
|
|
46
|
+
* reason when `signal` fired first.
|
|
47
|
+
*/
|
|
48
|
+
export declare function boundedFetch<T>(fetcher: typeof fetch, url: string, init: RequestInit, bounds: {
|
|
49
|
+
deadlineAt: number;
|
|
50
|
+
signal?: AbortSignal;
|
|
51
|
+
}, read: (response: Response) => Promise<T>): Promise<T>;
|
|
52
|
+
/** Keys are scoped to one configured issuer; failed refreshes keep the last successful set. Every `load` is bounded by
|
|
53
|
+
* one deadline (`timeoutMs`, default `DEFAULT_JWKS_TIMEOUT_MS`) over its discovery read and its key-set read together;
|
|
54
|
+
* callers that join an in-flight load share that load's deadline. */
|
|
31
55
|
export declare class JwksCache {
|
|
32
56
|
readonly issuer: string;
|
|
33
57
|
private readonly fetcher;
|
|
@@ -35,7 +59,10 @@ export declare class JwksCache {
|
|
|
35
59
|
private lastUnknownRefresh;
|
|
36
60
|
private loading;
|
|
37
61
|
private discovery;
|
|
38
|
-
|
|
62
|
+
private readonly timeoutMs;
|
|
63
|
+
constructor(issuer: string, fetcher?: typeof fetch, jwksUri?: string, options?: {
|
|
64
|
+
timeoutMs?: number;
|
|
65
|
+
});
|
|
39
66
|
load(): Promise<JSONWebKeySet>;
|
|
40
67
|
private fetchKeys;
|
|
41
68
|
forKid(kid: string | undefined): Promise<JSONWebKeySet>;
|
package/dist/index.js
CHANGED
|
@@ -71,7 +71,70 @@ export function covers(held, requested) {
|
|
|
71
71
|
}
|
|
72
72
|
return held === requested;
|
|
73
73
|
}
|
|
74
|
-
/**
|
|
74
|
+
/** A directory fetch ran past its deadline (or was aborted by its caller before that). `code` is `deadline_exceeded`;
|
|
75
|
+
* `deadlineAt` is the instant (epoch ms) the operation had to finish by. The `@north-light/sdk` maps it to its
|
|
76
|
+
* `DeadlineExceededError`. */
|
|
77
|
+
export class FetchDeadlineError extends Error {
|
|
78
|
+
deadlineAt;
|
|
79
|
+
url;
|
|
80
|
+
code = 'deadline_exceeded';
|
|
81
|
+
constructor(deadlineAt, url) {
|
|
82
|
+
super(`${url} did not answer by the deadline (${new Date(deadlineAt).toISOString()})`);
|
|
83
|
+
this.deadlineAt = deadlineAt;
|
|
84
|
+
this.url = url;
|
|
85
|
+
this.name = 'FetchDeadlineError';
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/** The default deadline for one `JwksCache.load`, covering its discovery read and its key-set read together. */
|
|
89
|
+
export const DEFAULT_JWKS_TIMEOUT_MS = 10_000;
|
|
90
|
+
/**
|
|
91
|
+
* One `fetch` plus the reading of its answer (`read`, e.g. `response.json()`), bounded by one absolute deadline. The
|
|
92
|
+
* request is aborted when the deadline passes or `signal` fires, and the returned promise rejects at that moment
|
|
93
|
+
* even when `fetcher` ignores its signal, so a directory that accepts the connection and never answers cannot hold a
|
|
94
|
+
* caller (or a lock the caller holds) past `deadlineAt`. Rejects `FetchDeadlineError` at the deadline and the signal's
|
|
95
|
+
* reason when `signal` fired first.
|
|
96
|
+
*/
|
|
97
|
+
export async function boundedFetch(fetcher, url, init, bounds, read) {
|
|
98
|
+
const { deadlineAt, signal } = bounds;
|
|
99
|
+
if (signal?.aborted)
|
|
100
|
+
throw signal.reason;
|
|
101
|
+
const remaining = deadlineAt - Date.now();
|
|
102
|
+
if (remaining <= 0)
|
|
103
|
+
throw new FetchDeadlineError(deadlineAt, url);
|
|
104
|
+
const controller = new AbortController();
|
|
105
|
+
let timer;
|
|
106
|
+
let onAbort;
|
|
107
|
+
const stop = new Promise((_resolve, reject) => {
|
|
108
|
+
timer = setTimeout(() => { const error = new FetchDeadlineError(deadlineAt, url); controller.abort(error); reject(error); }, remaining);
|
|
109
|
+
if (signal) {
|
|
110
|
+
onAbort = () => { controller.abort(signal.reason); reject(signal.reason); };
|
|
111
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
const work = (async () => {
|
|
115
|
+
const response = await fetcher(url, { ...init, signal: controller.signal });
|
|
116
|
+
// A response that arrives after the deadline won is discarded: close it so its connection is not retained.
|
|
117
|
+
if (controller.signal.aborted) {
|
|
118
|
+
void response.body?.cancel().catch(() => { });
|
|
119
|
+
throw controller.signal.reason;
|
|
120
|
+
}
|
|
121
|
+
return read(response);
|
|
122
|
+
})();
|
|
123
|
+
// The loser of the race must not surface as an unhandled rejection.
|
|
124
|
+
work.catch(() => { });
|
|
125
|
+
stop.catch(() => { });
|
|
126
|
+
try {
|
|
127
|
+
return await Promise.race([work, stop]);
|
|
128
|
+
}
|
|
129
|
+
finally {
|
|
130
|
+
clearTimeout(timer);
|
|
131
|
+
if (onAbort)
|
|
132
|
+
signal?.removeEventListener('abort', onAbort);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
/** Keys are scoped to one configured issuer; failed refreshes keep the last successful set. Every `load` is bounded by
|
|
136
|
+
* one deadline (`timeoutMs`, default `DEFAULT_JWKS_TIMEOUT_MS`) over its discovery read and its key-set read together;
|
|
137
|
+
* callers that join an in-flight load share that load's deadline. */
|
|
75
138
|
export class JwksCache {
|
|
76
139
|
issuer;
|
|
77
140
|
fetcher;
|
|
@@ -79,16 +142,20 @@ export class JwksCache {
|
|
|
79
142
|
lastUnknownRefresh = -Infinity;
|
|
80
143
|
loading;
|
|
81
144
|
discovery;
|
|
82
|
-
|
|
145
|
+
timeoutMs;
|
|
146
|
+
constructor(issuer, fetcher = fetch, jwksUri, options = {}) {
|
|
83
147
|
this.issuer = issuer;
|
|
84
148
|
this.fetcher = fetcher;
|
|
85
149
|
if (jwksUri)
|
|
86
150
|
this.discovery = { jwks_uri: jwksUri };
|
|
151
|
+
this.timeoutMs = options.timeoutMs ?? DEFAULT_JWKS_TIMEOUT_MS;
|
|
152
|
+
if (!Number.isFinite(this.timeoutMs) || this.timeoutMs <= 0 || this.timeoutMs > 2_147_483_647)
|
|
153
|
+
throw new RangeError('JwksCache timeoutMs must be a positive finite number of at most 2147483647');
|
|
87
154
|
}
|
|
88
155
|
async load() {
|
|
89
156
|
if (this.loading)
|
|
90
157
|
return this.loading;
|
|
91
|
-
const loading = this.fetchKeys();
|
|
158
|
+
const loading = this.fetchKeys(Date.now() + this.timeoutMs);
|
|
92
159
|
this.loading = loading;
|
|
93
160
|
try {
|
|
94
161
|
return await loading;
|
|
@@ -97,20 +164,22 @@ export class JwksCache {
|
|
|
97
164
|
this.loading = undefined;
|
|
98
165
|
}
|
|
99
166
|
}
|
|
100
|
-
async fetchKeys() {
|
|
167
|
+
async fetchKeys(deadlineAt) {
|
|
101
168
|
if (!this.discovery) {
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
169
|
+
const value = await boundedFetch(this.fetcher, `${this.issuer.replace(/\/$/, '')}/.well-known/openid-configuration`, {}, { deadlineAt }, async (response) => {
|
|
170
|
+
if (!response.ok)
|
|
171
|
+
throw new Error(`Directory discovery failed: ${response.status}`);
|
|
172
|
+
return await response.json();
|
|
173
|
+
});
|
|
106
174
|
if (!value || typeof value !== 'object' || !('jwks_uri' in value) || typeof value.jwks_uri !== 'string')
|
|
107
175
|
throw new Error('Directory discovery has no jwks_uri');
|
|
108
176
|
this.discovery = { jwks_uri: value.jwks_uri };
|
|
109
177
|
}
|
|
110
|
-
const
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
178
|
+
const keys = await boundedFetch(this.fetcher, this.discovery.jwks_uri, {}, { deadlineAt }, async (response) => {
|
|
179
|
+
if (!response.ok)
|
|
180
|
+
throw new Error(`Directory JWKS failed: ${response.status}`);
|
|
181
|
+
return await response.json();
|
|
182
|
+
});
|
|
114
183
|
if (!keys || typeof keys !== 'object' || !('keys' in keys) || !Array.isArray(keys.keys))
|
|
115
184
|
throw new Error('Invalid directory JWKS');
|
|
116
185
|
this.keys = keys;
|