Skip to content

Commit 2c04019

Browse files
Jami CogswellJami Cogswell
authored andcommitted
Simplify source root downloading with 'pipe'
1 parent 0e6aa81 commit 2c04019

File tree

1 file changed

+15
-24
lines changed

1 file changed

+15
-24
lines changed

extensions/ql-vscode/src/variant-analysis/view-autofixes.ts

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import { createTimeoutSignal } from "../common/fetch-stream";
4242
import { unzipToDirectoryConcurrently } from "../common/unzip-concurrently";
4343
import { reportUnzipProgress } from "../common/vscode/unzip-progress";
4444
import { getDirectoryNamesInsidePath } from "../common/files";
45+
import { Readable } from "stream";
4546

4647
// Limit to three repos when generating autofixes so not sending
4748
// too many requests to autofix. Since we only need to validate
@@ -459,11 +460,8 @@ async function downloadPublicCommitSource(
459460
);
460461

461462
// Set timeout
462-
const {
463-
signal,
464-
onData,
465-
dispose: disposeTimeout,
466-
} = createTimeoutSignal(downloadTimeout());
463+
const { signal, dispose: disposeTimeout } =
464+
createTimeoutSignal(downloadTimeout());
467465

468466
// Fetch the url
469467
let response: Response;
@@ -507,25 +505,18 @@ async function downloadPublicCommitSource(
507505
);
508506

509507
try {
510-
const reader = body.getReader();
511-
for (;;) {
512-
const { done, value } = await reader.read();
513-
if (done) {
514-
break;
515-
}
516-
517-
onData();
518-
reportProgress(value?.length ?? 0);
519-
520-
await new Promise((resolve, reject) => {
521-
archiveFileStream.write(value, (err) => {
522-
if (err) {
523-
reject(err);
524-
}
525-
resolve(undefined);
526-
});
527-
});
528-
}
508+
const readable = Readable.fromWeb(body);
509+
readable.on("data", (chunk) => {
510+
reportProgress(chunk?.length ?? 0);
511+
});
512+
await new Promise((resolve, reject) => {
513+
readable
514+
.pipe(archiveFileStream)
515+
.on("error", (err) => {
516+
reject(err);
517+
})
518+
.on("finish", () => resolve(undefined));
519+
});
529520

530521
await new Promise((resolve, reject) => {
531522
archiveFileStream.close((err) => {

0 commit comments

Comments
 (0)