Skip to content

Commit d3714e1

Browse files
authored
chore(util-body-length-node): move calculateBodyLength to it's own file (#3381)
1 parent 561c507 commit d3714e1

File tree

4 files changed

+65
-49
lines changed

4 files changed

+65
-49
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { createReadStream, lstatSync } from "fs";
2+
3+
import { calculateBodyLength } from "./calculateBodyLength";
4+
5+
describe(calculateBodyLength.name, () => {
6+
const arrayBuffer = new ArrayBuffer(1);
7+
const typedArray = new Uint8Array(1);
8+
const view = new DataView(arrayBuffer);
9+
10+
afterEach(() => {
11+
jest.clearAllMocks();
12+
});
13+
14+
it.each([
15+
[0, null],
16+
[0, undefined],
17+
])("should return %s for %s", (output, input) => {
18+
expect(calculateBodyLength(input)).toEqual(output);
19+
});
20+
21+
it("should handle string inputs", () => {
22+
expect(calculateBodyLength("foo")).toEqual(3);
23+
});
24+
25+
it("should handle string inputs with multi-byte characters", () => {
26+
expect(calculateBodyLength("2。")).toEqual(4);
27+
});
28+
29+
it("should handle inputs with byteLengths", () => {
30+
expect(calculateBodyLength(arrayBuffer)).toEqual(1);
31+
});
32+
33+
it("should handle TypedArray inputs", () => {
34+
expect(calculateBodyLength(typedArray)).toEqual(1);
35+
});
36+
37+
it("should handle DataView inputs", () => {
38+
expect(calculateBodyLength(view)).toEqual(1);
39+
});
40+
41+
it("should handle stream created using fs.createReadStream", () => {
42+
const fileSize = lstatSync(__filename).size;
43+
const fsReadStream = createReadStream(__filename);
44+
expect(calculateBodyLength(fsReadStream)).toEqual(fileSize);
45+
});
46+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { lstatSync } from "fs";
2+
3+
export const calculateBodyLength = (body: any): number | undefined => {
4+
if (!body) {
5+
return 0;
6+
}
7+
if (typeof body === "string") {
8+
return Buffer.from(body).length;
9+
} else if (typeof body.byteLength === "number") {
10+
// handles Uint8Array, ArrayBuffer, Buffer, and ArrayBufferView
11+
return body.byteLength;
12+
} else if (typeof body.size === "number") {
13+
return body.size;
14+
} else if (typeof body.path === "string") {
15+
// handles fs readable streams
16+
return lstatSync(body.path).size;
17+
}
18+
};

packages/util-body-length-node/src/index.spec.ts

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1 @@
1-
import { lstatSync } from "fs";
2-
3-
export function calculateBodyLength(body: any): number | undefined {
4-
if (!body) {
5-
return 0;
6-
}
7-
if (typeof body === "string") {
8-
return Buffer.from(body).length;
9-
} else if (typeof body.byteLength === "number") {
10-
// handles Uint8Array, ArrayBuffer, Buffer, and ArrayBufferView
11-
return body.byteLength;
12-
} else if (typeof body.size === "number") {
13-
return body.size;
14-
} else if (typeof body.path === "string") {
15-
// handles fs readable streams
16-
return lstatSync(body.path).size;
17-
}
18-
}
1+
export * from "./calculateBodyLength";

0 commit comments

Comments
 (0)