@@ -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,24 +57,18 @@ 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
-
62
+ this . options = Object . assign ( { } , options )
63
+ const password = this . options . password
63
64
// "hiding" the password so it doesn't show up in stack traces
64
65
// 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 ,
68
68
enumerable : false ,
69
- get ( ) {
70
- return password
71
- } ,
72
- set ( value ) {
73
- password = value
74
- }
69
+ value : password ,
70
+ writable : true
75
71
} )
76
-
77
- this . options = optionsWithPasswordHidden
78
72
this . options . max = this . options . max || this . options . poolSize || 10
79
73
this . log = this . options . log || function ( ) { }
80
74
this . Client = this . options . Client || Client || require ( 'pg' ) . Client
@@ -92,11 +86,11 @@ class Pool extends EventEmitter {
92
86
this . ended = false
93
87
}
94
88
95
- _isFull ( ) {
89
+ _isFull ( ) {
96
90
return this . _clients . length >= this . options . max
97
91
}
98
92
99
- _pulseQueue ( ) {
93
+ _pulseQueue ( ) {
100
94
this . log ( 'pulse queue' )
101
95
if ( this . ended ) {
102
96
this . log ( 'pulse queue ended' )
@@ -139,7 +133,7 @@ class Pool extends EventEmitter {
139
133
throw new Error ( 'unexpected condition' )
140
134
}
141
135
142
- _remove ( client ) {
136
+ _remove ( client ) {
143
137
const removed = removeWhere (
144
138
this . _idle ,
145
139
item => item . client === client
@@ -154,7 +148,7 @@ class Pool extends EventEmitter {
154
148
this . emit ( 'remove' , client )
155
149
}
156
150
157
- connect ( cb ) {
151
+ connect ( cb ) {
158
152
if ( this . ending ) {
159
153
const err = new Error ( 'Cannot use a pool after calling end on the pool' )
160
154
return cb ? cb ( err ) : this . Promise . reject ( err )
@@ -200,7 +194,7 @@ class Pool extends EventEmitter {
200
194
return result
201
195
}
202
196
203
- newClient ( pendingItem ) {
197
+ newClient ( pendingItem ) {
204
198
const client = new this . Client ( this . options )
205
199
this . _clients . push ( client )
206
200
const idleListener = makeIdleListener ( this , client )
@@ -248,7 +242,7 @@ class Pool extends EventEmitter {
248
242
}
249
243
250
244
// acquire a client for a pending work item
251
- _acquireClient ( client , pendingItem , idleListener , isNew ) {
245
+ _acquireClient ( client , pendingItem , idleListener , isNew ) {
252
246
if ( isNew ) {
253
247
this . emit ( 'connect' , client )
254
248
}
@@ -292,7 +286,7 @@ class Pool extends EventEmitter {
292
286
293
287
// release a client back to the poll, include an error
294
288
// to remove it from the pool
295
- _release ( client , idleListener , err ) {
289
+ _release ( client , idleListener , err ) {
296
290
client . on ( 'error' , idleListener )
297
291
298
292
if ( err || this . ending ) {
@@ -314,7 +308,7 @@ class Pool extends EventEmitter {
314
308
this . _pulseQueue ( )
315
309
}
316
310
317
- query ( text , values , cb ) {
311
+ query ( text , values , cb ) {
318
312
// guard clause against passing a function as the first parameter
319
313
if ( typeof text === 'function' ) {
320
314
const response = promisify ( this . Promise , text )
@@ -367,7 +361,7 @@ class Pool extends EventEmitter {
367
361
return response . result
368
362
}
369
363
370
- end ( cb ) {
364
+ end ( cb ) {
371
365
this . log ( 'ending' )
372
366
if ( this . ending ) {
373
367
const err = new Error ( 'Called end on pool more than once' )
@@ -380,15 +374,15 @@ class Pool extends EventEmitter {
380
374
return promised . result
381
375
}
382
376
383
- get waitingCount ( ) {
377
+ get waitingCount ( ) {
384
378
return this . _pendingQueue . length
385
379
}
386
380
387
- get idleCount ( ) {
381
+ get idleCount ( ) {
388
382
return this . _idle . length
389
383
}
390
384
391
- get totalCount ( ) {
385
+ get totalCount ( ) {
392
386
return this . _clients . length
393
387
}
394
388
}
0 commit comments