Skip to content

Commit aa9a042

Browse files
committed
Remove the 'Connect' and 'End' events, and other cleanup
1 parent bf62917 commit aa9a042

File tree

5 files changed

+49
-79
lines changed

5 files changed

+49
-79
lines changed

CHANGES.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
In next release ...
2+
3+
- The `Connect` and `End` events have been removed, in addition to the `Parameter`
4+
event; the `connect` method now returns an object with information about the
5+
established connection, namely whether the connection is encrypted and the
6+
connection parameters sent by the server.
7+
8+
- Fixed a regression where some symbols were not correctly exposed for importing.
9+
110
1.7.0 (2023-12-13)
211
------------------
312

src/client.ts

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,9 @@ import {
4141

4242
import { md5 } from './utils';
4343

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>;
5147
}
5248

5349
export interface ClientNotice extends DatabaseError {
@@ -108,13 +104,14 @@ type CallbackOf<U> = U extends any ? Callback<U> : never;
108104

109105
type Event = (
110106
ClientNotice |
111-
Connect |
112107
DatabaseError |
113-
End |
114-
Parameter |
115108
Notification
116109
);
117110

111+
type Connect = Error | null;
112+
113+
type End = NodeJS.ErrnoException | null;
114+
118115
type CloseHandler = () => void;
119116

120117
interface RowDataHandler {
@@ -164,7 +161,6 @@ export class Client {
164161
private readonly events = {
165162
connect: new TypedEvent<Connect>(),
166163
end: new TypedEvent<End>(),
167-
parameter: new TypedEvent<Parameter>(),
168164
error: new TypedEvent<DatabaseError>(),
169165
notice: new TypedEvent<ClientNotice>(),
170166
notification: new TypedEvent<Notification>()
@@ -196,6 +192,7 @@ export class Client {
196192

197193
private nextPreparedStatementId = 0;
198194
private activeDataHandlerInfo: RowDataHandlerInfo | null = null;
195+
private readonly parameters: Map<string, string> = new Map();
199196

200197
public closed = true;
201198
public processId: number | null = null;
@@ -213,7 +210,7 @@ export class Client {
213210

214211
this.stream.on('close', () => {
215212
this.closed = true;
216-
this.events.end.emit();
213+
this.events.end.emit(null);
217214
});
218215

219216
this.stream.on('connect', () => {
@@ -239,7 +236,7 @@ export class Client {
239236
if (this.ending && error.errno ===
240237
constants.errno.ECONNRESET) return;
241238

242-
this.events.end.emit();
239+
this.events.end.emit(error);
243240
}
244241
});
245242

@@ -408,9 +405,9 @@ export class Client {
408405
* @remarks
409406
* Don't forget to close the connection using {@link end} before exiting.
410407
*
411-
* @returns The connection encryption status.
408+
* @returns The connection information.
412409
*/
413-
connect(): Promise<boolean> {
410+
connect(): Promise<ConnectionInfo> {
414411
if (this.connecting) {
415412
throw new Error('Already connecting');
416413
}
@@ -428,7 +425,10 @@ export class Client {
428425
this.stream.destroy();
429426
throw error;
430427
}
431-
return this.stream instanceof TLSSocket;
428+
return {
429+
encrypted: this.stream instanceof TLSSocket,
430+
parameters: this.parameters as ReadonlyMap<string, string>,
431+
}
432432
});
433433

434434
const port = this.config.port || defaults.port;
@@ -448,7 +448,7 @@ export class Client {
448448
new Error(`Timeout after ${timeout} ms`)
449449
), timeout
450450
)),
451-
]) as Promise<boolean>
451+
]) as Promise<ConnectionInfo>
452452
}
453453
return p;
454454
}
@@ -479,28 +479,21 @@ export class Client {
479479
} else {
480480
this.stream.destroy();
481481
}
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+
);
484490
}
485491

486-
on(event: 'connect', callback: Callback<Connect>): void;
487-
on(event: 'end', callback: Callback<End>): void;
488-
on(event: 'parameter', callback: Callback<Parameter>): void;
489492
on(event: 'notification', callback: Callback<Notification>): void;
490493
on(event: 'error', callback: Callback<DatabaseError>): void;
491494
on(event: 'notice', callback: Callback<ClientNotice>): void;
492495
on(event: string, callback: CallbackOf<Event>): void {
493496
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-
}
504497
case 'error': {
505498
this.events.error.on(
506499
callback as Callback<DatabaseError>);
@@ -516,11 +509,6 @@ export class Client {
516509
callback as Callback<Notification>);
517510
break
518511
}
519-
case 'parameter': {
520-
this.events.parameter.on(
521-
callback as Callback<Parameter>);
522-
break
523-
}
524512
}
525513
}
526514

@@ -1109,10 +1097,7 @@ export class Client {
11091097
const reader = new Reader(buffer, start);
11101098
const name = reader.readCString(this.encoding);
11111099
const value = reader.readCString(this.encoding);
1112-
this.events.parameter.emit({
1113-
name: name,
1114-
value: value
1115-
});
1100+
this.parameters.set(name, value);
11161101
break;
11171102
}
11181103
case Message.ReadyForQuery: {

src/index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
export { Client, PreparedStatement } from './client';
1+
export {
2+
Client,
3+
ClientNotice,
4+
Configuration,
5+
DataTypeError,
6+
Notification,
7+
PreparedStatement,
8+
SSL,
9+
SSLMode
10+
} from './client';
211
export {
312
DataFormat,
413
DataType,
@@ -14,6 +23,8 @@ export {
1423
} from './result';
1524
export { Environment } from './defaults';
1625
export {
26+
ClientConnectionDefaults,
27+
ClientConnectionOptions,
1728
DatabaseError,
1829
ErrorLevel,
1930
TransactionStatus,

test/client.test.ts

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createServer, AddressInfo, Socket } from 'net';
2-
import { describe, expect, jest, test } from '@jest/globals';
2+
import { describe, expect, test } from '@jest/globals';
33
import { testWithClient } from './helper';
44
import {
55
Client,
@@ -136,37 +136,6 @@ function testSelect(
136136
});
137137
}
138138

139-
describe('Events', () => {
140-
testWithClient('End', async (client) => {
141-
expect.assertions(1);
142-
const f = jest.fn();
143-
client.on('end', f);
144-
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
145-
const p = new Promise((resolve, _) => {
146-
client.on('connect', async () => {
147-
await client.end();
148-
resolve(undefined);
149-
});
150-
});
151-
await p;
152-
expect(f).toBeCalled();
153-
});
154-
155-
testWithClient('Connect', async (client) => {
156-
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
157-
const p = new Promise((resolve, _) => {
158-
client.on('connect', () => {
159-
setTimeout(() => {
160-
expect(true).toBeTruthy();
161-
resolve(undefined);
162-
}, 125);
163-
});
164-
});
165-
expect.assertions(1);
166-
return p;
167-
});
168-
});
169-
170139
describe('Timeout', () => {
171140
test('Connection timeout', async () => {
172141
const server = createServer();

test/helper.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ export function testWithClient(name: string, fn: Test, timeout?: number) {
1111
});
1212
client.on('notice', console.log);
1313
test(name, async () => {
14-
let closed = true;
1514
let connected = false;
16-
client.on('connect', () => { closed = false; });
17-
client.on('end', () => { closed = true; });
1815
const p2 = client.connect();
1916
const p1 = fn(client);
2017
try {
@@ -25,9 +22,8 @@ export function testWithClient(name: string, fn: Test, timeout?: number) {
2522
if (!connected) {
2623
await p2;
2724
}
28-
if (!closed) {
25+
if (!client.closed) {
2926
await client.end();
30-
if (!closed) throw new Error("Expected client close event");
3127
if (!client.closed) throw new Error("Expected client to be closed");
3228
}
3329
}

0 commit comments

Comments
 (0)