mirror of
https://github.com/brianc/node-postgres.git
synced 2026-01-18 15:55:05 +00:00
initial commit
This commit is contained in:
parent
88aafd73fa
commit
92c1fede8e
3
.travis.yml
Normal file
3
.travis.yml
Normal file
@ -0,0 +1,3 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- '0.10'
|
||||
14
README.md
14
README.md
@ -1,4 +1,18 @@
|
||||
pg-connection-string
|
||||
====================
|
||||
|
||||
[](https://travis-ci.org/iceddev/pg-connection-string)
|
||||
|
||||
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/sometable')
|
||||
```
|
||||
|
||||
54
index.js
Normal file
54
index.js
Normal file
@ -0,0 +1,54 @@
|
||||
'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) {
|
||||
var config;
|
||||
//unix socket
|
||||
if(str.charAt(0) === '/') {
|
||||
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(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;
|
||||
}
|
||||
|
||||
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;
|
||||
config.database = result.pathname ? decodeURI(result.pathname.slice(1)) : null;
|
||||
var auth = (result.auth || ':').split(':');
|
||||
config.user = auth[0];
|
||||
config.password = auth[1];
|
||||
config.port = result.port;
|
||||
|
||||
var ssl = result.query.ssl;
|
||||
if (ssl === 'true' || ssl === '1') {
|
||||
config.ssl = true;
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
parse: parse
|
||||
};
|
||||
29
package.json
Normal file
29
package.json
Normal file
@ -0,0 +1,29 @@
|
||||
{
|
||||
"name": "pg-connection-string",
|
||||
"version": "0.1.0",
|
||||
"description": "Functions for dealing with a PostgresSQL connection string",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "tap ./test"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/iceddev/pg-connection-string"
|
||||
},
|
||||
"keywords": [
|
||||
"pg",
|
||||
"connection",
|
||||
"string",
|
||||
"parse"
|
||||
],
|
||||
"author": "Blaine Bublitz <blaine@iceddev.com> (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": {
|
||||
"tap": "^0.4.11"
|
||||
}
|
||||
}
|
||||
89
test/parse.js
Normal file
89
test/parse.js
Normal file
@ -0,0 +1,89 @@
|
||||
'use strict';
|
||||
|
||||
var test = require('tap').test;
|
||||
|
||||
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();
|
||||
});
|
||||
|
||||
test('escape spaces if present', function(t){
|
||||
var subject = parse('postgres://localhost/post gres');
|
||||
t.equal(subject.database, 'post gres');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('do not double escape spaces', function(t){
|
||||
var subject = parse('postgres://localhost/post%20gres');
|
||||
t.equal(subject.database, 'post gres');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('initializing with unix domain socket', function(t){
|
||||
var subject = parse('/var/run/');
|
||||
t.equal(subject.host, '/var/run/');
|
||||
t.end();
|
||||
});
|
||||
|
||||
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();
|
||||
});
|
||||
|
||||
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();
|
||||
});
|
||||
|
||||
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();
|
||||
});
|
||||
|
||||
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();
|
||||
});
|
||||
|
||||
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();
|
||||
});
|
||||
|
||||
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();
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user