Skip to content

Commit 1ddb7ad

Browse files
committed
Merge origin/master
2 parents 037515d + a215bfb commit 1ddb7ad

File tree

6 files changed

+106
-6
lines changed

6 files changed

+106
-6
lines changed

packages/pg-query-stream/src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class QueryStream extends Readable implements Submittable {
1313
cursor: any
1414
_result: any
1515

16+
callback: Function
1617
handleRowDescription: Function
1718
handleDataRow: Function
1819
handlePortalSuspended: Function
@@ -26,6 +27,13 @@ class QueryStream extends Readable implements Submittable {
2627

2728
super({ objectMode: true, autoDestroy: true, highWaterMark: batchSize || highWaterMark })
2829
this.cursor = new Cursor(text, values, config)
30+
this.cursor
31+
.on('end', (result) => {
32+
this.callback && this.callback(null, result)
33+
})
34+
.on('error', (err) => {
35+
this.callback && this.callback(err)
36+
})
2937

3038
// delegate Submittable callbacks to cursor
3139
this.handleRowDescription = this.cursor.handleRowDescription.bind(this.cursor)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Pool } from 'pg'
2+
import QueryStream from '../src'
3+
4+
describe('pool', function () {
5+
it('works', async function () {
6+
const pool = new Pool()
7+
const query = new QueryStream('SELECT * FROM generate_series(0, 10) num', [])
8+
const q = pool.query(query)
9+
query.on('data', (row) => {
10+
// just consume the whole stream
11+
})
12+
await q
13+
query.on('end', () => {
14+
pool.end()
15+
})
16+
})
17+
})

packages/pg/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ bench:
2727
@find benchmark -name "*-bench.js" | $(node-command)
2828

2929
test-unit:
30+
@chmod 600 test/unit/client/pgpass.file
3031
@find test/unit -name "*-tests.js" | $(node-command)
3132

3233
test-connection:

packages/pg/lib/client.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const queryQueueDeprecationNotice = nodeUtils.deprecate(
2323
const pgPassDeprecationNotice = nodeUtils.deprecate(
2424
() => {},
2525
'pgpass support is deprecated and will be removed in pg@9.0. ' +
26-
'You can provide an async function as the password property to the Client/Pool constructor that returns a password instead. Within this funciton you can call the pgpass module in your own code.'
26+
'You can provide an async function as the password property to the Client/Pool constructor that returns a password instead. Within this function you can call the pgpass module in your own code.'
2727
)
2828

2929
const byoPromiseDeprecationNotice = nodeUtils.deprecate(
@@ -252,7 +252,7 @@ class Client extends EventEmitter {
252252
if (typeof this.password === 'function') {
253253
this._Promise
254254
.resolve()
255-
.then(() => this.password())
255+
.then(() => this.password(this.connectionParameters))
256256
.then((pass) => {
257257
if (pass !== undefined) {
258258
if (typeof pass !== 'string') {
@@ -612,8 +612,12 @@ class Client extends EventEmitter {
612612
} else if (typeof config.submit === 'function') {
613613
readTimeout = config.query_timeout || this.connectionParameters.query_timeout
614614
result = query = config
615-
if (typeof values === 'function') {
616-
query.callback = query.callback || values
615+
if (!query.callback) {
616+
if (typeof values === 'function') {
617+
query.callback = values
618+
} else if (callback) {
619+
query.callback = callback
620+
}
617621
}
618622
} else {
619623
readTimeout = config.query_timeout || this.connectionParameters.query_timeout
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
const helper = require('./test-helper')
2+
const assert = require('assert')
3+
const suite = new helper.Suite()
4+
const pgpass = require('pgpass')
5+
6+
class Wait {
7+
constructor() {
8+
this.promise = new Promise((resolve) => {
9+
this.resolve = resolve
10+
})
11+
}
12+
13+
until() {
14+
return this.promise
15+
}
16+
17+
done(time) {
18+
if (time) {
19+
setTimeout(this.resolve.bind(this), time)
20+
} else {
21+
this.resolve()
22+
}
23+
}
24+
}
25+
26+
suite.test('password callback is called with conenction params', async function () {
27+
const wait = new Wait()
28+
const client = helper.client({
29+
user: 'foo',
30+
database: 'bar',
31+
host: 'baz',
32+
password: async (params) => {
33+
assert.equal(params.user, 'foo')
34+
assert.equal(params.database, 'bar')
35+
assert.equal(params.host, 'baz')
36+
wait.done(10)
37+
return 'password'
38+
},
39+
})
40+
client.connection.emit('authenticationCleartextPassword')
41+
await wait.until()
42+
assert.equal(client.user, 'foo')
43+
assert.equal(client.database, 'bar')
44+
assert.equal(client.host, 'baz')
45+
assert.equal(client.connectionParameters.password, 'password')
46+
})
47+
48+
suite.test('cleartext password auth does not crash with null password using pg-pass', async function () {
49+
process.env.PGPASSFILE = `${__dirname}/pgpass.file`
50+
// set this to undefined so pgpass will use the file
51+
delete process.env.PGPASSWORD
52+
const wait = new Wait()
53+
const client = helper.client({
54+
host: 'foo',
55+
port: 5432,
56+
database: 'bar',
57+
user: 'baz',
58+
password: (params) => {
59+
return new Promise((resolve) => {
60+
pgpass(params, (pass) => {
61+
wait.done(10)
62+
resolve(pass)
63+
})
64+
})
65+
},
66+
})
67+
client.connection.emit('authenticationCleartextPassword')
68+
await wait.until()
69+
assert.equal(client.password, 'quz')
70+
})

packages/pg/test/unit/client/test-helper.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ const helper = require('../test-helper')
33
const Connection = require('../../../lib/connection')
44
const { Client } = helper
55

6-
const makeClient = function () {
6+
const makeClient = function (config) {
77
const connection = new Connection({ stream: 'no' })
88
connection.startup = function () {}
99
connection.connect = function () {}
1010
connection.query = function (text) {
1111
this.queries.push(text)
1212
}
1313
connection.queries = []
14-
const client = new Client({ connection: connection })
14+
const client = new Client({ connection: connection, ...config })
1515
client.connect()
1616
client.connection.emit('connect')
1717
return client

0 commit comments

Comments
 (0)