fresh 1.0.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -4,16 +4,12 @@
4
4
  [![NPM Downloads][downloads-image]][downloads-url]
5
5
  [![Node.js Version][node-version-image]][node-version-url]
6
6
  [![Build Status][ci-image]][ci-url]
7
- [![Test Coverage][coveralls-image]][coveralls-url]
7
+ [![Test Coverage][coverage-image]][coverage-url]
8
8
 
9
- HTTP response freshness testing
9
+ HTTP response freshness and request precondition testing.
10
10
 
11
11
  ## Installation
12
12
 
13
- This is a [Node.js](https://nodejs.org/en/) module available through the
14
- [npm registry](https://www.npmjs.com/). Installation is done using the
15
- [`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):
16
-
17
13
  ```
18
14
  $ npm install fresh
19
15
  ```
@@ -21,97 +17,116 @@ $ npm install fresh
21
17
  ## API
22
18
 
23
19
  ```js
24
- var fresh = require('fresh')
20
+ import { allowsRange, fresh, preconditionFailed } from "fresh";
25
21
  ```
26
22
 
27
- ### fresh(reqHeaders, resHeaders)
23
+ Check freshness of the response using a lowercase-keyed request headers object and a response object with a `getHeader(name)` method. The method uses lowercase header names. A header value of `null` or `undefined` is treated as missing on both the request and response.
24
+
25
+ ### fresh(reqHeaders, res)
26
+
27
+ `fresh()` returns `true` when the request validators allow a `304 Not Modified` for the selected representation. It returns `false` when the request has no validators or validation fails. The caller decides whether the request method and response status permit a `304`.
28
+
29
+ The `FreshHeaders` type includes `if-none-match` and `if-modified-since`. `fresh()` reads the response’s `etag` or `last-modified` header as needed.
28
30
 
29
- Check freshness of the response using request and response headers.
31
+ ### preconditionFailed(reqHeaders, res)
30
32
 
31
- When the response is still "fresh" in the client's cache `true` is
32
- returned, otherwise `false` is returned to indicate that the client
33
- cache is now stale and the full response should be sent.
33
+ Check the GET and HEAD validators that can lead to `412 Precondition Failed`. Returns `true` if a precondition fails and `false` otherwise.
34
34
 
35
- When a client sends the `Cache-Control: no-cache` request header to
36
- indicate an end-to-end reload request, this module will return `false`
37
- to make handling these requests transparent.
35
+ `If-Match` uses strong ETag comparison and takes precedence over `If-Unmodified-Since`. A selected representation satisfies `If-Match: *` even without an ETag.
38
36
 
39
- ## Known Issues
37
+ The `PreconditionHeaders` type includes `if-match` and `if-unmodified-since`. `preconditionFailed()` reads the response’s `etag` or `last-modified` header as needed.
40
38
 
41
- This module is designed to only follow the HTTP specifications, not
42
- to work-around all kinda of client bugs (especially since this module
43
- typically does not receive enough information to understand what the
44
- client actually is).
39
+ ### allowsRange(reqHeaders, res, options?)
45
40
 
46
- There is a known issue that in certain versions of Safari, Safari
47
- will incorrectly make a request that allows this module to validate
48
- freshness of the resource even when Safari does not have a
49
- representation of the resource in the cache. The module
50
- [jumanji](https://www.npmjs.com/package/jumanji) can be used in
51
- an Express application to work-around this issue and also provides
52
- links to further reading on this Safari bug.
41
+ For a GET with `Range`, call `allowsRange()` after checking for `412` and `304`. It returns `true` if `If-Range` is absent or matches the selected representation. If it returns `true`, process `Range` as usual. If it returns `false`, ignore `Range` and send the full representation.
42
+
43
+ An ETag must match exactly and be strong. Characters inside the quotes are compared as supplied. To allow an HTTP-date match, pass `{ lastModifiedIsStrong: true }` when the caller knows the response’s `Last-Modified` is a [strong validator](https://www.rfc-editor.org/rfc/rfc9110.html#section-8.8.2.2). The date must match exactly.
44
+
45
+ The `RangeHeaders` type accepts `if-range`. `allowsRange()` checks `If-Range` and reads the response’s `etag` or `last-modified` header as needed.
53
46
 
54
47
  ## Example
55
48
 
56
49
  ### API usage
57
50
 
58
- <!-- eslint-disable no-redeclare -->
59
-
60
51
  ```js
61
- var reqHeaders = { 'if-none-match': '"foo"' }
62
- var resHeaders = { etag: '"bar"' }
63
- fresh(reqHeaders, resHeaders)
52
+ const reqHeaders = { "if-none-match": '"foo"' };
53
+ const headers = { etag: '"bar"' };
54
+ const res = {
55
+ getHeader(name) {
56
+ return headers[name];
57
+ },
58
+ };
59
+
60
+ fresh(reqHeaders, res);
64
61
  // => false
65
62
 
66
- var reqHeaders = { 'if-none-match': '"foo"' }
67
- var resHeaders = { etag: '"foo"' }
68
- fresh(reqHeaders, resHeaders)
63
+ headers.etag = '"foo"';
64
+ fresh(reqHeaders, res);
69
65
  // => true
70
66
  ```
71
67
 
72
68
  ### Using with Node.js http server
73
69
 
74
70
  ```js
75
- var fresh = require('fresh')
76
- var http = require('http')
77
-
78
- var server = http.createServer(function (req, res) {
79
- // perform server logic
80
- // ... including adding ETag / Last-Modified response headers
81
-
82
- if (isFresh(req, res)) {
83
- // client has a fresh copy of resource
84
- res.statusCode = 304
85
- res.end()
86
- return
71
+ import { createServer } from "node:http";
72
+ import { fresh, preconditionFailed } from "fresh";
73
+
74
+ const server = createServer(function (req, res) {
75
+ if (req.method !== "GET" && req.method !== "HEAD") {
76
+ res.writeHead(405, { Allow: "GET, HEAD" });
77
+ res.end();
78
+ return;
79
+ }
80
+
81
+ res.statusCode = 200;
82
+ res.setHeader("ETag", '"hello-world-v1"');
83
+
84
+ const headers = req.headersDistinct;
85
+
86
+ if (preconditionFailed(headers, res)) {
87
+ res.statusCode = 412;
88
+ res.end();
89
+ return;
90
+ }
91
+
92
+ if (fresh(headers, res)) {
93
+ res.statusCode = 304;
94
+ res.end();
95
+ return;
87
96
  }
88
97
 
89
- // send the resource
90
- res.statusCode = 200
91
- res.end('hello, world!')
92
- })
98
+ res.end("hello, world!");
99
+ });
100
+
101
+ server.listen(3000);
102
+ ```
93
103
 
94
- function isFresh (req, res) {
95
- return fresh(req.headers, {
96
- etag: res.getHeader('ETag'),
97
- 'last-modified': res.getHeader('Last-Modified')
98
- })
99
- }
104
+ ### Using with Fetch
100
105
 
101
- server.listen(3000)
106
+ ```js
107
+ const reqHeaders = {
108
+ "if-none-match": request.headers.get("if-none-match"),
109
+ "if-modified-since": request.headers.get("if-modified-since"),
110
+ };
111
+
112
+ const isFresh = fresh(reqHeaders, {
113
+ getHeader(name) {
114
+ return response.headers.get(name);
115
+ },
116
+ });
102
117
  ```
103
118
 
104
119
  ## License
105
120
 
106
121
  [MIT](LICENSE)
107
122
 
108
- [ci-image]: https://img.shields.io/github/workflow/status/jshttp/fresh/ci/master?label=ci
123
+ [ci-image]: https://img.shields.io/github/actions/workflow/status/jshttp/fresh/ci.yml?branch=master&label=ci
109
124
  [ci-url]: https://github.com/jshttp/fresh/actions/workflows/ci.yml
110
125
  [npm-image]: https://img.shields.io/npm/v/fresh.svg
111
126
  [npm-url]: https://npmjs.org/package/fresh
112
127
  [node-version-image]: https://img.shields.io/node/v/fresh.svg
113
128
  [node-version-url]: https://nodejs.org/en/
114
- [coveralls-image]: https://img.shields.io/coveralls/jshttp/fresh/master.svg
115
- [coveralls-url]: https://coveralls.io/r/jshttp/fresh?branch=master
129
+ [coverage-image]: https://img.shields.io/codecov/c/gh/jshttp/fresh
130
+ [coverage-url]: https://codecov.io/gh/jshttp/fresh
116
131
  [downloads-image]: https://img.shields.io/npm/dm/fresh.svg
117
132
  [downloads-url]: https://npmjs.org/package/fresh
@@ -0,0 +1,75 @@
1
+ /*!
2
+ * fresh
3
+ * Copyright(c) 2012 TJ Holowaychuk
4
+ * Copyright(c) 2016-2017 Douglas Christopher Wilson
5
+ * MIT Licensed
6
+ */
7
+ export type HeaderValue = string | readonly string[] | number | null | undefined;
8
+ /** Lowercase request headers read by {@link fresh} when checking for a 304. */
9
+ export interface FreshHeaders {
10
+ /**
11
+ * An HTTP-date. The response is fresh if Last-Modified is at or before this
12
+ * date. If-None-Match takes precedence.
13
+ */
14
+ "if-modified-since"?: HeaderValue;
15
+ /**
16
+ * An ETag list or `*`. The response is fresh if an ETag matches using weak
17
+ * comparison, or if `*` matches the selected representation.
18
+ */
19
+ "if-none-match"?: HeaderValue;
20
+ }
21
+ /** Lowercase request headers read by {@link preconditionFailed} when checking for a 412. */
22
+ export interface PreconditionHeaders {
23
+ /**
24
+ * An ETag list or `*`. The precondition passes if an ETag matches using strong
25
+ * comparison, or if `*` matches the selected representation. This header takes
26
+ * precedence over If-Unmodified-Since.
27
+ */
28
+ "if-match"?: HeaderValue;
29
+ /**
30
+ * An HTTP-date. The precondition fails if Last-Modified is later than this
31
+ * date. If-Match takes precedence.
32
+ */
33
+ "if-unmodified-since"?: HeaderValue;
34
+ }
35
+ /** Lowercase request headers read by {@link allowsRange} when evaluating If-Range. */
36
+ export interface RangeHeaders {
37
+ /**
38
+ * A single strong ETag or HTTP-date. An exact match lets the caller continue
39
+ * processing Range. Date matching requires `lastModifiedIsStrong`.
40
+ */
41
+ "if-range"?: HeaderValue;
42
+ }
43
+ /**
44
+ * A lowercase-keyed request header map, such as Node.js `req.headers` or
45
+ * `req.headersDistinct`. Each helper reads only the fields it needs.
46
+ */
47
+ export type Headers = Readonly<Record<string, HeaderValue>>;
48
+ /** A response that provides validators through a header getter. */
49
+ export interface Response {
50
+ /** Read `etag` or `last-modified` by its lowercase name as needed. */
51
+ getHeader(name: string): HeaderValue;
52
+ }
53
+ /**
54
+ * Check freshness of the response using request and response headers.
55
+ */
56
+ export declare function fresh(headers: FreshHeaders | Headers, res: Response): boolean;
57
+ /**
58
+ * Return true when a GET or HEAD precondition calls for a 412 response.
59
+ */
60
+ export declare function preconditionFailed(headers: PreconditionHeaders | Headers, res: Response): boolean;
61
+ export interface AllowsRangeOptions {
62
+ /**
63
+ * Allow an exact date match for If-Range. Set this to true only when the
64
+ * representation could not have changed twice in the second indicated by
65
+ * Last-Modified. Otherwise, a date-based If-Range falls back to a full response.
66
+ *
67
+ * @defaultValue false
68
+ * @see https://www.rfc-editor.org/rfc/rfc9110.html#section-8.8.2.2
69
+ */
70
+ lastModifiedIsStrong?: boolean;
71
+ }
72
+ /**
73
+ * Return true when If-Range is absent or matches the selected representation.
74
+ */
75
+ export declare function allowsRange(headers: RangeHeaders | Headers, res: Response, options?: AllowsRangeOptions): boolean;
package/dist/index.js ADDED
@@ -0,0 +1,266 @@
1
+ /*!
2
+ * fresh
3
+ * Copyright(c) 2012 TJ Holowaychuk
4
+ * Copyright(c) 2016-2017 Douglas Christopher Wilson
5
+ * MIT Licensed
6
+ */
7
+ const HTAB = 0x09;
8
+ const SPACE = 0x20;
9
+ const DOUBLE_QUOTE = 0x22;
10
+ const COMMA = 0x2c;
11
+ const COLON = 0x3a;
12
+ const RFC850_DATE = /^(?:Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday), [0-9]{2}-[a-z]{3}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} GMT$/i;
13
+ const ASCTIME_DATE = /^(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun) [a-z]{3} [ 0-9][0-9] [0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{4}$/i;
14
+ const GMT = 0x676d74;
15
+ const TICKS_PER_DAY = 24 * 60 * 61;
16
+ /**
17
+ * Check freshness of the response using request and response headers.
18
+ */
19
+ export function fresh(headers, res) {
20
+ const noneMatch = listHeader(headers["if-none-match"]);
21
+ if (noneMatch !== undefined)
22
+ return matchesTag(noneMatch, res, false);
23
+ const modifiedSince = singleHeader(headers["if-modified-since"]);
24
+ if (modifiedSince === undefined)
25
+ return false;
26
+ const lastModified = singleHeader(res.getHeader("last-modified"));
27
+ return (lastModified !== undefined &&
28
+ parseHttpDate(lastModified) <= parseHttpDate(modifiedSince));
29
+ }
30
+ /**
31
+ * Return true when a GET or HEAD precondition calls for a 412 response.
32
+ */
33
+ export function preconditionFailed(headers, res) {
34
+ const match = listHeader(headers["if-match"]);
35
+ if (match !== undefined)
36
+ return !matchesTag(match, res, true);
37
+ const unmodifiedSince = singleHeader(headers["if-unmodified-since"]);
38
+ if (unmodifiedSince === undefined)
39
+ return false;
40
+ const lastModified = singleHeader(res.getHeader("last-modified"));
41
+ return (lastModified !== undefined &&
42
+ parseHttpDate(lastModified) > parseHttpDate(unmodifiedSince));
43
+ }
44
+ /**
45
+ * Return true when If-Range is absent or matches the selected representation.
46
+ */
47
+ export function allowsRange(headers, res, options) {
48
+ const value = headers["if-range"];
49
+ if (value == null)
50
+ return true;
51
+ const ifRange = singleHeader(value);
52
+ if (ifRange === undefined)
53
+ return false;
54
+ // Supports strong etags only.
55
+ if (ifRange.charCodeAt(0) === DOUBLE_QUOTE) {
56
+ return (ifRange.indexOf('"', 1) === ifRange.length - 1 &&
57
+ ifRange === singleHeader(res.getHeader("etag")));
58
+ }
59
+ if (!options?.lastModifiedIsStrong)
60
+ return false;
61
+ const lastModified = singleHeader(res.getHeader("last-modified"));
62
+ return (lastModified !== undefined &&
63
+ parseHttpDate(lastModified) === parseHttpDate(ifRange));
64
+ }
65
+ function matchesTag(field, res, strong) {
66
+ if (field === "")
67
+ return false;
68
+ if (field === "*")
69
+ return true;
70
+ const etag = singleHeader(res.getHeader("etag"));
71
+ return etag !== undefined && hasTag(field, etag, strong);
72
+ }
73
+ function listHeader(value) {
74
+ if (typeof value === "string")
75
+ return value;
76
+ if (Array.isArray(value)) {
77
+ return value.length === 1 ? value[0] : value.join(", ");
78
+ }
79
+ }
80
+ function singleHeader(value) {
81
+ if (typeof value === "string")
82
+ return value;
83
+ if (Array.isArray(value)) {
84
+ return value.length === 1 ? value[0] : undefined;
85
+ }
86
+ }
87
+ /**
88
+ * Parse an HTTP date into a number that preserves chronological order.
89
+ */
90
+ function parseHttpDate(date) {
91
+ let day;
92
+ let monthStart;
93
+ let year;
94
+ let timeStart;
95
+ let shortYear = false;
96
+ // IMF-fixdate: "Sun, 06 Nov 1994 08:49:37 GMT".
97
+ if (date.length === 29 &&
98
+ isWeekday(readAsciiToken(date, 0)) &&
99
+ date.charCodeAt(3) === COMMA &&
100
+ date.charCodeAt(4) === SPACE &&
101
+ date.charCodeAt(25) === SPACE &&
102
+ readAsciiToken(date, 26) === GMT &&
103
+ date.charCodeAt(7) === SPACE &&
104
+ date.charCodeAt(11) === SPACE &&
105
+ date.charCodeAt(16) === SPACE &&
106
+ date.charCodeAt(19) === COLON &&
107
+ date.charCodeAt(22) === COLON) {
108
+ day = readTwoDigits(date, 5);
109
+ monthStart = 8;
110
+ year = readTwoDigits(date, 12) * 100 + readTwoDigits(date, 14);
111
+ timeStart = 17;
112
+ }
113
+ else if (date.length === 24 && ASCTIME_DATE.test(date)) {
114
+ day =
115
+ date.charCodeAt(8) === SPACE
116
+ ? date.charCodeAt(9) - 48
117
+ : readTwoDigits(date, 8);
118
+ monthStart = 4;
119
+ year = readTwoDigits(date, 20) * 100 + readTwoDigits(date, 22);
120
+ timeStart = 11;
121
+ }
122
+ else if (date.length >= 30 && date.length <= 33 && RFC850_DATE.test(date)) {
123
+ // The date and time after the weekday, comma, and space are fixed-width.
124
+ const start = date.length - 22;
125
+ day = readTwoDigits(date, start);
126
+ monthStart = start + 3;
127
+ year = readTwoDigits(date, start + 7);
128
+ timeStart = start + 10;
129
+ shortYear = true;
130
+ }
131
+ else {
132
+ return NaN;
133
+ }
134
+ const month = monthNumber(readAsciiToken(date, monthStart));
135
+ if (month < 0)
136
+ return NaN;
137
+ const hour = readTwoDigits(date, timeStart);
138
+ const minute = readTwoDigits(date, timeStart + 3);
139
+ const second = readTwoDigits(date, timeStart + 6);
140
+ if (day < 1 || day > 31 || hour > 23 || minute > 59 || second > 60) {
141
+ return NaN;
142
+ }
143
+ // Keep room for a leap second when ordering seconds within the day.
144
+ const time = (hour * 60 + minute) * 61 + second;
145
+ if (shortYear) {
146
+ const now = new Date();
147
+ const limit = now.getUTCFullYear() + 50;
148
+ year = limit - ((limit - year) % 100);
149
+ if (year === limit) {
150
+ const withinYear = (month * 31 + day) * TICKS_PER_DAY + time;
151
+ const limitWithinYear = (now.getUTCMonth() * 31 + now.getUTCDate()) * TICKS_PER_DAY +
152
+ (now.getUTCHours() * 60 + now.getUTCMinutes()) * 61 +
153
+ now.getUTCSeconds();
154
+ if (withinYear > limitWithinYear)
155
+ year -= 100;
156
+ }
157
+ }
158
+ if (year < 1900)
159
+ return NaN;
160
+ // Reserve 31 day slots for each month to preserve chronological order.
161
+ return ((year * 12 + month) * 31 + day) * TICKS_PER_DAY + time;
162
+ }
163
+ function readAsciiToken(date, offset) {
164
+ const first = date.charCodeAt(offset);
165
+ const second = date.charCodeAt(offset + 1);
166
+ const third = date.charCodeAt(offset + 2);
167
+ if ((first | second | third) > 0x7f)
168
+ return -1;
169
+ // Pack three ASCII bytes and set the lowercase bit in each.
170
+ return 0x202020 | (first << 16) | (second << 8) | third;
171
+ }
172
+ function isWeekday(token) {
173
+ switch (token) {
174
+ case 0x73756e: // sun
175
+ case 0x6d6f6e: // mon
176
+ case 0x747565: // tue
177
+ case 0x776564: // wed
178
+ case 0x746875: // thu
179
+ case 0x667269: // fri
180
+ case 0x736174: // sat
181
+ return true;
182
+ default:
183
+ return false;
184
+ }
185
+ }
186
+ function monthNumber(token) {
187
+ switch (token) {
188
+ case 0x6a616e: // jan
189
+ return 0;
190
+ case 0x666562: // feb
191
+ return 1;
192
+ case 0x6d6172: // mar
193
+ return 2;
194
+ case 0x617072: // apr
195
+ return 3;
196
+ case 0x6d6179: // may
197
+ return 4;
198
+ case 0x6a756e: // jun
199
+ return 5;
200
+ case 0x6a756c: // jul
201
+ return 6;
202
+ case 0x617567: // aug
203
+ return 7;
204
+ case 0x736570: // sep
205
+ return 8;
206
+ case 0x6f6374: // oct
207
+ return 9;
208
+ case 0x6e6f76: // nov
209
+ return 10;
210
+ case 0x646563: // dec
211
+ return 11;
212
+ default:
213
+ return -1;
214
+ }
215
+ }
216
+ function readTwoDigits(date, offset) {
217
+ const tens = date.charCodeAt(offset) - 48;
218
+ const ones = date.charCodeAt(offset + 1) - 48;
219
+ return tens >>> 0 <= 9 && ones >>> 0 <= 9 ? tens * 10 + ones : NaN;
220
+ }
221
+ function hasTag(field, tag, strong) {
222
+ const len = field.length;
223
+ if (tag.startsWith("W/")) {
224
+ if (strong)
225
+ return false;
226
+ tag = tag.slice(2);
227
+ }
228
+ let matched = false;
229
+ let index = 0;
230
+ while (index < len) {
231
+ const code = field.charCodeAt(index);
232
+ // Skip optional whitespace and empty list elements.
233
+ if (code === HTAB || code === SPACE || code === COMMA) {
234
+ index++;
235
+ continue;
236
+ }
237
+ let skipMatch = false;
238
+ if (code !== DOUBLE_QUOTE) {
239
+ if (!field.startsWith('W/"', index))
240
+ return false;
241
+ index += 2;
242
+ skipMatch = strong;
243
+ }
244
+ const start = index;
245
+ index = field.indexOf('"', start + 1) + 1;
246
+ if (index === 0)
247
+ return false;
248
+ // Fast path optimized return for single tags.
249
+ if (index === len) {
250
+ return matched || (!skipMatch && field.slice(start) === tag);
251
+ }
252
+ // Continue validating list boundaries after a match.
253
+ if (!matched && !skipMatch) {
254
+ matched = index - start === tag.length && field.startsWith(tag, start);
255
+ }
256
+ while (index < len) {
257
+ const separator = field.charCodeAt(index++);
258
+ if (separator === COMMA)
259
+ break;
260
+ if (separator !== HTAB && separator !== SPACE)
261
+ return false;
262
+ }
263
+ }
264
+ return matched;
265
+ }
266
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,IAAI,GAAG,IAAI,CAAC;AAClB,MAAM,KAAK,GAAG,IAAI,CAAC;AACnB,MAAM,YAAY,GAAG,IAAI,CAAC;AAC1B,MAAM,KAAK,GAAG,IAAI,CAAC;AACnB,MAAM,KAAK,GAAG,IAAI,CAAC;AAEnB,MAAM,WAAW,GACf,4HAA4H,CAAC;AAC/H,MAAM,YAAY,GAChB,6FAA6F,CAAC;AAChG,MAAM,GAAG,GAAG,QAAQ,CAAC;AACrB,MAAM,aAAa,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC;AAuDnC;;GAEG;AACH,MAAM,UAAU,KAAK,CAAC,OAA+B,EAAE,GAAa;IAClE,MAAM,SAAS,GAAG,UAAU,CAAC,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC;IACvD,IAAI,SAAS,KAAK,SAAS;QAAE,OAAO,UAAU,CAAC,SAAS,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC;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CAAC,KAAK,EAAE,CAAC,CAAC;YAC5C,IAAI,SAAS,KAAK,KAAK;gBAAE,MAAM;YAC/B,IAAI,SAAS,KAAK,IAAI,IAAI,SAAS,KAAK,KAAK;gBAAE,OAAO,KAAK,CAAC;QAC9D,CAAC;IACH,CAAC;IAED,OAAO,OAAO,CAAC;AACjB,CAAC","sourcesContent":["/*!\n * fresh\n * Copyright(c) 2012 TJ Holowaychuk\n * Copyright(c) 2016-2017 Douglas Christopher Wilson\n * MIT Licensed\n */\n\nconst HTAB = 0x09;\nconst SPACE = 0x20;\nconst DOUBLE_QUOTE = 0x22;\nconst COMMA = 0x2c;\nconst COLON = 0x3a;\n\nconst RFC850_DATE =\n /^(?:Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday), [0-9]{2}-[a-z]{3}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} GMT$/i;\nconst ASCTIME_DATE =\n /^(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun) [a-z]{3} [ 0-9][0-9] [0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{4}$/i;\nconst GMT = 0x676d74;\nconst TICKS_PER_DAY = 24 * 60 * 61;\n\nexport type HeaderValue =\n string | readonly string[] | number | null | undefined;\n\n/** Lowercase request headers read by {@link fresh} when checking for a 304. */\nexport interface FreshHeaders {\n /**\n * An HTTP-date. The response is fresh if Last-Modified is at or before this\n * date. If-None-Match takes precedence.\n */\n \"if-modified-since\"?: HeaderValue;\n /**\n * An ETag list or `*`. The response is fresh if an ETag matches using weak\n * comparison, or if `*` matches the selected representation.\n */\n \"if-none-match\"?: HeaderValue;\n}\n\n/** Lowercase request headers read by {@link preconditionFailed} when checking for a 412. */\nexport interface PreconditionHeaders {\n /**\n * An ETag list or `*`. The precondition passes if an ETag matches using strong\n * comparison, or if `*` matches the selected representation. This header takes\n * precedence over If-Unmodified-Since.\n */\n \"if-match\"?: HeaderValue;\n /**\n * An HTTP-date. The precondition fails if Last-Modified is later than this\n * date. If-Match takes precedence.\n */\n \"if-unmodified-since\"?: HeaderValue;\n}\n\n/** Lowercase request headers read by {@link allowsRange} when evaluating If-Range. */\nexport interface RangeHeaders {\n /**\n * A single strong ETag or HTTP-date. An exact match lets the caller continue\n * processing Range. Date matching requires `lastModifiedIsStrong`.\n */\n \"if-range\"?: HeaderValue;\n}\n\n/**\n * A lowercase-keyed request header map, such as Node.js `req.headers` or\n * `req.headersDistinct`. Each helper reads only the fields it needs.\n */\nexport type Headers = Readonly<Record<string, HeaderValue>>;\n\n/** A response that provides validators through a header getter. */\nexport interface Response {\n /** Read `etag` or `last-modified` by its lowercase name as needed. */\n getHeader(name: string): HeaderValue;\n}\n\n/**\n * Check freshness of the response using request and response headers.\n */\nexport function fresh(headers: FreshHeaders | Headers, res: Response): boolean {\n const noneMatch = listHeader(headers[\"if-none-match\"]);\n if (noneMatch !== undefined) return matchesTag(noneMatch, res, false);\n\n const modifiedSince = singleHeader(headers[\"if-modified-since\"]);\n if (modifiedSince === undefined) return false;\n\n const lastModified = singleHeader(res.getHeader(\"last-modified\"));\n return (\n lastModified !== undefined &&\n parseHttpDate(lastModified) <= parseHttpDate(modifiedSince)\n );\n}\n\n/**\n * Return true when a GET or HEAD precondition calls for a 412 response.\n */\nexport function preconditionFailed(\n headers: PreconditionHeaders | Headers,\n res: Response,\n): boolean {\n const match = listHeader(headers[\"if-match\"]);\n if (match !== undefined) return !matchesTag(match, res, true);\n\n const unmodifiedSince = singleHeader(headers[\"if-unmodified-since\"]);\n if (unmodifiedSince === undefined) return false;\n\n const lastModified = singleHeader(res.getHeader(\"last-modified\"));\n return (\n lastModified !== undefined &&\n parseHttpDate(lastModified) > parseHttpDate(unmodifiedSince)\n );\n}\n\nexport interface AllowsRangeOptions {\n /**\n * Allow an exact date match for If-Range. Set this to true only when the\n * representation could not have changed twice in the second indicated by\n * Last-Modified. Otherwise, a date-based If-Range falls back to a full response.\n *\n * @defaultValue false\n * @see https://www.rfc-editor.org/rfc/rfc9110.html#section-8.8.2.2\n */\n lastModifiedIsStrong?: boolean;\n}\n\n/**\n * Return true when If-Range is absent or matches the selected representation.\n */\nexport function allowsRange(\n headers: RangeHeaders | Headers,\n res: Response,\n options?: AllowsRangeOptions,\n): boolean {\n const value = headers[\"if-range\"];\n if (value == null) return true;\n\n const ifRange = singleHeader(value);\n if (ifRange === undefined) return false;\n\n // Supports strong etags only.\n if (ifRange.charCodeAt(0) === DOUBLE_QUOTE) {\n return (\n ifRange.indexOf('\"', 1) === ifRange.length - 1 &&\n ifRange === singleHeader(res.getHeader(\"etag\"))\n );\n }\n\n if (!options?.lastModifiedIsStrong) return false;\n const lastModified = singleHeader(res.getHeader(\"last-modified\"));\n return (\n lastModified !== undefined &&\n parseHttpDate(lastModified) === parseHttpDate(ifRange)\n );\n}\n\nfunction matchesTag(field: string, res: Response, strong: boolean): boolean {\n if (field === \"\") return false;\n if (field === \"*\") return true;\n\n const etag = singleHeader(res.getHeader(\"etag\"));\n return etag !== undefined && hasTag(field, etag, strong);\n}\n\nfunction listHeader(value: HeaderValue): string | undefined {\n if (typeof value === \"string\") return value;\n if (Array.isArray(value)) {\n return value.length === 1 ? value[0] : value.join(\", \");\n }\n}\n\nfunction singleHeader(value: HeaderValue | number): string | undefined {\n if (typeof value === \"string\") return value;\n if (Array.isArray(value)) {\n return value.length === 1 ? value[0] : undefined;\n }\n}\n\n/**\n * Parse an HTTP date into a number that preserves chronological order.\n */\nfunction parseHttpDate(date: string): number {\n let day: number;\n let monthStart: number;\n let year: number;\n let timeStart: number;\n let shortYear = false;\n\n // IMF-fixdate: \"Sun, 06 Nov 1994 08:49:37 GMT\".\n if (\n date.length === 29 &&\n isWeekday(readAsciiToken(date, 0)) &&\n date.charCodeAt(3) === COMMA &&\n date.charCodeAt(4) === SPACE &&\n date.charCodeAt(25) === SPACE &&\n readAsciiToken(date, 26) === GMT &&\n date.charCodeAt(7) === SPACE &&\n date.charCodeAt(11) === SPACE &&\n date.charCodeAt(16) === SPACE &&\n date.charCodeAt(19) === COLON &&\n date.charCodeAt(22) === COLON\n ) {\n day = readTwoDigits(date, 5);\n monthStart = 8;\n year = readTwoDigits(date, 12) * 100 + readTwoDigits(date, 14);\n timeStart = 17;\n } else if (date.length === 24 && ASCTIME_DATE.test(date)) {\n day =\n date.charCodeAt(8) === SPACE\n ? date.charCodeAt(9) - 48\n : readTwoDigits(date, 8);\n monthStart = 4;\n year = readTwoDigits(date, 20) * 100 + readTwoDigits(date, 22);\n timeStart = 11;\n } else if (date.length >= 30 && date.length <= 33 && RFC850_DATE.test(date)) {\n // The date and time after the weekday, comma, and space are fixed-width.\n const start = date.length - 22;\n day = readTwoDigits(date, start);\n monthStart = start + 3;\n year = readTwoDigits(date, start + 7);\n timeStart = start + 10;\n shortYear = true;\n } else {\n return NaN;\n }\n\n const month = monthNumber(readAsciiToken(date, monthStart));\n if (month < 0) return NaN;\n\n const hour = readTwoDigits(date, timeStart);\n const minute = readTwoDigits(date, timeStart + 3);\n const second = readTwoDigits(date, timeStart + 6);\n if (day < 1 || day > 31 || hour > 23 || minute > 59 || second > 60) {\n return NaN;\n }\n\n // Keep room for a leap second when ordering seconds within the day.\n const time = (hour * 60 + minute) * 61 + second;\n\n if (shortYear) {\n const now = new Date();\n const limit = now.getUTCFullYear() + 50;\n year = limit - ((limit - year) % 100);\n if (year === limit) {\n const withinYear = (month * 31 + day) * TICKS_PER_DAY + time;\n const limitWithinYear =\n (now.getUTCMonth() * 31 + now.getUTCDate()) * TICKS_PER_DAY +\n (now.getUTCHours() * 60 + now.getUTCMinutes()) * 61 +\n now.getUTCSeconds();\n if (withinYear > limitWithinYear) year -= 100;\n }\n }\n\n if (year < 1900) return NaN;\n\n // Reserve 31 day slots for each month to preserve chronological order.\n return ((year * 12 + month) * 31 + day) * TICKS_PER_DAY + time;\n}\n\nfunction readAsciiToken(date: string, offset: number): number {\n const first = date.charCodeAt(offset);\n const second = date.charCodeAt(offset + 1);\n const third = date.charCodeAt(offset + 2);\n if ((first | second | third) > 0x7f) return -1;\n // Pack three ASCII bytes and set the lowercase bit in each.\n return 0x202020 | (first << 16) | (second << 8) | third;\n}\n\nfunction isWeekday(token: number): boolean {\n switch (token) {\n case 0x73756e: // sun\n case 0x6d6f6e: // mon\n case 0x747565: // tue\n case 0x776564: // wed\n case 0x746875: // thu\n case 0x667269: // fri\n case 0x736174: // sat\n return true;\n default:\n return false;\n }\n}\n\nfunction monthNumber(token: number): number {\n switch (token) {\n case 0x6a616e: // jan\n return 0;\n case 0x666562: // feb\n return 1;\n case 0x6d6172: // mar\n return 2;\n case 0x617072: // apr\n return 3;\n case 0x6d6179: // may\n return 4;\n case 0x6a756e: // jun\n return 5;\n case 0x6a756c: // jul\n return 6;\n case 0x617567: // aug\n return 7;\n case 0x736570: // sep\n return 8;\n case 0x6f6374: // oct\n return 9;\n case 0x6e6f76: // nov\n return 10;\n case 0x646563: // dec\n return 11;\n default:\n return -1;\n }\n}\n\nfunction readTwoDigits(date: string, offset: number): number {\n const tens = date.charCodeAt(offset) - 48;\n const ones = date.charCodeAt(offset + 1) - 48;\n return tens >>> 0 <= 9 && ones >>> 0 <= 9 ? tens * 10 + ones : NaN;\n}\n\nfunction hasTag(field: string, tag: string, strong: boolean): boolean {\n const len = field.length;\n if (tag.startsWith(\"W/\")) {\n if (strong) return false;\n tag = tag.slice(2);\n }\n\n let matched = false;\n let index = 0;\n\n while (index < len) {\n const code = field.charCodeAt(index);\n\n // Skip optional whitespace and empty list elements.\n if (code === HTAB || code === SPACE || code === COMMA) {\n index++;\n continue;\n }\n\n let skipMatch = false;\n if (code !== DOUBLE_QUOTE) {\n if (!field.startsWith('W/\"', index)) return false;\n index += 2;\n skipMatch = strong;\n }\n\n const start = index;\n index = field.indexOf('\"', start + 1) + 1;\n if (index === 0) return false;\n\n // Fast path optimized return for single tags.\n if (index === len) {\n return matched || (!skipMatch && field.slice(start) === tag);\n }\n\n // Continue validating list boundaries after a match.\n if (!matched && !skipMatch) {\n matched = index - start === tag.length && field.startsWith(tag, start);\n }\n\n while (index < len) {\n const separator = field.charCodeAt(index++);\n if (separator === COMMA) break;\n if (separator !== HTAB && separator !== SPACE) return false;\n }\n }\n\n return matched;\n}\n"]}
package/package.json CHANGED
@@ -1,13 +1,7 @@
1
1
  {
2
2
  "name": "fresh",
3
+ "version": "3.0.0",
3
4
  "description": "HTTP response freshness testing",
4
- "version": "1.0.0",
5
- "author": "TJ Holowaychuk <tj@vision-media.ca> (http://tjholowaychuk.com)",
6
- "contributors": [
7
- "Douglas Christopher Wilson <doug@somethingdoug.com>",
8
- "Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)"
9
- ],
10
- "license": "MIT",
11
5
  "keywords": [
12
6
  "fresh",
13
7
  "http",
@@ -15,32 +9,56 @@
15
9
  "cache"
16
10
  ],
17
11
  "repository": "jshttp/fresh",
18
- "devDependencies": {
19
- "beautify-benchmark": "0.2.4",
20
- "benchmark": "2.1.4",
21
- "eslint": "8.12.0",
22
- "eslint-config-standard": "14.1.1",
23
- "eslint-plugin-import": "2.25.4",
24
- "eslint-plugin-markdown": "2.2.1",
25
- "eslint-plugin-node": "11.1.0",
26
- "eslint-plugin-promise": "6.0.0",
27
- "eslint-plugin-standard": "4.1.0",
28
- "mocha": "9.2.0",
29
- "nyc": "15.1.0"
12
+ "funding": {
13
+ "type": "opencollective",
14
+ "url": "https://opencollective.com/express"
30
15
  },
16
+ "license": "MIT",
17
+ "author": "TJ Holowaychuk <tj@vision-media.ca> (http://tjholowaychuk.com)",
18
+ "contributors": [
19
+ "Douglas Christopher Wilson <doug@somethingdoug.com>",
20
+ "Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)"
21
+ ],
22
+ "type": "module",
23
+ "exports": "./dist/index.js",
24
+ "main": "./dist/index.js",
25
+ "typings": "./dist/index.d.ts",
31
26
  "files": [
32
- "HISTORY.md",
33
- "LICENSE",
34
- "index.js"
27
+ "dist/"
35
28
  ],
29
+ "scripts": {
30
+ "bench": "vitest bench",
31
+ "build": "ts-scripts build",
32
+ "format": "ts-scripts format",
33
+ "prepare": "ts-scripts install && npm run build",
34
+ "size": "size-limit",
35
+ "specs": "ts-scripts specs",
36
+ "test": "ts-scripts test && npm run size"
37
+ },
38
+ "devDependencies": {
39
+ "@borderless/ts-scripts": "^0.15.0",
40
+ "@size-limit/preset-small-lib": "^14.3.0",
41
+ "@types/node": "^22.20.7",
42
+ "@vitest/coverage-v8": "^5.1.1",
43
+ "size-limit": "^14.3.0",
44
+ "typescript": "^7.0.2",
45
+ "vitest": "^5.1.1"
46
+ },
36
47
  "engines": {
37
- "node": ">= 0.8"
48
+ "node": ">=22"
38
49
  },
39
- "scripts": {
40
- "bench": "node benchmark/index.js",
41
- "lint": "eslint .",
42
- "test": "mocha --reporter spec --check-leaks --bail test/",
43
- "test-ci": "nyc --reporter=lcov --reporter=text npm test",
44
- "test-cov": "nyc --reporter=html --reporter=text npm test"
50
+ "size-limit": [
51
+ {
52
+ "path": "dist/index.js",
53
+ "limit": "1350 B"
54
+ }
55
+ ],
56
+ "ts-scripts": {
57
+ "dist": [
58
+ "dist"
59
+ ],
60
+ "project": [
61
+ "tsconfig.build.json"
62
+ ]
45
63
  }
46
64
  }
package/HISTORY.md DELETED
@@ -1,76 +0,0 @@
1
- 1.0.0 - 2024-09-04
2
- ==========
3
-
4
- * Drop support for Node.js below 0.8
5
- * Fix: Ignore `If-Modified-Since` in the presence of `If-None-Match`, according to [spec](https://www.rfc-editor.org/rfc/rfc9110.html#section-13.1.3-5). Fixes [#35](https://github.com/jshttp/fresh/issues/35)
6
-
7
- 0.5.2 / 2017-09-13
8
- ==================
9
-
10
- * Fix regression matching multiple ETags in `If-None-Match`
11
- * perf: improve `If-None-Match` token parsing
12
-
13
- 0.5.1 / 2017-09-11
14
- ==================
15
-
16
- * Fix handling of modified headers with invalid dates
17
- * perf: improve ETag match loop
18
-
19
- 0.5.0 / 2017-02-21
20
- ==================
21
-
22
- * Fix incorrect result when `If-None-Match` has both `*` and ETags
23
- * Fix weak `ETag` matching to match spec
24
- * perf: delay reading header values until needed
25
- * perf: skip checking modified time if ETag check failed
26
- * perf: skip parsing `If-None-Match` when no `ETag` header
27
- * perf: use `Date.parse` instead of `new Date`
28
-
29
- 0.4.0 / 2017-02-05
30
- ==================
31
-
32
- * Fix false detection of `no-cache` request directive
33
- * perf: enable strict mode
34
- * perf: hoist regular expressions
35
- * perf: remove duplicate conditional
36
- * perf: remove unnecessary boolean coercions
37
-
38
- 0.3.0 / 2015-05-12
39
- ==================
40
-
41
- * Add weak `ETag` matching support
42
-
43
- 0.2.4 / 2014-09-07
44
- ==================
45
-
46
- * Support Node.js 0.6
47
-
48
- 0.2.3 / 2014-09-07
49
- ==================
50
-
51
- * Move repository to jshttp
52
-
53
- 0.2.2 / 2014-02-19
54
- ==================
55
-
56
- * Revert "Fix for blank page on Safari reload"
57
-
58
- 0.2.1 / 2014-01-29
59
- ==================
60
-
61
- * Fix for blank page on Safari reload
62
-
63
- 0.2.0 / 2013-08-11
64
- ==================
65
-
66
- * Return stale for `Cache-Control: no-cache`
67
-
68
- 0.1.0 / 2012-06-15
69
- ==================
70
-
71
- * Add `If-None-Match: *` support
72
-
73
- 0.0.1 / 2012-06-10
74
- ==================
75
-
76
- * Initial release
package/index.js DELETED
@@ -1,136 +0,0 @@
1
- /*!
2
- * fresh
3
- * Copyright(c) 2012 TJ Holowaychuk
4
- * Copyright(c) 2016-2017 Douglas Christopher Wilson
5
- * MIT Licensed
6
- */
7
-
8
- 'use strict'
9
-
10
- /**
11
- * RegExp to check for no-cache token in Cache-Control.
12
- * @private
13
- */
14
-
15
- var CACHE_CONTROL_NO_CACHE_REGEXP = /(?:^|,)\s*?no-cache\s*?(?:,|$)/
16
-
17
- /**
18
- * Module exports.
19
- * @public
20
- */
21
-
22
- module.exports = fresh
23
-
24
- /**
25
- * Check freshness of the response using request and response headers.
26
- *
27
- * @param {Object} reqHeaders
28
- * @param {Object} resHeaders
29
- * @return {Boolean}
30
- * @public
31
- */
32
-
33
- function fresh (reqHeaders, resHeaders) {
34
- // fields
35
- var modifiedSince = reqHeaders['if-modified-since']
36
- var noneMatch = reqHeaders['if-none-match']
37
-
38
- // unconditional request
39
- if (!modifiedSince && !noneMatch) {
40
- return false
41
- }
42
-
43
- // Always return stale when Cache-Control: no-cache
44
- // to support end-to-end reload requests
45
- // https://tools.ietf.org/html/rfc2616#section-14.9.4
46
- var cacheControl = reqHeaders['cache-control']
47
- if (cacheControl && CACHE_CONTROL_NO_CACHE_REGEXP.test(cacheControl)) {
48
- return false
49
- }
50
-
51
- // if-none-match takes precedent over if-modified-since
52
- if (noneMatch) {
53
- if (noneMatch === '*') {
54
- return true
55
- }
56
- var etag = resHeaders.etag
57
-
58
- if (!etag) {
59
- return false
60
- }
61
-
62
- var matches = parseTokenList(noneMatch)
63
- for (var i = 0; i < matches.length; i++) {
64
- var match = matches[i]
65
- if (match === etag || match === 'W/' + etag || 'W/' + match === etag) {
66
- return true
67
- }
68
- }
69
-
70
- return false
71
- }
72
-
73
- // if-modified-since
74
- if (modifiedSince) {
75
- var lastModified = resHeaders['last-modified']
76
- var modifiedStale = !lastModified || !(parseHttpDate(lastModified) <= parseHttpDate(modifiedSince))
77
-
78
- if (modifiedStale) {
79
- return false
80
- }
81
- }
82
-
83
- return true
84
- }
85
-
86
- /**
87
- * Parse an HTTP Date into a number.
88
- *
89
- * @param {string} date
90
- * @private
91
- */
92
-
93
- function parseHttpDate (date) {
94
- var timestamp = date && Date.parse(date)
95
-
96
- // istanbul ignore next: guard against date.js Date.parse patching
97
- return typeof timestamp === 'number'
98
- ? timestamp
99
- : NaN
100
- }
101
-
102
- /**
103
- * Parse a HTTP token list.
104
- *
105
- * @param {string} str
106
- * @private
107
- */
108
-
109
- function parseTokenList (str) {
110
- var end = 0
111
- var list = []
112
- var start = 0
113
-
114
- // gather tokens
115
- for (var i = 0, len = str.length; i < len; i++) {
116
- switch (str.charCodeAt(i)) {
117
- case 0x20: /* */
118
- if (start === end) {
119
- start = end = i + 1
120
- }
121
- break
122
- case 0x2c: /* , */
123
- list.push(str.substring(start, end))
124
- start = end = i + 1
125
- break
126
- default:
127
- end = i + 1
128
- break
129
- }
130
- }
131
-
132
- // final token
133
- list.push(str.substring(start, end))
134
-
135
- return list
136
- }