Skip to content

Commit 363d80b

Browse files
author
Veetaha
committed
vscode: switched to more recent node 10 pipeline api
1 parent 78ee964 commit 363d80b

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed
Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import fetch from "node-fetch";
22
import * as fs from "fs";
3+
import * as stream from "stream";
4+
import * as util from "util";
35
import { strict as assert } from "assert";
46

7+
const pipeline = util.promisify(stream.pipeline);
8+
59
/**
610
* Downloads file from `url` and stores it at `destFilePath`.
711
* `onProgress` callback is called on recieveing each chunk of bytes
@@ -18,16 +22,14 @@ export async function downloadFile(
1822
const totalBytes = Number(response.headers.get('content-length'));
1923
assert(!Number.isNaN(totalBytes), "Sanity check of content-length protocol");
2024

25+
console.log("Downloading file of", totalBytes, "bytes size from", url, "to", destFilePath);
26+
2127
let readBytes = 0;
2228

23-
console.log("Downloading file of", totalBytes, "bytes size from", url, "to", destFilePath);
29+
response.body.on("data", (chunk: Buffer) => {
30+
readBytes += chunk.length;
31+
onProgress(readBytes, totalBytes);
32+
});
2433

25-
return new Promise<void>((resolve, reject) => response.body
26-
.on("data", (chunk: Buffer) => {
27-
readBytes += chunk.length;
28-
onProgress(readBytes, totalBytes);
29-
})
30-
.on("error", reject)
31-
.pipe(fs.createWriteStream(destFilePath).on("close", resolve))
32-
);
34+
await pipeline(response.body, fs.createWriteStream(destFilePath));
3335
}

0 commit comments

Comments
 (0)