Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ContentLength incorrectly set for empty files #1785

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/utils/compatibleAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,9 @@ async function send(req, res, filename, start, end, goNext, options) {
start,
end,
});
byteLength = end - start + 1;

// Handle files with zero bytes
byteLength = end === 0 ? 0 : end - start + 1;
} else {
bufferOrStream = /** @type {import("fs").readFileSync} */ (
options.outputFileSystem.readFileSync
Expand Down
17 changes: 16 additions & 1 deletion test/middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ describe.each([
"unknown",
);

instance.context.outputFileSystem.writeFileSync(
path.resolve(outputPath, "empty-file.txt"),
"",
);

req = request(app);
});

Expand Down Expand Up @@ -469,7 +474,7 @@ describe.each([
);
});

it('should return "200" code code for the "GET" request and "Content-Length" to the file with unicode', async () => {
it('should return "200" code for the "GET" request and "Content-Length" to the file with unicode', async () => {
const response = await req.get("/byte-length.html");

expect(response.statusCode).toEqual(200);
Expand All @@ -482,6 +487,16 @@ describe.each([
);
});

it('should return "200" code for the "GET" request and "Content-Length" of "0" when file is empty', async () => {
const response = await req.get("/empty-file.txt");

expect(response.statusCode).toEqual(200);
expect(response.headers["content-length"]).toEqual("0");
expect(response.headers["content-type"]).toEqual(
"text/plain; charset=utf-8",
);
});

it('should return the "200" code for the "GET" request to the "image image.svg" file', async () => {
const fileData = instance.context.outputFileSystem.readFileSync(
path.resolve(outputPath, "image image.svg"),
Expand Down