@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.
@@ -4136,22 +4136,6 @@ var Fireproof = (() => {
4136
4136
  }
4137
4137
  }
4138
4138
  };
4139
- var MultiBlockFetcher = class {
4140
- /** @type {API.BlockFetcher[]} */
4141
- #fetchers;
4142
- /** @param {API.BlockFetcher[]} fetchers */
4143
- constructor(...fetchers) {
4144
- this.#fetchers = fetchers;
4145
- }
4146
- /** @type {API.BlockFetcher['get']} */
4147
- async get(link2) {
4148
- for (const f of this.#fetchers) {
4149
- const v = await f.get(link2);
4150
- if (v)
4151
- return v;
4152
- }
4153
- }
4154
- };
4155
4139
 
4156
4140
  // ../../node_modules/.pnpm/yocto-queue@1.0.0/node_modules/yocto-queue/index.js
4157
4141
  var Node = class {
@@ -19034,7 +19018,7 @@ ${key.data.toString("base64")}
19034
19018
  return await (0, import_promises.writeFile)(path, data);
19035
19019
  }
19036
19020
 
19037
- // ../../node_modules/.pnpm/@web3-storage+pail@0.4.0/node_modules/@web3-storage/pail/src/clock/index.js
19021
+ // ../../node_modules/.pnpm/@web3-storage+pail@0.4.1/node_modules/@web3-storage/pail/src/clock/index.js
19038
19022
  var advance = async (blocks, head, event) => {
19039
19023
  const events = new EventFetcher(blocks);
19040
19024
  const headmap = new Map(head.map((cid) => [cid.toString(), cid]));
@@ -19108,7 +19092,6 @@ ${key.data.toString("base64")}
19108
19092
  return true;
19109
19093
  const [{ value: aevent }, { value: bevent }] = await Promise.all([events.get(a), events.get(b)]);
19110
19094
  const links3 = [...aevent.parents];
19111
- const seen = /* @__PURE__ */ new Set();
19112
19095
  while (links3.length) {
19113
19096
  const link2 = links3.shift();
19114
19097
  if (!link2)
@@ -19117,9 +19100,6 @@ ${key.data.toString("base64")}
19117
19100
  return true;
19118
19101
  if (bevent.parents.some((p) => link2.toString() === p.toString()))
19119
19102
  continue;
19120
- if (seen.has(link2.toString()))
19121
- continue;
19122
- seen.add(link2.toString());
19123
19103
  const { value: event } = await events.get(link2);
19124
19104
  links3.push(...event.parents);
19125
19105
  }
@@ -19160,7 +19140,7 @@ ${key.data.toString("base64")}
19160
19140
  };
19161
19141
  var shortLink = (l) => `${String(l).slice(0, 4)}..${String(l).slice(-4)}`;
19162
19142
 
19163
- // ../../node_modules/.pnpm/@web3-storage+pail@0.4.0/node_modules/@web3-storage/pail/src/shard.js
19143
+ // ../../node_modules/.pnpm/@web3-storage+pail@0.4.1/node_modules/@web3-storage/pail/src/shard.js
19164
19144
  var MaxKeyLength = 64;
19165
19145
  var MaxShardSize = 512 * 1024;
19166
19146
  var CID_TAG2 = new Token(Type.tag, 42);
@@ -19291,41 +19271,8 @@ ${key.data.toString("base64")}
19291
19271
  }
19292
19272
  }
19293
19273
  };
19294
- var encodedLength = (shard) => {
19295
- let entriesLength = 0;
19296
- for (const entry of shard.entries) {
19297
- entriesLength += entryEncodedLength(entry);
19298
- }
19299
- const tokens = [
19300
- new Token(Type.map, 3),
19301
- new Token(Type.string, "entries"),
19302
- new Token(Type.array, shard.entries.length),
19303
- new Token(Type.string, "maxKeyLength"),
19304
- new Token(Type.uint, shard.maxKeyLength),
19305
- new Token(Type.string, "maxSize"),
19306
- new Token(Type.uint, shard.maxSize)
19307
- ];
19308
- return tokensToLength(tokens) + entriesLength;
19309
- };
19310
- var entryEncodedLength = (entry) => {
19311
- const tokens = [
19312
- new Token(Type.array, entry.length),
19313
- new Token(Type.string, entry[0])
19314
- ];
19315
- if (Array.isArray(entry[1])) {
19316
- tokens.push(new Token(Type.array, entry[1].length));
19317
- for (const link2 of entry[1]) {
19318
- tokens.push(CID_TAG2);
19319
- tokens.push(new Token(Type.bytes, { length: link2.byteLength + 1 }));
19320
- }
19321
- } else {
19322
- tokens.push(CID_TAG2);
19323
- tokens.push(new Token(Type.bytes, { length: entry[1].byteLength + 1 }));
19324
- }
19325
- return tokensToLength(tokens);
19326
- };
19327
19274
 
19328
- // ../../node_modules/.pnpm/@web3-storage+pail@0.4.0/node_modules/@web3-storage/pail/src/index.js
19275
+ // ../../node_modules/.pnpm/@web3-storage+pail@0.4.1/node_modules/@web3-storage/pail/src/index.js
19329
19276
  var put = async (blocks, root2, key, value) => {
19330
19277
  const shards = new ShardFetcher(blocks);
19331
19278
  const rshard = await shards.get(root2);
@@ -19528,15 +19475,118 @@ ${key.data.toString("base64")}
19528
19475
  return [shard];
19529
19476
  };
19530
19477
 
19531
- // ../../node_modules/.pnpm/@web3-storage+pail@0.4.0/node_modules/@web3-storage/pail/src/batch/shard.js
19478
+ // ../../node_modules/.pnpm/@web3-storage+pail@0.4.1/node_modules/@web3-storage/pail/src/block.js
19479
+ var MemoryBlockstore2 = class {
19480
+ /** @type {Map<string, Uint8Array>} */
19481
+ #blocks = /* @__PURE__ */ new Map();
19482
+ /**
19483
+ * @param {Array<import('multiformats').Block>} [blocks]
19484
+ */
19485
+ constructor(blocks) {
19486
+ if (blocks) {
19487
+ this.#blocks = new Map(blocks.map((b) => [b.cid.toString(), b.bytes]));
19488
+ }
19489
+ }
19490
+ /** @type {API.BlockFetcher['get']} */
19491
+ async get(cid) {
19492
+ const bytes = this.#blocks.get(cid.toString());
19493
+ if (!bytes)
19494
+ return;
19495
+ return { cid, bytes };
19496
+ }
19497
+ /**
19498
+ * @param {API.UnknownLink} cid
19499
+ * @param {Uint8Array} bytes
19500
+ */
19501
+ async put(cid, bytes) {
19502
+ this.#blocks.set(cid.toString(), bytes);
19503
+ }
19504
+ /**
19505
+ * @param {API.UnknownLink} cid
19506
+ * @param {Uint8Array} bytes
19507
+ */
19508
+ putSync(cid, bytes) {
19509
+ this.#blocks.set(cid.toString(), bytes);
19510
+ }
19511
+ /** @param {API.UnknownLink} cid */
19512
+ async delete(cid) {
19513
+ this.#blocks.delete(cid.toString());
19514
+ }
19515
+ /** @param {API.UnknownLink} cid */
19516
+ deleteSync(cid) {
19517
+ this.#blocks.delete(cid.toString());
19518
+ }
19519
+ *entries() {
19520
+ for (const [str, bytes] of this.#blocks) {
19521
+ yield { cid: parse(str), bytes };
19522
+ }
19523
+ }
19524
+ };
19525
+ var MultiBlockFetcher = class {
19526
+ /** @type {API.BlockFetcher[]} */
19527
+ #fetchers;
19528
+ /** @param {API.BlockFetcher[]} fetchers */
19529
+ constructor(...fetchers) {
19530
+ this.#fetchers = fetchers;
19531
+ }
19532
+ /** @type {API.BlockFetcher['get']} */
19533
+ async get(link2) {
19534
+ for (const f of this.#fetchers) {
19535
+ const v = await f.get(link2);
19536
+ if (v)
19537
+ return v;
19538
+ }
19539
+ }
19540
+ };
19541
+
19542
+ // ../../node_modules/.pnpm/@web3-storage+pail@0.4.1/node_modules/@web3-storage/pail/src/batch/shard.js
19543
+ var ShardLinkByteLength = 36;
19544
+ var CID_TAG3 = new Token(Type.tag, 42);
19532
19545
  var create8 = (init2) => ({
19533
19546
  base: init2?.base,
19534
19547
  prefix: init2?.prefix ?? "",
19535
- entries: init2?.entries ?? [],
19548
+ entries: [...init2?.entries ?? []],
19536
19549
  ...configure(init2)
19537
19550
  });
19551
+ var encodedLength = (shard) => {
19552
+ let entriesLength = 0;
19553
+ for (const entry of shard.entries) {
19554
+ entriesLength += entryEncodedLength(entry);
19555
+ }
19556
+ const tokens = [
19557
+ new Token(Type.map, 3),
19558
+ new Token(Type.string, "entries"),
19559
+ new Token(Type.array, shard.entries.length),
19560
+ new Token(Type.string, "maxKeyLength"),
19561
+ new Token(Type.uint, shard.maxKeyLength),
19562
+ new Token(Type.string, "maxSize"),
19563
+ new Token(Type.uint, shard.maxSize)
19564
+ ];
19565
+ return tokensToLength(tokens) + entriesLength;
19566
+ };
19567
+ var entryEncodedLength = (entry) => {
19568
+ const tokens = [
19569
+ new Token(Type.array, entry.length),
19570
+ new Token(Type.string, entry[0])
19571
+ ];
19572
+ if (Array.isArray(entry[1])) {
19573
+ tokens.push(new Token(Type.array, entry[1].length));
19574
+ for (const item of entry[1]) {
19575
+ tokens.push(CID_TAG3);
19576
+ if (isLink(item)) {
19577
+ tokens.push(new Token(Type.bytes, { length: item.byteLength + 1 }));
19578
+ } else {
19579
+ tokens.push(new Token(Type.bytes, { length: ShardLinkByteLength + 1 }));
19580
+ }
19581
+ }
19582
+ } else {
19583
+ tokens.push(CID_TAG3);
19584
+ tokens.push(new Token(Type.bytes, { length: entry[1].byteLength + 1 }));
19585
+ }
19586
+ return tokensToLength(tokens);
19587
+ };
19538
19588
 
19539
- // ../../node_modules/.pnpm/@web3-storage+pail@0.4.0/node_modules/@web3-storage/pail/src/batch/index.js
19589
+ // ../../node_modules/.pnpm/@web3-storage+pail@0.4.1/node_modules/@web3-storage/pail/src/batch/index.js
19540
19590
  var Batcher = class _Batcher {
19541
19591
  #committed = false;
19542
19592
  /**
@@ -19551,7 +19601,7 @@ ${key.data.toString("base64")}
19551
19601
  constructor({ blocks, entries: entries3, prefix, maxSize, maxKeyLength, base: base4 }) {
19552
19602
  this.blocks = blocks;
19553
19603
  this.prefix = prefix;
19554
- this.entries = entries3;
19604
+ this.entries = [...entries3];
19555
19605
  this.base = base4;
19556
19606
  this.maxSize = maxSize;
19557
19607
  this.maxKeyLength = maxKeyLength;
@@ -19581,7 +19631,7 @@ ${key.data.toString("base64")}
19581
19631
  static async create({ blocks, link: link2, prefix }) {
19582
19632
  const shards = new ShardFetcher(blocks);
19583
19633
  const base4 = await shards.get(link2);
19584
- return new _Batcher({ blocks, entries: base4.value.entries, prefix, base: base4, ...configure(base4.value) });
19634
+ return new _Batcher({ blocks, prefix, base: base4, ...base4.value });
19585
19635
  }
19586
19636
  };
19587
19637
  var put2 = async (blocks, shard, key, value) => {
@@ -19618,7 +19668,7 @@ ${key.data.toString("base64")}
19618
19668
  entry = [pfxskeys[0].key, [batcher]];
19619
19669
  }
19620
19670
  shard.entries = putEntry(asShardEntries(shard.entries), asShardEntry(entry));
19621
- const size = encodedLength(withEntries(asShardEntries(shard.entries), shard));
19671
+ const size = encodedLength(shard);
19622
19672
  if (size > shard.maxSize) {
19623
19673
  const common = findCommonPrefix(
19624
19674
  asShardEntries(shard.entries),
@@ -19655,14 +19705,16 @@ ${key.data.toString("base64")}
19655
19705
  }
19656
19706
  };
19657
19707
  var traverse2 = async (shards, key, shard) => {
19658
- for (const e of shard.entries) {
19659
- const [k, v] = e;
19708
+ for (let i = 0; i < shard.entries.length; i++) {
19709
+ const [k, v] = shard.entries[i];
19660
19710
  if (key <= k)
19661
19711
  break;
19662
19712
  if (key.startsWith(k) && Array.isArray(v)) {
19663
19713
  if (isShardLink(v[0])) {
19664
19714
  const blk = await shards.get(v[0], shard.prefix + k);
19665
- v[0] = create8({ base: blk, prefix: blk.prefix, ...blk.value });
19715
+ const batcher = create8({ base: blk, prefix: blk.prefix, ...blk.value });
19716
+ shard.entries[i] = [k, v[1] == null ? [batcher] : [batcher, v[1]]];
19717
+ return traverse2(shards, key.slice(k.length), batcher);
19666
19718
  }
19667
19719
  return traverse2(shards, key.slice(k.length), v[0]);
19668
19720
  }
@@ -19716,9 +19768,9 @@ ${key.data.toString("base64")}
19716
19768
  static code = "ERR_BATCH_COMMITTED";
19717
19769
  };
19718
19770
 
19719
- // ../../node_modules/.pnpm/@web3-storage+pail@0.4.0/node_modules/@web3-storage/pail/src/crdt/index.js
19771
+ // ../../node_modules/.pnpm/@web3-storage+pail@0.4.1/node_modules/@web3-storage/pail/src/crdt/index.js
19720
19772
  var put3 = async (blocks, head, key, value) => {
19721
- const mblocks = new MemoryBlockstore();
19773
+ const mblocks = new MemoryBlockstore2();
19722
19774
  blocks = new MultiBlockFetcher(mblocks, blocks);
19723
19775
  if (!head.length) {
19724
19776
  const shard = await ShardBlock.create();
@@ -19802,7 +19854,7 @@ ${key.data.toString("base64")}
19802
19854
  var root = async (blocks, head) => {
19803
19855
  if (!head.length)
19804
19856
  throw new Error("cannot determine root of headless clock");
19805
- const mblocks = new MemoryBlockstore();
19857
+ const mblocks = new MemoryBlockstore2();
19806
19858
  blocks = new MultiBlockFetcher(mblocks, blocks);
19807
19859
  const events = new EventFetcher(blocks);
19808
19860
  if (head.length === 1) {
@@ -19861,7 +19913,7 @@ ${key.data.toString("base64")}
19861
19913
  return;
19862
19914
  const result = await root(blocks, head);
19863
19915
  if (result.additions.length) {
19864
- blocks = new MultiBlockFetcher(new MemoryBlockstore(result.additions), blocks);
19916
+ blocks = new MultiBlockFetcher(new MemoryBlockstore2(result.additions), blocks);
19865
19917
  }
19866
19918
  return get2(blocks, result.root, key);
19867
19919
  };
@@ -19870,7 +19922,7 @@ ${key.data.toString("base64")}
19870
19922
  return;
19871
19923
  const result = await root(blocks, head);
19872
19924
  if (result.additions.length) {
19873
- blocks = new MultiBlockFetcher(new MemoryBlockstore(result.additions), blocks);
19925
+ blocks = new MultiBlockFetcher(new MemoryBlockstore2(result.additions), blocks);
19874
19926
  }
19875
19927
  yield* entries(blocks, result.root, options);
19876
19928
  };
@@ -19954,7 +20006,7 @@ ${key.data.toString("base64")}
19954
20006
  return acc.concat(...rest);
19955
20007
  };
19956
20008
 
19957
- // ../../node_modules/.pnpm/@web3-storage+pail@0.4.0/node_modules/@web3-storage/pail/src/crdt/batch/index.js
20009
+ // ../../node_modules/.pnpm/@web3-storage+pail@0.4.1/node_modules/@web3-storage/pail/src/crdt/batch/index.js
19958
20010
  var Batcher2 = class _Batcher {
19959
20011
  #committed = false;
19960
20012
  /**
@@ -19973,7 +20025,7 @@ ${key.data.toString("base64")}
19973
20025
  this.blocks = blocks;
19974
20026
  this.head = head;
19975
20027
  this.prefix = prefix;
19976
- this.entries = entries3;
20028
+ this.entries = [...entries3];
19977
20029
  this.base = base4;
19978
20030
  this.maxSize = maxSize;
19979
20031
  this.maxKeyLength = maxKeyLength;
@@ -19999,7 +20051,7 @@ ${key.data.toString("base64")}
19999
20051
  const res = await commit(this);
20000
20052
  const data = { type: "batch", ops: this.ops, root: res.root };
20001
20053
  const event = await EventBlock.create(data, this.head);
20002
- const mblocks = new MemoryBlockstore();
20054
+ const mblocks = new MemoryBlockstore2();
20003
20055
  const blocks = new MultiBlockFetcher(mblocks, this.blocks);
20004
20056
  mblocks.putSync(event.cid, event.bytes);
20005
20057
  const head = await advance(blocks, this.head, event.cid);
@@ -20039,7 +20091,7 @@ ${key.data.toString("base64")}
20039
20091
  * @param {string} init.prefix
20040
20092
  */
20041
20093
  static async create({ blocks, head, prefix }) {
20042
- const mblocks = new MemoryBlockstore();
20094
+ const mblocks = new MemoryBlockstore2();
20043
20095
  blocks = new MultiBlockFetcher(mblocks, blocks);
20044
20096
  if (!head.length) {
20045
20097
  const base5 = await ShardBlock.create();
@@ -28480,10 +28532,8 @@ ${key.data.toString("base64")}
28480
28532
 
28481
28533
  // src/crdt-helpers.ts
28482
28534
  function time(tag2) {
28483
- console.time(tag2);
28484
28535
  }
28485
28536
  function timeEnd(tag2) {
28486
- console.timeEnd(tag2);
28487
28537
  }
28488
28538
  async function applyBulkUpdateToCrdt(tblocks, head, updates) {
28489
28539
  let result = null;
@@ -29531,7 +29581,7 @@ ${key.data.toString("base64")}
29531
29581
  try {
29532
29582
  head = await advance(tblocks, head, cid);
29533
29583
  } catch (e) {
29534
- console.error("failed to advance", cid.toString(), e);
29584
+ console.log("failed to advance head:", cid.toString());
29535
29585
  continue;
29536
29586
  }
29537
29587
  }