UNPKG

1.26 kBJavaScriptView Raw
1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3/**
4 * Calculate the total size of the data to be uploaded. The total size is not required for multipart upload, as it's
5 * only used in progress report.
6 */
7const byteLength = (input) => {
8 if (input === null || input === undefined)
9 return 0;
10 if (typeof input === 'string') {
11 let len = input.length;
12 for (let i = len - 1; i >= 0; i--) {
13 const code = input.charCodeAt(i);
14 if (code > 0x7f && code <= 0x7ff)
15 len++;
16 else if (code > 0x7ff && code <= 0xffff)
17 len += 2;
18 if (code >= 0xdc00 && code <= 0xdfff)
19 i--; // trail surrogate
20 }
21 return len;
22 }
23 else if (typeof input.byteLength === 'number') {
24 // handles Uint8Array, ArrayBuffer, Buffer, and ArrayBufferView
25 return input.byteLength;
26 }
27 else if (typeof input.size === 'number') {
28 // handles browser File object
29 return input.size;
30 }
31 // TODO: support Node.js stream size when Node.js runtime is supported out-of-box.
32 return undefined;
33};
34
35export { byteLength };
36//# sourceMappingURL=byteLength.mjs.map