Skip to content

Commit

Permalink
fix: constrain options transport to tcp or udp (#396)
Browse files Browse the repository at this point in the history
Update the types to express that the `transport` field will only
ever be `'tcp'` or `'udp'`.
  • Loading branch information
achingbrain authored Feb 5, 2025
1 parent 5a9d33c commit 8d5b325
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export interface Protocol {
export interface MultiaddrObject {
family: 4 | 6
host: string
transport: string
transport: 'tcp' | 'udp'
port: number
}

Expand Down
9 changes: 5 additions & 4 deletions src/multiaddr.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable complexity */
import { base58btc } from 'multiformats/bases/base58'
import { CID } from 'multiformats/cid'
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
Expand Down Expand Up @@ -73,7 +74,7 @@ export class Multiaddr implements MultiaddrInterface {

toOptions (): MultiaddrObject {
let family: 4 | 6 | undefined
let transport: string | undefined
let transport: 'tcp' | 'udp' | undefined
let host: string | undefined
let port: number | undefined
let zone = ''
Expand All @@ -92,19 +93,19 @@ export class Multiaddr implements MultiaddrInterface {

// default to https when protocol & port are omitted from DNS addrs
if (DNS_CODES.includes(code)) {
transport = tcp.name
transport = tcp.name === 'tcp' ? 'tcp' : 'udp'
port = 443
host = `${value ?? ''}${zone}`
family = code === dns6.code ? 6 : 4
}

if (code === tcp.code || code === udp.code) {
transport = getProtocol(code).name
transport = getProtocol(code).name === 'tcp' ? 'tcp' : 'udp'
port = parseInt(value ?? '')
}

if (code === ip4.code || code === ip6.code) {
transport = getProtocol(code).name
transport = getProtocol(code).name === 'tcp' ? 'tcp' : 'udp'
host = `${value ?? ''}${zone}`
family = code === ip6.code ? 6 : 4
}
Expand Down

0 comments on commit 8d5b325

Please sign in to comment.