Skip to content

Commit 4aff01e

Browse files
committed
Add 'packages/pg-connection-string/' from commit '5233b3e77e396a368130709e762fca836290a528'
git-subtree-dir: packages/pg-connection-string git-subtree-mainline: 3532880 git-subtree-split: 5233b3e
2 parents 3532880 + 5233b3e commit 4aff01e

File tree

12 files changed

+527
-0
lines changed

12 files changed

+527
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
service_name: travis-pro
2+
repo_token: 5F6dODinz9L9uFR6HatKmtsYDoV1A5S2N
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Logs
2+
logs
3+
*.log
4+
5+
# Runtime data
6+
pids
7+
*.pid
8+
*.seed
9+
10+
# Directory for instrumented libs generated by jscoverage/JSCover
11+
lib-cov
12+
13+
# Coverage directory used by tools like istanbul
14+
coverage
15+
16+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
17+
.grunt
18+
19+
# Compiled binary addons (http://nodejs.org/api/addons.html)
20+
build/Release
21+
22+
# Dependency directory
23+
# Deployed apps should consider commenting this line out:
24+
# see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git
25+
node_modules
26+
package-lock.json
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
language: node_js
2+
node_js:
3+
- '0.10'
4+
- '6.9'
5+
- '8'
6+
after_success: 'npm run coveralls'

packages/pg-connection-string/LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014 Iced Development
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
pg-connection-string
2+
====================
3+
4+
[![NPM](https://nodei.co/npm/pg-connection-string.png?compact=true)](https://nodei.co/npm/pg-connection-string/)
5+
6+
[![Build Status](https://travis-ci.org/iceddev/pg-connection-string.svg?branch=master)](https://travis-ci.org/iceddev/pg-connection-string)
7+
[![Coverage Status](https://coveralls.io/repos/github/iceddev/pg-connection-string/badge.svg?branch=master)](https://coveralls.io/github/iceddev/pg-connection-string?branch=master)
8+
9+
Functions for dealing with a PostgresSQL connection string
10+
11+
`parse` method taken from [node-postgres](https://github.com/brianc/node-postgres.git)
12+
Copyright (c) 2010-2014 Brian Carlson (brian.m.carlson@gmail.com)
13+
MIT License
14+
15+
## Usage
16+
17+
```js
18+
var parse = require('pg-connection-string').parse;
19+
20+
var config = parse('postgres://someuser:somepassword@somehost:381/somedatabase')
21+
```
22+
23+
The resulting config contains a subset of the following properties:
24+
25+
* `host` - Postgres server hostname or, for UNIX doamain sockets, the socket filename
26+
* `port` - port on which to connect
27+
* `user` - User with which to authenticate to the server
28+
* `password` - Corresponding password
29+
* `database` - Database name within the server
30+
* `client_encoding` - string encoding the client will use
31+
* `ssl`, either a boolean or an object with properties
32+
* `cert`
33+
* `key`
34+
* `ca`
35+
* any other query parameters (for example, `application_name`) are preserved intact.
36+
37+
## Connection Strings
38+
39+
The short summary of acceptable URLs is:
40+
41+
* `socket:<path>?<query>` - UNIX domain socket
42+
* `postgres://<user>:<password>@<host>:<port>/<database>?<query>` - TCP connection
43+
44+
But see below for more details.
45+
46+
### UNIX Domain Sockets
47+
48+
When user and password are not given, the socket path follows `socket:`, as in `socket:/var/run/pgsql`.
49+
This form can be shortened to just a path: `/var/run/pgsql`.
50+
51+
When user and password are given, they are included in the typical URL positions, with an empty `host`, as in `socket://user:pass@/var/run/pgsql`.
52+
53+
Query parameters follow a `?` character, including the following special query parameters:
54+
55+
* `db=<database>` - sets the database name (urlencoded)
56+
* `encoding=<encoding>` - sets the `client_encoding` property
57+
58+
### TCP Connections
59+
60+
TCP connections to the Postgres server are indicated with `pg:` or `postgres:` schemes (in fact, any scheme but `socket:` is accepted).
61+
If username and password are included, they should be urlencoded.
62+
The database name, however, should *not* be urlencoded.
63+
64+
Query parameters follow a `?` character, including the following special query parameters:
65+
* `host=<host>` - sets `host` property, overriding the URL's host
66+
* `encoding=<encoding>` - sets the `client_encoding` property
67+
* `ssl=1`, `ssl=true`, `ssl=0`, `ssl=false` - sets `ssl` to true or false, accordingly
68+
* `sslcert=<filename>` - reads data from the given file and includes the result as `ssl.cert`
69+
* `sslkey=<filename>` - reads data from the given file and includes the result as `ssl.key`
70+
* `sslrootcert=<filename>` - reads data from the given file and includes the result as `ssl.ca`
71+
72+
A bare relative URL, such as `salesdata`, will indicate a database name while leaving other properties empty.
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export function parse(connectionString: string): ConnectionOptions;
2+
3+
export interface ConnectionOptions {
4+
host: string | null;
5+
password?: string;
6+
user?: string;
7+
port?: string | null;
8+
database: string | null | undefined;
9+
client_encoding?: string;
10+
ssl?: boolean | string;
11+
12+
application_name?: string;
13+
fallback_application_name?: string;
14+
}
+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
'use strict';
2+
3+
var url = require('url');
4+
var fs = require('fs');
5+
6+
//Parse method copied from https://github.com/brianc/node-postgres
7+
//Copyright (c) 2010-2014 Brian Carlson (brian.m.carlson@gmail.com)
8+
//MIT License
9+
10+
//parses a connection string
11+
function parse(str) {
12+
//unix socket
13+
if(str.charAt(0) === '/') {
14+
var config = str.split(' ');
15+
return { host: config[0], database: config[1] };
16+
}
17+
18+
// url parse expects spaces encoded as %20
19+
var result = url.parse(/ |%[^a-f0-9]|%[a-f0-9][^a-f0-9]/i.test(str) ? encodeURI(str).replace(/\%25(\d\d)/g, "%$1") : str, true);
20+
var config = result.query;
21+
for (var k in config) {
22+
if (Array.isArray(config[k])) {
23+
config[k] = config[k][config[k].length-1];
24+
}
25+
}
26+
27+
var auth = (result.auth || ':').split(':');
28+
config.user = auth[0];
29+
config.password = auth.splice(1).join(':');
30+
31+
config.port = result.port;
32+
if(result.protocol == 'socket:') {
33+
config.host = decodeURI(result.pathname);
34+
config.database = result.query.db;
35+
config.client_encoding = result.query.encoding;
36+
return config;
37+
}
38+
if (!config.host) {
39+
// Only set the host if there is no equivalent query param.
40+
config.host = result.hostname;
41+
}
42+
43+
// If the host is missing it might be a URL-encoded path to a socket.
44+
var pathname = result.pathname;
45+
if (!config.host && pathname && /^%2f/i.test(pathname)) {
46+
var pathnameSplit = pathname.split('/');
47+
config.host = decodeURIComponent(pathnameSplit[0]);
48+
pathname = pathnameSplit.splice(1).join('/');
49+
}
50+
// result.pathname is not always guaranteed to have a '/' prefix (e.g. relative urls)
51+
// only strip the slash if it is present.
52+
if (pathname && pathname.charAt(0) === '/') {
53+
pathname = pathname.slice(1) || null;
54+
}
55+
config.database = pathname && decodeURI(pathname);
56+
57+
if (config.ssl === 'true' || config.ssl === '1') {
58+
config.ssl = true;
59+
}
60+
61+
if (config.ssl === '0') {
62+
config.ssl = false;
63+
}
64+
65+
if (config.sslcert || config.sslkey || config.sslrootcert) {
66+
config.ssl = {};
67+
}
68+
69+
if (config.sslcert) {
70+
config.ssl.cert = fs.readFileSync(config.sslcert).toString();
71+
}
72+
73+
if (config.sslkey) {
74+
config.ssl.key = fs.readFileSync(config.sslkey).toString();
75+
}
76+
77+
if (config.sslrootcert) {
78+
config.ssl.ca = fs.readFileSync(config.sslrootcert).toString();
79+
}
80+
81+
return config;
82+
}
83+
84+
85+
module.exports = parse;
86+
87+
parse.parse = parse;
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "pg-connection-string",
3+
"version": "2.2.0",
4+
"description": "Functions for dealing with a PostgresSQL connection string",
5+
"main": "./index.js",
6+
"types": "./index.d.ts",
7+
"scripts": {
8+
"test": "istanbul cover _mocha && npm run check-coverage",
9+
"check-coverage": "istanbul check-coverage --statements 100 --branches 100 --lines 100 --functions 100",
10+
"coveralls": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls"
11+
},
12+
"repository": {
13+
"type": "git",
14+
"url": "https://github.com/iceddev/pg-connection-string"
15+
},
16+
"keywords": [
17+
"pg",
18+
"connection",
19+
"string",
20+
"parse"
21+
],
22+
"author": "Blaine Bublitz <blaine@iceddev.com> (http://iceddev.com/)",
23+
"license": "MIT",
24+
"bugs": {
25+
"url": "https://github.com/iceddev/pg-connection-string/issues"
26+
},
27+
"homepage": "https://github.com/iceddev/pg-connection-string",
28+
"dependencies": {},
29+
"devDependencies": {
30+
"chai": "^4.1.1",
31+
"coveralls": "^3.0.4",
32+
"istanbul": "^0.4.5",
33+
"mocha": "^3.5.0"
34+
},
35+
"files": [
36+
"index.js",
37+
"index.d.ts"
38+
]
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
example ca
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
example cert
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
example key

0 commit comments

Comments
 (0)