@north-light/api 0.3.761 → 0.3.763

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.
@@ -8,12 +8,15 @@ import { hostname } from 'node:os';
8
8
  import { join } from 'node:path';
9
9
  import { CrtrError, general } from '../errors.js';
10
10
  import { atomicWriteJson, readJsonOrNull } from '../fs-utils.js';
11
+ import { withExclusiveDirectoryLockAsync } from '../exclusive-lock.js';
11
12
  import { secretsDir } from '../layout.js';
12
13
  import { ExitCode } from '../../types.js';
13
14
  import { northlightUnreachable } from './reach.js';
14
15
  export const personCredentialPath = () => join(secretsDir(), 'person-credential');
15
16
  export const personBearerPath = () => join(secretsDir(), 'person-bearer.json');
16
17
  const pendingPath = () => join(secretsDir(), 'person-credential.pending');
18
+ /** Serializes bearer refreshes across every crtr process on this computer, so a stale cache costs one exchange, not one per process. */
19
+ const refreshLockPath = () => join(secretsDir(), 'person-bearer.lock');
17
20
  const REFRESH_MARGIN_MS = 30_000;
18
21
  const sha256Hex = (text) => createHash('sha256').update(text).digest('hex');
19
22
  const trimUrl = (url) => url.replace(/\/+$/, '');
@@ -137,12 +140,23 @@ export async function personBearer(directoryUrl) {
137
140
  if (credential === undefined)
138
141
  throw notSignedIn('this computer is not signed in');
139
142
  const directory = trimUrl(directoryUrl ?? credential.directoryUrl);
140
- const cached = readJsonOrNull(personBearerPath());
141
- if (cached !== null && cached.directoryUrl === directory && typeof cached.access_token === 'string' && cached.expires_at_ms - Date.now() > REFRESH_MARGIN_MS)
142
- return cached.access_token;
143
- const bearer = await exchange(directory, credential.secret, true);
144
- writeBearer(directory, bearer);
145
- return bearer.access_token;
143
+ const fresh = () => {
144
+ const cached = readJsonOrNull(personBearerPath());
145
+ return cached !== null && cached.directoryUrl === directory && typeof cached.access_token === 'string' && cached.expires_at_ms - Date.now() > REFRESH_MARGIN_MS ? cached.access_token : undefined;
146
+ };
147
+ const hit = fresh();
148
+ if (hit !== undefined)
149
+ return hit;
150
+ // Many crtr processes see the cache go stale at the same moment; without this lock each one exchanged, and the directory's
151
+ // 600-an-hour limit per sign-in answered "Too many requests from this login". The first holder exchanges; the rest read its result.
152
+ return withExclusiveDirectoryLockAsync(refreshLockPath(), async () => {
153
+ const raced = fresh();
154
+ if (raced !== undefined)
155
+ return raced;
156
+ const bearer = await exchange(directory, credential.secret, true);
157
+ writeBearer(directory, bearer);
158
+ return bearer.access_token;
159
+ }, { timeoutMs: 40_000 });
146
160
  }
147
161
  /** A `fetch` for the control plane at `apiUrl`: each request carries the person's bearer for `directoryUrl` (fetched per request, so a long command never sends
148
162
  * an ended one), a sign-in refusal crosses as its own `CrtrError`, and a request that never got an HTTP answer is `northlight_unreachable` (a caller's own abort
@@ -167,8 +181,11 @@ export async function personAccount(directoryUrl) {
167
181
  if (credential === undefined)
168
182
  throw notSignedIn('this computer is not signed in');
169
183
  const directory = trimUrl(directoryUrl ?? credential.directoryUrl);
170
- const bearer = await exchange(directory, credential.secret, true);
171
- writeBearer(directory, bearer);
184
+ const bearer = await withExclusiveDirectoryLockAsync(refreshLockPath(), async () => {
185
+ const answer = await exchange(directory, credential.secret, true);
186
+ writeBearer(directory, answer);
187
+ return answer;
188
+ }, { timeoutMs: 40_000 });
172
189
  if (bearer.account === undefined)
173
190
  throw general('the directory\'s token answer names no account');
174
191
  return bearer.account;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@north-light/api",
3
- "version": "0.3.761",
3
+ "version": "0.3.763",
4
4
  "description": "Typed crtrd /v1 API contract — DTOs, route builders, the error contract, the CrtrClient, and the command-plugin manifest format. Zero runtime dependencies.",
5
5
  "type": "module",
6
6
  "main": "./dist/api/index.js",