From 6519929153e0006b61d205860de93c9e64b99d81 Mon Sep 17 00:00:00 2001 From: Denis DelGrosso <85250797+ddelgrosso1@users.noreply.github.com> Date: Wed, 11 Oct 2023 16:35:13 -0400 Subject: [PATCH] fix: simplify the code for downloadInChunks (#2323) * fix: simplify the code for downloadInChunks * cleanup p-limit function --- src/transfer-manager.ts | 50 ++++++++++++++++------------------------- 1 file changed, 19 insertions(+), 31 deletions(-) diff --git a/src/transfer-manager.ts b/src/transfer-manager.ts index a60fe2c39..6e659a4c5 100644 --- a/src/transfer-manager.ts +++ b/src/transfer-manager.ts @@ -651,42 +651,30 @@ export class TransferManager { let chunkEnd = start + chunkSize - 1; chunkEnd = chunkEnd > size ? size : chunkEnd; promises.push( - limit(() => - file - .download({ - start: chunkStart, - end: chunkEnd, - [GCCL_GCS_CMD_KEY]: GCCL_GCS_CMD_FEATURE.DOWNLOAD_SHARDED, - }) - .then(resp => { - return fileToWrite.write(resp[0], 0, resp[0].length, chunkStart); - }) - ) + limit(async () => { + const resp = await file.download({ + start: chunkStart, + end: chunkEnd, + [GCCL_GCS_CMD_KEY]: GCCL_GCS_CMD_FEATURE.DOWNLOAD_SHARDED, + }); + return fileToWrite.write(resp[0], 0, resp[0].length, chunkStart); + }) ); start += chunkSize; } - return new Promise((resolve, reject) => { - let results: DownloadResponse; - Promise.all(promises) - .then(data => { - results = data.map(result => result.buffer) as DownloadResponse; - if (options.validation === 'crc32c') { - return CRC32C.fromFile(filePath); - } - return; - }) - .then(() => { - resolve(results); - }) - .catch(e => { - reject(e); - }) - .finally(() => { - fileToWrite.close(); - }); - }); + let results: DownloadResponse; + try { + const data = await Promise.all(promises); + results = data.map(result => result.buffer) as DownloadResponse; + if (options.validation === 'crc32c') { + await CRC32C.fromFile(filePath); + } + return results; + } finally { + fileToWrite.close(); + } } /**