Skip to content

fix!: simplify API #427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 4 additions & 78 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![codecov](https://img.shields.io/codecov/c/github/multiformats/js-multiaddr.svg?style=flat-square)](https://codecov.io/gh/multiformats/js-multiaddr)
[![CI](https://img.shields.io/github/actions/workflow/status/multiformats/js-multiaddr/js-test-and-release.yml?branch=main\&style=flat-square)](https://github.com/multiformats/js-multiaddr/actions/workflows/js-test-and-release.yml?query=branch%3Amain)

> multiaddr implementation (binary + string representation of network addresses)
> The JavaScript implementation of the Multiaddr spec

# About

Expand All @@ -26,7 +26,6 @@ repo and examine the changes made.
A standard way to represent addresses that

- support any standard network protocol
- are self-describing
- have a binary packed format
- have a nice string representation
- encapsulate well
Expand All @@ -45,75 +44,16 @@ addr.bytes
addr.toString()
// '/ip4/127.0.0.1/udp/1234'

addr.protos()
addr.getComponents()
// [
// {code: 4, name: 'ip4', size: 32},
// {code: 273, name: 'udp', size: 16}
// { code: 4, name: 'ip4', value: '127.0.0.1' },
// { code: 273, name: 'udp', value: '1234' }
// ]

// gives you an object that is friendly with what Node.js core modules expect for addresses
addr.nodeAddress()
// {
// family: 4,
// port: 1234,
// address: "127.0.0.1"
// }

addr.encapsulate('/sctp/5678')
// Multiaddr(/ip4/127.0.0.1/udp/1234/sctp/5678)
```

## Resolving DNSADDR addresses

[DNSADDR](https://github.com/multiformats/multiaddr/blob/master/protocols/DNSADDR.md) is a spec that allows storing a TXT DNS record that contains a Multiaddr.

To resolve DNSADDR addresses, call the `.resolve()` function the multiaddr, optionally passing a `DNS` resolver.

DNSADDR addresses can resolve to multiple multiaddrs, since there is no limit to the number of TXT records that can be stored.

## Example - Resolving DNSADDR Multiaddrs

```TypeScript
import { multiaddr, resolvers } from '@multiformats/multiaddr'
import { dnsaddrResolver } from '@multiformats/multiaddr/resolvers'

resolvers.set('dnsaddr', dnsaddrResolver)

const ma = multiaddr('/dnsaddr/bootstrap.libp2p.io')

// resolve with a 5s timeout
const resolved = await ma.resolve({
signal: AbortSignal.timeout(5000)
})

console.info(resolved)
// [Multiaddr('/ip4/147.75...'), Multiaddr('/ip4/147.75...'), Multiaddr('/ip4/147.75...')...]
```

## Example - Using a custom DNS resolver to resolve DNSADDR Multiaddrs

See the docs for [@multiformats/dns](https://www.npmjs.com/package/@multiformats/dns) for a full breakdown of how to specify multiple resolvers or resolvers that can be used for specific TLDs.

```TypeScript
import { multiaddr } from '@multiformats/multiaddr'
import { dns } from '@multiformats/dns'
import { dnsJsonOverHttps } from '@multiformats/dns/resolvers'

const resolver = dns({
resolvers: {
'.': dnsJsonOverHttps('https://cloudflare-dns.com/dns-query')
}
})

const ma = multiaddr('/dnsaddr/bootstrap.libp2p.io')
const resolved = await ma.resolve({
dns: resolver
})

console.info(resolved)
// [Multiaddr('/ip4/147.75...'), Multiaddr('/ip4/147.75...'), Multiaddr('/ip4/147.75...')...]
```

## Example - Adding custom protocols

To add application-specific or experimental protocols, add a protocol codec
Expand Down Expand Up @@ -145,20 +85,6 @@ multiaddr(maWithCustomTuple)
registry.removeProtocol(protocol.code)
```

# Install

```console
$ npm i @multiformats/multiaddr
```

## Browser `<script>` tag

Loading this module through a script tag will make its exports available as `MultiformatsMultiaddr` in the global namespace.

```html
<script src="https://unpkg.com/@multiformats/multiaddr/dist/index.min.js"></script>
```

# API Docs

- <https://multiformats.github.io/js-multiaddr>
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/operations/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"devDependencies": {
"@multiformats/multiaddr-12.4.0": "npm:@multiformats/[email protected]",
"@multiformats/multiaddr": "../../",
"aegir": "^47.0.7",
"aegir": "^47.0.16",
"tinybench": "^4.0.1"
}
}
34 changes: 2 additions & 32 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@multiformats/multiaddr",
"version": "12.5.1",
"description": "multiaddr implementation (binary + string representation of network addresses)",
"description": "The JavaScript implementation of the Multiaddr spec",
"license": "Apache-2.0 OR MIT",
"homepage": "https://github.com/multiformats/js-multiaddr#readme",
"repository": {
Expand All @@ -22,22 +22,6 @@
],
"type": "module",
"types": "./dist/src/index.d.ts",
"typesVersions": {
"*": {
"*": [
"*",
"dist/*",
"dist/src/*",
"dist/src/*/index"
],
"src/*": [
"*",
"dist/*",
"dist/src/*",
"dist/src/*/index"
]
}
},
"files": [
"src",
"dist",
Expand All @@ -48,14 +32,6 @@
".": {
"types": "./dist/src/index.d.ts",
"import": "./dist/src/index.js"
},
"./convert": {
"types": "./dist/src/convert.d.ts",
"import": "./dist/src/convert.js"
},
"./resolvers": {
"types": "./dist/src/resolvers/index.d.ts",
"import": "./dist/src/resolvers/index.js"
}
},
"release": {
Expand Down Expand Up @@ -169,18 +145,12 @@
},
"dependencies": {
"@chainsafe/is-ip": "^2.0.1",
"@chainsafe/netmask": "^2.0.0",
"@multiformats/dns": "^1.0.3",
"abort-error": "^1.0.1",
"multiformats": "^13.0.0",
"uint8-varint": "^2.0.1",
"uint8arrays": "^5.0.0"
},
"devDependencies": {
"@types/sinon": "^17.0.2",
"aegir": "^47.0.16",
"sinon": "^21.0.0",
"sinon-ts": "^2.0.0"
"aegir": "^47.0.16"
},
"browser": {
"./dist/src/resolvers/dns.js": "./dist/src/resolvers/dns.browser.js"
Expand Down
62 changes: 0 additions & 62 deletions src/convert.ts

This file was deleted.

47 changes: 0 additions & 47 deletions src/filter/multiaddr-filter.ts

This file was deleted.

Loading