Skip to content

Commit

Permalink
chore(util-body-length-node): move calculateBodyLength to it's own fi…
Browse files Browse the repository at this point in the history
…le (#3381)
  • Loading branch information
trivikr authored Mar 8, 2022
1 parent 561c507 commit d3714e1
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 49 deletions.
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";

0 comments on commit d3714e1

Please sign in to comment.