Skip to content

Commit 65a3658

Browse files
authored
fix(util-body-length-browser): multi-byte body lengths for browser (#1101)
1 parent d5f7e8f commit 65a3658

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

Diff for: packages/util-body-length-browser/src/index.spec.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { calculateBodyLength } from "./";
2+
3+
const arrayBuffer = new ArrayBuffer(1);
4+
const typedArray = new Uint8Array(1);
5+
const view = new DataView(arrayBuffer);
6+
7+
describe("caclulateBodyLength", () => {
8+
it("should handle string inputs", () => {
9+
expect(calculateBodyLength("foo")).toEqual(3);
10+
});
11+
12+
it("should handle string inputs with multi-byte characters", () => {
13+
expect(calculateBodyLength("2。")).toEqual(4);
14+
});
15+
16+
it("should handle inputs with byteLengths", () => {
17+
expect(calculateBodyLength(arrayBuffer)).toEqual(1);
18+
});
19+
20+
it("should handle TypedArray inputs", () => {
21+
expect(calculateBodyLength(typedArray)).toEqual(1);
22+
});
23+
});

Diff for: packages/util-body-length-browser/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export function calculateBodyLength(body: any): number | undefined {
22
if (typeof body === "string") {
3-
return body.length;
3+
return new Blob([body]).size;
44
} else if (typeof body.byteLength === "number") {
55
// handles Uint8Array, ArrayBuffer, Buffer, and ArrayBufferView
66
return body.byteLength;

Diff for: packages/util-body-length-browser/tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"es2015.promise",
1212
"es2015.collection",
1313
"es2015.iterable",
14-
"es2015.symbol.wellknown"
14+
"es2015.symbol.wellknown",
15+
"dom"
1516
],
1617
"rootDir": "./src",
1718
"outDir": "./build",

0 commit comments

Comments
 (0)