Skip to content

Commit 92c1fed

Browse files
committed
initial commit
1 parent 88aafd7 commit 92c1fed

File tree

5 files changed

+189
-0
lines changed

5 files changed

+189
-0
lines changed

.travis.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
language: node_js
2+
node_js:
3+
- '0.10'

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
11
pg-connection-string
22
====================
33

4+
[![Build Status](https://travis-ci.org/iceddev/pg-connection-string.svg?branch=master)](https://travis-ci.org/iceddev/pg-connection-string)
5+
46
Functions for dealing with a PostgresSQL connection string
7+
8+
`parse` method taken from [node-postgres](https://github.com/brianc/node-postgres.git)
9+
Copyright (c) 2010-2014 Brian Carlson ([email protected])
10+
MIT License
11+
12+
## Usage
13+
14+
```js
15+
var parse = require('pg-connection-string').parse;
16+
17+
var config = parse('postgres://someuser:somepassword@somehost:381/sometable')
18+
```

index.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'use strict';
2+
3+
var url = require('url');
4+
5+
//Parse method copied from https://github.com/brianc/node-postgres
6+
//Copyright (c) 2010-2014 Brian Carlson ([email protected])
7+
//MIT License
8+
9+
//parses a connection string
10+
function parse(str) {
11+
var config;
12+
//unix socket
13+
if(str.charAt(0) === '/') {
14+
config = str.split(' ');
15+
return { host: config[0], database: config[1] };
16+
}
17+
// url parse expects spaces encoded as %20
18+
if(/ |%[^a-f0-9]|%[a-f0-9][^a-f0-9]/i.test(str)) {
19+
str = encodeURI(str).replace(/\%25(\d\d)/g, "%$1");
20+
}
21+
var result = url.parse(str, true);
22+
config = {};
23+
24+
if (result.query.application_name) {
25+
config.application_name = result.query.application_name;
26+
}
27+
if (result.query.fallback_application_name) {
28+
config.fallback_application_name = result.query.fallback_application_name;
29+
}
30+
31+
if(result.protocol == 'socket:') {
32+
config.host = decodeURI(result.pathname);
33+
config.database = result.query.db;
34+
config.client_encoding = result.query.encoding;
35+
return config;
36+
}
37+
config.host = result.hostname;
38+
config.database = result.pathname ? decodeURI(result.pathname.slice(1)) : null;
39+
var auth = (result.auth || ':').split(':');
40+
config.user = auth[0];
41+
config.password = auth[1];
42+
config.port = result.port;
43+
44+
var ssl = result.query.ssl;
45+
if (ssl === 'true' || ssl === '1') {
46+
config.ssl = true;
47+
}
48+
49+
return config;
50+
}
51+
52+
module.exports = {
53+
parse: parse
54+
};

package.json

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "pg-connection-string",
3+
"version": "0.1.0",
4+
"description": "Functions for dealing with a PostgresSQL connection string",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "tap ./test"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/iceddev/pg-connection-string"
12+
},
13+
"keywords": [
14+
"pg",
15+
"connection",
16+
"string",
17+
"parse"
18+
],
19+
"author": "Blaine Bublitz <[email protected]> (http://iceddev.com/)",
20+
"license": "MIT",
21+
"bugs": {
22+
"url": "https://github.com/iceddev/pg-connection-string/issues"
23+
},
24+
"homepage": "https://github.com/iceddev/pg-connection-string",
25+
"dependencies": {},
26+
"devDependencies": {
27+
"tap": "^0.4.11"
28+
}
29+
}

test/parse.js

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
'use strict';
2+
3+
var test = require('tap').test;
4+
5+
var parse = require('../').parse;
6+
7+
test('using connection string in client constructor', function(t){
8+
var subject = parse('postgres://brian:pw@boom:381/lala');
9+
t.equal(subject.user,'brian');
10+
t.equal(subject.password, 'pw');
11+
t.equal(subject.host, 'boom');
12+
t.equal(subject.port, '381');
13+
t.equal(subject.database, 'lala');
14+
t.end();
15+
});
16+
17+
test('escape spaces if present', function(t){
18+
var subject = parse('postgres://localhost/post gres');
19+
t.equal(subject.database, 'post gres');
20+
t.end();
21+
});
22+
23+
test('do not double escape spaces', function(t){
24+
var subject = parse('postgres://localhost/post%20gres');
25+
t.equal(subject.database, 'post gres');
26+
t.end();
27+
});
28+
29+
test('initializing with unix domain socket', function(t){
30+
var subject = parse('/var/run/');
31+
t.equal(subject.host, '/var/run/');
32+
t.end();
33+
});
34+
35+
test('initializing with unix domain socket and a specific database, the simple way', function(t){
36+
var subject = parse('/var/run/ mydb');
37+
t.equal(subject.host, '/var/run/');
38+
t.equal(subject.database, 'mydb');
39+
t.end();
40+
});
41+
42+
test('initializing with unix domain socket, the health way', function(t){
43+
var subject = parse('socket:/some path/?db=my[db]&encoding=utf8');
44+
t.equal(subject.host, '/some path/');
45+
t.equal(subject.database, 'my[db]', 'must to be escaped and unescaped trough "my%5Bdb%5D"');
46+
t.equal(subject.client_encoding, 'utf8');
47+
t.end();
48+
});
49+
50+
test('initializing with unix domain socket, the escaped health way', function(t){
51+
var subject = parse('socket:/some%20path/?db=my%2Bdb&encoding=utf8');
52+
t.equal(subject.host, '/some path/');
53+
t.equal(subject.database, 'my+db');
54+
t.equal(subject.client_encoding, 'utf8');
55+
t.end();
56+
});
57+
58+
test('password contains < and/or > characters', function(t){
59+
var sourceConfig = {
60+
user:'brian',
61+
password: 'hello<ther>e',
62+
port: 5432,
63+
host: 'localhost',
64+
database: 'postgres'
65+
};
66+
var connectionString = 'postgres://' + sourceConfig.user + ':' + sourceConfig.password + '@' + sourceConfig.host + ':' + sourceConfig.port + '/' + sourceConfig.database;
67+
var subject = parse(connectionString);
68+
t.equal(subject.password, sourceConfig.password);
69+
t.end();
70+
});
71+
72+
test('username or password contains weird characters', function(t){
73+
var strang = 'pg://my f%irst name:is&%awesome!@localhost:9000';
74+
var subject = parse(strang);
75+
t.equal(subject.user, 'my f%irst name');
76+
t.equal(subject.password, 'is&%awesome!');
77+
t.equal(subject.host, 'localhost');
78+
t.end();
79+
});
80+
81+
test('url is properly encoded', function(t){
82+
var encoded = 'pg://bi%25na%25%25ry%20:s%40f%23@localhost/%20u%2520rl';
83+
var subject = parse(encoded);
84+
t.equal(subject.user, 'bi%na%%ry ');
85+
t.equal(subject.password, 's@f#');
86+
t.equal(subject.host, 'localhost');
87+
t.equal(subject.database, ' u%20rl');
88+
t.end();
89+
});

0 commit comments

Comments
 (0)