// Copyright (c) 2022-2024 Open Text. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. 'use strict' var url = require('url') var fs = require('fs') //Parse method copied from https://github.com/brianc/node-postgres //Original work Copyright (c) 2010-2014 Brian Carlson (brian.m.carlson@gmail.com) //Modified work Copyright (c) 2022-2024 Open Text. //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] } } // if the tls mode specified in the connection string is an invalid option, use the default - prefer. if (config.tls_mode && !['disable', 'require', 'verify-ca', 'verify-full'].includes(config.tls_mode)) { config.tls_mode = 'prefer' } 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 } else if (result.protocol !== 'vertica:') { throw new Error("Invalid connection string. Only vertica:// scheme is supported."); } 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) return config } module.exports = parse parse.parse = parse