Skip to content

fix: send a one-byte asset with its byte instead of Content-Length 0 - #70

Merged
chenjiahan merged 1 commit into
rstackjs:mainfrom
cezaraugusto:fix/one-byte-content-length
Oct 20, 2026
Merged

chenjiahan merged 1 commit into
rstackjs:mainfrom
cezaraugusto:fix/one-byte-content-length

Conversation

@cezaraugusto

Copy link
Copy Markdown
Contributor

a one-byte file comes back with Content-Length: 0, and the byte is still written after the headers. on a keep-alive socket that breaks the next response

the cause is this line in createReadStreamOrReadFileSync

byteLength = end === 0 ? 0 : end - start + 1;

it was written for the empty file, but calcStartAndEnd clamps a one-byte file to end = 0 as well, so both look the same here. a bytes=0-0 range on any file hits it too

the caller already knows the byte count, the file size or the range length, so the stream is now sized from that. a stat with no size falls back to the old arithmetic

two tests, both red on main: a one-byte file answers Content-Length: 1 with its byte, and bytes=0-0 answers 206 with bytes 0-0/1

fixes #69

@chenjiahan chenjiahan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you!

@chenjiahan
chenjiahan merged commit 76c7381 into rstackjs:main Oct 20, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A one-byte asset is served with Content-Length: 0 and its byte still written, which corrupts a keep-alive connection

2 participants