@helia/libp2p 2.1.4-d4962087 → 2.1.5-05d3e4cc

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/src/index.ts CHANGED
@@ -18,15 +18,18 @@
18
18
  *
19
19
  * By default `withLibp2p` configures a libp2p node for the current environment.
20
20
  *
21
- * Any options passed will be merged with the default config.
21
+ * Each option passed replaces the default for that key. Pass `config: 'merge'`
22
+ * to merge the options into the default config instead.
22
23
  *
23
- * For full control over your node (and the bundle size), use `withLibp2pLight`:
24
+ * For full control over your node (and the bundle size), use `withLibp2pLight`
25
+ * from `@helia/libp2p/light`. That entry point does not load the default
26
+ * config or any of its dependencies:
24
27
  *
25
28
  * ```ts
26
- * import { createHelia } from 'helia'
27
- * import { withLibp2pLight } from '@helia/libp2p'
29
+ * import { createHeliaLight } from 'helia/light'
30
+ * import { withLibp2pLight } from '@helia/libp2p/light'
28
31
  *
29
- * const node = await withLibp2pLight(createHelia(), {
32
+ * const node = await withLibp2pLight(createHeliaLight(), {
30
33
  * //... libp2p config here
31
34
  * }).start()
32
35
  *
@@ -34,43 +37,18 @@
34
37
  * ```
35
38
  */
36
39
 
37
- import { loadOrCreateSelfKey } from '@libp2p/config'
38
- import { NotStartedError } from '@libp2p/interface'
39
- import { peerIdFromCID } from '@libp2p/peer-id'
40
- import forEach from 'it-foreach'
41
- import { createLibp2p, isLibp2p } from 'libp2p'
42
- import { userAgent } from 'libp2p/user-agent'
43
- import { libp2pRouting } from './routing.ts'
40
+ import { libp2pRouting, withLibp2pLight } from './light.ts'
44
41
  import { libp2pDefaults } from './utils/libp2p-defaults.ts'
42
+ import type { CreateLibp2pOptions, HeliaWithLibp2p } from './light.ts'
45
43
  import type { DefaultLibp2pServices } from './utils/libp2p-defaults.ts'
46
- import type { Helia, HeliaMixin, Peer, Provider } from '@helia/interface'
47
- import type { Libp2p, PeerInfo, ServiceMap } from '@libp2p/interface'
48
- import type { KeychainInit } from '@libp2p/keychain'
49
- import type { Multiaddr } from '@multiformats/multiaddr'
50
- import type { Libp2pOptions } from 'libp2p'
51
- import type { CID } from 'multiformats'
44
+ import type { Helia } from '@helia/interface'
45
+ import type { ServiceMap } from '@libp2p/interface'
52
46
 
47
+ export { libp2pRouting, withLibp2pLight }
48
+ export type { CreateLibp2pOptions, HeliaWithLibp2p }
53
49
  export { libp2pDefaults } from './utils/libp2p-defaults.ts'
54
50
  export type { DefaultLibp2pServices } from './utils/libp2p-defaults.ts'
55
51
 
56
- export interface CreateLibp2pOptions<T extends ServiceMap = ServiceMap> extends Libp2pOptions<T> {
57
- /**
58
- * If 'merge', any passed config will be merged into the default set. If
59
- * 'replace' any passed config keys will replace those in the default set.
60
- *
61
- * @default 'replace'
62
- */
63
- config?: 'merge' | 'replace'
64
- keychain?: KeychainInit
65
- }
66
-
67
- export interface HeliaWithLibp2p<M extends ServiceMap = DefaultLibp2pServices> extends Helia {
68
- /**
69
- * Libp2p provides a peer identity and a networking layer
70
- */
71
- libp2p: Libp2p<M>
72
- }
73
-
74
52
  /**
75
53
  * Return a Helia node augmented with a libp2p instance
76
54
  */
@@ -79,118 +57,3 @@ export function withLibp2p <H extends Helia, M extends ServiceMap> (helia: H, op
79
57
  export function withLibp2p <H extends Helia> (helia: H, opts?: any): H & HeliaWithLibp2p<DefaultLibp2pServices> {
80
58
  return withLibp2pLight(helia, libp2pDefaults(opts))
81
59
  }
82
-
83
- /**
84
- * Return a Helia node augmented with a libp2p instance. The passed
85
- * configuration is passed to `createLibp2p` without adding any default config.
86
- *
87
- * Use this if you do not wish to have any extra config added.
88
- */
89
- export function withLibp2pLight <H extends Helia, M extends ServiceMap = ServiceMap> (helia: H, opts: CreateLibp2pOptions<M>): H & HeliaWithLibp2p<M> {
90
- let libp2p: Libp2p
91
-
92
- // add a getter that informs the user they need to start Helia
93
- Object.defineProperty(helia, 'libp2p', {
94
- configurable: true,
95
- enumerable: true,
96
- get () {
97
- if (libp2p != null) {
98
- return libp2p
99
- }
100
-
101
- throw new NotStartedError()
102
- }
103
- })
104
-
105
- const mixin: HeliaMixin<H & { libp2p?: Libp2p<M> }, H & { libp2p?: Libp2p<M> }> = {
106
- name: 'libp2p',
107
- start: async (helia) => {
108
- if (libp2p == null) {
109
- // if no peer id was passed, try to load it from the keychain
110
- if (opts.privateKey == null) {
111
- opts.privateKey = await loadOrCreateSelfKey(helia.datastore, opts.keychain)
112
- }
113
-
114
- // copy options from Helia node
115
- opts.nodeInfo ??= {}
116
- opts.nodeInfo.name ??= helia.info.name
117
- opts.nodeInfo.version ??= helia.info.version
118
- opts.nodeInfo.userAgent ??= `${helia.info.name}/${helia.info.version} ${userAgent()}`
119
-
120
- opts.datastore ??= helia.datastore
121
-
122
- libp2p = await createLibp2p(opts)
123
-
124
- // override peer discovery methods to ensure we persist peer data in the
125
- // peer store, otherwise we can't dial by peer id without extra lookups
126
- const findProviders = helia.routing.findProviders.bind(helia.routing)
127
- helia.routing.findProviders = async function * (cid, options): AsyncIterable<Provider> {
128
- yield * forEach(findProviders.call(helia.routing, cid, options), async (peer) => {
129
- if (peer.router !== 'libp2p-router') {
130
- // only need to do this for peers not found via the libp2p router
131
- const info = toPeerInfo(peer)
132
- await libp2p.peerStore.merge(info.id, info)
133
- }
134
- })
135
- }
136
-
137
- const findPeer = helia.routing.findPeer.bind(helia.routing)
138
- helia.routing.findPeer = async function (cid, options): Promise<Peer> {
139
- const peer = await findPeer(cid, options)
140
-
141
- if (peer.router !== 'libp2p-router') {
142
- // only need to do this for peers not found via the libp2p router
143
- const info = toPeerInfo(peer)
144
- await libp2p.peerStore.merge(info.id, info)
145
- }
146
-
147
- return peer
148
- }
149
-
150
- // override peer discovery methods to ensure we persist peer data in the
151
- // peer store, otherwise we can't dial by peer id without extra lookups
152
- const getClosestPeers = helia.routing.getClosestPeers.bind(helia.routing)
153
- helia.routing.getClosestPeers = async function * (cid, options): AsyncIterable<Peer> {
154
- yield * forEach(getClosestPeers.call(helia.routing, cid, options), async (peer) => {
155
- if (peer.router !== 'libp2p-router') {
156
- // only need to do this for peers not found via the libp2p router
157
- const info = toPeerInfo(peer)
158
- await libp2p.peerStore.merge(info.id, info)
159
- }
160
- })
161
- }
162
- }
163
-
164
- try {
165
- if (!helia.hasRouter('libp2p-router')) {
166
- helia.addRouter(libp2pRouting(libp2p))
167
- }
168
-
169
- if (isLibp2p(helia.libp2p)) {
170
- // already configured libp2p
171
- await helia.libp2p.start()
172
- }
173
- } catch (err: any) {
174
- if (err.name !== 'NotStartedError') {
175
- throw err
176
- }
177
- }
178
- },
179
-
180
- stop: async () => {
181
- await libp2p?.stop()
182
- }
183
- }
184
-
185
- helia.addMixin(mixin)
186
-
187
- // @ts-expect-error libp2p property is missing, even though we just defined it
188
- return helia
189
- }
190
-
191
- function toPeerInfo (peer: { id: CID, multiaddrs: Multiaddr[] }): PeerInfo {
192
- return {
193
- id: peerIdFromCID(peer.id),
194
- multiaddrs: peer.multiaddrs
195
- }
196
- }
package/src/light.ts ADDED
@@ -0,0 +1,188 @@
1
+ /**
2
+ * @packageDocumentation
3
+ *
4
+ * Exports `withLibp2pLight`, which adds a libp2p node to Helia using only the
5
+ * config you pass to it.
6
+ *
7
+ * Unlike the main `@helia/libp2p` entry point, importing
8
+ * `@helia/libp2p/light` does not load the default libp2p config, so none of
9
+ * the default transports, services or their dependencies (including the native
10
+ * `node-datachannel` module used by `@libp2p/webrtc`) are loaded.
11
+ *
12
+ * @example Bring your own libp2p config
13
+ *
14
+ * ```ts
15
+ * import { createHeliaLight } from 'helia/light'
16
+ * import { withLibp2pLight } from '@helia/libp2p/light'
17
+ * import { tcp } from '@libp2p/tcp'
18
+ * import { noise } from '@libp2p/noise'
19
+ * import { yamux } from '@libp2p/yamux'
20
+ *
21
+ * const node = await withLibp2pLight(createHeliaLight(), {
22
+ * transports: [tcp()],
23
+ * connectionEncrypters: [noise()],
24
+ * streamMuxers: [yamux()]
25
+ * }).start()
26
+ *
27
+ * console.info(node.libp2p.peerId) // 12D3Koo...
28
+ * ```
29
+ */
30
+
31
+ import { loadOrCreateSelfKey } from '@libp2p/config'
32
+ import { InvalidParametersError, NotStartedError } from '@libp2p/interface'
33
+ import { peerIdFromCID } from '@libp2p/peer-id'
34
+ import forEach from 'it-foreach'
35
+ import { createLibp2p, isLibp2p } from 'libp2p'
36
+ import { userAgent } from 'libp2p/user-agent'
37
+ import { libp2pRouting } from './routing.ts'
38
+ import type { DefaultLibp2pServices } from './utils/libp2p-defaults.ts'
39
+ import type { Helia, HeliaMixin, Peer, Provider } from '@helia/interface'
40
+ import type { Libp2p, PeerInfo, ServiceMap } from '@libp2p/interface'
41
+ import type { KeychainInit } from '@libp2p/keychain'
42
+ import type { Multiaddr } from '@multiformats/multiaddr'
43
+ import type { Libp2pOptions } from 'libp2p'
44
+ import type { CID } from 'multiformats'
45
+
46
+ export { libp2pRouting }
47
+
48
+ export interface CreateLibp2pOptions<T extends ServiceMap = ServiceMap> extends Libp2pOptions<T> {
49
+ /**
50
+ * How `withLibp2p` combines these options with its default config. With
51
+ * 'merge' they are merged into the defaults, with 'replace' each key that is
52
+ * passed replaces the default for that key. `withLibp2pLight` has no default
53
+ * config, so it ignores this option.
54
+ *
55
+ * @default 'replace'
56
+ */
57
+ config?: 'merge' | 'replace'
58
+ keychain?: KeychainInit
59
+ }
60
+
61
+ export interface HeliaWithLibp2p<M extends ServiceMap = DefaultLibp2pServices> extends Helia {
62
+ /**
63
+ * Libp2p provides a peer identity and a networking layer
64
+ */
65
+ libp2p: Libp2p<M>
66
+ }
67
+
68
+ /**
69
+ * Return a Helia node augmented with a libp2p instance. The passed
70
+ * configuration is passed to `createLibp2p` without adding any default config.
71
+ *
72
+ * Use this if you do not wish to have any extra config added. To use the
73
+ * default config, call `withLibp2p` from `@helia/libp2p` instead.
74
+ *
75
+ * Import it from `@helia/libp2p/light` to avoid loading the default config.
76
+ */
77
+ export function withLibp2pLight <H extends Helia, M extends ServiceMap = ServiceMap> (helia: H, opts: CreateLibp2pOptions<M>): H & HeliaWithLibp2p<M> {
78
+ if (opts == null) {
79
+ throw new InvalidParametersError('withLibp2pLight requires libp2p options. Use withLibp2p from @helia/libp2p for the default config')
80
+ }
81
+
82
+ let libp2p: Libp2p
83
+
84
+ // add a getter that informs the user they need to start Helia
85
+ Object.defineProperty(helia, 'libp2p', {
86
+ configurable: true,
87
+ enumerable: true,
88
+ get () {
89
+ if (libp2p != null) {
90
+ return libp2p
91
+ }
92
+
93
+ throw new NotStartedError()
94
+ }
95
+ })
96
+
97
+ const mixin: HeliaMixin<H & { libp2p?: Libp2p<M> }, H & { libp2p?: Libp2p<M> }> = {
98
+ name: 'libp2p',
99
+ start: async (helia) => {
100
+ if (libp2p == null) {
101
+ // if no peer id was passed, try to load it from the keychain
102
+ if (opts.privateKey == null) {
103
+ opts.privateKey = await loadOrCreateSelfKey(helia.datastore, opts.keychain)
104
+ }
105
+
106
+ // copy options from Helia node
107
+ opts.nodeInfo ??= {}
108
+ opts.nodeInfo.name ??= helia.info.name
109
+ opts.nodeInfo.version ??= helia.info.version
110
+ opts.nodeInfo.userAgent ??= `${helia.info.name}/${helia.info.version} ${userAgent()}`
111
+
112
+ opts.datastore ??= helia.datastore
113
+
114
+ libp2p = await createLibp2p(opts)
115
+
116
+ // override peer discovery methods to ensure we persist peer data in the
117
+ // peer store, otherwise we can't dial by peer id without extra lookups
118
+ const findProviders = helia.routing.findProviders.bind(helia.routing)
119
+ helia.routing.findProviders = async function * (cid, options): AsyncIterable<Provider> {
120
+ yield * forEach(findProviders.call(helia.routing, cid, options), async (peer) => {
121
+ if (peer.router !== 'libp2p-router') {
122
+ // only need to do this for peers not found via the libp2p router
123
+ const info = toPeerInfo(peer)
124
+ await libp2p.peerStore.merge(info.id, info)
125
+ }
126
+ })
127
+ }
128
+
129
+ const findPeer = helia.routing.findPeer.bind(helia.routing)
130
+ helia.routing.findPeer = async function (cid, options): Promise<Peer> {
131
+ const peer = await findPeer(cid, options)
132
+
133
+ if (peer.router !== 'libp2p-router') {
134
+ // only need to do this for peers not found via the libp2p router
135
+ const info = toPeerInfo(peer)
136
+ await libp2p.peerStore.merge(info.id, info)
137
+ }
138
+
139
+ return peer
140
+ }
141
+
142
+ // override peer discovery methods to ensure we persist peer data in the
143
+ // peer store, otherwise we can't dial by peer id without extra lookups
144
+ const getClosestPeers = helia.routing.getClosestPeers.bind(helia.routing)
145
+ helia.routing.getClosestPeers = async function * (cid, options): AsyncIterable<Peer> {
146
+ yield * forEach(getClosestPeers.call(helia.routing, cid, options), async (peer) => {
147
+ if (peer.router !== 'libp2p-router') {
148
+ // only need to do this for peers not found via the libp2p router
149
+ const info = toPeerInfo(peer)
150
+ await libp2p.peerStore.merge(info.id, info)
151
+ }
152
+ })
153
+ }
154
+ }
155
+
156
+ try {
157
+ if (!helia.hasRouter('libp2p-router')) {
158
+ helia.addRouter(libp2pRouting(libp2p))
159
+ }
160
+
161
+ if (isLibp2p(helia.libp2p)) {
162
+ // already configured libp2p
163
+ await helia.libp2p.start()
164
+ }
165
+ } catch (err: any) {
166
+ if (err.name !== 'NotStartedError') {
167
+ throw err
168
+ }
169
+ }
170
+ },
171
+
172
+ stop: async () => {
173
+ await libp2p?.stop()
174
+ }
175
+ }
176
+
177
+ helia.addMixin(mixin)
178
+
179
+ // @ts-expect-error libp2p property is missing, even though we just defined it
180
+ return helia
181
+ }
182
+
183
+ function toPeerInfo (peer: { id: CID, multiaddrs: Multiaddr[] }): PeerInfo {
184
+ return {
185
+ id: peerIdFromCID(peer.id),
186
+ multiaddrs: peer.multiaddrs
187
+ }
188
+ }
package/src/routing.ts CHANGED
@@ -65,6 +65,35 @@ class Libp2pRouter implements Router {
65
65
  }
66
66
  }
67
67
 
68
+ /**
69
+ * Return a Helia router that uses the passed libp2p node to find providers,
70
+ * peers and records.
71
+ *
72
+ * `withLibp2pLight` and `withLibp2p` add this for you. Use it directly when you
73
+ * create the libp2p node yourself, and do not combine it with either of them,
74
+ * otherwise Helia routes through your node while `helia.libp2p` is a second,
75
+ * separate node.
76
+ *
77
+ * A node you create yourself is not started or stopped by Helia and does not
78
+ * use Helia's datastore or identity. This only adds routing, so add a block
79
+ * broker (for example bitswap) to fetch blocks over libp2p.
80
+ *
81
+ * @example
82
+ *
83
+ * ```ts
84
+ * import { createHeliaLight } from 'helia/light'
85
+ * import { libp2pRouting } from '@helia/libp2p/light'
86
+ * import { createLibp2p } from 'libp2p'
87
+ *
88
+ * const libp2p = await createLibp2p({
89
+ * //... libp2p config here
90
+ * })
91
+ *
92
+ * const helia = await createHeliaLight({
93
+ * routers: [libp2pRouting(libp2p)]
94
+ * }).start()
95
+ * ```
96
+ */
68
97
  export function libp2pRouting (libp2p: Libp2p): Router {
69
98
  return new Libp2pRouter(libp2p)
70
99
  }
@@ -16,7 +16,7 @@ import { webSockets } from '@libp2p/websockets'
16
16
  import { yamux } from '@libp2p/yamux'
17
17
  import { bootstrapConfig } from './bootstrappers.ts'
18
18
  import { merge, replace } from './config.ts'
19
- import type { CreateLibp2pOptions } from '../index.ts'
19
+ import type { CreateLibp2pOptions } from '../light.ts'
20
20
  import type { HTTP } from '@libp2p/http'
21
21
  import type { Identify, IdentifyPush } from '@libp2p/identify'
22
22
  import type { ServiceMap } from '@libp2p/interface'
@@ -21,7 +21,7 @@ import { webSockets } from '@libp2p/websockets'
21
21
  import { yamux } from '@libp2p/yamux'
22
22
  import { bootstrapConfig } from './bootstrappers.ts'
23
23
  import { merge, replace } from './config.ts'
24
- import type { CreateLibp2pOptions } from '../index.ts'
24
+ import type { CreateLibp2pOptions } from '../light.ts'
25
25
  import type { AutoTLS } from '@ipshipyard/libp2p-auto-tls'
26
26
  import type { CircuitRelayService } from '@libp2p/circuit-relay-v2'
27
27
  import type { HTTP } from '@libp2p/http'