@flipstream/sdk 0.13.0 → 0.15.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 (278) hide show
  1. package/CHANGELOG.md +120 -0
  2. package/README.md +143 -21
  3. package/dist/api/backend.d.ts +7 -0
  4. package/dist/api/backend.js +7 -0
  5. package/dist/api/backend.js.map +1 -1
  6. package/dist/api/credentials.js +4 -4
  7. package/dist/api/credentials.js.map +1 -1
  8. package/dist/api/datasets.js +30 -50
  9. package/dist/api/datasets.js.map +1 -1
  10. package/dist/api/handoff.d.ts +1 -1
  11. package/dist/api/handoff.js +7 -0
  12. package/dist/api/handoff.js.map +1 -1
  13. package/dist/api/log.d.ts +4 -4
  14. package/dist/api/log.js +23 -42
  15. package/dist/api/log.js.map +1 -1
  16. package/dist/api/me.d.ts +5 -3
  17. package/dist/api/me.js +3 -3
  18. package/dist/api/me.js.map +1 -1
  19. package/dist/api/members.d.ts +40 -17
  20. package/dist/api/members.js +66 -29
  21. package/dist/api/members.js.map +1 -1
  22. package/dist/api/ownRecord.d.ts +46 -0
  23. package/dist/api/ownRecord.js +57 -0
  24. package/dist/api/ownRecord.js.map +1 -0
  25. package/dist/api/refusal.d.ts +14 -0
  26. package/dist/api/refusal.js +23 -0
  27. package/dist/api/refusal.js.map +1 -0
  28. package/dist/api/wallet.d.ts +2 -1
  29. package/dist/api/wallet.js +18 -24
  30. package/dist/api/wallet.js.map +1 -1
  31. package/dist/api/workspaces.d.ts +7 -2
  32. package/dist/api/workspaces.js +14 -8
  33. package/dist/api/workspaces.js.map +1 -1
  34. package/dist/auth/discovery.js +4 -5
  35. package/dist/auth/discovery.js.map +1 -1
  36. package/dist/auth/transport.d.ts +6 -2
  37. package/dist/auth/transport.js +5 -3
  38. package/dist/auth/transport.js.map +1 -1
  39. package/dist/core/call.d.ts +9 -4
  40. package/dist/core/call.js +12 -8
  41. package/dist/core/call.js.map +1 -1
  42. package/dist/core/fetch.d.ts +20 -1
  43. package/dist/core/fetch.js +50 -3
  44. package/dist/core/fetch.js.map +1 -1
  45. package/dist/core/hosts.d.ts +11 -0
  46. package/dist/core/hosts.js +13 -0
  47. package/dist/core/hosts.js.map +1 -0
  48. package/dist/core/identify.d.ts +12 -2
  49. package/dist/core/identify.js +31 -19
  50. package/dist/core/identify.js.map +1 -1
  51. package/dist/core/inPlaceOf.d.ts +6 -0
  52. package/dist/core/inPlaceOf.js +17 -0
  53. package/dist/core/inPlaceOf.js.map +1 -0
  54. package/dist/core/invalid.d.ts +32 -9
  55. package/dist/core/invalid.js +36 -23
  56. package/dist/core/invalid.js.map +1 -1
  57. package/dist/core/notFound.d.ts +2 -2
  58. package/dist/core/notFound.js.map +1 -1
  59. package/dist/core/read.d.ts +4 -10
  60. package/dist/core/read.js +1 -11
  61. package/dist/core/read.js.map +1 -1
  62. package/dist/core/result.d.ts +14 -10
  63. package/dist/core/result.js +1 -0
  64. package/dist/core/result.js.map +1 -1
  65. package/dist/core/shared.d.ts +15 -9
  66. package/dist/core/shared.js +8 -6
  67. package/dist/core/shared.js.map +1 -1
  68. package/dist/core/signal.d.ts +7 -0
  69. package/dist/core/signal.js +9 -0
  70. package/dist/core/signal.js.map +1 -1
  71. package/dist/first-party/asPerson.d.ts +13 -0
  72. package/dist/first-party/asPerson.js +144 -0
  73. package/dist/first-party/asPerson.js.map +1 -0
  74. package/dist/first-party/attempts.d.ts +71 -0
  75. package/dist/first-party/attempts.js +132 -0
  76. package/dist/first-party/attempts.js.map +1 -0
  77. package/dist/first-party/authProxyMark.d.ts +7 -0
  78. package/dist/first-party/authProxyMark.js +8 -0
  79. package/dist/first-party/authProxyMark.js.map +1 -0
  80. package/dist/first-party/browser.d.ts +55 -0
  81. package/dist/first-party/browser.js +137 -0
  82. package/dist/first-party/browser.js.map +1 -0
  83. package/dist/first-party/browserSession.d.ts +95 -0
  84. package/dist/first-party/browserSession.js +242 -0
  85. package/dist/first-party/browserSession.js.map +1 -0
  86. package/dist/first-party/calls/consent.d.ts +77 -0
  87. package/dist/first-party/calls/consent.js +174 -0
  88. package/dist/first-party/calls/consent.js.map +1 -0
  89. package/dist/first-party/calls/device.d.ts +75 -0
  90. package/dist/first-party/calls/device.js +133 -0
  91. package/dist/first-party/calls/device.js.map +1 -0
  92. package/dist/first-party/calls/invitation.d.ts +198 -0
  93. package/dist/first-party/calls/invitation.js +240 -0
  94. package/dist/first-party/calls/invitation.js.map +1 -0
  95. package/dist/first-party/calls/logIn.d.ts +41 -0
  96. package/dist/first-party/calls/logIn.js +48 -0
  97. package/dist/first-party/calls/logIn.js.map +1 -0
  98. package/dist/first-party/calls/reset.d.ts +58 -0
  99. package/dist/first-party/calls/reset.js +163 -0
  100. package/dist/first-party/calls/reset.js.map +1 -0
  101. package/dist/first-party/calls/signUp.d.ts +21 -0
  102. package/dist/first-party/calls/signUp.js +72 -0
  103. package/dist/first-party/calls/signUp.js.map +1 -0
  104. package/dist/first-party/calls/twoStepSetup.d.ts +55 -0
  105. package/dist/first-party/calls/twoStepSetup.js +168 -0
  106. package/dist/first-party/calls/twoStepSetup.js.map +1 -0
  107. package/dist/first-party/claims.d.ts +88 -0
  108. package/dist/first-party/claims.js +181 -0
  109. package/dist/first-party/claims.js.map +1 -0
  110. package/dist/first-party/enrollment.d.ts +10 -0
  111. package/dist/first-party/enrollment.js +44 -0
  112. package/dist/first-party/enrollment.js.map +1 -0
  113. package/dist/first-party/entry.d.ts +549 -0
  114. package/dist/first-party/entry.js +580 -0
  115. package/dist/first-party/entry.js.map +1 -0
  116. package/dist/first-party/hooks.d.ts +66 -0
  117. package/dist/first-party/hooks.js +19 -0
  118. package/dist/first-party/hooks.js.map +1 -0
  119. package/dist/first-party/http.d.ts +191 -0
  120. package/dist/first-party/http.js +364 -0
  121. package/dist/first-party/http.js.map +1 -0
  122. package/dist/first-party/index.d.ts +14 -0
  123. package/dist/first-party/index.js +3 -0
  124. package/dist/first-party/index.js.map +1 -0
  125. package/dist/first-party/landed.d.ts +32 -0
  126. package/dist/first-party/landed.js +50 -0
  127. package/dist/first-party/landed.js.map +1 -0
  128. package/dist/first-party/landedProof.d.ts +12 -0
  129. package/dist/first-party/landedProof.js +20 -0
  130. package/dist/first-party/landedProof.js.map +1 -0
  131. package/dist/first-party/linkToken.d.ts +16 -0
  132. package/dist/first-party/linkToken.js +29 -0
  133. package/dist/first-party/linkToken.js.map +1 -0
  134. package/dist/first-party/links.d.ts +100 -0
  135. package/dist/first-party/links.js +84 -0
  136. package/dist/first-party/links.js.map +1 -0
  137. package/dist/first-party/locks.d.ts +36 -0
  138. package/dist/first-party/locks.js +64 -0
  139. package/dist/first-party/locks.js.map +1 -0
  140. package/dist/first-party/personTransport.d.ts +38 -0
  141. package/dist/first-party/personTransport.js +177 -0
  142. package/dist/first-party/personTransport.js.map +1 -0
  143. package/dist/first-party/proxy/app.d.ts +25 -0
  144. package/dist/first-party/proxy/app.js +94 -0
  145. package/dist/first-party/proxy/app.js.map +1 -0
  146. package/dist/first-party/proxy/bound.d.ts +5 -0
  147. package/dist/first-party/proxy/bound.js +13 -0
  148. package/dist/first-party/proxy/bound.js.map +1 -0
  149. package/dist/first-party/proxy/cases.d.ts +54 -0
  150. package/dist/first-party/proxy/cases.js +412 -0
  151. package/dist/first-party/proxy/cases.js.map +1 -0
  152. package/dist/first-party/proxy/decide.d.ts +29 -0
  153. package/dist/first-party/proxy/decide.js +140 -0
  154. package/dist/first-party/proxy/decide.js.map +1 -0
  155. package/dist/first-party/proxy/freeze.d.ts +2 -0
  156. package/dist/first-party/proxy/freeze.js +10 -0
  157. package/dist/first-party/proxy/freeze.js.map +1 -0
  158. package/dist/first-party/proxy/handler.d.ts +23 -0
  159. package/dist/first-party/proxy/handler.js +190 -0
  160. package/dist/first-party/proxy/handler.js.map +1 -0
  161. package/dist/first-party/proxy/headers.d.ts +8 -0
  162. package/dist/first-party/proxy/headers.js +20 -0
  163. package/dist/first-party/proxy/headers.js.map +1 -0
  164. package/dist/first-party/proxy/hop.d.ts +9 -0
  165. package/dist/first-party/proxy/hop.js +37 -0
  166. package/dist/first-party/proxy/hop.js.map +1 -0
  167. package/dist/first-party/proxy/host.d.ts +5 -0
  168. package/dist/first-party/proxy/host.js +11 -0
  169. package/dist/first-party/proxy/host.js.map +1 -0
  170. package/dist/first-party/proxy/index.d.ts +7 -0
  171. package/dist/first-party/proxy/index.js +6 -0
  172. package/dist/first-party/proxy/index.js.map +1 -0
  173. package/dist/first-party/proxy/pass-back.d.ts +21 -0
  174. package/dist/first-party/proxy/pass-back.js +51 -0
  175. package/dist/first-party/proxy/pass-back.js.map +1 -0
  176. package/dist/first-party/proxy/paths.d.ts +17 -0
  177. package/dist/first-party/proxy/paths.js +36 -0
  178. package/dist/first-party/proxy/paths.js.map +1 -0
  179. package/dist/first-party/proxy/shown.d.ts +5 -0
  180. package/dist/first-party/proxy/shown.js +16 -0
  181. package/dist/first-party/proxy/shown.js.map +1 -0
  182. package/dist/first-party/proxy/stand-in.d.ts +47 -0
  183. package/dist/first-party/proxy/stand-in.js +318 -0
  184. package/dist/first-party/proxy/stand-in.js.map +1 -0
  185. package/dist/first-party/report.d.ts +47 -0
  186. package/dist/first-party/report.js +38 -0
  187. package/dist/first-party/report.js.map +1 -0
  188. package/dist/first-party/result.d.ts +82 -0
  189. package/dist/first-party/result.js +40 -0
  190. package/dist/first-party/result.js.map +1 -0
  191. package/dist/first-party/session/cell.d.ts +177 -0
  192. package/dist/first-party/session/cell.js +89 -0
  193. package/dist/first-party/session/cell.js.map +1 -0
  194. package/dist/first-party/session/closed.d.ts +8 -0
  195. package/dist/first-party/session/closed.js +12 -0
  196. package/dist/first-party/session/closed.js.map +1 -0
  197. package/dist/first-party/session/index.d.ts +3 -0
  198. package/dist/first-party/session/index.js +3 -0
  199. package/dist/first-party/session/index.js.map +1 -0
  200. package/dist/first-party/session/logout.d.ts +82 -0
  201. package/dist/first-party/session/logout.js +328 -0
  202. package/dist/first-party/session/logout.js.map +1 -0
  203. package/dist/first-party/session/owed.d.ts +30 -0
  204. package/dist/first-party/session/owed.js +27 -0
  205. package/dist/first-party/session/owed.js.map +1 -0
  206. package/dist/first-party/session/refresh.d.ts +19 -0
  207. package/dist/first-party/session/refresh.js +229 -0
  208. package/dist/first-party/session/refresh.js.map +1 -0
  209. package/dist/first-party/session/results.d.ts +38 -0
  210. package/dist/first-party/session/results.js +74 -0
  211. package/dist/first-party/session/results.js.map +1 -0
  212. package/dist/first-party/session/session.d.ts +2 -0
  213. package/dist/first-party/session/session.js +379 -0
  214. package/dist/first-party/session/session.js.map +1 -0
  215. package/dist/first-party/session/shared.d.ts +34 -0
  216. package/dist/first-party/session/shared.js +104 -0
  217. package/dist/first-party/session/shared.js.map +1 -0
  218. package/dist/first-party/session/sharedSignal.d.ts +27 -0
  219. package/dist/first-party/session/sharedSignal.js +55 -0
  220. package/dist/first-party/session/sharedSignal.js.map +1 -0
  221. package/dist/first-party/session/signIn.d.ts +43 -0
  222. package/dist/first-party/session/signIn.js +294 -0
  223. package/dist/first-party/session/signIn.js.map +1 -0
  224. package/dist/first-party/session/signInThrough.d.ts +5 -0
  225. package/dist/first-party/session/signInThrough.js +95 -0
  226. package/dist/first-party/session/signInThrough.js.map +1 -0
  227. package/dist/first-party/session/sync.d.ts +17 -0
  228. package/dist/first-party/session/sync.js +143 -0
  229. package/dist/first-party/session/sync.js.map +1 -0
  230. package/dist/first-party/session/tokens.d.ts +18 -0
  231. package/dist/first-party/session/tokens.js +64 -0
  232. package/dist/first-party/session/tokens.js.map +1 -0
  233. package/dist/first-party/session/types.d.ts +270 -0
  234. package/dist/first-party/session/types.js +3 -0
  235. package/dist/first-party/session/types.js.map +1 -0
  236. package/dist/first-party/sessionWire.d.ts +31 -0
  237. package/dist/first-party/sessionWire.js +91 -0
  238. package/dist/first-party/sessionWire.js.map +1 -0
  239. package/dist/first-party/signInHooks.d.ts +57 -0
  240. package/dist/first-party/signInHooks.js +2 -0
  241. package/dist/first-party/signInHooks.js.map +1 -0
  242. package/dist/first-party/state.d.ts +51 -0
  243. package/dist/first-party/state.js +52 -0
  244. package/dist/first-party/state.js.map +1 -0
  245. package/dist/first-party/strip.d.ts +1 -0
  246. package/dist/first-party/strip.js +8 -0
  247. package/dist/first-party/strip.js.map +1 -0
  248. package/dist/first-party/twoStepLock.d.ts +26 -0
  249. package/dist/first-party/twoStepLock.js +58 -0
  250. package/dist/first-party/twoStepLock.js.map +1 -0
  251. package/dist/first-party/twoStepSession.d.ts +21 -0
  252. package/dist/first-party/twoStepSession.js +31 -0
  253. package/dist/first-party/twoStepSession.js.map +1 -0
  254. package/dist/first-party/wire.d.ts +63 -0
  255. package/dist/first-party/wire.js +5 -0
  256. package/dist/first-party/wire.js.map +1 -0
  257. package/dist/flipstream.d.ts +132 -36
  258. package/dist/flipstream.js +94 -47
  259. package/dist/flipstream.js.map +1 -1
  260. package/dist/index.d.ts +2 -2
  261. package/dist/index.js +3 -2
  262. package/dist/index.js.map +1 -1
  263. package/dist/node/keychainChild.js +60 -25
  264. package/dist/node/keychainChild.js.map +1 -1
  265. package/dist/node/keychainError.d.ts +4 -1
  266. package/dist/node/keychainError.js.map +1 -1
  267. package/dist/node/keychainStore.js +42 -8
  268. package/dist/node/keychainStore.js.map +1 -1
  269. package/dist/node/session.js +9 -8
  270. package/dist/node/session.js.map +1 -1
  271. package/dist/query/query.d.ts +52 -8
  272. package/dist/query/query.js +133 -19
  273. package/dist/query/query.js.map +1 -1
  274. package/dist/version.gen.js +1 -1
  275. package/docs/admin.md +53 -4
  276. package/docs/first-party.md +278 -0
  277. package/docs/node-sign-in.md +8 -4
  278. package/package.json +34 -5
@@ -0,0 +1,177 @@
1
+ import type { SignInSetup } from '../hooks.js';
2
+ import { z } from 'zod';
3
+ import type { AccessToken } from '../claims.js';
4
+ import type { LogoutAnswer, TwoStepChallenge } from '../http.js';
5
+ import { type AuthState } from '../state.js';
6
+ import type { SharedSignal } from './sharedSignal.js';
7
+ import type { RedeemResult, Session } from './types.js';
8
+ /** What pulse-auth answered to a logout this tab keeps owed with no timer: refused, or outside the contract. */
9
+ export type KeptAnswer = {
10
+ readonly kind: 'unconfirmed';
11
+ readonly reason: 'refused';
12
+ } | Extract<LogoutAnswer, {
13
+ readonly kind: 'broken';
14
+ }>;
15
+ /**
16
+ * A kept logout: the cookie it was for (the record's generation and serial as it was sent, read
17
+ * under the same cookie lock as the send), and the answer a try queued behind that send is told
18
+ * (sdk#531, PR 547 round 1).
19
+ */
20
+ export interface KeptLogout {
21
+ readonly generation: number;
22
+ readonly serial: number;
23
+ readonly answer: KeptAnswer;
24
+ }
25
+ /** Whether `kept` is for the logout `state` owes: the same cookie, by generation and serial. */
26
+ export declare function isKeptFor(kept: KeptLogout | null, state: Pick<AuthState, 'generation' | 'serial'>): kept is KeptLogout;
27
+ /** The floor for a scheduled refresh, so no token, however short, causes a refresh loop. */
28
+ export declare const MIN_REFRESH_DELAY_MS = 5000;
29
+ /** "Can't reach sign-in, trying again": 1 s, 2 s, 4 s … 30 s. */
30
+ export declare const BACKOFF_MS: readonly number[];
31
+ /** A 403 forces one token refresh, at most once a minute per tab (§2.5). */
32
+ export declare const FORBIDDEN_REFRESH_EVERY_MS = 60000;
33
+ /** What one tab tells the others on the channel. */
34
+ export declare const Message: z.ZodDiscriminatedUnion<[z.ZodObject<{
35
+ type: z.ZodLiteral<"token">;
36
+ generation: z.ZodInt;
37
+ serial: z.ZodInt;
38
+ accessToken: z.ZodString;
39
+ }, z.core.$strip>, z.ZodObject<{
40
+ type: z.ZodLiteral<"signed-out">;
41
+ generation: z.ZodInt;
42
+ }, z.core.$strip>], "type">;
43
+ export type Message = z.output<typeof Message>;
44
+ export type RefreshOutcome = 'ok' | 'signed-out' | 'unavailable' | 'broken' | 'stale';
45
+ export interface Held {
46
+ readonly access: AccessToken;
47
+ /** The shared generation this token belongs to. */
48
+ readonly generation: number;
49
+ readonly serial: number;
50
+ /** On this tab's clock: arrival + lifetime. A clock that is off can't make every token look expired. */
51
+ readonly expiresAt: number;
52
+ }
53
+ /**
54
+ * A two-step sign-in in progress: memory only (A4), with the attempt it finishes and the
55
+ * generation its link was redeemed in. A sign-out or a sign-in anywhere since ends it.
56
+ */
57
+ export type PendingTwoStep = ({
58
+ readonly purpose: 'setup';
59
+ readonly setup: SignInSetup;
60
+ } | {
61
+ readonly purpose: 'verify';
62
+ }) & {
63
+ readonly challenge: TwoStepChallenge;
64
+ readonly attemptId: string | null;
65
+ readonly email: string | null;
66
+ readonly generation: number;
67
+ };
68
+ /** One link's redeem: what a later press of the same link joins or reads. */
69
+ export interface RedeemRecord {
70
+ readonly attempt: Promise<RedeemResult>;
71
+ readonly attemptId: string;
72
+ readonly generation: number;
73
+ readonly callers: SharedSignal;
74
+ /**
75
+ * Since this redeem's own challenge was last installed, another sign-in in this tab took a
76
+ * challenge or signed in: every later press of this link answers `superseded`, whatever is
77
+ * pending by then (SDK-D1976, SDK-D2007). Set where that sign-in settles, cleared where this
78
+ * redeem installs its own challenge.
79
+ */
80
+ overtaken: boolean;
81
+ }
82
+ /**
83
+ * Everything one tab's session holds and changes. Each part of the session (the shared
84
+ * state, the token, logout, refresh, sign-in, sync) reads and writes this one cell, so what
85
+ * one part changes the next reads at once, as when they were one closure.
86
+ */
87
+ export interface SessionCell {
88
+ /** Web Locks and localStorage are both there: without them tabs can't agree. */
89
+ readonly supported: boolean;
90
+ status: Session;
91
+ held: Held | null;
92
+ principalKey: string | null;
93
+ revision: number;
94
+ /** The newest state this tab has seen, from storage or a message that came first. */
95
+ known: AuthState;
96
+ /**
97
+ * The stored generation read when this tab made, or heard, a sign-out storage refused (0: no
98
+ * record): what a closed session leaves in the page's slot (owed.ts, SDK-D1977). It lives exactly
99
+ * as long as `known` is that sign-out: written down, its logout confirmed, or a newer state that
100
+ * is not that sign-out adopted (`adoptNewer`) ends it (SDK-D2005). Null: nothing is owed.
101
+ */
102
+ owedStored: number | null;
103
+ /**
104
+ * The stored record, by generation and serial, whose pending logout pulse-auth confirmed while
105
+ * storage refused the write that clears its flag (sdk#506). While storage still reads that
106
+ * generation and serial with that flag, the flag reads as cleared; any other record ends it.
107
+ * Every write that sets a pending logout without moving the generation moves the serial, so a new
108
+ * flag is never this record. It ends when storage reads any other record, and at a `storage`
109
+ * event for the state key, since another tab's write may have cleared and set the flag again at
110
+ * the same pair (a build that doesn't move the serial). Never a count that lives only in memory
111
+ * (SDK-D1967).
112
+ */
113
+ paidLogout: {
114
+ readonly generation: number;
115
+ readonly serial: number;
116
+ } | null;
117
+ refreshInFlight: Promise<RefreshOutcome> | null;
118
+ refreshTimer: ReturnType<typeof setTimeout> | undefined;
119
+ restoreTimer: ReturnType<typeof setTimeout> | undefined;
120
+ backoffStep: number;
121
+ lastForcedRefresh: number;
122
+ logoutRetryTimer: ReturnType<typeof setTimeout> | undefined;
123
+ logoutBackoffStep: number;
124
+ /**
125
+ * The last refused or out-of-contract answer for its own generation and serial (one cookie): kept
126
+ * owed with no timer, and not sent again by `sync()` while that same cookie is owed
127
+ * (`isKeptFor`), only written down (SDK-D2012); the next page load, sign-in or restore this tab
128
+ * runs sends it (SDK-D1963, SDK-D2008). A try queued behind that send is told its answer
129
+ * (sdk#531). Ended by a confirmation for that cookie, by any route, or a 503 for it.
130
+ */
131
+ logoutKept: KeptLogout | null;
132
+ /**
133
+ * Sign-outs pressed in this tab, including one whose state write is still waiting (SDK-D1970).
134
+ * Memory only: never stored, and never compared with a stored number.
135
+ */
136
+ localSignOuts: number;
137
+ /** Private identity of each issued step and whether another tab ended it. */
138
+ readonly setupSteps: WeakMap<SignInSetup, 'issued' | 'superseded'>;
139
+ twoStep: PendingTwoStep | null;
140
+ /** Set when a sign-out or sign-in elsewhere ended the code screen's challenge. */
141
+ twoStepSuperseded: boolean;
142
+ /**
143
+ * Whether the last challenge was ended by a sign-out or sign-in elsewhere, rather than by its
144
+ * five minutes or a lock. Unlike `twoStepSuperseded`, a verify doesn't take it: a redeem pressed
145
+ * again reads it to tell `superseded` from `dead_link`. Cleared by a new challenge.
146
+ */
147
+ twoStepEndedByTab: boolean;
148
+ /** A link's redeem and its callers, by link (SDK-D1932), kept while its answer is final. */
149
+ readonly redeems: Map<string, RedeemRecord>;
150
+ readonly listeners: Set<() => void>;
151
+ /** Closed (`close()`): no timer is set again and nothing is sent to the other tabs. */
152
+ closed: boolean;
153
+ }
154
+ /**
155
+ * This tab adopts `state`, at least as new as what it knew and not an unrecorded sign-out: a record
156
+ * read or written, or a sign-in heard. The sign-out the slot's number stood for is over, so
157
+ * the number goes with it (SDK-D2005).
158
+ */
159
+ export declare function adoptNewer(cell: SessionCell, state: AuthState): void;
160
+ /**
161
+ * Ends this tab's code screen because something else moved the session on: another tab's sign-in
162
+ * or sign-out, or a refresh that started a new generation because another person came back
163
+ * (sdk#535). A code typed after it, or a redeem pressed again, reads `superseded`, and a setup
164
+ * step is marked so.
165
+ */
166
+ export declare function endTwoStep(cell: SessionCell): void;
167
+ /**
168
+ * A record written since this tab's unrecorded sign-out ends it (sdk#510): another tab's sign-in or
169
+ * sign-out, or this sign-out's own record, written down by a tab that heard it. What belonged to
170
+ * the sign-out goes with it: its kept mark, its retry timer and its back-off step. `known` may now
171
+ * go back in generation, so a kept mark left behind could name a later, real sign-out at the same
172
+ * generation, which `sync()` would then only write down. The generation alone can't tell that from
173
+ * this sign-out's own record, so the mark goes either way: after its own record, the next `sync()`
174
+ * sends the refused logout once more, the mistake SDK-D1967 prefers to a swallowed sign-out.
175
+ */
176
+ export declare function dropUnrecordedSignOut(cell: SessionCell): void;
177
+ export declare function createCell(supported: boolean): SessionCell;
@@ -0,0 +1,89 @@
1
+ import { z } from 'zod';
2
+ import { NO_RECORD } from '../state.js';
3
+ /** Whether `kept` is for the logout `state` owes: the same cookie, by generation and serial. */
4
+ export function isKeptFor(kept, state) {
5
+ return kept !== null && kept.generation === state.generation && kept.serial === state.serial;
6
+ }
7
+ /** The floor for a scheduled refresh, so no token, however short, causes a refresh loop. */
8
+ export const MIN_REFRESH_DELAY_MS = 5_000;
9
+ /** "Can't reach sign-in, trying again": 1 s, 2 s, 4 s … 30 s. */
10
+ export const BACKOFF_MS = [1_000, 2_000, 4_000, 8_000, 16_000, 30_000];
11
+ /** A 403 forces one token refresh, at most once a minute per tab (§2.5). */
12
+ export const FORBIDDEN_REFRESH_EVERY_MS = 60_000;
13
+ /** What one tab tells the others on the channel. */
14
+ export const Message = z.discriminatedUnion('type', [
15
+ z.object({
16
+ type: z.literal('token'),
17
+ generation: z.int().min(0),
18
+ serial: z.int().min(0),
19
+ accessToken: z.string(),
20
+ }),
21
+ z.object({ type: z.literal('signed-out'), generation: z.int().min(0) }),
22
+ ]);
23
+ /**
24
+ * This tab adopts `state`, at least as new as what it knew and not an unrecorded sign-out: a record
25
+ * read or written, or a sign-in heard. The sign-out the slot's number stood for is over, so
26
+ * the number goes with it (SDK-D2005).
27
+ */
28
+ export function adoptNewer(cell, state) {
29
+ cell.known = state;
30
+ cell.owedStored = null;
31
+ }
32
+ /**
33
+ * Ends this tab's code screen because something else moved the session on: another tab's sign-in
34
+ * or sign-out, or a refresh that started a new generation because another person came back
35
+ * (sdk#535). A code typed after it, or a redeem pressed again, reads `superseded`, and a setup
36
+ * step is marked so.
37
+ */
38
+ export function endTwoStep(cell) {
39
+ if (cell.twoStep?.purpose === 'setup') {
40
+ cell.setupSteps.set(cell.twoStep.setup, 'superseded');
41
+ }
42
+ cell.twoStep = null;
43
+ cell.twoStepSuperseded = true;
44
+ cell.twoStepEndedByTab = true;
45
+ }
46
+ /**
47
+ * A record written since this tab's unrecorded sign-out ends it (sdk#510): another tab's sign-in or
48
+ * sign-out, or this sign-out's own record, written down by a tab that heard it. What belonged to
49
+ * the sign-out goes with it: its kept mark, its retry timer and its back-off step. `known` may now
50
+ * go back in generation, so a kept mark left behind could name a later, real sign-out at the same
51
+ * generation, which `sync()` would then only write down. The generation alone can't tell that from
52
+ * this sign-out's own record, so the mark goes either way: after its own record, the next `sync()`
53
+ * sends the refused logout once more, the mistake SDK-D1967 prefers to a swallowed sign-out.
54
+ */
55
+ export function dropUnrecordedSignOut(cell) {
56
+ clearTimeout(cell.logoutRetryTimer);
57
+ cell.logoutRetryTimer = undefined;
58
+ cell.logoutBackoffStep = 0;
59
+ cell.logoutKept = null;
60
+ }
61
+ export function createCell(supported) {
62
+ return {
63
+ supported,
64
+ status: supported ? { status: 'restoring' } : { status: 'unsupported' },
65
+ held: null,
66
+ principalKey: null,
67
+ revision: 0,
68
+ known: NO_RECORD,
69
+ owedStored: null,
70
+ paidLogout: null,
71
+ refreshInFlight: null,
72
+ refreshTimer: undefined,
73
+ restoreTimer: undefined,
74
+ backoffStep: 0,
75
+ lastForcedRefresh: Number.NEGATIVE_INFINITY,
76
+ logoutRetryTimer: undefined,
77
+ logoutBackoffStep: 0,
78
+ logoutKept: null,
79
+ localSignOuts: 0,
80
+ setupSteps: new WeakMap(),
81
+ twoStep: null,
82
+ twoStepSuperseded: false,
83
+ twoStepEndedByTab: false,
84
+ redeems: new Map(),
85
+ listeners: new Set(),
86
+ closed: false,
87
+ };
88
+ }
89
+ //# sourceMappingURL=cell.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cell.js","sourceRoot":"","sources":["../../../src/first-party/session/cell.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * A call on a session after its `close()` (#464): a programming error, never an outcome. It is
3
+ * thrown, or rejected with, before anything is sent, so a closed session can't come back to
4
+ * life, signed in with no timer to renew it and no ear for the other tabs.
5
+ */
6
+ export declare class SessionClosedError extends Error {
7
+ constructor(call: string);
8
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * A call on a session after its `close()` (#464): a programming error, never an outcome. It is
3
+ * thrown, or rejected with, before anything is sent, so a closed session can't come back to
4
+ * life, signed in with no timer to renew it and no ear for the other tabs.
5
+ */
6
+ export class SessionClosedError extends Error {
7
+ constructor(call) {
8
+ super(`${call} was called on a closed session: make a new one`);
9
+ this.name = 'SessionClosedError';
10
+ }
11
+ }
12
+ //# sourceMappingURL=closed.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"closed.js","sourceRoot":"","sources":["../../../src/first-party/session/closed.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,OAAO,kBAAmB,SAAQ,KAAK;IAC3C,YAAY,IAAY;QACtB,KAAK,CAAC,GAAG,IAAI,iDAAiD,CAAC,CAAA;QAC/D,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAA;IAClC,CAAC;CACF"}
@@ -0,0 +1,3 @@
1
+ export { createAuthSession } from './session.js';
2
+ export { REFRESH_MARGIN_MS } from './types.js';
3
+ export type { AfterUnauthorized, AuthSession, RedeemResult, Session, SessionDeps, SessionView, SignedOut, TokenFor, Told, TwoStepResult, } from './types.js';
@@ -0,0 +1,3 @@
1
+ export { createAuthSession } from './session.js';
2
+ export { REFRESH_MARGIN_MS } from './types.js';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/first-party/session/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAA;AAChD,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA"}
@@ -0,0 +1,82 @@
1
+ import type { LogoutAnswer } from '../http.js';
2
+ import { type SessionCell } from './cell.js';
3
+ import { type SharedState } from './shared.js';
4
+ import type { Tokens } from './tokens.js';
5
+ import type { SessionDeps, Told } from './types.js';
6
+ /**
7
+ * What `recordSignOut` landed: the signed-out generation, whose pending logout `oweRevoke` revokes
8
+ * through, and the record's serial then, which `signOut()` pins (SDK-D2022). A sign-out keeps the
9
+ * serial. A sign-in or an adopted session recorded in the shared state, a renewal and a sign-in's
10
+ * revoke each move it.
11
+ */
12
+ export interface SignedOut {
13
+ readonly generation: number;
14
+ readonly serial: number;
15
+ }
16
+ /** Telling pulse-auth about a sign-out, until it confirms. */
17
+ export interface Logout {
18
+ /**
19
+ * Sends the pending logout for `generation`, inside the cookie lock the caller holds. The
20
+ * flag clears only on pulse-auth's confirmation, and only while it is still that
21
+ * generation's.
22
+ */
23
+ readonly sendLogout: (generation: number) => Promise<LogoutAnswer>;
24
+ /**
25
+ * Records a sign-out: a new signed-out generation and its pending logout in one write, told to
26
+ * every tab. When storage refuses the write, this tab keeps it in memory and tells the others; it
27
+ * is written down at the next logout try storage takes, or, for a logout kept with no timer, at the
28
+ * next `sync()` storage takes (`writeDown`, SDK-D2012). Answers the new generation and the
29
+ * record's serial with it (`SignedOut`). `landed` runs as the sign-out lands, written or kept in
30
+ * memory, before the tabs are told.
31
+ */
32
+ readonly recordSignOut: (landed?: () => void) => Promise<SignedOut>;
33
+ /**
34
+ * A closed session leaves a sign-out storage never took in the page's slot, for whichever session
35
+ * in the page looks next (owed.ts, sdk#483). Called by `close()`, and by a sign-out made after it.
36
+ */
37
+ readonly leaveIfOwed: () => void;
38
+ /**
39
+ * Sends an owed logout now. An answer that isn't a confirmation earns what `notConfirmed`
40
+ * decides: unreachable, a 5xx or a 429, it is tried again, backing off (1 s … 30 s), until it is
41
+ * confirmed or no longer owed, since a sign-out pulse-auth never heard of must not leave the
42
+ * session alive for as long as nobody reloads (§2.3); refused or outside the contract, it is kept
43
+ * with no timer. One chain per tab; tabs share the cookie lock and re-check inside it, so a
44
+ * confirmed logout stops every other. A try queued behind this tab's own send of the same owed
45
+ * logout (a restore's or a sign-in's), which pulse-auth refused or answered outside the contract
46
+ * while the try waited, is told that send's answer and sends nothing (sdk#531); nothing else
47
+ * answers from the kept mark. A try still out in this tab is shared while it waits for
48
+ * the cookie lock, then only for the generation it found owed there: nothing more is sent, and
49
+ * its outcome is this call's (sdk#517). A call that shares a try still waiting for the cookie
50
+ * lock shares what is left of its wait. Settles what became of that try; `pinnedSerial`, the
51
+ * serial a `signOut()`'s own sign-out landed at, tells a record that moved on since
52
+ * (`superseded`) from the try's own answer (`toldTo`).
53
+ */
54
+ readonly tryLogout: (pinnedSerial?: number) => Promise<Told>;
55
+ /**
56
+ * What a logout pulse-auth didn't confirm earns, for `generation`'s logout, the one place that
57
+ * decides it (SDK-D1963, SDK-D2008). Unreachable, a 5xx or a 429 (`unavailable`): tried again,
58
+ * backing off, since that may be gone in seconds. Refused (a 401 or 403) or outside the contract
59
+ * (`broken`, a host routed or deployed wrongly): time doesn't mend either, so it stays owed with
60
+ * no timer. It is sent again at the next page load, at a sign-in, at a restore this tab runs
61
+ * (`retry()`, `start()`, `refreshAfterUnauthorized()`), and when another tab posts a token this
62
+ * tab can't read: each sends an owed logout first anyway. `sync()` only writes it down (SDK-D2012),
63
+ * unless a record written since this tab's unrecorded sign-out ended the kept mark, this
64
+ * sign-out's own record included (written down by a tab that heard it): the next `sync()` then
65
+ * sends it once (`dropUnrecordedSignOut`). `generation` and `serial` name the cookie the logout
66
+ * was for, read under the same cookie lock as its send: the kept mark is that cookie's, and an
67
+ * unreachable answer for it ends the mark, as a confirmation for it does (`isKeptFor`).
68
+ */
69
+ readonly notConfirmed: (answer: Exclude<LogoutAnswer, {
70
+ readonly kind: 'confirmed';
71
+ }>, generation: number, serial: number) => void;
72
+ /** The next try of an owed logout, backing off; one timer per tab. */
73
+ readonly scheduleLogoutRetry: () => void;
74
+ /**
75
+ * Writes down a state only this tab's memory holds, once storage takes it, with no request: how
76
+ * a logout kept with no timer, which no try in this tab writes down, reaches a reload (SDK-D2012).
77
+ */
78
+ readonly writeDown: () => Promise<void>;
79
+ /** Clears a paid logout's flag in storage, when storage takes the write (sdk#506). */
80
+ readonly writeDownPaid: () => Promise<void>;
81
+ }
82
+ export declare function createLogout(deps: SessionDeps, cell: SessionCell, { shared, withState, withCookie, broadcast }: SharedState, { release }: Pick<Tokens, 'release'>): Logout;