@mdbase-dev/sdk 0.2.59 → 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: "051bc364e712b4ad25f58d4649a90624c10a910e35a9066221be4e95cfbfdeaa",
15702
- bytes: 4071435,
15703
- maxBytes: 4071435,
15701
+ sha256: "a27b57d4cb9c978725922af25928bb4242eb9580fa502815ea75993bbe92fe20",
15702
+ bytes: 4135951,
15703
+ maxBytes: 4135951,
15704
15704
  source: {
15705
15705
  repository: "mdbase-dev/mdbase-connect",
15706
- commit: "84f9cec4a0a9f22443a8e0f5b16b682503da0620"
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]);
@@ -38953,7 +39075,7 @@ var AppWebSignInSession = class {
38953
39075
  * sign-in gesture; a pending ledger keeps its SAME request for authorize(). */
38954
39076
  start() {
38955
39077
  return this.operation(async () => {
38956
- if (this.flow && this.base.status !== "blocked") return this.getSnapshot();
39078
+ if (this.flow && (this.base.status !== "blocked" || this.base.problem === "installation_removed")) return this.getSnapshot();
38957
39079
  this.publish({ status: "starting", problem: null, signOutResult: null });
38958
39080
  const p = this.options;
38959
39081
  this.flow ??= await AppProtectedInstallationSignIn.open({
@@ -38990,6 +39112,7 @@ var AppWebSignInSession = class {
38990
39112
  * exchange, confirmation or attestation controls are needed by consumers. */
38991
39113
  authorize(options = {}) {
38992
39114
  this.check();
39115
+ if (this.base.problem === "installation_removed") return Promise.reject(failure("refused"));
38993
39116
  if (this.authorizationRateLimit && Date.now() < this.authorizationRetryAt) return Promise.reject(this.authorizationRateLimit);
38994
39117
  const poll = this.pending !== null && this.pending === this.polling ? this.pending : null;
38995
39118
  if (!this.flow || this.pending && !poll) return Promise.reject(failure("busy"));
@@ -39027,7 +39150,14 @@ var AppWebSignInSession = class {
39027
39150
  await this.openDevice();
39028
39151
  this.check();
39029
39152
  await this.abandonUnfinishedNativeChoice();
39030
- const consent = await this.flow.startCollectionConsent(this.installation, this.options.requestedCreateCollections ?? this.options.freshRequestedCreateCollections ?? false, hint, this.options.freshRequestedPrivateDevice === true);
39153
+ let consent;
39154
+ try {
39155
+ consent = await this.flow.startCollectionConsent(this.installation, this.options.requestedCreateCollections ?? this.options.freshRequestedCreateCollections ?? false, hint, this.options.freshRequestedPrivateDevice === true);
39156
+ } catch (error4) {
39157
+ if (!await this.handleRemovedInstallation(error4)) throw error4;
39158
+ portal.close();
39159
+ return this.getSnapshot();
39160
+ }
39031
39161
  this.check();
39032
39162
  this.consentPending = true;
39033
39163
  portal.navigate(consent.verificationUri);
@@ -39423,7 +39553,13 @@ var AppWebSignInSession = class {
39423
39553
  /** `kept`: this installation's last listing instead of a read. It is not an
39424
39554
  * accepted inventory, so an approval missing from it is not a removal. */
39425
39555
  async refreshCollections(kept = null) {
39426
- const collections = kept ?? await this.flow.listApprovedCollections(this.installation);
39556
+ let collections;
39557
+ try {
39558
+ collections = kept ?? await this.flow.listApprovedCollections(this.installation);
39559
+ } catch (error4) {
39560
+ if (await this.handleRemovedInstallation(error4)) return;
39561
+ throw error4;
39562
+ }
39427
39563
  this.check();
39428
39564
  this.listCached = kept !== null;
39429
39565
  if (!kept) {
@@ -39449,6 +39585,25 @@ var AppWebSignInSession = class {
39449
39585
  if (this.freshApproval && picked && visible.some((c) => c.collectionId === picked)) await this.openSlot(picked, true);
39450
39586
  else if (visible.length === 1) await this.openSlot(visible[0].collectionId, true);
39451
39587
  }
39588
+ /** Only Connect's exact refusal of this installation's own bearer ends
39589
+ * access across its scopes. Keep the ledger for explicit, per-scope recovery. */
39590
+ async handleRemovedInstallation(error4) {
39591
+ if (!(error4 instanceof AppInstallationSignInError) || !isAppServerProblem(error4.server) || error4.server.status !== 401 || error4.server.code !== "invalid_connector") return false;
39592
+ this.check();
39593
+ this.listCached = false;
39594
+ for (const id4 of this.flow.view().collectionIds) this.removedScopes.add(id4);
39595
+ this.consentPending = false;
39596
+ this.closeApprovalWindow();
39597
+ for (const slot of this.slots.values()) {
39598
+ slot.usable = false;
39599
+ slot.lifetime?.abort();
39600
+ }
39601
+ this.publish({ status: "blocked", problem: "installation_removed", problemDetail: webSignInProblem(error4).detail, collections: Object.freeze([]) });
39602
+ await this.closeSlots();
39603
+ this.check();
39604
+ this.changed();
39605
+ return true;
39606
+ }
39452
39607
  schedulePoll() {
39453
39608
  if (this.pending && !this.closing && !this.options.signal.aborted) return;
39454
39609
  const read = this.foregroundRead;
@@ -39914,8 +40069,8 @@ var AppWebSignInSession = class {
39914
40069
  collectionListCached() {
39915
40070
  return this.listCached;
39916
40071
  }
39917
- /** Accepted COMPLETE inventory omission or terminal native/log refusal only.
39918
- * Local Forget, failed GETs and transient transport failures do not qualify. */
40072
+ /** A complete inventory omission, a terminal scope refusal, or rejection of
40073
+ * the installation's bearer. Transport and unrelated HTTP failures stay separate. */
39919
40074
  scopeWasRemoved(collectionId) {
39920
40075
  return this.removedScopes.has(collectionId);
39921
40076
  }
@@ -40555,9 +40710,9 @@ var SqliteIndex = class _SqliteIndex {
40555
40710
  }
40556
40711
  });
40557
40712
  }
40558
- column(st, i) {
40713
+ column(st, i, tag) {
40559
40714
  const capi = this.sqlite3.capi;
40560
- switch (capi.sqlite3_column_type(st.pointer, i)) {
40715
+ switch (tag) {
40561
40716
  case SQLITE_NULL:
40562
40717
  return { kind: "Null" };
40563
40718
  case SQLITE_INTEGER: {
@@ -40611,9 +40766,9 @@ var SqliteIndex = class _SqliteIndex {
40611
40766
  while (st.step()) {
40612
40767
  if (limits && ++rows2 > limits.maxRows) throw new IndexError("Full", "index reply row budget exceeded");
40613
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);
40614
40771
  if (limits) {
40615
- if (++valueCount > limits.maxValues) throw new IndexError("Full", "index reply value budget exceeded");
40616
- const tag = capi.sqlite3_column_type(st.pointer, c);
40617
40772
  if (tag === SQLITE_TEXT || tag === SQLITE_BLOB) {
40618
40773
  addBytes(5);
40619
40774
  addBytes(capi.sqlite3_column_bytes(st.pointer, c));
@@ -40621,7 +40776,7 @@ var SqliteIndex = class _SqliteIndex {
40621
40776
  else if (tag === SQLITE_NULL) addBytes(1);
40622
40777
  else throw new IndexError("Other", "unknown SQLite value type");
40623
40778
  }
40624
- values.push(this.column(st, c));
40779
+ values.push(this.column(st, c, tag));
40625
40780
  }
40626
40781
  }
40627
40782
  out.push({
@@ -40784,33 +40939,46 @@ function request2(bytes10) {
40784
40939
  return { mode: mode === 0 ? "Transaction" : "Autocommit", stmts };
40785
40940
  }
40786
40941
  var Writer2 = class {
40787
- parts = [];
40942
+ bytes = new Uint8Array(128);
40943
+ view = new DataView(this.bytes.buffer);
40788
40944
  length = 0;
40789
40945
  values = 0;
40790
40946
  constructor(op2) {
40791
40947
  this.put(MAGIC);
40792
40948
  this.byte(op2);
40793
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
+ }
40794
40964
  put(b) {
40795
- if (b.length > APP_INDEX_LIMITS.maxBytes - this.length) throw full();
40796
- this.length += b.length;
40797
- this.parts.push(b);
40965
+ const at = this.reserve(b.length);
40966
+ this.bytes.set(b, at);
40798
40967
  }
40799
40968
  byte(n) {
40800
- this.put(Uint8Array.of(n));
40969
+ const at = this.reserve(1);
40970
+ this.bytes[at] = n;
40801
40971
  }
40802
40972
  u32(n) {
40803
40973
  if (!Number.isSafeInteger(n) || n < 0 || n > 4294967295) throw invalid5();
40804
- const b = new Uint8Array(4);
40805
- new DataView(b.buffer).setUint32(0, n, true);
40806
- this.put(b);
40974
+ const at = this.reserve(4);
40975
+ this.view.setUint32(at, n, true);
40807
40976
  }
40808
40977
  int(n, signed) {
40809
40978
  if (typeof n !== "bigint" || n < (signed ? I64_MIN2 : 0n) || n > (signed ? I64_MAX : U64_MAX2)) throw invalid5();
40810
- const b = new Uint8Array(8), v = new DataView(b.buffer);
40811
- if (signed) v.setBigInt64(0, n, true);
40812
- else v.setBigUint64(0, n, true);
40813
- 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);
40814
40982
  }
40815
40983
  data(b) {
40816
40984
  this.u32(b.length);
@@ -40829,9 +40997,8 @@ var Writer2 = class {
40829
40997
  case "Real": {
40830
40998
  if (!Number.isFinite(v.value)) throw invalid5();
40831
40999
  this.byte(2);
40832
- const b = new Uint8Array(8);
40833
- new DataView(b.buffer).setFloat64(0, v.value, true);
40834
- this.put(b);
41000
+ const at = this.reserve(8);
41001
+ this.view.setFloat64(at, v.value, true);
40835
41002
  return;
40836
41003
  }
40837
41004
  case "Text":
@@ -40847,13 +41014,7 @@ var Writer2 = class {
40847
41014
  }
40848
41015
  }
40849
41016
  done() {
40850
- const out = new Uint8Array(this.length);
40851
- let at = 0;
40852
- for (const part of this.parts) {
40853
- out.set(part, at);
40854
- at += part.length;
40855
- }
40856
- return out;
41017
+ return this.bytes.slice(0, this.length);
40857
41018
  }
40858
41019
  };
40859
41020
  function results(out, expected) {