@cyberalien/deploy-utils 0.0.7 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -42,7 +42,7 @@ async function connectToSSHWithKeys(host, username, keysOrFilenames) {
42
42
  username,
43
43
  privateKey
44
44
  });
45
- } catch (err) {}
45
+ } catch {}
46
46
  throw new Error(`Failed to connect to ${host} with provided API keys`);
47
47
  }
48
48
  export { connectToSSH, connectToSSHWithKeys };
@@ -30,6 +30,44 @@ async function createDirectories(client, rootDir, filenames) {
30
30
  if (execResult.code !== 0) throw new Error(`Failed to create directories: ${execResult.stderr || execResult.stdout}`);
31
31
  }
32
32
  /**
33
+ * Create SFTP sessions
34
+ */
35
+ async function createSessions(client, total) {
36
+ const sftp = [await startSFTPSession(client)];
37
+ const addSession = async () => {
38
+ try {
39
+ sftp.push(await startSFTPSession(client));
40
+ } catch {}
41
+ };
42
+ if (total > 3) await addSession();
43
+ if (total > 10) await addSession();
44
+ if (total > 20) await addSession();
45
+ return sftp;
46
+ }
47
+ /**
48
+ * Process queue
49
+ */
50
+ async function processQueue(client, queue, callback) {
51
+ const sessions = await createSessions(client, queue.length);
52
+ await new Promise((resolve, reject) => {
53
+ let activeSessions = 0;
54
+ const next = (sftp) => {
55
+ const file = queue.shift();
56
+ if (!file) {
57
+ activeSessions--;
58
+ sftp.end();
59
+ if (!activeSessions) resolve(void 0);
60
+ return;
61
+ }
62
+ callback(sftp, file).then(() => next(sftp)).catch(reject);
63
+ };
64
+ for (const session of sessions) {
65
+ activeSessions++;
66
+ next(session);
67
+ }
68
+ });
69
+ }
70
+ /**
33
71
  * Upload multiple files
34
72
  *
35
73
  * For binary files use putSFTPFiles() instead
@@ -57,19 +95,19 @@ async function uploadSFTPFiles(client, remoteRootDir, files) {
57
95
  console.log(`[${index} / ${filesCount}] Uploading ${file}`);
58
96
  await uploadSFTPFile(sftp, target, typeof content === "string" ? content : JSON.stringify(content, null, 2) + "\n");
59
97
  }
98
+ sftp.end();
60
99
  }
61
100
  /**
62
101
  * Upload multiple local files
63
102
  */
64
103
  async function putSFTPFiles(client, localRootDir, remoteRootDir, filenames) {
65
104
  await createDirectories(client, remoteRootDir, filenames);
66
- const sftp = await startSFTPSession(client);
67
- const filesCount = filenames.length;
68
105
  let index = 0;
69
- for (const file of filenames) {
106
+ const filesCount = filenames.length;
107
+ await processQueue(client, [...filenames], async (sftp, file) => {
70
108
  index++;
71
109
  console.log(`[${index} / ${filesCount}] Uploading ${file}`);
72
110
  await putSFTPFile(sftp, join(localRootDir, file), join(remoteRootDir, file));
73
- }
111
+ });
74
112
  }
75
113
  export { putSFTPFiles, uploadSFTPFiles };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "type": "module",
4
4
  "description": "Functions for deploying stuff.",
5
5
  "author": "Vjacheslav Trushkin",
6
- "version": "0.0.7",
6
+ "version": "0.0.8",
7
7
  "license": "UNLICENSED",
8
8
  "homepage": "https://cyberalien.dev/",
9
9
  "sideEffects": false,