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

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 (38) 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/index.cjs +499 -370
  5. package/index.cjs.map +1 -1
  6. package/index.d.cts +162 -64
  7. package/index.d.ts +162 -64
  8. package/index.js +473 -344
  9. package/index.js.map +1 -1
  10. package/metafile-cjs.json +1 -1
  11. package/metafile-esm.json +1 -1
  12. package/package.json +3 -3
  13. package/react/index.cjs +17 -9
  14. package/react/index.cjs.map +1 -1
  15. package/react/index.d.cts +2 -1
  16. package/react/index.d.ts +2 -1
  17. package/react/index.js +17 -9
  18. package/react/index.js.map +1 -1
  19. package/react/metafile-cjs.json +1 -1
  20. package/react/metafile-esm.json +1 -1
  21. package/tests/blockstore/interceptor-gateway.test.ts +5 -1
  22. package/tests/blockstore/keyed-crypto-indexeddb-file.test.ts +8 -18
  23. package/tests/blockstore/keyed-crypto.test.ts +7 -30
  24. package/tests/blockstore/loader.test.ts +19 -17
  25. package/tests/blockstore/store.test.ts +48 -51
  26. package/tests/blockstore/transaction.test.ts +13 -11
  27. package/tests/fireproof/all-gateway.test.ts +49 -46
  28. package/tests/fireproof/attachable.test.ts +82 -0
  29. package/tests/fireproof/crdt.test.ts +49 -48
  30. package/tests/fireproof/database.test.ts +40 -40
  31. package/tests/fireproof/fireproof.test.ts +43 -42
  32. package/tests/fireproof/hello.test.ts +4 -4
  33. package/tests/fireproof/indexer.test.ts +44 -44
  34. package/tests/fireproof/utils.test.ts +4 -3
  35. package/tests/gateway/file/loader-config.test.ts +17 -17
  36. package/tests/gateway/indexeddb/loader-config.test.ts +4 -4
  37. package/tests/helpers.ts +80 -2
  38. package/tests/react/useFireproof.test.tsx +40 -4
@@ -43,21 +43,22 @@ describe("noop Gateway", function () {
43
43
  const sthis = ensureSuperThis();
44
44
  let ctx: { loader: bs.Loadable };
45
45
 
46
- afterEach(async function () {
46
+ afterEach(async () => {
47
47
  await db.close();
48
48
  await db.destroy();
49
49
  });
50
- beforeEach(async function () {
50
+ beforeEach(async () => {
51
51
  db = LedgerFactory("test-gateway-" + sthis.nextId().str, {
52
52
  logger: sthis.logger,
53
53
  });
54
- ctx = { loader: await db.crdt.blockstore.loader };
54
+ await db.ready();
55
+ ctx = { loader: db.crdt.blockstore.loader };
55
56
 
56
57
  // Extract stores from the loader
57
- carStore = (await ctx.loader.carStore()) as bs.DataStore;
58
- metaStore = (await ctx.loader.metaStore()) as bs.MetaStore;
59
- fileStore = (await ctx.loader.fileStore()) as bs.DataStore;
60
- walStore = (await ctx.loader.WALStore()) as bs.WALStore;
58
+ carStore = ctx.loader.attachedStores.local().active.car;
59
+ metaStore = ctx.loader.attachedStores.local().active.meta;
60
+ fileStore = ctx.loader.attachedStores.local().active.file;
61
+ walStore = ctx.loader.attachedStores.local().active.wal;
61
62
 
62
63
  // Extract and log gateways
63
64
  carGateway = carStore.realGateway;
@@ -66,7 +67,7 @@ describe("noop Gateway", function () {
66
67
  walGateway = walStore.realGateway;
67
68
  });
68
69
 
69
- it("should have valid stores and gateways", async function () {
70
+ it("should have valid stores and gateways", async () => {
70
71
  // Add assertions
71
72
  expect(carStore).toBeTruthy();
72
73
  expect(metaStore).toBeTruthy();
@@ -79,7 +80,7 @@ describe("noop Gateway", function () {
79
80
  expect(walGateway).toBeTruthy();
80
81
  });
81
82
 
82
- it("should have correct store names", async function () {
83
+ it("should have correct store names", async () => {
83
84
  // Check that all stores have the correct name
84
85
  expect(carStore.url().getParam(PARAM.NAME)).toContain("test-gateway");
85
86
  expect(metaStore.url().getParam(PARAM.NAME)).toContain("test-gateway");
@@ -87,7 +88,7 @@ describe("noop Gateway", function () {
87
88
  expect(walStore.url().getParam(PARAM.NAME)).toContain("test-gateway");
88
89
  });
89
90
 
90
- it("should have correct store types in URLs", async function () {
91
+ it("should have correct store types in URLs", async () => {
91
92
  // Check that all stores have the correct store type in their URL
92
93
  expect(carStore.url().toString()).toContain("store=data");
93
94
  expect(carStore.url().toString()).toContain("suffix=.car");
@@ -96,7 +97,7 @@ describe("noop Gateway", function () {
96
97
  expect(walStore.url().toString()).toContain("store=wal");
97
98
  });
98
99
 
99
- it("should have version specified in URLs", async function () {
100
+ it("should have version specified in URLs", async () => {
100
101
  // Verify that all stores have a version specified
101
102
  expect(carStore.url().toString()).toContain("version=");
102
103
  expect(metaStore.url().toString()).toContain("version=");
@@ -104,7 +105,7 @@ describe("noop Gateway", function () {
104
105
  expect(walStore.url().toString()).toContain("version=");
105
106
  });
106
107
 
107
- it("should have correct gateway types", async function () {
108
+ it("should have correct gateway types", async () => {
108
109
  // Check that all gateways are instances of the expected gateway class
109
110
  expect(typeof carGateway).toBe("object");
110
111
  expect(typeof metaGateway).toBe("object");
@@ -112,18 +113,18 @@ describe("noop Gateway", function () {
112
113
  expect(typeof walGateway).toBe("object");
113
114
  });
114
115
 
115
- it("should build CAR Gateway URL", async function () {
116
+ it("should build CAR Gateway URL", async () => {
116
117
  const testKey = "bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i";
117
118
  const carUrl = await carGateway.buildUrl(ctx, carStore.url(), testKey);
118
119
  expect(carUrl.Ok().hasParam("key")).toBeTruthy();
119
120
  });
120
121
 
121
- it("should start CAR Gateway", async function () {
122
+ it("should start CAR Gateway", async () => {
122
123
  const url = await carGateway.start(ctx, carStore.url());
123
124
  expect(url.Ok().asObj()).toEqual(carStore.url().asObj());
124
125
  });
125
126
 
126
- it("should put data in CAR Gateway", async function () {
127
+ it("should put data in CAR Gateway", async () => {
127
128
  const carUrl = await carGateway.buildUrl(ctx, carStore.url(), fileContent.cid);
128
129
  await carGateway.start(ctx, carStore.url());
129
130
  const carPutResult = await carGateway.put(ctx, carUrl.Ok(), {
@@ -133,7 +134,7 @@ describe("noop Gateway", function () {
133
134
  expect(carPutResult.isOk()).toBeTruthy();
134
135
  });
135
136
 
136
- it("should get data from CAR Gateway", async function () {
137
+ it("should get data from CAR Gateway", async () => {
137
138
  const carUrl = await carGateway.buildUrl(ctx, carStore.url(), fileContent.cid);
138
139
  await carGateway.start(ctx, carStore.url());
139
140
  await carGateway.put(ctx, carUrl.Ok(), {
@@ -146,7 +147,7 @@ describe("noop Gateway", function () {
146
147
  // customExpect(carGetResult.Ok(), (v) => expect(v).toEqual(testData), "carGetResult should match testData");
147
148
  });
148
149
 
149
- it("should delete data from CAR Gateway", async function () {
150
+ it("should delete data from CAR Gateway", async () => {
150
151
  const carUrl = await carGateway.buildUrl(ctx, carStore.url(), fileContent.cid);
151
152
  await carGateway.start(ctx, carStore.url());
152
153
  await carGateway.put(ctx, carUrl.Ok(), {
@@ -157,33 +158,33 @@ describe("noop Gateway", function () {
157
158
  expect(carDeleteResult.isOk()).toBeTruthy();
158
159
  });
159
160
 
160
- it("should close CAR Gateway", async function () {
161
+ it("should close CAR Gateway", async () => {
161
162
  await carGateway.close(ctx, carStore.url());
162
163
  });
163
- it("should build Meta Gateway URL", async function () {
164
+ it("should build Meta Gateway URL", async () => {
164
165
  const metaUrl = await metaGateway.buildUrl(ctx, metaStore.url(), "main");
165
166
  expect(metaUrl.Ok()).toBeTruthy();
166
167
  });
167
168
 
168
- it("should start Meta Gateway", async function () {
169
+ it("should start Meta Gateway", async () => {
169
170
  await metaGateway.start(ctx, metaStore.url());
170
171
  });
171
172
 
172
- it("should close Meta Gateway", async function () {
173
+ it("should close Meta Gateway", async () => {
173
174
  await metaGateway.start(ctx, metaStore.url());
174
175
  await metaGateway.close(ctx, metaStore.url());
175
176
  });
176
177
 
177
- it("should build File Gateway URL", async function () {
178
+ it("should build File Gateway URL", async () => {
178
179
  const fileUrl = await fileGateway.buildUrl(ctx, fileStore.url(), fileContent.cid);
179
180
  expect(fileUrl.Ok()).toBeTruthy();
180
181
  });
181
182
 
182
- it("should start File Gateway", async function () {
183
+ it("should start File Gateway", async () => {
183
184
  await fileGateway.start(ctx, fileStore.url());
184
185
  });
185
186
 
186
- it("should put data to File Gateway", async function () {
187
+ it("should put data to File Gateway", async () => {
187
188
  const fileUrl = await fileGateway.buildUrl(ctx, fileStore.url(), fileContent.cid);
188
189
  await fileGateway.start(ctx, fileStore.url());
189
190
  const filePutResult = await fileGateway.put(ctx, fileUrl.Ok(), {
@@ -193,7 +194,7 @@ describe("noop Gateway", function () {
193
194
  expect(filePutResult.Ok()).toBeFalsy();
194
195
  });
195
196
 
196
- it("should get data from File Gateway", async function () {
197
+ it("should get data from File Gateway", async () => {
197
198
  const fileUrl = await fileGateway.buildUrl(ctx, fileStore.url(), fileContent.cid);
198
199
  await fileGateway.start(ctx, fileStore.url());
199
200
  await fileGateway.put(ctx, fileUrl.Ok(), {
@@ -205,7 +206,7 @@ describe("noop Gateway", function () {
205
206
  expect(fileGetResult.Ok().payload).toEqual(fileContent.block);
206
207
  });
207
208
 
208
- it("should delete data from File Gateway", async function () {
209
+ it("should delete data from File Gateway", async () => {
209
210
  const fileUrl = await fileGateway.buildUrl(ctx, fileStore.url(), fileContent.cid);
210
211
  await fileGateway.start(ctx, fileStore.url());
211
212
  await fileGateway.put(ctx, fileUrl.Ok(), {
@@ -216,20 +217,20 @@ describe("noop Gateway", function () {
216
217
  expect(fileDeleteResult.isOk()).toBeTruthy();
217
218
  });
218
219
 
219
- it("should close File Gateway", async function () {
220
+ it("should close File Gateway", async () => {
220
221
  await fileGateway.close(ctx, fileStore.url());
221
222
  });
222
- it("should build WAL Gateway URL", async function () {
223
+ it("should build WAL Gateway URL", async () => {
223
224
  const testKey = "bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i";
224
225
  const walUrl = await walGateway.buildUrl(ctx, walStore.url(), testKey);
225
226
  expect(walUrl.Ok()).toBeTruthy();
226
227
  });
227
228
 
228
- it("should start WAL Gateway", async function () {
229
+ it("should start WAL Gateway", async () => {
229
230
  await walGateway.start(ctx, walStore.url());
230
231
  });
231
232
 
232
- it("should put data to WAL Gateway", async function () {
233
+ it("should put data to WAL Gateway", async () => {
233
234
  const testKey = "bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i";
234
235
  const walUrl = await walGateway.buildUrl(ctx, walStore.url(), testKey);
235
236
  await walGateway.start(ctx, walStore.url());
@@ -246,7 +247,7 @@ describe("noop Gateway", function () {
246
247
  expect(walPutResult.Ok()).toBeFalsy();
247
248
  });
248
249
 
249
- it("should get data from WAL Gateway", async function () {
250
+ it("should get data from WAL Gateway", async () => {
250
251
  const testKey = "bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i";
251
252
  const walUrl = await walGateway.buildUrl(ctx, walStore.url(), testKey);
252
253
  await walGateway.start(ctx, walStore.url());
@@ -285,7 +286,7 @@ describe("noop Gateway", function () {
285
286
  expect(ref).toEqual(walGetResult.Ok().payload);
286
287
  });
287
288
 
288
- it("should delete data from WAL Gateway", async function () {
289
+ it("should delete data from WAL Gateway", async () => {
289
290
  const testKey = "bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i";
290
291
  const walUrl = await walGateway.buildUrl(ctx, walStore.url(), testKey);
291
292
  await walGateway.start(ctx, walStore.url());
@@ -315,12 +316,12 @@ describe("noop Gateway", function () {
315
316
  expect(walDeleteResult.isOk()).toBeTruthy();
316
317
  });
317
318
 
318
- it("should close WAL Gateway", async function () {
319
+ it("should close WAL Gateway", async () => {
319
320
  await walGateway.start(ctx, walStore.url());
320
321
  await walGateway.close(ctx, walStore.url());
321
322
  });
322
323
 
323
- // it("should have correct CAR Gateway properties", async function () {
324
+ // it("should have correct CAR Gateway properties", async () =>{
324
325
  // // CAR Gateway assertions
325
326
  // expect(carGateway.fidLength).toBe(4);
326
327
  // expect(carGateway.headerSize).toBe(36);
@@ -331,7 +332,7 @@ describe("noop Gateway", function () {
331
332
  // expect(carStore.).toHaveProperty("url");
332
333
  // });
333
334
 
334
- // it("should have correct Meta Gateway properties", async function () {
335
+ // it("should have correct Meta Gateway properties", async () =>{
335
336
  // // Meta Gateway assertions
336
337
  // expect(metaGateway.fidLength).toBe(4);
337
338
  // expect(metaGateway.headerSize).toBe(36);
@@ -342,7 +343,7 @@ describe("noop Gateway", function () {
342
343
  // expect(last).not.toHaveProperty("url");
343
344
  // });
344
345
 
345
- // it("should have correct File Gateway properties", async function () {
346
+ // it("should have correct File Gateway properties", async () =>{
346
347
  // // File Gateway assertions
347
348
  // expect(fileGateway.fidLength).toBe(4);
348
349
  // expect(fileGateway.headerSize).toBe(36);
@@ -353,7 +354,7 @@ describe("noop Gateway", function () {
353
354
  // expect(last).toHaveProperty("url");
354
355
  // });
355
356
 
356
- // it("should have correct WAL Gateway properties", async function () {
357
+ // it("should have correct WAL Gateway properties", async () =>{
357
358
  // // WAL Gateway assertions
358
359
  // expect(walGateway.fidLength).toBe(4);
359
360
  // expect(walGateway.headerSize).toBe(36);
@@ -374,20 +375,22 @@ describe("noop Gateway subscribe", function () {
374
375
  const sthis = ensureSuperThis();
375
376
  let ctx: bs.SerdeGatewayCtx;
376
377
 
377
- afterEach(async function () {
378
+ afterEach(async () => {
378
379
  await db.close();
379
380
  await db.destroy();
380
381
  });
381
- beforeEach(async function () {
382
+ beforeEach(async () => {
382
383
  db = fireproof("test-gateway-" + sthis.nextId().str);
383
384
 
385
+ await db.ready();
386
+
384
387
  ctx = { loader: db.ledger.crdt.blockstore.loader };
385
388
  // Extract stores from the loader
386
- metaStore = (await db.ledger.crdt.blockstore.loader?.metaStore()) as bs.MetaStore;
389
+ metaStore = db.ledger.crdt.blockstore.loader.attachedStores.local().active.meta;
387
390
 
388
391
  metaGateway = metaStore.realGateway;
389
392
  });
390
- it("should subscribe to meta Gateway", async function () {
393
+ it("should subscribe to meta Gateway", async () => {
391
394
  const metaUrl = await metaGateway.buildUrl(ctx, metaStore.url(), "main");
392
395
  await metaGateway.start(ctx, metaStore.url());
393
396
 
@@ -427,11 +430,11 @@ describe("Gateway", function () {
427
430
 
428
431
  let ctx: bs.SerdeGatewayCtx;
429
432
 
430
- afterEach(async function () {
433
+ afterEach(async () => {
431
434
  await db.close();
432
435
  await db.destroy();
433
436
  });
434
- beforeEach(async function () {
437
+ beforeEach(async () => {
435
438
  db = fireproof("test-gateway-" + sthis.nextId().str);
436
439
  ctx = { loader: db.ledger.crdt.blockstore.loader };
437
440
  const ok = await db.put({ _id: "test", foo: "bar" });
@@ -440,7 +443,7 @@ describe("Gateway", function () {
440
443
 
441
444
  // Extract stores from the loader
442
445
  // carStore = (await db.blockstore.loader.carStore()) as unknown as ExtendedStore;
443
- metaStore = (await db.ledger.crdt.blockstore.loader?.metaStore()) as bs.MetaStore;
446
+ metaStore = db.ledger.crdt.blockstore.loader.attachedStores.local().active.meta;
444
447
  // fileStore = (await db.blockstore.loader.fileStore()) as unknown as ExtendedStore;
445
448
  // walStore = (await db.blockstore.loader.WALStore()) as unknown as ExtendedStore;
446
449
 
@@ -451,7 +454,7 @@ describe("Gateway", function () {
451
454
  // walGateway = walStore.gateway;
452
455
  });
453
456
 
454
- it("should get data from Meta Gateway", async function () {
457
+ it("should get data from Meta Gateway", async () => {
455
458
  const metaUrl = await metaGateway.buildUrl(ctx, metaStore.url(), "main");
456
459
  await metaGateway.start(ctx, metaStore.url());
457
460
  const metaGetResult = await metaGateway.get(ctx, metaUrl.Ok());
@@ -463,7 +466,7 @@ describe("Gateway", function () {
463
466
  expect(Object.keys(meta[0])).toEqual(["eventCid", "parents", "dbMeta"]);
464
467
  });
465
468
 
466
- it("should delete data from Meta Gateway", async function () {
469
+ it("should delete data from Meta Gateway", async () => {
467
470
  const metaUrl = await metaGateway.buildUrl(ctx, metaStore.url(), "main");
468
471
  await metaGateway.start(ctx, metaStore.url());
469
472
  // should we be testing .destroy() instead?
@@ -0,0 +1,82 @@
1
+ import { Attachable, Database, fireproof, GatewayUrlsParam } from "@fireproof/core";
2
+
3
+ describe("join function", () => {
4
+ // export const connect: ConnectFunction = (
5
+ // db: Database,
6
+ // remoteDbName = "",
7
+ // url = "netlify://localhost:8888?protocol=ws"
8
+ // ) => {
9
+ // const { sthis, name: dbName } = db;
10
+ // if (!dbName) {
11
+ // throw new Error("dbName is required");
12
+ // }
13
+ // const urlObj = BuildURI.from(url);
14
+ // const existingName = urlObj.getParam("name");
15
+ // urlObj.defParam("name", remoteDbName || existingName || dbName);
16
+ // urlObj.defParam("localName", dbName);
17
+ // urlObj.defParam("storekey", `@${dbName}:data@`);
18
+ // return connectionCache.get(urlObj.toString()).once(() => {
19
+ // makeKeyBagUrlExtractable(sthis);
20
+ // const connection = connectionFactory(sthis, urlObj);
21
+ // connection.connect(db.ledger.crdt.blockstore);
22
+ // return connection;
23
+ // });
24
+ // };
25
+ class AJoinable implements Attachable {
26
+ readonly name: string;
27
+ constructor(name: string) {
28
+ this.name = name;
29
+ }
30
+ prepare(): Promise<GatewayUrlsParam> {
31
+ return Promise.resolve({
32
+ car: { url: "memory://${this.name}" },
33
+ meta: { url: "memory://${this.name}" },
34
+ file: { url: "memory://${this.name}" },
35
+ });
36
+ }
37
+ }
38
+ function aJoinable(name: string): Attachable {
39
+ return new AJoinable(name);
40
+ }
41
+
42
+ let db: Database;
43
+ beforeAll(async () => {
44
+ const set = Math.random().toString(16);
45
+ await Promise.all(
46
+ Array.from({ length: 10 }).map(async (_, i) => {
47
+ const db = fireproof(`remote-db-${i}-${set}`, {
48
+ storeUrls: {
49
+ base: `memory://remote-db-${i}-${set}`,
50
+ },
51
+ });
52
+ for (let j = 0; j < 10; j++) {
53
+ await db.put({ _id: `${i}-${j}`, value: `${i}-${set}` });
54
+ }
55
+ await db.close();
56
+ return db;
57
+ }),
58
+ );
59
+
60
+ db = fireproof(`db-${set}`, {
61
+ storeUrls: {
62
+ base: `memory://db-${set}`,
63
+ },
64
+ });
65
+ });
66
+ afterAll(async () => {
67
+ await db.close();
68
+ });
69
+
70
+ it("it is joinable", async () => {
71
+ for (let i = 0; i < 10; i++) {
72
+ // create meta/car/files store from aJoinable
73
+ // retrieve meta from attachable and subscribe to it
74
+ // merge incoming meta with local
75
+ // the attach meta includes source url's which ref back to aJoinable Attachable
76
+
77
+ const attached = await db.attach(aJoinable("i" + i));
78
+ expect(attached).toBeDefined();
79
+ await attached.detach();
80
+ }
81
+ });
82
+ });