Skip to content

Commit 227483f

Browse files
committed
chore: move calculateBodyLength to it's own file
1 parent 89a70ae commit 227483f

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { calculateBodyLength } from "./";
1+
import { calculateBodyLength } from "./calculateBodyLength";
22

33
const arrayBuffer = new ArrayBuffer(1);
44
const typedArray = new Uint8Array(1);
55
const view = new DataView(arrayBuffer);
66

7-
describe("caclulateBodyLength", () => {
7+
describe(calculateBodyLength.name, () => {
88
it("should handle null/undefined objects", () => {
99
expect(calculateBodyLength(null)).toEqual(0);
1010
});
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+
};
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)