@flipstream/sdk 0.14.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 (218) hide show
  1. package/CHANGELOG.md +76 -0
  2. package/README.md +95 -1
  3. package/dist/auth/transport.d.ts +6 -2
  4. package/dist/auth/transport.js +5 -3
  5. package/dist/auth/transport.js.map +1 -1
  6. package/dist/core/call.d.ts +4 -0
  7. package/dist/core/call.js +7 -1
  8. package/dist/core/call.js.map +1 -1
  9. package/dist/core/fetch.d.ts +20 -1
  10. package/dist/core/fetch.js +50 -3
  11. package/dist/core/fetch.js.map +1 -1
  12. package/dist/core/hosts.d.ts +11 -0
  13. package/dist/core/hosts.js +13 -0
  14. package/dist/core/hosts.js.map +1 -0
  15. package/dist/core/identify.d.ts +6 -2
  16. package/dist/core/identify.js +16 -12
  17. package/dist/core/identify.js.map +1 -1
  18. package/dist/core/result.d.ts +1 -1
  19. package/dist/core/shared.d.ts +2 -0
  20. package/dist/core/shared.js +1 -0
  21. package/dist/core/shared.js.map +1 -1
  22. package/dist/core/signal.d.ts +7 -0
  23. package/dist/core/signal.js +9 -0
  24. package/dist/core/signal.js.map +1 -1
  25. package/dist/first-party/asPerson.d.ts +13 -0
  26. package/dist/first-party/asPerson.js +144 -0
  27. package/dist/first-party/asPerson.js.map +1 -0
  28. package/dist/first-party/attempts.d.ts +71 -0
  29. package/dist/first-party/attempts.js +132 -0
  30. package/dist/first-party/attempts.js.map +1 -0
  31. package/dist/first-party/authProxyMark.d.ts +7 -0
  32. package/dist/first-party/authProxyMark.js +8 -0
  33. package/dist/first-party/authProxyMark.js.map +1 -0
  34. package/dist/first-party/browser.d.ts +55 -0
  35. package/dist/first-party/browser.js +137 -0
  36. package/dist/first-party/browser.js.map +1 -0
  37. package/dist/first-party/browserSession.d.ts +95 -0
  38. package/dist/first-party/browserSession.js +242 -0
  39. package/dist/first-party/browserSession.js.map +1 -0
  40. package/dist/first-party/calls/consent.d.ts +77 -0
  41. package/dist/first-party/calls/consent.js +174 -0
  42. package/dist/first-party/calls/consent.js.map +1 -0
  43. package/dist/first-party/calls/device.d.ts +75 -0
  44. package/dist/first-party/calls/device.js +133 -0
  45. package/dist/first-party/calls/device.js.map +1 -0
  46. package/dist/first-party/calls/invitation.d.ts +198 -0
  47. package/dist/first-party/calls/invitation.js +240 -0
  48. package/dist/first-party/calls/invitation.js.map +1 -0
  49. package/dist/first-party/calls/logIn.d.ts +41 -0
  50. package/dist/first-party/calls/logIn.js +48 -0
  51. package/dist/first-party/calls/logIn.js.map +1 -0
  52. package/dist/first-party/calls/reset.d.ts +58 -0
  53. package/dist/first-party/calls/reset.js +163 -0
  54. package/dist/first-party/calls/reset.js.map +1 -0
  55. package/dist/first-party/calls/signUp.d.ts +21 -0
  56. package/dist/first-party/calls/signUp.js +72 -0
  57. package/dist/first-party/calls/signUp.js.map +1 -0
  58. package/dist/first-party/calls/twoStepSetup.d.ts +55 -0
  59. package/dist/first-party/calls/twoStepSetup.js +168 -0
  60. package/dist/first-party/calls/twoStepSetup.js.map +1 -0
  61. package/dist/first-party/claims.d.ts +88 -0
  62. package/dist/first-party/claims.js +181 -0
  63. package/dist/first-party/claims.js.map +1 -0
  64. package/dist/first-party/enrollment.d.ts +10 -0
  65. package/dist/first-party/enrollment.js +44 -0
  66. package/dist/first-party/enrollment.js.map +1 -0
  67. package/dist/first-party/entry.d.ts +549 -0
  68. package/dist/first-party/entry.js +580 -0
  69. package/dist/first-party/entry.js.map +1 -0
  70. package/dist/first-party/hooks.d.ts +66 -0
  71. package/dist/first-party/hooks.js +19 -0
  72. package/dist/first-party/hooks.js.map +1 -0
  73. package/dist/first-party/http.d.ts +191 -0
  74. package/dist/first-party/http.js +364 -0
  75. package/dist/first-party/http.js.map +1 -0
  76. package/dist/first-party/index.d.ts +14 -0
  77. package/dist/first-party/index.js +3 -0
  78. package/dist/first-party/index.js.map +1 -0
  79. package/dist/first-party/landed.d.ts +32 -0
  80. package/dist/first-party/landed.js +50 -0
  81. package/dist/first-party/landed.js.map +1 -0
  82. package/dist/first-party/landedProof.d.ts +12 -0
  83. package/dist/first-party/landedProof.js +20 -0
  84. package/dist/first-party/landedProof.js.map +1 -0
  85. package/dist/first-party/linkToken.d.ts +16 -0
  86. package/dist/first-party/linkToken.js +29 -0
  87. package/dist/first-party/linkToken.js.map +1 -0
  88. package/dist/first-party/links.d.ts +100 -0
  89. package/dist/first-party/links.js +84 -0
  90. package/dist/first-party/links.js.map +1 -0
  91. package/dist/first-party/locks.d.ts +36 -0
  92. package/dist/first-party/locks.js +64 -0
  93. package/dist/first-party/locks.js.map +1 -0
  94. package/dist/first-party/personTransport.d.ts +38 -0
  95. package/dist/first-party/personTransport.js +177 -0
  96. package/dist/first-party/personTransport.js.map +1 -0
  97. package/dist/first-party/proxy/app.d.ts +25 -0
  98. package/dist/first-party/proxy/app.js +94 -0
  99. package/dist/first-party/proxy/app.js.map +1 -0
  100. package/dist/first-party/proxy/bound.d.ts +5 -0
  101. package/dist/first-party/proxy/bound.js +13 -0
  102. package/dist/first-party/proxy/bound.js.map +1 -0
  103. package/dist/first-party/proxy/cases.d.ts +54 -0
  104. package/dist/first-party/proxy/cases.js +412 -0
  105. package/dist/first-party/proxy/cases.js.map +1 -0
  106. package/dist/first-party/proxy/decide.d.ts +29 -0
  107. package/dist/first-party/proxy/decide.js +140 -0
  108. package/dist/first-party/proxy/decide.js.map +1 -0
  109. package/dist/first-party/proxy/freeze.d.ts +2 -0
  110. package/dist/first-party/proxy/freeze.js +10 -0
  111. package/dist/first-party/proxy/freeze.js.map +1 -0
  112. package/dist/first-party/proxy/handler.d.ts +23 -0
  113. package/dist/first-party/proxy/handler.js +190 -0
  114. package/dist/first-party/proxy/handler.js.map +1 -0
  115. package/dist/first-party/proxy/headers.d.ts +8 -0
  116. package/dist/first-party/proxy/headers.js +20 -0
  117. package/dist/first-party/proxy/headers.js.map +1 -0
  118. package/dist/first-party/proxy/hop.d.ts +9 -0
  119. package/dist/first-party/proxy/hop.js +37 -0
  120. package/dist/first-party/proxy/hop.js.map +1 -0
  121. package/dist/first-party/proxy/host.d.ts +5 -0
  122. package/dist/first-party/proxy/host.js +11 -0
  123. package/dist/first-party/proxy/host.js.map +1 -0
  124. package/dist/first-party/proxy/index.d.ts +7 -0
  125. package/dist/first-party/proxy/index.js +6 -0
  126. package/dist/first-party/proxy/index.js.map +1 -0
  127. package/dist/first-party/proxy/pass-back.d.ts +21 -0
  128. package/dist/first-party/proxy/pass-back.js +51 -0
  129. package/dist/first-party/proxy/pass-back.js.map +1 -0
  130. package/dist/first-party/proxy/paths.d.ts +17 -0
  131. package/dist/first-party/proxy/paths.js +36 -0
  132. package/dist/first-party/proxy/paths.js.map +1 -0
  133. package/dist/first-party/proxy/shown.d.ts +5 -0
  134. package/dist/first-party/proxy/shown.js +16 -0
  135. package/dist/first-party/proxy/shown.js.map +1 -0
  136. package/dist/first-party/proxy/stand-in.d.ts +47 -0
  137. package/dist/first-party/proxy/stand-in.js +318 -0
  138. package/dist/first-party/proxy/stand-in.js.map +1 -0
  139. package/dist/first-party/report.d.ts +47 -0
  140. package/dist/first-party/report.js +38 -0
  141. package/dist/first-party/report.js.map +1 -0
  142. package/dist/first-party/result.d.ts +82 -0
  143. package/dist/first-party/result.js +40 -0
  144. package/dist/first-party/result.js.map +1 -0
  145. package/dist/first-party/session/cell.d.ts +177 -0
  146. package/dist/first-party/session/cell.js +89 -0
  147. package/dist/first-party/session/cell.js.map +1 -0
  148. package/dist/first-party/session/closed.d.ts +8 -0
  149. package/dist/first-party/session/closed.js +12 -0
  150. package/dist/first-party/session/closed.js.map +1 -0
  151. package/dist/first-party/session/index.d.ts +3 -0
  152. package/dist/first-party/session/index.js +3 -0
  153. package/dist/first-party/session/index.js.map +1 -0
  154. package/dist/first-party/session/logout.d.ts +82 -0
  155. package/dist/first-party/session/logout.js +328 -0
  156. package/dist/first-party/session/logout.js.map +1 -0
  157. package/dist/first-party/session/owed.d.ts +30 -0
  158. package/dist/first-party/session/owed.js +27 -0
  159. package/dist/first-party/session/owed.js.map +1 -0
  160. package/dist/first-party/session/refresh.d.ts +19 -0
  161. package/dist/first-party/session/refresh.js +229 -0
  162. package/dist/first-party/session/refresh.js.map +1 -0
  163. package/dist/first-party/session/results.d.ts +38 -0
  164. package/dist/first-party/session/results.js +74 -0
  165. package/dist/first-party/session/results.js.map +1 -0
  166. package/dist/first-party/session/session.d.ts +2 -0
  167. package/dist/first-party/session/session.js +379 -0
  168. package/dist/first-party/session/session.js.map +1 -0
  169. package/dist/first-party/session/shared.d.ts +34 -0
  170. package/dist/first-party/session/shared.js +104 -0
  171. package/dist/first-party/session/shared.js.map +1 -0
  172. package/dist/first-party/session/sharedSignal.d.ts +27 -0
  173. package/dist/first-party/session/sharedSignal.js +55 -0
  174. package/dist/first-party/session/sharedSignal.js.map +1 -0
  175. package/dist/first-party/session/signIn.d.ts +43 -0
  176. package/dist/first-party/session/signIn.js +294 -0
  177. package/dist/first-party/session/signIn.js.map +1 -0
  178. package/dist/first-party/session/signInThrough.d.ts +5 -0
  179. package/dist/first-party/session/signInThrough.js +95 -0
  180. package/dist/first-party/session/signInThrough.js.map +1 -0
  181. package/dist/first-party/session/sync.d.ts +17 -0
  182. package/dist/first-party/session/sync.js +143 -0
  183. package/dist/first-party/session/sync.js.map +1 -0
  184. package/dist/first-party/session/tokens.d.ts +18 -0
  185. package/dist/first-party/session/tokens.js +64 -0
  186. package/dist/first-party/session/tokens.js.map +1 -0
  187. package/dist/first-party/session/types.d.ts +270 -0
  188. package/dist/first-party/session/types.js +3 -0
  189. package/dist/first-party/session/types.js.map +1 -0
  190. package/dist/first-party/sessionWire.d.ts +31 -0
  191. package/dist/first-party/sessionWire.js +91 -0
  192. package/dist/first-party/sessionWire.js.map +1 -0
  193. package/dist/first-party/signInHooks.d.ts +57 -0
  194. package/dist/first-party/signInHooks.js +2 -0
  195. package/dist/first-party/signInHooks.js.map +1 -0
  196. package/dist/first-party/state.d.ts +51 -0
  197. package/dist/first-party/state.js +52 -0
  198. package/dist/first-party/state.js.map +1 -0
  199. package/dist/first-party/strip.d.ts +1 -0
  200. package/dist/first-party/strip.js +8 -0
  201. package/dist/first-party/strip.js.map +1 -0
  202. package/dist/first-party/twoStepLock.d.ts +26 -0
  203. package/dist/first-party/twoStepLock.js +58 -0
  204. package/dist/first-party/twoStepLock.js.map +1 -0
  205. package/dist/first-party/twoStepSession.d.ts +21 -0
  206. package/dist/first-party/twoStepSession.js +31 -0
  207. package/dist/first-party/twoStepSession.js.map +1 -0
  208. package/dist/first-party/wire.d.ts +63 -0
  209. package/dist/first-party/wire.js +5 -0
  210. package/dist/first-party/wire.js.map +1 -0
  211. package/dist/flipstream.d.ts +51 -9
  212. package/dist/flipstream.js +58 -22
  213. package/dist/flipstream.js.map +1 -1
  214. package/dist/node/session.js +3 -1
  215. package/dist/node/session.js.map +1 -1
  216. package/dist/version.gen.js +1 -1
  217. package/docs/first-party.md +278 -0
  218. package/package.json +34 -5
@@ -0,0 +1,52 @@
1
+ import { z } from 'zod';
2
+ /**
3
+ * Which session is current in this browser, shared by its tabs: the authority. The
4
+ * BroadcastChannel only makes changes quick (docs/architecture.md 2.3). It never holds a
5
+ * token.
6
+ */
7
+ export const STATE_KEY = 'flipstream-auth:state';
8
+ /** No record at all: a new browser, or Local Storage cleared. Never "signed out" by itself: the cookie decides (§2.3). */
9
+ export const NO_RECORD = {
10
+ generation: 0,
11
+ signedIn: false,
12
+ serial: 0,
13
+ logoutPending: null,
14
+ principal: null,
15
+ person: null,
16
+ };
17
+ const Generation = z.int().min(0);
18
+ const StateSchema = z.object({
19
+ generation: Generation,
20
+ signedIn: z.boolean(),
21
+ serial: Generation,
22
+ logoutPending: z.object({ generation: Generation }).nullable(),
23
+ // A record from before the fingerprint has none: nothing to compare, nothing assumed.
24
+ principal: z.string().nullable().default(null),
25
+ person: z.string().nullable().default(null),
26
+ });
27
+ /** The record in storage, or NO_RECORD when there is none or it can't be read. */
28
+ export function readState(storage) {
29
+ const raw = storage.getItem(STATE_KEY);
30
+ if (raw === null)
31
+ return NO_RECORD;
32
+ let json;
33
+ try {
34
+ json = JSON.parse(raw);
35
+ }
36
+ catch {
37
+ return NO_RECORD;
38
+ }
39
+ const parsed = StateSchema.safeParse(json);
40
+ return parsed.success ? parsed.data : NO_RECORD;
41
+ }
42
+ /** Whether the browser took the write. */
43
+ export function writeState(storage, state) {
44
+ return storage.setItem(STATE_KEY, JSON.stringify(state));
45
+ }
46
+ /** The newer of two states: the higher generation, then the higher serial. */
47
+ export function newer(a, b) {
48
+ if (a.generation !== b.generation)
49
+ return a.generation > b.generation ? a : b;
50
+ return a.serial >= b.serial ? a : b;
51
+ }
52
+ //# sourceMappingURL=state.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"state.js","sourceRoot":"","sources":["../../src/first-party/state.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAGvB;;;;GAIG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,uBAAuB,CAAA;AAqChD,0HAA0H;AAC1H,MAAM,CAAC,MAAM,SAAS,GAAc;IAClC,UAAU,EAAE,CAAC;IACb,QAAQ,EAAE,KAAK;IACf,MAAM,EAAE,CAAC;IACT,aAAa,EAAE,IAAI;IACnB,SAAS,EAAE,IAAI;IACf,MAAM,EAAE,IAAI;CACb,CAAA;AAED,MAAM,UAAU,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAA;AAEjC,MAAM,WAAW,GAAG,CAAC,CAAC,MAAM,CAAC;IAC3B,UAAU,EAAE,UAAU;IACtB,QAAQ,EAAE,CAAC,CAAC,OAAO,EAAE;IACrB,MAAM,EAAE,UAAU;IAClB,aAAa,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,UAAU,EAAE,UAAU,EAAE,CAAC,CAAC,QAAQ,EAAE;IAC9D,sFAAsF;IACtF,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC;IAC9C,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC;CAC5C,CAAC,CAAA;AAEF,kFAAkF;AAClF,MAAM,UAAU,SAAS,CAAC,OAAoB;IAC5C,MAAM,GAAG,GAAG,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,CAAA;IACtC,IAAI,GAAG,KAAK,IAAI;QAAE,OAAO,SAAS,CAAA;IAClC,IAAI,IAAa,CAAA;IACjB,IAAI,CAAC;QACH,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IACxB,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,SAAS,CAAA;IAClB,CAAC;IACD,MAAM,MAAM,GAAG,WAAW,CAAC,SAAS,CAAC,IAAI,CAAC,CAAA;IAC1C,OAAO,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAA;AACjD,CAAC;AAED,0CAA0C;AAC1C,MAAM,UAAU,UAAU,CAAC,OAAoB,EAAE,KAAgB;IAC/D,OAAO,OAAO,CAAC,OAAO,CAAC,SAAS,EAAE,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAA;AAC1D,CAAC;AAED,8EAA8E;AAC9E,MAAM,UAAU,KAAK,CAAC,CAAY,EAAE,CAAY;IAC9C,IAAI,CAAC,CAAC,UAAU,KAAK,CAAC,CAAC,UAAU;QAAE,OAAO,CAAC,CAAC,UAAU,GAAG,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;IAC7E,OAAO,CAAC,CAAC,MAAM,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;AACrC,CAAC"}
@@ -0,0 +1 @@
1
+ export { strip } from './linkToken.js';
@@ -0,0 +1,8 @@
1
+ // @flipstream/sdk/first-party/link-token (#465): what the app's boot module calls, before anything
2
+ // else is evaluated: `const cleaned = strip(location.href)`, then
3
+ // `if (cleaned !== null) history.replaceState(history.state, '', cleaned)`, which keeps the entry's
4
+ // state. The boot module holds only that, and the app's entry module imports it first.
5
+ // The same module the session reads (linkToken.ts), so the token it keeps is the one the session
6
+ // takes. Nothing else: no session, no network.
7
+ export { strip } from './linkToken.js';
8
+ //# sourceMappingURL=strip.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"strip.js","sourceRoot":"","sources":["../../src/first-party/strip.ts"],"names":[],"mappings":"AAAA,mGAAmG;AACnG,kEAAkE;AAClE,oGAAoG;AACpG,uFAAuF;AACvF,iGAAiG;AACjG,+CAA+C;AAC/C,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAA"}
@@ -0,0 +1,26 @@
1
+ import type { StorageLike } from './browser.js';
2
+ /** The address the code screen's link was asked for, from this browser's attempt. */
3
+ export declare function attemptEmail(storage: StorageLike, attemptId: string, now: number): string | null;
4
+ /** The kept end for `email` while it is still ahead of `now`: null when there is none ahead. */
5
+ export declare function keptLockEnd(storage: StorageLike, email: string, now: number): number | null;
6
+ /**
7
+ * Keeps `end` for `email`, and drops the ends already past. A write the browser refuses keeps
8
+ * nothing: the next lock then counts its own five minutes, as it did before.
9
+ */
10
+ export declare function keepLockEnd(storage: StorageLike, email: string, end: number, now: number): void;
11
+ /**
12
+ * Drops the kept end for `email`: pulse-auth counted a code as wrong, or took one, so its lock
13
+ * is over, and a later lock is a new one with its own five minutes. The kept end is only the
14
+ * latest the old lock could end, up to five minutes past the real end.
15
+ */
16
+ export declare function forgetLockEnd(storage: StorageLike, email: string): void;
17
+ /**
18
+ * How long pulse-auth refuses codes after the fifth wrong one in a row (`mfa.py`, LOCKOUT).
19
+ * Its answer says neither when the lock began nor when it ends, and the fifth wrong code is
20
+ * still answered as a wrong code: counted from the answer that says "locked", the end is
21
+ * the latest it can be. Up to the whole minute, as people read it: the minute shown is never
22
+ * before the lock ends.
23
+ */
24
+ export declare const TWO_STEP_LOCKOUT_MS: number;
25
+ export declare const MINUTE_MS = 60000;
26
+ export declare function firstLockEnd(answeredAt: number): number;
@@ -0,0 +1,58 @@
1
+ import { findAttempt } from './attempts.js';
2
+ import { normalizeEmail } from './claims.js';
3
+ /*
4
+ * The end of a two-step lock this browser was told about, per address (Michel, 17 Oct).
5
+ * pulse-auth's lock isn't moved by a code tried during it (`mfa.py`: `_check_lock` refuses
6
+ * before anything is counted), so an end still ahead stays the end, and a new link must not
7
+ * start the five minutes over. An address and a time only: never a token or a code.
8
+ */
9
+ const PREFIX = 'flipstream-auth:two-step-lock:';
10
+ const END = /^\d{13}$/;
11
+ /** The address the code screen's link was asked for, from this browser's attempt. */
12
+ export function attemptEmail(storage, attemptId, now) {
13
+ const lookup = findAttempt(storage, attemptId, now);
14
+ return lookup.kind === 'missing' ? null : normalizeEmail(lookup.attempt.email);
15
+ }
16
+ /** The kept end for `email` while it is still ahead of `now`: null when there is none ahead. */
17
+ export function keptLockEnd(storage, email, now) {
18
+ const end = endOf(storage.getItem(PREFIX + email));
19
+ return end !== null && end > now ? end : null;
20
+ }
21
+ /**
22
+ * Keeps `end` for `email`, and drops the ends already past. A write the browser refuses keeps
23
+ * nothing: the next lock then counts its own five minutes, as it did before.
24
+ */
25
+ export function keepLockEnd(storage, email, end, now) {
26
+ for (const key of storage.keys()) {
27
+ if (!key.startsWith(PREFIX))
28
+ continue;
29
+ const kept = endOf(storage.getItem(key));
30
+ if (kept === null || kept <= now)
31
+ storage.removeItem(key);
32
+ }
33
+ storage.setItem(PREFIX + email, String(end));
34
+ }
35
+ /**
36
+ * Drops the kept end for `email`: pulse-auth counted a code as wrong, or took one, so its lock
37
+ * is over, and a later lock is a new one with its own five minutes. The kept end is only the
38
+ * latest the old lock could end, up to five minutes past the real end.
39
+ */
40
+ export function forgetLockEnd(storage, email) {
41
+ storage.removeItem(PREFIX + email);
42
+ }
43
+ function endOf(value) {
44
+ return value !== null && END.test(value) ? Number(value) : null;
45
+ }
46
+ /**
47
+ * How long pulse-auth refuses codes after the fifth wrong one in a row (`mfa.py`, LOCKOUT).
48
+ * Its answer says neither when the lock began nor when it ends, and the fifth wrong code is
49
+ * still answered as a wrong code: counted from the answer that says "locked", the end is
50
+ * the latest it can be. Up to the whole minute, as people read it: the minute shown is never
51
+ * before the lock ends.
52
+ */
53
+ export const TWO_STEP_LOCKOUT_MS = 5 * 60_000;
54
+ export const MINUTE_MS = 60_000;
55
+ export function firstLockEnd(answeredAt) {
56
+ return Math.ceil((answeredAt + TWO_STEP_LOCKOUT_MS) / MINUTE_MS) * MINUTE_MS;
57
+ }
58
+ //# sourceMappingURL=twoStepLock.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"twoStepLock.js","sourceRoot":"","sources":["../../src/first-party/twoStepLock.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAE3C,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAE5C;;;;;GAKG;AAEH,MAAM,MAAM,GAAG,gCAAgC,CAAA;AAC/C,MAAM,GAAG,GAAG,UAAU,CAAA;AAEtB,qFAAqF;AACrF,MAAM,UAAU,YAAY,CAC1B,OAAoB,EACpB,SAAiB,EACjB,GAAW;IAEX,MAAM,MAAM,GAAG,WAAW,CAAC,OAAO,EAAE,SAAS,EAAE,GAAG,CAAC,CAAA;IACnD,OAAO,MAAM,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,cAAc,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;AAChF,CAAC;AAED,gGAAgG;AAChG,MAAM,UAAU,WAAW,CACzB,OAAoB,EACpB,KAAa,EACb,GAAW;IAEX,MAAM,GAAG,GAAG,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,GAAG,KAAK,CAAC,CAAC,CAAA;IAClD,OAAO,GAAG,KAAK,IAAI,IAAI,GAAG,GAAG,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAA;AAC/C,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,WAAW,CACzB,OAAoB,EACpB,KAAa,EACb,GAAW,EACX,GAAW;IAEX,KAAK,MAAM,GAAG,IAAI,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC;QACjC,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC;YAAE,SAAQ;QACrC,MAAM,IAAI,GAAG,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAA;QACxC,IAAI,IAAI,KAAK,IAAI,IAAI,IAAI,IAAI,GAAG;YAAE,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,CAAA;IAC3D,CAAC;IACD,OAAO,CAAC,OAAO,CAAC,MAAM,GAAG,KAAK,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC,CAAA;AAC9C,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,aAAa,CAAC,OAAoB,EAAE,KAAa;IAC/D,OAAO,CAAC,UAAU,CAAC,MAAM,GAAG,KAAK,CAAC,CAAA;AACpC,CAAC;AAED,SAAS,KAAK,CAAC,KAAoB;IACjC,OAAO,KAAK,KAAK,IAAI,IAAI,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAA;AACjE,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,GAAG,MAAM,CAAA;AAC7C,MAAM,CAAC,MAAM,SAAS,GAAG,MAAM,CAAA;AAE/B,MAAM,UAAU,YAAY,CAAC,UAAkB;IAC7C,OAAO,IAAI,CAAC,IAAI,CAAC,CAAC,UAAU,GAAG,mBAAmB,CAAC,GAAG,SAAS,CAAC,GAAG,SAAS,CAAA;AAC9E,CAAC"}
@@ -0,0 +1,21 @@
1
+ import type { StorageLike } from './browser.js';
2
+ import type { AuthSession } from './session/types.js';
3
+ import type { Wire } from './wire.js';
4
+ type Session = Pick<AuthSession, 'current' | 'isCurrent' | 'signInSetupChallenge' | 'canDeliverCodes'>;
5
+ export declare function setupHooks(session: Session, storage: StorageLike | null): {
6
+ setupRevision(): number | null;
7
+ signInSetupChallenge: (setup: import("./hooks.js").SignInSetup) => {
8
+ readonly kind: "pending";
9
+ readonly challenge: string;
10
+ } | {
11
+ readonly kind: "expired" | "superseded";
12
+ };
13
+ codeRecipient(): () => boolean;
14
+ addressLock(): {
15
+ keptLockEnd: (now: number) => number | null;
16
+ keepLockEnd: (end: number, now: number) => void;
17
+ forgetLockEnd: () => void;
18
+ } | null;
19
+ };
20
+ export declare function twoStepForSession(wire: Wire, session: Session, storage: StorageLike | null): import("./calls/twoStepSetup.js").TwoStepSetupCalls;
21
+ export {};
@@ -0,0 +1,31 @@
1
+ import { createTwoStepSetupCalls } from './calls/twoStepSetup.js';
2
+ import { normalizeEmail } from './claims.js';
3
+ import { keptLockEnd, keepLockEnd, forgetLockEnd } from './twoStepLock.js';
4
+ export function setupHooks(session, storage) {
5
+ return {
6
+ setupRevision() {
7
+ const current = session.current();
8
+ return current.status === 'signed-in' && session.isCurrent(current.revision) ? current.revision : null;
9
+ },
10
+ signInSetupChallenge: session.signInSetupChallenge,
11
+ codeRecipient() {
12
+ const pressed = session.current();
13
+ return () => pressed.status === 'signed-in' && session.canDeliverCodes(pressed.principal.userId);
14
+ },
15
+ addressLock() {
16
+ const current = session.current();
17
+ if (storage === null || current.status !== 'signed-in' || !session.isCurrent(current.revision))
18
+ return null;
19
+ const email = normalizeEmail(current.principal.email);
20
+ return {
21
+ keptLockEnd: (now) => keptLockEnd(storage, email, now),
22
+ keepLockEnd: (end, now) => keepLockEnd(storage, email, end, now),
23
+ forgetLockEnd: () => forgetLockEnd(storage, email),
24
+ };
25
+ },
26
+ };
27
+ }
28
+ export function twoStepForSession(wire, session, storage) {
29
+ return createTwoStepSetupCalls(wire, setupHooks(session, storage));
30
+ }
31
+ //# sourceMappingURL=twoStepSession.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"twoStepSession.js","sourceRoot":"","sources":["../../src/first-party/twoStepSession.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,uBAAuB,EAAE,MAAM,yBAAyB,CAAA;AACjE,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAE5C,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA;AAK1E,MAAM,UAAU,UAAU,CAAC,OAAgB,EAAE,OAA2B;IACtE,OAAO;QACL,aAAa;YACX,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,EAAE,CAAA;YACjC,OAAO,OAAO,CAAC,MAAM,KAAK,WAAW,IAAI,OAAO,CAAC,SAAS,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAA;QACxG,CAAC;QACD,oBAAoB,EAAE,OAAO,CAAC,oBAAoB;QAClD,aAAa;YACX,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,EAAE,CAAA;YACjC,OAAO,GAAG,EAAE,CAAC,OAAO,CAAC,MAAM,KAAK,WAAW,IAAI,OAAO,CAAC,eAAe,CAAC,OAAO,CAAC,SAAS,CAAC,MAAM,CAAC,CAAA;QAClG,CAAC;QACD,WAAW;YACT,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,EAAE,CAAA;YACjC,IAAI,OAAO,KAAK,IAAI,IAAI,OAAO,CAAC,MAAM,KAAK,WAAW,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,OAAO,CAAC,QAAQ,CAAC;gBAAE,OAAO,IAAI,CAAA;YAC3G,MAAM,KAAK,GAAG,cAAc,CAAC,OAAO,CAAC,SAAS,CAAC,KAAK,CAAC,CAAA;YACrD,OAAO;gBACL,WAAW,EAAE,CAAC,GAAW,EAAE,EAAE,CAAC,WAAW,CAAC,OAAO,EAAE,KAAK,EAAE,GAAG,CAAC;gBAC9D,WAAW,EAAE,CAAC,GAAW,EAAE,GAAW,EAAE,EAAE,CAAC,WAAW,CAAC,OAAO,EAAE,KAAK,EAAE,GAAG,EAAE,GAAG,CAAC;gBAChF,aAAa,EAAE,GAAG,EAAE,CAAC,aAAa,CAAC,OAAO,EAAE,KAAK,CAAC;aACnD,CAAA;QACH,CAAC;KACF,CAAA;AACH,CAAC;AAED,MAAM,UAAU,iBAAiB,CAAC,IAAU,EAAE,OAAgB,EAAE,OAA2B;IACzF,OAAO,uBAAuB,CAAC,IAAI,EAAE,UAAU,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,CAAA;AACpE,CAAC"}
@@ -0,0 +1,63 @@
1
+ import type { CallOptions } from './result.js';
2
+ export interface WireRequest {
3
+ readonly method: 'GET' | 'POST';
4
+ /** The server's own path, starting with '/': '/oauth/device/verify'. Never a full URL. */
5
+ readonly path: string;
6
+ /** Query pairs. The wire encodes them. */
7
+ readonly query?: Readonly<Record<string, string>>;
8
+ /** Sent as JSON. A POST without one sends `{}`, as the session's own calls do. */
9
+ readonly body?: Readonly<Record<string, unknown>>;
10
+ }
11
+ /** A server answered. `body`: the parsed JSON, or `undefined` when the answer isn't JSON. */
12
+ export interface WireAnswer {
13
+ readonly kind: 'answer';
14
+ readonly status: number;
15
+ readonly body: unknown;
16
+ }
17
+ /** No answer to read. */
18
+ export type WireStop =
19
+ /** Nothing came back in time: offline, a failed connection, the call's own time limit. */
20
+ {
21
+ readonly kind: 'unavailable';
22
+ } | {
23
+ readonly kind: 'aborted';
24
+ };
25
+ /**
26
+ * Every request the wire sends says `Accept: application/json`, and a body goes as
27
+ * `Content-Type: application/json`. So a family never sets a header.
28
+ *
29
+ * There is no way through the app's /auth proxy here (the lead, 2 Dec, on #465): every path it forwards
30
+ * sets or reads the session's cookie and answers a token the session must adopt, so a family
31
+ * goes through it only by the session's `signInThrough` (part 3), never raw.
32
+ */
33
+ export interface Wire {
34
+ /** Straight to a host, with no cookie and no token (`credentials: 'omit'`). */
35
+ readonly open: (host: 'auth' | 'admin', request: WireRequest, options: CallOptions | undefined) => Promise<WireAnswer | WireStop>;
36
+ /**
37
+ * Straight to pulse-auth as the signed-in person. Nobody signed in: `signed_out`, with nothing
38
+ * sent. A 401 renews the token once and sends again; refused again: `signed_out`.
39
+ */
40
+ readonly asPerson: (request: WireRequest, options: CallOptions | undefined,
41
+ /** Enrollment observes its dispatch. Calls issuing backup codes keep the sent answer across a caller abort. */
42
+ dispatch?: {
43
+ readonly onSend: () => void;
44
+ /** An anonymous approval can be a checked 200. Renew it once like a 401. */
45
+ readonly isUnauthorized?: (answer: WireAnswer) => boolean;
46
+ } | 'keep_sent_answer',
47
+ /** For successful keep_sent_answer responses only, including after the body has been read. */
48
+ canDeliver?: () => boolean) => Promise<WireAnswer | WireStop | {
49
+ readonly kind: 'signed_out';
50
+ }>;
51
+ /**
52
+ * An answer outside the contract: the path and the status, never a body. The family calls this
53
+ * once per call. `open` does not report a redirect (SDK-D1962).
54
+ */
55
+ readonly report: (answer: {
56
+ readonly path: string;
57
+ readonly status: number;
58
+ }) => void;
59
+ /** The clock, epoch ms. A family never reads `Date`: the lint refuses it. */
60
+ readonly now: () => number;
61
+ }
62
+ /** A 5xx, or a 429. A family asks this only after its own rules for that status. */
63
+ export declare function isUnavailable(answer: WireAnswer): boolean;
@@ -0,0 +1,5 @@
1
+ /** A 5xx, or a 429. A family asks this only after its own rules for that status. */
2
+ export function isUnavailable(answer) {
3
+ return answer.status === 429 || answer.status >= 500;
4
+ }
5
+ //# sourceMappingURL=wire.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wire.js","sourceRoot":"","sources":["../../src/first-party/wire.ts"],"names":[],"mappings":"AAoEA,oFAAoF;AACpF,MAAM,UAAU,aAAa,CAAC,MAAkB;IAC9C,OAAO,MAAM,CAAC,MAAM,KAAK,GAAG,IAAI,MAAM,CAAC,MAAM,IAAI,GAAG,CAAA;AACtD,CAAC"}
@@ -6,9 +6,12 @@ import { type Member } from './api/members.js';
6
6
  import type { Page, PageRequest } from './api/page.js';
7
7
  import { type Balance, type Ledger } from './api/wallet.js';
8
8
  import { type Workspace } from './api/workspaces.js';
9
+ import { type SharedReads } from './core/shared.js';
9
10
  import { type TokenAsk } from './core/fetch.js';
11
+ import { DEFAULT_HOSTS } from './core/hosts.js';
12
+ import { type Trace } from './core/identify.js';
10
13
  import type { ApiResult } from './core/result.js';
11
- import type { ServiceName, Transport } from './core/transport.js';
14
+ import type { ApiRequest, ServiceName, Transport } from './core/transport.js';
12
15
  import { type Health } from './query/health.js';
13
16
  import { type QueryRequest } from './query/query.js';
14
17
  import type { Catalog, CatalogSource, QueryResponse } from './spec/query-planner/types.gen.js';
@@ -593,17 +596,54 @@ export interface Flipstream {
593
596
  }
594
597
  /** The client inside `withScope`: every call in its scope, and no scope of its own (#376). */
595
598
  export type ScopedFlipstream = Omit<Flipstream, 'scope' | 'withScope'>;
596
- /** Flipstream's deployed hosts: a client's defaults, and what `createFlipstreamFor`'s `deployed` stands in for. */
597
- export declare const DEFAULT_HOSTS: {
598
- readonly query: "https://query.flipstream.io";
599
- readonly auth: "https://auth.flipstream.io";
600
- readonly admin: "https://pulse.admin.api.flipstream.io";
601
- readonly wallet: "https://pulse.wallet.api.flipstream.io";
602
- };
599
+ export { DEFAULT_HOSTS };
600
+ /**
601
+ * One SDK call's trace (#251): every request the call makes carries it, each with its own parent-id,
602
+ * and `sent` turns true once one of them goes out, so the call's result can name it.
603
+ */
604
+ interface CallTrace {
605
+ readonly trace: Trace;
606
+ sent: boolean;
607
+ /** True once the call has answered: a request built after that is never looked for. */
608
+ ended: boolean;
609
+ /** The traceparent each of its requests carried, so they're forgotten when the call ends. */
610
+ readonly traceparents: string[];
611
+ /** The shared reads that answered it in a scope (#376), each under its own trace, in the order it asked. */
612
+ readonly shared: CallTrace[];
613
+ }
603
614
  /** The hosts a client takes as Flipstream's own deployment, whatever it was configured with. */
604
615
  export type DeployedHosts = {
605
616
  readonly [Service in keyof typeof DEFAULT_HOSTS]: string;
606
617
  };
618
+ /**
619
+ * A scope this client made (#376): its shared reads, and the trace each one goes out under
620
+ * (`readTrace`): the scope's, when it was given a valid traceparent (#409, D579), else one of its
621
+ * own, never a call's, since it serves several (#377's code review, 3).
622
+ */
623
+ interface ScopeState {
624
+ readonly owner: (() => boolean) | undefined;
625
+ readonly shared: SharedReads<CallTrace>;
626
+ readonly readTrace: () => CallTrace;
627
+ }
628
+ /** What the browser session contributes to a main client made for its displayed person. */
629
+ export interface PersonClientSession {
630
+ /** Freezes the client and method groups handed to a withScope callback. */
631
+ readonly freezeScoped: (client: ScopedFlipstream) => ScopedFlipstream;
632
+ /** All scopes created by clients of this session, so an older scope can be recognised. */
633
+ readonly scopes: WeakMap<Scope, ScopeState>;
634
+ /** The answer boundary checks that this client still represents the displayed person. */
635
+ readonly current: () => boolean;
636
+ /** Admission pins the displayed person and rejects a closed session in the call's name. */
637
+ readonly admit: (call: string) => void;
638
+ /** A health call already admitted uses this to avoid a new send after close. */
639
+ readonly isClosed: () => boolean;
640
+ /** The page's network edge, traced for bearer and tokenless requests alike. */
641
+ readonly fetch: typeof fetch;
642
+ /** The session reports a manual redirect on a tokenless health request. */
643
+ readonly healthResponse: (status: number) => void;
644
+ /** Health accepts loopback HTTP when the session's configured service hosts do. */
645
+ readonly allowLocalhostHttp?: boolean | undefined;
646
+ }
607
647
  /** The SDK, for one caller (D421): the console with its transport, or anyone with a token function. */
608
648
  export declare function createFlipstream(options: FlipstreamOptions): Flipstream;
609
649
  /**
@@ -612,4 +652,6 @@ export declare function createFlipstream(options: FlipstreamOptions): Flipstream
612
652
  * cases (scripts/surface.mjs) use it so a local server can stand in for a deployed host, as
613
653
  * `createFileLock` takes a test's timings (#378's code review, 1).
614
654
  */
615
- export declare function createFlipstreamFor(options: FlipstreamOptions, deployed: DeployedHosts): Flipstream;
655
+ export declare function createFlipstreamFor(options: FlipstreamOptions & {
656
+ readonly transport?: (request: ApiRequest, fetch?: typeof globalThis.fetch) => ReturnType<Transport>;
657
+ }, deployed: DeployedHosts, person?: PersonClientSession): Flipstream;
@@ -13,18 +13,12 @@ import { standsFor } from './core/inPlaceOf.js';
13
13
  import { withReads } from './core/scope.js';
14
14
  import { sharedReads } from './core/shared.js';
15
15
  import { fetchTransport, isLoopbackHttp, tokenlessTransport } from './core/fetch.js';
16
+ import { DEFAULT_HOSTS } from './core/hosts.js';
16
17
  import { callerSender, callerTrace, identifies, identityOf, isConversationId, runtimeOf, sdkSender, traceFor, traceparent, valueHolds } from './core/identify.js';
17
18
  import { catalogGet, catalogList, consoleWarn } from './query/catalog.js';
18
19
  import { healthCheck } from './query/health.js';
19
20
  import { preparedQuery } from './query/query.js';
20
- /** Flipstream's deployed hosts: a client's defaults, and what `createFlipstreamFor`'s `deployed` stands in for. */
21
- export const DEFAULT_HOSTS = {
22
- query: 'https://query.flipstream.io',
23
- auth: 'https://auth.flipstream.io',
24
- // Today's admin and wallet APIs, behind #67's seam (D541, D544).
25
- admin: 'https://pulse.admin.api.flipstream.io',
26
- wallet: 'https://pulse.wallet.api.flipstream.io',
27
- };
21
+ export { DEFAULT_HOSTS };
28
22
  /**
29
23
  * The console a hand-off names, with no trailing slash: the caller's own, checked here so a broken one
30
24
  * fails when the client is made, or Flipstream's own once it's live, beside its own admin host
@@ -81,7 +75,7 @@ export function createFlipstream(options) {
81
75
  * cases (scripts/surface.mjs) use it so a local server can stand in for a deployed host, as
82
76
  * `createFileLock` takes a test's timings (#378's code review, 1).
83
77
  */
84
- export function createFlipstreamFor(options, deployed) {
78
+ export function createFlipstreamFor(options, deployed, person) {
85
79
  // First, so a client that can't say who it is throws before anything else is set up.
86
80
  const identity = identityOf(options);
87
81
  const runtime = runtimeOf();
@@ -120,8 +114,12 @@ export function createFlipstreamFor(options, deployed) {
120
114
  owner.sent = true;
121
115
  return result;
122
116
  };
123
- const transport = options.transport !== undefined
124
- ? answeredThrough(options.transport)
117
+ // The internal bearer transport accepts the same traced fetch as health. An application's
118
+ // transport keeps its one-argument contract and its ordinary response-based trace handling.
119
+ const ownTransport = options.transport;
120
+ const pageFetch = person === undefined ? undefined : sentThrough(person.fetch);
121
+ const transport = ownTransport !== undefined
122
+ ? pageFetch === undefined ? answeredThrough(ownTransport) : request => ownTransport(request, pageFetch)
125
123
  : fetchTransport({ token: options.token, fetch: sentThrough(options.fetch), hosts, allowLocalhostHttp: options.allowLocalhostHttp, holdsToken });
126
124
  const base = baseOf(hosts.query ?? DEFAULT_HOSTS.query);
127
125
  const seam = backend({
@@ -133,7 +131,21 @@ export function createFlipstreamFor(options, deployed) {
133
131
  const warn = options.onWarning ?? consoleWarn;
134
132
  // For health alone (#111), whichever way the SDK was made: only the query host, so the call that
135
133
  // works signed out can go nowhere else.
136
- const signedOut = tokenlessTransport({ fetch: sentThrough(options.fetch), hosts: { query: base }, allowLocalhostHttp: options.allowLocalhostHttp });
134
+ const healthTransport = tokenlessTransport({
135
+ fetch: pageFetch ?? sentThrough(options.fetch),
136
+ hosts: { query: base },
137
+ allowLocalhostHttp: person?.allowLocalhostHttp ?? options.allowLocalhostHttp,
138
+ });
139
+ const signedOut = person === undefined ? healthTransport : async (request) => {
140
+ if (person.isClosed())
141
+ return { kind: 'cancelled', reason: 'aborted' };
142
+ if (!person.current())
143
+ return { kind: 'cancelled', reason: 'principal-changed' };
144
+ const answer = await healthTransport(request);
145
+ if (answer.kind === 'response' && !request.signal.aborted && person.current())
146
+ person.healthResponse(answer.response.status);
147
+ return answer;
148
+ };
137
149
  /**
138
150
  * `spec` with what its requests say about who sent them (`User-Agent` in Node, `Flipstream-Client`
139
151
  * in a browser, #196): the SDK call's one trace, and a parent-id for each request, retries and a
@@ -171,16 +183,32 @@ export function createFlipstreamFor(options, deployed) {
171
183
  * (withReads, #349): it answers, or rejects, with no read still being waited on.
172
184
  */
173
185
  const asOneCall = async (name, callOptions = {}, run) => {
186
+ person?.admit(name);
174
187
  const deadlineMs = callerWaitMs(`${name}: deadlineMs`, callOptions.deadlineMs, undefined);
175
188
  // Another client's scope would mix two identities' reads: a programming error, before anything (#376).
176
- if (callOptions.scope !== undefined && !scopes.has(callOptions.scope))
189
+ const state = callOptions.scope === undefined ? undefined : scopes.get(callOptions.scope);
190
+ if (callOptions.scope !== undefined && state === undefined)
177
191
  throw new TypeError(`${name}: scope must be one this client's scope() made`);
192
+ const earlierScope = state !== undefined && state.owner !== person?.current;
178
193
  const { deadlineMs: _given, ...rest } = callOptions;
179
194
  void _given; // read once, so the unused-variables check keeps the one it drops
180
195
  const options = deadlineMs === undefined ? rest : { ...rest, deadlineMs };
196
+ // An earlier person's scope may validate arguments but can neither send nor serve a warm
197
+ // read. Both read paths check the signal after validation and before doing either.
198
+ const runOptions = earlierScope ? { ...options, signal: AbortSignal.abort() } : options;
181
199
  const traced = newTrace(options.traceparent);
182
200
  try {
183
- const result = await withReads((scope) => run(traced, options, scope));
201
+ const answer = await withReads((scope) => run(traced, runOptions, scope));
202
+ // A warm scope can answer without a transport call. Check the first-party client's person
203
+ // here too, after the last body read and immediately before handing the result to the caller.
204
+ let result = answer;
205
+ if (person !== undefined && (earlierScope || !person.current())) {
206
+ state?.shared.dropAll();
207
+ const refusedInput = !answer.ok && answer.error.kind === 'refused';
208
+ const callerAborted = !answer.ok && answer.error.kind === 'aborted' && options.signal?.aborted === true;
209
+ if (!refusedInput && !callerAborted)
210
+ result = { ok: false, error: { kind: 'cancelled' } };
211
+ }
184
212
  // The request that answered it, so it can be found in Logs (#251): a shared read's when its
185
213
  // failure is the answer, else the call's own, else the first shared read it asked for (#376).
186
214
  // A failure answered in place of a shared read's (D578's not_found) names that read too
@@ -270,9 +298,10 @@ export function createFlipstreamFor(options, deployed) {
270
298
  // The shared read each failure a scope handed a call came from, so the call names its trace (#376).
271
299
  const sharedFailures = new WeakMap();
272
300
  // Each scope this client made, by its handle: a handle it didn't make is refused (asOneCall).
273
- const scopes = new WeakMap();
301
+ const scopes = person?.scopes ?? new WeakMap();
274
302
  // Its options read once, as it's made: a traceparent that isn't a valid one is dropped, as a call's is.
275
303
  const makeScope = (options) => {
304
+ person?.admit('flipstream.scope');
276
305
  const shared = sharedReads();
277
306
  const trace = callerTrace(options?.traceparent);
278
307
  const readTrace = trace === undefined ? () => newTrace() : () => callTrace(trace);
@@ -283,24 +312,28 @@ export function createFlipstreamFor(options, deployed) {
283
312
  const scope = { close, [Symbol.asyncDispose]: close };
284
313
  if (typeof Symbol.asyncDispose !== 'symbol')
285
314
  Reflect.deleteProperty(scope, 'undefined');
286
- scopes.set(scope, { shared, readTrace });
315
+ scopes.set(scope, { shared, readTrace, owner: person?.current });
287
316
  return scope;
288
317
  };
289
318
  /** Every call of the client, each in `bound` unless it takes a scope of its own (#376). */
290
319
  const clientOf = (bound) => {
291
320
  /** `reads` as one SDK call, under one deadline, one trace and one scope, sharing reads in its scope (#376). */
292
321
  const readsOf = (name, reads) => (callOptions = {}) => asOneCall(name, bound === undefined || callOptions.scope !== undefined ? callOptions : { ...callOptions, scope: bound }, (traced, options, scope) => reads(readsUnder(options, traced, scope, options.scope === undefined ? undefined : scopes.get(options.scope))));
322
+ const get = (request, callOptions = {}) => {
323
+ const spec = catalogGet(base, request.source, warn);
324
+ return spec === null ? Promise.resolve(refused('path')) : asOneCall('flipstream.catalog.get', callOptions, (traced, options) => send(spec, options, traced));
325
+ };
293
326
  return {
294
327
  catalog: {
295
328
  list: (callOptions = {}) => asOneCall('flipstream.catalog.list', callOptions, (traced, options) => send(catalogList(base, warn), options, traced)),
296
- get(request, callOptions = {}) {
297
- const spec = catalogGet(base, request.source, warn);
298
- // Refused before anything is sent: the token function isn't asked, and nothing is spent.
299
- return spec === null ? Promise.resolve(refused('path')) : asOneCall('flipstream.catalog.get', callOptions, (traced, options) => send(spec, options, traced));
329
+ get: person === undefined ? get : async (request, callOptions) => {
330
+ person.admit('flipstream.catalog.get');
331
+ return get(request, callOptions);
300
332
  },
301
333
  },
302
334
  // Async, so a request that isn't an object rejects, as D436's do, rather than throwing here.
303
335
  async query(request, callOptions = {}) {
336
+ person?.admit('flipstream.query');
304
337
  // Refused before anything is sent, like catalog.get's: the token function isn't asked.
305
338
  const query = preparedQuery(base, request);
306
339
  if ('refused' in query)
@@ -336,7 +369,8 @@ export function createFlipstreamFor(options, deployed) {
336
369
  delete: async (request, callOptions) => readsOf('flipstream.datasets.delete', (read) => handOffDatasetDelete(seam, read, request))(callOptions),
337
370
  },
338
371
  datasetTypes: {
339
- list: () => Promise.resolve(listDatasetTypes()),
372
+ list: () => person === undefined ? Promise.resolve(listDatasetTypes())
373
+ : asOneCall('flipstream.datasetTypes.list', bound === undefined ? {} : { scope: bound }, () => Promise.resolve(listDatasetTypes())),
340
374
  },
341
375
  credentials: {
342
376
  // Async, so a page that isn't one rejects, as D436's do, rather than throwing here.
@@ -360,11 +394,13 @@ export function createFlipstreamFor(options, deployed) {
360
394
  ...clientOf(undefined),
361
395
  scope: makeScope,
362
396
  async withScope(run, options) {
397
+ person?.admit('flipstream.withScope');
363
398
  if (typeof run !== 'function')
364
399
  throw new TypeError('withScope: run must be a function');
365
400
  const scope = makeScope(options);
366
401
  try {
367
- return await run(clientOf(scope));
402
+ const client = clientOf(scope);
403
+ return await run(person === undefined ? client : person.freezeScoped(client));
368
404
  }
369
405
  finally {
370
406
  await scope.close();