diff --git a/packages/pg-connection-string/index.js b/packages/pg-connection-string/index.js index 139cc17b1..ec10c5de8 100644 --- a/packages/pg-connection-string/index.js +++ b/packages/pg-connection-string/index.js @@ -36,8 +36,9 @@ function parse(str, options = {}) { throw err } - // We'd like to use Object.fromEntries() here but Node.js 10 does not support it - for (const entry of result.searchParams.entries()) { + // Like libpq, only percent-decode the query values: URLSearchParams would turn a literal + // `+` into a space. We'd like to use Object.fromEntries() here but Node.js 10 does not support it + for (const entry of new URLSearchParams(result.search.replace(/\+/g, '%2B')).entries()) { config[entry[0]] = entry[1] } diff --git a/packages/pg-connection-string/test/parse.ts b/packages/pg-connection-string/test/parse.ts index 562c3ece0..ac831200a 100644 --- a/packages/pg-connection-string/test/parse.ts +++ b/packages/pg-connection-string/test/parse.ts @@ -192,6 +192,15 @@ describe('parse', function () { subject.fallback_application_name?.should.equal('TheAppFallback') }) + it('configuration parameters keep a literal plus sign', function () { + const connectionString = 'pg:///?application_name=a+b&user=c+d&password=e+f&options=g%2Bh%20i' + const subject = parse(connectionString) + subject.application_name?.should.equal('a+b') + subject.user?.should.equal('c+d') + subject.password?.should.equal('e+f') + subject.options?.should.equal('g+h i') + }) + it('configuration parameter options', function () { const connectionString = 'pg:///?options=-c geqo=off' const subject = parse(connectionString)