@opendatalabs/vana-sdk 4.4.0 → 4.5.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 (106) hide show
  1. package/README.md +176 -0
  2. package/dist/connect/account-origin.cjs +72 -0
  3. package/dist/connect/account-origin.cjs.map +1 -0
  4. package/dist/connect/account-origin.d.ts +92 -0
  5. package/dist/connect/account-origin.js +47 -0
  6. package/dist/connect/account-origin.js.map +1 -0
  7. package/dist/connect/account-origin.test.d.ts +1 -0
  8. package/dist/connect/chrome.cjs +284 -0
  9. package/dist/connect/chrome.cjs.map +1 -0
  10. package/dist/connect/chrome.d.ts +42 -0
  11. package/dist/connect/chrome.js +256 -0
  12. package/dist/connect/chrome.js.map +1 -0
  13. package/dist/connect/http-transports.cjs +100 -0
  14. package/dist/connect/http-transports.cjs.map +1 -0
  15. package/dist/connect/http-transports.d.ts +77 -0
  16. package/dist/connect/http-transports.js +75 -0
  17. package/dist/connect/http-transports.js.map +1 -0
  18. package/dist/connect/http-transports.test.d.ts +1 -0
  19. package/dist/connect/launcher.cjs +232 -0
  20. package/dist/connect/launcher.cjs.map +1 -0
  21. package/dist/connect/launcher.d.ts +129 -0
  22. package/dist/connect/launcher.js +220 -0
  23. package/dist/connect/launcher.js.map +1 -0
  24. package/dist/connect/launcher.test.d.ts +1 -0
  25. package/dist/connect/protocol.cjs +259 -0
  26. package/dist/connect/protocol.cjs.map +1 -0
  27. package/dist/connect/protocol.d.ts +331 -0
  28. package/dist/connect/protocol.js +215 -0
  29. package/dist/connect/protocol.js.map +1 -0
  30. package/dist/connect/protocol.test.d.ts +1 -0
  31. package/dist/connect.cjs +55 -0
  32. package/dist/connect.cjs.map +1 -0
  33. package/dist/connect.d.ts +35 -0
  34. package/dist/connect.js +34 -0
  35. package/dist/connect.js.map +1 -0
  36. package/dist/direct/connect-flow-sheet.test.d.ts +1 -0
  37. package/dist/direct/connect-flow.cjs +211 -1
  38. package/dist/direct/connect-flow.cjs.map +1 -1
  39. package/dist/direct/connect-flow.d.ts +75 -0
  40. package/dist/direct/connect-flow.js +213 -1
  41. package/dist/direct/connect-flow.js.map +1 -1
  42. package/dist/direct/connect-handler.cjs +493 -0
  43. package/dist/direct/connect-handler.cjs.map +1 -0
  44. package/dist/direct/connect-handler.d.ts +246 -0
  45. package/dist/direct/connect-handler.js +476 -0
  46. package/dist/direct/connect-handler.js.map +1 -0
  47. package/dist/direct/connect-handler.test.d.ts +1 -0
  48. package/dist/direct/controller.cjs +86 -17
  49. package/dist/direct/controller.cjs.map +1 -1
  50. package/dist/direct/controller.d.ts +101 -6
  51. package/dist/direct/controller.js +86 -16
  52. package/dist/direct/controller.js.map +1 -1
  53. package/dist/direct/endpoints.cjs +5 -2
  54. package/dist/direct/endpoints.cjs.map +1 -1
  55. package/dist/direct/endpoints.js +5 -2
  56. package/dist/direct/endpoints.js.map +1 -1
  57. package/dist/direct/request-authorization.cjs +65 -0
  58. package/dist/direct/request-authorization.cjs.map +1 -0
  59. package/dist/direct/request-authorization.d.ts +65 -0
  60. package/dist/direct/request-authorization.js +39 -0
  61. package/dist/direct/request-authorization.js.map +1 -0
  62. package/dist/direct/request-authorization.test.d.ts +1 -0
  63. package/dist/direct/types.cjs.map +1 -1
  64. package/dist/direct/types.d.ts +6 -0
  65. package/dist/direct/types.js.map +1 -1
  66. package/dist/direct/use-direct-vana-connect.cjs +18 -1
  67. package/dist/direct/use-direct-vana-connect.cjs.map +1 -1
  68. package/dist/direct/use-direct-vana-connect.js +18 -1
  69. package/dist/direct/use-direct-vana-connect.js.map +1 -1
  70. package/dist/direct/use-vana-connect.cjs +84 -0
  71. package/dist/direct/use-vana-connect.cjs.map +1 -0
  72. package/dist/direct/use-vana-connect.d.ts +59 -0
  73. package/dist/direct/use-vana-connect.js +64 -0
  74. package/dist/direct/use-vana-connect.js.map +1 -0
  75. package/dist/direct/use-vana-connect.test.d.ts +1 -0
  76. package/dist/index.browser.d.ts +1 -0
  77. package/dist/index.browser.js +104 -0
  78. package/dist/index.browser.js.map +4 -4
  79. package/dist/index.node.cjs +112 -0
  80. package/dist/index.node.cjs.map +3 -3
  81. package/dist/index.node.d.ts +1 -0
  82. package/dist/index.node.js +104 -0
  83. package/dist/index.node.js.map +4 -4
  84. package/dist/protocol/fixtures/pdpp-scope-keys.json +133 -0
  85. package/dist/protocol/pdpp-scopes.cjs +134 -0
  86. package/dist/protocol/pdpp-scopes.cjs.map +1 -0
  87. package/dist/protocol/pdpp-scopes.d.ts +123 -0
  88. package/dist/protocol/pdpp-scopes.js +103 -0
  89. package/dist/protocol/pdpp-scopes.js.map +1 -0
  90. package/dist/protocol/pdpp-scopes.test.d.ts +1 -0
  91. package/dist/protocol/scopes.cjs +17 -0
  92. package/dist/protocol/scopes.cjs.map +1 -1
  93. package/dist/protocol/scopes.d.ts +1 -0
  94. package/dist/protocol/scopes.js +18 -0
  95. package/dist/protocol/scopes.js.map +1 -1
  96. package/dist/react.cjs +10 -2
  97. package/dist/react.cjs.map +1 -1
  98. package/dist/react.d.ts +4 -1
  99. package/dist/react.js +11 -1
  100. package/dist/react.js.map +1 -1
  101. package/dist/server.cjs +61 -2
  102. package/dist/server.cjs.map +1 -1
  103. package/dist/server.d.ts +16 -4
  104. package/dist/server.js +67 -3
  105. package/dist/server.js.map +1 -1
  106. package/package.json +11 -1
@@ -13,6 +13,8 @@
13
13
  * @module direct/connect-flow
14
14
  */
15
15
  import type { AccessRequest, AccessRequestStatus, ApprovedDataResult } from "./types.js";
16
+ import { type ConnectEvent, type ConnectHandle, type ConnectOpenOptions } from "../connect/launcher.js";
17
+ import type { ConnectCompletion, ConnectProgressPhase, RequestAuthorization, SheetErrorCode } from "../connect/protocol.js";
16
18
  /**
17
19
  * Caller-supplied transports. These typically `fetch` the app's own backend
18
20
  * routes, which in turn delegate to a {@link DirectDataController}.
@@ -24,6 +26,64 @@ export interface DirectConnectTransports<T = unknown> {
24
26
  getStatus: (requestId: string) => Promise<AccessRequestStatus>;
25
27
  /** Ask the backend to read the approved data. */
26
28
  readResult: (requestId: string) => Promise<ApprovedDataResult<T>>;
29
+ /**
30
+ * Ask the backend to sign the sheet's short-lived read capability for a
31
+ * request it created for this browser (`controller.authorizeRequestRead`).
32
+ * Required for `presentation: "sheet"`; called each time the sheet asks.
33
+ */
34
+ authorizeRequest?: (requestId: string) => Promise<RequestAuthorization>;
35
+ }
36
+ /** How the person approves: a Vana tab (default) or Vana's in-page sheet. */
37
+ export type DirectConnectPresentation = "tab" | "sheet";
38
+ /** Sheet settings for `presentation: "sheet"`. */
39
+ export interface DirectConnectSheetOptions {
40
+ /** The embed client id Vana Account registered for this page's origin. */
41
+ clientId: string;
42
+ /** Label on the sheet chrome (Vana shows the registered name inside). */
43
+ appName: string;
44
+ /**
45
+ * Vana Account origin that serves the sheet. Required: there is no
46
+ * production default, so a dev app never silently talks to production.
47
+ * Use `VANA_ACCOUNT_ORIGINS[env]`, or `resolveVanaAccountOrigin` for
48
+ * preview deploys. `useVanaConnect` fills it from the backend's config.
49
+ */
50
+ accountOrigin: string;
51
+ /** Element the sheet mounts in. Defaults to `document.body`. */
52
+ container?: HTMLElement;
53
+ /** How long to wait for the sheet to start before falling back. */
54
+ readyTimeoutMs?: number;
55
+ /**
56
+ * Close the sheet this long (ms) after the data is read, so the person sees
57
+ * it finish. Defaults to 1500; `null` leaves it open for the person to close.
58
+ */
59
+ closeAfterDoneMs?: number | null;
60
+ /** Every raw launcher event, for logging or custom UI. */
61
+ onEvent?: (event: ConnectEvent) => void;
62
+ /** Launcher. Defaults to the SDK's `openConnectSheet`; injectable for tests. */
63
+ open?: (options: ConnectOpenOptions) => ConnectHandle;
64
+ }
65
+ /**
66
+ * What the sheet has reported, while it presents approval.
67
+ *
68
+ * @remarks
69
+ * These are hints for the UI, not outcomes. The flow settles only from the
70
+ * backend: `done` once the status poll says the request is readable and the
71
+ * read succeeds, `error` when the poll reports `denied`, `expired` or
72
+ * `completed`, or the read fails.
73
+ */
74
+ export interface DirectConnectSheetState {
75
+ /** `false` once the person closed the sheet after approving. */
76
+ open: boolean;
77
+ /**
78
+ * Set once the person approved: how the data will arrive. Approval is not
79
+ * delivery; wait for `state.type === "done"` before treating data as
80
+ * delivered.
81
+ */
82
+ completion?: ConnectCompletion;
83
+ /** Coarse `remote_collect` progress. */
84
+ progress?: ConnectProgressPhase;
85
+ /** A failure the sheet is showing; it stays open for the person to read. */
86
+ error?: SheetErrorCode;
27
87
  }
28
88
  /**
29
89
  * A handle to a tab opened synchronously under the user's click gesture.
@@ -73,6 +133,14 @@ export interface DirectConnectOptions {
73
133
  browserPlatformPolicy?: DirectBrowserPlatformPolicy;
74
134
  /** `setTimeout`. Injectable for tests. Defaults to `globalThis.setTimeout`. */
75
135
  setTimeoutFn?: (cb: () => void, ms: number) => unknown;
136
+ /**
137
+ * `"sheet"` presents approval in Vana's in-page sheet instead of a tab;
138
+ * needs {@link DirectConnectOptions.sheet} and the `authorizeRequest`
139
+ * transport. Defaults to `"tab"`.
140
+ */
141
+ presentation?: DirectConnectPresentation;
142
+ /** Sheet settings, or a loader for them (fetched at `start()`). */
143
+ sheet?: DirectConnectSheetOptions | (() => Promise<DirectConnectSheetOptions>);
76
144
  /** `clearTimeout`. Injectable for tests. Defaults to `globalThis.clearTimeout`. */
77
145
  clearTimeoutFn?: (handle: unknown) => void;
78
146
  /** Clock source in ms. Injectable for tests. Defaults to `Date.now`. */
@@ -103,6 +171,13 @@ export type DirectConnectState<T = unknown> = {
103
171
  * HTTPS `request.approvalUrl` as a manual "Open approval" link.
104
172
  */
105
173
  popupBlocked: boolean;
174
+ /** Present while Vana's sheet presents approval (`presentation: "sheet"`). */
175
+ sheet?: DirectConnectSheetState;
176
+ /**
177
+ * Set when the sheet could not run here and the flow fell back to the
178
+ * tab presentation (with `popupBlocked: true`).
179
+ */
180
+ sheetUnavailable?: SheetErrorCode;
106
181
  } | {
107
182
  type: "ready_to_open";
108
183
  request: AccessRequest;
@@ -1,4 +1,13 @@
1
1
  import { normalizeMobileContinuationUrl } from "./types.js";
2
+ import {
3
+ openConnectSheet
4
+ } from "../connect/launcher.js";
5
+ const SHEET_FALLBACK_CODES = [
6
+ "frame_blocked",
7
+ "origin_not_registered",
8
+ "incompatible_version"
9
+ ];
10
+ const DEFAULT_CLOSE_AFTER_DONE_MS = 1500;
2
11
  const DEFAULT_POLL_INTERVAL_MS = 1500;
3
12
  const DEFAULT_TIMEOUT_MS = 3e5;
4
13
  function toError(value) {
@@ -54,10 +63,18 @@ function createDirectConnectFlow(transports, options = {}) {
54
63
  let state = { type: "idle" };
55
64
  const listeners = /* @__PURE__ */ new Set();
56
65
  let pollHandle = null;
66
+ let pollTarget = null;
67
+ let pollInFlight = false;
68
+ let pollNudged = false;
57
69
  let running = false;
58
70
  let approvedRequest = null;
59
71
  let activeRunId = 0;
60
72
  let openedWindow = null;
73
+ let sheetHandle = null;
74
+ let sheetState = null;
75
+ let sheetUnavailable = null;
76
+ let sheetCloseAfterDoneMs = DEFAULT_CLOSE_AFTER_DONE_MS;
77
+ let sheetCloseTimer = null;
61
78
  function emit() {
62
79
  for (const listener of listeners) listener();
63
80
  }
@@ -70,6 +87,19 @@ function createDirectConnectFlow(transports, options = {}) {
70
87
  clearTimeoutFn(pollHandle);
71
88
  pollHandle = null;
72
89
  }
90
+ pollTarget = null;
91
+ pollNudged = false;
92
+ }
93
+ function nudgePoll() {
94
+ if (!running) return;
95
+ if (pollInFlight) {
96
+ pollNudged = true;
97
+ return;
98
+ }
99
+ const target = pollTarget;
100
+ if (pollHandle === null || !target) return;
101
+ clearPoll();
102
+ void poll(target.request, target.deadline);
73
103
  }
74
104
  function closeUnnavigatedWindow() {
75
105
  if (openedWindow) {
@@ -80,12 +110,114 @@ function createDirectConnectFlow(transports, options = {}) {
80
110
  function isRunningPhase() {
81
111
  return state.type === "creating" || state.type === "awaiting_approval" || state.type === "ready_to_open" || state.type === "reading";
82
112
  }
113
+ function closeSheet() {
114
+ const handle = sheetHandle;
115
+ sheetHandle = null;
116
+ handle?.close();
117
+ }
118
+ function clearSheet() {
119
+ if (sheetCloseTimer !== null) {
120
+ clearTimeoutFn(sheetCloseTimer);
121
+ sheetCloseTimer = null;
122
+ }
123
+ closeSheet();
124
+ sheetState = null;
125
+ sheetUnavailable = null;
126
+ }
127
+ function awaitingState(request) {
128
+ if (sheetUnavailable) {
129
+ return {
130
+ type: "awaiting_approval",
131
+ request,
132
+ popupBlocked: true,
133
+ sheetUnavailable
134
+ };
135
+ }
136
+ return {
137
+ type: "awaiting_approval",
138
+ request,
139
+ popupBlocked: false,
140
+ ...sheetState ? { sheet: { ...sheetState } } : {}
141
+ };
142
+ }
143
+ function updateSheet(patch) {
144
+ if (!sheetState) return;
145
+ sheetState = { ...sheetState, ...patch };
146
+ if (state.type === "awaiting_approval" && state.sheet) {
147
+ setState(awaitingState(state.request));
148
+ }
149
+ }
150
+ function failRun(error) {
151
+ running = false;
152
+ clearPoll();
153
+ setState({ type: "error", error });
154
+ }
155
+ function handleSheetEvent(event) {
156
+ switch (event.type) {
157
+ case "connected":
158
+ updateSheet({ completion: event.completion });
159
+ nudgePoll();
160
+ return;
161
+ case "progress":
162
+ updateSheet({ progress: event.phase });
163
+ return;
164
+ case "error": {
165
+ if (event.code === "request_failed") {
166
+ if (!running) return;
167
+ closeSheet();
168
+ failRun(new Error("Could not prepare the request for Vana's sheet"));
169
+ return;
170
+ }
171
+ if (SHEET_FALLBACK_CODES.includes(event.code)) {
172
+ sheetUnavailable = event.code;
173
+ sheetState = null;
174
+ closeSheet();
175
+ if (state.type === "awaiting_approval") {
176
+ setState(awaitingState(state.request));
177
+ }
178
+ return;
179
+ }
180
+ updateSheet({ error: event.code });
181
+ return;
182
+ }
183
+ case "close": {
184
+ if (!sheetHandle) return;
185
+ sheetHandle = null;
186
+ if (!running) return;
187
+ if (sheetState?.error) {
188
+ failRun(new Error(`Vana's sheet failed: ${sheetState.error}`));
189
+ return;
190
+ }
191
+ if (!sheetState?.completion) {
192
+ running = false;
193
+ activeRunId++;
194
+ clearPoll();
195
+ sheetState = null;
196
+ setState({ type: "idle" });
197
+ return;
198
+ }
199
+ updateSheet({ open: false });
200
+ return;
201
+ }
202
+ default:
203
+ return;
204
+ }
205
+ }
83
206
  async function readAndFinish(request) {
84
207
  setState({ type: "reading", request });
85
208
  try {
86
209
  const result = await transports.readResult(request.requestId);
87
210
  if (!running) return;
88
211
  setState({ type: "done", result });
212
+ if (sheetHandle) {
213
+ sheetHandle.setRequestStatus("completed");
214
+ if (sheetCloseAfterDoneMs !== null) {
215
+ sheetCloseTimer = setTimeoutFn(() => {
216
+ sheetCloseTimer = null;
217
+ closeSheet();
218
+ }, sheetCloseAfterDoneMs);
219
+ }
220
+ }
89
221
  } catch (err) {
90
222
  if (!running) return;
91
223
  setState({ type: "error", error: toError(err) });
@@ -94,9 +226,13 @@ function createDirectConnectFlow(transports, options = {}) {
94
226
  }
95
227
  }
96
228
  function scheduleNextPoll(request, deadline) {
229
+ const delay = pollNudged ? 0 : pollIntervalMs;
230
+ pollNudged = false;
231
+ pollTarget = { request, deadline };
97
232
  pollHandle = setTimeoutFn(() => {
233
+ pollHandle = null;
98
234
  void poll(request, deadline);
99
- }, pollIntervalMs);
235
+ }, delay);
100
236
  }
101
237
  function requestDeadline(request) {
102
238
  if (request.expiresAt !== void 0) {
@@ -106,6 +242,7 @@ function createDirectConnectFlow(transports, options = {}) {
106
242
  return now() + timeoutMs;
107
243
  }
108
244
  function startPolling(request, initialState) {
245
+ pollNudged = false;
109
246
  setState(initialState);
110
247
  const deadline = requestDeadline(request);
111
248
  if (now() >= deadline) {
@@ -129,6 +266,7 @@ function createDirectConnectFlow(transports, options = {}) {
129
266
  return;
130
267
  }
131
268
  let status;
269
+ pollInFlight = true;
132
270
  try {
133
271
  status = await transports.getStatus(request.requestId);
134
272
  } catch (err) {
@@ -136,6 +274,8 @@ function createDirectConnectFlow(transports, options = {}) {
136
274
  running = false;
137
275
  setState({ type: "error", error: toError(err) });
138
276
  return;
277
+ } finally {
278
+ pollInFlight = false;
139
279
  }
140
280
  if (!running) return;
141
281
  if (status.status === "pending" && state.type === "ready_to_open") {
@@ -158,6 +298,7 @@ function createDirectConnectFlow(transports, options = {}) {
158
298
  return;
159
299
  }
160
300
  if (status.status === "completed" || status.status === "denied" || status.status === "expired") {
301
+ sheetHandle?.setRequestStatus(status.status);
161
302
  running = false;
162
303
  setState({
163
304
  type: "error",
@@ -180,6 +321,11 @@ function createDirectConnectFlow(transports, options = {}) {
180
321
  running = true;
181
322
  approvedRequest = null;
182
323
  const runId = ++activeRunId;
324
+ clearSheet();
325
+ if (options.presentation === "sheet") {
326
+ await startSheet(runId);
327
+ return;
328
+ }
183
329
  const browserPlatform = (options.browserPlatformPolicy ?? defaultBrowserPlatformPolicy()).current();
184
330
  const approvalWindow = browserPlatform === "desktop" ? (options.openApprovalWindow ?? defaultOpenApprovalWindow)() : null;
185
331
  openedWindow = approvalWindow;
@@ -269,9 +415,75 @@ function createDirectConnectFlow(transports, options = {}) {
269
415
  activeRunId++;
270
416
  clearPoll();
271
417
  closeUnnavigatedWindow();
418
+ clearSheet();
272
419
  setState({ type: "idle" });
273
420
  }
274
421
  };
422
+ async function startSheet(runId) {
423
+ setState({ type: "creating" });
424
+ const fail = (error) => {
425
+ if (runId !== activeRunId) return;
426
+ clearSheet();
427
+ failRun(toError(error));
428
+ };
429
+ const authorizeRequest = transports.authorizeRequest;
430
+ if (!authorizeRequest) {
431
+ fail(
432
+ new Error('presentation: "sheet" needs the authorizeRequest transport')
433
+ );
434
+ return;
435
+ }
436
+ let sheetOptions;
437
+ try {
438
+ const configured = options.sheet;
439
+ if (!configured) {
440
+ throw new Error('presentation: "sheet" needs sheet options');
441
+ }
442
+ sheetOptions = typeof configured === "function" ? await configured() : configured;
443
+ } catch (err) {
444
+ fail(err);
445
+ return;
446
+ }
447
+ if (runId !== activeRunId) return;
448
+ const created = transports.createRequest();
449
+ created.catch(() => void 0);
450
+ sheetCloseAfterDoneMs = sheetOptions.closeAfterDoneMs === void 0 ? DEFAULT_CLOSE_AFTER_DONE_MS : sheetOptions.closeAfterDoneMs;
451
+ sheetState = { open: true };
452
+ try {
453
+ sheetHandle = (sheetOptions.open ?? openConnectSheet)({
454
+ clientId: sheetOptions.clientId,
455
+ appName: sheetOptions.appName,
456
+ vanaOrigin: sheetOptions.accountOrigin,
457
+ container: sheetOptions.container,
458
+ readyTimeoutMs: sheetOptions.readyTimeoutMs,
459
+ async getRequest() {
460
+ const request2 = await created;
461
+ return {
462
+ id: request2.requestId,
463
+ authorization: await authorizeRequest(request2.requestId)
464
+ };
465
+ },
466
+ onEvent(event) {
467
+ if (runId !== activeRunId) return;
468
+ sheetOptions.onEvent?.(event);
469
+ handleSheetEvent(event);
470
+ }
471
+ });
472
+ } catch {
473
+ sheetHandle = null;
474
+ sheetState = null;
475
+ sheetUnavailable = "sheet_failed";
476
+ }
477
+ let request;
478
+ try {
479
+ request = await created;
480
+ } catch (err) {
481
+ fail(err);
482
+ return;
483
+ }
484
+ if (runId !== activeRunId || !running) return;
485
+ startPolling(request, awaitingState(request));
486
+ }
275
487
  return flow;
276
488
  }
277
489
  export {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/direct/connect-flow.ts"],"sourcesContent":["/**\n * Framework-agnostic connect-flow state machine for the browser two-tab helper.\n *\n * @remarks\n * This is the testable core behind {@link useDirectVanaConnect}. It is pure\n * TypeScript (no React, no DOM-only APIs beyond an injectable window opener and\n * timers) so the full flow — create request, open Vana, poll status, read data —\n * can be exercised in a Node test environment.\n *\n * The React hook is a thin `useSyncExternalStore` binding over this store.\n *\n * @category Direct\n * @module direct/connect-flow\n */\n\nimport type {\n AccessRequest,\n AccessRequestStatus,\n AccessRequestStatusValue,\n ApprovedDataResult,\n} from \"./types\";\nimport { normalizeMobileContinuationUrl } from \"./types\";\n\n/**\n * Caller-supplied transports. These typically `fetch` the app's own backend\n * routes, which in turn delegate to a {@link DirectDataController}.\n */\nexport interface DirectConnectTransports<T = unknown> {\n /** Ask the backend to create an access request. */\n createRequest: () => Promise<AccessRequest>;\n /** Ask the backend for the current status of a request. */\n getStatus: (requestId: string) => Promise<AccessRequestStatus>;\n /** Ask the backend to read the approved data. */\n readResult: (requestId: string) => Promise<ApprovedDataResult<T>>;\n}\n\n/**\n * A handle to a tab opened synchronously under the user's click gesture.\n *\n * @remarks\n * The flow opens this tab *before* it knows the approval URL (popup blockers\n * only allow `window.open()` during the click's transient activation), then\n * navigates it once `createRequest` resolves.\n */\nexport interface ConnectWindow {\n /** Point the already-open tab at the approval URL. */\n navigate(url: string): void;\n /** Close the tab (used to clean up an un-navigated tab on failure/reset). */\n close(): void;\n}\n\n/** Browser class used only to choose the destination returned by Vana. */\nexport type DirectBrowserPlatform = \"desktop\" | \"mobile\";\n\n/** Injectable browser-platform policy; it never asserts whether an app exists. */\nexport interface DirectBrowserPlatformPolicy {\n current(): DirectBrowserPlatform;\n}\n\n/** Tunables for the connect flow. */\nexport interface DirectConnectOptions {\n /** Status poll interval in ms. Defaults to 1500. */\n pollIntervalMs?: number;\n /**\n * Overall timeout in ms before giving up. Defaults to 300000 (5 min).\n * Used only when the access request does not carry an authoritative\n * `expiresAt` value.\n */\n timeoutMs?: number;\n /**\n * Synchronously open a blank tab under the click's transient activation and\n * return a handle to navigate later, or `null` if the browser blocked it.\n * Defaults to `window.open(\"\", \"_blank\")` (with `opener` severed). Injectable\n * for tests.\n *\n * @remarks\n * Renamed from the pre-3.8 `openWindow?: (url) => void`. The old contract was\n * the BUI-622 bug itself (it was called with the URL *after* an `await`, so\n * the popup blocker suppressed it); it cannot be preserved while fixing the\n * bug. Custom openers must now open synchronously and return a navigable\n * handle.\n */\n openApprovalWindow?: () => ConnectWindow | null;\n /** SDK-owned mobile/desktop policy. Injectable for deterministic tests. */\n browserPlatformPolicy?: DirectBrowserPlatformPolicy;\n /** `setTimeout`. Injectable for tests. Defaults to `globalThis.setTimeout`. */\n setTimeoutFn?: (cb: () => void, ms: number) => unknown;\n /** `clearTimeout`. Injectable for tests. Defaults to `globalThis.clearTimeout`. */\n clearTimeoutFn?: (handle: unknown) => void;\n /** Clock source in ms. Injectable for tests. Defaults to `Date.now`. */\n now?: () => number;\n}\n\n/**\n * Discriminated connect-flow state.\n *\n * @remarks\n * `type` matches the builder guide: it starts at `\"idle\"` and is non-idle while\n * connecting. The intermediate phases give richer UIs something to render.\n *\n * Desktop and light-data requests move through `\"awaiting_approval\"` (Vana Web\n * opens in a popup). A deep Direct request on a mobile browser moves through\n * `\"ready_to_open\"` instead: the SDK exposes a plain HTTPS\n * `mobileContinuationUrl` for the UI to render as a primary \"Open Vana\" link,\n * never launching it automatically, and keeps polling in memory.\n */\nexport type DirectConnectState<T = unknown> =\n | { type: \"idle\" }\n | { type: \"creating\" }\n | {\n type: \"awaiting_approval\";\n request: AccessRequest;\n /**\n * `true` when the popup was blocked. The UI should render the universal\n * HTTPS `request.approvalUrl` as a manual \"Open approval\" link.\n */\n popupBlocked: boolean;\n }\n | {\n type: \"ready_to_open\";\n request: AccessRequest;\n /**\n * Validated HTTPS continuation URL the mobile UI renders as the primary\n * \"Open Vana\" tap. Polling continues while it is shown; its embedded\n * ticket may rotate to a fresh URL between polls.\n */\n mobileContinuationUrl: string;\n }\n | { type: \"reading\"; request: AccessRequest }\n | { type: \"done\"; result: ApprovedDataResult<T> }\n | { type: \"error\"; error: Error };\n\n/** Whether an explicit read retry reused consent or started fresh approval. */\nexport type DirectConnectRetryOutcome =\n | \"retried_existing_grant\"\n | \"fresh_approval_required\";\n\n/** The store returned by {@link createDirectConnectFlow}. */\nexport interface DirectConnectFlow<T = unknown> {\n /** Current state. */\n getState(): DirectConnectState<T>;\n /** Subscribe to state changes; returns an unsubscribe function. */\n subscribe(listener: () => void): () => void;\n /** Begin the flow. No-op if already running. */\n start(): Promise<void>;\n /**\n * Retry a failed read, reusing a still-live approved request when possible.\n *\n * @remarks\n * This explicit path avoids the observed double-approval symptom where\n * \"Try that again\" minted a new request after a transient read failure.\n * The return value tells callers whether existing consent was reused or a\n * fresh approval was required.\n */\n retryRead(): Promise<DirectConnectRetryOutcome>;\n /** Reset to `idle` and stop any in-flight polling. */\n reset(): void;\n}\n\nconst DEFAULT_POLL_INTERVAL_MS = 1500;\nconst DEFAULT_TIMEOUT_MS = 300_000;\n\nfunction toError(value: unknown): Error {\n return value instanceof Error ? value : new Error(String(value));\n}\n\nfunction isReadReadyStatus(status: AccessRequestStatusValue): boolean {\n return status === \"approved\" || status === \"ready_for_read\";\n}\n\nconst MOBILE_USER_AGENT =\n /Android|iPhone|iPad|iPod|Mobile|Silk|Kindle|Opera Mini|IEMobile/i;\n\nfunction defaultBrowserPlatformPolicy(): DirectBrowserPlatformPolicy {\n return {\n current() {\n if (typeof navigator === \"undefined\") return \"desktop\";\n const isTouchCapableIpad =\n navigator.platform === \"MacIntel\" && navigator.maxTouchPoints > 1;\n return MOBILE_USER_AGENT.test(navigator.userAgent) || isTouchCapableIpad\n ? \"mobile\"\n : \"desktop\";\n },\n };\n}\n\n/**\n * Default {@link DirectConnectOptions.openApprovalWindow}: open a blank tab\n * synchronously (inside the click gesture) and return a handle to navigate\n * once the approval URL is known. Returns `null` when blocked or non-DOM.\n */\nfunction defaultOpenApprovalWindow(): ConnectWindow | null {\n if (typeof window === \"undefined\" || !window.open) return null;\n // We can't pass the \"noopener\"/\"noreferrer\" feature string here: it makes\n // window.open() return null, which would throw away the handle we need to\n // navigate later. So we open plain and re-create both protections by hand.\n const opened = window.open(\"\", \"_blank\");\n if (!opened) return null;\n // Sever the opener link while the tab is still about:blank, so the approval\n // page can't reach back into the app (reverse tab-nabbing).\n try {\n opened.opener = null;\n } catch {\n // Some environments make `opener` read-only; best-effort only.\n }\n return {\n navigate(url: string) {\n // Restore the no-referrer protection the old \"noreferrer\" feature gave:\n // tag the blank document so the upcoming navigation sends no Referer to\n // the approval page (best-effort; the blank doc is same-origin here).\n try {\n const meta = opened.document.createElement(\"meta\");\n meta.name = \"referrer\";\n meta.content = \"no-referrer\";\n (opened.document.head ?? opened.document.documentElement)?.appendChild(\n meta,\n );\n } catch {\n // Cross-origin/unavailable document: skip, navigation still proceeds.\n }\n opened.location.href = url;\n },\n close() {\n opened.close();\n },\n };\n}\n\n/**\n * Create a connect-flow store.\n *\n * @param transports - Backend transports (`createRequest`, `getStatus`, `readResult`).\n * @param options - Polling/timeout tunables and injectable side effects.\n * @returns A {@link DirectConnectFlow} store.\n */\nexport function createDirectConnectFlow<T = unknown>(\n transports: DirectConnectTransports<T>,\n options: DirectConnectOptions = {},\n): DirectConnectFlow<T> {\n const pollIntervalMs = options.pollIntervalMs ?? DEFAULT_POLL_INTERVAL_MS;\n const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;\n // `openApprovalWindow` and `browserPlatformPolicy` are resolved lazily at\n // start() (see below) so options swapped in after construction are still\n // honoured — matching the latest-callback pattern the React hook uses for its\n // transports.\n const setTimeoutFn =\n options.setTimeoutFn ??\n ((cb: () => void, ms: number) => globalThis.setTimeout(cb, ms));\n const clearTimeoutFn =\n options.clearTimeoutFn ??\n ((handle: unknown) => {\n globalThis.clearTimeout(handle as never);\n });\n const now = options.now ?? (() => Date.now());\n let state: DirectConnectState<T> = { type: \"idle\" };\n const listeners = new Set<() => void>();\n let pollHandle: unknown = null;\n let running = false;\n // Retained only after status proved this request had a read-ready grant.\n // A retry rechecks that status before trusting the prior consent.\n let approvedRequest: AccessRequest | null = null;\n // Monotonic id for the current start() invocation. reset() (and an\n // immediately following start()) bumps it, so a previous run whose async\n // createRequest is still in flight can detect it has been superseded and\n // avoid touching shared state / the newer run's tab.\n let activeRunId = 0;\n // Holds the tab we opened only while it is still blank (un-navigated). Once\n // navigated to the approval URL we drop the reference so reset/cleanup never\n // closes the live approval tab the user is interacting with.\n let openedWindow: ConnectWindow | null = null;\n\n function emit(): void {\n for (const listener of listeners) listener();\n }\n\n function setState(next: DirectConnectState<T>): void {\n state = next;\n emit();\n }\n\n function clearPoll(): void {\n if (pollHandle !== null) {\n clearTimeoutFn(pollHandle);\n pollHandle = null;\n }\n }\n\n /** Close the opened tab if it is still blank (never navigated). */\n function closeUnnavigatedWindow(): void {\n if (openedWindow) {\n openedWindow.close();\n openedWindow = null;\n }\n }\n\n function isRunningPhase(): boolean {\n return (\n state.type === \"creating\" ||\n state.type === \"awaiting_approval\" ||\n state.type === \"ready_to_open\" ||\n state.type === \"reading\"\n );\n }\n\n async function readAndFinish(request: AccessRequest): Promise<void> {\n setState({ type: \"reading\", request });\n try {\n const result = await transports.readResult(request.requestId);\n if (!running) return;\n setState({ type: \"done\", result });\n } catch (err) {\n if (!running) return;\n setState({ type: \"error\", error: toError(err) });\n } finally {\n running = false;\n }\n }\n\n function scheduleNextPoll(request: AccessRequest, deadline: number): void {\n pollHandle = setTimeoutFn(() => {\n void poll(request, deadline);\n }, pollIntervalMs);\n }\n\n function requestDeadline(request: AccessRequest): number {\n if (request.expiresAt !== undefined) {\n const expiresAt = Date.parse(request.expiresAt);\n if (Number.isFinite(expiresAt)) return expiresAt;\n }\n return now() + timeoutMs;\n }\n\n /**\n * Enter the polling loop from the given initial state (either\n * `awaiting_approval` for desktop/light or `ready_to_open` for mobile-deep).\n * Errors out immediately if the request has already expired.\n */\n function startPolling(\n request: AccessRequest,\n initialState: DirectConnectState<T>,\n ): void {\n setState(initialState);\n const deadline = requestDeadline(request);\n if (now() >= deadline) {\n running = false;\n setState({\n type: \"error\",\n error: new Error(\"Access request expired\"),\n });\n return;\n }\n scheduleNextPoll(request, deadline);\n }\n\n async function poll(request: AccessRequest, deadline: number): Promise<void> {\n if (!running) return;\n if (now() >= deadline) {\n running = false;\n setState({\n type: \"error\",\n error: new Error(\"Timed out waiting for approval\"),\n });\n return;\n }\n let status: AccessRequestStatus;\n try {\n status = await transports.getStatus(request.requestId);\n } catch (err) {\n if (!running) return;\n running = false;\n setState({ type: \"error\", error: toError(err) });\n return;\n }\n if (!running) return;\n\n // A pending deep-mobile status may rotate the continuation ticket. Adopt a\n // fresh, still-valid URL so the rendered \"Open Vana\" link always points at a\n // live ticket; ignore it on the desktop/light path.\n if (status.status === \"pending\" && state.type === \"ready_to_open\") {\n const refreshed = normalizeMobileContinuationUrl(\n status.mobileContinuationUrl,\n );\n if (refreshed && refreshed !== state.mobileContinuationUrl) {\n request = { ...request, mobileContinuationUrl: refreshed };\n setState({\n type: \"ready_to_open\",\n request,\n mobileContinuationUrl: refreshed,\n });\n }\n }\n\n if (isReadReadyStatus(status.status)) {\n clearPoll();\n approvedRequest = request;\n await readAndFinish(request);\n return;\n }\n if (\n status.status === \"completed\" ||\n status.status === \"denied\" ||\n status.status === \"expired\"\n ) {\n running = false;\n setState({\n type: \"error\",\n error: new Error(`Access request ${status.status}`),\n });\n return;\n }\n scheduleNextPoll(request, deadline);\n }\n\n const flow: DirectConnectFlow<T> = {\n getState() {\n return state;\n },\n\n subscribe(listener: () => void) {\n listeners.add(listener);\n return () => listeners.delete(listener);\n },\n\n async start(): Promise<void> {\n if (running || isRunningPhase()) return;\n running = true;\n // start() deliberately keeps its established first-run semantics: every\n // explicit start creates a request. Only retryRead() may reuse consent.\n approvedRequest = null;\n const runId = ++activeRunId;\n // Read the platform policy at start time, like openApprovalWindow below,\n // so a policy swapped in after construction (a React rerender forwards\n // options through a ref) still decides this run's destination.\n const browserPlatform = (\n options.browserPlatformPolicy ?? defaultBrowserPlatformPolicy()\n ).current();\n\n // Desktop preserves the pre-mobile synchronous popup contract: open a\n // blank tab while the click's transient activation is live, then navigate\n // it once createRequest returns the approval URL (BUI-622). Mobile never\n // creates that transient tab; deep requests expose one explicit HTTPS\n // link, while light requests retain the manual approvalUrl fallback.\n // Read the opener option at start time so a swapped-in custom opener is\n // still honored for desktop flows.\n const approvalWindow =\n browserPlatform === \"desktop\"\n ? (options.openApprovalWindow ?? defaultOpenApprovalWindow)()\n : null;\n openedWindow = approvalWindow;\n\n setState({ type: \"creating\" });\n\n let request: AccessRequest;\n try {\n request = await transports.createRequest();\n } catch (err) {\n // If we were superseded (reset, possibly + a newer start()) while this\n // request was in flight, only clean up our own tab — never the shared\n // state or the newer run's window.\n if (runId !== activeRunId) {\n approvalWindow?.close();\n return;\n }\n running = false;\n closeUnnavigatedWindow();\n setState({ type: \"error\", error: toError(err) });\n return;\n }\n if (runId !== activeRunId) {\n approvalWindow?.close();\n return;\n }\n // Re-validate the continuation URL at the SDK boundary (defense in depth\n // for custom transports that bypass the default client).\n request = {\n ...request,\n mobileContinuationUrl: normalizeMobileContinuationUrl(\n request.mobileContinuationUrl,\n ),\n };\n\n // The SDK owns only the small mobile-versus-desktop destination choice.\n // A deep Direct request on mobile carries a validated continuation URL;\n // desktop keeps its popup contract, while mobile light exposes the HTTPS\n // approval URL as the existing manual fallback without opening a tab.\n const mobileContinuationUrl =\n browserPlatform === \"mobile\"\n ? request.mobileContinuationUrl\n : undefined;\n\n if (mobileContinuationUrl) {\n // Do not auto-launch: DCR creation is async, so the original Connect\n // gesture can no longer be trusted to retain iOS user activation. Let\n // the UI render an explicit primary \"Open Vana\" link; polling continues\n // in this tab.\n startPolling(request, {\n type: \"ready_to_open\",\n request,\n mobileContinuationUrl,\n });\n return;\n }\n\n // Desktop/light: navigate the synchronously-opened tab to the HTTPS\n // approval URL. `approvalWindow === null` means the popup was blocked;\n // surface it so the UI renders request.approvalUrl as a visible manual\n // \"Open approval\" link instead of hanging. We poll either way, so a manual\n // open still resolves the flow, and the timeout still bounds the wait.\n if (approvalWindow) {\n approvalWindow.navigate(request.approvalUrl);\n // Hand the tab off to the user; we no longer own/close it.\n openedWindow = null;\n }\n startPolling(request, {\n type: \"awaiting_approval\",\n request,\n popupBlocked: approvalWindow === null,\n });\n },\n\n async retryRead(): Promise<DirectConnectRetryOutcome> {\n if (running || isRunningPhase()) {\n throw new Error(\n \"Cannot retry a read while the connect flow is running\",\n );\n }\n\n const request = approvedRequest;\n const parsedExpiry = request?.expiresAt\n ? Date.parse(request.expiresAt)\n : Number.NaN;\n const requestExpired =\n Number.isFinite(parsedExpiry) && now() >= parsedExpiry;\n\n if (request && !requestExpired) {\n running = true;\n const runId = ++activeRunId;\n let status: AccessRequestStatus;\n try {\n status = await transports.getStatus(request.requestId);\n } catch (err) {\n if (runId !== activeRunId) {\n throw new Error(\"Read retry was superseded\");\n }\n running = false;\n const error = toError(err);\n setState({ type: \"error\", error });\n throw error;\n }\n if (runId !== activeRunId) {\n throw new Error(\"Read retry was superseded\");\n }\n if (isReadReadyStatus(status.status)) {\n await readAndFinish(request);\n return \"retried_existing_grant\";\n }\n running = false;\n }\n\n approvedRequest = null;\n await flow.start();\n return \"fresh_approval_required\";\n },\n\n reset(): void {\n running = false;\n approvedRequest = null;\n // Invalidate any in-flight start() so a late createRequest can't clobber\n // a subsequent run.\n activeRunId++;\n clearPoll();\n closeUnnavigatedWindow();\n setState({ type: \"idle\" });\n },\n };\n\n return flow;\n}\n"],"mappings":"AAqBA,SAAS,sCAAsC;AA0I/C,MAAM,2BAA2B;AACjC,MAAM,qBAAqB;AAE3B,SAAS,QAAQ,OAAuB;AACtC,SAAO,iBAAiB,QAAQ,QAAQ,IAAI,MAAM,OAAO,KAAK,CAAC;AACjE;AAEA,SAAS,kBAAkB,QAA2C;AACpE,SAAO,WAAW,cAAc,WAAW;AAC7C;AAEA,MAAM,oBACJ;AAEF,SAAS,+BAA4D;AACnE,SAAO;AAAA,IACL,UAAU;AACR,UAAI,OAAO,cAAc,YAAa,QAAO;AAC7C,YAAM,qBACJ,UAAU,aAAa,cAAc,UAAU,iBAAiB;AAClE,aAAO,kBAAkB,KAAK,UAAU,SAAS,KAAK,qBAClD,WACA;AAAA,IACN;AAAA,EACF;AACF;AAOA,SAAS,4BAAkD;AACzD,MAAI,OAAO,WAAW,eAAe,CAAC,OAAO,KAAM,QAAO;AAI1D,QAAM,SAAS,OAAO,KAAK,IAAI,QAAQ;AACvC,MAAI,CAAC,OAAQ,QAAO;AAGpB,MAAI;AACF,WAAO,SAAS;AAAA,EAClB,QAAQ;AAAA,EAER;AACA,SAAO;AAAA,IACL,SAAS,KAAa;AAIpB,UAAI;AACF,cAAM,OAAO,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ACA,YAAI,UAAU,aAAa;AACzB,gBAAM,IAAI,MAAM,2BAA2B;AAAA,QAC7C;AACA,YAAI,kBAAkB,OAAO,MAAM,GAAG;AACpC,gBAAM,cAAc,OAAO;AAC3B,iBAAO;AAAA,QACT;AACA,kBAAU;AAAA,MACZ;AAEA,wBAAkB;AAClB,YAAM,KAAK,MAAM;AACjB,aAAO;AAAA,IACT;AAAA,IAEA,QAAc;AACZ,gBAAU;AACV,wBAAkB;AAGlB;AACA,gBAAU;AACV,6BAAuB;AACvB,eAAS,EAAE,MAAM,OAAO,CAAC;AAAA,IAC3B;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
1
+ {"version":3,"sources":["../../src/direct/connect-flow.ts"],"sourcesContent":["/**\n * Framework-agnostic connect-flow state machine for the browser two-tab helper.\n *\n * @remarks\n * This is the testable core behind {@link useDirectVanaConnect}. It is pure\n * TypeScript (no React, no DOM-only APIs beyond an injectable window opener and\n * timers) so the full flow — create request, open Vana, poll status, read data —\n * can be exercised in a Node test environment.\n *\n * The React hook is a thin `useSyncExternalStore` binding over this store.\n *\n * @category Direct\n * @module direct/connect-flow\n */\n\nimport type {\n AccessRequest,\n AccessRequestStatus,\n AccessRequestStatusValue,\n ApprovedDataResult,\n} from \"./types\";\nimport { normalizeMobileContinuationUrl } from \"./types\";\nimport {\n openConnectSheet,\n type ConnectEvent,\n type ConnectHandle,\n type ConnectOpenOptions,\n} from \"../connect/launcher\";\nimport type {\n ConnectCompletion,\n ConnectProgressPhase,\n RequestAuthorization,\n SheetErrorCode,\n} from \"../connect/protocol\";\n\n/**\n * Caller-supplied transports. These typically `fetch` the app's own backend\n * routes, which in turn delegate to a {@link DirectDataController}.\n */\nexport interface DirectConnectTransports<T = unknown> {\n /** Ask the backend to create an access request. */\n createRequest: () => Promise<AccessRequest>;\n /** Ask the backend for the current status of a request. */\n getStatus: (requestId: string) => Promise<AccessRequestStatus>;\n /** Ask the backend to read the approved data. */\n readResult: (requestId: string) => Promise<ApprovedDataResult<T>>;\n /**\n * Ask the backend to sign the sheet's short-lived read capability for a\n * request it created for this browser (`controller.authorizeRequestRead`).\n * Required for `presentation: \"sheet\"`; called each time the sheet asks.\n */\n authorizeRequest?: (requestId: string) => Promise<RequestAuthorization>;\n}\n\n/** How the person approves: a Vana tab (default) or Vana's in-page sheet. */\nexport type DirectConnectPresentation = \"tab\" | \"sheet\";\n\n/** Sheet settings for `presentation: \"sheet\"`. */\nexport interface DirectConnectSheetOptions {\n /** The embed client id Vana Account registered for this page's origin. */\n clientId: string;\n /** Label on the sheet chrome (Vana shows the registered name inside). */\n appName: string;\n /**\n * Vana Account origin that serves the sheet. Required: there is no\n * production default, so a dev app never silently talks to production.\n * Use `VANA_ACCOUNT_ORIGINS[env]`, or `resolveVanaAccountOrigin` for\n * preview deploys. `useVanaConnect` fills it from the backend's config.\n */\n accountOrigin: string;\n /** Element the sheet mounts in. Defaults to `document.body`. */\n container?: HTMLElement;\n /** How long to wait for the sheet to start before falling back. */\n readyTimeoutMs?: number;\n /**\n * Close the sheet this long (ms) after the data is read, so the person sees\n * it finish. Defaults to 1500; `null` leaves it open for the person to close.\n */\n closeAfterDoneMs?: number | null;\n /** Every raw launcher event, for logging or custom UI. */\n onEvent?: (event: ConnectEvent) => void;\n /** Launcher. Defaults to the SDK's `openConnectSheet`; injectable for tests. */\n open?: (options: ConnectOpenOptions) => ConnectHandle;\n}\n\n/**\n * What the sheet has reported, while it presents approval.\n *\n * @remarks\n * These are hints for the UI, not outcomes. The flow settles only from the\n * backend: `done` once the status poll says the request is readable and the\n * read succeeds, `error` when the poll reports `denied`, `expired` or\n * `completed`, or the read fails.\n */\nexport interface DirectConnectSheetState {\n /** `false` once the person closed the sheet after approving. */\n open: boolean;\n /**\n * Set once the person approved: how the data will arrive. Approval is not\n * delivery; wait for `state.type === \"done\"` before treating data as\n * delivered.\n */\n completion?: ConnectCompletion;\n /** Coarse `remote_collect` progress. */\n progress?: ConnectProgressPhase;\n /** A failure the sheet is showing; it stays open for the person to read. */\n error?: SheetErrorCode;\n}\n\n/**\n * Sheet errors after which the flow closes the sheet and falls back to the\n * tab presentation (`popupBlocked: true`, so the UI shows the approval link).\n */\nconst SHEET_FALLBACK_CODES: readonly SheetErrorCode[] = [\n \"frame_blocked\",\n \"origin_not_registered\",\n \"incompatible_version\",\n];\n\nconst DEFAULT_CLOSE_AFTER_DONE_MS = 1500;\n\n/**\n * A handle to a tab opened synchronously under the user's click gesture.\n *\n * @remarks\n * The flow opens this tab *before* it knows the approval URL (popup blockers\n * only allow `window.open()` during the click's transient activation), then\n * navigates it once `createRequest` resolves.\n */\nexport interface ConnectWindow {\n /** Point the already-open tab at the approval URL. */\n navigate(url: string): void;\n /** Close the tab (used to clean up an un-navigated tab on failure/reset). */\n close(): void;\n}\n\n/** Browser class used only to choose the destination returned by Vana. */\nexport type DirectBrowserPlatform = \"desktop\" | \"mobile\";\n\n/** Injectable browser-platform policy; it never asserts whether an app exists. */\nexport interface DirectBrowserPlatformPolicy {\n current(): DirectBrowserPlatform;\n}\n\n/** Tunables for the connect flow. */\nexport interface DirectConnectOptions {\n /** Status poll interval in ms. Defaults to 1500. */\n pollIntervalMs?: number;\n /**\n * Overall timeout in ms before giving up. Defaults to 300000 (5 min).\n * Used only when the access request does not carry an authoritative\n * `expiresAt` value.\n */\n timeoutMs?: number;\n /**\n * Synchronously open a blank tab under the click's transient activation and\n * return a handle to navigate later, or `null` if the browser blocked it.\n * Defaults to `window.open(\"\", \"_blank\")` (with `opener` severed). Injectable\n * for tests.\n *\n * @remarks\n * Renamed from the pre-3.8 `openWindow?: (url) => void`. The old contract was\n * the BUI-622 bug itself (it was called with the URL *after* an `await`, so\n * the popup blocker suppressed it); it cannot be preserved while fixing the\n * bug. Custom openers must now open synchronously and return a navigable\n * handle.\n */\n openApprovalWindow?: () => ConnectWindow | null;\n /** SDK-owned mobile/desktop policy. Injectable for deterministic tests. */\n browserPlatformPolicy?: DirectBrowserPlatformPolicy;\n /** `setTimeout`. Injectable for tests. Defaults to `globalThis.setTimeout`. */\n setTimeoutFn?: (cb: () => void, ms: number) => unknown;\n /**\n * `\"sheet\"` presents approval in Vana's in-page sheet instead of a tab;\n * needs {@link DirectConnectOptions.sheet} and the `authorizeRequest`\n * transport. Defaults to `\"tab\"`.\n */\n presentation?: DirectConnectPresentation;\n /** Sheet settings, or a loader for them (fetched at `start()`). */\n sheet?:\n | DirectConnectSheetOptions\n | (() => Promise<DirectConnectSheetOptions>);\n /** `clearTimeout`. Injectable for tests. Defaults to `globalThis.clearTimeout`. */\n clearTimeoutFn?: (handle: unknown) => void;\n /** Clock source in ms. Injectable for tests. Defaults to `Date.now`. */\n now?: () => number;\n}\n\n/**\n * Discriminated connect-flow state.\n *\n * @remarks\n * `type` matches the builder guide: it starts at `\"idle\"` and is non-idle while\n * connecting. The intermediate phases give richer UIs something to render.\n *\n * Desktop and light-data requests move through `\"awaiting_approval\"` (Vana Web\n * opens in a popup). A deep Direct request on a mobile browser moves through\n * `\"ready_to_open\"` instead: the SDK exposes a plain HTTPS\n * `mobileContinuationUrl` for the UI to render as a primary \"Open Vana\" link,\n * never launching it automatically, and keeps polling in memory.\n */\nexport type DirectConnectState<T = unknown> =\n | { type: \"idle\" }\n | { type: \"creating\" }\n | {\n type: \"awaiting_approval\";\n request: AccessRequest;\n /**\n * `true` when the popup was blocked. The UI should render the universal\n * HTTPS `request.approvalUrl` as a manual \"Open approval\" link.\n */\n popupBlocked: boolean;\n /** Present while Vana's sheet presents approval (`presentation: \"sheet\"`). */\n sheet?: DirectConnectSheetState;\n /**\n * Set when the sheet could not run here and the flow fell back to the\n * tab presentation (with `popupBlocked: true`).\n */\n sheetUnavailable?: SheetErrorCode;\n }\n | {\n type: \"ready_to_open\";\n request: AccessRequest;\n /**\n * Validated HTTPS continuation URL the mobile UI renders as the primary\n * \"Open Vana\" tap. Polling continues while it is shown; its embedded\n * ticket may rotate to a fresh URL between polls.\n */\n mobileContinuationUrl: string;\n }\n | { type: \"reading\"; request: AccessRequest }\n | { type: \"done\"; result: ApprovedDataResult<T> }\n | { type: \"error\"; error: Error };\n\n/** Whether an explicit read retry reused consent or started fresh approval. */\nexport type DirectConnectRetryOutcome =\n | \"retried_existing_grant\"\n | \"fresh_approval_required\";\n\n/** The store returned by {@link createDirectConnectFlow}. */\nexport interface DirectConnectFlow<T = unknown> {\n /** Current state. */\n getState(): DirectConnectState<T>;\n /** Subscribe to state changes; returns an unsubscribe function. */\n subscribe(listener: () => void): () => void;\n /** Begin the flow. No-op if already running. */\n start(): Promise<void>;\n /**\n * Retry a failed read, reusing a still-live approved request when possible.\n *\n * @remarks\n * This explicit path avoids the observed double-approval symptom where\n * \"Try that again\" minted a new request after a transient read failure.\n * The return value tells callers whether existing consent was reused or a\n * fresh approval was required.\n */\n retryRead(): Promise<DirectConnectRetryOutcome>;\n /** Reset to `idle` and stop any in-flight polling. */\n reset(): void;\n}\n\nconst DEFAULT_POLL_INTERVAL_MS = 1500;\nconst DEFAULT_TIMEOUT_MS = 300_000;\n\nfunction toError(value: unknown): Error {\n return value instanceof Error ? value : new Error(String(value));\n}\n\nfunction isReadReadyStatus(status: AccessRequestStatusValue): boolean {\n return status === \"approved\" || status === \"ready_for_read\";\n}\n\nconst MOBILE_USER_AGENT =\n /Android|iPhone|iPad|iPod|Mobile|Silk|Kindle|Opera Mini|IEMobile/i;\n\nfunction defaultBrowserPlatformPolicy(): DirectBrowserPlatformPolicy {\n return {\n current() {\n if (typeof navigator === \"undefined\") return \"desktop\";\n const isTouchCapableIpad =\n navigator.platform === \"MacIntel\" && navigator.maxTouchPoints > 1;\n return MOBILE_USER_AGENT.test(navigator.userAgent) || isTouchCapableIpad\n ? \"mobile\"\n : \"desktop\";\n },\n };\n}\n\n/**\n * Default {@link DirectConnectOptions.openApprovalWindow}: open a blank tab\n * synchronously (inside the click gesture) and return a handle to navigate\n * once the approval URL is known. Returns `null` when blocked or non-DOM.\n */\nfunction defaultOpenApprovalWindow(): ConnectWindow | null {\n if (typeof window === \"undefined\" || !window.open) return null;\n // We can't pass the \"noopener\"/\"noreferrer\" feature string here: it makes\n // window.open() return null, which would throw away the handle we need to\n // navigate later. So we open plain and re-create both protections by hand.\n const opened = window.open(\"\", \"_blank\");\n if (!opened) return null;\n // Sever the opener link while the tab is still about:blank, so the approval\n // page can't reach back into the app (reverse tab-nabbing).\n try {\n opened.opener = null;\n } catch {\n // Some environments make `opener` read-only; best-effort only.\n }\n return {\n navigate(url: string) {\n // Restore the no-referrer protection the old \"noreferrer\" feature gave:\n // tag the blank document so the upcoming navigation sends no Referer to\n // the approval page (best-effort; the blank doc is same-origin here).\n try {\n const meta = opened.document.createElement(\"meta\");\n meta.name = \"referrer\";\n meta.content = \"no-referrer\";\n (opened.document.head ?? opened.document.documentElement)?.appendChild(\n meta,\n );\n } catch {\n // Cross-origin/unavailable document: skip, navigation still proceeds.\n }\n opened.location.href = url;\n },\n close() {\n opened.close();\n },\n };\n}\n\n/**\n * Create a connect-flow store.\n *\n * @param transports - Backend transports (`createRequest`, `getStatus`, `readResult`).\n * @param options - Polling/timeout tunables and injectable side effects.\n * @returns A {@link DirectConnectFlow} store.\n */\nexport function createDirectConnectFlow<T = unknown>(\n transports: DirectConnectTransports<T>,\n options: DirectConnectOptions = {},\n): DirectConnectFlow<T> {\n const pollIntervalMs = options.pollIntervalMs ?? DEFAULT_POLL_INTERVAL_MS;\n const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;\n // `openApprovalWindow` and `browserPlatformPolicy` are resolved lazily at\n // start() (see below) so options swapped in after construction are still\n // honoured — matching the latest-callback pattern the React hook uses for its\n // transports.\n const setTimeoutFn =\n options.setTimeoutFn ??\n ((cb: () => void, ms: number) => globalThis.setTimeout(cb, ms));\n const clearTimeoutFn =\n options.clearTimeoutFn ??\n ((handle: unknown) => {\n globalThis.clearTimeout(handle as never);\n });\n const now = options.now ?? (() => Date.now());\n let state: DirectConnectState<T> = { type: \"idle\" };\n const listeners = new Set<() => void>();\n let pollHandle: unknown = null;\n // The request and deadline the poll loop is on, so a sheet event can ask\n // for the next poll now instead of after the interval.\n let pollTarget: { request: AccessRequest; deadline: number } | null = null;\n let pollInFlight = false;\n let pollNudged = false;\n let running = false;\n // Retained only after status proved this request had a read-ready grant.\n // A retry rechecks that status before trusting the prior consent.\n let approvedRequest: AccessRequest | null = null;\n // Monotonic id for the current start() invocation. reset() (and an\n // immediately following start()) bumps it, so a previous run whose async\n // createRequest is still in flight can detect it has been superseded and\n // avoid touching shared state / the newer run's tab.\n let activeRunId = 0;\n // Holds the tab we opened only while it is still blank (un-navigated). Once\n // navigated to the approval URL we drop the reference so reset/cleanup never\n // closes the live approval tab the user is interacting with.\n let openedWindow: ConnectWindow | null = null;\n // The sheet of the current run (`presentation: \"sheet\"`), while open.\n let sheetHandle: ConnectHandle | null = null;\n let sheetState: DirectConnectSheetState | null = null;\n let sheetUnavailable: SheetErrorCode | null = null;\n let sheetCloseAfterDoneMs: number | null = DEFAULT_CLOSE_AFTER_DONE_MS;\n let sheetCloseTimer: unknown = null;\n\n function emit(): void {\n for (const listener of listeners) listener();\n }\n\n function setState(next: DirectConnectState<T>): void {\n state = next;\n emit();\n }\n\n function clearPoll(): void {\n if (pollHandle !== null) {\n clearTimeoutFn(pollHandle);\n pollHandle = null;\n }\n pollTarget = null;\n pollNudged = false;\n }\n\n /**\n * Poll now rather than after the interval. Sheet events call this: they\n * hint that the backend's status may have moved, but never settle the flow\n * themselves. The poll stays the one authority for `done` and `error`.\n */\n function nudgePoll(): void {\n if (!running) return;\n if (pollInFlight) {\n pollNudged = true;\n return;\n }\n const target = pollTarget;\n if (pollHandle === null || !target) return;\n clearPoll();\n void poll(target.request, target.deadline);\n }\n\n /** Close the opened tab if it is still blank (never navigated). */\n function closeUnnavigatedWindow(): void {\n if (openedWindow) {\n openedWindow.close();\n openedWindow = null;\n }\n }\n\n function isRunningPhase(): boolean {\n return (\n state.type === \"creating\" ||\n state.type === \"awaiting_approval\" ||\n state.type === \"ready_to_open\" ||\n state.type === \"reading\"\n );\n }\n\n /** Close the sheet without treating its close event as the person's. */\n function closeSheet(): void {\n const handle = sheetHandle;\n sheetHandle = null;\n handle?.close();\n }\n\n function clearSheet(): void {\n if (sheetCloseTimer !== null) {\n clearTimeoutFn(sheetCloseTimer);\n sheetCloseTimer = null;\n }\n closeSheet();\n sheetState = null;\n sheetUnavailable = null;\n }\n\n /** The waiting state for `request`, given what the sheet has reported. */\n function awaitingState(request: AccessRequest): DirectConnectState<T> {\n if (sheetUnavailable) {\n return {\n type: \"awaiting_approval\",\n request,\n popupBlocked: true,\n sheetUnavailable,\n };\n }\n return {\n type: \"awaiting_approval\",\n request,\n popupBlocked: false,\n ...(sheetState ? { sheet: { ...sheetState } } : {}),\n };\n }\n\n function updateSheet(patch: Partial<DirectConnectSheetState>): void {\n if (!sheetState) return;\n sheetState = { ...sheetState, ...patch };\n if (state.type === \"awaiting_approval\" && state.sheet) {\n setState(awaitingState(state.request));\n }\n }\n\n function failRun(error: Error): void {\n running = false;\n clearPoll();\n setState({ type: \"error\", error });\n }\n\n /**\n * Sheet events update what the UI shows and nudge the status poll; none of\n * them is an outcome. `connected` is the person's approval, not delivery.\n * A future terminal sheet event belongs here too, as another nudge: the\n * backend poll and read still decide `done` or `error`.\n */\n function handleSheetEvent(event: ConnectEvent): void {\n switch (event.type) {\n case \"connected\":\n updateSheet({ completion: event.completion });\n nudgePoll();\n return;\n case \"progress\":\n updateSheet({ progress: event.phase });\n return;\n case \"error\": {\n if (event.code === \"request_failed\") {\n // The backend could not create or sign the request for the sheet.\n if (!running) return;\n closeSheet();\n failRun(new Error(\"Could not prepare the request for Vana's sheet\"));\n return;\n }\n if (SHEET_FALLBACK_CODES.includes(event.code)) {\n // The sheet cannot run on this page: fall back to the approval URL.\n sheetUnavailable = event.code;\n sheetState = null;\n closeSheet();\n if (state.type === \"awaiting_approval\") {\n setState(awaitingState(state.request));\n }\n return;\n }\n updateSheet({ error: event.code });\n return;\n }\n case \"close\": {\n if (!sheetHandle) return; // closed by the flow itself\n sheetHandle = null;\n if (!running) return;\n if (sheetState?.error) {\n failRun(new Error(`Vana's sheet failed: ${sheetState.error}`));\n return;\n }\n if (!sheetState?.completion) {\n // Closed before approving: the person backed out.\n running = false;\n activeRunId++;\n clearPoll();\n sheetState = null;\n setState({ type: \"idle\" });\n return;\n }\n // Approved, then closed. The data may still be readable (a durable\n // Personal Server), so keep polling until the request settles.\n updateSheet({ open: false });\n return;\n }\n default:\n return;\n }\n }\n\n async function readAndFinish(request: AccessRequest): Promise<void> {\n setState({ type: \"reading\", request });\n try {\n const result = await transports.readResult(request.requestId);\n if (!running) return;\n setState({ type: \"done\", result });\n if (sheetHandle) {\n sheetHandle.setRequestStatus(\"completed\");\n if (sheetCloseAfterDoneMs !== null) {\n sheetCloseTimer = setTimeoutFn(() => {\n sheetCloseTimer = null;\n closeSheet();\n }, sheetCloseAfterDoneMs);\n }\n }\n } catch (err) {\n if (!running) return;\n setState({ type: \"error\", error: toError(err) });\n } finally {\n running = false;\n }\n }\n\n function scheduleNextPoll(request: AccessRequest, deadline: number): void {\n const delay = pollNudged ? 0 : pollIntervalMs;\n pollNudged = false;\n pollTarget = { request, deadline };\n pollHandle = setTimeoutFn(() => {\n pollHandle = null;\n void poll(request, deadline);\n }, delay);\n }\n\n function requestDeadline(request: AccessRequest): number {\n if (request.expiresAt !== undefined) {\n const expiresAt = Date.parse(request.expiresAt);\n if (Number.isFinite(expiresAt)) return expiresAt;\n }\n return now() + timeoutMs;\n }\n\n /**\n * Enter the polling loop from the given initial state (either\n * `awaiting_approval` for desktop/light or `ready_to_open` for mobile-deep).\n * Errors out immediately if the request has already expired.\n */\n function startPolling(\n request: AccessRequest,\n initialState: DirectConnectState<T>,\n ): void {\n pollNudged = false;\n setState(initialState);\n const deadline = requestDeadline(request);\n if (now() >= deadline) {\n running = false;\n setState({\n type: \"error\",\n error: new Error(\"Access request expired\"),\n });\n return;\n }\n scheduleNextPoll(request, deadline);\n }\n\n async function poll(request: AccessRequest, deadline: number): Promise<void> {\n if (!running) return;\n if (now() >= deadline) {\n running = false;\n setState({\n type: \"error\",\n error: new Error(\"Timed out waiting for approval\"),\n });\n return;\n }\n let status: AccessRequestStatus;\n pollInFlight = true;\n try {\n status = await transports.getStatus(request.requestId);\n } catch (err) {\n if (!running) return;\n running = false;\n setState({ type: \"error\", error: toError(err) });\n return;\n } finally {\n pollInFlight = false;\n }\n if (!running) return;\n\n // A pending deep-mobile status may rotate the continuation ticket. Adopt a\n // fresh, still-valid URL so the rendered \"Open Vana\" link always points at a\n // live ticket; ignore it on the desktop/light path.\n if (status.status === \"pending\" && state.type === \"ready_to_open\") {\n const refreshed = normalizeMobileContinuationUrl(\n status.mobileContinuationUrl,\n );\n if (refreshed && refreshed !== state.mobileContinuationUrl) {\n request = { ...request, mobileContinuationUrl: refreshed };\n setState({\n type: \"ready_to_open\",\n request,\n mobileContinuationUrl: refreshed,\n });\n }\n }\n\n if (isReadReadyStatus(status.status)) {\n clearPoll();\n approvedRequest = request;\n await readAndFinish(request);\n return;\n }\n if (\n status.status === \"completed\" ||\n status.status === \"denied\" ||\n status.status === \"expired\"\n ) {\n // The sheet shows the outcome; it stays open for the person to close.\n sheetHandle?.setRequestStatus(status.status);\n running = false;\n setState({\n type: \"error\",\n error: new Error(`Access request ${status.status}`),\n });\n return;\n }\n scheduleNextPoll(request, deadline);\n }\n\n const flow: DirectConnectFlow<T> = {\n getState() {\n return state;\n },\n\n subscribe(listener: () => void) {\n listeners.add(listener);\n return () => listeners.delete(listener);\n },\n\n async start(): Promise<void> {\n if (running || isRunningPhase()) return;\n running = true;\n // start() deliberately keeps its established first-run semantics: every\n // explicit start creates a request. Only retryRead() may reuse consent.\n approvedRequest = null;\n const runId = ++activeRunId;\n clearSheet();\n if (options.presentation === \"sheet\") {\n await startSheet(runId);\n return;\n }\n // Read the platform policy at start time, like openApprovalWindow below,\n // so a policy swapped in after construction (a React rerender forwards\n // options through a ref) still decides this run's destination.\n const browserPlatform = (\n options.browserPlatformPolicy ?? defaultBrowserPlatformPolicy()\n ).current();\n\n // Desktop preserves the pre-mobile synchronous popup contract: open a\n // blank tab while the click's transient activation is live, then navigate\n // it once createRequest returns the approval URL (BUI-622). Mobile never\n // creates that transient tab; deep requests expose one explicit HTTPS\n // link, while light requests retain the manual approvalUrl fallback.\n // Read the opener option at start time so a swapped-in custom opener is\n // still honored for desktop flows.\n const approvalWindow =\n browserPlatform === \"desktop\"\n ? (options.openApprovalWindow ?? defaultOpenApprovalWindow)()\n : null;\n openedWindow = approvalWindow;\n\n setState({ type: \"creating\" });\n\n let request: AccessRequest;\n try {\n request = await transports.createRequest();\n } catch (err) {\n // If we were superseded (reset, possibly + a newer start()) while this\n // request was in flight, only clean up our own tab — never the shared\n // state or the newer run's window.\n if (runId !== activeRunId) {\n approvalWindow?.close();\n return;\n }\n running = false;\n closeUnnavigatedWindow();\n setState({ type: \"error\", error: toError(err) });\n return;\n }\n if (runId !== activeRunId) {\n approvalWindow?.close();\n return;\n }\n // Re-validate the continuation URL at the SDK boundary (defense in depth\n // for custom transports that bypass the default client).\n request = {\n ...request,\n mobileContinuationUrl: normalizeMobileContinuationUrl(\n request.mobileContinuationUrl,\n ),\n };\n\n // The SDK owns only the small mobile-versus-desktop destination choice.\n // A deep Direct request on mobile carries a validated continuation URL;\n // desktop keeps its popup contract, while mobile light exposes the HTTPS\n // approval URL as the existing manual fallback without opening a tab.\n const mobileContinuationUrl =\n browserPlatform === \"mobile\"\n ? request.mobileContinuationUrl\n : undefined;\n\n if (mobileContinuationUrl) {\n // Do not auto-launch: DCR creation is async, so the original Connect\n // gesture can no longer be trusted to retain iOS user activation. Let\n // the UI render an explicit primary \"Open Vana\" link; polling continues\n // in this tab.\n startPolling(request, {\n type: \"ready_to_open\",\n request,\n mobileContinuationUrl,\n });\n return;\n }\n\n // Desktop/light: navigate the synchronously-opened tab to the HTTPS\n // approval URL. `approvalWindow === null` means the popup was blocked;\n // surface it so the UI renders request.approvalUrl as a visible manual\n // \"Open approval\" link instead of hanging. We poll either way, so a manual\n // open still resolves the flow, and the timeout still bounds the wait.\n if (approvalWindow) {\n approvalWindow.navigate(request.approvalUrl);\n // Hand the tab off to the user; we no longer own/close it.\n openedWindow = null;\n }\n startPolling(request, {\n type: \"awaiting_approval\",\n request,\n popupBlocked: approvalWindow === null,\n });\n },\n\n async retryRead(): Promise<DirectConnectRetryOutcome> {\n if (running || isRunningPhase()) {\n throw new Error(\n \"Cannot retry a read while the connect flow is running\",\n );\n }\n\n const request = approvedRequest;\n const parsedExpiry = request?.expiresAt\n ? Date.parse(request.expiresAt)\n : Number.NaN;\n const requestExpired =\n Number.isFinite(parsedExpiry) && now() >= parsedExpiry;\n\n if (request && !requestExpired) {\n running = true;\n const runId = ++activeRunId;\n let status: AccessRequestStatus;\n try {\n status = await transports.getStatus(request.requestId);\n } catch (err) {\n if (runId !== activeRunId) {\n throw new Error(\"Read retry was superseded\");\n }\n running = false;\n const error = toError(err);\n setState({ type: \"error\", error });\n throw error;\n }\n if (runId !== activeRunId) {\n throw new Error(\"Read retry was superseded\");\n }\n if (isReadReadyStatus(status.status)) {\n await readAndFinish(request);\n return \"retried_existing_grant\";\n }\n running = false;\n }\n\n approvedRequest = null;\n await flow.start();\n return \"fresh_approval_required\";\n },\n\n reset(): void {\n running = false;\n approvedRequest = null;\n // Invalidate any in-flight start() so a late createRequest can't clobber\n // a subsequent run.\n activeRunId++;\n clearPoll();\n closeUnnavigatedWindow();\n clearSheet();\n setState({ type: \"idle\" });\n },\n };\n\n /**\n * `presentation: \"sheet\"`: open Vana's sheet at once and create the request\n * alongside it, so the request is ready by the time the person has signed\n * in. The sheet asks for the request's read capability; the status poll and\n * the read are the tab flow's.\n */\n async function startSheet(runId: number): Promise<void> {\n setState({ type: \"creating\" });\n const fail = (error: unknown) => {\n if (runId !== activeRunId) return;\n clearSheet();\n failRun(toError(error));\n };\n\n const authorizeRequest = transports.authorizeRequest;\n if (!authorizeRequest) {\n fail(\n new Error('presentation: \"sheet\" needs the authorizeRequest transport'),\n );\n return;\n }\n let sheetOptions: DirectConnectSheetOptions;\n try {\n const configured = options.sheet;\n if (!configured) {\n throw new Error('presentation: \"sheet\" needs sheet options');\n }\n sheetOptions =\n typeof configured === \"function\" ? await configured() : configured;\n } catch (err) {\n fail(err);\n return;\n }\n if (runId !== activeRunId) return;\n\n const created = transports.createRequest();\n // Observed below; this keeps an early rejection from going unhandled.\n created.catch(() => undefined);\n sheetCloseAfterDoneMs =\n sheetOptions.closeAfterDoneMs === undefined\n ? DEFAULT_CLOSE_AFTER_DONE_MS\n : sheetOptions.closeAfterDoneMs;\n sheetState = { open: true };\n try {\n sheetHandle = (sheetOptions.open ?? openConnectSheet)({\n clientId: sheetOptions.clientId,\n appName: sheetOptions.appName,\n vanaOrigin: sheetOptions.accountOrigin,\n container: sheetOptions.container,\n readyTimeoutMs: sheetOptions.readyTimeoutMs,\n async getRequest() {\n const request = await created;\n return {\n id: request.requestId,\n authorization: await authorizeRequest(request.requestId),\n };\n },\n onEvent(event) {\n if (runId !== activeRunId) return;\n sheetOptions.onEvent?.(event);\n handleSheetEvent(event);\n },\n });\n } catch {\n // A launcher that cannot open (no DOM, bad origin) is a sheet that\n // cannot run here: fall back to the approval URL.\n sheetHandle = null;\n sheetState = null;\n sheetUnavailable = \"sheet_failed\";\n }\n\n let request: AccessRequest;\n try {\n request = await created;\n } catch (err) {\n fail(err);\n return;\n }\n if (runId !== activeRunId || !running) return;\n startPolling(request, awaitingState(request));\n }\n\n return flow;\n}\n"],"mappings":"AAqBA,SAAS,sCAAsC;AAC/C;AAAA,EACE;AAAA,OAIK;AAsFP,MAAM,uBAAkD;AAAA,EACtD;AAAA,EACA;AAAA,EACA;AACF;AAEA,MAAM,8BAA8B;AA8IpC,MAAM,2BAA2B;AACjC,MAAM,qBAAqB;AAE3B,SAAS,QAAQ,OAAuB;AACtC,SAAO,iBAAiB,QAAQ,QAAQ,IAAI,MAAM,OAAO,KAAK,CAAC;AACjE;AAEA,SAAS,kBAAkB,QAA2C;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