From 21343ca9896e2d348047cb83fbd6b5395633de47 Mon Sep 17 00:00:00 2001 From: Jonathan Johnson Date: Thu, 4 Apr 2024 21:52:52 -0700 Subject: [PATCH] Handle empty s3 response After the update to AWS SDK v3 when searching for a revision the Contents on the response is not present if the revision hasn't been created yet. This seemed like the smallest change to fix the problem, and optional chaining is supported in Node 14, but I had to bump up the ecmaVersion in the eslint config to allow it. --- .eslintrc.js | 2 +- lib/s3.js | 2 +- tests/unit/lib/s3-test.js | 9 +++++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index c9196f6..74aabdc 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,7 +1,7 @@ module.exports = { root: true, parserOptions: { - ecmaVersion: 6, + ecmaVersion: 'latest', sourceType: 'module' }, extends: 'eslint:recommended', diff --git a/lib/s3.js b/lib/s3.js index f090bde..a503336 100644 --- a/lib/s3.js +++ b/lib/s3.js @@ -166,7 +166,7 @@ module.exports = CoreObject.extend({ var revisionPrefix = joinUriSegments(prefix, options.filePattern + ":" + options.revisionKey); return listObjects({ Bucket: bucket, Prefix: revisionPrefix }) - .then((response) => response.Contents.find((element) => element.Key === revisionPrefix)); + .then((response) => response.Contents?.find((element) => element.Key === revisionPrefix)); }, fetchRevisions: function(options) { diff --git a/tests/unit/lib/s3-test.js b/tests/unit/lib/s3-test.js index 17c2ef3..9d499c2 100644 --- a/tests/unit/lib/s3-test.js +++ b/tests/unit/lib/s3-test.js @@ -242,6 +242,15 @@ describe('s3', function() { }); }); + it('succeeds when revision key search returns no values', function() { + s3Client.listObjects = function(params, cb) { + cb(undefined, {}); + }; + var promise = subject.upload(options); + + return assert.isFulfilled(promise); + }); + describe("when revisionKey was already uploaded", function() { beforeEach(function() { options.revisionKey = "123";