Skip to content

Commit

Permalink
Merge pull request #11 from caub/max
Browse files Browse the repository at this point in the history
thanks, @caub !
  • Loading branch information
monteslu committed Aug 30, 2017
2 parents 45d8232 + 9ab62ff commit cf107b0
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ build/Release
# Deployed apps should consider commenting this line out:
# see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git
node_modules
package-lock.json
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
language: node_js
node_js:
- '0.10'
- '6.9'
- '8'
36 changes: 13 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ var url = require('url');

//parses a connection string
function parse(str) {
var config;
//unix socket
if(str.charAt(0) === '/') {
config = str.split(' ');
var 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(/ |%[^a-f0-9]|%[a-f0-9][^a-f0-9]/i.test(str) ? encodeURI(str).replace(/\%25(\d\d)/g, "%$1") : str, true);
var config = result.query;
for (var k in config) {
if (Array.isArray(config[k])) {
config[k] = config[k][config[k].length-1];
}
}
var result = url.parse(str, true);
config = {};

config.port = result.port;
if(result.protocol == 'socket:') {
Expand All @@ -42,26 +44,14 @@ function parse(str) {
config.user = auth[0];
config.password = auth.splice(1).join(':');

var ssl = result.query.ssl;
if (ssl === 'true' || ssl === '1') {
if (config.ssl === 'true' || config.ssl === '1') {
config.ssl = true;
}

['db', 'database', 'encoding', 'client_encoding', 'host', 'port', 'user', 'password', 'ssl']
.forEach(function(key) {
delete result.query[key];
});

Object.getOwnPropertyNames(result.query).forEach(function(key) {
var value = result.query[key];
if (Array.isArray(value))
value = value[value.length-1];
config[key] = value;
});

return config;
}

module.exports = {
parse: parse
};

module.exports = parse;

parse.parse = parse;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@
"homepage": "https://github.com/iceddev/pg-connection-string",
"dependencies": {},
"devDependencies": {
"tap": "^0.4.11"
"tap": "^10.3.3"
}
}
18 changes: 17 additions & 1 deletion test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,20 @@ test('configuration parameter ssl=1', function(t){
t.end();
});

test('set ssl', function (t) {
var subject = parse('pg://myhost/db?ssl=1');
t.equal(subject.ssl, true);
t.end();
});

test('allow other params like max, ...', function (t) {
var subject = parse('pg://myhost/db?max=18&min=4');
t.equal(subject.max, '18');
t.equal(subject.min, '4');
t.end();
});


test('configuration parameter keepalives', function(t){
var connectionString = 'pg:///?keepalives=1';
var subject = parse(connectionString);
Expand All @@ -182,9 +196,11 @@ test('do not override a config field with value from query string', function(t){
t.end();
});


test('return last value of repeated parameter', function(t){
var connectionString = 'pg:///?keepalives=1&keepalives=0';
var subject = parse(connectionString);
t.equal(subject.keepalives, '0');
t.end();
});
});

0 comments on commit cf107b0

Please sign in to comment.