Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

Commit

Permalink
fix: use valid rfc3986 ipv6 host syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtPooki committed Mar 23, 2023
1 parent 1b8a242 commit da150aa
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,22 @@ function parseMultiaddr (ma: Multiaddr): { url: string, certhashes: MultihashDig
// eslint-disable-next-line complexity
const { url, certhashes, remotePeer } = parts.reduce((state: { url: string, certhashes: MultihashDigest[], seenHost: boolean, seenPort: boolean, remotePeer?: PeerId }, [proto, value]) => {
switch (proto) {
case protocols('ip4').code:
case protocols('ip6').code:
case protocols('dns4').code:
// @ts-ignore - ignore fallthrough
case protocols('dns6').code:
if (value?.includes(':')) {
/**
* This resolves cases where `new globalThis.WebTransport` fails to construct because of an invalid URL being passed.
*
* `new URL('https://::1:4001/blah')` will throw a `TypeError: Failed to construct 'URL': Invalid URL`
* `new URL('https://[::1]:4001/blah')` is valid and will not.
*
* @see https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2
*/
value = `[${value}]`
}
case protocols('ip4').code:
case protocols('dns4').code:
if (state.seenHost || state.seenPort) {
throw new Error('Invalid multiaddr, saw host and already saw the host or port')
}
Expand Down

0 comments on commit da150aa

Please sign in to comment.