@fireproof/core-test 0.23.0 → 0.23.1-dev-issue-1057

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 (91) hide show
  1. package/blockstore/interceptor-gateway.test.js.map +1 -1
  2. package/blockstore/keyed-crypto-indexeddb-file.test.js.map +1 -1
  3. package/blockstore/keyed-crypto.test.js.map +1 -1
  4. package/blockstore/loader.test.js.map +1 -1
  5. package/blockstore/standalone.test.js.map +1 -1
  6. package/blockstore/store.test.js.map +1 -1
  7. package/blockstore/transaction.test.js.map +1 -1
  8. package/fireproof/all-gateway.test.js.map +1 -1
  9. package/fireproof/attachable.test.js.map +1 -1
  10. package/fireproof/cars/bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i.js.map +1 -1
  11. package/fireproof/charwise-boolean.test.js.map +1 -1
  12. package/fireproof/compact-strategy.test.js.map +1 -1
  13. package/fireproof/concurrent.test.js.map +1 -1
  14. package/fireproof/crdt.test.js.map +1 -1
  15. package/fireproof/database.test.js.map +1 -1
  16. package/fireproof/deleted-docs-handling.test.js.map +1 -1
  17. package/fireproof/fireproof.test.fixture.js.map +1 -1
  18. package/fireproof/fireproof.test.js.map +1 -1
  19. package/fireproof/hello.test.js.map +1 -1
  20. package/fireproof/indexer.test.js.map +1 -1
  21. package/fireproof/multiple-ledger.test.js.map +1 -1
  22. package/fireproof/query-docs.test.js.map +1 -1
  23. package/fireproof/query-limit-issue.test.js.map +1 -1
  24. package/fireproof/query-property-inconsistency.test.js.map +1 -1
  25. package/fireproof/query-result-properties.test.js.map +1 -1
  26. package/fireproof/stable-cid.test.js.map +1 -1
  27. package/fireproof/utils.test.js.map +1 -1
  28. package/gateway/file/loader-config.test.js.map +1 -1
  29. package/gateway/indexeddb/create-db-on-write.test.js.map +1 -1
  30. package/gateway/indexeddb/loader-config.test.js.map +1 -1
  31. package/global-setup.js.map +1 -1
  32. package/helpers.js.map +1 -1
  33. package/package.json +19 -19
  34. package/protocols/cloud/msger.test.js.map +1 -1
  35. package/runtime/fp-envelope-serialize.test.js.map +1 -1
  36. package/runtime/key-bag.test.js.map +1 -1
  37. package/runtime/meta-key-hack.test.js.map +1 -1
  38. package/setup.file.js.map +1 -1
  39. package/setup.indexeddb.js.map +1 -1
  40. package/setup.memory.js.map +1 -1
  41. package/vitest.config.js.map +1 -1
  42. package/vitest.file.config.js.map +1 -1
  43. package/vitest.indexeddb.config.js.map +1 -1
  44. package/vitest.memory.config.js.map +1 -1
  45. package/blockstore/fp-envelope.test.ts-off +0 -65
  46. package/blockstore/fragment-gateway.test.ts-off +0 -106
  47. package/blockstore/interceptor-gateway.test.ts +0 -259
  48. package/blockstore/keyed-crypto-indexeddb-file.test.ts +0 -134
  49. package/blockstore/keyed-crypto.test.ts +0 -381
  50. package/blockstore/loader.test.ts +0 -313
  51. package/blockstore/standalone.test.ts +0 -156
  52. package/blockstore/store.test.ts +0 -199
  53. package/blockstore/transaction.test.ts +0 -132
  54. package/fireproof/all-gateway.test.ts +0 -477
  55. package/fireproof/attachable.test.ts +0 -677
  56. package/fireproof/cars/bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i.car +0 -0
  57. package/fireproof/cars/bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i.ts +0 -324
  58. package/fireproof/charwise-boolean.test.ts +0 -68
  59. package/fireproof/compact-strategy.test.ts +0 -40
  60. package/fireproof/concurrent.test.ts +0 -37
  61. package/fireproof/crdt.test.ts +0 -572
  62. package/fireproof/database.test.ts +0 -772
  63. package/fireproof/deleted-docs-handling.test.ts +0 -112
  64. package/fireproof/fireproof.test.fixture.ts +0 -133
  65. package/fireproof/fireproof.test.ts +0 -767
  66. package/fireproof/hello.test.ts +0 -75
  67. package/fireproof/indexer.test.ts +0 -459
  68. package/fireproof/multiple-ledger.test.ts +0 -67
  69. package/fireproof/query-docs.test.ts +0 -117
  70. package/fireproof/query-limit-issue.test.ts +0 -147
  71. package/fireproof/query-property-inconsistency.test.ts +0 -90
  72. package/fireproof/query-result-properties.test.ts +0 -43
  73. package/fireproof/stable-cid.test.ts +0 -72
  74. package/fireproof/utils.test.ts +0 -137
  75. package/gateway/file/loader-config.test.ts +0 -309
  76. package/gateway/indexeddb/create-db-on-write.test.ts +0 -202
  77. package/gateway/indexeddb/loader-config.test.ts +0 -80
  78. package/global-setup.ts +0 -11
  79. package/helpers.ts +0 -177
  80. package/protocols/cloud/msger.test.ts +0 -559
  81. package/runtime/fp-envelope-serialize.test.ts +0 -266
  82. package/runtime/key-bag.test.ts +0 -243
  83. package/runtime/meta-key-hack.test.ts +0 -103
  84. package/setup.file.ts +0 -1
  85. package/setup.indexeddb.ts +0 -0
  86. package/setup.memory.ts +0 -2
  87. package/tsconfig.json +0 -18
  88. package/vitest.config.ts +0 -8
  89. package/vitest.file.config.ts +0 -11
  90. package/vitest.indexeddb.config.ts +0 -34
  91. package/vitest.memory.config.ts +0 -24
@@ -1,381 +0,0 @@
1
- import { BuildURI, LogCollector, runtimeFn, toCryptoRuntime, URI } from "@adviser/cement";
2
- import { base58btc } from "multiformats/bases/base58";
3
- // import { sha256 as hasher } from "multiformats/hashes/sha2";
4
- // import * as dagCodec from "@ipld/dag-cbor";
5
- import * as cborg from "cborg";
6
- import type { KeyBagProviderIndexedDB } from "@fireproof/core-gateways-indexeddb";
7
- import { mockLoader, MockSuperThis, mockSuperThis } from "../helpers.js";
8
- import { ensureSuperThis, keyedCryptoFactory, storeType2DataMetaWal } from "@fireproof/core-runtime";
9
- import { V2KeysItem, PARAM, StoreType } from "@fireproof/core-types-base";
10
- import { describe, beforeEach, it, expect } from "vitest";
11
- import { coerceMaterial, getKeyBag, KeyBag, toKeyWithFingerPrint } from "@fireproof/core-keybag";
12
- import { KeyBagProviderFile } from "@fireproof/core-gateways-file";
13
- import { CryptoAction, IvKeyIdData, KeyWithFingerPrint, Loadable } from "@fireproof/core-types-blockstore";
14
- import { createAttachedStores, getDefaultURI } from "@fireproof/core-blockstore";
15
-
16
- describe("KeyBag", () => {
17
- let url: URI;
18
- let sthis: MockSuperThis;
19
-
20
- beforeEach(async () => {
21
- sthis = mockSuperThis();
22
- await sthis.start();
23
- if (runtimeFn().isBrowser) {
24
- url = URI.from("indexeddb://fp-keybag");
25
- } else {
26
- url = URI.merge(`file://./dist/tests/key.bag`, sthis.env.get("FP_KEYBAG_URL"));
27
- }
28
- });
29
- it("default-path", async () => {
30
- const old = sthis.env.get("FP_KEYBAG_URL");
31
- sthis.env.delete("FP_KEYBAG_URL");
32
- const kb = await getKeyBag(sthis);
33
- if (runtimeFn().isBrowser) {
34
- expect(kb.rt.url.toString()).toBe(`indexeddb://fp-keybag`);
35
- } else {
36
- expect(kb.rt.url.toString()).toBe(`file://${sthis.env.get("HOME")}/.fireproof/keybag`);
37
- }
38
- sthis.env.set("FP_KEYBAG_URL", old);
39
- });
40
- it("from env", async () => {
41
- const old = sthis.env.get("FP_KEYBAG_URL");
42
- sthis.env.set("FP_KEYBAG_URL", url.toString());
43
- const kb = await getKeyBag(sthis);
44
- expect(kb.rt.url.toString()).toBe(url.toString());
45
- sthis.env.set("FP_KEYBAG_URL", old);
46
- });
47
-
48
- it("extract keyMaterial", async () => {
49
- const dkb = await getKeyBag(sthis);
50
- const old = sthis.env.get("FP_KEYBAG_URL");
51
- sthis.env.set("FP_KEYBAG_URL", BuildURI.from(dkb.rt.url).setParam("extractKey", "_deprecated_internal_api").toString());
52
- const kb = await getKeyBag(sthis);
53
- const key = kb.rt.crypto.randomBytes(kb.rt.keyLength);
54
- const keyStr = base58btc.encode(key);
55
- const keyName = "extract.test" + Math.random();
56
- const res = await kb.getNamedKey(keyName, false, keyStr);
57
- expect(res.isOk()).toBeTruthy();
58
- const gkb = await kb.getNamedKey(keyName, true);
59
- expect(gkb.isOk()).toBeTruthy();
60
-
61
- expect(
62
- await gkb
63
- .Ok()
64
- .get()
65
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
66
- .then((i) => i!.extract()),
67
- ).toEqual({
68
- key,
69
- keyStr,
70
- });
71
- sthis.env.set("FP_KEYBAG_URL", old);
72
- await sthis.logger.Flush();
73
- expect(sthis.ctx.get<LogCollector>("logCollector")?.Logs()).toEqual([
74
- {
75
- level: "warn",
76
- module: "KeyBag",
77
- msg: "extractKey is enabled via _deprecated_internal_api --- handle keys safely!!!",
78
- },
79
- ]);
80
- });
81
-
82
- it("simple add", async () => {
83
- const kb = await getKeyBag(sthis, {
84
- url: url.toString(),
85
- });
86
- const name = "setkey" + Math.random();
87
- expect((await kb.getNamedKey(name, true)).isErr()).toBeTruthy();
88
-
89
- const key = base58btc.encode(kb.rt.crypto.randomBytes(kb.rt.keyLength));
90
- const res = await kb.getNamedKey(name, false, key);
91
- expect(res.isOk()).toBeTruthy();
92
- expect((await kb.getNamedKey(name, true)).Ok()).toEqual(res.Ok());
93
-
94
- const name2 = "implicit" + Math.random();
95
- const created = await kb.getNamedKey(name2);
96
- expect(created.isOk()).toBeTruthy();
97
-
98
- expect((await kb.getNamedKey(name2)).Ok()).toEqual(created.Ok());
99
-
100
- let diskBag: V2KeysItem;
101
- let diskBag2: V2KeysItem;
102
- const provider = await kb.rt.getBagProvider();
103
- if (runtimeFn().isBrowser) {
104
- const p = provider as KeyBagProviderIndexedDB;
105
- diskBag = await p._prepare().then((db) => db.get("bag", name));
106
- diskBag2 = await p._prepare().then((db) => db.get("bag", name2));
107
- } else {
108
- const p = provider as KeyBagProviderFile;
109
- if (typeof p._prepare !== "function") {
110
- return;
111
- }
112
- const { sysFS } = await p._prepare(name);
113
-
114
- diskBag = await sysFS.readfile((await p._prepare(name)).fName).then((data) => {
115
- return JSON.parse(sthis.txt.decode(data)) as V2KeysItem;
116
- });
117
- diskBag2 = await sysFS.readfile((await p._prepare(name2)).fName).then((data) => {
118
- return JSON.parse(sthis.txt.decode(data)) as V2KeysItem;
119
- });
120
- }
121
- expect((await toKeyWithFingerPrint(kb, coerceMaterial(kb, Object.values(diskBag.keys)[0].key), true)).Ok().fingerPrint).toEqual(
122
- (await res.Ok().get())?.fingerPrint,
123
- );
124
- expect(
125
- (await toKeyWithFingerPrint(kb, coerceMaterial(kb, Object.values(diskBag2.keys)[0].key), true)).Ok().fingerPrint,
126
- ).toEqual((await created.Ok().get())?.fingerPrint);
127
- const algo = {
128
- name: "AES-GCM",
129
- iv: kb.rt.crypto.randomBytes(12),
130
- tagLength: 128,
131
- };
132
- const data = kb.rt.crypto.randomBytes(122);
133
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
134
- expect(await kb.rt.crypto.encrypt(algo, (await res.Ok().get())!.key, data))
135
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
136
- .toEqual(await kb.rt.crypto.encrypt(algo, (await created.Ok().get())!.key, data));
137
- const kf = (await created.Ok().get()) as KeyWithFingerPrint;
138
- expect(await kb.rt.crypto.encrypt(algo, await kb.subtleKey(Object.values(diskBag.keys)[0].key), data)).toEqual(
139
- await kb.rt.crypto.encrypt(algo, kf.key, data),
140
- );
141
- expect(await kb.rt.crypto.encrypt(algo, await kb.subtleKey(Object.values(diskBag2.keys)[0].key), data)).toEqual(
142
- await kb.rt.crypto.encrypt(algo, kf.key, data),
143
- );
144
- });
145
-
146
- it("default key", async () => {
147
- const kb = await getKeyBag(sthis, {
148
- url: url.build().setParam("extractKey", "_deprecated_internal_api"),
149
- });
150
- const name = "default-key" + Math.random();
151
- const rMyKey = await kb.getNamedKey(name);
152
-
153
- for (let i = 0; i < 10; ++i) {
154
- expect(await kb.getNamedKey(name).then((i) => i.Ok().id)).toEqual(rMyKey.Ok().id);
155
- }
156
- expect(Object.keys((await kb.getNamedKey(name).then((i) => i.Ok().asV2KeysItem())).keys).length).toBe(1);
157
-
158
- const myKey = (await rMyKey.Ok().get()) as KeyWithFingerPrint;
159
- expect(myKey.fingerPrint).toMatch(/^z/);
160
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
161
- await rMyKey.Ok().upsert((await myKey.extract())!.key);
162
- const myKey1 = (await rMyKey.Ok().get()) as KeyWithFingerPrint;
163
- expect(myKey.fingerPrint).toEqual(myKey1.fingerPrint);
164
-
165
- expect(Object.keys((await kb.getNamedKey(name).then((i) => i.Ok().asV2KeysItem())).keys).length).toBe(1);
166
-
167
- const rMyKey1 = await kb.getNamedKey(name);
168
- expect(rMyKey1.Ok()).toEqual(rMyKey.Ok());
169
- const res1 = await rMyKey1.Ok().upsert(kb.rt.crypto.randomBytes(kb.rt.keyLength));
170
- expect(res1.isOk()).toBeTruthy();
171
-
172
- const myKey2 = (await rMyKey1.Ok().get()) as KeyWithFingerPrint;
173
- expect(myKey.fingerPrint).toEqual(myKey2.fingerPrint);
174
- expect(Object.keys((await kb.getNamedKey(name).then((i) => i.Ok().asV2KeysItem())).keys).length).toBe(2);
175
-
176
- const res = await rMyKey1.Ok().upsert(kb.rt.crypto.randomBytes(kb.rt.keyLength), true);
177
- expect(res.isOk()).toBeTruthy();
178
- const myKey3 = (await rMyKey.Ok().get()) as KeyWithFingerPrint;
179
- expect(Object.keys((await kb.getNamedKey(name).then((i) => i.Ok().asV2KeysItem())).keys).length).toBe(3);
180
-
181
- expect(myKey.fingerPrint).not.toEqual(myKey3.fingerPrint);
182
- });
183
-
184
- it("default and multiple fingerprints", async () => {
185
- const kb = await getKeyBag(sthis, {
186
- url: url.toString(),
187
- crypto: toCryptoRuntime({
188
- randomBytes: (size) => new Uint8Array(size).map((_, i) => i),
189
- }),
190
- });
191
- const key = base58btc.encode(kb.rt.crypto.randomBytes(kb.rt.keyLength));
192
- const name = "default-key" + Math.random();
193
- const fpr = (await toKeyWithFingerPrint(kb, coerceMaterial(kb, key), true)).Ok().fingerPrint;
194
- const rMyKey = await kb.getNamedKey(name, false, key);
195
- expect(rMyKey.isOk()).toBeTruthy();
196
- const myKey = rMyKey.Ok();
197
-
198
- const rUpsert1 = await myKey.upsert(key, true);
199
- expect(rUpsert1.Ok().modified).toBeFalsy();
200
-
201
- expect((await myKey.get())?.fingerPrint).toEqual(fpr);
202
- expect((await myKey.get(fpr))?.fingerPrint).toEqual(fpr);
203
-
204
- const keys = [{ key, fpr }];
205
- for (let i = 0; i < 10; ++i) {
206
- const key = base58btc.encode(kb.rt.crypto.randomBytes(kb.rt.keyLength));
207
- const fpr = (await toKeyWithFingerPrint(kb, coerceMaterial(kb, key), true)).Ok().fingerPrint;
208
- keys.push({ key, fpr });
209
- const rUpsert = await myKey.upsert(key, true);
210
- expect(rUpsert.Ok().modified).toBeTruthy();
211
- for (const { fpr } of keys) {
212
- expect((await myKey.get(fpr))?.fingerPrint).toEqual(fpr);
213
- }
214
- expect((await myKey.get())?.fingerPrint).toEqual(fpr);
215
- }
216
- const provider = await kb.rt.getBagProvider();
217
- let diskBag: V2KeysItem;
218
- if (!("_prepare" in provider)) {
219
- diskBag = (await provider.get(name)) as V2KeysItem;
220
- } else {
221
- if (runtimeFn().isBrowser) {
222
- const p = provider as KeyBagProviderIndexedDB;
223
- diskBag = await p._prepare().then((db) => db.get("bag", name));
224
- } else {
225
- const p = provider as KeyBagProviderFile;
226
- const { sysFS } = await p._prepare(name);
227
- diskBag = await sysFS.readfile((await p._prepare(name)).fName).then((data) => {
228
- return JSON.parse(sthis.txt.decode(data)) as V2KeysItem;
229
- });
230
- }
231
- }
232
- expect(Object.values(diskBag.keys).length).toEqual(keys.length);
233
- });
234
- });
235
-
236
- describe("KeyedCryptoStore", () => {
237
- let kb: KeyBag;
238
- // let logger: Logger;
239
- let baseUrl: URI;
240
- const sthis = ensureSuperThis();
241
- let loader: Loadable;
242
- beforeEach(async () => {
243
- await sthis.start();
244
- // logger = MockLogger().logger;
245
- // let kbUrl: URI;
246
- // if (runtimeFn().isBrowser) {
247
- // kbUrl = URI.from("indexeddb://fp-keybag");
248
- // baseUrl = URI.from("indexeddb://fp-keyed-crypto-store");
249
- // } else {
250
- // kbUrl = URI.merge(`file://./dist/tests/key.bag`, sthis.env.get("FP_KEYBAG_URL"));
251
- // baseUrl = URI.merge("file://./dist/tests/keyed-crypto-store", sthis.env.get("FP_STORAGE_URL"));
252
- // }
253
- // baseUrl = baseUrl.build().defParam(PARAM.NAME, "test").URI();
254
-
255
- const envURL = sthis.env.get("FP_KEYBAG_URL");
256
- if (envURL) {
257
- baseUrl = getDefaultURI(sthis, URI.from(envURL).protocol);
258
- } else {
259
- baseUrl = getDefaultURI(sthis);
260
- }
261
- baseUrl = baseUrl.build().setParam(PARAM.NAME, "test").URI();
262
- kb = await getKeyBag(sthis, {});
263
- loader = mockLoader(sthis);
264
- });
265
- it("no crypto", async () => {
266
- const url = baseUrl.build().setParam(PARAM.STORE_KEY, "insecure").URI();
267
-
268
- for (const pstore of (await createAttachedStores(url, loader, "insecure")).stores.baseStores) {
269
- const store = await pstore;
270
- // await store.start();
271
- const kc = await store.keyedCrypto();
272
- expect(kc.constructor.name).toBe("noCrypto");
273
- // expect(kc.isEncrypting).toBe(false);
274
- expect(kc.constructor.name).toBe("noCrypto");
275
- // expect(kc.isEncrypting).toBe(false);
276
- }
277
- });
278
-
279
- it("create key", async () => {
280
- for (const pstore of (await createAttachedStores(baseUrl, loader, "insecure")).stores.baseStores) {
281
- const store = await pstore; // await bs.ensureStart(await pstore, logger);
282
- const kc = await store.keyedCrypto();
283
- expect(kc.constructor.name).toBe("cryptoAction");
284
- // expect(kc.isEncrypting).toBe(true);
285
- expect(store.url().getParam(PARAM.STORE_KEY)).toBe(
286
- `@test-${storeType2DataMetaWal(store.url().getParam(PARAM.STORE) as StoreType)}@`,
287
- );
288
- }
289
- });
290
-
291
- it("key ref keybag", async () => {
292
- const key = base58btc.encode(kb.rt.crypto.randomBytes(kb.rt.keyLength));
293
- const genKey = await kb.getNamedKey("@heute@", false, key);
294
- const url = baseUrl.build().setParam(PARAM.STORE_KEY, "@heute@").URI();
295
- for (const pstore of (await createAttachedStores(url, loader, "insecure")).stores.baseStores) {
296
- const store = await pstore;
297
- // await store.start();
298
- expect(store.url().getParam(PARAM.STORE_KEY)).toBe(`@heute@`);
299
- const kc = await store.keyedCrypto();
300
- expect(kc.constructor.name).toBe("cryptoAction");
301
- const testData = kb.rt.crypto.randomBytes(1024);
302
- const iv = kb.rt.crypto.randomBytes(12);
303
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
304
- const blk = await kc._encrypt({ bytes: testData, key: (await kc.key.get())!.key, iv });
305
- expect(blk).not.toEqual(testData);
306
- const fpkey = (await genKey.Ok().get()) as KeyWithFingerPrint;
307
- expect(fpkey.fingerPrint).toEqual(fpkey.fingerPrint);
308
- const dec = new Uint8Array(await kc.crypto.decrypt(kc.algo(iv), fpkey.key, blk));
309
- expect(dec).toEqual(testData);
310
- }
311
- });
312
-
313
- it("key", async () => {
314
- const key = base58btc.encode(kb.rt.crypto.randomBytes(kb.rt.keyLength));
315
- const url = baseUrl.build().setParam(PARAM.STORE_KEY, key).URI();
316
- for (const pstore of (await createAttachedStores(url, loader, "insecure")).stores.baseStores) {
317
- // for (const pstore of [strt.makeDataStore(loader), strt.makeMetaStore(loader), strt.makeWALStore(loader)]) {
318
- const store = await pstore;
319
- // await store.start();
320
- expect(store.url().getParam(PARAM.STORE_KEY)).toBe(key);
321
- const kc = await store.keyedCrypto();
322
- expect(kc.constructor.name).toBe("cryptoAction");
323
- const testData = kb.rt.crypto.randomBytes(1024);
324
- const iv = kb.rt.crypto.randomBytes(12);
325
- const ks = (await kc.key.get()) as KeyWithFingerPrint;
326
- const blk = await kc._encrypt({ bytes: testData, key: ks.key, iv });
327
- expect(blk).not.toEqual(testData);
328
- const dec = await kc._decrypt({ bytes: blk, key: ks.key, iv });
329
- expect(dec).toEqual(testData);
330
- }
331
- });
332
- });
333
-
334
- describe("KeyedCrypto", () => {
335
- let kb: KeyBag;
336
- let kycr: CryptoAction;
337
- let keyStr: string;
338
- const sthis = ensureSuperThis();
339
- beforeEach(async () => {
340
- // let url: URI;
341
- // if (runtimeFn().isBrowser) {
342
- // url = URI.from("indexeddb://fp-keybag");
343
- // } else {
344
- // url = URI.merge(`file://./dist/tests/key.bag`, sthis.env.get("FP_KEYBAG_URL"));
345
- // }
346
- kb = await getKeyBag(sthis, {
347
- // url,
348
- });
349
- keyStr = base58btc.encode(kb.rt.crypto.randomBytes(kb.rt.keyLength));
350
- kycr = await keyedCryptoFactory(URI.from(`test://bla?storekey=${keyStr}`), kb, sthis);
351
- });
352
- it("codec explict iv", async () => {
353
- const testData = kb.rt.crypto.randomBytes(1024);
354
- const iv = kb.rt.crypto.randomBytes(12);
355
- const codec = kycr.codec(iv, { noIVVerify: true });
356
- const blk = (await codec.encode(testData)) as Uint8Array;
357
- const myDec = cborg.decode(blk) as IvKeyIdData;
358
- expect(myDec.iv).toEqual(iv);
359
- const kc = (await kycr.key.get()) as KeyWithFingerPrint;
360
- expect(base58btc.encode(myDec.keyId)).toEqual(kc.fingerPrint);
361
- const dec = await codec.decode(blk);
362
- expect(dec.data).toEqual(testData);
363
- });
364
-
365
- it("codec implict iv", async () => {
366
- const testData = kb.rt.crypto.randomBytes(1024);
367
- const codec = kycr.codec();
368
- const blk = await codec.encode(testData);
369
- expect(blk.length).toBeGreaterThanOrEqual(12 + testData.length);
370
- const dec = await codec.decode(blk);
371
- expect(dec.data).toEqual(testData);
372
- });
373
-
374
- it("codec implict iv same for multiple clients", async () => {
375
- const testData = kb.rt.crypto.randomBytes(1024);
376
- const codec = kycr.codec();
377
- const blk = await codec.encode(testData);
378
- const blk2 = await codec.encode(testData);
379
- expect(blk).toEqual(blk2);
380
- });
381
- });
@@ -1,313 +0,0 @@
1
- import * as codec from "@ipld/dag-cbor";
2
- import { sha256 as hasher } from "multiformats/hashes/sha2";
3
- import { CID } from "multiformats/cid";
4
- import { CRDTMeta, CarTransaction, IndexTransactionMeta, SuperThis } from "@fireproof/core";
5
- import { simpleBlockOpts } from "../helpers.js";
6
- import {
7
- EncryptedBlockstore,
8
- Loader,
9
- CompactionFetcher,
10
- CarTransactionImpl,
11
- anyBlock2FPBlock,
12
- parseCarFile,
13
- } from "@fireproof/core-blockstore";
14
- import { asyncBlockEncode, ensureSuperThis } from "@fireproof/core-runtime";
15
- import {
16
- FPBlock,
17
- TransactionMeta,
18
- isCarBlockItemReady,
19
- CarGroup,
20
- isCarBlockItemStale,
21
- AnyLink,
22
- } from "@fireproof/core-types-blockstore";
23
- import { describe, afterEach, beforeEach, it, expect, assert } from "vitest";
24
-
25
- class MyMemoryBlockStore extends EncryptedBlockstore {
26
- readonly memblock = new Map<string, FPBlock>();
27
- loader: Loader;
28
- constructor(sthis: SuperThis) {
29
- const ebOpts = simpleBlockOpts(sthis, "MyMemoryBlockStore"); //, "MyMemoryBlockStore");
30
- // const ebOpts = {
31
- // name: "MyMemoryBlockStore",
32
- // } as BlockstoreOpts;
33
- super(sthis, ebOpts);
34
- this.loader = new Loader(sthis, ebOpts);
35
- }
36
- ready(): Promise<void> {
37
- return Promise.resolve();
38
- }
39
- close(): Promise<void> {
40
- return this.loader.close();
41
- }
42
- readonly transactions = new Set<CarTransaction>();
43
- // readonly lastTxMeta?: TransactionMeta;
44
- readonly compacting: boolean = false;
45
-
46
- override async put(fp: FPBlock): Promise<void> {
47
- this.memblock.set(fp.cid.toString(), fp);
48
- }
49
-
50
- // transaction<M ext(fn: (t: CarTransaction) => Promise<MetaType>, opts?: { noLoader: boolean }): Promise<MetaType> {
51
- // throw new Error("Method not implemented.");
52
- // }
53
-
54
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
55
- getFile(car: AnyLink, cid: AnyLink, isPublic?: boolean): Promise<Uint8Array> {
56
- throw new Error("Method not implemented.");
57
- }
58
- compact(): Promise<void> {
59
- throw new Error("Method not implemented.");
60
- }
61
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
62
- defaultCompact(blocks: CompactionFetcher): Promise<TransactionMeta> {
63
- throw new Error("Method not implemented.");
64
- }
65
- }
66
-
67
- describe("basic Loader simple", function () {
68
- let loader: Loader;
69
- let block: FPBlock;
70
- let t: CarTransaction;
71
- const sthis = ensureSuperThis();
72
-
73
- afterEach(async () => {
74
- await loader.close();
75
- await loader.destroy();
76
- });
77
-
78
- beforeEach(async () => {
79
- const testDbName = "test-loader-commit";
80
- await sthis.start();
81
- const mockM = new MyMemoryBlockStore(sthis);
82
- t = new CarTransactionImpl(mockM as EncryptedBlockstore);
83
- loader = new Loader(sthis, {
84
- ...simpleBlockOpts(sthis, testDbName),
85
- public: true,
86
- });
87
- await loader.ready();
88
- block = await anyBlock2FPBlock(
89
- await asyncBlockEncode({
90
- value: { hello: "world" },
91
- hasher,
92
- codec,
93
- }),
94
- );
95
- await t.put(block);
96
- await mockM.put(block);
97
- });
98
- it("should have an empty car log", function () {
99
- expect(loader.carLog.length).toBe(0);
100
- });
101
- it("should commit", async () => {
102
- const carGroup = await loader.commit(t, { head: [block.cid] });
103
- expect(loader.carLog.length).toBe(1);
104
- const reader = await loader.loadCar(carGroup[0], loader.attachedStores.local());
105
- assert(isCarBlockItemReady(reader));
106
- expect(reader).toBeTruthy();
107
- const parsed = await parseCarFile<CRDTMeta>(reader, loader.logger);
108
- expect(parsed.cars).toBeTruthy();
109
- expect(parsed.cars.length).toBe(0);
110
- expect(parsed.meta).toBeTruthy();
111
- expect(parsed.meta.head).toBeTruthy();
112
- });
113
- });
114
-
115
- describe("basic Loader with two commits", function () {
116
- let loader: Loader;
117
- let block: FPBlock;
118
- let block2: FPBlock;
119
- let block3: FPBlock;
120
- let block4: FPBlock;
121
- let t: CarTransaction;
122
- let carCid: CarGroup;
123
- let carCid0: CarGroup;
124
-
125
- const sthis = ensureSuperThis();
126
-
127
- afterEach(async () => {
128
- await loader.close();
129
- await loader.destroy();
130
- });
131
-
132
- beforeEach(async () => {
133
- await sthis.start();
134
- const mockM = new MyMemoryBlockStore(sthis);
135
- t = new CarTransactionImpl(mockM);
136
- loader = new Loader(sthis, {
137
- ...simpleBlockOpts(sthis, "test-loader-two-commit"),
138
- public: true,
139
- });
140
- await loader.ready();
141
-
142
- block = await anyBlock2FPBlock(
143
- await asyncBlockEncode({
144
- value: { hello: "world" },
145
- hasher,
146
- codec,
147
- }),
148
- );
149
- await t.put(block);
150
- carCid0 = await loader.commit(t, { head: [block.cid] });
151
-
152
- block2 = await anyBlock2FPBlock(
153
- await asyncBlockEncode({
154
- value: { hello: "universe" },
155
- hasher,
156
- codec,
157
- }),
158
- );
159
- await t.put(block2);
160
- carCid = await loader.commit(t, { head: [block2.cid] });
161
-
162
- block3 = await anyBlock2FPBlock(
163
- await asyncBlockEncode({
164
- value: { hello: "multiverse" },
165
- hasher,
166
- codec,
167
- }),
168
- );
169
- await t.put(block3);
170
-
171
- block4 = await anyBlock2FPBlock(
172
- await asyncBlockEncode({
173
- value: { hello: "megaverse" },
174
- hasher,
175
- codec,
176
- }),
177
- );
178
-
179
- await t.put(block4);
180
- });
181
-
182
- it("should have a car log", function () {
183
- expect(loader.carLog.length).toBe(2);
184
- expect(loader.carLog.asArray()[0].toString()).toBe(carCid.toString());
185
- expect(loader.carLog.asArray()[1].toString()).toBe(carCid0.toString());
186
- });
187
-
188
- it("should commit", async () => {
189
- const reader = await loader.loadCar(carCid[0], loader.attachedStores.local());
190
- expect(reader).toBeTruthy();
191
- assert(isCarBlockItemReady(reader));
192
- const parsed = await parseCarFile<CRDTMeta>(reader, loader.logger);
193
- expect(parsed.cars).toBeTruthy();
194
- expect(parsed.compact.length).toBe(0);
195
- expect(parsed.cars.length).toBe(1);
196
- expect(parsed.meta).toBeTruthy();
197
- expect(parsed.meta.head).toBeTruthy();
198
- });
199
-
200
- it("should compact", async () => {
201
- const compactCid = await loader.commit(t, { head: [block2.cid] }, { compact: true });
202
- expect(loader.carLog.length).toBe(1);
203
-
204
- const reader = await loader.loadCar(compactCid[0], loader.attachedStores.local());
205
- expect(reader).toBeTruthy();
206
- assert(isCarBlockItemReady(reader));
207
- const parsed = await parseCarFile<CRDTMeta>(reader, loader.logger);
208
- expect(parsed.cars).toBeTruthy();
209
- expect(parsed.compact.length).toBe(2);
210
- expect(parsed.cars.length).toBe(0);
211
- expect(parsed.meta).toBeTruthy();
212
- expect(parsed.meta.head).toBeTruthy();
213
- });
214
-
215
- it("compact should erase old files", async () => {
216
- const cs = await loader.attachedStores.local().active.car;
217
- await loader.commit(t, { head: [block2.cid] }, { compact: true });
218
- expect(loader.carLog.length).toBe(1);
219
- await loader.commit(t, { head: [block3.cid] }, { compact: false });
220
- expect(loader.carLog.length).toBe(2);
221
- expect(await cs.load(carCid[0])).toBeTruthy();
222
- await loader.commit(t, { head: [block3.cid] }, { compact: true });
223
- expect(loader.carLog.length).toBe(1);
224
- const e0 = await cs.load(carCid[0]).catch((e) => e);
225
- expect(e0 instanceof Error).toBeTruthy();
226
- await loader.commit(t, { head: [block4.cid] }, { compact: false });
227
- expect(loader.carLog.length).toBe(2);
228
-
229
- const e = await loader.loadCar(carCid[0], loader.attachedStores.local());
230
- expect(e).toBeTruthy();
231
- assert(isCarBlockItemStale(e));
232
- expect(e.item.status).toBe("stale");
233
- expect(e.item.statusCause.message).toMatch(/(missing car file)|(not found)/);
234
- }, 10000);
235
- });
236
-
237
- describe("basic Loader with index commits", function () {
238
- let block: FPBlock;
239
- let ib: EncryptedBlockstore;
240
- let indexerResult: IndexTransactionMeta;
241
- let cid: CID;
242
- // let indexMap: Map<string, CID>;
243
- const sthis = ensureSuperThis();
244
-
245
- afterEach(async () => {
246
- await ib.close();
247
- await ib.destroy();
248
- });
249
-
250
- beforeEach(async () => {
251
- const name = "test-loader-index" + Math.random();
252
- await sthis.start();
253
- // t = new CarTransaction()
254
- ib = new EncryptedBlockstore(sthis, simpleBlockOpts(sthis, name));
255
- await ib.ready();
256
- block = await anyBlock2FPBlock(
257
- await asyncBlockEncode({
258
- value: { hello: "world" },
259
- hasher,
260
- codec,
261
- }),
262
- );
263
- // console.log('block', block.cid)
264
-
265
- cid = CID.parse("bafybeia4luuns6dgymy5kau5rm7r4qzrrzg6cglpzpogussprpy42cmcn4");
266
- indexerResult = {
267
- indexes: {
268
- hello: {
269
- byId: cid,
270
- byKey: cid,
271
- head: [cid as CID<unknown, number, number, 1>],
272
- name: "hello",
273
- map: "(doc) => doc.hello",
274
- },
275
- },
276
- };
277
- // indexMap = new Map();
278
- });
279
-
280
- it("should start with an empty car log", function () {
281
- expect(ib.loader).toBeTruthy();
282
- expect(ib.loader.carLog.length).toBe(0);
283
- });
284
-
285
- it("should commit the index metadata", async () => {
286
- const { cars: carCid } = await ib.transaction<IndexTransactionMeta>(
287
- async (t) => {
288
- await t.put(block);
289
- return indexerResult;
290
- } /* , indexMap */,
291
- );
292
-
293
- expect(carCid).toBeTruthy();
294
- expect(ib.loader).toBeTruthy();
295
- const carLog = ib.loader.carLog;
296
-
297
- expect(carLog.length).toBe(1);
298
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
299
- const reader = await ib.loader.loadCar(carCid![0], ib.loader.attachedStores.local());
300
- expect(reader).toBeTruthy();
301
- assert(isCarBlockItemReady(reader));
302
- const parsed = await parseCarFile<IndexTransactionMeta>(reader, sthis.logger);
303
- expect(parsed.cars).toBeTruthy();
304
- expect(parsed.cars.length).toBe(0);
305
- expect(parsed.meta).toBeTruthy();
306
- expect(parsed.meta.indexes).toBeTruthy();
307
- const indexes = parsed.meta.indexes;
308
- expect(indexes).toBeTruthy();
309
- expect(indexes.hello).toBeTruthy();
310
- expect(indexes.hello.map).toBe("(doc) => doc.hello");
311
- expect(indexes.hello.name).toBe("hello");
312
- });
313
- });