Skip to content

Commit

Permalink
fix(util-body-length-node): fs.ReadStream with path of Buffer type (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Mar 8, 2022
1 parent d3714e1 commit d220242
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
14 changes: 11 additions & 3 deletions packages/util-body-length-node/src/calculateBodyLength.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,17 @@ describe(calculateBodyLength.name, () => {
expect(calculateBodyLength(view)).toEqual(1);
});

it("should handle stream created using fs.createReadStream", () => {
describe("should handle stream created using fs.createReadStream", () => {
const fileSize = lstatSync(__filename).size;
const fsReadStream = createReadStream(__filename);
expect(calculateBodyLength(fsReadStream)).toEqual(fileSize);

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);
});
});
});
2 changes: 1 addition & 1 deletion packages/util-body-length-node/src/calculateBodyLength.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const calculateBodyLength = (body: any): number | undefined => {
return body.byteLength;
} else if (typeof body.size === "number") {
return body.size;
} else if (typeof body.path === "string") {
} else if (typeof body.path === "string" || Buffer.isBuffer(body.path)) {
// handles fs readable streams
return lstatSync(body.path).size;
}
Expand Down

0 comments on commit d220242

Please sign in to comment.