mirror of
https://github.com/fluencelabs/js-multiaddr-to-uri
synced 2024-12-03 22:00:23 +00:00
initial commit
This commit is contained in:
commit
f159234297
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
node_modules
|
||||
coverage
|
||||
.nyc_output
|
5
.travis.yml
Normal file
5
.travis.yml
Normal file
@ -0,0 +1,5 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- 6
|
||||
- 8
|
||||
- 9
|
21
LICENSE
Normal file
21
LICENSE
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2018 Alan Shaw
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
36
README.md
Normal file
36
README.md
Normal file
@ -0,0 +1,36 @@
|
||||
# multiaddr-to-stupid-string
|
||||
|
||||
[![Build Status](https://travis-ci.org/tableflip/multiaddr-to-stupid-string.svg?branch=master)](https://travis-ci.org/tableflip/multiaddr-to-stupid-string) [![dependencies Status](https://david-dm.org/tableflip/multiaddr-to-stupid-string/status.svg)](https://david-dm.org/tableflip/multiaddr-to-stupid-string) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
|
||||
|
||||
|
||||
> Convert a Multiaddr to a stupid string /dnsaddr/ipfs.io/http -> http://ipfs.io
|
||||
|
||||
## Install
|
||||
|
||||
```sh
|
||||
npm install multiaddr-to-stupid-string
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
const toStupid = require('multiaddr-to-stupid-string')
|
||||
|
||||
console.log(toStupid('/dnsaddr/protocol.ai/https'))
|
||||
// -> https://protocol.ai
|
||||
```
|
||||
|
||||
Note:
|
||||
|
||||
* Might be lossy - e.g. a DNSv6 multiaddr
|
||||
* Can throw if the passed multiaddr:
|
||||
* is not a valid multiaddr
|
||||
* is not supported as a stupid string e.g. circuit
|
||||
|
||||
## Contribute
|
||||
|
||||
Feel free to dive in! [Open an issue](https://github.com/tableflip/multiaddr-to-stupid-string/issues/new) or submit PRs.
|
||||
|
||||
## License
|
||||
|
||||
[MIT](LICENSE) © Alan Shaw
|
39
index.js
Normal file
39
index.js
Normal file
@ -0,0 +1,39 @@
|
||||
const Multiaddr = require('multiaddr')
|
||||
const reduceValue = (_, v) => v
|
||||
|
||||
const StupidReducers = {
|
||||
ip4: reduceValue,
|
||||
ip6: reduceValue,
|
||||
tcp: (str, content, i, parts) => (
|
||||
parts.some(p => ['http', 'https', 'ws', 'wss'].includes(p.protocol))
|
||||
? `${str}:${content}`
|
||||
: `tcp://${str}:${content}`
|
||||
),
|
||||
udp: (str, content) => `udp://${str}:${content}`,
|
||||
dnsaddr: reduceValue,
|
||||
dns4: reduceValue,
|
||||
dns6: reduceValue,
|
||||
ipfs: (str, content) => `${str}/ipfs/${content}`,
|
||||
p2p: (str, content) => `${str}/p2p/${content}`,
|
||||
http: str => `http://${str}`,
|
||||
https: str => `https://${str}`,
|
||||
ws: str => `ws://${str}`,
|
||||
wss: str => `wss://${str}`,
|
||||
'p2p-websocket-star': str => `${str}/p2p-websocket-star`,
|
||||
'p2p-webrtc-star': str => `${str}/p2p-webrtc-star`,
|
||||
'p2p-webrtc-direct': str => `${str}/p2p-webrtc-direct`
|
||||
}
|
||||
|
||||
module.exports = (multiaddr) => (
|
||||
Multiaddr(multiaddr)
|
||||
.stringTuples()
|
||||
.map(tuple => ({
|
||||
protocol: Multiaddr.protocols.codes[tuple[0]].name,
|
||||
content: tuple[1]
|
||||
}))
|
||||
.reduce((str, part, i, parts) => {
|
||||
const reduce = StupidReducers[part.protocol]
|
||||
if (!reduce) throw new Error(`Unsupported protocol ${part.protocol}`)
|
||||
return reduce(str, part.content, i, parts)
|
||||
}, '')
|
||||
)
|
8271
package-lock.json
generated
Normal file
8271
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
34
package.json
Normal file
34
package.json
Normal file
@ -0,0 +1,34 @@
|
||||
{
|
||||
"name": "multiaddr-to-stupid-string",
|
||||
"version": "0.0.0",
|
||||
"description": "Convert a Multiaddr to a stupid string /dnsaddr/ipfs.io/http -> http://ipfs.io",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "npm run lint && npm run test:coverage",
|
||||
"test:coverage": "nyc --reporter=lcov --reporter=text ava",
|
||||
"lint": "standard"
|
||||
},
|
||||
"keywords": [
|
||||
"multiaddr",
|
||||
"toString",
|
||||
"URL"
|
||||
],
|
||||
"author": "Alan Shaw",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"ava": "^0.25.0",
|
||||
"nyc": "^11.6.0",
|
||||
"standard": "^11.0.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"multiaddr": "^3.0.2"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/tableflip/multiaddr-to-stupid-string.git"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/tableflip/multiaddr-to-stupid-string/issues"
|
||||
},
|
||||
"homepage": "https://github.com/tableflip/multiaddr-to-stupid-string#readme"
|
||||
}
|
84
test.js
Normal file
84
test.js
Normal file
@ -0,0 +1,84 @@
|
||||
const test = require('ava')
|
||||
const toStupid = require('./')
|
||||
|
||||
test('should convert multiaddr to stupid', (t) => {
|
||||
const data = [
|
||||
['/ip4/127.0.0.1', '127.0.0.1'],
|
||||
['/ip4/127.0.0.1/http', 'http://127.0.0.1'],
|
||||
['/ip6/fc00::', 'fc00::'],
|
||||
['/ip6/fc00::/http', 'http://fc00::'],
|
||||
['/ip4/0.0.7.6/tcp/1234', 'tcp://0.0.7.6:1234'],
|
||||
['/ip6/::/tcp/0', 'tcp://:::0'],
|
||||
['/ip4/0.0.7.6/udp/1234', 'udp://0.0.7.6:1234'],
|
||||
['/ip6/::/udp/0', 'udp://:::0'],
|
||||
['/dnsaddr/ipfs.io', 'ipfs.io'],
|
||||
['/dns4/ipfs.io', 'ipfs.io'],
|
||||
['/dns4/libp2p.io', 'libp2p.io'],
|
||||
['/dns6/protocol.ai', 'protocol.ai'],
|
||||
['/dns4/protocol.ai/tcp/80', 'tcp://protocol.ai:80'],
|
||||
['/dns6/protocol.ai/tcp/80', 'tcp://protocol.ai:80'],
|
||||
['/dnsaddr/protocol.ai/tcp/80', 'tcp://protocol.ai:80'],
|
||||
['/dnsaddr/ipfs.io/ws', 'ws://ipfs.io'],
|
||||
['/dnsaddr/ipfs.io/http', 'http://ipfs.io'],
|
||||
['/dnsaddr/ipfs.io/https', 'https://ipfs.io'],
|
||||
['/ip4/1.2.3.4/tcp/3456/ws', 'ws://1.2.3.4:3456'],
|
||||
['/ip6/::/tcp/0/ws', 'ws://:::0'],
|
||||
['/dnsaddr/ipfs.io/wss', 'wss://ipfs.io'],
|
||||
['/ip4/1.2.3.4/tcp/3456/wss', 'wss://1.2.3.4:3456'],
|
||||
['/ip6/::/tcp/0/wss', 'wss://:::0'],
|
||||
[
|
||||
'/ip4/1.2.3.4/tcp/3456/ws/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo',
|
||||
'ws://1.2.3.4:3456/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo'
|
||||
],
|
||||
[
|
||||
'/dnsaddr/ipfs.io/ws/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
|
||||
'ws://ipfs.io/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'
|
||||
],
|
||||
[
|
||||
'/dnsaddr/ipfs.io/wss/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
|
||||
'wss://ipfs.io/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'
|
||||
],
|
||||
[
|
||||
'/ip6/::/tcp/0/ws/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo5',
|
||||
'ws://:::0/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo5'
|
||||
],
|
||||
[
|
||||
'/dns4/wrtc-star.discovery.libp2p.io/tcp/443/wss/p2p-webrtc-star/ipfs/QmTysQQiTGMdfRsDQp516oZ9bR3FiSCDnicUnqny2q1d79',
|
||||
'wss://wrtc-star.discovery.libp2p.io:443/p2p-webrtc-star/ipfs/QmTysQQiTGMdfRsDQp516oZ9bR3FiSCDnicUnqny2q1d79'
|
||||
],
|
||||
['/ip4/1.2.3.4/tcp/3456/http/p2p-webrtc-direct', 'http://1.2.3.4:3456/p2p-webrtc-direct'],
|
||||
['/ip6/::/tcp/0/http/p2p-webrtc-direct', 'http://:::0/p2p-webrtc-direct'],
|
||||
['/ip4/1.2.3.4/tcp/3456/ws/p2p-websocket-star', 'ws://1.2.3.4:3456/p2p-websocket-star'],
|
||||
['/ip6/::/tcp/0/ws/p2p-websocket-star', 'ws://:::0/p2p-websocket-star'],
|
||||
[
|
||||
'/dnsaddr/localhost/ws/p2p-websocket-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
|
||||
'ws://localhost/p2p-websocket-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'
|
||||
],
|
||||
[
|
||||
'/ip4/1.2.3.4/tcp/3456/ws/p2p-websocket-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
|
||||
'ws://1.2.3.4:3456/p2p-websocket-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'
|
||||
],
|
||||
[
|
||||
'/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star/ipfs/Qma3uqwymdqwXtC4uvmqqwwMhTDHD7xp9FzM75tQB5qRM3',
|
||||
'wss://ws-star.discovery.libp2p.io:443/p2p-websocket-star/ipfs/Qma3uqwymdqwXtC4uvmqqwwMhTDHD7xp9FzM75tQB5qRM3'
|
||||
],
|
||||
[
|
||||
'/ip4/127.0.0.1/tcp/20008/ws/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj',
|
||||
'ws://127.0.0.1:20008/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj'
|
||||
],
|
||||
[
|
||||
'/ip4/1.2.3.4/tcp/3456/ws/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
|
||||
'ws://1.2.3.4:3456/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'
|
||||
],
|
||||
[
|
||||
'/ip4/1.2.3.4/tcp/3456/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4',
|
||||
'tcp://1.2.3.4:3456/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4'
|
||||
]
|
||||
]
|
||||
|
||||
data.forEach(d => t.is(toStupid(d[0]), d[1]))
|
||||
})
|
||||
|
||||
test('should throw for unsupported protocol', (t) => {
|
||||
t.throws(() => toStupid('/quic'))
|
||||
})
|
Loading…
Reference in New Issue
Block a user