From 6a8e4492decf69f379dd4d9caa14f2f49f573956 Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Mon, 28 Feb 2022 19:23:47 +0000 Subject: [PATCH 1/5] chore: move calculateBodyLength to it's own file --- .../src/calculateBodyLength.spec.ts | 35 ++++--------------- 1 file changed, 6 insertions(+), 29 deletions(-) diff --git a/packages/util-body-length-node/src/calculateBodyLength.spec.ts b/packages/util-body-length-node/src/calculateBodyLength.spec.ts index ff4de3bd86ae..d17ede74ae27 100644 --- a/packages/util-body-length-node/src/calculateBodyLength.spec.ts +++ b/packages/util-body-length-node/src/calculateBodyLength.spec.ts @@ -1,21 +1,12 @@ -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); +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); +describe(calculateBodyLength.name, () => { + it("should handle null/undefined objects", () => { + expect(calculateBodyLength(null)).toEqual(0); }); it("should handle string inputs", () => { @@ -37,18 +28,4 @@ describe(calculateBodyLength.name, () => { it("should handle DataView inputs", () => { expect(calculateBodyLength(view)).toEqual(1); }); - - describe("should handle stream created using fs.createReadStream", () => { - const fileSize = lstatSync(__filename).size; - - it("when path is a string", () => { - const fsReadStream = createReadStream(__filename); - expect(calculateBodyLength(fsReadStream)).toEqual(fileSize); - }); - - it("when path is a Buffer", () => { - const fsReadStream = createReadStream(Buffer.from(__filename)); - expect(calculateBodyLength(fsReadStream)).toEqual(fileSize); - }); - }); }); From eb0a33b1359a97b3f5703edb1b3fe71207756437 Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Mon, 28 Feb 2022 19:38:04 +0000 Subject: [PATCH 2/5] test: add tests for undefined and file stream --- .../src/calculateBodyLength.spec.ts | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/packages/util-body-length-node/src/calculateBodyLength.spec.ts b/packages/util-body-length-node/src/calculateBodyLength.spec.ts index d17ede74ae27..7b19d2a21afd 100644 --- a/packages/util-body-length-node/src/calculateBodyLength.spec.ts +++ b/packages/util-body-length-node/src/calculateBodyLength.spec.ts @@ -1,12 +1,23 @@ +import { lstatSync } from "fs"; + import { calculateBodyLength } from "./calculateBodyLength"; -const arrayBuffer = new ArrayBuffer(1); -const typedArray = new Uint8Array(1); -const view = new DataView(arrayBuffer); +jest.mock("fs"); describe(calculateBodyLength.name, () => { - it("should handle null/undefined objects", () => { - expect(calculateBodyLength(null)).toEqual(0); + 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", () => { @@ -28,4 +39,13 @@ describe(calculateBodyLength.name, () => { it("should handle DataView inputs", () => { expect(calculateBodyLength(view)).toEqual(1); }); + + it("should handle stream created using fs.createReadStream", () => { + const mockSize = { size: 10 }; + (lstatSync as jest.Mock).mockReturnValue(mockSize); + + // Populate path as string to mock body created from fs.createReadStream + const mockBody = { path: "mockPath" }; + expect(calculateBodyLength(mockBody)).toEqual(mockSize.size); + }); }); From 22fb896327fb284a703fec89b367b32748d45909 Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Mon, 28 Feb 2022 19:56:09 +0000 Subject: [PATCH 3/5] test: call createReadStream instead of mocking --- .../src/calculateBodyLength.spec.ts | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/packages/util-body-length-node/src/calculateBodyLength.spec.ts b/packages/util-body-length-node/src/calculateBodyLength.spec.ts index 7b19d2a21afd..a25e43cab1d7 100644 --- a/packages/util-body-length-node/src/calculateBodyLength.spec.ts +++ b/packages/util-body-length-node/src/calculateBodyLength.spec.ts @@ -1,9 +1,7 @@ -import { lstatSync } from "fs"; +import { createReadStream, lstatSync } from "fs"; import { calculateBodyLength } from "./calculateBodyLength"; -jest.mock("fs"); - describe(calculateBodyLength.name, () => { const arrayBuffer = new ArrayBuffer(1); const typedArray = new Uint8Array(1); @@ -41,11 +39,8 @@ describe(calculateBodyLength.name, () => { }); it("should handle stream created using fs.createReadStream", () => { - const mockSize = { size: 10 }; - (lstatSync as jest.Mock).mockReturnValue(mockSize); - - // Populate path as string to mock body created from fs.createReadStream - const mockBody = { path: "mockPath" }; - expect(calculateBodyLength(mockBody)).toEqual(mockSize.size); + const fileSize = lstatSync(__filename).size; + const fsReadStream = createReadStream(__filename); + expect(calculateBodyLength(fsReadStream)).toEqual(fileSize); }); }); From 6a7483f08139872763b1f50e6d123d7dbfe8df22 Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Mon, 28 Feb 2022 21:11:51 +0000 Subject: [PATCH 4/5] fix(util-body-length-node): support fd.createReadStream --- .../src/calculateBodyLength.spec.ts | 19 +++++++++++++++---- .../src/calculateBodyLength.ts | 5 ++++- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/packages/util-body-length-node/src/calculateBodyLength.spec.ts b/packages/util-body-length-node/src/calculateBodyLength.spec.ts index a25e43cab1d7..dd91607ee577 100644 --- a/packages/util-body-length-node/src/calculateBodyLength.spec.ts +++ b/packages/util-body-length-node/src/calculateBodyLength.spec.ts @@ -1,4 +1,4 @@ -import { createReadStream, lstatSync } from "fs"; +import { createReadStream, lstatSync, promises } from "fs"; import { calculateBodyLength } from "./calculateBodyLength"; @@ -38,9 +38,20 @@ describe(calculateBodyLength.name, () => { expect(calculateBodyLength(view)).toEqual(1); }); - it("should handle stream created using fs.createReadStream", () => { + describe("fs.ReadStream", () => { const fileSize = lstatSync(__filename).size; - const fsReadStream = createReadStream(__filename); - expect(calculateBodyLength(fsReadStream)).toEqual(fileSize); + + it("should handle stream created using fs.createReadStream", () => { + const fsReadStream = createReadStream(__filename); + expect(calculateBodyLength(fsReadStream)).toEqual(fileSize); + }); + + it("should handle stream created using fd.createReadStream", async () => { + const fd = await promises.open(__filename, "r"); + if ((fd as any).createReadStream) { + const fdReadStream = (fd as any).createReadStream(); + expect(calculateBodyLength(fdReadStream)).toEqual(fileSize); + } + }); }); }); diff --git a/packages/util-body-length-node/src/calculateBodyLength.ts b/packages/util-body-length-node/src/calculateBodyLength.ts index 4669869145a8..8d3293f821ca 100644 --- a/packages/util-body-length-node/src/calculateBodyLength.ts +++ b/packages/util-body-length-node/src/calculateBodyLength.ts @@ -1,4 +1,4 @@ -import { lstatSync } from "fs"; +import { fstatSync, lstatSync } from "fs"; export const calculateBodyLength = (body: any): number | undefined => { if (!body) { @@ -14,5 +14,8 @@ export const calculateBodyLength = (body: any): number | undefined => { } else if (typeof body.path === "string" || Buffer.isBuffer(body.path)) { // handles fs readable streams return lstatSync(body.path).size; + } else if (typeof body.fd === "number") { + // handles fd readable streams + return fstatSync(body.fd).size; } }; From 23377a1677c4e9889f09f669e635516df9353f9a Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Tue, 8 Mar 2022 20:56:01 +0000 Subject: [PATCH 5/5] test: re-add test for path being Buffer --- .../src/calculateBodyLength.spec.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/util-body-length-node/src/calculateBodyLength.spec.ts b/packages/util-body-length-node/src/calculateBodyLength.spec.ts index dd91607ee577..cb5ae86d13e0 100644 --- a/packages/util-body-length-node/src/calculateBodyLength.spec.ts +++ b/packages/util-body-length-node/src/calculateBodyLength.spec.ts @@ -41,9 +41,16 @@ describe(calculateBodyLength.name, () => { describe("fs.ReadStream", () => { const fileSize = lstatSync(__filename).size; - it("should handle stream created using fs.createReadStream", () => { - const fsReadStream = createReadStream(__filename); - expect(calculateBodyLength(fsReadStream)).toEqual(fileSize); + describe("should handle stream created using fs.createReadStream", () => { + it("when path is a string", () => { + const fsReadStream = createReadStream(__filename); + expect(calculateBodyLength(fsReadStream)).toEqual(fileSize); + }); + + it("when path is a Buffer", () => { + const fsReadStream = createReadStream(Buffer.from(__filename)); + expect(calculateBodyLength(fsReadStream)).toEqual(fileSize); + }); }); it("should handle stream created using fd.createReadStream", async () => {