@flipstream/sdk 0.15.0 → 0.16.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.
Files changed (99) hide show
  1. package/CHANGELOG.md +57 -0
  2. package/dist/api/datasets.js +8 -8
  3. package/dist/api/datasets.js.map +1 -1
  4. package/dist/api/log.js +6 -10
  5. package/dist/api/log.js.map +1 -1
  6. package/dist/api/pathArgument.d.ts +7 -0
  7. package/dist/api/pathArgument.js +12 -0
  8. package/dist/api/pathArgument.js.map +1 -0
  9. package/dist/first-party/attempts.d.ts +1 -1
  10. package/dist/first-party/authProxyMark.d.ts +5 -2
  11. package/dist/first-party/authProxyMark.js +5 -2
  12. package/dist/first-party/authProxyMark.js.map +1 -1
  13. package/dist/first-party/calls/consent.d.ts +30 -18
  14. package/dist/first-party/calls/consent.js.map +1 -1
  15. package/dist/first-party/calls/device.d.ts +42 -20
  16. package/dist/first-party/calls/device.js.map +1 -1
  17. package/dist/first-party/calls/invitation.d.ts +99 -53
  18. package/dist/first-party/calls/invitation.js +23 -5
  19. package/dist/first-party/calls/invitation.js.map +1 -1
  20. package/dist/first-party/calls/logIn.d.ts +30 -10
  21. package/dist/first-party/calls/logIn.js.map +1 -1
  22. package/dist/first-party/calls/reset.d.ts +4 -1
  23. package/dist/first-party/calls/reset.js.map +1 -1
  24. package/dist/first-party/calls/signUp.d.ts +1 -1
  25. package/dist/first-party/calls/signUp.js +1 -1
  26. package/dist/first-party/calls/signUp.js.map +1 -1
  27. package/dist/first-party/calls/twoStepSetup.d.ts +28 -5
  28. package/dist/first-party/calls/twoStepSetup.js.map +1 -1
  29. package/dist/first-party/entry.d.ts +223 -199
  30. package/dist/first-party/entry.js +21 -19
  31. package/dist/first-party/entry.js.map +1 -1
  32. package/dist/first-party/hooks.d.ts +22 -7
  33. package/dist/first-party/hooks.js.map +1 -1
  34. package/dist/first-party/linkToken.d.ts +2 -3
  35. package/dist/first-party/linkToken.js +2 -3
  36. package/dist/first-party/linkToken.js.map +1 -1
  37. package/dist/first-party/links.d.ts +3 -3
  38. package/dist/first-party/links.js +4 -4
  39. package/dist/first-party/links.js.map +1 -1
  40. package/dist/first-party/proxy/app.d.ts +15 -8
  41. package/dist/first-party/proxy/app.js.map +1 -1
  42. package/dist/first-party/proxy/cases.d.ts +34 -17
  43. package/dist/first-party/proxy/cases.js +2 -3
  44. package/dist/first-party/proxy/cases.js.map +1 -1
  45. package/dist/first-party/proxy/decide.d.ts +6 -4
  46. package/dist/first-party/proxy/decide.js +2 -3
  47. package/dist/first-party/proxy/decide.js.map +1 -1
  48. package/dist/first-party/proxy/headers.d.ts +2 -3
  49. package/dist/first-party/proxy/headers.js.map +1 -1
  50. package/dist/first-party/proxy/pass-back.d.ts +14 -9
  51. package/dist/first-party/proxy/pass-back.js +2 -6
  52. package/dist/first-party/proxy/pass-back.js.map +1 -1
  53. package/dist/first-party/proxy/paths.d.ts +5 -7
  54. package/dist/first-party/proxy/paths.js +5 -7
  55. package/dist/first-party/proxy/paths.js.map +1 -1
  56. package/dist/first-party/proxy/stand-in.d.ts +2 -0
  57. package/dist/first-party/proxy/stand-in.js +28 -5
  58. package/dist/first-party/proxy/stand-in.js.map +1 -1
  59. package/dist/first-party/report.d.ts +20 -11
  60. package/dist/first-party/report.js.map +1 -1
  61. package/dist/first-party/result.d.ts +86 -18
  62. package/dist/first-party/result.js.map +1 -1
  63. package/dist/first-party/session/cell.d.ts +29 -5
  64. package/dist/first-party/session/cell.js +16 -2
  65. package/dist/first-party/session/cell.js.map +1 -1
  66. package/dist/first-party/session/closed.d.ts +6 -3
  67. package/dist/first-party/session/closed.js +6 -3
  68. package/dist/first-party/session/closed.js.map +1 -1
  69. package/dist/first-party/session/logout.d.ts +40 -5
  70. package/dist/first-party/session/logout.js +314 -43
  71. package/dist/first-party/session/logout.js.map +1 -1
  72. package/dist/first-party/session/owed.d.ts +98 -10
  73. package/dist/first-party/session/owed.js +143 -10
  74. package/dist/first-party/session/owed.js.map +1 -1
  75. package/dist/first-party/session/refresh.d.ts +1 -1
  76. package/dist/first-party/session/refresh.js +28 -11
  77. package/dist/first-party/session/refresh.js.map +1 -1
  78. package/dist/first-party/session/session.js +3 -6
  79. package/dist/first-party/session/session.js.map +1 -1
  80. package/dist/first-party/session/shared.js +49 -8
  81. package/dist/first-party/session/shared.js.map +1 -1
  82. package/dist/first-party/session/signIn.d.ts +10 -5
  83. package/dist/first-party/session/signIn.js +59 -10
  84. package/dist/first-party/session/signIn.js.map +1 -1
  85. package/dist/first-party/session/signInThrough.d.ts +2 -1
  86. package/dist/first-party/session/signInThrough.js +67 -18
  87. package/dist/first-party/session/signInThrough.js.map +1 -1
  88. package/dist/first-party/session/sync.js +39 -8
  89. package/dist/first-party/session/sync.js.map +1 -1
  90. package/dist/first-party/signInHooks.d.ts +43 -15
  91. package/dist/first-party/state.d.ts +20 -0
  92. package/dist/first-party/state.js +3 -0
  93. package/dist/first-party/state.js.map +1 -1
  94. package/dist/flipstream.d.ts +29 -10
  95. package/dist/flipstream.js +69 -20
  96. package/dist/flipstream.js.map +1 -1
  97. package/dist/version.gen.js +1 -1
  98. package/docs/first-party.md +52 -30
  99. package/package.json +1 -1
@@ -27,7 +27,20 @@ export const Message = z.discriminatedUnion('type', [
27
27
  */
28
28
  export function adoptNewer(cell, state) {
29
29
  cell.known = state;
30
- cell.owedStored = null;
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;
31
44
  }
32
45
  /**
33
46
  * Ends this tab's code screen because something else moved the session on: another tab's sign-in
@@ -66,7 +79,8 @@ export function createCell(supported) {
66
79
  principalKey: null,
67
80
  revision: 0,
68
81
  known: NO_RECORD,
69
- owedStored: null,
82
+ owedFor: null,
83
+ slotFor: null,
70
84
  paidLogout: null,
71
85
  refreshInFlight: null,
72
86
  refreshTimer: undefined,
@@ -1 +1 @@
1
- {"version":3,"file":"cell.js","sourceRoot":"","sources":["../../../src/first-party/session/cell.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAGvB,OAAO,EAAE,SAAS,EAAkB,MAAM,aAAa,CAAA;AAoBvD,gGAAgG;AAChG,MAAM,UAAU,SAAS,CAAC,IAAuB,EAAE,KAA+C;IAChG,OAAO,IAAI,KAAK,IAAI,IAAI,IAAI,CAAC,UAAU,KAAK,KAAK,CAAC,UAAU,IAAI,IAAI,CAAC,MAAM,KAAK,KAAK,CAAC,MAAM,CAAA;AAC9F,CAAC;AAED,4FAA4F;AAC5F,MAAM,CAAC,MAAM,oBAAoB,GAAG,KAAK,CAAA;AACzC,iEAAiE;AACjE,MAAM,CAAC,MAAM,UAAU,GAAsB,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC,CAAA;AACzF,4EAA4E;AAC5E,MAAM,CAAC,MAAM,0BAA0B,GAAG,MAAM,CAAA;AAEhD,oDAAoD;AACpD,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,CAAC,kBAAkB,CAAC,MAAM,EAAE;IAClD,CAAC,CAAC,MAAM,CAAC;QACP,IAAI,EAAE,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC;QACxB,UAAU,EAAE,CAAC,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;QAC1B,MAAM,EAAE,CAAC,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;QACtB,WAAW,EAAE,CAAC,CAAC,MAAM,EAAE;KACxB,CAAC;IACF,CAAC,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC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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.
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
5
4
  */
6
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
+ */
7
10
  constructor(call: string);
8
11
  }
@@ -1,9 +1,12 @@
1
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.
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
5
4
  */
6
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
+ */
7
10
  constructor(call) {
8
11
  super(`${call} was called on a closed session: make a new one`);
9
12
  this.name = 'SessionClosedError';
@@ -1 +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"}
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"}
@@ -3,6 +3,16 @@ import { type SessionCell } from './cell.js';
3
3
  import { type SharedState } from './shared.js';
4
4
  import type { Tokens } from './tokens.js';
5
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
+ };
6
16
  /**
7
17
  * What `recordSignOut` landed: the signed-out generation, whose pending logout `oweRevoke` revokes
8
18
  * through, and the record's serial then, which `signOut()` pins (SDK-D2022). A sign-out keeps the
@@ -21,15 +31,31 @@ export interface Logout {
21
31
  * generation's.
22
32
  */
23
33
  readonly sendLogout: (generation: number) => Promise<LogoutAnswer>;
34
+ /**
35
+ * Pay a copy or a ride another session confirmed, before the caller's `sendLogout`. The caller
36
+ * holds the cookie lock, as the try does. `paid`: a copy or a ride was paid, and nothing is
37
+ * sent. `nothing-owed`: nothing is owed, including when a send's wait finds the flag gone, and
38
+ * nothing is sent. `send`: send `generation`, what is owed now (SDK-D2058).
39
+ */
40
+ readonly payBeforeSend: () => Promise<{
41
+ readonly kind: 'paid';
42
+ } | {
43
+ readonly kind: 'nothing-owed';
44
+ } | {
45
+ readonly kind: 'send';
46
+ readonly generation: number;
47
+ readonly serial: number;
48
+ }>;
24
49
  /**
25
50
  * Records a sign-out: a new signed-out generation and its pending logout in one write, told to
26
51
  * every tab. When storage refuses the write, this tab keeps it in memory and tells the others; it
27
52
  * is written down at the next logout try storage takes, or, for a logout kept with no timer, at the
28
53
  * next `sync()` storage takes (`writeDown`, SDK-D2012). Answers the new generation and the
29
54
  * record's serial with it (`SignedOut`). `landed` runs as the sign-out lands, written or kept in
30
- * memory, before the tabs are told.
55
+ * memory, before the tabs are told. `owing`: whose cookie the logout is owed for, kept beside it
56
+ * when storage refuses (owed.ts).
31
57
  */
32
- readonly recordSignOut: (landed?: () => void) => Promise<SignedOut>;
58
+ readonly recordSignOut: (owing: Owing, landed?: () => void) => Promise<SignedOut>;
33
59
  /**
34
60
  * A closed session leaves a sign-out storage never took in the page's slot, for whichever session
35
61
  * in the page looks next (owed.ts, sdk#483). Called by `close()`, and by a sign-out made after it.
@@ -58,8 +84,9 @@ export interface Logout {
58
84
  * backing off, since that may be gone in seconds. Refused (a 401 or 403) or outside the contract
59
85
  * (`broken`, a host routed or deployed wrongly): time doesn't mend either, so it stays owed with
60
86
  * 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),
87
+ * (`retry()`, `start()`, `refreshAfterUnauthorized()`), and, unless a sign-out is still owed here
88
+ * (its own or the page's slot), when another tab posts a token this tab can't read: each sends an
89
+ * owed logout first anyway. `sync()` only writes it down (SDK-D2012),
63
90
  * unless a record written since this tab's unrecorded sign-out ended the kept mark, this
64
91
  * sign-out's own record included (written down by a tab that heard it): the next `sync()` then
65
92
  * sends it once (`dropUnrecordedSignOut`). `generation` and `serial` name the cookie the logout
@@ -74,9 +101,17 @@ export interface Logout {
74
101
  /**
75
102
  * Writes down a state only this tab's memory holds, once storage takes it, with no request: how
76
103
  * a logout kept with no timer, which no try in this tab writes down, reaches a reload (SDK-D2012).
104
+ * A copy or a ride another session has confirmed since is paid first, so the pending flag is not
105
+ * written back over that confirm (sdk#586). One that does not land clears the flag in memory only.
77
106
  */
78
107
  readonly writeDown: () => Promise<void>;
79
- /** Clears a paid logout's flag in storage, when storage takes the write (sdk#506). */
108
+ /**
109
+ * Writes a paid sign-out down once storage takes it, with no request (sdk#586). The sdk#506
110
+ * branch stays first: a flag storage already holds. The other branch writes over the base
111
+ * record, its generation, serial and person, or over a lower record that is not signed in and
112
+ * owes no logout (SDK-D2053). A heard sign-out, or one whose serial is unknown, writes only
113
+ * over that lower record. A paid entry is read here and by the give-way check (SDK-D2050).
114
+ */
80
115
  readonly writeDownPaid: () => Promise<void>;
81
116
  }
82
117
  export declare function createLogout(deps: SessionDeps, cell: SessionCell, { shared, withState, withCookie, broadcast }: SharedState, { release }: Pick<Tokens, 'release'>): Logout;
@@ -1,6 +1,6 @@
1
1
  import { readState } from '../state.js';
2
- import { BACKOFF_MS, isKeptFor } from './cell.js';
3
- import { leaveOwed } from './owed.js';
2
+ import { BACKOFF_MS, isKeptFor, owedLogout } from './cell.js';
3
+ import { leaveOwed, owedWithoutReached, revokedOn } from './owed.js';
4
4
  import { isNewer } from './shared.js';
5
5
  const CONFIRMED = { kind: 'confirmed' };
6
6
  const NOT_ASKED = { kind: 'unconfirmed', reason: 'unavailable' };
@@ -81,27 +81,34 @@ export function createLogout(deps, cell, { shared, withState, withCookie, broadc
81
81
  // the slot's included, whatever generation an object counted for it.
82
82
  if (deps.owed !== null)
83
83
  deps.owed.left = null;
84
- const cleared = await withState((state) => ({
85
- write: state.logoutPending?.generation === generation
84
+ const cleared = await withState((state) => {
85
+ const write = state.logoutPending?.generation === generation
86
86
  ? { ...state, logoutPending: null }
87
- : null,
88
- result: undefined,
89
- }));
90
- // Storage refused: the confirmation is kept in memory, so this tab, and the slot it would
91
- // leave when closed, don't owe it again (sdk#483). A flag storage holds is remembered as paid
92
- // by its stored generation and serial, so a tie with the record doesn't bring it back (sdk#506).
93
- if (!cleared.ran) {
94
- // Read after the refused write let go of the state lock, so another tab may have written
95
- // since. Safe: the only write that sets a flag at the same generation (a redeem's revoke)
96
- // runs under the cookie lock, which every caller of sendLogout holds now.
87
+ : null;
88
+ // A landed clear drops `owedFor` (`adoptNewer`). A refused clear, or a confirm that
89
+ // wrote nothing because a newer sign-out moved the flag, keeps it (sdk#572).
90
+ return { write, result: write !== null };
91
+ });
92
+ // The clear did not land: storage refused the write, or this confirm wrote nothing because
93
+ // a newer sign-out had moved the flag. The confirmation is kept in memory, so this tab, and
94
+ // the slot it would leave when closed, don't owe it again (sdk#483). A flag storage holds at
95
+ // this generation is remembered as paid, so a tie with the record doesn't bring it back (sdk#506).
96
+ if (!cleared.ran || cleared.value !== true) {
97
+ // Read after the clear let go of the state lock, so another tab may have written since.
98
+ // Safe: the only write that sets a flag at the same generation (a redeem's revoke) runs
99
+ // under the cookie lock, which every caller of sendLogout holds now.
97
100
  const stored = storage === null ? null : readState(storage);
98
101
  if (stored?.generation === generation && stored.logoutPending?.generation === generation) {
99
102
  cell.paidLogout = { generation, serial: stored.serial };
100
103
  }
101
104
  if (cell.known.logoutPending?.generation === generation)
102
105
  cell.known = { ...cell.known, logoutPending: null };
106
+ // The write did not land. Keep what this sign-out was made against, so a record written
107
+ // since can end it (sdk#572, SDK-D1967). A write that lands drops it, below.
108
+ }
109
+ else {
110
+ cell.owedFor = null;
103
111
  }
104
- cell.owedStored = null;
105
112
  cell.logoutKept = null;
106
113
  }
107
114
  return answer;
@@ -134,10 +141,10 @@ export function createLogout(deps, cell, { shared, withState, withCookie, broadc
134
141
  }
135
142
  }
136
143
  };
137
- const recordSignOut = async (landed) => {
144
+ const recordSignOut = async (owing, landed) => {
138
145
  const signedOut = (state) => {
139
146
  const generation = state.generation + 1;
140
- return { ...state, generation, signedIn: false, logoutPending: { generation }, principal: null, person: null };
147
+ return { ...state, generation, signedIn: false, logoutPending: { generation }, principal: null, person: null, madeBy: 'sign-out' };
141
148
  };
142
149
  const told = (generation) => {
143
150
  landed?.();
@@ -157,9 +164,37 @@ export function createLogout(deps, cell, { shared, withState, withCookie, broadc
157
164
  // close() the channel is closed: the sign-out reaches this page's slot and no other tab.
158
165
  // What storage read now is what the slot carries if this object closes first (SDK-D1977).
159
166
  // Read before the number is set: shared() adopting a record ends any earlier one (SDK-D2005).
160
- const next = signedOut(shared());
161
- cell.owedStored ??= storage === null ? 0 : readState(storage).generation;
167
+ const before = shared();
168
+ const next = signedOut(before);
169
+ // Whose cookie the logout is owed for: a revoke names its own sign-in's person (null when its
170
+ // answer couldn't be read); a sign-out, the person signed in here, from the record or, on a
171
+ // record without one, the token this tab holds at that generation (as a refresh compares,
172
+ // refresh.ts). Unknown: null.
173
+ const held = cell.held;
174
+ const person = owing.kind === 'revoke'
175
+ ? owing.person
176
+ : before.person ?? (held !== null && held.generation === before.generation ? held.access.person : null);
177
+ const record = storage === null ? null : readState(storage);
178
+ const stored = record === null ? 0 : record.generation;
179
+ const serial = record === null ? 0 : record.serial;
180
+ // The record's person, not whose cookie the logout is owed for. A ride renames that other
181
+ // field and leaves this one (SDK-D2053).
182
+ const basePerson = record === null ? null : record.person;
183
+ // A paid entry is not owed (SDK-D2047): this sign-out starts its own. An unpaid one already
184
+ // owed here (heard while this waited for the state lock): this revoke rides on it, as
185
+ // `oweRevoke` would have, so it is renamed as a ride (PR 566 O1).
186
+ const open = owedLogout(cell);
187
+ cell.owedFor = open === null
188
+ ? person === null
189
+ ? { stored, serial, basePerson, person, revoke: null }
190
+ : { stored, serial, basePerson, person, revoke: owing.kind === 'revoke' ? 'made' : null }
191
+ : owing.kind === 'revoke'
192
+ // A new object from `revokedOn` carries no `reachedAt`. The same object keeps it (SDK-D2055).
193
+ ? revokedOn(open, person, stored)
194
+ : open;
162
195
  cell.known = next;
196
+ // This session's own sign-out now: `owedFor` decides, never the slot's entry (sdk#567).
197
+ cell.slotFor = null;
163
198
  told(cell.known.generation);
164
199
  // Closed already (a sign-in answered after close(), or a signOut() that waited for the state
165
200
  // lock): close() left nothing, so this does (sdk#483 finding 1).
@@ -171,23 +206,74 @@ export function createLogout(deps, cell, { shared, withState, withCookie, broadc
171
206
  // for, with that number: a sign-in recorded since then drops the slot (SDK-D1977). Memory against
172
207
  // memory, never the count against a stored number (SDK-D1967, SDK-D2005).
173
208
  const leaveIfOwed = () => {
174
- if (deps.owed === null || cell.owedStored === null)
209
+ // A paid entry is not owed, so a close does not leave it (SDK-D2047).
210
+ const owedFor = owedLogout(cell);
211
+ if (deps.owed === null || owedFor === null || cell.known.signedIn)
175
212
  return;
176
213
  const known = cell.known;
177
- if (known.signedIn || known.logoutPending?.generation !== known.generation)
214
+ const left = deps.owed.left;
215
+ // The ride is already in the slot, and a later ride renamed it in place under the cookie
216
+ // lock. Merging this ride again is two riders, and that merge is not associative (SDK-D2046).
217
+ if (owedFor.revoke === 'rode' &&
218
+ left !== null &&
219
+ left.revoke === 'rode' &&
220
+ left.generation === known.generation &&
221
+ left.stored === owedFor.stored)
178
222
  return;
179
- leaveOwed(deps.owed, { generation: cell.known.generation, stored: cell.owedStored });
223
+ leaveOwed(deps.owed, { generation: known.generation, ...owedWithoutReached(owedFor) });
180
224
  };
181
225
  /**
182
- * Clears a paid logout's flag in storage, at the first write it takes: no request (sdk#506).
183
- * What is written is what this tab knows: the paid record with its flag cleared, or a newer
184
- * state only memory holds (a sign-out storage refused, or one heard from another tab), which
185
- * `writeDown` would write at the same moment. Only while the stored record is still the paid one.
226
+ * Writes a paid sign-out down once storage takes it, with no request. The sdk#506 branch stays
227
+ * first: a flag storage already holds, cleared while that record is still the paid one. The other
228
+ * branch is derived, not a second field: the flag is already clear, `owedFor` or else `slotFor`
229
+ * is still set, and known is newer than storage (sdk#586). The base is that generation, that
230
+ * serial and that record's person (`basePerson`), not the person the logout is owed for: a ride
231
+ * renames the owed person and leaves the record's (SDK-D2053). A heard sign-out names no owed
232
+ * person and still matches its base, because the entry kept the record's person. A null serial
233
+ * names no record, so it writes only over a lower record that is not signed in and owes no
234
+ * logout. The same person signed in again after a lost record, then renewed until the serial
235
+ * matches, is the one this cannot see. The slot is read inside the state lock, after reconcile,
236
+ * and neither branch writes while it holds an entry. A refused write leaves the mark, and the
237
+ * next sync tries again.
186
238
  */
239
+ const paidMark = () => {
240
+ if (cell.known.logoutPending?.generation === cell.known.generation)
241
+ return null;
242
+ const owed = cell.owedFor ?? cell.slotFor;
243
+ if (owed === null || storage === null || !isNewer(cell.known, readState(storage)))
244
+ return null;
245
+ return { stored: owed.stored, serial: owed.serial, basePerson: owed.basePerson };
246
+ };
187
247
  const writeDownPaid = async () => {
188
- if (cell.paidLogout === null)
248
+ if (cell.paidLogout !== null) {
249
+ await withState((state) => ({
250
+ write: (deps.owed?.left ?? null) !== null || cell.paidLogout === null ? null : state,
251
+ result: undefined,
252
+ }));
189
253
  return;
190
- await withState((state) => ({ write: cell.paidLogout === null ? null : state, result: undefined }));
254
+ }
255
+ if (paidMark() === null || storage === null)
256
+ return;
257
+ const recordStore = storage;
258
+ await withState((state) => {
259
+ const mark = paidMark();
260
+ const record = readState(recordStore);
261
+ const slotHeld = (deps.owed?.left ?? null) !== null;
262
+ // The record's person, kept where the entry was made. The person the logout is owed for is
263
+ // a different field, and a ride renames that one (SDK-D2053).
264
+ const onBase = mark !== null &&
265
+ !slotHeld &&
266
+ record.generation === mark.stored &&
267
+ mark.serial !== null &&
268
+ record.serial === mark.serial &&
269
+ record.person === mark.basePerson;
270
+ const lowerQuiet = mark !== null &&
271
+ !slotHeld &&
272
+ record.generation < mark.stored &&
273
+ !record.signedIn &&
274
+ record.logoutPending?.generation !== record.generation;
275
+ return { write: onBase || lowerQuiet ? state : null, result: undefined };
276
+ });
191
277
  };
192
278
  /**
193
279
  * Writes down a state only this tab's memory holds (a sign-out storage refused, or one
@@ -201,6 +287,145 @@ export function createLogout(deps, cell, { shared, withState, withCookie, broadc
201
287
  result: undefined,
202
288
  }));
203
289
  };
290
+ // Whether this call can pay, decided before any wait. `send` when it cannot: the try then writes
291
+ // the pending sign-out as its first wait, which is when `sync()` sees it (sdk#542 O1). Awaiting
292
+ // a helper that already knows the answer is `send` lets that caller continue before the write.
293
+ const payCopyEarly = () => {
294
+ const state = shared();
295
+ const owed = state.logoutPending;
296
+ // The page slot has to exist and be empty. An entry still held is not a confirm (SDK-D2052).
297
+ const slotEmpty = deps.owed !== null && deps.owed.left === null;
298
+ if (owed === null || owed.generation !== state.generation || !slotEmpty || storage === null)
299
+ return 'send';
300
+ // A live ride the slot held, not a sign-out this session still has to send. An open sign-out's
301
+ // slot is empty until close, and a ride born inside recordSignOut never reached the slot, so an
302
+ // empty slot proves nothing for either (SDK-D2052, SDK-D2055).
303
+ const ride = owedLogout(cell);
304
+ // `state.generation` is the pending generation. A ride pays only while that equals the
305
+ // generation it reached the slot at, so a heard newer sign-out sends (SDK-D2057).
306
+ if (ride !== null)
307
+ return ride.revoke === 'rode' && ride.reachedAt === state.generation ? 'look' : 'send';
308
+ return cell.slotFor === null ? 'send' : 'look';
309
+ };
310
+ const SEND = { kind: 'send' };
311
+ /**
312
+ * Pay a logout another session already confirmed, before anything writes the pending sign-out.
313
+ * One helper for the try and for a call that shares it. A copy (`slotFor`, sdk#583, SDK-D2049)
314
+ * or a live ride (`owedFor` with `revoke: 'rode'`, sdk#588, SDK-D2052), and only while the page
315
+ * slot exists and is empty.
316
+ * Either is not sent when the record is at or below the generation it was made against and
317
+ * that record owes no logout of its own (SDK-D2049, SDK-D2056, SDK-D2057). A ride pays only
318
+ * while the pending generation equals `reachedAt`, the generation it reached the slot at. The
319
+ * write of either lands only on the base (generation, serial and
320
+ * `basePerson`) or a lower record that is not signed in and owes no logout, never while the slot
321
+ * holds an entry (SDK-D2053). One that does not land pays in memory: the flag clears, the entry
322
+ * stays, nothing is written and nothing is sent. A ride the slot never held sends (SDK-D2055).
323
+ * `paid`: nothing is sent. `send`: the try re-reads and may send. A refused write clears the
324
+ * flag and leaves the entry.
325
+ */
326
+ const payCopy = async () => {
327
+ if (payCopyEarly() === 'send')
328
+ return SEND;
329
+ const state = shared();
330
+ const owed = state.logoutPending;
331
+ const ride = owedLogout(cell);
332
+ const copy = ride === null ? cell.slotFor : null;
333
+ const mark = ride ?? copy;
334
+ // Once more, after the early check: a reconcile since can drop the entry. The callback decides the rest.
335
+ if (owed === null || owed.generation !== state.generation || mark === null || storage === null)
336
+ return SEND;
337
+ const generation = owed.generation;
338
+ const recordStore = storage;
339
+ // A holder, not a `let` the callback assigns: TypeScript would keep that `let` at `null` (TS#9998).
340
+ // `null` means the callback never ran. That is not a refused write, which ran and decided `pay`.
341
+ const decision = { value: null };
342
+ const cleared = await withState((current) => {
343
+ // `current` is known after reconcile, under the lock.
344
+ const record = readState(recordStore);
345
+ const slotEmpty = deps.owed !== null && deps.owed.left === null;
346
+ const still = ride !== null ? cell.owedFor === ride : cell.slotFor === copy;
347
+ const pending = current.generation === generation && current.logoutPending?.generation === generation;
348
+ // The record's person where the entry was made. The person the logout is owed for is a
349
+ // different field, and a ride renames that one (SDK-D2053).
350
+ const onBase = record.generation === mark.stored &&
351
+ mark.serial !== null &&
352
+ record.serial === mark.serial &&
353
+ record.person === mark.basePerson;
354
+ const lowerQuiet = record.generation < mark.stored &&
355
+ !record.signedIn &&
356
+ record.logoutPending?.generation !== record.generation;
357
+ const land = onBase || lowerQuiet;
358
+ // Generation for the copy and the ride, and not a record that owes its own logout: that
359
+ // flag is the browser's cookie, so the try sends (SDK-D2049, SDK-D2056, SDK-D2057).
360
+ const stands = record.generation <= mark.stored && record.logoutPending?.generation !== record.generation;
361
+ const fromSlot = ride === null || ride.reachedAt === generation;
362
+ if (slotEmpty && still && pending && stands && fromSlot) {
363
+ decision.value = { kind: 'pay', generation: current.generation, serial: current.serial };
364
+ if (!land) {
365
+ cell.known = { ...current, logoutPending: null };
366
+ return { write: null, result: 'pay' };
367
+ }
368
+ return { write: { ...current, logoutPending: null }, result: 'pay' };
369
+ }
370
+ decision.value = 'send';
371
+ return { write: null, result: 'send' };
372
+ });
373
+ const paid = decision.value;
374
+ if (paid !== null && paid !== 'send' && !cleared.ran) {
375
+ if (cell.known.logoutPending?.generation === generation) {
376
+ cell.known = { ...cell.known, logoutPending: null };
377
+ }
378
+ }
379
+ if (paid !== null && paid !== 'send' && isKeptFor(cell.logoutKept, cell.known))
380
+ cell.logoutKept = null;
381
+ return paid !== null && paid !== 'send'
382
+ ? { kind: 'paid', generation: paid.generation, serial: paid.serial }
383
+ : SEND;
384
+ };
385
+ /**
386
+ * After a pay, a sign-out past the one just paid. `nothing`: nothing is owed, or only the one
387
+ * just paid. One read for the try and for `payBeforeSend`, so the two cannot drift. The caller
388
+ * writes. The try writes that newer flag down before it sends. A restore does not: it keeps
389
+ * main's send, and a refused or unanswered send leaves the flag for the timer's try, which
390
+ * writes it down first (SDK-D2058).
391
+ */
392
+ const owedPastPay = (pay) => {
393
+ const now = shared();
394
+ const newer = now.logoutPending;
395
+ if (newer === null || newer.generation !== now.generation || newer.generation <= pay.generation) {
396
+ return { kind: 'nothing' };
397
+ }
398
+ return { kind: 'newer', generation: newer.generation, serial: now.serial };
399
+ };
400
+ const payBeforeSend = async () => {
401
+ const state = shared();
402
+ const owed = state.logoutPending;
403
+ if (owed === null || owed.generation !== state.generation)
404
+ return { kind: 'nothing-owed' };
405
+ let pay;
406
+ try {
407
+ pay = payCopyEarly() === 'send' ? SEND : await payCopy();
408
+ }
409
+ catch (error) {
410
+ // A throw is not a pay. After any wait, send what is owed now,
411
+ // never what was read before it.
412
+ report({ kind: 'threw', step: 'logout', cause: error });
413
+ pay = SEND;
414
+ }
415
+ if (pay.kind === 'paid') {
416
+ const past = owedPastPay(pay);
417
+ if (past.kind === 'nothing')
418
+ return { kind: 'paid' };
419
+ return { kind: 'send', generation: past.generation, serial: past.serial };
420
+ }
421
+ // A send that waited may have moved on. Send what is owed now, or nothing when the flag is gone.
422
+ const again = shared();
423
+ const still = again.logoutPending;
424
+ if (still !== null && still.generation === again.generation) {
425
+ return { kind: 'send', generation: still.generation, serial: again.serial };
426
+ }
427
+ return { kind: 'nothing-owed' };
428
+ };
204
429
  /**
205
430
  * Sends the logout the shared state still owes, under the cookie lock. `answered`: what
206
431
  * pulse-auth said to the logout `generation` owed, or, for a try queued behind this tab's own send
@@ -213,26 +438,42 @@ export function createLogout(deps, cell, { shared, withState, withCookie, broadc
213
438
  // The mark as this try starts: one set later is a send this try queued behind.
214
439
  const before = cell.logoutKept;
215
440
  const locked = await withCookie(async () => {
441
+ // The copy or the ride is paid before persistKnown. Writing the pending flag first drops it,
442
+ // and the try then sends a logout the confirm already paid (sdk#583 round 1).
443
+ const pay = payCopyEarly() === 'send' ? SEND : await payCopy();
444
+ if (pay.kind === 'paid') {
445
+ // A sign-out past the one just paid is still owed: write it and send it. The answer
446
+ // otherwise is the state the pay cleared, not a later read of whatever landed since.
447
+ if (owedPastPay(pay).kind === 'nothing') {
448
+ read({ kind: 'nothing-owed' });
449
+ return { kind: 'nothing-owed', generation: pay.generation, serial: pay.serial };
450
+ }
451
+ }
216
452
  await persistKnown();
217
- // Closed while this waited for the lock: nothing new is sent (SDK-D1930). The flag stays
218
- // recorded, for another tab or the next page load; close() set no retry timer.
453
+ // Closed while this waited: nothing new is sent (SDK-D1930). The flag stays recorded, for
454
+ // another tab or the next page load; close() set no retry timer. The check sits after the
455
+ // write. One before it drops a pending sign-out storage had refused. The pay needs no check
456
+ // of its own: a pay that lands writes the clear and sends nothing, and one that does not land
457
+ // clears the flag in memory only. A send falls through to
458
+ // this write and then this check.
219
459
  if (cell.closed)
220
460
  return NOT_SENT;
221
- const state = shared();
222
- const owed = state.logoutPending;
223
- if (owed !== null && owed.generation === state.generation) {
224
- read({ kind: 'sends', generation: owed.generation });
461
+ // Re-read after the wait. The generation, the kept mark and the serial are this read's.
462
+ const again = shared();
463
+ const still = again.logoutPending;
464
+ if (still !== null && still.generation === again.generation) {
465
+ read({ kind: 'sends', generation: still.generation });
225
466
  // Kept while this waited for the lock, for this same cookie (this tab's restore or sign-in
226
467
  // sent it): pulse-auth's answer stands, and is this try's too. Sending it again would ask
227
468
  // the same question (sdk#531). A mark this try saw as it started is an older send: the
228
469
  // timer's try and any later one send, as before (PR 547 round 1).
229
470
  const kept = cell.logoutKept;
230
- if (kept !== before && isKeptFor(kept, state))
231
- return { kind: 'answered', answer: kept.answer, generation: owed.generation, serial: state.serial };
232
- return { kind: 'answered', answer: await sendLogout(owed.generation), generation: owed.generation, serial: state.serial };
471
+ if (kept !== before && isKeptFor(kept, again))
472
+ return { kind: 'answered', answer: kept.answer, generation: still.generation, serial: again.serial };
473
+ return { kind: 'answered', answer: await sendLogout(still.generation), generation: still.generation, serial: again.serial };
233
474
  }
234
475
  read({ kind: 'nothing-owed' });
235
- return { kind: 'nothing-owed', generation: state.generation, serial: state.serial };
476
+ return { kind: 'nothing-owed', generation: again.generation, serial: again.serial };
236
477
  });
237
478
  return locked.ran ? locked.value : NOT_SENT;
238
479
  };
@@ -300,10 +541,21 @@ export function createLogout(deps, cell, { shared, withState, withCookie, broadc
300
541
  // a reload in between doesn't restore the session (SDK-D2012). No request is sent. A write-down
301
542
  // that throws is reported, and the call still takes the shared try's outcome: that try's settle
302
543
  // owns the timer and the kept mark, so a sharer never arms the back-off (SDK-D1963).
303
- const share = (outcome) => persistKnown().then(() => outcome, (error) => {
304
- report({ kind: 'threw', step: 'logout', cause: error });
305
- return outcome;
306
- });
544
+ const share = (outcome) => {
545
+ const wrote = (error) => {
546
+ report({ kind: 'threw', step: 'logout', cause: error });
547
+ return outcome;
548
+ };
549
+ const writeThen = () => persistKnown().then(() => outcome, wrote);
550
+ // No copy to pay: the write is this call's first wait, as it was before the gate (sdk#542 O1).
551
+ if (payCopyEarly() === 'send')
552
+ return writeThen();
553
+ return payCopy().then((pay) => (pay.kind === 'paid' ? outcome : writeThen()), (error) => {
554
+ // A throw is not a pay. The pending flag is still written down (sdk#583 round 2).
555
+ report({ kind: 'threw', step: 'logout', cause: error });
556
+ return writeThen();
557
+ });
558
+ };
307
559
  const tryLogout = (pinnedSerial) => {
308
560
  const state = shared();
309
561
  const owed = state.logoutPending !== null && state.logoutPending.generation === state.generation
@@ -323,6 +575,25 @@ export function createLogout(deps, cell, { shared, withState, withCookie, broadc
323
575
  void tryLogout();
324
576
  }, delay);
325
577
  };
326
- return { sendLogout, recordSignOut, leaveIfOwed, tryLogout, notConfirmed, scheduleLogoutRetry, writeDown: persistKnown, writeDownPaid };
578
+ // The kept branch's write. A copy or a ride another session confirmed since that answer is paid
579
+ // first, so the pending flag is not written back over the confirm (sdk#586). One that does not
580
+ // land clears the flag in memory only. Either way nothing is sent.
581
+ const writeDown = async () => {
582
+ if (payCopyEarly() !== 'send') {
583
+ try {
584
+ const pay = await payCopy();
585
+ // A paid answer is written by the next sync, under the write-down's guards, as share()
586
+ // leaves it (sdk#586 round 2). persistKnown would write it by newness alone.
587
+ if (pay.kind === 'paid')
588
+ return;
589
+ }
590
+ catch (error) {
591
+ // A throw is not a pay. The pending flag is still written down, as share() does (sdk#586).
592
+ report({ kind: 'threw', step: 'logout', cause: error });
593
+ }
594
+ }
595
+ await persistKnown();
596
+ };
597
+ return { sendLogout, payBeforeSend, recordSignOut, leaveIfOwed, tryLogout, notConfirmed, scheduleLogoutRetry, writeDown, writeDownPaid };
327
598
  }
328
599
  //# sourceMappingURL=logout.js.map