Skip to content

Commit

Permalink
fix: remove Blob usage
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Jul 15, 2020
1 parent 5323a81 commit 9c1fcb2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
16 changes: 10 additions & 6 deletions packages/fetch-http-handler/src/stream-collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ import { StreamCollector } from "@aws-sdk/types";
import { fromBase64 } from "@aws-sdk/util-base64-browser";

//reference: https://snack.expo.io/r1JCSWRGU
export const streamCollector: StreamCollector = (stream: Blob | ReadableStream): Promise<Uint8Array> => {
if (stream instanceof Blob) {
return collectBlob(stream);
}
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);
}

return collectStream(stream);
};
return collectStream(stream);
};

async function collectBlob(blob: Blob): Promise<Uint8Array> {
const base64 = await readToBase64(blob);
Expand Down
18 changes: 17 additions & 1 deletion packages/util-body-length-browser/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
export function calculateBodyLength(body: any): number | undefined {
if (typeof body === "string") {
return new Blob([body]).size;
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;
}
}

return len;
} else if (typeof body.byteLength === "number") {
// handles Uint8Array, ArrayBuffer, Buffer, and ArrayBufferView
return body.byteLength;
Expand Down

0 comments on commit 9c1fcb2

Please sign in to comment.