Skip to content
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

Replace decompress-targz with tar-fs and gunzip-maybe #79

Merged
merged 2 commits into from
Apr 13, 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
23 changes: 12 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vscode/test-web",
"version": "0.0.41",
"version": "0.0.42",
"scripts": {
"install-extensions": "yarn --cwd=fs-provider && yarn --cwd=sample",
"compile": "tsc -p ./ && yarn compile-fs-provider",
Expand All @@ -25,18 +25,18 @@
"dependencies": {
"@koa/router": "^12.0.0",
"@koa/cors": "^4.0.0",
"koa": "^2.14.1",
"koa": "^2.14.2",
"koa-morgan": "^1.0.1",
"koa-mount": "^4.0.0",
"koa-static": "^5.0.0",
"minimist": "^1.2.8",
"playwright": "^1.32.2",
"playwright": "^1.32.3",
"vscode-uri": "^3.0.7",
"http-proxy-agent": "^5.0.0",
"https-proxy-agent": "^5.0.1",
"decompress": "^4.2.1",
"decompress-targz": "^4.1.1",
"get-stream": "6.0.1"
"get-stream": "6.0.1",
"tar-fs": "^2.1.1",
"gunzip-maybe": "^1.4.2"
},
"devDependencies": {
"@types/koa": "^2.13.6",
Expand All @@ -46,12 +46,13 @@
"@types/koa__router": "^12.0.0",
"@types/minimist": "^1.2.2",
"@types/node": "16.x",
"@typescript-eslint/eslint-plugin": "^5.57.0",
"@typescript-eslint/parser": "^5.57.0",
"@types/decompress": "^4.2.4",
"eslint": "^8.37.0",
"@types/gunzip-maybe": "^1.4.0",
"@types/tar-fs": "^2.0.1",
"@typescript-eslint/eslint-plugin": "^5.58.0",
"@typescript-eslint/parser": "^5.58.0",
"eslint": "^8.38.0",
"eslint-plugin-header": "^3.1.1",
"typescript": "^5.0.3"
"typescript": "^5.0.4"
},
"license": "MIT",
"author": "Visual Studio Code Team",
Expand Down
56 changes: 20 additions & 36 deletions src/server/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { promises as fs, existsSync, createWriteStream } from 'fs';
import { promises as fs, existsSync } from 'fs';
import * as path from 'path';

import * as https from 'https';
Expand All @@ -12,8 +12,6 @@ import * as createHttpsProxyAgent from 'https-proxy-agent';
import * as createHttpProxyAgent from 'http-proxy-agent';
import { URL } from 'url';

import * as decompress from 'decompress';
import * as decompressTargz from 'decompress-targz';
import { Static } from './main';

interface DownloadInfo {
Expand All @@ -28,9 +26,16 @@ async function getLatestVersion(quality: 'stable' | 'insider'): Promise<Download

const reset = '\x1b[G\x1b[0K';

async function download(downloadUrl: string, destination: string, message: string) {
async function downloadAndUntar(downloadUrl: string, destination: string, message: string) : Promise<void> {
process.stdout.write(message);

if (!existsSync(destination)) {
await fs.mkdir(destination, { recursive: true });
}

const tar = await import('tar-fs');
const gunzip = await import('gunzip-maybe');

return new Promise((resolve, reject) => {
const httpLibrary = downloadUrl.startsWith('https') ? https : http;

Expand All @@ -39,10 +44,6 @@ async function download(downloadUrl: string, destination: string, message: strin
let received = 0;
let timeout: NodeJS.Timeout | undefined;

const outStream = createWriteStream(destination);
outStream.on('close', () => resolve(destination));
outStream.on('error', reject);

res.on('data', chunk => {
if (!timeout) {
timeout = setTimeout(() => {
Expand All @@ -62,29 +63,21 @@ async function download(downloadUrl: string, destination: string, message: strin
});


res.on('error', reject);
res.pipe(outStream);
const extract = res.pipe(gunzip()).pipe(tar.extract(destination, { strip: 1 }));
extract.on('finish', () => {
process.stdout.write(`Extracted to ${destination}\n`);
resolve();
});
extract.on('error', reject);
});
});
}

async function unzip(source: string, destination: string, message: string) {
process.stdout.write(message);
if (!existsSync(destination)) {
await fs.mkdir(destination, { recursive: true });
}

await decompress(source, destination, {
plugins: [
decompressTargz()
],
strip: 1
});
process.stdout.write(`${reset}${message}: complete\n`);
}

export async function downloadAndUnzipVSCode(quality: 'stable' | 'insider', vscodeTestDir: string): Promise<Static> {
const info = await getLatestVersion(quality);
if (!info.url.endsWith('.tar.gz')) {
throw new Error(`Unexpected download URL: ${info.url}. Should end with .tar.gz`);
}

const folderName = `vscode-web-${quality}-${info.version}`;

Expand All @@ -101,21 +94,12 @@ export async function downloadAndUnzipVSCode(quality: 'stable' | 'insider', vsco

const productName = `VS Code ${quality === 'stable' ? 'Stable' : 'Insiders'}`;

const tmpArchiveName = path.join(vscodeTestDir, `vscode-web-${quality}-${info.version}-tmp`);
try {
await download(info.url, tmpArchiveName, `Downloading ${productName}`);
await unzip(tmpArchiveName, downloadedPath, `Unpacking ${productName}`);
await downloadAndUntar(info.url, downloadedPath, `Downloading ${productName}`);
await fs.writeFile(path.join(downloadedPath, 'version'), folderName);
} catch (err) {
console.error(err);
throw Error(`Failed to download and unpack ${productName}`);
} finally {
try {
fs.unlink(tmpArchiveName);
} catch (e) {
// ignore
}

}
return { type: 'static', location: downloadedPath, quality, version: info.version };
}
Expand Down Expand Up @@ -195,4 +179,4 @@ export async function fileExists(path: string): Promise<boolean> {
} catch {
return false;
}
}
}
Loading