Skip to content

Commit

Permalink
Merge pull request #126 from malthe/issue-125-disconnect
Browse files Browse the repository at this point in the history
Add back 'end' event and fix graceful shutdown error handling
  • Loading branch information
malthe authored Jun 19, 2024
2 parents 4449fb8 + 27a7100 commit e4466c8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
In next release ...

- Added "end" event which was removed in v1.8.0.

- The `end` method now correctly throws an error if a network error
occurred during the resulting protocol traffic.

## v2.0.3 (2024-06-15)

- Fix issue where larger results would sometimes have duplicates (#122).
Expand Down
30 changes: 18 additions & 12 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,17 @@ interface PreFlightQueue {

const DEFAULTS = new Defaults(env as Record<string, string>);

export type EventMap<
T = {
error: DatabaseError;
notice: ClientNotice;
notification: Notification;
},
> = {
[K in keyof T]: [T[K]];
};

export interface EventMap {
/** The connection has ended, possibly due to a network error. */
end: [NodeJS.ErrnoException | null];
/** A database error has occurred. */
error: [DatabaseError];
/** A client notice (typically a warning) has been received. */
notice: [ClientNotice];
/** A client notification has been received. */
notification: [Notification];
}

type Resolve<T> = (value?: T) => void;

Expand Down Expand Up @@ -209,6 +211,7 @@ export class ClientImpl {

this.stream.on('close', () => {
this.closed = true;
this.events.emit('end', null);
this.ending?.();
});

Expand All @@ -233,10 +236,12 @@ export class ClientImpl {
} else {
// Don't raise ECONNRESET errors - they can & should be
// ignored during disconnect.
if (error.errno === constants.errno.ECONNRESET) return;

if (this.ending) {
if (error.errno === constants.errno.ECONNRESET) return;
this.ending();
this.ending(error);
}
this.events.emit('end', error);
}
});

Expand Down Expand Up @@ -286,7 +291,8 @@ export class ClientImpl {

const abort = (error: Error) => {
this.handleError(error);
this.connecting?.(error);
if (!this.connecting) throw error;
this.connecting(error);
};

const startup = (stream?: Socket) => {
Expand Down
3 changes: 3 additions & 0 deletions test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,12 @@ describe('Query', () => {
idleInTransactionSessionTimeout: 500,
});
const errors: string[] = [];
let error: NodeJS.ErrnoException | null = null;
client.on('end', (isError) => error = isError);
client.on('error', (error) => errors.push(error.code));
await new Promise((resolve) => setTimeout(resolve, 625));
equal(client.closed, true);
equal(error, null);
deepEqual(errors, ['57P05']);
});

Expand Down

0 comments on commit e4466c8

Please sign in to comment.