@fireproof/core 0.19.121-dev → 0.20.0-dev-preview-05

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 (103) hide show
  1. package/README.md +13 -12
  2. package/deno/index.d.ts +7 -0
  3. package/deno/index.js +66 -0
  4. package/deno/index.js.map +1 -0
  5. package/deno/metafile-esm.json +1 -0
  6. package/deno.json +3 -4
  7. package/index.cjs +1797 -1431
  8. package/index.cjs.map +1 -1
  9. package/index.d.cts +760 -381
  10. package/index.d.ts +760 -381
  11. package/index.js +1976 -913
  12. package/index.js.map +1 -1
  13. package/metafile-cjs.json +1 -1
  14. package/metafile-esm.json +1 -1
  15. package/node/{node-filesystem.cjs → index.cjs} +17 -6
  16. package/node/index.cjs.map +1 -0
  17. package/node/index.d.cts +7 -0
  18. package/node/index.d.ts +7 -0
  19. package/node/{node-filesystem.js → index.js} +25 -5
  20. package/node/index.js.map +1 -0
  21. package/node/metafile-cjs.json +1 -1
  22. package/node/metafile-esm.json +1 -1
  23. package/package.json +27 -17
  24. package/react/index.cjs +22 -22
  25. package/react/index.cjs.map +1 -1
  26. package/react/index.d.cts +7 -7
  27. package/react/index.d.ts +7 -7
  28. package/react/index.js +22 -22
  29. package/react/index.js.map +1 -1
  30. package/react/metafile-cjs.json +1 -1
  31. package/react/metafile-esm.json +1 -1
  32. package/tests/blockstore/fp-envelope.test.ts-off +65 -0
  33. package/tests/blockstore/interceptor-gateway.test.ts +122 -0
  34. package/tests/blockstore/keyed-crypto-indexdb-file.test.ts +130 -0
  35. package/tests/blockstore/keyed-crypto.test.ts +75 -118
  36. package/tests/blockstore/loader.test.ts +18 -9
  37. package/tests/blockstore/store.test.ts +40 -31
  38. package/tests/blockstore/transaction.test.ts +14 -13
  39. package/tests/fireproof/all-gateway.test.ts +286 -216
  40. package/tests/fireproof/cars/bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i.ts +324 -316
  41. package/tests/fireproof/crdt.test.ts +78 -19
  42. package/tests/fireproof/fireproof.test.ts +111 -92
  43. package/tests/fireproof/hello.test.ts +21 -17
  44. package/tests/fireproof/indexer.test.ts +74 -50
  45. package/tests/fireproof/{database.test.ts → ledger.test.ts} +241 -45
  46. package/tests/fireproof/multiple-ledger.test.ts +2 -2
  47. package/tests/fireproof/utils.test.ts +47 -6
  48. package/tests/gateway/file/loader-config.test.ts +307 -0
  49. package/tests/gateway/fp-envelope-serialize.test.ts +256 -0
  50. package/tests/gateway/indexdb/loader-config.test.ts +79 -0
  51. package/tests/helpers.ts +44 -17
  52. package/tests/react/useFireproof.test.tsx +2 -2
  53. package/tests/www/gallery.html +2 -2
  54. package/tests/www/todo-aws.html +1 -1
  55. package/tests/www/todo-ipfs.html +1 -1
  56. package/tests/www/todo-local.html +1 -1
  57. package/tests/www/todo.html +25 -4
  58. package/web/index.cjs +218 -0
  59. package/web/index.cjs.map +1 -0
  60. package/web/index.d.cts +30 -0
  61. package/web/index.d.ts +30 -0
  62. package/web/index.js +195 -0
  63. package/web/index.js.map +1 -0
  64. package/web/metafile-cjs.json +1 -1
  65. package/web/metafile-esm.json +1 -1
  66. package/chunk-7EWIAXTM.js +0 -7
  67. package/chunk-7EWIAXTM.js.map +0 -1
  68. package/chunk-F4FC6B2T.js +0 -63
  69. package/chunk-F4FC6B2T.js.map +0 -1
  70. package/chunk-PZ5AY32C.js +0 -10
  71. package/chunk-PZ5AY32C.js.map +0 -1
  72. package/chunk-RXC4JGJT.js +0 -301
  73. package/chunk-RXC4JGJT.js.map +0 -1
  74. package/gateway-C62S56GY.js +0 -66
  75. package/gateway-C62S56GY.js.map +0 -1
  76. package/gateway-VVS4QWDA.js +0 -145
  77. package/gateway-VVS4QWDA.js.map +0 -1
  78. package/key-bag-file-PWZ3QE7B.js +0 -55
  79. package/key-bag-file-PWZ3QE7B.js.map +0 -1
  80. package/key-bag-indexdb-SYG3YD4D.js +0 -51
  81. package/key-bag-indexdb-SYG3YD4D.js.map +0 -1
  82. package/node/chunk-4A4RAVNS.js +0 -17
  83. package/node/chunk-4A4RAVNS.js.map +0 -1
  84. package/node/mem-filesystem.cjs +0 -72
  85. package/node/mem-filesystem.cjs.map +0 -1
  86. package/node/mem-filesystem.d.cts +0 -25
  87. package/node/mem-filesystem.d.ts +0 -25
  88. package/node/mem-filesystem.js +0 -40
  89. package/node/mem-filesystem.js.map +0 -1
  90. package/node/node-filesystem.cjs.map +0 -1
  91. package/node/node-filesystem.d.cts +0 -35
  92. package/node/node-filesystem.d.ts +0 -35
  93. package/node/node-filesystem.js.map +0 -1
  94. package/tests/fireproof/config.test.ts +0 -172
  95. package/utils-ZVVGAXFE.js +0 -13
  96. package/utils-ZVVGAXFE.js.map +0 -1
  97. package/web/gateway-impl.cjs +0 -183
  98. package/web/gateway-impl.cjs.map +0 -1
  99. package/web/gateway-impl.d.cts +0 -31
  100. package/web/gateway-impl.d.ts +0 -31
  101. package/web/gateway-impl.js +0 -162
  102. package/web/gateway-impl.js.map +0 -1
  103. /package/tests/blockstore/{fragment-gateway.test.ts → fragment-gateway.test.ts-off} +0 -0
@@ -1,41 +1,44 @@
1
- import { Database, bs, ensureSuperThis } from "@fireproof/core";
2
- import { URI } from "@adviser/cement";
1
+ import { Ledger, LedgerFactory, PARAM, bs, ensureSuperThis } from "@fireproof/core";
3
2
 
4
3
  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
- }
4
+ import { simpleCID } from "../helpers.js";
5
+
6
+ // import { DataStore, MetaStore, WALState, WALStore } from "../../src/blockstore/types.js";
7
+ // import { Gateway } from "../../src/blockstore/gateway.js";
8
+ // import { FPEnvelopeMeta, FPEnvelopeType } from "../../src/blockstore/fp-envelope.js";
9
+
10
+ // function customExpect(value: unknown, matcher: (val: unknown) => void, message: string): void {
11
+ // try {
12
+ // matcher(value);
13
+ // } catch (error) {
14
+ // void error;
15
+ // // console.error(error);
16
+ // throw new Error(message);
17
+ // }
18
+ // }
19
+
20
+ // interface ExtendedGateway extends bs.Gateway {
21
+ // readonly logger: Logger;
22
+ // readonly headerSize: number;
23
+ // readonly fidLength: number;
24
+ // }
25
+
26
+ // interface ExtendedStore {
27
+ // readonly gateway: ExtendedGateway;
28
+ // readonly _url: URI;
29
+ // readonly name: string;
30
+ // }
27
31
 
28
32
  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
-
33
+ let db: Ledger;
34
+ let carStore: bs.DataStore;
35
+ let metaStore: bs.MetaStore;
36
+ let fileStore: bs.DataStore;
37
+ let walStore: bs.WALStore;
38
+ let carGateway: bs.SerdeGateway;
39
+ let metaGateway: bs.SerdeGateway;
40
+ let fileGateway: bs.SerdeGateway;
41
+ let walGateway: bs.SerdeGateway;
39
42
  const sthis = ensureSuperThis();
40
43
 
41
44
  afterEach(async function () {
@@ -43,19 +46,21 @@ describe("noop Gateway", function () {
43
46
  await db.destroy();
44
47
  });
45
48
  beforeEach(async function () {
46
- db = new Database("test-gateway-" + Math.random().toString(36).substring(7));
49
+ db = LedgerFactory("test-gateway-" + sthis.nextId().str, {
50
+ logger: sthis.logger,
51
+ });
47
52
 
48
53
  // Extract stores from the loader
49
- carStore = (await db.blockstore.loader?.carStore()) as unknown as ExtendedStore;
50
- metaStore = (await db.blockstore.loader?.metaStore()) as unknown as ExtendedStore;
51
- fileStore = (await db.blockstore.loader?.fileStore()) as unknown as ExtendedStore;
52
- walStore = (await db.blockstore.loader?.WALStore()) as unknown as ExtendedStore;
54
+ carStore = (await db.crdt.blockstore.loader?.carStore()) as bs.DataStore;
55
+ metaStore = (await db.crdt.blockstore.loader?.metaStore()) as bs.MetaStore;
56
+ fileStore = (await db.crdt.blockstore.loader?.fileStore()) as bs.DataStore;
57
+ walStore = (await db.crdt.blockstore.loader?.WALStore()) as bs.WALStore;
53
58
 
54
59
  // Extract and log gateways
55
- carGateway = carStore?.gateway;
56
- metaGateway = metaStore?.gateway;
57
- fileGateway = fileStore?.gateway;
58
- walGateway = walStore?.gateway;
60
+ carGateway = carStore.realGateway;
61
+ metaGateway = metaStore.realGateway;
62
+ fileGateway = fileStore.realGateway;
63
+ walGateway = walStore.realGateway;
59
64
  });
60
65
 
61
66
  it("should have valid stores and gateways", async function () {
@@ -73,26 +78,27 @@ describe("noop Gateway", function () {
73
78
 
74
79
  it("should have correct store names", async function () {
75
80
  // Check that all stores have the correct name
76
- expect(carStore?.name).toContain("test-gateway");
77
- expect(metaStore?.name).toContain("test-gateway");
78
- expect(fileStore?.name).toContain("test-gateway");
79
- expect(walStore?.name).toContain("test-gateway");
81
+ expect(carStore.url().getParam(PARAM.NAME)).toContain("test-gateway");
82
+ expect(metaStore.url().getParam(PARAM.NAME)).toContain("test-gateway");
83
+ expect(fileStore.url().getParam(PARAM.NAME)).toContain("test-gateway");
84
+ expect(walStore.url().getParam(PARAM.NAME)).toContain("test-gateway");
80
85
  });
81
86
 
82
87
  it("should have correct store types in URLs", async function () {
83
88
  // Check that all stores have the correct store type in their URL
84
- expect(carStore?._url.toString()).toContain("store=data");
85
- expect(metaStore?._url.toString()).toContain("store=meta");
86
- expect(fileStore?._url.toString()).toContain("store=data");
87
- expect(walStore?._url.toString()).toContain("store=wal");
89
+ expect(carStore.url().toString()).toContain("store=data");
90
+ expect(carStore.url().toString()).toContain("suffix=.car");
91
+ expect(metaStore.url().toString()).toContain("store=meta");
92
+ expect(fileStore.url().toString()).toContain("store=data");
93
+ expect(walStore.url().toString()).toContain("store=wal");
88
94
  });
89
95
 
90
96
  it("should have version specified in URLs", async function () {
91
97
  // Verify that all stores have a version specified
92
- expect(carStore?._url.toString()).toContain("version=");
93
- expect(metaStore?._url.toString()).toContain("version=");
94
- expect(fileStore?._url.toString()).toContain("version=");
95
- expect(walStore?._url.toString()).toContain("version=");
98
+ expect(carStore.url().toString()).toContain("version=");
99
+ expect(metaStore.url().toString()).toContain("version=");
100
+ expect(fileStore.url().toString()).toContain("version=");
101
+ expect(walStore.url().toString()).toContain("version=");
96
102
  });
97
103
 
98
104
  it("should have correct gateway types", async function () {
@@ -105,202 +111,263 @@ describe("noop Gateway", function () {
105
111
 
106
112
  it("should build CAR Gateway URL", async function () {
107
113
  const testKey = "bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i";
108
- const carUrl = await carGateway?.buildUrl(carStore?._url, testKey);
109
- expect(carUrl?.Ok()).toBeTruthy();
114
+ const carUrl = await carGateway.buildUrl(sthis, carStore.url(), testKey);
115
+ expect(carUrl.Ok().hasParam("key")).toBeTruthy();
110
116
  });
111
117
 
112
118
  it("should start CAR Gateway", async function () {
113
- await carGateway?.start(carStore?._url);
119
+ const url = await carGateway.start(sthis, carStore.url());
120
+ expect(url.Ok().asObj()).toEqual(carStore.url().asObj());
114
121
  });
115
122
 
116
123
  it("should put data in CAR Gateway", async function () {
117
- const testKey = "bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i";
118
- const testData = fileContent;
119
- const carUrl = await carGateway?.buildUrl(carStore?._url, testKey);
120
- await carGateway?.start(carStore?._url);
121
- const carPutResult = await carGateway?.put(carUrl?.Ok(), testData);
122
- expect(carPutResult?.Ok()).toBeFalsy();
124
+ const carUrl = await carGateway.buildUrl(sthis, carStore.url(), fileContent.cid);
125
+ await carGateway.start(sthis, carStore.url());
126
+ const carPutResult = await carGateway.put(sthis, carUrl.Ok(), {
127
+ type: bs.FPEnvelopeType.CAR,
128
+ payload: fileContent.block,
129
+ });
130
+ expect(carPutResult.isOk()).toBeTruthy();
123
131
  });
124
132
 
125
133
  it("should get data from CAR Gateway", async function () {
126
- const testKey = "bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i";
127
- const testData = fileContent;
128
- const carUrl = await carGateway?.buildUrl(carStore?._url, testKey);
129
- await carGateway?.start(carStore?._url);
130
- await carGateway?.put(carUrl?.Ok(), testData);
131
- const carGetResult = await carGateway?.get(carUrl?.Ok());
132
- customExpect(carGetResult?.Ok(), (v) => expect(v).toEqual(testData), "carGetResult should match testData");
134
+ const carUrl = await carGateway.buildUrl(sthis, carStore.url(), fileContent.cid);
135
+ await carGateway.start(sthis, carStore.url());
136
+ await carGateway.put(sthis, carUrl.Ok(), {
137
+ type: bs.FPEnvelopeType.CAR,
138
+ payload: fileContent.block,
139
+ });
140
+ const carGetResult = await carGateway.get(sthis, carUrl.Ok());
141
+ expect(carGetResult.Ok().type).toEqual("car");
142
+ expect(carGetResult.Ok().payload).toEqual(fileContent.block);
143
+ // customExpect(carGetResult.Ok(), (v) => expect(v).toEqual(testData), "carGetResult should match testData");
133
144
  });
134
145
 
135
146
  it("should delete data from CAR Gateway", async function () {
136
- const testKey = "bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i";
137
- const testData = fileContent;
138
- const carUrl = await carGateway?.buildUrl(carStore?._url, testKey);
139
- await carGateway?.start(carStore?._url);
140
- await carGateway?.put(carUrl?.Ok(), testData);
141
- const carDeleteResult = await carGateway?.delete(carUrl?.Ok());
142
- expect(carDeleteResult?.Ok()).toBeFalsy();
147
+ const carUrl = await carGateway.buildUrl(sthis, carStore.url(), fileContent.cid);
148
+ await carGateway.start(sthis, carStore.url());
149
+ await carGateway.put(sthis, carUrl.Ok(), {
150
+ type: bs.FPEnvelopeType.CAR,
151
+ payload: fileContent.block,
152
+ });
153
+ const carDeleteResult = await carGateway.delete(sthis, carUrl.Ok());
154
+ expect(carDeleteResult.isOk()).toBeTruthy();
143
155
  });
144
156
 
145
157
  it("should close CAR Gateway", async function () {
146
- await carGateway?.close(carStore?._url);
158
+ await carGateway.close(sthis, carStore.url());
147
159
  });
148
160
  it("should build Meta Gateway URL", async function () {
149
- const metaUrl = await metaGateway?.buildUrl(metaStore?._url, "main");
150
- expect(metaUrl?.Ok()).toBeTruthy();
161
+ const metaUrl = await metaGateway.buildUrl(sthis, metaStore.url(), "main");
162
+ expect(metaUrl.Ok()).toBeTruthy();
151
163
  });
152
164
 
153
165
  it("should start Meta Gateway", async function () {
154
- await metaGateway?.start(metaStore?._url);
166
+ await metaGateway.start(sthis, metaStore.url());
155
167
  });
156
168
 
157
169
  it("should close Meta Gateway", async function () {
158
- await metaGateway?.start(metaStore?._url);
159
- await metaGateway?.close(metaStore?._url);
170
+ await metaGateway.start(sthis, metaStore.url());
171
+ await metaGateway.close(sthis, metaStore.url());
160
172
  });
161
173
 
162
174
  it("should build File Gateway URL", async function () {
163
- const testKey = "bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i";
164
- const fileUrl = await fileGateway?.buildUrl(fileStore?._url, testKey);
165
- expect(fileUrl?.Ok()).toBeTruthy();
175
+ const fileUrl = await fileGateway.buildUrl(sthis, fileStore.url(), fileContent.cid);
176
+ expect(fileUrl.Ok()).toBeTruthy();
166
177
  });
167
178
 
168
179
  it("should start File Gateway", async function () {
169
- await fileGateway?.start(fileStore?._url);
180
+ await fileGateway.start(sthis, fileStore.url());
170
181
  });
171
182
 
172
183
  it("should put data to File Gateway", async function () {
173
- const testKey = "bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i";
174
- const testData = fileContent;
175
- const fileUrl = await fileGateway?.buildUrl(fileStore?._url, testKey);
176
- await fileGateway?.start(fileStore?._url);
177
- const filePutResult = await fileGateway?.put(fileUrl?.Ok(), testData);
178
- expect(filePutResult?.Ok()).toBeFalsy();
184
+ const fileUrl = await fileGateway.buildUrl(sthis, fileStore.url(), fileContent.cid);
185
+ await fileGateway.start(sthis, fileStore.url());
186
+ const filePutResult = await fileGateway.put(sthis, fileUrl.Ok(), {
187
+ type: bs.FPEnvelopeType.FILE,
188
+ payload: fileContent.block,
189
+ });
190
+ expect(filePutResult.Ok()).toBeFalsy();
179
191
  });
180
192
 
181
193
  it("should get data from File Gateway", async function () {
182
- const testKey = "bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i";
183
- const testData = fileContent;
184
- const fileUrl = await fileGateway?.buildUrl(fileStore?._url, testKey);
185
- await fileGateway?.start(fileStore?._url);
186
- await fileGateway?.put(fileUrl?.Ok(), testData);
187
- const fileGetResult = await fileGateway?.get(fileUrl?.Ok());
188
- customExpect(fileGetResult?.Ok(), (v) => expect(v).toEqual(testData), "fileGetResult should match testData");
194
+ const fileUrl = await fileGateway.buildUrl(sthis, fileStore.url(), fileContent.cid);
195
+ await fileGateway.start(sthis, fileStore.url());
196
+ await fileGateway.put(sthis, fileUrl.Ok(), {
197
+ type: bs.FPEnvelopeType.FILE,
198
+ payload: fileContent.block,
199
+ });
200
+ const fileGetResult = await fileGateway.get(sthis, fileUrl.Ok());
201
+ expect(fileGetResult.Ok().type).toEqual("file");
202
+ expect(fileGetResult.Ok().payload).toEqual(fileContent.block);
189
203
  });
190
204
 
191
205
  it("should delete data from File Gateway", async function () {
192
- const testKey = "bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i";
193
- const testData = fileContent;
194
- const fileUrl = await fileGateway?.buildUrl(fileStore?._url, testKey);
195
- await fileGateway?.start(fileStore?._url);
196
- await fileGateway?.put(fileUrl?.Ok(), testData);
197
- const fileDeleteResult = await fileGateway?.delete(fileUrl?.Ok());
198
- expect(fileDeleteResult?.Ok()).toBeFalsy();
206
+ const fileUrl = await fileGateway.buildUrl(sthis, fileStore.url(), fileContent.cid);
207
+ await fileGateway.start(sthis, fileStore.url());
208
+ await fileGateway.put(sthis, fileUrl.Ok(), {
209
+ type: bs.FPEnvelopeType.FILE,
210
+ payload: fileContent.block,
211
+ });
212
+ const fileDeleteResult = await fileGateway.delete(sthis, fileUrl.Ok());
213
+ expect(fileDeleteResult.isOk()).toBeTruthy();
199
214
  });
200
215
 
201
216
  it("should close File Gateway", async function () {
202
- await fileGateway?.close(fileStore?._url);
217
+ await fileGateway.close(sthis, fileStore.url());
203
218
  });
204
219
  it("should build WAL Gateway URL", async function () {
205
220
  const testKey = "bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i";
206
- const walUrl = await walGateway?.buildUrl(walStore?._url, testKey);
207
- expect(walUrl?.Ok()).toBeTruthy();
221
+ const walUrl = await walGateway.buildUrl(sthis, walStore.url(), testKey);
222
+ expect(walUrl.Ok()).toBeTruthy();
208
223
  });
209
224
 
210
225
  it("should start WAL Gateway", async function () {
211
- await walGateway?.start(walStore?._url);
226
+ await walGateway.start(sthis, walStore.url());
212
227
  });
213
228
 
214
229
  it("should put data to WAL Gateway", async function () {
215
230
  const testKey = "bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i";
216
- const walUrl = await walGateway?.buildUrl(walStore?._url, testKey);
217
- await walGateway?.start(walStore?._url);
218
- const walTestDataString = JSON.stringify({
219
- operations: [],
220
- noLoaderOps: [],
221
- fileOperations: [],
231
+ const walUrl = await walGateway.buildUrl(sthis, walStore.url(), testKey);
232
+ await walGateway.start(sthis, walStore.url());
233
+ // const walTestDataString = JSON.stringify();
234
+ // const walTestData = sthis.txt.encode(walTestDataString);
235
+ const walPutResult = await walGateway.put(sthis, walUrl.Ok(), {
236
+ type: bs.FPEnvelopeType.WAL,
237
+ payload: {
238
+ operations: [],
239
+ noLoaderOps: [],
240
+ fileOperations: [],
241
+ },
222
242
  });
223
- const walTestData = sthis.txt.encode(walTestDataString);
224
- const walPutResult = await walGateway?.put(walUrl?.Ok(), walTestData);
225
- expect(walPutResult?.Ok()).toBeFalsy();
243
+ expect(walPutResult.Ok()).toBeFalsy();
226
244
  });
227
245
 
228
246
  it("should get data from WAL Gateway", async function () {
229
247
  const testKey = "bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i";
230
- const walUrl = await walGateway?.buildUrl(walStore?._url, testKey);
231
- await walGateway?.start(walStore?._url);
232
- const walTestDataString = JSON.stringify({
233
- operations: [],
234
- noLoaderOps: [],
235
- fileOperations: [],
248
+ const walUrl = await walGateway.buildUrl(sthis, walStore.url(), testKey);
249
+ await walGateway.start(sthis, walStore.url());
250
+ const ref: bs.WALState = {
251
+ operations: [
252
+ {
253
+ cars: [await simpleCID(sthis)],
254
+ },
255
+ ],
256
+ noLoaderOps: [
257
+ {
258
+ cars: [await simpleCID(sthis)],
259
+ },
260
+ ],
261
+ fileOperations: [
262
+ {
263
+ cid: await simpleCID(sthis),
264
+ public: false,
265
+ },
266
+ ],
267
+ };
268
+ // const walTestDataString = JSON.stringify({
269
+ // operations: [],
270
+ // noLoaderOps: [],
271
+ // fileOperations: [],
272
+ // });
273
+ // const walTestData = sthis.txt.encode(walTestDataString);
274
+ await walGateway.put(sthis, walUrl.Ok(), {
275
+ type: bs.FPEnvelopeType.WAL,
276
+ payload: ref,
236
277
  });
237
- const walTestData = sthis.txt.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 = sthis.txt.decode(okResult);
242
- expect(decodedResult).toEqual(walTestDataString);
278
+ const walGetResult = await walGateway.get(sthis, walUrl.Ok());
279
+ expect(walGetResult.isOk()).toBeTruthy();
280
+ // const okResult = walGetResult.Ok();
281
+ // const decodedResult = sthis.txt.decode(okResult);
282
+ expect(ref).toEqual(walGetResult.Ok().payload);
243
283
  });
244
284
 
245
285
  it("should delete data from WAL Gateway", async function () {
246
286
  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: [],
287
+ const walUrl = await walGateway.buildUrl(sthis, walStore.url(), testKey);
288
+ await walGateway.start(sthis, walStore.url());
289
+ const ref: bs.WALState = {
290
+ operations: [
291
+ {
292
+ cars: [await simpleCID(sthis)],
293
+ },
294
+ ],
295
+ noLoaderOps: [
296
+ {
297
+ cars: [await simpleCID(sthis)],
298
+ },
299
+ ],
300
+ fileOperations: [
301
+ {
302
+ cid: await simpleCID(sthis),
303
+ public: false,
304
+ },
305
+ ],
306
+ };
307
+ await walGateway.put(sthis, walUrl.Ok(), {
308
+ type: bs.FPEnvelopeType.WAL,
309
+ payload: ref,
253
310
  });
254
- const walTestData = sthis.txt.encode(walTestDataString);
255
- await walGateway?.put(walUrl?.Ok(), walTestData);
256
- const walDeleteResult = await walGateway?.delete(walUrl?.Ok());
257
- expect(walDeleteResult?.Ok()).toBeFalsy();
311
+ const walDeleteResult = await walGateway.delete(sthis, walUrl.Ok());
312
+ expect(walDeleteResult.isOk()).toBeTruthy();
258
313
  });
259
314
 
260
315
  it("should close WAL Gateway", async function () {
261
- await walGateway?.start(walStore?._url);
262
- await walGateway?.close(walStore?._url);
263
- });
264
-
265
- it("should have correct CAR Gateway properties", async function () {
266
- // CAR Gateway assertions
267
- expect(carGateway?.fidLength).toBe(4);
268
- expect(carGateway?.headerSize).toBe(36);
269
- expect(carGateway?.logger._attributes).toHaveProperty("module");
270
- expect(carGateway?.logger._attributes).toHaveProperty("url");
271
- });
272
-
273
- it("should have correct Meta Gateway properties", async function () {
274
- // Meta Gateway assertions
275
- expect(metaGateway?.fidLength).toBe(4);
276
- expect(metaGateway?.headerSize).toBe(36);
277
- expect(metaGateway?.logger._attributes).toHaveProperty("module");
278
- expect(metaGateway?.logger._attributes).not.toHaveProperty("url");
279
- });
280
-
281
- it("should have correct File Gateway properties", async function () {
282
- // File Gateway assertions
283
- expect(fileGateway?.fidLength).toBe(4);
284
- expect(fileGateway?.headerSize).toBe(36);
285
- expect(fileGateway?.logger._attributes).toHaveProperty("module");
286
- expect(fileGateway?.logger._attributes).toHaveProperty("url");
287
- });
288
-
289
- it("should have correct WAL Gateway properties", async function () {
290
- // WAL Gateway assertions
291
- expect(walGateway?.fidLength).toBe(4);
292
- expect(walGateway?.headerSize).toBe(36);
293
- expect(walGateway?.logger._attributes).toHaveProperty("module");
294
- expect(walGateway?.logger._attributes).not.toHaveProperty("url");
295
- });
316
+ await walGateway.start(sthis, walStore.url());
317
+ await walGateway.close(sthis, walStore.url());
318
+ });
319
+
320
+ // it("should have correct CAR Gateway properties", async function () {
321
+ // // CAR Gateway assertions
322
+ // expect(carGateway.fidLength).toBe(4);
323
+ // expect(carGateway.headerSize).toBe(36);
324
+ // carGateway.logger.Error().Msg("CAR Gateway properties");
325
+ // await sthis.logger.Flush();
326
+ // const last = sthis.ctx.logCollector.Logs().slice(-1)[0];
327
+ // expect(last).toHaveProperty("module");
328
+ // expect(carStore.).toHaveProperty("url");
329
+ // });
330
+
331
+ // it("should have correct Meta Gateway properties", async function () {
332
+ // // Meta Gateway assertions
333
+ // expect(metaGateway.fidLength).toBe(4);
334
+ // expect(metaGateway.headerSize).toBe(36);
335
+ // metaGateway.logger.Error().Msg("CAR Gateway properties");
336
+ // await sthis.logger.Flush();
337
+ // const last = sthis.ctx.logCollector.Logs().slice(-1)[0];
338
+ // expect(last).toHaveProperty("module");
339
+ // expect(last).not.toHaveProperty("url");
340
+ // });
341
+
342
+ // it("should have correct File Gateway properties", async function () {
343
+ // // File Gateway assertions
344
+ // expect(fileGateway.fidLength).toBe(4);
345
+ // expect(fileGateway.headerSize).toBe(36);
346
+ // fileGateway.logger.Error().Msg("CAR Gateway properties");
347
+ // await sthis.logger.Flush();
348
+ // const last = sthis.ctx.logCollector.Logs().slice(-1)[0];
349
+ // expect(last).toHaveProperty("module");
350
+ // expect(last).toHaveProperty("url");
351
+ // });
352
+
353
+ // it("should have correct WAL Gateway properties", async function () {
354
+ // // WAL Gateway assertions
355
+ // expect(walGateway.fidLength).toBe(4);
356
+ // expect(walGateway.headerSize).toBe(36);
357
+ // walGateway.logger.Error().Msg("CAR Gateway properties");
358
+ // await sthis.logger.Flush();
359
+ // const last = sthis.ctx.logCollector.Logs().slice(-1)[0];
360
+ // expect(last).toHaveProperty("module");
361
+ // expect(last).not.toHaveProperty("url");
362
+ // });
296
363
  });
297
364
 
298
365
  describe("noop Gateway subscribe", function () {
299
- let db: Database;
366
+ let db: Ledger;
300
367
 
301
- let metaStore: ExtendedStore;
368
+ let metaStore: bs.MetaStore;
302
369
 
303
- let metaGateway: ExtendedGateway;
370
+ let metaGateway: bs.SerdeGateway;
304
371
  const sthis = ensureSuperThis();
305
372
 
306
373
  afterEach(async function () {
@@ -308,30 +375,30 @@ describe("noop Gateway subscribe", function () {
308
375
  await db.destroy();
309
376
  });
310
377
  beforeEach(async function () {
311
- db = new Database("test-gateway-" + Math.random().toString(36).substring(7));
378
+ db = LedgerFactory("test-gateway-" + sthis.nextId().str);
312
379
 
313
380
  // Extract stores from the loader
314
- metaStore = (await db.blockstore.loader?.metaStore()) as unknown as ExtendedStore;
381
+ metaStore = (await db.crdt.blockstore.loader?.metaStore()) as bs.MetaStore;
315
382
 
316
- metaGateway = metaStore?.gateway;
383
+ metaGateway = metaStore.realGateway;
317
384
  });
318
385
  it("should subscribe to meta Gateway", async function () {
319
- const metaUrl = await metaGateway?.buildUrl(metaStore?._url, "main");
320
- await metaGateway?.start(metaStore?._url);
386
+ const metaUrl = await metaGateway.buildUrl(sthis, metaStore.url(), "main");
387
+ await metaGateway.start(sthis, metaStore.url());
321
388
 
322
389
  let resolve: () => void;
323
390
  let didCall = false;
324
391
  const p = new Promise<void>((r) => {
325
392
  resolve = r;
326
393
  });
327
- const metaSubscribeResult = await metaGateway?.subscribe?.(metaUrl?.Ok(), async (data: Uint8Array) => {
328
- const decodedData = sthis.txt.decode(data);
329
- expect(decodedData).toContain("[]");
330
- didCall = true;
331
- resolve();
332
- });
333
- if (!metaSubscribeResult?.isErr()) {
334
- expect(metaSubscribeResult?.Ok()).toBeTruthy();
394
+ if (metaGateway.subscribe) {
395
+ const metaSubscribeResult = (await metaGateway.subscribe(sthis, metaUrl.Ok(), async (data: bs.FPEnvelopeMeta) => {
396
+ // const decodedData = sthis.txt.decode(data);
397
+ expect(data.payload).toContain("[]");
398
+ didCall = true;
399
+ resolve();
400
+ })) as bs.UnsubscribeResult;
401
+ expect(metaSubscribeResult.isOk()).toBeTruthy();
335
402
  const ok = await db.put({ _id: "key1", hello: "world1" });
336
403
  expect(ok).toBeTruthy();
337
404
  expect(ok.id).toBe("key1");
@@ -342,13 +409,13 @@ describe("noop Gateway subscribe", function () {
342
409
  });
343
410
 
344
411
  describe("Gateway", function () {
345
- let db: Database;
412
+ let db: Ledger;
346
413
  // let carStore: ExtendedStore;
347
- let metaStore: ExtendedStore;
414
+ let metaStore: bs.MetaStore;
348
415
  // let fileStore: ExtendedStore;
349
416
  // let walStore: ExtendedStore;
350
417
  // let carGateway: ExtendedGateway;
351
- let metaGateway: ExtendedGateway;
418
+ let metaGateway: bs.SerdeGateway;
352
419
  // let fileGateway: ExtendedGateway;
353
420
  // let walGateway: ExtendedGateway;
354
421
  const sthis = ensureSuperThis();
@@ -358,38 +425,41 @@ describe("Gateway", function () {
358
425
  await db.destroy();
359
426
  });
360
427
  beforeEach(async function () {
361
- db = new Database("test-gateway-" + Math.random().toString(36).substring(7));
428
+ db = LedgerFactory("test-gateway-" + sthis.nextId().str);
362
429
  const ok = await db.put({ _id: "test", foo: "bar" });
363
430
  expect(ok).toBeTruthy();
364
431
  expect(ok.id).toBe("test");
365
432
 
366
433
  // Extract stores from the loader
367
- // carStore = (await db.blockstore.loader?.carStore()) as unknown as ExtendedStore;
368
- metaStore = (await db.blockstore.loader?.metaStore()) as unknown as ExtendedStore;
369
- // fileStore = (await db.blockstore.loader?.fileStore()) as unknown as ExtendedStore;
370
- // walStore = (await db.blockstore.loader?.WALStore()) as unknown as ExtendedStore;
434
+ // carStore = (await db.blockstore.loader.carStore()) as unknown as ExtendedStore;
435
+ metaStore = (await db.crdt.blockstore.loader?.metaStore()) as bs.MetaStore;
436
+ // fileStore = (await db.blockstore.loader.fileStore()) as unknown as ExtendedStore;
437
+ // walStore = (await db.blockstore.loader.WALStore()) as unknown as ExtendedStore;
371
438
 
372
439
  // Extract and log gateways
373
- // carGateway = carStore?.gateway;
374
- metaGateway = metaStore?.gateway;
375
- // fileGateway = fileStore?.gateway;
376
- // walGateway = walStore?.gateway;
440
+ // carGateway = carStore.gateway;
441
+ metaGateway = metaStore.realGateway;
442
+ // fileGateway = fileStore.gateway;
443
+ // walGateway = walStore.gateway;
377
444
  });
378
445
 
379
446
  it("should get data from Meta Gateway", async function () {
380
- const metaUrl = await metaGateway?.buildUrl(metaStore?._url, "main");
381
- await metaGateway?.start(metaStore?._url);
382
- const metaGetResult = await metaGateway?.get(metaUrl?.Ok());
383
- const metaGetResultOk = metaGetResult?.Ok();
384
- const decodedMetaGetResultOk = sthis.txt.decode(metaGetResultOk);
385
- expect(decodedMetaGetResultOk).toContain("parents");
447
+ const metaUrl = await metaGateway.buildUrl(sthis, metaStore.url(), "main");
448
+ await metaGateway.start(sthis, metaStore.url());
449
+ const metaGetResult = await metaGateway.get(sthis, metaUrl.Ok());
450
+ expect(metaGetResult.isOk()).toBeTruthy();
451
+ const meta = metaGetResult.Ok().payload as bs.DbMetaEvent[];
452
+ // const metaGetResultOk = metaGetResult.Ok();
453
+ // const decodedMetaGetResultOk = sthis.txt.decode(metaGetResultOk);
454
+ expect(meta.length).toBe(1);
455
+ expect(Object.keys(meta[0])).toEqual(["eventCid", "parents", "dbMeta"]);
386
456
  });
387
457
 
388
458
  it("should delete data from Meta Gateway", async function () {
389
- const metaUrl = await metaGateway?.buildUrl(metaStore?._url, "main");
390
- await metaGateway?.start(metaStore?._url);
459
+ const metaUrl = await metaGateway.buildUrl(sthis, metaStore.url(), "main");
460
+ await metaGateway.start(sthis, metaStore.url());
391
461
  // should we be testing .destroy() instead?
392
- const metaDeleteResult = await metaGateway?.delete(metaUrl?.Ok());
393
- expect(metaDeleteResult?.Ok()).toBeFalsy();
462
+ const metaDeleteResult = await metaGateway.delete(sthis, metaUrl.Ok());
463
+ expect(metaDeleteResult.isOk()).toBeTruthy();
394
464
  });
395
465
  });