@cyberalien/deploy-utils 0.0.7 → 0.0.9

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,46 +30,82 @@ 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
36
74
  */
37
75
  async function uploadSFTPFiles(client, remoteRootDir, files) {
76
+ let index = 0;
38
77
  const filenames = [];
39
78
  let filesCount = 0;
40
79
  for (const file in files) {
41
80
  filesCount++;
42
- if (files[file] === null) continue;
81
+ if (files[file] === null) {
82
+ console.log(`[${index} / ${filesCount}] Deleting ${file}`);
83
+ await execSFTPCommand(client, `rm -f "${join(remoteRootDir, file)}"`);
84
+ continue;
85
+ }
43
86
  filenames.push(file);
44
87
  }
88
+ if (!filenames.length) return;
45
89
  await createDirectories(client, remoteRootDir, filenames);
46
- const sftp = await startSFTPSession(client);
47
- let index = 0;
48
- for (const file in files) {
90
+ await processQueue(client, filenames, async (sftp, file) => {
49
91
  index++;
50
92
  const content = files[file];
51
93
  const target = join(remoteRootDir, file);
52
- if (content === null) {
53
- console.log(`[${index} / ${filesCount}] Deleting ${file}`);
54
- await execSFTPCommand(client, `rm -f "${target}"`);
55
- continue;
56
- }
57
94
  console.log(`[${index} / ${filesCount}] Uploading ${file}`);
58
95
  await uploadSFTPFile(sftp, target, typeof content === "string" ? content : JSON.stringify(content, null, 2) + "\n");
59
- }
96
+ });
60
97
  }
61
98
  /**
62
99
  * Upload multiple local files
63
100
  */
64
101
  async function putSFTPFiles(client, localRootDir, remoteRootDir, filenames) {
65
102
  await createDirectories(client, remoteRootDir, filenames);
66
- const sftp = await startSFTPSession(client);
67
- const filesCount = filenames.length;
68
103
  let index = 0;
69
- for (const file of filenames) {
104
+ const filesCount = filenames.length;
105
+ await processQueue(client, [...filenames], async (sftp, file) => {
70
106
  index++;
71
107
  console.log(`[${index} / ${filesCount}] Uploading ${file}`);
72
108
  await putSFTPFile(sftp, join(localRootDir, file), join(remoteRootDir, file));
73
- }
109
+ });
74
110
  }
75
111
  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.9",
7
7
  "license": "UNLICENSED",
8
8
  "homepage": "https://cyberalien.dev/",
9
9
  "sideEffects": false,