@helia/utils 2.3.5-33e46813 → 2.3.5-7bf85e0c

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,148 +0,0 @@
1
- import { setMaxListeners } from '@libp2p/interface';
2
- import { anySignal } from 'any-signal';
3
- import { Storage } from "./storage.js";
4
- /**
5
- * Storage subclass that can cancel any ongoing operation at any point.
6
- */
7
- export class SessionStorage extends Storage {
8
- closeController;
9
- constructor(components, init) {
10
- super(components);
11
- // because brokers are allowed to continue searching for providers after the
12
- // session has been created, we need a way to tell them that the user has
13
- // finished using the session any in-flight requests should be cancelled
14
- this.closeController = new AbortController();
15
- setMaxListeners(Infinity, this.closeController.signal);
16
- this.log = components.logger.forComponent(`helia:session-storage:${init.root}`);
17
- }
18
- close() {
19
- this.closeController.abort();
20
- }
21
- async addPeer(peer, options) {
22
- await Promise.all(this.blockBrokers
23
- .map(broker => broker.addPeer(peer, options)));
24
- }
25
- /**
26
- * Put a block to the underlying datastore
27
- */
28
- async put(cid, block, options = {}) {
29
- const signal = anySignal([this.closeController.signal, options.signal]);
30
- setMaxListeners(Infinity, signal);
31
- try {
32
- return await super.put(cid, block, {
33
- ...options,
34
- signal
35
- });
36
- }
37
- finally {
38
- signal.clear();
39
- }
40
- }
41
- /**
42
- * Put a multiple blocks to the underlying datastore
43
- */
44
- async *putMany(blocks, options = {}) {
45
- const signal = anySignal([this.closeController.signal, options.signal]);
46
- setMaxListeners(Infinity, signal);
47
- try {
48
- yield* super.putMany(blocks, {
49
- ...options,
50
- signal
51
- });
52
- }
53
- finally {
54
- signal.clear();
55
- }
56
- }
57
- /**
58
- * Get a block by cid
59
- */
60
- async *get(cid, options = {}) {
61
- const signal = anySignal([this.closeController.signal, options.signal]);
62
- setMaxListeners(Infinity, signal);
63
- try {
64
- yield* super.get(cid, {
65
- ...options,
66
- signal
67
- });
68
- }
69
- finally {
70
- signal.clear();
71
- }
72
- }
73
- /**
74
- * Get multiple blocks back from an (async) iterable of cids
75
- */
76
- async *getMany(cids, options = {}) {
77
- const signal = anySignal([this.closeController.signal, options.signal]);
78
- setMaxListeners(Infinity, signal);
79
- try {
80
- yield* super.getMany(cids, {
81
- ...options,
82
- signal
83
- });
84
- }
85
- finally {
86
- signal.clear();
87
- }
88
- }
89
- /**
90
- * Delete a block from the blockstore
91
- */
92
- async delete(cid, options = {}) {
93
- const signal = anySignal([this.closeController.signal, options.signal]);
94
- setMaxListeners(Infinity, signal);
95
- try {
96
- await super.delete(cid, {
97
- ...options,
98
- signal
99
- });
100
- }
101
- finally {
102
- signal.clear();
103
- }
104
- }
105
- /**
106
- * Delete multiple blocks from the blockstore
107
- */
108
- async *deleteMany(cids, options = {}) {
109
- const signal = anySignal([this.closeController.signal, options.signal]);
110
- setMaxListeners(Infinity, signal);
111
- try {
112
- yield* super.deleteMany(cids, {
113
- ...options,
114
- signal
115
- });
116
- }
117
- finally {
118
- signal.clear();
119
- }
120
- }
121
- async has(cid, options = {}) {
122
- const signal = anySignal([this.closeController.signal, options.signal]);
123
- setMaxListeners(Infinity, signal);
124
- try {
125
- return await super.has(cid, {
126
- ...options,
127
- signal
128
- });
129
- }
130
- finally {
131
- signal.clear();
132
- }
133
- }
134
- async *getAll(options = {}) {
135
- const signal = anySignal([this.closeController.signal, options.signal]);
136
- setMaxListeners(Infinity, signal);
137
- try {
138
- yield* super.getAll({
139
- ...options,
140
- signal
141
- });
142
- }
143
- finally {
144
- signal.clear();
145
- }
146
- }
147
- }
148
- //# sourceMappingURL=session-storage.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"session-storage.js","sourceRoot":"","sources":["../../../src/utils/session-storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AACtC,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AActC;;GAEG;AACH,MAAM,OAAO,cAAe,SA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AM;aACP,CAAC,CAAA;QACJ,CAAC;gBAAS,CAAC;YACT,MAAM,CAAC,KAAK,EAAE,CAAA;QAChB,CAAC;IACH,CAAC;IAED,KAAK,CAAC,CAAE,MAAM,CAAE,UAAsE,EAAE;QACtF,MAAM,MAAM,GAAG,SAAS,CAAC,CAAC,IAAI,CAAC,eAAe,CAAC,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC,CAAA;QACvE,eAAe,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAA;QAEjC,IAAI,CAAC;YACH,KAAM,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC;gBACnB,GAAG,OAAO;gBACV,MAAM;aACP,CAAC,CAAA;QACJ,CAAC;gBAAS,CAAC;YACT,MAAM,CAAC,KAAK,EAAE,CAAA;QAChB,CAAC;IACH,CAAC;CACF"}
@@ -1,56 +0,0 @@
1
- import type { HasherLoader } from '@helia/interface';
2
- import type { BlockBroker, Pair, DeleteManyBlocksProgressEvents, DeleteBlockProgressEvents, GetBlockProgressEvents, GetManyBlocksProgressEvents, PutManyBlocksProgressEvents, PutBlockProgressEvents, GetAllBlocksProgressEvents, GetOfflineOptions, BlockRetrievalOptions } from '@helia/interface/blocks';
3
- import type { AbortOptions, ComponentLogger, Logger } from '@libp2p/interface';
4
- import type { Blockstore, InputPair } from 'interface-blockstore';
5
- import type { AwaitIterable } from 'interface-store';
6
- import type { CID } from 'multiformats/cid';
7
- import type { MultihashHasher } from 'multiformats/hashes/interface';
8
- import type { ProgressEvent, ProgressOptions } from 'progress-events';
9
- export interface StorageComponents<Broker extends BlockBroker<ProgressEvent<any, any>, ProgressEvent<any, any>>> {
10
- blockstore: Blockstore;
11
- logger: ComponentLogger;
12
- blockBrokers: Broker[];
13
- getHasher: HasherLoader;
14
- }
15
- export interface StorageInit {
16
- maxIdentityHashDigestLength?: number;
17
- }
18
- export declare class Storage<Broker extends BlockBroker<ProgressEvent<any, any>, ProgressEvent<any, any>>> implements Blockstore {
19
- protected readonly child: Blockstore;
20
- protected readonly getHasher: HasherLoader;
21
- protected log: Logger;
22
- protected readonly logger: ComponentLogger;
23
- protected readonly blockBrokers: Broker[];
24
- /**
25
- * Create a new BlockStorage
26
- */
27
- constructor(components: StorageComponents<Broker>, init?: StorageInit);
28
- /**
29
- * Put a block to the underlying datastore
30
- */
31
- put(cid: CID, block: Uint8Array, options?: AbortOptions & ProgressOptions<PutBlockProgressEvents>): Promise<CID>;
32
- /**
33
- * Put a multiple blocks to the underlying datastore
34
- */
35
- putMany(blocks: AwaitIterable<InputPair>, options?: AbortOptions & ProgressOptions<PutManyBlocksProgressEvents>): AsyncGenerator<CID>;
36
- /**
37
- * Get a block by cid
38
- */
39
- get(cid: CID, options?: GetOfflineOptions & AbortOptions & ProgressOptions<GetBlockProgressEvents>): AsyncGenerator<Uint8Array>;
40
- /**
41
- * Get multiple blocks back from an (async) iterable of cids
42
- */
43
- getMany(cids: AwaitIterable<CID>, options?: GetOfflineOptions & AbortOptions & ProgressOptions<GetManyBlocksProgressEvents>): AsyncGenerator<Pair>;
44
- /**
45
- * Delete a block from the blockstore
46
- */
47
- delete(cid: CID, options?: AbortOptions & ProgressOptions<DeleteBlockProgressEvents>): Promise<void>;
48
- /**
49
- * Delete multiple blocks from the blockstore
50
- */
51
- deleteMany(cids: AwaitIterable<CID>, options?: AbortOptions & ProgressOptions<DeleteManyBlocksProgressEvents>): AsyncGenerator<CID>;
52
- has(cid: CID, options?: AbortOptions): Promise<boolean>;
53
- getAll(options?: AbortOptions & ProgressOptions<GetAllBlocksProgressEvents>): AsyncGenerator<Pair>;
54
- }
55
- export declare const getCidBlockVerifierFunction: (cid: CID, hasher: MultihashHasher) => Required<BlockRetrievalOptions>["validateFn"];
56
- //# sourceMappingURL=storage.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../../../src/utils/storage.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,kBAAkB,CA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C;IAI1D,MAAM,CAAE,OAAO,GAAE,YAAY,GAAG,eAAe,CAAC,0BAA0B,CAAM,GAAG,cAAc,CAAC,IAAI,CAAC;CAIhH;AAqED,eAAO,MAAM,2BAA2B,GAAI,KAAK,GAAG,EAAE,QAAQ,eAAe,KAAG,QAAQ,CAAC,qBAAqB,CAAC,CAAC,YAAY,CAwB3H,CAAA"}
@@ -1,225 +0,0 @@
1
- import { InvalidMultihashError, InvalidParametersError, setMaxListeners } from '@libp2p/interface';
2
- import { anySignal } from 'any-signal';
3
- import { IdentityBlockstore } from 'blockstore-core/identity';
4
- import filter from 'it-filter';
5
- import forEach from 'it-foreach';
6
- import { CustomProgressEvent } from 'progress-events';
7
- import { equals as uint8ArrayEquals } from 'uint8arrays/equals';
8
- import { BlockNotFoundWhileOfflineError, InvalidConfigurationError, LoadBlockFailedError } from "../errors.js";
9
- import { isPromise } from './is-promise.js';
10
- const DEFAULT_MAX_IDENTITY_HASH_DIGEST_LENGTH = 128;
11
- export class Storage {
12
- child;
13
- getHasher;
14
- log;
15
- logger;
16
- blockBrokers;
17
- /**
18
- * Create a new BlockStorage
19
- */
20
- constructor(components, init = {}) {
21
- this.log = components.logger.forComponent('helia:networked-storage');
22
- this.logger = components.logger;
23
- this.blockBrokers = components.blockBrokers;
24
- this.child = new IdentityBlockstore(components.blockstore, {
25
- maxDigestLength: init.maxIdentityHashDigestLength ?? DEFAULT_MAX_IDENTITY_HASH_DIGEST_LENGTH
26
- });
27
- this.getHasher = components.getHasher;
28
- }
29
- /**
30
- * Put a block to the underlying datastore
31
- */
32
- async put(cid, block, options = {}) {
33
- if (await this.child.has(cid, options)) {
34
- options.onProgress?.(new CustomProgressEvent('blocks:put:duplicate', cid));
35
- return cid;
36
- }
37
- options.onProgress?.(new CustomProgressEvent('blocks:put:providers:notify', cid));
38
- await Promise.all(this.blockBrokers.map(async (broker) => broker.announce?.(cid, options)));
39
- options.onProgress?.(new CustomProgressEvent('blocks:put:blockstore:put', cid));
40
- return this.child.put(cid, block, options);
41
- }
42
- /**
43
- * Put a multiple blocks to the underlying datastore
44
- */
45
- async *putMany(blocks, options = {}) {
46
- const missingBlocks = filter(blocks, async ({ cid }) => {
47
- const has = await this.child.has(cid, options);
48
- if (has) {
49
- options.onProgress?.(new CustomProgressEvent('blocks:put-many:duplicate', cid));
50
- }
51
- return !has;
52
- });
53
- const notifyEach = forEach(missingBlocks, async ({ cid }) => {
54
- options.onProgress?.(new CustomProgressEvent('blocks:put-many:providers:notify', cid));
55
- await Promise.all(this.blockBrokers.map(async (broker) => broker.announce?.(cid, options)));
56
- });
57
- options.onProgress?.(new CustomProgressEvent('blocks:put-many:blockstore:put-many'));
58
- yield* this.child.putMany(notifyEach, options);
59
- }
60
- /**
61
- * Get a block by cid
62
- */
63
- async *get(cid, options = {}) {
64
- const has = await this.child.has(cid, options);
65
- const offline = options.offline === true;
66
- if (!has) {
67
- if (offline) {
68
- throw new BlockNotFoundWhileOfflineError('The block was present in the blockstore and the node is running offline so cannot fetch it');
69
- }
70
- const hasher = await this.getHasher(cid.multihash.code);
71
- options?.signal?.throwIfAborted();
72
- // we do not have the block locally, get it from a block provider
73
- options.onProgress?.(new CustomProgressEvent('blocks:get:providers:get', cid));
74
- const block = await raceBlockRetrievers(cid, this.blockBrokers, hasher, {
75
- ...options,
76
- log: this.log
77
- });
78
- options.onProgress?.(new CustomProgressEvent('blocks:get:blockstore:put', cid));
79
- await this.child.put(cid, block, options);
80
- // notify other block providers of the new block
81
- options.onProgress?.(new CustomProgressEvent('blocks:get:providers:notify', cid));
82
- await Promise.all(this.blockBrokers.map(async (broker) => broker.announce?.(cid, options)));
83
- yield block;
84
- return;
85
- }
86
- options.onProgress?.(new CustomProgressEvent('blocks:get:blockstore:get', cid));
87
- yield* this.child.get(cid, options);
88
- }
89
- /**
90
- * Get multiple blocks back from an (async) iterable of cids
91
- */
92
- async *getMany(cids, options = {}) {
93
- options.onProgress?.(new CustomProgressEvent('blocks:get-many:blockstore:get-many'));
94
- yield* this.child.getMany(forEach(cids, async (cid) => {
95
- const has = await this.child.has(cid, options);
96
- const offline = options.offline === true;
97
- if (!has) {
98
- if (offline) {
99
- throw new BlockNotFoundWhileOfflineError('The block was present in the blockstore and the node is running offline so cannot fetch it');
100
- }
101
- const hasher = await this.getHasher(cid.multihash.code);
102
- options?.signal?.throwIfAborted();
103
- // we do not have the block locally, get it from a block provider
104
- options.onProgress?.(new CustomProgressEvent('blocks:get-many:providers:get', cid));
105
- const block = await raceBlockRetrievers(cid, this.blockBrokers, hasher, {
106
- ...options,
107
- log: this.log
108
- });
109
- options.onProgress?.(new CustomProgressEvent('blocks:get-many:blockstore:put', cid));
110
- await this.child.put(cid, block, options);
111
- // notify other block providers of the new block
112
- options.onProgress?.(new CustomProgressEvent('blocks:get-many:providers:notify', cid));
113
- await Promise.all(this.blockBrokers.map(async (broker) => broker.announce?.(cid, options)));
114
- }
115
- }));
116
- }
117
- /**
118
- * Delete a block from the blockstore
119
- */
120
- async delete(cid, options = {}) {
121
- options.onProgress?.(new CustomProgressEvent('blocks:delete:blockstore:delete', cid));
122
- await this.child.delete(cid, options);
123
- }
124
- /**
125
- * Delete multiple blocks from the blockstore
126
- */
127
- async *deleteMany(cids, options = {}) {
128
- options.onProgress?.(new CustomProgressEvent('blocks:delete-many:blockstore:delete-many'));
129
- yield* this.child.deleteMany((async function* () {
130
- for await (const cid of cids) {
131
- yield cid;
132
- }
133
- }()), options);
134
- }
135
- async has(cid, options = {}) {
136
- return this.child.has(cid, options);
137
- }
138
- async *getAll(options = {}) {
139
- options.onProgress?.(new CustomProgressEvent('blocks:get-all:blockstore:get-many'));
140
- yield* this.child.getAll(options);
141
- }
142
- }
143
- /**
144
- * Race block providers cancelling any pending requests once the block has been
145
- * found.
146
- */
147
- async function raceBlockRetrievers(cid, blockBrokers, hasher, options) {
148
- const validateFn = getCidBlockVerifierFunction(cid, hasher);
149
- const controller = new AbortController();
150
- const signal = anySignal([controller.signal, options.signal]);
151
- setMaxListeners(Infinity, controller.signal, signal);
152
- const retrievers = [];
153
- for (const broker of blockBrokers) {
154
- if (isRetrievingBlockBroker(broker)) {
155
- retrievers.push(broker);
156
- }
157
- }
158
- if (retrievers.length === 0) {
159
- throw new InvalidConfigurationError(`No block brokers capable of retrieving blocks are configured, the CID ${cid} cannot be fetched from the network`);
160
- }
161
- try {
162
- return await Promise.any(retrievers
163
- .map(async (retriever) => {
164
- try {
165
- let blocksWereValidated = false;
166
- const block = await retriever.retrieve(cid, {
167
- ...options,
168
- signal,
169
- validateFn: async (block) => {
170
- await validateFn(block);
171
- options.signal?.throwIfAborted();
172
- blocksWereValidated = true;
173
- }
174
- });
175
- if (!blocksWereValidated) {
176
- // the blockBroker either did not throw an error when attempting to validate the block
177
- // or did not call the validateFn at all. We should validate the block ourselves
178
- await validateFn(block);
179
- options.signal?.throwIfAborted();
180
- }
181
- return block;
182
- }
183
- catch (err) {
184
- options.log.error('could not retrieve verified block for %c from %s - %e', cid, retriever.name, err);
185
- throw err;
186
- }
187
- }));
188
- }
189
- catch (err) {
190
- throw new LoadBlockFailedError(err.errors, `Failed to load block for ${cid}`);
191
- }
192
- finally {
193
- // we have the block from the fastest block retriever, abort any still
194
- // in-flight retrieve attempts
195
- controller.abort();
196
- signal.clear();
197
- }
198
- }
199
- function isRetrievingBlockBroker(broker) {
200
- return typeof broker.retrieve === 'function';
201
- }
202
- export const getCidBlockVerifierFunction = (cid, hasher) => {
203
- if (hasher == null) {
204
- throw new InvalidParametersError(`No hasher configured for multihash code 0x${cid.multihash.code.toString(16)}, please configure one. You can look up which hash this is at https://github.com/multiformats/multicodec/blob/master/table.csv`);
205
- }
206
- return async (block) => {
207
- // verify block
208
- let hash;
209
- const res = hasher.digest(block, {
210
- // support truncated hashes where they are truncated
211
- truncate: cid.multihash.digest.byteLength
212
- });
213
- if (isPromise(res)) {
214
- hash = await res;
215
- }
216
- else {
217
- hash = res;
218
- }
219
- if (!uint8ArrayEquals(hash.digest, cid.multihash.digest)) {
220
- // if a hash mismatch occurs for a TrustlessGatewayBlockBroker, we should try another gateway
221
- throw new InvalidMultihashError('Hash of downloaded block did not match multihash from passed CID');
222
- }
223
- };
224
- };
225
- //# sourceMappingURL=storage.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"storage.js","sourceRoot":"","sources":["../../../src/utils/storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,sBAAsB,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AAClG,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AACtC,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAA;AAC7D,OAAO,MAAM,MAAM,WAAW,CAAA;AAC9B,OAAO,OAAO,MAAM,YAAY,CAAA;AAChC,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAA;AACrD,OAAO,EAAE,MAAM,IAAI,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AAC/D,OAAO,EAAE,8BAA8B,EAAE,yBAAyB,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAA;AAC9G,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAqB3C,MAAM,uCAAuC,GAAG,GAAG,CAAA;AAEnD,MAAM,OAAO,OAAO;IACC,KAAK,CAAY;IACjB,SAAS,CAAc;IAChC,GAAG,CAAQ;IACF,MAAM,CAAiB;IACvB,YAAY,CAAU;IAEzC;;OAEG;IACH,YAAa,UAAqC,EAAE,OAAoB,EAAE;QACxE,IAAI,CAAC,GAAG,GAAG,UAAU,CAAC,MAAM,CAAC,YAAY,CAAC,yBAAyB,CAAC,CAAA;QACpE,IAAI,CAAC,MAAM,GAAG,UAAU,CAAC,MAAM,CAAA;QAC/B,IAAI,CAAC,YAAY,GAAG,UAAU,CAAC,YAAY,CAAA;QAC3C,IAAI,CAAC,KAAK,GAAG,IAAI,kBAAkB,CAAC,UAAU,CAAC,UAAU,EAAE;YACzD,eAAe,EAAE,IAAI,CAAC,2BAA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gBAAgB,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC;YACzD,6FAA6F;YAC7F,MAAM,IAAI,qBAAqB,CAAC,kEAAkE,CAAC,CAAA;QACrG,CAAC;IACH,CAAC,CAAA;AACH,CAAC,CAAA"}
@@ -1,175 +0,0 @@
1
- import { setMaxListeners } from '@libp2p/interface'
2
- import { anySignal } from 'any-signal'
3
- import { Storage } from './storage.ts'
4
- import type { StorageComponents } from './storage.ts'
5
- import type { Pair, DeleteManyBlocksProgressEvents, DeleteBlockProgressEvents, GetBlockProgressEvents, GetManyBlocksProgressEvents, PutManyBlocksProgressEvents, PutBlockProgressEvents, GetAllBlocksProgressEvents, GetOfflineOptions, SessionBlockstore, SessionBlockBroker } from '@helia/interface/blocks'
6
- import type { AbortOptions, PeerId } from '@libp2p/interface'
7
- import type { Multiaddr } from '@multiformats/multiaddr'
8
- import type { InputPair } from 'interface-blockstore'
9
- import type { AwaitIterable } from 'interface-store'
10
- import type { CID } from 'multiformats/cid'
11
- import type { ProgressOptions } from 'progress-events'
12
-
13
- export interface SessionStorageInit {
14
- root: CID
15
- }
16
-
17
- /**
18
- * Storage subclass that can cancel any ongoing operation at any point.
19
- */
20
- export class SessionStorage extends Storage<SessionBlockBroker> implements SessionBlockstore {
21
- private readonly closeController: AbortController
22
-
23
- constructor (components: StorageComponents<SessionBlockBroker>, init: SessionStorageInit) {
24
- super(components)
25
-
26
- // because brokers are allowed to continue searching for providers after the
27
- // session has been created, we need a way to tell them that the user has
28
- // finished using the session any in-flight requests should be cancelled
29
- this.closeController = new AbortController()
30
- setMaxListeners(Infinity, this.closeController.signal)
31
-
32
- this.log = components.logger.forComponent(`helia:session-storage:${init.root}`)
33
- }
34
-
35
- close (): void {
36
- this.closeController.abort()
37
- }
38
-
39
- async addPeer (peer: PeerId | Multiaddr | Multiaddr[], options?: AbortOptions): Promise<void> {
40
- await Promise.all(
41
- this.blockBrokers
42
- .map(broker => broker.addPeer(peer, options))
43
- )
44
- }
45
-
46
- /**
47
- * Put a block to the underlying datastore
48
- */
49
- async put (cid: CID, block: Uint8Array, options: AbortOptions & ProgressOptions<PutBlockProgressEvents> = {}): Promise<CID> {
50
- const signal = anySignal([this.closeController.signal, options.signal])
51
- setMaxListeners(Infinity, signal)
52
-
53
- try {
54
- return await super.put(cid, block, {
55
- ...options,
56
- signal
57
- })
58
- } finally {
59
- signal.clear()
60
- }
61
- }
62
-
63
- /**
64
- * Put a multiple blocks to the underlying datastore
65
- */
66
- async * putMany (blocks: AwaitIterable<InputPair>, options: AbortOptions & ProgressOptions<PutManyBlocksProgressEvents> = {}): AsyncGenerator<CID> {
67
- const signal = anySignal([this.closeController.signal, options.signal])
68
- setMaxListeners(Infinity, signal)
69
-
70
- try {
71
- yield * super.putMany(blocks, {
72
- ...options,
73
- signal
74
- })
75
- } finally {
76
- signal.clear()
77
- }
78
- }
79
-
80
- /**
81
- * Get a block by cid
82
- */
83
- async * get (cid: CID, options: GetOfflineOptions & AbortOptions & ProgressOptions<GetBlockProgressEvents> = {}): AsyncGenerator<Uint8Array> {
84
- const signal = anySignal([this.closeController.signal, options.signal])
85
- setMaxListeners(Infinity, signal)
86
-
87
- try {
88
- yield * super.get(cid, {
89
- ...options,
90
- signal
91
- })
92
- } finally {
93
- signal.clear()
94
- }
95
- }
96
-
97
- /**
98
- * Get multiple blocks back from an (async) iterable of cids
99
- */
100
- async * getMany (cids: AwaitIterable<CID>, options: GetOfflineOptions & AbortOptions & ProgressOptions<GetManyBlocksProgressEvents> = {}): AsyncGenerator<Pair> {
101
- const signal = anySignal([this.closeController.signal, options.signal])
102
- setMaxListeners(Infinity, signal)
103
-
104
- try {
105
- yield * super.getMany(cids, {
106
- ...options,
107
- signal
108
- })
109
- } finally {
110
- signal.clear()
111
- }
112
- }
113
-
114
- /**
115
- * Delete a block from the blockstore
116
- */
117
- async delete (cid: CID, options: AbortOptions & ProgressOptions<DeleteBlockProgressEvents> = {}): Promise<void> {
118
- const signal = anySignal([this.closeController.signal, options.signal])
119
- setMaxListeners(Infinity, signal)
120
-
121
- try {
122
- await super.delete(cid, {
123
- ...options,
124
- signal
125
- })
126
- } finally {
127
- signal.clear()
128
- }
129
- }
130
-
131
- /**
132
- * Delete multiple blocks from the blockstore
133
- */
134
- async * deleteMany (cids: AwaitIterable<CID>, options: AbortOptions & ProgressOptions<DeleteManyBlocksProgressEvents> = {}): AsyncGenerator<CID> {
135
- const signal = anySignal([this.closeController.signal, options.signal])
136
- setMaxListeners(Infinity, signal)
137
-
138
- try {
139
- yield * super.deleteMany(cids, {
140
- ...options,
141
- signal
142
- })
143
- } finally {
144
- signal.clear()
145
- }
146
- }
147
-
148
- async has (cid: CID, options: AbortOptions = {}): Promise<boolean> {
149
- const signal = anySignal([this.closeController.signal, options.signal])
150
- setMaxListeners(Infinity, signal)
151
-
152
- try {
153
- return await super.has(cid, {
154
- ...options,
155
- signal
156
- })
157
- } finally {
158
- signal.clear()
159
- }
160
- }
161
-
162
- async * getAll (options: AbortOptions & ProgressOptions<GetAllBlocksProgressEvents> = {}): AsyncGenerator<Pair> {
163
- const signal = anySignal([this.closeController.signal, options.signal])
164
- setMaxListeners(Infinity, signal)
165
-
166
- try {
167
- yield * super.getAll({
168
- ...options,
169
- signal
170
- })
171
- } finally {
172
- signal.clear()
173
- }
174
- }
175
- }