Skip to content

Commit

Permalink
feat!: convert to interface (#274)
Browse files Browse the repository at this point in the history
- Renames the `Multiaddr` class to `DefaultMultiaddr`
- Exports `Multiaddr` as an interface

The `multiaddr` function (introduced in v9.0.0) should be used to create new instances of `Multiaddr` instead of instantiating the `Multiaddr` class directly.

This allows us to remove the `inspect` symbol from the interface and avoid use of `Object.defineProperty` which is very slow.

Fixes: #202

BREAKING CHANGE: the `Multiaddr` class is now an interface
  • Loading branch information
achingbrain authored Sep 21, 2022
1 parent 413155e commit 36de98f
Show file tree
Hide file tree
Showing 4 changed files with 481 additions and 448 deletions.
18 changes: 4 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
## Table of contents <!-- omit in toc -->

- [Install](#install)
- [Lead Maintainer](#lead-maintainer)
- [Background](#background)
- [What is multiaddr?](#what-is-multiaddr)
- [Browser: `<script>` Tag](#browser-script-tag)
Expand All @@ -25,10 +24,6 @@
$ npm i @multiformats/multiaddr
```

## Lead Maintainer

[Jacob Heun](https://github.com/jacobheun)

## Background

### What is multiaddr?
Expand Down Expand Up @@ -57,15 +52,11 @@ the global namespace.
## Usage

```js
// if we are coming from <= 8.x you can use the factory function
const { multiaddr } = require('multiaddr')
import { multiaddr } from 'multiaddr'
const addr = multiaddr("/ip4/127.0.0.1/udp/1234")
// <Multiaddr /ip4/127.0.0.1/udp/1234>

// or just the class directly
const { Multiaddr } = require('multiaddr')

const addr = new Multiaddr("/ip4/127.0.0.1/udp/1234")
const addr = multiaddr("/ip4/127.0.0.1/udp/1234")
// <Multiaddr /ip4/127.0.0.1/udp/1234>

addr.bytes
Expand Down Expand Up @@ -106,10 +97,9 @@ addr.encapsulate('/sctp/5678')
To provide multiaddr resolvers you can do:

```js
const { Multiaddr } = require('multiaddr')
const resolvers = require('multiaddr/src/resolvers')
import { resolvers } from 'multiaddr'

Multiaddr.resolvers.set('dnsaddr', resolvers.dnsaddrResolver)
resolvers.set('dnsaddr', resolvers.dnsaddrResolver)
```

The available resolvers are:
Expand Down
Loading

0 comments on commit 36de98f

Please sign in to comment.