Skip to content

Commit

Permalink
Fix Content-Range header when file size is 0
Browse files Browse the repository at this point in the history
  • Loading branch information
kensnyder committed Nov 23, 2024
1 parent 91845f0 commit deaad84
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 14 deletions.
6 changes: 5 additions & 1 deletion packages/bunshine/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog

## v3.1.0
## v3.1.1 - Nov 23, 2024

- Fix Content-Range header when file size is 0

## v3.1.0 - Nov 23, 2024

- Remove useless exports of response factories
- More unit tests
Expand Down
24 changes: 12 additions & 12 deletions packages/bunshine/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

A Bun HTTP & WebSocket server that is a little ray of sunshine.

<img alt="Bunshine Logo" src="https://github.com/kensnyder/bunshine/raw/main/packages/bunshine/assets/bunshine-logo.png?v=3.1.0" width="200" height="187" />
<img alt="Bunshine Logo" src="https://github.com/kensnyder/bunshine/raw/main/packages/bunshine/assets/bunshine-logo.png?v=3.1.1" width="200" height="187" />

[![NPM Link](https://img.shields.io/npm/v/bunshine?v=3.1.0)](https://npmjs.com/package/bunshine)
[![Language: TypeScript](https://badgen.net/static/language/TS?v=3.1.0)](https://github.com/search?q=repo:kensnyder/bunshine++language:TypeScript&type=code)
[![Code Coverage](https://codecov.io/gh/kensnyder/bunshine/graph/badge.svg?token=4LLWB8NBNT&v=3.1.0)](https://codecov.io/gh/kensnyder/bunshine)
[![Dependencies: 1](https://badgen.net/static/dependencies/1/green?v=3.1.0)](https://www.npmjs.com/package/bunshine?activeTab=dependencies)
![Tree shakeable](https://badgen.net/static/tree%20shakeable/yes/green?v=3.1.0)
[![ISC License](https://badgen.net/github/license/kensnyder/bunshine?v=3.1.0)](https://opensource.org/licenses/ISC)
[![NPM Link](https://img.shields.io/npm/v/bunshine?v=3.1.1)](https://npmjs.com/package/bunshine)
[![Language: TypeScript](https://badgen.net/static/language/TS?v=3.1.1)](https://github.com/search?q=repo:kensnyder/bunshine++language:TypeScript&type=code)
[![Code Coverage](https://codecov.io/gh/kensnyder/bunshine/graph/badge.svg?token=4LLWB8NBNT&v=3.1.1)](https://codecov.io/gh/kensnyder/bunshine)
[![Dependencies: 1](https://badgen.net/static/dependencies/1/green?v=3.1.1)](https://www.npmjs.com/package/bunshine?activeTab=dependencies)
![Tree shakeable](https://badgen.net/static/tree%20shakeable/yes/green?v=3.1.1)
[![ISC License](https://badgen.net/github/license/kensnyder/bunshine?v=3.1.1)](https://opensource.org/licenses/ISC)

## Installation

Expand Down Expand Up @@ -1028,7 +1028,7 @@ example:

Screenshot:

<img alt="devLogger" src="https://github.com/kensnyder/bunshine/raw/main/assets/devLogger-screenshot.png?v=3.1.0" width="524" height="78" />
<img alt="devLogger" src="https://github.com/kensnyder/bunshine/raw/main/assets/devLogger-screenshot.png?v=3.1.1" width="524" height="78" />

`prodLogger` outputs logs in JSON with the following shape:

Expand All @@ -1044,9 +1044,9 @@ Request log:
"method": "GET",
"pathname": "/home",
"runtime": "Bun v1.1.34",
"poweredBy": "Bunshine v3.1.0",
"poweredBy": "Bunshine v3.1.1",
"machine": "server1",
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.1.0.0 Safari/537.36",
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",
"pid": 123
}
```
Expand All @@ -1063,9 +1063,9 @@ Response log:
"method": "GET",
"pathname": "/home",
"runtime": "Bun v1.1.34",
"poweredBy": "Bunshine v3.1.0",
"poweredBy": "Bunshine v3.1.1",
"machine": "server1",
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.1.0.0 Safari/537.36",
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",
"pid": 123,
"took": 5
}
Expand Down
2 changes: 1 addition & 1 deletion packages/bunshine/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bunshine",
"version": "3.1.0",
"version": "3.1.1",
"module": "server/server.ts",
"type": "module",
"main": "index.ts",
Expand Down
23 changes: 23 additions & 0 deletions packages/bunshine/src/parseRangeHeader/parseRangeHeader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,29 @@ describe('parseRangeHeader', () => {
status: 206,
});
});
it('should return 416 on ranged empty file', () => {
const result = parseRangeHeader({
rangeHeader: 'bytes=0-',
totalFileSize: 0,
});
expect(result).toEqual({
slice: null,
contentLength: 0,
status: 416,
});
});
it('should return 206 on open-ended request from 0', () => {
const result = parseRangeHeader({
rangeHeader: 'bytes=0-',
totalFileSize: 1000,
defaultChunkSize: 100,
});
expect(result).toEqual({
slice: { start: 0, end: 99 },
contentLength: 100,
status: 206,
});
});
it('should return 206 on open-ended request (chunk is smaller)', () => {
const result = parseRangeHeader({
rangeHeader: 'bytes=900-',
Expand Down
4 changes: 4 additions & 0 deletions packages/bunshine/src/parseRangeHeader/parseRangeHeader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export default function parseRangeHeader({
// range header missing or empty
return { slice: null, contentLength: totalFileSize, status: 200 };
}
if (totalFileSize === 0) {
// server should respond with "content-range: bytes */0"
return { slice: null, contentLength: 0, status: 416 };
}
// only support single ranges
const match = rangeHeader.match(/^bytes=(\d*)-(\d*)$/i);
if (!match || (match[1] === '' && match[2] === '')) {
Expand Down

0 comments on commit deaad84

Please sign in to comment.