-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
language: node_js | ||
node_js: | ||
- '0.10' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,18 @@ | ||
pg-connection-string | ||
==================== | ||
|
||
[![Build Status](https://travis-ci.org/iceddev/pg-connection-string.svg?branch=master)](https://travis-ci.org/iceddev/pg-connection-string) | ||
|
||
Functions for dealing with a PostgresSQL connection string | ||
|
||
`parse` method taken from [node-postgres](https://github.com/brianc/node-postgres.git) | ||
Copyright (c) 2010-2014 Brian Carlson (brian.m.carlson@gmail.com) | ||
MIT License | ||
|
||
## Usage | ||
|
||
```js | ||
var parse = require('pg-connection-string').parse; | ||
|
||
var config = parse('postgres://someuser:somepassword@somehost:381/sometable') | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
'use strict'; | ||
|
||
var url = require('url'); | ||
|
||
//Parse method copied from https://github.com/brianc/node-postgres | ||
//Copyright (c) 2010-2014 Brian Carlson (brian.m.carlson@gmail.com) | ||
//MIT License | ||
|
||
//parses a connection string | ||
function parse(str) { | ||
var config; | ||
//unix socket | ||
if(str.charAt(0) === '/') { | ||
config = str.split(' '); | ||
return { host: config[0], database: config[1] }; | ||
} | ||
// url parse expects spaces encoded as %20 | ||
if(/ |%[^a-f0-9]|%[a-f0-9][^a-f0-9]/i.test(str)) { | ||
str = encodeURI(str).replace(/\%25(\d\d)/g, "%$1"); | ||
} | ||
var result = url.parse(str, true); | ||
config = {}; | ||
|
||
if (result.query.application_name) { | ||
config.application_name = result.query.application_name; | ||
} | ||
if (result.query.fallback_application_name) { | ||
config.fallback_application_name = result.query.fallback_application_name; | ||
} | ||
|
||
if(result.protocol == 'socket:') { | ||
config.host = decodeURI(result.pathname); | ||
config.database = result.query.db; | ||
config.client_encoding = result.query.encoding; | ||
return config; | ||
} | ||
config.host = result.hostname; | ||
config.database = result.pathname ? decodeURI(result.pathname.slice(1)) : null; | ||
var auth = (result.auth || ':').split(':'); | ||
config.user = auth[0]; | ||
config.password = auth[1]; | ||
config.port = result.port; | ||
|
||
var ssl = result.query.ssl; | ||
if (ssl === 'true' || ssl === '1') { | ||
config.ssl = true; | ||
} | ||
|
||
return config; | ||
} | ||
|
||
module.exports = { | ||
parse: parse | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"name": "pg-connection-string", | ||
"version": "0.1.0", | ||
"description": "Functions for dealing with a PostgresSQL connection string", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "tap ./test" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/iceddev/pg-connection-string" | ||
}, | ||
"keywords": [ | ||
"pg", | ||
"connection", | ||
"string", | ||
"parse" | ||
], | ||
"author": "Blaine Bublitz <blaine@iceddev.com> (http://iceddev.com/)", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/iceddev/pg-connection-string/issues" | ||
}, | ||
"homepage": "https://github.com/iceddev/pg-connection-string", | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"tap": "^0.4.11" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
'use strict'; | ||
|
||
var test = require('tap').test; | ||
|
||
var parse = require('../').parse; | ||
|
||
test('using connection string in client constructor', function(t){ | ||
var subject = parse('postgres://brian:pw@boom:381/lala'); | ||
t.equal(subject.user,'brian'); | ||
t.equal(subject.password, 'pw'); | ||
t.equal(subject.host, 'boom'); | ||
t.equal(subject.port, '381'); | ||
t.equal(subject.database, 'lala'); | ||
t.end(); | ||
}); | ||
|
||
test('escape spaces if present', function(t){ | ||
var subject = parse('postgres://localhost/post gres'); | ||
t.equal(subject.database, 'post gres'); | ||
t.end(); | ||
}); | ||
|
||
test('do not double escape spaces', function(t){ | ||
var subject = parse('postgres://localhost/post%20gres'); | ||
t.equal(subject.database, 'post gres'); | ||
t.end(); | ||
}); | ||
|
||
test('initializing with unix domain socket', function(t){ | ||
var subject = parse('/var/run/'); | ||
t.equal(subject.host, '/var/run/'); | ||
t.end(); | ||
}); | ||
|
||
test('initializing with unix domain socket and a specific database, the simple way', function(t){ | ||
var subject = parse('/var/run/ mydb'); | ||
t.equal(subject.host, '/var/run/'); | ||
t.equal(subject.database, 'mydb'); | ||
t.end(); | ||
}); | ||
|
||
test('initializing with unix domain socket, the health way', function(t){ | ||
var subject = parse('socket:/some path/?db=my[db]&encoding=utf8'); | ||
t.equal(subject.host, '/some path/'); | ||
t.equal(subject.database, 'my[db]', 'must to be escaped and unescaped trough "my%5Bdb%5D"'); | ||
t.equal(subject.client_encoding, 'utf8'); | ||
t.end(); | ||
}); | ||
|
||
test('initializing with unix domain socket, the escaped health way', function(t){ | ||
var subject = parse('socket:/some%20path/?db=my%2Bdb&encoding=utf8'); | ||
t.equal(subject.host, '/some path/'); | ||
t.equal(subject.database, 'my+db'); | ||
t.equal(subject.client_encoding, 'utf8'); | ||
t.end(); | ||
}); | ||
|
||
test('password contains < and/or > characters', function(t){ | ||
var sourceConfig = { | ||
user:'brian', | ||
password: 'hello<ther>e', | ||
port: 5432, | ||
host: 'localhost', | ||
database: 'postgres' | ||
}; | ||
var connectionString = 'postgres://' + sourceConfig.user + ':' + sourceConfig.password + '@' + sourceConfig.host + ':' + sourceConfig.port + '/' + sourceConfig.database; | ||
var subject = parse(connectionString); | ||
t.equal(subject.password, sourceConfig.password); | ||
t.end(); | ||
}); | ||
|
||
test('username or password contains weird characters', function(t){ | ||
var strang = 'pg://my f%irst name:is&%awesome!@localhost:9000'; | ||
var subject = parse(strang); | ||
t.equal(subject.user, 'my f%irst name'); | ||
t.equal(subject.password, 'is&%awesome!'); | ||
t.equal(subject.host, 'localhost'); | ||
t.end(); | ||
}); | ||
|
||
test('url is properly encoded', function(t){ | ||
var encoded = 'pg://bi%25na%25%25ry%20:s%40f%23@localhost/%20u%2520rl'; | ||
var subject = parse(encoded); | ||
t.equal(subject.user, 'bi%na%%ry '); | ||
t.equal(subject.password, 's@f#'); | ||
t.equal(subject.host, 'localhost'); | ||
t.equal(subject.database, ' u%20rl'); | ||
t.end(); | ||
}); |