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