Skip to content

Integrating generic connection-string #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ build/Release
# 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
package-lock.json

# IDE files and folders:
.idea/
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: node_js
node_js:
- '0.10'
- '6.9'
- '6'
- '8'
- '12'
after_success: 'npm run coveralls'
110 changes: 45 additions & 65 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,78 +1,58 @@
'use strict';

var url = require('url');
var fs = require('fs');
const fs = require('fs');
const ConnectionString = require('connection-string');

//Parse method copied from https://github.com/brianc/node-postgres
//Copyright (c) 2010-2014 Brian Carlson ([email protected])
//MIT License

//parses a connection string
// 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];

const cs = new ConnectionString(str);

const config = cs.params ? Object.assign({}, cs.params) : {};

// In this version we ignore multi-host support, using compatibility properties for the first host + port:
config.host = cs.hostname;
config.port = cs.port;

config.database = cs.path && cs.path[0];
config.user = cs.user;
config.password = cs.password;

if (config.encoding) {
config.client_encoding = config.encoding;
}

if (cs.hosts && cs.hosts[0].type === 'socket') {
return config;
}

if (config.ssl === 'true' || config.ssl === '1') {
config.ssl = true;
}
}

var auth = (result.auth || ':').split(':');
config.user = auth[0];
config.password = auth.splice(1).join(':');
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();
}

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);

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;
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "pg-connection-string",
"version": "2.1.0",
"version": "3.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",
"test": "istanbul cover node_modules/mocha/bin/_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"
},
Expand All @@ -25,12 +25,14 @@
"url": "https://github.com/iceddev/pg-connection-string/issues"
},
"homepage": "https://github.com/iceddev/pg-connection-string",
"dependencies": {},
"dependencies": {
"connection-string": "^2.6.0"
},
"devDependencies": {
"chai": "^4.1.1",
"coveralls": "^3.0.4",
"istanbul": "^0.4.5",
"mocha": "^3.5.0"
"mocha": "^6.1.4"
},
"files": [
"index.js",
Expand Down
Loading