-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpg_connection_url_parser.ts
53 lines (43 loc) · 2.06 KB
/
pg_connection_url_parser.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
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { ConnectionUrlParser } from "../../common/lib/utils/connection_url_parser";
export class PgConnectionUrlParser extends ConnectionUrlParser {
/**
* The node-postgres library supports several formats of the connection URL.
* Some examples include:
* - /cloudsql/myproject:zone:mydb (UNIX socket domain)
* - postgresql://dbuser:[email protected]:3211/mydb (TCP connection)
* - example.aslfdewrlk.us-east-1.rds.amazonaws.com
*/
private static readonly PROTOCOL = "postgres://";
private static readonly TCP_CONNECTION_STRING_PATTERN = /postgres:\/\/(?:([^@\s]+)@)?(?<hosts>([^/\s]+))(?:\/(\w+))?(?:\?(.+))?/i;
getHostPortPairsFromUrl(initialConnection: string): string[] {
// The URL could either be the hostname, a UNIX socket domain or a TCP connection url.
if (initialConnection.startsWith("socket:")) {
// TODO: support UNIX domain socket?
throw new Error("UNIX domain socket url pattern unsupported");
}
if (!initialConnection.startsWith(PgConnectionUrlParser.PROTOCOL)) {
// If the URL doesn't start with either socket:// or postgres:// then it is a hostname.
return [initialConnection];
}
const match = initialConnection.match(PgConnectionUrlParser.TCP_CONNECTION_STRING_PATTERN);
if (match && match.groups) {
const hosts = match.groups["hosts"];
if (hosts) {
return hosts.trim().split(ConnectionUrlParser.HOST_SEPARATOR);
}
}
return [];
}
}