Skip to content

Commit d8431bf

Browse files
authored
fix: use codes instead of strings for dnsaddr detection (#259)
This was missed from #258
1 parent 45e6bf5 commit d8431bf

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

Diff for: src/index.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ const DNS_CODES = [
2020
getProtocol('dnsaddr').code
2121
]
2222

23+
const P2P_CODES = [
24+
getProtocol('p2p').code,
25+
getProtocol('ipfs').code
26+
]
27+
28+
const TCP_UDP_CODES = [
29+
getProtocol('tcp').code,
30+
getProtocol('udp').code
31+
]
32+
2333
export interface Protocol {
2434
code: number
2535
size: number
@@ -459,20 +469,20 @@ export class Multiaddr {
459469
const codes = this.protoCodes()
460470
const names = this.protoNames()
461471
const parts = this.toString().split('/').slice(1)
462-
let protocol = parts[2]
472+
let protocol = getProtocol(parts[2]).code
463473
let port = parseInt(parts[3])
464474

465475
// default to https when protocol & port are omitted from DNS addrs
466-
if (DNS_CODES.includes(codes[0]) && protocol === 'p2p') {
467-
protocol = 'tcp'
476+
if (DNS_CODES.includes(codes[0]) && P2P_CODES.includes(codes[1])) {
477+
protocol = getProtocol('tcp').code
468478
port = 443
469479
}
470480

471481
if (parts.length < 4) {
472482
throw new Error('multiaddr must have a valid format: "/{ip4, ip6, dns4, dns6, dnsaddr}/{address}/{tcp, udp}/{port}".')
473483
} else if (!IP_CODES.includes(codes[0]) && !DNS_CODES.includes(codes[0])) {
474484
throw new Error(`no protocol with name: "'${names[0]}'". Must have a valid family name: "{ip4, ip6, dns, dns4, dns6, dnsaddr}".`)
475-
} else if (protocol !== 'tcp' && protocol !== 'udp') {
485+
} else if (!TCP_UDP_CODES.includes(protocol)) {
476486
throw new Error(`no protocol with name: "'${names[1]}'". Must have a valid transport protocol: "{tcp, udp}".`)
477487
}
478488

0 commit comments

Comments
 (0)