From ed709fc20aec5c16ecde51b6296eafd8c2f54b76 Mon Sep 17 00:00:00 2001 From: Fendor Date: Sat, 1 Jul 2023 11:38:28 +0200 Subject: [PATCH] Remove unused code --- package.json | 4 +- src/utils.ts | 157 +-------------------------------------------------- yarn.lock | 32 ----------- 3 files changed, 2 insertions(+), 191 deletions(-) diff --git a/package.json b/package.json index 962aa8ad..ac5466d2 100644 --- a/package.json +++ b/package.json @@ -564,7 +564,6 @@ "@types/node": "^20.1.0", "@types/vscode": "^1.79.1", "@types/which": "^3.0.0", - "@types/yauzl": "^2.9.1", "@typescript-eslint/eslint-plugin": "^5.59.5", "@typescript-eslint/parser": "^5.57.1", "@vscode/test-electron": "^2.3.3", @@ -585,7 +584,6 @@ "dependencies": { "ts-pattern": "^4.2.2", "vscode-languageclient": "^7.0.0", - "which": "^3.0.1", - "yauzl": "^2.10.0" + "which": "^3.0.1" } } diff --git a/src/utils.ts b/src/utils.ts index ffb66f2e..2c2e84ec 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,15 +1,10 @@ import * as child_process from 'child_process'; import * as fs from 'fs'; -import * as http from 'http'; import * as https from 'https'; import * as os from 'os'; -import { extname } from 'path'; -import { promisify } from 'util'; -import { OutputChannel, ProgressLocation, window, workspace, WorkspaceFolder } from 'vscode'; +import { OutputChannel, workspace, WorkspaceFolder } from 'vscode'; import { Logger } from 'vscode-languageclient'; import * as which from 'which'; -import * as yazul from 'yauzl'; -import { createGunzip } from 'zlib'; // Used for environment variables later on export interface IEnvVars { @@ -125,18 +120,6 @@ export function comparePVP(l: string, r: string): number { */ const userAgentHeader = { 'User-Agent': 'vscode-haskell' }; -/** downloadFile may get called twice on the same src and destination: - * When this happens, we should only download the file once but return two - * promises that wait on the same download. This map keeps track of which - * files are currently being downloaded and we short circuit any calls to - * downloadFile which have a hit in this map by returning the promise stored - * here. - * Note that we have to use a double nested map since array/pointer/object - * equality is by reference, not value in Map. And we are using a tuple of - * [src, dest] as the key. - */ -const inFlightDownloads = new Map>>(); - export async function httpsGetSilently(options: https.RequestOptions): Promise { const opts: https.RequestOptions = { ...options, @@ -176,144 +159,6 @@ export async function httpsGetSilently(options: https.RequestOptions): Promise { - if (err.code === 'ENOENT') { - return; - } - throw err; -} - -export async function downloadFile(titleMsg: string, src: string, dest: string): Promise { - // Check to see if we're already in the process of downloading the same thing - const inFlightDownload = inFlightDownloads.get(src)?.get(dest); - if (inFlightDownload) { - return inFlightDownload; - } - - // If it already is downloaded just use that - if (fs.existsSync(dest)) { - return false; - } - - // Download it to a .tmp location first, then rename it! - // This way if the download fails halfway through or something then we know - // to delete it and try again - const downloadDest = dest + '.download'; - if (fs.existsSync(downloadDest)) { - fs.unlinkSync(downloadDest); - } - - const downloadTask = window - .withProgress( - { - location: ProgressLocation.Notification, - title: titleMsg, - cancellable: false, - }, - async (progress) => { - const p = new Promise((resolve, reject) => { - const srcUrl = new URL(src); - const opts: https.RequestOptions = { - host: srcUrl.host, - path: srcUrl.pathname, - protocol: srcUrl.protocol, - port: srcUrl.port, - headers: userAgentHeader, - }; - getWithRedirects(opts, (res) => { - const totalSize = parseInt(res.headers['content-length'] || '1', 10); - const fileStream = fs.createWriteStream(downloadDest, { mode: 0o744 }); - let curSize = 0; - - // Decompress it if it's a gzip or zip - const needsGunzip = - res.headers['content-type'] === 'application/gzip' || extname(srcUrl.pathname ?? '') === '.gz'; - const needsUnzip = - res.headers['content-type'] === 'application/zip' || extname(srcUrl.pathname ?? '') === '.zip'; - if (needsGunzip) { - const gunzip = createGunzip(); - gunzip.on('error', reject); - res.pipe(gunzip).pipe(fileStream); - } else if (needsUnzip) { - const zipDest = downloadDest + '.zip'; - const zipFs = fs.createWriteStream(zipDest); - zipFs.on('error', reject); - zipFs.on('close', () => { - yazul.open(zipDest, (err, zipfile) => { - if (err) { - throw err; - } - if (!zipfile) { - throw Error("Couldn't decompress zip"); - } - - // We only expect *one* file inside each zip - zipfile.on('entry', (entry: yazul.Entry) => { - zipfile.openReadStream(entry, (err2, readStream) => { - if (err2) { - throw err2; - } - readStream?.pipe(fileStream); - }); - }); - }); - }); - res.pipe(zipFs); - } else { - res.pipe(fileStream); - } - - function toMB(bytes: number) { - return bytes / (1024 * 1024); - } - - res.on('data', (chunk: Buffer) => { - curSize += chunk.byteLength; - const msg = `${toMB(curSize).toFixed(1)}MB / ${toMB(totalSize).toFixed(1)}MB`; - progress.report({ message: msg, increment: (chunk.length / totalSize) * 100 }); - }); - res.on('error', reject); - fileStream.on('close', resolve); - }).on('error', reject); - }); - try { - await p; - // Finally rename it to the actual dest - fs.renameSync(downloadDest, dest); - } finally { - // And remember to remove it from the list of current downloads - inFlightDownloads.get(src)?.delete(dest); - } - } - ) - .then(() => true); - - try { - if (inFlightDownloads.has(src)) { - inFlightDownloads.get(src)?.set(dest, downloadTask); - } else { - inFlightDownloads.set(src, new Map([[dest, downloadTask]])); - } - return await downloadTask; - } catch (e: any) { - await promisify(fs.unlink)(downloadDest).catch(ignoreFileNotExists); - throw new Error(`Failed to download ${src}:\n${e.message}`); - } -} - -function getWithRedirects(opts: https.RequestOptions, f: (res: http.IncomingMessage) => void): http.ClientRequest { - return https.get(opts, (res) => { - if (res.statusCode === 301 || res.statusCode === 302) { - if (!res.headers.location) { - console.error('301/302 without a location header'); - return; - } - https.get(res.headers.location, f); - } else { - f(res); - } - }); -} /* * Checks if the executable is on the PATH diff --git a/yarn.lock b/yarn.lock index fe10686b..829f06df 100644 --- a/yarn.lock +++ b/yarn.lock @@ -256,13 +256,6 @@ dependencies: "@types/yargs-parser" "*" -"@types/yauzl@^2.9.1": - version "2.10.0" - resolved "https://registry.yarnpkg.com/@types/yauzl/-/yauzl-2.10.0.tgz#b3248295276cf8c6f153ebe6a9aba0c988cb2599" - integrity sha512-Cn6WYCm0tXv8p6k+A8PvbDG763EDpBoTzHdA+Q/MF6H3sapGjCm9NzoaJncJS9tUKSuCoDs9XHxYYsQDgxR6kw== - dependencies: - "@types/node" "*" - "@typescript-eslint/eslint-plugin@^5.59.5": version "5.59.5" resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.59.5.tgz#f156827610a3f8cefc56baeaa93cd4a5f32966b4" @@ -680,11 +673,6 @@ browserslist@^4.14.5: node-releases "^2.0.6" update-browserslist-db "^1.0.9" -buffer-crc32@~0.2.3: - version "0.2.13" - resolved "https://registry.yarnpkg.com/buffer-crc32/-/buffer-crc32-0.2.13.tgz#0d333e3f00eac50aa1454abd30ef8c2a5d9a7242" - integrity sha512-VO9Ht/+p3SN7SKWqcrgEzjGbRSJYTx+Q1pTQC0wrWqHx0vpJraQ6GtHx8tvcg1rlK1byhU5gccxgOgj7B0TDkQ== - buffer-from@^1.0.0: version "1.1.2" resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.2.tgz#2b146a6fd72e80b4f55d255f35ed59a3a9a41bd5" @@ -1040,13 +1028,6 @@ fastq@^1.6.0: dependencies: reusify "^1.0.4" -fd-slicer@~1.1.0: - version "1.1.0" - resolved "https://registry.yarnpkg.com/fd-slicer/-/fd-slicer-1.1.0.tgz#25c7c89cb1f9077f8891bbe61d8f390eae256f1e" - integrity sha512-cE1qsB/VwyQozZ+q1dGxR8LBYNZeofhEdUNGSMbQD3Gw2lAzX9Zb3uIU6Ebc/Fmyjo9AWWfnn0AUCHqtevs/8g== - dependencies: - pend "~1.2.0" - file-entry-cache@^6.0.1: version "6.0.1" resolved "https://registry.yarnpkg.com/file-entry-cache/-/file-entry-cache-6.0.1.tgz#211b2dd9659cb0394b073e7323ac3c933d522027" @@ -1733,11 +1714,6 @@ path-type@^4.0.0: resolved "https://registry.yarnpkg.com/path-type/-/path-type-4.0.0.tgz#84ed01c0a7ba380afe09d90a8c180dcd9d03043b" integrity sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw== -pend@~1.2.0: - version "1.2.0" - resolved "https://registry.yarnpkg.com/pend/-/pend-1.2.0.tgz#7a57eb550a6783f9115331fcf4663d5c8e007a50" - integrity sha512-F3asv42UuXchdzt+xXqfW1OGlVBe+mxa2mqI0pg5yAHZPvFmY3Y6drSf/GQ1A86WgWEN9Kzh/WrgKa6iGcHXLg== - picocolors@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/picocolors/-/picocolors-1.0.0.tgz#cb5bdc74ff3f51892236eaf79d68bc44564ab81c" @@ -2298,14 +2274,6 @@ yargs@16.2.0: y18n "^5.0.5" yargs-parser "^20.2.2" -yauzl@^2.10.0: - version "2.10.0" - resolved "https://registry.yarnpkg.com/yauzl/-/yauzl-2.10.0.tgz#c7eb17c93e112cb1086fa6d8e51fb0667b79a5f9" - integrity sha512-p4a9I6X6nu6IhoGmBqAcbJy1mlC4j27vEPZX9F4L4/vZT3Lyq1VkFHw/V/PUcB9Buo+DG3iHkT0x3Qya58zc3g== - dependencies: - buffer-crc32 "~0.2.3" - fd-slicer "~1.1.0" - yocto-queue@^0.1.0: version "0.1.0" resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b"