Skip to content

Commit

Permalink
fix(util-body-length-browser): multi-byte body lengths for browser (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tkashi committed Apr 28, 2020
1 parent d5f7e8f commit 65a3658
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
23 changes: 23 additions & 0 deletions packages/util-body-length-browser/src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { calculateBodyLength } from "./";

const arrayBuffer = new ArrayBuffer(1);
const typedArray = new Uint8Array(1);
const view = new DataView(arrayBuffer);

describe("caclulateBodyLength", () => {
it("should handle string inputs", () => {
expect(calculateBodyLength("foo")).toEqual(3);
});

it("should handle string inputs with multi-byte characters", () => {
expect(calculateBodyLength("2。")).toEqual(4);
});

it("should handle inputs with byteLengths", () => {
expect(calculateBodyLength(arrayBuffer)).toEqual(1);
});

it("should handle TypedArray inputs", () => {
expect(calculateBodyLength(typedArray)).toEqual(1);
});
});
2 changes: 1 addition & 1 deletion packages/util-body-length-browser/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export function calculateBodyLength(body: any): number | undefined {
if (typeof body === "string") {
return body.length;
return new Blob([body]).size;
} else if (typeof body.byteLength === "number") {
// handles Uint8Array, ArrayBuffer, Buffer, and ArrayBufferView
return body.byteLength;
Expand Down
3 changes: 2 additions & 1 deletion packages/util-body-length-browser/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"es2015.promise",
"es2015.collection",
"es2015.iterable",
"es2015.symbol.wellknown"
"es2015.symbol.wellknown",
"dom"
],
"rootDir": "./src",
"outDir": "./build",
Expand Down

0 comments on commit 65a3658

Please sign in to comment.