From 9ab62ff9f3050bc8d1096cd6f1258cd288be75fd Mon Sep 17 00:00:00 2001 From: caub Date: Sat, 1 Jul 2017 11:32:37 +0200 Subject: [PATCH] allow min/max params for pg-pool --- .gitignore | 1 + .travis.yml | 2 ++ index.js | 36 +++++++++++++----------------------- package.json | 2 +- test/parse.js | 18 +++++++++++++++++- 5 files changed, 34 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index da23d0d..f28f01f 100644 --- a/.gitignore +++ b/.gitignore @@ -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 \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 244b7e8..202c307 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,5 @@ language: node_js node_js: - '0.10' + - '6.9' + - '8' diff --git a/index.js b/index.js index b2863dd..0042fae 100644 --- a/index.js +++ b/index.js @@ -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:') { @@ -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; diff --git a/package.json b/package.json index f3b14c9..9b0e62e 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,6 @@ "homepage": "https://github.com/iceddev/pg-connection-string", "dependencies": {}, "devDependencies": { - "tap": "^0.4.11" + "tap": "^10.3.3" } } diff --git a/test/parse.js b/test/parse.js index bd1171b..8f4bcb4 100644 --- a/test/parse.js +++ b/test/parse.js @@ -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); @@ -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(); -}); \ No newline at end of file +}); +