@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.
@@ -1,14 +1,13 @@
1
1
  import { Blocks } from "@peerbit/blocks-interface";
2
- import { AbstractLevel } from "abstract-level";
3
- import { LazyLevelOptions } from "@peerbit/lazy-level";
4
2
  import { PeerId } from "@libp2p/interface/peer-id";
5
3
  import { PublicSignKey } from "@peerbit/crypto";
6
- export declare class LevelBlockStore implements Blocks {
7
- private _level;
4
+ import { AnyStore } from "@peerbit/any-store";
5
+ export declare class AnyBlockStore implements Blocks {
6
+ private _store;
8
7
  private _opening;
9
- private _closed;
10
8
  private _onClose;
11
- constructor(level: AbstractLevel<any, string, Uint8Array>, options?: LazyLevelOptions);
9
+ private _closeController;
10
+ constructor(store?: AnyStore);
12
11
  get(cid: string, options?: {
13
12
  raw?: boolean;
14
13
  links?: string[];
@@ -21,10 +20,7 @@ export declare class LevelBlockStore implements Blocks {
21
20
  has(cid: string): Promise<boolean>;
22
21
  start(): Promise<void>;
23
22
  stop(): Promise<void>;
24
- idle(): Promise<void>;
25
- status(): "open" | "opening" | "closed" | "closing";
23
+ status(): import("@peerbit/any-store").MaybePromise<"opening" | "open" | "closing" | "closed">;
26
24
  waitFor(peer: PeerId | PublicSignKey): Promise<void>;
27
- }
28
- export declare class MemoryLevelBlockStore extends LevelBlockStore {
29
- constructor(options?: LazyLevelOptions);
25
+ size(): Promise<number>;
30
26
  }
@@ -1,20 +1,19 @@
1
1
  import { cidifyString, codecCodes, createBlock, defaultHasher, stringifyCid } from "./block.js";
2
2
  import * as Block from "multiformats/block";
3
- import { MemoryLevel } from "memory-level";
4
3
  import { waitFor } from "@peerbit/time";
5
- import LazyLevel from "@peerbit/lazy-level";
6
- export class LevelBlockStore {
7
- _level;
4
+ import { createStore } from "@peerbit/any-store";
5
+ export class AnyBlockStore {
6
+ _store;
8
7
  _opening;
9
- _closed = false;
10
8
  _onClose;
11
- constructor(level, options) {
12
- this._level = new LazyLevel(level, options);
9
+ _closeController;
10
+ constructor(store = createStore()) {
11
+ this._store = store;
13
12
  }
14
13
  async get(cid, options) {
15
14
  const cidObject = cidifyString(cid);
16
15
  try {
17
- const bytes = await this._level.get(cid);
16
+ const bytes = await this._store.get(cid);
18
17
  if (!bytes) {
19
18
  return undefined;
20
19
  }
@@ -38,30 +37,28 @@ export class LevelBlockStore {
38
37
  const block = await createBlock(bytes, "raw");
39
38
  const cid = stringifyCid(block.cid);
40
39
  const bbytes = block.bytes;
41
- await this._level.put(cid, bbytes);
40
+ await this._store.put(cid, bbytes);
42
41
  return cid;
43
42
  }
44
43
  async rm(cid) {
45
- await this._level.del(cid);
44
+ await this._store.del(cid);
46
45
  }
47
46
  async *iterator() {
48
- for await (const [key, value] of this._level.iterator()) {
47
+ for await (const [key, value] of this._store.iterator()) {
49
48
  yield [key, value];
50
49
  }
51
50
  }
52
51
  async has(cid) {
53
- return !!(await this._level.get(cid));
52
+ return !!(await this._store.get(cid));
54
53
  }
55
54
  async start() {
56
- this._closed = false;
57
- await this._level.open();
55
+ await this._store.open();
56
+ this._closeController = new AbortController();
58
57
  try {
59
- this._opening = waitFor(() => this._level.status() === "open", {
58
+ this._opening = waitFor(async () => (await this._store.status()) === "open", {
60
59
  delayInterval: 100,
61
60
  timeout: 10 * 1000,
62
- stopperCallback: (fn) => {
63
- this._onClose = fn;
64
- }
61
+ signal: this._closeController.signal
65
62
  });
66
63
  await this._opening;
67
64
  }
@@ -70,24 +67,18 @@ export class LevelBlockStore {
70
67
  }
71
68
  }
72
69
  async stop() {
73
- await this.idle();
74
- this._closed = true;
75
70
  this._onClose && this._onClose();
76
- return this._level.close();
77
- }
78
- async idle() {
79
- await this._level.idle();
71
+ this._closeController.abort();
72
+ return this._store.close();
80
73
  }
81
74
  status() {
82
- return this._level.status();
75
+ return this._store.status();
83
76
  }
84
77
  async waitFor(peer) {
85
78
  return; // Offline storage // TODO this feels off resolving
86
79
  }
87
- }
88
- export class MemoryLevelBlockStore extends LevelBlockStore {
89
- constructor(options) {
90
- super(new MemoryLevel({ valueEncoding: "view" }), options);
80
+ async size() {
81
+ return this._store.size();
91
82
  }
92
83
  }
93
- //# sourceMappingURL=level.js.map
84
+ //# sourceMappingURL=any-blockstore.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"any-blockstore.js","sourceRoot":"","sources":["../../src/any-blockstore.ts"],"names":[],"mappings":"AACA,OAAO,EACN,YAAY,EACZ,UAAU,EACV,WAAW,EACX,aAAa,EACb,YAAY,EACZ,MAAM,YAAY,CAAC;AACpB,OAAO,KAAK,KAAK,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAGxC,OAAO,EAAY,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAE3D,MAAM,OAAO,aAAa;IACjB,MAAM,CAAW;IACjB,QAAQ,CAAe;IACvB,QAAQ,CAA0B;IAClC,gBAAgB,CAAkB;IAC1C,YAAY,QAAkB,WAAW,EAAE;QAC1C,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IACrB,CAAC;IAED,KAAK,CAAC,GAAG,CACR,GAAW,EACX,OAKC;QAED,MAAM,SAAS,GAAG,YAAY,CAAC,GAAG,CAAC,CAAC;QACpC,IAAI,CAAC;YACJ,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;YACzC,IAAI,CAAC,KAAK,EAAE,CAAC;gBACZ,OAAO,SAAS,CAAC;YAClB,CAAC;YACD,MAAM,KAAK,GAAG,UAAU,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;YACzC,MAAM,KAAK,GAAG,MAAM,KAAK,CAAC,MAAM,CAAC;gBAChC,KAAK;gBACL,KAAK;gBACL,MAAM,EAAE,OAAO,EAAE,MAAM,IAAI,aAAa;aACxC,CAAC,CAAC;YACH,OAAQ,KAAgD,CAAC,KAAK,CAAC;QAChE,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CAAC,QAAQ,GAAG,OAAO,CACtB,KAAK,IAAI,EAAE,CAAC,CAAC,MAAM,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,KAAK,MAAM,EACnD;gBACC,aAAa,EAAE,GAAG;gBAClB,OAAO,EAAE,EAAE,GAAG,IAAI;gBAClB,MAAM,EAAE,IAAI,CAAC,gBAAgB,CAAC,MAAM;aACpC,CACD,CAAC;YACF,MAAM,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,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;QACjC,IAAI,CAAC,gBAAgB,CAAC,KAAK,EAAE,CAAC;QAC9B,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,OAAO,CAAC,mDAAmD;IAC5D,CAAC;IAED,KAAK,CAAC,IAAI;QACT,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;IAC3B,CAAC;CACD"}
@@ -2,8 +2,8 @@ import { CID } from "multiformats/cid";
2
2
  import * as raw from "multiformats/codecs/raw";
3
3
  import * as dagCbor from "@ipld/dag-cbor";
4
4
  import * as Block from "multiformats/block";
5
- import type { MultihashHasher } from "multiformats/hashes/hasher";
6
- export declare const defaultHasher: import("multiformats/hashes/hasher").Hasher<"sha2-256", 18>;
5
+ import type { MultihashHasher } from "multiformats/hashes/interface";
6
+ export declare const defaultHasher: import("multiformats/dist/src/hashes/hasher").Hasher<"sha2-256", 18>;
7
7
  export declare const codecCodes: {
8
8
  113: typeof dagCbor;
9
9
  85: typeof raw;
@@ -1 +1 @@
1
- {"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AK,EAC/B,KAAU,EACV,MAAc,EACd,OAGC,EAC0C,EAAE;IAC7C,MAAM,KAAK,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC;IAC/B,KAAK,GAAG,iBAAiB,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,CAAC,CAAC;IACxD,MAAM,KAAK,GAAG,MAAM,KAAK,CAAC,MAAM,CAAC;QAChC,KAAK;QACL,KAAK;QACL,MAAM,EAAE,OAAO,EAAE,MAAM,IAAI,aAAa;KACxC,CAAC,CAAC;IACH,OAAO,KAAwC,CAAC;AACjD,CAAC,CAAC"}
@@ -1,5 +1,6 @@
1
1
  export { cidifyString, stringifyCid, createBlock, getBlockValue } from "./block.js";
2
2
  export { DirectBlock } from "./libp2p.js";
3
- export * from "./store.js";
4
- export * from "./level.js";
3
+ export * from "./interface.js";
4
+ export * from "./any-blockstore.js";
5
5
  export * from "./libp2p.js";
6
+ export * from "./remote.js";
package/lib/esm/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  export { cidifyString, stringifyCid, createBlock, getBlockValue } from "./block.js";
2
2
  export { DirectBlock } from "./libp2p.js";
3
- export * from "./store.js";
4
- export * from "./level.js";
3
+ export * from "./interface.js";
4
+ export * from "./any-blockstore.js";
5
5
  export * from "./libp2p.js";
6
+ export * from "./remote.js";
6
7
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,YAAY,EACZ,YAAY,EACZ,WAAW,EACX,aAAa,EACb,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,YAAY,EACZ,YAAY,EACZ,WAAW,EACX,aAAa,EACb,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,cAAc,gBAAgB,CAAC;AAC/B,cAAc,qBAAqB,CAAC;AACpC,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC"}
@@ -1,5 +1,6 @@
1
+ import { MaybePromise } from "@peerbit/any-store";
1
2
  import { Blocks as IBlockStore } from "@peerbit/blocks-interface";
2
- export type StoreStatus = "open" | "opening" | "closed" | "closing";
3
+ export type StoreStatus = MaybePromise<"open" | "opening" | "closed" | "closing">;
3
4
  export interface BlockStore extends IBlockStore {
4
5
  start(): Promise<void>;
5
6
  stop(): Promise<void>;
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=interface.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interface.js","sourceRoot":"","sources":["../../src/interface.ts"],"names":[],"mappings":""}
@@ -2,25 +2,11 @@ import { Blocks as IBlocks } from "@peerbit/blocks-interface";
2
2
  import { DirectStream } from "@peerbit/stream";
3
3
  import { DirectStreamComponents } from "@peerbit/stream";
4
4
  import { GetOptions } from "@peerbit/blocks-interface";
5
- export declare class BlockMessage {
6
- }
7
- export declare class BlockRequest extends BlockMessage {
8
- cid: string;
9
- constructor(cid: string);
10
- }
11
- export declare class BlockResponse extends BlockMessage {
12
- cid: string;
13
- bytes: Uint8Array;
14
- constructor(cid: string, bytes: Uint8Array);
15
- }
16
5
  export type DirectBlockComponents = DirectStreamComponents;
17
6
  export declare class DirectBlock extends DirectStream implements IBlocks {
18
- private _localStore;
19
- private _responseHandler?;
20
- private _resolvers;
21
- private _loadFetchQueue;
22
- private _readFromPeersPromises;
23
- _open: boolean;
7
+ private remoteBlocks;
8
+ private onDataFn;
9
+ private onPeerConnectedFn;
24
10
  constructor(components: DirectBlockComponents, options?: {
25
11
  directory?: string;
26
12
  canRelayMessage?: boolean;
@@ -33,8 +19,7 @@ export declare class DirectBlock extends DirectStream implements IBlocks {
33
19
  rm(cid: string): Promise<void>;
34
20
  iterator(): AsyncGenerator<[string, Uint8Array], void, void>;
35
21
  start(): Promise<void>;
36
- private handleFetchRequest;
37
- private _readFromPeers;
38
22
  stop(): Promise<void>;
39
- get status(): import("./store.js").StoreStatus;
23
+ size(): Promise<number>;
24
+ get status(): import("./interface.js").StoreStatus;
40
25
  }
package/lib/esm/libp2p.js CHANGED
@@ -1,208 +1,69 @@
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, serialize, deserialize } from "@dao-xyz/borsh";
12
1
  import { DirectStream } from "@peerbit/stream";
13
- import { LevelBlockStore, MemoryLevelBlockStore } from "./level.js";
14
- import { Level } from "level";
15
- import PQueue from "p-queue";
16
- export class BlockMessage {
17
- }
18
- let BlockRequest = class BlockRequest extends BlockMessage {
19
- cid;
20
- constructor(cid) {
21
- super();
22
- this.cid = cid;
23
- }
24
- };
25
- __decorate([
26
- field({ type: "string" }),
27
- __metadata("design:type", String)
28
- ], BlockRequest.prototype, "cid", void 0);
29
- BlockRequest = __decorate([
30
- variant(0),
31
- __metadata("design:paramtypes", [String])
32
- ], BlockRequest);
33
- export { BlockRequest };
34
- let BlockResponse = class BlockResponse extends BlockMessage {
35
- cid;
36
- bytes;
37
- constructor(cid, bytes) {
38
- super();
39
- this.cid = cid;
40
- this.bytes = bytes;
41
- }
42
- };
43
- __decorate([
44
- field({ type: "string" }),
45
- __metadata("design:type", String)
46
- ], BlockResponse.prototype, "cid", void 0);
47
- __decorate([
48
- field({ type: Uint8Array }),
49
- __metadata("design:type", Uint8Array)
50
- ], BlockResponse.prototype, "bytes", void 0);
51
- BlockResponse = __decorate([
52
- variant(1),
53
- __metadata("design:paramtypes", [String, Uint8Array])
54
- ], BlockResponse);
55
- export { BlockResponse };
2
+ import { AnyBlockStore } from "./any-blockstore.js";
3
+ import { createStore } from "@peerbit/any-store";
4
+ import { BlockMessage, RemoteBlocks } from "./remote.js";
5
+ import { deserialize, serialize } from "@dao-xyz/borsh";
56
6
  export class DirectBlock extends DirectStream {
57
- _localStore;
58
- _responseHandler;
59
- _resolvers;
60
- _loadFetchQueue;
61
- _readFromPeersPromises;
62
- _open = false;
7
+ remoteBlocks;
8
+ onDataFn;
9
+ onPeerConnectedFn;
63
10
  constructor(components, options) {
64
- super(components, ["direct-block/1.0.0"], {
65
- emitSelf: false,
11
+ super(components, ["/lazyblock/0.0.0"], {
66
12
  signaturePolicy: "StrictNoSign",
67
13
  messageProcessingConcurrency: options?.messageProcessingConcurrency || 10,
68
- canRelayMessage: options?.canRelayMessage ?? true
14
+ canRelayMessage: options?.canRelayMessage ?? true,
15
+ connectionManager: {
16
+ dialer: false,
17
+ pruner: false
18
+ }
69
19
  });
70
- const localTimeout = options?.localTimeout || 1000;
71
- this._loadFetchQueue = new PQueue({
72
- concurrency: options?.messageProcessingConcurrency || 10
20
+ this.remoteBlocks = new RemoteBlocks({
21
+ local: new AnyBlockStore(createStore(options?.directory)),
22
+ publish: (message) => this.publish(serialize(message)),
23
+ localTimeout: options?.localTimeout || 1000,
24
+ messageProcessingConcurrency: options?.messageProcessingConcurrency || 10,
25
+ waitFor: this.waitFor.bind(this)
73
26
  });
74
- this._localStore =
75
- options?.directory != null
76
- ? new LevelBlockStore(new Level(options.directory))
77
- : new MemoryLevelBlockStore();
78
- this._resolvers = new Map();
79
- this._readFromPeersPromises = new Map();
80
- this._responseHandler = async (evt) => {
81
- if (!evt) {
82
- return;
83
- }
84
- const message = evt.detail;
85
- try {
86
- const decoded = deserialize(message.data, BlockMessage);
87
- if (decoded instanceof BlockRequest && this._localStore) {
88
- this._loadFetchQueue.add(() => this.handleFetchRequest(decoded, localTimeout));
89
- }
90
- else if (decoded instanceof BlockResponse) {
91
- // TODO make sure we are not storing too much bytes in ram (like filter large blocks)
92
- this._resolvers.get(decoded.cid)?.(decoded.bytes);
93
- }
94
- }
95
- catch (error) {
96
- console.error("Got error for libp2p block transport: ", error);
97
- return; // timeout o r invalid cid
98
- }
27
+ this.onDataFn = (data) => {
28
+ data.detail?.data &&
29
+ this.remoteBlocks.onMessage(deserialize(data.detail.data, BlockMessage));
99
30
  };
31
+ this.onPeerConnectedFn = (evt) => this.remoteBlocks.onReachable(evt.detail);
100
32
  }
101
33
  async put(bytes) {
102
- if (!this._localStore) {
103
- throw new Error("Local store not set");
104
- }
105
- return this._localStore.put(bytes);
34
+ return this.remoteBlocks.put(bytes);
106
35
  }
107
36
  async has(cid) {
108
- return this._localStore.has(cid);
37
+ return this.remoteBlocks.has(cid);
109
38
  }
110
39
  async get(cid, options) {
111
- const cidObject = cidifyString(cid);
112
- let value = this._localStore
113
- ? await this._localStore.get(cid, options)
114
- : undefined;
115
- if (!value) {
116
- // try to get it remotelly
117
- value = await this._readFromPeers(cid, cidObject, options);
118
- if (options?.replicate && value) {
119
- this._localStore.put(value);
120
- }
121
- }
122
- return value;
40
+ return this.remoteBlocks.get(cid, options);
123
41
  }
124
42
  async rm(cid) {
125
- await this._localStore?.rm(cid);
43
+ return this.remoteBlocks.rm(cid);
126
44
  }
127
45
  async *iterator() {
128
- for await (const [key, value] of this._localStore.iterator()) {
46
+ for await (const [key, value] of this.remoteBlocks.iterator()) {
129
47
  yield [key, value];
130
48
  }
131
49
  }
132
50
  async start() {
133
- await this._localStore?.start();
51
+ this.addEventListener("data", this.onDataFn);
52
+ this.addEventListener("peer:reachable", this.onPeerConnectedFn);
134
53
  await super.start();
135
- this.addEventListener("data", this._responseHandler);
136
- this._open = true;
137
- }
138
- async handleFetchRequest(request, localTimeout) {
139
- const cid = stringifyCid(request.cid);
140
- const bytes = await this._localStore.get(cid, {
141
- timeout: localTimeout
142
- });
143
- if (!bytes) {
144
- return;
145
- }
146
- const response = serialize(new BlockResponse(cid, bytes));
147
- await this.publish(response);
148
- }
149
- async _readFromPeers(cidString, cidObject, options = {}) {
150
- const codec = codecCodes[cidObject.code];
151
- let promise = this._readFromPeersPromises.get(cidString);
152
- if (!promise) {
153
- promise = new Promise((resolve, reject) => {
154
- const timeoutCallback = setTimeout(() => {
155
- resolve(undefined);
156
- }, options.timeout || 30 * 1000);
157
- this._resolvers.set(cidString, async (bytes) => {
158
- const value = await checkDecodeBlock(cidObject, bytes, {
159
- codec,
160
- hasher: options?.hasher
161
- });
162
- clearTimeout(timeoutCallback);
163
- this._resolvers.delete(cidString); // TODO concurrency might not work as expected here
164
- resolve(value);
165
- });
166
- });
167
- this._readFromPeersPromises.set(cidString, promise);
168
- const publish = (to) => this.publish(serialize(new BlockRequest(cidString)), { to: to });
169
- const publishOnNewPeers = (e) => {
170
- return publish([e.detail]);
171
- };
172
- this.addEventListener("peer:reachable", publishOnNewPeers);
173
- await publish();
174
- // we want to make sure that if some new peers join, we also try to ask them
175
- const result = await promise;
176
- this._readFromPeersPromises.delete(cidString);
177
- // stop asking new peers, because we already got an response
178
- this.removeEventListener("peer:reachable", publishOnNewPeers);
179
- return result?.bytes;
180
- }
181
- else {
182
- const result = await promise;
183
- return result?.bytes;
184
- }
54
+ await this.remoteBlocks.start();
185
55
  }
186
56
  async stop() {
187
- // Dont listen for more incoming messages
188
- this.removeEventListener("data", this._responseHandler);
189
- // Wait for processing request
190
- this._loadFetchQueue.clear();
191
- await this._loadFetchQueue.onIdle(); // wait for pending
57
+ this.removeEventListener("data", this.onDataFn);
58
+ this.removeEventListener("peer:reachable", this.onPeerConnectedFn);
192
59
  await super.stop();
193
- await this._localStore?.stop();
194
- this._readFromPeersPromises.clear();
195
- this._resolvers.clear();
196
- this._open = false;
197
- // we dont cleanup subscription because we dont know if someone else is sbuscribing also
60
+ await this.remoteBlocks.stop();
61
+ }
62
+ async size() {
63
+ return this.remoteBlocks?.size() || 0;
198
64
  }
199
65
  get status() {
200
- if (this._open) {
201
- return this._localStore?.status() || this.started;
202
- }
203
- else {
204
- return "closed";
205
- }
66
+ return this.remoteBlocks?.status || this.started;
206
67
  }
207
68
  }
208
69
  //# sourceMappingURL=libp2p.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"libp2p.js","sourceRoot":"","sources":["../../src/libp2p.ts"],"names":[],"mappings":";;;;;;;;;AAEA,OAAO,EACN,YAAY,EACZ,YAAY,EACZ,UAAU,EACV,gBAAgB,EAChB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAGxE,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAK/C,OAAO,EAAE,eAAe,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AACpE,OAAO,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AAE9B,OAAO,MAAM,MAAM,SAAS,CAAC;AAE7B,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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KAAK,CAAC,CAAC;QACpB,CAAC;IACF,CAAC;IAED,KAAK,CAAC,KAAK;QACV,IAAI,CAAC,gBAAgB,CAAC,MAAM,EAAE,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;CACD"}
@@ -0,0 +1,59 @@
1
+ import { BlockStore } from "./interface.js";
2
+ import { Blocks as IBlocks } from "@peerbit/blocks-interface";
3
+ import { PublicSignKey } from "@peerbit/crypto";
4
+ import { AnyBlockStore } from "./any-blockstore.js";
5
+ import { GetOptions } from "@peerbit/blocks-interface";
6
+ import type { PeerId } from "@libp2p/interface/peer-id";
7
+ export declare class BlockMessage {
8
+ }
9
+ export declare class BlockRequest extends BlockMessage {
10
+ cid: string;
11
+ constructor(cid: string);
12
+ }
13
+ export declare class BlockResponse extends BlockMessage {
14
+ cid: string;
15
+ bytes: Uint8Array;
16
+ constructor(cid: string, bytes: Uint8Array);
17
+ }
18
+ export declare class RemoteBlocks implements IBlocks {
19
+ readonly options: {
20
+ local: AnyBlockStore;
21
+ localTimeout?: number;
22
+ messageProcessingConcurrency?: number;
23
+ publish: (message: BlockRequest | BlockResponse, options?: {
24
+ to?: string[];
25
+ }) => Promise<Uint8Array | void>;
26
+ waitFor(peer: PeerId | PublicSignKey): Promise<void>;
27
+ };
28
+ localStore: BlockStore;
29
+ private _responseHandler?;
30
+ private _resolvers;
31
+ private _loadFetchQueue;
32
+ private _readFromPeersPromises;
33
+ _open: boolean;
34
+ private _events;
35
+ private closeController;
36
+ constructor(options: {
37
+ local: AnyBlockStore;
38
+ localTimeout?: number;
39
+ messageProcessingConcurrency?: number;
40
+ publish: (message: BlockRequest | BlockResponse, options?: {
41
+ to?: string[];
42
+ }) => Promise<Uint8Array | void>;
43
+ waitFor(peer: PeerId | PublicSignKey): Promise<void>;
44
+ });
45
+ put(bytes: Uint8Array): Promise<string>;
46
+ has(cid: string): Promise<boolean>;
47
+ get(cid: string, options?: GetOptions | undefined): Promise<Uint8Array | undefined>;
48
+ rm(cid: string): Promise<void>;
49
+ iterator(): AsyncGenerator<[string, Uint8Array], void, void>;
50
+ start(): Promise<void>;
51
+ onMessage(data: BlockMessage): any;
52
+ onReachable(publicKey: PublicSignKey): void;
53
+ private handleFetchRequest;
54
+ private _readFromPeers;
55
+ stop(): Promise<void>;
56
+ waitFor(peer: PeerId | PublicSignKey): Promise<void>;
57
+ size(): Promise<number>;
58
+ get status(): import("./interface.js").StoreStatus;
59
+ }