pg-connection-string
Advanced tools
Comparing version 0.1.3 to 2.0.0
31
index.js
@@ -11,22 +11,17 @@ 'use strict'; | ||
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 = {}; | ||
if (result.query.application_name) { | ||
config.application_name = result.query.application_name; | ||
} | ||
if (result.query.fallback_application_name) { | ||
config.fallback_application_name = result.query.fallback_application_name; | ||
} | ||
config.port = result.port; | ||
@@ -53,4 +48,3 @@ if(result.protocol == 'socket:') { | ||
var ssl = result.query.ssl; | ||
if (ssl === 'true' || ssl === '1') { | ||
if (config.ssl === 'true' || config.ssl === '1') { | ||
config.ssl = true; | ||
@@ -62,4 +56,5 @@ } | ||
module.exports = { | ||
parse: parse | ||
}; | ||
module.exports = parse; | ||
parse.parse = parse; |
{ | ||
"name": "pg-connection-string", | ||
"version": "0.1.3", | ||
"version": "2.0.0", | ||
"description": "Functions for dealing with a PostgresSQL connection string", | ||
"main": "index.js", | ||
"main": "./index.js", | ||
"types": "./index.d.ts", | ||
"scripts": { | ||
"test": "tap ./test" | ||
"test": "istanbul cover _mocha && npm run check-coverage", | ||
"check-coverage": "istanbul check-coverage --statements 100 --branches 100 --lines 100 --functions 100", | ||
"coveralls": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls" | ||
}, | ||
@@ -27,4 +30,6 @@ "repository": { | ||
"devDependencies": { | ||
"tap": "^0.4.11" | ||
"chai": "^4.1.1", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^3.5.0" | ||
} | ||
} |
pg-connection-string | ||
==================== | ||
[![NPM](https://nodei.co/npm/pg-connection-string.png?compact=true)](https://nodei.co/npm/pg-connection-string/) | ||
[![Build Status](https://travis-ci.org/iceddev/pg-connection-string.svg?branch=master)](https://travis-ci.org/iceddev/pg-connection-string) | ||
[![Coverage Status](https://coveralls.io/repos/iceddev/pg-connection-string/badge.svg?branch=master)](https://coveralls.io/r/iceddev/pg-connection-string?branch=master) | ||
@@ -17,3 +20,3 @@ Functions for dealing with a PostgresSQL connection string | ||
var config = parse('postgres://someuser:somepassword@somehost:381/sometable') | ||
var config = parse('postgres://someuser:somepassword@somehost:381/somedatabase') | ||
``` |
'use strict'; | ||
var test = require('tap').test; | ||
var chai = require('chai'); | ||
var expect = chai.expect; | ||
chai.should(); | ||
var parse = require('../').parse; | ||
test('using connection string in client constructor', function(t){ | ||
var subject = parse('postgres://brian:pw@boom:381/lala'); | ||
t.equal(subject.user,'brian'); | ||
t.equal(subject.password, 'pw'); | ||
t.equal(subject.host, 'boom'); | ||
t.equal(subject.port, '381'); | ||
t.equal(subject.database, 'lala'); | ||
t.end(); | ||
}); | ||
describe('parse', function(){ | ||
test('escape spaces if present', function(t){ | ||
var subject = parse('postgres://localhost/post gres'); | ||
t.equal(subject.database, 'post gres'); | ||
t.end(); | ||
}); | ||
it('using connection string in client constructor', function(){ | ||
var subject = parse('postgres://brian:pw@boom:381/lala'); | ||
subject.user.should.equal('brian'); | ||
subject.password.should.equal( 'pw'); | ||
subject.host.should.equal( 'boom'); | ||
subject.port.should.equal( '381'); | ||
subject.database.should.equal( 'lala'); | ||
}); | ||
test('do not double escape spaces', function(t){ | ||
var subject = parse('postgres://localhost/post%20gres'); | ||
t.equal(subject.database, 'post gres'); | ||
t.end(); | ||
}); | ||
it('escape spaces if present', function(){ | ||
var subject = parse('postgres://localhost/post gres'); | ||
subject.database.should.equal('post gres'); | ||
}); | ||
test('initializing with unix domain socket', function(t){ | ||
var subject = parse('/var/run/'); | ||
t.equal(subject.host, '/var/run/'); | ||
t.end(); | ||
}); | ||
it('do not double escape spaces', function(){ | ||
var subject = parse('postgres://localhost/post%20gres'); | ||
subject.database.should.equal('post gres'); | ||
}); | ||
test('initializing with unix domain socket and a specific database, the simple way', function(t){ | ||
var subject = parse('/var/run/ mydb'); | ||
t.equal(subject.host, '/var/run/'); | ||
t.equal(subject.database, 'mydb'); | ||
t.end(); | ||
}); | ||
it('initializing with unix domain socket', function(){ | ||
var subject = parse('/var/run/'); | ||
subject.host.should.equal('/var/run/'); | ||
}); | ||
test('initializing with unix domain socket, the health way', function(t){ | ||
var subject = parse('socket:/some path/?db=my[db]&encoding=utf8'); | ||
t.equal(subject.host, '/some path/'); | ||
t.equal(subject.database, 'my[db]', 'must to be escaped and unescaped trough "my%5Bdb%5D"'); | ||
t.equal(subject.client_encoding, 'utf8'); | ||
t.end(); | ||
}); | ||
it('initializing with unix domain socket and a specific database, the simple way', function(){ | ||
var subject = parse('/var/run/ mydb'); | ||
subject.host.should.equal('/var/run/'); | ||
subject.database.should.equal('mydb'); | ||
}); | ||
test('initializing with unix domain socket, the escaped health way', function(t){ | ||
var subject = parse('socket:/some%20path/?db=my%2Bdb&encoding=utf8'); | ||
t.equal(subject.host, '/some path/'); | ||
t.equal(subject.database, 'my+db'); | ||
t.equal(subject.client_encoding, 'utf8'); | ||
t.end(); | ||
}); | ||
it('initializing with unix domain socket, the health way', function(){ | ||
var subject = parse('socket:/some path/?db=my[db]&encoding=utf8'); | ||
subject.host.should.equal('/some path/'); | ||
subject.database.should.equal('my[db]', 'must to be escaped and unescaped trough "my%5Bdb%5D"'); | ||
subject.client_encoding.should.equal('utf8'); | ||
}); | ||
test('password contains < and/or > characters', function(t){ | ||
var sourceConfig = { | ||
user:'brian', | ||
password: 'hello<ther>e', | ||
port: 5432, | ||
host: 'localhost', | ||
database: 'postgres' | ||
}; | ||
var connectionString = 'postgres://' + sourceConfig.user + ':' + sourceConfig.password + '@' + sourceConfig.host + ':' + sourceConfig.port + '/' + sourceConfig.database; | ||
var subject = parse(connectionString); | ||
t.equal(subject.password, sourceConfig.password); | ||
t.end(); | ||
}); | ||
it('initializing with unix domain socket, the escaped health way', function(){ | ||
var subject = parse('socket:/some%20path/?db=my%2Bdb&encoding=utf8'); | ||
subject.host.should.equal('/some path/'); | ||
subject.database.should.equal('my+db'); | ||
subject.client_encoding.should.equal('utf8'); | ||
}); | ||
test('password contains colons', function(t){ | ||
var sourceConfig = { | ||
user:'brian', | ||
password: 'hello:pass:world', | ||
port: 5432, | ||
host: 'localhost', | ||
database: 'postgres' | ||
}; | ||
var connectionString = 'postgres://' + sourceConfig.user + ':' + sourceConfig.password + '@' + sourceConfig.host + ':' + sourceConfig.port + '/' + sourceConfig.database; | ||
var subject = parse(connectionString); | ||
t.equal(subject.password, sourceConfig.password); | ||
t.end(); | ||
}); | ||
it('password contains < and/or > characters', function(){ | ||
var sourceConfig = { | ||
user:'brian', | ||
password: 'hello<ther>e', | ||
port: 5432, | ||
host: 'localhost', | ||
database: 'postgres' | ||
}; | ||
var connectionString = 'postgres://' + sourceConfig.user + ':' + sourceConfig.password + '@' + sourceConfig.host + ':' + sourceConfig.port + '/' + sourceConfig.database; | ||
var subject = parse(connectionString); | ||
subject.password.should.equal(sourceConfig.password); | ||
}); | ||
test('username or password contains weird characters', function(t){ | ||
var strang = 'pg://my f%irst name:is&%awesome!@localhost:9000'; | ||
var subject = parse(strang); | ||
t.equal(subject.user, 'my f%irst name'); | ||
t.equal(subject.password, 'is&%awesome!'); | ||
t.equal(subject.host, 'localhost'); | ||
t.end(); | ||
}); | ||
it('password contains colons', function(){ | ||
var sourceConfig = { | ||
user:'brian', | ||
password: 'hello:pass:world', | ||
port: 5432, | ||
host: 'localhost', | ||
database: 'postgres' | ||
}; | ||
var connectionString = 'postgres://' + sourceConfig.user + ':' + sourceConfig.password + '@' + sourceConfig.host + ':' + sourceConfig.port + '/' + sourceConfig.database; | ||
var subject = parse(connectionString); | ||
subject.password.should.equal(sourceConfig.password); | ||
}); | ||
test('url is properly encoded', function(t){ | ||
var encoded = 'pg://bi%25na%25%25ry%20:s%40f%23@localhost/%20u%2520rl'; | ||
var subject = parse(encoded); | ||
t.equal(subject.user, 'bi%na%%ry '); | ||
t.equal(subject.password, 's@f#'); | ||
t.equal(subject.host, 'localhost'); | ||
t.equal(subject.database, ' u%20rl'); | ||
t.end(); | ||
}); | ||
it('username or password contains weird characters', function(){ | ||
var strang = 'pg://my f%irst name:is&%awesome!@localhost:9000'; | ||
var subject = parse(strang); | ||
subject.user.should.equal('my f%irst name'); | ||
subject.password.should.equal('is&%awesome!'); | ||
subject.host.should.equal('localhost'); | ||
}); | ||
test('relative url sets database', function(t){ | ||
var relative = 'different_db_on_default_host'; | ||
var subject = parse(relative); | ||
t.equal(subject.database, 'different_db_on_default_host'); | ||
t.end(); | ||
}); | ||
it('url is properly encoded', function(){ | ||
var encoded = 'pg://bi%25na%25%25ry%20:s%40f%23@localhost/%20u%2520rl'; | ||
var subject = parse(encoded); | ||
subject.user.should.equal('bi%na%%ry '); | ||
subject.password.should.equal('s@f#'); | ||
subject.host.should.equal('localhost'); | ||
subject.database.should.equal(' u%20rl'); | ||
}); | ||
test('no pathname returns null database', function (t) { | ||
var subject = parse('pg://myhost'); | ||
t.equal(subject.host, 'myhost'); | ||
t.type(subject.database, 'null'); | ||
it('relative url sets database', function(){ | ||
var relative = 'different_db_on_default_host'; | ||
var subject = parse(relative); | ||
subject.database.should.equal('different_db_on_default_host'); | ||
}); | ||
t.end(); | ||
}); | ||
it('no pathname returns null database', function () { | ||
var subject = parse('pg://myhost'); | ||
(subject.database === null).should.equal(true); | ||
}); | ||
test('pathname of "/" returns null database', function (t) { | ||
var subject = parse('pg://myhost/'); | ||
t.equal(subject.host, 'myhost'); | ||
t.type(subject.database, 'null'); | ||
it('pathname of "/" returns null database', function () { | ||
var subject = parse('pg://myhost/'); | ||
subject.host.should.equal('myhost'); | ||
(subject.database === null).should.equal(true); | ||
}); | ||
t.end(); | ||
it('configuration parameter application_name', function(){ | ||
var connectionString = 'pg:///?application_name=TheApp'; | ||
var subject = parse(connectionString); | ||
subject.application_name.should.equal('TheApp'); | ||
}); | ||
it('configuration parameter fallback_application_name', function(){ | ||
var connectionString = 'pg:///?fallback_application_name=TheAppFallback'; | ||
var subject = parse(connectionString); | ||
subject.fallback_application_name.should.equal('TheAppFallback'); | ||
}); | ||
it('configuration parameter fallback_application_name', function(){ | ||
var connectionString = 'pg:///?fallback_application_name=TheAppFallback'; | ||
var subject = parse(connectionString); | ||
subject.fallback_application_name.should.equal('TheAppFallback'); | ||
}); | ||
it('configuration parameter ssl=true', function(){ | ||
var connectionString = 'pg:///?ssl=true'; | ||
var subject = parse(connectionString); | ||
subject.ssl.should.equal(true); | ||
}); | ||
it('configuration parameter ssl=1', function(){ | ||
var connectionString = 'pg:///?ssl=1'; | ||
var subject = parse(connectionString); | ||
subject.ssl.should.equal(true); | ||
}); | ||
it('set ssl', function () { | ||
var subject = parse('pg://myhost/db?ssl=1'); | ||
subject.ssl.should.equal(true); | ||
}); | ||
it('allow other params like max, ...', function () { | ||
var subject = parse('pg://myhost/db?max=18&min=4'); | ||
subject.max.should.equal('18'); | ||
subject.min.should.equal('4'); | ||
}); | ||
it('configuration parameter keepalives', function(){ | ||
var connectionString = 'pg:///?keepalives=1'; | ||
var subject = parse(connectionString); | ||
subject.keepalives.should.equal('1'); | ||
}); | ||
it('unknown configuration parameter is passed into client', function(){ | ||
var connectionString = 'pg:///?ThereIsNoSuchPostgresParameter=1234'; | ||
var subject = parse(connectionString); | ||
subject.ThereIsNoSuchPostgresParameter.should.equal('1234'); | ||
}); | ||
it('do not override a config field with value from query string', function(){ | ||
var subject = parse('socket:/some path/?db=my[db]&encoding=utf8&client_encoding=bogus'); | ||
subject.host.should.equal('/some path/'); | ||
subject.database.should.equal('my[db]', 'must to be escaped and unescaped through "my%5Bdb%5D"'); | ||
subject.client_encoding.should.equal('utf8'); | ||
}); | ||
it('return last value of repeated parameter', function(){ | ||
var connectionString = 'pg:///?keepalives=1&keepalives=0'; | ||
var subject = parse(connectionString); | ||
subject.keepalives.should.equal('0'); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
12111
9
209
0
22
3
2