1414
1515import { InstanceConnectionInfo } from './instance-connection-info' ;
1616import { 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+ }
1794
1895export function parseInstanceConnectionName (
1996 instanceConnectionName : string | undefined
@@ -26,20 +103,8 @@ export function parseInstanceConnectionName(
26103 } ) ;
27104 }
28105
29- const connectionNameRegex =
30- / (?< projectId > [ ^ : ] + ( : [ ^ : ] + ) ? ) : (?< regionId > [ ^ : ] + ) : (?< instanceId > [ ^ : ] + ) / ;
31106 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 ) {
43108 throw new CloudSQLConnectorError ( {
44109 message :
45110 'Malformed instance connection name provided: expected format ' +
@@ -52,5 +117,6 @@ export function parseInstanceConnectionName(
52117 projectId : matches . groups . projectId ,
53118 regionId : matches . groups . regionId ,
54119 instanceId : matches . groups . instanceId ,
120+ domainName : undefined ,
55121 } ;
56122}
0 commit comments