14
14
15
15
import { InstanceConnectionInfo } from './instance-connection-info' ;
16
16
import { CloudSQLConnectorError } from './errors' ;
17
+ import { resolveTxtRecord } from './dns-lookup' ;
18
+
19
+ export function isSameInstance (
20
+ a : InstanceConnectionInfo ,
21
+ b : InstanceConnectionInfo
22
+ ) : boolean {
23
+ return (
24
+ a . instanceId === b . instanceId &&
25
+ a . regionId === b . regionId &&
26
+ a . projectId === b . projectId &&
27
+ a . domainName === b . domainName
28
+ ) ;
29
+ }
30
+
31
+ export async function resolveInstanceName (
32
+ instanceConnectionName ?: string ,
33
+ domainName ?: string
34
+ ) : Promise < InstanceConnectionInfo > {
35
+ if ( ! instanceConnectionName && ! domainName ) {
36
+ throw new CloudSQLConnectorError ( {
37
+ message :
38
+ 'Missing instance connection name, expected: "PROJECT:REGION:INSTANCE" or a valid domain name.' ,
39
+ code : 'ENOCONNECTIONNAME' ,
40
+ } ) ;
41
+ } else if (
42
+ instanceConnectionName &&
43
+ isInstanceConnectionName ( instanceConnectionName )
44
+ ) {
45
+ return parseInstanceConnectionName ( instanceConnectionName ) ;
46
+ } else if ( domainName && isValidDomainName ( domainName ) ) {
47
+ return await resolveDomainName ( domainName ) ;
48
+ } else {
49
+ throw new CloudSQLConnectorError ( {
50
+ message :
51
+ 'Malformed Instance connection name, expected an instance connection name in the form "PROJECT:REGION:INSTANCE" or a valid domain name' ,
52
+ code : 'EBADCONNECTIONNAME' ,
53
+ } ) ;
54
+ }
55
+ }
56
+
57
+ const connectionNameRegex =
58
+ / ^ (?< projectId > [ ^ : ] + ( : [ ^ : ] + ) ? ) : (?< regionId > [ ^ : ] + ) : (?< instanceId > [ ^ : ] + ) $ / ;
59
+
60
+ // The domain name pattern in accordance with RFC 1035, RFC 1123 and RFC 2181.
61
+ // From Go Connector:
62
+ const domainNameRegex =
63
+ / ^ (?: [ _ a - z 0 - 9 ] (?: [ _ a - z 0 - 9 - ] { 0 , 61 } [ a - z 0 - 9 ] ) ? \. ) + (?: [ a - z ] (?: [ a - z 0 - 9 - ] { 0 , 61 } [ a - z 0 - 9 ] ) ? ) ? $ / ;
64
+
65
+ export function isValidDomainName ( name : string ) : boolean {
66
+ const matches = String ( name ) . match ( domainNameRegex ) ;
67
+ return Boolean ( matches ) ;
68
+ }
69
+
70
+ export function isInstanceConnectionName ( name : string ) : boolean {
71
+ const matches = String ( name ) . match ( connectionNameRegex ) ;
72
+ return Boolean ( matches ) ;
73
+ }
74
+
75
+ export async function resolveDomainName (
76
+ name : string
77
+ ) : Promise < InstanceConnectionInfo > {
78
+ const icn = await resolveTxtRecord ( name ) ;
79
+ if ( ! isInstanceConnectionName ( icn ) ) {
80
+ throw new CloudSQLConnectorError ( {
81
+ message :
82
+ 'Malformed instance connection name returned for domain ' +
83
+ name +
84
+ ' : ' +
85
+ icn ,
86
+ code : 'EBADDOMAINCONNECTIONNAME' ,
87
+ } ) ;
88
+ }
89
+
90
+ const info = parseInstanceConnectionName ( icn ) ;
91
+ info . domainName = name ;
92
+ return info ;
93
+ }
17
94
18
95
export function parseInstanceConnectionName (
19
96
instanceConnectionName : string | undefined
@@ -26,20 +103,8 @@ export function parseInstanceConnectionName(
26
103
} ) ;
27
104
}
28
105
29
- const connectionNameRegex =
30
- / (?< projectId > [ ^ : ] + ( : [ ^ : ] + ) ? ) : (?< regionId > [ ^ : ] + ) : (?< instanceId > [ ^ : ] + ) / ;
31
106
const matches = String ( instanceConnectionName ) . match ( connectionNameRegex ) ;
32
- if ( ! matches ) {
33
- throw new CloudSQLConnectorError ( {
34
- message :
35
- 'Malformed instance connection name provided: expected format ' +
36
- `of "PROJECT:REGION:INSTANCE", got ${ instanceConnectionName } ` ,
37
- code : 'EBADCONNECTIONNAME' ,
38
- } ) ;
39
- }
40
-
41
- const unmatchedItems = matches [ 0 ] !== matches . input ;
42
- if ( unmatchedItems || ! matches . groups ) {
107
+ if ( ! matches || ! matches . groups ) {
43
108
throw new CloudSQLConnectorError ( {
44
109
message :
45
110
'Malformed instance connection name provided: expected format ' +
@@ -52,5 +117,6 @@ export function parseInstanceConnectionName(
52
117
projectId : matches . groups . projectId ,
53
118
regionId : matches . groups . regionId ,
54
119
instanceId : matches . groups . instanceId ,
120
+ domainName : undefined ,
55
121
} ;
56
122
}
0 commit comments