Skip to content

Commit

Permalink
Add ETag support
Browse files Browse the repository at this point in the history
  • Loading branch information
mjackson committed Dec 20, 2024
1 parent de7b61e commit e24f757
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/headers/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ header.getPreferred(['gzip', 'deflate']); // 'gzip'
- `connection`
- `contentEncoding`
- `contentLanguage`
- `etag`
- `host`
- `location`
- `referer`
Expand Down
29 changes: 29 additions & 0 deletions packages/headers/src/lib/super-headers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ describe('SuperHeaders', () => {
assert.equal(headers.get('Date'), 'Fri, 01 Jan 2021 00:00:00 GMT');
});

it('handles the etag property', () => {
let headers = new SuperHeaders({ etag: 'abc' });
assert.equal(headers.get('ETag'), '"abc"');
});

it('handles the expires property', () => {
let headers = new SuperHeaders({ expires: new Date('2021-01-01T00:00:00Z') });
assert.equal(headers.get('Expires'), 'Fri, 01 Jan 2021 00:00:00 GMT');
Expand Down Expand Up @@ -526,6 +531,30 @@ describe('SuperHeaders', () => {
assert.equal(headers.date, null);
});

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

assert.equal(headers.etag, null);

headers.etag = 'abc';
assert.equal(headers.etag, '"abc"');

headers.etag = '"def"';
assert.equal(headers.etag, '"def"');

headers.etag = 'W/"def"';
assert.equal(headers.etag, 'W/"def"');

headers.etag = '';
assert.equal(headers.etag, '""');

headers.etag = '""';
assert.equal(headers.etag, '""');

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

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

Expand Down
22 changes: 22 additions & 0 deletions packages/headers/src/lib/super-headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ interface SuperHeadersPropertyInit {
* The [`Date`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Date) header value.
*/
date?: string | DateInit;
/**
* The [`ETag`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag) header value.
*/
etag?: string;
/**
* The [`Expires`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Expires) header value.
*/
Expand Down Expand Up @@ -543,6 +547,24 @@ export class SuperHeaders extends Headers {
this.#setDateValue('date', value);
}

/**
* The `ETag` header provides a unique identifier for the current version of the resource.
*
* [MDN `ETag` Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag)
*
* [HTTP/1.1 Specification](https://datatracker.ietf.org/doc/html/rfc7232#section-2.3)
*/
get etag(): string | null {
return this.get('etag');
}

set etag(value: string | undefined | null) {
this.#setValue(
'etag',
typeof value === 'string' && !/^(W\/)?".*"$/.test(value) ? `"${value}"` : value,
);
}

/**
* The `Expires` header contains the date/time after which the response is considered stale.
*
Expand Down

0 comments on commit e24f757

Please sign in to comment.