Skip to content

Commit 6519929

Browse files
authored
fix: simplify the code for downloadInChunks (#2323)
* fix: simplify the code for downloadInChunks * cleanup p-limit function
1 parent a7d09c1 commit 6519929

File tree

1 file changed

+19
-31
lines changed

1 file changed

+19
-31
lines changed

src/transfer-manager.ts

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -651,42 +651,30 @@ export class TransferManager {
651651
let chunkEnd = start + chunkSize - 1;
652652
chunkEnd = chunkEnd > size ? size : chunkEnd;
653653
promises.push(
654-
limit(() =>
655-
file
656-
.download({
657-
start: chunkStart,
658-
end: chunkEnd,
659-
[GCCL_GCS_CMD_KEY]: GCCL_GCS_CMD_FEATURE.DOWNLOAD_SHARDED,
660-
})
661-
.then(resp => {
662-
return fileToWrite.write(resp[0], 0, resp[0].length, chunkStart);
663-
})
664-
)
654+
limit(async () => {
655+
const resp = await file.download({
656+
start: chunkStart,
657+
end: chunkEnd,
658+
[GCCL_GCS_CMD_KEY]: GCCL_GCS_CMD_FEATURE.DOWNLOAD_SHARDED,
659+
});
660+
return fileToWrite.write(resp[0], 0, resp[0].length, chunkStart);
661+
})
665662
);
666663

667664
start += chunkSize;
668665
}
669666

670-
return new Promise((resolve, reject) => {
671-
let results: DownloadResponse;
672-
Promise.all(promises)
673-
.then(data => {
674-
results = data.map(result => result.buffer) as DownloadResponse;
675-
if (options.validation === 'crc32c') {
676-
return CRC32C.fromFile(filePath);
677-
}
678-
return;
679-
})
680-
.then(() => {
681-
resolve(results);
682-
})
683-
.catch(e => {
684-
reject(e);
685-
})
686-
.finally(() => {
687-
fileToWrite.close();
688-
});
689-
});
667+
let results: DownloadResponse;
668+
try {
669+
const data = await Promise.all(promises);
670+
results = data.map(result => result.buffer) as DownloadResponse;
671+
if (options.validation === 'crc32c') {
672+
await CRC32C.fromFile(filePath);
673+
}
674+
return results;
675+
} finally {
676+
fileToWrite.close();
677+
}
690678
}
691679

692680
/**

0 commit comments

Comments
 (0)