From 0ff40e733b58096dd3e14a7a1c787a22058f54a1 Mon Sep 17 00:00:00 2001 From: Daniel Rozenberg Date: Tue, 28 Jan 2020 19:28:03 -0500 Subject: [PATCH 1/3] host= query param takes precedence --- index.js | 5 ++++- test/parse.js | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 3cabb372..29d3653c 100644 --- a/index.js +++ b/index.js @@ -35,7 +35,10 @@ function parse(str) { config.client_encoding = result.query.encoding; return config; } - config.host = result.hostname; + if (!config.host) { + // Only set the host if there is no equivalent query param. + config.host = result.hostname; + } // result.pathname is not always guaranteed to have a '/' prefix (e.g. relative urls) // only strip the slash if it is present. diff --git a/test/parse.js b/test/parse.js index 4de28719..c973fb5c 100644 --- a/test/parse.js +++ b/test/parse.js @@ -120,6 +120,19 @@ describe('parse', function(){ (subject.database === null).should.equal(true); }); + it('configuration parameter host', function() { + var subject = parse('pg://user:pass@/dbname?host=/unix/socket'); + subject.user.should.equal('user'); + subject.password.should.equal('pass'); + subject.host.should.equal('/unix/socket'); + subject.database.should.equal('dbname'); + }); + + it('configuration parameter host overrides url host', function() { + var subject = parse('pg://user:pass@localhost/dbname?host=/unix/socket'); + subject.host.should.equal('/unix/socket'); + }); + it('configuration parameter application_name', function(){ var connectionString = 'pg:///?application_name=TheApp'; var subject = parse(connectionString); From b309db074ff545ed93c6396a98029b5cce7b943d Mon Sep 17 00:00:00 2001 From: Daniel Rozenberg Date: Tue, 28 Jan 2020 19:29:38 -0500 Subject: [PATCH 2/3] Support URL-encoded socket names --- index.js | 10 ++++++++-- test/parse.js | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 29d3653c..7cfc8293 100644 --- a/index.js +++ b/index.js @@ -40,11 +40,17 @@ function parse(str) { config.host = result.hostname; } + // If the host is missing it might be a URL-encoded path to a socket. + var pathname = result.pathname; + if (!config.host && pathname && pathname.toLowerCase().startsWith('%2f')) { + var pathnameSplit = pathname.split('/'); + config.host = decodeURIComponent(pathnameSplit[0]); + pathname = pathnameSplit.splice(1).join('/'); + } // result.pathname is not always guaranteed to have a '/' prefix (e.g. relative urls) // only strip the slash if it is present. - var pathname = result.pathname; if (pathname && pathname.charAt(0) === '/') { - pathname = result.pathname.slice(1) || null; + pathname = pathname.slice(1) || null; } config.database = pathname && decodeURI(pathname); diff --git a/test/parse.js b/test/parse.js index c973fb5c..07f886e1 100644 --- a/test/parse.js +++ b/test/parse.js @@ -133,6 +133,30 @@ describe('parse', function(){ subject.host.should.equal('/unix/socket'); }); + it('url with encoded socket', function() { + var subject = parse('pg://user:pass@%2Funix%2Fsocket/dbname'); + subject.user.should.equal('user'); + subject.password.should.equal('pass'); + subject.host.should.equal('/unix/socket'); + subject.database.should.equal('dbname'); + }); + + it('url with real host and an encoded db name', function() { + var subject = parse('pg://user:pass@localhost/%2Fdbname'); + subject.user.should.equal('user'); + subject.password.should.equal('pass'); + subject.host.should.equal('localhost'); + subject.database.should.equal('%2Fdbname'); + }); + + it('configuration parameter host treats encoded socket as part of the db name', function() { + var subject = parse('pg://user:pass@%2Funix%2Fsocket/dbname?host=localhost'); + subject.user.should.equal('user'); + subject.password.should.equal('pass'); + subject.host.should.equal('localhost'); + subject.database.should.equal('%2Funix%2Fsocket/dbname'); + }); + it('configuration parameter application_name', function(){ var connectionString = 'pg:///?application_name=TheApp'; var subject = parse(connectionString); From 7ec9b70180b5aaadb75c4fde3f35ff86031ce279 Mon Sep 17 00:00:00 2001 From: Daniel Rozenberg Date: Tue, 28 Jan 2020 19:40:51 -0500 Subject: [PATCH 3/3] Use regex instead of startsWith which is unsupported in node 0.10 --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 7cfc8293..7e914ba1 100644 --- a/index.js +++ b/index.js @@ -42,7 +42,7 @@ function parse(str) { // If the host is missing it might be a URL-encoded path to a socket. var pathname = result.pathname; - if (!config.host && pathname && pathname.toLowerCase().startsWith('%2f')) { + if (!config.host && pathname && /^%2f/i.test(pathname)) { var pathnameSplit = pathname.split('/'); config.host = decodeURIComponent(pathnameSplit[0]); pathname = pathnameSplit.splice(1).join('/');