From 3b2f4ea0d7f7c7fb714651710c6473a3f0f5a4cf Mon Sep 17 00:00:00 2001 From: Matheus Moreira Date: Mon, 27 Apr 2020 21:05:49 +0200 Subject: [PATCH] Fix slice on payloadFromBlob throwing exception --- codemod/output/Evaporate.ts | 7 ++----- codemod/output/PutPart.ts | 8 ++++---- codemod/output/Utils.ts | 15 ++++++++------- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/codemod/output/Evaporate.ts b/codemod/output/Evaporate.ts index 380f3065..33b2a08f 100644 --- a/codemod/output/Evaporate.ts +++ b/codemod/output/Evaporate.ts @@ -14,7 +14,7 @@ import { removeAtIndex, readableFileSize, s3EncodedObjectName, - getBlobSlice + getSupportedBlobSlice } from './Utils' class Evaporate { @@ -478,10 +478,7 @@ class Evaporate { return 'Option readableStreamPartMethod is required when readableStreams is set.' } } else { - if ( - typeof Blob === 'undefined' || - typeof getBlobSlice() === 'undefined' - ) { + if (!getSupportedBlobSlice()) { return 'Evaporate requires support for Blob [webkitSlice || mozSlice || slice]' } } diff --git a/codemod/output/PutPart.ts b/codemod/output/PutPart.ts index 900cfd62..17d57436 100644 --- a/codemod/output/PutPart.ts +++ b/codemod/output/PutPart.ts @@ -10,7 +10,7 @@ import { ERROR, PAUSING } from './Constants' -import { getBlobSlice } from './Utils' +import { getSupportedBlobSlice } from './Utils' //http://docs.aws.amazon.com/AmazonS3/latest/API/mpUploadUploadPart.html class PutPart extends SignedS3AWSRequest { @@ -299,10 +299,10 @@ class PutPart extends SignedS3AWSRequest { // 2nd parameter changed. For example Gecko went from slice(start,length) -> mozSlice(start, end) -> slice(start, end). // As of 12/12/12, it seems that the unified 'slice' is the best bet, hence it being first in the list. See // https://developer.mozilla.org/en-US/docs/DOM/Blob for more info. - const file = this.fileUpload.file + const { file } = this.fileUpload - const slicerFn = getBlobSlice() - const blob = slicerFn(this.start, this.end) + const slicerKey = getSupportedBlobSlice() + const blob = file[slicerKey](this.start, this.end) if (this.con.computeContentMd5) { return new Promise(resolve => { diff --git a/codemod/output/Utils.ts b/codemod/output/Utils.ts index 5255b62e..57978d6b 100644 --- a/codemod/output/Utils.ts +++ b/codemod/output/Utils.ts @@ -234,12 +234,13 @@ function noOpLogger() { } } -function getBlobSlice() { - return ( - Blob.prototype.slice || - (Blob as any).prototype.webkitSlice || - (Blob as any).prototype.mozSlice - ) +function getSupportedBlobSlice() { + if (typeof Blob === 'undefined') { + return null + } + + const blobProperties = Object.keys(Blob.prototype) + return blobProperties.find(key => key.toLowerCase().includes('slice')) } export { @@ -260,5 +261,5 @@ export { removeAtIndex, readableFileSize, noOpLogger, - getBlobSlice + getSupportedBlobSlice }