Skip to content

Include pg-connection-string into monorepo #2184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 49 commits into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
88aafd7
Initial commit
phated Jul 5, 2014
92c1fed
initial commit
phated Jul 5, 2014
df2a24c
attach port always - ref brianc/node-postgres#604
phated Jul 6, 2014
cb9bee1
0.1.1
phated Jul 6, 2014
ba511f7
Support usage of relative urls to set database on the default host
Sep 12, 2014
c502985
Merge pull request #1 from slickmb/task/relative_url
phated Sep 13, 2014
245abd6
0.1.2
phated Sep 13, 2014
fbdd033
Add supporting password with colon
Sep 26, 2014
0a07e3d
Merge pull request #2 from asynxis/master
phated Sep 26, 2014
4c151b9
0.1.3
phated Sep 26, 2014
c612dfa
Update README.md
misaxi Oct 12, 2015
07a7143
Merge pull request #6 from misaxi/master
phated Oct 12, 2015
cdf06ed
Copy all but special-cased params from URL query string to config
motiz88 Dec 30, 2015
54c2044
feat: add basic typings
NoNameProvided Apr 27, 2017
e6643e4
Merge pull request #7 from motiz88/extra-keys
monteslu Jun 27, 2017
45d8232
Merge pull request #10 from NoNameProvided/patch-1
monteslu Jun 27, 2017
9ab62ff
allow min/max params for pg-pool
caub Jul 1, 2017
cf107b0
Merge pull request #11 from caub/max
monteslu Aug 30, 2017
1368735
Use mocha, istanbul, and coveralls (#16)
monteslu Aug 30, 2017
eafb7ac
2.0.0
monteslu Aug 30, 2017
279fdea
Add supporting username and password for socket connections
ywkim Nov 22, 2017
929fcb7
Fix typings
benny-medflyt Dec 13, 2017
ece7645
typings: turns out "host" can actually be `null`
benny-medflyt Dec 13, 2017
c11dbb1
Only publish the required files
benjie Apr 18, 2019
e9270e8
Add support for TLS parameters in URI
hjr3 May 17, 2019
ac63695
Merge pull request #26 from hjr3/ssl-support
hjr3 May 23, 2019
b53c2bd
Merge pull request #24 from benjie/patch-1
hjr3 May 23, 2019
43114f4
Merge pull request #17 from ywkim/master
hjr3 May 23, 2019
7b62226
ssl=0 is now parses to false
hjr3 May 23, 2019
d7b96e6
Merge pull request #28 from hjr3/ssl-parsing
hjr3 May 23, 2019
726f620
Update index.d.ts
benny-medflyt May 27, 2019
4db1a7e
Merge pull request #18 from benny-medflyt/patch-1
hjr3 Jun 11, 2019
06c46ac
2.1.0
abowerman Jun 19, 2019
c9ee9cd
Update coveralls badge
abowerman Jun 19, 2019
0f72f29
Merge pull request #30 from iceddev/fix-coverall-badge
monteslu Jun 19, 2019
c75c392
fix readme newline typo
abowerman Jun 19, 2019
e4c1002
actually include coveralls
abowerman Jun 19, 2019
a483bdf
Merge pull request #31 from iceddev/fix-coverall-badge
monteslu Jun 19, 2019
0ff40e7
host= query param takes precedence
danielrozenberg Jan 29, 2020
b309db0
Support URL-encoded socket names
danielrozenberg Jan 29, 2020
7ec9b70
Use regex instead of startsWith which is unsupported in node 0.10
danielrozenberg Jan 29, 2020
9b89828
Merge pull request #34 from danielrozenberg/support-other-socket-modes
hjr3 Feb 1, 2020
b4e0ba3
Include documentation on the URL format in the README
djmitche Feb 16, 2020
4356679
Merge pull request #36 from djmitche/format-docs
hjr3 Feb 17, 2020
5233b3e
Release v2.2.0
hjr3 Mar 20, 2020
4aff01e
Add 'packages/pg-connection-string/' from commit '5233b3e77e396a36813…
brianc Apr 28, 2020
3a831fc
Run lint --fix
brianc Apr 28, 2020
16344cb
Update test command for travis
brianc Apr 28, 2020
ddf8112
Check for the correct binary
brianc Apr 28, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
{
"plugins": [
"prettier"
],
"plugins": ["prettier"],
"parser": "@typescript-eslint/parser",
"extends": [
"plugin:prettier/recommended",
"prettier/@typescript-eslint"
],
"ignorePatterns": [
"node_modules",
"packages/pg-protocol/dist/**/*"
],
"extends": ["plugin:prettier/recommended", "prettier/@typescript-eslint"],
"ignorePatterns": ["node_modules", "coverage", "packages/pg-protocol/dist/**/*"],
"parserOptions": {
"ecmaVersion": 2017,
"sourceType": "module"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"test": "yarn lint && yarn lerna exec yarn test",
"build": "yarn lerna exec --scope pg-protocol yarn build",
"pretest": "yarn build",
"lint": "!([[ -e node_modules/.bin/prettier ]]) || eslint '*/**/*.{js,ts,tsx}'"
"lint": "if [ -x ./node_modules/.bin/prettier ]; then eslint '*/**/*.{js,ts,tsx}'; fi;"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^2.27.0",
Expand Down
2 changes: 2 additions & 0 deletions packages/pg-connection-string/.coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
service_name: travis-pro
repo_token: 5F6dODinz9L9uFR6HatKmtsYDoV1A5S2N
26 changes: 26 additions & 0 deletions packages/pg-connection-string/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# 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
6 changes: 6 additions & 0 deletions packages/pg-connection-string/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
language: node_js
node_js:
- '0.10'
- '6.9'
- '8'
after_success: 'npm run coveralls'
21 changes: 21 additions & 0 deletions packages/pg-connection-string/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2014 Iced Development

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
72 changes: 72 additions & 0 deletions packages/pg-connection-string/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
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)
Copyright (c) 2010-2014 Brian Carlson ([email protected])
MIT License

## Usage

```js
var parse = require('pg-connection-string').parse;

var config = parse('postgres://someuser:somepassword@somehost:381/somedatabase')
```

The resulting config contains a subset of the following properties:

* `host` - Postgres server hostname or, for UNIX doamain sockets, the socket filename
* `port` - port on which to connect
* `user` - User with which to authenticate to the server
* `password` - Corresponding password
* `database` - Database name within the server
* `client_encoding` - string encoding the client will use
* `ssl`, either a boolean or an object with properties
* `cert`
* `key`
* `ca`
* any other query parameters (for example, `application_name`) are preserved intact.

## Connection Strings

The short summary of acceptable URLs is:

* `socket:<path>?<query>` - UNIX domain socket
* `postgres://<user>:<password>@<host>:<port>/<database>?<query>` - TCP connection

But see below for more details.

### UNIX Domain Sockets

When user and password are not given, the socket path follows `socket:`, as in `socket:/var/run/pgsql`.
This form can be shortened to just a path: `/var/run/pgsql`.

When user and password are given, they are included in the typical URL positions, with an empty `host`, as in `socket://user:pass@/var/run/pgsql`.

Query parameters follow a `?` character, including the following special query parameters:

* `db=<database>` - sets the database name (urlencoded)
* `encoding=<encoding>` - sets the `client_encoding` property

### TCP Connections

TCP connections to the Postgres server are indicated with `pg:` or `postgres:` schemes (in fact, any scheme but `socket:` is accepted).
If username and password are included, they should be urlencoded.
The database name, however, should *not* be urlencoded.

Query parameters follow a `?` character, including the following special query parameters:
* `host=<host>` - sets `host` property, overriding the URL's host
* `encoding=<encoding>` - sets the `client_encoding` property
* `ssl=1`, `ssl=true`, `ssl=0`, `ssl=false` - sets `ssl` to true or false, accordingly
* `sslcert=<filename>` - reads data from the given file and includes the result as `ssl.cert`
* `sslkey=<filename>` - reads data from the given file and includes the result as `ssl.key`
* `sslrootcert=<filename>` - reads data from the given file and includes the result as `ssl.ca`

A bare relative URL, such as `salesdata`, will indicate a database name while leaving other properties empty.
14 changes: 14 additions & 0 deletions packages/pg-connection-string/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export function parse(connectionString: string): ConnectionOptions

export interface ConnectionOptions {
host: string | null
password?: string
user?: string
port?: string | null
database: string | null | undefined
client_encoding?: string
ssl?: boolean | string

application_name?: string
fallback_application_name?: string
}
89 changes: 89 additions & 0 deletions packages/pg-connection-string/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
'use strict'

var url = require('url')
var fs = require('fs')

//Parse method copied from https://github.com/brianc/node-postgres
//Copyright (c) 2010-2014 Brian Carlson ([email protected])
//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]
}
}

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
}
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)

if (config.ssl === 'true' || config.ssl === '1') {
config.ssl = true
}

if (config.ssl === '0') {
config.ssl = false
}

if (config.sslcert || config.sslkey || config.sslrootcert) {
config.ssl = {}
}

if (config.sslcert) {
config.ssl.cert = fs.readFileSync(config.sslcert).toString()
}

if (config.sslkey) {
config.ssl.key = fs.readFileSync(config.sslkey).toString()
}

if (config.sslrootcert) {
config.ssl.ca = fs.readFileSync(config.sslrootcert).toString()
}

return config
}

module.exports = parse

parse.parse = parse
39 changes: 39 additions & 0 deletions packages/pg-connection-string/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "pg-connection-string",
"version": "2.2.0",
"description": "Functions for dealing with a PostgresSQL connection string",
"main": "./index.js",
"types": "./index.d.ts",
"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"
},
"repository": {
"type": "git",
"url": "https://github.com/iceddev/pg-connection-string"
},
"keywords": [
"pg",
"connection",
"string",
"parse"
],
"author": "Blaine Bublitz <[email protected]> (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": {
"chai": "^4.1.1",
"coveralls": "^3.0.4",
"istanbul": "^0.4.5",
"mocha": "^3.5.0"
},
"files": [
"index.js",
"index.d.ts"
]
}
1 change: 1 addition & 0 deletions packages/pg-connection-string/test/example.ca
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
example ca
1 change: 1 addition & 0 deletions packages/pg-connection-string/test/example.cert
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
example cert
1 change: 1 addition & 0 deletions packages/pg-connection-string/test/example.key
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
example key
Loading