Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: brianc/node-postgres
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: pg@8.4.2
Choose a base ref
...
head repository: brianc/node-postgres
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: pg@8.5.0
Choose a head ref
  • 9 commits
  • 58 files changed
  • 4 contributors

Commits on Nov 2, 2020

  1. 2
    Copy the full SHA
    415bf09 View commit details
  2. Copy the full SHA
    c22c2f0 View commit details
  3. Copy the full SHA
    52dfca4 View commit details

Commits on Nov 3, 2020

  1. feat(): pg-query-stream typescript (#2376)

    * feat(): start converting pg-query stream
    
    * feat(): solution project, initial version of typescript-pg-query stream
    
    * chore(): mocha with typescript
    
    * fix(): eslint ignore query stream dist
    
    * refactor(pg-query-stream): convert test to ts
    
    * chore(): fixed type errors
    
    * chore(): fix helper usage
    
    * chore(): use ts-node compatibile with node v8
    
    * fix(): addd es extension
    
    * chore(): remove emitClose and added compilation for async iterators
    
    * chore(): condition for asyc iteration test
    
    * chore(): rename class to match ts-defs
    
    * chore(): tests to import from src instead of dist
    
    * chore(): remove prettier from peer deps:
    
    * chore(): update lock file
    chyzwar authored Nov 3, 2020
    Copy the full SHA
    78a14a1 View commit details

Commits on Nov 4, 2020

  1. Speed up bind functionality (#2286)

    Move from 3 loops (prepareValue, check for buffers, write param types, write param values) to a single loop. This speeds up the insert benchmark by around 100 queries per second. Performance improvement depends on number of parameters being bound.
    brianc authored Nov 4, 2020
    Copy the full SHA
    07988f9 View commit details

Commits on Nov 6, 2020

  1. Copy the full SHA
    8bed670 View commit details

Commits on Nov 9, 2020

  1. Forward options’ ssl.key even when non-enumerable (#2394)

    * Test client certificate authentication
    
    * Forward options’ ssl.key even when non-enumerable
    charmander authored Nov 9, 2020
    Copy the full SHA
    0012a43 View commit details

Commits on Nov 10, 2020

  1. Update sponsors & changelog

    brianc committed Nov 10, 2020
    Copy the full SHA
    dce02e8 View commit details
  2. Publish

     - pg-cursor@2.5.0
     - pg-protocol@1.4.0
     - pg-query-stream@3.4.0
     - pg@8.5.0
    brianc committed Nov 10, 2020
    Copy the full SHA
    ec1dcab View commit details
Showing with 2,011 additions and 1,324 deletions.
  1. +1 −1 .eslintrc
  2. +36 −0 .travis.yml
  3. +1 −0 .yarnrc
  4. +6 −0 CHANGELOG.md
  5. +2 −0 SPONSORS.md
  6. +12 −12 package.json
  7. +2 −2 packages/pg-cursor/package.json
  8. +7 −3 packages/pg-protocol/package.json
  9. +2 −2 packages/pg-protocol/src/buffer-writer.ts
  10. +29 −0 packages/pg-protocol/src/outbound-serializer.test.ts
  11. +45 −35 packages/pg-protocol/src/serializer.ts
  12. +1 −0 packages/pg-protocol/tsconfig.json
  13. +17 −6 packages/pg-query-stream/package.json
  14. +31 −12 packages/pg-query-stream/{index.js → src/index.ts}
  15. +0 −112 packages/pg-query-stream/test/async-iterator.es6
  16. +0 −4 packages/pg-query-stream/test/async-iterator.js
  17. +116 −0 packages/pg-query-stream/test/async-iterator.ts
  18. +8 −7 packages/pg-query-stream/test/{client-options.js → client-options.ts}
  19. +17 −15 packages/pg-query-stream/test/{close.js → close.ts}
  20. +8 −9 packages/pg-query-stream/test/{concat.js → concat.ts}
  21. +0 −26 packages/pg-query-stream/test/config.js
  22. +26 −0 packages/pg-query-stream/test/config.ts
  23. +2 −3 packages/pg-query-stream/test/{empty-query.js → empty-query.ts}
  24. +5 −6 packages/pg-query-stream/test/{error.js → error.ts}
  25. +8 −8 packages/pg-query-stream/test/{fast-reader.js → fast-reader.ts}
  26. +4 −3 packages/pg-query-stream/test/{helper.js → helper.ts}
  27. +0 −17 packages/pg-query-stream/test/instant.js
  28. +17 −0 packages/pg-query-stream/test/instant.ts
  29. +8 −7 packages/pg-query-stream/test/{issue-3.js → issue-3.ts}
  30. +9 −9 packages/pg-query-stream/test/{passing-options.js → passing-options.ts}
  31. +0 −23 packages/pg-query-stream/test/pauses.js
  32. +26 −0 packages/pg-query-stream/test/pauses.ts
  33. +6 −6 packages/pg-query-stream/test/{slow-reader.js → slow-reader.ts}
  34. +0 −25 packages/pg-query-stream/test/stream-tester-timestamp.js
  35. +26 −0 packages/pg-query-stream/test/stream-tester-timestamp.ts
  36. +0 −12 packages/pg-query-stream/test/stream-tester.js
  37. +12 −0 packages/pg-query-stream/test/stream-tester.ts
  38. +26 −0 packages/pg-query-stream/tsconfig.json
  39. +31 −28 packages/pg/bench.js
  40. +12 −6 packages/pg/lib/connection.js
  41. +0 −3 packages/pg/lib/index.js
  42. +14 −14 packages/pg/lib/query.js
  43. +4 −3 packages/pg/lib/utils.js
  44. +2 −2 packages/pg/package.json
  45. +26 −13 packages/pg/test/integration/client/error-handling-tests.js
  46. +23 −0 packages/pg/test/integration/connection-pool/tls-tests.js
  47. +9 −0 packages/pg/test/test-helper.js
  48. +71 −0 packages/pg/test/tls/GNUmakefile
  49. +11 −0 packages/pg/test/tls/test-client-ca.crt
  50. +5 −0 packages/pg/test/tls/test-client-ca.key
  51. +9 −0 packages/pg/test/tls/test-client.crt
  52. +5 −0 packages/pg/test/tls/test-client.key
  53. +11 −0 packages/pg/test/tls/test-server-ca.crt
  54. +5 −0 packages/pg/test/tls/test-server-ca.key
  55. +9 −0 packages/pg/test/tls/test-server.crt
  56. +5 −0 packages/pg/test/tls/test-server.key
  57. +12 −0 tsconfig.json
  58. +1,231 −890 yarn.lock
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
"plugins": ["prettier"],
"parser": "@typescript-eslint/parser",
"extends": ["plugin:prettier/recommended", "prettier/@typescript-eslint"],
"ignorePatterns": ["node_modules", "coverage", "packages/pg-protocol/dist/**/*"],
"ignorePatterns": ["node_modules", "coverage", "packages/pg-protocol/dist/**/*", "packages/pg-query-stream/dist/**/*"],
"parserOptions": {
"ecmaVersion": 2017,
"sourceType": "module"
36 changes: 36 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -43,6 +43,38 @@ matrix:
postgresql: '9.5'
dist: precise

# Run tests/paths with client certificate authentication
- node_js: lts/*
env:
- CC=clang CXX=clang++ npm_config_clang=1 PGUSER=postgres PGDATABASE=postgres
PGSSLMODE=verify-full
PGSSLROOTCERT=$TRAVIS_BUILD_DIR/packages/pg/test/tls/test-server-ca.crt
PGSSLCERT=$TRAVIS_BUILD_DIR/packages/pg/test/tls/test-client.crt
PGSSLKEY=$TRAVIS_BUILD_DIR/packages/pg/test/tls/test-client.key
PG_CLIENT_CERT_TEST=1
before_script:
- chmod go= packages/pg/test/tls/test-client.key
- |
sudo sed -i \
-e '/^ssl_cert_file =/d' \
-e '/^ssl_key_file =/d' \
/etc/postgresql/10/main/postgresql.conf
cat <<'travis ci breaks heredoc' | sudo tee -a /etc/postgresql/10/main/postgresql.conf > /dev/null
ssl_cert_file = 'test-server.crt'
ssl_key_file = 'test-server.key'
ssl_ca_file = 'test-client-ca.crt'
- printf 'hostssl all all %s cert\n' 127.0.0.1/32 ::1/128 | sudo tee /etc/postgresql/10/main/pg_hba.conf > /dev/null
- sudo make -C packages/pg/test/tls install DESTDIR=/var/ramfs/postgresql/10/main
- sudo systemctl restart postgresql@10-main
- yarn build
script:
- cd packages/pg
- node test/integration/connection-pool/tls-tests.js
- npm install --no-save pg-native
- node test/integration/connection-pool/tls-tests.js native

# different PostgreSQL versions on Node LTS
- node_js: lts/erbium
addons:
@@ -57,6 +89,10 @@ matrix:
addons:
postgresql: '9.6'

# only run lint on latest Node LTS
- node_js: lts/*
script: yarn lint

# PostgreSQL 9.2 only works on precise
- node_js: lts/carbon
addons:
1 change: 1 addition & 0 deletions .yarnrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--install.ignore-engines true
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -4,6 +4,12 @@ For richer information consult the commit log on github with referenced pull req

We do not include break-fix version release in this file.

### pg@8.5.0

- Fix bug forwarding [ssl key](https://github.com/brianc/node-postgres/pull/2394).
- Convert pg-query-stream internals to [typescript](https://github.com/brianc/node-postgres/pull/2376).
- Performance [improvements](https://github.com/brianc/node-postgres/pull/2286).

### pg@8.4.0

- Switch to optional peer dependencies & remove [semver](https://github.com/brianc/node-postgres/commit/a02dfac5ad2e2abf0dc3a9817f953938acdc19b1) package which has been a small thorn in the side of a few users.
2 changes: 2 additions & 0 deletions SPONSORS.md
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ node-postgres is made possible by the helpful contributors from the community as
- [Nafundi](https://nafundi.com)
- [CrateDB](https://crate.io/)
- [BitMEX](https://www.bitmex.com/app/trade/XBTUSD)
- [Dataform](https://dataform.co/)

# Supporters

@@ -32,3 +33,4 @@ node-postgres is made possible by the helpful contributors from the community as
- Simple Analytics
- Trevor Linton
- Ian Walter
- @Guido4000
24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -10,22 +10,22 @@
"packages/*"
],
"scripts": {
"test": "yarn lint && yarn lerna exec yarn test",
"build": "yarn lerna exec --scope pg-protocol yarn build",
"test": "yarn lerna exec yarn test",
"build": "tsc --build",
"build:watch": "tsc --build --watch",
"pretest": "yarn build",
"lint": "if [ -x ./node_modules/.bin/prettier ]; then eslint '*/**/*.{js,ts,tsx}'; fi;"
"lint": "eslint '*/**/*.{js,ts,tsx}'"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^2.27.0",
"@typescript-eslint/parser": "^2.27.0",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.1",
"@typescript-eslint/eslint-plugin": "^4.4.0",
"@typescript-eslint/parser": "^4.4.0",
"eslint": "^7.11.0",
"eslint-config-prettier": "^6.12.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.1.2",
"lerna": "^3.19.0"
},
"optionalDependencies": {
"prettier": "2.0.4"
"eslint-plugin-prettier": "^3.1.4",
"lerna": "^3.19.0",
"prettier": "2.1.2",
"typescript": "^4.0.3"
},
"prettier": {
"semi": false,
4 changes: 2 additions & 2 deletions packages/pg-cursor/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pg-cursor",
"version": "2.4.2",
"version": "2.5.0",
"description": "Query cursor extension for node-postgres",
"main": "index.js",
"directories": {
@@ -17,6 +17,6 @@
"license": "MIT",
"devDependencies": {
"mocha": "^7.1.2",
"pg": "^8.4.2"
"pg": "^8.5.0"
}
}
10 changes: 7 additions & 3 deletions packages/pg-protocol/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pg-protocol",
"version": "1.3.0",
"version": "1.4.0",
"description": "The postgres client/server binary protocol, implemented in TypeScript",
"main": "dist/index.js",
"types": "dist/index.d.ts",
@@ -13,13 +13,17 @@
"chunky": "^0.0.0",
"mocha": "^7.1.2",
"ts-node": "^8.5.4",
"typescript": "^3.7.3"
"typescript": "^4.0.3"
},
"scripts": {
"test": "mocha dist/**/*.test.js",
"build": "tsc",
"build:watch": "tsc --watch",
"prepublish": "yarn build",
"pretest": "yarn build"
}
},
"files": [
"/dist/*{js,ts,map}",
"/src"
]
}
4 changes: 2 additions & 2 deletions packages/pg-protocol/src/buffer-writer.ts
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ export class Writer {
private offset: number = 5
private headerPosition: number = 0
constructor(private size = 256) {
this.buffer = Buffer.alloc(size)
this.buffer = Buffer.allocUnsafe(size)
}

private ensure(size: number): void {
@@ -15,7 +15,7 @@ export class Writer {
// exponential growth factor of around ~ 1.5
// https://stackoverflow.com/questions/2269063/buffer-growth-strategy
var newSize = oldBuffer.length + (oldBuffer.length >> 1) + size
this.buffer = Buffer.alloc(newSize)
this.buffer = Buffer.allocUnsafe(newSize)
oldBuffer.copy(this.buffer)
}
}
29 changes: 29 additions & 0 deletions packages/pg-protocol/src/outbound-serializer.test.ts
Original file line number Diff line number Diff line change
@@ -110,6 +110,10 @@ describe('serializer', () => {
var expectedBuffer = new BufferList()
.addCString('bang') // portal name
.addCString('woo') // statement name
.addInt16(4)
.addInt16(0)
.addInt16(0)
.addInt16(0)
.addInt16(0)
.addInt16(4)
.addInt32(1)
@@ -125,6 +129,31 @@ describe('serializer', () => {
})
})

it('with custom valueMapper', function () {
const actual = serialize.bind({
portal: 'bang',
statement: 'woo',
values: ['1', 'hi', null, 'zing'],
valueMapper: () => null,
})
var expectedBuffer = new BufferList()
.addCString('bang') // portal name
.addCString('woo') // statement name
.addInt16(4)
.addInt16(0)
.addInt16(0)
.addInt16(0)
.addInt16(0)
.addInt16(4)
.addInt32(-1)
.addInt32(-1)
.addInt32(-1)
.addInt32(-1)
.addInt16(0)
.join(true, 'B')
assert.deepEqual(actual, expectedBuffer)
})

it('with named statement, portal, and buffer value', function () {
const actual = serialize.bind({
portal: 'bang',
80 changes: 45 additions & 35 deletions packages/pg-protocol/src/serializer.ts
Original file line number Diff line number Diff line change
@@ -101,56 +101,66 @@ const parse = (query: ParseOpts): Buffer => {
return writer.flush(code.parse)
}

type ValueMapper = (param: any, index: number) => any

type BindOpts = {
portal?: string
binary?: boolean
statement?: string
values?: any[]
// optional map from JS value to postgres value per parameter
valueMapper?: ValueMapper
}

const paramWriter = new Writer()

// make this a const enum so typescript will inline the value
const enum ParamType {
STRING = 0,
BINARY = 1,
}

const writeValues = function (values: any[], valueMapper?: ValueMapper): void {
for (let i = 0; i < values.length; i++) {
const mappedVal = valueMapper ? valueMapper(values[i], i) : values[i]
if (mappedVal == null) {
// add the param type (string) to the writer
writer.addInt16(ParamType.STRING)
// write -1 to the param writer to indicate null
paramWriter.addInt32(-1)
} else if (mappedVal instanceof Buffer) {
// add the param type (binary) to the writer
writer.addInt16(ParamType.BINARY)
// add the buffer to the param writer
paramWriter.addInt32(mappedVal.length)
paramWriter.add(mappedVal)
} else {
// add the param type (string) to the writer
writer.addInt16(ParamType.STRING)
paramWriter.addInt32(Buffer.byteLength(mappedVal))
paramWriter.addString(mappedVal)
}
}
}

const bind = (config: BindOpts = {}): Buffer => {
// normalize config
const portal = config.portal || ''
const statement = config.statement || ''
const binary = config.binary || false
var values = config.values || emptyArray
var len = values.length
const values = config.values || emptyArray
const len = values.length

var useBinary = false
// TODO(bmc): all the loops in here aren't nice, we can do better
for (var j = 0; j < len; j++) {
useBinary = useBinary || values[j] instanceof Buffer
}
writer.addCString(portal).addCString(statement)
writer.addInt16(len)

var buffer = writer.addCString(portal).addCString(statement)
if (!useBinary) {
buffer.addInt16(0)
} else {
buffer.addInt16(len)
for (j = 0; j < len; j++) {
buffer.addInt16(values[j] instanceof Buffer ? 1 : 0)
}
}
buffer.addInt16(len)
for (var i = 0; i < len; i++) {
var val = values[i]
if (val === null || typeof val === 'undefined') {
buffer.addInt32(-1)
} else if (val instanceof Buffer) {
buffer.addInt32(val.length)
buffer.add(val)
} else {
buffer.addInt32(Buffer.byteLength(val))
buffer.addString(val)
}
}
writeValues(values, config.valueMapper)

if (binary) {
buffer.addInt16(1) // format codes to use binary
buffer.addInt16(1)
} else {
buffer.addInt16(0) // format codes to use text
}
writer.addInt16(len)
writer.add(paramWriter.flush())

// format code
writer.addInt16(binary ? ParamType.BINARY : ParamType.STRING)
return writer.flush(code.bind)
}

1 change: 1 addition & 0 deletions packages/pg-protocol/tsconfig.json
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"incremental": true,
"baseUrl": ".",
"declaration": true,
"paths": {
23 changes: 17 additions & 6 deletions packages/pg-query-stream/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "pg-query-stream",
"version": "3.3.2",
"version": "3.4.0",
"description": "Postgres query result returned as readable stream",
"main": "index.js",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"scripts": {
"test": "mocha"
"test": "mocha -r ts-node/register test/**/*.ts"
},
"repository": {
"type": "git",
@@ -16,22 +17,32 @@
"query",
"stream"
],
"files": [
"/dist/*{js,ts,map}",
"/src"
],
"author": "Brian M. Carlson",
"license": "MIT",
"bugs": {
"url": "https://github.com/brianc/node-postgres/issues"
},
"devDependencies": {
"@types/chai": "^4.2.13",
"@types/mocha": "^8.0.3",
"@types/node": "^14.0.0",
"@types/pg": "^7.14.5",
"JSONStream": "~0.7.1",
"concat-stream": "~1.0.1",
"eslint-plugin-promise": "^3.5.0",
"mocha": "^7.1.2",
"pg": "^8.4.2",
"pg": "^8.5.0",
"stream-spec": "~0.3.5",
"stream-tester": "0.0.5",
"through": "~2.3.4"
"through": "~2.3.4",
"ts-node": "^8.5.4",
"typescript": "^4.0.3"
},
"dependencies": {
"pg-cursor": "^2.4.2"
"pg-cursor": "^2.5.0"
}
}
Loading