Skip to content

Commit

Permalink
Make requested code changes
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Jul 16, 2020
1 parent 9c1fcb2 commit 5ab7bb7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 22 deletions.
16 changes: 6 additions & 10 deletions packages/fetch-http-handler/src/stream-collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Uint8Array> => {
if (stream instanceof Blob) {
return collectBlob(stream);
}
export const streamCollector: StreamCollector = (stream: Blob | ReadableStream): Promise<Uint8Array> => {
if (typeof Blob === "function" && stream instanceof Blob) {
return collectBlob(stream);
}

return collectStream(stream);
};
return collectStream(stream as ReadableStream);
};

async function collectBlob(blob: Blob): Promise<Uint8Array> {
const base64 = await readToBase64(blob);
Expand Down
16 changes: 4 additions & 12 deletions packages/util-body-length-browser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 5ab7bb7

Please sign in to comment.