Skip to content

Commit

Permalink
Add Accept-Ranges support
Browse files Browse the repository at this point in the history
  • Loading branch information
mjackson committed Dec 20, 2024
1 parent 891c059 commit 3d49e31
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 13 deletions.
11 changes: 6 additions & 5 deletions packages/headers/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,12 @@ header.getPreferred(['gzip', 'deflate']); // 'gzip'
```

- Added `SuperHeaders.prototype` (getters and setters) for:
- `headers.accept`
- `headers.acceptEncoding`
- `headers.connection`
- `headers.host`
- `headers.referer`
- `accept`
- `acceptEncoding`
- `acceptRanges`
- `connection`
- `host`
- `referer`

## v0.8.0 (2024-11-14)

Expand Down
12 changes: 12 additions & 0 deletions packages/headers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ headers.acceptLanguage.accepts('ja'); // false
headers.acceptLanguage.getPreferred(['en-US', 'en-GB']); // 'en-US'
headers.acceptLanguage.getPreferred(['en', 'fr']); // 'en'

// Accept-Ranges
headers.acceptRanges = 'bytes';

// Connection
headers.connection = 'close';

// Content-Type
headers.contentType = 'application/json; charset=utf-8';

Expand Down Expand Up @@ -92,6 +98,12 @@ console.log(headers.cookie.get('user_id')); // '12345'
headers.cookie.set('theme', 'dark');
console.log(headers.get('Cookie')); // 'session_id=abc123; user_id=12345; theme=dark'

// Host
headers.host = 'example.com';

// Referer
headers.referer = 'https://example.com/';

// Set-Cookie
headers.setCookie = ['session_id=abc123; Path=/; HttpOnly'];

Expand Down
17 changes: 17 additions & 0 deletions packages/headers/src/lib/super-headers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ describe('SuperHeaders', () => {
assert.equal(headers.get('Accept-Language'), 'en-us,en;q=0.9');
});

it('handles the acceptRanges property', () => {
let headers = new SuperHeaders({ acceptRanges: 'bytes' });
assert.equal(headers.get('Accept-Ranges'), 'bytes');
});

it('handles the age property', () => {
let headers = new SuperHeaders({ age: 42 });
assert.equal(headers.get('Age'), '42');
Expand Down Expand Up @@ -327,6 +332,18 @@ describe('SuperHeaders', () => {
assert.equal(headers.acceptLanguage.toString(), '');
});

it('supports the acceptRanges property', () => {
let headers = new SuperHeaders();

assert.equal(headers.acceptRanges, null);

headers.acceptRanges = 'bytes';
assert.equal(headers.acceptRanges, 'bytes');

headers.acceptRanges = null;
assert.equal(headers.acceptRanges, null);
});

it('supports the age property', () => {
let headers = new SuperHeaders();

Expand Down
43 changes: 35 additions & 8 deletions packages/headers/src/lib/super-headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ interface SuperHeadersPropertyInit {
* The [`Accept-Language`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Language) header value.
*/
acceptLanguage?: string | AcceptLanguageInit;
/**
* The [`Accept-Ranges`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Ranges) header value.
*/
acceptRanges?: string;
/**
* The [`Age`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Age) header value.
*/
Expand All @@ -36,6 +40,10 @@ interface SuperHeadersPropertyInit {
* The [`Cache-Control`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control) header value.
*/
cacheControl?: string | CacheControlInit;
/**
* The [`Connection`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Connection) header value.
*/
connection?: string;
/**
* The [`Content-Disposition`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition) header value.
*/
Expand All @@ -60,6 +68,10 @@ interface SuperHeadersPropertyInit {
* The [`Expires`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Expires) header value.
*/
expires?: string | DateInit;
/**
* The [`Host`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Host) header value.
*/
host?: string;
/**
* The [`If-Modified-Since`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Modified-Since) header value.
*/
Expand All @@ -72,6 +84,10 @@ interface SuperHeadersPropertyInit {
* The [`Last-Modified`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Last-Modified) header value.
*/
lastModified?: string | DateInit;
/**
* The [`Referer`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referer) header value.
*/
referer?: string;
/**
* The [`Set-Cookie`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie) header value(s).
*/
Expand Down Expand Up @@ -115,15 +131,11 @@ export class SuperHeaders extends Headers {
for (let name of Object.getOwnPropertyNames(init)) {
let value = init[name];

if (typeof value === 'string') {
this.set(name, value);
let descriptor = Object.getOwnPropertyDescriptor(SuperHeaders.prototype, name);
if (descriptor?.set) {
descriptor.set.call(this, value);
} else {
let descriptor = Object.getOwnPropertyDescriptor(SuperHeaders.prototype, name);
if (descriptor?.set) {
descriptor.set.call(this, value);
} else {
this.set(name, value.toString());
}
this.set(name, value.toString());
}
}
}
Expand Down Expand Up @@ -346,6 +358,21 @@ export class SuperHeaders extends Headers {
this.#setHeaderValue('accept-language', AcceptLanguage, value);
}

/**
* The `Accept-Ranges` header indicates the server's acceptance of range requests.
*
* [MDN `Accept-Ranges` Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Ranges)
*
* [HTTP/1.1 Specification](https://datatracker.ietf.org/doc/html/rfc7233#section-2.3)
*/
get acceptRanges(): string | null {
return this.get('accept-ranges');
}

set acceptRanges(value: string | undefined | null) {
this.#setValue('accept-ranges', value);
}

/**
* The `Age` header contains the time in seconds an object was in a proxy cache.
*
Expand Down

0 comments on commit 3d49e31

Please sign in to comment.