@fireproof/core 0.19.8-dev-global → 0.19.8-dev-series-1

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 (64) hide show
  1. package/README.md +34 -0
  2. package/chunk-7EWIAXTM.js +7 -0
  3. package/chunk-7EWIAXTM.js.map +1 -0
  4. package/chunk-OFGPKRCM.js +290 -0
  5. package/chunk-OFGPKRCM.js.map +1 -0
  6. package/chunk-PB4BKL4O.js +7 -0
  7. package/chunk-PB4BKL4O.js.map +1 -0
  8. package/chunk-WS3YRPIA.js +75 -0
  9. package/chunk-WS3YRPIA.js.map +1 -0
  10. package/deno.json +20 -0
  11. package/gateway-5FCWPX5W.js +144 -0
  12. package/gateway-5FCWPX5W.js.map +1 -0
  13. package/{store-indexdb-WLRSICCB.js → gateway-H7UD6TNB.js} +49 -82
  14. package/gateway-H7UD6TNB.js.map +1 -0
  15. package/index.cjs +2317 -1838
  16. package/index.cjs.map +1 -1
  17. package/index.d.cts +663 -535
  18. package/index.d.ts +663 -535
  19. package/index.global.js +26290 -20732
  20. package/index.global.js.map +1 -1
  21. package/index.js +1618 -1032
  22. package/index.js.map +1 -1
  23. package/key-bag-file-WADZBHYG.js +54 -0
  24. package/key-bag-file-WADZBHYG.js.map +1 -0
  25. package/key-bag-indexdb-PGVAI3FJ.js +50 -0
  26. package/key-bag-indexdb-PGVAI3FJ.js.map +1 -0
  27. package/mem-filesystem-YPPJV7Q2.js +41 -0
  28. package/mem-filesystem-YPPJV7Q2.js.map +1 -0
  29. package/metafile-cjs.json +1 -1
  30. package/metafile-esm.json +1 -1
  31. package/metafile-iife.json +1 -1
  32. package/node-filesystem-INX4ZTHE.js +45 -0
  33. package/node-filesystem-INX4ZTHE.js.map +1 -0
  34. package/package.json +12 -8
  35. package/tests/blockstore/fragment-gateway.test.ts +107 -0
  36. package/tests/blockstore/keyed-crypto.test.ts +332 -0
  37. package/tests/blockstore/loader.test.ts +24 -19
  38. package/tests/blockstore/store.test.ts +51 -40
  39. package/tests/blockstore/transaction.test.ts +19 -15
  40. package/tests/fireproof/all-gateway.test.ts +394 -0
  41. package/tests/fireproof/cars/bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i.car +0 -0
  42. package/tests/fireproof/cars/bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i.ts +316 -0
  43. package/tests/fireproof/config.test.ts +94 -78
  44. package/tests/fireproof/crdt.test.ts +34 -28
  45. package/tests/fireproof/database.test.ts +22 -14
  46. package/tests/fireproof/fireproof.test.fixture.ts +133 -0
  47. package/tests/fireproof/fireproof.test.ts +331 -219
  48. package/tests/fireproof/hello.test.ts +34 -18
  49. package/tests/fireproof/indexer.test.ts +34 -27
  50. package/tests/fireproof/utils.test.ts +84 -0
  51. package/tests/helpers.ts +28 -57
  52. package/tests/www/todo-local.html +1 -1
  53. package/tests/www/todo.html +12 -15
  54. package/utils-QO2HIWGI.js +14 -0
  55. package/utils-QO2HIWGI.js.map +1 -0
  56. package/chunk-BNL4PVBF.js +0 -314
  57. package/chunk-BNL4PVBF.js.map +0 -1
  58. package/chunk-JW2QT6BF.js +0 -184
  59. package/chunk-JW2QT6BF.js.map +0 -1
  60. package/node-sys-container-MIEX6ELJ.js +0 -29
  61. package/node-sys-container-MIEX6ELJ.js.map +0 -1
  62. package/store-file-VJ6BI4II.js +0 -191
  63. package/store-file-VJ6BI4II.js.map +0 -1
  64. package/store-indexdb-WLRSICCB.js.map +0 -1
@@ -1,16 +1,16 @@
1
1
  import { CID } from "multiformats";
2
- import { rt, bs } from "@fireproof/core";
3
- import { MockLogger } from "@adviser/cement";
4
- import { NotFoundError } from "../../src/blockstore/gateway";
2
+ import { bs, NotFoundError, SuperThis } from "@fireproof/core";
3
+ import { mockSuperThis } from "../helpers";
5
4
 
6
5
  const decoder = new TextDecoder("utf-8");
7
6
 
8
- function runtime() {
9
- return bs.toStoreRuntime({}, MockLogger().logger);
7
+ function runtime(sthis: SuperThis) {
8
+ return bs.toStoreRuntime({}, sthis);
10
9
  }
11
10
 
12
- function mockLoader(name: string): bs.Loadable {
11
+ function mockLoader(sthis: SuperThis, name: string): bs.Loadable {
13
12
  return {
13
+ sthis,
14
14
  name,
15
15
  ebOpts: {
16
16
  store: {},
@@ -20,18 +20,19 @@ function mockLoader(name: string): bs.Loadable {
20
20
 
21
21
  describe("DataStore", function () {
22
22
  let store: bs.DataStore;
23
- let raw: bs.TestStore;
23
+ let raw: bs.TestGateway;
24
24
 
25
+ const sthis = mockSuperThis();
25
26
  afterEach(async () => {
26
27
  await store.close();
27
28
  await store.destroy();
28
29
  });
29
30
 
30
31
  beforeEach(async () => {
31
- await rt.SysContainer.start();
32
- store = await runtime().makeDataStore(mockLoader("test"));
32
+ await sthis.start();
33
+ store = await runtime(sthis).makeDataStore(mockLoader(sthis, "test"));
33
34
  await store.start();
34
- raw = await bs.testStoreFactory(store.url);
35
+ raw = await bs.testStoreFactory(store.url(), sthis);
35
36
  });
36
37
 
37
38
  it("should have a name", function () {
@@ -44,26 +45,28 @@ describe("DataStore", function () {
44
45
  bytes: new Uint8Array([55, 56, 57]),
45
46
  };
46
47
  await store.save(car);
47
- const data = await raw.get(store.url, car.cid.toString());
48
+ const data = await raw.get(store.url(), car.cid.toString());
48
49
  expect(decoder.decode(data)).toEqual(decoder.decode(car.bytes));
49
50
  });
50
51
  });
51
52
 
52
53
  describe("DataStore with a saved car", function () {
53
54
  let store: bs.DataStore;
54
- let raw: bs.TestStore;
55
+ let raw: bs.TestGateway;
55
56
  let car: bs.AnyBlock;
56
57
 
58
+ const sthis = mockSuperThis();
59
+
57
60
  afterEach(async () => {
58
61
  await store.close();
59
62
  await store.destroy();
60
63
  });
61
64
 
62
65
  beforeEach(async function () {
63
- await rt.SysContainer.start();
64
- store = await runtime().makeDataStore(mockLoader("test2"));
66
+ await sthis.start();
67
+ store = await runtime(sthis).makeDataStore(mockLoader(sthis, "test2"));
65
68
  await store.start();
66
- raw = await bs.testStoreFactory(store.url);
69
+ raw = await bs.testStoreFactory(store.url(), sthis);
67
70
  car = {
68
71
  cid: "cid" as unknown as CID,
69
72
  bytes: new Uint8Array([55, 56, 57, 80]),
@@ -72,7 +75,7 @@ describe("DataStore with a saved car", function () {
72
75
  });
73
76
 
74
77
  it("should have a car", async function () {
75
- const data = await raw.get(store.url, car.cid.toString());
78
+ const data = await raw.get(store.url(), car.cid.toString());
76
79
  expect(decoder.decode(data)).toEqual(decoder.decode(car.bytes));
77
80
  });
78
81
 
@@ -85,15 +88,16 @@ describe("DataStore with a saved car", function () {
85
88
 
86
89
  it("should remove a car", async function () {
87
90
  await store.remove(car.cid);
88
- const error = (await store.load(car.cid).catch((e: Error) => e)) as NotFoundError;
89
- expect(error.code).toMatch("ENOENT");
90
- // matches(error.message, "ENOENT");
91
+ const { e: error } = (await store.load(car.cid).catch((e: Error) => ({ e }))) as { e: NotFoundError };
92
+ expect(error).toBeTruthy();
91
93
  });
92
94
  });
93
95
 
94
96
  describe("MetaStore", function () {
95
97
  let store: bs.MetaStore;
96
- let raw: bs.TestStore;
98
+ let raw: bs.TestGateway;
99
+
100
+ const sthis = mockSuperThis();
97
101
 
98
102
  afterEach(async () => {
99
103
  await store.close();
@@ -101,10 +105,10 @@ describe("MetaStore", function () {
101
105
  });
102
106
 
103
107
  beforeEach(async function () {
104
- await rt.SysContainer.start();
105
- store = await runtime().makeMetaStore(mockLoader("test"));
108
+ await sthis.start();
109
+ store = await runtime(sthis).makeMetaStore(mockLoader(sthis, "test"));
106
110
  await store.start();
107
- raw = await bs.testStoreFactory(store.url);
111
+ raw = await bs.testStoreFactory(store.url(), sthis);
108
112
  });
109
113
 
110
114
  it("should have a name", function () {
@@ -115,21 +119,23 @@ describe("MetaStore", function () {
115
119
  const cid = CID.parse("bafybeia4luuns6dgymy5kau5rm7r4qzrrzg6cglpzpogussprpy42cmcn4");
116
120
  const h: bs.DbMeta = {
117
121
  cars: [cid],
118
- key: undefined,
122
+ // key: undefined,
119
123
  };
120
124
  await store.save(h);
121
- const file = await raw.get(store.url, "main");
122
- const header = JSON.parse(decoder.decode(file));
123
- expect(header).toBeTruthy();
124
- expect(header.cars).toBeTruthy();
125
- expect(header.cars[0]["/"]).toEqual(cid.toString());
125
+ const file = await raw.get(store.url(), "main");
126
+ const [blockMeta] = await store.handleByteHeads(file);
127
+ const decodedHeader = blockMeta.dbMeta;
128
+ expect(decodedHeader).toBeTruthy();
129
+ expect(decodedHeader.cars).toBeTruthy();
130
+ expect(decodedHeader.cars[0].toString()).toEqual(cid.toString());
126
131
  });
127
132
  });
128
133
 
129
134
  describe("MetaStore with a saved header", function () {
130
135
  let store: bs.MetaStore;
131
- let raw: bs.TestStore;
136
+ let raw: bs.TestGateway;
132
137
  let cid: CID;
138
+ const sthis = mockSuperThis();
133
139
 
134
140
  afterEach(async () => {
135
141
  await store.close();
@@ -137,21 +143,26 @@ describe("MetaStore with a saved header", function () {
137
143
  });
138
144
 
139
145
  beforeEach(async function () {
140
- await rt.SysContainer.start();
141
- store = await runtime().makeMetaStore(mockLoader("test-saved-header"));
146
+ await sthis.start();
147
+ store = await runtime(sthis).makeMetaStore(mockLoader(sthis, "test-saved-header"));
142
148
  await store.start();
143
- raw = await bs.testStoreFactory(store.url);
149
+ raw = await bs.testStoreFactory(store.url(), sthis);
144
150
  cid = CID.parse("bafybeia4luuns6dgymy5kau5rm7r4qzrrzg6cglpzpogussprpy42cmcn4");
145
- await store.save({ cars: [cid], key: undefined });
151
+ await store.save({ cars: [cid] /*, key: undefined */ });
146
152
  });
147
153
 
148
154
  it("should have a header", async function () {
149
- const data = decoder.decode(await raw.get(store.url, "main"));
150
- expect(data).toMatch(/car/);
151
- const header = JSON.parse(data);
152
- expect(header).toBeTruthy();
153
- expect(header.cars).toBeTruthy();
154
- expect(header.cars[0]["/"]).toEqual(cid.toString());
155
+ const bytes = await raw.get(store.url(), "main");
156
+ const data = decoder.decode(bytes);
157
+ expect(data).toMatch(/parents/);
158
+ const header = JSON.parse(data)[0];
159
+ expect(header).toBeDefined();
160
+ expect(header.parents).toBeDefined();
161
+ const [blockMeta] = await store.handleByteHeads(bytes);
162
+ const decodedHeader = blockMeta.dbMeta;
163
+ expect(decodedHeader).toBeDefined();
164
+ expect(decodedHeader.cars).toBeDefined();
165
+ expect(decodedHeader.cars[0].toString()).toEqual(cid.toString());
155
166
  });
156
167
 
157
168
  it("should load a header", async function () {
@@ -1,9 +1,10 @@
1
1
  import { CID } from "multiformats";
2
- // import { matches, equalsJSON } from "../helpers.js";
3
- import { bs } from "@fireproof/core";
2
+ import { bs, SuperThis } from "@fireproof/core";
3
+ import { mockSuperThis } from "../helpers";
4
4
 
5
5
  describe("Fresh TransactionBlockstore", function () {
6
6
  let blocks: bs.BaseBlockstore;
7
+ const sthis = mockSuperThis();
7
8
  beforeEach(function () {
8
9
  blocks = new bs.BaseBlockstore();
9
10
  });
@@ -14,7 +15,7 @@ describe("Fresh TransactionBlockstore", function () {
14
15
  expect(blocks.loader).toBeFalsy();
15
16
  });
16
17
  it("should not put", async function () {
17
- const value = new TextEncoder().encode("value");
18
+ const value = sthis.txt.encode("value");
18
19
  const e = await blocks.put("key" as unknown as bs.AnyLink, value).catch((e) => e);
19
20
  expect(e.message).toMatch(/transaction/g);
20
21
  });
@@ -32,8 +33,9 @@ describe("Fresh TransactionBlockstore", function () {
32
33
 
33
34
  describe("TransactionBlockstore with name", function () {
34
35
  let blocks: bs.EncryptedBlockstore;
36
+ const sthis = mockSuperThis();
35
37
  beforeEach(function () {
36
- blocks = new bs.EncryptedBlockstore({ name: "test" });
38
+ blocks = new bs.EncryptedBlockstore(sthis, { name: "test" });
37
39
  });
38
40
  it("should have a name", function () {
39
41
  expect(blocks.name).toEqual("test");
@@ -42,7 +44,7 @@ describe("TransactionBlockstore with name", function () {
42
44
  expect(blocks.loader).toBeTruthy();
43
45
  });
44
46
  it("should get from loader", async function () {
45
- const bytes = new TextEncoder().encode("bytes");
47
+ const bytes = sthis.txt.encode("bytes");
46
48
  expect(blocks.loader).toBeTruthy();
47
49
  blocks.loader.getBlock = async (cid) => {
48
50
  return { cid, bytes };
@@ -55,14 +57,15 @@ describe("TransactionBlockstore with name", function () {
55
57
  describe("A transaction", function () {
56
58
  let tblocks: bs.CarTransaction;
57
59
  let blocks: bs.EncryptedBlockstore;
60
+ const sthis = mockSuperThis();
58
61
  beforeEach(async function () {
59
- blocks = new bs.EncryptedBlockstore({ name: "test" });
62
+ blocks = new bs.EncryptedBlockstore(sthis, { name: "test" });
60
63
  tblocks = new bs.CarTransaction(blocks);
61
64
  blocks.transactions.add(tblocks);
62
65
  });
63
66
  it("should put and get", async function () {
64
67
  const cid = CID.parse("bafybeia4luuns6dgymy5kau5rm7r4qzrrzg6cglpzpogussprpy42cmcn4");
65
- const bytes = new TextEncoder().encode("bytes");
68
+ const bytes = sthis.txt.encode("bytes");
66
69
  await tblocks.put(cid, bytes);
67
70
  expect(blocks.transactions.has(tblocks)).toBeTruthy();
68
71
  const got = await tblocks.get(cid);
@@ -72,14 +75,15 @@ describe("A transaction", function () {
72
75
  });
73
76
  });
74
77
 
75
- function asUInt8Array(str: string) {
76
- return new TextEncoder().encode(str);
78
+ function asUInt8Array(str: string, sthis: SuperThis) {
79
+ return sthis.txt.encode(str);
77
80
  }
78
81
 
79
82
  describe("TransactionBlockstore with a completed transaction", function () {
80
83
  let blocks: bs.BaseBlockstore;
81
84
  let cid: CID;
82
85
  let cid2: CID;
86
+ const sthis = mockSuperThis();
83
87
 
84
88
  beforeEach(async function () {
85
89
  cid = CID.parse("bafybeia4luuns6dgymy5kau5rm7r4qzrrzg6cglpzpogussprpy42cmcn4");
@@ -87,13 +91,13 @@ describe("TransactionBlockstore with a completed transaction", function () {
87
91
 
88
92
  blocks = new bs.BaseBlockstore();
89
93
  await blocks.transaction(async (tblocks) => {
90
- await tblocks.put(cid, asUInt8Array("value"));
91
- await tblocks.put(cid2, asUInt8Array("value2"));
94
+ await tblocks.put(cid, asUInt8Array("value", sthis));
95
+ await tblocks.put(cid, asUInt8Array("value", sthis));
92
96
  return { head: [] };
93
97
  });
94
98
  await blocks.transaction(async (tblocks) => {
95
- await tblocks.put(cid, asUInt8Array("value"));
96
- await tblocks.put(cid2, asUInt8Array("value2"));
99
+ await tblocks.put(cid, asUInt8Array("value", sthis));
100
+ await tblocks.put(cid2, asUInt8Array("value2", sthis));
97
101
  return { head: [] };
98
102
  });
99
103
  });
@@ -104,10 +108,10 @@ describe("TransactionBlockstore with a completed transaction", function () {
104
108
  it("should get", async function () {
105
109
  const value = (await blocks.get(cid)) as bs.AnyBlock;
106
110
  expect(value.cid).toEqual(cid);
107
- expect(value.bytes.toString()).toEqual(asUInt8Array("value").toString());
111
+ expect(value.bytes.toString()).toEqual(asUInt8Array("value", sthis).toString());
108
112
 
109
113
  const value2 = (await blocks.get(cid2)) as bs.AnyBlock;
110
- expect(value2.bytes.toString()).toEqual(asUInt8Array("value2").toString());
114
+ expect(value2.bytes.toString()).toEqual(asUInt8Array("value2", sthis).toString());
111
115
  });
112
116
  it("should yield entries", async function () {
113
117
  const blz = [];
@@ -0,0 +1,394 @@
1
+ import { Database, bs } from "@fireproof/core";
2
+ import { URI } from "@adviser/cement";
3
+
4
+ import { fileContent } from "./cars/bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i.js";
5
+
6
+ function customExpect(value: unknown, matcher: (val: unknown) => void, message: string): void {
7
+ try {
8
+ matcher(value);
9
+ } catch (error) {
10
+ void error;
11
+ // console.error(error);
12
+ throw new Error(message);
13
+ }
14
+ }
15
+
16
+ interface ExtendedGateway extends bs.Gateway {
17
+ logger: { _attributes: { module: string; url?: string } };
18
+ headerSize: number;
19
+ fidLength: number;
20
+ }
21
+
22
+ interface ExtendedStore {
23
+ gateway: ExtendedGateway;
24
+ _url: URI;
25
+ name: string;
26
+ }
27
+
28
+ describe("noop Gateway", function () {
29
+ let db: Database;
30
+ let carStore: ExtendedStore;
31
+ let metaStore: ExtendedStore;
32
+ let fileStore: ExtendedStore;
33
+ let walStore: ExtendedStore;
34
+ let carGateway: ExtendedGateway;
35
+ let metaGateway: ExtendedGateway;
36
+ let fileGateway: ExtendedGateway;
37
+ let walGateway: ExtendedGateway;
38
+
39
+ afterEach(async function () {
40
+ await db.close();
41
+ await db.destroy();
42
+ });
43
+ beforeEach(async function () {
44
+ db = new Database("test-gateway-" + Math.random().toString(36).substring(7));
45
+
46
+ // Extract stores from the loader
47
+ carStore = (await db.blockstore.loader?.carStore()) as unknown as ExtendedStore;
48
+ metaStore = (await db.blockstore.loader?.metaStore()) as unknown as ExtendedStore;
49
+ fileStore = (await db.blockstore.loader?.fileStore()) as unknown as ExtendedStore;
50
+ walStore = (await db.blockstore.loader?.WALStore()) as unknown as ExtendedStore;
51
+
52
+ // Extract and log gateways
53
+ carGateway = carStore?.gateway;
54
+ metaGateway = metaStore?.gateway;
55
+ fileGateway = fileStore?.gateway;
56
+ walGateway = walStore?.gateway;
57
+ });
58
+
59
+ it("should have valid stores and gateways", async function () {
60
+ // Add assertions
61
+ expect(carStore).toBeTruthy();
62
+ expect(metaStore).toBeTruthy();
63
+ expect(fileStore).toBeTruthy();
64
+ expect(walStore).toBeTruthy();
65
+
66
+ expect(carGateway).toBeTruthy();
67
+ expect(metaGateway).toBeTruthy();
68
+ expect(fileGateway).toBeTruthy();
69
+ expect(walGateway).toBeTruthy();
70
+ });
71
+
72
+ it("should have correct store names", async function () {
73
+ // Check that all stores have the correct name
74
+ expect(carStore?.name).toContain("test-gateway");
75
+ expect(metaStore?.name).toContain("test-gateway");
76
+ expect(fileStore?.name).toContain("test-gateway");
77
+ expect(walStore?.name).toContain("test-gateway");
78
+ });
79
+
80
+ it("should have correct store types in URLs", async function () {
81
+ // Check that all stores have the correct store type in their URL
82
+ expect(carStore?._url.toString()).toContain("store=data");
83
+ expect(metaStore?._url.toString()).toContain("store=meta");
84
+ expect(fileStore?._url.toString()).toContain("store=data");
85
+ expect(walStore?._url.toString()).toContain("store=wal");
86
+ });
87
+
88
+ it("should have version specified in URLs", async function () {
89
+ // Verify that all stores have a version specified
90
+ expect(carStore?._url.toString()).toContain("version=");
91
+ expect(metaStore?._url.toString()).toContain("version=");
92
+ expect(fileStore?._url.toString()).toContain("version=");
93
+ expect(walStore?._url.toString()).toContain("version=");
94
+ });
95
+
96
+ it("should have correct gateway types", async function () {
97
+ // Check that all gateways are instances of the expected gateway class
98
+ expect(typeof carGateway).toBe("object");
99
+ expect(typeof metaGateway).toBe("object");
100
+ expect(typeof fileGateway).toBe("object");
101
+ expect(typeof walGateway).toBe("object");
102
+ });
103
+
104
+ it("should build CAR Gateway URL", async function () {
105
+ const testKey = "bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i";
106
+ const carUrl = await carGateway?.buildUrl(carStore?._url, testKey);
107
+ expect(carUrl?.Ok()).toBeTruthy();
108
+ });
109
+
110
+ it("should start CAR Gateway", async function () {
111
+ await carGateway?.start(carStore?._url);
112
+ });
113
+
114
+ it("should put data in CAR Gateway", async function () {
115
+ const testKey = "bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i";
116
+ const testData = fileContent;
117
+ const carUrl = await carGateway?.buildUrl(carStore?._url, testKey);
118
+ await carGateway?.start(carStore?._url);
119
+ const carPutResult = await carGateway?.put(carUrl?.Ok(), testData);
120
+ expect(carPutResult?.Ok()).toBeFalsy();
121
+ });
122
+
123
+ it("should get data from CAR Gateway", async function () {
124
+ const testKey = "bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i";
125
+ const testData = fileContent;
126
+ const carUrl = await carGateway?.buildUrl(carStore?._url, testKey);
127
+ await carGateway?.start(carStore?._url);
128
+ await carGateway?.put(carUrl?.Ok(), testData);
129
+ const carGetResult = await carGateway?.get(carUrl?.Ok());
130
+ customExpect(carGetResult?.Ok(), (v) => expect(v).toEqual(testData), "carGetResult should match testData");
131
+ });
132
+
133
+ it("should delete data from CAR Gateway", async function () {
134
+ const testKey = "bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i";
135
+ const testData = fileContent;
136
+ const carUrl = await carGateway?.buildUrl(carStore?._url, testKey);
137
+ await carGateway?.start(carStore?._url);
138
+ await carGateway?.put(carUrl?.Ok(), testData);
139
+ const carDeleteResult = await carGateway?.delete(carUrl?.Ok());
140
+ expect(carDeleteResult?.Ok()).toBeFalsy();
141
+ });
142
+
143
+ it("should close CAR Gateway", async function () {
144
+ await carGateway?.close(carStore?._url);
145
+ });
146
+ it("should build Meta Gateway URL", async function () {
147
+ const metaUrl = await metaGateway?.buildUrl(metaStore?._url, "main");
148
+ expect(metaUrl?.Ok()).toBeTruthy();
149
+ });
150
+
151
+ it("should start Meta Gateway", async function () {
152
+ await metaGateway?.start(metaStore?._url);
153
+ });
154
+
155
+ it("should close Meta Gateway", async function () {
156
+ await metaGateway?.start(metaStore?._url);
157
+ await metaGateway?.close(metaStore?._url);
158
+ });
159
+
160
+ it("should build File Gateway URL", async function () {
161
+ const testKey = "bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i";
162
+ const fileUrl = await fileGateway?.buildUrl(fileStore?._url, testKey);
163
+ expect(fileUrl?.Ok()).toBeTruthy();
164
+ });
165
+
166
+ it("should start File Gateway", async function () {
167
+ await fileGateway?.start(fileStore?._url);
168
+ });
169
+
170
+ it("should put data to File Gateway", async function () {
171
+ const testKey = "bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i";
172
+ const testData = fileContent;
173
+ const fileUrl = await fileGateway?.buildUrl(fileStore?._url, testKey);
174
+ await fileGateway?.start(fileStore?._url);
175
+ const filePutResult = await fileGateway?.put(fileUrl?.Ok(), testData);
176
+ expect(filePutResult?.Ok()).toBeFalsy();
177
+ });
178
+
179
+ it("should get data from File Gateway", async function () {
180
+ const testKey = "bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i";
181
+ const testData = fileContent;
182
+ const fileUrl = await fileGateway?.buildUrl(fileStore?._url, testKey);
183
+ await fileGateway?.start(fileStore?._url);
184
+ await fileGateway?.put(fileUrl?.Ok(), testData);
185
+ const fileGetResult = await fileGateway?.get(fileUrl?.Ok());
186
+ customExpect(fileGetResult?.Ok(), (v) => expect(v).toEqual(testData), "fileGetResult should match testData");
187
+ });
188
+
189
+ it("should delete data from File Gateway", async function () {
190
+ const testKey = "bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i";
191
+ const testData = fileContent;
192
+ const fileUrl = await fileGateway?.buildUrl(fileStore?._url, testKey);
193
+ await fileGateway?.start(fileStore?._url);
194
+ await fileGateway?.put(fileUrl?.Ok(), testData);
195
+ const fileDeleteResult = await fileGateway?.delete(fileUrl?.Ok());
196
+ expect(fileDeleteResult?.Ok()).toBeFalsy();
197
+ });
198
+
199
+ it("should close File Gateway", async function () {
200
+ await fileGateway?.close(fileStore?._url);
201
+ });
202
+ it("should build WAL Gateway URL", async function () {
203
+ const testKey = "bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i";
204
+ const walUrl = await walGateway?.buildUrl(walStore?._url, testKey);
205
+ expect(walUrl?.Ok()).toBeTruthy();
206
+ });
207
+
208
+ it("should start WAL Gateway", async function () {
209
+ await walGateway?.start(walStore?._url);
210
+ });
211
+
212
+ it("should put data to WAL Gateway", async function () {
213
+ const testKey = "bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i";
214
+ const walUrl = await walGateway?.buildUrl(walStore?._url, testKey);
215
+ await walGateway?.start(walStore?._url);
216
+ const walTestDataString = JSON.stringify({
217
+ operations: [],
218
+ noLoaderOps: [],
219
+ fileOperations: [],
220
+ });
221
+ const walEncoder = new TextEncoder();
222
+ const walTestData = walEncoder.encode(walTestDataString);
223
+ const walPutResult = await walGateway?.put(walUrl?.Ok(), walTestData);
224
+ expect(walPutResult?.Ok()).toBeFalsy();
225
+ });
226
+
227
+ it("should get data from WAL Gateway", async function () {
228
+ const testKey = "bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i";
229
+ const walUrl = await walGateway?.buildUrl(walStore?._url, testKey);
230
+ await walGateway?.start(walStore?._url);
231
+ const walTestDataString = JSON.stringify({
232
+ operations: [],
233
+ noLoaderOps: [],
234
+ fileOperations: [],
235
+ });
236
+ const walEncoder = new TextEncoder();
237
+ const walTestData = walEncoder.encode(walTestDataString);
238
+ await walGateway?.put(walUrl?.Ok(), walTestData);
239
+ const walGetResult = await walGateway?.get(walUrl?.Ok());
240
+ const okResult = walGetResult?.Ok();
241
+ const decodedResult = new TextDecoder().decode(okResult);
242
+ expect(decodedResult).toEqual(walTestDataString);
243
+ });
244
+
245
+ it("should delete data from WAL Gateway", async function () {
246
+ const testKey = "bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i";
247
+ const walUrl = await walGateway?.buildUrl(walStore?._url, testKey);
248
+ await walGateway?.start(walStore?._url);
249
+ const walTestDataString = JSON.stringify({
250
+ operations: [],
251
+ noLoaderOps: [],
252
+ fileOperations: [],
253
+ });
254
+ const walEncoder = new TextEncoder();
255
+ const walTestData = walEncoder.encode(walTestDataString);
256
+ await walGateway?.put(walUrl?.Ok(), walTestData);
257
+ const walDeleteResult = await walGateway?.delete(walUrl?.Ok());
258
+ expect(walDeleteResult?.Ok()).toBeFalsy();
259
+ });
260
+
261
+ it("should close WAL Gateway", async function () {
262
+ await walGateway?.start(walStore?._url);
263
+ await walGateway?.close(walStore?._url);
264
+ });
265
+
266
+ it("should have correct CAR Gateway properties", async function () {
267
+ // CAR Gateway assertions
268
+ expect(carGateway?.fidLength).toBe(4);
269
+ expect(carGateway?.headerSize).toBe(36);
270
+ expect(carGateway?.logger._attributes).toHaveProperty("module");
271
+ expect(carGateway?.logger._attributes).toHaveProperty("url");
272
+ });
273
+
274
+ it("should have correct Meta Gateway properties", async function () {
275
+ // Meta Gateway assertions
276
+ expect(metaGateway?.fidLength).toBe(4);
277
+ expect(metaGateway?.headerSize).toBe(36);
278
+ expect(metaGateway?.logger._attributes).toHaveProperty("module");
279
+ expect(metaGateway?.logger._attributes).not.toHaveProperty("url");
280
+ });
281
+
282
+ it("should have correct File Gateway properties", async function () {
283
+ // File Gateway assertions
284
+ expect(fileGateway?.fidLength).toBe(4);
285
+ expect(fileGateway?.headerSize).toBe(36);
286
+ expect(fileGateway?.logger._attributes).toHaveProperty("module");
287
+ expect(fileGateway?.logger._attributes).toHaveProperty("url");
288
+ });
289
+
290
+ it("should have correct WAL Gateway properties", async function () {
291
+ // WAL Gateway assertions
292
+ expect(walGateway?.fidLength).toBe(4);
293
+ expect(walGateway?.headerSize).toBe(36);
294
+ expect(walGateway?.logger._attributes).toHaveProperty("module");
295
+ expect(walGateway?.logger._attributes).not.toHaveProperty("url");
296
+ });
297
+ });
298
+
299
+ describe("noop Gateway subscribe", function () {
300
+ let db: Database;
301
+
302
+ let metaStore: ExtendedStore;
303
+
304
+ let metaGateway: ExtendedGateway;
305
+
306
+ afterEach(async function () {
307
+ await db.close();
308
+ await db.destroy();
309
+ });
310
+ beforeEach(async function () {
311
+ db = new Database("test-gateway-" + Math.random().toString(36).substring(7));
312
+
313
+ // Extract stores from the loader
314
+ metaStore = (await db.blockstore.loader?.metaStore()) as unknown as ExtendedStore;
315
+
316
+ metaGateway = metaStore?.gateway;
317
+ });
318
+ it("should subscribe to meta Gateway", async function () {
319
+ const metaUrl = await metaGateway?.buildUrl(metaStore?._url, "main");
320
+ await metaGateway?.start(metaStore?._url);
321
+
322
+ let resolve: () => void;
323
+ let didCall = false;
324
+ const p = new Promise<void>((r) => {
325
+ resolve = r;
326
+ });
327
+ const metaSubscribeResult = await metaGateway?.subscribe?.(metaUrl?.Ok(), async (data: Uint8Array) => {
328
+ const decodedData = new TextDecoder().decode(data);
329
+ expect(decodedData).toContain("[]");
330
+ didCall = true;
331
+ resolve();
332
+ });
333
+ if (!metaSubscribeResult?.isErr()) {
334
+ expect(metaSubscribeResult?.Ok()).toBeTruthy();
335
+ const ok = await db.put({ _id: "key1", hello: "world1" });
336
+ expect(ok).toBeTruthy();
337
+ expect(ok.id).toBe("key1");
338
+ await p;
339
+ expect(didCall).toBeTruthy();
340
+ }
341
+ });
342
+ });
343
+
344
+ describe("Gateway", function () {
345
+ let db: Database;
346
+ // let carStore: ExtendedStore;
347
+ let metaStore: ExtendedStore;
348
+ // let fileStore: ExtendedStore;
349
+ // let walStore: ExtendedStore;
350
+ // let carGateway: ExtendedGateway;
351
+ let metaGateway: ExtendedGateway;
352
+ // let fileGateway: ExtendedGateway;
353
+ // let walGateway: ExtendedGateway;
354
+
355
+ afterEach(async function () {
356
+ await db.close();
357
+ await db.destroy();
358
+ });
359
+ beforeEach(async function () {
360
+ db = new Database("test-gateway-" + Math.random().toString(36).substring(7));
361
+ const ok = await db.put({ _id: "test", foo: "bar" });
362
+ expect(ok).toBeTruthy();
363
+ expect(ok.id).toBe("test");
364
+
365
+ // Extract stores from the loader
366
+ // carStore = (await db.blockstore.loader?.carStore()) as unknown as ExtendedStore;
367
+ metaStore = (await db.blockstore.loader?.metaStore()) as unknown as ExtendedStore;
368
+ // fileStore = (await db.blockstore.loader?.fileStore()) as unknown as ExtendedStore;
369
+ // walStore = (await db.blockstore.loader?.WALStore()) as unknown as ExtendedStore;
370
+
371
+ // Extract and log gateways
372
+ // carGateway = carStore?.gateway;
373
+ metaGateway = metaStore?.gateway;
374
+ // fileGateway = fileStore?.gateway;
375
+ // walGateway = walStore?.gateway;
376
+ });
377
+
378
+ it("should get data from Meta Gateway", async function () {
379
+ const metaUrl = await metaGateway?.buildUrl(metaStore?._url, "main");
380
+ await metaGateway?.start(metaStore?._url);
381
+ const metaGetResult = await metaGateway?.get(metaUrl?.Ok());
382
+ const metaGetResultOk = metaGetResult?.Ok();
383
+ const decodedMetaGetResultOk = new TextDecoder().decode(metaGetResultOk);
384
+ expect(decodedMetaGetResultOk).toContain("parents");
385
+ });
386
+
387
+ it("should delete data from Meta Gateway", async function () {
388
+ const metaUrl = await metaGateway?.buildUrl(metaStore?._url, "main");
389
+ await metaGateway?.start(metaStore?._url);
390
+ // should we be testing .destroy() instead?
391
+ const metaDeleteResult = await metaGateway?.delete(metaUrl?.Ok());
392
+ expect(metaDeleteResult?.Ok()).toBeFalsy();
393
+ });
394
+ });