@mdbase-dev/sdk 0.1.0-beta.225 → 0.1.0-beta.227

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.
@@ -17775,7 +17775,9 @@ var AppProtectedInstallationSignIn = class _AppProtectedInstallationSignIn {
17775
17775
  }
17776
17776
  const attempt = async () => {
17777
17777
  check3();
17778
- if (this.state.recoveryFor || typeof requestedCreateCollections !== "boolean" || typeof requestedPrivateDevice !== "boolean") throw fail3("binding");
17778
+ if (typeof requestedCreateCollections !== "boolean" || typeof requestedPrivateDevice !== "boolean") throw fail3("binding");
17779
+ const recovery2 = this.state.recoveryFor;
17780
+ if (recovery2 && (hint !== recovery2.collectionId || requestedCreateCollections || requestedPrivateDevice && !(this.state.requestedPrivateDevice === true && this.state.privateDeviceCollectionIds?.includes(recovery2.collectionId) === true))) throw fail3("binding");
17779
17781
  let r = this.state.consentRequest;
17780
17782
  if (!r || r.completed) {
17781
17783
  const bytes10 = crypto.getRandomValues(new Uint8Array(32));
@@ -17791,7 +17793,7 @@ var AppProtectedInstallationSignIn = class _AppProtectedInstallationSignIn {
17791
17793
  const s = this.state, token = s.token, req = r;
17792
17794
  let reply;
17793
17795
  try {
17794
- reply = await this.consentWindow(req.requestId, () => this.json("/v1/pairing-requests", false, { installation: { app_id: s.appId, request_id: req.requestId, pairing_secret: req.secret, installation_id: s.installationId, device_id: s.deviceId, kind: s.kind, requested_create_collections: req.requestedCreateCollections, ...req.requestedPrivateDevice === true ? { requested_private_device: true } : {}, reconsent: true }, ...hint ? { collection_hint: hint } : {} }, { bearer: token }));
17796
+ reply = await this.consentWindow(req.requestId, () => this.json("/v1/pairing-requests", false, { installation: { app_id: s.appId, request_id: req.requestId, pairing_secret: req.secret, installation_id: s.installationId, device_id: s.deviceId, kind: s.kind, requested_create_collections: req.requestedCreateCollections, ...req.requestedPrivateDevice === true ? { requested_private_device: true } : {}, ...s.recoveryFor ? { recovery_collection_id: s.recoveryFor.collectionId } : {}, reconsent: true }, ...hint ? { collection_hint: hint } : {} }, { bearer: token }));
17795
17797
  } catch (error4) {
17796
17798
  if (req.requestedPrivateDevice !== true || !(error4 instanceof AppInstallationSignInError) || !privateDeviceFieldRefused.has(error4)) throw error4;
17797
17799
  check3();
@@ -17802,7 +17804,8 @@ var AppProtectedInstallationSignIn = class _AppProtectedInstallationSignIn {
17802
17804
  }
17803
17805
  check3();
17804
17806
  const { status, value: v } = reply;
17805
- object2(v, "pairing_id,pairing_secret,verification_uri,expires_in,installation_device,app_id,app_origin,app_name");
17807
+ object2(v, `pairing_id,pairing_secret,verification_uri,expires_in,installation_device,app_id,app_origin,app_name${this.recoveryTargetKeys()}`);
17808
+ this.assertRecoveryTarget(v);
17806
17809
  let uri = null;
17807
17810
  if (status !== 200 && status !== 201 || v.pairing_id !== r.requestId || v.pairing_secret !== r.secret || (uri = verificationUri(v.verification_uri, `${s.cpOrigin}/pair/${r.requestId}`, hint)) === null || v.installation_device !== true || v.app_id !== s.appId || v.app_origin !== s.origin || v.app_name !== this.options.appName || !Number.isSafeInteger(v.expires_in) || v.expires_in < 0 || v.expires_in > 600 || token !== this.state.token) throw fail3("response");
17808
17811
  return Object.freeze({ ...this.collectionConsentView(), verificationUri: uri });
@@ -17829,12 +17832,13 @@ var AppProtectedInstallationSignIn = class _AppProtectedInstallationSignIn {
17829
17832
  const { status, value: v } = await this.consentWindow(r.requestId, () => this.json(`/v1/pairing-requests/${r.requestId}/exchange`, false, void 0, { bearer: r.secret }));
17830
17833
  check3();
17831
17834
  const portal = portalKeys(v);
17832
- const keys2 = `status,request_id,account_id,connector_id,device_id,installation_id,kind,challenge,approval_mode,app_id,app_origin,expires_at${portal}`;
17835
+ const keys2 = `status,request_id,account_id,connector_id,device_id,installation_id,kind,challenge,approval_mode,app_id,app_origin,expires_at${portal}${this.recoveryTargetKeys()}`;
17833
17836
  const done = v.status === "scope_updated";
17834
17837
  const picked = done && (Object.hasOwn(v, "selected_collection_id") || Object.hasOwn(v, "created_collection_ids"));
17835
17838
  const native = done && Object.hasOwn(v, "native_selection");
17836
17839
  const deviceKey = !done ? null : Object.hasOwn(v, "added_private_device_collection_ids") ? "added_private_device_collection_ids" : Object.hasOwn(v, "private_device_collection_ids") ? "private_device_collection_ids" : null;
17837
17840
  object2(v, `${keys2}${done ? ",added_collection_ids,approved_create_collections" : ""}${deviceKey ? `,${deviceKey}` : ""}${picked ? ",selected_collection_id,created_collection_ids" : ""}${native ? ",native_selection" : ""}${done ? timerKeys(v) : ""}`);
17841
+ this.assertRecoveryTarget(v);
17838
17842
  if (!done && (status !== 202 || v.status !== "awaiting_approval") || done && status !== 200 || v.request_id !== r.requestId || v.account_id !== s.selection.accountId || v.connector_id !== s.selection.connectorId || v.device_id !== s.deviceId || v.installation_id !== s.installationId || v.kind !== s.kind || v.app_id !== s.appId || v.app_origin !== s.origin || v.approval_mode !== s.selection.approvalMode || !Number.isSafeInteger(v.expires_at) || v.expires_at < 0) throw fail3("binding");
17839
17843
  const confirmation2 = portalMetadata(v);
17840
17844
  if (r.portalConfirmed && !portal) throw fail3("response");
@@ -17860,7 +17864,9 @@ var AppProtectedInstallationSignIn = class _AppProtectedInstallationSignIn {
17860
17864
  if (v.timer_consent === true && !collectionSelection) throw fail3("response");
17861
17865
  const timerScope = collectionSelection ? [.../* @__PURE__ */ new Set([...addedCollectionIds, collectionSelection.selectedCollectionId])] : addedCollectionIds;
17862
17866
  const reminder = timerConsent(v, r.requestId, timerScope, confirmation2.portalConfirmed === true, collectionSelection?.selectedCollectionId);
17863
- await this.save({ ...s, collectionIds, privateDeviceCollectionIds: privateDeviceCollectionIds.length ? privateDeviceCollectionIds : void 0, nativeSelection, ...nativeSelection || !collectionSelection && addedPrivateIds.length > 0 ? { collectionSelection: void 0 } : collectionSelection ? { collectionSelection } : {}, ...reminder ? { timerConsent: reminder } : {}, createCollections: s.createCollections || v.approved_create_collections, consentRequest: { ...r, ...confirmation2, challenge, completed: true, addedCollectionIds, approvedCreateCollections: v.approved_create_collections } });
17867
+ const nextSelection = nativeSelection || !collectionSelection && addedPrivateIds.length > 0 ? void 0 : collectionSelection ?? s.collectionSelection;
17868
+ if (s.recoveryFor && (native || v.approved_create_collections || !_AppProtectedInstallationSignIn.recovered(s.recoveryFor.collectionId, collectionIds, privateDeviceCollectionIds, nextSelection))) throw fail3("binding");
17869
+ await this.save({ ...s, collectionIds, privateDeviceCollectionIds: privateDeviceCollectionIds.length ? privateDeviceCollectionIds : void 0, nativeSelection, collectionSelection: nextSelection, ...reminder ? { timerConsent: reminder } : {}, createCollections: s.createCollections || v.approved_create_collections, consentRequest: { ...r, ...confirmation2, challenge, completed: true, addedCollectionIds, approvedCreateCollections: v.approved_create_collections } });
17864
17870
  check3();
17865
17871
  return this.collectionConsentView();
17866
17872
  });
@@ -26849,7 +26855,7 @@ var AppCpPeopleAuthority = class {
26849
26855
  var NS = /^[A-Za-z0-9._-]{1,64}$/;
26850
26856
  var ID = /^[A-Za-z0-9._:-]{1,128}$/;
26851
26857
  var MAX2 = 2 * 1024 * 1024;
26852
- var error2 = (code, reason) => mdbaseError(code, "Native timer request could not complete.", reason);
26858
+ var error2 = (code, reason) => mdbaseError(code, "Timer request could not complete.", reason);
26853
26859
  var key = (scope) => JSON.stringify([scope.account, scope.installation, scope.collection]);
26854
26860
  function segment(value2, pattern) {
26855
26861
  if (typeof value2 !== "string" || !pattern.test(value2)) throw error2("invalid_request", "timer_input");
@@ -27073,7 +27079,8 @@ function attachAppTimerReceiver(port, options) {
27073
27079
  return;
27074
27080
  }
27075
27081
  if (!response2.ok) {
27076
- if (response2.status >= 500) throw error2("unavailable", "timer_http");
27082
+ const unavailable6 = !!pending && response2.status === 503 && typeof value2?.error?.code === "string";
27083
+ if (response2.status >= 500 && !unavailable6) throw error2("unavailable", "timer_http");
27077
27084
  if (value2?.error?.operation_outcome === "unknown") throw error2("outcome_unknown", "timer_write_unknown");
27078
27085
  if (pending) {
27079
27086
  await options.journal.write(previousChannel);
@@ -27081,7 +27088,8 @@ function attachAppTimerReceiver(port, options) {
27081
27088
  ctrl.signal.throwIfAborted();
27082
27089
  }
27083
27090
  dispatched = false;
27084
- throw mdbaseError(response2.status === 401 || response2.status === 403 ? "unauthenticated" : response2.status === 409 ? "conflict" : response2.status === 404 ? "upgrade_required" : response2.status === 429 ? "rate_limited" : "invalid_request", "Native timer request was refused.", "timer_refused");
27091
+ if (unavailable6) throw error2("unavailable", "timer_http");
27092
+ throw mdbaseError(response2.status === 401 || response2.status === 403 ? "unauthenticated" : response2.status === 409 ? "conflict" : response2.status === 404 ? "upgrade_required" : response2.status === 429 ? "rate_limited" : "invalid_request", "Timer request was refused.", "timer_refused");
27085
27093
  }
27086
27094
  check3();
27087
27095
  ctrl.signal.throwIfAborted();
@@ -37431,6 +37439,20 @@ var AppWebCloudCopyHost = class _AppWebCloudCopyHost {
37431
37439
  }
37432
37440
  return this.peopleMetadata.view;
37433
37441
  }
37442
+ /** The reminder consent a timer port for `collection` is bound to, or null
37443
+ * when no port would be opened for it. A port stays current only while
37444
+ * this is unchanged, so an open collection compares it to learn that its
37445
+ * ports carry a timer state the consent no longer gives. */
37446
+ timerConsentKey(collection) {
37447
+ const granted = this.timerGrant(collection);
37448
+ return granted ? JSON.stringify(granted.consent) : null;
37449
+ }
37450
+ timerGrant(collection) {
37451
+ const callback = this.source.timerConsent, consent = callback?.call(this.source);
37452
+ if (!consent?.approved) return null;
37453
+ const grant = consent.grants.find((reference) => reference.collectionId === collection);
37454
+ return grant ? { callback, consent, grant } : null;
37455
+ }
37434
37456
  /** SAME Worker host/scope/child lifetime. Only the original chosen receipt
37435
37457
  * selects a fixed native CP receiver; no session or bearer leaves Worker. */
37436
37458
  openTimersPort(options) {
@@ -37438,10 +37460,9 @@ var AppWebCloudCopyHost = class _AppWebCloudCopyHost {
37438
37460
  if (!this.readVerified || !this.logRequest || !this.collectionScope || options.signal.aborted) throw fail15("binding");
37439
37461
  const { scope, generation, signal } = options, original = this.collectionScope;
37440
37462
  if (scope.account !== original.account || scope.installation !== original.installation || scope.collection !== original.collection || uuidToBytes(generation).every((value2) => value2 === 0)) throw fail15("binding");
37441
- const callback = this.source.timerConsent, consent = callback?.call(this.source);
37442
- if (!consent?.approved) return null;
37443
- const grant = consent.grants.find((reference) => reference.collectionId === scope.collection);
37444
- if (!grant) return null;
37463
+ const granted = this.timerGrant(scope.collection);
37464
+ if (!granted) return null;
37465
+ const { callback, consent, grant } = granted;
37445
37466
  const WorkerScope2 = globalThis.WorkerGlobalScope;
37446
37467
  if (typeof WorkerScope2 !== "function" || !(globalThis instanceof WorkerScope2)) throw fail15("binding");
37447
37468
  if (uuidToBytes(grant.grantId).every((value2) => value2 === 0) || !/^[0-9a-f]{64}$/.test(grant.declarationDigest)) throw fail15("binding");
@@ -40724,6 +40745,14 @@ function createAppWebCollectionAdapter(source) {
40724
40745
  let push = null, paused = false, pausing = null, kicked = false;
40725
40746
  let ended = null;
40726
40747
  const attached = /* @__PURE__ */ new Map();
40748
+ const timerKeys2 = /* @__PURE__ */ new Map();
40749
+ const timerKey = () => {
40750
+ try {
40751
+ return host.timerConsentKey(scope.collection);
40752
+ } catch {
40753
+ return void 0;
40754
+ }
40755
+ };
40727
40756
  let problem4 = null;
40728
40757
  const listeners = /* @__PURE__ */ new Set();
40729
40758
  const current2 = () => {
@@ -41181,6 +41210,7 @@ function createAppWebCollectionAdapter(source) {
41181
41210
  const generation = crypto.randomUUID();
41182
41211
  let attachOpened = null;
41183
41212
  let detachOpened = null;
41213
+ let renewOpened = null;
41184
41214
  if (isPrivate) {
41185
41215
  report("syncing");
41186
41216
  const key3 = await host.settlePrivateKey({ signal: life.signal });
@@ -41261,11 +41291,13 @@ function createAppWebCollectionAdapter(source) {
41261
41291
  });
41262
41292
  timers = host.openTimersPort({ scope, generation, signal: life.signal });
41263
41293
  check3();
41294
+ const openedKey = timerKey() ?? null;
41264
41295
  data = new MessageChannel();
41265
41296
  command = new MessageChannel();
41266
41297
  control = controlFor(data.port1, command.port1, setForeground, abort);
41267
41298
  check3();
41268
41299
  const detach = (handoff) => {
41300
+ timerKeys2.delete(handoff.dataPort);
41269
41301
  if (data && command && handoff.dataPort === data.port2) {
41270
41302
  const own = { data, command, control, timers };
41271
41303
  data = null;
@@ -41306,13 +41338,14 @@ function createAppWebCollectionAdapter(source) {
41306
41338
  }
41307
41339
  }
41308
41340
  };
41309
- const attach = () => {
41341
+ const attach = (mode = setupMode === "reconcile-original" ? setupMode : "inspect") => {
41310
41342
  if (!current2() || ended !== null) return null;
41311
41343
  const d = new MessageChannel(), c = new MessageChannel();
41312
41344
  let t = null, ctl = null;
41313
41345
  try {
41314
41346
  t = host.openTimersPort({ scope, generation, signal: life.signal });
41315
41347
  let value2 = null;
41348
+ const key3 = timerKey() ?? null;
41316
41349
  ctl = controlFor(d.port1, c.port1, () => {
41317
41350
  }, () => {
41318
41351
  if (value2) detach(value2);
@@ -41322,13 +41355,14 @@ function createAppWebCollectionAdapter(source) {
41322
41355
  scope,
41323
41356
  generation,
41324
41357
  displayName,
41325
- setupMode: setupMode === "reconcile-original" ? setupMode : "inspect",
41358
+ setupMode: mode,
41326
41359
  dataPort: d.port2,
41327
41360
  controlPort: c.port2,
41328
41361
  ...people,
41329
41362
  ...t ? { timerPort: t.port } : {}
41330
41363
  });
41331
41364
  attached.set(d.port2, { data: d, command: c, control: ctl, timers: t });
41365
+ timerKeys2.set(d.port2, key3);
41332
41366
  return value2;
41333
41367
  } catch {
41334
41368
  try {
@@ -41343,8 +41377,18 @@ function createAppWebCollectionAdapter(source) {
41343
41377
  return null;
41344
41378
  }
41345
41379
  };
41380
+ const renew = (handoff) => {
41381
+ if (!current2() || !timerKeys2.has(handoff.dataPort)) return null;
41382
+ const key3 = timerKey();
41383
+ if (key3 === void 0 || key3 === timerKeys2.get(handoff.dataPort)) return null;
41384
+ const value2 = attach(handoff.setupMode);
41385
+ if (value2) detach(handoff);
41386
+ return value2;
41387
+ };
41346
41388
  attachOpened = attach;
41347
41389
  detachOpened = detach;
41390
+ renewOpened = renew;
41391
+ timerKeys2.set(data.port2, openedKey);
41348
41392
  options.handoff(Object.freeze({
41349
41393
  schema: "mdbn-app-collection/1",
41350
41394
  scope,
@@ -41367,7 +41411,8 @@ function createAppWebCollectionAdapter(source) {
41367
41411
  attach: () => attachOpened ? attachOpened() : null,
41368
41412
  detach: (handoff) => {
41369
41413
  detachOpened?.(handoff);
41370
- }
41414
+ },
41415
+ renewTimers: (handoff) => renewOpened ? renewOpened(handoff) : null
41371
41416
  });
41372
41417
  const unlock = async (secret) => {
41373
41418
  check3();
@@ -42437,14 +42482,16 @@ var ApplicationWorkerHost = class {
42437
42482
  } catch {
42438
42483
  }
42439
42484
  }
42440
- /** Give every tab without ports for its open collection its own. */
42485
+ /** Give every tab without ports for its open collection its own, and new
42486
+ * ones to a tab whose ports were made under a reminder consent that has
42487
+ * since changed (an approval granted reminders while it was open). */
42441
42488
  reconcile() {
42442
42489
  if (this.stopping) return;
42443
42490
  for (const client of [...this.clients.values()]) {
42444
42491
  if (client.gone || client.selected === null) continue;
42445
42492
  const connection = this.connectionOf(client.selected);
42446
- if (!connection?.isCurrent() || typeof connection.attach !== "function" || client.generation === connection.generation) continue;
42447
- const value2 = connection.attach();
42493
+ if (!connection?.isCurrent() || typeof connection.attach !== "function") continue;
42494
+ const value2 = client.generation !== connection.generation ? connection.attach() : client.handoff ? connection.renewTimers?.(client.handoff) ?? null : null;
42448
42495
  if (!value2) continue;
42449
42496
  client.generation = connection.generation;
42450
42497
  client.handoff = value2;
@@ -42797,10 +42844,8 @@ var ApplicationWorkerHost = class {
42797
42844
  break;
42798
42845
  case "select": {
42799
42846
  const id4 = payload;
42800
- if (client.selected !== id4) {
42801
- this.leave(client);
42802
- client.selected = id4;
42803
- }
42847
+ this.leave(client);
42848
+ client.selected = id4;
42804
42849
  try {
42805
42850
  await this.session.openCollection(id4);
42806
42851
  } finally {
@@ -27385,7 +27385,9 @@ ${end.comment}` : end.comment;
27385
27385
  }
27386
27386
  const attempt = async () => {
27387
27387
  check3();
27388
- if (this.state.recoveryFor || typeof requestedCreateCollections !== "boolean" || typeof requestedPrivateDevice !== "boolean") throw fail3("binding");
27388
+ if (typeof requestedCreateCollections !== "boolean" || typeof requestedPrivateDevice !== "boolean") throw fail3("binding");
27389
+ const recovery2 = this.state.recoveryFor;
27390
+ if (recovery2 && (hint !== recovery2.collectionId || requestedCreateCollections || requestedPrivateDevice && !(this.state.requestedPrivateDevice === true && this.state.privateDeviceCollectionIds?.includes(recovery2.collectionId) === true))) throw fail3("binding");
27389
27391
  let r = this.state.consentRequest;
27390
27392
  if (!r || r.completed) {
27391
27393
  const bytes10 = crypto.getRandomValues(new Uint8Array(32));
@@ -27401,7 +27403,7 @@ ${end.comment}` : end.comment;
27401
27403
  const s = this.state, token = s.token, req = r;
27402
27404
  let reply;
27403
27405
  try {
27404
- reply = await this.consentWindow(req.requestId, () => this.json("/v1/pairing-requests", false, { installation: { app_id: s.appId, request_id: req.requestId, pairing_secret: req.secret, installation_id: s.installationId, device_id: s.deviceId, kind: s.kind, requested_create_collections: req.requestedCreateCollections, ...req.requestedPrivateDevice === true ? { requested_private_device: true } : {}, reconsent: true }, ...hint ? { collection_hint: hint } : {} }, { bearer: token }));
27406
+ reply = await this.consentWindow(req.requestId, () => this.json("/v1/pairing-requests", false, { installation: { app_id: s.appId, request_id: req.requestId, pairing_secret: req.secret, installation_id: s.installationId, device_id: s.deviceId, kind: s.kind, requested_create_collections: req.requestedCreateCollections, ...req.requestedPrivateDevice === true ? { requested_private_device: true } : {}, ...s.recoveryFor ? { recovery_collection_id: s.recoveryFor.collectionId } : {}, reconsent: true }, ...hint ? { collection_hint: hint } : {} }, { bearer: token }));
27405
27407
  } catch (error5) {
27406
27408
  if (req.requestedPrivateDevice !== true || !(error5 instanceof AppInstallationSignInError) || !privateDeviceFieldRefused.has(error5)) throw error5;
27407
27409
  check3();
@@ -27412,7 +27414,8 @@ ${end.comment}` : end.comment;
27412
27414
  }
27413
27415
  check3();
27414
27416
  const { status, value: v } = reply;
27415
- object5(v, "pairing_id,pairing_secret,verification_uri,expires_in,installation_device,app_id,app_origin,app_name");
27417
+ object5(v, `pairing_id,pairing_secret,verification_uri,expires_in,installation_device,app_id,app_origin,app_name${this.recoveryTargetKeys()}`);
27418
+ this.assertRecoveryTarget(v);
27416
27419
  let uri = null;
27417
27420
  if (status !== 200 && status !== 201 || v.pairing_id !== r.requestId || v.pairing_secret !== r.secret || (uri = verificationUri(v.verification_uri, `${s.cpOrigin}/pair/${r.requestId}`, hint)) === null || v.installation_device !== true || v.app_id !== s.appId || v.app_origin !== s.origin || v.app_name !== this.options.appName || !Number.isSafeInteger(v.expires_in) || v.expires_in < 0 || v.expires_in > 600 || token !== this.state.token) throw fail3("response");
27418
27421
  return Object.freeze({ ...this.collectionConsentView(), verificationUri: uri });
@@ -27439,12 +27442,13 @@ ${end.comment}` : end.comment;
27439
27442
  const { status, value: v } = await this.consentWindow(r.requestId, () => this.json(`/v1/pairing-requests/${r.requestId}/exchange`, false, void 0, { bearer: r.secret }));
27440
27443
  check3();
27441
27444
  const portal = portalKeys(v);
27442
- const keys2 = `status,request_id,account_id,connector_id,device_id,installation_id,kind,challenge,approval_mode,app_id,app_origin,expires_at${portal}`;
27445
+ const keys2 = `status,request_id,account_id,connector_id,device_id,installation_id,kind,challenge,approval_mode,app_id,app_origin,expires_at${portal}${this.recoveryTargetKeys()}`;
27443
27446
  const done = v.status === "scope_updated";
27444
27447
  const picked = done && (Object.hasOwn(v, "selected_collection_id") || Object.hasOwn(v, "created_collection_ids"));
27445
27448
  const native = done && Object.hasOwn(v, "native_selection");
27446
27449
  const deviceKey = !done ? null : Object.hasOwn(v, "added_private_device_collection_ids") ? "added_private_device_collection_ids" : Object.hasOwn(v, "private_device_collection_ids") ? "private_device_collection_ids" : null;
27447
27450
  object5(v, `${keys2}${done ? ",added_collection_ids,approved_create_collections" : ""}${deviceKey ? `,${deviceKey}` : ""}${picked ? ",selected_collection_id,created_collection_ids" : ""}${native ? ",native_selection" : ""}${done ? timerKeys(v) : ""}`);
27451
+ this.assertRecoveryTarget(v);
27448
27452
  if (!done && (status !== 202 || v.status !== "awaiting_approval") || done && status !== 200 || v.request_id !== r.requestId || v.account_id !== s.selection.accountId || v.connector_id !== s.selection.connectorId || v.device_id !== s.deviceId || v.installation_id !== s.installationId || v.kind !== s.kind || v.app_id !== s.appId || v.app_origin !== s.origin || v.approval_mode !== s.selection.approvalMode || !Number.isSafeInteger(v.expires_at) || v.expires_at < 0) throw fail3("binding");
27449
27453
  const confirmation2 = portalMetadata(v);
27450
27454
  if (r.portalConfirmed && !portal) throw fail3("response");
@@ -27470,7 +27474,9 @@ ${end.comment}` : end.comment;
27470
27474
  if (v.timer_consent === true && !collectionSelection) throw fail3("response");
27471
27475
  const timerScope = collectionSelection ? [.../* @__PURE__ */ new Set([...addedCollectionIds, collectionSelection.selectedCollectionId])] : addedCollectionIds;
27472
27476
  const reminder = timerConsent(v, r.requestId, timerScope, confirmation2.portalConfirmed === true, collectionSelection?.selectedCollectionId);
27473
- await this.save({ ...s, collectionIds, privateDeviceCollectionIds: privateDeviceCollectionIds.length ? privateDeviceCollectionIds : void 0, nativeSelection, ...nativeSelection || !collectionSelection && addedPrivateIds.length > 0 ? { collectionSelection: void 0 } : collectionSelection ? { collectionSelection } : {}, ...reminder ? { timerConsent: reminder } : {}, createCollections: s.createCollections || v.approved_create_collections, consentRequest: { ...r, ...confirmation2, challenge, completed: true, addedCollectionIds, approvedCreateCollections: v.approved_create_collections } });
27477
+ const nextSelection = nativeSelection || !collectionSelection && addedPrivateIds.length > 0 ? void 0 : collectionSelection ?? s.collectionSelection;
27478
+ if (s.recoveryFor && (native || v.approved_create_collections || !_AppProtectedInstallationSignIn.recovered(s.recoveryFor.collectionId, collectionIds, privateDeviceCollectionIds, nextSelection))) throw fail3("binding");
27479
+ await this.save({ ...s, collectionIds, privateDeviceCollectionIds: privateDeviceCollectionIds.length ? privateDeviceCollectionIds : void 0, nativeSelection, collectionSelection: nextSelection, ...reminder ? { timerConsent: reminder } : {}, createCollections: s.createCollections || v.approved_create_collections, consentRequest: { ...r, ...confirmation2, challenge, completed: true, addedCollectionIds, approvedCreateCollections: v.approved_create_collections } });
27474
27480
  check3();
27475
27481
  return this.collectionConsentView();
27476
27482
  });
@@ -39355,7 +39361,8 @@ ${end.comment}` : end.comment;
39355
39361
  return;
39356
39362
  }
39357
39363
  if (!response2.ok) {
39358
- if (response2.status >= 500) throw error3("unavailable", "timer_http");
39364
+ const unavailable9 = !!pending && response2.status === 503 && typeof value2?.error?.code === "string";
39365
+ if (response2.status >= 500 && !unavailable9) throw error3("unavailable", "timer_http");
39359
39366
  if (value2?.error?.operation_outcome === "unknown") throw error3("outcome_unknown", "timer_write_unknown");
39360
39367
  if (pending) {
39361
39368
  await options.journal.write(previousChannel);
@@ -39363,7 +39370,8 @@ ${end.comment}` : end.comment;
39363
39370
  ctrl.signal.throwIfAborted();
39364
39371
  }
39365
39372
  dispatched = false;
39366
- throw mdbaseError(response2.status === 401 || response2.status === 403 ? "unauthenticated" : response2.status === 409 ? "conflict" : response2.status === 404 ? "upgrade_required" : response2.status === 429 ? "rate_limited" : "invalid_request", "Native timer request was refused.", "timer_refused");
39373
+ if (unavailable9) throw error3("unavailable", "timer_http");
39374
+ throw mdbaseError(response2.status === 401 || response2.status === 403 ? "unauthenticated" : response2.status === 409 ? "conflict" : response2.status === 404 ? "upgrade_required" : response2.status === 429 ? "rate_limited" : "invalid_request", "Timer request was refused.", "timer_refused");
39367
39375
  }
39368
39376
  check3();
39369
39377
  ctrl.signal.throwIfAborted();
@@ -39438,7 +39446,7 @@ ${end.comment}` : end.comment;
39438
39446
  slot = null;
39439
39447
  pending.signal?.removeEventListener("abort", pending.abort);
39440
39448
  if (frame[4]) pending.resolve(frame[5]);
39441
- else pending.reject(mdbaseError(frame[5], "Native timer request could not complete.", typeof frame[6] === "string" ? frame[6] : void 0));
39449
+ else pending.reject(mdbaseError(frame[5], "Timer request could not complete.", typeof frame[6] === "string" ? frame[6] : void 0));
39442
39450
  };
39443
39451
  const run2 = (verb, args, request3, channel = false) => {
39444
39452
  if (closed || options.signal.aborted || request3.signal?.aborted) return Promise.reject(error3("cancelled", "timer_port_closed"));
@@ -39567,7 +39575,7 @@ ${end.comment}` : end.comment;
39567
39575
  NS2 = /^[A-Za-z0-9._-]{1,64}$/;
39568
39576
  ID3 = /^[A-Za-z0-9._:-]{1,128}$/;
39569
39577
  MAX4 = 2 * 1024 * 1024;
39570
- error3 = (code, reason) => mdbaseError(code, "Native timer request could not complete.", reason);
39578
+ error3 = (code, reason) => mdbaseError(code, "Timer request could not complete.", reason);
39571
39579
  key = (scope) => JSON.stringify([scope.account, scope.installation, scope.collection]);
39572
39580
  }
39573
39581
  });
@@ -44960,6 +44968,20 @@ ${end.comment}` : end.comment;
44960
44968
  }
44961
44969
  return this.peopleMetadata.view;
44962
44970
  }
44971
+ /** The reminder consent a timer port for `collection` is bound to, or null
44972
+ * when no port would be opened for it. A port stays current only while
44973
+ * this is unchanged, so an open collection compares it to learn that its
44974
+ * ports carry a timer state the consent no longer gives. */
44975
+ timerConsentKey(collection2) {
44976
+ const granted = this.timerGrant(collection2);
44977
+ return granted ? JSON.stringify(granted.consent) : null;
44978
+ }
44979
+ timerGrant(collection2) {
44980
+ const callback = this.source.timerConsent, consent = callback?.call(this.source);
44981
+ if (!consent?.approved) return null;
44982
+ const grant = consent.grants.find((reference) => reference.collectionId === collection2);
44983
+ return grant ? { callback, consent, grant } : null;
44984
+ }
44963
44985
  /** SAME Worker host/scope/child lifetime. Only the original chosen receipt
44964
44986
  * selects a fixed native CP receiver; no session or bearer leaves Worker. */
44965
44987
  openTimersPort(options) {
@@ -44967,10 +44989,9 @@ ${end.comment}` : end.comment;
44967
44989
  if (!this.readVerified || !this.logRequest || !this.collectionScope || options.signal.aborted) throw fail16("binding");
44968
44990
  const { scope, generation, signal } = options, original = this.collectionScope;
44969
44991
  if (scope.account !== original.account || scope.installation !== original.installation || scope.collection !== original.collection || uuidToBytes(generation).every((value2) => value2 === 0)) throw fail16("binding");
44970
- const callback = this.source.timerConsent, consent = callback?.call(this.source);
44971
- if (!consent?.approved) return null;
44972
- const grant = consent.grants.find((reference) => reference.collectionId === scope.collection);
44973
- if (!grant) return null;
44992
+ const granted = this.timerGrant(scope.collection);
44993
+ if (!granted) return null;
44994
+ const { callback, consent, grant } = granted;
44974
44995
  const WorkerScope = globalThis.WorkerGlobalScope;
44975
44996
  if (typeof WorkerScope !== "function" || !(globalThis instanceof WorkerScope)) throw fail16("binding");
44976
44997
  if (uuidToBytes(grant.grantId).every((value2) => value2 === 0) || !/^[0-9a-f]{64}$/.test(grant.declarationDigest)) throw fail16("binding");
@@ -45758,6 +45779,14 @@ ${end.comment}` : end.comment;
45758
45779
  let push = null, paused = false, pausing = null, kicked = false;
45759
45780
  let ended = null;
45760
45781
  const attached = /* @__PURE__ */ new Map();
45782
+ const timerKeys2 = /* @__PURE__ */ new Map();
45783
+ const timerKey = () => {
45784
+ try {
45785
+ return host.timerConsentKey(scope.collection);
45786
+ } catch {
45787
+ return void 0;
45788
+ }
45789
+ };
45761
45790
  let problem7 = null;
45762
45791
  const listeners = /* @__PURE__ */ new Set();
45763
45792
  const current3 = () => {
@@ -46215,6 +46244,7 @@ ${end.comment}` : end.comment;
46215
46244
  const generation = crypto.randomUUID();
46216
46245
  let attachOpened = null;
46217
46246
  let detachOpened = null;
46247
+ let renewOpened = null;
46218
46248
  if (isPrivate) {
46219
46249
  report("syncing");
46220
46250
  const key3 = await host.settlePrivateKey({ signal: life.signal });
@@ -46295,11 +46325,13 @@ ${end.comment}` : end.comment;
46295
46325
  });
46296
46326
  timers = host.openTimersPort({ scope, generation, signal: life.signal });
46297
46327
  check3();
46328
+ const openedKey = timerKey() ?? null;
46298
46329
  data = new MessageChannel();
46299
46330
  command = new MessageChannel();
46300
46331
  control = controlFor(data.port1, command.port1, setForeground, abort);
46301
46332
  check3();
46302
46333
  const detach = (handoff) => {
46334
+ timerKeys2.delete(handoff.dataPort);
46303
46335
  if (data && command && handoff.dataPort === data.port2) {
46304
46336
  const own2 = { data, command, control, timers };
46305
46337
  data = null;
@@ -46340,13 +46372,14 @@ ${end.comment}` : end.comment;
46340
46372
  }
46341
46373
  }
46342
46374
  };
46343
- const attach = () => {
46375
+ const attach = (mode = setupMode === "reconcile-original" ? setupMode : "inspect") => {
46344
46376
  if (!current3() || ended !== null) return null;
46345
46377
  const d = new MessageChannel(), c = new MessageChannel();
46346
46378
  let t = null, ctl = null;
46347
46379
  try {
46348
46380
  t = host.openTimersPort({ scope, generation, signal: life.signal });
46349
46381
  let value2 = null;
46382
+ const key3 = timerKey() ?? null;
46350
46383
  ctl = controlFor(d.port1, c.port1, () => {
46351
46384
  }, () => {
46352
46385
  if (value2) detach(value2);
@@ -46356,13 +46389,14 @@ ${end.comment}` : end.comment;
46356
46389
  scope,
46357
46390
  generation,
46358
46391
  displayName,
46359
- setupMode: setupMode === "reconcile-original" ? setupMode : "inspect",
46392
+ setupMode: mode,
46360
46393
  dataPort: d.port2,
46361
46394
  controlPort: c.port2,
46362
46395
  ...people,
46363
46396
  ...t ? { timerPort: t.port } : {}
46364
46397
  });
46365
46398
  attached.set(d.port2, { data: d, command: c, control: ctl, timers: t });
46399
+ timerKeys2.set(d.port2, key3);
46366
46400
  return value2;
46367
46401
  } catch {
46368
46402
  try {
@@ -46377,8 +46411,18 @@ ${end.comment}` : end.comment;
46377
46411
  return null;
46378
46412
  }
46379
46413
  };
46414
+ const renew = (handoff) => {
46415
+ if (!current3() || !timerKeys2.has(handoff.dataPort)) return null;
46416
+ const key3 = timerKey();
46417
+ if (key3 === void 0 || key3 === timerKeys2.get(handoff.dataPort)) return null;
46418
+ const value2 = attach(handoff.setupMode);
46419
+ if (value2) detach(handoff);
46420
+ return value2;
46421
+ };
46380
46422
  attachOpened = attach;
46381
46423
  detachOpened = detach;
46424
+ renewOpened = renew;
46425
+ timerKeys2.set(data.port2, openedKey);
46382
46426
  options.handoff(Object.freeze({
46383
46427
  schema: "mdbn-app-collection/1",
46384
46428
  scope,
@@ -46401,7 +46445,8 @@ ${end.comment}` : end.comment;
46401
46445
  attach: () => attachOpened ? attachOpened() : null,
46402
46446
  detach: (handoff) => {
46403
46447
  detachOpened?.(handoff);
46404
- }
46448
+ },
46449
+ renewTimers: (handoff) => renewOpened ? renewOpened(handoff) : null
46405
46450
  });
46406
46451
  const unlock = async (secret) => {
46407
46452
  check3();