@fireproof/core 0.20.0-dev-preview-40 → 0.20.0-dev-preview-50

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.
Files changed (45) hide show
  1. package/README.md +6 -4
  2. package/deno/index.js +2 -2
  3. package/deno/index.js.map +1 -1
  4. package/deno.json +3 -2
  5. package/index.cjs +955 -599
  6. package/index.cjs.map +1 -1
  7. package/index.d.cts +334 -127
  8. package/index.d.ts +334 -127
  9. package/index.js +939 -582
  10. package/index.js.map +1 -1
  11. package/indexeddb/index.cjs.map +1 -1
  12. package/indexeddb/index.js.map +1 -1
  13. package/indexeddb/metafile-cjs.json +1 -1
  14. package/indexeddb/metafile-esm.json +1 -1
  15. package/metafile-cjs.json +1 -1
  16. package/metafile-esm.json +1 -1
  17. package/package.json +7 -5
  18. package/react/index.cjs +17 -9
  19. package/react/index.cjs.map +1 -1
  20. package/react/index.d.cts +5 -4
  21. package/react/index.d.ts +5 -4
  22. package/react/index.js +17 -9
  23. package/react/index.js.map +1 -1
  24. package/react/metafile-cjs.json +1 -1
  25. package/react/metafile-esm.json +1 -1
  26. package/tests/blockstore/interceptor-gateway.test.ts +15 -1
  27. package/tests/blockstore/keyed-crypto-indexeddb-file.test.ts +8 -18
  28. package/tests/blockstore/keyed-crypto.test.ts +31 -53
  29. package/tests/blockstore/loader.test.ts +21 -19
  30. package/tests/blockstore/store.test.ts +52 -56
  31. package/tests/blockstore/transaction.test.ts +13 -11
  32. package/tests/fireproof/all-gateway.test.ts +53 -50
  33. package/tests/fireproof/attachable.test.ts +356 -0
  34. package/tests/fireproof/crdt.test.ts +100 -60
  35. package/tests/fireproof/database.test.ts +95 -54
  36. package/tests/fireproof/fireproof.test.ts +58 -55
  37. package/tests/fireproof/hello.test.ts +4 -4
  38. package/tests/fireproof/indexer.test.ts +44 -44
  39. package/tests/fireproof/stable-cid.test.ts +69 -0
  40. package/tests/fireproof/utils.test.ts +21 -10
  41. package/tests/gateway/file/loader-config.test.ts +25 -25
  42. package/tests/gateway/fp-envelope-serialize.test.ts +8 -8
  43. package/tests/gateway/indexeddb/loader-config.test.ts +6 -6
  44. package/tests/helpers.ts +81 -2
  45. package/tests/react/useFireproof.test.tsx +59 -17
@@ -41,11 +41,11 @@ describe("dreamcode", function () {
41
41
  let result: IndexRows<string, Doc>;
42
42
  let db: Database;
43
43
  const sthis = ensureSuperThis();
44
- afterEach(async function () {
44
+ afterEach(async () => {
45
45
  await db.close();
46
46
  await db.destroy();
47
47
  });
48
- beforeEach(async function () {
48
+ beforeEach(async () => {
49
49
  await sthis.start();
50
50
  db = fireproof("test-db");
51
51
  ok = await db.put({ _id: "test-1", text: "fireproof", dream: true });
@@ -65,7 +65,7 @@ describe("dreamcode", function () {
65
65
  expect(result.rows.length).toBe(1);
66
66
  expect(result.rows[0].key).toBe("fireproof");
67
67
  });
68
- it("should query with function", async function () {
68
+ it("should query with function", async () => {
69
69
  const result = await db.query<boolean, Doc>((doc) => doc.dream);
70
70
  expect(result).toBeTruthy();
71
71
  expect(result.rows).toBeTruthy();
@@ -89,7 +89,7 @@ describe("public API", function () {
89
89
  await db.destroy();
90
90
  });
91
91
 
92
- beforeEach(async function () {
92
+ beforeEach(async () => {
93
93
  await sthis.start();
94
94
  db = fireproof("test-api");
95
95
  // index = index(db, 'test-index', (doc) => doc.foo)
@@ -131,23 +131,24 @@ describe("database fullconfig", () => {
131
131
  // meta: `${base}/meta?taste=${taste}`,
132
132
  data: {
133
133
  meta: base.build().pathname("dist/full/meta"),
134
- data: base.build().pathname("dist/full/data"),
134
+ car: base.build().pathname("dist/full/data"),
135
135
  wal: base.build().pathname("dist/full/wal"),
136
136
  },
137
137
  idx: {
138
138
  meta: base.build().pathname("dist/full/idx-meta"),
139
- data: base.build().pathname("dist/full/idx-data"),
139
+ car: base.build().pathname("dist/full/idx-data"),
140
140
  wal: base.build().pathname("dist/full/idx-wal"),
141
141
  },
142
142
  // wal: `${base}/wal?taste=${taste}`,
143
143
  },
144
144
  });
145
+ await db.ready();
145
146
 
146
- const carStore = await db.ledger.crdt.blockstore.loader?.carStore();
147
+ const carStore = await db.ledger.crdt.blockstore.loader.attachedStores.local().active.car;
147
148
  expect(carStore.url().getParam(PARAM.NAME)).toBe("my-funky-name");
148
- const metaStore = await db.ledger.crdt.blockstore.loader?.metaStore();
149
+ const metaStore = await db.ledger.crdt.blockstore.loader.attachedStores.local().active.meta;
149
150
  expect(metaStore.url().getParam(PARAM.NAME)).toBe("my-funky-name");
150
- const walStore = await db.ledger.crdt.blockstore.loader?.WALStore();
151
+ const walStore = await db.ledger.crdt.blockstore.loader.attachedStores.local().active.wal;
151
152
  expect(walStore.url().getParam(PARAM.NAME)).toBe("my-funky-name");
152
153
 
153
154
  expect(db).toBeTruthy();
@@ -163,26 +164,26 @@ describe("basic ledger", function () {
163
164
  }
164
165
  let db: Database;
165
166
  const sthis = ensureSuperThis();
166
- afterEach(async function () {
167
+ afterEach(async () => {
167
168
  await db.close();
168
169
  await db.destroy();
169
170
  });
170
- beforeEach(async function () {
171
+ beforeEach(async () => {
171
172
  await sthis.start();
172
173
  db = fireproof("test-basic");
173
174
  });
174
- it("can put with id", async function () {
175
+ it("can put with id", async () => {
175
176
  const ok = await db.put({ _id: "test", foo: "bar" });
176
177
  expect(ok).toBeTruthy();
177
178
  expect(ok.id).toBe("test");
178
179
  });
179
- it("can put without id", async function () {
180
+ it("can put without id", async () => {
180
181
  const ok = await db.put({ foo: "bam" });
181
182
  expect(ok).toBeTruthy();
182
183
  const got = await db.get<Doc>(ok.id);
183
184
  expect(got.foo).toBe("bam");
184
185
  });
185
- it("can bulk an array", async function () {
186
+ it("can bulk an array", async () => {
186
187
  const ok = await db.bulk([{ foo: "cool" }, { foo: "dude" }]);
187
188
  expect(ok).toBeTruthy();
188
189
  expect(ok.ids.length).toBe(2);
@@ -191,7 +192,7 @@ describe("basic ledger", function () {
191
192
  const got2 = await db.get<Doc>(ok.ids[1]);
192
193
  expect(got2.foo).toBe("dude");
193
194
  });
194
- it("can define an index", async function () {
195
+ it("can define an index", async () => {
195
196
  const ok = await db.put({ _id: "test", foo: "bar" });
196
197
  expect(ok).toBeTruthy();
197
198
  const idx = index<string, { foo: string }>(db, "test-index", (doc) => doc.foo);
@@ -201,7 +202,7 @@ describe("basic ledger", function () {
201
202
  expect(result.rows.length).toBe(1);
202
203
  expect(result.rows[0].key).toBe("bar");
203
204
  });
204
- it("can define an index with a default function", async function () {
205
+ it("can define an index with a default function", async () => {
205
206
  const ok = await db.put({ _id: "test", foo: "bar" });
206
207
  expect(ok).toBeTruthy();
207
208
  const idx = index(db, "foo");
@@ -211,7 +212,7 @@ describe("basic ledger", function () {
211
212
  expect(result.rows.length).toBe(1);
212
213
  expect(result.rows[0].key).toBe("bar");
213
214
  });
214
- it("should query with multiple successive functions", async function () {
215
+ it("should query with multiple successive functions", async () => {
215
216
  interface TestDoc {
216
217
  _id: string;
217
218
  foo: string;
@@ -236,16 +237,16 @@ describe("basic ledger", function () {
236
237
  describe("benchmarking with compaction", function () {
237
238
  let db: Database;
238
239
  const sthis = ensureSuperThis();
239
- afterEach(async function () {
240
+ afterEach(async () => {
240
241
  await db.close();
241
242
  await db.destroy();
242
243
  });
243
- beforeEach(async function () {
244
+ beforeEach(async () => {
244
245
  // erase the existing test data
245
246
  await sthis.start();
246
247
  db = fireproof("test-benchmark-compaction", { autoCompact: 3 });
247
248
  });
248
- it.skip("insert during compaction", async function () {
249
+ it.skip("insert during compaction", async () => {
249
250
  const ok = await db.put({ _id: "test", foo: "fast" });
250
251
  expect(ok).toBeTruthy();
251
252
  expect(ok.id).toBe("test");
@@ -290,11 +291,11 @@ describe("benchmarking with compaction", function () {
290
291
  describe("benchmarking a ledger", function () {
291
292
  let db: Database;
292
293
  const sthis = ensureSuperThis();
293
- afterEach(async function () {
294
+ afterEach(async () => {
294
295
  await db.close();
295
296
  await db.destroy();
296
297
  });
297
- beforeEach(async function () {
298
+ beforeEach(async () => {
298
299
  await sthis.start();
299
300
  // erase the existing test data
300
301
  db = fireproof("test-benchmark", { autoCompact: 100000, public: true });
@@ -448,11 +449,11 @@ describe("Reopening a ledger", function () {
448
449
  }
449
450
  let db: Database;
450
451
  const sthis = ensureSuperThis();
451
- afterEach(async function () {
452
+ afterEach(async () => {
452
453
  await db.close();
453
454
  await db.destroy();
454
455
  });
455
- beforeEach(async function () {
456
+ beforeEach(async () => {
456
457
  // erase the existing test data
457
458
  await sthis.start();
458
459
 
@@ -465,12 +466,12 @@ describe("Reopening a ledger", function () {
465
466
  expect(db.ledger.crdt.clock.head.length).toBe(1);
466
467
  });
467
468
 
468
- it("should persist data", async function () {
469
+ it("should persist data", async () => {
469
470
  const doc = await db.get<Doc>("test");
470
471
  expect(doc.foo).toBe("bar");
471
472
  });
472
473
 
473
- it("should have the same data on reopen", async function () {
474
+ it("should have the same data on reopen", async () => {
474
475
  const db2 = fireproof("test-reopen");
475
476
  const doc = await db2.get<FooType>("test");
476
477
  expect(doc.foo).toBe("bar");
@@ -480,27 +481,27 @@ describe("Reopening a ledger", function () {
480
481
  await db2.close();
481
482
  });
482
483
 
483
- it("should have a car in the car log", async function () {
484
+ it("should have a car in the car log", async () => {
484
485
  await db.ledger.crdt.ready();
485
486
  const blocks = db.ledger.crdt.blockstore as bs.EncryptedBlockstore;
486
487
  const loader = blocks.loader;
487
488
  expect(loader).toBeDefined();
488
489
  expect(loader.carLog).toBeDefined();
489
- expect(loader.carLog.length).toBe(1);
490
+ expect(loader.carLog.length).toBe(1 + 1 /* genesis */);
490
491
  });
491
492
 
492
- it("should have carlog after reopen", async function () {
493
+ it("should have carlog after reopen", async () => {
493
494
  const db2 = fireproof("test-reopen");
494
495
  await db2.ledger.crdt.ready();
495
496
  const blocks = db2.ledger.crdt.blockstore as bs.EncryptedBlockstore;
496
497
  const loader = blocks.loader;
497
498
  expect(loader).toBeDefined();
498
499
  expect(loader.carLog).toBeDefined();
499
- expect(loader.carLog.length).toBe(1);
500
+ expect(loader.carLog.length).toBe(1 + 1 /* genesis */);
500
501
  await db2.close();
501
502
  });
502
503
 
503
- it("faster, should have the same data on reopen after reopen and update", async function () {
504
+ it("faster, should have the same data on reopen after reopen and update", async () => {
504
505
  for (let i = 0; i < 4; i++) {
505
506
  // console.log('iteration', i)
506
507
  const db = fireproof("test-reopen");
@@ -508,17 +509,17 @@ describe("Reopening a ledger", function () {
508
509
  await db.ready();
509
510
  const blocks = db.ledger.crdt.blockstore as bs.EncryptedBlockstore;
510
511
  const loader = blocks.loader;
511
- expect(loader.carLog.length).toBe(i + 1);
512
+ expect(loader.carLog.length).toBe(i + 1 + 1 /* genesis */);
512
513
  const ok = await db.put({ _id: `test${i}`, fire: "proof".repeat(50 * 1024) });
513
514
  expect(ok).toBeTruthy();
514
- expect(loader.carLog.length).toBe(i + 2);
515
+ expect(loader.carLog.length).toBe(i + 2 + 1 /* genesis */);
515
516
  const doc = await db.get<FireType>(`test${i}`);
516
517
  expect(doc.fire).toBe("proof".repeat(50 * 1024));
517
518
  await db.close();
518
519
  }
519
520
  }, 20000);
520
521
 
521
- it.skip("passing slow, should have the same data on reopen after reopen and update", async function () {
522
+ it.skip("passing slow, should have the same data on reopen after reopen and update", async () => {
522
523
  for (let i = 0; i < 200; i++) {
523
524
  // console.log("iteration", i);
524
525
  // console.time("db open");
@@ -553,11 +554,11 @@ describe("Reopening a ledger with indexes", function () {
553
554
  let didMap: boolean;
554
555
  let mapFn: MapFn<Doc>;
555
556
  const sthis = ensureSuperThis();
556
- afterEach(async function () {
557
+ afterEach(async () => {
557
558
  await db.close();
558
559
  await db.destroy();
559
560
  });
560
- beforeEach(async function () {
561
+ beforeEach(async () => {
561
562
  await sthis.start();
562
563
  db = fireproof("test-reopen-idx");
563
564
  const ok = await db.put({ _id: "test", foo: "bar" });
@@ -572,7 +573,7 @@ describe("Reopening a ledger with indexes", function () {
572
573
  idx = index<string, Doc>(db, "foo", mapFn);
573
574
  });
574
575
 
575
- it("should persist data", async function () {
576
+ it("should persist data", async () => {
576
577
  const doc = await db.get<Doc>("test");
577
578
  expect(doc.foo).toBe("bar");
578
579
  const idx2 = index<string, Doc>(db, "foo");
@@ -585,7 +586,7 @@ describe("Reopening a ledger with indexes", function () {
585
586
  expect(didMap).toBeTruthy();
586
587
  });
587
588
 
588
- it("should reuse the index", async function () {
589
+ it("should reuse the index", async () => {
589
590
  const idx2 = index(db, "foo", mapFn);
590
591
  expect(idx2).toBe(idx);
591
592
  const result = await idx2.query();
@@ -603,7 +604,7 @@ describe("Reopening a ledger with indexes", function () {
603
604
  expect(didMap).toBeFalsy();
604
605
  });
605
606
 
606
- it("should have the same data on reopen", async function () {
607
+ it("should have the same data on reopen", async () => {
607
608
  const db2 = fireproof("test-reopen-idx");
608
609
  const doc = await db2.get<FooType>("test");
609
610
  expect(doc.foo).toBe("bar");
@@ -612,7 +613,7 @@ describe("Reopening a ledger with indexes", function () {
612
613
  expect(db2.ledger.crdt.clock.head).toEqual(db.ledger.crdt.clock.head);
613
614
  });
614
615
 
615
- it("should have the same data on reopen after a query", async function () {
616
+ it("should have the same data on reopen after a query", async () => {
616
617
  const r0 = await idx.query();
617
618
  expect(r0).toBeTruthy();
618
619
  expect(r0.rows).toBeTruthy();
@@ -627,7 +628,7 @@ describe("Reopening a ledger with indexes", function () {
627
628
  expect(db2.ledger.crdt.clock.head).toEqual(db.ledger.crdt.clock.head);
628
629
  });
629
630
 
630
- // it('should query the same data on reopen', async function () {
631
+ // it('should query the same data on reopen', async () =>{
631
632
  // const r0 = await idx.query()
632
633
  // assert(r0)
633
634
  // assert(r0.rows)
@@ -650,10 +651,10 @@ describe("Reopening a ledger with indexes", function () {
650
651
 
651
652
  describe("basic js verify", function () {
652
653
  const sthis = ensureSuperThis();
653
- beforeAll(async function () {
654
+ beforeAll(async () => {
654
655
  await sthis.start();
655
656
  });
656
- it("should include cids in arrays", async function () {
657
+ it("should include cids in arrays", async () => {
657
658
  const db = fireproof("test-verify");
658
659
  const ok = await db.put({ _id: "test", foo: ["bar", "bam"] });
659
660
  expect(ok.id).toBe("test");
@@ -662,7 +663,7 @@ describe("basic js verify", function () {
662
663
  const blocks = db.ledger.crdt.blockstore as bs.EncryptedBlockstore;
663
664
  const loader = blocks.loader;
664
665
  expect(loader).toBeTruthy();
665
- const cid = loader.carLog[0][0];
666
+ const cid = loader.carLog.asArray()[0][0];
666
667
  const cid2 = db.ledger.crdt.clock.head[0];
667
668
  expect(cid).not.toBe(cid2);
668
669
  expect(cid).not.toBe(cid2);
@@ -697,17 +698,18 @@ describe("same workload twice, same CID", function () {
697
698
  },
698
699
  };
699
700
 
700
- afterEach(async function () {
701
+ afterEach(async () => {
701
702
  await dbA.close();
702
703
  await dbA.destroy();
703
704
  await dbB.close();
704
705
  await dbB.destroy();
705
706
  });
706
- beforeEach(async function () {
707
+ beforeEach(async () => {
707
708
  let ok: DocResponse;
708
709
  await sthis.start();
709
710
  // todo this fails because the test setup doesn't properly configure both ledgers to use the same key
710
711
  dbA = fireproof("test-dual-workload-a", configA);
712
+ await dbA.destroy();
711
713
  for (const doc of docs) {
712
714
  ok = await dbA.put(doc);
713
715
  expect(ok).toBeTruthy();
@@ -717,6 +719,7 @@ describe("same workload twice, same CID", function () {
717
719
 
718
720
  // todo this fails because the test setup doesn't properly configure both ledgers to use the same key
719
721
  dbB = fireproof("test-dual-workload-b", configB);
722
+ await dbA.destroy();
720
723
  for (const doc of docs) {
721
724
  ok = await dbB.put(doc);
722
725
  expect(ok).toBeTruthy();
@@ -724,32 +727,32 @@ describe("same workload twice, same CID", function () {
724
727
  }
725
728
  headB = dbB.ledger.crdt.clock.head.toString();
726
729
  });
727
- it("should have head A and B", async function () {
730
+ it("should have head A and B", async () => {
728
731
  expect(headA).toBeTruthy();
729
732
  expect(headB).toBeTruthy();
730
733
  expect(headA).toEqual(headB);
731
734
  expect(headA.length).toBeGreaterThan(10);
732
735
  });
733
- it("should have same car log", async function () {
736
+ it("should have same car log", async () => {
734
737
  const logA = dbA.ledger.crdt.blockstore.loader?.carLog;
735
738
  expect(logA).toBeTruthy();
736
739
  assert(logA);
737
- expect(logA.length).toBe(docs.length);
740
+ expect(logA.length).toBe(docs.length + 1 /*genesis*/);
738
741
 
739
742
  const logB = dbB.ledger.crdt.blockstore.loader?.carLog;
740
743
  expect(logB).toBeTruthy();
741
744
  assert(logB);
742
- expect(logB.length).toBe(docs.length);
745
+ expect(logB.length).toBe(docs.length + 1 /*genesis*/);
743
746
 
744
- const logA2 = logA.map((c) => c.toString());
745
- const logB2 = logB.map((c) => c.toString());
747
+ const logA2 = logA.asArray().map((c) => c.toString());
748
+ const logB2 = logB.asArray().map((c) => c.toString());
746
749
 
747
750
  expect(logA2.length).toBe(logB2.length);
748
751
 
749
752
  // todo this fails because the test setup doesn't properly configure both ledgers to use the same key
750
753
  // expect(logA2).toEqual(logB2);
751
754
  });
752
- it("should have same car log after compact", async function () {
755
+ it("should have same car log after compact", async () => {
753
756
  await dbA.compact();
754
757
  await dbB.compact();
755
758
 
@@ -763,8 +766,8 @@ describe("same workload twice, same CID", function () {
763
766
  assert(cmpLogB);
764
767
  expect(cmpLogB.length).toBe(1);
765
768
 
766
- const cmpLogA2 = cmpLogA.map((c) => c.toString());
767
- const cmpLogB2 = cmpLogB.map((c) => c.toString());
769
+ const cmpLogA2 = cmpLogA.asArray().map((c) => c.toString());
770
+ const cmpLogB2 = cmpLogB.asArray().map((c) => c.toString());
768
771
 
769
772
  expect(cmpLogA2.length).toBe(cmpLogB2.length);
770
773
 
@@ -49,23 +49,23 @@ describe("hello public API", () => {
49
49
 
50
50
  describe("Simplified Reopening a ledger", function () {
51
51
  let db: Database;
52
- afterEach(async function () {
52
+ afterEach(async () => {
53
53
  await db.close();
54
54
  await db.destroy();
55
55
  });
56
- beforeEach(async function () {
56
+ beforeEach(async () => {
57
57
  db = fireproof("test-reopen-simple");
58
58
  const ok = await db.put({ _id: "test", foo: "bar" });
59
59
  expect(ok).toBeTruthy();
60
60
  expect(ok.id).toBe("test");
61
61
  });
62
62
 
63
- it("should persist data", async function () {
63
+ it("should persist data", async () => {
64
64
  const doc = await db.get<{ foo: string }>("test");
65
65
  expect(doc.foo).toBe("bar");
66
66
  });
67
67
 
68
- it("should have the same data on reopen", async function () {
68
+ it("should have the same data on reopen", async () => {
69
69
  const db2 = fireproof("test-reopen-simple");
70
70
  const doc = await db2.get<{ foo: string }>("test");
71
71
  expect(doc.foo).toBe("bar");