@mdbase-dev/sdk 0.2.73 → 0.2.76

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.
@@ -40664,6 +40664,8 @@ var AppScopedSignInSession = class {
40664
40664
  };
40665
40665
 
40666
40666
  // src/app-storage/sqlite-index.ts
40667
+ var validatedPackets = /* @__PURE__ */ new WeakMap();
40668
+ var resultByteLengths = /* @__PURE__ */ new WeakMap();
40667
40669
  var IndexError = class extends Error {
40668
40670
  constructor(kind, detail, stmt = null) {
40669
40671
  super(`${kind}${stmt === null ? "" : `@${stmt}`}(${detail})`);
@@ -40682,6 +40684,8 @@ var SQLITE_FLOAT = 2;
40682
40684
  var SQLITE_TEXT = 3;
40683
40685
  var SQLITE_BLOB = 4;
40684
40686
  var SQLITE_NULL = 5;
40687
+ var SQLITE_ROW = 100;
40688
+ var SQLITE_DONE = 101;
40685
40689
  function classifyError(e, stmt = null) {
40686
40690
  if (e instanceof IndexError) return e;
40687
40691
  const name2 = e?.name ?? "";
@@ -40720,12 +40724,14 @@ var SqliteIndex = class _SqliteIndex {
40720
40724
  const synchronous = opts.synchronous ?? "NORMAL";
40721
40725
  if (synchronous !== "NORMAL" && synchronous !== "FULL") throw new IndexError("Other", "unsupported synchronous profile");
40722
40726
  if (opts.pragmas !== false) {
40723
- db.exec("PRAGMA page_size=32768");
40727
+ db.exec(synchronous === "FULL" ? "PRAGMA page_size=65536" : "PRAGMA page_size=32768");
40724
40728
  db.exec("PRAGMA locking_mode=EXCLUSIVE");
40725
40729
  db.exec("PRAGMA journal_mode=WAL");
40726
40730
  db.exec(`PRAGMA synchronous=${synchronous}`);
40727
40731
  if (synchronous === "FULL") {
40728
- db.exec("PRAGMA cache_size=-32768; PRAGMA wal_autocheckpoint=4096");
40732
+ db.exec("PRAGMA cache_size=-65536");
40733
+ const frames = db.selectValue("PRAGMA page_size") === 65536 ? 2048 : 4096;
40734
+ db.exec(`PRAGMA wal_autocheckpoint=${frames}`);
40729
40735
  }
40730
40736
  }
40731
40737
  if (opts.verifyPragmas && (String(db.selectValue("PRAGMA locking_mode")).toLowerCase() !== "exclusive" || String(db.selectValue("PRAGMA journal_mode")).toLowerCase() !== "wal" || db.selectValue("PRAGMA synchronous") !== (synchronous === "FULL" ? 2 : 1))) throw new IndexError("Other", "SQLite did not apply the requested storage profile");
@@ -40758,6 +40764,14 @@ var SqliteIndex = class _SqliteIndex {
40758
40764
  }
40759
40765
  return st;
40760
40766
  }
40767
+ statementBuffer(params2) {
40768
+ let bytes10 = 0;
40769
+ for (const p of params2) {
40770
+ if (p.kind === "Blob") bytes10 += Math.max(1, p.value.byteLength);
40771
+ if (p.kind === "Text") bytes10 += Math.max(1, p.value.length * 3);
40772
+ }
40773
+ return bytes10 ? this.sqlite3.wasm.alloc(bytes10) : 0;
40774
+ }
40761
40775
  bind(st, params2, buffer) {
40762
40776
  const pointer = st.pointer;
40763
40777
  const wasm = this.sqlite3.wasm;
@@ -40796,6 +40810,40 @@ var SqliteIndex = class _SqliteIndex {
40796
40810
  if (rc) this.db.checkRc(rc);
40797
40811
  }
40798
40812
  }
40813
+ bindPacket(st, packet, i, base) {
40814
+ const raw = this.sqlite3.wasm.exports;
40815
+ const pointer = st.pointer, view = packet.view;
40816
+ let at = packet.offsets[i];
40817
+ const count = view.getUint32(at - 4, true);
40818
+ for (let idx = 1; idx <= count; idx++) {
40819
+ const tag = view.getUint8(at++);
40820
+ let rc;
40821
+ switch (tag) {
40822
+ case 0:
40823
+ rc = raw.sqlite3_bind_null(pointer, idx);
40824
+ break;
40825
+ case 1:
40826
+ rc = raw.sqlite3_bind_int64(pointer, idx, view.getBigInt64(at, true));
40827
+ at += 8;
40828
+ break;
40829
+ case 2:
40830
+ st.bind(idx, view.getFloat64(at, true));
40831
+ at += 8;
40832
+ continue;
40833
+ case 3:
40834
+ case 4: {
40835
+ const length = view.getUint32(at, true);
40836
+ at += 4;
40837
+ rc = tag === 3 ? raw.sqlite3_bind_text(pointer, idx, base + at, length, 0) : raw.sqlite3_bind_blob(pointer, idx, base + at, length, 0);
40838
+ at += length;
40839
+ break;
40840
+ }
40841
+ default:
40842
+ throw new IndexError("Other", "invalid tag in validated packet");
40843
+ }
40844
+ if (rc) this.db.checkRc(rc);
40845
+ }
40846
+ }
40799
40847
  column(pointer, i, tag, addBytes) {
40800
40848
  const raw = this.sqlite3.wasm.exports;
40801
40849
  switch (tag) {
@@ -40825,6 +40873,19 @@ var SqliteIndex = class _SqliteIndex {
40825
40873
  /** `IndexStorage::run`. */
40826
40874
  run(batch, limits) {
40827
40875
  if (this.closed) throw new IndexError("Other", "index closed");
40876
+ const packet = validatedPackets.get(batch);
40877
+ if (!packet?.hasPayload) return this.runBatch(batch, limits, packet);
40878
+ const wasm = this.sqlite3.wasm;
40879
+ const view = packet.view;
40880
+ const buffer = wasm.alloc(view.byteLength + 1);
40881
+ try {
40882
+ wasm.heap8u().set(new Uint8Array(view.buffer, view.byteOffset, view.byteLength), buffer);
40883
+ return this.runBatch(batch, limits, packet, buffer);
40884
+ } finally {
40885
+ wasm.dealloc(buffer);
40886
+ }
40887
+ }
40888
+ runBatch(batch, limits, packet, packetBase = 0) {
40828
40889
  const raw = this.sqlite3.wasm.exports;
40829
40890
  const tx = batch.mode === "Transaction";
40830
40891
  const out = [];
@@ -40844,26 +40905,28 @@ var SqliteIndex = class _SqliteIndex {
40844
40905
  throw classifyError(e);
40845
40906
  }
40846
40907
  }
40847
- for (let i = 0; i < batch.stmts.length; i++) {
40908
+ for (let i = 0; i < (packet?.offsets.length ?? batch.stmts.length); i++) {
40848
40909
  const s = batch.stmts[i];
40849
40910
  try {
40850
40911
  const st = this.prepare(s.sql);
40851
- let bindBytes = 0;
40852
- for (const p of s.params) {
40853
- if (p.kind === "Blob") bindBytes += Math.max(1, p.value.byteLength);
40854
- if (p.kind === "Text") bindBytes += Math.max(1, p.value.length * 3);
40855
- }
40856
- const buffer = bindBytes ? this.sqlite3.wasm.alloc(bindBytes) : 0;
40912
+ const pointer = st.pointer;
40913
+ let buffer = 0;
40857
40914
  try {
40858
- this.bind(st, s.params, buffer);
40859
- const pointer = st.pointer;
40860
- const columns = st.columnCount;
40915
+ if (packet) {
40916
+ this.bindPacket(st, packet, i, packetBase);
40917
+ } else {
40918
+ const params2 = s.params;
40919
+ buffer = this.statementBuffer(params2);
40920
+ this.bind(st, params2, buffer);
40921
+ }
40922
+ const columns = raw.sqlite3_column_count(pointer);
40861
40923
  if (limits && (!Number.isSafeInteger(columns) || columns < 0 || columns > limits.maxColumns)) {
40862
40924
  throw new IndexError("Full", "index reply column budget exceeded");
40863
40925
  }
40864
40926
  addBytes(24);
40865
40927
  const values = [];
40866
- while (st.step()) {
40928
+ let rc;
40929
+ while ((rc = raw.sqlite3_step(pointer)) === SQLITE_ROW) {
40867
40930
  if (limits && ++rows2 > limits.maxRows) throw new IndexError("Full", "index reply row budget exceeded");
40868
40931
  for (let c = 0; c < columns; c++) {
40869
40932
  if (limits && ++valueCount > limits.maxValues) throw new IndexError("Full", "index reply value budget exceeded");
@@ -40871,6 +40934,7 @@ var SqliteIndex = class _SqliteIndex {
40871
40934
  values.push(this.column(pointer, c, tag, addBytes));
40872
40935
  }
40873
40936
  }
40937
+ if (rc !== SQLITE_DONE) this.db.checkRc(rc);
40874
40938
  out.push({
40875
40939
  columns,
40876
40940
  values,
@@ -40879,7 +40943,9 @@ var SqliteIndex = class _SqliteIndex {
40879
40943
  });
40880
40944
  } finally {
40881
40945
  try {
40882
- st.reset(true);
40946
+ const clear = raw.sqlite3_clear_bindings(pointer);
40947
+ const reset = raw.sqlite3_reset(pointer);
40948
+ if (clear || reset) this.db.checkRc(clear || reset);
40883
40949
  } finally {
40884
40950
  if (buffer) this.sqlite3.wasm.dealloc(buffer);
40885
40951
  }
@@ -40905,6 +40971,7 @@ var SqliteIndex = class _SqliteIndex {
40905
40971
  throw classifyError(e);
40906
40972
  }
40907
40973
  }
40974
+ if (limits) resultByteLengths.set(out, bytes10);
40908
40975
  return out;
40909
40976
  }
40910
40977
  /** Finalize statements and close. A fenced/uncertain handle must not be marked clean. */
@@ -40946,15 +41013,19 @@ var invalid5 = () => new IndexError("Other", "invalid app index ABI");
40946
41013
  var full = () => new IndexError("Full", "app index ABI budget exceeded");
40947
41014
  var utf85 = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
40948
41015
  var encoder2 = new TextEncoder();
41016
+ function validateText(bytes10, view, at, end) {
41017
+ while (at + 4 <= end && !(view.getUint32(at) & 2155905152)) at += 4;
41018
+ while (at < end && bytes10[at] < 128) at++;
41019
+ if (at < end) utf85.decode(bytes10.subarray(at, end));
41020
+ }
40949
41021
  var Reader2 = class {
40950
41022
  constructor(bytes10) {
40951
41023
  this.bytes = bytes10;
40952
- if (bytes10.length > APP_INDEX_LIMITS.maxBytes) throw full();
40953
41024
  this.view = new DataView(bytes10.buffer, bytes10.byteOffset, bytes10.byteLength);
40954
41025
  }
40955
41026
  bytes;
40956
41027
  pos = 0;
40957
- values = 0;
41028
+ hasPayload = false;
40958
41029
  view;
40959
41030
  advance(n) {
40960
41031
  if (!Number.isSafeInteger(n) || n < 0 || n > this.bytes.length - this.pos) throw invalid5();
@@ -40984,27 +41055,39 @@ var Reader2 = class {
40984
41055
  text(max) {
40985
41056
  return utf85.decode(this.data(max));
40986
41057
  }
40987
- value() {
40988
- if (++this.values > APP_INDEX_LIMITS.maxValues) throw full();
40989
- switch (this.byte()) {
40990
- case 0:
40991
- return { kind: "Null" };
40992
- case 1: {
40993
- const at = this.advance(8);
40994
- return { kind: "Integer", value: this.view.getBigInt64(at, true) };
40995
- }
40996
- case 2: {
40997
- const at = this.advance(8);
40998
- const value2 = this.view.getFloat64(at, true);
40999
- if (!Number.isFinite(value2)) throw invalid5();
41000
- return { kind: "Real", value: value2 };
41001
- }
41002
- case 3:
41003
- return { kind: "Text", value: this.text(APP_INDEX_LIMITS.maxBytes) };
41004
- case 4:
41005
- return { kind: "Blob", value: this.data(APP_INDEX_LIMITS.maxBytes) };
41006
- default:
41007
- throw invalid5();
41058
+ params(count, out) {
41059
+ for (let i = 0; i < count; i++) {
41060
+ const tag = this.byte();
41061
+ switch (tag) {
41062
+ case 0:
41063
+ out?.push({ kind: "Null" });
41064
+ break;
41065
+ case 1: {
41066
+ const at = this.advance(8);
41067
+ out?.push({ kind: "Integer", value: this.view.getBigInt64(at, true) });
41068
+ break;
41069
+ }
41070
+ case 2: {
41071
+ const value2 = this.view.getFloat64(this.advance(8), true);
41072
+ if (!Number.isFinite(value2)) throw invalid5();
41073
+ out?.push({ kind: "Real", value: value2 });
41074
+ break;
41075
+ }
41076
+ case 3:
41077
+ case 4: {
41078
+ const at = this.advance(this.count(APP_INDEX_LIMITS.maxBytes, 1));
41079
+ this.hasPayload = true;
41080
+ if (out) {
41081
+ const data = this.bytes.subarray(at, this.pos);
41082
+ out.push(tag === 3 ? { kind: "Text", value: utf85.decode(data) } : { kind: "Blob", value: data.slice() });
41083
+ } else if (tag === 3) {
41084
+ validateText(this.bytes, this.view, at, this.pos);
41085
+ }
41086
+ break;
41087
+ }
41088
+ default:
41089
+ throw invalid5();
41090
+ }
41008
41091
  }
41009
41092
  }
41010
41093
  done() {
@@ -41024,22 +41107,41 @@ function request2(bytes10) {
41024
41107
  if (mode > 1) throw invalid5();
41025
41108
  const count = r.count(APP_INDEX_LIMITS.maxStatements, 8);
41026
41109
  const stmts = [];
41110
+ const offsets = new Uint32Array(count);
41111
+ let total = 0;
41027
41112
  for (let i = 0; i < count; i++) {
41028
41113
  const sql = r.text(APP_INDEX_LIMITS.maxSqlBytes);
41029
41114
  const n = r.count(APP_INDEX_LIMITS.maxParameters, 1);
41030
- const params2 = [];
41031
- for (let j = 0; j < n; j++) params2.push(r.value());
41032
- stmts.push({ sql, params: params2 });
41115
+ total += n;
41116
+ if (total > APP_INDEX_LIMITS.maxValues) throw full();
41117
+ offsets[i] = r.pos;
41118
+ r.params(n);
41119
+ let decoded;
41120
+ stmts.push({
41121
+ sql,
41122
+ get params() {
41123
+ if (decoded) return decoded;
41124
+ r.pos = offsets[i];
41125
+ const params2 = [];
41126
+ r.params(n, params2);
41127
+ return decoded = params2;
41128
+ }
41129
+ });
41033
41130
  }
41034
41131
  r.done();
41035
- return { mode: mode === 0 ? "Transaction" : "Autocommit", stmts };
41132
+ const batch = { mode: mode === 0 ? "Transaction" : "Autocommit", stmts };
41133
+ validatedPackets.set(batch, { view: r.view, offsets, hasPayload: r.hasPayload });
41134
+ return batch;
41036
41135
  }
41037
41136
  var Writer2 = class {
41038
- bytes = new Uint8Array(128);
41039
- view = new DataView(this.bytes.buffer);
41137
+ bytes;
41138
+ view;
41040
41139
  length = 0;
41041
41140
  values = 0;
41042
- constructor(op2) {
41141
+ constructor(op2, size2 = 128) {
41142
+ if (size2 > APP_INDEX_LIMITS.maxBytes) throw full();
41143
+ this.bytes = new Uint8Array(size2);
41144
+ this.view = new DataView(this.bytes.buffer);
41043
41145
  this.put(MAGIC);
41044
41146
  this.byte(op2);
41045
41147
  }
@@ -41077,8 +41179,9 @@ var Writer2 = class {
41077
41179
  else this.view.setBigUint64(at, n, true);
41078
41180
  }
41079
41181
  data(b) {
41080
- this.u32(b.length);
41081
- this.put(b);
41182
+ const at = this.reserve(4 + b.length);
41183
+ this.view.setUint32(at, b.length, true);
41184
+ this.bytes.set(b, at + 4);
41082
41185
  }
41083
41186
  value(v) {
41084
41187
  if (++this.values > APP_INDEX_LIMITS.maxValues) throw full();
@@ -41115,7 +41218,7 @@ var Writer2 = class {
41115
41218
  };
41116
41219
  function results(out, expected) {
41117
41220
  if (!Array.isArray(out) || out.length !== expected) throw invalid5();
41118
- const w = new Writer2(2);
41221
+ const w = new Writer2(2, resultByteLengths.get(out));
41119
41222
  w.u32(out.length);
41120
41223
  let rows2 = 0;
41121
41224
  for (const r of out) {
@@ -41161,7 +41264,8 @@ var AppBinaryIndexHost = class {
41161
41264
  let expected = null;
41162
41265
  try {
41163
41266
  if (this.needsRecovery) throw invalid5();
41164
- const batch = request2(bytes10);
41267
+ if (bytes10.byteLength > APP_INDEX_LIMITS.maxBytes) throw full();
41268
+ const batch = request2(new Uint8Array(bytes10));
41165
41269
  expected = batch.stmts.length;
41166
41270
  const limits = APP_INDEX_LIMITS;
41167
41271
  return results(this.index.run(batch, limits), expected);
@@ -41192,7 +41296,7 @@ function appSqlHost(host, exports) {
41192
41296
  host.fence();
41193
41297
  throw full();
41194
41298
  }
41195
- const reply = host.run(new Uint8Array(x.memory.buffer, ptr, len).slice());
41299
+ const reply = host.run(new Uint8Array(x.memory.buffer, ptr, len));
41196
41300
  const out = x.alloc(reply.length);
41197
41301
  range(x.memory, out, reply.length);
41198
41302
  if (reentered || out === 0 || out < ptr + len && ptr < out + reply.length) throw invalid5();