Skip to content

Commit 62657b1

Browse files
authored
fix(util-body-length-browser): handle trail surrogate character (#3866)
1 parent ae31e05 commit 62657b1

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed
Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
import { calculateBodyLength } from "./calculateBodyLength";
22

3-
const arrayBuffer = new ArrayBuffer(1);
4-
const typedArray = new Uint8Array(1);
5-
63
describe(calculateBodyLength.name, () => {
7-
it("should handle string inputs", () => {
8-
expect(calculateBodyLength("foo")).toEqual(3);
4+
describe("should handle string input", () => {
5+
it.each([
6+
{ desc: "basic", input: "foo", output: 3 },
7+
{ desc: "emoji", input: "foo 🥺", output: 8 },
8+
{ desc: "multi-byte characters", input: "2。", output: 4 },
9+
])("%s", ({ input, output }) => {
10+
expect(calculateBodyLength(input)).toEqual(output);
11+
});
912
});
1013

11-
it("should handle string inputs with multi-byte characters", () => {
12-
expect(calculateBodyLength("2。")).toEqual(4);
13-
});
14+
describe("should handle input with byteLength", () => {
15+
const sizes = [1, 256, 65536];
1416

15-
it("should handle inputs with byteLengths", () => {
16-
expect(calculateBodyLength(arrayBuffer)).toEqual(1);
17-
});
17+
describe("ArrayBuffer", () => {
18+
it.each(sizes)("size: %s", (size) => {
19+
expect(calculateBodyLength(new ArrayBuffer(size))).toEqual(size);
20+
});
21+
});
1822

19-
it("should handle TypedArray inputs", () => {
20-
expect(calculateBodyLength(typedArray)).toEqual(1);
23+
describe("TypedArray", () => {
24+
it.each(sizes)("size: %s", (size) => {
25+
expect(calculateBodyLength(new Uint8Array(size))).toEqual(size);
26+
});
27+
});
2128
});
2229

2330
it("should handle File object", () => {
@@ -34,9 +41,11 @@ describe(calculateBodyLength.name, () => {
3441
expect(calculateBodyLength(mockFileObject)).toEqual(mockFileObject.size);
3542
});
3643

37-
it.each([true, 1, {}, []])("throws error if Body Length computation fails for: %s", (body) => {
38-
expect(() => {
39-
expect(calculateBodyLength(body));
40-
}).toThrowError(`Body Length computation failed for ${body}`);
44+
describe("throws error if Body Length computation fails", () => {
45+
it.each([true, 1, {}, []])("%s", (body) => {
46+
expect(() => {
47+
expect(calculateBodyLength(body));
48+
}).toThrowError(`Body Length computation failed for ${body}`);
49+
});
4150
});
4251
});

packages/util-body-length-browser/src/calculateBodyLength.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export const calculateBodyLength = (body: any): number | undefined => {
66
const code = body.charCodeAt(i);
77
if (code > 0x7f && code <= 0x7ff) len++;
88
else if (code > 0x7ff && code <= 0xffff) len += 2;
9+
if (code >= 0xdc00 && code <= 0xdfff) i--; //trail surrogate
910
}
1011

1112
return len;

0 commit comments

Comments
 (0)