diff --git a/packages/pg-connection-string/.coveralls.yml b/packages/pg-connection-string/.coveralls.yml new file mode 100644 index 00000000..0709f6e0 --- /dev/null +++ b/packages/pg-connection-string/.coveralls.yml @@ -0,0 +1,2 @@ +service_name: travis-pro +repo_token: 5F6dODinz9L9uFR6HatKmtsYDoV1A5S2N diff --git a/packages/pg-connection-string/.gitignore b/packages/pg-connection-string/.gitignore new file mode 100644 index 00000000..f28f01f7 --- /dev/null +++ b/packages/pg-connection-string/.gitignore @@ -0,0 +1,26 @@ +# 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.json \ No newline at end of file diff --git a/packages/pg-connection-string/.travis.yml b/packages/pg-connection-string/.travis.yml new file mode 100644 index 00000000..daf50ba6 --- /dev/null +++ b/packages/pg-connection-string/.travis.yml @@ -0,0 +1,6 @@ +language: node_js +node_js: + - '0.10' + - '6.9' + - '8' +after_success: 'npm run coveralls' diff --git a/packages/pg-connection-string/LICENSE b/packages/pg-connection-string/LICENSE new file mode 100644 index 00000000..b068a6cb --- /dev/null +++ b/packages/pg-connection-string/LICENSE @@ -0,0 +1,21 @@ +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. \ No newline at end of file diff --git a/packages/pg-connection-string/README.md b/packages/pg-connection-string/README.md new file mode 100644 index 00000000..d5b45ab9 --- /dev/null +++ b/packages/pg-connection-string/README.md @@ -0,0 +1,72 @@ +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/github/iceddev/pg-connection-string/badge.svg?branch=master)](https://coveralls.io/github/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') +``` + +The resulting config contains a subset of the following properties: + +* `host` - Postgres server hostname or, for UNIX doamain sockets, the socket filename +* `port` - port on which to connect +* `user` - User with which to authenticate to the server +* `password` - Corresponding password +* `database` - Database name within the server +* `client_encoding` - string encoding the client will use +* `ssl`, either a boolean or an object with properties + * `cert` + * `key` + * `ca` +* any other query parameters (for example, `application_name`) are preserved intact. + +## Connection Strings + +The short summary of acceptable URLs is: + + * `socket:?` - UNIX domain socket + * `postgres://:@:/?` - TCP connection + +But see below for more details. + +### UNIX Domain Sockets + +When user and password are not given, the socket path follows `socket:`, as in `socket:/var/run/pgsql`. +This form can be shortened to just a path: `/var/run/pgsql`. + +When user and password are given, they are included in the typical URL positions, with an empty `host`, as in `socket://user:pass@/var/run/pgsql`. + +Query parameters follow a `?` character, including the following special query parameters: + + * `db=` - sets the database name (urlencoded) + * `encoding=` - sets the `client_encoding` property + +### TCP Connections + +TCP connections to the Postgres server are indicated with `pg:` or `postgres:` schemes (in fact, any scheme but `socket:` is accepted). +If username and password are included, they should be urlencoded. +The database name, however, should *not* be urlencoded. + +Query parameters follow a `?` character, including the following special query parameters: + * `host=` - sets `host` property, overriding the URL's host + * `encoding=` - sets the `client_encoding` property + * `ssl=1`, `ssl=true`, `ssl=0`, `ssl=false` - sets `ssl` to true or false, accordingly + * `sslcert=` - reads data from the given file and includes the result as `ssl.cert` + * `sslkey=` - reads data from the given file and includes the result as `ssl.key` + * `sslrootcert=` - reads data from the given file and includes the result as `ssl.ca` + +A bare relative URL, such as `salesdata`, will indicate a database name while leaving other properties empty. diff --git a/packages/pg-connection-string/index.d.ts b/packages/pg-connection-string/index.d.ts new file mode 100644 index 00000000..1d2f1606 --- /dev/null +++ b/packages/pg-connection-string/index.d.ts @@ -0,0 +1,14 @@ +export function parse(connectionString: string): ConnectionOptions; + +export interface ConnectionOptions { + host: string | null; + password?: string; + user?: string; + port?: string | null; + database: string | null | undefined; + client_encoding?: string; + ssl?: boolean | string; + + application_name?: string; + fallback_application_name?: string; +} diff --git a/packages/pg-connection-string/index.js b/packages/pg-connection-string/index.js new file mode 100644 index 00000000..7e914ba1 --- /dev/null +++ b/packages/pg-connection-string/index.js @@ -0,0 +1,87 @@ +'use strict'; + +var url = require('url'); +var fs = require('fs'); + +//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]; + } + } + + var auth = (result.auth || ':').split(':'); + config.user = auth[0]; + config.password = auth.splice(1).join(':'); + + 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; + } + if (!config.host) { + // Only set the host if there is no equivalent query param. + 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 && /^%2f/i.test(pathname)) { + 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. + if (pathname && pathname.charAt(0) === '/') { + pathname = pathname.slice(1) || null; + } + config.database = pathname && decodeURI(pathname); + + if (config.ssl === 'true' || config.ssl === '1') { + config.ssl = true; + } + + if (config.ssl === '0') { + config.ssl = false; + } + + if (config.sslcert || config.sslkey || config.sslrootcert) { + config.ssl = {}; + } + + if (config.sslcert) { + config.ssl.cert = fs.readFileSync(config.sslcert).toString(); + } + + if (config.sslkey) { + config.ssl.key = fs.readFileSync(config.sslkey).toString(); + } + + if (config.sslrootcert) { + config.ssl.ca = fs.readFileSync(config.sslrootcert).toString(); + } + + return config; +} + + +module.exports = parse; + +parse.parse = parse; diff --git a/packages/pg-connection-string/package.json b/packages/pg-connection-string/package.json new file mode 100644 index 00000000..49345369 --- /dev/null +++ b/packages/pg-connection-string/package.json @@ -0,0 +1,39 @@ +{ + "name": "pg-connection-string", + "version": "2.2.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", + "coveralls": "^3.0.4", + "istanbul": "^0.4.5", + "mocha": "^3.5.0" + }, + "files": [ + "index.js", + "index.d.ts" + ] +} diff --git a/packages/pg-connection-string/test/example.ca b/packages/pg-connection-string/test/example.ca new file mode 100644 index 00000000..0a6dcf40 --- /dev/null +++ b/packages/pg-connection-string/test/example.ca @@ -0,0 +1 @@ +example ca diff --git a/packages/pg-connection-string/test/example.cert b/packages/pg-connection-string/test/example.cert new file mode 100644 index 00000000..7693b3fe --- /dev/null +++ b/packages/pg-connection-string/test/example.cert @@ -0,0 +1 @@ +example cert diff --git a/packages/pg-connection-string/test/example.key b/packages/pg-connection-string/test/example.key new file mode 100644 index 00000000..1aef9935 --- /dev/null +++ b/packages/pg-connection-string/test/example.key @@ -0,0 +1 @@ +example key diff --git a/packages/pg-connection-string/test/parse.js b/packages/pg-connection-string/test/parse.js new file mode 100644 index 00000000..07f886e1 --- /dev/null +++ b/packages/pg-connection-string/test/parse.js @@ -0,0 +1,257 @@ +'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('initializing with unix domain socket, username and password', function(){ + var subject = parse('socket://brian:pw@/var/run/?db=mydb'); + subject.user.should.equal('brian'); + subject.password.should.equal('pw'); + subject.host.should.equal('/var/run/'); + subject.database.should.equal('mydb'); + }); + + 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 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('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); + 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('configuration parameter ssl=0', function(){ + var connectionString = 'pg:///?ssl=0'; + var subject = parse(connectionString); + subject.ssl.should.equal(false); + }); + + it('set ssl', function () { + var subject = parse('pg://myhost/db?ssl=1'); + subject.ssl.should.equal(true); + }); + + it('configuration parameter sslcert=/path/to/cert', function(){ + var connectionString = 'pg:///?sslcert=' + __dirname + '/example.cert'; + var subject = parse(connectionString); + subject.ssl.should.eql({ + cert: 'example cert\n' + }); + }); + + it('configuration parameter sslkey=/path/to/key', function(){ + var connectionString = 'pg:///?sslkey=' + __dirname + '/example.key'; + var subject = parse(connectionString); + subject.ssl.should.eql({ + key: 'example key\n' + }); + }); + + it('configuration parameter sslrootcert=/path/to/ca', function(){ + var connectionString = 'pg:///?sslrootcert=' + __dirname + '/example.ca'; + var subject = parse(connectionString); + subject.ssl.should.eql({ + ca: 'example ca\n' + }); + }); + + 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'); + }); +});