Skip to content

Commit

Permalink
Implement DownloadManager for TLS library downloads and refactor Modu…
Browse files Browse the repository at this point in the history
…leClient to utilize it
  • Loading branch information
DemonMartin committed Jan 25, 2025
1 parent ec5659b commit a6bdeb6
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 19 deletions.
20 changes: 2 additions & 18 deletions src/utils/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import fetch from 'node-fetch';
import { fileURLToPath } from 'node:url';
import os from 'node:os';
import { isMainThread } from 'worker_threads';
import DownloadManager from './downloadManager.js';

let __filename, __dirname;

Expand Down Expand Up @@ -73,25 +74,8 @@ class ModuleClient {
if (this.customPath) {
throw new Error('Custom path provided but library does not exist: ' + this.TLS_LIB_PATH);
}
console.log('[tlsClient] Detected missing TLS library');
console.log('[tlsClient] DownloadPath: ' + this.tlsDependencyPath.DOWNLOAD_PATH);
console.log('[tlsClient] DestinationPath: ' + this.TLS_LIB_PATH);
console.log('[tlsClient] Downloading TLS library... This may take a while');

const response = await fetch(this.tlsDependencyPath.DOWNLOAD_PATH);
if (!response.ok) {
throw new Error(`Unexpected response ${response.statusText}`);
}
const fileStream = fs.createWriteStream(this.TLS_LIB_PATH);
response.body.pipe(fileStream);

return new Promise((resolve, reject) => {
fileStream.on('finish', () => {
console.log('[tlsClient] Successfully downloaded TLS library');
resolve();
});
fileStream.on('error', reject);
});
await DownloadManager.download(this.tlsDependencyPath.DOWNLOAD_PATH, this.TLS_LIB_PATH);
}

/**
Expand Down
59 changes: 59 additions & 0 deletions src/utils/downloadManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import fs from 'node:fs';
import fetch from 'node-fetch';

class DownloadManager {
static #downloading = false;
static #downloadPromise = null;

/**
* Downloads a file with mutex-like locking to prevent multiple simultaneous downloads
* @param {string} url - URL to download from
* @param {string} destination - Where to save the file
* @returns {Promise<void>}
*/
static async download(url, destination) {
// If already downloading, return the existing promise
if (this.#downloading) {
return this.#downloadPromise;
}

// If file exists, no need to download
if (fs.existsSync(destination)) {
return Promise.resolve();
}

// Set downloading flag and create new promise
this.#downloading = true;
this.#downloadPromise = (async () => {
try {
console.log('[tlsClient] Detected missing TLS library');
console.log('[tlsClient] DownloadPath: ' + url);
console.log('[tlsClient] DestinationPath: ' + destination);
console.log('[tlsClient] Downloading TLS library... This may take a while');

const response = await fetch(url);
if (!response.ok) {
throw new Error(`Unexpected response ${response.statusText}`);
}

const fileStream = fs.createWriteStream(destination);
await new Promise((resolve, reject) => {
response.body.pipe(fileStream);
fileStream.on('finish', () => {
console.log('[tlsClient] Successfully downloaded TLS library');
resolve();
});
fileStream.on('error', reject);
});
} finally {
// Reset flags when done or on error
this.#downloading = false;
this.#downloadPromise = null;
}
})();

return this.#downloadPromise;
}
}

export default DownloadManager;
1 change: 0 additions & 1 deletion src/utils/path.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { execSync } from 'node:child_process';
import { URL } from 'node:url';

class TlsDependency {
Expand Down

0 comments on commit a6bdeb6

Please sign in to comment.