@fireproof/core 0.19.100 → 0.19.101

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. package/{chunk-OFGPKRCM.js → chunk-3EB3ENHT.js} +54 -25
  2. package/chunk-3EB3ENHT.js.map +1 -0
  3. package/chunk-HQ7D3PEU.js +61 -0
  4. package/chunk-HQ7D3PEU.js.map +1 -0
  5. package/chunk-PZ5AY32C.js +10 -0
  6. package/deno-filesystem-Q2IJ7YDR.js +57 -0
  7. package/deno-filesystem-Q2IJ7YDR.js.map +1 -0
  8. package/{gateway-5FCWPX5W.js → gateway-GK5QZ6KP.js} +13 -12
  9. package/gateway-GK5QZ6KP.js.map +1 -0
  10. package/{gateway-H7UD6TNB.js → gateway-TQTGDRCN.js} +9 -8
  11. package/gateway-TQTGDRCN.js.map +1 -0
  12. package/index.cjs +2158 -1718
  13. package/index.cjs.map +1 -1
  14. package/index.d.cts +261 -117
  15. package/index.d.ts +261 -117
  16. package/index.global.js +11834 -11354
  17. package/index.global.js.map +1 -1
  18. package/index.js +1865 -1519
  19. package/index.js.map +1 -1
  20. package/{key-bag-file-WADZBHYG.js → key-bag-file-VOSSK46F.js} +4 -3
  21. package/{key-bag-file-WADZBHYG.js.map → key-bag-file-VOSSK46F.js.map} +1 -1
  22. package/{key-bag-indexdb-PGVAI3FJ.js → key-bag-indexdb-AXTQOSMC.js} +4 -3
  23. package/{key-bag-indexdb-PGVAI3FJ.js.map → key-bag-indexdb-AXTQOSMC.js.map} +1 -1
  24. package/key-bag-memory-LWE6ARPX.js +29 -0
  25. package/key-bag-memory-LWE6ARPX.js.map +1 -0
  26. package/metafile-cjs.json +1 -1
  27. package/metafile-esm.json +1 -1
  28. package/metafile-iife.json +1 -1
  29. package/{node-filesystem-INX4ZTHE.js → node-filesystem-CFRXFSO7.js} +6 -9
  30. package/node-filesystem-CFRXFSO7.js.map +1 -0
  31. package/package.json +1 -1
  32. package/tests/blockstore/keyed-crypto-indexdb-file.test.ts +129 -0
  33. package/tests/blockstore/keyed-crypto.test.ts +63 -227
  34. package/tests/blockstore/loader.test.ts +19 -11
  35. package/tests/blockstore/store.test.ts +23 -19
  36. package/tests/blockstore/transaction.test.ts +12 -12
  37. package/tests/fireproof/all-gateway.test.ts +201 -193
  38. package/tests/fireproof/cars/bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i.ts +324 -316
  39. package/tests/fireproof/crdt.test.ts +67 -16
  40. package/tests/fireproof/database.test.ts +183 -21
  41. package/tests/fireproof/fireproof.test.ts +83 -74
  42. package/tests/fireproof/hello.test.ts +18 -14
  43. package/tests/fireproof/indexer.test.ts +53 -43
  44. package/tests/fireproof/utils.test.ts +18 -6
  45. package/tests/gateway/file/loader-config.test.ts +303 -0
  46. package/tests/gateway/indexdb/loader-config.test.ts +75 -0
  47. package/tests/helpers.ts +27 -9
  48. package/tests/react/useFireproof.test.tsx +1 -1
  49. package/{utils-QO2HIWGI.js → utils-STA2C35G.js} +4 -3
  50. package/utils-STA2C35G.js.map +1 -0
  51. package/chunk-OFGPKRCM.js.map +0 -1
  52. package/chunk-WS3YRPIA.js +0 -75
  53. package/chunk-WS3YRPIA.js.map +0 -1
  54. package/gateway-5FCWPX5W.js.map +0 -1
  55. package/gateway-H7UD6TNB.js.map +0 -1
  56. package/mem-filesystem-YPPJV7Q2.js +0 -41
  57. package/mem-filesystem-YPPJV7Q2.js.map +0 -1
  58. package/node-filesystem-INX4ZTHE.js.map +0 -1
  59. package/tests/fireproof/config.test.ts +0 -172
  60. /package/{utils-QO2HIWGI.js.map → chunk-PZ5AY32C.js.map} +0 -0
  61. /package/tests/fireproof/{fireproof.test.fixture.ts → fireproof.fixture.ts} +0 -0
@@ -1,13 +1,13 @@
1
1
  import {
2
2
  toArrayBuffer
3
- } from "./chunk-WS3YRPIA.js";
4
- import "./chunk-OFGPKRCM.js";
3
+ } from "./chunk-HQ7D3PEU.js";
4
+ import "./chunk-3EB3ENHT.js";
5
+ import "./chunk-PZ5AY32C.js";
5
6
 
6
7
  // src/runtime/gateways/file/node-filesystem.ts
7
- import { runtimeFn } from "@adviser/cement";
8
8
  var NodeFileSystem = class {
9
9
  async start() {
10
- this.fs = await import("node:fs/promises");
10
+ this.fs = await import("fs/promises");
11
11
  return this;
12
12
  }
13
13
  async mkdir(path, options) {
@@ -27,19 +27,16 @@ var NodeFileSystem = class {
27
27
  return toArrayBuffer(ret);
28
28
  }
29
29
  stat(path) {
30
- return this.fs?.stat(path);
30
+ return this.fs.stat(path);
31
31
  }
32
32
  async unlink(path) {
33
33
  return this.fs?.unlink(path);
34
34
  }
35
35
  async writefile(path, data) {
36
- if (runtimeFn().isDeno) {
37
- return this.fs?.writeFile(path, data);
38
- }
39
36
  return this.fs?.writeFile(path, Buffer.from(data));
40
37
  }
41
38
  };
42
39
  export {
43
40
  NodeFileSystem
44
41
  };
45
- //# sourceMappingURL=node-filesystem-INX4ZTHE.js.map
42
+ //# sourceMappingURL=node-filesystem-CFRXFSO7.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/runtime/gateways/file/node-filesystem.ts"],"sourcesContent":["import type { PathLike, MakeDirectoryOptions, ObjectEncodingOptions } from \"fs\";\nimport type { mkdir, readdir, rm, copyFile, readFile, stat, unlink, writeFile } from \"fs/promises\";\nimport { toArrayBuffer } from \"./utils.js\";\nimport { FPStats, SysFileSystem } from \"../../../types.js\";\n\nexport class NodeFileSystem implements SysFileSystem {\n fs?: {\n mkdir: typeof mkdir;\n readdir: typeof readdir;\n rm: typeof rm;\n copyFile: typeof copyFile;\n readFile: typeof readFile;\n stat: typeof stat;\n unlink: typeof unlink;\n writeFile: typeof writeFile;\n };\n\n async start(): Promise<SysFileSystem> {\n this.fs = await import(\"fs/promises\");\n return this;\n }\n async mkdir(path: PathLike, options?: { recursive: boolean }): Promise<string | undefined> {\n return this.fs?.mkdir(path, options);\n }\n async readdir(path: PathLike, options?: ObjectEncodingOptions): Promise<string[]> {\n return this.fs?.readdir(path, options) as Promise<string[]>;\n }\n async rm(path: PathLike, options?: MakeDirectoryOptions & { recursive: boolean }): Promise<void> {\n return this.fs?.rm(path, options);\n }\n async copyFile(source: PathLike, destination: PathLike): Promise<void> {\n return this.fs?.copyFile(source, destination);\n }\n async readfile(path: PathLike, options?: { encoding: BufferEncoding; flag?: string }): Promise<Uint8Array> {\n const ret = (await this.fs?.readFile(path, options)) as Buffer;\n return toArrayBuffer(ret);\n }\n stat(path: PathLike): Promise<FPStats> {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n return this.fs!.stat(path);\n }\n async unlink(path: PathLike): Promise<void> {\n return this.fs?.unlink(path);\n }\n async writefile(path: PathLike, data: Uint8Array | string): Promise<void> {\n return this.fs?.writeFile(path, Buffer.from(data));\n }\n}\n"],"mappings":";;;;;;;AAKO,IAAM,iBAAN,MAA8C;AAAA,EAYnD,MAAM,QAAgC;AACpC,SAAK,KAAK,MAAM,OAAO,aAAa;AACpC,WAAO;AAAA,EACT;AAAA,EACA,MAAM,MAAM,MAAgB,SAA+D;AACzF,WAAO,KAAK,IAAI,MAAM,MAAM,OAAO;AAAA,EACrC;AAAA,EACA,MAAM,QAAQ,MAAgB,SAAoD;AAChF,WAAO,KAAK,IAAI,QAAQ,MAAM,OAAO;AAAA,EACvC;AAAA,EACA,MAAM,GAAG,MAAgB,SAAwE;AAC/F,WAAO,KAAK,IAAI,GAAG,MAAM,OAAO;AAAA,EAClC;AAAA,EACA,MAAM,SAAS,QAAkB,aAAsC;AACrE,WAAO,KAAK,IAAI,SAAS,QAAQ,WAAW;AAAA,EAC9C;AAAA,EACA,MAAM,SAAS,MAAgB,SAA4E;AACzG,UAAM,MAAO,MAAM,KAAK,IAAI,SAAS,MAAM,OAAO;AAClD,WAAO,cAAc,GAAG;AAAA,EAC1B;AAAA,EACA,KAAK,MAAkC;AAErC,WAAO,KAAK,GAAI,KAAK,IAAI;AAAA,EAC3B;AAAA,EACA,MAAM,OAAO,MAA+B;AAC1C,WAAO,KAAK,IAAI,OAAO,IAAI;AAAA,EAC7B;AAAA,EACA,MAAM,UAAU,MAAgB,MAA0C;AACxE,WAAO,KAAK,IAAI,UAAU,MAAM,OAAO,KAAK,IAAI,CAAC;AAAA,EACnD;AACF;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fireproof/core",
3
- "version": "0.19.100",
3
+ "version": "0.19.101",
4
4
  "description": "Live database for the web.",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -0,0 +1,129 @@
1
+ import { bs, PARAM, rt } from "@fireproof/core";
2
+ import { runtimeFn, toCryptoRuntime, URI } from "@adviser/cement";
3
+ import { base58btc } from "multiformats/bases/base58";
4
+ import { mockSuperThis } from "../helpers";
5
+
6
+ describe("KeyBag indexdb and file", () => {
7
+ let url: URI;
8
+ const sthis = mockSuperThis();
9
+ beforeAll(async () => {
10
+ await sthis.start();
11
+ if (runtimeFn().isBrowser) {
12
+ url = URI.from("indexdb://fp-keybag");
13
+ } else {
14
+ url = URI.merge(`file://./dist/tests/key.bag`, sthis.env.get("FP_KEYBAG_URL"));
15
+ }
16
+ });
17
+ it("default-path", async () => {
18
+ const old = sthis.env.get("FP_KEYBAG_URL");
19
+ sthis.env.delete("FP_KEYBAG_URL");
20
+ const kb = await rt.kb.getKeyBag(sthis);
21
+ if (runtimeFn().isBrowser) {
22
+ expect(kb.rt.url.toString()).toBe(`indexdb://fp-keybag`);
23
+ } else {
24
+ expect(kb.rt.url.toString()).toBe(`file://${sthis.env.get("HOME")}/.fireproof/keybag`);
25
+ }
26
+ sthis.env.set("FP_KEYBAG_URL", old);
27
+ });
28
+ it("from env", async () => {
29
+ const old = sthis.env.get("FP_KEYBAG_URL");
30
+ sthis.env.set("FP_KEYBAG_URL", url.toString());
31
+ const kb = await rt.kb.getKeyBag(sthis);
32
+ expect(kb.rt.url.toString()).toBe(url.toString());
33
+ sthis.env.set("FP_KEYBAG_URL", old);
34
+ });
35
+ it("simple add", async () => {
36
+ const kb = await rt.kb.getKeyBag(sthis, {
37
+ url: url.toString(),
38
+ crypto: toCryptoRuntime({
39
+ randomBytes: (size) => new Uint8Array(size).map((_, i) => i),
40
+ }),
41
+ });
42
+ const name = "setkey" + Math.random();
43
+ expect((await kb.getNamedKey(name, true)).isErr()).toBeTruthy();
44
+
45
+ const key = base58btc.encode(kb.rt.crypto.randomBytes(kb.rt.keyLength));
46
+ const res = await kb.setNamedKey(name, key);
47
+ expect(res.isOk()).toBeTruthy();
48
+ expect((await kb.getNamedKey(name, true)).Ok()).toEqual(res.Ok());
49
+
50
+ const name2 = "implicit";
51
+ const created = await kb.getNamedKey(name2);
52
+ expect(created.isOk()).toBeTruthy();
53
+
54
+ expect((await kb.getNamedKey(name2)).Ok()).toEqual(created.Ok());
55
+
56
+ let diskBag: rt.kb.KeyItem;
57
+ let diskBag2: rt.kb.KeyItem;
58
+ const provider = await kb.rt.getBag();
59
+ if (runtimeFn().isBrowser) {
60
+ const p = provider as rt.kb.KeyBagProviderIndexDB;
61
+ diskBag = await p._prepare().then((db) => db.get("bag", name));
62
+ diskBag2 = await p._prepare().then((db) => db.get("bag", name2));
63
+ } else {
64
+ const p = provider as rt.kb.KeyBagProviderFile;
65
+ const { sysFS } = await p._prepare(name);
66
+
67
+ diskBag = await sysFS.readfile((await p._prepare(name)).fName).then((data) => {
68
+ return JSON.parse(new TextDecoder().decode(data)) as rt.kb.KeyItem;
69
+ });
70
+ diskBag2 = await sysFS.readfile((await p._prepare(name2)).fName).then((data) => {
71
+ return JSON.parse(new TextDecoder().decode(data)) as rt.kb.KeyItem;
72
+ });
73
+ }
74
+ expect(await kb.toKeyWithFingerPrint(diskBag.key)).toEqual(res);
75
+ expect(await kb.toKeyWithFingerPrint(diskBag2.key)).toEqual(created);
76
+ const algo = {
77
+ name: "AES-GCM",
78
+ iv: kb.rt.crypto.randomBytes(12),
79
+ tagLength: 128,
80
+ };
81
+ const data = kb.rt.crypto.randomBytes(122);
82
+ expect(await kb.rt.crypto.encrypt(algo, res.Ok().key, data)).toEqual(await kb.rt.crypto.encrypt(algo, created.Ok().key, data));
83
+ expect(await kb.rt.crypto.encrypt(algo, await kb.subtleKey(diskBag.key), data)).toEqual(
84
+ await kb.rt.crypto.encrypt(algo, created.Ok().key, data),
85
+ );
86
+ expect(await kb.rt.crypto.encrypt(algo, await kb.subtleKey(diskBag2.key), data)).toEqual(
87
+ await kb.rt.crypto.encrypt(algo, created.Ok().key, data),
88
+ );
89
+ });
90
+ });
91
+
92
+ describe("KeyedCryptoStore", () => {
93
+ let kb: rt.kb.KeyBag;
94
+ // let logger: Logger;
95
+ let baseUrl: URI;
96
+ const sthis = mockSuperThis();
97
+ beforeEach(async () => {
98
+ await sthis.start();
99
+ // logger = MockLogger().logger;
100
+ let kbUrl: URI;
101
+ if (runtimeFn().isBrowser) {
102
+ kbUrl = URI.from("indexdb://fp-keybag");
103
+ baseUrl = URI.from("indexdb://fp-keyed-crypto-store");
104
+ } else {
105
+ kbUrl = URI.merge(`file://./dist/tests/key.bag`, sthis.env.get("FP_KEYBAG_URL"));
106
+ baseUrl = URI.merge("file://./dist/tests/keyed-crypto-store", sthis.env.get("FP_STORAGE_URL"));
107
+ }
108
+ baseUrl = baseUrl.build().defParam(PARAM.NAME, "test").URI();
109
+ kb = await rt.kb.getKeyBag(sthis, {
110
+ url: kbUrl,
111
+ });
112
+ });
113
+ it("no crypto", async () => {
114
+ const strt = bs.toStoreRuntime(sthis);
115
+ const url = baseUrl.build().setParam(PARAM.STORE_KEY, "insecure").URI();
116
+
117
+ for (const pstore of [
118
+ strt.makeDataStore({ sthis, url, keybag: kb }),
119
+ strt.makeMetaStore({ sthis, url, keybag: kb }),
120
+ strt.makeWALStore({ sthis, url, keybag: kb }),
121
+ ]) {
122
+ const store = await pstore;
123
+ // await store.start();
124
+ const kc = await store.keyedCrypto();
125
+ expect(kc.constructor.name).toBe("noCrypto");
126
+ // expect(kc.isEncrypting).toBe(false);
127
+ }
128
+ });
129
+ });
@@ -1,219 +1,86 @@
1
- import { bs, rt } from "@fireproof/core";
2
- import { BuildURI, Logger, MockLogger, runtimeFn, toCryptoRuntime, URI } from "@adviser/cement";
1
+ import { bs, PARAM, rt } from "@fireproof/core";
2
+ import { URI } from "@adviser/cement";
3
3
  import { base58btc } from "multiformats/bases/base58";
4
4
  import { sha256 as hasher } from "multiformats/hashes/sha2";
5
5
  import * as dagCodec from "@ipld/dag-cbor";
6
- import { MockSuperThis, mockSuperThis } from "../helpers";
7
-
8
- describe("KeyBag", () => {
9
- let url: URI;
10
- let sthis: MockSuperThis;
11
-
12
- beforeEach(async () => {
13
- sthis = mockSuperThis();
14
- await sthis.start();
15
- if (runtimeFn().isBrowser) {
16
- url = URI.from("indexdb://fp-keybag");
17
- } else {
18
- url = URI.merge(`file://./dist/tests/key.bag`, sthis.env.get("FP_KEYBAG_URL"));
19
- }
20
- });
21
- it("default-path", async () => {
22
- const old = sthis.env.get("FP_KEYBAG_URL");
23
- sthis.env.delete("FP_KEYBAG_URL");
24
- const kb = await rt.kb.getKeyBag(sthis);
25
- if (runtimeFn().isBrowser) {
26
- expect(kb.rt.url.toString()).toBe(`indexdb://fp-keybag`);
27
- } else {
28
- expect(kb.rt.url.toString()).toBe(`file://${sthis.env.get("HOME")}/.fireproof/keybag`);
29
- }
30
- sthis.env.set("FP_KEYBAG_URL", old);
31
- });
32
- it("from env", async () => {
33
- const old = sthis.env.get("FP_KEYBAG_URL");
34
- sthis.env.set("FP_KEYBAG_URL", url.toString());
35
- const kb = await rt.kb.getKeyBag(sthis);
36
- expect(kb.rt.url.toString()).toBe(url.toString());
37
- sthis.env.set("FP_KEYBAG_URL", old);
38
- });
39
-
40
- it("extract keyMaterial", async () => {
41
- const dkb = await rt.kb.getKeyBag(sthis);
42
- const old = sthis.env.get("FP_KEYBAG_URL");
43
- sthis.env.set("FP_KEYBAG_URL", BuildURI.from(dkb.rt.url).setParam("extractKey", "_deprecated_internal_api").toString());
44
- const kb = await rt.kb.getKeyBag(sthis);
45
- const key = kb.rt.crypto.randomBytes(kb.rt.keyLength);
46
- const keyStr = base58btc.encode(key);
47
- const res = await kb.setNamedKey("extract.test", keyStr);
48
- expect(res.isOk()).toBeTruthy();
49
- const gkb = await kb.getNamedExtractableKey("extract.test", true);
50
- expect(gkb.isOk()).toBeTruthy();
51
- expect(await gkb.Ok().extract()).toEqual({
52
- key,
53
- keyStr,
54
- });
55
- sthis.env.set("FP_KEYBAG_URL", old);
56
- await sthis.logger.Flush();
57
- expect(sthis.logCollector.Logs()).toEqual([
58
- {
59
- level: "warn",
60
- module: "KeyBag",
61
- msg: "extractKey is enabled via _deprecated_internal_api --- handle keys safely!!!",
62
- },
63
- ]);
64
- });
65
-
66
- it("simple add", async () => {
67
- const kb = await rt.kb.getKeyBag(sthis, {
68
- url: url.toString(),
69
- crypto: toCryptoRuntime({
70
- randomBytes: (size) => new Uint8Array(size).map((_, i) => i),
71
- }),
72
- });
73
- const name = "setkey" + Math.random();
74
- expect((await kb.getNamedKey(name, true)).isErr()).toBeTruthy();
75
-
76
- const key = base58btc.encode(kb.rt.crypto.randomBytes(kb.rt.keyLength));
77
- const res = await kb.setNamedKey(name, key);
78
- expect(res.isOk()).toBeTruthy();
79
- expect((await kb.getNamedKey(name, true)).Ok()).toEqual(res.Ok());
80
-
81
- const name2 = "implicit";
82
- const created = await kb.getNamedKey(name2);
83
- expect(created.isOk()).toBeTruthy();
84
-
85
- expect((await kb.getNamedKey(name2)).Ok()).toEqual(created.Ok());
86
-
87
- let diskBag: rt.kb.KeyItem;
88
- let diskBag2: rt.kb.KeyItem;
89
- const provider = await kb.rt.getBag();
90
- if (runtimeFn().isBrowser) {
91
- const p = provider as rt.kb.KeyBagProviderIndexDB;
92
- diskBag = await p._prepare().then((db) => db.get("bag", name));
93
- diskBag2 = await p._prepare().then((db) => db.get("bag", name2));
94
- } else {
95
- const p = provider as rt.kb.KeyBagProviderFile;
96
- const { sysFS } = await p._prepare(name);
97
-
98
- diskBag = await sysFS.readfile((await p._prepare(name)).fName).then((data) => {
99
- return JSON.parse(new TextDecoder().decode(data)) as rt.kb.KeyItem;
100
- });
101
- diskBag2 = await sysFS.readfile((await p._prepare(name2)).fName).then((data) => {
102
- return JSON.parse(new TextDecoder().decode(data)) as rt.kb.KeyItem;
103
- });
104
- }
105
- expect(await kb.toKeyWithFingerPrint(diskBag.key)).toEqual(res);
106
- expect(await kb.toKeyWithFingerPrint(diskBag2.key)).toEqual(created);
107
- const algo = {
108
- name: "AES-GCM",
109
- iv: kb.rt.crypto.randomBytes(12),
110
- tagLength: 128,
111
- };
112
- const data = kb.rt.crypto.randomBytes(122);
113
- expect(await kb.rt.crypto.encrypt(algo, res.Ok().key, data)).toEqual(await kb.rt.crypto.encrypt(algo, created.Ok().key, data));
114
- expect(await kb.rt.crypto.encrypt(algo, await kb.subtleKey(diskBag.key), data)).toEqual(
115
- await kb.rt.crypto.encrypt(algo, created.Ok().key, data),
116
- );
117
- expect(await kb.rt.crypto.encrypt(algo, await kb.subtleKey(diskBag2.key), data)).toEqual(
118
- await kb.rt.crypto.encrypt(algo, created.Ok().key, data),
119
- );
120
- });
121
- });
6
+ import { mockSuperThis } from "../helpers";
122
7
 
123
8
  describe("KeyedCryptoStore", () => {
124
9
  let kb: rt.kb.KeyBag;
125
- let logger: Logger;
10
+ // let logger: Logger;
126
11
  let baseUrl: URI;
127
12
  const sthis = mockSuperThis();
128
13
  beforeEach(async () => {
129
14
  await sthis.start();
130
- logger = MockLogger().logger;
131
- let kbUrl: URI;
132
- if (runtimeFn().isBrowser) {
133
- kbUrl = URI.from("indexdb://fp-keybag");
134
- baseUrl = URI.from("indexdb://fp-keyed-crypto-store");
15
+ // logger = MockLogger().logger;
16
+ // let kbUrl: URI;
17
+ // if (runtimeFn().isBrowser) {
18
+ // kbUrl = URI.from("indexdb://fp-keybag");
19
+ // baseUrl = URI.from("indexdb://fp-keyed-crypto-store");
20
+ // } else {
21
+ // kbUrl = URI.merge(`file://./dist/tests/key.bag`, sthis.env.get("FP_KEYBAG_URL"));
22
+ // baseUrl = URI.merge("file://./dist/tests/keyed-crypto-store", sthis.env.get("FP_STORAGE_URL"));
23
+ // }
24
+ // baseUrl = baseUrl.build().defParam(PARAM.NAME, "test").URI();
25
+
26
+ const envURL = sthis.env.get("FP_KEYBAG_URL");
27
+ if (envURL) {
28
+ baseUrl = bs.getDefaultURI(sthis, URI.from(envURL).protocol);
135
29
  } else {
136
- kbUrl = URI.merge(`file://./dist/tests/key.bag`, sthis.env.get("FP_KEYBAG_URL"));
137
- // baseUrl = URI.merge("file://./dist/tests/keyed-crypto-store", sthis.env.get("FP_STORAGE_URL"));
138
- baseUrl = URI.from(sthis.env.get("FP_STORAGE_URL"));
30
+ baseUrl = bs.getDefaultURI(sthis);
139
31
  }
32
+ baseUrl = baseUrl.build().setParam(PARAM.NAME, "test").URI();
140
33
  kb = await rt.kb.getKeyBag(sthis, {
141
- url: kbUrl,
34
+ // url: kbUrl,
142
35
  });
143
36
  });
144
37
  it("no crypto", async () => {
145
- const loader = {
146
- sthis,
147
- name: "test",
148
- ebOpts: {
149
- keyBag: {
150
- keyRuntime: kb.rt,
151
- },
152
- store: {
153
- stores: {
154
- base: baseUrl.build().setParam("storekey", "insecure"),
155
- },
156
- },
157
- },
158
- } as unknown as bs.Loadable;
159
- const strt = bs.toStoreRuntime({}, sthis);
160
-
161
- for (const pstore of [strt.makeDataStore(loader), strt.makeMetaStore(loader), strt.makeWALStore(loader)]) {
38
+ const strt = bs.toStoreRuntime(sthis);
39
+ const url = baseUrl.build().setParam(PARAM.STORE_KEY, "insecure").URI();
40
+
41
+ for (const pstore of [
42
+ strt.makeDataStore({ sthis, url, keybag: kb }),
43
+ strt.makeMetaStore({ sthis, url, keybag: kb }),
44
+ strt.makeWALStore({ sthis, url, keybag: kb }),
45
+ ]) {
162
46
  const store = await pstore;
163
47
  // await store.start();
164
48
  const kc = await store.keyedCrypto();
165
49
  expect(kc.constructor.name).toBe("noCrypto");
166
50
  // expect(kc.isEncrypting).toBe(false);
51
+ expect(kc.constructor.name).toBe("noCrypto");
52
+ // expect(kc.isEncrypting).toBe(false);
167
53
  }
168
54
  });
169
55
 
170
56
  it("create key", async () => {
171
- const loader = {
172
- sthis,
173
- name: "test",
174
- ebOpts: {
175
- keyBag: {
176
- keyRuntime: kb.rt,
177
- },
178
- store: {
179
- stores: {
180
- base: baseUrl,
181
- },
182
- },
183
- },
184
- } as unknown as bs.Loadable;
185
- const strt = bs.toStoreRuntime({}, sthis);
186
- for (const pstore of [strt.makeDataStore(loader), strt.makeMetaStore(loader), strt.makeWALStore(loader)]) {
187
- const store = await bs.ensureStart(await pstore, logger);
57
+ const strt = bs.toStoreRuntime(sthis);
58
+ for (const pstore of [
59
+ strt.makeDataStore({ sthis, url: baseUrl, keybag: kb }),
60
+ strt.makeMetaStore({ sthis, url: baseUrl, keybag: kb }),
61
+ strt.makeWALStore({ sthis, url: baseUrl, keybag: kb }),
62
+ ]) {
63
+ const store = await pstore; // await bs.ensureStart(await pstore, logger);
188
64
  const kc = await store.keyedCrypto();
189
65
  expect(kc.constructor.name).toBe("keyedCrypto");
190
66
  // expect(kc.isEncrypting).toBe(true);
191
- expect(store.url().getParam("storekey")).toBe(`@test:${store.url().getParam("store")}@`);
67
+ expect(store.url().getParam(PARAM.STORE_KEY)).toBe(`@test:${store.url().getParam(PARAM.STORE)}@`);
192
68
  }
193
69
  });
194
70
 
195
71
  it("key ref keybag", async () => {
196
72
  const key = base58btc.encode(kb.rt.crypto.randomBytes(kb.rt.keyLength));
197
73
  const genKey = await kb.setNamedKey("@heute@", key);
198
- const loader = {
199
- sthis,
200
- name: "test",
201
- ebOpts: {
202
- keyBag: {
203
- keyRuntime: kb.rt,
204
- },
205
- store: {
206
- stores: {
207
- base: baseUrl.build().setParam("storekey", "@heute@").URI(),
208
- },
209
- },
210
- },
211
- } as unknown as bs.Loadable;
212
- const strt = bs.toStoreRuntime({}, sthis);
213
- for (const pstore of [strt.makeDataStore(loader), strt.makeMetaStore(loader), strt.makeWALStore(loader)]) {
74
+ const url = baseUrl.build().setParam(PARAM.STORE_KEY, "@heute@").URI();
75
+ const strt = bs.toStoreRuntime(sthis);
76
+ for (const pstore of [
77
+ strt.makeDataStore({ sthis, url, keybag: kb }),
78
+ strt.makeMetaStore({ sthis, url, keybag: kb }),
79
+ strt.makeWALStore({ sthis, url, keybag: kb }),
80
+ ]) {
214
81
  const store = await pstore;
215
82
  // await store.start();
216
- expect(store.url().getParam("storekey")).toBe(`@heute@`);
83
+ expect(store.url().getParam(PARAM.STORE_KEY)).toBe(`@heute@`);
217
84
  const kc = await store.keyedCrypto();
218
85
  expect(kc.constructor.name).toBe("keyedCrypto");
219
86
  const testData = kb.rt.crypto.randomBytes(1024);
@@ -229,25 +96,17 @@ describe("KeyedCryptoStore", () => {
229
96
 
230
97
  it("key", async () => {
231
98
  const key = base58btc.encode(kb.rt.crypto.randomBytes(kb.rt.keyLength));
232
- const loader = {
233
- sthis,
234
- name: "test",
235
- ebOpts: {
236
- keyBag: {
237
- keyRuntime: kb.rt,
238
- },
239
- store: {
240
- stores: {
241
- base: BuildURI.from(baseUrl).setParam("storekey", key),
242
- },
243
- },
244
- },
245
- } as unknown as bs.Loadable;
246
- const strt = bs.toStoreRuntime({}, sthis);
247
- for (const pstore of [strt.makeDataStore(loader), strt.makeMetaStore(loader), strt.makeWALStore(loader)]) {
99
+ const strt = bs.toStoreRuntime(sthis);
100
+ const url = baseUrl.build().setParam(PARAM.STORE_KEY, key).URI();
101
+ for (const pstore of [
102
+ strt.makeDataStore({ sthis, url, keybag: kb }),
103
+ strt.makeMetaStore({ sthis, url, keybag: kb }),
104
+ strt.makeWALStore({ sthis, url, keybag: kb }),
105
+ ]) {
106
+ // for (const pstore of [strt.makeDataStore(loader), strt.makeMetaStore(loader), strt.makeWALStore(loader)]) {
248
107
  const store = await pstore;
249
108
  // await store.start();
250
- expect(store.url().getParam("storekey")).toBe(key);
109
+ expect(store.url().getParam(PARAM.STORE_KEY)).toBe(key);
251
110
  const kc = await store.keyedCrypto();
252
111
  expect(kc.constructor.name).toBe("keyedCrypto");
253
112
  const testData = kb.rt.crypto.randomBytes(1024);
@@ -266,14 +125,14 @@ describe("KeyedCrypto", () => {
266
125
  let keyStr: string;
267
126
  const sthis = mockSuperThis();
268
127
  beforeEach(async () => {
269
- let url: URI;
270
- if (runtimeFn().isBrowser) {
271
- url = URI.from("indexdb://fp-keybag");
272
- } else {
273
- url = URI.merge(`file://./dist/tests/key.bag`, sthis.env.get("FP_KEYBAG_URL"));
274
- }
128
+ // let url: URI;
129
+ // if (runtimeFn().isBrowser) {
130
+ // url = URI.from("indexdb://fp-keybag");
131
+ // } else {
132
+ // url = URI.merge(`file://./dist/tests/key.bag`, sthis.env.get("FP_KEYBAG_URL"));
133
+ // }
275
134
  kb = await rt.kb.getKeyBag(sthis, {
276
- url,
135
+ // url,
277
136
  });
278
137
  keyStr = base58btc.encode(kb.rt.crypto.randomBytes(kb.rt.keyLength));
279
138
  kycr = await rt.kc.keyedCryptoFactory(URI.from(`test://bla?storekey=${keyStr}`), kb, sthis);
@@ -307,26 +166,3 @@ describe("KeyedCrypto", () => {
307
166
  expect(blk).toEqual(blk2);
308
167
  });
309
168
  });
310
-
311
- // describe("KeyedCryptoStore RunLength", () => {
312
- // const logger = MockLogger().logger;
313
- // it("de/encode", () => {
314
- // for (const data of [
315
- // new Uint8Array(),
316
- // new Uint8Array(10).fill(10),
317
- // new Uint8Array(127).fill(127),
318
- // new Uint8Array(128).fill(128),
319
- // new Uint8Array(1024).fill(17),
320
- // ]) {
321
- // const res = rt.kc.encodeRunLength(data, logger);
322
- // expect(res.length).toBeLessThanOrEqual(data.length + (data.length > 127 ? 4 : 1));
323
- // for (let ofs = 0; ofs < 1024; ofs += 61) {
324
- // const ofsRes = new Uint8Array([...new Uint8Array(ofs).fill(23), ...res]);
325
- // const dec = rt.kc.decodeRunLength(ofsRes, ofs, logger);
326
- // expect(dec.data).toEqual(data);
327
- // expect(dec.data.length).toBe(data.length);
328
- // expect(dec.next).toBe(ofs + data.length + (data.length > 127 ? 4 : 1));
329
- // }
330
- // }
331
- // });
332
- // });
@@ -4,17 +4,18 @@ import { BlockView } from "multiformats";
4
4
  import { CID } from "multiformats/cid";
5
5
  import { MemoryBlockstore } from "@web3-storage/pail/block";
6
6
  import { CRDTMeta, IndexTransactionMeta, SuperThis, bs, rt } from "@fireproof/core";
7
- import { mockSuperThis } from "../helpers";
7
+ import { mockSuperThis, simpleBlockOpts } from "../helpers";
8
8
 
9
9
  class MyMemoryBlockStore extends bs.EncryptedBlockstore {
10
10
  readonly memblock = new MemoryBlockstore();
11
11
  loader: bs.Loader;
12
12
  constructor(sthis: SuperThis) {
13
- const ebOpts = {
14
- name: "MyMemoryBlockStore",
15
- };
13
+ const ebOpts = simpleBlockOpts(sthis, "MyMemoryBlockStore"); //, "MyMemoryBlockStore");
14
+ // const ebOpts = {
15
+ // name: "MyMemoryBlockStore",
16
+ // } as bs.BlockstoreOpts;
16
17
  super(sthis, ebOpts);
17
- this.loader = new bs.Loader("MyMemoryBlockStore", {}, sthis);
18
+ this.loader = new bs.Loader(sthis, ebOpts);
18
19
  }
19
20
  ready(): Promise<void> {
20
21
  return Promise.resolve();
@@ -63,7 +64,10 @@ describe("basic Loader simple", function () {
63
64
  await sthis.start();
64
65
  const mockM = new MyMemoryBlockStore(sthis);
65
66
  t = new bs.CarTransaction(mockM as bs.EncryptedBlockstore);
66
- loader = new bs.Loader(testDbName, { public: true }, sthis);
67
+ loader = new bs.Loader(sthis, {
68
+ ...simpleBlockOpts(sthis, testDbName),
69
+ public: true,
70
+ });
67
71
  await loader.ready();
68
72
  block = await rt.mf.block.encode({
69
73
  value: { hello: "world" },
@@ -100,17 +104,20 @@ describe("basic Loader with two commits", function () {
100
104
  let carCid0: bs.CarGroup;
101
105
 
102
106
  const sthis = mockSuperThis();
103
-
104
- afterEach(async function () {
107
+ afterEach(async () => {
105
108
  await loader.close();
106
109
  await loader.destroy();
107
110
  });
108
111
 
109
- beforeEach(async function () {
112
+ beforeEach(async () => {
110
113
  await sthis.start();
111
114
  const mockM = new MyMemoryBlockStore(sthis);
112
115
  t = new bs.CarTransaction(mockM);
113
- loader = new bs.Loader("test-loader-two-commit", { public: true }, sthis);
116
+ loader = new bs.Loader(sthis, {
117
+ ...simpleBlockOpts(sthis, "test-loader-two-commit"),
118
+ public: true,
119
+ });
120
+
114
121
  block = await rt.mf.block.encode({
115
122
  value: { hello: "world" },
116
123
  hasher,
@@ -211,8 +218,9 @@ describe("basic Loader with index commits", function () {
211
218
  beforeEach(async function () {
212
219
  const name = "test-loader-index" + Math.random();
213
220
  await sthis.start();
221
+ await sthis.start();
214
222
  // t = new CarTransaction()
215
- ib = new bs.EncryptedBlockstore(sthis, { name });
223
+ ib = new bs.EncryptedBlockstore(sthis, simpleBlockOpts(sthis, name));
216
224
  block = await rt.mf.block.encode({
217
225
  value: { hello: "world" },
218
226
  hasher,