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

Commit

Permalink
feat!: update to latest libp2p interfaces (#131)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: uses new single-issue libp2p interface modules
  • Loading branch information
achingbrain authored Jun 15, 2022
1 parent efbafd6 commit c9d1e62
Show file tree
Hide file tree
Showing 13 changed files with 129 additions and 112 deletions.
114 changes: 61 additions & 53 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
# libp2p-mdns JavaScript implementation
# @libp2p/mdns <!-- omit in toc -->

[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://protocol.ai)
[![](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/)
[![](https://img.shields.io/badge/freenode-%23libp2p-yellow.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23libp2p)
[![Discourse posts](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg)](https://discuss.libp2p.io)
[![](https://img.shields.io/codecov/c/github/libp2p/js-libp2p-mdns.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p-mdns)
[![Build Status](https://github.com/libp2p/js-libp2p-mdns/actions/workflows/js-test-and-release.yml/badge.svg?branch=main)](https://github.com/libp2p/js-libp2p-mdns/actions/workflows/js-test-and-release.yml)
[![Dependency Status](https://david-dm.org/libp2p/js-libp2p-mdns.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-mdns)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
[![libp2p.io](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/)
[![IRC](https://img.shields.io/badge/freenode-%23libp2p-yellow.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23libp2p)
[![Discuss](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg?style=flat-square)](https://discuss.libp2p.io)
[![codecov](https://img.shields.io/codecov/c/github/libp2p/js-libp2p-mdns.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p-mdns)
[![CI](https://img.shields.io/github/workflow/status/libp2p/js-libp2p-interfaces/test%20&%20maybe%20release/master?style=flat-square)](https://github.com/libp2p/js-libp2p-mdns/actions/workflows/js-test-and-release.yml)

> JavaScript libp2p MulticastDNS discovery implementation
> Node.js libp2p mDNS discovery implementation for peer discovery
## Table of contents <!-- omit in toc -->

- [Install](#install)
- [Usage](#usage)
- [MDNS messages](#mdns-messages)
- [Contribute](#contribute)
- [License](#license)
- [Contribution](#contribution)

## Install

```console
$ npm i @libp2p/mdns
```

## Usage

Expand All @@ -31,68 +43,64 @@ setTimeout(() => mdns.stop(), 20 * 1000)
- `peerId` - PeerId to announce
- `multiaddrs` - multiaddrs to announce
- `broadcast` - (true/false) announce our presence through mDNS, default `false`
- `interval` - query interval, default 10 * 1000 (10 seconds)
- `serviceTag` - name of the service announce , default 'ipfs.local`
- `interval` - query interval, default 10 \* 1000 (10 seconds)
- `serviceTag` - name of the service announce , default 'ipfs.local\`
- `compat` - enable/disable compatibility with go-libp2p-mdns, default `true`

## MDNS messages

A query is sent to discover the IPFS nodes on the local network

```
{ type: 'query',
questions: [ { name: 'ipfs.local', type: 'PTR' } ]
}
```
{ type: 'query',
questions: [ { name: 'ipfs.local', type: 'PTR' } ]
}

When a query is detected, each IPFS node sends an answer about itself

```
[ { name: 'ipfs.local',
type: 'PTR',
class: 'IN',
ttl: 120,
data: 'QmNPubsDWATVngE3d5WDSNe7eVrFLuk38qb9t6vdLnu2aK.ipfs.local' },
{ name: 'QmNPubsDWATVngE3d5WDSNe7eVrFLuk38qb9t6vdLnu2aK.ipfs.local',
type: 'SRV',
class: 'IN',
ttl: 120,
data:
{ priority: 10,
weight: 1,
port: '20002',
target: 'LAPTOP-G5LJ7VN9' } },
{ name: 'QmNPubsDWATVngE3d5WDSNe7eVrFLuk38qb9t6vdLnu2aK.ipfs.local',
type: 'TXT',
class: 'IN',
ttl: 120,
data: ['QmNPubsDWATVngE3d5WDSNe7eVrFLuk38qb9t6vdLnu2aK'] },
{ name: 'LAPTOP-G5LJ7VN9',
type: 'A',
class: 'IN',
ttl: 120,
data: '127.0.0.1' },
{ name: 'LAPTOP-G5LJ7VN9',
type: 'AAAA',
class: 'IN',
ttl: 120,
data: '::1' } ]
```
[ { name: 'ipfs.local',
type: 'PTR',
class: 'IN',
ttl: 120,
data: 'QmNPubsDWATVngE3d5WDSNe7eVrFLuk38qb9t6vdLnu2aK.ipfs.local' },
{ name: 'QmNPubsDWATVngE3d5WDSNe7eVrFLuk38qb9t6vdLnu2aK.ipfs.local',
type: 'SRV',
class: 'IN',
ttl: 120,
data:
{ priority: 10,
weight: 1,
port: '20002',
target: 'LAPTOP-G5LJ7VN9' } },
{ name: 'QmNPubsDWATVngE3d5WDSNe7eVrFLuk38qb9t6vdLnu2aK.ipfs.local',
type: 'TXT',
class: 'IN',
ttl: 120,
data: ['QmNPubsDWATVngE3d5WDSNe7eVrFLuk38qb9t6vdLnu2aK'] },
{ name: 'LAPTOP-G5LJ7VN9',
type: 'A',
class: 'IN',
ttl: 120,
data: '127.0.0.1' },
{ name: 'LAPTOP-G5LJ7VN9',
type: 'AAAA',
class: 'IN',
ttl: 120,
data: '::1' } ]

## Contribute

The libp2p implementation in JavaScript is a work in progress. As such, there are a few things you can do right now to help out:

- Go through the modules and **check out existing issues**. This is especially useful for modules in active development. Some knowledge of IPFS/libp2p may be required, as well as the infrastructure behind it - for instance, you may need to read up on p2p and more complex operations like muxing to be able to help technically.
- **Perform code reviews**. More eyes will help a) speed the project along b) ensure quality and c) reduce possible future bugs.
- Go through the modules and **check out existing issues**. This is especially useful for modules in active development. Some knowledge of IPFS/libp2p may be required, as well as the infrastructure behind it - for instance, you may need to read up on p2p and more complex operations like muxing to be able to help technically.
- **Perform code reviews**. More eyes will help a) speed the project along b) ensure quality and c) reduce possible future bugs.

## License

Licensed under either of

* Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / http://www.apache.org/licenses/LICENSE-2.0)
* MIT ([LICENSE-MIT](LICENSE-MIT) / http://opensource.org/licenses/MIT)
- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)

### Contribution
## Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
14 changes: 10 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
],
"exports": {
".": {
"types": "./src/index.d.ts",
"import": "./dist/src/index.js"
}
},
Expand Down Expand Up @@ -129,18 +130,23 @@
"release": "aegir release"
},
"dependencies": {
"@libp2p/interfaces": "^2.0.1",
"@libp2p/logger": "^1.1.3",
"@libp2p/components": "^1.0.0",
"@libp2p/interface-peer-discovery": "^1.0.0",
"@libp2p/interface-peer-id": "^1.0.2",
"@libp2p/interface-peer-info": "^1.0.1",
"@libp2p/interfaces": "^3.0.2",
"@libp2p/logger": "^2.0.0",
"@libp2p/peer-id": "^1.1.9",
"@multiformats/multiaddr": "^10.1.5",
"multicast-dns": "^7.2.0",
"multiformats": "^9.6.3"
},
"devDependencies": {
"@libp2p/interface-compliance-tests": "^2.0.1",
"@libp2p/interface-address-manager": "^1.0.1",
"@libp2p/interface-peer-discovery-compliance-tests": "^1.0.0",
"@libp2p/peer-id-factory": "^1.0.9",
"@types/multicast-dns": "^7.2.1",
"aegir": "^37.0.8",
"aegir": "^37.2.0",
"delay": "^5.0.0",
"p-defer": "^4.0.0",
"p-wait-for": "^4.1.0",
Expand Down
6 changes: 3 additions & 3 deletions src/compat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import { EventEmitter, CustomEvent } from '@libp2p/interfaces/events'
import { Responder } from './responder.js'
import { Querier } from './querier.js'
import type { PeerDiscovery, PeerDiscoveryEvents } from '@libp2p/interfaces/peer-discovery'
import type { Components, Initializable } from '@libp2p/interfaces/components'
import { symbol } from '@libp2p/interfaces/peer-discovery'
import type { PeerDiscovery, PeerDiscoveryEvents } from '@libp2p/interface-peer-discovery'
import type { Components, Initializable } from '@libp2p/components'
import { symbol } from '@libp2p/interface-peer-discovery'

export interface GoMulticastDNSInit {
queryPeriod?: number
Expand Down
6 changes: 3 additions & 3 deletions src/compat/querier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events'
import MDNS from 'multicast-dns'
import { logger } from '@libp2p/logger'
import { SERVICE_TAG_LOCAL, MULTICAST_IP, MULTICAST_PORT } from './constants.js'
import type { PeerDiscovery, PeerDiscoveryEvents } from '@libp2p/interfaces/peer-discovery'
import type { PeerDiscovery, PeerDiscoveryEvents } from '@libp2p/interface-peer-discovery'
import type { ResponsePacket } from 'multicast-dns'
import type { RemoteInfo } from 'dgram'
import { Components, Initializable } from '@libp2p/interfaces/components'
import { Components, Initializable } from '@libp2p/components'
import { findPeerInfoInAnswers } from './utils.js'
import { symbol } from '@libp2p/interfaces/peer-discovery'
import { symbol } from '@libp2p/interface-peer-discovery'

const log = logger('libp2p:mdns:compat:querier')

Expand Down
2 changes: 1 addition & 1 deletion src/compat/responder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { SERVICE_TAG_LOCAL } from './constants.js'
import { MultiaddrObject, protocols } from '@multiformats/multiaddr'
import type { RemoteInfo } from 'dgram'
import type { Answer } from 'dns-packet'
import { Components, Initializable } from '@libp2p/interfaces/components'
import { Components, Initializable } from '@libp2p/components'

const log = logger('libp2p:mdns:compat:responder')

Expand Down
13 changes: 7 additions & 6 deletions src/compat/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { PeerInfo } from '@libp2p/interfaces/peer-info'
import type { PeerId } from '@libp2p/interfaces/peer-id'
import type { PeerInfo } from '@libp2p/interface-peer-info'
import type { PeerId } from '@libp2p/interface-peer-id'
import { logger } from '@libp2p/logger'
import { peerIdFromString } from '@libp2p/peer-id'
import { Multiaddr } from '@multiformats/multiaddr'
import { multiaddr } from '@multiformats/multiaddr'
import type { Multiaddr } from '@multiformats/multiaddr'
import type { Answer } from 'dns-packet'
import { SERVICE_TAG_LOCAL } from './constants.js'

Expand Down Expand Up @@ -64,11 +65,11 @@ export function findPeerInfoInAnswers (answers: Answer[], ourPeerId: PeerId): Pe
answers.forEach(answer => {
if (answer.type === 'SRV') {
if (hosts.A[answer.data.target] != null) {
multiaddrs.push(new Multiaddr(`/ip4/${hosts.A[answer.data.target]}/tcp/${answer.data.port}/p2p/${peerId.toString()}`))
multiaddrs.push(multiaddr(`/ip4/${hosts.A[answer.data.target]}/tcp/${answer.data.port}/p2p/${peerId.toString()}`))
} else if (hosts.AAAA[answer.data.target] != null) {
multiaddrs.push(new Multiaddr(`/ip6/${hosts.AAAA[answer.data.target]}/tcp/${answer.data.port}/p2p/${peerId.toString()}`))
multiaddrs.push(multiaddr(`/ip6/${hosts.AAAA[answer.data.target]}/tcp/${answer.data.port}/p2p/${peerId.toString()}`))
} else {
multiaddrs.push(new Multiaddr(`/dnsaddr/${answer.data.target}/tcp/${answer.data.port}/p2p/${peerId.toString()}`))
multiaddrs.push(multiaddr(`/dnsaddr/${answer.data.target}/tcp/${answer.data.port}/p2p/${peerId.toString()}`))
}
}
})
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events'
import { logger } from '@libp2p/logger'
import * as query from './query.js'
import { GoMulticastDNS } from './compat/index.js'
import type { PeerDiscovery, PeerDiscoveryEvents } from '@libp2p/interfaces/peer-discovery'
import type { PeerInfo } from '@libp2p/interfaces/peer-info'
import { Components, Initializable } from '@libp2p/interfaces/components'
import { symbol } from '@libp2p/interfaces/peer-discovery'
import type { PeerDiscovery, PeerDiscoveryEvents } from '@libp2p/interface-peer-discovery'
import type { PeerInfo } from '@libp2p/interface-peer-info'
import { Components, Initializable } from '@libp2p/components'
import { symbol } from '@libp2p/interface-peer-discovery'

const log = logger('libp2p:mdns')

Expand Down
4 changes: 2 additions & 2 deletions src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import os from 'os'
import { logger } from '@libp2p/logger'
import { Multiaddr, MultiaddrObject, protocols } from '@multiformats/multiaddr'
import { peerIdFromString } from '@libp2p/peer-id'
import type { PeerId } from '@libp2p/interfaces/peer-id'
import type { PeerInfo } from '@libp2p/interfaces/peer-info'
import type { PeerId } from '@libp2p/interface-peer-id'
import type { PeerInfo } from '@libp2p/interface-peer-info'
import type { MulticastDNS, ResponsePacket, QueryPacket } from 'multicast-dns'
import type { SrvAnswer, StringAnswer, TxtAnswer, Answer } from 'dns-packet'

Expand Down
12 changes: 6 additions & 6 deletions test/compat/go-multicast-dns.spec.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
/* eslint-env mocha */
import { expect } from 'aegir/chai'
import { Multiaddr } from '@multiformats/multiaddr'
import { multiaddr } from '@multiformats/multiaddr'
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import pDefer from 'p-defer'
import { GoMulticastDNS } from '../../src/compat/index.js'
import { Components } from '@libp2p/interfaces/components'
import { Components } from '@libp2p/components'
import { stubInterface } from 'ts-sinon'
import type { AddressManager } from '@libp2p/interfaces/address-manager'
import type { PeerInfo } from '@libp2p/interfaces/peer-info'
import type { AddressManager } from '@libp2p/interface-address-manager'
import type { PeerInfo } from '@libp2p/interface-peer-info'

let port = 20000

async function createGoMulticastDNS () {
const peerId = await createEd25519PeerId()
const addressManager = stubInterface<AddressManager>()
addressManager.getAddresses.returns([
new Multiaddr(`/ip4/127.0.0.1/tcp/${port++}/p2p/${peerId.toString()}`),
new Multiaddr(`/ip4/127.0.0.1/tcp/${port++}/p2p/${peerId.toString()}`)
multiaddr(`/ip4/127.0.0.1/tcp/${port++}/p2p/${peerId.toString()}`),
multiaddr(`/ip4/127.0.0.1/tcp/${port++}/p2p/${peerId.toString()}`)
])

const components = new Components({
Expand Down
4 changes: 2 additions & 2 deletions test/compat/querier.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import OS from 'os'
import delay from 'delay'
import { Querier } from '../../src/compat/querier.js'
import { SERVICE_TAG_LOCAL } from '../../src/compat/constants.js'
import type { PeerId } from '@libp2p/interfaces/peer-id'
import type { PeerId } from '@libp2p/interface-peer-id'
import type { RemoteInfo } from 'dgram'
import type { Answer } from 'dns-packet'
import { Components } from '@libp2p/interfaces/components'
import { Components } from '@libp2p/components'

describe('Querier', () => {
let querier: Querier
Expand Down
15 changes: 8 additions & 7 deletions test/compat/responder.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
/* eslint-env mocha */

import { expect } from 'aegir/chai'
import { Multiaddr } from '@multiformats/multiaddr'
import type { Multiaddr } from '@multiformats/multiaddr'
import { multiaddr } from '@multiformats/multiaddr'
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import mDNS from 'multicast-dns'
import delay from 'delay'
import pDefer from 'p-defer'
import { Responder } from '../../src/compat/responder.js'
import { SERVICE_TAG_LOCAL, MULTICAST_IP, MULTICAST_PORT } from '../../src/compat/constants.js'
import type { PeerId } from '@libp2p/interfaces/peer-id'
import type { PeerId } from '@libp2p/interface-peer-id'
import type { ResponsePacket } from 'multicast-dns'
import { Components } from '@libp2p/interfaces/components'
import { Components } from '@libp2p/components'
import { stubInterface } from 'ts-sinon'
import { findPeerInfoInAnswers } from '../../src/compat/utils.js'
import type { AddressManager } from '@libp2p/interfaces/address-manager'
import type { PeerInfo } from '@libp2p/interfaces/peer-info'
import type { AddressManager } from '@libp2p/interface-address-manager'
import type { PeerInfo } from '@libp2p/interface-peer-info'

describe('Responder', () => {
let responder: Responder
Expand All @@ -30,8 +31,8 @@ describe('Responder', () => {
])

multiadddrs = [
new Multiaddr(`/ip4/127.0.0.1/tcp/20001/p2p/${peerIds[0].toString()}`),
new Multiaddr(`/ip4/127.0.0.1/tcp/20002/p2p/${peerIds[0].toString()}`)
multiaddr(`/ip4/127.0.0.1/tcp/20001/p2p/${peerIds[0].toString()}`),
multiaddr(`/ip4/127.0.0.1/tcp/20002/p2p/${peerIds[0].toString()}`)
]

const addressManager = stubInterface<AddressManager>()
Expand Down
12 changes: 6 additions & 6 deletions test/compliance.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* eslint-env mocha */

import tests from '@libp2p/interface-compliance-tests/peer-discovery'
import { Multiaddr } from '@multiformats/multiaddr'
import tests from '@libp2p/interface-peer-discovery-compliance-tests'
import { multiaddr } from '@multiformats/multiaddr'
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { MulticastDNS } from '../src/index.js'
import type { AddressManager } from '@libp2p/interfaces/address-manager'
import type { AddressManager } from '@libp2p/interface-address-manager'
import { CustomEvent } from '@libp2p/interfaces/events'
import { Components } from '@libp2p/interfaces/components'
import { Components } from '@libp2p/components'
import { stubInterface } from 'ts-sinon'

let mdns: MulticastDNS
Expand All @@ -21,7 +21,7 @@ describe('compliance tests', () => {

const addressManager = stubInterface<AddressManager>()
addressManager.getAddresses.returns([
new Multiaddr(`/ip4/127.0.0.1/tcp/13921/p2p/${peerId1.toString()}`)
multiaddr(`/ip4/127.0.0.1/tcp/13921/p2p/${peerId1.toString()}`)
])

mdns = new MulticastDNS({
Expand All @@ -39,7 +39,7 @@ describe('compliance tests', () => {
intervalId = setInterval(() => mdns._onPeer(new CustomEvent('peer', {
detail: {
id: peerId2,
multiaddrs: [new Multiaddr(maStr)],
multiaddrs: [multiaddr(maStr)],
protocols: []
}
})), 1000)
Expand Down
Loading

0 comments on commit c9d1e62

Please sign in to comment.