@peerbit/blocks 2.0.32-efee9d3 → 2.1.0-2bc15a6
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.
- package/dist/benchmark/e2e.js +9 -8
- package/dist/benchmark/e2e.js.map +1 -1
- package/dist/benchmark/multiformats.js +8 -8
- package/dist/benchmark/multiformats.js.map +1 -1
- package/dist/src/any-blockstore.d.ts +3 -2
- package/dist/src/any-blockstore.d.ts.map +1 -1
- package/dist/src/any-blockstore.js +11 -9
- package/dist/src/any-blockstore.js.map +1 -1
- package/dist/src/index.d.ts +0 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +0 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/libp2p.d.ts +2 -2
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +10 -8
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/remote.d.ts +5 -4
- package/dist/src/remote.d.ts.map +1 -1
- package/dist/src/remote.js +24 -14
- package/dist/src/remote.js.map +1 -1
- package/package.json +11 -10
- package/src/any-blockstore.ts +18 -14
- package/src/index.ts +0 -6
- package/src/libp2p.ts +15 -12
- package/src/remote.ts +49 -32
- package/dist/src/block.d.ts +0 -27
- package/dist/src/block.d.ts.map +0 -1
- package/dist/src/block.js +0 -89
- package/dist/src/block.js.map +0 -1
- package/src/block.ts +0 -114
package/dist/benchmark/e2e.js
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { tcp } from "@libp2p/tcp";
|
|
2
|
+
import { stringifyCid } from "@peerbit/blocks-interface";
|
|
2
3
|
import { TestSession } from "@peerbit/libp2p-test-utils";
|
|
3
4
|
import { waitForPeers } from "@peerbit/stream";
|
|
4
5
|
import { delay } from "@peerbit/time";
|
|
6
|
+
import B from "benchmark";
|
|
5
7
|
import crypto from "crypto";
|
|
6
|
-
import { DirectBlock
|
|
7
|
-
|
|
8
|
-
// Run with "node --loader ts-node/esm ./src/__benchmark__/e2e.ts"
|
|
8
|
+
import { DirectBlock } from "../src/index.js";
|
|
9
|
+
// Run with "node --loader ts-node/esm ./benchmark/e2e.ts"
|
|
9
10
|
// size: 1kb x 827 ops/sec ±2.03% (87 runs sampled)
|
|
10
11
|
// size: 1000kb x 40.51 ops/sec ±4.09% (62 runs sampled)
|
|
11
12
|
const session = await TestSession.disconnected(4, {
|
|
12
13
|
transports: [tcp()],
|
|
13
14
|
services: {
|
|
14
|
-
blocks: (c) => new DirectBlock(c)
|
|
15
|
-
}
|
|
15
|
+
blocks: (c) => new DirectBlock(c),
|
|
16
|
+
},
|
|
16
17
|
});
|
|
17
18
|
/*
|
|
18
19
|
┌─┐
|
|
@@ -32,7 +33,7 @@ const session = await TestSession.disconnected(4, {
|
|
|
32
33
|
await session.connect([
|
|
33
34
|
[session.peers[0], session.peers[1]],
|
|
34
35
|
[session.peers[1], session.peers[2]],
|
|
35
|
-
[session.peers[2], session.peers[3]]
|
|
36
|
+
[session.peers[2], session.peers[3]],
|
|
36
37
|
]);
|
|
37
38
|
await session.connect();
|
|
38
39
|
await waitForPeers(session.peers[0].services.blocks, session.peers[1].services.blocks);
|
|
@@ -60,7 +61,7 @@ for (const size of sizes) {
|
|
|
60
61
|
await session.peers[session.peers.length - 1].services.blocks.get(stringifyCid(cid));
|
|
61
62
|
deferred.resolve();
|
|
62
63
|
}
|
|
63
|
-
}
|
|
64
|
+
},
|
|
64
65
|
});
|
|
65
66
|
}
|
|
66
67
|
suite
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"e2e.js","sourceRoot":"","sources":["../../benchmark/e2e.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,
|
|
1
|
+
{"version":3,"file":"e2e.js","sourceRoot":"","sources":["../../benchmark/e2e.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAClC,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CA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,CAAC;gBACrC,MAAM,GAAG,GAAG,MAAM,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;gBAC5D,MAAM,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,GAAG,CAChE,YAAY,CAAC,GAAG,CAAC,CACjB,CAAC;gBACF,QAAQ,CAAC,OAAO,EAAE,CAAC;YACpB,CAAC;QACF,CAAC;KACD,CAAC,CAAC;AACJ,CAAC;AACD,KAAK;KACH,EAAE,CAAC,OAAO,EAAE,CAAC,KAAU,EAAE,EAAE;IAC3B,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;AACnC,CAAC,CAAC;KACD,EAAE,CAAC,UAAU,EAAE,UAAqB,GAAG,IAAW;IAClD,OAAO,CAAC,IAAI,EAAE,CAAC;AAChB,CAAC,CAAC;KACD,GAAG,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC"}
|
|
@@ -9,13 +9,13 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
9
9
|
};
|
|
10
10
|
var CIDSha256_1;
|
|
11
11
|
import { deserialize, field, serialize, variant } from "@dao-xyz/borsh";
|
|
12
|
+
import { checkDecodeBlock, cidifyString, codecMap, stringifyCid, } from "@peerbit/blocks-interface";
|
|
12
13
|
import B from "benchmark";
|
|
13
14
|
import crypto from "crypto";
|
|
14
|
-
import { sha256 } from "multiformats/hashes/sha2";
|
|
15
15
|
import { encode } from "multiformats/block";
|
|
16
|
-
import {
|
|
16
|
+
import { sha256 } from "multiformats/hashes/sha2";
|
|
17
17
|
import { equals } from "uint8arrays";
|
|
18
|
-
// Run with "node --loader ts-node/esm ./
|
|
18
|
+
// Run with "node --loader ts-node/esm ./benchmark/multiformats.ts"
|
|
19
19
|
// size: 1kb x 785 ops/sec ±2.66% (86 runs sampled)
|
|
20
20
|
// size: 1000kb x 38.14 ops/sec ±1.43% (40 runs sampled)
|
|
21
21
|
class DummyCid {
|
|
@@ -75,20 +75,20 @@ for (const size of sizes) {
|
|
|
75
75
|
const cid = await encode({
|
|
76
76
|
value: rng,
|
|
77
77
|
codec,
|
|
78
|
-
hasher: sha256
|
|
78
|
+
hasher: sha256,
|
|
79
79
|
});
|
|
80
80
|
const cidString = stringifyCid(cid.cid);
|
|
81
81
|
const cidObject = cidifyString(cidString);
|
|
82
82
|
const checked = await checkDecodeBlock(cidObject, rng, {
|
|
83
83
|
hasher: sha256,
|
|
84
|
-
codec
|
|
84
|
+
codec,
|
|
85
85
|
});
|
|
86
86
|
if (!checked) {
|
|
87
87
|
throw new Error("Not verified");
|
|
88
88
|
}
|
|
89
89
|
deferred.resolve();
|
|
90
90
|
}
|
|
91
|
-
}
|
|
91
|
+
},
|
|
92
92
|
});
|
|
93
93
|
suite.add("dummy, size: " + size / 1e3 + "kb", {
|
|
94
94
|
defer: true,
|
|
@@ -104,7 +104,7 @@ for (const size of sizes) {
|
|
|
104
104
|
}
|
|
105
105
|
deferred.resolve();
|
|
106
106
|
}
|
|
107
|
-
}
|
|
107
|
+
},
|
|
108
108
|
});
|
|
109
109
|
suite.add("sha256 digest hex, size: " + size / 1e3 + "kb", {
|
|
110
110
|
defer: true,
|
|
@@ -118,7 +118,7 @@ for (const size of sizes) {
|
|
|
118
118
|
}
|
|
119
119
|
deferred.resolve();
|
|
120
120
|
}
|
|
121
|
-
}
|
|
121
|
+
},
|
|
122
122
|
});
|
|
123
123
|
}
|
|
124
124
|
suite
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"multiformats.js","sourceRoot":"","sources":["../../benchmark/multiformats.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,OAAO,EAAE,WAAW,EAAE,KAAK,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AACxE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BAC7B,MAAM,IAAI,KAAK,CAAC,cAAc,CAAC,CAAC;gBACjC,CAAC;gBACD,QAAQ,CAAC,OAAO,EAAE,CAAC;YACpB,CAAC;QACF,CAAC;KACD,CAAC,CAAC;IAEH,KAAK,CAAC,GAAG,CAAC,2BAA2B,GAAG,IAAI,GAAG,GAAG,GAAG,IAAI,EAAE;QAC1D,KAAK,EAAE,IAAI;QACX,EAAE,EAAE,CAAC,QAAa,EAAE,EAAE;YACrB,CAAC;gBACA,MAAM,GAAG,GAAG,SAAS,CAAC,IAAI,CAAC,CAAC;gBAE5B,MAAM,GAAG,GAAG,MAAM,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;gBAElE,MAAM,IAAI,GAAG,MAAM,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;gBAEnE,IAAI,GAAG,KAAK,IAAI,EAAE,CAAC;oBAClB,MAAM,IAAI,KAAK,CAAC,cAAc,CAAC,CAAC;gBACjC,CAAC;gBACD,QAAQ,CAAC,OAAO,EAAE,CAAC;YACpB,CAAC;QACF,CAAC;KACD,CAAC,CAAC;AACJ,CAAC;AACD,KAAK;KACH,EAAE,CAAC,OAAO,EAAE,CAAC,KAAU,EAAE,EAAE;IAC3B,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;AACnC,CAAC,CAAC;KACD,GAAG,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { type Blocks } from "@peerbit/blocks-interface";
|
|
2
1
|
import { type PeerId } from "@libp2p/interface";
|
|
3
|
-
import { PublicSignKey } from "@peerbit/crypto";
|
|
4
2
|
import { type AnyStore } from "@peerbit/any-store-interface";
|
|
3
|
+
import { type Blocks } from "@peerbit/blocks-interface";
|
|
4
|
+
import { type PublicSignKey } from "@peerbit/crypto";
|
|
5
5
|
export declare class AnyBlockStore implements Blocks {
|
|
6
6
|
private _store;
|
|
7
7
|
private _opening;
|
|
@@ -23,5 +23,6 @@ export declare class AnyBlockStore implements Blocks {
|
|
|
23
23
|
status(): import("@peerbit/any-store-interface").MaybePromise<"opening" | "open" | "closing" | "closed">;
|
|
24
24
|
waitFor(peer: PeerId | PublicSignKey): Promise<void>;
|
|
25
25
|
size(): Promise<number>;
|
|
26
|
+
persisted(): boolean | Promise<boolean>;
|
|
26
27
|
}
|
|
27
28
|
//# sourceMappingURL=any-blockstore.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"any-blockstore.d.ts","sourceRoot":"","sources":["../../src/any-blockstore.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"any-blockstore.d.ts","sourceRoot":"","sources":["../../src/any-blockstore.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAEhD,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,8BAA8B,CAAC;AAC7D,OAAO,EACN,KAAK,MAAM,EAMX,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAIrD,qBAAa,aAAc,YAAW,MAAM;IAC3C,OAAO,CAAC,MAAM,CAAW;IACzB,OAAO,CAAC,QAAQ,CAAe;IAC/B,OAAO,CAAC,QAAQ,CAA0B;IAC1C,OAAO,CAAC,gBAAgB,CAAkB;gBAC9B,KAAK,GAAE,QAAwB;IAIrC,GAAG,CACR,GAAG,EAAE,MAAM,EACX,OAAO,CAAC,EAAE;QACT,GAAG,CAAC,EAAE,OAAO,CAAC;QACd,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;QACjB,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,MAAM,CAAC,EAAE,GAAG,CAAC;KACb,GACC,OAAO,CAAC,UAAU,GAAG,SAAS,CAAC;IAyB5B,GAAG,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,MAAM,CAAC;IAQvC,EAAE,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAI7B,QAAQ,IAAI,cAAc,CAAC,CAAC,MAAM,EAAE,UAAU,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC;IAM7D,GAAG,CAAC,GAAG,EAAE,MAAM;IAIf,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAmBtB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAM3B,MAAM;IAGA,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC;IAIpD,IAAI;IAIV,SAAS,IAAI,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;CAGvC"}
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import {} from "@peerbit/blocks-interface";
|
|
2
|
-
import { cidifyString, codecCodes, createBlock, defaultHasher, stringifyCid } from "./block.js";
|
|
3
|
-
import { decode } from "multiformats/block";
|
|
4
|
-
import { waitFor } from "@peerbit/time";
|
|
5
1
|
import {} from "@libp2p/interface";
|
|
6
|
-
import { PublicSignKey } from "@peerbit/crypto";
|
|
7
2
|
import { createStore } from "@peerbit/any-store";
|
|
8
3
|
import {} from "@peerbit/any-store-interface";
|
|
4
|
+
import { cidifyString, codecCodes, createBlock, defaultHasher, stringifyCid, } from "@peerbit/blocks-interface";
|
|
5
|
+
import {} from "@peerbit/crypto";
|
|
6
|
+
import { waitFor } from "@peerbit/time";
|
|
7
|
+
import { decode } from "multiformats/block";
|
|
9
8
|
export class AnyBlockStore {
|
|
10
9
|
_store;
|
|
11
10
|
_opening;
|
|
@@ -25,7 +24,7 @@ export class AnyBlockStore {
|
|
|
25
24
|
const block = await decode({
|
|
26
25
|
bytes,
|
|
27
26
|
codec,
|
|
28
|
-
hasher: options?.hasher || defaultHasher
|
|
27
|
+
hasher: options?.hasher || defaultHasher,
|
|
29
28
|
});
|
|
30
29
|
return block.bytes;
|
|
31
30
|
}
|
|
@@ -62,7 +61,7 @@ export class AnyBlockStore {
|
|
|
62
61
|
this._opening = waitFor(async () => (await this._store.status()) === "open", {
|
|
63
62
|
delayInterval: 100,
|
|
64
63
|
timeout: 10 * 1000,
|
|
65
|
-
signal: this._closeController.signal
|
|
64
|
+
signal: this._closeController.signal,
|
|
66
65
|
});
|
|
67
66
|
await this._opening;
|
|
68
67
|
}
|
|
@@ -71,7 +70,7 @@ export class AnyBlockStore {
|
|
|
71
70
|
}
|
|
72
71
|
}
|
|
73
72
|
async stop() {
|
|
74
|
-
this._onClose
|
|
73
|
+
this._onClose?.();
|
|
75
74
|
this._closeController?.abort();
|
|
76
75
|
return this._store.close();
|
|
77
76
|
}
|
|
@@ -79,10 +78,13 @@ export class AnyBlockStore {
|
|
|
79
78
|
return this._store.status();
|
|
80
79
|
}
|
|
81
80
|
async waitFor(peer) {
|
|
82
|
-
|
|
81
|
+
// Offline storage // TODO this feels off resolving
|
|
83
82
|
}
|
|
84
83
|
async size() {
|
|
85
84
|
return this._store.size();
|
|
86
85
|
}
|
|
86
|
+
persisted() {
|
|
87
|
+
return this._store.persisted();
|
|
88
|
+
}
|
|
87
89
|
}
|
|
88
90
|
//# sourceMappingURL=any-blockstore.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"any-blockstore.js","sourceRoot":"","sources":["../../src/any-blockstore.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,MAAM,
|
|
1
|
+
{"version":3,"file":"any-blockstore.js","sourceRoot":"","sources":["../../src/any-blockstore.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,EAA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AAM,IAAI,CAAC,QAAQ,CAAC;QACrB,CAAC;gBAAS,CAAC;YACV,IAAI,CAAC,QAAQ,GAAG,SAAS,CAAC;QAC3B,CAAC;IACF,CAAC;IAED,KAAK,CAAC,IAAI;QACT,IAAI,CAAC,QAAQ,EAAE,EAAE,CAAC;QAClB,IAAI,CAAC,gBAAgB,EAAE,KAAK,EAAE,CAAC;QAC/B,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;IAC5B,CAAC;IAED,MAAM;QACL,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;IAC7B,CAAC;IACD,KAAK,CAAC,OAAO,CAAC,IAA4B;QACzC,mDAAmD;IACpD,CAAC;IAED,KAAK,CAAC,IAAI;QACT,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;IAC3B,CAAC;IAED,SAAS;QACR,OAAO,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC;IAChC,CAAC;CACD"}
|
package/dist/src/index.d.ts
CHANGED
package/dist/src/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,cAAc,gBAAgB,CAAC;AAC/B,cAAc,qBAAqB,CAAC;AACpC,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC"}
|
package/dist/src/index.js
CHANGED
package/dist/src/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,cAAc,gBAAgB,CAAC;AAC/B,cAAc,qBAAqB,CAAC;AACpC,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC"}
|
package/dist/src/libp2p.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { GetOptions, Blocks as IBlocks } from "@peerbit/blocks-interface";
|
|
2
2
|
import { DirectStream } from "@peerbit/stream";
|
|
3
3
|
import { type DirectStreamComponents } from "@peerbit/stream";
|
|
4
|
-
import type { GetOptions } from "@peerbit/blocks-interface";
|
|
5
4
|
export type DirectBlockComponents = DirectStreamComponents;
|
|
6
5
|
export declare class DirectBlock extends DirectStream implements IBlocks {
|
|
7
6
|
private remoteBlocks;
|
|
@@ -22,5 +21,6 @@ export declare class DirectBlock extends DirectStream implements IBlocks {
|
|
|
22
21
|
stop(): Promise<void>;
|
|
23
22
|
size(): Promise<number>;
|
|
24
23
|
get status(): boolean | import("./interface.js").StoreStatus;
|
|
24
|
+
persisted(): boolean | Promise<boolean>;
|
|
25
25
|
}
|
|
26
26
|
//# sourceMappingURL=libp2p.d.ts.map
|
package/dist/src/libp2p.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"libp2p.d.ts","sourceRoot":"","sources":["../../src/libp2p.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"libp2p.d.ts","sourceRoot":"","sources":["../../src/libp2p.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,IAAI,OAAO,EAAE,MAAM,2BAA2B,CAAC;AAE/E,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EAAE,KAAK,sBAAsB,EAAE,MAAM,iBAAiB,CAAC;AAK9D,MAAM,MAAM,qBAAqB,GAAG,sBAAsB,CAAC;AAE3D,qBAAa,WAAY,SAAQ,YAAa,YAAW,OAAO;IAC/D,OAAO,CAAC,YAAY,CAAe;IACnC,OAAO,CAAC,QAAQ,CAAM;IACtB,OAAO,CAAC,iBAAiB,CAAM;gBAG9B,UAAU,EAAE,qBAAqB,EACjC,OAAO,CAAC,EAAE;QACT,SAAS,CAAC,EAAE,MAAM,CAAC;QACnB,eAAe,CAAC,EAAE,OAAO,CAAC;QAC1B,YAAY,CAAC,EAAE,MAAM,CAAC;QACtB,4BAA4B,CAAC,EAAE,MAAM,CAAC;KACtC;IA8BI,GAAG,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,MAAM,CAAC;IAIvC,GAAG,CAAC,GAAG,EAAE,MAAM;IAGf,GAAG,CACR,GAAG,EAAE,MAAM,EACX,OAAO,CAAC,EAAE,UAAU,GAAG,SAAS,GAC9B,OAAO,CAAC,UAAU,GAAG,SAAS,CAAC;IAI5B,EAAE,CAAC,GAAG,EAAE,MAAM;IAIb,QAAQ,IAAI,cAAc,CAAC,CAAC,MAAM,EAAE,UAAU,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC;IAM7D,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAOtB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAOrB,IAAI;IAGV,IAAI,MAAM,mDAET;IAED,SAAS,IAAI,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;CAGvC"}
|
package/dist/src/libp2p.js
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import {} from "@
|
|
1
|
+
import { deserialize, serialize } from "@dao-xyz/borsh";
|
|
2
|
+
import { createStore } from "@peerbit/any-store";
|
|
3
|
+
import {} from "@peerbit/crypto";
|
|
2
4
|
import { DirectStream } from "@peerbit/stream";
|
|
3
5
|
import {} from "@peerbit/stream";
|
|
6
|
+
import { AnyWhere } from "@peerbit/stream-interface";
|
|
4
7
|
import { AnyBlockStore } from "./any-blockstore.js";
|
|
5
|
-
import { createStore } from "@peerbit/any-store";
|
|
6
8
|
import { BlockMessage, RemoteBlocks } from "./remote.js";
|
|
7
|
-
import { PublicSignKey } from "@peerbit/crypto";
|
|
8
|
-
import { AnyWhere, DataMessage } from "@peerbit/stream-interface";
|
|
9
|
-
import { deserialize, serialize } from "@dao-xyz/borsh";
|
|
10
9
|
export class DirectBlock extends DirectStream {
|
|
11
10
|
remoteBlocks;
|
|
12
11
|
onDataFn;
|
|
@@ -17,15 +16,15 @@ export class DirectBlock extends DirectStream {
|
|
|
17
16
|
canRelayMessage: options?.canRelayMessage ?? true,
|
|
18
17
|
connectionManager: {
|
|
19
18
|
dialer: false,
|
|
20
|
-
pruner: false
|
|
21
|
-
}
|
|
19
|
+
pruner: false,
|
|
20
|
+
},
|
|
22
21
|
});
|
|
23
22
|
this.remoteBlocks = new RemoteBlocks({
|
|
24
23
|
local: new AnyBlockStore(createStore(options?.directory)),
|
|
25
24
|
publish: (message) => this.publish(serialize(message), { mode: new AnyWhere() }),
|
|
26
25
|
localTimeout: options?.localTimeout || 1000,
|
|
27
26
|
messageProcessingConcurrency: options?.messageProcessingConcurrency || 10,
|
|
28
|
-
waitFor: this.waitFor.bind(this)
|
|
27
|
+
waitFor: this.waitFor.bind(this),
|
|
29
28
|
});
|
|
30
29
|
this.onDataFn = (data) => {
|
|
31
30
|
data.detail?.data?.length &&
|
|
@@ -69,5 +68,8 @@ export class DirectBlock extends DirectStream {
|
|
|
69
68
|
get status() {
|
|
70
69
|
return this.remoteBlocks?.status || this.started;
|
|
71
70
|
}
|
|
71
|
+
persisted() {
|
|
72
|
+
return this.remoteBlocks.persisted();
|
|
73
|
+
}
|
|
72
74
|
}
|
|
73
75
|
//# sourceMappingURL=libp2p.js.map
|
package/dist/src/libp2p.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"libp2p.js","sourceRoot":"","sources":["../../src/libp2p.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"libp2p.js","sourceRoot":"","sources":["../../src/libp2p.ts"],"names":[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AE,IAAI,CAAC,QAAQ,CAAC,CAAC;QAC7C,IAAI,CAAC,gBAAgB,CAAC,gBAAgB,EAAE,IAAI,CAAC,iBAAiB,CAAC,CAAC;QAChE,MAAM,KAAK,CAAC,KAAK,EAAE,CAAC;QACpB,MAAM,IAAI,CAAC,YAAY,CAAC,KAAK,EAAE,CAAC;IACjC,CAAC;IAED,KAAK,CAAC,IAAI;QACT,IAAI,CAAC,mBAAmB,CAAC,MAAM,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;QAChD,IAAI,CAAC,mBAAmB,CAAC,gBAAgB,EAAE,IAAI,CAAC,iBAAiB,CAAC,CAAC;QACnE,MAAM,KAAK,CAAC,IAAI,EAAE,CAAC;QACnB,MAAM,IAAI,CAAC,YAAY,CAAC,IAAI,EAAE,CAAC;IAChC,CAAC;IAED,KAAK,CAAC,IAAI;QACT,OAAO,IAAI,CAAC,YAAY,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;IACvC,CAAC;IACD,IAAI,MAAM;QACT,OAAO,IAAI,CAAC,YAAY,EAAE,MAAM,IAAI,IAAI,CAAC,OAAO,CAAC;IAClD,CAAC;IAED,SAAS;QACR,OAAO,IAAI,CAAC,YAAY,CAAC,SAAS,EAAE,CAAC;IACtC,CAAC;CACD"}
|
package/dist/src/remote.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type
|
|
1
|
+
import type { PeerId } from "@libp2p/interface";
|
|
2
|
+
import { type GetOptions, type Blocks as IBlocks } from "@peerbit/blocks-interface";
|
|
3
3
|
import { PublicSignKey } from "@peerbit/crypto";
|
|
4
4
|
import { AnyBlockStore } from "./any-blockstore.js";
|
|
5
|
-
import type {
|
|
6
|
-
|
|
5
|
+
import type { BlockStore } from "./interface.js";
|
|
6
|
+
export declare const logger: import("pino").Logger<never>;
|
|
7
7
|
export declare class BlockMessage {
|
|
8
8
|
}
|
|
9
9
|
export declare class BlockRequest extends BlockMessage {
|
|
@@ -56,5 +56,6 @@ export declare class RemoteBlocks implements IBlocks {
|
|
|
56
56
|
waitFor(peer: PeerId | PublicSignKey): Promise<void>;
|
|
57
57
|
size(): Promise<number>;
|
|
58
58
|
get status(): import("./interface.js").StoreStatus;
|
|
59
|
+
persisted(): boolean | Promise<boolean>;
|
|
59
60
|
}
|
|
60
61
|
//# sourceMappingURL=remote.d.ts.map
|
package/dist/src/remote.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"remote.d.ts","sourceRoot":"","sources":["../../src/remote.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"remote.d.ts","sourceRoot":"","sources":["../../src/remote.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAEhD,OAAO,EACN,KAAK,UAAU,EACf,KAAK,MAAM,IAAI,OAAO,EAKtB,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAMhD,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAEjD,eAAO,MAAM,MAAM,8BAAwC,CAAC;AAE5D,qBAAa,YAAY;CAAG;AAE5B,qBACa,YAAa,SAAQ,YAAY;IAE7C,GAAG,EAAE,MAAM,CAAC;gBAEA,GAAG,EAAE,MAAM;CAIvB;AAED,qBACa,aAAc,SAAQ,YAAY;IAE9C,GAAG,EAAE,MAAM,CAAC;IAGZ,KAAK,EAAE,UAAU,CAAC;gBAEN,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU;CAK1C;AAED,qBAAa,YAAa,YAAW,OAAO;IAiB1C,QAAQ,CAAC,OAAO,EAAE;QACjB,KAAK,EAAE,aAAa,CAAC;QACrB,YAAY,CAAC,EAAE,MAAM,CAAC;QACtB,4BAA4B,CAAC,EAAE,MAAM,CAAC;QACtC,OAA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AI,CAAC;IAM7D,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAO5B,SAAS,CAAC,IAAI,EAAE,YAAY;IAG5B,WAAW,CAAC,SAAS,EAAE,aAAa;YAMtB,kBAAkB;YAclB,cAAc;IAmFtB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAc3B,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC;IAI9C,IAAI;IAIV,IAAI,MAAM,yCAMT;IAED,SAAS,IAAI,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;CAGvC"}
|
package/dist/src/remote.js
CHANGED
|
@@ -7,15 +7,17 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
7
7
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
8
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
9
|
};
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
10
|
+
import { field, variant } from "@dao-xyz/borsh";
|
|
11
|
+
import { CustomEvent, TypedEventEmitter } from "@libp2p/interface";
|
|
12
|
+
import { checkDecodeBlock, cidifyString, codecCodes, stringifyCid, } from "@peerbit/blocks-interface";
|
|
13
|
+
import { PublicSignKey } from "@peerbit/crypto";
|
|
14
|
+
import { logger as loggerFn } from "@peerbit/logger";
|
|
15
|
+
import { AbortError } from "@peerbit/time";
|
|
12
16
|
import { CID } from "multiformats";
|
|
13
17
|
import {} from "multiformats/block";
|
|
14
|
-
import { PublicSignKey } from "@peerbit/crypto";
|
|
15
|
-
import { AnyBlockStore } from "./any-blockstore.js";
|
|
16
18
|
import PQueue from "p-queue";
|
|
17
|
-
import {
|
|
18
|
-
|
|
19
|
+
import { AnyBlockStore } from "./any-blockstore.js";
|
|
20
|
+
export const logger = loggerFn({ module: "blocks-remote" });
|
|
19
21
|
export class BlockMessage {
|
|
20
22
|
}
|
|
21
23
|
let BlockRequest = class BlockRequest extends BlockMessage {
|
|
@@ -70,7 +72,7 @@ export class RemoteBlocks {
|
|
|
70
72
|
this.options = options;
|
|
71
73
|
const localTimeout = options?.localTimeout || 1000;
|
|
72
74
|
this._loadFetchQueue = new PQueue({
|
|
73
|
-
concurrency: options?.messageProcessingConcurrency || 10
|
|
75
|
+
concurrency: options?.messageProcessingConcurrency || 10,
|
|
74
76
|
});
|
|
75
77
|
this.localStore = options?.local;
|
|
76
78
|
this._resolvers = new Map();
|
|
@@ -86,8 +88,8 @@ export class RemoteBlocks {
|
|
|
86
88
|
}
|
|
87
89
|
}
|
|
88
90
|
catch (error) {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
+
logger.error("Got error for libp2p block transport: ", error);
|
|
92
|
+
// timeout o r invalid cid
|
|
91
93
|
}
|
|
92
94
|
};
|
|
93
95
|
}
|
|
@@ -137,7 +139,7 @@ export class RemoteBlocks {
|
|
|
137
139
|
async handleFetchRequest(request, localTimeout) {
|
|
138
140
|
const cid = stringifyCid(request.cid);
|
|
139
141
|
const bytes = await this.localStore.get(cid, {
|
|
140
|
-
timeout: localTimeout
|
|
142
|
+
timeout: localTimeout,
|
|
141
143
|
});
|
|
142
144
|
if (!bytes) {
|
|
143
145
|
return;
|
|
@@ -156,13 +158,15 @@ export class RemoteBlocks {
|
|
|
156
158
|
clearTimeout(timeoutCallback);
|
|
157
159
|
this._resolvers.delete(cidString);
|
|
158
160
|
this.closeController.signal.removeEventListener("abort", abortHandler);
|
|
161
|
+
options?.signal?.removeEventListener("abort", abortHandler);
|
|
159
162
|
reject(new AbortError());
|
|
160
163
|
};
|
|
161
164
|
this.closeController.signal.addEventListener("abort", abortHandler);
|
|
165
|
+
options?.signal?.addEventListener("abort", abortHandler);
|
|
162
166
|
this._resolvers.set(cidString, async (bytes) => {
|
|
163
167
|
const value = await checkDecodeBlock(cidObject, bytes, {
|
|
164
168
|
codec,
|
|
165
|
-
hasher: options?.hasher
|
|
169
|
+
hasher: options?.hasher,
|
|
166
170
|
});
|
|
167
171
|
clearTimeout(timeoutCallback);
|
|
168
172
|
this._resolvers.delete(cidString); // TODO concurrency might not work as expected here
|
|
@@ -172,15 +176,18 @@ export class RemoteBlocks {
|
|
|
172
176
|
});
|
|
173
177
|
this._readFromPeersPromises.set(cidString, promise);
|
|
174
178
|
const publish = (to) => {
|
|
175
|
-
if (!options?.from || options.from.includes(to))
|
|
176
|
-
this.options.publish(new BlockRequest(cidString), {
|
|
179
|
+
if (!options?.from || options.from.includes(to)) {
|
|
180
|
+
return this.options.publish(new BlockRequest(cidString), {
|
|
181
|
+
to: [to],
|
|
182
|
+
});
|
|
183
|
+
}
|
|
177
184
|
};
|
|
178
185
|
const publishOnNewPeers = (e) => {
|
|
179
186
|
return publish(e.detail.hashcode());
|
|
180
187
|
};
|
|
181
188
|
this._events.addEventListener("peer:reachable", publishOnNewPeers);
|
|
182
189
|
this.options.publish(new BlockRequest(cidString), {
|
|
183
|
-
to: options.from
|
|
190
|
+
to: options.from,
|
|
184
191
|
});
|
|
185
192
|
// we want to make sure that if some new peers join, we also try to ask them
|
|
186
193
|
const result = await promise;
|
|
@@ -220,5 +227,8 @@ export class RemoteBlocks {
|
|
|
220
227
|
return "closed";
|
|
221
228
|
}
|
|
222
229
|
}
|
|
230
|
+
persisted() {
|
|
231
|
+
return this.localStore?.persisted() || false;
|
|
232
|
+
}
|
|
223
233
|
}
|
|
224
234
|
//# sourceMappingURL=remote.js.map
|
package/dist/src/remote.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"remote.js","sourceRoot":"","sources":["../../src/remote.ts"],"names":[],"mappings":";;;;;;;;;
|
|
1
|
+
{"version":3,"file":"remote.js","sourceRoot":"","sources":["../../src/remote.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAEhD,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AACnE,OAAO,EA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8BAA8B;QAC9B,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,CAAC;QAC7B,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,CAAC;QAC7B,MAAM,IAAI,CAAC,eAAe,CAAC,MAAM,EAAE,CAAC,CAAC,mBAAmB;QACxD,MAAM,IAAI,CAAC,UAAU,EAAE,IAAI,EAAE,CAAC;QAC9B,IAAI,CAAC,sBAAsB,CAAC,KAAK,EAAE,CAAC;QACpC,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,CAAC;QACxB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,wFAAwF;IACzF,CAAC;IAED,OAAO,CAAC,IAA4B;QACnC,OAAO,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IACnC,CAAC;IAED,KAAK,CAAC,IAAI;QACT,OAAO,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE,CAAC;IAC/B,CAAC;IAED,IAAI,MAAM;QACT,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;YAChB,OAAO,IAAI,CAAC,UAAU,EAAE,MAAM,EAAE,CAAC;QAClC,CAAC;aAAM,CAAC;YACP,OAAO,QAAQ,CAAC;QACjB,CAAC;IACF,CAAC;IAED,SAAS;QACR,OAAO,IAAI,CAAC,UAAU,EAAE,SAAS,EAAE,IAAI,KAAK,CAAC;IAC9C,CAAC;CACD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peerbit/blocks",
|
|
3
|
-
"version": "2.0
|
|
3
|
+
"version": "2.1.0-2bc15a6",
|
|
4
4
|
"description": "Block store streaming",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "./dist/src/index.d.ts",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
}
|
|
35
35
|
},
|
|
36
36
|
"eslintConfig": {
|
|
37
|
-
"extends": "
|
|
37
|
+
"extends": "peerbit",
|
|
38
38
|
"parserOptions": {
|
|
39
39
|
"project": true,
|
|
40
40
|
"sourceType": "module"
|
|
@@ -51,7 +51,8 @@
|
|
|
51
51
|
"scripts": {
|
|
52
52
|
"clean": "aegir clean",
|
|
53
53
|
"build": "aegir build --no-bundle",
|
|
54
|
-
"test": "aegir test --target node"
|
|
54
|
+
"test": "aegir test --target node",
|
|
55
|
+
"lint": "aegir lint"
|
|
55
56
|
},
|
|
56
57
|
"engines": {
|
|
57
58
|
"node": ">=16.15.1"
|
|
@@ -73,16 +74,16 @@
|
|
|
73
74
|
"dao.xyz"
|
|
74
75
|
],
|
|
75
76
|
"devDependencies": {
|
|
76
|
-
"@peerbit/libp2p-test-utils": "2.1.
|
|
77
|
+
"@peerbit/libp2p-test-utils": "2.1.4-2bc15a6"
|
|
77
78
|
},
|
|
78
79
|
"dependencies": {
|
|
79
|
-
"@dao-xyz/borsh": "^5.2.
|
|
80
|
-
"@peerbit/any-store": "2.0
|
|
81
|
-
"@peerbit/stream": "4.0.
|
|
82
|
-
"@peerbit/blocks-interface": "1.
|
|
83
|
-
"@peerbit/crypto": "2.
|
|
80
|
+
"@dao-xyz/borsh": "^5.2.3",
|
|
81
|
+
"@peerbit/any-store": "2.1.0-2bc15a6",
|
|
82
|
+
"@peerbit/stream": "4.0.6-2bc15a6",
|
|
83
|
+
"@peerbit/blocks-interface": "1.3.0-2bc15a6",
|
|
84
|
+
"@peerbit/crypto": "2.3.0-2bc15a6",
|
|
84
85
|
"@ipld/dag-cbor": "^9.0.2",
|
|
85
|
-
"libp2p": "^1.
|
|
86
|
+
"libp2p": "^1.8.1",
|
|
86
87
|
"multiformats": "^13.0.1"
|
|
87
88
|
}
|
|
88
89
|
}
|
package/src/any-blockstore.ts
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type PeerId } from "@libp2p/interface";
|
|
2
|
+
import { createStore } from "@peerbit/any-store";
|
|
3
|
+
import { type AnyStore } from "@peerbit/any-store-interface";
|
|
2
4
|
import {
|
|
5
|
+
type Blocks,
|
|
3
6
|
cidifyString,
|
|
4
7
|
codecCodes,
|
|
5
8
|
createBlock,
|
|
6
9
|
defaultHasher,
|
|
7
|
-
stringifyCid
|
|
8
|
-
} from "
|
|
9
|
-
import {
|
|
10
|
+
stringifyCid,
|
|
11
|
+
} from "@peerbit/blocks-interface";
|
|
12
|
+
import { type PublicSignKey } from "@peerbit/crypto";
|
|
10
13
|
import { waitFor } from "@peerbit/time";
|
|
11
|
-
import { type
|
|
12
|
-
import { PublicSignKey } from "@peerbit/crypto";
|
|
13
|
-
import { createStore } from "@peerbit/any-store";
|
|
14
|
-
import { type AnyStore } from "@peerbit/any-store-interface";
|
|
14
|
+
import { type Block, decode } from "multiformats/block";
|
|
15
15
|
|
|
16
16
|
export class AnyBlockStore implements Blocks {
|
|
17
17
|
private _store: AnyStore;
|
|
@@ -29,7 +29,7 @@ export class AnyBlockStore implements Blocks {
|
|
|
29
29
|
links?: string[];
|
|
30
30
|
timeout?: number;
|
|
31
31
|
hasher?: any;
|
|
32
|
-
}
|
|
32
|
+
},
|
|
33
33
|
): Promise<Uint8Array | undefined> {
|
|
34
34
|
const cidObject = cidifyString(cid);
|
|
35
35
|
try {
|
|
@@ -41,7 +41,7 @@ export class AnyBlockStore implements Blocks {
|
|
|
41
41
|
const block = await decode({
|
|
42
42
|
bytes,
|
|
43
43
|
codec,
|
|
44
|
-
hasher: options?.hasher || defaultHasher
|
|
44
|
+
hasher: options?.hasher || defaultHasher,
|
|
45
45
|
});
|
|
46
46
|
return (block as Block<Uint8Array, any, any, any>).bytes;
|
|
47
47
|
} catch (error: any) {
|
|
@@ -87,8 +87,8 @@ export class AnyBlockStore implements Blocks {
|
|
|
87
87
|
{
|
|
88
88
|
delayInterval: 100,
|
|
89
89
|
timeout: 10 * 1000,
|
|
90
|
-
signal: this._closeController.signal
|
|
91
|
-
}
|
|
90
|
+
signal: this._closeController.signal,
|
|
91
|
+
},
|
|
92
92
|
);
|
|
93
93
|
await this._opening;
|
|
94
94
|
} finally {
|
|
@@ -97,7 +97,7 @@ export class AnyBlockStore implements Blocks {
|
|
|
97
97
|
}
|
|
98
98
|
|
|
99
99
|
async stop(): Promise<void> {
|
|
100
|
-
this._onClose
|
|
100
|
+
this._onClose?.();
|
|
101
101
|
this._closeController?.abort();
|
|
102
102
|
return this._store.close();
|
|
103
103
|
}
|
|
@@ -106,10 +106,14 @@ export class AnyBlockStore implements Blocks {
|
|
|
106
106
|
return this._store.status();
|
|
107
107
|
}
|
|
108
108
|
async waitFor(peer: PeerId | PublicSignKey): Promise<void> {
|
|
109
|
-
|
|
109
|
+
// Offline storage // TODO this feels off resolving
|
|
110
110
|
}
|
|
111
111
|
|
|
112
112
|
async size() {
|
|
113
113
|
return this._store.size();
|
|
114
114
|
}
|
|
115
|
+
|
|
116
|
+
persisted(): boolean | Promise<boolean> {
|
|
117
|
+
return this._store.persisted();
|
|
118
|
+
}
|
|
115
119
|
}
|
package/src/index.ts
CHANGED
package/src/libp2p.ts
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { deserialize, serialize } from "@dao-xyz/borsh";
|
|
2
|
+
import { createStore } from "@peerbit/any-store";
|
|
3
|
+
import type { GetOptions, Blocks as IBlocks } from "@peerbit/blocks-interface";
|
|
4
|
+
import { type PublicSignKey } from "@peerbit/crypto";
|
|
2
5
|
import { DirectStream } from "@peerbit/stream";
|
|
3
6
|
import { type DirectStreamComponents } from "@peerbit/stream";
|
|
7
|
+
import { AnyWhere, type DataMessage } from "@peerbit/stream-interface";
|
|
4
8
|
import { AnyBlockStore } from "./any-blockstore.js";
|
|
5
|
-
import type { GetOptions } from "@peerbit/blocks-interface";
|
|
6
|
-
import { createStore } from "@peerbit/any-store";
|
|
7
9
|
import { BlockMessage, RemoteBlocks } from "./remote.js";
|
|
8
|
-
import { PublicSignKey } from "@peerbit/crypto";
|
|
9
|
-
import { AnyWhere, DataMessage } from "@peerbit/stream-interface";
|
|
10
|
-
import { deserialize, serialize } from "@dao-xyz/borsh";
|
|
11
10
|
|
|
12
11
|
export type DirectBlockComponents = DirectStreamComponents;
|
|
13
12
|
|
|
@@ -23,15 +22,15 @@ export class DirectBlock extends DirectStream implements IBlocks {
|
|
|
23
22
|
canRelayMessage?: boolean;
|
|
24
23
|
localTimeout?: number;
|
|
25
24
|
messageProcessingConcurrency?: number;
|
|
26
|
-
}
|
|
25
|
+
},
|
|
27
26
|
) {
|
|
28
27
|
super(components, ["/lazyblock/0.0.0"], {
|
|
29
28
|
messageProcessingConcurrency: options?.messageProcessingConcurrency || 10,
|
|
30
29
|
canRelayMessage: options?.canRelayMessage ?? true,
|
|
31
30
|
connectionManager: {
|
|
32
31
|
dialer: false,
|
|
33
|
-
pruner: false
|
|
34
|
-
}
|
|
32
|
+
pruner: false,
|
|
33
|
+
},
|
|
35
34
|
});
|
|
36
35
|
this.remoteBlocks = new RemoteBlocks({
|
|
37
36
|
local: new AnyBlockStore(createStore(options?.directory)),
|
|
@@ -39,14 +38,14 @@ export class DirectBlock extends DirectStream implements IBlocks {
|
|
|
39
38
|
this.publish(serialize(message), { mode: new AnyWhere() }),
|
|
40
39
|
localTimeout: options?.localTimeout || 1000,
|
|
41
40
|
messageProcessingConcurrency: options?.messageProcessingConcurrency || 10,
|
|
42
|
-
waitFor: this.waitFor.bind(this)
|
|
41
|
+
waitFor: this.waitFor.bind(this),
|
|
43
42
|
});
|
|
44
43
|
|
|
45
44
|
this.onDataFn = (data: CustomEvent<DataMessage>) => {
|
|
46
45
|
data.detail?.data?.length &&
|
|
47
46
|
data.detail?.data.length > 0 &&
|
|
48
47
|
this.remoteBlocks.onMessage(
|
|
49
|
-
deserialize(data.detail.data!, BlockMessage)
|
|
48
|
+
deserialize(data.detail.data!, BlockMessage),
|
|
50
49
|
);
|
|
51
50
|
};
|
|
52
51
|
this.onPeerConnectedFn = (evt: CustomEvent<PublicSignKey>) =>
|
|
@@ -62,7 +61,7 @@ export class DirectBlock extends DirectStream implements IBlocks {
|
|
|
62
61
|
}
|
|
63
62
|
async get(
|
|
64
63
|
cid: string,
|
|
65
|
-
options?: GetOptions | undefined
|
|
64
|
+
options?: GetOptions | undefined,
|
|
66
65
|
): Promise<Uint8Array | undefined> {
|
|
67
66
|
return this.remoteBlocks.get(cid, options);
|
|
68
67
|
}
|
|
@@ -97,4 +96,8 @@ export class DirectBlock extends DirectStream implements IBlocks {
|
|
|
97
96
|
get status() {
|
|
98
97
|
return this.remoteBlocks?.status || this.started;
|
|
99
98
|
}
|
|
99
|
+
|
|
100
|
+
persisted(): boolean | Promise<boolean> {
|
|
101
|
+
return this.remoteBlocks.persisted();
|
|
102
|
+
}
|
|
100
103
|
}
|
package/src/remote.ts
CHANGED
|
@@ -1,23 +1,26 @@
|
|
|
1
|
-
import
|
|
2
|
-
import type {
|
|
1
|
+
import { field, variant } from "@dao-xyz/borsh";
|
|
2
|
+
import type { PeerId } from "@libp2p/interface";
|
|
3
|
+
import { CustomEvent, TypedEventEmitter } from "@libp2p/interface";
|
|
3
4
|
import {
|
|
4
|
-
|
|
5
|
+
type GetOptions,
|
|
6
|
+
type Blocks as IBlocks,
|
|
7
|
+
checkDecodeBlock,
|
|
5
8
|
cidifyString,
|
|
6
9
|
codecCodes,
|
|
7
|
-
|
|
8
|
-
} from "
|
|
9
|
-
import {
|
|
10
|
+
stringifyCid,
|
|
11
|
+
} from "@peerbit/blocks-interface";
|
|
12
|
+
import { PublicSignKey } from "@peerbit/crypto";
|
|
13
|
+
import { logger as loggerFn } from "@peerbit/logger";
|
|
14
|
+
import { AbortError } from "@peerbit/time";
|
|
10
15
|
import { CID } from "multiformats";
|
|
11
16
|
import { type Block } from "multiformats/block";
|
|
12
|
-
import { PublicSignKey } from "@peerbit/crypto";
|
|
13
|
-
import { AnyBlockStore } from "./any-blockstore.js";
|
|
14
|
-
import type { GetOptions } from "@peerbit/blocks-interface";
|
|
15
17
|
import PQueue from "p-queue";
|
|
16
|
-
import {
|
|
17
|
-
import type {
|
|
18
|
-
|
|
18
|
+
import { AnyBlockStore } from "./any-blockstore.js";
|
|
19
|
+
import type { BlockStore } from "./interface.js";
|
|
20
|
+
|
|
21
|
+
export const logger = loggerFn({ module: "blocks-remote" });
|
|
19
22
|
|
|
20
|
-
export class BlockMessage {
|
|
23
|
+
export class BlockMessage {}
|
|
21
24
|
|
|
22
25
|
@variant(0)
|
|
23
26
|
export class BlockRequest extends BlockMessage {
|
|
@@ -68,14 +71,14 @@ export class RemoteBlocks implements IBlocks {
|
|
|
68
71
|
messageProcessingConcurrency?: number;
|
|
69
72
|
publish: (
|
|
70
73
|
message: BlockRequest | BlockResponse,
|
|
71
|
-
options?: { to?: string[] }
|
|
74
|
+
options?: { to?: string[] },
|
|
72
75
|
) => Promise<Uint8Array | void>;
|
|
73
76
|
waitFor(peer: PeerId | PublicSignKey): Promise<void>;
|
|
74
|
-
}
|
|
77
|
+
},
|
|
75
78
|
) {
|
|
76
79
|
const localTimeout = options?.localTimeout || 1000;
|
|
77
80
|
this._loadFetchQueue = new PQueue({
|
|
78
|
-
concurrency: options?.messageProcessingConcurrency || 10
|
|
81
|
+
concurrency: options?.messageProcessingConcurrency || 10,
|
|
79
82
|
});
|
|
80
83
|
this.localStore = options?.local;
|
|
81
84
|
this._resolvers = new Map();
|
|
@@ -85,7 +88,7 @@ export class RemoteBlocks implements IBlocks {
|
|
|
85
88
|
try {
|
|
86
89
|
if (message instanceof BlockRequest && this.localStore) {
|
|
87
90
|
this._loadFetchQueue.add(() =>
|
|
88
|
-
this.handleFetchRequest(message, localTimeout)
|
|
91
|
+
this.handleFetchRequest(message, localTimeout),
|
|
89
92
|
);
|
|
90
93
|
} else if (message instanceof BlockResponse) {
|
|
91
94
|
// TODO make sure we are not storing too much bytes in ram (like filter large blocks)
|
|
@@ -93,8 +96,8 @@ export class RemoteBlocks implements IBlocks {
|
|
|
93
96
|
this._resolvers.get(message.cid)?.(message.bytes);
|
|
94
97
|
}
|
|
95
98
|
} catch (error) {
|
|
96
|
-
|
|
97
|
-
|
|
99
|
+
logger.error("Got error for libp2p block transport: ", error);
|
|
100
|
+
// timeout o r invalid cid
|
|
98
101
|
}
|
|
99
102
|
};
|
|
100
103
|
}
|
|
@@ -111,7 +114,7 @@ export class RemoteBlocks implements IBlocks {
|
|
|
111
114
|
}
|
|
112
115
|
async get(
|
|
113
116
|
cid: string,
|
|
114
|
-
options?: GetOptions | undefined
|
|
117
|
+
options?: GetOptions | undefined,
|
|
115
118
|
): Promise<Uint8Array | undefined> {
|
|
116
119
|
const cidObject = cidifyString(cid);
|
|
117
120
|
let value = this.localStore
|
|
@@ -150,17 +153,17 @@ export class RemoteBlocks implements IBlocks {
|
|
|
150
153
|
}
|
|
151
154
|
onReachable(publicKey: PublicSignKey) {
|
|
152
155
|
this._events.dispatchEvent(
|
|
153
|
-
new CustomEvent("peer:reachable", { detail: publicKey })
|
|
156
|
+
new CustomEvent("peer:reachable", { detail: publicKey }),
|
|
154
157
|
);
|
|
155
158
|
}
|
|
156
159
|
|
|
157
160
|
private async handleFetchRequest(
|
|
158
161
|
request: BlockRequest,
|
|
159
|
-
localTimeout: number
|
|
162
|
+
localTimeout: number,
|
|
160
163
|
) {
|
|
161
164
|
const cid = stringifyCid(request.cid);
|
|
162
165
|
const bytes = await this.localStore.get(cid, {
|
|
163
|
-
timeout: localTimeout
|
|
166
|
+
timeout: localTimeout,
|
|
164
167
|
});
|
|
165
168
|
if (!bytes) {
|
|
166
169
|
return;
|
|
@@ -171,7 +174,12 @@ export class RemoteBlocks implements IBlocks {
|
|
|
171
174
|
private async _readFromPeers(
|
|
172
175
|
cidString: string,
|
|
173
176
|
cidObject: CID,
|
|
174
|
-
options: {
|
|
177
|
+
options: {
|
|
178
|
+
signal?: AbortSignal;
|
|
179
|
+
timeout?: number;
|
|
180
|
+
hasher?: any;
|
|
181
|
+
from?: string[];
|
|
182
|
+
} = {},
|
|
175
183
|
): Promise<Uint8Array | undefined> {
|
|
176
184
|
const codec = (codecCodes as any)[cidObject.code];
|
|
177
185
|
let promise = this._readFromPeersPromises.get(cidString);
|
|
@@ -182,41 +190,46 @@ export class RemoteBlocks implements IBlocks {
|
|
|
182
190
|
() => {
|
|
183
191
|
resolve(undefined);
|
|
184
192
|
},
|
|
185
|
-
options.timeout || 30 * 1000
|
|
193
|
+
options.timeout || 30 * 1000,
|
|
186
194
|
);
|
|
187
195
|
const abortHandler = () => {
|
|
188
196
|
clearTimeout(timeoutCallback);
|
|
189
197
|
this._resolvers.delete(cidString);
|
|
190
198
|
this.closeController.signal.removeEventListener(
|
|
191
199
|
"abort",
|
|
192
|
-
abortHandler
|
|
200
|
+
abortHandler,
|
|
193
201
|
);
|
|
202
|
+
options?.signal?.removeEventListener("abort", abortHandler);
|
|
194
203
|
reject(new AbortError());
|
|
195
204
|
};
|
|
196
205
|
this.closeController.signal.addEventListener("abort", abortHandler);
|
|
206
|
+
options?.signal?.addEventListener("abort", abortHandler);
|
|
197
207
|
|
|
198
208
|
this._resolvers.set(cidString, async (bytes: Uint8Array) => {
|
|
199
209
|
const value = await checkDecodeBlock(cidObject, bytes, {
|
|
200
210
|
codec,
|
|
201
|
-
hasher: options?.hasher
|
|
211
|
+
hasher: options?.hasher,
|
|
202
212
|
});
|
|
203
213
|
|
|
204
214
|
clearTimeout(timeoutCallback);
|
|
205
215
|
this._resolvers.delete(cidString); // TODO concurrency might not work as expected here
|
|
206
216
|
this.closeController.signal.removeEventListener(
|
|
207
217
|
"abort",
|
|
208
|
-
abortHandler
|
|
218
|
+
abortHandler,
|
|
209
219
|
);
|
|
210
220
|
resolve(value);
|
|
211
221
|
});
|
|
212
|
-
}
|
|
222
|
+
},
|
|
213
223
|
);
|
|
214
224
|
|
|
215
225
|
this._readFromPeersPromises.set(cidString, promise);
|
|
216
226
|
|
|
217
227
|
const publish = (to: string) => {
|
|
218
|
-
if (!options?.from || options.from.includes(to))
|
|
219
|
-
this.options.publish(new BlockRequest(cidString), {
|
|
228
|
+
if (!options?.from || options.from.includes(to)) {
|
|
229
|
+
return this.options.publish(new BlockRequest(cidString), {
|
|
230
|
+
to: [to],
|
|
231
|
+
});
|
|
232
|
+
}
|
|
220
233
|
};
|
|
221
234
|
|
|
222
235
|
const publishOnNewPeers = (e: CustomEvent<PublicSignKey>) => {
|
|
@@ -224,7 +237,7 @@ export class RemoteBlocks implements IBlocks {
|
|
|
224
237
|
};
|
|
225
238
|
this._events.addEventListener("peer:reachable", publishOnNewPeers);
|
|
226
239
|
this.options.publish(new BlockRequest(cidString), {
|
|
227
|
-
to: options.from
|
|
240
|
+
to: options.from,
|
|
228
241
|
});
|
|
229
242
|
|
|
230
243
|
// we want to make sure that if some new peers join, we also try to ask them
|
|
@@ -270,4 +283,8 @@ export class RemoteBlocks implements IBlocks {
|
|
|
270
283
|
return "closed";
|
|
271
284
|
}
|
|
272
285
|
}
|
|
286
|
+
|
|
287
|
+
persisted(): boolean | Promise<boolean> {
|
|
288
|
+
return this.localStore?.persisted() || false;
|
|
289
|
+
}
|
|
273
290
|
}
|
package/dist/src/block.d.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { CID } from "multiformats";
|
|
2
|
-
import * as raw from "multiformats/codecs/raw";
|
|
3
|
-
import * as dagCbor from "@ipld/dag-cbor";
|
|
4
|
-
import { type Block } from "multiformats/block";
|
|
5
|
-
import type { MultihashHasher } from "multiformats/hashes/interface";
|
|
6
|
-
export declare const defaultHasher: import("multiformats/dist/src/hashes/hasher").Hasher<"sha2-256", 18>;
|
|
7
|
-
export declare const codecCodes: {
|
|
8
|
-
113: typeof dagCbor;
|
|
9
|
-
85: typeof raw;
|
|
10
|
-
};
|
|
11
|
-
export declare const codecMap: {
|
|
12
|
-
raw: typeof raw;
|
|
13
|
-
"dag-cbor": typeof dagCbor;
|
|
14
|
-
};
|
|
15
|
-
export declare const cidifyString: (str: string) => CID;
|
|
16
|
-
export declare const stringifyCid: (cid: any) => string;
|
|
17
|
-
export declare const checkDecodeBlock: (expectedCID: CID | string, bytes: Uint8Array, options: {
|
|
18
|
-
hasher?: any;
|
|
19
|
-
codec?: any;
|
|
20
|
-
}) => Promise<Block<any, any, any, any>>;
|
|
21
|
-
export declare const getBlockValue: <T>(block: Block<T, any, any, any>, links?: string[]) => Promise<T>;
|
|
22
|
-
export declare const prepareBlockWrite: (value: any, codec: any, links?: string[]) => any;
|
|
23
|
-
export declare const createBlock: (value: any, format: string, options?: {
|
|
24
|
-
hasher?: MultihashHasher<number>;
|
|
25
|
-
links?: string[];
|
|
26
|
-
}) => Promise<Block<any, any, any, any>>;
|
|
27
|
-
//# sourceMappingURL=block.d.ts.map
|
package/dist/src/block.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"block.d.ts","sourceRoot":"","sources":["../../src/block.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AACnC,OAAO,KAAK,GAAG,MAAM,yBAAyB,CAAC;AAC/C,OAAO,KAAK,OAAO,MAAM,gBAAgB,CAAC;AAG1C,OAAO,EAAE,KAAK,KAAK,EAAkB,MAAM,oBAAoB,CAAC;AAChE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC;AAMrE,eAAO,MAAM,aAAa,sEAAS,CAAC;AAEpC,eAAO,MAAM,UAAU;;;CAGtB,CAAC;AACF,eAAO,MAAM,QAAQ;;;CAGpB,CAAC;AAEF,eAAO,MAAM,YAAY,QAAS,MAAM,KAAG,GAM1C,CAAC;AAEF,eAAO,MAAM,YAAY,QAAS,GAAG,KAAG,MASvC,CAAC;AAEF,eAAO,MAAM,gBAAgB,gBACf,GAAG,GAAG,MAAM,SAClB,UAAU,WACR;IAAE,MAAM,CAAC,EAAE,GAAG,CAAC;IAAC,KAAK,CAAC,EAAE,GAAG,CAAA;CAAE,KACpC,QAAQ,MAAM,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAanC,CAAC;AACF,eAAO,MAAM,aAAa,aAClB,MAAM,CAAC,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,UACtB,MAAM,EAAE,KACd,QAAQ,CAAC,CAiBX,CAAC;AAEF,eAAO,MAAM,iBAAiB,UAAW,GAAG,SAAS,GAAG,UAAU,MAAM,EAAE,QAazE,CAAC;AAEF,eAAO,MAAM,WAAW,UAChB,GAAG,UACF,MAAM,YACJ;IACT,MAAM,CAAC,EAAE,gBAAgB,MAAM,CAAC,CAAC;IACjC,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;CACjB,KACC,QAAQ,MAAM,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CASnC,CAAC"}
|
package/dist/src/block.js
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import { CID } from "multiformats";
|
|
2
|
-
import * as raw from "multiformats/codecs/raw";
|
|
3
|
-
import * as dagCbor from "@ipld/dag-cbor";
|
|
4
|
-
import { sha256 } from "multiformats/hashes/sha2";
|
|
5
|
-
import { base58btc } from "multiformats/bases/base58";
|
|
6
|
-
import { encode, decode } from "multiformats/block";
|
|
7
|
-
const unsupportedCodecError = () => new Error("unsupported codec");
|
|
8
|
-
const defaultBase = base58btc;
|
|
9
|
-
export const defaultHasher = sha256;
|
|
10
|
-
export const codecCodes = {
|
|
11
|
-
[dagCbor.code]: dagCbor,
|
|
12
|
-
[raw.code]: raw
|
|
13
|
-
};
|
|
14
|
-
export const codecMap = {
|
|
15
|
-
raw: raw,
|
|
16
|
-
"dag-cbor": dagCbor
|
|
17
|
-
};
|
|
18
|
-
export const cidifyString = (str) => {
|
|
19
|
-
if (!str) {
|
|
20
|
-
return str; // TODO fix types
|
|
21
|
-
}
|
|
22
|
-
return CID.parse(str, defaultBase);
|
|
23
|
-
};
|
|
24
|
-
export const stringifyCid = (cid) => {
|
|
25
|
-
if (!cid || typeof cid === "string") {
|
|
26
|
-
return cid;
|
|
27
|
-
}
|
|
28
|
-
if (cid["/"]) {
|
|
29
|
-
return defaultBase.encode(cid["/"]);
|
|
30
|
-
}
|
|
31
|
-
return cid.toString(defaultBase);
|
|
32
|
-
};
|
|
33
|
-
export const checkDecodeBlock = async (expectedCID, bytes, options) => {
|
|
34
|
-
const cidObject = typeof expectedCID === "string" ? cidifyString(expectedCID) : expectedCID;
|
|
35
|
-
const codec = options.codec || codecCodes[cidObject.code];
|
|
36
|
-
const block = await decode({
|
|
37
|
-
bytes: bytes,
|
|
38
|
-
codec,
|
|
39
|
-
hasher: options?.hasher || defaultHasher
|
|
40
|
-
});
|
|
41
|
-
if (!block.cid.equals(cidObject)) {
|
|
42
|
-
throw new Error("CID does not match");
|
|
43
|
-
}
|
|
44
|
-
return block;
|
|
45
|
-
};
|
|
46
|
-
export const getBlockValue = async (block, links) => {
|
|
47
|
-
if (block.cid.code === dagCbor.code) {
|
|
48
|
-
const value = block.value;
|
|
49
|
-
links = links || [];
|
|
50
|
-
links.forEach((prop) => {
|
|
51
|
-
if (value[prop]) {
|
|
52
|
-
value[prop] = Array.isArray(value[prop])
|
|
53
|
-
? value[prop].map(stringifyCid)
|
|
54
|
-
: stringifyCid(value[prop]);
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
|
-
return value;
|
|
58
|
-
}
|
|
59
|
-
if (block.cid.code === raw.code) {
|
|
60
|
-
return block.value;
|
|
61
|
-
}
|
|
62
|
-
throw new Error("Unsupported");
|
|
63
|
-
};
|
|
64
|
-
export const prepareBlockWrite = (value, codec, links) => {
|
|
65
|
-
if (!codec)
|
|
66
|
-
throw unsupportedCodecError();
|
|
67
|
-
if (codec.code === dagCbor.code) {
|
|
68
|
-
links = links || [];
|
|
69
|
-
links.forEach((prop) => {
|
|
70
|
-
if (value[prop]) {
|
|
71
|
-
value[prop] = Array.isArray(value[prop])
|
|
72
|
-
? value[prop].map(cidifyString)
|
|
73
|
-
: cidifyString(value[prop]);
|
|
74
|
-
}
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
return value;
|
|
78
|
-
};
|
|
79
|
-
export const createBlock = async (value, format, options) => {
|
|
80
|
-
const codec = codecMap[format];
|
|
81
|
-
value = prepareBlockWrite(value, codec, options?.links);
|
|
82
|
-
const block = await encode({
|
|
83
|
-
value,
|
|
84
|
-
codec,
|
|
85
|
-
hasher: options?.hasher || defaultHasher
|
|
86
|
-
});
|
|
87
|
-
return block;
|
|
88
|
-
};
|
|
89
|
-
//# sourceMappingURL=block.js.map
|
package/dist/src/block.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"block.js","sourceRoot":"","sources":["../../src/block.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AACnC,OAAO,KAAK,GAAG,MAAM,yBAAyB,CAAC;AAC/C,OAAO,KAAK,OAAO,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAC;AAClD,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AACtD,OAAO,EAAc,MAAM,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAGhE,MAAM,qBAAqB,GAAG,GAAG,EAAE,CAAC,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAAC;AAEnE,MAAM,WAAW,GAAG,SAAS,CAAC;AAE9B,MAAM,CAAC,MAAM,aAAa,GAAG,MAAM,CAAC;AAEpC,MAAM,CAAC,MAAM,UAAU,GAAG;IACzB,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,OAAO;IACvB,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,GAAG;CACf,CAAC;AACF,MAAM,CAAC,MAAM,QAAQ,GAAG;IACvB,GAAG,EAAE,GAAG;IACR,UAAU,EAAE,OAAO;CACnB,CAAC;AAEF,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,GAAW,EAAO,EAAE;IAChD,IAAI,CAAC,GAAG,EAAE,CAAC;QACV,OAAO,GAAiB,CAAC,CAAC,iBAAiB;IAC5C,CAAC;IAED,OAAO,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CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC;YAC9B,CAAC;QACF,CAAC,CAAC,CAAC;IACJ,CAAC;IACD,OAAO,KAAK,CAAC;AACd,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,WAAW,GAAG,KAAK,EAC/B,KAAU,EACV,MAAc,EACd,OAGC,EACoC,EAAE;IACvC,MAAM,KAAK,GAAI,QAAgB,CAAC,MAAM,CAAC,CAAC;IACxC,KAAK,GAAG,iBAAiB,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,CAAC,CAAC;IACxD,MAAM,KAAK,GAAG,MAAM,MAAM,CAAC;QAC1B,KAAK;QACL,KAAK;QACL,MAAM,EAAE,OAAO,EAAE,MAAM,IAAI,aAAa;KACxC,CAAC,CAAC;IACH,OAAO,KAAkC,CAAC;AAC3C,CAAC,CAAC"}
|
package/src/block.ts
DELETED
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
import { CID } from "multiformats";
|
|
2
|
-
import * as raw from "multiformats/codecs/raw";
|
|
3
|
-
import * as dagCbor from "@ipld/dag-cbor";
|
|
4
|
-
import { sha256 } from "multiformats/hashes/sha2";
|
|
5
|
-
import { base58btc } from "multiformats/bases/base58";
|
|
6
|
-
import { type Block, encode, decode } from "multiformats/block";
|
|
7
|
-
import type { MultihashHasher } from "multiformats/hashes/interface";
|
|
8
|
-
|
|
9
|
-
const unsupportedCodecError = () => new Error("unsupported codec");
|
|
10
|
-
|
|
11
|
-
const defaultBase = base58btc;
|
|
12
|
-
|
|
13
|
-
export const defaultHasher = sha256;
|
|
14
|
-
|
|
15
|
-
export const codecCodes = {
|
|
16
|
-
[dagCbor.code]: dagCbor,
|
|
17
|
-
[raw.code]: raw
|
|
18
|
-
};
|
|
19
|
-
export const codecMap = {
|
|
20
|
-
raw: raw,
|
|
21
|
-
"dag-cbor": dagCbor
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
export const cidifyString = (str: string): CID => {
|
|
25
|
-
if (!str) {
|
|
26
|
-
return str as any as CID; // TODO fix types
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
return CID.parse(str, defaultBase);
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
export const stringifyCid = (cid: any): string => {
|
|
33
|
-
if (!cid || typeof cid === "string") {
|
|
34
|
-
return cid;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
if (cid["/"]) {
|
|
38
|
-
return defaultBase.encode(cid["/"]);
|
|
39
|
-
}
|
|
40
|
-
return cid.toString(defaultBase);
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
export const checkDecodeBlock = async (
|
|
44
|
-
expectedCID: CID | string,
|
|
45
|
-
bytes: Uint8Array,
|
|
46
|
-
options: { hasher?: any; codec?: any }
|
|
47
|
-
): Promise<Block<any, any, any, any>> => {
|
|
48
|
-
const cidObject =
|
|
49
|
-
typeof expectedCID === "string" ? cidifyString(expectedCID) : expectedCID;
|
|
50
|
-
const codec = options.codec || (codecCodes as any)[cidObject.code];
|
|
51
|
-
const block = await decode({
|
|
52
|
-
bytes: bytes,
|
|
53
|
-
codec,
|
|
54
|
-
hasher: options?.hasher || defaultHasher
|
|
55
|
-
});
|
|
56
|
-
if (!block.cid.equals(cidObject)) {
|
|
57
|
-
throw new Error("CID does not match");
|
|
58
|
-
}
|
|
59
|
-
return block as Block<any, any, any, 1>;
|
|
60
|
-
};
|
|
61
|
-
export const getBlockValue = async <T>(
|
|
62
|
-
block: Block<T, any, any, any>,
|
|
63
|
-
links?: string[]
|
|
64
|
-
): Promise<T> => {
|
|
65
|
-
if (block.cid.code === dagCbor.code) {
|
|
66
|
-
const value = block.value as any;
|
|
67
|
-
links = links || [];
|
|
68
|
-
links.forEach((prop) => {
|
|
69
|
-
if (value[prop]) {
|
|
70
|
-
value[prop] = Array.isArray(value[prop])
|
|
71
|
-
? value[prop].map(stringifyCid)
|
|
72
|
-
: stringifyCid(value[prop]);
|
|
73
|
-
}
|
|
74
|
-
});
|
|
75
|
-
return value;
|
|
76
|
-
}
|
|
77
|
-
if (block.cid.code === raw.code) {
|
|
78
|
-
return block.value as T;
|
|
79
|
-
}
|
|
80
|
-
throw new Error("Unsupported");
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
export const prepareBlockWrite = (value: any, codec: any, links?: string[]) => {
|
|
84
|
-
if (!codec) throw unsupportedCodecError();
|
|
85
|
-
if (codec.code === dagCbor.code) {
|
|
86
|
-
links = links || [];
|
|
87
|
-
links.forEach((prop) => {
|
|
88
|
-
if (value[prop]) {
|
|
89
|
-
value[prop] = Array.isArray(value[prop])
|
|
90
|
-
? value[prop].map(cidifyString)
|
|
91
|
-
: cidifyString(value[prop]);
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
return value;
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
export const createBlock = async (
|
|
99
|
-
value: any,
|
|
100
|
-
format: string,
|
|
101
|
-
options?: {
|
|
102
|
-
hasher?: MultihashHasher<number>;
|
|
103
|
-
links?: string[];
|
|
104
|
-
}
|
|
105
|
-
): Promise<Block<any, any, any, any>> => {
|
|
106
|
-
const codec = (codecMap as any)[format];
|
|
107
|
-
value = prepareBlockWrite(value, codec, options?.links);
|
|
108
|
-
const block = await encode({
|
|
109
|
-
value,
|
|
110
|
-
codec,
|
|
111
|
-
hasher: options?.hasher || defaultHasher
|
|
112
|
-
});
|
|
113
|
-
return block as Block<any, any, any, any>;
|
|
114
|
-
};
|