1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | import { isIP } from "net";
|
19 |
|
20 | export interface TcpSubchannelAddress {
|
21 | port: number;
|
22 | host: string;
|
23 | }
|
24 |
|
25 | export interface IpcSubchannelAddress {
|
26 | path: string;
|
27 | }
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | export type SubchannelAddress = TcpSubchannelAddress | IpcSubchannelAddress;
|
36 |
|
37 | export function isTcpSubchannelAddress(
|
38 | address: SubchannelAddress
|
39 | ): address is TcpSubchannelAddress {
|
40 | return 'port' in address;
|
41 | }
|
42 |
|
43 | export function subchannelAddressEqual(
|
44 | address1: SubchannelAddress,
|
45 | address2: SubchannelAddress
|
46 | ): boolean {
|
47 | if (isTcpSubchannelAddress(address1)) {
|
48 | return (
|
49 | isTcpSubchannelAddress(address2) &&
|
50 | address1.host === address2.host &&
|
51 | address1.port === address2.port
|
52 | );
|
53 | } else {
|
54 | return !isTcpSubchannelAddress(address2) && address1.path === address2.path;
|
55 | }
|
56 | }
|
57 |
|
58 | export function subchannelAddressToString(address: SubchannelAddress): string {
|
59 | if (isTcpSubchannelAddress(address)) {
|
60 | return address.host + ':' + address.port;
|
61 | } else {
|
62 | return address.path;
|
63 | }
|
64 | }
|
65 |
|
66 | const DEFAULT_PORT = 443;
|
67 |
|
68 | export function stringToSubchannelAddress(addressString: string, port?: number): SubchannelAddress {
|
69 | if (isIP(addressString)) {
|
70 | return {
|
71 | host: addressString,
|
72 | port: port ?? DEFAULT_PORT
|
73 | };
|
74 | } else {
|
75 | return {
|
76 | path: addressString
|
77 | };
|
78 | }
|
79 | } |
\ | No newline at end of file |