Skip to content

Commit ec7dfd3

Browse files
committed
fix(Transports): Allow for number as port
1 parent c84d279 commit ec7dfd3

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

src/base/Transports.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import {tcpAddress} from './Transports'
2+
3+
describe('tcpAddress', () => {
4+
5+
const defaults = {
6+
host: '127.0.0.1',
7+
port: 4321
8+
}
9+
10+
test('parses a string with host and port', () => {
11+
expect(tcpAddress('example.com:2010', defaults)).toEqual({
12+
host: 'example.com',
13+
port: 2010
14+
})
15+
})
16+
17+
test('parses a number as the port', () => {
18+
expect(tcpAddress(2020, defaults)).toEqual({
19+
host: '127.0.0.1',
20+
port: 2020
21+
})
22+
})
23+
24+
test('parses a string with just port', () => {
25+
expect(tcpAddress('2030', defaults)).toEqual({
26+
host: '127.0.0.1',
27+
port: 2030
28+
})
29+
})
30+
31+
})

src/base/Transports.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export interface TcpAddress {
3232
}
3333

3434
export function tcpAddress(
35-
address: undefined | string | Omit<TcpAddress, 'type'>,
35+
address: undefined | string | number | Omit<TcpAddress, 'type'>,
3636
defaults: {
3737
host: string
3838
port: number
@@ -55,6 +55,8 @@ export function tcpAddress(
5555
} else {
5656
return defaults
5757
}
58+
} else if (typeof address === 'number') {
59+
return { host: defaults.host, port: address}
5860
} else {
5961
const { host = defaults.host, port = defaults.port } = address
6062
return { host, port }

0 commit comments

Comments
 (0)