/
endpoint.ts
81 lines (71 loc) · 2.3 KB
/
endpoint.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { Token } from '@aws-cdk/core';
/**
* Connection endpoint of a database cluster or instance
*
* Consists of a combination of hostname and port.
*/
export class Endpoint {
/**
* The minimum port value
*/
private static readonly MIN_PORT = 1;
/**
* The maximum port value
*/
private static readonly MAX_PORT = 65535;
/**
* Determines if a port is valid
*
* @param port: The port number
* @returns boolean whether the port is valid
*/
private static isValidPort(port: number): boolean {
return Number.isInteger(port) && port >= Endpoint.MIN_PORT && port <= Endpoint.MAX_PORT;
}
/**
* The hostname of the endpoint
*/
public readonly hostname: string;
/**
* The port number of the endpoint.
*
* This can potentially be a CDK token. If you need to embed the port in a string (e.g. instance user data script),
* use {@link Endpoint.portAsString}.
*/
public readonly port: number;
/**
* The combination of ``HOSTNAME:PORT`` for this endpoint.
*/
public readonly socketAddress: string;
/**
* Constructs an Endpoint instance.
*
* @param address - The hostname or address of the endpoint
* @param port - The port number of the endpoint
*/
constructor(address: string, port: number) {
if (!Token.isUnresolved(port) && !Endpoint.isValidPort(port)) {
throw new Error(`Port must be an integer between [${Endpoint.MIN_PORT}, ${Endpoint.MAX_PORT}] but got: ${port}`);
}
this.hostname = address;
this.port = port;
const portDesc = Token.isUnresolved(port) ? Token.asString(port) : port;
this.socketAddress = `${address}:${portDesc}`;
}
/**
* Returns the port number as a string representation that can be used for embedding within other strings.
*
* This is intended to deal with CDK's token system. Numeric CDK tokens are not expanded when their string
* representation is embedded in a string. This function returns the port either as an unresolved string token or
* as a resolved string representation of the port value.
*
* @returns {string} An (un)resolved string representation of the endpoint's port number
*/
public portAsString(): string {
if (Token.isUnresolved(this.port)) {
return Token.asString(this.port);
} else {
return this.port.toString();
}
}
}