@fireproof/core-test 0.22.0-keybag → 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 (101) hide show
  1. package/LICENSE.md +197 -228
  2. package/README.md +269 -0
  3. package/blockstore/interceptor-gateway.test.js.map +1 -1
  4. package/blockstore/keyed-crypto-indexeddb-file.test.js +1 -0
  5. package/blockstore/keyed-crypto-indexeddb-file.test.js.map +1 -1
  6. package/blockstore/keyed-crypto.test.js +1 -0
  7. package/blockstore/keyed-crypto.test.js.map +1 -1
  8. package/blockstore/loader.test.js.map +1 -1
  9. package/blockstore/standalone.test.js.map +1 -1
  10. package/blockstore/store.test.js.map +1 -1
  11. package/blockstore/transaction.test.js.map +1 -1
  12. package/fireproof/all-gateway.test.js.map +1 -1
  13. package/fireproof/attachable.test.js.map +1 -1
  14. package/fireproof/cars/bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i.js.map +1 -1
  15. package/fireproof/charwise-boolean.test.js.map +1 -1
  16. package/fireproof/compact-strategy.test.js.map +1 -1
  17. package/fireproof/concurrent.test.js +1 -1
  18. package/fireproof/concurrent.test.js.map +1 -1
  19. package/fireproof/crdt.test.js +1 -0
  20. package/fireproof/crdt.test.js.map +1 -1
  21. package/fireproof/database.test.js.map +1 -1
  22. package/fireproof/deleted-docs-handling.test.js.map +1 -1
  23. package/fireproof/fireproof.test.fixture.d.ts +3 -3
  24. package/fireproof/fireproof.test.fixture.js.map +1 -1
  25. package/fireproof/fireproof.test.js +1 -0
  26. package/fireproof/fireproof.test.js.map +1 -1
  27. package/fireproof/hello.test.js.map +1 -1
  28. package/fireproof/indexer.test.js.map +1 -1
  29. package/fireproof/multiple-ledger.test.js.map +1 -1
  30. package/fireproof/query-docs.test.js.map +1 -1
  31. package/fireproof/query-limit-issue.test.js.map +1 -1
  32. package/fireproof/query-property-inconsistency.test.js.map +1 -1
  33. package/fireproof/query-result-properties.test.js.map +1 -1
  34. package/fireproof/stable-cid.test.js.map +1 -1
  35. package/fireproof/utils.test.js.map +1 -1
  36. package/gateway/file/loader-config.test.js +2 -2
  37. package/gateway/file/loader-config.test.js.map +1 -1
  38. package/gateway/indexeddb/create-db-on-write.test.d.ts +1 -0
  39. package/gateway/indexeddb/create-db-on-write.test.js +145 -0
  40. package/gateway/indexeddb/create-db-on-write.test.js.map +1 -0
  41. package/gateway/indexeddb/loader-config.test.js.map +1 -1
  42. package/global-setup.js.map +1 -1
  43. package/helpers.js.map +1 -1
  44. package/package.json +21 -22
  45. package/protocols/cloud/msger.test.js.map +1 -1
  46. package/runtime/fp-envelope-serialize.test.js.map +1 -1
  47. package/runtime/key-bag.test.js.map +1 -1
  48. package/runtime/meta-key-hack.test.js.map +1 -1
  49. package/setup.file.js.map +1 -1
  50. package/setup.indexeddb.js.map +1 -1
  51. package/setup.memory.js.map +1 -1
  52. package/vitest.config.js.map +1 -1
  53. package/vitest.file.config.js.map +1 -1
  54. package/vitest.indexeddb.config.js.map +1 -1
  55. package/vitest.memory.config.js.map +1 -1
  56. package/blockstore/fp-envelope.test.ts-off +0 -65
  57. package/blockstore/fragment-gateway.test.ts-off +0 -106
  58. package/blockstore/interceptor-gateway.test.ts +0 -259
  59. package/blockstore/keyed-crypto-indexeddb-file.test.ts +0 -134
  60. package/blockstore/keyed-crypto.test.ts +0 -381
  61. package/blockstore/loader.test.ts +0 -313
  62. package/blockstore/standalone.test.ts +0 -156
  63. package/blockstore/store.test.ts +0 -199
  64. package/blockstore/transaction.test.ts +0 -132
  65. package/fireproof/all-gateway.test.ts +0 -477
  66. package/fireproof/attachable.test.ts +0 -677
  67. package/fireproof/cars/bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i.car +0 -0
  68. package/fireproof/cars/bafkreidxwt2nhvbl4fnqfw3ctlt6zbrir4kqwmjo5im6rf4q5si27kgo2i.ts +0 -324
  69. package/fireproof/charwise-boolean.test.ts +0 -68
  70. package/fireproof/compact-strategy.test.ts +0 -40
  71. package/fireproof/concurrent.test.ts +0 -38
  72. package/fireproof/crdt.test.ts +0 -572
  73. package/fireproof/database.test.ts +0 -772
  74. package/fireproof/deleted-docs-handling.test.ts +0 -112
  75. package/fireproof/fireproof.test.fixture.ts +0 -133
  76. package/fireproof/fireproof.test.ts +0 -767
  77. package/fireproof/hello.test.ts +0 -75
  78. package/fireproof/indexer.test.ts +0 -459
  79. package/fireproof/multiple-ledger.test.ts +0 -67
  80. package/fireproof/query-docs.test.ts +0 -117
  81. package/fireproof/query-limit-issue.test.ts +0 -147
  82. package/fireproof/query-property-inconsistency.test.ts +0 -90
  83. package/fireproof/query-result-properties.test.ts +0 -43
  84. package/fireproof/stable-cid.test.ts +0 -72
  85. package/fireproof/utils.test.ts +0 -137
  86. package/gateway/file/loader-config.test.ts +0 -309
  87. package/gateway/indexeddb/loader-config.test.ts +0 -80
  88. package/global-setup.ts +0 -11
  89. package/helpers.ts +0 -177
  90. package/protocols/cloud/msger.test.ts +0 -559
  91. package/runtime/fp-envelope-serialize.test.ts +0 -266
  92. package/runtime/key-bag.test.ts +0 -243
  93. package/runtime/meta-key-hack.test.ts +0 -103
  94. package/setup.file.ts +0 -1
  95. package/setup.indexeddb.ts +0 -0
  96. package/setup.memory.ts +0 -2
  97. package/tsconfig.json +0 -18
  98. package/vitest.config.ts +0 -8
  99. package/vitest.file.config.ts +0 -11
  100. package/vitest.indexeddb.config.ts +0 -34
  101. package/vitest.memory.config.ts +0 -24
@@ -1 +1 @@
1
- {"version":3,"file":"vitest.indexeddb.config.js","sourceRoot":"","sources":["vitest.indexeddb.config.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAE7C,eAAe,YAAY,CAAC;IAC1B,IAAI,EAAE;QACJ,IAAI,EAAE,WAAW;QACjB,OAAO,EAAE,CAAC,SAAS,EAAE,iBAAiB,EAAE,aAAa,EAAE,cAAc,CAAC;QACtE,OAAO,EAAE,CAAC,0BAA0B,CAAC;QACrC,OAAO,EAAE;YACP,OAAO,EAAE,IAAI;YACb,QAAQ,EAAE,IAAI;YACd,QAAQ,EAAE,YAAY;YAGtB,SAAS,EAAE;gBACT;oBACE,OAAO,EAAE,UAAU;iBAEpB;aACF;SAQF;QACD,OAAO,EAAE,IAAI;QACb,UAAU,EAAE,sBAAsB;KACnC;CACF,CAAC,CAAC"}
1
+ {"version":3,"file":"vitest.indexeddb.config.js","sourceRoot":"","sources":["../jsr/vitest.indexeddb.config.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAE7C,eAAe,YAAY,CAAC;IAC1B,IAAI,EAAE;QACJ,IAAI,EAAE,WAAW;QACjB,OAAO,EAAE,CAAC,SAAS,EAAE,iBAAiB,EAAE,aAAa,EAAE,cAAc,CAAC;QACtE,OAAO,EAAE,CAAC,0BAA0B,CAAC;QACrC,OAAO,EAAE;YACP,OAAO,EAAE,IAAI;YACb,QAAQ,EAAE,IAAI;YACd,QAAQ,EAAE,YAAY;YAGtB,SAAS,EAAE;gBACT;oBACE,OAAO,EAAE,UAAU;iBAEpB;aACF;SAQF;QACD,OAAO,EAAE,IAAI;QACb,UAAU,EAAE,sBAAsB;KACnC;CACF,CAAC,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"vitest.memory.config.js","sourceRoot":"","sources":["vitest.memory.config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAE7C,eAAe,YAAY,CAAC;IAC1B,IAAI,EAAE;QACJ,IAAI,EAAE,QAAQ;QACd,OAAO,EAAE;YACP,SAAS;YACT,iBAAiB;YACjB,UAAU;YACV,aAAa;YACb,eAAe;YACf,gBAAgB;YAChB,mBAAmB;YACnB,cAAc;YACd,gDAAgD;SACjD;QACD,OAAO,EAAE,CAAC,0BAA0B,CAAC;QACrC,QAAQ,EAAE;YACR,OAAO,EAAE,CAAC,aAAa,EAAE,eAAe,EAAE,gBAAgB,CAAC;SAC5D;QACD,OAAO,EAAE,IAAI;QACb,UAAU,EAAE,mBAAmB;KAChC;CACF,CAAC,CAAC"}
1
+ {"version":3,"file":"vitest.memory.config.js","sourceRoot":"","sources":["../jsr/vitest.memory.config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAE7C,eAAe,YAAY,CAAC;IAC1B,IAAI,EAAE;QACJ,IAAI,EAAE,QAAQ;QACd,OAAO,EAAE;YACP,SAAS;YACT,iBAAiB;YACjB,UAAU;YACV,aAAa;YACb,eAAe;YACf,gBAAgB;YAChB,mBAAmB;YACnB,cAAc;YACd,gDAAgD;SACjD;QACD,OAAO,EAAE,CAAC,0BAA0B,CAAC;QACrC,QAAQ,EAAE;YACR,OAAO,EAAE,CAAC,aAAa,EAAE,eAAe,EAAE,gBAAgB,CAAC;SAC5D;QACD,OAAO,EAAE,IAAI;QACb,UAAU,EAAE,mBAAmB;KAChC;CACF,CAAC,CAAC"}
@@ -1,65 +0,0 @@
1
-
2
- describe("FPEnvelope", () => {
3
- // const sthis = mockSuperThis();
4
- // it("unknown bytes", () => {
5
- // expect(bs.FPMsgMatch2Envelope(Uint8Array.from([1, 2, 3]), "bla").Err().message).toStrictEqual(
6
- // "failed to decode envelope: Error: CBOR decode error: too many terminals, data makes no sense"
7
- // );
8
- // });
9
-
10
- // it("unknown type", () => {
11
- // expect(bs.FPMsgMatch2Envelope(encode({ type: "blax", payload: 4 }), "bla"))
12
- // .toStrictEqual(Result.Err("expected type to be bla"));
13
- // })
14
-
15
- // it("no type", () => {
16
- // expect(bs.FPMsgMatch2Envelope(encode({ type: "blax", payload: 4 })))
17
- // .toStrictEqual(Result.Ok({ type: "blax", payload: 4 }));
18
- // })
19
-
20
- // it("car type", () => {
21
- // expect(bs.FPMsg2Car(bs.Car2FPMsg(Uint8Array.from([1, 2, 3])).Ok().payload).Ok()).toStrictEqual(Uint8Array.from([1, 2, 3]));
22
- // })
23
-
24
- // it("file type", () => {
25
- // expect(bs.FPMsg2File(bs.File2FPMsg(Uint8Array.from([1, 2, 3])).Ok().payload).Ok()).toStrictEqual(Uint8Array.from([1, 2, 3]));
26
- // })
27
-
28
- // it("meta type", async () => {
29
- // const ref = {
30
- // eventCid: await simpleCID(sthis),
31
- // dbMeta: {
32
- // cars: [
33
- // await simpleCID(sthis)
34
- // ]
35
- // },
36
- // parents: [
37
- // await simpleCID(sthis),
38
- // await simpleCID(sthis)
39
- // ]
40
- // } satisfies DbMetaEvent;
41
- // expect(bs.FPMsg2Meta(bs.Meta2FPMsg([ref])).Ok()).toEqual(ref);
42
- // })
43
-
44
- // it("wal type", () => {
45
- // const ref: bs.WALState = {
46
- // fileOperations: [],
47
- // noLoaderOps: [],
48
- // operations: [
49
- // {
50
- // cars: [
51
- // CID.parse("bag4yvqabciqdzvfxrxfi6feubspyz666zegmp3z5w556mr4ykya2kkdm22r7pyy")
52
- // ]
53
- // },
54
- // {
55
- // cars: [
56
- // CID.parse("bag4yvqabciqd2ul2tw4mdcpvfq2pdqhvnqp2ktuyrtcl3j3gwhxbjzjt62xzeaq")
57
- // ]
58
- // }
59
- // ]
60
- // };
61
- // const res = bs.FPMsg2WAL(bs.WAL2FPMsg(sthis, ref).Ok()).Ok();
62
- // expect(res).toStrictEqual(ref);
63
- // expect(res.operations[0].cars[0].version).toStrictEqual(1);
64
- // })
65
- })
@@ -1,106 +0,0 @@
1
- import { Result, URI } from "@adviser/cement";
2
- import { bs, ensureSuperThis } from "@fireproof/core";
3
-
4
- class TraceGateway implements bs.Gateway {
5
- readonly buildUrlFn = vitest.fn();
6
-
7
- readonly fragSize: number;
8
- constructor(fragSize = 0) {
9
- this.fragSize = fragSize;
10
- }
11
-
12
- buildUrl(baseUrl: URI, key: string): Promise<Result<URI>> {
13
- this.buildUrlFn(baseUrl, key);
14
- return Promise.resolve(Result.Ok(baseUrl.build().setParam("key", key).URI()));
15
- }
16
- readonly startFn = vitest.fn();
17
- start(baseUrl: URI): Promise<Result<URI>> {
18
- this.startFn(baseUrl);
19
- const burl = baseUrl.build();
20
- if (this.fragSize) {
21
- burl.setParam("fragSize", this.fragSize.toString());
22
- }
23
- return Promise.resolve(Result.Ok(burl.URI()));
24
- }
25
- readonly closeFn = vitest.fn();
26
- close(baseUrl: URI): Promise<bs.VoidResult> {
27
- this.closeFn(baseUrl);
28
- return Promise.resolve(Result.Ok(undefined));
29
- }
30
- readonly destroyFn = vitest.fn();
31
- destroy(baseUrl: URI): Promise<bs.VoidResult> {
32
- this.destroyFn(baseUrl);
33
- return Promise.resolve(Result.Ok(undefined));
34
- }
35
- readonly getFn = vitest.fn();
36
- putCalls = 0;
37
- async get(url: URI): Promise<Result<Uint8Array>> {
38
- const idx = this.putCalls++;
39
- this.getFn(url);
40
- return Result.Ok(this.putFn.mock.calls[idx][1]);
41
- }
42
- readonly putFn = vitest.fn();
43
- async put(url: URI, data: Uint8Array): Promise<Result<void>> {
44
- this.putFn(url, data);
45
- return Result.Ok(undefined);
46
- }
47
- readonly deleteFn = vitest.fn();
48
- async delete(url: URI): Promise<Result<void>> {
49
- this.deleteFn(url);
50
- return Result.Ok(undefined);
51
- }
52
- }
53
-
54
- describe("FragmentGateway", () => {
55
- const sthis = ensureSuperThis();
56
- it("passthrough", async () => {
57
- const innerGW = new TraceGateway();
58
- const fgw = new bs.FragmentGateway(sthis, innerGW);
59
- const url = URI.from("http://example.com?key=3333");
60
-
61
- expect(await fgw.put(url, new Uint8Array([1, 2, 3, 4]))).toEqual(Result.Ok(undefined));
62
- expect(innerGW.putFn).toHaveBeenCalledWith(url, new Uint8Array([1, 2, 3, 4]));
63
-
64
- expect(await fgw.get(url)).toEqual(Result.Ok(new Uint8Array([1, 2, 3, 4])));
65
- expect(innerGW.getFn).toHaveBeenCalledWith(url);
66
- });
67
-
68
- function slice(total: number, headerSize: number, fragSize: number): { len?: string; ofs: string }[] {
69
- const res = [];
70
- for (let ofs = 0; ofs < total; ofs += fragSize - headerSize) {
71
- res.push({ len: total.toString(), ofs: ofs.toString() });
72
- }
73
- return res;
74
- }
75
-
76
- it("enable frag", async () => {
77
- const innerGW = new TraceGateway(128);
78
- const fgw = new bs.FragmentGateway(sthis, innerGW);
79
- const url = (await fgw.start(URI.from("http://example.com?key=3333"))).Ok();
80
- const buf = new Uint8Array(1024).fill(1).map((_, i) => i);
81
-
82
- expect(await fgw.put(url, buf)).toEqual(Result.Ok(undefined));
83
-
84
- const ref = slice(1024, fgw.headerSize, 128);
85
-
86
- expect(
87
- innerGW.putFn.mock.calls.map((i) => {
88
- return {
89
- len: i[0].getParam("len"),
90
- ofs: i[0].getParam("ofs"),
91
- };
92
- }),
93
- ).toEqual(ref);
94
-
95
- expect((await fgw.get(url)).Ok()).toEqual(buf);
96
- ref[0].len = undefined;
97
- expect(
98
- innerGW.getFn.mock.calls.map((i) => {
99
- return {
100
- len: i[0].getParam("len"),
101
- ofs: i[0].getParam("ofs"),
102
- };
103
- }),
104
- ).toEqual(ref);
105
- });
106
- });
@@ -1,259 +0,0 @@
1
- import { BuildURI, Result, URI } from "@adviser/cement";
2
- import { fireproof, SuperThis } from "@fireproof/core";
3
- import * as bs from "@fireproof/core-types-blockstore";
4
- import { describe, expect, it, vitest } from "vitest";
5
- import { PassThroughGateway, URIInterceptor } from "@fireproof/core-gateways-base";
6
- import { MemoryGateway } from "@fireproof/core-gateways-memory";
7
- import { registerStoreProtocol } from "@fireproof/core-blockstore";
8
-
9
- class TestInterceptor extends PassThroughGateway {
10
- readonly fn = vitest.fn();
11
-
12
- async buildUrl(ctx: bs.SerdeGatewayCtx, baseUrl: URI, key: string): Promise<Result<bs.SerdeGatewayBuildUrlReturn>> {
13
- const ret = await super.buildUrl(ctx, baseUrl, key);
14
- this.fn("buildUrl", ret);
15
- return ret;
16
- }
17
-
18
- async start(ctx: bs.SerdeGatewayCtx, baseUrl: URI): Promise<Result<bs.SerdeGatewayStartReturn>> {
19
- const ret = await super.start(ctx, baseUrl);
20
- this.fn("start", ret);
21
- return ret;
22
- }
23
- async close(ctx: bs.SerdeGatewayCtx, baseUrl: URI): Promise<Result<bs.SerdeGatewayCloseReturn>> {
24
- const ret = await super.close(ctx, baseUrl);
25
- this.fn("close", ret);
26
- return ret;
27
- }
28
- async delete(ctx: bs.SerdeGatewayCtx, baseUrl: URI): Promise<Result<bs.SerdeGatewayDeleteReturn>> {
29
- const ret = await super.delete(ctx, baseUrl);
30
- this.fn("delete", ret);
31
- return ret;
32
- }
33
- async destroy(ctx: bs.SerdeGatewayCtx, baseUrl: URI): Promise<Result<bs.SerdeGatewayDestroyReturn>> {
34
- const ret = await super.destroy(ctx, baseUrl);
35
- this.fn("destroy", ret);
36
- return ret;
37
- }
38
- async put<T>(ctx: bs.SerdeGatewayCtx, url: URI, body: bs.FPEnvelope<T>): Promise<Result<bs.SerdeGatewayPutReturn<T>>> {
39
- const ret = await super.put<T>(ctx, url, body);
40
- this.fn("put", ret);
41
- return ret;
42
- }
43
- async get<S>(ctx: bs.SerdeGatewayCtx, url: URI): Promise<Result<bs.SerdeGatewayGetReturn<S>>> {
44
- const ret = await super.get<S>(ctx, url);
45
- this.fn("get", ret);
46
- return ret;
47
- }
48
- async subscribe(
49
- ctx: bs.SerdeGatewayCtx,
50
- url: URI,
51
- callback: (meta: bs.FPEnvelopeMeta) => Promise<void>,
52
- ): Promise<Result<bs.SerdeGatewaySubscribeReturn>> {
53
- const ret = await super.subscribe(ctx, url, callback);
54
- this.fn("subscribe", ret);
55
- return ret;
56
- }
57
- }
58
-
59
- export class URITrackGateway implements bs.Gateway {
60
- readonly uris: Set<string>;
61
- readonly memgw: MemoryGateway;
62
-
63
- constructor(sthis: SuperThis, memorys: Map<string, Uint8Array>, uris: Set<string>) {
64
- this.memgw = new MemoryGateway(sthis, memorys);
65
- this.uris = uris;
66
- }
67
-
68
- uriAdd(uri: URI) {
69
- if (!uri.getParam("itis")) {
70
- throw new Error("itis not set");
71
- }
72
- if (this.uris.has(uri.toString())) {
73
- throw new Error(`uri already added:${uri.toString()}`);
74
- }
75
- this.uris.add(uri.toString());
76
- }
77
-
78
- buildUrl(baseUrl: URI, key: string): Promise<Result<URI>> {
79
- this.uriAdd(baseUrl);
80
- return this.memgw.buildUrl(baseUrl, key);
81
- }
82
- start(baseUrl: URI): Promise<Result<URI>> {
83
- this.uriAdd(baseUrl);
84
- return this.memgw.start(baseUrl);
85
- }
86
- close(uri: URI): Promise<bs.VoidResult> {
87
- this.uriAdd(uri);
88
- return this.memgw.close(uri);
89
- }
90
- destroy(baseUrl: URI): Promise<bs.VoidResult> {
91
- this.uriAdd(baseUrl);
92
- return this.memgw.destroy(baseUrl);
93
- }
94
-
95
- put(url: URI, bytes: Uint8Array, sthis: SuperThis): Promise<bs.VoidResult> {
96
- // console.log("put", url.getParam(PARAM.KEY), url.toString());
97
- this.uriAdd(url);
98
- return this.memgw.put(url.build().cleanParams("itis").URI(), bytes, sthis);
99
- }
100
-
101
- async get(url: URI, sthis: SuperThis): Promise<bs.GetResult> {
102
- this.uriAdd(url);
103
- const ret = await this.memgw.get(url.build().cleanParams("itis").URI(), sthis);
104
- // if (ret.isErr()) {
105
- // console.log("get-err", url.getParam(PARAM.KEY), url.toString());
106
- // }
107
- return ret;
108
- }
109
- delete(url: URI): Promise<bs.VoidResult> {
110
- this.uriAdd(url);
111
- return this.memgw.delete(url);
112
- }
113
-
114
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
115
- subscribe(url: URI, callback: (meta: Uint8Array) => void, sthis: SuperThis): Promise<bs.UnsubscribeResult> {
116
- this.uriAdd(url);
117
- return Promise.resolve(
118
- Result.Ok(() => {
119
- /* noop */
120
- }),
121
- );
122
- }
123
-
124
- async getPlain(url: URI, key: string): Promise<Result<Uint8Array>> {
125
- this.uriAdd(url);
126
- return this.memgw.getPlain(url, key);
127
- }
128
- }
129
-
130
- describe("InterceptorGateway", () => {
131
- it("passthrough", async () => {
132
- const gwi = new TestInterceptor();
133
- const db = fireproof("interceptor-gateway", {
134
- gatewayInterceptor: gwi,
135
- });
136
- expect(
137
- await db.put({
138
- _id: "foo",
139
- foo: 4,
140
- }),
141
- );
142
- expect(await db.get("foo")).toEqual({
143
- _id: "foo",
144
- foo: 4,
145
- });
146
- await db.close();
147
- await db.destroy();
148
- // await sleep(1000);
149
- expect(gwi.fn.mock.calls.length).toBe(54);
150
- // might be a stupid test
151
- expect(gwi.fn.mock.calls.map((i) => i[0]).sort() /* not ok there are some operation */).toEqual(
152
- [
153
- "start",
154
- "start",
155
- "buildUrl",
156
- "get",
157
- "buildUrl",
158
- "buildUrl",
159
- "buildUrl",
160
- "buildUrl",
161
- "buildUrl",
162
- "buildUrl",
163
- "buildUrl",
164
- "buildUrl",
165
- "get",
166
- "get",
167
- "start",
168
- "start",
169
- "buildUrl",
170
- "get",
171
- "get",
172
- "buildUrl",
173
- "put",
174
- "put",
175
- "buildUrl",
176
- "put",
177
- "buildUrl",
178
- "put",
179
- "put",
180
- "put",
181
- "put",
182
- "put",
183
- "start",
184
- "start",
185
- "start",
186
- "start",
187
- "close",
188
- "close",
189
- "close",
190
- "close",
191
- "buildUrl",
192
- "get",
193
- "close",
194
- "close",
195
- "close",
196
- "close",
197
- "destroy",
198
- "destroy",
199
- "destroy",
200
- "destroy",
201
- "destroy",
202
- "destroy",
203
- "destroy",
204
- "destroy",
205
- "subscribe",
206
- "subscribe",
207
- ].sort() /* not ok there are some operation */,
208
- );
209
- });
210
-
211
- it("use the uri-interceptor", async () => {
212
- let callCount = 0;
213
- const gwUris = new Set<string>();
214
- const unreg = registerStoreProtocol({
215
- protocol: "uriTest:",
216
- isDefault: false,
217
- defaultURI: () => {
218
- return BuildURI.from("uriTest://").pathname("ram").URI();
219
- },
220
- gateway: async (sthis) => {
221
- return new URITrackGateway(sthis, new Map<string, Uint8Array>(), gwUris);
222
- },
223
- });
224
- const db = fireproof("interceptor-gateway", {
225
- storeUrls: {
226
- base: "uriTest://inspector-gateway",
227
- },
228
- gatewayInterceptor: URIInterceptor.withMapper(async (uri: URI) =>
229
- uri
230
- .build()
231
- .setParam("itis", "" + ++callCount)
232
- .URI(),
233
- ),
234
- });
235
- await Promise.all(
236
- Array(5)
237
- .fill(0)
238
- .map((_, i) => db.put({ _id: "foo" + i, foo: i })),
239
- );
240
- expect((await db.allDocs<{ foo: number }>()).rows.map((i) => i.value.foo)).toEqual(
241
- Array(5)
242
- .fill(0)
243
- .map((_, i) => i),
244
- );
245
- await db.close();
246
- expect(callCount).toBe(gwUris.size);
247
- expect(
248
- Array.from(gwUris)
249
- .map((i) => URI.from(i).getParam("itis"))
250
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
251
- .sort((a, b) => +a! - +b!),
252
- ).toEqual(
253
- Array(gwUris.size)
254
- .fill(1)
255
- .map((_, i) => "" + (i + 1)),
256
- );
257
- unreg();
258
- });
259
- });
@@ -1,134 +0,0 @@
1
- import { runtimeFn, toCryptoRuntime, URI } from "@adviser/cement";
2
- import { base58btc } from "multiformats/bases/base58";
3
- import { mockLoader, mockSuperThis } from "../helpers.js";
4
- import { ensureSuperThis } from "@fireproof/core-runtime";
5
- import { V2KeysItem, PARAM } from "@fireproof/core-types-base";
6
- import { describe, beforeAll, it, expect, beforeEach } from "vitest";
7
- import { coerceMaterial, getKeyBag, toKeyWithFingerPrint } from "@fireproof/core-keybag";
8
- import { KeyBagProviderIndexedDB } from "@fireproof/core-gateways-indexeddb";
9
- import { KeyBagProviderFile } from "@fireproof/core-gateways-file";
10
- import { KeyWithFingerPrint, Loadable } from "@fireproof/core-types-blockstore";
11
- import { createAttachedStores } from "@fireproof/core-blockstore";
12
-
13
- describe("KeyBag indexeddb and file", () => {
14
- let url: URI;
15
- const sthis = mockSuperThis();
16
- beforeAll(async () => {
17
- await sthis.start();
18
- if (runtimeFn().isBrowser) {
19
- url = URI.from("indexeddb://fp-keybag");
20
- } else {
21
- url = URI.merge(`file://./dist/tests/key.bag`, sthis.env.get("FP_KEYBAG_URL"));
22
- }
23
- });
24
- it("default-path", async () => {
25
- const old = sthis.env.get("FP_KEYBAG_URL");
26
- sthis.env.delete("FP_KEYBAG_URL");
27
- const kb = await getKeyBag(sthis);
28
- if (runtimeFn().isBrowser) {
29
- expect(kb.rt.url.toString()).toBe(`indexeddb://fp-keybag`);
30
- } else {
31
- expect(kb.rt.url.toString()).toBe(`file://${sthis.env.get("HOME")}/.fireproof/keybag`);
32
- }
33
- sthis.env.set("FP_KEYBAG_URL", old);
34
- });
35
- it("from env", async () => {
36
- const old = sthis.env.get("FP_KEYBAG_URL");
37
- sthis.env.set("FP_KEYBAG_URL", url.toString());
38
- const kb = await getKeyBag(sthis);
39
- expect(kb.rt.url.toString()).toBe(url.toString());
40
- sthis.env.set("FP_KEYBAG_URL", old);
41
- });
42
- it("simple add", async () => {
43
- const kb = await getKeyBag(sthis, {
44
- url: url.toString(),
45
- crypto: toCryptoRuntime({
46
- randomBytes: (size) => new Uint8Array(size).map((_, i) => i),
47
- }),
48
- });
49
- const name = "setkey" + Math.random();
50
- expect((await kb.getNamedKey(name, true)).isErr()).toBeTruthy();
51
-
52
- const key = base58btc.encode(kb.rt.crypto.randomBytes(kb.rt.keyLength));
53
- const res = await kb.getNamedKey(name, false, key);
54
- expect(res.isOk()).toBeTruthy();
55
- expect((await kb.getNamedKey(name, true)).Ok()).toEqual(res.Ok());
56
-
57
- const name2 = "implicit" + Math.random();
58
- const created = await kb.getNamedKey(name2);
59
- expect(created.isOk()).toBeTruthy();
60
-
61
- expect((await kb.getNamedKey(name2)).Ok()).toEqual(created.Ok());
62
-
63
- let diskBag: V2KeysItem;
64
- let diskBag2: V2KeysItem;
65
- const provider = await kb.rt.getBagProvider();
66
- if (runtimeFn().isBrowser) {
67
- const p = provider as KeyBagProviderIndexedDB;
68
- diskBag = await p._prepare().then((db) => db.get("bag", name));
69
- diskBag2 = await p._prepare().then((db) => db.get("bag", name2));
70
- } else {
71
- const p = provider as KeyBagProviderFile;
72
- const { sysFS } = await p._prepare(name);
73
-
74
- diskBag = await sysFS.readfile((await p._prepare(name)).fName).then((data) => {
75
- return JSON.parse(sthis.txt.decode(data)) as V2KeysItem;
76
- });
77
- diskBag2 = await sysFS.readfile((await p._prepare(name2)).fName).then((data) => {
78
- return JSON.parse(sthis.txt.decode(data)) as V2KeysItem;
79
- });
80
- }
81
- expect((await toKeyWithFingerPrint(kb, coerceMaterial(kb, Object.values(diskBag.keys)[0].key), true)).Ok().fingerPrint).toEqual(
82
- (await res.Ok().get())?.fingerPrint,
83
- );
84
- expect(
85
- (await toKeyWithFingerPrint(kb, coerceMaterial(kb, Object.values(diskBag2.keys)[0].key), true)).Ok().fingerPrint,
86
- ).toEqual((await created.Ok().get())?.fingerPrint);
87
- const algo = {
88
- name: "AES-GCM",
89
- iv: kb.rt.crypto.randomBytes(12),
90
- tagLength: 128,
91
- };
92
- const data = kb.rt.crypto.randomBytes(122);
93
- const rkc = (await res.Ok().get()) as KeyWithFingerPrint;
94
- const ckc = (await created.Ok().get()) as KeyWithFingerPrint;
95
- expect(await kb.rt.crypto.encrypt(algo, rkc.key, data)).toEqual(await kb.rt.crypto.encrypt(algo, ckc.key, data));
96
- expect(await kb.rt.crypto.encrypt(algo, await kb.subtleKey(Object.values(diskBag.keys)[0].key), data)).toEqual(
97
- await kb.rt.crypto.encrypt(algo, ckc.key, data),
98
- );
99
- expect(await kb.rt.crypto.encrypt(algo, await kb.subtleKey(Object.values(diskBag2.keys)[0].key), data)).toEqual(
100
- await kb.rt.crypto.encrypt(algo, ckc.key, data),
101
- );
102
- });
103
- });
104
-
105
- describe("KeyedCryptoStore", () => {
106
- let loader: Loadable;
107
- // let logger: Logger;
108
- let baseUrl: URI;
109
- const sthis = ensureSuperThis();
110
- beforeEach(async () => {
111
- await sthis.start();
112
- // logger = MockLogger().logger;
113
- // let kbUrl: URI;
114
- if (runtimeFn().isBrowser) {
115
- // kbUrl = URI.from("indexeddb://fp-keybag");
116
- baseUrl = URI.from("indexeddb://fp-keyed-crypto-store");
117
- } else {
118
- // kbUrl = URI.merge(`file://./dist/tests/key.bag`, sthis.env.get("FP_KEYBAG_URL"));
119
- baseUrl = URI.merge("file://./dist/tests/keyed-crypto-store", sthis.env.get("FP_STORAGE_URL"));
120
- }
121
- baseUrl = baseUrl.build().defParam(PARAM.NAME, "test").URI();
122
- loader = mockLoader(sthis);
123
- });
124
- it("no crypto", async () => {
125
- const url = baseUrl.build().setParam(PARAM.STORE_KEY, "insecure").URI();
126
- for (const pstore of (await createAttachedStores(url, loader, "insecure")).stores.baseStores) {
127
- const store = await pstore;
128
- // await store.start();
129
- const kc = await store.keyedCrypto();
130
- expect(kc.constructor.name).toBe("noCrypto");
131
- // expect(kc.isEncrypting).toBe(false);
132
- }
133
- });
134
- });