-
Notifications
You must be signed in to change notification settings - Fork 436
/
index.ts
174 lines (152 loc) · 4.93 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
/**
* @packageDocumentation
*
* Use the `createLibp2p` function to create a libp2p node.
*
* @example
*
* ```typescript
* import { createLibp2p } from 'libp2p'
*
* const node = await createLibp2p({
* // ...other options
* })
* ```
*/
import { createLibp2pNode } from './libp2p.js'
import type { AddressManagerInit } from './address-manager/index.js'
import type { Components } from './components.js'
import type { ConnectionManagerInit } from './connection-manager/index.js'
import type { TransportManagerInit } from './transport-manager.js'
import type { Libp2p, ServiceMap, RecursivePartial, ComponentLogger, NodeInfo, ConnectionProtector, ConnectionEncrypter, ConnectionGater, ContentRouting, Metrics, PeerDiscovery, PeerId, PeerRouting, StreamMuxerFactory, Transport, PrivateKey } from '@libp2p/interface'
import type { PersistentPeerStoreInit } from '@libp2p/peer-store'
import type { DNS } from '@multiformats/dns'
import type { Datastore } from 'interface-datastore'
export type ServiceFactoryMap<T extends Record<string, unknown> = Record<string, unknown>> = {
[Property in keyof T]: (components: Components) => T[Property]
}
/**
* For Libp2p configurations and modules details read the [Configuration Document](https://github.com/libp2p/js-libp2p/tree/main/doc/CONFIGURATION.md).
*/
export interface Libp2pInit<T extends ServiceMap = { x: Record<string, unknown> }> {
/**
* peerId instance (it will be created if not provided)
*/
peerId: PeerId
/**
* Private key associated with the peerId
*/
privateKey: PrivateKey
/**
* Metadata about the node - implementation name, version number, etc
*/
nodeInfo: NodeInfo
/**
* Addresses for transport listening and to advertise to the network
*/
addresses: AddressManagerInit
/**
* libp2p Connection Manager configuration
*/
connectionManager: ConnectionManagerInit
/**
* A connection gater can deny new connections based on user criteria
*/
connectionGater: ConnectionGater
/**
* libp2p transport manager configuration
*/
transportManager: TransportManagerInit
/**
* An optional datastore to persist peer information, DHT records, etc.
*
* An in-memory datastore will be used if one is not provided.
*/
datastore: Datastore
/**
* libp2p PeerStore configuration
*/
peerStore: PersistentPeerStoreInit
/**
* An array that must include at least 1 compliant transport
*/
transports?: Array<(components: Components) => Transport>
streamMuxers?: Array<(components: Components) => StreamMuxerFactory>
connectionEncryption?: Array<(components: Components) => ConnectionEncrypter>
peerDiscovery?: Array<(components: Components) => PeerDiscovery>
peerRouters?: Array<(components: Components) => PeerRouting>
contentRouters?: Array<(components: Components) => ContentRouting>
/**
* A Metrics implementation can be supplied to collect metrics on this node
*/
metrics?(components: Components): Metrics
/**
* A ConnectionProtector can be used to create a secure overlay on top of the network using pre-shared keys
*/
connectionProtector?(components: Components): ConnectionProtector
/**
* Arbitrary libp2p modules
*/
services: ServiceFactoryMap<T>
/**
* An optional logging implementation that can be used to write runtime logs.
*
* Set the `DEBUG` env var or the `debug` key on LocalStorage to see logs.
*
* @example
*
* Node.js:
*
* ```console
* $ DEBUG="*libp2p:*" node myscript.js
* ```
*
* Browsers:
*
* ```TypeScript
* localStorage.setItem('debug', '*libp2p:*')
* ```
*/
logger?: ComponentLogger
/**
* An optional DNS resolver configuration. If omitted the default DNS resolver
* for the platform will be used which means `node:dns` on Node.js and
* DNS-JSON-over-HTTPS for browsers using Google and Cloudflare servers.
*/
dns?: DNS
}
export type { Libp2p }
export type Libp2pOptions<T extends ServiceMap = Record<string, unknown>> = RecursivePartial<Libp2pInit<T>> & { start?: boolean }
/**
* Returns a new instance of the Libp2p interface, generating a new PeerId
* if one is not passed as part of the options.
*
* The node will be started unless `start: false` is passed as an option.
*
* @example
*
* ```TypeScript
* import { createLibp2p } from 'libp2p'
* import { tcp } from '@libp2p/tcp'
* import { mplex } from '@libp2p/mplex'
* import { noise } from '@chainsafe/libp2p-noise'
* import { yamux } from '@chainsafe/libp2p-yamux'
*
* // specify options
* const options = {
* transports: [tcp()],
* streamMuxers: [yamux(), mplex()],
* connectionEncryption: [noise()]
* }
*
* // create libp2p
* const libp2p = await createLibp2p(options)
* ```
*/
export async function createLibp2p <T extends ServiceMap = { x: Record<string, unknown> }> (options: Libp2pOptions<T> = {}): Promise<Libp2p<T>> {
const node = await createLibp2pNode(options)
if (options.start !== false) {
await node.start()
}
return node
}