Gzip artifacts

Co-authored-by: bjorn3 <bjorn3@users.noreply.github.com>

Override miniz_oxide to build it with optimizations

Building this crate with optimizations decreases the gzipping
part of `cargo xtask dist` from `30-40s` down to `3s`,
the overhead for `rustc` to apply optimizations is miserable on this background
This commit is contained in:
Veetaha 2020-06-21 15:58:34 +03:00
parent 980a67f446
commit f92bfb5807
6 changed files with 68 additions and 24 deletions

View file

@ -3,6 +3,7 @@ import * as vscode from "vscode";
import * as stream from "stream";
import * as crypto from "crypto";
import * as fs from "fs";
import * as zlib from "zlib";
import * as util from "util";
import * as path from "path";
import { log, assert } from "./util";
@ -65,6 +66,7 @@ interface DownloadOpts {
url: string;
dest: string;
mode?: number;
gunzip?: boolean;
}
export async function download(opts: DownloadOpts) {
@ -82,7 +84,7 @@ export async function download(opts: DownloadOpts) {
},
async (progress, _cancellationToken) => {
let lastPercentage = 0;
await downloadFile(opts.url, tempFile, opts.mode, (readBytes, totalBytes) => {
await downloadFile(opts.url, tempFile, opts.mode, !!opts.gunzip, (readBytes, totalBytes) => {
const newPercentage = (readBytes / totalBytes) * 100;
progress.report({
message: newPercentage.toFixed(0) + "%",
@ -97,16 +99,11 @@ export async function download(opts: DownloadOpts) {
await fs.promises.rename(tempFile, opts.dest);
}
/**
* Downloads file from `url` and stores it at `destFilePath` with `mode` (unix permissions).
* `onProgress` callback is called on recieveing each chunk of bytes
* to track the progress of downloading, it gets the already read and total
* amount of bytes to read as its parameters.
*/
async function downloadFile(
url: string,
destFilePath: fs.PathLike,
mode: number | undefined,
gunzip: boolean,
onProgress: (readBytes: number, totalBytes: number) => void
): Promise<void> {
const res = await fetch(url);
@ -130,7 +127,10 @@ async function downloadFile(
});
const destFileStream = fs.createWriteStream(destFilePath, { mode });
await pipeline(res.body, destFileStream);
const srcStream = gunzip ? res.body.pipe(zlib.createGunzip()) : res.body;
await pipeline(srcStream, destFileStream);
await new Promise<void>(resolve => {
destFileStream.on("close", resolve);
destFileStream.destroy();