UNPKG

@aws-amplify/storage

Version:

Storage category of aws-amplify

54 lines (52 loc) 2 kB
'use strict'; // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 Object.defineProperty(exports, "__esModule", { value: true }); exports.getDataChunker = void 0; const validation_1 = require("../../../../../../errors/types/validation"); const StorageError_1 = require("../../../../../../errors/StorageError"); const calculatePartSize_1 = require("./calculatePartSize"); const getDataChunker = (data, totalSize) => { const partSize = (0, calculatePartSize_1.calculatePartSize)(totalSize); if (data instanceof Blob) { return helper(data, 0, data.size, partSize); } else if (ArrayBuffer.isView(data)) { return helper(data.buffer, data.byteOffset, data.byteLength, partSize); } else if (data instanceof ArrayBuffer) { return helper(data, 0, data.byteLength, partSize); } else if (typeof data === 'string') { const blob = new Blob([data]); return (0, exports.getDataChunker)(blob, blob.size); } else { throw new StorageError_1.StorageError({ name: validation_1.StorageValidationErrorCode.InvalidUploadSource, ...validation_1.validationErrorMap[validation_1.StorageValidationErrorCode.InvalidUploadSource], }); } }; exports.getDataChunker = getDataChunker; const helper = function* (buffer, byteOffset, byteLength, partSize) { let partNumber = 1; let startByte = byteOffset; let endByte = byteOffset + Math.min(partSize, byteLength); while (endByte < byteLength + byteOffset) { yield { partNumber, data: buffer.slice(startByte, endByte), size: partSize, }; partNumber += 1; startByte = endByte; endByte = startByte + partSize; } yield { partNumber, data: buffer.slice(startByte, byteLength + byteOffset), size: byteLength + byteOffset - startByte, }; }; //# sourceMappingURL=getDataChunker.js.map