Skip to content

Commit cf107b0

Browse files
authored
Merge pull request #11 from caub/max
thanks, @caub !
2 parents 45d8232 + 9ab62ff commit cf107b0

File tree

5 files changed

+34
-25
lines changed

5 files changed

+34
-25
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ build/Release
2323
# Deployed apps should consider commenting this line out:
2424
# see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git
2525
node_modules
26+
package-lock.json

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
language: node_js
22
node_js:
33
- '0.10'
4+
- '6.9'
5+
- '8'

index.js

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,20 @@ var url = require('url');
88

99
//parses a connection string
1010
function parse(str) {
11-
var config;
1211
//unix socket
1312
if(str.charAt(0) === '/') {
14-
config = str.split(' ');
13+
var config = str.split(' ');
1514
return { host: config[0], database: config[1] };
1615
}
16+
1717
// 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");
18+
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);
19+
var config = result.query;
20+
for (var k in config) {
21+
if (Array.isArray(config[k])) {
22+
config[k] = config[k][config[k].length-1];
23+
}
2024
}
21-
var result = url.parse(str, true);
22-
config = {};
2325

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

45-
var ssl = result.query.ssl;
46-
if (ssl === 'true' || ssl === '1') {
47+
if (config.ssl === 'true' || config.ssl === '1') {
4748
config.ssl = true;
4849
}
4950

50-
['db', 'database', 'encoding', 'client_encoding', 'host', 'port', 'user', 'password', 'ssl']
51-
.forEach(function(key) {
52-
delete result.query[key];
53-
});
54-
55-
Object.getOwnPropertyNames(result.query).forEach(function(key) {
56-
var value = result.query[key];
57-
if (Array.isArray(value))
58-
value = value[value.length-1];
59-
config[key] = value;
60-
});
61-
6251
return config;
6352
}
6453

65-
module.exports = {
66-
parse: parse
67-
};
54+
55+
module.exports = parse;
56+
57+
parse.parse = parse;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@
2525
"homepage": "https://github.com/iceddev/pg-connection-string",
2626
"dependencies": {},
2727
"devDependencies": {
28-
"tap": "^0.4.11"
28+
"tap": "^10.3.3"
2929
}
3030
}

test/parse.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,20 @@ test('configuration parameter ssl=1', function(t){
160160
t.end();
161161
});
162162

163+
test('set ssl', function (t) {
164+
var subject = parse('pg://myhost/db?ssl=1');
165+
t.equal(subject.ssl, true);
166+
t.end();
167+
});
168+
169+
test('allow other params like max, ...', function (t) {
170+
var subject = parse('pg://myhost/db?max=18&min=4');
171+
t.equal(subject.max, '18');
172+
t.equal(subject.min, '4');
173+
t.end();
174+
});
175+
176+
163177
test('configuration parameter keepalives', function(t){
164178
var connectionString = 'pg:///?keepalives=1';
165179
var subject = parse(connectionString);
@@ -182,9 +196,11 @@ test('do not override a config field with value from query string', function(t){
182196
t.end();
183197
});
184198

199+
185200
test('return last value of repeated parameter', function(t){
186201
var connectionString = 'pg:///?keepalives=1&keepalives=0';
187202
var subject = parse(connectionString);
188203
t.equal(subject.keepalives, '0');
189204
t.end();
190-
});
205+
});
206+

0 commit comments

Comments
 (0)