Skip to content

Commit

Permalink
Update ssh.js to add error handling.
Browse files Browse the repository at this point in the history
Was running into an issue where I couldn't catch an error if the SSH client timed out. Adding the `.on` catch enabled me to catch it from the parent application. If it needs more changes, please let me know. For now I'm just patching it with `pnpm patch` which I would prefer not to do.
  • Loading branch information
SelfhostedPro authored May 16, 2023
1 parent e55461b commit 78838be
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions lib/ssh.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,43 @@
var Client = require('ssh2').Client,
http = require('http');

module.exports = function(opt) {
module.exports = function (opt) {
var conn = new Client();
var agent = new http.Agent();

agent.createConnection = function(options, fn) {
conn.once('ready', function() {
conn.exec('docker system dial-stdio', function(err, stream) {
if (err) {
conn.end();
agent.destroy();
return;
}
agent.createConnection = function (options, fn) {
try {
conn.once('ready', function () {
conn.exec('docker system dial-stdio', function (err, stream) {
if (err) {
handleError(err);
}

fn(null, stream);

stream.once('close', () => {
conn.end();
agent.destroy();
fn(null, stream);

stream.addListener('error', (err) => {
handleError(err);
});
stream.once('close', () => {
conn.end();
agent.destroy();
});
});
});
}).connect(opt);

conn.once('end', () => agent.destroy());
}).on('error', (err) => {
handleError(err);
})
.connect(opt);
conn.once('end', () => agent.destroy());
return agent;

} catch (err) {
handleError(err);
}
};

return agent;
function handleError(err) {
conn.end();
agent.destroy();
throw err;
}
};

0 comments on commit 78838be

Please sign in to comment.