pg-connection-string
Advanced tools
Comparing version 2.5.0 to 2.6.0
68
index.js
'use strict' | ||
var url = require('url') | ||
var fs = require('fs') | ||
//Parse method copied from https://github.com/brianc/node-postgres | ||
@@ -14,47 +11,49 @@ //Copyright (c) 2010-2014 Brian Carlson (brian.m.carlson@gmail.com) | ||
if (str.charAt(0) === '/') { | ||
var config = str.split(' ') | ||
const 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] | ||
} | ||
// Check for empty host in URL | ||
const config = {} | ||
let result | ||
let dummyHost = false | ||
if (/ |%[^a-f0-9]|%[a-f0-9][^a-f0-9]/i.test(str)) { | ||
// Ensure spaces are encoded as %20 | ||
str = encodeURI(str).replace(/\%25(\d\d)/g, '%$1') | ||
} | ||
var auth = (result.auth || ':').split(':') | ||
config.user = auth[0] | ||
config.password = auth.splice(1).join(':') | ||
try { | ||
result = new URL(str, 'postgres://base') | ||
} catch (e) { | ||
// The URL is invalid so try again with a dummy host | ||
result = new URL(str.replace('@/', '@___DUMMY___/'), 'postgres://base') | ||
dummyHost = true | ||
} | ||
// We'd like to use Object.fromEntries() here but Node.js 10 does not support it | ||
for (const entry of result.searchParams.entries()) { | ||
config[entry[0]] = entry[1] | ||
} | ||
config.user = config.user || decodeURIComponent(result.username) | ||
config.password = config.password || decodeURIComponent(result.password) | ||
config.port = result.port | ||
if (result.protocol == 'socket:') { | ||
config.host = decodeURI(result.pathname) | ||
config.database = result.query.db | ||
config.client_encoding = result.query.encoding | ||
config.database = result.searchParams.get('db') | ||
config.client_encoding = result.searchParams.get('encoding') | ||
return config | ||
} | ||
const hostname = dummyHost ? '' : result.hostname | ||
if (!config.host) { | ||
// Only set the host if there is no equivalent query param. | ||
config.host = result.hostname | ||
config.host = decodeURIComponent(hostname) | ||
} else if (hostname) { | ||
result.pathname = hostname + result.pathname | ||
} | ||
// 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) | ||
const pathname = result.pathname.slice(1) || null | ||
config.database = pathname ? decodeURI(pathname) : null | ||
@@ -73,2 +72,5 @@ if (config.ssl === 'true' || config.ssl === '1') { | ||
// Only try to load fs if we expect to read from the disk | ||
const fs = config.sslcert || config.sslkey || config.sslrootcert ? require('fs') : null | ||
if (config.sslcert) { | ||
@@ -75,0 +77,0 @@ config.ssl.cert = fs.readFileSync(config.sslcert).toString() |
{ | ||
"name": "pg-connection-string", | ||
"version": "2.5.0", | ||
"version": "2.6.0", | ||
"description": "Functions for dealing with a PostgresSQL connection string", | ||
@@ -39,3 +39,3 @@ "main": "./index.js", | ||
], | ||
"gitHead": "d45947938263bec30a1e3252452f04177b785f66" | ||
"gitHead": "14b840e96e57fc0617b5c4758f6318f774148ee4" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8885
101