123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- '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;
|