Skip to content

Commit aacb119

Browse files
authored
Merge pull request #17 from ohwillie/prettier
Add linting, remove babel config, add package lock
2 parents d5c28b7 + 37712e7 commit aacb119

18 files changed

+5764
-724
lines changed

.babelrc

-11
This file was deleted.

.eslintrc.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"env": {
3+
"node": true,
4+
"es6": true,
5+
"mocha": true
6+
},
7+
"extends": ["plugin:prettier/recommended"]
8+
}

.huskyrc.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"hooks": {
3+
"pre-commit": "lint-staged"
4+
}
5+
}

.lintstagedrc.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"*.{js,json,md,yml,yaml}": ["prettier-eslint --write", "git add"]
3+
}

.prettierrc.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"singleQuote": true,
3+
"tabWidth": 4,
4+
"trailingComma": "es5"
5+
}

.travis.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
sudo: false
22
language: node_js
33
node_js:
4-
- "9"
5-
- "8"
6-
- "6"
4+
- '9'
5+
- '8'
6+
- '6'

README.md

+37-24
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@ if (!policy.storable()) {
1616

1717
// Cache the data AND the policy object in your cache
1818
// (this is pseudocode, roll your own cache (lru-cache package works))
19-
letsPretendThisIsSomeCache.set(request.url, {policy, response}, policy.timeToLive());
19+
letsPretendThisIsSomeCache.set(
20+
request.url,
21+
{ policy, response },
22+
policy.timeToLive()
23+
);
2024
```
2125

2226
```js
2327
// And later, when you receive a new request:
24-
const {policy, response} = letsPretendThisIsSomeCache.get(newRequest.url);
28+
const { policy, response } = letsPretendThisIsSomeCache.get(newRequest.url);
2529

2630
// It's not enough that it exists in the cache, it has to match the new request, too:
2731
if (policy && policy.satisfiesWithoutRevalidation(newRequest)) {
@@ -59,15 +63,15 @@ const response = {
5963
const options = {
6064
shared: true,
6165
cacheHeuristic: 0.1,
62-
immutableMinTimeToLive: 24*3600*1000, // 24h
66+
immutableMinTimeToLive: 24 * 3600 * 1000, // 24h
6367
ignoreCargoCult: false,
6468
trustServerDate: true,
6569
};
6670
```
6771

6872
If `options.shared` is `true` (default), then the response is evaluated from a perspective of a shared cache (i.e. `private` is not cacheable and `s-maxage` is respected). If `options.shared` is `false`, then the response is evaluated from a perspective of a single-user cache (i.e. `private` is cacheable and `s-maxage` is ignored). `shared: true` is recommended for HTTP clients.
6973

70-
`options.cacheHeuristic` is a fraction of response's age that is used as a fallback cache duration. The default is 0.1 (10%), e.g. if a file hasn't been modified for 100 days, it'll be cached for 100*0.1 = 10 days.
74+
`options.cacheHeuristic` is a fraction of response's age that is used as a fallback cache duration. The default is 0.1 (10%), e.g. if a file hasn't been modified for 100 days, it'll be cached for 100\*0.1 = 10 days.
7175

7276
`options.immutableMinTimeToLive` is a number of milliseconds to assume as the default time to cache responses with `Cache-Control: immutable`. Note that [per RFC](http://httpwg.org/http-extensions/immutable.html) these can become stale, so `max-age` still overrides the default.
7377

@@ -97,7 +101,7 @@ cachedResponse.headers = cachePolicy.responseHeaders(cachedResponse);
97101

98102
### `timeToLive()`
99103

100-
Returns approximate time in *milliseconds* until the response becomes stale (i.e. not fresh).
104+
Returns approximate time in _milliseconds_ until the response becomes stale (i.e. not fresh).
101105

102106
After that time (when `timeToLive() <= 0`) the response might not be usable without revalidation. However, there are exceptions, e.g. a client can explicitly allow stale responses, so always check with `satisfiesWithoutRevalidation()`.
103107

@@ -125,14 +129,16 @@ updateRequest.headers = cachePolicy.revalidationHeaders(updateRequest);
125129

126130
Use this method to update the cache after receiving a new response from the origin server. It returns an object with two keys:
127131

128-
* `policy` — A new `CachePolicy` with HTTP headers updated from `revalidationResponse`. You can always replace the old cached `CachePolicy` with the new one.
129-
* `modified` — Boolean indicating whether the response body has changed.
130-
* If `false`, then a valid 304 Not Modified response has been received, and you can reuse the old cached response body.
131-
* If `true`, you should use new response's body (if present), or make another request to the origin server without any conditional headers (i.e. don't use `revalidationHeaders()` this time) to get the new resource.
132+
- `policy` — A new `CachePolicy` with HTTP headers updated from `revalidationResponse`. You can always replace the old cached `CachePolicy` with the new one.
133+
- `modified` — Boolean indicating whether the response body has changed.
134+
- If `false`, then a valid 304 Not Modified response has been received, and you can reuse the old cached response body.
135+
- If `true`, you should use new response's body (if present), or make another request to the origin server without any conditional headers (i.e. don't use `revalidationHeaders()` this time) to get the new resource.
132136

133137
```js
134138
// When serving requests from cache:
135-
const {oldPolicy, oldResponse} = letsPretendThisIsSomeCache.get(newRequest.url);
139+
const { oldPolicy, oldResponse } = letsPretendThisIsSomeCache.get(
140+
newRequest.url
141+
);
136142

137143
if (!oldPolicy.satisfiesWithoutRevalidation(newRequest)) {
138144
// Change the request to ask the origin server if the cached response can be used
@@ -142,11 +148,18 @@ if (!oldPolicy.satisfiesWithoutRevalidation(newRequest)) {
142148
const newResponse = await makeRequest(newResponse);
143149

144150
// Create updated policy and combined response from the old and new data
145-
const {policy, modified} = oldPolicy.revalidatedPolicy(newRequest, newResponse);
151+
const { policy, modified } = oldPolicy.revalidatedPolicy(
152+
newRequest,
153+
newResponse
154+
);
146155
const response = modified ? newResponse : oldResponse;
147156

148157
// Update the cache with the newer/fresher response
149-
letsPretendThisIsSomeCache.set(newRequest.url, {policy, response}, policy.timeToLive());
158+
letsPretendThisIsSomeCache.set(
159+
newRequest.url,
160+
{ policy, response },
161+
policy.timeToLive()
162+
);
150163

151164
// And proceed returning cached response as usual
152165
response.headers = policy.responseHeaders();
@@ -160,21 +173,21 @@ if (!oldPolicy.satisfiesWithoutRevalidation(newRequest)) {
160173

161174
## Used by
162175

163-
* [ImageOptim API](https://imageoptim.com/api), [make-fetch-happen](https://github.com/zkat/make-fetch-happen), [cacheable-request](https://www.npmjs.com/package/cacheable-request) ([got](https://www.npmjs.com/package/got)), [npm/registry-fetch](https://github.com/npm/registry-fetch), [etc.](https://github.com/kornelski/http-cache-semantics/network/dependents)
176+
- [ImageOptim API](https://imageoptim.com/api), [make-fetch-happen](https://github.com/zkat/make-fetch-happen), [cacheable-request](https://www.npmjs.com/package/cacheable-request) ([got](https://www.npmjs.com/package/got)), [npm/registry-fetch](https://github.com/npm/registry-fetch), [etc.](https://github.com/kornelski/http-cache-semantics/network/dependents)
164177

165178
## Implemented
166179

167-
* `Cache-Control` response header with all the quirks.
168-
* `Expires` with check for bad clocks.
169-
* `Pragma` response header.
170-
* `Age` response header.
171-
* `Vary` response header.
172-
* Default cacheability of statuses and methods.
173-
* Requests for stale data.
174-
* Filtering of hop-by-hop headers.
175-
* Basic revalidation request
180+
- `Cache-Control` response header with all the quirks.
181+
- `Expires` with check for bad clocks.
182+
- `Pragma` response header.
183+
- `Age` response header.
184+
- `Vary` response header.
185+
- Default cacheability of statuses and methods.
186+
- Requests for stale data.
187+
- Filtering of hop-by-hop headers.
188+
- Basic revalidation request
176189

177190
## Unimplemented
178191

179-
* Merging of range requests, If-Range (but correctly supports them as non-cacheable)
180-
* Revalidation of multiple representations
192+
- Merging of range requests, If-Range (but correctly supports them as non-cacheable)
193+
- Revalidation of multiple representations

0 commit comments

Comments
 (0)