fix: TDZ crash on SSH connect failure — sessionId referenced before assignment

When SSH timed out, the onClose callback referenced `const sessionId`
before connect() resolved, causing a Temporal Dead Zone ReferenceError
that killed the process. Changed to `let` with try/catch so connection
failures send an error message to the client instead of crashing.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Vantz Stockwell 2026-03-14 00:50:35 -04:00
parent 8d95fc5652
commit 639ac329a8

View File

@ -45,16 +45,22 @@ export class TerminalGateway {
private async handleMessage(client: any, msg: any) { private async handleMessage(client: any, msg: any) {
switch (msg.type) { switch (msg.type) {
case 'connect': { case 'connect': {
const sessionId = await this.ssh.connect( let sessionId = '';
try {
sessionId = await this.ssh.connect(
msg.hostId, msg.hostId,
(data) => this.send(client, { type: 'data', sessionId, data }), (data) => this.send(client, { type: 'data', sessionId, data }),
(reason) => this.send(client, { type: 'disconnected', sessionId, reason }), (reason) => this.send(client, { type: 'disconnected', sessionId, reason }),
async (fingerprint, isNew) => { async (fingerprint, isNew) => {
// Send verification request to client
this.send(client, { type: 'host-key-verify', fingerprint, isNew }); this.send(client, { type: 'host-key-verify', fingerprint, isNew });
return true; // auto-accept for now, full flow in Task 12 return true; // auto-accept for now, full flow in Task 12
}, },
); );
} catch (err: any) {
this.logger.error(`[WS] SSH connect failed: ${err.message}`);
this.send(client, { type: 'error', message: `Connection failed: ${err.message}` });
break;
}
const sessions = this.clientSessions.get(client) || []; const sessions = this.clientSessions.get(client) || [];
sessions.push(sessionId); sessions.push(sessionId);
this.clientSessions.set(client, sessions); this.clientSessions.set(client, sessions);