@mdbase-dev/sdk 0.1.40 → 0.2.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.
@@ -7017,7 +7017,7 @@ var MdbaseSDK = (() => {
7017
7017
  init_codec();
7018
7018
  init_wire();
7019
7019
  CHANGES_PAGE = 1e3;
7020
- DEVICE_WAITS = /* @__PURE__ */ new Set(["no_device_online", "not_served"]);
7020
+ DEVICE_WAITS = /* @__PURE__ */ new Set(["no_device_online", "not_served", "device_not_serving"]);
7021
7021
  VALIDATE_PAGE = "validate-page-v1";
7022
7022
  BASES_DISCOVERY_UNREADABLE = "bases-discovery-unreadable-v1";
7023
7023
  Write = class {
@@ -9400,6 +9400,7 @@ var MdbaseSDK = (() => {
9400
9400
  init_errors();
9401
9401
  COLLECTION_UNAVAILABLE_REASONS = [
9402
9402
  "device_offline",
9403
+ "device_not_serving",
9403
9404
  "desktop_update_required",
9404
9405
  "old_connector_running",
9405
9406
  "approval_required",
@@ -9981,6 +9982,15 @@ var MdbaseSDK = (() => {
9981
9982
  application_connection_unavailable: "unavailable",
9982
9983
  readiness_unavailable: "unavailable",
9983
9984
  grant_activation_timeout: "unavailable",
9985
+ // Relay pipe closes (pipeCloseError), published as a Private snapshot's problem. A
9986
+ // close without a known reason names no cause: an intermediary can strip the close
9987
+ // status of a real refusal (it arrives as 1005).
9988
+ relay_closed: "unavailable",
9989
+ pipe_closed: "unavailable",
9990
+ authorization_unavailable: "unavailable",
9991
+ grant_lease_expired: "unavailable",
9992
+ grant_inactive: "forbidden",
9993
+ connector_busy: "rate_limited",
9984
9994
  application_connection_people_unavailable: "people_access",
9985
9995
  people_unavailable: "people_access",
9986
9996
  people_response: "people_access",
@@ -10014,7 +10024,7 @@ var MdbaseSDK = (() => {
10014
10024
  NON_PROBLEMS = new Set(NON_PROBLEM_CODES);
10015
10025
  DEVELOPER = Object.assign(/* @__PURE__ */ Object.create(null), {
10016
10026
  device_offline: "Collection availability device_offline: no device serving this Private collection is connected to the relay.",
10017
- device_not_serving: "Relay close not_served: the device holds a current grant for this Private collection but is not serving it (paused, opening, recovering or failed). Check the collection's status in mdbase on that computer; connect with waitForDevice keeps retrying.",
10027
+ device_not_serving: "Collection availability device_not_serving / relay close not_served: the device is online but is not serving this Private collection right now (paused, opening, recovering, failed, or removed on that computer). Check the collection's status in mdbase on that computer; connect with waitForDevice keeps retrying.",
10018
10028
  desktop_update_required: "Collection availability desktop_update_required: the serving device's last relay connection was refused as incompatible.",
10019
10029
  old_connector_running: "Collection availability old_connector_running: the serving device reports a legacy connector still holding its collections.",
10020
10030
  device_approval_required: "Collection availability approval_required: no device has signed an approval for this app's grant yet.",
@@ -36878,6 +36888,9 @@ ${end.comment}` : end.comment;
36878
36888
  });
36879
36889
 
36880
36890
  // src/transport/relay-pipe.ts
36891
+ function pipeCloseCode(reason) {
36892
+ return typeof reason === "string" && Object.hasOwn(PIPE_CLOSE_CODES, reason) ? PIPE_CLOSE_CODES[reason] : 4e3;
36893
+ }
36881
36894
  function pipeCloseError(code, reason) {
36882
36895
  const r = typeof reason === "string" && CLOSE_REASONS.has(reason) ? reason : void 0;
36883
36896
  const close = relayCloseDiagnostic(code);
@@ -37002,7 +37015,7 @@ ${end.comment}` : end.comment;
37002
37015
  signal?.removeEventListener("abort", onAbort);
37003
37016
  resolve2(carrier);
37004
37017
  } else if (msg.type === "pipe_close") {
37005
- fail20(pipeCloseError(4e3, msg.reason));
37018
+ fail20(pipeCloseError(pipeCloseCode(msg.reason), msg.reason));
37006
37019
  ws.close();
37007
37020
  }
37008
37021
  return;
@@ -37037,7 +37050,7 @@ ${end.comment}` : end.comment;
37037
37050
  ws.onclose = (ev) => fail20(pipeCloseError(ev.code, ev.reason));
37038
37051
  });
37039
37052
  }
37040
- var MAX_PIPE_FRAME, CLOSE_REASONS;
37053
+ var MAX_PIPE_FRAME, CLOSE_REASONS, PIPE_CLOSE_CODES;
37041
37054
  var init_relay_pipe = __esm({
37042
37055
  "src/transport/relay-pipe.ts"() {
37043
37056
  "use strict";
@@ -37047,6 +37060,15 @@ ${end.comment}` : end.comment;
37047
37060
  init_cbor();
37048
37061
  MAX_PIPE_FRAME = 4 + 65535;
37049
37062
  CLOSE_REASONS = new Set("unauthenticated grant_inactive connector_offline connector_busy device_mismatch device_key_mismatch closed client_closed handshake_timeout idle lifetime frame_too_large invalid_frame not_served relay_closing authorization_unavailable grant_revoked grant_lease_expired grant_pending_approval grant_revoked_on_device unknown_grant".split(" "));
37063
+ PIPE_CLOSE_CODES = Object.assign(/* @__PURE__ */ Object.create(null), {
37064
+ unauthenticated: 4401,
37065
+ grant_inactive: 4403,
37066
+ grant_revoked_on_device: 4403,
37067
+ grant_pending_approval: 4403,
37068
+ unknown_grant: 4403,
37069
+ connector_offline: 4404,
37070
+ connector_busy: 4429
37071
+ });
37050
37072
  }
37051
37073
  });
37052
37074
 
@@ -37151,55 +37173,76 @@ ${end.comment}` : end.comment;
37151
37173
  const usable = route2.targets.filter(
37152
37174
  (t) => UUID9.test(t.device) && /^[0-9a-f]{64}$/.test(t.noise_pk) && !!t.relay_collection && UUID9.test(t.relay_collection)
37153
37175
  );
37154
- const stuck = usable.filter((t) => t.device === sticky);
37155
- if (stuck.length) return stuck;
37156
- return [...usable.filter((t) => t.online === true), ...usable.filter((t) => t.online !== true)];
37176
+ const rank = (t) => {
37177
+ const tier2 = t.online === true ? 0 : t.online === void 0 ? 1 : 2;
37178
+ return tier2 * 2 + (t.device === sticky ? 0 : 1);
37179
+ };
37180
+ return usable.map((target2, index) => ({ target: target2, index, rank: rank(target2) })).sort((a, b) => a.rank - b.rank || a.index - b.index).map(({ target: target2 }) => target2);
37181
+ }
37182
+ function attemptTargets(route2, sticky) {
37183
+ const ordered = orderTargets(route2, sticky);
37184
+ const online = ordered.filter((t) => t.online === true);
37185
+ return online.length ? online : ordered.slice(0, 1);
37186
+ }
37187
+ function isRetryableTargetError(error5, signal) {
37188
+ return !signal?.aborted && isMdbaseError(error5) && (error5.code === "unavailable" || error5.code === "rate_limited");
37157
37189
  }
37158
37190
  function nextRelayConnector(o) {
37159
37191
  const collection2 = o.collection, next = o.next, staticKey = o.staticKey;
37160
37192
  if (!UUID9.test(collection2)) throw mdbaseError("invalid_request", "collection must be a UUID");
37161
37193
  const routeCall = next.route.bind(next), pipeCall = next.openPipe.bind(next);
37194
+ const description = `next-relay:${collection2}`;
37162
37195
  let sticky = null;
37163
- const connector = noiseConnector({
37164
- description: `next-relay:${collection2}`,
37165
- staticKey,
37166
- target: async (signal) => {
37196
+ return registerNextRelayFence({
37197
+ description,
37198
+ open: async (hello, signal) => {
37199
+ if (signal?.aborted) throw mdbaseError("cancelled", "connect aborted");
37167
37200
  const route2 = await bridged(() => routeCall(collection2, signal ? { signal } : {}));
37201
+ if (signal?.aborted) throw mdbaseError("cancelled", "connect aborted");
37168
37202
  if (route2.collection.toLowerCase() !== collection2.toLowerCase() || !UUID9.test(route2.grant)) {
37169
37203
  throw mdbaseError("unauthenticated", "the route does not match this collection's grant", "route_mismatch");
37170
37204
  }
37171
37205
  const availability = parseCollectionAvailability(route2.availability);
37172
37206
  if (availability?.state === "unavailable") throw collectionUnavailableError(availability.reason);
37173
- const selected = orderTargets(route2, sticky)[0];
37174
- if (!selected) {
37207
+ const targets2 = attemptTargets(route2, sticky);
37208
+ if (!targets2.length) {
37175
37209
  throw mdbaseError(
37176
37210
  "unavailable",
37177
37211
  route2.reason === "no_device_registered" ? "this collection has no device set up to serve it" : "no device of this collection is online",
37178
37212
  route2.reason ?? "no_device_online"
37179
37213
  );
37180
37214
  }
37181
- const target2 = Object.freeze({ ...selected });
37182
- const hex14 = target2.noise_pk;
37183
- const remoteStatic = new Uint8Array(32);
37184
- for (let i = 0; i < 32; i++) remoteStatic[i] = parseInt(hex14.slice(2 * i, 2 * i + 2), 16);
37185
- return {
37186
- // Must equal the daemon's pipe_open prologue: relay collection, grant, device.
37187
- prologue: clientPrologue(uuidToBytes(target2.relay_collection), uuidToBytes(route2.grant), uuidToBytes(target2.device)),
37188
- remoteStatic,
37189
- device: target2.device,
37190
- openCarrier: async (signal2) => {
37191
- const pipe = await bridged(() => pipeCall(collection2, target2, signal2 ? { signal: signal2 } : {}));
37192
- return duplexCarrier(pipe);
37215
+ let firstError;
37216
+ for (const selected of targets2) {
37217
+ const target2 = Object.freeze({ ...selected });
37218
+ const hex14 = target2.noise_pk;
37219
+ const remoteStatic = new Uint8Array(32);
37220
+ for (let i = 0; i < 32; i++) remoteStatic[i] = parseInt(hex14.slice(2 * i, 2 * i + 2), 16);
37221
+ const connector = noiseConnector({
37222
+ description,
37223
+ staticKey,
37224
+ target: async () => ({
37225
+ // Must equal the daemon's pipe_open prologue: relay collection, grant, device.
37226
+ prologue: clientPrologue(uuidToBytes(target2.relay_collection), uuidToBytes(route2.grant), uuidToBytes(target2.device)),
37227
+ remoteStatic,
37228
+ device: target2.device,
37229
+ openCarrier: async (carrierSignal) => {
37230
+ const pipe = await bridged(() => pipeCall(collection2, target2, carrierSignal ? { signal: carrierSignal } : {}));
37231
+ return duplexCarrier(pipe);
37232
+ }
37233
+ })
37234
+ });
37235
+ try {
37236
+ const opened = await connector.open(hello, signal);
37237
+ sticky = opened.device ?? null;
37238
+ return opened;
37239
+ } catch (error5) {
37240
+ if (!isRetryableTargetError(error5, signal)) throw error5;
37241
+ if (sticky === target2.device) sticky = null;
37242
+ firstError ??= error5;
37193
37243
  }
37194
- };
37195
- }
37196
- });
37197
- return registerNextRelayFence({
37198
- description: connector.description,
37199
- open: async (hello, signal) => {
37200
- const opened = await connector.open(hello, signal);
37201
- sticky = opened.device ?? null;
37202
- return opened;
37244
+ }
37245
+ throw firstError;
37203
37246
  },
37204
37247
  get target() {
37205
37248
  return sticky;
@@ -38543,8 +38586,10 @@ ${end.comment}` : end.comment;
38543
38586
  /**
38544
38587
  * A connector for `connect()`: each open uses a fresh route, refuses an
38545
38588
  * unavailable collection with its reason, and opens a Noise IK session through the
38546
- * relay to a serving device (sticky while it stays listed). The first open pins
38547
- * its original consent/key identity; new consent requires a new connector.
38589
+ * relay to a serving device (preferring the last reached device while it stays
38590
+ * online and serving, and failing over to another online target otherwise). The
38591
+ * first open pins its original consent/key identity; new consent requires a new
38592
+ * connector.
38548
38593
  */
38549
38594
  connector(collection2) {
38550
38595
  const id6 = collection2.toLowerCase();
@@ -38559,20 +38604,23 @@ ${end.comment}` : end.comment;
38559
38604
  this.routeProbe = void 0;
38560
38605
  const grant = await (original ??= this.heldGrant(id6));
38561
38606
  await this.assertCurrent(grant);
38562
- const connector = noiseConnector({
38563
- description: `app-grant:${id6}`,
38564
- staticKey: staticKeyOf2(grant),
38565
- target: async (s) => {
38566
- await this.assertCurrent(grant);
38567
- const { token, route: route2, availability } = primed && !primed.signal?.aborted && performance.now() < primed.until && primed.credentials === this.tokens.get(id6) && sameGrant(grant, primed.credentials) && primed.credentials.expiresAt > Date.now() ? { token: primed.credentials.access, route: primed.route, availability: { state: "available" } } : await this.route(id6, s);
38568
- await this.assertCurrent(grant);
38569
- if (availability.state === "unavailable") throw collectionUnavailableError(availability.reason);
38570
- if (!UUID10.test(route2.grant)) throw mdbaseError("unauthenticated", "the route names no grant", "route_mismatch");
38571
- const target2 = orderTargets(route2, sticky)[0];
38572
- if (!target2) throw collectionUnavailableError("device_offline");
38573
- const remoteStatic = new Uint8Array(32);
38574
- for (let i = 0; i < 32; i++) remoteStatic[i] = parseInt(target2.noise_pk.slice(2 * i, 2 * i + 2), 16);
38575
- return {
38607
+ if (signal?.aborted) throw mdbaseError("cancelled", "connect aborted");
38608
+ const { token, route: route2, availability } = primed && !primed.signal?.aborted && performance.now() < primed.until && primed.credentials === this.tokens.get(id6) && sameGrant(grant, primed.credentials) && primed.credentials.expiresAt > Date.now() ? { token: primed.credentials.access, route: primed.route, availability: { state: "available" } } : await this.route(id6, signal);
38609
+ await this.assertCurrent(grant);
38610
+ if (signal?.aborted) throw mdbaseError("cancelled", "connect aborted");
38611
+ if (availability.state === "unavailable") throw collectionUnavailableError(availability.reason);
38612
+ if (!UUID10.test(route2.grant)) throw mdbaseError("unauthenticated", "the route names no grant", "route_mismatch");
38613
+ const targets2 = attemptTargets(route2, sticky);
38614
+ if (!targets2.length) throw collectionUnavailableError("device_offline");
38615
+ let firstError;
38616
+ for (const target2 of targets2) {
38617
+ await this.assertCurrent(grant);
38618
+ const remoteStatic = new Uint8Array(32);
38619
+ for (let i = 0; i < 32; i++) remoteStatic[i] = parseInt(target2.noise_pk.slice(2 * i, 2 * i + 2), 16);
38620
+ const connector = noiseConnector({
38621
+ description: `app-grant:${id6}`,
38622
+ staticKey: staticKeyOf2(grant),
38623
+ target: async () => ({
38576
38624
  prologue: clientPrologue(uuidToBytes(target2.relay_collection), uuidToBytes(route2.grant), uuidToBytes(target2.device)),
38577
38625
  remoteStatic,
38578
38626
  device: target2.device,
@@ -38593,19 +38641,28 @@ ${end.comment}` : end.comment;
38593
38641
  throw e;
38594
38642
  }
38595
38643
  }
38596
- };
38644
+ })
38645
+ });
38646
+ let opened;
38647
+ try {
38648
+ opened = await connector.open(hello, signal);
38649
+ } catch (error5) {
38650
+ if (!isRetryableTargetError(error5, signal)) throw error5;
38651
+ if (sticky === target2.device) sticky = null;
38652
+ firstError ??= error5;
38653
+ continue;
38597
38654
  }
38598
- });
38599
- const opened = await connector.open(hello, signal);
38600
- try {
38601
- await this.assertCurrent(grant);
38602
- } catch (e) {
38603
- opened.port.close();
38604
- throw e;
38655
+ try {
38656
+ await this.assertCurrent(grant);
38657
+ } catch (e) {
38658
+ opened.port.close();
38659
+ throw e;
38660
+ }
38661
+ bindOriginalAppGrant(opened.port, grant);
38662
+ sticky = opened.device ?? null;
38663
+ return opened;
38605
38664
  }
38606
- bindOriginalAppGrant(opened.port, grant);
38607
- sticky = opened.device ?? null;
38608
- return opened;
38665
+ throw firstError;
38609
38666
  },
38610
38667
  get target() {
38611
38668
  return sticky;
@@ -50795,12 +50852,12 @@ ${end.comment}` : end.comment;
50795
50852
  // src/application/asset-pins.json
50796
50853
  var asset_pins_default = {
50797
50854
  runtime: {
50798
- sha256: "1b70aad8903a333cbe422e6b57e8677929c6b556651e69f2a0e7bff1f1ddb52c",
50799
- bytes: 4004324,
50800
- maxBytes: 4004324,
50855
+ sha256: "381efa4ca7c0648254068b587cf5bea04c0587b0d0b37359f6093ab78a55ee19",
50856
+ bytes: 4008498,
50857
+ maxBytes: 4008498,
50801
50858
  source: {
50802
50859
  repository: "mdbase-dev/mdbase-connect",
50803
- commit: "74f2d1018b66ab3ae99bc0afb5fb3a8009b25e23"
50860
+ commit: "48acdc9d1a95b2d299565f0df122b80d32697229"
50804
50861
  }
50805
50862
  },
50806
50863
  sqlite: {
@@ -52668,7 +52725,7 @@ ${end.comment}` : end.comment;
52668
52725
  this.set(open2, { status: "opening", problem: null });
52669
52726
  await checkBinding();
52670
52727
  const client = await (this.options.connect ?? defaultConnect(this.options.declaration))(grants, collectionId, signal, (why) => {
52671
- if (this.open === open2 && !signal.aborted && (why.reason === "not_served" || why.reason === "no_device_online"))
52728
+ if (this.open === open2 && !signal.aborted && (why.reason === "not_served" || why.reason === "device_not_serving" || why.reason === "no_device_online"))
52672
52729
  this.set(open2, { status: "blocked", problem: problemOf3(why), problemDetail: problemDetailOf(why), readiness: readiness(false, null) });
52673
52730
  });
52674
52731
  if (this.open !== open2 || signal.aborted || client.collection !== collectionId) {