@@ -12,20 +12,20 @@ const removeWhere = (list, predicate) => {
1212}
1313
1414class IdleItem {
15- constructor ( client , idleListener , timeoutId ) {
15+ constructor ( client , idleListener , timeoutId ) {
1616 this . client = client
1717 this . idleListener = idleListener
1818 this . timeoutId = timeoutId
1919 }
2020}
2121
2222class PendingItem {
23- constructor ( callback ) {
23+ constructor ( callback ) {
2424 this . callback = callback
2525 }
2626}
2727
28- function promisify ( Promise , callback ) {
28+ function promisify ( Promise , callback ) {
2929 if ( callback ) {
3030 return { callback : callback , result : undefined }
3131 }
@@ -41,8 +41,8 @@ function promisify(Promise, callback) {
4141 return { callback : cb , result : result }
4242}
4343
44- function makeIdleListener ( pool , client ) {
45- return function idleListener ( err ) {
44+ function makeIdleListener ( pool , client ) {
45+ return function idleListener ( err ) {
4646 err . client = client
4747
4848 client . removeListener ( 'error' , idleListener )
@@ -57,24 +57,18 @@ function makeIdleListener(pool, client) {
5757}
5858
5959class Pool extends EventEmitter {
60- constructor ( options , Client ) {
60+ constructor ( options , Client ) {
6161 super ( )
62-
62+ this . options = Object . assign ( { } , options )
63+ const password = this . options . password
6364 // "hiding" the password so it doesn't show up in stack traces
6465 // or if the client is console.logged
65- const optionsWithPasswordHidden = Object . assign ( { } , options )
66- let password = optionsWithPasswordHidden . password
67- Object . defineProperty ( optionsWithPasswordHidden , 'password' , {
66+ Object . defineProperty ( this . options , 'password' , {
67+ configurable : true ,
6868 enumerable : false ,
69- get ( ) {
70- return password
71- } ,
72- set ( value ) {
73- password = value
74- }
69+ value : password ,
70+ writable : true
7571 } )
76-
77- this . options = optionsWithPasswordHidden
7872 this . options . max = this . options . max || this . options . poolSize || 10
7973 this . log = this . options . log || function ( ) { }
8074 this . Client = this . options . Client || Client || require ( 'pg' ) . Client
@@ -92,11 +86,11 @@ class Pool extends EventEmitter {
9286 this . ended = false
9387 }
9488
95- _isFull ( ) {
89+ _isFull ( ) {
9690 return this . _clients . length >= this . options . max
9791 }
9892
99- _pulseQueue ( ) {
93+ _pulseQueue ( ) {
10094 this . log ( 'pulse queue' )
10195 if ( this . ended ) {
10296 this . log ( 'pulse queue ended' )
@@ -139,7 +133,7 @@ class Pool extends EventEmitter {
139133 throw new Error ( 'unexpected condition' )
140134 }
141135
142- _remove ( client ) {
136+ _remove ( client ) {
143137 const removed = removeWhere (
144138 this . _idle ,
145139 item => item . client === client
@@ -154,7 +148,7 @@ class Pool extends EventEmitter {
154148 this . emit ( 'remove' , client )
155149 }
156150
157- connect ( cb ) {
151+ connect ( cb ) {
158152 if ( this . ending ) {
159153 const err = new Error ( 'Cannot use a pool after calling end on the pool' )
160154 return cb ? cb ( err ) : this . Promise . reject ( err )
@@ -200,7 +194,7 @@ class Pool extends EventEmitter {
200194 return result
201195 }
202196
203- newClient ( pendingItem ) {
197+ newClient ( pendingItem ) {
204198 const client = new this . Client ( this . options )
205199 this . _clients . push ( client )
206200 const idleListener = makeIdleListener ( this , client )
@@ -248,7 +242,7 @@ class Pool extends EventEmitter {
248242 }
249243
250244 // acquire a client for a pending work item
251- _acquireClient ( client , pendingItem , idleListener , isNew ) {
245+ _acquireClient ( client , pendingItem , idleListener , isNew ) {
252246 if ( isNew ) {
253247 this . emit ( 'connect' , client )
254248 }
@@ -292,7 +286,7 @@ class Pool extends EventEmitter {
292286
293287 // release a client back to the poll, include an error
294288 // to remove it from the pool
295- _release ( client , idleListener , err ) {
289+ _release ( client , idleListener , err ) {
296290 client . on ( 'error' , idleListener )
297291
298292 if ( err || this . ending ) {
@@ -314,7 +308,7 @@ class Pool extends EventEmitter {
314308 this . _pulseQueue ( )
315309 }
316310
317- query ( text , values , cb ) {
311+ query ( text , values , cb ) {
318312 // guard clause against passing a function as the first parameter
319313 if ( typeof text === 'function' ) {
320314 const response = promisify ( this . Promise , text )
@@ -367,7 +361,7 @@ class Pool extends EventEmitter {
367361 return response . result
368362 }
369363
370- end ( cb ) {
364+ end ( cb ) {
371365 this . log ( 'ending' )
372366 if ( this . ending ) {
373367 const err = new Error ( 'Called end on pool more than once' )
@@ -380,15 +374,15 @@ class Pool extends EventEmitter {
380374 return promised . result
381375 }
382376
383- get waitingCount ( ) {
377+ get waitingCount ( ) {
384378 return this . _pendingQueue . length
385379 }
386380
387- get idleCount ( ) {
381+ get idleCount ( ) {
388382 return this . _idle . length
389383 }
390384
391- get totalCount ( ) {
385+ get totalCount ( ) {
392386 return this . _clients . length
393387 }
394388}
0 commit comments