@mdbase-dev/sdk 0.2.61 → 0.2.62

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.
@@ -15698,12 +15698,12 @@ function appReleaseTrust() {
15698
15698
  // src/application/asset-pins.json
15699
15699
  var asset_pins_default = {
15700
15700
  runtime: {
15701
- sha256: "18f9cc9ce4e613f23c8d4120fcdd58c7c0865224737e30f905a7173fdb8ada2d",
15702
- bytes: 4093898,
15703
- maxBytes: 4093898,
15701
+ sha256: "a27b57d4cb9c978725922af25928bb4242eb9580fa502815ea75993bbe92fe20",
15702
+ bytes: 4135951,
15703
+ maxBytes: 4135951,
15704
15704
  source: {
15705
15705
  repository: "mdbase-dev/mdbase-connect",
15706
- commit: "2da2b0bc84ee9ecd0ecdef79ea0e2e1ef334bccd"
15706
+ commit: "7ed4e0b55a8c1becfc10ddbea55d794543ca1e15"
15707
15707
  }
15708
15708
  },
15709
15709
  sqlite: {
@@ -16125,9 +16125,10 @@ function structMap(entries) {
16125
16125
  for (const [k, v] of entries) if (v !== void 0) m2.set(k, v);
16126
16126
  return m2;
16127
16127
  }
16128
+ var hexBytes = Array.from({ length: 256 }, (_, n) => n.toString(16).padStart(2, "0"));
16128
16129
  function toHex(b) {
16129
16130
  let s = "";
16130
- for (const x of b) s += x.toString(16).padStart(2, "0");
16131
+ for (const x of b) s += hexBytes[x];
16131
16132
  return s;
16132
16133
  }
16133
16134
  function fromHex(s) {
@@ -19289,6 +19290,12 @@ function timeout(value2) {
19289
19290
  }
19290
19291
 
19291
19292
  // src/records/session.ts
19293
+ function matches(record3, key3, value2) {
19294
+ if (key3 === null) return value2 === record3.body;
19295
+ const actual = fieldOf(record3.frontmatter, key3);
19296
+ if (actual === void 0 || value2 === void 0) return actual === value2;
19297
+ return equal2(toValue(actual), toValue(toPlain(value2)));
19298
+ }
19292
19299
  var RecordSessionImpl = class {
19293
19300
  constructor(client, record3, reserve) {
19294
19301
  this.client = client;
@@ -19296,7 +19303,7 @@ var RecordSessionImpl = class {
19296
19303
  complete(record3);
19297
19304
  this.base = copy(record3);
19298
19305
  this.body = record3.body;
19299
- this.state = isProtected(record3) ? "conflict" : resting(record3);
19306
+ this.state = isProtected(record3) ? "conflict" : this.restState;
19300
19307
  this.publish();
19301
19308
  }
19302
19309
  client;
@@ -19306,6 +19313,8 @@ var RecordSessionImpl = class {
19306
19313
  patch = /* @__PURE__ */ new Map();
19307
19314
  remote = null;
19308
19315
  captured = null;
19316
+ pendingEdits = /* @__PURE__ */ new Map();
19317
+ driveGeneration = 0;
19309
19318
  lastReceipt = null;
19310
19319
  state;
19311
19320
  why = null;
@@ -19346,6 +19355,24 @@ var RecordSessionImpl = class {
19346
19355
  recovery: { ...hint, receipt: null, write: null }
19347
19356
  };
19348
19357
  }
19358
+ if (!this.active && !hint && this.base.state.state === "pending" && !this.pendingEdits.size) {
19359
+ const edits = /* @__PURE__ */ new Map();
19360
+ for (const { ops, receipt: receipt3 } of await this.client.pendingWrites({ signal })) {
19361
+ for (const op2 of ops) {
19362
+ if (op2.kind !== "update" || op2.id !== this.id) continue;
19363
+ const mutation2 = receipt3.mutation;
19364
+ if (op2.body !== void 0 || op2.bodyEdits?.length) {
19365
+ const body = op2.body ?? receipt3.records?.find((record3) => record3.id === this.id)?.body ?? this.base.body;
19366
+ edits.set(null, { mutation: mutation2, value: body });
19367
+ }
19368
+ for (const [key3, value2] of Object.entries(op2.patch ?? {}))
19369
+ edits.set(key3, { mutation: mutation2, value: toValue(value2) });
19370
+ for (const key3 of op2.unset ?? []) edits.set(key3, { mutation: mutation2, value: void 0 });
19371
+ }
19372
+ }
19373
+ this.charge(this.body, this.patch, this.base, this.remote, this.lastReceipt, this.captured, edits);
19374
+ this.pendingEdits = edits;
19375
+ }
19349
19376
  signal?.throwIfAborted();
19350
19377
  if (this.captured) {
19351
19378
  this.state = "recovery";
@@ -19363,8 +19390,11 @@ var RecordSessionImpl = class {
19363
19390
  get dirty() {
19364
19391
  return this.body !== this.base.body || this.patch.size > 0;
19365
19392
  }
19393
+ get restState() {
19394
+ return !this.pendingEdits.size && confirmed(this.base) ? "saved" : "saved_on_device";
19395
+ }
19366
19396
  get retained() {
19367
- return this.dirty || this.captured !== null || !confirmed(this.base);
19397
+ return this.dirty || this.captured !== null || this.pendingEdits.size > 0 || !confirmed(this.base);
19368
19398
  }
19369
19399
  getSnapshot = () => this.snapshot;
19370
19400
  subscribe = (listener) => {
@@ -19414,13 +19444,13 @@ var RecordSessionImpl = class {
19414
19444
  };
19415
19445
  edited() {
19416
19446
  if (!this.captured && this.state !== "conflict" && this.state !== "deleted") {
19417
- this.state = this.dirty ? "unsaved" : resting(this.base);
19447
+ this.state = this.dirty ? "unsaved" : this.restState;
19418
19448
  this.why = null;
19419
19449
  }
19420
19450
  this.publish();
19421
19451
  this.schedule();
19422
19452
  }
19423
- charge(body = this.body, patch = this.patch, base = this.base, remote = this.remote, receipt3 = this.lastReceipt, captured = this.captured) {
19453
+ charge(body = this.body, patch = this.patch, base = this.base, remote = this.remote, receipt3 = this.lastReceipt, captured = this.captured, pendingEdits = this.pendingEdits) {
19424
19454
  let bytes10 = retainedBytes(base) + textBytes(body) + encode(new Map([...patch].map(([k, v]) => [k, v ?? null]))).byteLength * 2;
19425
19455
  if (remote) bytes10 += retainedBytes(remote);
19426
19456
  if (captured)
@@ -19429,6 +19459,8 @@ var RecordSessionImpl = class {
19429
19459
  [...captured.patch].map(([k, v]) => [k, v ?? null])
19430
19460
  )
19431
19461
  ).byteLength * 2;
19462
+ for (const [key3, edit] of pendingEdits)
19463
+ bytes10 += encode([key3, edit.mutation, edit.value ?? null]).byteLength * 2;
19432
19464
  if (receipt3) bytes10 += retainedBytes(receipt3);
19433
19465
  this.reserve(bytes10);
19434
19466
  }
@@ -19479,6 +19511,73 @@ var RecordSessionImpl = class {
19479
19511
  complete(record3, this.id);
19480
19512
  return record3;
19481
19513
  }
19514
+ /** The projection can change after either a refusal or a successful write
19515
+ * followed by a peer edit. Only the original MID distinguishes them. */
19516
+ async readWithReceipts(signal) {
19517
+ const receipts = /* @__PURE__ */ new Map();
19518
+ for (; ; ) {
19519
+ const current2 = await this.read(signal);
19520
+ if (this.captured) return { current: current2, receipts };
19521
+ const count = receipts.size;
19522
+ for (const [key3, { mutation: mutation2, value: value2 }] of this.pendingEdits) {
19523
+ const present = matches(current2, key3, value2);
19524
+ if (!confirmed(current2) && present) continue;
19525
+ if (!receipts.has(mutation2)) {
19526
+ const receipt4 = await this.client.receipt(mutation2, { signal });
19527
+ signal.throwIfAborted();
19528
+ if (receipt4.mutation !== mutation2)
19529
+ throw refusal("outcome_unknown", "native receipt changed identity", "mutation_identity_mismatch");
19530
+ receipts.set(mutation2, receipt4);
19531
+ }
19532
+ const receipt3 = receipts.get(mutation2);
19533
+ if (receipt3.state === "unknown" || receipt3.state === "pending" && !present)
19534
+ throw refusal("outcome_unknown", "the projection does not qualify this device write yet", "unqualified_readback");
19535
+ }
19536
+ if (receipts.size === count) return { current: current2, receipts };
19537
+ }
19538
+ }
19539
+ /** Restore only a refused write's missing edits. Newer typing and field
19540
+ * edits win, including an explicit unset; unrelated remote fields survive. */
19541
+ reconcilePending(current2, receipts) {
19542
+ if (!receipts.size) return;
19543
+ const remaining = new Map(this.pendingEdits);
19544
+ const patch = new Map(this.patch);
19545
+ const bodyEdited = this.body !== this.base.body;
19546
+ let body = bodyEdited ? this.body : current2.body;
19547
+ let rolledBack = false;
19548
+ for (const [key3, { mutation: mutation2, value: value2 }] of this.pendingEdits) {
19549
+ const receipt3 = receipts.get(mutation2);
19550
+ if (!receipt3 || receipt3.state === "pending" || receipt3.state === "unknown") continue;
19551
+ remaining.delete(key3);
19552
+ if (receipt3.state !== "rejected" && receipt3.status !== "conflicted" || matches(current2, key3, value2)) continue;
19553
+ rolledBack = true;
19554
+ if (key3 === null) {
19555
+ if (!bodyEdited) body = value2;
19556
+ } else if (!patch.has(key3)) {
19557
+ patch.set(key3, value2);
19558
+ }
19559
+ }
19560
+ const last = this.lastReceipt && (receipts.get(this.lastReceipt.mutation) ?? this.lastReceipt);
19561
+ if (rolledBack) {
19562
+ for (const [key3, value2] of patch) {
19563
+ if (matches(current2, key3, value2)) patch.delete(key3);
19564
+ }
19565
+ const differs = body !== current2.body || patch.size > 0;
19566
+ this.charge(body, patch, current2, differs ? current2 : null, last, this.captured, remaining);
19567
+ this.base = copy(current2);
19568
+ this.remote = differs ? copy(current2) : null;
19569
+ this.body = body;
19570
+ this.patch = patch;
19571
+ if (differs) {
19572
+ this.state = "conflict";
19573
+ this.why = refusal("conflict", "a device write was refused at head", "record_session_conflict").toProblem();
19574
+ }
19575
+ } else {
19576
+ this.charge(this.body, this.patch, this.base, this.remote, last, this.captured, remaining);
19577
+ }
19578
+ this.pendingEdits = remaining;
19579
+ this.lastReceipt = last;
19580
+ }
19482
19581
  /** Native change notifications are hints only; hydrate the same UUID. */
19483
19582
  refresh = () => {
19484
19583
  if (!this.active) return Promise.resolve();
@@ -19490,20 +19589,26 @@ var RecordSessionImpl = class {
19490
19589
  this.refreshRunning = (async () => {
19491
19590
  do {
19492
19591
  this.refreshAgain = false;
19592
+ const generation = this.driveGeneration;
19493
19593
  try {
19494
- const current2 = await this.read(signal);
19594
+ const { current: current2, receipts } = await this.readWithReceipts(signal);
19595
+ if (generation !== this.driveGeneration) {
19596
+ this.refreshAgain = true;
19597
+ continue;
19598
+ }
19495
19599
  if (this.captured || this.running) {
19496
19600
  if (this.captured && !this.running)
19497
19601
  void this.start(false, {}).catch(() => {
19498
19602
  });
19499
19603
  continue;
19500
19604
  }
19605
+ this.reconcilePending(current2, receipts);
19501
19606
  this.charge(this.body, this.patch, this.base, current2);
19502
19607
  if (!this.dirty) {
19503
19608
  this.base = copy(current2);
19504
19609
  this.body = current2.body;
19505
19610
  this.remote = null;
19506
- this.state = isProtected(current2) ? "conflict" : resting(current2);
19611
+ this.state = isProtected(current2) ? "conflict" : this.restState;
19507
19612
  this.why = null;
19508
19613
  } else if (current2.revision !== this.base.revision || isProtected(current2)) {
19509
19614
  this.remote = copy(current2);
@@ -19517,6 +19622,10 @@ var RecordSessionImpl = class {
19517
19622
  this.publish();
19518
19623
  } catch (error4) {
19519
19624
  if (signal.aborted) return;
19625
+ if (generation !== this.driveGeneration || this.running) {
19626
+ this.refreshAgain = !this.running;
19627
+ continue;
19628
+ }
19520
19629
  this.why = problem2(error4);
19521
19630
  this.state = this.why.code === "not_found" ? "deleted" : this.captured ? "recovery" : "error";
19522
19631
  this.publish();
@@ -19557,8 +19666,12 @@ var RecordSessionImpl = class {
19557
19666
  until: Date.now() + duration,
19558
19667
  margin: Math.min(2e3, Math.floor(duration / 4))
19559
19668
  };
19560
- const drive = () => this.drive(signal, budget, write);
19669
+ const drive = () => {
19670
+ this.driveGeneration++;
19671
+ return this.drive(signal, budget, write);
19672
+ };
19561
19673
  const run2 = (prior ? prior.then(drive, drive) : drive()).finally(() => {
19674
+ this.driveGeneration++;
19562
19675
  limit.clear();
19563
19676
  if (this.running !== run2) return;
19564
19677
  this.running = null;
@@ -19578,12 +19691,13 @@ var RecordSessionImpl = class {
19578
19691
  continue;
19579
19692
  }
19580
19693
  if (!submit)
19581
- return confirmed(this.base) ? { ok: true, record: copy(this.base) } : {
19694
+ return this.restState === "saved" ? { ok: true, record: copy(this.base) } : {
19582
19695
  ok: true,
19583
19696
  record: copy(this.base),
19584
19697
  saved: "on_device"
19585
19698
  };
19586
- const current2 = await this.read(signal);
19699
+ const { current: current2, receipts } = await this.readWithReceipts(signal);
19700
+ this.reconcilePending(current2, receipts);
19587
19701
  if (isProtected(current2)) {
19588
19702
  this.charge(this.body, this.patch, this.base, current2);
19589
19703
  this.remote = copy(current2);
@@ -19604,10 +19718,10 @@ var RecordSessionImpl = class {
19604
19718
  this.base = copy(current2);
19605
19719
  this.body = current2.body;
19606
19720
  this.remote = null;
19607
- this.state = resting(current2);
19721
+ this.state = this.restState;
19608
19722
  this.why = null;
19609
19723
  this.publish();
19610
- return confirmed(current2) ? { ok: true, record: copy(current2) } : { ok: true, record: copy(current2), saved: "on_device" };
19724
+ return this.restState === "saved" ? { ok: true, record: copy(current2) } : { ok: true, record: copy(current2), saved: "on_device" };
19611
19725
  }
19612
19726
  if (current2.revision !== this.base.revision || this.state === "conflict") {
19613
19727
  this.charge(this.body, this.patch, this.base, current2);
@@ -19743,14 +19857,23 @@ var RecordSessionImpl = class {
19743
19857
  }
19744
19858
  const current2 = await this.read(signal);
19745
19859
  const kept = receipt3.state === "pending";
19746
- if (kept ? isProtected(current2) : !confirmed(current2))
19860
+ const edits = new Map(this.pendingEdits);
19861
+ const submitted = new Map(captured.patch);
19862
+ if (captured.body !== this.base.body) submitted.set(null, captured.body);
19863
+ for (const [key3, value2] of submitted) {
19864
+ if (kept) edits.set(key3, { mutation: receipt3.mutation, value: value2 });
19865
+ else edits.delete(key3);
19866
+ }
19867
+ if (kept ? isProtected(current2) || [...submitted].some(([key3, value2]) => !matches(current2, key3, value2)) : !confirmed(current2))
19747
19868
  throw refusal(
19748
19869
  "outcome_unknown",
19749
- "confirmed receipt has pending/protected current readback",
19870
+ "record readback does not qualify the native receipt",
19750
19871
  "unqualified_readback"
19751
19872
  );
19752
- this.charge(this.body, this.patch, current2, null);
19753
19873
  const sameBody = this.body === captured.body;
19874
+ const conflict2 = !kept && !sameBody && current2.body !== captured.body && current2.body !== this.body;
19875
+ this.charge(this.body, this.patch, current2, conflict2 ? current2 : null, receipt3, null, edits);
19876
+ this.pendingEdits = edits;
19754
19877
  for (const [key3, value2] of captured.patch) {
19755
19878
  const latest = this.patch.get(key3);
19756
19879
  if (this.patch.has(key3) && (value2 === void 0 ? latest === void 0 : latest !== void 0 && equal2(value2, latest)))
@@ -19759,8 +19882,10 @@ var RecordSessionImpl = class {
19759
19882
  this.base = copy(current2);
19760
19883
  if (sameBody) this.body = current2.body;
19761
19884
  this.captured = null;
19762
- this.remote = null;
19763
- this.state = this.dirty ? "unsaved" : resting(current2);
19885
+ this.remote = conflict2 ? copy(current2) : null;
19886
+ if (conflict2)
19887
+ throw refusal("conflict", "record body changed while saving an earlier draft", "record_session_conflict");
19888
+ this.state = this.dirty ? "unsaved" : this.restState;
19764
19889
  this.why = null;
19765
19890
  this.publish();
19766
19891
  }
@@ -19779,7 +19904,7 @@ var RecordSessionImpl = class {
19779
19904
  "a confirmed unprotected native record is required for resolution",
19780
19905
  "native_protection_resolution_required"
19781
19906
  );
19782
- const body = "body" in choice ? choice.body : choice.keep === "theirs" ? remote.body : this.body;
19907
+ const body = "body" in choice ? choice.body : choice.keep === "theirs" || this.body === this.base.body ? remote.body : this.body;
19783
19908
  const patch = "keep" in choice && choice.keep === "theirs" ? /* @__PURE__ */ new Map() : this.patch;
19784
19909
  this.charge(body, patch, remote, null);
19785
19910
  this.base = copy(remote);
@@ -19792,9 +19917,6 @@ var RecordSessionImpl = class {
19792
19917
  this.schedule();
19793
19918
  };
19794
19919
  };
19795
- function resting(record3) {
19796
- return confirmed(record3) ? "saved" : "saved_on_device";
19797
- }
19798
19920
  function equal2(a, b) {
19799
19921
  const x = encode(a), y = encode(b);
19800
19922
  return x.length === y.length && x.every((v, i) => v === y[i]);
@@ -40588,9 +40710,9 @@ var SqliteIndex = class _SqliteIndex {
40588
40710
  }
40589
40711
  });
40590
40712
  }
40591
- column(st, i) {
40713
+ column(st, i, tag) {
40592
40714
  const capi = this.sqlite3.capi;
40593
- switch (capi.sqlite3_column_type(st.pointer, i)) {
40715
+ switch (tag) {
40594
40716
  case SQLITE_NULL:
40595
40717
  return { kind: "Null" };
40596
40718
  case SQLITE_INTEGER: {
@@ -40644,9 +40766,9 @@ var SqliteIndex = class _SqliteIndex {
40644
40766
  while (st.step()) {
40645
40767
  if (limits && ++rows2 > limits.maxRows) throw new IndexError("Full", "index reply row budget exceeded");
40646
40768
  for (let c = 0; c < columns; c++) {
40769
+ if (limits && ++valueCount > limits.maxValues) throw new IndexError("Full", "index reply value budget exceeded");
40770
+ const tag = capi.sqlite3_column_type(st.pointer, c);
40647
40771
  if (limits) {
40648
- if (++valueCount > limits.maxValues) throw new IndexError("Full", "index reply value budget exceeded");
40649
- const tag = capi.sqlite3_column_type(st.pointer, c);
40650
40772
  if (tag === SQLITE_TEXT || tag === SQLITE_BLOB) {
40651
40773
  addBytes(5);
40652
40774
  addBytes(capi.sqlite3_column_bytes(st.pointer, c));
@@ -40654,7 +40776,7 @@ var SqliteIndex = class _SqliteIndex {
40654
40776
  else if (tag === SQLITE_NULL) addBytes(1);
40655
40777
  else throw new IndexError("Other", "unknown SQLite value type");
40656
40778
  }
40657
- values.push(this.column(st, c));
40779
+ values.push(this.column(st, c, tag));
40658
40780
  }
40659
40781
  }
40660
40782
  out.push({
@@ -40817,33 +40939,46 @@ function request2(bytes10) {
40817
40939
  return { mode: mode === 0 ? "Transaction" : "Autocommit", stmts };
40818
40940
  }
40819
40941
  var Writer2 = class {
40820
- parts = [];
40942
+ bytes = new Uint8Array(128);
40943
+ view = new DataView(this.bytes.buffer);
40821
40944
  length = 0;
40822
40945
  values = 0;
40823
40946
  constructor(op2) {
40824
40947
  this.put(MAGIC);
40825
40948
  this.byte(op2);
40826
40949
  }
40950
+ reserve(size2) {
40951
+ const start = this.length;
40952
+ const end = start + size2;
40953
+ if (end > APP_INDEX_LIMITS.maxBytes) throw full();
40954
+ if (end > this.bytes.length) {
40955
+ const capacity = Math.min(APP_INDEX_LIMITS.maxBytes, Math.max(end, this.bytes.length * 2));
40956
+ const next = new Uint8Array(capacity);
40957
+ next.set(this.bytes.subarray(0, start));
40958
+ this.bytes = next;
40959
+ this.view = new DataView(next.buffer);
40960
+ }
40961
+ this.length = end;
40962
+ return start;
40963
+ }
40827
40964
  put(b) {
40828
- if (b.length > APP_INDEX_LIMITS.maxBytes - this.length) throw full();
40829
- this.length += b.length;
40830
- this.parts.push(b);
40965
+ const at = this.reserve(b.length);
40966
+ this.bytes.set(b, at);
40831
40967
  }
40832
40968
  byte(n) {
40833
- this.put(Uint8Array.of(n));
40969
+ const at = this.reserve(1);
40970
+ this.bytes[at] = n;
40834
40971
  }
40835
40972
  u32(n) {
40836
40973
  if (!Number.isSafeInteger(n) || n < 0 || n > 4294967295) throw invalid5();
40837
- const b = new Uint8Array(4);
40838
- new DataView(b.buffer).setUint32(0, n, true);
40839
- this.put(b);
40974
+ const at = this.reserve(4);
40975
+ this.view.setUint32(at, n, true);
40840
40976
  }
40841
40977
  int(n, signed) {
40842
40978
  if (typeof n !== "bigint" || n < (signed ? I64_MIN2 : 0n) || n > (signed ? I64_MAX : U64_MAX2)) throw invalid5();
40843
- const b = new Uint8Array(8), v = new DataView(b.buffer);
40844
- if (signed) v.setBigInt64(0, n, true);
40845
- else v.setBigUint64(0, n, true);
40846
- this.put(b);
40979
+ const at = this.reserve(8);
40980
+ if (signed) this.view.setBigInt64(at, n, true);
40981
+ else this.view.setBigUint64(at, n, true);
40847
40982
  }
40848
40983
  data(b) {
40849
40984
  this.u32(b.length);
@@ -40862,9 +40997,8 @@ var Writer2 = class {
40862
40997
  case "Real": {
40863
40998
  if (!Number.isFinite(v.value)) throw invalid5();
40864
40999
  this.byte(2);
40865
- const b = new Uint8Array(8);
40866
- new DataView(b.buffer).setFloat64(0, v.value, true);
40867
- this.put(b);
41000
+ const at = this.reserve(8);
41001
+ this.view.setFloat64(at, v.value, true);
40868
41002
  return;
40869
41003
  }
40870
41004
  case "Text":
@@ -40880,13 +41014,7 @@ var Writer2 = class {
40880
41014
  }
40881
41015
  }
40882
41016
  done() {
40883
- const out = new Uint8Array(this.length);
40884
- let at = 0;
40885
- for (const part of this.parts) {
40886
- out.set(part, at);
40887
- at += part.length;
40888
- }
40889
- return out;
41017
+ return this.bytes.slice(0, this.length);
40890
41018
  }
40891
41019
  };
40892
41020
  function results(out, expected) {