Skip to content

feat: support of http-path multiaddresses #116

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

Merged
merged 20 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
40 changes: 32 additions & 8 deletions lib/multiaddr.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,32 @@
* @returns {string} Parsed URI, e.g. `http://127.0.0.1:80`
*/
export function multiaddrToHttpUrl (addr) {
const [, hostType, hostValue, ipProtocol, port, scheme, ...rest] = addr.split('/')
const [multiAddr, httpPathMultiAddr] = addr.split('/http-path/')
const [, hostType, hostValue, ...multiAddrParts] = multiAddr.split('/')
let scheme, path, rest
if (httpPathMultiAddr) {
scheme = multiAddrParts.shift()
rest = multiAddrParts
path = decodeURIComponent(httpPathMultiAddr)
if (!path) {
throw Object.assign(
new Error(`Cannot parse "${addr}": http-path is empty`),
{ code: 'MULTIADDR_HAS_EMPTY_HTTP_PATH' }
)
}
} else {
const ipProtocol = multiAddrParts.shift()
const port = multiAddrParts.shift()
scheme = multiAddrParts.shift()
rest = multiAddrParts

if (ipProtocol !== 'tcp') {
throw Object.assign(
new Error(`Cannot parse "${addr}": unsupported protocol "${ipProtocol}"`),
{ code: 'UNSUPPORTED_MULTIADDR_PROTO' }
)
if (ipProtocol !== 'tcp') {
throw Object.assign(
new Error(`Cannot parse "${addr}": unsupported protocol "${ipProtocol}"`),
{ code: 'UNSUPPORTED_MULTIADDR_PROTO' }
)
}
path = getUriPort(scheme, port)
}

if (scheme !== 'http' && scheme !== 'https') {
Expand All @@ -25,8 +44,13 @@ export function multiaddrToHttpUrl (addr) {
{ code: 'MULTIADDR_HAS_TOO_MANY_PARTS' }
)
}

return `${scheme}://${getUriHost(hostType, hostValue)}${getUriPort(scheme, port)}`
if (scheme !== 'http' && scheme !== 'https') {
throw Object.assign(
new Error(`Cannot parse "${addr}": unsupported scheme "${scheme}"`),
{ code: 'UNSUPPORTED_MULTIADDR_SCHEME' }
)
}
return `${scheme}://${getUriHost(hostType, hostValue)}${path}`
}

function getUriHost (hostType, hostValue) {
Expand Down
3 changes: 2 additions & 1 deletion test/multiaddr.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const HAPPY_CASES = [
['/ip4/127.0.0.1/tcp/8080/https', 'https://127.0.0.1:8080'],
['/dns/meridian.space/tcp/8080/http', 'http://meridian.space:8080'],
['/dns4/meridian.space/tcp/8080/http', 'http://meridian.space:8080'],
['/dns6/meridian.space/tcp/8080/http', 'http://meridian.space:8080']
['/dns6/meridian.space/tcp/8080/http', 'http://meridian.space:8080'],
['/dns/meridian.space/https/http-path/%2Fipni-provider%2F12D3KooWJ91c8xqshrNe7QAXPFAaeRr0Wq2UrgXGPf8UmMZMwyZ5', 'https://meridian.space/ipni-provider/12D3KooWJ91c8xqshrNe7QAXPFAaeRr0Wq2UrgXGPf8UmMZMwyZ5']
]

for (const [multiaddr, expectedUri] of HAPPY_CASES) {
Expand Down