@peerbit/indexer-sqlite3 2.1.2-07ba572 → 2.1.2-3f16953

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.
package/dist/index.min.js CHANGED
@@ -19003,12 +19003,15 @@ function hashBlocks(w, v2, p, pos, len) {
19003
19003
  // ../../crypto/dist/src/utils.js
19004
19004
  var import_libsodium_wrappers = __toESM(require_libsodium_wrappers(), 1);
19005
19005
  var fromHexString = (hexString) => import_libsodium_wrappers.default.from_hex(hexString);
19006
- var toHexString = (bytes) => import_libsodium_wrappers.default.to_hex(bytes);
19006
+ var asU8 = (bytes) => {
19007
+ return bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.byteLength);
19008
+ };
19009
+ var toHexString = (bytes) => import_libsodium_wrappers.default.to_hex(asU8(bytes));
19007
19010
  var toBase64 = (arr) => {
19008
- return import_libsodium_wrappers.default.to_base64(arr, import_libsodium_wrappers.default.base64_variants.ORIGINAL);
19011
+ return import_libsodium_wrappers.default.to_base64(asU8(arr), import_libsodium_wrappers.default.base64_variants.ORIGINAL);
19009
19012
  };
19010
19013
  var toBase58 = (arr) => {
19011
- return base58btc.baseEncode(arr);
19014
+ return base58btc.baseEncode(asU8(arr));
19012
19015
  };
19013
19016
 
19014
19017
  // ../../crypto/dist/src/hash.browser.js
@@ -19503,10 +19506,11 @@ var toId = (obj) => {
19503
19506
  }
19504
19507
  throw new Error("BigInt is not less than 2^64 - 1. Max value is " + (2 ** 64 - 1) + ". Provided value: " + obj);
19505
19508
  }
19506
- if (obj instanceof Uint8Array) {
19507
- return new Uint8ArrayKey(obj);
19509
+ if (obj instanceof Uint8Array || ArrayBuffer.isView(obj)) {
19510
+ const bytes = obj instanceof Uint8Array ? obj : new Uint8Array(obj.buffer, obj.byteOffset, obj.byteLength);
19511
+ return new Uint8ArrayKey(bytes);
19508
19512
  }
19509
- throw new Error("Unexpected index key: " + typeof obj + ", expected: string, number, bigint or Uint8Array");
19513
+ throw new Error("Unexpected index key: " + typeof obj + ", expected: string, number, bigint, Uint8Array or ArrayBufferView");
19510
19514
  };
19511
19515
 
19512
19516
  // ../../../../node_modules/.pnpm/uuid@10.0.0/node_modules/uuid/dist/esm-browser/stringify.js
@@ -21938,6 +21942,30 @@ async function getIgnoreFK(stmt, values) {
21938
21942
  }
21939
21943
  var SQLLiteIndex = class {
21940
21944
  properties;
21945
+ // SQLite writes are inherently serialized per connection.
21946
+ // We still need an explicit async barrier because our API is async and
21947
+ // awaits between statements (root insert -> many child inserts). Without
21948
+ // a barrier, concurrent `put()` and `del()` can interleave mid-insert and
21949
+ // create large volumes of FK constraint noise (and occasional timeouts in
21950
+ // browser/webworker runners).
21951
+ // TODO(perf): This is intentionally coarse-grained for correctness.
21952
+ // Possible optimizations:
21953
+ // 1) wrap nested writes in explicit transactions to reduce lock time;
21954
+ // 2) use table/key-scoped write queues when overlap detection is available.
21955
+ // Any relaxation must keep concurrent put/del stability across all runners.
21956
+ _writeBarrier = Promise.resolve();
21957
+ async withWriteBarrier(fn) {
21958
+ const prev = this._writeBarrier;
21959
+ let release;
21960
+ const next = new Promise((r) => release = r);
21961
+ this._writeBarrier = prev.then(() => next, () => next);
21962
+ await prev.catch(() => void 0);
21963
+ try {
21964
+ return await fn();
21965
+ } finally {
21966
+ release();
21967
+ }
21968
+ }
21941
21969
  primaryKeyArr;
21942
21970
  primaryKeyString;
21943
21971
  planner;
@@ -22118,28 +22146,30 @@ var SQLLiteIndex = class {
22118
22146
  return void 0;
22119
22147
  }
22120
22148
  async put(value, _id) {
22121
- const classOfValue = value.constructor;
22122
- return insert(async (values, table) => {
22123
- let preId = values[table.primaryIndex];
22124
- let statement = void 0;
22125
- try {
22126
- if (preId != null) {
22127
- statement = this.properties.db.statements.get(replaceStatementKey(table));
22128
- this.fkMode === "race-tolerant" ? await runIgnoreFK(statement, values) : await statement.run(values);
22129
- return preId;
22130
- } else {
22131
- statement = this.properties.db.statements.get(putStatementKey(table));
22132
- const out = this.fkMode === "race-tolerant" ? await getIgnoreFK(statement, values) : await statement.get(values);
22133
- if (out == null) {
22134
- return void 0;
22149
+ return this.withWriteBarrier(async () => {
22150
+ const classOfValue = value.constructor;
22151
+ return insert(async (values, table) => {
22152
+ let preId = values[table.primaryIndex];
22153
+ let statement = void 0;
22154
+ try {
22155
+ if (preId != null) {
22156
+ statement = this.properties.db.statements.get(replaceStatementKey(table));
22157
+ this.fkMode === "race-tolerant" ? await runIgnoreFK(statement, values) : await statement.run(values);
22158
+ return preId;
22159
+ } else {
22160
+ statement = this.properties.db.statements.get(putStatementKey(table));
22161
+ const out = this.fkMode === "race-tolerant" ? await getIgnoreFK(statement, values) : await statement.get(values);
22162
+ if (out == null) {
22163
+ return void 0;
22164
+ }
22165
+ return out[table.primary];
22135
22166
  }
22136
- return out[table.primary];
22167
+ } finally {
22168
+ await statement?.reset?.();
22137
22169
  }
22138
- } finally {
22139
- await statement?.reset?.();
22140
- }
22141
- }, value, this.tables, resolveTable(this.scopeString ? [this.scopeString] : [], this.tables, classOfValue, true), getSchema(classOfValue).fields, (_fn) => {
22142
- throw new Error("Unexpected");
22170
+ }, value, this.tables, resolveTable(this.scopeString ? [this.scopeString] : [], this.tables, classOfValue, true), getSchema(classOfValue).fields, (_fn) => {
22171
+ throw new Error("Unexpected");
22172
+ });
22143
22173
  });
22144
22174
  }
22145
22175
  iterate(request, options) {
@@ -22249,30 +22279,32 @@ var SQLLiteIndex = class {
22249
22279
  return this.count();
22250
22280
  }
22251
22281
  async del(query) {
22252
- let ret = [];
22253
- let once = false;
22254
- let lastError = void 0;
22255
- for (const table of this._rootTables) {
22256
- try {
22257
- const { sql, bindable } = convertDeleteRequestToQuery(query, this.tables, table);
22258
- const stmt = await this.properties.db.prepare(sql, sql);
22259
- const results = await stmt.all(bindable);
22260
- for (const result of results) {
22261
- ret.push(toId(convertFromSQLType(result[table.primary], table.primaryField.from.type)));
22262
- }
22263
- once = true;
22264
- } catch (error2) {
22265
- if (error2 instanceof MissingFieldError) {
22266
- lastError = error2;
22267
- continue;
22282
+ return this.withWriteBarrier(async () => {
22283
+ let ret = [];
22284
+ let once = false;
22285
+ let lastError = void 0;
22286
+ for (const table of this._rootTables) {
22287
+ try {
22288
+ const { sql, bindable } = convertDeleteRequestToQuery(query, this.tables, table);
22289
+ const stmt = await this.properties.db.prepare(sql, sql);
22290
+ const results = await stmt.all(bindable);
22291
+ for (const result of results) {
22292
+ ret.push(toId(convertFromSQLType(result[table.primary], table.primaryField.from.type)));
22293
+ }
22294
+ once = true;
22295
+ } catch (error2) {
22296
+ if (error2 instanceof MissingFieldError) {
22297
+ lastError = error2;
22298
+ continue;
22299
+ }
22300
+ throw error2;
22268
22301
  }
22269
- throw error2;
22270
22302
  }
22271
- }
22272
- if (!once) {
22273
- throw lastError;
22274
- }
22275
- return ret;
22303
+ if (!once) {
22304
+ throw lastError;
22305
+ }
22306
+ return ret;
22307
+ });
22276
22308
  }
22277
22309
  async sum(query) {
22278
22310
  let ret = void 0;