cacheable-request 4.0.0 → 5.2.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 +19 -3
- package/package.json +9 -9
- package/src/index.js +15 -6
package/README.md
CHANGED
|
@@ -117,7 +117,9 @@ Returns an event emitter.
|
|
|
117
117
|
|
|
118
118
|
Type: `object`, `string`
|
|
119
119
|
|
|
120
|
-
Any of the default request functions options
|
|
120
|
+
- Any of the default request functions options.
|
|
121
|
+
- Any [`http-cache-semantics`](https://github.com/kornelski/http-cache-semantics#constructor-options) options.
|
|
122
|
+
- Any of the following:
|
|
121
123
|
|
|
122
124
|
###### opts.cache
|
|
123
125
|
|
|
@@ -131,10 +133,17 @@ If the cache should be used. Setting this to false will completely bypass the ca
|
|
|
131
133
|
Type: `boolean`<br>
|
|
132
134
|
Default: `false`
|
|
133
135
|
|
|
134
|
-
If set to `false`, after a cached resource's TTL expires it is kept in the cache and will be revalidated on the next request with `If-None-Match`/`If-Modified-Since` headers.
|
|
135
|
-
|
|
136
136
|
If set to `true` once a cached resource has expired it is deleted and will have to be re-requested.
|
|
137
137
|
|
|
138
|
+
If set to `false` (default), after a cached resource's TTL expires it is kept in the cache and will be revalidated on the next request with `If-None-Match`/`If-Modified-Since` headers.
|
|
139
|
+
|
|
140
|
+
###### opts.maxTtl
|
|
141
|
+
|
|
142
|
+
Type: `number`<br>
|
|
143
|
+
Default: `undefined`
|
|
144
|
+
|
|
145
|
+
Limits TTL. The `number` represents milliseconds.
|
|
146
|
+
|
|
138
147
|
###### opts.automaticFailover
|
|
139
148
|
|
|
140
149
|
Type: `boolean`<br>
|
|
@@ -142,6 +151,13 @@ Default: `false`
|
|
|
142
151
|
|
|
143
152
|
When set to `true`, if the DB connection fails we will automatically fallback to a network request. DB errors will still be emitted to notify you of the problem even though the request callback may succeed.
|
|
144
153
|
|
|
154
|
+
###### opts.forceRefresh
|
|
155
|
+
|
|
156
|
+
Type: `boolean`<br>
|
|
157
|
+
Default: `false`
|
|
158
|
+
|
|
159
|
+
Forces refreshing the cache. If the response could be retrieved from the cache, it will perform a new request and override the cache instead.
|
|
160
|
+
|
|
145
161
|
##### cb
|
|
146
162
|
|
|
147
163
|
Type: `function`
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cacheable-request",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.2.0",
|
|
4
4
|
"description": "Wrap native HTTP requests with RFC compliant cache support",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "lukechilds/cacheable-request",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
],
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"clone-response": "^1.0.2",
|
|
33
|
-
"get-stream": "^
|
|
33
|
+
"get-stream": "^4.0.0",
|
|
34
34
|
"http-cache-semantics": "^4.0.0",
|
|
35
35
|
"keyv": "^3.0.0",
|
|
36
36
|
"lowercase-keys": "^1.0.1",
|
|
@@ -38,17 +38,17 @@
|
|
|
38
38
|
"responselike": "^1.0.2"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"@keyv/sqlite": "^
|
|
42
|
-
"ava": "
|
|
41
|
+
"@keyv/sqlite": "^2.0.0",
|
|
42
|
+
"ava": "^0.25.0",
|
|
43
43
|
"coveralls": "^3.0.0",
|
|
44
44
|
"create-test-server": "2.1.1",
|
|
45
|
-
"delay": "^
|
|
45
|
+
"delay": "^4.0.0",
|
|
46
46
|
"eslint-config-xo-lukechilds": "^1.0.0",
|
|
47
|
-
"nyc": "^
|
|
48
|
-
"pify": "^
|
|
49
|
-
"sqlite3": "^4.0.
|
|
47
|
+
"nyc": "^13.0.1",
|
|
48
|
+
"pify": "^4.0.0",
|
|
49
|
+
"sqlite3": "^4.0.2",
|
|
50
50
|
"this": "^1.0.2",
|
|
51
|
-
"xo": "
|
|
51
|
+
"xo": "^0.22.0"
|
|
52
52
|
},
|
|
53
53
|
"xo": {
|
|
54
54
|
"extends": "xo-lukechilds"
|
package/src/index.js
CHANGED
|
@@ -35,8 +35,11 @@ class CacheableRequest {
|
|
|
35
35
|
url = normalizeUrlObject(urlLib.parse(opts.toString()));
|
|
36
36
|
opts = {};
|
|
37
37
|
} else {
|
|
38
|
-
const [pathname, ...
|
|
39
|
-
|
|
38
|
+
const [pathname, ...searchParts] = (opts.path || '').split('?');
|
|
39
|
+
const search = searchParts.length > 0 ?
|
|
40
|
+
`?${searchParts.join('?')}` :
|
|
41
|
+
'';
|
|
42
|
+
url = normalizeUrlObject({ ...opts, pathname, search });
|
|
40
43
|
}
|
|
41
44
|
opts = {
|
|
42
45
|
headers: {},
|
|
@@ -64,7 +67,8 @@ class CacheableRequest {
|
|
|
64
67
|
const makeRequest = opts => {
|
|
65
68
|
madeRequest = true;
|
|
66
69
|
const handler = response => {
|
|
67
|
-
if (revalidate) {
|
|
70
|
+
if (revalidate && !opts.forceRefresh) {
|
|
71
|
+
response.status = response.statusCode;
|
|
68
72
|
const revalidatedPolicy = CachePolicy.fromObject(revalidate.cachePolicy).revalidatedPolicy(opts, response);
|
|
69
73
|
if (!revalidatedPolicy.modified) {
|
|
70
74
|
const headers = revalidatedPolicy.policy.responseHeaders();
|
|
@@ -75,7 +79,7 @@ class CacheableRequest {
|
|
|
75
79
|
}
|
|
76
80
|
|
|
77
81
|
if (!response.fromCache) {
|
|
78
|
-
response.cachePolicy = new CachePolicy(opts, response);
|
|
82
|
+
response.cachePolicy = new CachePolicy(opts, response, opts);
|
|
79
83
|
response.fromCache = false;
|
|
80
84
|
}
|
|
81
85
|
|
|
@@ -93,7 +97,12 @@ class CacheableRequest {
|
|
|
93
97
|
statusCode: response.fromCache ? revalidate.statusCode : response.statusCode,
|
|
94
98
|
body
|
|
95
99
|
};
|
|
96
|
-
|
|
100
|
+
|
|
101
|
+
let ttl = opts.strictTtl ? response.cachePolicy.timeToLive() : undefined;
|
|
102
|
+
if (opts.maxTtl) {
|
|
103
|
+
ttl = ttl ? Math.min(ttl, opts.maxTtl) : opts.maxTtl;
|
|
104
|
+
}
|
|
105
|
+
|
|
97
106
|
await this.cache.set(key, value, ttl);
|
|
98
107
|
} catch (err) {
|
|
99
108
|
ee.emit('error', new CacheableRequest.CacheError(err));
|
|
@@ -133,7 +142,7 @@ class CacheableRequest {
|
|
|
133
142
|
}
|
|
134
143
|
|
|
135
144
|
const policy = CachePolicy.fromObject(cacheEntry.cachePolicy);
|
|
136
|
-
if (policy.satisfiesWithoutRevalidation(opts)) {
|
|
145
|
+
if (policy.satisfiesWithoutRevalidation(opts) && !opts.forceRefresh) {
|
|
137
146
|
const headers = policy.responseHeaders();
|
|
138
147
|
const response = new Response(cacheEntry.statusCode, headers, cacheEntry.body, cacheEntry.url);
|
|
139
148
|
response.cachePolicy = policy;
|