Skip to content

Commit

Permalink
Add linting, remove babel config, add package lock
Browse files Browse the repository at this point in the history
  • Loading branch information
robhanlon22 committed Feb 9, 2019
1 parent d5c28b7 commit 37712e7
Show file tree
Hide file tree
Showing 18 changed files with 5,764 additions and 724 deletions.
11 changes: 0 additions & 11 deletions .babelrc

This file was deleted.

8 changes: 8 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"env": {
"node": true,
"es6": true,
"mocha": true
},
"extends": ["plugin:prettier/recommended"]
}
5 changes: 5 additions & 0 deletions .huskyrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"hooks": {
"pre-commit": "lint-staged"
}
}
3 changes: 3 additions & 0 deletions .lintstagedrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"*.{js,json,md,yml,yaml}": ["prettier-eslint --write", "git add"]
}
5 changes: 5 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"singleQuote": true,
"tabWidth": 4,
"trailingComma": "es5"
}
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
sudo: false
language: node_js
node_js:
- "9"
- "8"
- "6"
- '9'
- '8'
- '6'
61 changes: 37 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ if (!policy.storable()) {

// Cache the data AND the policy object in your cache
// (this is pseudocode, roll your own cache (lru-cache package works))
letsPretendThisIsSomeCache.set(request.url, {policy, response}, policy.timeToLive());
letsPretendThisIsSomeCache.set(
request.url,
{ policy, response },
policy.timeToLive()
);
```

```js
// And later, when you receive a new request:
const {policy, response} = letsPretendThisIsSomeCache.get(newRequest.url);
const { policy, response } = letsPretendThisIsSomeCache.get(newRequest.url);

// It's not enough that it exists in the cache, it has to match the new request, too:
if (policy && policy.satisfiesWithoutRevalidation(newRequest)) {
Expand Down Expand Up @@ -59,15 +63,15 @@ const response = {
const options = {
shared: true,
cacheHeuristic: 0.1,
immutableMinTimeToLive: 24*3600*1000, // 24h
immutableMinTimeToLive: 24 * 3600 * 1000, // 24h
ignoreCargoCult: false,
trustServerDate: true,
};
```

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.

`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.
`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.

`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.

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

### `timeToLive()`

Returns approximate time in *milliseconds* until the response becomes stale (i.e. not fresh).
Returns approximate time in _milliseconds_ until the response becomes stale (i.e. not fresh).

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()`.

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

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

* `policy` — A new `CachePolicy` with HTTP headers updated from `revalidationResponse`. You can always replace the old cached `CachePolicy` with the new one.
* `modified` — Boolean indicating whether the response body has changed.
* If `false`, then a valid 304 Not Modified response has been received, and you can reuse the old cached response body.
* 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.
- `policy` — A new `CachePolicy` with HTTP headers updated from `revalidationResponse`. You can always replace the old cached `CachePolicy` with the new one.
- `modified` — Boolean indicating whether the response body has changed.
- If `false`, then a valid 304 Not Modified response has been received, and you can reuse the old cached response body.
- 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.

```js
// When serving requests from cache:
const {oldPolicy, oldResponse} = letsPretendThisIsSomeCache.get(newRequest.url);
const { oldPolicy, oldResponse } = letsPretendThisIsSomeCache.get(
newRequest.url
);

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

// Create updated policy and combined response from the old and new data
const {policy, modified} = oldPolicy.revalidatedPolicy(newRequest, newResponse);
const { policy, modified } = oldPolicy.revalidatedPolicy(
newRequest,
newResponse
);
const response = modified ? newResponse : oldResponse;

// Update the cache with the newer/fresher response
letsPretendThisIsSomeCache.set(newRequest.url, {policy, response}, policy.timeToLive());
letsPretendThisIsSomeCache.set(
newRequest.url,
{ policy, response },
policy.timeToLive()
);

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

## Used by

* [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)
- [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)

## Implemented

* `Cache-Control` response header with all the quirks.
* `Expires` with check for bad clocks.
* `Pragma` response header.
* `Age` response header.
* `Vary` response header.
* Default cacheability of statuses and methods.
* Requests for stale data.
* Filtering of hop-by-hop headers.
* Basic revalidation request
- `Cache-Control` response header with all the quirks.
- `Expires` with check for bad clocks.
- `Pragma` response header.
- `Age` response header.
- `Vary` response header.
- Default cacheability of statuses and methods.
- Requests for stale data.
- Filtering of hop-by-hop headers.
- Basic revalidation request

## Unimplemented

* Merging of range requests, If-Range (but correctly supports them as non-cacheable)
* Revalidation of multiple representations
- Merging of range requests, If-Range (but correctly supports them as non-cacheable)
- Revalidation of multiple representations
Loading

0 comments on commit 37712e7

Please sign in to comment.