UNPKG

autosql

Version:

An auto-parser of JSON into SQL.

51 lines (50 loc) 1.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.setSSH = setSSH; const promises_1 = require("fs/promises"); async function setSSH(sshKeys) { let SSHClient; try { SSHClient = require("ssh2").Client; } catch (err) { throw new Error(`SSH tunnel config specified but 'ssh2' is not installed. Please run: npm install ssh2`); } if (!sshKeys || !sshKeys.username) { throw new Error("No SSH username provided in sshKeys config."); } if (sshKeys.private_key_path && !sshKeys.private_key) { sshKeys.private_key = await (0, promises_1.readFile)(sshKeys.private_key_path, "utf-8"); } const sshConfig = { host: sshKeys.host, port: sshKeys.port, username: sshKeys.username, readyTimeout: sshKeys.timeout ?? 10000, ...(sshKeys.password && { password: sshKeys.password }), ...(sshKeys.private_key && { privateKey: sshKeys.private_key }), ...(sshKeys.debug && { debug: (msg) => { if (msg.includes("Outgoing") || msg.includes("Client")) { console.log(msg); } } }) }; const sshClient = new SSHClient(); // ✅ Wrap ssh connection & forwardOut into a single promise const stream = await new Promise((resolve, reject) => { sshClient .on("ready", () => { sshClient.forwardOut(sshKeys.source_address ?? "127.0.0.1", sshKeys.source_port ?? 0, sshKeys.destination_address, sshKeys.destination_port, (err, stream) => { if (err) return reject(err); stream.on("close", () => sshClient.end()); resolve(stream); }); }) .on("error", reject) .connect(sshConfig); }); return { stream, sshClient }; }