package/package.json000644 000765 000024 0000001746 13151626547013037 0ustar00000000 000000 { "name": "pg-connection-string", "version": "2.0.0", "description": "Functions for dealing with a PostgresSQL connection string", "main": "./index.js", "types": "./index.d.ts", "scripts": { "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" }, "repository": { "type": "git", "url": "https://github.com/iceddev/pg-connection-string" }, "keywords": [ "pg", "connection", "string", "parse" ], "author": "Blaine Bublitz (http://iceddev.com/)", "license": "MIT", "bugs": { "url": "https://github.com/iceddev/pg-connection-string/issues" }, "homepage": "https://github.com/iceddev/pg-connection-string", "dependencies": {}, "devDependencies": { "chai": "^4.1.1", "istanbul": "^0.4.5", "mocha": "^3.5.0" } } package/.npmignore000644 000765 000024 0000001050 13151573345012531 0ustar00000000 000000 # Logs logs *.log # Runtime data pids *.pid *.seed # Directory for instrumented libs generated by jscoverage/JSCover lib-cov # Coverage directory used by tools like istanbul coverage # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) .grunt # Compiled binary addons (http://nodejs.org/api/addons.html) build/Release # Dependency directory # 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.jsonpackage/README.md000644 000765 000024 0000001516 13151626464012021 0ustar00000000 000000 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) Functions for dealing with a PostgresSQL connection string `parse` method taken from [node-postgres](https://github.com/brianc/node-postgres.git) Copyright (c) 2010-2014 Brian Carlson (brian.m.carlson@gmail.com) MIT License ## Usage ```js var parse = require('pg-connection-string').parse; var config = parse('postgres://someuser:somepassword@somehost:381/somedatabase') ``` package/LICENSE000644 000765 000024 0000002072 13151573345011544 0ustar00000000 000000 The MIT License (MIT) Copyright (c) 2014 Iced Development Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.package/index.js000644 000765 000024 0000003022 13151573345012200 0ustar00000000 000000 'use strict'; var url = require('url'); //Parse method copied from https://github.com/brianc/node-postgres //Copyright (c) 2010-2014 Brian Carlson (brian.m.carlson@gmail.com) //MIT License //parses a connection string function parse(str) { //unix socket if(str.charAt(0) === '/') { var config = str.split(' '); return { host: config[0], database: config[1] }; } // url parse expects spaces encoded as %20 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]; } } config.port = result.port; if(result.protocol == 'socket:') { config.host = decodeURI(result.pathname); config.database = result.query.db; config.client_encoding = result.query.encoding; return config; } 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. var pathname = result.pathname; if (pathname && pathname.charAt(0) === '/') { pathname = result.pathname.slice(1) || null; } config.database = pathname && decodeURI(pathname); var auth = (result.auth || ':').split(':'); config.user = auth[0]; config.password = auth.splice(1).join(':'); if (config.ssl === 'true' || config.ssl === '1') { config.ssl = true; } return config; } module.exports = parse; parse.parse = parse; package/index.d.ts000644 000765 000024 0000000560 13151573345012440 0ustar00000000 000000 export function parse(connectionString: string): ConnectionOptions; export interface ConnectionOptions { host: string | null; password: string | null; user: string | null; port: number | null; database: string | null; client_encoding: string | null; ssl: boolean | null; application_name: string | null; fallback_application_name: string | null; } package/.coveralls.yml000644 000765 000024 0000000107 13151626464013330 0ustar00000000 000000 service_name: travis-pro repo_token: 5F6dODinz9L9uFR6HatKmtsYDoV1A5S2N package/.travis.yml000644 000765 000024 0000000133 13151626464012645 0ustar00000000 000000 language: node_js node_js: - '0.10' - '6.9' - '8' after_success: 'npm run coveralls' package/test/parse.js000644 000765 000024 0000014625 13151626464013176 0ustar00000000 000000 'use strict'; var chai = require('chai'); var expect = chai.expect; chai.should(); var parse = require('../').parse; describe('parse', function(){ 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'); }); it('escape spaces if present', function(){ var subject = parse('postgres://localhost/post gres'); subject.database.should.equal('post gres'); }); it('do not double escape spaces', function(){ var subject = parse('postgres://localhost/post%20gres'); subject.database.should.equal('post gres'); }); it('initializing with unix domain socket', function(){ var subject = parse('/var/run/'); subject.host.should.equal('/var/run/'); }); 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'); }); 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'); }); 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'); }); it('password contains < and/or > characters', function(){ var sourceConfig = { user:'brian', password: 'helloe', 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); }); 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); }); 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'); }); 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'); }); 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'); }); it('no pathname returns null database', function () { var subject = parse('pg://myhost'); (subject.database === null).should.equal(true); }); it('pathname of "/" returns null database', function () { var subject = parse('pg://myhost/'); subject.host.should.equal('myhost'); (subject.database === null).should.equal(true); }); 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'); }); });