Skip to content

Commit 1008aa4

Browse files
authored
fix: await progress-log writes before finishing CLI download (#1082)
1 parent ddf9a82 commit 1008aa4

1 file changed

Lines changed: 20 additions & 12 deletions

File tree

src/core/cliManager.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,8 @@ export class CliManager {
799799
? "unknown"
800800
: prettyBytes(contentLength);
801801

802+
let progressWrite: Promise<void> = Promise.resolve();
803+
802804
// Pipe data received from the request to the stream.
803805
readStream.on("data", (buffer: Buffer) => {
804806
writeStream.write(buffer, () => {
@@ -810,7 +812,7 @@ export class CliManager {
810812
: (buffer.byteLength / contentLength) * 100,
811813
});
812814
if (onProgress) {
813-
onProgress(
815+
progressWrite = onProgress(
814816
written,
815817
Number.isNaN(contentLength) ? null : contentLength,
816818
).catch((error) => {
@@ -827,27 +829,33 @@ export class CliManager {
827829
return new Promise<boolean>((resolve, reject) => {
828830
writeStream.on("error", (error) => {
829831
readStream.destroy();
830-
reject(
831-
new Error(
832-
`Unable to download binary: ${errToStr(error, "no reason given")}`,
832+
void progressWrite.then(() =>
833+
reject(
834+
new Error(
835+
`Unable to download binary: ${errToStr(error, "no reason given")}`,
836+
),
833837
),
834838
);
835839
});
836840
readStream.on("error", (error) => {
837841
writeStream.close();
838-
reject(
839-
new Error(
840-
`Unable to download binary: ${errToStr(error, "no reason given")}`,
842+
void progressWrite.then(() =>
843+
reject(
844+
new Error(
845+
`Unable to download binary: ${errToStr(error, "no reason given")}`,
846+
),
841847
),
842848
);
843849
});
844850
readStream.on("close", () => {
845851
writeStream.close();
846-
if (cancelled) {
847-
resolve(false);
848-
} else {
849-
resolve(true);
850-
}
852+
void progressWrite.then(() => {
853+
if (cancelled) {
854+
resolve(false);
855+
} else {
856+
resolve(true);
857+
}
858+
});
851859
});
852860
});
853861
},

0 commit comments

Comments
 (0)