Skip to content

Commit d220242

Browse files
authored
fix(util-body-length-node): fs.ReadStream with path of Buffer type (#3384)
1 parent d3714e1 commit d220242

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,17 @@ describe(calculateBodyLength.name, () => {
3838
expect(calculateBodyLength(view)).toEqual(1);
3939
});
4040

41-
it("should handle stream created using fs.createReadStream", () => {
41+
describe("should handle stream created using fs.createReadStream", () => {
4242
const fileSize = lstatSync(__filename).size;
43-
const fsReadStream = createReadStream(__filename);
44-
expect(calculateBodyLength(fsReadStream)).toEqual(fileSize);
43+
44+
it("when path is a string", () => {
45+
const fsReadStream = createReadStream(__filename);
46+
expect(calculateBodyLength(fsReadStream)).toEqual(fileSize);
47+
});
48+
49+
it("when path is a Buffer", () => {
50+
const fsReadStream = createReadStream(Buffer.from(__filename));
51+
expect(calculateBodyLength(fsReadStream)).toEqual(fileSize);
52+
});
4553
});
4654
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const calculateBodyLength = (body: any): number | undefined => {
1111
return body.byteLength;
1212
} else if (typeof body.size === "number") {
1313
return body.size;
14-
} else if (typeof body.path === "string") {
14+
} else if (typeof body.path === "string" || Buffer.isBuffer(body.path)) {
1515
// handles fs readable streams
1616
return lstatSync(body.path).size;
1717
}

0 commit comments

Comments
 (0)