-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement DownloadManager for TLS library downloads and refactor Modu…
…leClient to utilize it
- Loading branch information
1 parent
ec5659b
commit a6bdeb6
Showing
3 changed files
with
61 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters