@oxyhq/core 2.4.0 → 2.4.1

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.
@@ -445,6 +445,34 @@ function OxyServicesFedCMMixin(Base) {
445
445
  debug.log('Request timed out after', timeoutMs, 'ms (mediation:', requestedMediation + ')');
446
446
  controller.abort();
447
447
  }, timeoutMs);
448
+ // Hard settle guarantee for the timeout path.
449
+ //
450
+ // The `setTimeout` above aborts the request's `AbortController`, which is
451
+ // the COOPERATIVE cancel signal. For a regular `fetch` an abort deterministically
452
+ // rejects the awaited promise — but `navigator.credentials.get()` is a
453
+ // browser-internal FedCM primitive whose abort behaviour is NOT guaranteed
454
+ // to settle the awaited promise in every Chrome version / internal state
455
+ // (the credential request can sit "pending" while the browser-side flow is
456
+ // stuck, ignoring the signal). If that happens, `await credentials.get(...)`
457
+ // never resolves OR rejects, this IIFE hangs forever, and — because this is
458
+ // ONE step of the ordered cold-boot sequence — the whole cold boot hangs and
459
+ // the terminal `/sso` bounce never fires. That was the production hang.
460
+ //
461
+ // `settlePromise` races the credential lookup against a timer that ALWAYS
462
+ // resolves to `null` shortly after the abort deadline. The abort still fires
463
+ // first (so the browser is asked to cancel), but even if `credentials.get`
464
+ // never settles, the race resolves and the step falls through cleanly to the
465
+ // next cold-boot step. The small `FEDCM_ABORT_SETTLE_GRACE_MS` margin gives a
466
+ // well-behaved browser the chance to surface its own AbortError (preserving
467
+ // the existing error path) before we force a clean `null`.
468
+ let settleTimer;
469
+ const settlePromise = new Promise((resolve) => {
470
+ const ctor = this.constructor;
471
+ settleTimer = setTimeout(() => {
472
+ debug.log('Request hard-settled to null', timeoutMs + ctor.FEDCM_ABORT_SETTLE_GRACE_MS, 'ms (credentials.get never settled after abort)');
473
+ resolve(null);
474
+ }, timeoutMs + ctor.FEDCM_ABORT_SETTLE_GRACE_MS);
475
+ });
448
476
  // Normalise the caller's mode to the modern W3C value first. A modern
449
477
  // browser accepts it; an older one (Chrome 125–131) rejects it with a
450
478
  // synchronous TypeError, in which case we retry with the legacy value.
@@ -481,7 +509,13 @@ function OxyServicesFedCMMixin(Base) {
481
509
  debug.log('Calling navigator.credentials.get with mediation:', requestedMediation, modernMode ? `mode: ${modernMode}` : '');
482
510
  let credential;
483
511
  try {
484
- credential = await credentials.get(buildCredentialOptions(modernMode));
512
+ // Race the browser FedCM lookup against the hard settle guarantee so
513
+ // a `credentials.get` that ignores the abort signal can never hang
514
+ // the cold boot (see `settlePromise`).
515
+ credential = await Promise.race([
516
+ credentials.get(buildCredentialOptions(modernMode)),
517
+ settlePromise,
518
+ ]);
485
519
  }
486
520
  catch (modeError) {
487
521
  // Chrome 125–131 only knows the legacy 'button'/'widget' enum and
@@ -490,7 +524,10 @@ function OxyServicesFedCMMixin(Base) {
490
524
  if (modernMode && isUnknownModeEnumError(modeError)) {
491
525
  const legacyMode = MODERN_TO_LEGACY_MODE[modernMode];
492
526
  debug.log(`Browser rejected modern mode '${modernMode}'; retrying with legacy mode '${legacyMode}'`);
493
- credential = await credentials.get(buildCredentialOptions(legacyMode));
527
+ credential = await Promise.race([
528
+ credentials.get(buildCredentialOptions(legacyMode)),
529
+ settlePromise,
530
+ ]);
494
531
  }
495
532
  else {
496
533
  throw modeError;
@@ -517,6 +554,9 @@ function OxyServicesFedCMMixin(Base) {
517
554
  }
518
555
  finally {
519
556
  clearTimeout(timeout);
557
+ if (settleTimer !== undefined) {
558
+ clearTimeout(settleTimer);
559
+ }
520
560
  // Only reset the shared lock if it still belongs to THIS request. When an
521
561
  // interactive request aborts a slow silent one, the silent settles (and
522
562
  // runs this `finally`) AFTER the interactive has already taken over the
@@ -767,5 +807,15 @@ function OxyServicesFedCMMixin(Base) {
767
807
  // 20-30s cold-boot stall. Do NOT lower below 4s (it would clip live success).
768
808
  _a.FEDCM_SILENT_TIMEOUT = 4000 // 4 seconds for silent mediation
769
809
  ,
810
+ // Grace margin between the cooperative abort deadline (`FEDCM_SILENT_TIMEOUT`
811
+ // / `FEDCM_TIMEOUT`) and the HARD settle of `requestIdentityCredential`. The
812
+ // abort fires first; a well-behaved browser surfaces its own `AbortError`
813
+ // within this window (keeping the existing error path intact). If — as seen
814
+ // in production — `navigator.credentials.get()` ignores the abort and the
815
+ // awaited promise never settles, the hard settle resolves the request to
816
+ // `null` this many ms later, guaranteeing the cold-boot step always settles.
817
+ // 500ms is ample for a browser to deliver an abort rejection while keeping the
818
+ // worst-case dead wait tight (silent: 4.5s, interactive: 15.5s).
819
+ _a.FEDCM_ABORT_SETTLE_GRACE_MS = 500,
770
820
  _a;
771
821
  }
@@ -25,6 +25,15 @@
25
25
  */
26
26
  Object.defineProperty(exports, "__esModule", { value: true });
27
27
  exports.runColdBoot = runColdBoot;
28
+ /**
29
+ * The unique sentinel a step's `run()` resolves to (via the internal race)
30
+ * when the overall cold-boot deadline expires before that step settled. It is
31
+ * NOT a {@link ColdBootStepResult} — the runner detects it by identity and
32
+ * treats it as "this step did not settle in time; move on".
33
+ *
34
+ * @internal
35
+ */
36
+ const DEADLINE_EXPIRED = Symbol('coldBoot.deadlineExpired');
28
37
  /**
29
38
  * Run the ordered cold-boot steps and resolve to the first recovered session,
30
39
  * or `unauthenticated` if none recovers one.
@@ -41,31 +50,71 @@ exports.runColdBoot = runColdBoot;
41
50
  * 4. After the loop with no winner → `{ kind: 'unauthenticated' }`.
42
51
  */
43
52
  async function runColdBoot(options) {
44
- const { steps, onStepError } = options;
45
- for (const step of steps) {
46
- if (step.enabled) {
47
- let isEnabled;
53
+ const { steps, onStepError, overallDeadlineMs, onStepDeadline } = options;
54
+ // Arm the optional overall deadline. The budget is SHARED across the whole
55
+ // loop (not reset per step): a single timer resolves a reusable
56
+ // `DEADLINE_EXPIRED` sentinel that every per-step race can observe. Once it
57
+ // fires, later steps race against an already-resolved promise and so never
58
+ // block, yet the loop keeps iterating so the terminal step still fires.
59
+ const deadlineMs = typeof overallDeadlineMs === 'number' &&
60
+ Number.isFinite(overallDeadlineMs) &&
61
+ overallDeadlineMs > 0
62
+ ? overallDeadlineMs
63
+ : null;
64
+ let deadlineTimer;
65
+ let deadlinePromise;
66
+ if (deadlineMs !== null) {
67
+ deadlinePromise = new Promise((resolve) => {
68
+ deadlineTimer = setTimeout(() => resolve(DEADLINE_EXPIRED), deadlineMs);
69
+ });
70
+ }
71
+ try {
72
+ for (const step of steps) {
73
+ if (step.enabled) {
74
+ let isEnabled;
75
+ try {
76
+ isEnabled = step.enabled();
77
+ }
78
+ catch (error) {
79
+ onStepError?.(step.id, error);
80
+ continue;
81
+ }
82
+ if (!isEnabled)
83
+ continue;
84
+ }
85
+ let result;
48
86
  try {
49
- isEnabled = step.enabled();
87
+ // Without a deadline: legacy behaviour — await the step directly.
88
+ // With a deadline: race the step against the shared deadline. The
89
+ // step's `run()` still STARTS synchronously up to its first `await`
90
+ // (so a terminal step's synchronous navigation side effect always
91
+ // executes), but a non-settling step can no longer block the loop —
92
+ // the race resolves with the sentinel and we move on.
93
+ result = deadlinePromise
94
+ ? await Promise.race([step.run(), deadlinePromise])
95
+ : await step.run();
50
96
  }
51
97
  catch (error) {
52
98
  onStepError?.(step.id, error);
53
99
  continue;
54
100
  }
55
- if (!isEnabled)
101
+ if (result === DEADLINE_EXPIRED) {
102
+ // The deadline tripped before this step settled. Abandon the await and
103
+ // continue: subsequent steps race against the already-resolved deadline
104
+ // (so they cannot block), which lets a terminal side-effect step still
105
+ // run while guaranteeing the loop terminates promptly.
106
+ onStepDeadline?.(step.id);
56
107
  continue;
108
+ }
109
+ if (result.kind === 'session') {
110
+ return { kind: 'session', via: step.id, session: result.session };
111
+ }
57
112
  }
58
- let result;
59
- try {
60
- result = await step.run();
61
- }
62
- catch (error) {
63
- onStepError?.(step.id, error);
64
- continue;
65
- }
66
- if (result.kind === 'session') {
67
- return { kind: 'session', via: step.id, session: result.session };
113
+ return { kind: 'unauthenticated' };
114
+ }
115
+ finally {
116
+ if (deadlineTimer !== undefined) {
117
+ clearTimeout(deadlineTimer);
68
118
  }
69
119
  }
70
- return { kind: 'unauthenticated' };
71
120
  }