Skip to content

Commit c3d8913

Browse files
committed
fix(util-body-length-browser): handle trail surrogate character
1 parent ae31e05 commit c3d8913

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ const arrayBuffer = new ArrayBuffer(1);
44
const typedArray = new Uint8Array(1);
55

66
describe(calculateBodyLength.name, () => {
7-
it("should handle string inputs", () => {
8-
expect(calculateBodyLength("foo")).toEqual(3);
9-
});
10-
11-
it("should handle string inputs with multi-byte characters", () => {
12-
expect(calculateBodyLength("2。")).toEqual(4);
7+
it.each([
8+
{ desc: "basic", input: "foo", output: 3 },
9+
{ desc: "emoji", input: "foo 🥺", output: 8 },
10+
{ desc: "multi-byte characters", input: "2。", output: 4 },
11+
])("should handle string input: %s", ({ input, output }) => {
12+
expect(calculateBodyLength(input)).toEqual(output);
1313
});
1414

1515
it("should handle inputs with byteLengths", () => {

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)