@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.
@@ -30000,10 +30000,12 @@ ${end.comment}` : end.comment;
30000
30000
  if (typeof value2 === "number" && Number.isSafeInteger(value2)) return BigInt(value2);
30001
30001
  throw new IndexError("Other", "SQLite integer is not losslessly represented");
30002
30002
  }
30003
- var IndexError, textEncoder2, SQLITE_INTEGER, SQLITE_FLOAT, SQLITE_TEXT, SQLITE_BLOB, SQLITE_NULL, HOST_TABLE, SqliteIndex;
30003
+ var validatedPackets, resultByteLengths, IndexError, textEncoder2, SQLITE_INTEGER, SQLITE_FLOAT, SQLITE_TEXT, SQLITE_BLOB, SQLITE_NULL, SQLITE_ROW, SQLITE_DONE, HOST_TABLE, SqliteIndex;
30004
30004
  var init_sqlite_index = __esm({
30005
30005
  "src/app-storage/sqlite-index.ts"() {
30006
30006
  "use strict";
30007
+ validatedPackets = /* @__PURE__ */ new WeakMap();
30008
+ resultByteLengths = /* @__PURE__ */ new WeakMap();
30007
30009
  IndexError = class extends Error {
30008
30010
  constructor(kind, detail, stmt = null) {
30009
30011
  super(`${kind}${stmt === null ? "" : `@${stmt}`}(${detail})`);
@@ -30022,6 +30024,8 @@ ${end.comment}` : end.comment;
30022
30024
  SQLITE_TEXT = 3;
30023
30025
  SQLITE_BLOB = 4;
30024
30026
  SQLITE_NULL = 5;
30027
+ SQLITE_ROW = 100;
30028
+ SQLITE_DONE = 101;
30025
30029
  HOST_TABLE = "CREATE TABLE IF NOT EXISTS _mdbn_host(k TEXT PRIMARY KEY, v) WITHOUT ROWID";
30026
30030
  SqliteIndex = class _SqliteIndex {
30027
30031
  constructor(sqlite3, db, info) {
@@ -30043,12 +30047,14 @@ ${end.comment}` : end.comment;
30043
30047
  const synchronous = opts.synchronous ?? "NORMAL";
30044
30048
  if (synchronous !== "NORMAL" && synchronous !== "FULL") throw new IndexError("Other", "unsupported synchronous profile");
30045
30049
  if (opts.pragmas !== false) {
30046
- db.exec("PRAGMA page_size=32768");
30050
+ db.exec(synchronous === "FULL" ? "PRAGMA page_size=65536" : "PRAGMA page_size=32768");
30047
30051
  db.exec("PRAGMA locking_mode=EXCLUSIVE");
30048
30052
  db.exec("PRAGMA journal_mode=WAL");
30049
30053
  db.exec(`PRAGMA synchronous=${synchronous}`);
30050
30054
  if (synchronous === "FULL") {
30051
- db.exec("PRAGMA cache_size=-32768; PRAGMA wal_autocheckpoint=4096");
30055
+ db.exec("PRAGMA cache_size=-65536");
30056
+ const frames = db.selectValue("PRAGMA page_size") === 65536 ? 2048 : 4096;
30057
+ db.exec(`PRAGMA wal_autocheckpoint=${frames}`);
30052
30058
  }
30053
30059
  }
30054
30060
  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");
@@ -30081,6 +30087,14 @@ ${end.comment}` : end.comment;
30081
30087
  }
30082
30088
  return st;
30083
30089
  }
30090
+ statementBuffer(params2) {
30091
+ let bytes10 = 0;
30092
+ for (const p of params2) {
30093
+ if (p.kind === "Blob") bytes10 += Math.max(1, p.value.byteLength);
30094
+ if (p.kind === "Text") bytes10 += Math.max(1, p.value.length * 3);
30095
+ }
30096
+ return bytes10 ? this.sqlite3.wasm.alloc(bytes10) : 0;
30097
+ }
30084
30098
  bind(st, params2, buffer) {
30085
30099
  const pointer = st.pointer;
30086
30100
  const wasm = this.sqlite3.wasm;
@@ -30119,6 +30133,40 @@ ${end.comment}` : end.comment;
30119
30133
  if (rc) this.db.checkRc(rc);
30120
30134
  }
30121
30135
  }
30136
+ bindPacket(st, packet, i, base) {
30137
+ const raw = this.sqlite3.wasm.exports;
30138
+ const pointer = st.pointer, view2 = packet.view;
30139
+ let at = packet.offsets[i];
30140
+ const count = view2.getUint32(at - 4, true);
30141
+ for (let idx = 1; idx <= count; idx++) {
30142
+ const tag = view2.getUint8(at++);
30143
+ let rc;
30144
+ switch (tag) {
30145
+ case 0:
30146
+ rc = raw.sqlite3_bind_null(pointer, idx);
30147
+ break;
30148
+ case 1:
30149
+ rc = raw.sqlite3_bind_int64(pointer, idx, view2.getBigInt64(at, true));
30150
+ at += 8;
30151
+ break;
30152
+ case 2:
30153
+ st.bind(idx, view2.getFloat64(at, true));
30154
+ at += 8;
30155
+ continue;
30156
+ case 3:
30157
+ case 4: {
30158
+ const length = view2.getUint32(at, true);
30159
+ at += 4;
30160
+ rc = tag === 3 ? raw.sqlite3_bind_text(pointer, idx, base + at, length, 0) : raw.sqlite3_bind_blob(pointer, idx, base + at, length, 0);
30161
+ at += length;
30162
+ break;
30163
+ }
30164
+ default:
30165
+ throw new IndexError("Other", "invalid tag in validated packet");
30166
+ }
30167
+ if (rc) this.db.checkRc(rc);
30168
+ }
30169
+ }
30122
30170
  column(pointer, i, tag, addBytes) {
30123
30171
  const raw = this.sqlite3.wasm.exports;
30124
30172
  switch (tag) {
@@ -30148,6 +30196,19 @@ ${end.comment}` : end.comment;
30148
30196
  /** `IndexStorage::run`. */
30149
30197
  run(batch, limits) {
30150
30198
  if (this.closed) throw new IndexError("Other", "index closed");
30199
+ const packet = validatedPackets.get(batch);
30200
+ if (!packet?.hasPayload) return this.runBatch(batch, limits, packet);
30201
+ const wasm = this.sqlite3.wasm;
30202
+ const view2 = packet.view;
30203
+ const buffer = wasm.alloc(view2.byteLength + 1);
30204
+ try {
30205
+ wasm.heap8u().set(new Uint8Array(view2.buffer, view2.byteOffset, view2.byteLength), buffer);
30206
+ return this.runBatch(batch, limits, packet, buffer);
30207
+ } finally {
30208
+ wasm.dealloc(buffer);
30209
+ }
30210
+ }
30211
+ runBatch(batch, limits, packet, packetBase = 0) {
30151
30212
  const raw = this.sqlite3.wasm.exports;
30152
30213
  const tx = batch.mode === "Transaction";
30153
30214
  const out = [];
@@ -30167,26 +30228,28 @@ ${end.comment}` : end.comment;
30167
30228
  throw classifyError(e);
30168
30229
  }
30169
30230
  }
30170
- for (let i = 0; i < batch.stmts.length; i++) {
30231
+ for (let i = 0; i < (packet?.offsets.length ?? batch.stmts.length); i++) {
30171
30232
  const s = batch.stmts[i];
30172
30233
  try {
30173
30234
  const st = this.prepare(s.sql);
30174
- let bindBytes = 0;
30175
- for (const p of s.params) {
30176
- if (p.kind === "Blob") bindBytes += Math.max(1, p.value.byteLength);
30177
- if (p.kind === "Text") bindBytes += Math.max(1, p.value.length * 3);
30178
- }
30179
- const buffer = bindBytes ? this.sqlite3.wasm.alloc(bindBytes) : 0;
30235
+ const pointer = st.pointer;
30236
+ let buffer = 0;
30180
30237
  try {
30181
- this.bind(st, s.params, buffer);
30182
- const pointer = st.pointer;
30183
- const columns = st.columnCount;
30238
+ if (packet) {
30239
+ this.bindPacket(st, packet, i, packetBase);
30240
+ } else {
30241
+ const params2 = s.params;
30242
+ buffer = this.statementBuffer(params2);
30243
+ this.bind(st, params2, buffer);
30244
+ }
30245
+ const columns = raw.sqlite3_column_count(pointer);
30184
30246
  if (limits && (!Number.isSafeInteger(columns) || columns < 0 || columns > limits.maxColumns)) {
30185
30247
  throw new IndexError("Full", "index reply column budget exceeded");
30186
30248
  }
30187
30249
  addBytes(24);
30188
30250
  const values = [];
30189
- while (st.step()) {
30251
+ let rc;
30252
+ while ((rc = raw.sqlite3_step(pointer)) === SQLITE_ROW) {
30190
30253
  if (limits && ++rows2 > limits.maxRows) throw new IndexError("Full", "index reply row budget exceeded");
30191
30254
  for (let c = 0; c < columns; c++) {
30192
30255
  if (limits && ++valueCount > limits.maxValues) throw new IndexError("Full", "index reply value budget exceeded");
@@ -30194,6 +30257,7 @@ ${end.comment}` : end.comment;
30194
30257
  values.push(this.column(pointer, c, tag, addBytes));
30195
30258
  }
30196
30259
  }
30260
+ if (rc !== SQLITE_DONE) this.db.checkRc(rc);
30197
30261
  out.push({
30198
30262
  columns,
30199
30263
  values,
@@ -30202,7 +30266,9 @@ ${end.comment}` : end.comment;
30202
30266
  });
30203
30267
  } finally {
30204
30268
  try {
30205
- st.reset(true);
30269
+ const clear = raw.sqlite3_clear_bindings(pointer);
30270
+ const reset = raw.sqlite3_reset(pointer);
30271
+ if (clear || reset) this.db.checkRc(clear || reset);
30206
30272
  } finally {
30207
30273
  if (buffer) this.sqlite3.wasm.dealloc(buffer);
30208
30274
  }
@@ -30228,6 +30294,7 @@ ${end.comment}` : end.comment;
30228
30294
  throw classifyError(e);
30229
30295
  }
30230
30296
  }
30297
+ if (limits) resultByteLengths.set(out, bytes10);
30231
30298
  return out;
30232
30299
  }
30233
30300
  /** Finalize statements and close. A fenced/uncertain handle must not be marked clean. */
@@ -30254,6 +30321,11 @@ ${end.comment}` : end.comment;
30254
30321
  });
30255
30322
 
30256
30323
  // src/app-storage/app-binary-index.ts
30324
+ function validateText(bytes10, view2, at, end) {
30325
+ while (at + 4 <= end && !(view2.getUint32(at) & 2155905152)) at += 4;
30326
+ while (at < end && bytes10[at] < 128) at++;
30327
+ if (at < end) utf83.decode(bytes10.subarray(at, end));
30328
+ }
30257
30329
  function request(bytes10) {
30258
30330
  const r = new Reader2(bytes10);
30259
30331
  if (!r.take(8).every((b, i) => b === MAGIC[i])) throw invalid7();
@@ -30267,19 +30339,35 @@ ${end.comment}` : end.comment;
30267
30339
  if (mode > 1) throw invalid7();
30268
30340
  const count = r.count(APP_INDEX_LIMITS.maxStatements, 8);
30269
30341
  const stmts = [];
30342
+ const offsets = new Uint32Array(count);
30343
+ let total = 0;
30270
30344
  for (let i = 0; i < count; i++) {
30271
30345
  const sql = r.text(APP_INDEX_LIMITS.maxSqlBytes);
30272
30346
  const n = r.count(APP_INDEX_LIMITS.maxParameters, 1);
30273
- const params2 = [];
30274
- for (let j = 0; j < n; j++) params2.push(r.value());
30275
- stmts.push({ sql, params: params2 });
30347
+ total += n;
30348
+ if (total > APP_INDEX_LIMITS.maxValues) throw full();
30349
+ offsets[i] = r.pos;
30350
+ r.params(n);
30351
+ let decoded;
30352
+ stmts.push({
30353
+ sql,
30354
+ get params() {
30355
+ if (decoded) return decoded;
30356
+ r.pos = offsets[i];
30357
+ const params2 = [];
30358
+ r.params(n, params2);
30359
+ return decoded = params2;
30360
+ }
30361
+ });
30276
30362
  }
30277
30363
  r.done();
30278
- return { mode: mode === 0 ? "Transaction" : "Autocommit", stmts };
30364
+ const batch = { mode: mode === 0 ? "Transaction" : "Autocommit", stmts };
30365
+ validatedPackets.set(batch, { view: r.view, offsets, hasPayload: r.hasPayload });
30366
+ return batch;
30279
30367
  }
30280
30368
  function results(out, expected) {
30281
30369
  if (!Array.isArray(out) || out.length !== expected) throw invalid7();
30282
- const w = new Writer2(2);
30370
+ const w = new Writer2(2, resultByteLengths.get(out));
30283
30371
  w.u32(out.length);
30284
30372
  let rows2 = 0;
30285
30373
  for (const r of out) {
@@ -30329,7 +30417,7 @@ ${end.comment}` : end.comment;
30329
30417
  host.fence();
30330
30418
  throw full();
30331
30419
  }
30332
- const reply = host.run(new Uint8Array(x.memory.buffer, ptr, len).slice());
30420
+ const reply = host.run(new Uint8Array(x.memory.buffer, ptr, len));
30333
30421
  const out = x.alloc(reply.length);
30334
30422
  range(x.memory, out, reply.length);
30335
30423
  if (reentered || out === 0 || out < ptr + len && ptr < out + reply.length) throw invalid7();
@@ -30368,12 +30456,11 @@ ${end.comment}` : end.comment;
30368
30456
  Reader2 = class {
30369
30457
  constructor(bytes10) {
30370
30458
  this.bytes = bytes10;
30371
- if (bytes10.length > APP_INDEX_LIMITS.maxBytes) throw full();
30372
30459
  this.view = new DataView(bytes10.buffer, bytes10.byteOffset, bytes10.byteLength);
30373
30460
  }
30374
30461
  bytes;
30375
30462
  pos = 0;
30376
- values = 0;
30463
+ hasPayload = false;
30377
30464
  view;
30378
30465
  advance(n) {
30379
30466
  if (!Number.isSafeInteger(n) || n < 0 || n > this.bytes.length - this.pos) throw invalid7();
@@ -30403,27 +30490,39 @@ ${end.comment}` : end.comment;
30403
30490
  text(max) {
30404
30491
  return utf83.decode(this.data(max));
30405
30492
  }
30406
- value() {
30407
- if (++this.values > APP_INDEX_LIMITS.maxValues) throw full();
30408
- switch (this.byte()) {
30409
- case 0:
30410
- return { kind: "Null" };
30411
- case 1: {
30412
- const at = this.advance(8);
30413
- return { kind: "Integer", value: this.view.getBigInt64(at, true) };
30414
- }
30415
- case 2: {
30416
- const at = this.advance(8);
30417
- const value2 = this.view.getFloat64(at, true);
30418
- if (!Number.isFinite(value2)) throw invalid7();
30419
- return { kind: "Real", value: value2 };
30420
- }
30421
- case 3:
30422
- return { kind: "Text", value: this.text(APP_INDEX_LIMITS.maxBytes) };
30423
- case 4:
30424
- return { kind: "Blob", value: this.data(APP_INDEX_LIMITS.maxBytes) };
30425
- default:
30426
- throw invalid7();
30493
+ params(count, out) {
30494
+ for (let i = 0; i < count; i++) {
30495
+ const tag = this.byte();
30496
+ switch (tag) {
30497
+ case 0:
30498
+ out?.push({ kind: "Null" });
30499
+ break;
30500
+ case 1: {
30501
+ const at = this.advance(8);
30502
+ out?.push({ kind: "Integer", value: this.view.getBigInt64(at, true) });
30503
+ break;
30504
+ }
30505
+ case 2: {
30506
+ const value2 = this.view.getFloat64(this.advance(8), true);
30507
+ if (!Number.isFinite(value2)) throw invalid7();
30508
+ out?.push({ kind: "Real", value: value2 });
30509
+ break;
30510
+ }
30511
+ case 3:
30512
+ case 4: {
30513
+ const at = this.advance(this.count(APP_INDEX_LIMITS.maxBytes, 1));
30514
+ this.hasPayload = true;
30515
+ if (out) {
30516
+ const data = this.bytes.subarray(at, this.pos);
30517
+ out.push(tag === 3 ? { kind: "Text", value: utf83.decode(data) } : { kind: "Blob", value: data.slice() });
30518
+ } else if (tag === 3) {
30519
+ validateText(this.bytes, this.view, at, this.pos);
30520
+ }
30521
+ break;
30522
+ }
30523
+ default:
30524
+ throw invalid7();
30525
+ }
30427
30526
  }
30428
30527
  }
30429
30528
  done() {
@@ -30431,11 +30530,14 @@ ${end.comment}` : end.comment;
30431
30530
  }
30432
30531
  };
30433
30532
  Writer2 = class {
30434
- bytes = new Uint8Array(128);
30435
- view = new DataView(this.bytes.buffer);
30533
+ bytes;
30534
+ view;
30436
30535
  length = 0;
30437
30536
  values = 0;
30438
- constructor(op2) {
30537
+ constructor(op2, size2 = 128) {
30538
+ if (size2 > APP_INDEX_LIMITS.maxBytes) throw full();
30539
+ this.bytes = new Uint8Array(size2);
30540
+ this.view = new DataView(this.bytes.buffer);
30439
30541
  this.put(MAGIC);
30440
30542
  this.byte(op2);
30441
30543
  }
@@ -30473,8 +30575,9 @@ ${end.comment}` : end.comment;
30473
30575
  else this.view.setBigUint64(at, n, true);
30474
30576
  }
30475
30577
  data(b) {
30476
- this.u32(b.length);
30477
- this.put(b);
30578
+ const at = this.reserve(4 + b.length);
30579
+ this.view.setUint32(at, b.length, true);
30580
+ this.bytes.set(b, at + 4);
30478
30581
  }
30479
30582
  value(v) {
30480
30583
  if (++this.values > APP_INDEX_LIMITS.maxValues) throw full();
@@ -30526,7 +30629,8 @@ ${end.comment}` : end.comment;
30526
30629
  let expected = null;
30527
30630
  try {
30528
30631
  if (this.needsRecovery) throw invalid7();
30529
- const batch = request(bytes10);
30632
+ if (bytes10.byteLength > APP_INDEX_LIMITS.maxBytes) throw full();
30633
+ const batch = request(new Uint8Array(bytes10));
30530
30634
  expected = batch.stmts.length;
30531
30635
  const limits = APP_INDEX_LIMITS;
30532
30636
  return results(this.index.run(batch, limits), expected);