1 | js-multiaddr
|
2 | ============
|
3 |
|
4 | [![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
|
5 | [![](https://img.shields.io/badge/project-multiformats-blue.svg?style=flat-square)](https://github.com/multiformats/multiformats)
|
6 | [![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](https://webchat.freenode.net/?channels=%23ipfs)
|
7 | [![Dependency Status](https://david-dm.org/multiformats/js-multiaddr.svg?style=flat-square)](https://david-dm.org/multiformats/js-multiaddr)
|
8 | [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
|
9 | [![](https://img.shields.io/badge/readme%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)
|
10 | [![](https://img.shields.io/travis/multiformats/js-multiaddr.svg?style=flat-square)](https://travis-ci.com/multiformats/js-multiaddr)
|
11 | [![codecov](https://img.shields.io/codecov/c/github/multiformats/js-multiaddr.svg?style=flat-square)](https://codecov.io/gh/multiformats/js-multiaddr)
|
12 |
|
13 | > JavaScript implementation of [multiaddr](https://github.com/multiformats/multiaddr).
|
14 |
|
15 | ## Lead Maintainer
|
16 |
|
17 | [Jacob Heun](https://github.com/jacobheun)
|
18 |
|
19 | ## Table of Contents
|
20 |
|
21 | - [js-multiaddr](#js-multiaddr)
|
22 | - [Lead Maintainer](#lead-maintainer)
|
23 | - [Table of Contents](#table-of-contents)
|
24 | - [Background](#background)
|
25 | - [What is multiaddr?](#what-is-multiaddr)
|
26 | - [Install](#install)
|
27 | - [NPM](#npm)
|
28 | - [Browser: `<script>` Tag](#browser-script-tag)
|
29 | - [Usage](#usage)
|
30 | - [API](#api)
|
31 | - [Resolvers](#resolvers)
|
32 | - [Contribute](#contribute)
|
33 | - [License](#license)
|
34 |
|
35 | ## Background
|
36 |
|
37 | ### What is multiaddr?
|
38 |
|
39 | A standard way to represent addresses that
|
40 |
|
41 | - support any standard network protocol
|
42 | - are self-describing
|
43 | - have a binary packed format
|
44 | - have a nice string representation
|
45 | - encapsulate well
|
46 |
|
47 | ## Install
|
48 |
|
49 | ### NPM
|
50 | ```sh
|
51 | npm i multiaddr
|
52 | ```
|
53 |
|
54 | ### Browser: `<script>` Tag
|
55 |
|
56 | Loading this module through a script tag will make the `Multiaddr` obj available in
|
57 | the global namespace.
|
58 |
|
59 | ```html
|
60 | <script src="https://unpkg.com/multiaddr/dist/index.min.js"></script>
|
61 | ```
|
62 |
|
63 | ## Usage
|
64 |
|
65 | ```js
|
66 | // if we are coming from <= 8.x you can use the factory function
|
67 | const { multiaddr } = require('multiaddr')
|
68 | const addr = multiaddr("/ip4/127.0.0.1/udp/1234")
|
69 | // <Multiaddr /ip4/127.0.0.1/udp/1234>
|
70 |
|
71 | // or just the class directly
|
72 | const { Multiaddr } = require('multiaddr')
|
73 |
|
74 | const addr = new Multiaddr("/ip4/127.0.0.1/udp/1234")
|
75 | // <Multiaddr /ip4/127.0.0.1/udp/1234>
|
76 |
|
77 | addr.bytes
|
78 | // <Uint8Array 04 7f 00 00 01 11 04 d2>
|
79 |
|
80 | addr.toString()
|
81 | // '/ip4/127.0.0.1/udp/1234'
|
82 |
|
83 | addr.protos()
|
84 | /*
|
85 | [
|
86 | {code: 4, name: 'ip4', size: 32},
|
87 | {code: 273, name: 'udp', size: 16}
|
88 | ]
|
89 | */
|
90 |
|
91 | // gives you an object that is friendly with what Node.js core modules expect for addresses
|
92 | addr.nodeAddress()
|
93 | /*
|
94 | {
|
95 | family: 4,
|
96 | port: 1234,
|
97 | address: "127.0.0.1"
|
98 | }
|
99 | */
|
100 |
|
101 | addr.encapsulate('/sctp/5678')
|
102 | // <Multiaddr /ip4/127.0.0.1/udp/1234/sctp/5678>
|
103 | ```
|
104 |
|
105 | ## API
|
106 |
|
107 | https://multiformats.github.io/js-multiaddr/
|
108 |
|
109 | ## Resolvers
|
110 |
|
111 | `multiaddr` allows multiaddrs to be resolved when appropriate resolvers are provided. This module already has resolvers available, but you can also create your own. Resolvers should always be set in the same module that is calling `multiaddr.resolve()` to avoid conflicts if multiple versions of `multiaddr` are in your dependency tree.
|
112 | To provide multiaddr resolvers you can do:
|
113 |
|
114 | ```js
|
115 | const { Multiaddr } = require('multiaddr')
|
116 | const resolvers = require('multiaddr/src/resolvers')
|
117 |
|
118 | Multiaddr.resolvers.set('dnsaddr', resolvers.dnsaddrResolver)
|
119 | ```
|
120 |
|
121 | The available resolvers are:
|
122 |
|
123 | | Name | type | Description |
|
124 | |-------------|------|-------------|
|
125 | | `dnsaddrResolver` | `dnsaddr` | dnsaddr resolution with TXT Records |
|
126 |
|
127 | A resolver receives a `Multiaddr` as a parameter and returns a `Promise<Array<string>>`.
|
128 |
|
129 | ## Contribute
|
130 |
|
131 | Contributions welcome. Please check out [the issues](https://github.com/multiformats/js-multiaddr/issues).
|
132 |
|
133 | Check out our [contributing document](https://github.com/multiformats/multiformats/blob/master/contributing.md) for more information on how we work, and about contributing in general. Please be aware that all interactions related to multiformats are subject to the IPFS [Code of Conduct](https://github.com/ipfs/community/blob/master/code-of-conduct.md).
|
134 |
|
135 | Small note: If editing the README, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification.
|
136 |
|
137 | ## License
|
138 |
|
139 | [MIT](LICENSE) © 2016 Protocol Labs Inc.
|