From bcb5503c85b8d95c6ae47b553dab01d20851dbf8 Mon Sep 17 00:00:00 2001 From: Blake Embrey Date: Mon, 20 Jul 2020 10:13:11 -0700 Subject: [PATCH] fix: remove `Blob` usage (#1384) * fix: remove `Blob` usage --- packages/fetch-http-handler/src/stream-collector.ts | 4 ++-- packages/util-body-length-browser/src/index.ts | 10 +++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/fetch-http-handler/src/stream-collector.ts b/packages/fetch-http-handler/src/stream-collector.ts index a3501ef2d8dc..c820487a8cc1 100644 --- a/packages/fetch-http-handler/src/stream-collector.ts +++ b/packages/fetch-http-handler/src/stream-collector.ts @@ -3,11 +3,11 @@ import { fromBase64 } from "@aws-sdk/util-base64-browser"; //reference: https://snack.expo.io/r1JCSWRGU export const streamCollector: StreamCollector = (stream: Blob | ReadableStream): Promise => { - if (stream instanceof Blob) { + if (typeof Blob === "function" && stream instanceof Blob) { return collectBlob(stream); } - return collectStream(stream); + return collectStream(stream as ReadableStream); }; async function collectBlob(blob: Blob): Promise { diff --git a/packages/util-body-length-browser/src/index.ts b/packages/util-body-length-browser/src/index.ts index 3ab61ebf1e63..431f6afeae14 100644 --- a/packages/util-body-length-browser/src/index.ts +++ b/packages/util-body-length-browser/src/index.ts @@ -1,6 +1,14 @@ export function calculateBodyLength(body: any): number | undefined { if (typeof body === "string") { - return new Blob([body]).size; + let len = body.length; + + for (let i = len - 1; i >= 0; i--) { + const code = body.charCodeAt(i); + if (code > 0x7f && code <= 0x7ff) len++; + else if (code > 0x7ff && code <= 0xffff) len += 2; + } + + return len; } else if (typeof body.byteLength === "number") { // handles Uint8Array, ArrayBuffer, Buffer, and ArrayBufferView return body.byteLength;