cacheable-request 5.2.1 → 7.0.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.
- package/package.json +10 -10
- package/src/index.js +26 -19
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cacheable-request",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "7.0.1",
|
|
4
4
|
"description": "Wrap native HTTP requests with RFC compliant cache support",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "lukechilds/cacheable-request",
|
|
@@ -30,25 +30,25 @@
|
|
|
30
30
|
],
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"clone-response": "^1.0.2",
|
|
33
|
-
"get-stream": "^
|
|
33
|
+
"get-stream": "^5.1.0",
|
|
34
34
|
"http-cache-semantics": "^4.0.0",
|
|
35
|
-
"keyv": "^
|
|
36
|
-
"lowercase-keys": "^
|
|
37
|
-
"normalize-url": "^
|
|
38
|
-
"responselike": "^
|
|
35
|
+
"keyv": "^4.0.0",
|
|
36
|
+
"lowercase-keys": "^2.0.0",
|
|
37
|
+
"normalize-url": "^4.1.0",
|
|
38
|
+
"responselike": "^2.0.0"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@keyv/sqlite": "^2.0.0",
|
|
42
|
-
"ava": "^
|
|
42
|
+
"ava": "^1.1.0",
|
|
43
43
|
"coveralls": "^3.0.0",
|
|
44
|
-
"create-test-server": "
|
|
44
|
+
"create-test-server": "3.0.0",
|
|
45
45
|
"delay": "^4.0.0",
|
|
46
46
|
"eslint-config-xo-lukechilds": "^1.0.0",
|
|
47
|
-
"nyc": "^
|
|
47
|
+
"nyc": "^14.1.1",
|
|
48
48
|
"pify": "^4.0.0",
|
|
49
49
|
"sqlite3": "^4.0.2",
|
|
50
50
|
"this": "^1.0.2",
|
|
51
|
-
"xo": "^0.
|
|
51
|
+
"xo": "^0.23.0"
|
|
52
52
|
},
|
|
53
53
|
"xo": {
|
|
54
54
|
"extends": "xo-lukechilds"
|
package/src/index.js
CHANGED
|
@@ -41,6 +41,7 @@ class CacheableRequest {
|
|
|
41
41
|
'';
|
|
42
42
|
url = normalizeUrlObject({ ...opts, pathname, search });
|
|
43
43
|
}
|
|
44
|
+
|
|
44
45
|
opts = {
|
|
45
46
|
headers: {},
|
|
46
47
|
method: 'GET',
|
|
@@ -57,7 +58,8 @@ class CacheableRequest {
|
|
|
57
58
|
urlLib.format(url),
|
|
58
59
|
{
|
|
59
60
|
stripWWW: false,
|
|
60
|
-
removeTrailingSlash: false
|
|
61
|
+
removeTrailingSlash: false,
|
|
62
|
+
stripAuthentication: false
|
|
61
63
|
}
|
|
62
64
|
);
|
|
63
65
|
const key = `${opts.method}:${normalizedUrlString}`;
|
|
@@ -71,8 +73,10 @@ class CacheableRequest {
|
|
|
71
73
|
|
|
72
74
|
const requestErrorPromise = new Promise(resolve => {
|
|
73
75
|
requestErrorCallback = () => {
|
|
74
|
-
requestErrored
|
|
75
|
-
|
|
76
|
+
if (!requestErrored) {
|
|
77
|
+
requestErrored = true;
|
|
78
|
+
resolve();
|
|
79
|
+
}
|
|
76
80
|
};
|
|
77
81
|
});
|
|
78
82
|
|
|
@@ -82,7 +86,7 @@ class CacheableRequest {
|
|
|
82
86
|
const revalidatedPolicy = CachePolicy.fromObject(revalidate.cachePolicy).revalidatedPolicy(opts, response);
|
|
83
87
|
if (!revalidatedPolicy.modified) {
|
|
84
88
|
const headers = revalidatedPolicy.policy.responseHeaders();
|
|
85
|
-
response = new Response(
|
|
89
|
+
response = new Response(revalidate.statusCode, headers, revalidate.body, revalidate.url);
|
|
86
90
|
response.cachePolicy = revalidatedPolicy.policy;
|
|
87
91
|
response.fromCache = true;
|
|
88
92
|
}
|
|
@@ -125,16 +129,16 @@ class CacheableRequest {
|
|
|
125
129
|
}
|
|
126
130
|
|
|
127
131
|
await this.cache.set(key, value, ttl);
|
|
128
|
-
} catch (
|
|
129
|
-
ee.emit('error', new CacheableRequest.CacheError(
|
|
132
|
+
} catch (error) {
|
|
133
|
+
ee.emit('error', new CacheableRequest.CacheError(error));
|
|
130
134
|
}
|
|
131
135
|
})();
|
|
132
136
|
} else if (opts.cache && revalidate) {
|
|
133
137
|
(async () => {
|
|
134
138
|
try {
|
|
135
139
|
await this.cache.delete(key);
|
|
136
|
-
} catch (
|
|
137
|
-
ee.emit('error', new CacheableRequest.CacheError(
|
|
140
|
+
} catch (error) {
|
|
141
|
+
ee.emit('error', new CacheableRequest.CacheError(error));
|
|
138
142
|
}
|
|
139
143
|
})();
|
|
140
144
|
}
|
|
@@ -150,8 +154,8 @@ class CacheableRequest {
|
|
|
150
154
|
req.once('error', requestErrorCallback);
|
|
151
155
|
req.once('abort', requestErrorCallback);
|
|
152
156
|
ee.emit('request', req);
|
|
153
|
-
} catch (
|
|
154
|
-
ee.emit('error', new CacheableRequest.RequestError(
|
|
157
|
+
} catch (error) {
|
|
158
|
+
ee.emit('error', new CacheableRequest.RequestError(error));
|
|
155
159
|
}
|
|
156
160
|
};
|
|
157
161
|
|
|
@@ -182,15 +186,18 @@ class CacheableRequest {
|
|
|
182
186
|
}
|
|
183
187
|
};
|
|
184
188
|
|
|
185
|
-
|
|
189
|
+
const errorHandler = error => ee.emit('error', new CacheableRequest.CacheError(error));
|
|
190
|
+
this.cache.once('error', errorHandler);
|
|
191
|
+
ee.on('response', () => this.cache.removeListener('error', errorHandler));
|
|
186
192
|
|
|
187
193
|
try {
|
|
188
194
|
await get(opts);
|
|
189
|
-
} catch (
|
|
195
|
+
} catch (error) {
|
|
190
196
|
if (opts.automaticFailover && !madeRequest) {
|
|
191
197
|
makeRequest(opts);
|
|
192
198
|
}
|
|
193
|
-
|
|
199
|
+
|
|
200
|
+
ee.emit('error', new CacheableRequest.CacheError(error));
|
|
194
201
|
}
|
|
195
202
|
})();
|
|
196
203
|
|
|
@@ -226,18 +233,18 @@ function normalizeUrlObject(url) {
|
|
|
226
233
|
}
|
|
227
234
|
|
|
228
235
|
CacheableRequest.RequestError = class extends Error {
|
|
229
|
-
constructor(
|
|
230
|
-
super(
|
|
236
|
+
constructor(error) {
|
|
237
|
+
super(error.message);
|
|
231
238
|
this.name = 'RequestError';
|
|
232
|
-
Object.assign(this,
|
|
239
|
+
Object.assign(this, error);
|
|
233
240
|
}
|
|
234
241
|
};
|
|
235
242
|
|
|
236
243
|
CacheableRequest.CacheError = class extends Error {
|
|
237
|
-
constructor(
|
|
238
|
-
super(
|
|
244
|
+
constructor(error) {
|
|
245
|
+
super(error.message);
|
|
239
246
|
this.name = 'CacheError';
|
|
240
|
-
Object.assign(this,
|
|
247
|
+
Object.assign(this, error);
|
|
241
248
|
}
|
|
242
249
|
};
|
|
243
250
|
|