@helia/utils 2.5.2 → 3.0.0-3a1dd561
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/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/dist/typedoc-urls.json +0 -19
- 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
package/src/routing.ts
DELETED
|
@@ -1,376 +0,0 @@
|
|
|
1
|
-
import { NoRoutersAvailableError } from '@helia/interface'
|
|
2
|
-
import { NotFoundError, start, stop } from '@libp2p/interface'
|
|
3
|
-
import { PeerQueue } from '@libp2p/utils'
|
|
4
|
-
import merge from 'it-merge'
|
|
5
|
-
import { CustomProgressEvent } from 'progress-events'
|
|
6
|
-
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
7
|
-
import { GetFailedError } from './errors.ts'
|
|
8
|
-
import type { Routing as RoutingInterface, Provider, RoutingOptions, RoutingFindProvidersProgressEvents, RoutingProvideProgressEvents, RoutingPutProgressEvents, RoutingGetProgressEvents, RoutingFindPeerProgressEvents, RoutingGetClosestPeersProgressEvents, RoutingCancelReprovideProgressEvents } from '@helia/interface'
|
|
9
|
-
import type { ComponentLogger, Logger, Metrics, PeerId, PeerInfo, Startable } from '@libp2p/interface'
|
|
10
|
-
import type { CID } from 'multiformats/cid'
|
|
11
|
-
|
|
12
|
-
const DEFAULT_PROVIDER_LOOKUP_CONCURRENCY = 5
|
|
13
|
-
|
|
14
|
-
export interface RoutingInit {
|
|
15
|
-
routers: Array<Partial<RoutingInterface>>
|
|
16
|
-
providerLookupConcurrency?: number
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export interface RoutingComponents {
|
|
20
|
-
logger: ComponentLogger
|
|
21
|
-
metrics?: Metrics
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export class Routing implements RoutingInterface, Startable {
|
|
25
|
-
public name: string
|
|
26
|
-
|
|
27
|
-
private readonly log: Logger
|
|
28
|
-
private readonly routers: Array<Partial<RoutingInterface>>
|
|
29
|
-
private readonly providerLookupConcurrency: number
|
|
30
|
-
|
|
31
|
-
constructor (components: RoutingComponents, init: RoutingInit) {
|
|
32
|
-
this.name = 'helia'
|
|
33
|
-
this.log = components.logger.forComponent('helia:routing')
|
|
34
|
-
this.routers = init.routers ?? []
|
|
35
|
-
this.providerLookupConcurrency = init.providerLookupConcurrency ?? DEFAULT_PROVIDER_LOOKUP_CONCURRENCY
|
|
36
|
-
|
|
37
|
-
this.findProviders = components.metrics?.traceFunction('helia.routing.findProviders', this.findProviders.bind(this), {
|
|
38
|
-
optionsIndex: 1
|
|
39
|
-
}) ?? this.findProviders
|
|
40
|
-
this.provide = components.metrics?.traceFunction('helia.routing.provide', this.provide.bind(this), {
|
|
41
|
-
optionsIndex: 1
|
|
42
|
-
}) ?? this.provide
|
|
43
|
-
this.cancelReprovide = components.metrics?.traceFunction('helia.routing.cancelReprovide', this.cancelReprovide.bind(this), {
|
|
44
|
-
optionsIndex: 1
|
|
45
|
-
}) ?? this.cancelReprovide
|
|
46
|
-
this.put = components.metrics?.traceFunction('helia.routing.put', this.put.bind(this), {
|
|
47
|
-
optionsIndex: 2
|
|
48
|
-
}) ?? this.put
|
|
49
|
-
this.get = components.metrics?.traceFunction('helia.routing.get', this.get.bind(this), {
|
|
50
|
-
optionsIndex: 1
|
|
51
|
-
}) ?? this.get
|
|
52
|
-
this.findPeer = components.metrics?.traceFunction('helia.routing.findPeer', this.findPeer.bind(this), {
|
|
53
|
-
optionsIndex: 1
|
|
54
|
-
}) ?? this.findPeer
|
|
55
|
-
this.getClosestPeers = components.metrics?.traceFunction('helia.routing.getClosestPeers', this.getClosestPeers.bind(this), {
|
|
56
|
-
optionsIndex: 1
|
|
57
|
-
}) ?? this.getClosestPeers
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
async start (): Promise<void> {
|
|
61
|
-
await start(...this.routers)
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
async stop (): Promise<void> {
|
|
65
|
-
await stop(...this.routers)
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Iterates over all content routers in parallel to find providers of the
|
|
70
|
-
* given key
|
|
71
|
-
*/
|
|
72
|
-
async * findProviders (key: CID, options: RoutingOptions<RoutingFindProvidersProgressEvents> = {}): AsyncIterable<Provider> {
|
|
73
|
-
if (this.routers.length === 0) {
|
|
74
|
-
throw new NoRoutersAvailableError('No content routers available')
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// provider multiaddrs are only cached for a limited time, so they can come
|
|
78
|
-
// back as an empty array - when this happens we have to do a FIND_PEER
|
|
79
|
-
// query to get updated addresses, but we shouldn't block on this so use a
|
|
80
|
-
// separate bounded queue to perform this lookup
|
|
81
|
-
const queue = new PeerQueue<Provider | null>({
|
|
82
|
-
concurrency: this.providerLookupConcurrency
|
|
83
|
-
})
|
|
84
|
-
|
|
85
|
-
let foundProviders = 0
|
|
86
|
-
const errors: Error[] = []
|
|
87
|
-
const self = this
|
|
88
|
-
let routersFinished = 0
|
|
89
|
-
|
|
90
|
-
this.log('findProviders for %c start using routers %s', key, this.routers.map(r => r.toString()).join(', '))
|
|
91
|
-
|
|
92
|
-
const routers = supports(this.routers, 'findProviders')
|
|
93
|
-
.map(async function * (router) {
|
|
94
|
-
let foundProviders = 0
|
|
95
|
-
|
|
96
|
-
options?.onProgress?.(new CustomProgressEvent('helia:routing:find-providers:start', {
|
|
97
|
-
routing: router.name,
|
|
98
|
-
cid: key
|
|
99
|
-
}))
|
|
100
|
-
|
|
101
|
-
try {
|
|
102
|
-
for await (const prov of router.findProviders(key, options)) {
|
|
103
|
-
foundProviders++
|
|
104
|
-
|
|
105
|
-
// @ts-expect-error router.name is a string, needs to be specific
|
|
106
|
-
options?.onProgress?.(new CustomProgressEvent('helia:routing:find-providers:provider', {
|
|
107
|
-
routing: router.name,
|
|
108
|
-
cid: key,
|
|
109
|
-
provider: prov
|
|
110
|
-
}))
|
|
111
|
-
|
|
112
|
-
yield prov
|
|
113
|
-
}
|
|
114
|
-
} catch (err: any) {
|
|
115
|
-
errors.push(err)
|
|
116
|
-
} finally {
|
|
117
|
-
self.log('router %s found %d providers for %c', router, foundProviders, key)
|
|
118
|
-
|
|
119
|
-
options?.onProgress?.(new CustomProgressEvent('helia:routing:find-providers:end', {
|
|
120
|
-
routing: router.name,
|
|
121
|
-
cid: key,
|
|
122
|
-
found: foundProviders
|
|
123
|
-
}))
|
|
124
|
-
|
|
125
|
-
routersFinished++
|
|
126
|
-
|
|
127
|
-
// if all routers have finished and there are no jobs to find updated
|
|
128
|
-
// peer multiaddres running or queued, cause the generator to exit
|
|
129
|
-
if (routersFinished === routers.length && queue.size === 0) {
|
|
130
|
-
queue.emitIdle()
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
})
|
|
134
|
-
|
|
135
|
-
for await (const peer of merge(
|
|
136
|
-
queue.toGenerator(),
|
|
137
|
-
...routers)
|
|
138
|
-
) {
|
|
139
|
-
// the peer was yielded by a content router without multiaddrs and we
|
|
140
|
-
// failed to load them
|
|
141
|
-
if (peer == null) {
|
|
142
|
-
continue
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
// have to refresh peer info for this peer to get updated multiaddrs
|
|
146
|
-
if (peer.multiaddrs.length === 0) {
|
|
147
|
-
// already looking this peer up
|
|
148
|
-
if (queue.find(peer.id) != null) {
|
|
149
|
-
continue
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
queue.add(async () => {
|
|
153
|
-
try {
|
|
154
|
-
const provider = await this.findPeer(peer.id, options)
|
|
155
|
-
|
|
156
|
-
if (provider.multiaddrs.length === 0) {
|
|
157
|
-
return null
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
return {
|
|
161
|
-
...provider,
|
|
162
|
-
protocols: peer.protocols,
|
|
163
|
-
routing: peer.routing
|
|
164
|
-
}
|
|
165
|
-
} catch (err) {
|
|
166
|
-
this.log.error('could not load multiaddrs for peer %p - %e', peer.id, err)
|
|
167
|
-
return null
|
|
168
|
-
}
|
|
169
|
-
}, {
|
|
170
|
-
peerId: peer.id,
|
|
171
|
-
signal: options.signal
|
|
172
|
-
})
|
|
173
|
-
.catch(err => {
|
|
174
|
-
this.log.error('could not load multiaddrs for peer %p - %e', peer.id, err)
|
|
175
|
-
})
|
|
176
|
-
|
|
177
|
-
continue
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
foundProviders++
|
|
181
|
-
yield peer
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
this.log('findProviders finished, found %d providers for %c', foundProviders, key)
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* Iterates over all content routers in parallel to notify it is
|
|
189
|
-
* a provider of the given key
|
|
190
|
-
*/
|
|
191
|
-
async provide (key: CID, options: RoutingOptions<RoutingProvideProgressEvents> = {}): Promise<void> {
|
|
192
|
-
if (this.routers.length === 0) {
|
|
193
|
-
throw new NoRoutersAvailableError('No content routers available')
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
await Promise.all(
|
|
197
|
-
supports(this.routers, 'provide')
|
|
198
|
-
.map(async (router) => {
|
|
199
|
-
options?.onProgress?.(new CustomProgressEvent('helia:routing:provide:start', {
|
|
200
|
-
routing: router.name,
|
|
201
|
-
cid: key
|
|
202
|
-
}))
|
|
203
|
-
|
|
204
|
-
await router.provide(key, options)
|
|
205
|
-
|
|
206
|
-
options?.onProgress?.(new CustomProgressEvent('helia:routing:provide:end', {
|
|
207
|
-
routing: router.name,
|
|
208
|
-
cid: key
|
|
209
|
-
}))
|
|
210
|
-
})
|
|
211
|
-
)
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
async cancelReprovide (key: CID, options: RoutingOptions<RoutingCancelReprovideProgressEvents> = {}): Promise<void> {
|
|
215
|
-
await Promise.all(
|
|
216
|
-
supports(this.routers, 'cancelReprovide')
|
|
217
|
-
.map(async (router) => {
|
|
218
|
-
options?.onProgress?.(new CustomProgressEvent('helia:routing:cancel-reprovide:start', {
|
|
219
|
-
routing: router.name,
|
|
220
|
-
cid: key
|
|
221
|
-
}))
|
|
222
|
-
|
|
223
|
-
await router.cancelReprovide(key, options)
|
|
224
|
-
|
|
225
|
-
options?.onProgress?.(new CustomProgressEvent('helia:routing:cancel-reprovide:end', {
|
|
226
|
-
routing: router.name,
|
|
227
|
-
cid: key
|
|
228
|
-
}))
|
|
229
|
-
})
|
|
230
|
-
)
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* Store the given key/value pair in the available content routings
|
|
235
|
-
*/
|
|
236
|
-
async put (key: Uint8Array, value: Uint8Array, options?: RoutingOptions<RoutingPutProgressEvents>): Promise<void> {
|
|
237
|
-
await Promise.all(
|
|
238
|
-
supports(this.routers, 'put')
|
|
239
|
-
.map(async (router) => {
|
|
240
|
-
options?.onProgress?.(new CustomProgressEvent('helia:routing:put:start', {
|
|
241
|
-
routing: router.name,
|
|
242
|
-
key,
|
|
243
|
-
value
|
|
244
|
-
}))
|
|
245
|
-
|
|
246
|
-
await router.put(key, value, options)
|
|
247
|
-
|
|
248
|
-
options?.onProgress?.(new CustomProgressEvent('helia:routing:put:end', {
|
|
249
|
-
routing: router.name,
|
|
250
|
-
key,
|
|
251
|
-
value
|
|
252
|
-
}))
|
|
253
|
-
})
|
|
254
|
-
)
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
/**
|
|
258
|
-
* Get the value to the given key. The first value offered by any configured
|
|
259
|
-
* router will be returned.
|
|
260
|
-
*/
|
|
261
|
-
async get (key: Uint8Array, options?: RoutingOptions<RoutingGetProgressEvents>): Promise<Uint8Array> {
|
|
262
|
-
const errors: Error[] = []
|
|
263
|
-
let result: Uint8Array | undefined
|
|
264
|
-
|
|
265
|
-
try {
|
|
266
|
-
result = await Promise.any(
|
|
267
|
-
supports(this.routers, 'get')
|
|
268
|
-
.map(async (router) => {
|
|
269
|
-
options?.onProgress?.(new CustomProgressEvent('helia:routing:get:start', {
|
|
270
|
-
routing: router.name,
|
|
271
|
-
key
|
|
272
|
-
}))
|
|
273
|
-
|
|
274
|
-
try {
|
|
275
|
-
return await router.get(key, options)
|
|
276
|
-
} catch (err: any) {
|
|
277
|
-
this.log('router %s failed with %e', router, err)
|
|
278
|
-
errors.push(err)
|
|
279
|
-
} finally {
|
|
280
|
-
options?.onProgress?.(new CustomProgressEvent('helia:routing:get:end', {
|
|
281
|
-
routing: router.name,
|
|
282
|
-
key
|
|
283
|
-
}))
|
|
284
|
-
}
|
|
285
|
-
})
|
|
286
|
-
)
|
|
287
|
-
} catch {
|
|
288
|
-
// ignore AggregateError as we will throw a better-named one
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
if (result == null) {
|
|
292
|
-
throw new GetFailedError(errors, `Failed to get value key ${uint8ArrayToString(key, 'base58btc')}`)
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
return result
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
/**
|
|
299
|
-
* Iterates over all peer routers in parallel to find the given peer
|
|
300
|
-
*/
|
|
301
|
-
async findPeer (id: PeerId, options?: RoutingOptions<RoutingFindPeerProgressEvents>): Promise<PeerInfo> {
|
|
302
|
-
if (this.routers.length === 0) {
|
|
303
|
-
throw new NoRoutersAvailableError('No peer routers available')
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
const self = this
|
|
307
|
-
const source = merge(
|
|
308
|
-
...supports(this.routers, 'findPeer')
|
|
309
|
-
.map(router => (async function * () {
|
|
310
|
-
options?.onProgress?.(new CustomProgressEvent('helia:routing:find-peer:start', {
|
|
311
|
-
routing: router.name,
|
|
312
|
-
peerId: id
|
|
313
|
-
}))
|
|
314
|
-
|
|
315
|
-
try {
|
|
316
|
-
yield await router.findPeer(id, options)
|
|
317
|
-
} catch (err) {
|
|
318
|
-
self.log.error(err)
|
|
319
|
-
} finally {
|
|
320
|
-
options?.onProgress?.(new CustomProgressEvent('helia:routing:find-peer:end', {
|
|
321
|
-
routing: router.name,
|
|
322
|
-
peerId: id
|
|
323
|
-
}))
|
|
324
|
-
}
|
|
325
|
-
})())
|
|
326
|
-
)
|
|
327
|
-
|
|
328
|
-
for await (const peer of source) {
|
|
329
|
-
if (peer == null) {
|
|
330
|
-
continue
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
return peer
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
throw new NotFoundError('Could not find peer in routing')
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
/**
|
|
340
|
-
* Attempt to find the closest peers on the network to the given key
|
|
341
|
-
*/
|
|
342
|
-
async * getClosestPeers (key: Uint8Array, options: RoutingOptions<RoutingGetClosestPeersProgressEvents> = {}): AsyncIterable<PeerInfo> {
|
|
343
|
-
if (this.routers.length === 0) {
|
|
344
|
-
throw new NoRoutersAvailableError('No peer routers available')
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
for await (const peer of merge(
|
|
348
|
-
...supports(this.routers, 'getClosestPeers')
|
|
349
|
-
.map(async function * (router) {
|
|
350
|
-
options?.onProgress?.(new CustomProgressEvent('helia:routing:get-closest-peers:start', {
|
|
351
|
-
routing: router.name,
|
|
352
|
-
key
|
|
353
|
-
}))
|
|
354
|
-
|
|
355
|
-
try {
|
|
356
|
-
yield * router.getClosestPeers(key, options)
|
|
357
|
-
} finally {
|
|
358
|
-
options?.onProgress?.(new CustomProgressEvent('helia:routing:get-closest-peers:end', {
|
|
359
|
-
routing: router.name,
|
|
360
|
-
key
|
|
361
|
-
}))
|
|
362
|
-
}
|
|
363
|
-
})
|
|
364
|
-
)) {
|
|
365
|
-
if (peer == null) {
|
|
366
|
-
continue
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
yield peer
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
function supports <Operation extends keyof Routing> (routers: any[], key: Operation): Array<Pick<Routing, Operation | 'name'>> {
|
|
375
|
-
return routers.filter(router => router[key] != null)
|
|
376
|
-
}
|
package/src/storage.ts
DELETED
|
@@ -1,195 +0,0 @@
|
|
|
1
|
-
import { start, stop } from '@libp2p/interface'
|
|
2
|
-
import createMortice from 'mortice'
|
|
3
|
-
import { BlockPinnedError } from './errors.ts'
|
|
4
|
-
import type { Routing } from '@helia/interface'
|
|
5
|
-
import type { Blocks, Pair, DeleteManyBlocksProgressEvents, DeleteBlockProgressEvents, GetBlockProgressEvents, GetManyBlocksProgressEvents, PutManyBlocksProgressEvents, PutBlockProgressEvents, GetAllBlocksProgressEvents, GetOfflineOptions, SessionBlockstore } from '@helia/interface/blocks'
|
|
6
|
-
import type { Pins } from '@helia/interface/pins'
|
|
7
|
-
import type { AbortOptions, Startable } from '@libp2p/interface'
|
|
8
|
-
import type { Blockstore, InputPair } from 'interface-blockstore'
|
|
9
|
-
import type { AwaitIterable } from 'interface-store'
|
|
10
|
-
import type { Mortice } from 'mortice'
|
|
11
|
-
import type { CID } from 'multiformats/cid'
|
|
12
|
-
import type { ProgressOptions } from 'progress-events'
|
|
13
|
-
|
|
14
|
-
export interface BlockStorageInit {
|
|
15
|
-
holdGcLock?: boolean
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export interface GetOptions extends AbortOptions {
|
|
19
|
-
progress?(evt: Event): void
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* BlockStorage is a hybrid blockstore that puts/gets blocks from a configured
|
|
24
|
-
* blockstore (that may be on disk, s3, or something else). If the blocks are
|
|
25
|
-
* not present Bitswap will be used to fetch them from network peers.
|
|
26
|
-
*/
|
|
27
|
-
export class BlockStorage implements Blocks, Startable {
|
|
28
|
-
public lock: Mortice
|
|
29
|
-
private readonly child: Blocks
|
|
30
|
-
private readonly pins: Pins
|
|
31
|
-
private readonly routing: Routing
|
|
32
|
-
private started: boolean
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Create a new BlockStorage
|
|
36
|
-
*/
|
|
37
|
-
constructor (blockstore: Blocks, pins: Pins, routing: Routing, options: BlockStorageInit = {}) {
|
|
38
|
-
this.child = blockstore
|
|
39
|
-
this.pins = pins
|
|
40
|
-
this.routing = routing
|
|
41
|
-
this.lock = createMortice({
|
|
42
|
-
singleProcess: options.holdGcLock
|
|
43
|
-
})
|
|
44
|
-
this.started = false
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
isStarted (): boolean {
|
|
48
|
-
return this.started
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
async start (): Promise<void> {
|
|
52
|
-
await start(this.child)
|
|
53
|
-
this.started = true
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
async stop (): Promise<void> {
|
|
57
|
-
await stop(this.child)
|
|
58
|
-
this.started = false
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
unwrap (): Blockstore {
|
|
62
|
-
return this.child
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Put a block to the underlying datastore
|
|
67
|
-
*/
|
|
68
|
-
async put (cid: CID, block: Uint8Array, options: AbortOptions & ProgressOptions<PutBlockProgressEvents> = {}): Promise<CID> {
|
|
69
|
-
options?.signal?.throwIfAborted()
|
|
70
|
-
const releaseLock = await this.lock.readLock()
|
|
71
|
-
|
|
72
|
-
try {
|
|
73
|
-
return await this.child.put(cid, block, options)
|
|
74
|
-
} finally {
|
|
75
|
-
releaseLock()
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Put a multiple blocks to the underlying datastore
|
|
81
|
-
*/
|
|
82
|
-
async * putMany (blocks: AwaitIterable<InputPair>, options: AbortOptions & ProgressOptions<PutManyBlocksProgressEvents> = {}): AsyncGenerator<CID> {
|
|
83
|
-
options?.signal?.throwIfAborted()
|
|
84
|
-
const releaseLock = await this.lock.readLock()
|
|
85
|
-
|
|
86
|
-
try {
|
|
87
|
-
yield * this.child.putMany(blocks, options)
|
|
88
|
-
} finally {
|
|
89
|
-
releaseLock()
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Get a block by cid
|
|
95
|
-
*/
|
|
96
|
-
async * get (cid: CID, options: GetOfflineOptions & AbortOptions & ProgressOptions<GetBlockProgressEvents> = {}): AsyncGenerator<Uint8Array> {
|
|
97
|
-
options?.signal?.throwIfAborted()
|
|
98
|
-
const releaseLock = await this.lock.readLock()
|
|
99
|
-
|
|
100
|
-
try {
|
|
101
|
-
yield * this.child.get(cid, options)
|
|
102
|
-
} finally {
|
|
103
|
-
releaseLock()
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Get multiple blocks back from an (async) iterable of cids
|
|
109
|
-
*/
|
|
110
|
-
async * getMany (cids: AwaitIterable<CID>, options: GetOfflineOptions & AbortOptions & ProgressOptions<GetManyBlocksProgressEvents> = {}): AsyncGenerator<Pair> {
|
|
111
|
-
options?.signal?.throwIfAborted()
|
|
112
|
-
const releaseLock = await this.lock.readLock()
|
|
113
|
-
|
|
114
|
-
try {
|
|
115
|
-
yield * this.child.getMany(cids, options)
|
|
116
|
-
} finally {
|
|
117
|
-
releaseLock()
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Delete a block from the blockstore
|
|
123
|
-
*/
|
|
124
|
-
async delete (cid: CID, options: AbortOptions & ProgressOptions<DeleteBlockProgressEvents> = {}): Promise<void> {
|
|
125
|
-
options?.signal?.throwIfAborted()
|
|
126
|
-
const releaseLock = await this.lock.writeLock()
|
|
127
|
-
|
|
128
|
-
try {
|
|
129
|
-
if (await this.pins.isPinned(cid)) {
|
|
130
|
-
throw new BlockPinnedError('Block was pinned - please unpin and try again')
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// stop re-providing this CID if necessary
|
|
134
|
-
await this.routing.cancelReprovide(cid, options)
|
|
135
|
-
|
|
136
|
-
await this.child.delete(cid, options)
|
|
137
|
-
} finally {
|
|
138
|
-
releaseLock()
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* Delete multiple blocks from the blockstore
|
|
144
|
-
*/
|
|
145
|
-
async * deleteMany (cids: AwaitIterable<CID>, options: AbortOptions & ProgressOptions<DeleteManyBlocksProgressEvents> = {}): AsyncGenerator<CID> {
|
|
146
|
-
options?.signal?.throwIfAborted()
|
|
147
|
-
const releaseLock = await this.lock.writeLock()
|
|
148
|
-
|
|
149
|
-
try {
|
|
150
|
-
const storage = this
|
|
151
|
-
|
|
152
|
-
yield * this.child.deleteMany((async function * (): AsyncGenerator<CID> {
|
|
153
|
-
for await (const cid of cids) {
|
|
154
|
-
if (await storage.pins.isPinned(cid)) {
|
|
155
|
-
throw new BlockPinnedError('Block was pinned - please unpin and try again')
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
// stop re-providing this CID if necessary
|
|
159
|
-
await storage.routing.cancelReprovide(cid, options)
|
|
160
|
-
|
|
161
|
-
yield cid
|
|
162
|
-
}
|
|
163
|
-
}()), options)
|
|
164
|
-
} finally {
|
|
165
|
-
releaseLock()
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
async has (cid: CID, options: AbortOptions = {}): Promise<boolean> {
|
|
170
|
-
options?.signal?.throwIfAborted()
|
|
171
|
-
const releaseLock = await this.lock.readLock()
|
|
172
|
-
|
|
173
|
-
try {
|
|
174
|
-
return await this.child.has(cid, options)
|
|
175
|
-
} finally {
|
|
176
|
-
releaseLock()
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
async * getAll (options: AbortOptions & ProgressOptions<GetAllBlocksProgressEvents> = {}): AsyncGenerator<Pair> {
|
|
181
|
-
options?.signal?.throwIfAborted()
|
|
182
|
-
const releaseLock = await this.lock.readLock()
|
|
183
|
-
|
|
184
|
-
try {
|
|
185
|
-
yield * this.child.getAll(options)
|
|
186
|
-
} finally {
|
|
187
|
-
releaseLock()
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
createSession (root: CID, options?: AbortOptions): SessionBlockstore {
|
|
192
|
-
options?.signal?.throwIfAborted()
|
|
193
|
-
return this.child.createSession(root, options)
|
|
194
|
-
}
|
|
195
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { Key } from 'interface-datastore'
|
|
2
|
-
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
3
|
-
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
4
|
-
import { InvalidDatastoreVersionError } from '../errors.ts'
|
|
5
|
-
import type { Datastore } from 'interface-datastore'
|
|
6
|
-
|
|
7
|
-
const DS_VERSION_KEY = new Key('/version')
|
|
8
|
-
const CURRENT_VERSION = 1
|
|
9
|
-
|
|
10
|
-
export async function assertDatastoreVersionIsCurrent (datastore: Datastore): Promise<void> {
|
|
11
|
-
if (!(await datastore.has(DS_VERSION_KEY))) {
|
|
12
|
-
await datastore.put(DS_VERSION_KEY, uint8ArrayFromString(`${CURRENT_VERSION}`))
|
|
13
|
-
|
|
14
|
-
return
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const buf = await datastore.get(DS_VERSION_KEY)
|
|
18
|
-
const str = uint8ArrayToString(buf)
|
|
19
|
-
const version = parseInt(str, 10)
|
|
20
|
-
|
|
21
|
-
if (version !== CURRENT_VERSION) {
|
|
22
|
-
// TODO: write migrations when we break compatibility - for an example, see https://github.com/ipfs/js-ipfs-repo/tree/master/packages/ipfs-repo-migrations
|
|
23
|
-
throw new InvalidDatastoreVersionError('Invalid datastore version, a datastore migration may be required')
|
|
24
|
-
}
|
|
25
|
-
}
|
package/src/utils/get-codec.ts
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
/* eslint max-depth: ["error", 7] */
|
|
2
|
-
|
|
3
|
-
import { UnknownCodecError } from '@helia/interface'
|
|
4
|
-
import * as dagCbor from '@ipld/dag-cbor'
|
|
5
|
-
import * as dagJson from '@ipld/dag-json'
|
|
6
|
-
import * as dagPb from '@ipld/dag-pb'
|
|
7
|
-
import * as json from 'multiformats/codecs/json'
|
|
8
|
-
import * as raw from 'multiformats/codecs/raw'
|
|
9
|
-
import { isPromise } from './is-promise.ts'
|
|
10
|
-
import type { Await } from '@helia/interface'
|
|
11
|
-
import type { BlockCodec } from 'multiformats/codecs/interface'
|
|
12
|
-
|
|
13
|
-
export function getCodec <T = any, Code extends number = any> (initialCodecs: Array<BlockCodec<any, any>> = [], loadCodec?: (code: number) => Await<BlockCodec<any, any>>): (code: Code) => Await<BlockCodec<Code, T>> {
|
|
14
|
-
const codecs: Record<number, BlockCodec<any, any>> = {
|
|
15
|
-
[dagPb.code]: dagPb,
|
|
16
|
-
[raw.code]: raw,
|
|
17
|
-
[dagCbor.code]: dagCbor,
|
|
18
|
-
[dagJson.code]: dagJson,
|
|
19
|
-
[json.code]: json
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
initialCodecs.forEach(codec => {
|
|
23
|
-
codecs[codec.code] = codec
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
return async (code) => {
|
|
27
|
-
let codec = codecs[code]
|
|
28
|
-
|
|
29
|
-
if (codec == null && loadCodec != null) {
|
|
30
|
-
const res = loadCodec(code)
|
|
31
|
-
|
|
32
|
-
if (isPromise(res)) {
|
|
33
|
-
codec = await res
|
|
34
|
-
} else {
|
|
35
|
-
codec = res
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
codecs[codec.code] = codec
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
if (codec != null) {
|
|
42
|
-
return codec
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
throw new UnknownCodecError(`Could not load codec for ${code}`)
|
|
46
|
-
}
|
|
47
|
-
}
|
package/src/utils/get-hasher.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { UnknownHashAlgorithmError } from '@helia/interface'
|
|
2
|
-
import { identity } from 'multiformats/hashes/identity'
|
|
3
|
-
import { sha256, sha512 } from 'multiformats/hashes/sha2'
|
|
4
|
-
import { isPromise } from './is-promise.ts'
|
|
5
|
-
import type { Await } from '@helia/interface'
|
|
6
|
-
import type { MultihashHasher } from 'multiformats/hashes/interface'
|
|
7
|
-
|
|
8
|
-
export function getHasher (initialHashers: MultihashHasher[] = [], loadHasher?: (code: number) => Await<MultihashHasher>): (code: number) => Await<MultihashHasher> {
|
|
9
|
-
const hashers: Record<number, MultihashHasher> = {
|
|
10
|
-
[sha256.code]: sha256,
|
|
11
|
-
[sha512.code]: sha512,
|
|
12
|
-
[identity.code]: identity
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
initialHashers.forEach(hasher => {
|
|
16
|
-
hashers[hasher.code] = hasher
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
return async (code) => {
|
|
20
|
-
let hasher = hashers[code]
|
|
21
|
-
|
|
22
|
-
if (hasher == null && loadHasher != null) {
|
|
23
|
-
const res = loadHasher(code)
|
|
24
|
-
|
|
25
|
-
if (isPromise(res)) {
|
|
26
|
-
hasher = await res
|
|
27
|
-
} else {
|
|
28
|
-
hasher = res
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
hashers[hasher.code] = hasher
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
if (hasher != null) {
|
|
35
|
-
return hasher
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
throw new UnknownHashAlgorithmError(`No hasher configured for multihash code 0x${code.toString(16)}, please configure one. You can look up which hash this is at https://github.com/multiformats/multicodec/blob/master/table.csv`)
|
|
39
|
-
}
|
|
40
|
-
}
|