@peerbit/blocks 1.1.7 → 2.0.2
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/lib/esm/{level.d.ts → any-blockstore.d.ts} +7 -11
- package/lib/esm/{level.js → any-blockstore.js} +21 -30
- package/lib/esm/any-blockstore.js.map +1 -0
- package/lib/esm/block.d.ts +2 -2
- package/lib/esm/block.js.map +1 -1
- package/lib/esm/index.d.ts +3 -2
- package/lib/esm/index.js +3 -2
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/{store.d.ts → interface.d.ts} +2 -1
- package/lib/esm/interface.js +2 -0
- package/lib/esm/interface.js.map +1 -0
- package/lib/esm/libp2p.d.ts +5 -20
- package/lib/esm/libp2p.js +38 -177
- package/lib/esm/libp2p.js.map +1 -1
- package/lib/esm/remote.d.ts +59 -0
- package/lib/esm/remote.js +245 -0
- package/lib/esm/remote.js.map +1 -0
- package/package.json +8 -10
- package/src/{level.ts → any-blockstore.ts} +25 -37
- package/src/block.ts +1 -1
- package/src/index.ts +3 -2
- package/src/{store.ts → interface.ts} +4 -1
- package/src/libp2p.ts +45 -195
- package/src/remote.ts +298 -0
- package/lib/esm/level.js.map +0 -1
- package/lib/esm/store.js +0 -2
- package/lib/esm/store.js.map +0 -1
- package/lib/esm/streams.d.ts +0 -1
- package/lib/esm/streams.js +0 -2
- package/lib/esm/streams.js.map +0 -1
- package/src/streams.ts +0 -1
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
import { stringifyCid, cidifyString, codecCodes, checkDecodeBlock } from "./block.js";
|
|
11
|
+
import { variant, field } from "@dao-xyz/borsh";
|
|
12
|
+
import PQueue from "p-queue";
|
|
13
|
+
import { AbortError } from "@peerbit/time";
|
|
14
|
+
import { TypedEventEmitter, CustomEvent } from "@libp2p/interface";
|
|
15
|
+
export class BlockMessage {
|
|
16
|
+
}
|
|
17
|
+
let BlockRequest = class BlockRequest extends BlockMessage {
|
|
18
|
+
cid;
|
|
19
|
+
constructor(cid) {
|
|
20
|
+
super();
|
|
21
|
+
this.cid = cid;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
__decorate([
|
|
25
|
+
field({ type: "string" }),
|
|
26
|
+
__metadata("design:type", String)
|
|
27
|
+
], BlockRequest.prototype, "cid", void 0);
|
|
28
|
+
BlockRequest = __decorate([
|
|
29
|
+
variant(0),
|
|
30
|
+
__metadata("design:paramtypes", [String])
|
|
31
|
+
], BlockRequest);
|
|
32
|
+
export { BlockRequest };
|
|
33
|
+
let BlockResponse = class BlockResponse extends BlockMessage {
|
|
34
|
+
cid;
|
|
35
|
+
bytes;
|
|
36
|
+
constructor(cid, bytes) {
|
|
37
|
+
super();
|
|
38
|
+
this.cid = cid;
|
|
39
|
+
this.bytes = bytes;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
__decorate([
|
|
43
|
+
field({ type: "string" }),
|
|
44
|
+
__metadata("design:type", String)
|
|
45
|
+
], BlockResponse.prototype, "cid", void 0);
|
|
46
|
+
__decorate([
|
|
47
|
+
field({ type: Uint8Array }),
|
|
48
|
+
__metadata("design:type", Uint8Array)
|
|
49
|
+
], BlockResponse.prototype, "bytes", void 0);
|
|
50
|
+
BlockResponse = __decorate([
|
|
51
|
+
variant(1),
|
|
52
|
+
__metadata("design:paramtypes", [String, Uint8Array])
|
|
53
|
+
], BlockResponse);
|
|
54
|
+
export { BlockResponse };
|
|
55
|
+
export class RemoteBlocks {
|
|
56
|
+
options;
|
|
57
|
+
localStore;
|
|
58
|
+
_responseHandler;
|
|
59
|
+
_resolvers;
|
|
60
|
+
_loadFetchQueue;
|
|
61
|
+
_readFromPeersPromises;
|
|
62
|
+
_open = false;
|
|
63
|
+
_events;
|
|
64
|
+
closeController;
|
|
65
|
+
constructor(options) {
|
|
66
|
+
this.options = options;
|
|
67
|
+
const localTimeout = options?.localTimeout || 1000;
|
|
68
|
+
this._loadFetchQueue = new PQueue({
|
|
69
|
+
concurrency: options?.messageProcessingConcurrency || 10
|
|
70
|
+
});
|
|
71
|
+
this.localStore = options?.local;
|
|
72
|
+
this._resolvers = new Map();
|
|
73
|
+
this._readFromPeersPromises = new Map();
|
|
74
|
+
/* this._responseHandler = (async (evt: CustomEvent<DataMessage>) => {
|
|
75
|
+
if (!evt) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
const message = evt.detail;
|
|
79
|
+
if (!message.data) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
try {
|
|
84
|
+
const decoded = deserialize(message.data, BlockMessage);
|
|
85
|
+
if (decoded instanceof BlockRequest && this._localStore) {
|
|
86
|
+
this._loadFetchQueue.add(() =>
|
|
87
|
+
this.handleFetchRequest(decoded, localTimeout)
|
|
88
|
+
);
|
|
89
|
+
} else if (decoded instanceof BlockResponse) {
|
|
90
|
+
// TODO make sure we are not storing too much bytes in ram (like filter large blocks)
|
|
91
|
+
|
|
92
|
+
this._resolvers.get(decoded.cid)?.(decoded.bytes);
|
|
93
|
+
}
|
|
94
|
+
} catch (error) {
|
|
95
|
+
console.error("Got error for libp2p block transport: ", error);
|
|
96
|
+
return; // timeout o r invalid cid
|
|
97
|
+
}
|
|
98
|
+
}) */
|
|
99
|
+
this._responseHandler = async (message) => {
|
|
100
|
+
try {
|
|
101
|
+
if (message instanceof BlockRequest && this.localStore) {
|
|
102
|
+
this._loadFetchQueue.add(() => this.handleFetchRequest(message, localTimeout));
|
|
103
|
+
}
|
|
104
|
+
else if (message instanceof BlockResponse) {
|
|
105
|
+
// TODO make sure we are not storing too much bytes in ram (like filter large blocks)
|
|
106
|
+
this._resolvers.get(message.cid)?.(message.bytes);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
catch (error) {
|
|
110
|
+
console.error("Got error for libp2p block transport: ", error);
|
|
111
|
+
return; // timeout o r invalid cid
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
async put(bytes) {
|
|
116
|
+
if (!this.localStore) {
|
|
117
|
+
throw new Error("Local store not set");
|
|
118
|
+
}
|
|
119
|
+
return this.localStore.put(bytes);
|
|
120
|
+
}
|
|
121
|
+
async has(cid) {
|
|
122
|
+
return this.localStore.has(cid);
|
|
123
|
+
}
|
|
124
|
+
async get(cid, options) {
|
|
125
|
+
const cidObject = cidifyString(cid);
|
|
126
|
+
let value = this.localStore
|
|
127
|
+
? await this.localStore.get(cid, options)
|
|
128
|
+
: undefined;
|
|
129
|
+
if (!value) {
|
|
130
|
+
// try to get it remotelly
|
|
131
|
+
value = await this._readFromPeers(cid, cidObject, options);
|
|
132
|
+
if (options?.replicate && value) {
|
|
133
|
+
this.localStore.put(value);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return value;
|
|
137
|
+
}
|
|
138
|
+
async rm(cid) {
|
|
139
|
+
await this.localStore?.rm(cid);
|
|
140
|
+
}
|
|
141
|
+
async *iterator() {
|
|
142
|
+
for await (const [key, value] of this.localStore.iterator()) {
|
|
143
|
+
yield [key, value];
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
async start() {
|
|
147
|
+
this._events = new TypedEventEmitter();
|
|
148
|
+
this.closeController = new AbortController();
|
|
149
|
+
await this.localStore?.start();
|
|
150
|
+
this._open = true;
|
|
151
|
+
}
|
|
152
|
+
onMessage(data) {
|
|
153
|
+
return this._responseHandler(data);
|
|
154
|
+
}
|
|
155
|
+
onReachable(publicKey) {
|
|
156
|
+
this._events.dispatchEvent(new CustomEvent("peer:reachable", { detail: publicKey }));
|
|
157
|
+
}
|
|
158
|
+
async handleFetchRequest(request, localTimeout) {
|
|
159
|
+
const cid = stringifyCid(request.cid);
|
|
160
|
+
const bytes = await this.localStore.get(cid, {
|
|
161
|
+
timeout: localTimeout
|
|
162
|
+
});
|
|
163
|
+
if (!bytes) {
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
await this.options.publish(new BlockResponse(cid, bytes));
|
|
167
|
+
}
|
|
168
|
+
async _readFromPeers(cidString, cidObject, options = {}) {
|
|
169
|
+
const codec = codecCodes[cidObject.code];
|
|
170
|
+
let promise = this._readFromPeersPromises.get(cidString);
|
|
171
|
+
if (!promise) {
|
|
172
|
+
promise = new Promise((resolve, reject) => {
|
|
173
|
+
const timeoutCallback = setTimeout(() => {
|
|
174
|
+
resolve(undefined);
|
|
175
|
+
}, options.timeout || 30 * 1000);
|
|
176
|
+
const abortHandler = () => {
|
|
177
|
+
clearTimeout(timeoutCallback);
|
|
178
|
+
this._resolvers.delete(cidString);
|
|
179
|
+
this.closeController.signal.removeEventListener("abort", abortHandler);
|
|
180
|
+
reject(new AbortError());
|
|
181
|
+
};
|
|
182
|
+
this.closeController.signal.addEventListener("abort", abortHandler);
|
|
183
|
+
this._resolvers.set(cidString, async (bytes) => {
|
|
184
|
+
const value = await checkDecodeBlock(cidObject, bytes, {
|
|
185
|
+
codec,
|
|
186
|
+
hasher: options?.hasher
|
|
187
|
+
});
|
|
188
|
+
clearTimeout(timeoutCallback);
|
|
189
|
+
this._resolvers.delete(cidString); // TODO concurrency might not work as expected here
|
|
190
|
+
this.closeController.signal.removeEventListener("abort", abortHandler);
|
|
191
|
+
resolve(value);
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
this._readFromPeersPromises.set(cidString, promise);
|
|
195
|
+
const publish = (to) => {
|
|
196
|
+
if (!options?.from || options.from.includes(to))
|
|
197
|
+
this.options.publish(new BlockRequest(cidString), { to: [to] });
|
|
198
|
+
};
|
|
199
|
+
const publishOnNewPeers = (e) => {
|
|
200
|
+
return publish(e.detail.hashcode());
|
|
201
|
+
};
|
|
202
|
+
this._events.addEventListener("peer:reachable", publishOnNewPeers);
|
|
203
|
+
this.options.publish(new BlockRequest(cidString), {
|
|
204
|
+
to: options.from
|
|
205
|
+
});
|
|
206
|
+
// we want to make sure that if some new peers join, we also try to ask them
|
|
207
|
+
const result = await promise;
|
|
208
|
+
this._readFromPeersPromises.delete(cidString);
|
|
209
|
+
// stop asking new peers, because we already got an response
|
|
210
|
+
this._events.removeEventListener("peer:reachable", publishOnNewPeers);
|
|
211
|
+
return result?.bytes;
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
const result = await promise;
|
|
215
|
+
return result?.bytes;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
async stop() {
|
|
219
|
+
// Dont listen for more incoming messages
|
|
220
|
+
// Wait for processing request
|
|
221
|
+
this.closeController.abort();
|
|
222
|
+
this._loadFetchQueue.clear();
|
|
223
|
+
await this._loadFetchQueue.onIdle(); // wait for pending
|
|
224
|
+
await this.localStore?.stop();
|
|
225
|
+
this._readFromPeersPromises.clear();
|
|
226
|
+
this._resolvers.clear();
|
|
227
|
+
this._open = false;
|
|
228
|
+
// we dont cleanup subscription because we dont know if someone else is sbuscribing also
|
|
229
|
+
}
|
|
230
|
+
waitFor(peer) {
|
|
231
|
+
return this.options.waitFor(peer);
|
|
232
|
+
}
|
|
233
|
+
async size() {
|
|
234
|
+
return this.localStore.size();
|
|
235
|
+
}
|
|
236
|
+
get status() {
|
|
237
|
+
if (this._open) {
|
|
238
|
+
return this.localStore?.status();
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
return "closed";
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
//# sourceMappingURL=remote.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"remote.js","sourceRoot":"","sources":["../../src/remote.ts"],"names":[],"mappings":";;;;;;;;;AAEA,OAAO,EACN,YAAY,EACZ,YAAY,EACZ,UAAU,EACV,gBAAgB,EAChB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,OAAO,EAAE,KAAK,EAA0B,MAAM,gBAAgB,CAAC;AAMxE,OAAO,MAAM,MAAM,SAAS,CAAC;AAC7B,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAE3C,OAAO,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEnE,MAAM,OAAO,YAAY;CAAG;AAGrB,IAAM,YAAY,GAAlB,MAAM,YAAa,SAAQ,YAAY;IAE7C,GAAG,CAAS;IAEZ,YAAY,GAAW;QACtB,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;IAChB,CAAC;CACD,CAAA;AANA;IADC,KAAK,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC;;yCACd;AAFA,YAAY;IADxB,OAAO,CAAC,CAAC,CAAC;;GACE,YAAY,CAQxB;;AAGM,IAAM,aAAa,GAAnB,MAAM,aAAc,SAAQ,YAAY;IAE9C,GAAG,CAAS;IAGZ,KAAK,CAAa;IAElB,YAAY,GAAW,EAAE,KAAiB;QACzC,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACpB,CAAC;CACD,CAAA;AAVA;IADC,KAAK,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC;;0CACd;AAGZ;IADC,KAAK,CAAC,EAAE,IAAI,EAAE,UAAU,EAAE,CAAC;8BACrB,UAAU;4CAAC;AALN,aA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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;CACD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peerbit/blocks",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"description": "Block store streaming",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -49,18 +49,16 @@
|
|
|
49
49
|
"dao.xyz"
|
|
50
50
|
],
|
|
51
51
|
"devDependencies": {
|
|
52
|
-
"@peerbit/libp2p-test-utils": "
|
|
52
|
+
"@peerbit/libp2p-test-utils": "2.0.0"
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {
|
|
55
55
|
"@dao-xyz/borsh": "^5.1.8",
|
|
56
56
|
"@ipld/dag-cbor": "^9.0.2",
|
|
57
|
-
"@peerbit/
|
|
58
|
-
"@peerbit/
|
|
59
|
-
"@peerbit/
|
|
60
|
-
"@peerbit/stream": "
|
|
61
|
-
"
|
|
62
|
-
"libp2p": "0.46.9",
|
|
63
|
-
"memory-level": "^1.0.0"
|
|
57
|
+
"@peerbit/any-store": "^1.0.2",
|
|
58
|
+
"@peerbit/blocks-interface": "1.2.1",
|
|
59
|
+
"@peerbit/crypto": "2.1.0",
|
|
60
|
+
"@peerbit/stream": "2.0.2",
|
|
61
|
+
"libp2p": "^1.1.0"
|
|
64
62
|
},
|
|
65
|
-
"gitHead": "
|
|
63
|
+
"gitHead": "cba5d4b1a6f3343f044c7d3194494c3572710cc8"
|
|
66
64
|
}
|
|
@@ -7,24 +7,18 @@ import {
|
|
|
7
7
|
stringifyCid
|
|
8
8
|
} from "./block.js";
|
|
9
9
|
import * as Block from "multiformats/block";
|
|
10
|
-
import { AbstractLevel } from "abstract-level";
|
|
11
|
-
import { MemoryLevel } from "memory-level";
|
|
12
10
|
import { waitFor } from "@peerbit/time";
|
|
13
|
-
import LazyLevel, { LazyLevelOptions } from "@peerbit/lazy-level";
|
|
14
|
-
import { PutOptions } from "@peerbit/blocks-interface";
|
|
15
11
|
import { PeerId } from "@libp2p/interface/peer-id";
|
|
16
12
|
import { PublicSignKey } from "@peerbit/crypto";
|
|
13
|
+
import { AnyStore, createStore } from "@peerbit/any-store";
|
|
17
14
|
|
|
18
|
-
export class
|
|
19
|
-
private
|
|
15
|
+
export class AnyBlockStore implements Blocks {
|
|
16
|
+
private _store: AnyStore;
|
|
20
17
|
private _opening: Promise<any>;
|
|
21
|
-
private _closed = false;
|
|
22
18
|
private _onClose: (() => any) | undefined;
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
) {
|
|
27
|
-
this._level = new LazyLevel(level, options);
|
|
19
|
+
private _closeController: AbortController;
|
|
20
|
+
constructor(store: AnyStore = createStore()) {
|
|
21
|
+
this._store = store;
|
|
28
22
|
}
|
|
29
23
|
|
|
30
24
|
async get(
|
|
@@ -38,7 +32,7 @@ export class LevelBlockStore implements Blocks {
|
|
|
38
32
|
): Promise<Uint8Array | undefined> {
|
|
39
33
|
const cidObject = cidifyString(cid);
|
|
40
34
|
try {
|
|
41
|
-
const bytes = await this.
|
|
35
|
+
const bytes = await this._store.get(cid);
|
|
42
36
|
if (!bytes) {
|
|
43
37
|
return undefined;
|
|
44
38
|
}
|
|
@@ -64,36 +58,37 @@ export class LevelBlockStore implements Blocks {
|
|
|
64
58
|
const block = await createBlock(bytes, "raw");
|
|
65
59
|
const cid = stringifyCid(block.cid);
|
|
66
60
|
const bbytes = block.bytes;
|
|
67
|
-
await this.
|
|
61
|
+
await this._store.put(cid, bbytes);
|
|
68
62
|
return cid;
|
|
69
63
|
}
|
|
70
64
|
|
|
71
65
|
async rm(cid: string): Promise<void> {
|
|
72
|
-
await this.
|
|
66
|
+
await this._store.del(cid);
|
|
73
67
|
}
|
|
74
68
|
|
|
75
69
|
async *iterator(): AsyncGenerator<[string, Uint8Array], void, void> {
|
|
76
|
-
for await (const [key, value] of this.
|
|
70
|
+
for await (const [key, value] of this._store.iterator()) {
|
|
77
71
|
yield [key, value];
|
|
78
72
|
}
|
|
79
73
|
}
|
|
80
74
|
|
|
81
75
|
async has(cid: string) {
|
|
82
|
-
return !!(await this.
|
|
76
|
+
return !!(await this._store.get(cid));
|
|
83
77
|
}
|
|
84
78
|
|
|
85
79
|
async start(): Promise<void> {
|
|
86
|
-
this.
|
|
87
|
-
|
|
80
|
+
await this._store.open();
|
|
81
|
+
this._closeController = new AbortController();
|
|
88
82
|
|
|
89
83
|
try {
|
|
90
|
-
this._opening = waitFor(
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
84
|
+
this._opening = waitFor(
|
|
85
|
+
async () => (await this._store.status()) === "open",
|
|
86
|
+
{
|
|
87
|
+
delayInterval: 100,
|
|
88
|
+
timeout: 10 * 1000,
|
|
89
|
+
signal: this._closeController.signal
|
|
95
90
|
}
|
|
96
|
-
|
|
91
|
+
);
|
|
97
92
|
await this._opening;
|
|
98
93
|
} finally {
|
|
99
94
|
this._onClose = undefined;
|
|
@@ -101,26 +96,19 @@ export class LevelBlockStore implements Blocks {
|
|
|
101
96
|
}
|
|
102
97
|
|
|
103
98
|
async stop(): Promise<void> {
|
|
104
|
-
await this.idle();
|
|
105
|
-
this._closed = true;
|
|
106
99
|
this._onClose && this._onClose();
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
async idle(): Promise<void> {
|
|
111
|
-
await this._level.idle();
|
|
100
|
+
this._closeController.abort();
|
|
101
|
+
return this._store.close();
|
|
112
102
|
}
|
|
113
103
|
|
|
114
104
|
status() {
|
|
115
|
-
return this.
|
|
105
|
+
return this._store.status();
|
|
116
106
|
}
|
|
117
107
|
async waitFor(peer: PeerId | PublicSignKey): Promise<void> {
|
|
118
108
|
return; // Offline storage // TODO this feels off resolving
|
|
119
109
|
}
|
|
120
|
-
}
|
|
121
110
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
super(new MemoryLevel({ valueEncoding: "view" }), options);
|
|
111
|
+
async size() {
|
|
112
|
+
return this._store.size();
|
|
125
113
|
}
|
|
126
114
|
}
|
package/src/block.ts
CHANGED
|
@@ -4,7 +4,7 @@ import * as dagCbor from "@ipld/dag-cbor";
|
|
|
4
4
|
import { sha256 } from "multiformats/hashes/sha2";
|
|
5
5
|
import { base58btc } from "multiformats/bases/base58";
|
|
6
6
|
import * as Block from "multiformats/block";
|
|
7
|
-
import type { MultihashHasher } from "multiformats/hashes/
|
|
7
|
+
import type { MultihashHasher } from "multiformats/hashes/interface";
|
|
8
8
|
|
|
9
9
|
const unsupportedCodecError = () => new Error("unsupported codec");
|
|
10
10
|
|
package/src/index.ts
CHANGED
|
@@ -5,6 +5,7 @@ export {
|
|
|
5
5
|
getBlockValue
|
|
6
6
|
} from "./block.js";
|
|
7
7
|
export { DirectBlock } from "./libp2p.js";
|
|
8
|
-
export * from "./
|
|
9
|
-
export * from "./
|
|
8
|
+
export * from "./interface.js";
|
|
9
|
+
export * from "./any-blockstore.js";
|
|
10
10
|
export * from "./libp2p.js";
|
|
11
|
+
export * from "./remote.js";
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
import { MaybePromise } from "@peerbit/any-store";
|
|
1
2
|
import { Blocks as IBlockStore } from "@peerbit/blocks-interface";
|
|
2
3
|
|
|
3
|
-
export type StoreStatus =
|
|
4
|
+
export type StoreStatus = MaybePromise<
|
|
5
|
+
"open" | "opening" | "closed" | "closing"
|
|
6
|
+
>;
|
|
4
7
|
export interface BlockStore extends IBlockStore {
|
|
5
8
|
start(): Promise<void>;
|
|
6
9
|
stop(): Promise<void>;
|