Skip to content

Commit 3cfd63e

Browse files
committed
fix(util-body-length-node): support fd.createReadStream
1 parent c676502 commit 3cfd63e

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createReadStream, lstatSync } from "fs";
1+
import { createReadStream, lstatSync, promises } from "fs";
22

33
import { calculateBodyLength } from "./calculateBodyLength";
44

@@ -38,9 +38,20 @@ describe(calculateBodyLength.name, () => {
3838
expect(calculateBodyLength(view)).toEqual(1);
3939
});
4040

41-
it("should handle stream created using fs.createReadStream", () => {
41+
describe("fs.ReadStream", () => {
4242
const fileSize = lstatSync(__filename).size;
43-
const fsReadStream = createReadStream(__filename);
44-
expect(calculateBodyLength(fsReadStream)).toEqual(fileSize);
43+
44+
it("should handle stream created using fs.createReadStream", () => {
45+
const fsReadStream = createReadStream(__filename);
46+
expect(calculateBodyLength(fsReadStream)).toEqual(fileSize);
47+
});
48+
49+
it("should handle stream created using fd.createReadStream", async () => {
50+
const fd = await promises.open(__filename, "r");
51+
if ((fd as any).createReadStream) {
52+
const fdReadStream = (fd as any).createReadStream();
53+
expect(calculateBodyLength(fdReadStream)).toEqual(fileSize);
54+
}
55+
});
4556
});
4657
});

packages/util-body-length-node/src/calculateBodyLength.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { lstatSync } from "fs";
1+
import { fstatSync, lstatSync } from "fs";
22

33
export const calculateBodyLength = (body: any): number | undefined => {
44
if (!body) {
@@ -14,5 +14,8 @@ export const calculateBodyLength = (body: any): number | undefined => {
1414
} else if (typeof body.path === "string") {
1515
// handles fs readable streams
1616
return lstatSync(body.path).size;
17+
} else if (typeof body.fd === "number") {
18+
// handles fd readable streams
19+
return fstatSync(body.fd).size;
1720
}
1821
};

0 commit comments

Comments
 (0)