@fireproof/core 0.17.2 → 0.17.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -4129,22 +4129,6 @@ var Fireproof = (() => {
4129
4129
  }
4130
4130
  }
4131
4131
  };
4132
- var MultiBlockFetcher = class {
4133
- /** @type {API.BlockFetcher[]} */
4134
- #fetchers;
4135
- /** @param {API.BlockFetcher[]} fetchers */
4136
- constructor(...fetchers) {
4137
- this.#fetchers = fetchers;
4138
- }
4139
- /** @type {API.BlockFetcher['get']} */
4140
- async get(link2) {
4141
- for (const f of this.#fetchers) {
4142
- const v = await f.get(link2);
4143
- if (v)
4144
- return v;
4145
- }
4146
- }
4147
- };
4148
4132
 
4149
4133
  // ../../node_modules/.pnpm/yocto-queue@1.0.0/node_modules/yocto-queue/index.js
4150
4134
  var Node = class {
@@ -10980,7 +10964,7 @@ You can use close({ resize: true }) to resize header`);
10980
10964
  }
10981
10965
  };
10982
10966
 
10983
- // ../../node_modules/.pnpm/@web3-storage+pail@0.4.0/node_modules/@web3-storage/pail/src/clock/index.js
10967
+ // ../../node_modules/.pnpm/@web3-storage+pail@0.4.1/node_modules/@web3-storage/pail/src/clock/index.js
10984
10968
  var advance = async (blocks, head, event) => {
10985
10969
  const events = new EventFetcher(blocks);
10986
10970
  const headmap = new Map(head.map((cid) => [cid.toString(), cid]));
@@ -11054,7 +11038,6 @@ You can use close({ resize: true }) to resize header`);
11054
11038
  return true;
11055
11039
  const [{ value: aevent }, { value: bevent }] = await Promise.all([events.get(a), events.get(b)]);
11056
11040
  const links3 = [...aevent.parents];
11057
- const seen = /* @__PURE__ */ new Set();
11058
11041
  while (links3.length) {
11059
11042
  const link2 = links3.shift();
11060
11043
  if (!link2)
@@ -11063,9 +11046,6 @@ You can use close({ resize: true }) to resize header`);
11063
11046
  return true;
11064
11047
  if (bevent.parents.some((p) => link2.toString() === p.toString()))
11065
11048
  continue;
11066
- if (seen.has(link2.toString()))
11067
- continue;
11068
- seen.add(link2.toString());
11069
11049
  const { value: event } = await events.get(link2);
11070
11050
  links3.push(...event.parents);
11071
11051
  }
@@ -11106,7 +11086,7 @@ You can use close({ resize: true }) to resize header`);
11106
11086
  };
11107
11087
  var shortLink = (l) => `${String(l).slice(0, 4)}..${String(l).slice(-4)}`;
11108
11088
 
11109
- // ../../node_modules/.pnpm/@web3-storage+pail@0.4.0/node_modules/@web3-storage/pail/src/shard.js
11089
+ // ../../node_modules/.pnpm/@web3-storage+pail@0.4.1/node_modules/@web3-storage/pail/src/shard.js
11110
11090
  var MaxKeyLength = 64;
11111
11091
  var MaxShardSize = 512 * 1024;
11112
11092
  var CID_TAG2 = new Token(Type.tag, 42);
@@ -11237,41 +11217,8 @@ You can use close({ resize: true }) to resize header`);
11237
11217
  }
11238
11218
  }
11239
11219
  };
11240
- var encodedLength = (shard) => {
11241
- let entriesLength = 0;
11242
- for (const entry of shard.entries) {
11243
- entriesLength += entryEncodedLength(entry);
11244
- }
11245
- const tokens = [
11246
- new Token(Type.map, 3),
11247
- new Token(Type.string, "entries"),
11248
- new Token(Type.array, shard.entries.length),
11249
- new Token(Type.string, "maxKeyLength"),
11250
- new Token(Type.uint, shard.maxKeyLength),
11251
- new Token(Type.string, "maxSize"),
11252
- new Token(Type.uint, shard.maxSize)
11253
- ];
11254
- return tokensToLength(tokens) + entriesLength;
11255
- };
11256
- var entryEncodedLength = (entry) => {
11257
- const tokens = [
11258
- new Token(Type.array, entry.length),
11259
- new Token(Type.string, entry[0])
11260
- ];
11261
- if (Array.isArray(entry[1])) {
11262
- tokens.push(new Token(Type.array, entry[1].length));
11263
- for (const link2 of entry[1]) {
11264
- tokens.push(CID_TAG2);
11265
- tokens.push(new Token(Type.bytes, { length: link2.byteLength + 1 }));
11266
- }
11267
- } else {
11268
- tokens.push(CID_TAG2);
11269
- tokens.push(new Token(Type.bytes, { length: entry[1].byteLength + 1 }));
11270
- }
11271
- return tokensToLength(tokens);
11272
- };
11273
11220
 
11274
- // ../../node_modules/.pnpm/@web3-storage+pail@0.4.0/node_modules/@web3-storage/pail/src/index.js
11221
+ // ../../node_modules/.pnpm/@web3-storage+pail@0.4.1/node_modules/@web3-storage/pail/src/index.js
11275
11222
  var put = async (blocks, root2, key, value) => {
11276
11223
  const shards = new ShardFetcher(blocks);
11277
11224
  const rshard = await shards.get(root2);
@@ -11474,15 +11421,118 @@ You can use close({ resize: true }) to resize header`);
11474
11421
  return [shard];
11475
11422
  };
11476
11423
 
11477
- // ../../node_modules/.pnpm/@web3-storage+pail@0.4.0/node_modules/@web3-storage/pail/src/batch/shard.js
11424
+ // ../../node_modules/.pnpm/@web3-storage+pail@0.4.1/node_modules/@web3-storage/pail/src/block.js
11425
+ var MemoryBlockstore2 = class {
11426
+ /** @type {Map<string, Uint8Array>} */
11427
+ #blocks = /* @__PURE__ */ new Map();
11428
+ /**
11429
+ * @param {Array<import('multiformats').Block>} [blocks]
11430
+ */
11431
+ constructor(blocks) {
11432
+ if (blocks) {
11433
+ this.#blocks = new Map(blocks.map((b) => [b.cid.toString(), b.bytes]));
11434
+ }
11435
+ }
11436
+ /** @type {API.BlockFetcher['get']} */
11437
+ async get(cid) {
11438
+ const bytes = this.#blocks.get(cid.toString());
11439
+ if (!bytes)
11440
+ return;
11441
+ return { cid, bytes };
11442
+ }
11443
+ /**
11444
+ * @param {API.UnknownLink} cid
11445
+ * @param {Uint8Array} bytes
11446
+ */
11447
+ async put(cid, bytes) {
11448
+ this.#blocks.set(cid.toString(), bytes);
11449
+ }
11450
+ /**
11451
+ * @param {API.UnknownLink} cid
11452
+ * @param {Uint8Array} bytes
11453
+ */
11454
+ putSync(cid, bytes) {
11455
+ this.#blocks.set(cid.toString(), bytes);
11456
+ }
11457
+ /** @param {API.UnknownLink} cid */
11458
+ async delete(cid) {
11459
+ this.#blocks.delete(cid.toString());
11460
+ }
11461
+ /** @param {API.UnknownLink} cid */
11462
+ deleteSync(cid) {
11463
+ this.#blocks.delete(cid.toString());
11464
+ }
11465
+ *entries() {
11466
+ for (const [str, bytes] of this.#blocks) {
11467
+ yield { cid: parse(str), bytes };
11468
+ }
11469
+ }
11470
+ };
11471
+ var MultiBlockFetcher = class {
11472
+ /** @type {API.BlockFetcher[]} */
11473
+ #fetchers;
11474
+ /** @param {API.BlockFetcher[]} fetchers */
11475
+ constructor(...fetchers) {
11476
+ this.#fetchers = fetchers;
11477
+ }
11478
+ /** @type {API.BlockFetcher['get']} */
11479
+ async get(link2) {
11480
+ for (const f of this.#fetchers) {
11481
+ const v = await f.get(link2);
11482
+ if (v)
11483
+ return v;
11484
+ }
11485
+ }
11486
+ };
11487
+
11488
+ // ../../node_modules/.pnpm/@web3-storage+pail@0.4.1/node_modules/@web3-storage/pail/src/batch/shard.js
11489
+ var ShardLinkByteLength = 36;
11490
+ var CID_TAG3 = new Token(Type.tag, 42);
11478
11491
  var create8 = (init2) => ({
11479
11492
  base: init2?.base,
11480
11493
  prefix: init2?.prefix ?? "",
11481
- entries: init2?.entries ?? [],
11494
+ entries: [...init2?.entries ?? []],
11482
11495
  ...configure(init2)
11483
11496
  });
11497
+ var encodedLength = (shard) => {
11498
+ let entriesLength = 0;
11499
+ for (const entry of shard.entries) {
11500
+ entriesLength += entryEncodedLength(entry);
11501
+ }
11502
+ const tokens = [
11503
+ new Token(Type.map, 3),
11504
+ new Token(Type.string, "entries"),
11505
+ new Token(Type.array, shard.entries.length),
11506
+ new Token(Type.string, "maxKeyLength"),
11507
+ new Token(Type.uint, shard.maxKeyLength),
11508
+ new Token(Type.string, "maxSize"),
11509
+ new Token(Type.uint, shard.maxSize)
11510
+ ];
11511
+ return tokensToLength(tokens) + entriesLength;
11512
+ };
11513
+ var entryEncodedLength = (entry) => {
11514
+ const tokens = [
11515
+ new Token(Type.array, entry.length),
11516
+ new Token(Type.string, entry[0])
11517
+ ];
11518
+ if (Array.isArray(entry[1])) {
11519
+ tokens.push(new Token(Type.array, entry[1].length));
11520
+ for (const item of entry[1]) {
11521
+ tokens.push(CID_TAG3);
11522
+ if (isLink(item)) {
11523
+ tokens.push(new Token(Type.bytes, { length: item.byteLength + 1 }));
11524
+ } else {
11525
+ tokens.push(new Token(Type.bytes, { length: ShardLinkByteLength + 1 }));
11526
+ }
11527
+ }
11528
+ } else {
11529
+ tokens.push(CID_TAG3);
11530
+ tokens.push(new Token(Type.bytes, { length: entry[1].byteLength + 1 }));
11531
+ }
11532
+ return tokensToLength(tokens);
11533
+ };
11484
11534
 
11485
- // ../../node_modules/.pnpm/@web3-storage+pail@0.4.0/node_modules/@web3-storage/pail/src/batch/index.js
11535
+ // ../../node_modules/.pnpm/@web3-storage+pail@0.4.1/node_modules/@web3-storage/pail/src/batch/index.js
11486
11536
  var Batcher = class _Batcher {
11487
11537
  #committed = false;
11488
11538
  /**
@@ -11497,7 +11547,7 @@ You can use close({ resize: true }) to resize header`);
11497
11547
  constructor({ blocks, entries: entries3, prefix, maxSize, maxKeyLength, base: base4 }) {
11498
11548
  this.blocks = blocks;
11499
11549
  this.prefix = prefix;
11500
- this.entries = entries3;
11550
+ this.entries = [...entries3];
11501
11551
  this.base = base4;
11502
11552
  this.maxSize = maxSize;
11503
11553
  this.maxKeyLength = maxKeyLength;
@@ -11527,7 +11577,7 @@ You can use close({ resize: true }) to resize header`);
11527
11577
  static async create({ blocks, link: link2, prefix }) {
11528
11578
  const shards = new ShardFetcher(blocks);
11529
11579
  const base4 = await shards.get(link2);
11530
- return new _Batcher({ blocks, entries: base4.value.entries, prefix, base: base4, ...configure(base4.value) });
11580
+ return new _Batcher({ blocks, prefix, base: base4, ...base4.value });
11531
11581
  }
11532
11582
  };
11533
11583
  var put2 = async (blocks, shard, key, value) => {
@@ -11564,7 +11614,7 @@ You can use close({ resize: true }) to resize header`);
11564
11614
  entry = [pfxskeys[0].key, [batcher]];
11565
11615
  }
11566
11616
  shard.entries = putEntry(asShardEntries(shard.entries), asShardEntry(entry));
11567
- const size = encodedLength(withEntries(asShardEntries(shard.entries), shard));
11617
+ const size = encodedLength(shard);
11568
11618
  if (size > shard.maxSize) {
11569
11619
  const common = findCommonPrefix(
11570
11620
  asShardEntries(shard.entries),
@@ -11601,14 +11651,16 @@ You can use close({ resize: true }) to resize header`);
11601
11651
  }
11602
11652
  };
11603
11653
  var traverse2 = async (shards, key, shard) => {
11604
- for (const e of shard.entries) {
11605
- const [k, v] = e;
11654
+ for (let i = 0; i < shard.entries.length; i++) {
11655
+ const [k, v] = shard.entries[i];
11606
11656
  if (key <= k)
11607
11657
  break;
11608
11658
  if (key.startsWith(k) && Array.isArray(v)) {
11609
11659
  if (isShardLink(v[0])) {
11610
11660
  const blk = await shards.get(v[0], shard.prefix + k);
11611
- v[0] = create8({ base: blk, prefix: blk.prefix, ...blk.value });
11661
+ const batcher = create8({ base: blk, prefix: blk.prefix, ...blk.value });
11662
+ shard.entries[i] = [k, v[1] == null ? [batcher] : [batcher, v[1]]];
11663
+ return traverse2(shards, key.slice(k.length), batcher);
11612
11664
  }
11613
11665
  return traverse2(shards, key.slice(k.length), v[0]);
11614
11666
  }
@@ -11662,9 +11714,9 @@ You can use close({ resize: true }) to resize header`);
11662
11714
  static code = "ERR_BATCH_COMMITTED";
11663
11715
  };
11664
11716
 
11665
- // ../../node_modules/.pnpm/@web3-storage+pail@0.4.0/node_modules/@web3-storage/pail/src/crdt/index.js
11717
+ // ../../node_modules/.pnpm/@web3-storage+pail@0.4.1/node_modules/@web3-storage/pail/src/crdt/index.js
11666
11718
  var put3 = async (blocks, head, key, value) => {
11667
- const mblocks = new MemoryBlockstore();
11719
+ const mblocks = new MemoryBlockstore2();
11668
11720
  blocks = new MultiBlockFetcher(mblocks, blocks);
11669
11721
  if (!head.length) {
11670
11722
  const shard = await ShardBlock.create();
@@ -11748,7 +11800,7 @@ You can use close({ resize: true }) to resize header`);
11748
11800
  var root = async (blocks, head) => {
11749
11801
  if (!head.length)
11750
11802
  throw new Error("cannot determine root of headless clock");
11751
- const mblocks = new MemoryBlockstore();
11803
+ const mblocks = new MemoryBlockstore2();
11752
11804
  blocks = new MultiBlockFetcher(mblocks, blocks);
11753
11805
  const events = new EventFetcher(blocks);
11754
11806
  if (head.length === 1) {
@@ -11807,7 +11859,7 @@ You can use close({ resize: true }) to resize header`);
11807
11859
  return;
11808
11860
  const result = await root(blocks, head);
11809
11861
  if (result.additions.length) {
11810
- blocks = new MultiBlockFetcher(new MemoryBlockstore(result.additions), blocks);
11862
+ blocks = new MultiBlockFetcher(new MemoryBlockstore2(result.additions), blocks);
11811
11863
  }
11812
11864
  return get2(blocks, result.root, key);
11813
11865
  };
@@ -11816,7 +11868,7 @@ You can use close({ resize: true }) to resize header`);
11816
11868
  return;
11817
11869
  const result = await root(blocks, head);
11818
11870
  if (result.additions.length) {
11819
- blocks = new MultiBlockFetcher(new MemoryBlockstore(result.additions), blocks);
11871
+ blocks = new MultiBlockFetcher(new MemoryBlockstore2(result.additions), blocks);
11820
11872
  }
11821
11873
  yield* entries(blocks, result.root, options);
11822
11874
  };
@@ -11900,7 +11952,7 @@ You can use close({ resize: true }) to resize header`);
11900
11952
  return acc.concat(...rest);
11901
11953
  };
11902
11954
 
11903
- // ../../node_modules/.pnpm/@web3-storage+pail@0.4.0/node_modules/@web3-storage/pail/src/crdt/batch/index.js
11955
+ // ../../node_modules/.pnpm/@web3-storage+pail@0.4.1/node_modules/@web3-storage/pail/src/crdt/batch/index.js
11904
11956
  var Batcher2 = class _Batcher {
11905
11957
  #committed = false;
11906
11958
  /**
@@ -11919,7 +11971,7 @@ You can use close({ resize: true }) to resize header`);
11919
11971
  this.blocks = blocks;
11920
11972
  this.head = head;
11921
11973
  this.prefix = prefix;
11922
- this.entries = entries3;
11974
+ this.entries = [...entries3];
11923
11975
  this.base = base4;
11924
11976
  this.maxSize = maxSize;
11925
11977
  this.maxKeyLength = maxKeyLength;
@@ -11945,7 +11997,7 @@ You can use close({ resize: true }) to resize header`);
11945
11997
  const res = await commit(this);
11946
11998
  const data = { type: "batch", ops: this.ops, root: res.root };
11947
11999
  const event = await EventBlock.create(data, this.head);
11948
- const mblocks = new MemoryBlockstore();
12000
+ const mblocks = new MemoryBlockstore2();
11949
12001
  const blocks = new MultiBlockFetcher(mblocks, this.blocks);
11950
12002
  mblocks.putSync(event.cid, event.bytes);
11951
12003
  const head = await advance(blocks, this.head, event.cid);
@@ -11985,7 +12037,7 @@ You can use close({ resize: true }) to resize header`);
11985
12037
  * @param {string} init.prefix
11986
12038
  */
11987
12039
  static async create({ blocks, head, prefix }) {
11988
- const mblocks = new MemoryBlockstore();
12040
+ const mblocks = new MemoryBlockstore2();
11989
12041
  blocks = new MultiBlockFetcher(mblocks, blocks);
11990
12042
  if (!head.length) {
11991
12043
  const base5 = await ShardBlock.create();
@@ -20431,10 +20483,8 @@ You can use close({ resize: true }) to resize header`);
20431
20483
 
20432
20484
  // src/crdt-helpers.ts
20433
20485
  function time(tag2) {
20434
- console.time(tag2);
20435
20486
  }
20436
20487
  function timeEnd(tag2) {
20437
- console.timeEnd(tag2);
20438
20488
  }
20439
20489
  async function applyBulkUpdateToCrdt(tblocks, head, updates) {
20440
20490
  let result = null;
@@ -21482,7 +21532,7 @@ You can use close({ resize: true }) to resize header`);
21482
21532
  try {
21483
21533
  head = await advance(tblocks, head, cid);
21484
21534
  } catch (e) {
21485
- console.error("failed to advance", cid.toString(), e);
21535
+ console.log("failed to advance head:", cid.toString());
21486
21536
  continue;
21487
21537
  }
21488
21538
  }