@oddington/sdk 0.1.52 → 0.1.53

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.
package/README.md CHANGED
@@ -265,17 +265,28 @@ uses the CLI-backed app connection and presents the signed-in viewer:
265
265
 
266
266
  ```ts
267
267
  const { status, account } = browserAuth.getState();
268
- if (status === "signed_in") {
268
+ if ((status === "signed_in" || status === "reconnecting") && account) {
269
269
  // account.email and account.displayName identify this app's current viewer.
270
+ // Keep viewer presentation while reconnecting; protected access awaits renewal.
270
271
  }
271
272
  ```
272
273
 
273
274
  Use this account for viewer-aware presentation such as a Mine filter. Subscribe
274
275
  to keep that presentation current; it is not an authorization decision. There
275
276
  is no Inbox identity call, extra hosted lookup, browser provider session or CLI
276
- token. The handle and `os.init` share one exact-origin connection. Expiry clears
277
- account presentation, and same-session renewal restores it. Local sign-out ends
278
- the host session for its browsers; a failed confirmation reports unavailable.
277
+ token. The handle and `os.init` share one exact-origin connection. When an
278
+ established viewer's short access proof expires, the handle reports `reconnecting`
279
+ and retains the confirmed account for presentation while the SDK begins or joins
280
+ one shared, bounded recovery of the same session. No protected operation is
281
+ admitted before renewal succeeds. Confirmed connection and account recovery
282
+ restore `signed_in`; failed or unconfirmed recovery settles to `unavailable`.
283
+ A later lifecycle event or protected operation may recover the still-eligible
284
+ same session from `unavailable`; the SDK does not start an automatic retry loop
285
+ after an exhausted attempt. Local handles never report `signed_out` or `expired`.
286
+ Local sign-out immediately reports `unavailable` without an account, closes document
287
+ access and requests host-wide sign-out. Confirmed sign-out resolves while remaining
288
+ `unavailable`; failed confirmation rejects while remaining `unavailable`. Reopen
289
+ the app through OS after local sign-out.
279
290
  Disposal preserves the connection's stored proofs for document refresh. Local
280
291
  passcode and consent actions are unavailable: sign in with `os login` first.
281
292
 
@@ -297,7 +308,7 @@ provider internally through exact-origin `GET /api/auth`, owned by OS. The SDK
297
308
  uses same-origin credentials, refuses redirects and bypasses caching. Missing or
298
309
  invalid setup makes authentication unavailable without a legacy endpoint or
299
310
  app-configured provider fallback; ordinary app modules do not consume this
300
- internal bootstrap. State is `initializing`, `signed_out`, `signed_in`, `expired`
311
+ internal bootstrap. Hosted state is `initializing`, `signed_out`, `signed_in`, `expired`
301
312
  or `unavailable`; actions complete without returning a session. Invitation-only
302
313
  code delivery never creates accounts or reveals account existence. Keep form
303
314
  selection, entered values, copy and navigation in the app. The handle exposes no
package/dist/browser.js CHANGED
@@ -1607,6 +1607,18 @@ function mapping(value) {
1607
1607
  return invalidChange();
1608
1608
  }
1609
1609
  }
1610
+ function captureDataChangeOptions(value) {
1611
+ if (value === void 0) return {};
1612
+ let options = mapping(value);
1613
+ if (Object.keys(options).some((key) => !["summary", "source"].includes(key))) return invalidChange();
1614
+ let line = (value2) => !dataText(value2) || !value2.trim().length || [...value2].length > 120 || /[\u0000-\u001f\u007f-\u009f\u2028\u2029]/u.test(value2) ? invalidChange() : value2, captured = {};
1615
+ if (Object.hasOwn(options, "summary") && (captured.summary = line(options.summary)), Object.hasOwn(options, "source")) {
1616
+ let source2 = mapping(options.source);
1617
+ if (Object.keys(source2).sort().join(",") !== "id,kind" || source2.kind !== "app" && source2.kind !== "routine") return invalidChange();
1618
+ captured.source = { kind: source2.kind, id: line(source2.id) };
1619
+ }
1620
+ return captured;
1621
+ }
1610
1622
  function captureValue(value, ancestors = /* @__PURE__ */ new Set(), depth = 0) {
1611
1623
  if (value === null || typeof value == "string" || typeof value == "boolean" || typeof value == "number" && Number.isFinite(value)) return value;
1612
1624
  if (!value || typeof value != "object" || ancestors.has(value) || depth > 32) return invalidChange();
@@ -1883,11 +1895,11 @@ var genericData = {
1883
1895
  all: async (query, options) => send3({ operation: "records.all", ...captureRecordsAll(query, options) }),
1884
1896
  search: async (query, options) => send3({ operation: "records.search", ...captureRecordsSearchInput(query, options) })
1885
1897
  },
1886
- changes() {
1887
- let updates = [], creates = [], deletes = [], applyId, method, pending, confirmed, assertMutable = () => {
1898
+ changes(options) {
1899
+ let metadata = captureDataChangeOptions(options), updates = [], creates = [], deletes = [], applyId, method, pending, confirmed, assertMutable = () => {
1888
1900
  if (applyId) throw new DataOperationError("invalid_change", !1, "unapplied");
1889
1901
  }, snapshot = (nextUpdates = updates, nextCreates = creates, nextDeletes = deletes) => {
1890
- let { updates: updates2, creates: creates2, deletes: deletes2 } = captureDataChanges(nextUpdates, nextCreates, nextDeletes), input = { updates: updates2, ...creates2.length ? { creates: creates2 } : {}, ...deletes2.length ? { deletes: deletes2 } : {} };
1902
+ let { updates: updates2, creates: creates2, deletes: deletes2 } = captureDataChanges(nextUpdates, nextCreates, nextDeletes), input = { ...metadata, updates: updates2, ...creates2.length ? { creates: creates2 } : {}, ...deletes2.length ? { deletes: deletes2 } : {} };
1891
1903
  return checkDataRequestSize({
1892
1904
  repository,
1893
1905
  operation: "changes.apply",
@@ -2921,7 +2933,7 @@ function parseInboxRecords(value) {
2921
2933
 
2922
2934
  var InboxOperationError = class extends Error {
2923
2935
  constructor(status, reason) {
2924
- super(reason === "handler_unavailable" ? "Deploy the routine that owns this question\u2019s response handler." : reason === "conflict" ? "This question already has an accepted answer" : reason === "unavailable" ? "Inbox request unavailable. Retry the same action." : reason === "invalid_input" ? "Invalid Inbox request" : "Inbox access unavailable");
2936
+ super(reason === "action_processing" ? "The previous answer is still processing. Wait for it to finish." : reason === "action_failed" ? "The previous answer could not be completed. Further answers are on hold." : reason === "handler_unavailable" ? "Deploy the routine that owns this question\u2019s response handler." : reason === "conflict" ? "This question already has an accepted answer" : reason === "unavailable" ? "Inbox request unavailable. Retry the same action." : reason === "invalid_input" ? "Invalid Inbox request" : "Inbox access unavailable");
2925
2937
  this.status = status;
2926
2938
  this.reason = reason;
2927
2939
  }
@@ -2939,8 +2951,16 @@ function questionAvailability(value, available = !1) {
2939
2951
  }
2940
2952
  function parseInboxUnavailableFailure(value, status) {
2941
2953
  try {
2942
- if (status !== 503) return;
2943
2954
  let envelope = object4(value);
2955
+ if (status === 409) {
2956
+ keys3(envelope, ["error"]);
2957
+ for (let reason of ["action_processing", "action_failed"]) {
2958
+ let error = new InboxOperationError(409, reason);
2959
+ if (envelope.error === error.message) return error;
2960
+ }
2961
+ return;
2962
+ }
2963
+ if (status !== 503) return;
2944
2964
  keys3(envelope, ["error", "unavailableReason"], ["error", "unavailableReason"]), assert(text4(envelope.error));
2945
2965
  let state = questionAvailability(envelope);
2946
2966
  return new InboxOperationError(503, state.unavailableReason);
@@ -3445,7 +3465,9 @@ function appContext(origin, active2, changed) {
3445
3465
  async function renew(initialConnection = !1) {
3446
3466
  if (renewal) return renewal;
3447
3467
  if (!binding || !pair || !alive()) throw new Error();
3448
- renewal = (async () => {
3468
+ renewal = Promise.resolve().then(async () => {
3469
+ if (!alive()) throw new Error();
3470
+ clearTimeout(renewalTimer), binding.expiresAt <= Date.now() && notify("recovering");
3449
3471
  let requestController = new AbortController(), abort = () => requestController.abort();
3450
3472
  controller.signal.addEventListener("abort", abort, { once: !0 });
3451
3473
  let timeout = setTimeout(abort, 2e4), previous = serialized;
@@ -3485,17 +3507,17 @@ function appContext(origin, active2, changed) {
3485
3507
  }
3486
3508
  } catch {
3487
3509
  }
3488
- throw (error instanceof AppRenewalUnavailable || error instanceof AppResponseReadError || requestController.signal.aborted) && alive() ? current2() && (renewalTimer = setTimeout(
3510
+ throw (error instanceof AppRenewalUnavailable || error instanceof AppResponseReadError || requestController.signal.aborted) && alive() ? current2() ? renewalTimer = setTimeout(
3489
3511
  () => {
3490
3512
  renew().catch(() => {
3491
3513
  });
3492
3514
  },
3493
3515
  Math.min(5e3, (binding.expiresAt - Date.now()) / 5)
3494
- )) : invalidate(), new AppRenewalUnavailable();
3516
+ ) : notify("unavailable") : invalidate(), new AppRenewalUnavailable();
3495
3517
  } finally {
3496
3518
  clearTimeout(timeout), controller.signal.removeEventListener("abort", abort);
3497
3519
  }
3498
- })();
3520
+ });
3499
3521
  try {
3500
3522
  await renewal;
3501
3523
  } finally {
@@ -3684,7 +3706,8 @@ function appContext(origin, active2, changed) {
3684
3706
  return Object.freeze({ email: value.account.email, displayName: value.account.displayName });
3685
3707
  },
3686
3708
  signOut: async () => {
3687
- if (current2() || await resolve(), !current2()) throw new Error("Local app context unavailable.");
3709
+ if (!alive() || !credential)
3710
+ throw invalidate(), new Error("Local app context unavailable.");
3688
3711
  let signal = AbortSignal.timeout(2e4), pending = send3("sign-out", credential, signal, { method: "POST" });
3689
3712
  invalidate("signed_out");
3690
3713
  let response2 = await pending, value = await appResponse(response2, 4096, signal);
@@ -4017,7 +4040,7 @@ function refresh(c) {
4017
4040
  let promise = (async () => {
4018
4041
  try {
4019
4042
  let account = await c.context.account();
4020
- c.active && !c.closed && c.revision === revision && publish(c, { status: "signed_in", account });
4043
+ c.active && !c.closed && c.revision === revision && (c.account = account, publish(c, { status: "signed_in", account }));
4021
4044
  } catch {
4022
4045
  c.active && !c.closed && c.revision === revision && publish(c, { status: "unavailable", error: "unavailable" });
4023
4046
  }
@@ -4040,7 +4063,22 @@ function createLocalBrowserAuth(origin, failure) {
4040
4063
  ready: Promise.resolve()
4041
4064
  };
4042
4065
  coordinator2.context = appContext(origin, () => coordinator2.active, (state) => {
4043
- !coordinator2.active || coordinator2.closed || (state === "connected" ? coordinator2.state.status !== "initializing" && (coordinator2.revision++, refresh(coordinator2)) : (coordinator2.revision++, publish(coordinator2, { status: state === "expired" ? "expired" : state === "signed_out" ? "signed_out" : "unavailable" })));
4066
+ if (!(!coordinator2.active || coordinator2.closed))
4067
+ if (state === "connected")
4068
+ coordinator2.state.status !== "initializing" && (coordinator2.revision++, refresh(coordinator2));
4069
+ else if (state === "expired" || state === "recovering") {
4070
+ if (!coordinator2.account) return;
4071
+ let revision = ++coordinator2.revision;
4072
+ publish(coordinator2, { status: "reconnecting", account: coordinator2.account }), state === "expired" && Promise.resolve().then(async () => {
4073
+ if (!(!coordinator2.active || coordinator2.closed || coordinator2.revision !== revision))
4074
+ try {
4075
+ await coordinator2.context.resolve();
4076
+ } catch {
4077
+ coordinator2.active && !coordinator2.closed && coordinator2.revision === revision && publish(coordinator2, { status: "unavailable", error: "unavailable" });
4078
+ }
4079
+ });
4080
+ } else
4081
+ coordinator2.revision++, state === "signed_out" && (coordinator2.account = void 0), publish(coordinator2, { status: "unavailable" });
4044
4082
  }), c = coordinator2, coordinators.set(origin, c), c.ready = Promise.resolve().then(() => refresh(coordinator2));
4045
4083
  }
4046
4084
  let coordinator = c;
@@ -4057,7 +4095,7 @@ function createLocalBrowserAuth(origin, failure) {
4057
4095
  let assertActive = () => {
4058
4096
  if (!active2) throw failure("disposed");
4059
4097
  }, unavailable5 = async () => Object.freeze({
4060
- status: active2 ? coordinator.state.status === "signed_out" ? "signed_out" : "unavailable" : "disposed"
4098
+ status: active2 ? "unavailable" : "disposed"
4061
4099
  }), handle2 = Object.freeze({
4062
4100
  authorization: Object.freeze({ get: unavailable5, approve: unavailable5, deny: unavailable5 }),
4063
4101
  getState() {
@@ -4077,13 +4115,13 @@ function createLocalBrowserAuth(origin, failure) {
4077
4115
  async signOut() {
4078
4116
  if (assertActive(), coordinator.state.pending === "sign_out") throw failure("busy");
4079
4117
  if (coordinator.closed) throw failure("unavailable");
4080
- coordinator.closed = !0, coordinator.revision++, publish(coordinator, { status: coordinator.state.status, pending: "sign_out" });
4118
+ coordinator.closed = !0, coordinator.revision++, coordinator.account = void 0, publish(coordinator, { status: "unavailable", pending: "sign_out" });
4081
4119
  try {
4082
4120
  await coordinator.context.signOut();
4083
4121
  } catch {
4084
4122
  throw coordinator.context.invalidate(), coordinator.active && publish(coordinator, { status: "unavailable", error: "unavailable" }), failure(active2 ? "unavailable" : "disposed");
4085
4123
  }
4086
- coordinator.active && publish(coordinator, { status: "signed_out" }), assertActive();
4124
+ coordinator.active && publish(coordinator, { status: "unavailable" }), assertActive();
4087
4125
  },
4088
4126
  dispose() {
4089
4127
  if (active2) {