Skip to content

Commit

Permalink
fix: make transports optional (#2295)
Browse files Browse the repository at this point in the history
Follow up to #2293 that makes the transports array optional.
  • Loading branch information
achingbrain authored Dec 4, 2023
1 parent 18db7a4 commit 887c6ff
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/libp2p/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export interface Libp2pInit<T extends ServiceMap = { x: Record<string, unknown>
/**
* An array that must include at least 1 compliant transport
*/
transports: Array<(components: Components) => Transport>
transports?: Array<(components: Components) => Transport>
streamMuxers?: Array<(components: Components) => StreamMuxerFactory>
connectionEncryption?: Array<(components: Components) => ConnectionEncrypter>
peerDiscovery?: Array<(components: Components) => PeerDiscovery>
Expand Down Expand Up @@ -150,7 +150,7 @@ export type Libp2pOptions<T extends ServiceMap = Record<string, unknown>> = Recu
* const libp2p = await createLibp2p(options)
* ```
*/
export async function createLibp2p <T extends ServiceMap = { x: Record<string, unknown> }> (options: Libp2pOptions<T>): Promise<Libp2p<T>> {
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) {
Expand Down
4 changes: 2 additions & 2 deletions packages/libp2p/src/libp2p.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
})

// Transport modules
init.transports.forEach((fn, index) => {
init.transports?.forEach((fn, index) => {
this.components.transportManager.add(this.configureComponent(`transport-${index}`, fn(this.components)))
})

Expand Down Expand Up @@ -385,7 +385,7 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
* Returns a new Libp2pNode instance - this exposes more of the internals than the
* libp2p interface and is useful for testing and debugging.
*/
export async function createLibp2pNode <T extends ServiceMap = Record<string, unknown>> (options: Libp2pOptions<T>): Promise<Libp2pNode<T>> {
export async function createLibp2pNode <T extends ServiceMap = Record<string, unknown>> (options: Libp2pOptions<T> = {}): Promise<Libp2pNode<T>> {
options.peerId ??= await createEd25519PeerId()

return new Libp2pNode(validateConfig(options))
Expand Down
19 changes: 19 additions & 0 deletions packages/libp2p/test/core/core.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* eslint-env mocha */

import { expect } from 'aegir/chai'
import { createLibp2p } from '../../src/index.js'
import type { Libp2p } from '@libp2p/interface'

describe('core', () => {
let libp2p: Libp2p

after(async () => {
await libp2p.stop()
})

it('should start a minimal node', async () => {
libp2p = await createLibp2p({})

expect(libp2p).to.have.property('status', 'started')
})
})

0 comments on commit 887c6ff

Please sign in to comment.