@icp-sdk/auth 8.0.3 → 9.0.0

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.
Files changed (85) hide show
  1. package/dist/esm/client/app-delegation-source.d.ts +65 -0
  2. package/dist/esm/client/app-delegation-source.js +58 -0
  3. package/dist/esm/client/app-delegation-source.js.map +1 -0
  4. package/dist/esm/client/app-lock.d.ts +63 -0
  5. package/dist/esm/client/app-lock.js +92 -0
  6. package/dist/esm/client/app-lock.js.map +1 -0
  7. package/dist/esm/client/auth-client.d.ts +270 -53
  8. package/dist/esm/client/auth-client.js +1017 -414
  9. package/dist/esm/client/auth-client.js.map +1 -1
  10. package/dist/esm/client/base64.d.ts +8 -0
  11. package/dist/esm/client/base64.js +28 -0
  12. package/dist/esm/client/base64.js.map +1 -0
  13. package/dist/esm/client/cookie-state-storage.d.ts +59 -0
  14. package/dist/esm/client/cookie-state-storage.js +245 -0
  15. package/dist/esm/client/cookie-state-storage.js.map +1 -0
  16. package/dist/esm/client/credential-storage.d.ts +48 -0
  17. package/dist/esm/client/credential-storage.js +2 -0
  18. package/dist/esm/client/credential-storage.js.map +1 -0
  19. package/dist/esm/client/db.d.ts +6 -1
  20. package/dist/esm/client/db.js +56 -15
  21. package/dist/esm/client/db.js.map +1 -1
  22. package/dist/esm/client/foreground-refresh.d.ts +35 -0
  23. package/dist/esm/client/foreground-refresh.js +83 -0
  24. package/dist/esm/client/foreground-refresh.js.map +1 -0
  25. package/dist/esm/client/idb-credential-storage.d.ts +25 -0
  26. package/dist/esm/client/idb-credential-storage.js +69 -0
  27. package/dist/esm/client/idb-credential-storage.js.map +1 -0
  28. package/dist/esm/client/index.d.ts +14 -3
  29. package/dist/esm/client/index.js +13 -3
  30. package/dist/esm/client/index.js.map +1 -1
  31. package/dist/esm/client/local-credential-storage.d.ts +26 -0
  32. package/dist/esm/client/local-credential-storage.js +59 -0
  33. package/dist/esm/client/local-credential-storage.js.map +1 -0
  34. package/dist/esm/client/memory-credential-storage.d.ts +22 -0
  35. package/dist/esm/client/memory-credential-storage.js +35 -0
  36. package/dist/esm/client/memory-credential-storage.js.map +1 -0
  37. package/dist/esm/client/session-delegation.d.ts +40 -0
  38. package/dist/esm/client/session-delegation.js +100 -0
  39. package/dist/esm/client/session-delegation.js.map +1 -0
  40. package/dist/esm/client/session-identity.d.ts +97 -0
  41. package/dist/esm/client/session-identity.js +392 -0
  42. package/dist/esm/client/session-identity.js.map +1 -0
  43. package/dist/esm/client/session-minter.d.ts +94 -0
  44. package/dist/esm/client/session-minter.js +185 -0
  45. package/dist/esm/client/session-minter.js.map +1 -0
  46. package/dist/esm/client/shared-memory-credential-storage.d.ts +44 -0
  47. package/dist/esm/client/shared-memory-credential-storage.js +243 -0
  48. package/dist/esm/client/shared-memory-credential-storage.js.map +1 -0
  49. package/dist/esm/client/slots.d.ts +59 -0
  50. package/dist/esm/client/slots.js +67 -0
  51. package/dist/esm/client/slots.js.map +1 -0
  52. package/dist/esm/client/sso.d.ts +33 -0
  53. package/dist/esm/client/sso.js +120 -0
  54. package/dist/esm/client/sso.js.map +1 -0
  55. package/dist/esm/client/state-storage.d.ts +135 -0
  56. package/dist/esm/client/state-storage.js +178 -0
  57. package/dist/esm/client/state-storage.js.map +1 -0
  58. package/package.json +2 -1
  59. package/src/client/app-delegation-source.ts +86 -0
  60. package/src/client/app-lock.ts +120 -0
  61. package/src/client/auth-client.ts +1300 -487
  62. package/src/client/base64.ts +28 -0
  63. package/src/client/cookie-state-storage.ts +277 -0
  64. package/src/client/credential-storage.ts +56 -0
  65. package/src/client/db.ts +56 -12
  66. package/src/client/foreground-refresh.ts +92 -0
  67. package/src/client/idb-credential-storage.ts +82 -0
  68. package/src/client/index.ts +24 -8
  69. package/src/client/local-credential-storage.ts +68 -0
  70. package/src/client/memory-credential-storage.ts +41 -0
  71. package/src/client/session-delegation.ts +138 -0
  72. package/src/client/session-identity.ts +510 -0
  73. package/src/client/session-minter.ts +286 -0
  74. package/src/client/shared-memory-credential-storage.ts +303 -0
  75. package/src/client/slots.ts +78 -0
  76. package/src/client/sso.ts +122 -0
  77. package/src/client/state-storage.ts +284 -0
  78. package/dist/esm/client/idle-manager.d.ts +0 -83
  79. package/dist/esm/client/idle-manager.js +0 -100
  80. package/dist/esm/client/idle-manager.js.map +0 -1
  81. package/dist/esm/client/storage.d.ts +0 -51
  82. package/dist/esm/client/storage.js +0 -90
  83. package/dist/esm/client/storage.js.map +0 -1
  84. package/src/client/idle-manager.ts +0 -162
  85. package/src/client/storage.ts +0 -114
@@ -0,0 +1,120 @@
1
+ /**
2
+ * Runs `mint` while holding a named lock, where the environment has one.
3
+ *
4
+ * The lock is the library coordinating with itself rather than anything a store
5
+ * provides, so a store answers only whether another tab can read it and needs to
6
+ * know nothing about the Web Locks API.
7
+ *
8
+ * Where there is no lock — an older browser, or a store no other tab reads —
9
+ * `mint` runs straight away and every tab mints for itself. That costs calls and
10
+ * never correctness: coordination may only ever suppress a mint, never be
11
+ * required for one.
12
+ *
13
+ * `mint` is handed a signal that fires when the lock is taken away by
14
+ * {@link stealMintLock}. It is the only thing a mint can act on, because a call
15
+ * already sent cannot be recalled: the mint runs to completion and the signal is
16
+ * what tells it to throw away the result rather than store it.
17
+ * @param name - The lock to hold, or `null` to run without one.
18
+ * @param mint - The work to serialise, taking the signal described above.
19
+ */
20
+ export function withMintLock<T>(
21
+ name: string | null,
22
+ mint: (stolen: AbortSignal) => Promise<T>,
23
+ ): Promise<T> {
24
+ const locks = globalThis.navigator?.locks;
25
+ if (name === null || locks === undefined) {
26
+ // Nothing can take a lock that was never held, so the signal never fires.
27
+ return mint(new AbortController().signal);
28
+ }
29
+
30
+ const stolen = new AbortController();
31
+ const held = locks.request(name, () => mint(stolen.signal)) as Promise<T>;
32
+
33
+ // A steal releases this tab's lock and rejects this promise, in this document.
34
+ // That rejection is the only notice another tab's sign-out can deliver, so it
35
+ // is turned into the signal `mint` was given rather than being surfaced: the
36
+ // mint decides what to do about it, and the caller sees whatever it returns.
37
+ held.catch(() => stolen.abort());
38
+ return held;
39
+ }
40
+
41
+ /**
42
+ * Takes the lock away from whoever holds it, without waiting for them.
43
+ *
44
+ * What signing out does. Queueing would make a sign-out the user asked for wait
45
+ * on a canister call in another tab, and a mint that finished afterwards would
46
+ * write a credential into the slot the sign-out had just cleared.
47
+ * @param name - The lock to take, or `null` where none is held.
48
+ */
49
+ export async function stealMintLock(name: string | null): Promise<void> {
50
+ const locks = globalThis.navigator?.locks;
51
+ if (name === null || locks === undefined) return;
52
+
53
+ // Granted at once, and released at once: holding it would only make the caller
54
+ // the next thing a queued mint waits for.
55
+ await locks.request(name, { steal: true }, () => undefined).catch(() => undefined);
56
+ }
57
+
58
+ /**
59
+ * A lock this holder keeps until it gives it up, and a signal for losing it.
60
+ *
61
+ * Unlike {@link withMintLock}, what needs guarding here is not one call but a
62
+ * span — a ceremony is several calls and then several writes — so the lock is
63
+ * held across it and released by the holder rather than by a callback returning.
64
+ */
65
+ export interface HeldLock {
66
+ /** Fires when someone else takes this lock away. */
67
+ readonly stolen: AbortSignal;
68
+
69
+ /**
70
+ * Gives the lock up.
71
+ *
72
+ * Idempotent, and safe to call after it was stolen: releasing a lock this
73
+ * holder no longer has does nothing.
74
+ */
75
+ release(): void;
76
+ }
77
+
78
+ /**
79
+ * Takes a lock away from whoever holds it and keeps it until released.
80
+ *
81
+ * Taken rather than queued for, like {@link stealMintLock} and for the same
82
+ * reason: the newer intent is the one to honour. A user who presses sign in
83
+ * again means the second press, and an origin has one signer window, so the two
84
+ * cannot both proceed anyway. The incumbent finds out through {@link
85
+ * HeldLock.stolen} and throws its result away instead of writing it.
86
+ *
87
+ * Where there is no lock — an older browser, or a caller that passes `null`
88
+ * because nothing else could be holding one — the result never reports a steal
89
+ * and releasing does nothing. That costs coordination, never correctness:
90
+ * coordination may only ever suppress work, never be required for it.
91
+ * @param name - The lock to take, or `null` to take none.
92
+ */
93
+ export function stealLock(name: string | null): HeldLock {
94
+ const stolen = new AbortController();
95
+ const locks = globalThis.navigator?.locks;
96
+ if (name === null || locks === undefined) {
97
+ return { stolen: stolen.signal, release: () => {} };
98
+ }
99
+
100
+ // The callback holds the lock for as long as what it returns is pending, so
101
+ // this is what `release` settles. A steal rejects the `request` promise in this
102
+ // document, which is the only notice the platform gives.
103
+ let release: () => void = () => {};
104
+ const held = new Promise<void>((resolve) => {
105
+ release = resolve;
106
+ });
107
+ void locks
108
+ .request(name, { steal: true }, () => held)
109
+ .catch((error: unknown) => {
110
+ // Only a steal, which arrives as an `AbortError`. A request can fail for
111
+ // reasons that are nothing to do with another holder — a frame going away
112
+ // takes its callback with it — and reporting one of those as a steal would
113
+ // tell the caller its work was superseded when nothing had superseded it.
114
+ if (error instanceof DOMException && error.name === 'AbortError') {
115
+ stolen.abort();
116
+ }
117
+ });
118
+
119
+ return { stolen: stolen.signal, release: () => release() };
120
+ }