-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathindex.ts
184 lines (159 loc) · 5.54 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* @packageDocumentation
*
* Create a Helia node.
*
* @example
*
* ```typescript
* import { MemoryDatastore } from 'datastore-core'
* import { MemoryBlockstore } from 'blockstore-core'
* import { createHelia } from 'helia'
* import { unixfs } from '@helia/unixfs'
* import { CID } from 'multiformats/cid'
*
* const node = await createHelia({
* blockstore: new MemoryBlockstore(),
* datastore: new MemoryDatastore()
* })
* const fs = unixfs(node)
* fs.cat(CID.parse('bafyFoo'))
* ```
*/
import { logger } from '@libp2p/logger'
import { MemoryBlockstore } from 'blockstore-core'
import { MemoryDatastore } from 'datastore-core'
import { HeliaImpl } from './helia.js'
import { createLibp2p } from './utils/libp2p.js'
import { name, version } from './version.js'
import type { DefaultLibp2pServices } from './utils/libp2p-defaults.js'
import type { Helia } from '@helia/interface'
import type { BlockBroker } from '@helia/interface/blocks'
import type { Libp2p } from '@libp2p/interface'
import type { Blockstore } from 'interface-blockstore'
import type { Datastore } from 'interface-datastore'
import type { Libp2pOptions } from 'libp2p'
import type { CID } from 'multiformats/cid'
import type { MultihashHasher } from 'multiformats/hashes/interface'
// re-export interface types so people don't have to depend on @helia/interface
// if they don't want to
export * from '@helia/interface'
export * from '@helia/interface/blocks'
export * from '@helia/interface/pins'
const log = logger('helia')
/**
* DAGWalkers take a block and yield CIDs encoded in that block
*/
export interface DAGWalker {
codec: number
walk(block: Uint8Array): AsyncGenerator<CID, void, undefined>
}
/**
* Options used to create a Helia node.
*/
export interface HeliaInit<T extends Libp2p = Libp2p> {
/**
* A libp2p node is required to perform network operations. Either a
* preconfigured node or options to configure a node can be passed
* here.
*
* If node options are passed, they will be merged with the default
* config for the current platform. In this case all passed config
* keys will replace those from the default config.
*/
libp2p?: T | Libp2pOptions
/**
* The blockstore is where blocks are stored
*/
blockstore?: Blockstore
/**
* The datastore is where data is stored
*/
datastore?: Datastore
/**
* By default sha256, sha512 and identity hashes are supported for
* bitswap operations. To bitswap blocks with CIDs using other hashes
* pass appropriate MultihashHashers here.
*/
hashers?: MultihashHasher[]
/**
* In order to pin CIDs that correspond to a DAG, it's necessary to know
* how to traverse that DAG. DAGWalkers take a block and yield any CIDs
* encoded within that block.
*/
dagWalkers?: DAGWalker[]
/**
* A list of strategies used to fetch blocks when they are not present in
* the local blockstore
*/
blockBrokers?: Array<(components: any) => BlockBroker>
/**
* Pass `false` to not start the Helia node
*/
start?: boolean
/**
* Garbage collection requires preventing blockstore writes during searches
* for unpinned blocks as DAGs are typically pinned after they've been
* imported - without locking this could lead to the deletion of blocks while
* they are being added to the blockstore.
*
* By default this lock is held on the main process (e.g. node cluster's
* primary process, the renderer thread in browsers) and other processes will
* contact the main process for access (worker processes in node cluster,
* webworkers in the browser).
*
* If Helia is being run wholly in a non-primary process, with no other process
* expected to access the blockstore (e.g. being run in the background in a
* webworker), pass true here to hold the gc lock in this process.
*/
holdGcLock?: boolean
}
/**
* Create and return a Helia node
*/
export async function createHelia <T extends Libp2p> (init: HeliaInit<T>): Promise<Helia<T>>
export async function createHelia (init?: HeliaInit<Libp2p<DefaultLibp2pServices>>): Promise<Helia<Libp2p<DefaultLibp2pServices>>>
export async function createHelia (init: HeliaInit = {}): Promise<Helia<unknown>> {
const datastore = init.datastore ?? new MemoryDatastore()
const blockstore = init.blockstore ?? new MemoryBlockstore()
let libp2p: Libp2p
if (isLibp2p(init.libp2p)) {
libp2p = init.libp2p
} else {
libp2p = await createLibp2p(datastore, init.libp2p)
}
const helia = new HeliaImpl({
...init,
datastore,
blockstore,
libp2p
})
if (init.start !== false) {
await helia.start()
}
// add helia to agent version
addHeliaToAgentVersion(helia)
return helia
}
function isLibp2p (obj: any): obj is Libp2p {
if (obj == null) {
return false
}
// a non-exhaustive list of methods found on the libp2p object
const funcs = ['dial', 'dialProtocol', 'hangUp', 'handle', 'unhandle', 'getMultiaddrs', 'getProtocols']
// if these are all functions it's probably a libp2p object
return funcs.every(m => typeof obj[m] === 'function')
}
function addHeliaToAgentVersion (helia: Helia<any>): void {
// add helia to agent version
try {
const existingAgentVersion = helia.libp2p.services.identify.host.agentVersion
if (existingAgentVersion.match(/js-libp2p\/\d+\.\d+\.\d+\sUserAgent=/) == null) {
// the user changed the agent version
return
}
helia.libp2p.services.identify.host.agentVersion = `${name}/${version} ${helia.libp2p.services.identify.host.agentVersion}`
} catch (err) {
log.error('could not add Helia to agent version', err)
}
}