From 5ab7bb7992a1a77bb2dd832bd5d75f27c3c9585c Mon Sep 17 00:00:00 2001 From: Blake Embrey Date: Wed, 15 Jul 2020 21:02:16 -0700 Subject: [PATCH] Make requested code changes --- .../fetch-http-handler/src/stream-collector.ts | 16 ++++++---------- packages/util-body-length-browser/src/index.ts | 16 ++++------------ 2 files changed, 10 insertions(+), 22 deletions(-) diff --git a/packages/fetch-http-handler/src/stream-collector.ts b/packages/fetch-http-handler/src/stream-collector.ts index 31344b8ba5167..c820487a8cc18 100644 --- a/packages/fetch-http-handler/src/stream-collector.ts +++ b/packages/fetch-http-handler/src/stream-collector.ts @@ -2,17 +2,13 @@ import { StreamCollector } from "@aws-sdk/types"; import { fromBase64 } from "@aws-sdk/util-base64-browser"; //reference: https://snack.expo.io/r1JCSWRGU -export const streamCollector: StreamCollector = - // `Blob` does not exist on Cloudflare Workers. - typeof Blob === "undefined" - ? collectStream - : (stream: Blob | ReadableStream): Promise => { - if (stream instanceof Blob) { - return collectBlob(stream); - } +export const streamCollector: StreamCollector = (stream: Blob | ReadableStream): Promise => { + if (typeof Blob === "function" && stream instanceof Blob) { + return collectBlob(stream); + } - return collectStream(stream); - }; + return collectStream(stream as ReadableStream); +}; async function collectBlob(blob: Blob): Promise { const base64 = await readToBase64(blob); diff --git a/packages/util-body-length-browser/src/index.ts b/packages/util-body-length-browser/src/index.ts index 6af6cd3e1512b..d0091239e67e2 100644 --- a/packages/util-body-length-browser/src/index.ts +++ b/packages/util-body-length-browser/src/index.ts @@ -2,18 +2,10 @@ export function calculateBodyLength(body: any): number | undefined { if (typeof body === "string") { let len = body.length; - // Source: https://github.com/DylanPiercey/byte-length - for (let i = len; i--; ) { - const code = body.charCodeAt(i); - if (0xdc00 <= code && code <= 0xdfff) { - i--; - } - - if (0x7f < code && code <= 0x7ff) { - len++; - } else if (0x7ff < code && code <= 0xffff) { - len += 2; - } + for (var i = len - 1; i >= 0; i--) { + var code = body.charCodeAt(i); + if (code > 0x7f && code <= 0x7ff) len++; + else if (code > 0x7ff && code <= 0xffff) len += 2; } return len;