@mentra/cloud-client 3.4.0-dev.1006 → 3.4.0-dev.1009

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mentra/cloud-client",
3
- "version": "3.4.0-dev.1006",
3
+ "version": "3.4.0-dev.1009",
4
4
  "type": "module",
5
5
  "main": "./src/index.ts",
6
6
  "exports": {
@@ -12,7 +12,7 @@
12
12
  "test": "bun test"
13
13
  },
14
14
  "dependencies": {
15
- "@mentra/cloud-protocol": "3.4.0-dev.1006",
15
+ "@mentra/cloud-protocol": "3.4.0-dev.1009",
16
16
  "tweetnacl": "^1.0.3"
17
17
  },
18
18
  "devDependencies": {
@@ -11,9 +11,10 @@
11
11
  * fetches the subject token on demand for subject tokens that themselves
12
12
  * expire. An `accessToken`/`refreshToken` variant skips straight to refresh.
13
13
  * - Near expiry (within a 60s margin) it refreshes at `/refresh` and rotates.
14
- * - Exchange, refresh, and each per-miniapp mint are single-flighted, so a
15
- * reconnect storm cannot fire a burst of competing requests (and a rotated
16
- * refresh token cannot be invalidated out from under a concurrent caller).
14
+ * - Exchange and refresh are single-flighted, so a reconnect storm cannot fire
15
+ * a burst of competing requests (and a rotated refresh token cannot be
16
+ * invalidated out from under a concurrent caller). Miniapp mints run one at
17
+ * a time per package and are shared as `getMiniappToken` describes.
17
18
  * - If Core definitively rejects the refresh token (see
18
19
  * `DEAD_REFRESH_TOKEN_ERRORS`) and the host can fetch a fresh subject token
19
20
  * on demand, we clear the dead refresh token and exchange once. If no fresh
@@ -63,10 +64,12 @@ export interface AuthModule {
63
64
  getRuntimeToken(opts?: {forceRefresh?: boolean}): Promise<string>
64
65
  // current Core token, refreshing as needed (Core-backed mode only).
65
66
  getCoreToken(opts?: {forceRefresh?: boolean}): Promise<string>
66
- // a miniapp-scoped token, cached per packageName and re-minted before expiry
67
+ // a miniapp-scoped token, cached per packageName and re-minted before expiry.
68
+ // Pass `{ fresh: true }` to skip the cache and mint now, for the moments the
69
+ // token's claims must be current (a miniapp opening).
67
70
  getMiniappToken(
68
71
  packageName: string,
69
- opts?: {minTtlMs?: number},
72
+ opts?: {minTtlMs?: number; fresh?: boolean},
70
73
  ): Promise<{token: string; expiresAt: number}>
71
74
  // Core-owned user/oem identity, read from the Core access token.
72
75
  // Runtime-only deployments do not expose this surface.
@@ -135,6 +138,16 @@ interface MiniappTokenEntry {
135
138
  expiresAt: number
136
139
  }
137
140
 
141
+ /**
142
+ * One miniapp mint. `requestSent()` turns true just before the request to Core
143
+ * leaves. Until then a caller who needs a token minted from now on can still
144
+ * share it, because Core has not yet read anything on its behalf.
145
+ */
146
+ interface MiniappMint {
147
+ promise: Promise<MiniappTokenEntry>
148
+ requestSent: () => boolean
149
+ }
150
+
138
151
  interface RuntimeTokenResponse {
139
152
  access_token: string
140
153
  token_type: string
@@ -259,6 +272,8 @@ export class Auth implements AuthModule {
259
272
 
260
273
  /** Miniapp tokens cached per packageName until near expiry. */
261
274
  private readonly miniappCache = new Map<string, MiniappTokenEntry>()
275
+ /** The newest mint per package (and session), until it settles. See `mintMiniappToken`. */
276
+ private readonly miniappMints = new Map<string, MiniappMint>()
262
277
  /** Core-brokered runtime token cache. */
263
278
  private runtimeToken: RuntimeTokenEntry | null = null
264
279
 
@@ -346,34 +361,61 @@ export class Auth implements AuthModule {
346
361
  /**
347
362
  * Mint (or return a cached) miniapp-scoped token for `packageName`.
348
363
  *
349
- * Cached per packageName and re-minted only when near expiry, so a steady
350
- * miniapp does not hit the cloud on every call. The mint is single-flighted
351
- * per packageName so two near-simultaneous launches of the same miniapp share
352
- * one request. The access token is the Bearer here and is never exposed to the
353
- * miniapp; only the returned miniapp-scoped token is.
364
+ * By default the token is cached per packageName and re-minted only when near
365
+ * expiry, so a steady miniapp does not hit the cloud on every call.
366
+ *
367
+ * `fresh: true` skips the cache and reaches Core, because what Core puts in a
368
+ * token (the features the user holds, today) can change within its hour. It
369
+ * can share a mint only if that mint's request has not left yet; a mint whose
370
+ * request is already out may have read the user's features before the change,
371
+ * so the caller gets a new mint, which starts once the earlier one settles.
372
+ * Callers waiting on that follow-up share it. `minTtlMs` has no use with
373
+ * `fresh`: the token it returns was minted for this call.
374
+ *
375
+ * Mints for one package never overlap, and a default call that finds one in
376
+ * flight takes it rather than starting another. The access token is the
377
+ * Bearer here and is never exposed to the miniapp; only the returned
378
+ * miniapp-scoped token is.
354
379
  */
355
380
  async getMiniappToken(
356
381
  packageName: string,
357
- opts?: {minTtlMs?: number},
382
+ opts?: {minTtlMs?: number; fresh?: boolean},
358
383
  ): Promise<{token: string; expiresAt: number}> {
359
384
  this.assertSessionActive()
360
385
  const generation = this.sessionGeneration
361
- const marginSeconds = this.tokenMarginSeconds(opts?.minTtlMs)
362
- const cached = this.miniappCache.get(packageName)
363
- if (cached && !this.isExpiring(cached.expiresAt, marginSeconds)) {
364
- return {token: cached.token, expiresAt: cached.expiresAt}
386
+ const fresh = opts?.fresh === true
387
+ if (!fresh) {
388
+ const marginSeconds = this.tokenMarginSeconds(opts?.minTtlMs)
389
+ const cached = this.miniappCache.get(packageName)
390
+ if (cached && !this.isExpiring(cached.expiresAt, marginSeconds)) {
391
+ return {token: cached.token, expiresAt: cached.expiresAt}
392
+ }
365
393
  }
366
394
 
367
- return this.store.singleFlight(`${MINIAPP_FLIGHT_PREFIX}${packageName}:${generation}`, async () => {
368
- // Re-check the cache inside the flight: a concurrent mint that resolved
369
- // while we were queued may have already filled it.
370
- const fresh = this.miniappCache.get(packageName)
371
- if (fresh && !this.isExpiring(fresh.expiresAt, marginSeconds)) {
372
- return {token: fresh.token, expiresAt: fresh.expiresAt}
373
- }
395
+ const entry = await this.mintMiniappToken(packageName, generation, fresh)
396
+ return {token: entry.token, expiresAt: entry.expiresAt}
397
+ }
374
398
 
399
+ /**
400
+ * The mint a caller should wait on. Mints for a package form a chain, one at a
401
+ * time; `miniappMints` holds the newest link.
402
+ *
403
+ * - A default caller takes the newest mint, whatever its state.
404
+ * - A fresh caller takes it only while its request is unsent. Otherwise it
405
+ * appends a mint that starts when the newest one settles, win or lose, and
406
+ * becomes the newest, so later fresh callers share it until it too sends.
407
+ */
408
+ private mintMiniappToken(packageName: string, generation: number, fresh: boolean): Promise<MiniappTokenEntry> {
409
+ const key = `${MINIAPP_FLIGHT_PREFIX}${packageName}:${generation}`
410
+ const newest = this.miniappMints.get(key)
411
+ if (newest && (!fresh || !newest.requestSent())) return newest.promise
412
+
413
+ let requestSent = false
414
+ const promise = (async () => {
415
+ if (newest) await newest.promise.catch(() => undefined)
375
416
  const accessToken = await this.getCoreToken()
376
417
  const http = this.requireCoreHttp()
418
+ requestSent = true
377
419
  // A dev build of a package is that package: the request names it and
378
420
  // nothing else. The phone has already applied the installed signer rule.
379
421
  const res = await http.post<MiniappTokenEntry>(MINIAPP_TOKEN_PATH, {packageName}, {bearer: accessToken})
@@ -382,8 +424,16 @@ export class Auth implements AuthModule {
382
424
  const entry: MiniappTokenEntry = {token: res.token, expiresAt: res.expiresAt}
383
425
  this.miniappCache.set(packageName, entry)
384
426
  this.logger.debug("minted miniapp token", {packageName})
385
- return {token: entry.token, expiresAt: entry.expiresAt}
386
- })
427
+ return entry
428
+ })()
429
+ const mint: MiniappMint = {promise, requestSent: () => requestSent}
430
+ this.miniappMints.set(key, mint)
431
+ // A later mint that replaced this one as the newest stays registered.
432
+ const release = () => {
433
+ if (this.miniappMints.get(key) === mint) this.miniappMints.delete(key)
434
+ }
435
+ void promise.then(release, release)
436
+ return promise
387
437
  }
388
438
 
389
439
  /**