mirror of
https://github.com/fluencelabs/js-multiaddr-to-uri
synced 2024-12-04 10:30:34 +00:00
3021c00543
The upgrade to multiaddr 7 brings with it a change that means multiaddrs with libp2p peer ID keys like `/ipfs/QmXXX` will be transformed to `/p 2p/QmXXX` when converted from a string, to a multiaddr instance and back to a string. This is because `p2p` and `ipfs` are name aliases for the same codec. `p2p` has now become the default and older `ipfs` names do not round trip(they are converted to `p2p` - as stated above). This change allows us to use the new multiaddr 7 lib but retain backward compatibility by ensuring the protocol specified in a string multiaddr is the protocol used in the resulting URI. |
||
---|---|---|
.gitignore | ||
.travis.yml | ||
index.js | ||
LICENSE | ||
package-lock.json | ||
package.json | ||
README.md | ||
test.js |
multiaddr-to-uri
Convert a Multiaddr to a URI /dnsaddr/ipfs.io/http -> http://ipfs.io
Install
npm install multiaddr-to-uri
Usage
const toUri = require('multiaddr-to-uri')
console.log(toUri('/dnsaddr/protocol.ai/https'))
// -> https://protocol.ai
console.log(toUri('/ip4/127.0.0.1/tcp/8080'))
// -> http://127.0.0.1:8080
console.log(toUri('/ip4/127.0.0.1/tcp/8080', { assumeHttp: false }))
// -> tcp://127.0.0.1:8080
Note:
- When
/tcp
is the last (terminating) protocol HTTP is assumed by default (implicitassumeHttp: true
)- this means produced URIs will start with
http://
instead oftcp://
- passing
{ assumeHttp: false }
disables this behavior
- this means produced URIs will start with
- Might be lossy - e.g. a DNSv6 multiaddr
- Can throw if the passed multiaddr:
- is not a valid multiaddr
- is not supported as a URI e.g. circuit
Contribute
Feel free to dive in! Open an issue or submit PRs.
License
MIT © Alan Shaw