Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

Commit

Permalink
fix: update interfaces
Browse files Browse the repository at this point in the history
Updates to latest code from libp2p/js-libp2p-interfaces#180
  • Loading branch information
achingbrain committed Mar 15, 2022
1 parent a0a9169 commit 4a74428
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,14 @@
"release": "semantic-release"
},
"dependencies": {
"@libp2p/peer-id": "^1.0.4",
"@libp2p/logger": "^1.1.2",
"@libp2p/peer-id": "^1.1.8",
"@multiformats/mafmt": "^11.0.2",
"@multiformats/multiaddr": "^10.1.2",
"debug": "^4.3.1"
"@multiformats/multiaddr": "^10.1.7"
},
"devDependencies": {
"@libp2p/interface-compliance-tests": "^1.0.8",
"@libp2p/interfaces": "^1.1.1",
"@types/debug": "^4.1.5",
"@libp2p/interface-compliance-tests": "^1.1.16",
"@libp2p/interfaces": "^1.3.14",
"aegir": "^36.1.3"
}
}
20 changes: 9 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { PeerId } from '@libp2p/peer-id'
import { Multiaddr } from '@multiformats/multiaddr'
import { P2P } from '@multiformats/mafmt'
import { EventEmitter } from 'events'
import debug from 'debug'
import type PeerDiscovery from '@libp2p/interfaces/peer-discovery'
import { CustomEvent, EventEmitter } from '@libp2p/interfaces'
import { logger } from '@libp2p/logger'
import type { PeerDiscovery, PeerDiscoveryEvents } from '@libp2p/interfaces/peer-discovery'
import type { PeerData } from '@libp2p/interfaces/peer-data'
import { peerIdFromString } from '@libp2p/peer-id'

const log = Object.assign(debug('libp2p:bootstrap'), {
error: debug('libp2p:bootstrap:error')
})
const log = logger('libp2p:bootstrap')

export interface BootstrapOptions {
/**
Expand All @@ -25,10 +23,10 @@ export interface BootstrapOptions {
/**
* Emits 'peer' events on a regular interval for each peer in the provided list.
*/
export class Bootstrap extends EventEmitter implements PeerDiscovery {
export class Bootstrap extends EventEmitter<PeerDiscoveryEvents> implements PeerDiscovery {
static tag = 'bootstrap'

private timer?: NodeJS.Timer
private timer?: ReturnType<typeof setInterval>
private readonly list: PeerData[]
private readonly interval: number

Expand Down Expand Up @@ -56,7 +54,7 @@ export class Bootstrap extends EventEmitter implements PeerDiscovery {
}

const peerData: PeerData = {
id: PeerId.fromString(peerIdStr),
id: peerIdFromString(peerIdStr),
multiaddrs: [ma],
protocols: []
}
Expand Down Expand Up @@ -91,7 +89,7 @@ export class Bootstrap extends EventEmitter implements PeerDiscovery {
}

this.list.forEach((peerData) => {
this.emit('peer', peerData)
this.dispatchEvent(new CustomEvent<PeerData>('peer', { detail: peerData }))
})
}

Expand Down
14 changes: 9 additions & 5 deletions test/bootstrap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import { expect } from 'aegir/utils/chai.js'
import { IPFS } from '@multiformats/mafmt'
import { PeerId } from '@libp2p/peer-id'
import { Bootstrap } from '../src/index.js'
import peerList from './fixtures/default-peers.js'
import partialValidPeerList from './fixtures/some-invalid-peers.js'
import type { PeerData } from '@libp2p/interfaces/peer-data'
import { isPeerId } from '@libp2p/interfaces/peer-id'

describe('bootstrap', () => {
it('should throw if no peer list is provided', () => {
Expand All @@ -21,7 +21,9 @@ describe('bootstrap', () => {
interval: 2000
})

const p = new Promise((resolve) => r.once('peer', resolve))
const p = new Promise((resolve) => r.addEventListener('peer', resolve, {
once: true
}))
r.start()

await p
Expand All @@ -37,9 +39,11 @@ describe('bootstrap', () => {
})

const p = new Promise<void>((resolve) => {
r.once('peer', ({ id, multiaddrs }) => {
r.addEventListener('peer', (evt) => {
const { id, multiaddrs } = evt.detail

expect(id).to.exist()
expect(id).to.be.an.instanceOf(PeerId)
expect(isPeerId(id)).to.be.true()
expect(multiaddrs.length).to.eq(1)
expect(IPFS.matches(multiaddrs[0].toString())).equals(true)
resolve()
Expand All @@ -60,7 +64,7 @@ describe('bootstrap', () => {
})

let emitted: PeerData[] = []
r.on('peer', p => emitted.push(p))
r.addEventListener('peer', p => emitted.push(p.detail))

// Should fire emit event for each peer in list on start,
// so wait 50 milliseconds then check
Expand Down

0 comments on commit 4a74428

Please sign in to comment.