From de1d31cbff48d26d5f1bc3b78a093ba01af349a9 Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Tue, 6 Sep 2022 15:29:37 +0200 Subject: [PATCH] fix: dedupe toOptions/nodeAddress code (#266) Let one method parse the multiaddr details and call it from the other. --- src/index.ts | 35 ++++++----------------------------- 1 file changed, 6 insertions(+), 29 deletions(-) diff --git a/src/index.ts b/src/index.ts index 0ba8a868..bbbde4a5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,10 +9,6 @@ import { equals as uint8ArrayEquals } from 'uint8arrays/equals' const inspect = Symbol.for('nodejs.util.inspect.custom') -const IP_CODES = [ - getProtocol('ip4').code, - getProtocol('ip6').code -] const DNS_CODES = [ getProtocol('dns').code, getProtocol('dns4').code, @@ -25,11 +21,6 @@ const P2P_CODES = [ getProtocol('ipfs').code ] -const TCP_UDP_CODES = [ - getProtocol('tcp').code, - getProtocol('udp').code -] - export interface Protocol { code: number size: number @@ -485,30 +476,16 @@ export class Multiaddr { * ``` */ nodeAddress (): NodeAddress { - const codes = this.protoCodes() - const names = this.protoNames() - const parts = this.toString().split('/').slice(1) - let protocol = getProtocol(parts[2]).code - let port = parseInt(parts[3]) + const options = this.toOptions() - // default to https when protocol & port are omitted from DNS addrs - if (DNS_CODES.includes(codes[0]) && P2P_CODES.includes(codes[1])) { - protocol = getProtocol('tcp').code - port = 443 - } - - if (parts.length < 4) { - throw new Error('multiaddr must have a valid format: "/{ip4, ip6, dns4, dns6, dnsaddr}/{address}/{tcp, udp}/{port}".') - } else if (!IP_CODES.includes(codes[0]) && !DNS_CODES.includes(codes[0])) { - throw new Error(`no protocol with name: "'${names[0]}'". Must have a valid family name: "{ip4, ip6, dns, dns4, dns6, dnsaddr}".`) - } else if (!TCP_UDP_CODES.includes(protocol)) { - throw new Error(`no protocol with name: "'${names[1]}'". Must have a valid transport protocol: "{tcp, udp}".`) + if (options.transport !== 'tcp' && options.transport !== 'udp') { + throw new Error(`multiaddr must have a valid format - no protocol with name: "${options.transport}". Must have a valid transport protocol: "{tcp, udp}"`) } return { - family: (codes[0] === 41 || codes[0] === 55) ? 6 : 4, - address: parts[1], - port // tcp or udp port + family: options.family, + address: options.host, + port: options.port } }