@helia/utils 2.5.2 → 3.0.0
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/index.min.js +1 -1
- package/dist/index.min.js.map +4 -4
- package/dist/src/abstract-session.d.ts +3 -3
- package/dist/src/abstract-session.d.ts.map +1 -1
- package/dist/src/abstract-session.js.map +1 -1
- package/dist/src/errors.d.ts +4 -0
- package/dist/src/errors.d.ts.map +1 -1
- package/dist/src/errors.js +4 -0
- package/dist/src/errors.js.map +1 -1
- package/dist/src/graph-walker.d.ts +3 -21
- package/dist/src/graph-walker.d.ts.map +1 -1
- package/dist/src/graph-walker.js +20 -17
- package/dist/src/graph-walker.js.map +1 -1
- package/dist/src/index.d.ts +5 -168
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +5 -130
- package/dist/src/index.js.map +1 -1
- package/dist/src/is-cid.d.ts +3 -0
- package/dist/src/is-cid.d.ts.map +1 -0
- package/dist/src/is-cid.js +8 -0
- package/dist/src/is-cid.js.map +1 -0
- package/dist/src/is-promise.d.ts.map +1 -0
- package/dist/src/is-promise.js.map +1 -0
- package/dist/typedoc-urls.json +11 -10
- package/package.json +18 -37
- package/src/abstract-session.ts +4 -4
- package/src/errors.ts +5 -0
- package/src/graph-walker.ts +30 -43
- package/src/index.ts +5 -333
- package/src/is-cid.ts +9 -0
- package/dist/src/pins.d.ts +0 -21
- package/dist/src/pins.d.ts.map +0 -1
- package/dist/src/pins.js +0 -169
- package/dist/src/pins.js.map +0 -1
- package/dist/src/routing.d.ts +0 -49
- package/dist/src/routing.d.ts.map +0 -1
- package/dist/src/routing.js +0 -305
- package/dist/src/routing.js.map +0 -1
- package/dist/src/storage.d.ts +0 -63
- package/dist/src/storage.d.ts.map +0 -1
- package/dist/src/storage.js +0 -159
- package/dist/src/storage.js.map +0 -1
- package/dist/src/utils/datastore-version.d.ts +0 -3
- package/dist/src/utils/datastore-version.d.ts.map +0 -1
- package/dist/src/utils/datastore-version.js +0 -20
- package/dist/src/utils/datastore-version.js.map +0 -1
- package/dist/src/utils/get-codec.d.ts +0 -4
- package/dist/src/utils/get-codec.d.ts.map +0 -1
- package/dist/src/utils/get-codec.js +0 -38
- package/dist/src/utils/get-codec.js.map +0 -1
- package/dist/src/utils/get-hasher.d.ts +0 -4
- package/dist/src/utils/get-hasher.d.ts.map +0 -1
- package/dist/src/utils/get-hasher.js +0 -32
- package/dist/src/utils/get-hasher.js.map +0 -1
- package/dist/src/utils/is-promise.d.ts.map +0 -1
- package/dist/src/utils/is-promise.js.map +0 -1
- package/dist/src/utils/networked-storage.d.ts +0 -27
- package/dist/src/utils/networked-storage.d.ts.map +0 -1
- package/dist/src/utils/networked-storage.js +0 -52
- package/dist/src/utils/networked-storage.js.map +0 -1
- package/dist/src/utils/session-storage.d.ts +0 -48
- package/dist/src/utils/session-storage.d.ts.map +0 -1
- package/dist/src/utils/session-storage.js +0 -148
- package/dist/src/utils/session-storage.js.map +0 -1
- package/dist/src/utils/storage.d.ts +0 -56
- package/dist/src/utils/storage.d.ts.map +0 -1
- package/dist/src/utils/storage.js +0 -225
- package/dist/src/utils/storage.js.map +0 -1
- package/src/pins.ts +0 -247
- package/src/routing.ts +0 -376
- package/src/storage.ts +0 -195
- package/src/utils/datastore-version.ts +0 -25
- package/src/utils/get-codec.ts +0 -47
- package/src/utils/get-hasher.ts +0 -40
- package/src/utils/networked-storage.ts +0 -73
- package/src/utils/session-storage.ts +0 -175
- package/src/utils/storage.ts +0 -295
- /package/dist/src/{utils/is-promise.d.ts → is-promise.d.ts} +0 -0
- /package/dist/src/{utils/is-promise.js → is-promise.js} +0 -0
- /package/src/{utils/is-promise.ts → is-promise.ts} +0 -0
|
@@ -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,CAAA;AACpD,OAAO,KAAK,EAAE,WAAW,EAAE,IAAI,EAAE,8BAA8B,EAAE,yBAAyB,EAAE,sBAAsB,EAAE,2BAA2B,EAAE,2BAA2B,EAAE,sBAAsB,EAAE,0BAA0B,EAAE,iBAAiB,EAAE,qBAAqB,EAAE,MAAM,yBAA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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,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A6C,GAAG,CAAC,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,gIAAgI,CAAC,CAAA;IAChP,CAAC;IAED,OAAO,KAAK,EAAE,KAAiB,EAAiB,EAAE;QAChD,eAAe;QACf,IAAI,IAA6B,CAAA;QACjC,MAAM,GAAG,GAAG,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE;YAC/B,oDAAoD;YACpD,QAAQ,EAAE,GAAG,CAAC,SAAS,CAAC,MAAM,CAAC,UAAU;SAC1C,CAAC,CAAA;QAEF,IAAI,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC;YACnB,IAAI,GAAG,MAAM,GAAG,CAAA;QAClB,CAAC;aAAM,CAAC;YACN,IAAI,GAAG,GAAG,CAAA;QACZ,CAAC;QAED,IAAI,CAAC,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"}
|
package/src/pins.ts
DELETED
|
@@ -1,247 +0,0 @@
|
|
|
1
|
-
import { InvalidParametersError } from '@libp2p/interface'
|
|
2
|
-
import { Queue } from '@libp2p/utils'
|
|
3
|
-
import * as cborg from 'cborg'
|
|
4
|
-
import { Key } from 'interface-datastore'
|
|
5
|
-
import toBuffer from 'it-to-buffer'
|
|
6
|
-
import { base36 } from 'multiformats/bases/base36'
|
|
7
|
-
import { createUnsafe } from 'multiformats/block'
|
|
8
|
-
import { CID } from 'multiformats/cid'
|
|
9
|
-
import { CustomProgressEvent } from 'progress-events'
|
|
10
|
-
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
|
|
11
|
-
import { AlreadyPinnedError } from './errors.ts'
|
|
12
|
-
import type { CodecLoader } from '@helia/interface'
|
|
13
|
-
import type { GetBlockProgressEvents } from '@helia/interface/blocks'
|
|
14
|
-
import type { AddOptions, AddPinEvents, IsPinnedOptions, LsOptions, Pin, Pins, RmOptions } from '@helia/interface/pins'
|
|
15
|
-
import type { AbortOptions } from '@libp2p/interface'
|
|
16
|
-
import type { Blockstore } from 'interface-blockstore'
|
|
17
|
-
import type { Datastore } from 'interface-datastore'
|
|
18
|
-
import type { Version } from 'multiformats/cid'
|
|
19
|
-
import type { ProgressOptions } from 'progress-events'
|
|
20
|
-
|
|
21
|
-
interface DatastorePin {
|
|
22
|
-
/**
|
|
23
|
-
* 0 for a direct pin or an arbitrary (+ve, whole) number or Infinity
|
|
24
|
-
*/
|
|
25
|
-
depth: number
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* User-specific metadata for the pin
|
|
29
|
-
*/
|
|
30
|
-
metadata: Record<string, string | number | boolean>
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
interface DatastorePinnedBlock {
|
|
34
|
-
pinCount: number
|
|
35
|
-
pinnedBy: Uint8Array[]
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Callback for updating a {@link DatastorePinnedBlock}'s properties when
|
|
40
|
-
* calling `#updatePinnedBlock`
|
|
41
|
-
*
|
|
42
|
-
* The callback should return `false` to prevent any pinning modifications to
|
|
43
|
-
* the block, and true in all other cases.
|
|
44
|
-
*/
|
|
45
|
-
interface WithPinnedBlockCallback {
|
|
46
|
-
(pinnedBlock: DatastorePinnedBlock): boolean
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const DATASTORE_PIN_PREFIX = '/pin/'
|
|
50
|
-
const DATASTORE_BLOCK_PREFIX = '/pinned-block/'
|
|
51
|
-
const DATASTORE_ENCODING = base36
|
|
52
|
-
const DAG_WALK_QUEUE_CONCURRENCY = 1
|
|
53
|
-
|
|
54
|
-
interface WalkDagOptions extends AbortOptions, ProgressOptions<GetBlockProgressEvents | AddPinEvents> {
|
|
55
|
-
depth: number
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function toDSKey (cid: CID): Key {
|
|
59
|
-
if (cid.version === 0) {
|
|
60
|
-
cid = cid.toV1()
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
return new Key(`${DATASTORE_PIN_PREFIX}${cid.toString(DATASTORE_ENCODING)}`)
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export class PinsImpl implements Pins {
|
|
67
|
-
private readonly datastore: Datastore
|
|
68
|
-
private readonly blockstore: Blockstore
|
|
69
|
-
private readonly getCodec: CodecLoader
|
|
70
|
-
|
|
71
|
-
constructor (datastore: Datastore, blockstore: Blockstore, getCodec: CodecLoader) {
|
|
72
|
-
this.datastore = datastore
|
|
73
|
-
this.blockstore = blockstore
|
|
74
|
-
this.getCodec = getCodec
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
async * add (cid: CID<unknown, number, number, Version>, options: AddOptions = {}): AsyncGenerator<CID, void, undefined> {
|
|
78
|
-
const pinKey = toDSKey(cid)
|
|
79
|
-
|
|
80
|
-
if (await this.datastore.has(pinKey)) {
|
|
81
|
-
throw new AlreadyPinnedError('Already pinned')
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const depth = Math.round(options.depth ?? Infinity)
|
|
85
|
-
|
|
86
|
-
if (depth < 0) {
|
|
87
|
-
throw new InvalidParametersError('Depth must be greater than or equal to 0')
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// use a queue to walk the DAG instead of recursion so we can traverse very large DAGs
|
|
91
|
-
const queue = new Queue<AsyncGenerator<CID>>({
|
|
92
|
-
concurrency: DAG_WALK_QUEUE_CONCURRENCY
|
|
93
|
-
})
|
|
94
|
-
|
|
95
|
-
for await (const childCid of this.#walkDag(cid, queue, {
|
|
96
|
-
...options,
|
|
97
|
-
depth
|
|
98
|
-
})) {
|
|
99
|
-
await this.#updatePinnedBlock(childCid, (pinnedBlock: DatastorePinnedBlock) => {
|
|
100
|
-
// do not update pinned block if this block is already pinned by this CID
|
|
101
|
-
if (pinnedBlock.pinnedBy.find(c => uint8ArrayEquals(c, cid.bytes)) != null) {
|
|
102
|
-
return false
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
pinnedBlock.pinCount++
|
|
106
|
-
pinnedBlock.pinnedBy.push(cid.bytes)
|
|
107
|
-
return true
|
|
108
|
-
}, options)
|
|
109
|
-
|
|
110
|
-
yield childCid
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
const pin: DatastorePin = {
|
|
114
|
-
depth,
|
|
115
|
-
metadata: options.metadata ?? {}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
await this.datastore.put(pinKey, cborg.encode(pin), options)
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Walk a DAG in an iterable fashion
|
|
123
|
-
*/
|
|
124
|
-
async * #walkDag (cid: CID, queue: Queue<AsyncGenerator<CID>>, options: WalkDagOptions): AsyncGenerator<CID> {
|
|
125
|
-
if (options.depth === -1) {
|
|
126
|
-
return
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
const codec = await this.getCodec(cid.code)
|
|
130
|
-
const bytes = await toBuffer(this.blockstore.get(cid, options))
|
|
131
|
-
const block = createUnsafe({ bytes, cid, codec })
|
|
132
|
-
|
|
133
|
-
yield cid
|
|
134
|
-
|
|
135
|
-
// walk dag, ensure all blocks are present
|
|
136
|
-
for (const [,cid] of block.links()) {
|
|
137
|
-
yield * await queue.add(async () => {
|
|
138
|
-
return this.#walkDag(cid, queue, {
|
|
139
|
-
...options,
|
|
140
|
-
depth: options.depth - 1
|
|
141
|
-
})
|
|
142
|
-
})
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* Update the pin count for the CID
|
|
148
|
-
*/
|
|
149
|
-
async #updatePinnedBlock (cid: CID, withPinnedBlock: WithPinnedBlockCallback, options: AddOptions): Promise<void> {
|
|
150
|
-
const blockKey = new Key(`${DATASTORE_BLOCK_PREFIX}${DATASTORE_ENCODING.encode(cid.multihash.bytes)}`)
|
|
151
|
-
|
|
152
|
-
let pinnedBlock: DatastorePinnedBlock = {
|
|
153
|
-
pinCount: 0,
|
|
154
|
-
pinnedBy: []
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
try {
|
|
158
|
-
pinnedBlock = cborg.decode(await this.datastore.get(blockKey, options))
|
|
159
|
-
} catch (err: any) {
|
|
160
|
-
if (err.name !== 'NotFoundError') {
|
|
161
|
-
throw err
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
const shouldContinue = withPinnedBlock(pinnedBlock)
|
|
166
|
-
|
|
167
|
-
if (!shouldContinue) {
|
|
168
|
-
return
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
if (pinnedBlock.pinCount === 0) {
|
|
172
|
-
if (await this.datastore.has(blockKey)) {
|
|
173
|
-
await this.datastore.delete(blockKey)
|
|
174
|
-
return
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
await this.datastore.put(blockKey, cborg.encode(pinnedBlock), options)
|
|
179
|
-
options.onProgress?.(new CustomProgressEvent<CID>('helia:pin:add', cid))
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
async * rm (cid: CID<unknown, number, number, Version>, options: RmOptions = {}): AsyncGenerator<CID, void, undefined> {
|
|
183
|
-
const pinKey = toDSKey(cid)
|
|
184
|
-
const buf = await this.datastore.get(pinKey, options)
|
|
185
|
-
const pin = cborg.decode(buf)
|
|
186
|
-
|
|
187
|
-
await this.datastore.delete(pinKey, options)
|
|
188
|
-
|
|
189
|
-
// use a queue to walk the DAG instead of recursion so we can traverse very large DAGs
|
|
190
|
-
const queue = new Queue<AsyncGenerator<CID>>({
|
|
191
|
-
concurrency: DAG_WALK_QUEUE_CONCURRENCY
|
|
192
|
-
})
|
|
193
|
-
|
|
194
|
-
for await (const childCid of this.#walkDag(cid, queue, {
|
|
195
|
-
...options,
|
|
196
|
-
depth: pin.depth
|
|
197
|
-
})) {
|
|
198
|
-
await this.#updatePinnedBlock(childCid, (pinnedBlock): boolean => {
|
|
199
|
-
pinnedBlock.pinCount--
|
|
200
|
-
pinnedBlock.pinnedBy = pinnedBlock.pinnedBy.filter(c => uint8ArrayEquals(c, cid.bytes))
|
|
201
|
-
return true
|
|
202
|
-
}, {
|
|
203
|
-
...options,
|
|
204
|
-
depth: pin.depth
|
|
205
|
-
})
|
|
206
|
-
|
|
207
|
-
yield childCid
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
async * ls (options: LsOptions = {}): AsyncGenerator<Pin, void, undefined> {
|
|
212
|
-
for await (const { key, value } of this.datastore.query({
|
|
213
|
-
prefix: DATASTORE_PIN_PREFIX + (options.cid != null ? `${options.cid.toString(base36)}` : '')
|
|
214
|
-
}, options)) {
|
|
215
|
-
const cid = CID.parse(key.toString().substring(5), base36)
|
|
216
|
-
const pin = cborg.decode(value)
|
|
217
|
-
|
|
218
|
-
yield {
|
|
219
|
-
cid,
|
|
220
|
-
...pin
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
async isPinned (cid: CID, options: IsPinnedOptions = {}): Promise<boolean> {
|
|
226
|
-
const blockKey = new Key(`${DATASTORE_BLOCK_PREFIX}${DATASTORE_ENCODING.encode(cid.multihash.bytes)}`)
|
|
227
|
-
|
|
228
|
-
return this.datastore.has(blockKey, options)
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
async get (cid: CID, options?: AbortOptions): Promise<Pin> {
|
|
232
|
-
const pinKey = toDSKey(cid)
|
|
233
|
-
const buf = await this.datastore.get(pinKey, options)
|
|
234
|
-
|
|
235
|
-
return cborg.decode(buf)
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
async setMetadata (cid: CID, metadata: Record<string, string | number | boolean> | undefined, options?: AbortOptions): Promise<void> {
|
|
239
|
-
const pinKey = toDSKey(cid)
|
|
240
|
-
const buf = await this.datastore.get(pinKey, options)
|
|
241
|
-
const pin: DatastorePin = cborg.decode(buf)
|
|
242
|
-
|
|
243
|
-
pin.metadata = metadata ?? {}
|
|
244
|
-
|
|
245
|
-
await this.datastore.put(pinKey, cborg.encode(pin), options)
|
|
246
|
-
}
|
|
247
|
-
}
|