cacheable-request 4.0.1 → 5.2.1

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.
Files changed (3) hide show
  1. package/README.md +19 -3
  2. package/package.json +9 -9
  3. package/src/index.js +34 -5
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 plus:
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": "4.0.1",
3
+ "version": "5.2.1",
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": "^3.0.0",
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": "^1.2.6",
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": "^3.0.0",
45
+ "delay": "^4.0.0",
46
46
  "eslint-config-xo-lukechilds": "^1.0.0",
47
- "nyc": "^12.0.2",
48
- "pify": "^3.0.0",
49
- "sqlite3": "^4.0.1",
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
@@ -66,8 +66,19 @@ class CacheableRequest {
66
66
 
67
67
  const makeRequest = opts => {
68
68
  madeRequest = true;
69
+ let requestErrored = false;
70
+ let requestErrorCallback;
71
+
72
+ const requestErrorPromise = new Promise(resolve => {
73
+ requestErrorCallback = () => {
74
+ requestErrored = true;
75
+ resolve();
76
+ };
77
+ });
78
+
69
79
  const handler = response => {
70
- if (revalidate) {
80
+ if (revalidate && !opts.forceRefresh) {
81
+ response.status = response.statusCode;
71
82
  const revalidatedPolicy = CachePolicy.fromObject(revalidate.cachePolicy).revalidatedPolicy(opts, response);
72
83
  if (!revalidatedPolicy.modified) {
73
84
  const headers = revalidatedPolicy.policy.responseHeaders();
@@ -78,7 +89,7 @@ class CacheableRequest {
78
89
  }
79
90
 
80
91
  if (!response.fromCache) {
81
- response.cachePolicy = new CachePolicy(opts, response);
92
+ response.cachePolicy = new CachePolicy(opts, response, opts);
82
93
  response.fromCache = false;
83
94
  }
84
95
 
@@ -88,7 +99,18 @@ class CacheableRequest {
88
99
 
89
100
  (async () => {
90
101
  try {
91
- const body = await getStream.buffer(response);
102
+ const bodyPromise = getStream.buffer(response);
103
+
104
+ await Promise.race([
105
+ requestErrorPromise,
106
+ new Promise(resolve => response.once('end', resolve))
107
+ ]);
108
+
109
+ if (requestErrored) {
110
+ return;
111
+ }
112
+
113
+ const body = await bodyPromise;
92
114
 
93
115
  const value = {
94
116
  cachePolicy: response.cachePolicy.toObject(),
@@ -96,7 +118,12 @@ class CacheableRequest {
96
118
  statusCode: response.fromCache ? revalidate.statusCode : response.statusCode,
97
119
  body
98
120
  };
99
- const ttl = opts.strictTtl ? response.cachePolicy.timeToLive() : undefined;
121
+
122
+ let ttl = opts.strictTtl ? response.cachePolicy.timeToLive() : undefined;
123
+ if (opts.maxTtl) {
124
+ ttl = ttl ? Math.min(ttl, opts.maxTtl) : opts.maxTtl;
125
+ }
126
+
100
127
  await this.cache.set(key, value, ttl);
101
128
  } catch (err) {
102
129
  ee.emit('error', new CacheableRequest.CacheError(err));
@@ -120,6 +147,8 @@ class CacheableRequest {
120
147
 
121
148
  try {
122
149
  const req = request(opts, handler);
150
+ req.once('error', requestErrorCallback);
151
+ req.once('abort', requestErrorCallback);
123
152
  ee.emit('request', req);
124
153
  } catch (err) {
125
154
  ee.emit('error', new CacheableRequest.RequestError(err));
@@ -136,7 +165,7 @@ class CacheableRequest {
136
165
  }
137
166
 
138
167
  const policy = CachePolicy.fromObject(cacheEntry.cachePolicy);
139
- if (policy.satisfiesWithoutRevalidation(opts)) {
168
+ if (policy.satisfiesWithoutRevalidation(opts) && !opts.forceRefresh) {
140
169
  const headers = policy.responseHeaders();
141
170
  const response = new Response(cacheEntry.statusCode, headers, cacheEntry.body, cacheEntry.url);
142
171
  response.cachePolicy = policy;