@passioncloud/sftp-connection
Advanced tools
Comparing version 1.0.1 to 1.0.2
34
index.js
const Client = require('ssh2-sftp-client'); | ||
const sftp = new Client(); | ||
let currentHost = '' | ||
let currentPort = '' | ||
@@ -12,4 +14,11 @@ async function establishSftpConnection({ | ||
}) { | ||
const sftpConnectionAvailable = await isSftpConnectionAvailable(); | ||
const sftpConnectionAvailable = await isSftpConnectionAvailable({ | ||
host, | ||
port, | ||
}); | ||
// for this connection to work, on windows, SYSTEM must have permissions to read the private key | ||
if (!sftpConnectionAvailable) { | ||
currentHost = host | ||
currentPort = port | ||
await sftp.connect({ | ||
@@ -20,3 +29,3 @@ host, | ||
password, | ||
privateKey | ||
privateKey, | ||
}); | ||
@@ -26,6 +35,21 @@ } | ||
async function isSftpConnectionAvailable() { | ||
async function isSftpConnectionAvailable({ | ||
host, | ||
port | ||
}) { | ||
try { | ||
await sftp.exists('/'); | ||
return true; | ||
if (host == currentHost && port === currentPort) { | ||
// you are connection to the same sftp server and a connection | ||
// already exists. return true | ||
return true; | ||
} | ||
else { | ||
// you are trying to connect to a different sftp server | ||
// end the current sftp connection and then return false | ||
currentHost = host | ||
currentPort = port | ||
await sftp.end() | ||
return false; | ||
} | ||
} | ||
@@ -47,3 +71,3 @@ catch (e) { return false; } | ||
password, | ||
privateKey | ||
privateKey, | ||
}); | ||
@@ -50,0 +74,0 @@ return sftp; |
{ | ||
"name": "@passioncloud/sftp-connection", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
2682
67