@helia/libp2p 2.0.0 → 2.1.0-ae012cf9

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.
@@ -19,13 +19,13 @@ import { tls } from '@libp2p/tls'
19
19
  import { uPnPNAT } from '@libp2p/upnp-nat'
20
20
  import { webRTC, webRTCDirect } from '@libp2p/webrtc'
21
21
  import { webSockets } from '@libp2p/websockets'
22
- import { userAgent } from 'libp2p/user-agent'
23
22
  import { bootstrapConfig } from './bootstrappers.ts'
24
- import type { Libp2pDefaultsOptions } from './libp2p.ts'
23
+ import type { CreateLibp2pOptions } from '../index.ts'
25
24
  import type { AutoTLS } from '@ipshipyard/libp2p-auto-tls'
26
25
  import type { CircuitRelayService } from '@libp2p/circuit-relay-v2'
27
26
  import type { HTTP } from '@libp2p/http'
28
27
  import type { Identify, IdentifyPush } from '@libp2p/identify'
28
+ import type { ServiceMap } from '@libp2p/interface'
29
29
  import type { KadDHT } from '@libp2p/kad-dht'
30
30
  import type { Keychain } from '@libp2p/keychain'
31
31
  import type { Ping } from '@libp2p/ping'
@@ -47,6 +47,8 @@ export interface DefaultLibp2pServices extends Record<string, unknown> {
47
47
  http: HTTP
48
48
  }
49
49
 
50
+ export type DefaultLibp2pOptions<M extends ServiceMap> = Libp2pOptions<DefaultLibp2pServices & M> & Required<Pick<Libp2pOptions<DefaultLibp2pServices & M>, 'nodeInfo' | 'addresses' | 'transports' | 'connectionEncrypters' | 'streamMuxers' | 'peerDiscovery' | 'services'>>
51
+
50
52
  /**
51
53
  * Returns the default libp2p config used by Helia which can then be modified or
52
54
  * extended to suit individual applications.
@@ -55,7 +57,8 @@ export interface DefaultLibp2pServices extends Record<string, unknown> {
55
57
  *
56
58
  * ```ts
57
59
  * import { myService } from '@example/my-service'
58
- * import { createHelia, libp2pDefaults } from 'helia'
60
+ * import { createHelia } from 'helia'
61
+ * import { libp2pDefaults } from '@helia/libp2p'
59
62
  *
60
63
  * // get a copy of the default libp2p config
61
64
  * const libp2p = libp2pDefaults()
@@ -72,19 +75,13 @@ export interface DefaultLibp2pServices extends Record<string, unknown> {
72
75
  * helia.libp2p.services.myService.serviceMethod()
73
76
  * ```
74
77
  */
75
- export function libp2pDefaults (options: Libp2pDefaultsOptions = {}): Libp2pOptions<DefaultLibp2pServices> & Required<Pick<Libp2pOptions<DefaultLibp2pServices>, 'services'>> {
76
- let agentVersion: string | undefined
77
-
78
- if (options.name != null && options.version != null) {
79
- agentVersion = `${options.name}/${options.version} ${userAgent()}`
80
- }
81
-
78
+ export function libp2pDefaults <M extends ServiceMap = ServiceMap> (options: CreateLibp2pOptions<M> = {}): DefaultLibp2pOptions<M> {
82
79
  return {
83
- privateKey: options.privateKey,
84
- dns: options.dns,
85
80
  nodeInfo: {
86
- userAgent: agentVersion
81
+ ...options.nodeInfo
87
82
  },
83
+ privateKey: options.privateKey,
84
+ dns: options.dns,
88
85
  addresses: {
89
86
  listen: [
90
87
  '/ip4/0.0.0.0/tcp/0',
@@ -128,7 +125,8 @@ export function libp2pDefaults (options: Libp2pDefaultsOptions = {}): Libp2pOpti
128
125
  ping: ping(),
129
126
  relay: circuitRelayServer(),
130
127
  upnp: uPnPNAT(),
131
- http: http()
132
- }
128
+ http: http(),
129
+ ...options.services
130
+ } as any
133
131
  }
134
132
  }
@@ -1,5 +1,3 @@
1
- import { loadOrCreateSelfKey } from '@libp2p/config'
2
- import { createLibp2p as create } from 'libp2p'
3
1
  import { libp2pDefaults } from './libp2p-defaults.ts'
4
2
  import type { Helia } from '@helia/interface'
5
3
  import type { Libp2p, PrivateKey } from '@libp2p/interface'
@@ -19,25 +17,21 @@ export interface Libp2pDefaultsOptions {
19
17
  version?: string
20
18
  }
21
19
 
22
- export async function createLibp2p <T extends Record<string, unknown>> (helia: Helia, options: CreateLibp2pOptions<T>): Promise<Libp2p<T>> {
20
+ export async function createLibp2pOptions <T extends Record<string, unknown>> (helia: Helia, options: CreateLibp2pOptions<T>): Promise<Libp2p<T>> {
23
21
  const libp2pOptions = options ?? {}
24
22
 
25
- // if no peer id was passed, try to load it from the keychain
26
- if (libp2pOptions.privateKey == null && options.datastore != null) {
27
- libp2pOptions.privateKey = await loadOrCreateSelfKey(options.datastore, options.keychain)
28
- }
29
-
30
23
  const defaults: any = libp2pDefaults({
31
24
  ...libp2pOptions,
32
- name: libp2pOptions.nodeInfo?.name ?? helia.info.name,
33
- version: libp2pOptions.nodeInfo?.version ?? helia.info.version
25
+ nodeInfo: {
26
+ ...libp2pOptions.nodeInfo,
27
+ name: libp2pOptions.nodeInfo?.name ?? helia.info.name,
28
+ version: libp2pOptions.nodeInfo?.version ?? helia.info.version
29
+ }
34
30
  })
35
31
  defaults.datastore = defaults.datastore ?? options.datastore
36
32
 
37
- const node = await create<T>({
33
+ return {
38
34
  ...defaults,
39
35
  ...libp2pOptions
40
- })
41
-
42
- return node
36
+ }
43
37
  }
@@ -1,9 +0,0 @@
1
- {
2
- "CreateLibp2pOptions": "https://ipfs.github.io/helia/interfaces/_helia_libp2p.CreateLibp2pOptions.html",
3
- "DefaultLibp2pServices": "https://ipfs.github.io/helia/interfaces/_helia_libp2p.DefaultLibp2pServices.html",
4
- "HeliaWithLibp2p": "https://ipfs.github.io/helia/interfaces/_helia_libp2p.HeliaWithLibp2p.html",
5
- ".:HeliaWithLibp2p": "https://ipfs.github.io/helia/interfaces/_helia_libp2p.HeliaWithLibp2p.html",
6
- "libp2pDefaults": "https://ipfs.github.io/helia/functions/_helia_libp2p.libp2pDefaults.html",
7
- "withLibp2p": "https://ipfs.github.io/helia/functions/_helia_libp2p.withLibp2p.html",
8
- ".:withLibp2p": "https://ipfs.github.io/helia/functions/_helia_libp2p.withLibp2p.html"
9
- }