From d91ed0f8de3b373a8720807e1142cd7796259766 Mon Sep 17 00:00:00 2001 From: Kelvin Luck Date: Fri, 8 Jul 2022 23:35:06 +0100 Subject: [PATCH] fix: Optimise upload when `allowOverwrite` is true There is no need to try and figure out if a revision has already been deployed if we are going to overwrite it anyway. Let's save ourselves the network request! --- lib/s3.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/s3.js b/lib/s3.js index c821371..66313f9 100644 --- a/lib/s3.js +++ b/lib/s3.js @@ -62,6 +62,7 @@ module.exports = CoreObject.extend({ var isGzipped = gzippedFilePaths.indexOf(options.filePattern) !== -1; var isBrotliCompressed = brotliCompressedFilePaths.indexOf(options.filePattern) !== -1; var serverSideEncryption = options.serverSideEncryption; + var checkForOverwrite = RSVP.resolve(); var params = { Bucket: bucket, @@ -83,13 +84,17 @@ module.exports = CoreObject.extend({ params.ContentEncoding = 'br'; } - return this.findRevision(options) - .then(function(found) { - if (found !== undefined && !allowOverwrite) { - return RSVP.reject("REVISION ALREADY UPLOADED! (set `allowOverwrite: true` if you want to support overwriting revisions)"); - } - return RSVP.resolve(); - }) + if (!allowOverwrite) { + checkForOverwrite = this.findRevision(options) + .then(function(found) { + if (found !== undefined) { + return RSVP.reject("REVISION ALREADY UPLOADED! (set `allowOverwrite: true` if you want to support overwriting revisions)"); + } + return RSVP.resolve(); + }) + } + + return checkForOverwrite .then(readFile.bind(this, options.filePath)) .then(function(fileContents) { params.Body = fileContents;