GeezerVison is showing github.com — Merge pull request #2184 from brianc/bmc/pg-connection-string · brianc/node-postgres@e93ecac
File treeExpand file treeCollapse file tree Open diff view settings packages/pg-connection-string
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: .eslintrc +3-11Lines changed: 3 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | { | 2 | | - "plugins": [ | 3 | | - "prettier" | 4 | | - ], | | 2 | + "plugins": ["prettier"], | 5 | 3 | "parser": "@typescript-eslint/parser", | 6 | | - "extends": [ | 7 | | - "plugin:prettier/recommended", | 8 | | - "prettier/@typescript-eslint" | 9 | | - ], | 10 | | - "ignorePatterns": [ | 11 | | - "node_modules", | 12 | | - "packages/pg-protocol/dist/**/*" | 13 | | - ], | | 4 | + "extends": ["plugin:prettier/recommended", "prettier/@typescript-eslint"], | | 5 | + "ignorePatterns": ["node_modules", "coverage", "packages/pg-protocol/dist/**/*"], | 14 | 6 | "parserOptions": { | 15 | 7 | "ecmaVersion": 2017, | 16 | 8 | "sourceType": "module" | |
Collapse file Copy file name to clipboardExpand all lines: package.json +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 13 | 13 | "test": "yarn lint && yarn lerna exec yarn test", | 14 | 14 | "build": "yarn lerna exec --scope pg-protocol yarn build", | 15 | 15 | "pretest": "yarn build", | 16 | | - "lint": "!([[ -e node_modules/.bin/prettier ]]) || eslint '*/**/*.{js,ts,tsx}'" | | 16 | + "lint": "if [ -x ./node_modules/.bin/prettier ]; then eslint '*/**/*.{js,ts,tsx}'; fi;" | 17 | 17 | }, | 18 | 18 | "devDependencies": { | 19 | 19 | "@typescript-eslint/eslint-plugin": "^2.27.0", | |
Collapse file Copy file name to clipboard +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +service_name: travis-pro | | 2 | +repo_token: 5F6dODinz9L9uFR6HatKmtsYDoV1A5S2N |
Collapse file Copy file name to clipboard +26Lines changed: 26 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Logs | | 2 | +logs | | 3 | +*.log | | 4 | + | | 5 | +# Runtime data | | 6 | +pids | | 7 | +*.pid | | 8 | +*.seed | | 9 | + | | 10 | +# Directory for instrumented libs generated by jscoverage/JSCover | | 11 | +lib-cov | | 12 | + | | 13 | +# Coverage directory used by tools like istanbul | | 14 | +coverage | | 15 | + | | 16 | +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | | 17 | +.grunt | | 18 | + | | 19 | +# Compiled binary addons (http://nodejs.org/api/addons.html) | | 20 | +build/Release | | 21 | + | | 22 | +# Dependency directory | | 23 | +# Deployed apps should consider commenting this line out: | | 24 | +# see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git | | 25 | +node_modules | | 26 | +package-lock.json |
Collapse file Copy file name to clipboard +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +language: node_js | | 2 | +node_js: | | 3 | + - '0.10' | | 4 | + - '6.9' | | 5 | + - '8' | | 6 | +after_success: 'npm run coveralls' |
Collapse file Copy file name to clipboard +21Lines changed: 21 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +The MIT License (MIT) | | 2 | + | | 3 | +Copyright (c) 2014 Iced Development | | 4 | + | | 5 | +Permission is hereby granted, free of charge, to any person obtaining a copy | | 6 | +of this software and associated documentation files (the "Software"), to deal | | 7 | +in the Software without restriction, including without limitation the rights | | 8 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | | 9 | +copies of the Software, and to permit persons to whom the Software is | | 10 | +furnished to do so, subject to the following conditions: | | 11 | + | | 12 | +The above copyright notice and this permission notice shall be included in all | | 13 | +copies or substantial portions of the Software. | | 14 | + | | 15 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | | 16 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | | 17 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | | 18 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | | 19 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | | 20 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | | 21 | +SOFTWARE. |
Collapse file Copy file name to clipboard +72Lines changed: 72 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +pg-connection-string | | 2 | +==================== | | 3 | + | | 4 | +[](https://nodei.co/npm/pg-connection-string/) | | 5 | + | | 6 | +[](https://travis-ci.org/iceddev/pg-connection-string) | | 7 | +[](https://coveralls.io/github/iceddev/pg-connection-string?branch=master) | | 8 | + | | 9 | +Functions for dealing with a PostgresSQL connection string | | 10 | + | | 11 | +`parse` method taken from [node-postgres](https://github.com/brianc/node-postgres.git) | | 12 | + | | 13 | +MIT License | | 14 | + | | 15 | +## Usage | | 16 | + | | 17 | +```js | | 18 | +var parse = require('pg-connection-string').parse; | | 19 | + | | 20 | +var config = parse('postgres://someuser:somepassword@somehost:381/somedatabase') | | 21 | +``` | | 22 | + | | 23 | +The resulting config contains a subset of the following properties: | | 24 | + | | 25 | +* `host` - Postgres server hostname or, for UNIX doamain sockets, the socket filename | | 26 | +* `port` - port on which to connect | | 27 | +* `user` - User with which to authenticate to the server | | 28 | +* `password` - Corresponding password | | 29 | +* `database` - Database name within the server | | 30 | +* `client_encoding` - string encoding the client will use | | 31 | +* `ssl`, either a boolean or an object with properties | | 32 | + * `cert` | | 33 | + * `key` | | 34 | + * `ca` | | 35 | +* any other query parameters (for example, `application_name`) are preserved intact. | | 36 | + | | 37 | +## Connection Strings | | 38 | + | | 39 | +The short summary of acceptable URLs is: | | 40 | + | | 41 | + * `socket:<path>?<query>` - UNIX domain socket | | 42 | + * `postgres://<user>:<password>@<host>:<port>/<database>?<query>` - TCP connection | | 43 | + | | 44 | +But see below for more details. | | 45 | + | | 46 | +### UNIX Domain Sockets | | 47 | + | | 48 | +When user and password are not given, the socket path follows `socket:`, as in `socket:/var/run/pgsql`. | | 49 | +This form can be shortened to just a path: `/var/run/pgsql`. | | 50 | + | | 51 | +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`. | | 52 | + | | 53 | +Query parameters follow a `?` character, including the following special query parameters: | | 54 | + | | 55 | + * `db=<database>` - sets the database name (urlencoded) | | 56 | + * `encoding=<encoding>` - sets the `client_encoding` property | | 57 | + | | 58 | +### TCP Connections | | 59 | + | | 60 | +TCP connections to the Postgres server are indicated with `pg:` or `postgres:` schemes (in fact, any scheme but `socket:` is accepted). | | 61 | +If username and password are included, they should be urlencoded. | | 62 | +The database name, however, should *not* be urlencoded. | | 63 | + | | 64 | +Query parameters follow a `?` character, including the following special query parameters: | | 65 | + * `host=<host>` - sets `host` property, overriding the URL's host | | 66 | + * `encoding=<encoding>` - sets the `client_encoding` property | | 67 | + * `ssl=1`, `ssl=true`, `ssl=0`, `ssl=false` - sets `ssl` to true or false, accordingly | | 68 | + * `sslcert=<filename>` - reads data from the given file and includes the result as `ssl.cert` | | 69 | + * `sslkey=<filename>` - reads data from the given file and includes the result as `ssl.key` | | 70 | + * `sslrootcert=<filename>` - reads data from the given file and includes the result as `ssl.ca` | | 71 | + | | 72 | +A bare relative URL, such as `salesdata`, will indicate a database name while leaving other properties empty. |
Collapse file Copy file name to clipboard +14Lines changed: 14 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +export function parse(connectionString: string): ConnectionOptions | | 2 | + | | 3 | +export interface ConnectionOptions { | | 4 | + host: string | null | | 5 | + password?: string | | 6 | + user?: string | | 7 | + port?: string | null | | 8 | + database: string | null | undefined | | 9 | + client_encoding?: string | | 10 | + ssl?: boolean | string | | 11 | + | | 12 | + application_name?: string | | 13 | + fallback_application_name?: string | | 14 | +} |
Collapse file Copy file name to clipboard +89Lines changed: 89 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict' | | 2 | + | | 3 | +var url = require('url') | | 4 | +var fs = require('fs') | | 5 | + | | 6 | +//Parse method copied from https://github.com/brianc/node-postgres | | 7 | + | | 8 | +//MIT License | | 9 | + | | 10 | +//parses a connection string | | 11 | +function parse(str) { | | 12 | + //unix socket | | 13 | + if (str.charAt(0) === '/') { | | 14 | + var config = str.split(' ') | | 15 | + return { host: config[0], database: config[1] } | | 16 | + } | | 17 | + | | 18 | + // url parse expects spaces encoded as %20 | | 19 | + var result = url.parse( | | 20 | + / |%[^a-f0-9]|%[a-f0-9][^a-f0-9]/i.test(str) ? encodeURI(str).replace(/\%25(\d\d)/g, '%$1') : str, | | 21 | + true | | 22 | + ) | | 23 | + var config = result.query | | 24 | + for (var k in config) { | | 25 | + if (Array.isArray(config[k])) { | | 26 | + config[k] = config[k][config[k].length - 1] | | 27 | + } | | 28 | + } | | 29 | + | | 30 | + var auth = (result.auth || ':').split(':') | | 31 | + config.user = auth[0] | | 32 | + config.password = auth.splice(1).join(':') | | 33 | + | | 34 | + config.port = result.port | | 35 | + if (result.protocol == 'socket:') { | | 36 | + config.host = decodeURI(result.pathname) | | 37 | + config.database = result.query.db | | 38 | + config.client_encoding = result.query.encoding | | 39 | + return config | | 40 | + } | | 41 | + if (!config.host) { | | 42 | + // Only set the host if there is no equivalent query param. | | 43 | + config.host = result.hostname | | 44 | + } | | 45 | + | | 46 | + // If the host is missing it might be a URL-encoded path to a socket. | | 47 | + var pathname = result.pathname | | 48 | + if (!config.host && pathname && /^%2f/i.test(pathname)) { | | 49 | + var pathnameSplit = pathname.split('/') | | 50 | + config.host = decodeURIComponent(pathnameSplit[0]) | | 51 | + pathname = pathnameSplit.splice(1).join('/') | | 52 | + } | | 53 | + // result.pathname is not always guaranteed to have a '/' prefix (e.g. relative urls) | | 54 | + // only strip the slash if it is present. | | 55 | + if (pathname && pathname.charAt(0) === '/') { | | 56 | + pathname = pathname.slice(1) || null | | 57 | + } | | 58 | + config.database = pathname && decodeURI(pathname) | | 59 | + | | 60 | + if (config.ssl === 'true' || config.ssl === '1') { | | 61 | + config.ssl = true | | 62 | + } | | 63 | + | | 64 | + if (config.ssl === '0') { | | 65 | + config.ssl = false | | 66 | + } | | 67 | + | | 68 | + if (config.sslcert || config.sslkey || config.sslrootcert) { | | 69 | + config.ssl = {} | | 70 | + } | | 71 | + | | 72 | + if (config.sslcert) { | | 73 | + config.ssl.cert = fs.readFileSync(config.sslcert).toString() | | 74 | + } | | 75 | + | | 76 | + if (config.sslkey) { | | 77 | + config.ssl.key = fs.readFileSync(config.sslkey).toString() | | 78 | + } | | 79 | + | | 80 | + if (config.sslrootcert) { | | 81 | + config.ssl.ca = fs.readFileSync(config.sslrootcert).toString() | | 82 | + } | | 83 | + | | 84 | + return config | | 85 | +} | | 86 | + | | 87 | +module.exports = parse | | 88 | + | | 89 | +parse.parse = parse |
Collapse file Copy file name to clipboard +39Lines changed: 39 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +{ | | 2 | + "name": "pg-connection-string", | | 3 | + "version": "2.2.0", | | 4 | + "description": "Functions for dealing with a PostgresSQL connection string", | | 5 | + "main": "./index.js", | | 6 | + "types": "./index.d.ts", | | 7 | + "scripts": { | | 8 | + "test": "istanbul cover _mocha && npm run check-coverage", | | 9 | + "check-coverage": "istanbul check-coverage --statements 100 --branches 100 --lines 100 --functions 100", | | 10 | + "coveralls": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls" | | 11 | + }, | | 12 | + "repository": { | | 13 | + "type": "git", | | 14 | + "url": "https://github.com/iceddev/pg-connection-string" | | 15 | + }, | | 16 | + "keywords": [ | | 17 | + "pg", | | 18 | + "connection", | | 19 | + "string", | | 20 | + "parse" | | 21 | + ], | | 22 | + | | 23 | + "license": "MIT", | | 24 | + "bugs": { | | 25 | + "url": "https://github.com/iceddev/pg-connection-string/issues" | | 26 | + }, | | 27 | + "homepage": "https://github.com/iceddev/pg-connection-string", | | 28 | + "dependencies": {}, | | 29 | + "devDependencies": { | | 30 | + "chai": "^4.1.1", | | 31 | + "coveralls": "^3.0.4", | | 32 | + "istanbul": "^0.4.5", | | 33 | + "mocha": "^3.5.0" | | 34 | + }, | | 35 | + "files": [ | | 36 | + "index.js", | | 37 | + "index.d.ts" | | 38 | + ] | | 39 | +} |
|