Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: properly reject when socket ends during connection #122

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/amqp-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { AMQPProperties } from './amqp-properties.js'
import type { AMQPConsumer } from './amqp-consumer.js'

/**
* Convience class for queues
* Convenience class for queues
*/
export class AMQPQueue {
readonly channel: AMQPChannel
Expand Down
23 changes: 15 additions & 8 deletions src/amqp-socket-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,21 @@ export class AMQPClient extends AMQPBaseClient {
}

override connect(): Promise<AMQPBaseClient> {
let rejectConnection: (reason: Error) => void
const socket = this.connectSocket()
socket.on('connect', () => {
socket.on('error', (err) => this.onerror(new AMQPError(err.message, this)));
socket.on('end', () => {
if (rejectConnection) {
rejectConnection(new AMQPError('Connection ended', this))
}
})
socket.on('close', (hadError: boolean) => {
const clientClosed = this.closed;
this.closed = true;
if (!hadError && !clientClosed) this.onerror(new AMQPError('Socket closed', this));
});
});
Object.defineProperty(this, 'socket', {
value: socket,
writable: true,
Expand All @@ -59,6 +73,7 @@ export class AMQPClient extends AMQPBaseClient {
// enable TCP keepalive if AMQP heartbeats are disabled
if (this.heartbeat === 0) socket.setKeepAlive(true, 60)
return new Promise((resolve, reject) => {
rejectConnection = reject;
socket.on('timeout', () => reject(new AMQPError("timeout", this)))
socket.on('error', (err) => reject(new AMQPError(err.message, this)))
const onConnect = (conn: AMQPBaseClient) => {
Expand All @@ -80,14 +95,6 @@ export class AMQPClient extends AMQPBaseClient {
const sendStart = () => this.send(new Uint8Array([65, 77, 81, 80, 0, 0, 9, 1]))
const conn = this.tls ? tls.connect(options, sendStart) : net.connect(options, sendStart)
conn.on('data', this.onRead.bind(this))
conn.on('connect', () => {
conn.on('error', (err) => this.onerror(new AMQPError(err.message, this)))
conn.on('close', (hadError: boolean) => {
const clientClosed = this.closed
this.closed = true
if (!hadError && !clientClosed) this.onerror(new AMQPError("Socket closed", this))
})
})
return conn
}

Expand Down
Loading