@fireproof/core 0.19.101 → 0.19.102

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-3EB3ENHT.js → chunk-OFGPKRCM.js} +25 -54
  2. package/chunk-OFGPKRCM.js.map +1 -0
  3. package/chunk-WS3YRPIA.js +75 -0
  4. package/chunk-WS3YRPIA.js.map +1 -0
  5. package/{gateway-GK5QZ6KP.js → gateway-5FCWPX5W.js} +12 -13
  6. package/gateway-5FCWPX5W.js.map +1 -0
  7. package/{gateway-TQTGDRCN.js → gateway-H7UD6TNB.js} +8 -9
  8. package/gateway-H7UD6TNB.js.map +1 -0
  9. package/index.cjs +1571 -1992
  10. package/index.cjs.map +1 -1
  11. package/index.d.cts +117 -257
  12. package/index.d.ts +117 -257
  13. package/index.global.js +12280 -12741
  14. package/index.global.js.map +1 -1
  15. package/index.js +1463 -1790
  16. package/index.js.map +1 -1
  17. package/{key-bag-file-VOSSK46F.js → key-bag-file-WADZBHYG.js} +3 -4
  18. package/{key-bag-file-VOSSK46F.js.map → key-bag-file-WADZBHYG.js.map} +1 -1
  19. package/{key-bag-indexdb-AXTQOSMC.js → key-bag-indexdb-PGVAI3FJ.js} +3 -4
  20. package/{key-bag-indexdb-AXTQOSMC.js.map → key-bag-indexdb-PGVAI3FJ.js.map} +1 -1
  21. package/mem-filesystem-YPPJV7Q2.js +41 -0
  22. package/mem-filesystem-YPPJV7Q2.js.map +1 -0
  23. package/metafile-cjs.json +1 -1
  24. package/metafile-esm.json +1 -1
  25. package/metafile-iife.json +1 -1
  26. package/{node-filesystem-CFRXFSO7.js → node-filesystem-INX4ZTHE.js} +9 -6
  27. package/node-filesystem-INX4ZTHE.js.map +1 -0
  28. package/package.json +1 -1
  29. package/tests/blockstore/keyed-crypto.test.ts +227 -63
  30. package/tests/blockstore/loader.test.ts +11 -19
  31. package/tests/blockstore/store.test.ts +19 -23
  32. package/tests/blockstore/transaction.test.ts +12 -12
  33. package/tests/fireproof/all-gateway.test.ts +193 -201
  34. package/tests/fireproof/cars/bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i.ts +316 -324
  35. package/tests/fireproof/config.test.ts +172 -0
  36. package/tests/fireproof/crdt.test.ts +16 -67
  37. package/tests/fireproof/database.test.ts +21 -183
  38. package/tests/fireproof/fireproof.test.ts +74 -83
  39. package/tests/fireproof/hello.test.ts +14 -18
  40. package/tests/fireproof/indexer.test.ts +43 -53
  41. package/tests/fireproof/utils.test.ts +6 -18
  42. package/tests/helpers.ts +9 -27
  43. package/tests/react/useFireproof.test.tsx +1 -1
  44. package/{utils-STA2C35G.js → utils-QO2HIWGI.js} +3 -4
  45. package/chunk-3EB3ENHT.js.map +0 -1
  46. package/chunk-HQ7D3PEU.js +0 -61
  47. package/chunk-HQ7D3PEU.js.map +0 -1
  48. package/chunk-PZ5AY32C.js +0 -10
  49. package/deno-filesystem-Q2IJ7YDR.js +0 -57
  50. package/deno-filesystem-Q2IJ7YDR.js.map +0 -1
  51. package/gateway-GK5QZ6KP.js.map +0 -1
  52. package/gateway-TQTGDRCN.js.map +0 -1
  53. package/key-bag-memory-LWE6ARPX.js +0 -29
  54. package/key-bag-memory-LWE6ARPX.js.map +0 -1
  55. package/node-filesystem-CFRXFSO7.js.map +0 -1
  56. package/tests/blockstore/keyed-crypto-indexdb-file.test.ts +0 -129
  57. package/tests/gateway/file/loader-config.test.ts +0 -303
  58. package/tests/gateway/indexdb/loader-config.test.ts +0 -75
  59. package/utils-STA2C35G.js.map +0 -1
  60. /package/tests/fireproof/{fireproof.fixture.ts → fireproof.test.fixture.ts} +0 -0
  61. /package/{chunk-PZ5AY32C.js.map → utils-QO2HIWGI.js.map} +0 -0
@@ -1,13 +1,13 @@
1
1
  import {
2
2
  toArrayBuffer
3
- } from "./chunk-HQ7D3PEU.js";
4
- import "./chunk-3EB3ENHT.js";
5
- import "./chunk-PZ5AY32C.js";
3
+ } from "./chunk-WS3YRPIA.js";
4
+ import "./chunk-OFGPKRCM.js";
6
5
 
7
6
  // 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("fs/promises");
10
+ this.fs = await import("node:fs/promises");
11
11
  return this;
12
12
  }
13
13
  async mkdir(path, options) {
@@ -27,16 +27,19 @@ 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
+ }
36
39
  return this.fs?.writeFile(path, Buffer.from(data));
37
40
  }
38
41
  };
39
42
  export {
40
43
  NodeFileSystem
41
44
  };
42
- //# sourceMappingURL=node-filesystem-CFRXFSO7.js.map
45
+ //# sourceMappingURL=node-filesystem-INX4ZTHE.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/runtime/gateways/file/node-filesystem.ts"],"sourcesContent":["import type { PathLike, MakeDirectoryOptions, Stats, ObjectEncodingOptions } from \"node:fs\";\nimport type { mkdir, readdir, rm, copyFile, readFile, stat, unlink, writeFile } from \"node:fs/promises\";\nimport { toArrayBuffer } from \"./utils.js\";\nimport { SysFileSystem } from \"../../../types.js\";\nimport { runtimeFn } from \"@adviser/cement\";\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(\"node: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<Stats> {\n return this.fs?.stat(path) as Promise<Stats>;\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 if (runtimeFn().isDeno) {\n return this.fs?.writeFile(path, data);\n }\n return this.fs?.writeFile(path, Buffer.from(data));\n }\n}\n\n// import { type NodeMap, join } from \"../../sys-container.js\";\n// import type { ObjectEncodingOptions, PathLike } from \"fs\";\n// import * as fs from \"fs/promises\";\n// import * as path from \"path\";\n// import * as os from \"os\";\n// import * as url from \"url\";\n// import { toArrayBuffer } from \"./utils.js\";\n\n// export async function createNodeSysContainer(): Promise<NodeMap> {\n// // const nodePath = \"node:path\";\n// // const nodeOS = \"node:os\";\n// // const nodeURL = \"node:url\";\n// // const nodeFS = \"node:fs\";\n// // const fs = (await import(\"node:fs\")).promises;\n// // const assert = \"assert\";\n// // const path = await import(\"node:path\");\n// return {\n// state: \"node\",\n// ...path,\n// // ...(await import(\"node:os\")),\n// // ...(await import(\"node:url\")),\n// ...os,\n// ...url,\n// ...fs,\n// join,\n// stat: fs.stat as NodeMap[\"stat\"],\n// readdir: fs.readdir as NodeMap[\"readdir\"],\n// readfile: async (path: PathLike, options?: ObjectEncodingOptions): Promise<Uint8Array> => {\n// const rs = await fs.readFile(path, options);\n// return toArrayBuffer(rs);\n// },\n// writefile: fs.writeFile as NodeMap[\"writefile\"],\n// };\n// }\n"],"mappings":";;;;;;AAIA,SAAS,iBAAiB;AAEnB,IAAM,iBAAN,MAA8C;AAAA,EAYnD,MAAM,QAAgC;AACpC,SAAK,KAAK,MAAM,OAAO,kBAAkB;AACzC,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,MAAgC;AACnC,WAAO,KAAK,IAAI,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,QAAI,UAAU,EAAE,QAAQ;AACtB,aAAO,KAAK,IAAI,UAAU,MAAM,IAAI;AAAA,IACtC;AACA,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.101",
3
+ "version": "0.19.102",
4
4
  "description": "Live database for the web.",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -1,86 +1,219 @@
1
- import { bs, PARAM, rt } from "@fireproof/core";
2
- import { URI } from "@adviser/cement";
1
+ import { bs, rt } from "@fireproof/core";
2
+ import { BuildURI, Logger, MockLogger, runtimeFn, toCryptoRuntime, 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 } from "../helpers";
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
+ });
7
122
 
8
123
  describe("KeyedCryptoStore", () => {
9
124
  let kb: rt.kb.KeyBag;
10
- // let logger: Logger;
125
+ let logger: Logger;
11
126
  let baseUrl: URI;
12
127
  const sthis = mockSuperThis();
13
128
  beforeEach(async () => {
14
129
  await sthis.start();
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);
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");
29
135
  } else {
30
- baseUrl = bs.getDefaultURI(sthis);
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"));
31
139
  }
32
- baseUrl = baseUrl.build().setParam(PARAM.NAME, "test").URI();
33
140
  kb = await rt.kb.getKeyBag(sthis, {
34
- // url: kbUrl,
141
+ url: kbUrl,
35
142
  });
36
143
  });
37
144
  it("no crypto", async () => {
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
- ]) {
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)]) {
46
162
  const store = await pstore;
47
163
  // await store.start();
48
164
  const kc = await store.keyedCrypto();
49
165
  expect(kc.constructor.name).toBe("noCrypto");
50
166
  // expect(kc.isEncrypting).toBe(false);
51
- expect(kc.constructor.name).toBe("noCrypto");
52
- // expect(kc.isEncrypting).toBe(false);
53
167
  }
54
168
  });
55
169
 
56
170
  it("create key", async () => {
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);
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);
64
188
  const kc = await store.keyedCrypto();
65
189
  expect(kc.constructor.name).toBe("keyedCrypto");
66
190
  // expect(kc.isEncrypting).toBe(true);
67
- expect(store.url().getParam(PARAM.STORE_KEY)).toBe(`@test:${store.url().getParam(PARAM.STORE)}@`);
191
+ expect(store.url().getParam("storekey")).toBe(`@test:${store.url().getParam("store")}@`);
68
192
  }
69
193
  });
70
194
 
71
195
  it("key ref keybag", async () => {
72
196
  const key = base58btc.encode(kb.rt.crypto.randomBytes(kb.rt.keyLength));
73
197
  const genKey = await kb.setNamedKey("@heute@", key);
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
- ]) {
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)]) {
81
214
  const store = await pstore;
82
215
  // await store.start();
83
- expect(store.url().getParam(PARAM.STORE_KEY)).toBe(`@heute@`);
216
+ expect(store.url().getParam("storekey")).toBe(`@heute@`);
84
217
  const kc = await store.keyedCrypto();
85
218
  expect(kc.constructor.name).toBe("keyedCrypto");
86
219
  const testData = kb.rt.crypto.randomBytes(1024);
@@ -96,17 +229,25 @@ describe("KeyedCryptoStore", () => {
96
229
 
97
230
  it("key", async () => {
98
231
  const key = base58btc.encode(kb.rt.crypto.randomBytes(kb.rt.keyLength));
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)]) {
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)]) {
107
248
  const store = await pstore;
108
249
  // await store.start();
109
- expect(store.url().getParam(PARAM.STORE_KEY)).toBe(key);
250
+ expect(store.url().getParam("storekey")).toBe(key);
110
251
  const kc = await store.keyedCrypto();
111
252
  expect(kc.constructor.name).toBe("keyedCrypto");
112
253
  const testData = kb.rt.crypto.randomBytes(1024);
@@ -125,14 +266,14 @@ describe("KeyedCrypto", () => {
125
266
  let keyStr: string;
126
267
  const sthis = mockSuperThis();
127
268
  beforeEach(async () => {
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
- // }
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
+ }
134
275
  kb = await rt.kb.getKeyBag(sthis, {
135
- // url,
276
+ url,
136
277
  });
137
278
  keyStr = base58btc.encode(kb.rt.crypto.randomBytes(kb.rt.keyLength));
138
279
  kycr = await rt.kc.keyedCryptoFactory(URI.from(`test://bla?storekey=${keyStr}`), kb, sthis);
@@ -166,3 +307,26 @@ describe("KeyedCrypto", () => {
166
307
  expect(blk).toEqual(blk2);
167
308
  });
168
309
  });
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,18 +4,17 @@ 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, simpleBlockOpts } from "../helpers";
7
+ import { mockSuperThis } 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 = simpleBlockOpts(sthis, "MyMemoryBlockStore"); //, "MyMemoryBlockStore");
14
- // const ebOpts = {
15
- // name: "MyMemoryBlockStore",
16
- // } as bs.BlockstoreOpts;
13
+ const ebOpts = {
14
+ name: "MyMemoryBlockStore",
15
+ };
17
16
  super(sthis, ebOpts);
18
- this.loader = new bs.Loader(sthis, ebOpts);
17
+ this.loader = new bs.Loader("MyMemoryBlockStore", {}, sthis);
19
18
  }
20
19
  ready(): Promise<void> {
21
20
  return Promise.resolve();
@@ -64,10 +63,7 @@ describe("basic Loader simple", function () {
64
63
  await sthis.start();
65
64
  const mockM = new MyMemoryBlockStore(sthis);
66
65
  t = new bs.CarTransaction(mockM as bs.EncryptedBlockstore);
67
- loader = new bs.Loader(sthis, {
68
- ...simpleBlockOpts(sthis, testDbName),
69
- public: true,
70
- });
66
+ loader = new bs.Loader(testDbName, { public: true }, sthis);
71
67
  await loader.ready();
72
68
  block = await rt.mf.block.encode({
73
69
  value: { hello: "world" },
@@ -104,20 +100,17 @@ describe("basic Loader with two commits", function () {
104
100
  let carCid0: bs.CarGroup;
105
101
 
106
102
  const sthis = mockSuperThis();
107
- afterEach(async () => {
103
+
104
+ afterEach(async function () {
108
105
  await loader.close();
109
106
  await loader.destroy();
110
107
  });
111
108
 
112
- beforeEach(async () => {
109
+ beforeEach(async function () {
113
110
  await sthis.start();
114
111
  const mockM = new MyMemoryBlockStore(sthis);
115
112
  t = new bs.CarTransaction(mockM);
116
- loader = new bs.Loader(sthis, {
117
- ...simpleBlockOpts(sthis, "test-loader-two-commit"),
118
- public: true,
119
- });
120
-
113
+ loader = new bs.Loader("test-loader-two-commit", { public: true }, sthis);
121
114
  block = await rt.mf.block.encode({
122
115
  value: { hello: "world" },
123
116
  hasher,
@@ -218,9 +211,8 @@ describe("basic Loader with index commits", function () {
218
211
  beforeEach(async function () {
219
212
  const name = "test-loader-index" + Math.random();
220
213
  await sthis.start();
221
- await sthis.start();
222
214
  // t = new CarTransaction()
223
- ib = new bs.EncryptedBlockstore(sthis, simpleBlockOpts(sthis, name));
215
+ ib = new bs.EncryptedBlockstore(sthis, { name });
224
216
  block = await rt.mf.block.encode({
225
217
  value: { hello: "world" },
226
218
  hasher,
@@ -1,18 +1,21 @@
1
1
  import { CID } from "multiformats";
2
- import { bs, NotFoundError, SuperThis, rt } from "@fireproof/core";
3
- import { mockSuperThis, noopUrl } from "../helpers";
2
+ import { bs, NotFoundError, SuperThis } from "@fireproof/core";
3
+ import { mockSuperThis } from "../helpers";
4
+
5
+ const decoder = new TextDecoder("utf-8");
4
6
 
5
7
  function runtime(sthis: SuperThis) {
6
- return bs.toStoreRuntime(sthis);
8
+ return bs.toStoreRuntime({}, sthis);
7
9
  }
8
10
 
9
- async function mockLoader(sthis: SuperThis, name?: string): Promise<bs.StoreFactoryItem> {
10
- const url = noopUrl(name);
11
+ function mockLoader(sthis: SuperThis, name: string): bs.Loadable {
11
12
  return {
12
13
  sthis,
13
- url: url,
14
- keybag: await rt.kb.getKeyBag(sthis),
15
- };
14
+ name,
15
+ ebOpts: {
16
+ store: {},
17
+ },
18
+ } as bs.Loadable;
16
19
  }
17
20
 
18
21
  describe("DataStore", function () {
@@ -27,7 +30,7 @@ describe("DataStore", function () {
27
30
 
28
31
  beforeEach(async () => {
29
32
  await sthis.start();
30
- store = await runtime(sthis).makeDataStore(await mockLoader(sthis));
33
+ store = await runtime(sthis).makeDataStore(mockLoader(sthis, "test"));
31
34
  await store.start();
32
35
  raw = await bs.testStoreFactory(store.url(), sthis);
33
36
  });
@@ -43,7 +46,7 @@ describe("DataStore", function () {
43
46
  };
44
47
  await store.save(car);
45
48
  const data = await raw.get(store.url(), car.cid.toString());
46
- expect(sthis.txt.decode(data)).toEqual(sthis.txt.decode(car.bytes));
49
+ expect(decoder.decode(data)).toEqual(decoder.decode(car.bytes));
47
50
  });
48
51
  });
49
52
 
@@ -51,6 +54,7 @@ describe("DataStore with a saved car", function () {
51
54
  let store: bs.DataStore;
52
55
  let raw: bs.TestGateway;
53
56
  let car: bs.AnyBlock;
57
+
54
58
  const sthis = mockSuperThis();
55
59
 
56
60
  afterEach(async () => {
@@ -60,10 +64,9 @@ describe("DataStore with a saved car", function () {
60
64
 
61
65
  beforeEach(async function () {
62
66
  await sthis.start();
63
- store = await runtime(sthis).makeDataStore(await mockLoader(sthis, "test2"));
67
+ store = await runtime(sthis).makeDataStore(mockLoader(sthis, "test2"));
64
68
  await store.start();
65
69
  raw = await bs.testStoreFactory(store.url(), sthis);
66
- raw = await bs.testStoreFactory(store.url(), sthis);
67
70
  car = {
68
71
  cid: "cid" as unknown as CID,
69
72
  bytes: new Uint8Array([55, 56, 57, 80]),
@@ -73,7 +76,7 @@ describe("DataStore with a saved car", function () {
73
76
 
74
77
  it("should have a car", async function () {
75
78
  const data = await raw.get(store.url(), car.cid.toString());
76
- expect(sthis.txt.decode(data)).toEqual(sthis.txt.decode(car.bytes));
79
+ expect(decoder.decode(data)).toEqual(decoder.decode(car.bytes));
77
80
  });
78
81
 
79
82
  it("should load a car", async function () {
@@ -103,7 +106,7 @@ describe("MetaStore", function () {
103
106
 
104
107
  beforeEach(async function () {
105
108
  await sthis.start();
106
- store = await runtime(sthis).makeMetaStore(await mockLoader(sthis, "test"));
109
+ store = await runtime(sthis).makeMetaStore(mockLoader(sthis, "test"));
107
110
  await store.start();
108
111
  raw = await bs.testStoreFactory(store.url(), sthis);
109
112
  });
@@ -133,7 +136,6 @@ describe("MetaStore with a saved header", function () {
133
136
  let raw: bs.TestGateway;
134
137
  let cid: CID;
135
138
  const sthis = mockSuperThis();
136
- // let onload: bs.DbMeta[];
137
139
 
138
140
  afterEach(async () => {
139
141
  await store.close();
@@ -142,22 +144,16 @@ describe("MetaStore with a saved header", function () {
142
144
 
143
145
  beforeEach(async function () {
144
146
  await sthis.start();
145
- store = await runtime(sthis).makeMetaStore(await mockLoader(sthis, "test-saved-header"));
147
+ store = await runtime(sthis).makeMetaStore(mockLoader(sthis, "test-saved-header"));
146
148
  await store.start();
147
149
  raw = await bs.testStoreFactory(store.url(), sthis);
148
150
  cid = CID.parse("bafybeia4luuns6dgymy5kau5rm7r4qzrrzg6cglpzpogussprpy42cmcn4");
149
151
  await store.save({ cars: [cid] /*, key: undefined */ });
150
152
  });
151
153
 
152
- // it("should load", async function () {
153
- // expect(onload).toBeTruthy();
154
- // expect(onload?.length).toEqual(1);
155
- // expect(onload?.[0].cars.toString()).toEqual(cid.toString());
156
- // });
157
-
158
154
  it("should have a header", async function () {
159
155
  const bytes = await raw.get(store.url(), "main");
160
- const data = sthis.txt.decode(bytes);
156
+ const data = decoder.decode(bytes);
161
157
  expect(data).toMatch(/parents/);
162
158
  const header = JSON.parse(data)[0];
163
159
  expect(header).toBeDefined();
@@ -1,16 +1,16 @@
1
1
  import { CID } from "multiformats";
2
2
  import { bs, SuperThis } from "@fireproof/core";
3
- import { mockSuperThis, simpleBlockOpts } from "../helpers";
3
+ import { mockSuperThis } from "../helpers";
4
4
 
5
5
  describe("Fresh TransactionBlockstore", function () {
6
6
  let blocks: bs.BaseBlockstore;
7
7
  const sthis = mockSuperThis();
8
8
  beforeEach(function () {
9
- blocks = new bs.BaseBlockstore(simpleBlockOpts(sthis));
9
+ blocks = new bs.BaseBlockstore();
10
+ });
11
+ it("should not have a name", function () {
12
+ expect(blocks.name).toBeFalsy();
10
13
  });
11
- // it("should not have a name", function () {
12
- // expect(blocks.name).toBeFalsy();
13
- // });
14
14
  it("should not have a loader", function () {
15
15
  expect(blocks.loader).toBeFalsy();
16
16
  });
@@ -34,12 +34,12 @@ describe("Fresh TransactionBlockstore", function () {
34
34
  describe("TransactionBlockstore with name", function () {
35
35
  let blocks: bs.EncryptedBlockstore;
36
36
  const sthis = mockSuperThis();
37
- beforeEach(async function () {
38
- blocks = new bs.EncryptedBlockstore(sthis, simpleBlockOpts(sthis));
37
+ beforeEach(function () {
38
+ blocks = new bs.EncryptedBlockstore(sthis, { name: "test" });
39
+ });
40
+ it("should have a name", function () {
41
+ expect(blocks.name).toEqual("test");
39
42
  });
40
- // it("should have a name", function () {
41
- // expect(blocks.name).toEqual("test");
42
- // });
43
43
  it("should have a loader", function () {
44
44
  expect(blocks.loader).toBeTruthy();
45
45
  });
@@ -59,7 +59,7 @@ describe("A transaction", function () {
59
59
  let blocks: bs.EncryptedBlockstore;
60
60
  const sthis = mockSuperThis();
61
61
  beforeEach(async function () {
62
- blocks = new bs.EncryptedBlockstore(sthis, simpleBlockOpts(sthis, "test"));
62
+ blocks = new bs.EncryptedBlockstore(sthis, { name: "test" });
63
63
  tblocks = new bs.CarTransaction(blocks);
64
64
  blocks.transactions.add(tblocks);
65
65
  });
@@ -89,7 +89,7 @@ describe("TransactionBlockstore with a completed transaction", function () {
89
89
  cid = CID.parse("bafybeia4luuns6dgymy5kau5rm7r4qzrrzg6cglpzpogussprpy42cmcn4");
90
90
  cid2 = CID.parse("bafybeibgouhn5ktecpjuovt52zamzvm4dlve5ak7x6d5smms3itkhplnhm");
91
91
 
92
- blocks = new bs.BaseBlockstore(simpleBlockOpts(sthis));
92
+ blocks = new bs.BaseBlockstore();
93
93
  await blocks.transaction(async (tblocks) => {
94
94
  await tblocks.put(cid, asUInt8Array("value", sthis));
95
95
  await tblocks.put(cid, asUInt8Array("value", sthis));