@@ -12,20 +12,20 @@ const removeWhere = (list, predicate) => {
12
12
}
13
13
14
14
class IdleItem {
15
- constructor ( client , idleListener , timeoutId ) {
15
+ constructor ( client , idleListener , timeoutId ) {
16
16
this . client = client
17
17
this . idleListener = idleListener
18
18
this . timeoutId = timeoutId
19
19
}
20
20
}
21
21
22
22
class PendingItem {
23
- constructor ( callback ) {
23
+ constructor ( callback ) {
24
24
this . callback = callback
25
25
}
26
26
}
27
27
28
- function promisify ( Promise , callback ) {
28
+ function promisify ( Promise , callback ) {
29
29
if ( callback ) {
30
30
return { callback : callback , result : undefined }
31
31
}
@@ -41,8 +41,8 @@ function promisify (Promise, callback) {
41
41
return { callback : cb , result : result }
42
42
}
43
43
44
- function makeIdleListener ( pool , client ) {
45
- return function idleListener ( err ) {
44
+ function makeIdleListener ( pool , client ) {
45
+ return function idleListener ( err ) {
46
46
err . client = client
47
47
48
48
client . removeListener ( 'error' , idleListener )
@@ -57,9 +57,24 @@ function makeIdleListener (pool, client) {
57
57
}
58
58
59
59
class Pool extends EventEmitter {
60
- constructor ( options , Client ) {
60
+ constructor ( options , Client ) {
61
61
super ( )
62
- this . options = Object . assign ( { } , options )
62
+
63
+ // "hiding" the password so it doesn't show up in stack traces
64
+ // or if the client is console.logged
65
+ const optionsWithPasswordHidden = Object . assign ( { } , options )
66
+ let password = optionsWithPasswordHidden . password
67
+ Object . defineProperty ( optionsWithPasswordHidden , 'password' , {
68
+ enumerable : false ,
69
+ get ( ) {
70
+ return password
71
+ } ,
72
+ set ( value ) {
73
+ password = value
74
+ }
75
+ } )
76
+
77
+ this . options = optionsWithPasswordHidden
63
78
this . options . max = this . options . max || this . options . poolSize || 10
64
79
this . log = this . options . log || function ( ) { }
65
80
this . Client = this . options . Client || Client || require ( 'pg' ) . Client
@@ -77,11 +92,11 @@ class Pool extends EventEmitter {
77
92
this . ended = false
78
93
}
79
94
80
- _isFull ( ) {
95
+ _isFull ( ) {
81
96
return this . _clients . length >= this . options . max
82
97
}
83
98
84
- _pulseQueue ( ) {
99
+ _pulseQueue ( ) {
85
100
this . log ( 'pulse queue' )
86
101
if ( this . ended ) {
87
102
this . log ( 'pulse queue ended' )
@@ -124,7 +139,7 @@ class Pool extends EventEmitter {
124
139
throw new Error ( 'unexpected condition' )
125
140
}
126
141
127
- _remove ( client ) {
142
+ _remove ( client ) {
128
143
const removed = removeWhere (
129
144
this . _idle ,
130
145
item => item . client === client
@@ -139,7 +154,7 @@ class Pool extends EventEmitter {
139
154
this . emit ( 'remove' , client )
140
155
}
141
156
142
- connect ( cb ) {
157
+ connect ( cb ) {
143
158
if ( this . ending ) {
144
159
const err = new Error ( 'Cannot use a pool after calling end on the pool' )
145
160
return cb ? cb ( err ) : this . Promise . reject ( err )
@@ -185,7 +200,7 @@ class Pool extends EventEmitter {
185
200
return result
186
201
}
187
202
188
- newClient ( pendingItem ) {
203
+ newClient ( pendingItem ) {
189
204
const client = new this . Client ( this . options )
190
205
this . _clients . push ( client )
191
206
const idleListener = makeIdleListener ( this , client )
@@ -233,7 +248,7 @@ class Pool extends EventEmitter {
233
248
}
234
249
235
250
// acquire a client for a pending work item
236
- _acquireClient ( client , pendingItem , idleListener , isNew ) {
251
+ _acquireClient ( client , pendingItem , idleListener , isNew ) {
237
252
if ( isNew ) {
238
253
this . emit ( 'connect' , client )
239
254
}
@@ -277,7 +292,7 @@ class Pool extends EventEmitter {
277
292
278
293
// release a client back to the poll, include an error
279
294
// to remove it from the pool
280
- _release ( client , idleListener , err ) {
295
+ _release ( client , idleListener , err ) {
281
296
client . on ( 'error' , idleListener )
282
297
283
298
if ( err || this . ending ) {
@@ -299,7 +314,7 @@ class Pool extends EventEmitter {
299
314
this . _pulseQueue ( )
300
315
}
301
316
302
- query ( text , values , cb ) {
317
+ query ( text , values , cb ) {
303
318
// guard clause against passing a function as the first parameter
304
319
if ( typeof text === 'function' ) {
305
320
const response = promisify ( this . Promise , text )
@@ -352,7 +367,7 @@ class Pool extends EventEmitter {
352
367
return response . result
353
368
}
354
369
355
- end ( cb ) {
370
+ end ( cb ) {
356
371
this . log ( 'ending' )
357
372
if ( this . ending ) {
358
373
const err = new Error ( 'Called end on pool more than once' )
@@ -365,15 +380,15 @@ class Pool extends EventEmitter {
365
380
return promised . result
366
381
}
367
382
368
- get waitingCount ( ) {
383
+ get waitingCount ( ) {
369
384
return this . _pendingQueue . length
370
385
}
371
386
372
- get idleCount ( ) {
387
+ get idleCount ( ) {
373
388
return this . _idle . length
374
389
}
375
390
376
- get totalCount ( ) {
391
+ get totalCount ( ) {
377
392
return this . _clients . length
378
393
}
379
394
}
0 commit comments