Skip to content

Remove unused code #898

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
}
}
157 changes: 1 addition & 156 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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<string, Map<string, Thenable<boolean>>>();

export async function httpsGetSilently(options: https.RequestOptions): Promise<string> {
const opts: https.RequestOptions = {
...options,
Expand Down Expand Up @@ -176,144 +159,6 @@ export async function httpsGetSilently(options: https.RequestOptions): Promise<s
});
}

async function ignoreFileNotExists(err: NodeJS.ErrnoException): Promise<void> {
if (err.code === 'ENOENT') {
return;
}
throw err;
}

export async function downloadFile(titleMsg: string, src: string, dest: string): Promise<boolean> {
// 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<void>((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
Expand Down
32 changes: 0 additions & 32 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down