Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,18 @@ export class FluentSocket extends EventEmitter {
this.processMessages(protocol.decodeServerStream(this.passThroughStream));

return new Promise<void>((resolve, reject) => {
// This may call both resolve and reject, but the ES standard says this is OK
this.once(FluentSocketEvent.CONNECTED, () => resolve());
this.once(FluentSocketEvent.ERROR, err => reject(err));
const onConnected = () => {
resolve();
// Avoid a memory leak and remove the other listener
this.removeListener(FluentSocketEvent.ERROR, onError);
};
const onError = (err: Error) => {
reject(err);
// Avoid a memory leak and remove the other listener
this.removeListener(FluentSocketEvent.CONNECTED, onConnected);
};
this.once(FluentSocketEvent.CONNECTED, onConnected);
this.once(FluentSocketEvent.ERROR, onError);
});
}

Expand Down Expand Up @@ -483,7 +492,7 @@ export class FluentSocket extends EventEmitter {
this.onMessage(message);
}
} catch (e) {
this.close(CloseState.RECONNECT, e);
this.close(CloseState.RECONNECT, e as Error);
}
}

Expand Down
13 changes: 13 additions & 0 deletions test/test.socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ describe("FluentSocket", () => {
sinon.assert.calledOnce(connectStub);
});

it("should not preserve error handlers after connect", done => {
const {socket, connectStub} = createFluentSocket({disableReconnect: true});

socket.on(FluentSocketEvent.WRITABLE, () => {
expect(socket.listenerCount(FluentSocketEvent.ERROR)).to.equal(0);
done();
});

socket.connect();

sinon.assert.calledOnce(connectStub);
});

it("should not block for draining on write by default", done => {
const {socket, stream, connectStub} = createFluentSocket({
disableReconnect: true,
Expand Down