# peer-info

> IPFS Peer abstraction JavaScript implementation

Latest version **0.17.5** (published 2020-02-19) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install peer-info
pnpm add peer-info
yarn add peer-info
bun add peer-info
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.17.5 |
| Published | 2020-02-19 |
| First published | 2015-09-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=10.0.0 |
| Dependencies | 4 |
| Unpacked size | 5.7 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 37 |
| Maintainers | daviddias, pgte |
| Keywords | IPFS |

## Links

- npm: https://www.npmjs.com/package/peer-info
- Repository: https://github.com/libp2p/js-peer-info
- Issues: https://github.com/libp2p/js-peer-info/issues
- npm.io page: https://npm.io/package/peer-info

## Dependencies (4)

- [mafmt](https://npm.io/package/mafmt.md) ^7.1.0
- [peer-id](https://npm.io/package/peer-id.md) ~0.13.2
- [multiaddr](https://npm.io/package/multiaddr.md) ^7.3.0
- [unique-by](https://npm.io/package/unique-by.md) ^1.0.0

## Recent versions

- 0.17.5 (latest) — 2020-02-19
- 0.17.0 (beta) — 2019-09-19
- 0.17.4 — 2020-02-17
- 0.17.3 — 2020-02-13
- 0.17.2 — 2020-02-11
- 0.17.1 — 2020-01-02
- 0.16.2 — 2019-09-19
- 0.16.1 — 2019-09-11
- 0.16.0 — 2019-07-12
- 0.15.1 — 2019-01-09
- 0.15.0 — 2018-11-29
- 0.14.1 — 2018-04-09
- 0.14.0 — 2018-04-05
- 0.11.6 — 2018-01-28
- 0.11.4 — 2017-12-14
- … 26 more at https://npm.io/package/peer-info/versions

## README

# js-peer-info

[![](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-peer-info.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-peer-info)
[![](https://img.shields.io/travis/libp2p/js-peer-info.svg?style=flat-square)](https://travis-ci.com/libp2p/js-peer-info)
[![Dependency Status](https://david-dm.org/libp2p/js-peer-info.svg?style=flat-square)](https://david-dm.org/libp2p/js-peer-info)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
![](https://img.shields.io/badge/npm-%3E%3D3.0.0-orange.svg?style=flat-square)
![](https://img.shields.io/badge/Node.js-%3E%3D6.0.0-orange.svg?style=flat-square)

## Lead Maintainer

[Vasco Santos](https://github.com/vasco-santos)

## Table of Contents

- [js-peer-info](#js-peer-info)
  - [Lead Maintainer](#lead-maintainer)
  - [Table of Contents](#table-of-contents)
  - [Installation](#installation)
    - [npm](#npm)
    - [Node.JS, Browserify, Webpack](#nodejs-browserify-webpack)
    - [Browser: `<script>` Tag](#browser-script-tag)
  - [Usage](#usage)
  - [API](#api)
    - [`PeerInfo.create([id])`](#peerinfocreateid)
    - [`new PeerInfo(id)`](#new-peerinfoid)
    - [`.id`](#id)
    - [`protocols`](#protocols)
    - [`.protocols.add(protocol)`](#protocolsaddprotocol)
    - [`.protocols.delete(protocol)`](#protocolsdeleteprotocol)
    - [`.multiaddrs`](#multiaddrs)
    - [`.multiaddrs.add(addr)`](#multiaddrsaddaddr)
    - [`.multiaddrs.addSafe(addr)`](#multiaddrsaddsafeaddr)
    - [`.multiaddrs.delete(addr)`](#multiaddrsdeleteaddr)
    - [`.multiaddrs.replace(existing, fresh)`](#multiaddrsreplaceexisting-fresh)
    - [`.connect(ma)`](#connectma)
    - [`.disconnect()`](#disconnect)
    - [`.isConnected()`](#isconnected)
  - [Contribute](#contribute)
  - [License](#license)

## Installation

### npm

```sh
> npm i peer-info
```

### Node.JS, Browserify, Webpack

```js
const PeerInfo = require('peer-info')
```

### Browser: `<script>` Tag

Loading this module through a script tag will make the `PeerInfo` obj available in the global namespace.

```html
<script src="https://unpkg.com/peer-info/dist/index.min.js"></script>
<!-- OR -->
<script src="https://unpkg.com/peer-info/dist/index.js"></script>
```

## Usage

```js
const PeerInfo = require('peer-info')
const peer = new PeerInfo()

// TCP port 5001
peer.multiaddrs.add('/ip4/1.2.3.4/tcp/5001')

// UDP port 8001
peer.multiaddrs.add('/ip4/1.2.3.4/udp/8001')

// mic/speaker soundwaves using frequencies 697 and 1209
peer.multiaddrs.add('/sonic/bfsk/697/1209')
```

## API

```js
const PeerInfo = require('peer-info')
```

### `PeerInfo.create([id])`

- `id` optional - can be a PeerId or a JSON object(will be parsed with https://github.com/libp2p/js-peer-id#createfromjsonobj)

Creates a new PeerInfo instance and if no `id` is passed it
generates a new underlying [PeerID](https://github.com/libp2p/js-peer-id)
for it.

Returns `Promise<PeerInfo>`.

### `new PeerInfo(id)`

- `id: PeerId` - instance of PeerId (optional)

Creates a new PeerInfo instance from an existing PeerId.

### `.id`

The [PeerId](https://github.com/libp2p/js-peer-id) of the peer this info relates to.

### `protocols`

A list of protocols that `peer` supports.

### `.protocols.add(protocol)`

Adds a protocol that `peer` can support. `protocol` is a string.

### `.protocols.delete(protocol)`

Removes a protocol that `peer` no longer supports. `protocol` is a string.

### `.multiaddrs`

A list of multiaddresses instances that `peer` can be reached at.

### `.multiaddrs.add(addr)`

- `addr: Multiaddr`

Adds a new multiaddress that `peer` can be reached at. `addr` is an instance of
a [multiaddr](https://github.com/multiformats/js-multiaddr).

### `.multiaddrs.addSafe(addr)`

- `addr: Multiaddr`

The `addSafe` call, in comparison to `add`, will only add the multiaddr to
`multiaddrs` if the same multiaddr tries to be added twice.

This is a simple mechanism to prevent `multiaddrs` from becoming bloated with
unusable addresses, which happens when we exchange observed multiaddrs with
peers which will not provide a useful multiaddr to be shared to the rest of the
network (e.g. a multiaddr referring to a peer inside a LAN being shared to the
outside world).

### `.multiaddrs.delete(addr)`

- `addr: Multiaddr`

Removes a multiaddress instance `addr` from `peer`.

### `.multiaddrs.replace(existing, fresh)`

- `existing: Multiaddr`
- `fresh: Multiaddr`

Removes the array of multiaddresses `existing` from `peer`, and adds the array
of multiaddresses `fresh`.

### `.connect(ma)`

Records the given multiaddr, `ma` as the active multiaddr of the peer.

- `ma: Multiaddr`

### `.disconnect()`

Removes the existing connected Multiaddr from tracking.

### `.isConnected()`

Returns `true`  if a connected Multiaddr exists, otherwise returns `false`.

## Contribute

PRs accepted.

Small note: If editing the Readme, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification.

## License

[MIT © David Dias](LICENSE)

---
_Source: https://npm.io/package/peer-info · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
