Skip to content

Commit 8d5b325

Browse files
authored
fix: constrain options transport to tcp or udp (#396)
Update the types to express that the `transport` field will only ever be `'tcp'` or `'udp'`.
1 parent 5a9d33c commit 8d5b325

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export interface Protocol {
115115
export interface MultiaddrObject {
116116
family: 4 | 6
117117
host: string
118-
transport: string
118+
transport: 'tcp' | 'udp'
119119
port: number
120120
}
121121

src/multiaddr.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable complexity */
12
import { base58btc } from 'multiformats/bases/base58'
23
import { CID } from 'multiformats/cid'
34
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
@@ -73,7 +74,7 @@ export class Multiaddr implements MultiaddrInterface {
7374

7475
toOptions (): MultiaddrObject {
7576
let family: 4 | 6 | undefined
76-
let transport: string | undefined
77+
let transport: 'tcp' | 'udp' | undefined
7778
let host: string | undefined
7879
let port: number | undefined
7980
let zone = ''
@@ -92,19 +93,19 @@ export class Multiaddr implements MultiaddrInterface {
9293

9394
// default to https when protocol & port are omitted from DNS addrs
9495
if (DNS_CODES.includes(code)) {
95-
transport = tcp.name
96+
transport = tcp.name === 'tcp' ? 'tcp' : 'udp'
9697
port = 443
9798
host = `${value ?? ''}${zone}`
9899
family = code === dns6.code ? 6 : 4
99100
}
100101

101102
if (code === tcp.code || code === udp.code) {
102-
transport = getProtocol(code).name
103+
transport = getProtocol(code).name === 'tcp' ? 'tcp' : 'udp'
103104
port = parseInt(value ?? '')
104105
}
105106

106107
if (code === ip4.code || code === ip6.code) {
107-
transport = getProtocol(code).name
108+
transport = getProtocol(code).name === 'tcp' ? 'tcp' : 'udp'
108109
host = `${value ?? ''}${zone}`
109110
family = code === ip6.code ? 6 : 4
110111
}

0 commit comments

Comments
 (0)