chore(pg-connection-string): use tsx for tests

This commit is contained in:
Herman J. Radtke III 2025-04-20 08:14:17 -04:00
parent 9bfc967e91
commit 6be857e9d3
11 changed files with 1043 additions and 154 deletions

View File

@ -12,6 +12,7 @@ lib-cov
# Coverage directory used by tools like istanbul
coverage
.nyc_output
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
@ -23,4 +24,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
# TypeScript output directory
dist

View File

@ -0,0 +1,4 @@
{
"extension": ["js", "ts"],
"require": "tsx"
}

View File

@ -0,0 +1 @@
{"processes":{"7702f7d6-fe80-4107-8fc1-2818e7cb6a88":{"parent":null,"children":[]}},"files":{"/Users/herman/Code/node-postgres/packages/pg-connection-string/index.js":["7702f7d6-fe80-4107-8fc1-2818e7cb6a88"]},"externalIds":{}}

View File

@ -3,9 +3,6 @@ pg-connection-string
[![NPM](https://nodei.co/npm/pg-connection-string.png?compact=true)](https://nodei.co/npm/pg-connection-string/)
[![Build Status](https://travis-ci.org/iceddev/pg-connection-string.svg?branch=master)](https://travis-ci.org/iceddev/pg-connection-string)
[![Coverage Status](https://coveralls.io/repos/github/iceddev/pg-connection-string/badge.svg?branch=master)](https://coveralls.io/github/iceddev/pg-connection-string?branch=master)
Functions for dealing with a PostgresSQL connection string
`parse` method taken from [node-postgres](https://github.com/brianc/node-postgres.git)

View File

@ -7,6 +7,13 @@ export interface Options {
useLibpqCompat?: boolean
}
interface SSLConfig {
ca?: string
cert?: string | null
key?: string
rejectUnauthorized?: boolean
}
export interface ConnectionOptions {
host: string | null
password?: string
@ -14,11 +21,15 @@ export interface ConnectionOptions {
port?: string | null
database: string | null | undefined
client_encoding?: string
ssl?: boolean | string
ssl?: boolean | string | SSLConfig
application_name?: string
fallback_application_name?: string
options?: string
keepalives?: number
// We allow any other options to be passed through
[key: string]: unknown
}
export function toClientConfig(config: ConnectionOptions): ClientConfig

View File

@ -169,12 +169,8 @@ function toClientConfig(config) {
if (typeof sslConfig === 'boolean') {
c[key] = sslConfig
}
// else path is taken. multiple tests produce a sslConfig that is an object
// and we can console.log to see that we take this path
//
// see https://github.com/istanbuljs/babel-plugin-istanbul/issues/186#issuecomment-1137765139
// istanbul ignore else
else if (typeof sslConfig === 'object') {
if (typeof sslConfig === 'object') {
c[key] = toConnectionOptions(sslConfig)
}
} else if (value !== undefined && value !== null) {

View File

@ -13,9 +13,8 @@
}
},
"scripts": {
"test": "istanbul cover _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"
"test": "nyc --reporter=lcov mocha && npm run check-coverage",
"check-coverage": "nyc check-coverage --statements 100 --branches 100 --lines 100 --functions 100"
},
"repository": {
"type": "git",
@ -35,10 +34,14 @@
},
"homepage": "https://github.com/brianc/node-postgres/tree/master/packages/pg-connection-string",
"devDependencies": {
"@types/pg": "^8.12.0",
"chai": "^4.1.1",
"coveralls": "^3.0.4",
"istanbul": "^0.4.5",
"mocha": "^10.5.2"
"mocha": "^10.5.2",
"nyc": "^15",
"tsx": "^4.19.4",
"typescript": "^4.0.3"
},
"files": [
"index.js",

View File

@ -1,21 +1,19 @@
'use strict'
const chai = require('chai')
import chai from 'chai'
const expect = chai.expect
chai.should()
const { parse, toClientConfig, parseIntoClientConfig } = require('../')
import { parse, toClientConfig, parseIntoClientConfig } from '../'
describe('toClientConfig', function () {
it('converts connection info', function () {
const config = parse('postgres://brian:pw@boom:381/lala')
const clientConfig = toClientConfig(config)
clientConfig.user.should.equal('brian')
clientConfig.password.should.equal('pw')
clientConfig.host.should.equal('boom')
clientConfig.port.should.equal(381)
clientConfig.database.should.equal('lala')
clientConfig.user?.should.equal('brian')
clientConfig.password?.should.equal('pw')
clientConfig.host?.should.equal('boom')
clientConfig.port?.should.equal(381)
clientConfig.database?.should.equal('lala')
})
it('converts query params', function () {
@ -24,45 +22,47 @@ describe('toClientConfig', function () {
)
const clientConfig = toClientConfig(config)
clientConfig.application_name.should.equal('TheApp')
clientConfig.fallback_application_name.should.equal('TheAppFallback')
clientConfig.client_encoding.should.equal('utf8')
clientConfig.options.should.equal('-c geqo=off')
clientConfig.application_name?.should.equal('TheApp')
clientConfig.fallback_application_name?.should.equal('TheAppFallback')
clientConfig.client_encoding?.should.equal('utf8')
clientConfig.options?.should.equal('-c geqo=off')
})
it('converts SSL boolean', function () {
const config = parse('pg:///?ssl=true')
const clientConfig = toClientConfig(config)
clientConfig.ssl.should.equal(true)
clientConfig.ssl?.should.equal(true)
})
it('converts sslmode=disable', function () {
const config = parse('pg:///?sslmode=disable')
const clientConfig = toClientConfig(config)
clientConfig.ssl.should.equal(false)
clientConfig.ssl?.should.equal(false)
})
it('converts sslmode=noverify', function () {
const config = parse('pg:///?sslmode=no-verify')
const clientConfig = toClientConfig(config)
clientConfig.ssl.rejectUnauthorized.should.equal(false)
clientConfig.ssl?.should.deep.equal({
rejectUnauthorized: false,
})
})
it('converts other sslmode options', function () {
const config = parse('pg:///?sslmode=verify-ca')
const clientConfig = toClientConfig(config)
clientConfig.ssl.should.deep.equal({})
clientConfig.ssl?.should.deep.equal({})
})
it('converts other sslmode options', function () {
const config = parse('pg:///?sslmode=verify-ca')
const clientConfig = toClientConfig(config)
clientConfig.ssl.should.deep.equal({})
clientConfig.ssl?.should.deep.equal({})
})
it('converts ssl cert options', function () {
@ -77,7 +77,7 @@ describe('toClientConfig', function () {
const config = parse(connectionString)
const clientConfig = toClientConfig(config)
clientConfig.ssl.should.deep.equal({
clientConfig.ssl?.should.deep.equal({
ca: 'example ca\n',
cert: 'example cert\n',
key: 'example key\n',
@ -87,9 +87,9 @@ describe('toClientConfig', function () {
it('converts unix domain sockets', function () {
const config = parse('socket:/some path/?db=my[db]&encoding=utf8&client_encoding=bogus')
const clientConfig = toClientConfig(config)
clientConfig.host.should.equal('/some path/')
clientConfig.database.should.equal('my[db]', 'must to be escaped and unescaped through "my%5Bdb%5D"')
clientConfig.client_encoding.should.equal('utf8')
clientConfig.host?.should.equal('/some path/')
clientConfig.database?.should.equal('my[db]', 'must to be escaped and unescaped through "my%5Bdb%5D"')
clientConfig.client_encoding?.should.equal('utf8')
})
it('handles invalid port', function () {
@ -106,9 +106,9 @@ describe('toClientConfig', function () {
const clientConfig = toClientConfig(config)
clientConfig.host.should.equal('boom')
clientConfig.database.should.equal('lala')
clientConfig.ssl.should.deep.equal({})
clientConfig.host?.should.equal('boom')
clientConfig.database?.should.equal('lala')
clientConfig.ssl?.should.deep.equal({})
})
})
@ -116,10 +116,10 @@ describe('parseIntoClientConfig', function () {
it('converts url', function () {
const clientConfig = parseIntoClientConfig('postgres://brian:pw@boom:381/lala')
clientConfig.user.should.equal('brian')
clientConfig.password.should.equal('pw')
clientConfig.host.should.equal('boom')
clientConfig.port.should.equal(381)
clientConfig.database.should.equal('lala')
clientConfig.user?.should.equal('brian')
clientConfig.password?.should.equal('pw')
clientConfig.host?.should.equal('boom')
clientConfig.port?.should.equal(381)
clientConfig.database?.should.equal('lala')
})
})

View File

@ -1,62 +1,60 @@
'use strict'
const chai = require('chai')
import chai from 'chai'
const expect = chai.expect
chai.should()
const parse = require('../').parse
import { parse } from '../'
describe('parse', function () {
it('using connection string in client constructor', function () {
const subject = parse('postgres://brian:pw@boom:381/lala')
subject.user.should.equal('brian')
subject.password.should.equal('pw')
subject.host.should.equal('boom')
subject.port.should.equal('381')
subject.database.should.equal('lala')
subject.user?.should.equal('brian')
subject.password?.should.equal('pw')
subject.host?.should.equal('boom')
subject.port?.should.equal('381')
subject.database?.should.equal('lala')
})
it('escape spaces if present', function () {
const subject = parse('postgres://localhost/post gres')
subject.database.should.equal('post gres')
subject.database?.should.equal('post gres')
})
it('do not double escape spaces', function () {
const subject = parse('postgres://localhost/post%20gres')
subject.database.should.equal('post gres')
subject.database?.should.equal('post gres')
})
it('initializing with unix domain socket', function () {
const subject = parse('/var/run/')
subject.host.should.equal('/var/run/')
const subject = parse('/const/run/')
subject.host?.should.equal('/const/run/')
})
it('initializing with unix domain socket and a specific database, the simple way', function () {
const subject = parse('/var/run/ mydb')
subject.host.should.equal('/var/run/')
subject.database.should.equal('mydb')
const subject = parse('/const/run/ mydb')
subject.host?.should.equal('/const/run/')
subject.database?.should.equal('mydb')
})
it('initializing with unix domain socket, the health way', function () {
const subject = parse('socket:/some path/?db=my[db]&encoding=utf8')
subject.host.should.equal('/some path/')
subject.database.should.equal('my[db]', 'must to be escaped and unescaped trough "my%5Bdb%5D"')
subject.client_encoding.should.equal('utf8')
subject.host?.should.equal('/some path/')
subject.database?.should.equal('my[db]', 'must to be escaped and unescaped trough "my%5Bdb%5D"')
subject.client_encoding?.should.equal('utf8')
})
it('initializing with unix domain socket, the escaped health way', function () {
const subject = parse('socket:/some%20path/?db=my%2Bdb&encoding=utf8')
subject.host.should.equal('/some path/')
subject.database.should.equal('my+db')
subject.client_encoding.should.equal('utf8')
subject.host?.should.equal('/some path/')
subject.database?.should.equal('my+db')
subject.client_encoding?.should.equal('utf8')
})
it('initializing with unix domain socket, username and password', function () {
const subject = parse('socket://brian:pw@/var/run/?db=mydb')
subject.user.should.equal('brian')
subject.password.should.equal('pw')
subject.host.should.equal('/var/run/')
subject.database.should.equal('mydb')
const subject = parse('socket://brian:pw@/const/run/?db=mydb')
subject.user?.should.equal('brian')
subject.password?.should.equal('pw')
subject.host?.should.equal('/const/run/')
subject.database?.should.equal('mydb')
})
it('password contains < and/or > characters', function () {
@ -79,7 +77,7 @@ describe('parse', function () {
'/' +
sourceConfig.database
const subject = parse(connectionString)
subject.password.should.equal(sourceConfig.password)
subject.password?.should.equal(sourceConfig.password)
})
it('password contains colons', function () {
@ -102,30 +100,30 @@ describe('parse', function () {
'/' +
sourceConfig.database
const subject = parse(connectionString)
subject.password.should.equal(sourceConfig.password)
subject.password?.should.equal(sourceConfig.password)
})
it('username or password contains weird characters', function () {
const strang = 'pg://my f%irst name:is&%awesome!@localhost:9000'
const subject = parse(strang)
subject.user.should.equal('my f%irst name')
subject.password.should.equal('is&%awesome!')
subject.host.should.equal('localhost')
subject.user?.should.equal('my f%irst name')
subject.password?.should.equal('is&%awesome!')
subject.host?.should.equal('localhost')
})
it('url is properly encoded', function () {
const encoded = 'pg://bi%25na%25%25ry%20:s%40f%23@localhost/%20u%2520rl'
const subject = parse(encoded)
subject.user.should.equal('bi%na%%ry ')
subject.password.should.equal('s@f#')
subject.host.should.equal('localhost')
subject.database.should.equal(' u%20rl')
subject.user?.should.equal('bi%na%%ry ')
subject.password?.should.equal('s@f#')
subject.host?.should.equal('localhost')
subject.database?.should.equal(' u%20rl')
})
it('relative url sets database', function () {
const relative = 'different_db_on_default_host'
const subject = parse(relative)
subject.database.should.equal('different_db_on_default_host')
subject.database?.should.equal('different_db_on_default_host')
})
it('no pathname returns null database', function () {
@ -135,93 +133,93 @@ describe('parse', function () {
it('pathname of "/" returns null database', function () {
const subject = parse('pg://myhost/')
subject.host.should.equal('myhost')
subject.host?.should.equal('myhost')
;(subject.database === null).should.equal(true)
})
it('configuration parameter host', function () {
const subject = parse('pg://user:pass@/dbname?host=/unix/socket')
subject.user.should.equal('user')
subject.password.should.equal('pass')
subject.host.should.equal('/unix/socket')
subject.database.should.equal('dbname')
subject.user?.should.equal('user')
subject.password?.should.equal('pass')
subject.host?.should.equal('/unix/socket')
subject.database?.should.equal('dbname')
})
it('configuration parameter host overrides url host', function () {
const subject = parse('pg://user:pass@localhost/dbname?host=/unix/socket')
subject.database.should.equal('dbname')
subject.host.should.equal('/unix/socket')
subject.database?.should.equal('dbname')
subject.host?.should.equal('/unix/socket')
})
it('url with encoded socket', function () {
const subject = parse('pg://user:pass@%2Funix%2Fsocket/dbname')
subject.user.should.equal('user')
subject.password.should.equal('pass')
subject.host.should.equal('/unix/socket')
subject.database.should.equal('dbname')
subject.user?.should.equal('user')
subject.password?.should.equal('pass')
subject.host?.should.equal('/unix/socket')
subject.database?.should.equal('dbname')
})
it('url with real host and an encoded db name', function () {
const subject = parse('pg://user:pass@localhost/%2Fdbname')
subject.user.should.equal('user')
subject.password.should.equal('pass')
subject.host.should.equal('localhost')
subject.database.should.equal('%2Fdbname')
subject.user?.should.equal('user')
subject.password?.should.equal('pass')
subject.host?.should.equal('localhost')
subject.database?.should.equal('%2Fdbname')
})
it('configuration parameter host treats encoded host as part of the db name', function () {
const subject = parse('pg://user:pass@%2Funix%2Fsocket/dbname?host=localhost')
subject.user.should.equal('user')
subject.password.should.equal('pass')
subject.host.should.equal('localhost')
subject.database.should.equal('%2Funix%2Fsocket/dbname')
subject.user?.should.equal('user')
subject.password?.should.equal('pass')
subject.host?.should.equal('localhost')
subject.database?.should.equal('%2Funix%2Fsocket/dbname')
})
it('configuration parameter application_name', function () {
const connectionString = 'pg:///?application_name=TheApp'
const subject = parse(connectionString)
subject.application_name.should.equal('TheApp')
subject.application_name?.should.equal('TheApp')
})
it('configuration parameter fallback_application_name', function () {
const connectionString = 'pg:///?fallback_application_name=TheAppFallback'
const subject = parse(connectionString)
subject.fallback_application_name.should.equal('TheAppFallback')
subject.fallback_application_name?.should.equal('TheAppFallback')
})
it('configuration parameter options', function () {
const connectionString = 'pg:///?options=-c geqo=off'
const subject = parse(connectionString)
subject.options.should.equal('-c geqo=off')
subject.options?.should.equal('-c geqo=off')
})
it('configuration parameter ssl=true', function () {
const connectionString = 'pg:///?ssl=true'
const subject = parse(connectionString)
subject.ssl.should.equal(true)
subject.ssl?.should.equal(true)
})
it('configuration parameter ssl=1', function () {
const connectionString = 'pg:///?ssl=1'
const subject = parse(connectionString)
subject.ssl.should.equal(true)
subject.ssl?.should.equal(true)
})
it('configuration parameter ssl=0', function () {
const connectionString = 'pg:///?ssl=0'
const subject = parse(connectionString)
subject.ssl.should.equal(false)
subject.ssl?.should.equal(false)
})
it('set ssl', function () {
const subject = parse('pg://myhost/db?ssl=1')
subject.ssl.should.equal(true)
subject.ssl?.should.equal(true)
})
it('configuration parameter sslcert=/path/to/cert', function () {
const connectionString = 'pg:///?sslcert=' + __dirname + '/example.cert'
const subject = parse(connectionString)
subject.ssl.should.eql({
subject.ssl?.should.eql({
cert: 'example cert\n',
})
})
@ -229,7 +227,7 @@ describe('parse', function () {
it('configuration parameter sslkey=/path/to/key', function () {
const connectionString = 'pg:///?sslkey=' + __dirname + '/example.key'
const subject = parse(connectionString)
subject.ssl.should.eql({
subject.ssl?.should.eql({
key: 'example key\n',
})
})
@ -237,7 +235,7 @@ describe('parse', function () {
it('configuration parameter sslrootcert=/path/to/ca', function () {
const connectionString = 'pg:///?sslrootcert=' + __dirname + '/example.ca'
const subject = parse(connectionString)
subject.ssl.should.eql({
subject.ssl?.should.eql({
ca: 'example ca\n',
})
})
@ -245,7 +243,7 @@ describe('parse', function () {
it('configuration parameter sslmode=no-verify', function () {
const connectionString = 'pg:///?sslmode=no-verify'
const subject = parse(connectionString)
subject.ssl.should.eql({
subject.ssl?.should.eql({
rejectUnauthorized: false,
})
})
@ -253,37 +251,37 @@ describe('parse', function () {
it('configuration parameter sslmode=disable', function () {
const connectionString = 'pg:///?sslmode=disable'
const subject = parse(connectionString)
subject.ssl.should.eql(false)
subject.ssl?.should.eql(false)
})
it('configuration parameter sslmode=prefer', function () {
const connectionString = 'pg:///?sslmode=prefer'
const subject = parse(connectionString)
subject.ssl.should.eql({})
subject.ssl?.should.eql({})
})
it('configuration parameter sslmode=require', function () {
const connectionString = 'pg:///?sslmode=require'
const subject = parse(connectionString)
subject.ssl.should.eql({})
subject.ssl?.should.eql({})
})
it('configuration parameter sslmode=verify-ca', function () {
const connectionString = 'pg:///?sslmode=verify-ca'
const subject = parse(connectionString)
subject.ssl.should.eql({})
subject.ssl?.should.eql({})
})
it('configuration parameter sslmode=verify-full', function () {
const connectionString = 'pg:///?sslmode=verify-full'
const subject = parse(connectionString)
subject.ssl.should.eql({})
subject.ssl?.should.eql({})
})
it('configuration parameter ssl=true and sslmode=require still work with sslrootcert=/path/to/ca', function () {
const connectionString = 'pg:///?ssl=true&sslrootcert=' + __dirname + '/example.ca&sslmode=require'
const subject = parse(connectionString)
subject.ssl.should.eql({
subject.ssl?.should.eql({
ca: 'example ca\n',
})
})
@ -291,13 +289,13 @@ describe('parse', function () {
it('configuration parameter sslmode=disable with uselibpqcompat query param', function () {
const connectionString = 'pg:///?sslmode=disable&uselibpqcompat=true'
const subject = parse(connectionString)
subject.ssl.should.eql(false)
subject.ssl?.should.eql(false)
})
it('configuration parameter sslmode=prefer with uselibpqcompat query param', function () {
const connectionString = 'pg:///?sslmode=prefer&uselibpqcompat=true'
const subject = parse(connectionString)
subject.ssl.should.eql({
subject.ssl?.should.eql({
rejectUnauthorized: false,
})
})
@ -305,7 +303,7 @@ describe('parse', function () {
it('configuration parameter sslmode=require with uselibpqcompat query param', function () {
const connectionString = 'pg:///?sslmode=require&uselibpqcompat=true'
const subject = parse(connectionString)
subject.ssl.should.eql({
subject.ssl?.should.eql({
rejectUnauthorized: false,
})
})
@ -320,35 +318,43 @@ describe('parse', function () {
it('configuration parameter sslmode=verify-ca and sslrootcert with uselibpqcompat query param', function () {
const connectionString = 'pg:///?sslmode=verify-ca&uselibpqcompat=true&sslrootcert=' + __dirname + '/example.ca'
const subject = parse(connectionString)
subject.ssl.should.have.property('checkServerIdentity').that.is.a('function')
subject.ssl?.should.have.property('checkServerIdentity').that.is.a('function')
// We prove above that the checkServerIdentity function is defined
//
// FIXME: remove this if we upgrade to TypeScript 5
// @ts-ignore
expect(subject.ssl.checkServerIdentity()).be.undefined
})
it('configuration parameter sslmode=verify-full with uselibpqcompat query param', function () {
const connectionString = 'pg:///?sslmode=verify-full&uselibpqcompat=true'
const subject = parse(connectionString)
subject.ssl.should.eql({})
subject.ssl?.should.eql({})
})
it('configuration parameter ssl=true and sslmode=require still work with sslrootcert=/path/to/ca with uselibpqcompat query param', function () {
const connectionString =
'pg:///?ssl=true&sslrootcert=' + __dirname + '/example.ca&sslmode=require&uselibpqcompat=true'
const subject = parse(connectionString)
subject.ssl.should.have.property('ca', 'example ca\n')
subject.ssl.should.have.property('checkServerIdentity').that.is.a('function')
expect(subject.ssl.checkServerIdentity()).be.undefined
subject.ssl?.should.have.property('ca', 'example ca\n')
subject.ssl?.should.have.property('checkServerIdentity').that.is.a('function')
// We prove above that the checkServerIdentity function is defined
//
// FIXME: remove this if we upgrade to TypeScript 5
// @ts-ignore
expect(subject.ssl?.checkServerIdentity()).be.undefined
})
it('configuration parameter sslmode=disable with useLibpqCompat option', function () {
const connectionString = 'pg:///?sslmode=disable'
const subject = parse(connectionString, { useLibpqCompat: true })
subject.ssl.should.eql(false)
subject.ssl?.should.eql(false)
})
it('configuration parameter sslmode=prefer with useLibpqCompat option', function () {
const connectionString = 'pg:///?sslmode=prefer'
const subject = parse(connectionString, { useLibpqCompat: true })
subject.ssl.should.eql({
subject.ssl?.should.eql({
rejectUnauthorized: false,
})
})
@ -356,7 +362,7 @@ describe('parse', function () {
it('configuration parameter sslmode=require with useLibpqCompat option', function () {
const connectionString = 'pg:///?sslmode=require'
const subject = parse(connectionString, { useLibpqCompat: true })
subject.ssl.should.eql({
subject.ssl?.should.eql({
rejectUnauthorized: false,
})
})
@ -371,22 +377,30 @@ describe('parse', function () {
it('configuration parameter sslmode=verify-ca and sslrootcert with useLibpqCompat option', function () {
const connectionString = 'pg:///?sslmode=verify-ca&sslrootcert=' + __dirname + '/example.ca'
const subject = parse(connectionString, { useLibpqCompat: true })
subject.ssl.should.have.property('checkServerIdentity').that.is.a('function')
expect(subject.ssl.checkServerIdentity()).be.undefined
subject.ssl?.should.have.property('checkServerIdentity').that.is.a('function')
// We prove above that the checkServerIdentity function is defined
//
// FIXME: remove this if we upgrade to TypeScript 5
// @ts-ignore
expect(subject.ssl?.checkServerIdentity()).be.undefined
})
it('configuration parameter sslmode=verify-full with useLibpqCompat option', function () {
const connectionString = 'pg:///?sslmode=verify-full'
const subject = parse(connectionString, { useLibpqCompat: true })
subject.ssl.should.eql({})
subject.ssl?.should.eql({})
})
it('configuration parameter ssl=true and sslmode=require still work with sslrootcert=/path/to/ca with useLibpqCompat option', function () {
const connectionString = 'pg:///?ssl=true&sslrootcert=' + __dirname + '/example.ca&sslmode=require'
const subject = parse(connectionString, { useLibpqCompat: true })
subject.ssl.should.have.property('ca', 'example ca\n')
subject.ssl.should.have.property('checkServerIdentity').that.is.a('function')
expect(subject.ssl.checkServerIdentity()).be.undefined
subject.ssl?.should.have.property('ca', 'example ca\n')
subject.ssl?.should.have.property('checkServerIdentity').that.is.a('function')
// We prove above that the checkServerIdentity function is defined
//
// FIXME: remove this if we upgrade to TypeScript 5
// @ts-ignore
expect(subject.ssl?.checkServerIdentity()).be.undefined
})
it('does not allow sslcompat query parameter and useLibpqCompat option at the same time', function () {
@ -398,38 +412,38 @@ describe('parse', function () {
it('allow other params like max, ...', function () {
const subject = parse('pg://myhost/db?max=18&min=4')
subject.max.should.equal('18')
subject.min.should.equal('4')
subject.max?.should.equal('18')
subject.min?.should.equal('4')
})
it('configuration parameter keepalives', function () {
const connectionString = 'pg:///?keepalives=1'
const subject = parse(connectionString)
subject.keepalives.should.equal('1')
subject.keepalives?.should.equal('1')
})
it('unknown configuration parameter is passed into client', function () {
const connectionString = 'pg:///?ThereIsNoSuchPostgresParameter=1234'
const subject = parse(connectionString)
subject.ThereIsNoSuchPostgresParameter.should.equal('1234')
subject.ThereIsNoSuchPostgresParameter?.should.equal('1234')
})
it('do not override a config field with value from query string', function () {
const subject = parse('socket:/some path/?db=my[db]&encoding=utf8&client_encoding=bogus')
subject.host.should.equal('/some path/')
subject.database.should.equal('my[db]', 'must to be escaped and unescaped through "my%5Bdb%5D"')
subject.client_encoding.should.equal('utf8')
subject.host?.should.equal('/some path/')
subject.database?.should.equal('my[db]', 'must to be escaped and unescaped through "my%5Bdb%5D"')
subject.client_encoding?.should.equal('utf8')
})
it('return last value of repeated parameter', function () {
const connectionString = 'pg:///?keepalives=1&keepalives=0'
const subject = parse(connectionString)
subject.keepalives.should.equal('0')
subject.keepalives?.should.equal('0')
})
it('use the port specified in the query parameters', function () {
const connectionString = 'postgres:///?host=localhost&port=1234'
const subject = parse(connectionString)
subject.port.should.equal('1234')
subject.port?.should.equal('1234')
})
})

View File

@ -0,0 +1,19 @@
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"target": "es6",
"noImplicitAny": true,
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"incremental": true,
"baseUrl": ".",
"declaration": true
},
"include": [
"test/**/*"
]
}

860
yarn.lock

File diff suppressed because it is too large Load Diff