Skip to content

Commit

Permalink
Cleanup wrapWithTls.
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurschreiber committed Sep 16, 2024
1 parent 04c8869 commit 069d254
Showing 1 changed file with 21 additions and 32 deletions.
53 changes: 21 additions & 32 deletions src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2164,8 +2164,6 @@ class Connection extends EventEmitter {
async wrapWithTls(socket: net.Socket, signal: AbortSignal): Promise<tls.TLSSocket> {
signal.throwIfAborted();

const { promise, resolve, reject } = withResolvers<tls.TLSSocket>();

const secureContext = tls.createSecureContext(this.secureContextOptions);
// If connect to an ip address directly,
// need to set the servername to an empty string
Expand All @@ -2179,43 +2177,34 @@ class Connection extends EventEmitter {
servername: this.config.options.serverName ? this.config.options.serverName : serverName,
};

const { promise, resolve, reject } = withResolvers<tls.TLSSocket>();
const encryptsocket = tls.connect(encryptOptions);

const onAbort = () => {
encryptsocket.removeListener('error', onError);
encryptsocket.removeListener('connect', onConnect);

encryptsocket.destroy();

reject(signal.reason);
};
try {
const onAbort = () => { reject(signal.reason); };
signal.addEventListener('abort', onAbort, { once: true });

const onError = (err: Error) => {
signal.removeEventListener('abort', onAbort);
try {
const onError = reject;
const onConnect = () => { resolve(encryptsocket); };

encryptsocket.removeListener('error', onError);
encryptsocket.removeListener('connect', onConnect);
encryptsocket.once('error', onError);
encryptsocket.once('secureConnect', onConnect);

try {
return await promise;
} finally {
encryptsocket.removeListener('error', onError);
encryptsocket.removeListener('connect', onConnect);
}
} finally {
signal.removeEventListener('abort', onAbort);
}
} catch (err: any) {
encryptsocket.destroy();

reject(err);
};

const onConnect = () => {
signal.removeEventListener('abort', onAbort);

encryptsocket.removeListener('error', onError);
encryptsocket.removeListener('connect', onConnect);

resolve(encryptsocket);
};

signal.addEventListener('abort', onAbort, { once: true });

encryptsocket.on('error', onError);
encryptsocket.on('secureConnect', onConnect);

return await promise;
throw err;
}
}

async connectOnPort(port: number, multiSubnetFailover: boolean, signal: AbortSignal, customConnector?: () => Promise<net.Socket>) {
Expand Down

0 comments on commit 069d254

Please sign in to comment.