@helia/utils 2.5.2-1361bfa5 → 2.5.2-16d805e1

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 (77) hide show
  1. package/dist/index.min.js +1 -22
  2. package/dist/index.min.js.map +4 -4
  3. package/dist/src/index.d.ts +5 -171
  4. package/dist/src/index.d.ts.map +1 -1
  5. package/dist/src/index.js +5 -162
  6. package/dist/src/index.js.map +1 -1
  7. package/dist/src/is-cid.d.ts.map +1 -0
  8. package/dist/src/is-cid.js.map +1 -0
  9. package/dist/src/is-promise.d.ts.map +1 -0
  10. package/dist/src/is-promise.js.map +1 -0
  11. package/package.json +7 -24
  12. package/src/index.ts +5 -368
  13. package/dist/src/pins.d.ts +0 -21
  14. package/dist/src/pins.d.ts.map +0 -1
  15. package/dist/src/pins.js +0 -141
  16. package/dist/src/pins.js.map +0 -1
  17. package/dist/src/routing.d.ts +0 -51
  18. package/dist/src/routing.d.ts.map +0 -1
  19. package/dist/src/routing.js +0 -311
  20. package/dist/src/routing.js.map +0 -1
  21. package/dist/src/storage.d.ts +0 -62
  22. package/dist/src/storage.d.ts.map +0 -1
  23. package/dist/src/storage.js +0 -159
  24. package/dist/src/storage.js.map +0 -1
  25. package/dist/src/utils/constants.d.ts +0 -4
  26. package/dist/src/utils/constants.d.ts.map +0 -1
  27. package/dist/src/utils/constants.js +0 -4
  28. package/dist/src/utils/constants.js.map +0 -1
  29. package/dist/src/utils/datastore-version.d.ts +0 -3
  30. package/dist/src/utils/datastore-version.d.ts.map +0 -1
  31. package/dist/src/utils/datastore-version.js +0 -20
  32. package/dist/src/utils/datastore-version.js.map +0 -1
  33. package/dist/src/utils/get-codec.d.ts +0 -4
  34. package/dist/src/utils/get-codec.d.ts.map +0 -1
  35. package/dist/src/utils/get-codec.js +0 -37
  36. package/dist/src/utils/get-codec.js.map +0 -1
  37. package/dist/src/utils/get-crypto.d.ts +0 -4
  38. package/dist/src/utils/get-crypto.d.ts.map +0 -1
  39. package/dist/src/utils/get-crypto.js +0 -35
  40. package/dist/src/utils/get-crypto.js.map +0 -1
  41. package/dist/src/utils/get-hasher.d.ts +0 -4
  42. package/dist/src/utils/get-hasher.d.ts.map +0 -1
  43. package/dist/src/utils/get-hasher.js +0 -32
  44. package/dist/src/utils/get-hasher.js.map +0 -1
  45. package/dist/src/utils/is-cid.d.ts.map +0 -1
  46. package/dist/src/utils/is-cid.js.map +0 -1
  47. package/dist/src/utils/is-promise.d.ts.map +0 -1
  48. package/dist/src/utils/is-promise.js.map +0 -1
  49. package/dist/src/utils/networked-storage.d.ts +0 -28
  50. package/dist/src/utils/networked-storage.d.ts.map +0 -1
  51. package/dist/src/utils/networked-storage.js +0 -52
  52. package/dist/src/utils/networked-storage.js.map +0 -1
  53. package/dist/src/utils/session-storage.d.ts +0 -47
  54. package/dist/src/utils/session-storage.d.ts.map +0 -1
  55. package/dist/src/utils/session-storage.js +0 -148
  56. package/dist/src/utils/session-storage.js.map +0 -1
  57. package/dist/src/utils/storage.d.ts +0 -55
  58. package/dist/src/utils/storage.d.ts.map +0 -1
  59. package/dist/src/utils/storage.js +0 -225
  60. package/dist/src/utils/storage.js.map +0 -1
  61. package/src/pins.ts +0 -209
  62. package/src/routing.ts +0 -389
  63. package/src/storage.ts +0 -194
  64. package/src/utils/constants.ts +0 -3
  65. package/src/utils/datastore-version.ts +0 -25
  66. package/src/utils/get-codec.ts +0 -45
  67. package/src/utils/get-crypto.ts +0 -44
  68. package/src/utils/get-hasher.ts +0 -40
  69. package/src/utils/networked-storage.ts +0 -74
  70. package/src/utils/session-storage.ts +0 -174
  71. package/src/utils/storage.ts +0 -294
  72. /package/dist/src/{utils/is-cid.d.ts → is-cid.d.ts} +0 -0
  73. /package/dist/src/{utils/is-cid.js → is-cid.js} +0 -0
  74. /package/dist/src/{utils/is-promise.d.ts → is-promise.d.ts} +0 -0
  75. /package/dist/src/{utils/is-promise.js → is-promise.js} +0 -0
  76. /package/src/{utils/is-cid.ts → is-cid.ts} +0 -0
  77. /package/src/{utils/is-promise.ts → is-promise.ts} +0 -0
@@ -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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,CAAA;AACH,CAAC,CAAA"}
package/src/pins.ts DELETED
@@ -1,209 +0,0 @@
1
- import { InvalidParametersError } from '@libp2p/interface'
2
- import * as cborg from 'cborg'
3
- import { Key } from 'interface-datastore'
4
- import { base36 } from 'multiformats/bases/base36'
5
- import { CID } from 'multiformats/cid'
6
- import { CustomProgressEvent } from 'progress-events'
7
- import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
8
- import { AlreadyPinnedError } from './errors.ts'
9
- import { depthFirstWalker } from './graph-walker.ts'
10
- import type { CodecLoader } from '@helia/interface'
11
- import type { AddOptions, IsPinnedOptions, LsOptions, Pin, Pins, RmOptions } from '@helia/interface'
12
- import type { AbortOptions } from '@libp2p/interface'
13
- import type { Blockstore } from 'interface-blockstore'
14
- import type { Datastore } from 'interface-datastore'
15
- import type { Version } from 'multiformats/cid'
16
-
17
- interface DatastorePin {
18
- /**
19
- * 0 for a direct pin or an arbitrary (+ve, whole) number or Infinity
20
- */
21
- depth: number
22
-
23
- /**
24
- * User-specific metadata for the pin
25
- */
26
- metadata: Record<string, string | number | boolean>
27
- }
28
-
29
- interface DatastorePinnedBlock {
30
- pinCount: number
31
- pinnedBy: Uint8Array[]
32
- }
33
-
34
- /**
35
- * Callback for updating a {@link DatastorePinnedBlock}'s properties when
36
- * calling `#updatePinnedBlock`
37
- *
38
- * The callback should return `false` to prevent any pinning modifications to
39
- * the block, and true in all other cases.
40
- */
41
- interface WithPinnedBlockCallback {
42
- (pinnedBlock: DatastorePinnedBlock): boolean
43
- }
44
-
45
- const DATASTORE_PIN_PREFIX = '/pin/'
46
- const DATASTORE_BLOCK_PREFIX = '/pinned-block/'
47
- const DATASTORE_ENCODING = base36
48
-
49
- function toDSKey (cid: CID): Key {
50
- if (cid.version === 0) {
51
- cid = cid.toV1()
52
- }
53
-
54
- return new Key(`${DATASTORE_PIN_PREFIX}${cid.toString(DATASTORE_ENCODING)}`)
55
- }
56
-
57
- export class PinsImpl implements Pins {
58
- private readonly datastore: Datastore
59
- private readonly blockstore: Blockstore
60
- private readonly getCodec: CodecLoader
61
-
62
- constructor (datastore: Datastore, blockstore: Blockstore, getCodec: CodecLoader) {
63
- this.datastore = datastore
64
- this.blockstore = blockstore
65
- this.getCodec = getCodec
66
- }
67
-
68
- async * add (cid: CID<unknown, number, number, Version>, options: AddOptions = {}): AsyncGenerator<CID, void, undefined> {
69
- const pinKey = toDSKey(cid)
70
-
71
- if (await this.datastore.has(pinKey)) {
72
- throw new AlreadyPinnedError('Already pinned')
73
- }
74
-
75
- const depth = Math.round(options.depth ?? Infinity)
76
-
77
- if (depth < 0) {
78
- throw new InvalidParametersError('Depth must be greater than or equal to 0')
79
- }
80
-
81
- const walker = (options.walker ?? depthFirstWalker())({
82
- blockstore: this.blockstore,
83
- getCodec: this.getCodec
84
- })
85
-
86
- for await (const node of walker.walk(cid, { ...options, depth })) {
87
- const childCid = node.block.cid
88
- await this.#updatePinnedBlock(childCid, (pinnedBlock: DatastorePinnedBlock) => {
89
- // do not update pinned block if this block is already pinned by this CID
90
- if (pinnedBlock.pinnedBy.find(c => uint8ArrayEquals(c, cid.bytes)) != null) {
91
- return false
92
- }
93
-
94
- pinnedBlock.pinCount++
95
- pinnedBlock.pinnedBy.push(cid.bytes)
96
- return true
97
- }, options)
98
-
99
- yield childCid
100
- }
101
-
102
- const pin: DatastorePin = {
103
- depth,
104
- metadata: options.metadata ?? {}
105
- }
106
-
107
- await this.datastore.put(pinKey, cborg.encode(pin), options)
108
- }
109
-
110
- /**
111
- * Update the pin count for the CID
112
- */
113
- async #updatePinnedBlock (cid: CID, withPinnedBlock: WithPinnedBlockCallback, options: AddOptions): Promise<void> {
114
- const blockKey = new Key(`${DATASTORE_BLOCK_PREFIX}${DATASTORE_ENCODING.encode(cid.multihash.bytes)}`)
115
-
116
- let pinnedBlock: DatastorePinnedBlock = {
117
- pinCount: 0,
118
- pinnedBy: []
119
- }
120
-
121
- try {
122
- pinnedBlock = cborg.decode(await this.datastore.get(blockKey, options))
123
- } catch (err: any) {
124
- if (err.name !== 'NotFoundError') {
125
- throw err
126
- }
127
- }
128
-
129
- const shouldContinue = withPinnedBlock(pinnedBlock)
130
-
131
- if (!shouldContinue) {
132
- return
133
- }
134
-
135
- if (pinnedBlock.pinCount === 0) {
136
- if (await this.datastore.has(blockKey)) {
137
- await this.datastore.delete(blockKey)
138
- return
139
- }
140
- }
141
-
142
- await this.datastore.put(blockKey, cborg.encode(pinnedBlock), options)
143
- options.onProgress?.(new CustomProgressEvent<CID>('helia:pin:add', cid))
144
- }
145
-
146
- async * rm (cid: CID<unknown, number, number, Version>, options: RmOptions = {}): AsyncGenerator<CID, void, undefined> {
147
- const pinKey = toDSKey(cid)
148
- const buf = await this.datastore.get(pinKey, options)
149
- const pin = cborg.decode(buf)
150
-
151
- await this.datastore.delete(pinKey, options)
152
-
153
- const walker = (options.walker ?? depthFirstWalker())({
154
- blockstore: this.blockstore,
155
- getCodec: this.getCodec
156
- })
157
-
158
- for await (const node of walker.walk(cid, { ...options, depth: pin.depth })) {
159
- const childCid = node.block.cid
160
- await this.#updatePinnedBlock(childCid, (pinnedBlock): boolean => {
161
- pinnedBlock.pinCount--
162
- pinnedBlock.pinnedBy = pinnedBlock.pinnedBy.filter(c => uint8ArrayEquals(c, cid.bytes))
163
- return true
164
- }, {
165
- ...options,
166
- depth: pin.depth
167
- })
168
-
169
- yield childCid
170
- }
171
- }
172
-
173
- async * ls (options: LsOptions = {}): AsyncGenerator<Pin, void, undefined> {
174
- for await (const { key, value } of this.datastore.query({
175
- prefix: DATASTORE_PIN_PREFIX + (options.cid != null ? `${options.cid.toString(base36)}` : '')
176
- }, options)) {
177
- const cid = CID.parse(key.toString().substring(5), base36)
178
- const pin = cborg.decode(value)
179
-
180
- yield {
181
- cid,
182
- ...pin
183
- }
184
- }
185
- }
186
-
187
- async isPinned (cid: CID, options: IsPinnedOptions = {}): Promise<boolean> {
188
- const blockKey = new Key(`${DATASTORE_BLOCK_PREFIX}${DATASTORE_ENCODING.encode(cid.multihash.bytes)}`)
189
-
190
- return this.datastore.has(blockKey, options)
191
- }
192
-
193
- async get (cid: CID, options?: AbortOptions): Promise<Pin> {
194
- const pinKey = toDSKey(cid)
195
- const buf = await this.datastore.get(pinKey, options)
196
-
197
- return cborg.decode(buf)
198
- }
199
-
200
- async setMetadata (cid: CID, metadata: Record<string, string | number | boolean> | undefined, options?: AbortOptions): Promise<void> {
201
- const pinKey = toDSKey(cid)
202
- const buf = await this.datastore.get(pinKey, options)
203
- const pin: DatastorePin = cborg.decode(buf)
204
-
205
- pin.metadata = metadata ?? {}
206
-
207
- await this.datastore.put(pinKey, cborg.encode(pin), options)
208
- }
209
- }