cacheable-request 2.1.3 → 4.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/README.md +3 -2
- package/package.json +54 -55
- package/src/index.js +83 -23
package/README.md
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
[](https://travis-ci.org/lukechilds/cacheable-request)
|
|
6
6
|
[](https://coveralls.io/github/lukechilds/cacheable-request?branch=master)
|
|
7
|
+
[](https://www.npmjs.com/package/cacheable-request)
|
|
7
8
|
[](https://www.npmjs.com/package/cacheable-request)
|
|
8
9
|
|
|
9
10
|
[RFC 7234](http://httpwg.org/specs/rfc7234.html) compliant HTTP caching for native Node.js HTTP/HTTPS requests. Caching works out of the box in memory or is easily pluggable with a wide range of storage adapters.
|
|
@@ -28,7 +29,7 @@
|
|
|
28
29
|
## Install
|
|
29
30
|
|
|
30
31
|
```shell
|
|
31
|
-
npm install
|
|
32
|
+
npm install cacheable-request
|
|
32
33
|
```
|
|
33
34
|
|
|
34
35
|
## Usage
|
|
@@ -59,7 +60,7 @@ const cacheableRequest = new CacheableRequest(electron.net);
|
|
|
59
60
|
For example, to use Redis as a cache backend, you just need to install the official Redis Keyv storage adapter:
|
|
60
61
|
|
|
61
62
|
```
|
|
62
|
-
npm install
|
|
63
|
+
npm install @keyv/redis
|
|
63
64
|
```
|
|
64
65
|
|
|
65
66
|
And then you can pass `CacheableRequest` your connection string:
|
package/package.json
CHANGED
|
@@ -1,57 +1,56 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
2
|
+
"name": "cacheable-request",
|
|
3
|
+
"version": "4.0.1",
|
|
4
|
+
"description": "Wrap native HTTP requests with RFC compliant cache support",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": "lukechilds/cacheable-request",
|
|
7
|
+
"author": "Luke Childs <lukechilds123@gmail.com> (http://lukechilds.co.uk)",
|
|
8
|
+
"main": "src/index.js",
|
|
9
|
+
"engines": {
|
|
10
|
+
"node": ">=8"
|
|
11
|
+
},
|
|
12
|
+
"scripts": {
|
|
13
|
+
"test": "xo && nyc ava",
|
|
14
|
+
"coverage": "nyc report --reporter=text-lcov | coveralls"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"src"
|
|
18
|
+
],
|
|
19
|
+
"keywords": [
|
|
20
|
+
"HTTP",
|
|
21
|
+
"HTTPS",
|
|
22
|
+
"cache",
|
|
23
|
+
"caching",
|
|
24
|
+
"layer",
|
|
25
|
+
"cacheable",
|
|
26
|
+
"RFC 7234",
|
|
27
|
+
"RFC",
|
|
28
|
+
"7234",
|
|
29
|
+
"compliant"
|
|
30
|
+
],
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"clone-response": "^1.0.2",
|
|
33
|
+
"get-stream": "^3.0.0",
|
|
34
|
+
"http-cache-semantics": "^4.0.0",
|
|
35
|
+
"keyv": "^3.0.0",
|
|
36
|
+
"lowercase-keys": "^1.0.1",
|
|
37
|
+
"normalize-url": "^3.1.0",
|
|
38
|
+
"responselike": "^1.0.2"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@keyv/sqlite": "^1.2.6",
|
|
42
|
+
"ava": "*",
|
|
43
|
+
"coveralls": "^3.0.0",
|
|
44
|
+
"create-test-server": "2.1.1",
|
|
45
|
+
"delay": "^3.0.0",
|
|
46
|
+
"eslint-config-xo-lukechilds": "^1.0.0",
|
|
47
|
+
"nyc": "^12.0.2",
|
|
48
|
+
"pify": "^3.0.0",
|
|
49
|
+
"sqlite3": "^4.0.1",
|
|
50
|
+
"this": "^1.0.2",
|
|
51
|
+
"xo": "*"
|
|
52
|
+
},
|
|
53
|
+
"xo": {
|
|
54
|
+
"extends": "xo-lukechilds"
|
|
55
|
+
}
|
|
57
56
|
}
|
package/src/index.js
CHANGED
|
@@ -27,21 +27,40 @@ class CacheableRequest {
|
|
|
27
27
|
|
|
28
28
|
createCacheableRequest(request) {
|
|
29
29
|
return (opts, cb) => {
|
|
30
|
+
let url;
|
|
30
31
|
if (typeof opts === 'string') {
|
|
31
|
-
|
|
32
|
+
url = normalizeUrlObject(urlLib.parse(opts));
|
|
33
|
+
opts = {};
|
|
34
|
+
} else if (opts instanceof urlLib.URL) {
|
|
35
|
+
url = normalizeUrlObject(urlLib.parse(opts.toString()));
|
|
36
|
+
opts = {};
|
|
37
|
+
} else {
|
|
38
|
+
const [pathname, ...searchParts] = (opts.path || '').split('?');
|
|
39
|
+
const search = searchParts.length > 0 ?
|
|
40
|
+
`?${searchParts.join('?')}` :
|
|
41
|
+
'';
|
|
42
|
+
url = normalizeUrlObject({ ...opts, pathname, search });
|
|
32
43
|
}
|
|
33
|
-
opts =
|
|
44
|
+
opts = {
|
|
34
45
|
headers: {},
|
|
35
46
|
method: 'GET',
|
|
36
47
|
cache: true,
|
|
37
48
|
strictTtl: false,
|
|
38
|
-
automaticFailover: false
|
|
39
|
-
|
|
49
|
+
automaticFailover: false,
|
|
50
|
+
...opts,
|
|
51
|
+
...urlObjectToRequestOptions(url)
|
|
52
|
+
};
|
|
40
53
|
opts.headers = lowercaseKeys(opts.headers);
|
|
41
54
|
|
|
42
55
|
const ee = new EventEmitter();
|
|
43
|
-
const
|
|
44
|
-
|
|
56
|
+
const normalizedUrlString = normalizeUrl(
|
|
57
|
+
urlLib.format(url),
|
|
58
|
+
{
|
|
59
|
+
stripWWW: false,
|
|
60
|
+
removeTrailingSlash: false
|
|
61
|
+
}
|
|
62
|
+
);
|
|
63
|
+
const key = `${opts.method}:${normalizedUrlString}`;
|
|
45
64
|
let revalidate = false;
|
|
46
65
|
let madeRequest = false;
|
|
47
66
|
|
|
@@ -66,8 +85,11 @@ class CacheableRequest {
|
|
|
66
85
|
let clonedResponse;
|
|
67
86
|
if (opts.cache && response.cachePolicy.storable()) {
|
|
68
87
|
clonedResponse = cloneResponse(response);
|
|
69
|
-
|
|
70
|
-
|
|
88
|
+
|
|
89
|
+
(async () => {
|
|
90
|
+
try {
|
|
91
|
+
const body = await getStream.buffer(response);
|
|
92
|
+
|
|
71
93
|
const value = {
|
|
72
94
|
cachePolicy: response.cachePolicy.toObject(),
|
|
73
95
|
url: response.url,
|
|
@@ -75,12 +97,19 @@ class CacheableRequest {
|
|
|
75
97
|
body
|
|
76
98
|
};
|
|
77
99
|
const ttl = opts.strictTtl ? response.cachePolicy.timeToLive() : undefined;
|
|
78
|
-
|
|
79
|
-
})
|
|
80
|
-
|
|
100
|
+
await this.cache.set(key, value, ttl);
|
|
101
|
+
} catch (err) {
|
|
102
|
+
ee.emit('error', new CacheableRequest.CacheError(err));
|
|
103
|
+
}
|
|
104
|
+
})();
|
|
81
105
|
} else if (opts.cache && revalidate) {
|
|
82
|
-
|
|
83
|
-
|
|
106
|
+
(async () => {
|
|
107
|
+
try {
|
|
108
|
+
await this.cache.delete(key);
|
|
109
|
+
} catch (err) {
|
|
110
|
+
ee.emit('error', new CacheableRequest.CacheError(err));
|
|
111
|
+
}
|
|
112
|
+
})();
|
|
84
113
|
}
|
|
85
114
|
|
|
86
115
|
ee.emit('response', clonedResponse || response);
|
|
@@ -97,9 +126,11 @@ class CacheableRequest {
|
|
|
97
126
|
}
|
|
98
127
|
};
|
|
99
128
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
129
|
+
(async () => {
|
|
130
|
+
const get = async opts => {
|
|
131
|
+
await Promise.resolve();
|
|
132
|
+
|
|
133
|
+
const cacheEntry = opts.cache ? await this.cache.get(key) : undefined;
|
|
103
134
|
if (typeof cacheEntry === 'undefined') {
|
|
104
135
|
return makeRequest(opts);
|
|
105
136
|
}
|
|
@@ -120,22 +151,51 @@ class CacheableRequest {
|
|
|
120
151
|
opts.headers = policy.revalidationHeaders(opts);
|
|
121
152
|
makeRequest(opts);
|
|
122
153
|
}
|
|
123
|
-
}
|
|
154
|
+
};
|
|
124
155
|
|
|
125
|
-
|
|
156
|
+
this.cache.on('error', err => ee.emit('error', new CacheableRequest.CacheError(err)));
|
|
126
157
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
158
|
+
try {
|
|
159
|
+
await get(opts);
|
|
160
|
+
} catch (err) {
|
|
161
|
+
if (opts.automaticFailover && !madeRequest) {
|
|
162
|
+
makeRequest(opts);
|
|
163
|
+
}
|
|
164
|
+
ee.emit('error', new CacheableRequest.CacheError(err));
|
|
130
165
|
}
|
|
131
|
-
|
|
132
|
-
});
|
|
166
|
+
})();
|
|
133
167
|
|
|
134
168
|
return ee;
|
|
135
169
|
};
|
|
136
170
|
}
|
|
137
171
|
}
|
|
138
172
|
|
|
173
|
+
function urlObjectToRequestOptions(url) {
|
|
174
|
+
const options = { ...url };
|
|
175
|
+
options.path = `${url.pathname || '/'}${url.search || ''}`;
|
|
176
|
+
delete options.pathname;
|
|
177
|
+
delete options.search;
|
|
178
|
+
return options;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function normalizeUrlObject(url) {
|
|
182
|
+
// If url was parsed by url.parse or new URL:
|
|
183
|
+
// - hostname will be set
|
|
184
|
+
// - host will be hostname[:port]
|
|
185
|
+
// - port will be set if it was explicit in the parsed string
|
|
186
|
+
// Otherwise, url was from request options:
|
|
187
|
+
// - hostname or host may be set
|
|
188
|
+
// - host shall not have port encoded
|
|
189
|
+
return {
|
|
190
|
+
protocol: url.protocol,
|
|
191
|
+
auth: url.auth,
|
|
192
|
+
hostname: url.hostname || url.host || 'localhost',
|
|
193
|
+
port: url.port,
|
|
194
|
+
pathname: url.pathname,
|
|
195
|
+
search: url.search
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
139
199
|
CacheableRequest.RequestError = class extends Error {
|
|
140
200
|
constructor(err) {
|
|
141
201
|
super(err.message);
|