@@ -41,13 +41,9 @@ import {
41
41
42
42
import { md5 } from './utils' ;
43
43
44
- export type Connect = Error | null ;
45
-
46
- export type End = void ;
47
-
48
- export interface Parameter {
49
- name : string ;
50
- value : string ;
44
+ export interface ConnectionInfo {
45
+ encrypted : boolean ;
46
+ parameters : ReadonlyMap < string , string > ;
51
47
}
52
48
53
49
export interface ClientNotice extends DatabaseError {
@@ -108,13 +104,14 @@ type CallbackOf<U> = U extends any ? Callback<U> : never;
108
104
109
105
type Event = (
110
106
ClientNotice |
111
- Connect |
112
107
DatabaseError |
113
- End |
114
- Parameter |
115
108
Notification
116
109
) ;
117
110
111
+ type Connect = Error | null ;
112
+
113
+ type End = NodeJS . ErrnoException | null ;
114
+
118
115
type CloseHandler = ( ) => void ;
119
116
120
117
interface RowDataHandler {
@@ -164,7 +161,6 @@ export class Client {
164
161
private readonly events = {
165
162
connect : new TypedEvent < Connect > ( ) ,
166
163
end : new TypedEvent < End > ( ) ,
167
- parameter : new TypedEvent < Parameter > ( ) ,
168
164
error : new TypedEvent < DatabaseError > ( ) ,
169
165
notice : new TypedEvent < ClientNotice > ( ) ,
170
166
notification : new TypedEvent < Notification > ( )
@@ -196,6 +192,7 @@ export class Client {
196
192
197
193
private nextPreparedStatementId = 0 ;
198
194
private activeDataHandlerInfo : RowDataHandlerInfo | null = null ;
195
+ private readonly parameters : Map < string , string > = new Map ( ) ;
199
196
200
197
public closed = true ;
201
198
public processId : number | null = null ;
@@ -213,7 +210,7 @@ export class Client {
213
210
214
211
this . stream . on ( 'close' , ( ) => {
215
212
this . closed = true ;
216
- this . events . end . emit ( ) ;
213
+ this . events . end . emit ( null ) ;
217
214
} ) ;
218
215
219
216
this . stream . on ( 'connect' , ( ) => {
@@ -239,7 +236,7 @@ export class Client {
239
236
if ( this . ending && error . errno ===
240
237
constants . errno . ECONNRESET ) return ;
241
238
242
- this . events . end . emit ( ) ;
239
+ this . events . end . emit ( error ) ;
243
240
}
244
241
} ) ;
245
242
@@ -408,9 +405,9 @@ export class Client {
408
405
* @remarks
409
406
* Don't forget to close the connection using {@link end} before exiting.
410
407
*
411
- * @returns The connection encryption status .
408
+ * @returns The connection information .
412
409
*/
413
- connect ( ) : Promise < boolean > {
410
+ connect ( ) : Promise < ConnectionInfo > {
414
411
if ( this . connecting ) {
415
412
throw new Error ( 'Already connecting' ) ;
416
413
}
@@ -428,7 +425,10 @@ export class Client {
428
425
this . stream . destroy ( ) ;
429
426
throw error ;
430
427
}
431
- return this . stream instanceof TLSSocket ;
428
+ return {
429
+ encrypted : this . stream instanceof TLSSocket ,
430
+ parameters : this . parameters as ReadonlyMap < string , string > ,
431
+ }
432
432
} ) ;
433
433
434
434
const port = this . config . port || defaults . port ;
@@ -448,7 +448,7 @@ export class Client {
448
448
new Error ( `Timeout after ${ timeout } ms` )
449
449
) , timeout
450
450
) ) ,
451
- ] ) as Promise < boolean >
451
+ ] ) as Promise < ConnectionInfo >
452
452
}
453
453
return p ;
454
454
}
@@ -479,28 +479,21 @@ export class Client {
479
479
} else {
480
480
this . stream . destroy ( ) ;
481
481
}
482
-
483
- return this . events . end . once ( ) ;
482
+ return new Promise < void > ( ( resolve , reject ) =>
483
+ this . events . end . once ( ) . then (
484
+ value => {
485
+ if ( value === null ) resolve ( ) ;
486
+ reject ( value ) ;
487
+ }
488
+ )
489
+ ) ;
484
490
}
485
491
486
- on ( event : 'connect' , callback : Callback < Connect > ) : void ;
487
- on ( event : 'end' , callback : Callback < End > ) : void ;
488
- on ( event : 'parameter' , callback : Callback < Parameter > ) : void ;
489
492
on ( event : 'notification' , callback : Callback < Notification > ) : void ;
490
493
on ( event : 'error' , callback : Callback < DatabaseError > ) : void ;
491
494
on ( event : 'notice' , callback : Callback < ClientNotice > ) : void ;
492
495
on ( event : string , callback : CallbackOf < Event > ) : void {
493
496
switch ( event ) {
494
- case 'connect' : {
495
- this . events . connect . on (
496
- callback as Callback < Connect > ) ;
497
- break ;
498
- }
499
- case 'end' : {
500
- this . events . end . on (
501
- callback as Callback < End > ) ;
502
- break ;
503
- }
504
497
case 'error' : {
505
498
this . events . error . on (
506
499
callback as Callback < DatabaseError > ) ;
@@ -516,11 +509,6 @@ export class Client {
516
509
callback as Callback < Notification > ) ;
517
510
break
518
511
}
519
- case 'parameter' : {
520
- this . events . parameter . on (
521
- callback as Callback < Parameter > ) ;
522
- break
523
- }
524
512
}
525
513
}
526
514
@@ -1109,10 +1097,7 @@ export class Client {
1109
1097
const reader = new Reader ( buffer , start ) ;
1110
1098
const name = reader . readCString ( this . encoding ) ;
1111
1099
const value = reader . readCString ( this . encoding ) ;
1112
- this . events . parameter . emit ( {
1113
- name : name ,
1114
- value : value
1115
- } ) ;
1100
+ this . parameters . set ( name , value ) ;
1116
1101
break ;
1117
1102
}
1118
1103
case Message . ReadyForQuery : {
0 commit comments