@peerbit/blocks 2.0.32 → 2.1.0-55cebfe

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/dist/benchmark/e2e.d.ts +2 -0
  2. package/dist/benchmark/e2e.d.ts.map +1 -0
  3. package/dist/benchmark/e2e.js +75 -0
  4. package/dist/benchmark/e2e.js.map +1 -0
  5. package/dist/benchmark/multiformats.d.ts +2 -0
  6. package/dist/benchmark/multiformats.d.ts.map +1 -0
  7. package/dist/benchmark/multiformats.js +129 -0
  8. package/dist/benchmark/multiformats.js.map +1 -0
  9. package/{lib/esm → dist/src}/any-blockstore.d.ts +7 -5
  10. package/dist/src/any-blockstore.d.ts.map +1 -0
  11. package/{lib/esm → dist/src}/any-blockstore.js +13 -7
  12. package/dist/src/any-blockstore.js.map +1 -0
  13. package/{lib/esm → dist/src}/index.d.ts +1 -1
  14. package/dist/src/index.d.ts.map +1 -0
  15. package/{lib/esm → dist/src}/index.js +0 -1
  16. package/dist/src/index.js.map +1 -0
  17. package/{lib/esm → dist/src}/interface.d.ts +3 -2
  18. package/dist/src/interface.d.ts.map +1 -0
  19. package/{lib/esm → dist/src}/libp2p.d.ts +5 -4
  20. package/dist/src/libp2p.d.ts.map +1 -0
  21. package/{lib/esm → dist/src}/libp2p.js +11 -6
  22. package/dist/src/libp2p.js.map +1 -0
  23. package/{lib/esm → dist/src}/remote.d.ts +6 -4
  24. package/dist/src/remote.d.ts.map +1 -0
  25. package/{lib/esm → dist/src}/remote.js +26 -12
  26. package/dist/src/remote.js.map +1 -0
  27. package/package.json +87 -63
  28. package/src/any-blockstore.ts +19 -14
  29. package/src/index.ts +0 -6
  30. package/src/interface.ts +2 -2
  31. package/src/libp2p.ts +16 -14
  32. package/src/remote.ts +49 -32
  33. package/LICENSE +0 -202
  34. package/lib/esm/any-blockstore.js.map +0 -1
  35. package/lib/esm/block.d.ts +0 -26
  36. package/lib/esm/block.js +0 -89
  37. package/lib/esm/block.js.map +0 -1
  38. package/lib/esm/index.js.map +0 -1
  39. package/lib/esm/libp2p.js.map +0 -1
  40. package/lib/esm/remote.js.map +0 -1
  41. package/src/block.ts +0 -114
  42. /package/{lib/esm → dist/src}/interface.js +0 -0
  43. /package/{lib/esm → dist/src}/interface.js.map +0 -0
package/package.json CHANGED
@@ -1,65 +1,89 @@
1
1
  {
2
- "name": "@peerbit/blocks",
3
- "version": "2.0.32",
4
- "description": "Block store streaming",
5
- "type": "module",
6
- "sideEffects": false,
7
- "private": false,
8
- "publishConfig": {
9
- "access": "public"
10
- },
11
- "module": "lib/esm/index.js",
12
- "types": "lib/esm/index.d.ts",
13
- "exports": {
14
- "import": "./lib/esm/index.js"
15
- },
16
- "scripts": {
17
- "clean": "shx rm -rf lib/*",
18
- "build": "yarn clean && tsc -p tsconfig.json",
19
- "test": "node ../../../node_modules/.bin/jest test -c ../../../jest.config.ts --runInBand --forceExit",
20
- "test:unit": "node ../../../node_modules/.bin/jest test -c ../../../jest.config.unit.ts --runInBand --forceExit",
21
- "test:integration": "node ../node_modules/.bin/jest test -c ../../../jest.config.integration.ts --runInBand --forceExit"
22
- },
23
- "files": [
24
- "lib",
25
- "src",
26
- "!src/**/__tests__",
27
- "!lib/**/__tests__",
28
- "!src/**/__benchmark__",
29
- "!lib/**/__benchmark__",
30
- "LICENSE"
31
- ],
32
- "engines": {
33
- "node": ">=16.15.1"
34
- },
35
- "repository": {
36
- "type": "git",
37
- "url": "git+https://github.com/dao-xyz/peerbit.git"
38
- },
39
- "keywords": [
40
- "peerbit"
41
- ],
42
- "author": "dao.xyz",
43
- "license": "MIT",
44
- "bugs": {
45
- "url": "https://github.com/dao-xyz/peerbit/issues"
46
- },
47
- "homepage": "https://github.com/dao-xyz/peerbit#readme",
48
- "localMaintainers": [
49
- "dao.xyz"
50
- ],
51
- "devDependencies": {
52
- "@peerbit/libp2p-test-utils": "2.1.3"
53
- },
54
- "dependencies": {
55
- "@dao-xyz/borsh": "^5.2.1",
56
- "@ipld/dag-cbor": "^9.0.2",
57
- "@peerbit/any-store": "^2.0.2",
58
- "@peerbit/blocks-interface": "1.2.19",
59
- "@peerbit/crypto": "2.2.0",
60
- "@peerbit/stream": "4.0.5",
61
- "libp2p": "^1.2.3",
62
- "multiformats": "^13.0.1"
63
- },
64
- "gitHead": "fcd486b9a5c3b7d94e2ae14f1b06ab2a0f7841ad"
2
+ "name": "@peerbit/blocks",
3
+ "version": "2.1.0-55cebfe",
4
+ "description": "Block store streaming",
5
+ "type": "module",
6
+ "types": "./dist/src/index.d.ts",
7
+ "typesVersions": {
8
+ "*": {
9
+ "*": [
10
+ "*",
11
+ "dist/*",
12
+ "dist/src/*",
13
+ "dist/src/*/index"
14
+ ],
15
+ "src/*": [
16
+ "*",
17
+ "dist/*",
18
+ "dist/src/*",
19
+ "dist/src/*/index"
20
+ ]
21
+ }
22
+ },
23
+ "files": [
24
+ "src",
25
+ "dist",
26
+ "!dist/e2e",
27
+ "!dist/test",
28
+ "!**/*.tsbuildinfo"
29
+ ],
30
+ "exports": {
31
+ ".": {
32
+ "types": "./dist/src/index.d.ts",
33
+ "import": "./dist/src/index.js"
34
+ }
35
+ },
36
+ "eslintConfig": {
37
+ "extends": "peerbit",
38
+ "parserOptions": {
39
+ "project": true,
40
+ "sourceType": "module"
41
+ },
42
+ "ignorePatterns": [
43
+ "!.aegir.js",
44
+ "test/ts-use",
45
+ "*.d.ts"
46
+ ]
47
+ },
48
+ "publishConfig": {
49
+ "access": "public"
50
+ },
51
+ "scripts": {
52
+ "clean": "aegir clean",
53
+ "build": "aegir build --no-bundle",
54
+ "test": "aegir test --target node",
55
+ "lint": "aegir lint"
56
+ },
57
+ "engines": {
58
+ "node": ">=16.15.1"
59
+ },
60
+ "repository": {
61
+ "type": "git",
62
+ "url": "git+https://github.com/dao-xyz/peerbit.git"
63
+ },
64
+ "keywords": [
65
+ "peerbit"
66
+ ],
67
+ "author": "dao.xyz",
68
+ "license": "MIT",
69
+ "bugs": {
70
+ "url": "https://github.com/dao-xyz/peerbit/issues"
71
+ },
72
+ "homepage": "https://github.com/dao-xyz/peerbit#readme",
73
+ "localMaintainers": [
74
+ "dao.xyz"
75
+ ],
76
+ "devDependencies": {
77
+ "@peerbit/libp2p-test-utils": "2.1.4-55cebfe"
78
+ },
79
+ "dependencies": {
80
+ "@dao-xyz/borsh": "^5.2.3",
81
+ "@peerbit/any-store": "2.1.0-55cebfe",
82
+ "@peerbit/stream": "4.0.6-55cebfe",
83
+ "@peerbit/blocks-interface": "1.3.0-55cebfe",
84
+ "@peerbit/crypto": "2.3.0-55cebfe",
85
+ "@ipld/dag-cbor": "^9.0.2",
86
+ "libp2p": "^1.8.1",
87
+ "multiformats": "^13.0.1"
88
+ }
65
89
  }
@@ -1,16 +1,17 @@
1
- import { Blocks } from "@peerbit/blocks-interface";
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 "./block.js";
9
- import { decode, type Block } from "multiformats/block";
10
+ stringifyCid,
11
+ } from "@peerbit/blocks-interface";
12
+ import { type PublicSignKey } from "@peerbit/crypto";
10
13
  import { waitFor } from "@peerbit/time";
11
- import { PeerId } from "@libp2p/interface";
12
- import { PublicSignKey } from "@peerbit/crypto";
13
- import { AnyStore, createStore } from "@peerbit/any-store";
14
+ import { type Block, decode } from "multiformats/block";
14
15
 
15
16
  export class AnyBlockStore implements Blocks {
16
17
  private _store: AnyStore;
@@ -28,7 +29,7 @@ export class AnyBlockStore implements Blocks {
28
29
  links?: string[];
29
30
  timeout?: number;
30
31
  hasher?: any;
31
- }
32
+ },
32
33
  ): Promise<Uint8Array | undefined> {
33
34
  const cidObject = cidifyString(cid);
34
35
  try {
@@ -36,11 +37,11 @@ export class AnyBlockStore implements Blocks {
36
37
  if (!bytes) {
37
38
  return undefined;
38
39
  }
39
- const codec = codecCodes[cidObject.code];
40
+ const codec = (codecCodes as any)[cidObject.code];
40
41
  const block = await decode({
41
42
  bytes,
42
43
  codec,
43
- hasher: options?.hasher || defaultHasher
44
+ hasher: options?.hasher || defaultHasher,
44
45
  });
45
46
  return (block as Block<Uint8Array, any, any, any>).bytes;
46
47
  } catch (error: any) {
@@ -86,8 +87,8 @@ export class AnyBlockStore implements Blocks {
86
87
  {
87
88
  delayInterval: 100,
88
89
  timeout: 10 * 1000,
89
- signal: this._closeController.signal
90
- }
90
+ signal: this._closeController.signal,
91
+ },
91
92
  );
92
93
  await this._opening;
93
94
  } finally {
@@ -96,7 +97,7 @@ export class AnyBlockStore implements Blocks {
96
97
  }
97
98
 
98
99
  async stop(): Promise<void> {
99
- this._onClose && this._onClose();
100
+ this._onClose?.();
100
101
  this._closeController?.abort();
101
102
  return this._store.close();
102
103
  }
@@ -105,10 +106,14 @@ export class AnyBlockStore implements Blocks {
105
106
  return this._store.status();
106
107
  }
107
108
  async waitFor(peer: PeerId | PublicSignKey): Promise<void> {
108
- return; // Offline storage // TODO this feels off resolving
109
+ // Offline storage // TODO this feels off resolving
109
110
  }
110
111
 
111
112
  async size() {
112
113
  return this._store.size();
113
114
  }
115
+
116
+ persisted(): boolean | Promise<boolean> {
117
+ return this._store.persisted();
118
+ }
114
119
  }
package/src/index.ts CHANGED
@@ -1,9 +1,3 @@
1
- export {
2
- cidifyString,
3
- stringifyCid,
4
- createBlock,
5
- getBlockValue
6
- } from "./block.js";
7
1
  export { DirectBlock } from "./libp2p.js";
8
2
  export * from "./interface.js";
9
3
  export * from "./any-blockstore.js";
package/src/interface.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { MaybePromise } from "@peerbit/any-store";
2
- import { Blocks as IBlockStore } from "@peerbit/blocks-interface";
1
+ import type { MaybePromise } from "@peerbit/any-store-interface";
2
+ import type { Blocks as IBlockStore } from "@peerbit/blocks-interface";
3
3
 
4
4
  export type StoreStatus = MaybePromise<
5
5
  "open" | "opening" | "closed" | "closing"
package/src/libp2p.ts CHANGED
@@ -1,14 +1,12 @@
1
- import { BlockStore } from "./interface.js";
2
- import { Blocks as IBlocks } from "@peerbit/blocks-interface";
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";
3
5
  import { DirectStream } from "@peerbit/stream";
4
- import { DirectStreamComponents } from "@peerbit/stream";
6
+ import { type DirectStreamComponents } from "@peerbit/stream";
7
+ import { AnyWhere, type DataMessage } from "@peerbit/stream-interface";
5
8
  import { AnyBlockStore } from "./any-blockstore.js";
6
- import { GetOptions } from "@peerbit/blocks-interface";
7
- import { createStore } from "@peerbit/any-store";
8
9
  import { BlockMessage, RemoteBlocks } from "./remote.js";
9
- import { PublicSignKey } from "@peerbit/crypto";
10
- import { AnyWhere, DataMessage } from "@peerbit/stream-interface";
11
- import { deserialize, serialize } from "@dao-xyz/borsh";
12
10
 
13
11
  export type DirectBlockComponents = DirectStreamComponents;
14
12
 
@@ -24,15 +22,15 @@ export class DirectBlock extends DirectStream implements IBlocks {
24
22
  canRelayMessage?: boolean;
25
23
  localTimeout?: number;
26
24
  messageProcessingConcurrency?: number;
27
- }
25
+ },
28
26
  ) {
29
27
  super(components, ["/lazyblock/0.0.0"], {
30
28
  messageProcessingConcurrency: options?.messageProcessingConcurrency || 10,
31
29
  canRelayMessage: options?.canRelayMessage ?? true,
32
30
  connectionManager: {
33
31
  dialer: false,
34
- pruner: false
35
- }
32
+ pruner: false,
33
+ },
36
34
  });
37
35
  this.remoteBlocks = new RemoteBlocks({
38
36
  local: new AnyBlockStore(createStore(options?.directory)),
@@ -40,14 +38,14 @@ export class DirectBlock extends DirectStream implements IBlocks {
40
38
  this.publish(serialize(message), { mode: new AnyWhere() }),
41
39
  localTimeout: options?.localTimeout || 1000,
42
40
  messageProcessingConcurrency: options?.messageProcessingConcurrency || 10,
43
- waitFor: this.waitFor.bind(this)
41
+ waitFor: this.waitFor.bind(this),
44
42
  });
45
43
 
46
44
  this.onDataFn = (data: CustomEvent<DataMessage>) => {
47
45
  data.detail?.data?.length &&
48
46
  data.detail?.data.length > 0 &&
49
47
  this.remoteBlocks.onMessage(
50
- deserialize(data.detail.data!, BlockMessage)
48
+ deserialize(data.detail.data!, BlockMessage),
51
49
  );
52
50
  };
53
51
  this.onPeerConnectedFn = (evt: CustomEvent<PublicSignKey>) =>
@@ -63,7 +61,7 @@ export class DirectBlock extends DirectStream implements IBlocks {
63
61
  }
64
62
  async get(
65
63
  cid: string,
66
- options?: GetOptions | undefined
64
+ options?: GetOptions | undefined,
67
65
  ): Promise<Uint8Array | undefined> {
68
66
  return this.remoteBlocks.get(cid, options);
69
67
  }
@@ -98,4 +96,8 @@ export class DirectBlock extends DirectStream implements IBlocks {
98
96
  get status() {
99
97
  return this.remoteBlocks?.status || this.started;
100
98
  }
99
+
100
+ persisted(): boolean | Promise<boolean> {
101
+ return this.remoteBlocks.persisted();
102
+ }
101
103
  }
package/src/remote.ts CHANGED
@@ -1,21 +1,24 @@
1
- import { BlockStore } from "./interface.js";
2
- import { Blocks as IBlocks } from "@peerbit/blocks-interface";
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
- stringifyCid,
5
+ type GetOptions,
6
+ type Blocks as IBlocks,
7
+ checkDecodeBlock,
5
8
  cidifyString,
6
9
  codecCodes,
7
- checkDecodeBlock
8
- } from "./block.js";
9
- import { variant, field } from "@dao-xyz/borsh";
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 { GetOptions } from "@peerbit/blocks-interface";
15
17
  import PQueue from "p-queue";
16
- import { AbortError } from "@peerbit/time";
17
- import type { PeerId } from "@libp2p/interface";
18
- import { TypedEventEmitter, CustomEvent } from "@libp2p/interface";
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
23
  export class BlockMessage {}
21
24
 
@@ -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
- console.error("Got error for libp2p block transport: ", error);
97
- return; // timeout o r invalid cid
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,9 +174,14 @@ export class RemoteBlocks implements IBlocks {
171
174
  private async _readFromPeers(
172
175
  cidString: string,
173
176
  cidObject: CID,
174
- options: { timeout?: number; hasher?: any; from?: string[] } = {}
177
+ options: {
178
+ signal?: AbortSignal;
179
+ timeout?: number;
180
+ hasher?: any;
181
+ from?: string[];
182
+ } = {},
175
183
  ): Promise<Uint8Array | undefined> {
176
- const codec = codecCodes[cidObject.code];
184
+ const codec = (codecCodes as any)[cidObject.code];
177
185
  let promise = this._readFromPeersPromises.get(cidString);
178
186
  if (!promise) {
179
187
  promise = new Promise<Block<any, any, any, 1> | undefined>(
@@ -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), { to: [to] });
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
  }