Skip to content

chore(util-body-length-node): move calculateBodyLength to it's own file #3381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions packages/util-body-length-node/src/calculateBodyLength.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { createReadStream, lstatSync } from "fs";

import { calculateBodyLength } from "./calculateBodyLength";

describe(calculateBodyLength.name, () => {
const arrayBuffer = new ArrayBuffer(1);
const typedArray = new Uint8Array(1);
const view = new DataView(arrayBuffer);

afterEach(() => {
jest.clearAllMocks();
});

it.each([
[0, null],
[0, undefined],
])("should return %s for %s", (output, input) => {
expect(calculateBodyLength(input)).toEqual(output);
});

it("should handle string inputs", () => {
expect(calculateBodyLength("foo")).toEqual(3);
});

it("should handle string inputs with multi-byte characters", () => {
expect(calculateBodyLength("2。")).toEqual(4);
});

it("should handle inputs with byteLengths", () => {
expect(calculateBodyLength(arrayBuffer)).toEqual(1);
});

it("should handle TypedArray inputs", () => {
expect(calculateBodyLength(typedArray)).toEqual(1);
});

it("should handle DataView inputs", () => {
expect(calculateBodyLength(view)).toEqual(1);
});

it("should handle stream created using fs.createReadStream", () => {
const fileSize = lstatSync(__filename).size;
const fsReadStream = createReadStream(__filename);
expect(calculateBodyLength(fsReadStream)).toEqual(fileSize);
});
});
18 changes: 18 additions & 0 deletions packages/util-body-length-node/src/calculateBodyLength.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { lstatSync } from "fs";

export const calculateBodyLength = (body: any): number | undefined => {
if (!body) {
return 0;
}
if (typeof body === "string") {
return Buffer.from(body).length;
} else if (typeof body.byteLength === "number") {
// handles Uint8Array, ArrayBuffer, Buffer, and ArrayBufferView
return body.byteLength;
} else if (typeof body.size === "number") {
return body.size;
} else if (typeof body.path === "string") {
// handles fs readable streams
return lstatSync(body.path).size;
}
};
31 changes: 0 additions & 31 deletions packages/util-body-length-node/src/index.spec.ts

This file was deleted.

19 changes: 1 addition & 18 deletions packages/util-body-length-node/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1 @@
import { lstatSync } from "fs";

export function calculateBodyLength(body: any): number | undefined {
if (!body) {
return 0;
}
if (typeof body === "string") {
return Buffer.from(body).length;
} else if (typeof body.byteLength === "number") {
// handles Uint8Array, ArrayBuffer, Buffer, and ArrayBufferView
return body.byteLength;
} else if (typeof body.size === "number") {
return body.size;
} else if (typeof body.path === "string") {
// handles fs readable streams
return lstatSync(body.path).size;
}
}
export * from "./calculateBodyLength";