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 (!address1 && !address2) {
|
48 | return true;
|
49 | }
|
50 | if (!address1 || !address2) {
|
51 | return false;
|
52 | }
|
53 | if (isTcpSubchannelAddress(address1)) {
|
54 | return (
|
55 | isTcpSubchannelAddress(address2) &&
|
56 | address1.host === address2.host &&
|
57 | address1.port === address2.port
|
58 | );
|
59 | } else {
|
60 | return !isTcpSubchannelAddress(address2) && address1.path === address2.path;
|
61 | }
|
62 | }
|
63 |
|
64 | export function subchannelAddressToString(address: SubchannelAddress): string {
|
65 | if (isTcpSubchannelAddress(address)) {
|
66 | return address.host + ':' + address.port;
|
67 | } else {
|
68 | return address.path;
|
69 | }
|
70 | }
|
71 |
|
72 | const DEFAULT_PORT = 443;
|
73 |
|
74 | export function stringToSubchannelAddress(addressString: string, port?: number): SubchannelAddress {
|
75 | if (isIP(addressString)) {
|
76 | return {
|
77 | host: addressString,
|
78 | port: port ?? DEFAULT_PORT
|
79 | };
|
80 | } else {
|
81 | return {
|
82 | path: addressString
|
83 | };
|
84 | }
|
85 | } |
\ | No newline at end of file |