cacheable-request 7.0.2 → 7.0.3
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/LICENSE +2 -1
- package/README.md +12 -10
- package/dist/index.d.ts +28 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +271 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +106 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +19 -0
- package/dist/types.js.map +1 -0
- package/package.json +77 -26
- package/src/index.js +0 -251
package/LICENSE
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2017 Luke Childs
|
|
3
|
+
Copyright (c) 2017-2021 Luke Childs
|
|
4
|
+
Copyright (c) 2022 Jared Wray
|
|
4
5
|
|
|
5
6
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
7
|
of this software and associated documentation files (the "Software"), to deal
|
package/README.md
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
> Wrap native HTTP requests with RFC compliant cache support
|
|
4
4
|
|
|
5
|
-
[](https://github.com/jaredwray/cacheable-request/actions/workflows/tests.yaml)
|
|
6
|
+
[](https://codecov.io/gh/jaredwray/cacheable-request)
|
|
7
7
|
[](https://www.npmjs.com/package/cacheable-request)
|
|
8
8
|
[](https://www.npmjs.com/package/cacheable-request)
|
|
9
9
|
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
- Updates `Age` header on cached responses
|
|
21
21
|
- Can completely bypass cache on a per request basis
|
|
22
22
|
- In memory cache by default
|
|
23
|
-
- Official support for Redis, MongoDB, SQLite, PostgreSQL and MySQL storage adapters
|
|
23
|
+
- Official support for Redis, Memcache, Etcd, MongoDB, SQLite, PostgreSQL and MySQL storage adapters
|
|
24
24
|
- Easily plug in your own or third-party storage adapters
|
|
25
25
|
- If DB connection fails, cache is automatically bypassed ([disabled by default](#optsautomaticfailover))
|
|
26
26
|
- Adds cache support to any existing HTTP code with minimal changes
|
|
@@ -35,8 +35,8 @@ npm install cacheable-request
|
|
|
35
35
|
## Usage
|
|
36
36
|
|
|
37
37
|
```js
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
import http from 'http';
|
|
39
|
+
import CacheableRequest from 'cacheable-request';
|
|
40
40
|
|
|
41
41
|
// Then instead of
|
|
42
42
|
const req = http.request('http://example.com', cb);
|
|
@@ -55,7 +55,7 @@ const cacheableRequest = new CacheableRequest(electron.net);
|
|
|
55
55
|
|
|
56
56
|
## Storage Adapters
|
|
57
57
|
|
|
58
|
-
`cacheable-request` uses [Keyv](https://github.com/
|
|
58
|
+
`cacheable-request` uses [Keyv](https://github.com/jaredwray/keyv) to support a wide range of storage adapters.
|
|
59
59
|
|
|
60
60
|
For example, to use Redis as a cache backend, you just need to install the official Redis Keyv storage adapter:
|
|
61
61
|
|
|
@@ -69,7 +69,7 @@ And then you can pass `CacheableRequest` your connection string:
|
|
|
69
69
|
const cacheableRequest = new CacheableRequest(http.request, 'redis://user:pass@localhost:6379');
|
|
70
70
|
```
|
|
71
71
|
|
|
72
|
-
[View all official Keyv storage adapters.](https://github.com/
|
|
72
|
+
[View all official Keyv storage adapters.](https://github.com/jaredwray/keyv#official-storage-adapters)
|
|
73
73
|
|
|
74
74
|
Keyv also supports anything that follows the Map API so it's easy to write your own storage adapter or use a third-party solution.
|
|
75
75
|
|
|
@@ -86,7 +86,7 @@ const storageAdapter = new QuickLRU({ maxSize: 1000 });
|
|
|
86
86
|
const cacheableRequest = new CacheableRequest(http.request, storageAdapter);
|
|
87
87
|
```
|
|
88
88
|
|
|
89
|
-
View the [Keyv docs](https://github.com/
|
|
89
|
+
View the [Keyv docs](https://github.com/jaredwray/keyv) for more information on how to use storage adapters.
|
|
90
90
|
|
|
91
91
|
## API
|
|
92
92
|
|
|
@@ -105,7 +105,7 @@ Request function to wrap with cache support. Should be [`http.request`](https://
|
|
|
105
105
|
Type: `Keyv storage adapter`<br>
|
|
106
106
|
Default: `new Map()`
|
|
107
107
|
|
|
108
|
-
A [Keyv](https://github.com/
|
|
108
|
+
A [Keyv](https://github.com/jaredwray/keyv) storage adapter instance, or connection string if using with an official Keyv storage adapter.
|
|
109
109
|
|
|
110
110
|
### Instance
|
|
111
111
|
|
|
@@ -203,4 +203,6 @@ cacheableRequest('example.com', cb)
|
|
|
203
203
|
|
|
204
204
|
## License
|
|
205
205
|
|
|
206
|
-
MIT © Luke Childs
|
|
206
|
+
MIT © Luke Childs 2017-2021
|
|
207
|
+
|
|
208
|
+
MIT © Jared Wray 2022
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import EventEmitter from 'node:events';
|
|
4
|
+
declare const CacheableRequest: {
|
|
5
|
+
(request: Function, cacheAdapter?: any): (options: any, cb?: ((response: {}) => void) | undefined) => EventEmitter;
|
|
6
|
+
RequestError: {
|
|
7
|
+
new (error: any): {
|
|
8
|
+
name: string;
|
|
9
|
+
message: string;
|
|
10
|
+
stack?: string | undefined;
|
|
11
|
+
};
|
|
12
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function | undefined): void;
|
|
13
|
+
prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined;
|
|
14
|
+
stackTraceLimit: number;
|
|
15
|
+
};
|
|
16
|
+
CacheError: {
|
|
17
|
+
new (error: any): {
|
|
18
|
+
name: string;
|
|
19
|
+
message: string;
|
|
20
|
+
stack?: string | undefined;
|
|
21
|
+
};
|
|
22
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function | undefined): void;
|
|
23
|
+
prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined;
|
|
24
|
+
stackTraceLimit: number;
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
export default CacheableRequest;
|
|
28
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA,OAAO,YAAY,MAAM,aAAa,CAAC;AAYvC,QAAA,MAAM,gBAAgB;cAAsB,QAAQ,iBAAiB,GAAG,aAgBtD,GAAG,mBAAkB,EAAE,KAAK,IAAI;;oBAuN9B,GAAG;;;;;;;;;;oBAOH,GAAG;;;;;;;;;CAjOtB,CAAC;AAwOF,eAAe,gBAAgB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
const node_events_1 = __importDefault(require("node:events"));
|
|
30
|
+
const node_url_1 = __importDefault(require("node:url"));
|
|
31
|
+
const node_crypto_1 = __importDefault(require("node:crypto"));
|
|
32
|
+
const node_stream_1 = __importStar(require("node:stream"));
|
|
33
|
+
const normalize_url_1 = __importDefault(require("normalize-url"));
|
|
34
|
+
const get_stream_1 = __importDefault(require("get-stream"));
|
|
35
|
+
const http_cache_semantics_1 = __importDefault(require("http-cache-semantics"));
|
|
36
|
+
const responselike_1 = __importDefault(require("responselike"));
|
|
37
|
+
const keyv_1 = __importDefault(require("keyv"));
|
|
38
|
+
const mimic_response_1 = __importDefault(require("mimic-response"));
|
|
39
|
+
const { Readable } = node_stream_1.default;
|
|
40
|
+
const CacheableRequest = function (request, cacheAdapter) {
|
|
41
|
+
let cache = {};
|
|
42
|
+
if (cacheAdapter instanceof keyv_1.default) {
|
|
43
|
+
cache = cacheAdapter;
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
cache = new keyv_1.default({
|
|
47
|
+
uri: (typeof cacheAdapter === 'string' && cacheAdapter) || '',
|
|
48
|
+
store: typeof cacheAdapter !== 'string' && cacheAdapter,
|
|
49
|
+
namespace: 'cacheable-request',
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
return createCacheableRequest(request, cache);
|
|
53
|
+
};
|
|
54
|
+
function createCacheableRequest(request, cache) {
|
|
55
|
+
return (options, cb) => {
|
|
56
|
+
let url;
|
|
57
|
+
if (typeof options === 'string') {
|
|
58
|
+
url = normalizeUrlObject(node_url_1.default.parse(options));
|
|
59
|
+
options = {};
|
|
60
|
+
}
|
|
61
|
+
else if (options instanceof node_url_1.default.URL) {
|
|
62
|
+
url = normalizeUrlObject(node_url_1.default.parse(options.toString()));
|
|
63
|
+
options = {};
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
const [pathname, ...searchParts] = (options.path || '').split('?');
|
|
67
|
+
const search = searchParts.length > 0
|
|
68
|
+
? `?${searchParts.join('?')}`
|
|
69
|
+
: '';
|
|
70
|
+
url = normalizeUrlObject({ ...options, pathname, search });
|
|
71
|
+
}
|
|
72
|
+
options = {
|
|
73
|
+
headers: {},
|
|
74
|
+
method: 'GET',
|
|
75
|
+
cache: true,
|
|
76
|
+
strictTtl: false,
|
|
77
|
+
automaticFailover: false,
|
|
78
|
+
...options,
|
|
79
|
+
...urlObjectToRequestOptions(url),
|
|
80
|
+
};
|
|
81
|
+
options.headers = Object.fromEntries(Object.entries(options.headers).map(([key, value]) => [key.toLowerCase(), value]));
|
|
82
|
+
const ee = new node_events_1.default();
|
|
83
|
+
const normalizedUrlString = (0, normalize_url_1.default)(node_url_1.default.format(url), {
|
|
84
|
+
stripWWW: false,
|
|
85
|
+
removeTrailingSlash: false,
|
|
86
|
+
stripAuthentication: false,
|
|
87
|
+
});
|
|
88
|
+
let key = `${options.method}:${normalizedUrlString}`;
|
|
89
|
+
// POST, PATCH, and PUT requests may be cached, depending on the response
|
|
90
|
+
// cache-control headers. As a result, the body of the request should be
|
|
91
|
+
// added to the cache key in order to avoid collisions.
|
|
92
|
+
if (options.body && ['POST', 'PATCH', 'PUT'].includes(options.method)) {
|
|
93
|
+
if (options.body instanceof Readable) {
|
|
94
|
+
// Streamed bodies should completely skip the cache because they may
|
|
95
|
+
// or may not be hashable and in either case the stream would need to
|
|
96
|
+
// close before the cache key could be generated.
|
|
97
|
+
options.cache = false;
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
key += `:${node_crypto_1.default.createHash('md5').update(options.body).digest('hex')}`;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
let revalidate = false;
|
|
104
|
+
let madeRequest = false;
|
|
105
|
+
const makeRequest = (options_) => {
|
|
106
|
+
madeRequest = true;
|
|
107
|
+
let requestErrored = false;
|
|
108
|
+
let requestErrorCallback;
|
|
109
|
+
const requestErrorPromise = new Promise(resolve => {
|
|
110
|
+
requestErrorCallback = () => {
|
|
111
|
+
if (!requestErrored) {
|
|
112
|
+
requestErrored = true;
|
|
113
|
+
resolve();
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
});
|
|
117
|
+
const handler = (response) => {
|
|
118
|
+
if (revalidate && !options_.forceRefresh) {
|
|
119
|
+
response.status = response.statusCode;
|
|
120
|
+
const revalidatedPolicy = http_cache_semantics_1.default.fromObject(revalidate.cachePolicy).revalidatedPolicy(options_, response);
|
|
121
|
+
if (!revalidatedPolicy.modified) {
|
|
122
|
+
const headers = revalidatedPolicy.policy.responseHeaders();
|
|
123
|
+
response = new responselike_1.default(revalidate.statusCode, headers, revalidate.body, revalidate.url);
|
|
124
|
+
response.cachePolicy = revalidatedPolicy.policy;
|
|
125
|
+
response.fromCache = true;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (!response.fromCache) {
|
|
129
|
+
response.cachePolicy = new http_cache_semantics_1.default(options_, response, options_);
|
|
130
|
+
response.fromCache = false;
|
|
131
|
+
}
|
|
132
|
+
let clonedResponse;
|
|
133
|
+
if (options_.cache && response.cachePolicy.storable()) {
|
|
134
|
+
clonedResponse = cloneResponse(response);
|
|
135
|
+
(async () => {
|
|
136
|
+
try {
|
|
137
|
+
const bodyPromise = get_stream_1.default.buffer(response);
|
|
138
|
+
await Promise.race([
|
|
139
|
+
requestErrorPromise,
|
|
140
|
+
new Promise(resolve => response.once('end', resolve)), // eslint-disable-line no-promise-executor-return
|
|
141
|
+
]);
|
|
142
|
+
const body = await bodyPromise;
|
|
143
|
+
const value = {
|
|
144
|
+
cachePolicy: response.cachePolicy.toObject(),
|
|
145
|
+
url: response.url,
|
|
146
|
+
statusCode: response.fromCache ? revalidate.statusCode : response.statusCode,
|
|
147
|
+
body,
|
|
148
|
+
};
|
|
149
|
+
let ttl = options_.strictTtl ? response.cachePolicy.timeToLive() : undefined;
|
|
150
|
+
if (options_.maxTtl) {
|
|
151
|
+
ttl = ttl ? Math.min(ttl, options_.maxTtl) : options_.maxTtl;
|
|
152
|
+
}
|
|
153
|
+
await cache.set(key, value, ttl);
|
|
154
|
+
}
|
|
155
|
+
catch (error) {
|
|
156
|
+
ee.emit('error', new CacheableRequest.CacheError(error));
|
|
157
|
+
}
|
|
158
|
+
})();
|
|
159
|
+
}
|
|
160
|
+
else if (options_.cache && revalidate) {
|
|
161
|
+
(async () => {
|
|
162
|
+
try {
|
|
163
|
+
await cache.delete(key);
|
|
164
|
+
}
|
|
165
|
+
catch (error) {
|
|
166
|
+
ee.emit('error', new CacheableRequest.CacheError(error));
|
|
167
|
+
}
|
|
168
|
+
})();
|
|
169
|
+
}
|
|
170
|
+
ee.emit('response', clonedResponse || response);
|
|
171
|
+
if (typeof cb === 'function') {
|
|
172
|
+
cb(clonedResponse || response);
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
try {
|
|
176
|
+
const request_ = request(options_, handler);
|
|
177
|
+
request_.once('error', requestErrorCallback);
|
|
178
|
+
request_.once('abort', requestErrorCallback);
|
|
179
|
+
ee.emit('request', request_);
|
|
180
|
+
}
|
|
181
|
+
catch (error) {
|
|
182
|
+
ee.emit('error', new CacheableRequest.RequestError(error));
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
(async () => {
|
|
186
|
+
const get = async (options_) => {
|
|
187
|
+
await Promise.resolve();
|
|
188
|
+
const cacheEntry = options_.cache ? await cache.get(key) : undefined;
|
|
189
|
+
if (typeof cacheEntry === 'undefined') {
|
|
190
|
+
makeRequest(options_);
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
const policy = http_cache_semantics_1.default.fromObject(cacheEntry.cachePolicy);
|
|
194
|
+
if (policy.satisfiesWithoutRevalidation(options_) && !options_.forceRefresh) {
|
|
195
|
+
const headers = policy.responseHeaders();
|
|
196
|
+
const response = new responselike_1.default(cacheEntry.statusCode, headers, cacheEntry.body, cacheEntry.url);
|
|
197
|
+
response.cachePolicy = policy;
|
|
198
|
+
response.fromCache = true;
|
|
199
|
+
ee.emit('response', response);
|
|
200
|
+
if (typeof cb === 'function') {
|
|
201
|
+
cb(response);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
revalidate = cacheEntry;
|
|
206
|
+
options_.headers = policy.revalidationHeaders(options_);
|
|
207
|
+
makeRequest(options_);
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
const errorHandler = (error) => ee.emit('error', new CacheableRequest.CacheError(error));
|
|
211
|
+
cache.once?.('error', errorHandler);
|
|
212
|
+
ee.on('error', () => cache.removeListener?.('error', errorHandler));
|
|
213
|
+
ee.on('response', () => cache.removeListener?.('error', errorHandler));
|
|
214
|
+
try {
|
|
215
|
+
await get(options);
|
|
216
|
+
}
|
|
217
|
+
catch (error) {
|
|
218
|
+
if (options.automaticFailover && !madeRequest) {
|
|
219
|
+
makeRequest(options);
|
|
220
|
+
}
|
|
221
|
+
ee.emit('error', new CacheableRequest.CacheError(error));
|
|
222
|
+
}
|
|
223
|
+
})();
|
|
224
|
+
return ee;
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
function cloneResponse(response) {
|
|
228
|
+
const clone = new node_stream_1.PassThrough({ autoDestroy: false });
|
|
229
|
+
(0, mimic_response_1.default)(response, clone);
|
|
230
|
+
return response.pipe(clone);
|
|
231
|
+
}
|
|
232
|
+
function urlObjectToRequestOptions(url) {
|
|
233
|
+
const options = { ...url };
|
|
234
|
+
options.path = `${url.pathname || '/'}${url.search || ''}`;
|
|
235
|
+
delete options.pathname;
|
|
236
|
+
delete options.search;
|
|
237
|
+
return options;
|
|
238
|
+
}
|
|
239
|
+
function normalizeUrlObject(url) {
|
|
240
|
+
// If url was parsed by url.parse or new URL:
|
|
241
|
+
// - hostname will be set
|
|
242
|
+
// - host will be hostname[:port]
|
|
243
|
+
// - port will be set if it was explicit in the parsed string
|
|
244
|
+
// Otherwise, url was from request options:
|
|
245
|
+
// - hostname or host may be set
|
|
246
|
+
// - host shall not have port encoded
|
|
247
|
+
return {
|
|
248
|
+
protocol: url.protocol,
|
|
249
|
+
auth: url.auth,
|
|
250
|
+
hostname: url.hostname || url.host || 'localhost',
|
|
251
|
+
port: url.port,
|
|
252
|
+
pathname: url.pathname,
|
|
253
|
+
search: url.search,
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
CacheableRequest.RequestError = class extends Error {
|
|
257
|
+
constructor(error) {
|
|
258
|
+
super(error.message);
|
|
259
|
+
this.name = 'RequestError';
|
|
260
|
+
Object.assign(this, error);
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
CacheableRequest.CacheError = class extends Error {
|
|
264
|
+
constructor(error) {
|
|
265
|
+
super(error.message);
|
|
266
|
+
this.name = 'CacheError';
|
|
267
|
+
Object.assign(this, error);
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
exports.default = CacheableRequest;
|
|
271
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,8DAAuC;AACvC,wDAA8B;AAC9B,8DAAiC;AACjC,2DAAuE;AACvE,kEAAyC;AACzC,4DAAmC;AACnC,gFAA+C;AAC/C,gEAAoC;AACpC,gDAAwB;AACxB,oEAA2C;AAE3C,MAAM,EAAE,QAAQ,EAAE,GAAG,qBAAM,CAAC;AAC5B,MAAM,gBAAgB,GAAG,UAAU,OAAiB,EAAE,YAAkB;IACvE,IAAI,KAAK,GAAQ,EAAE,CAAC;IACpB,IAAI,YAAY,YAAY,cAAI,EAAE;QACjC,KAAK,GAAG,YAAY,CAAC;KACrB;SAAM;QACN,KAAK,GAAG,IAAI,cAAI,CAAC;YAChB,GAAG,EAAE,CAAC,OAAO,YAAY,KAAK,QAAQ,IAAI,YAAY,CAAC,IAAI,EA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AAE,GAAG,CAAC,IAAI;QACd,QAAQ,EAAE,GAAG,CAAC,QAAQ,IAAI,GAAG,CAAC,IAAI,IAAI,WAAW;QACjD,IAAI,EAAE,GAAG,CAAC,IAAI;QACd,QAAQ,EAAE,GAAG,CAAC,QAAQ;QACtB,MAAM,EAAE,GAAG,CAAC,MAAM;KAClB,CAAC;AACH,CAAC;AAED,gBAAgB,CAAC,YAAY,GAAG,KAAM,SAAQ,KAAK;IAClD,YAAY,KAAU;QACrB,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QACrB,IAAI,CAAC,IAAI,GAAG,cAAc,CAAC;QAC3B,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;IAC5B,CAAC;CACD,CAAC;AACF,gBAAgB,CAAC,UAAU,GAAG,KAAM,SAAQ,KAAK;IAChD,YAAY,KAAU;QACrB,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QACrB,IAAI,CAAC,IAAI,GAAG,YAAY,CAAC;QACzB,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;IAC5B,CAAC;CACD,CAAC;AAEF,kBAAe,gBAAgB,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
3
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
4
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
5
|
+
import { request, RequestOptions, ClientRequest, ServerResponse } from 'node:http';
|
|
6
|
+
import { URL } from 'node:url';
|
|
7
|
+
import { EventEmitter } from 'node:events';
|
|
8
|
+
import { Buffer } from 'node:buffer';
|
|
9
|
+
import { Store } from 'keyv';
|
|
10
|
+
import ResponseLike from 'responselike';
|
|
11
|
+
import { CachePolicyObject } from 'http-cache-semantics';
|
|
12
|
+
export type RequestFn = typeof request;
|
|
13
|
+
export type RequestFunction = typeof request;
|
|
14
|
+
export type CacheResponse = ServerResponse | typeof ResponseLike;
|
|
15
|
+
export type CacheableRequestFunction = (options: CacheableOptions, cb?: (response: CacheResponse) => void) => Emitter;
|
|
16
|
+
export type CacheableOptions = Options & RequestOptions | string | URL;
|
|
17
|
+
export type StorageAdapter = Store<any>;
|
|
18
|
+
export interface Options {
|
|
19
|
+
/**
|
|
20
|
+
* If the cache should be used. Setting this to `false` will completely bypass the cache for the current request.
|
|
21
|
+
* @default true
|
|
22
|
+
*/
|
|
23
|
+
cache?: boolean | undefined;
|
|
24
|
+
/**
|
|
25
|
+
* If set to `true` once a cached resource has expired it is deleted and will have to be re-requested.
|
|
26
|
+
*
|
|
27
|
+
* If set to `false`, after a cached resource's TTL expires it is kept in the cache and will be revalidated
|
|
28
|
+
* on the next request with `If-None-Match`/`If-Modified-Since` headers.
|
|
29
|
+
* @default false
|
|
30
|
+
*/
|
|
31
|
+
strictTtl?: boolean | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* Limits TTL. The `number` represents milliseconds.
|
|
34
|
+
* @default undefined
|
|
35
|
+
*/
|
|
36
|
+
maxTtl?: number | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* When set to `true`, if the DB connection fails we will automatically fallback to a network request.
|
|
39
|
+
* DB errors will still be emitted to notify you of the problem even though the request callback may succeed.
|
|
40
|
+
* @default false
|
|
41
|
+
*/
|
|
42
|
+
automaticFailover?: boolean | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* Forces refreshing the cache. If the response could be retrieved from the cache, it will perform a
|
|
45
|
+
* new request and override the cache instead.
|
|
46
|
+
* @default false
|
|
47
|
+
*/
|
|
48
|
+
forceRefresh?: boolean | undefined;
|
|
49
|
+
remoteAddress?: boolean | undefined;
|
|
50
|
+
url?: string | undefined;
|
|
51
|
+
headers?: Record<string, string | string[] | undefined>;
|
|
52
|
+
body?: Buffer;
|
|
53
|
+
}
|
|
54
|
+
export interface CacheValue extends Record<string, any> {
|
|
55
|
+
url: string;
|
|
56
|
+
statusCode: number;
|
|
57
|
+
body: Buffer | string;
|
|
58
|
+
cachePolicy: CachePolicyObject;
|
|
59
|
+
}
|
|
60
|
+
export interface Emitter extends EventEmitter {
|
|
61
|
+
addListener(event: 'request', listener: (request: ClientRequest) => void): this;
|
|
62
|
+
addListener(event: 'response', listener: (response: CacheResponse) => void): this;
|
|
63
|
+
addListener(event: 'error', listener: (error: RequestError | CacheError) => void): this;
|
|
64
|
+
on(event: 'request', listener: (request: ClientRequest) => void): this;
|
|
65
|
+
on(event: 'response', listener: (response: CacheResponse) => void): this;
|
|
66
|
+
on(event: 'error', listener: (error: RequestError | CacheError) => void): this;
|
|
67
|
+
once(event: 'request', listener: (request: ClientRequest) => void): this;
|
|
68
|
+
once(event: 'response', listener: (response: CacheResponse) => void): this;
|
|
69
|
+
once(event: 'error', listener: (error: RequestError | CacheError) => void): this;
|
|
70
|
+
prependListener(event: 'request', listener: (request: ClientRequest) => void): this;
|
|
71
|
+
prependListener(event: 'response', listener: (response: CacheResponse) => void): this;
|
|
72
|
+
prependListener(event: 'error', listener: (error: RequestError | CacheError) => void): this;
|
|
73
|
+
prependOnceListener(event: 'request', listener: (request: ClientRequest) => void): this;
|
|
74
|
+
prependOnceListener(event: 'response', listener: (response: CacheResponse) => void): this;
|
|
75
|
+
prependOnceListener(event: 'error', listener: (error: RequestError | CacheError) => void): this;
|
|
76
|
+
removeListener(event: 'request', listener: (request: ClientRequest) => void): this;
|
|
77
|
+
removeListener(event: 'response', listener: (response: CacheResponse) => void): this;
|
|
78
|
+
removeListener(event: 'error', listener: (error: RequestError | CacheError) => void): this;
|
|
79
|
+
off(event: 'request', listener: (request: ClientRequest) => void): this;
|
|
80
|
+
off(event: 'response', listener: (response: CacheResponse) => void): this;
|
|
81
|
+
off(event: 'error', listener: (error: RequestError | CacheError) => void): this;
|
|
82
|
+
removeAllListeners(event?: 'request' | 'response' | 'error'): this;
|
|
83
|
+
listeners(event: 'request'): Array<(request: ClientRequest) => void>;
|
|
84
|
+
listeners(event: 'response'): Array<(response: CacheResponse) => void>;
|
|
85
|
+
listeners(event: 'error'): Array<(error: RequestError | CacheError) => void>;
|
|
86
|
+
rawListeners(event: 'request'): Array<(request: ClientRequest) => void>;
|
|
87
|
+
rawListeners(event: 'response'): Array<(response: CacheResponse) => void>;
|
|
88
|
+
rawListeners(event: 'error'): Array<(error: RequestError | CacheError) => void>;
|
|
89
|
+
emit(event: 'request', request: ClientRequest): boolean;
|
|
90
|
+
emit(event: 'response', response: CacheResponse): boolean;
|
|
91
|
+
emit(event: 'error', error: RequestError | CacheError): boolean;
|
|
92
|
+
eventNames(): Array<'request' | 'response' | 'error'>;
|
|
93
|
+
listenerCount(type: 'request' | 'response' | 'error'): number;
|
|
94
|
+
}
|
|
95
|
+
export declare class RequestError extends Error {
|
|
96
|
+
constructor(error: Error);
|
|
97
|
+
}
|
|
98
|
+
export declare class CacheError extends Error {
|
|
99
|
+
constructor(error: Error);
|
|
100
|
+
}
|
|
101
|
+
export interface UrlOption {
|
|
102
|
+
path: string;
|
|
103
|
+
pathname?: string;
|
|
104
|
+
search?: string;
|
|
105
|
+
}
|
|
106
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;;;AASA,OAAO,EAAC,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,cAAc,EAAC,MAAM,WAAW,CAAC;AACjF,OAAO,EAAC,GAAG,EAAC,MAAM,UAAU,CAAC;AAC7B,OAAO,EAAC,YAAY,EAAC,MAAM,aAAa,CAAC;AACzC,OAAO,EAAC,MAAM,EAAC,MAAM,aAAa,CAAC;AACnC,OAAO,EAAC,KAAK,EAAC,MAAM,MAAM,CAAC;AAC3B,OAAO,YAAY,MAAM,cAAc,CAAC;AACxC,OAAO,EAAC,iBAAiB,EAAC,MAAM,sBAAsB,CAAC;AAEvD,MAAM,MAAM,SA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zB,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,CAAC;CAChB"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// Type definitions for cacheable-request 6.0
|
|
2
|
+
// Project: https://github.com/lukechilds/cacheable-request#readme
|
|
3
|
+
// Definitions by: BendingBender <https://github.com/BendingBender>
|
|
4
|
+
// Paul Melnikow <https://github.com/paulmelnikow>
|
|
5
|
+
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
|
6
|
+
// TypeScript Version: 2.3
|
|
7
|
+
export class RequestError extends Error {
|
|
8
|
+
constructor(error) {
|
|
9
|
+
super(error.message);
|
|
10
|
+
Object.assign(this, error);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export class CacheError extends Error {
|
|
14
|
+
constructor(error) {
|
|
15
|
+
super(error.message);
|
|
16
|
+
Object.assign(this, error);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,6CAA6C;AAC7C,kEAAkE;AAClE,mEAAmE;AACnE,kEAAkE;AAClE,kEAAkE;AAClE,0BAA0B;AA+H1B,MAAM,OAAO,YAAa,SAAQ,KAAK;IACtC,YAAY,KAAY;QACvB,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QACrB,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;IAC5B,CAAC;CACD;AACD,MAAM,OAAO,UAAW,SAAQ,KAAK;IACpC,YAAY,KAAY;QACvB,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QACrB,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;IAC5B,CAAC;CACD"}
|
package/package.json
CHANGED
|
@@ -1,20 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cacheable-request",
|
|
3
|
-
"version": "7.0.
|
|
3
|
+
"version": "7.0.3",
|
|
4
4
|
"description": "Wrap native HTTP requests with RFC compliant cache support",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"repository": "
|
|
7
|
-
"author": "
|
|
8
|
-
"main": "
|
|
6
|
+
"repository": "jaredwray/cacheable-request",
|
|
7
|
+
"author": "Jared Wray <me@jaredwray.com> (http://jaredwray.com)",
|
|
8
|
+
"main": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
9
10
|
"engines": {
|
|
10
|
-
"node": ">=
|
|
11
|
+
"node": ">=14.16"
|
|
11
12
|
},
|
|
12
13
|
"scripts": {
|
|
13
|
-
"test": "xo &&
|
|
14
|
-
"
|
|
14
|
+
"test": "xo --fix && jest --coverage",
|
|
15
|
+
"build": "tsc --project tsconfig.build.json",
|
|
16
|
+
"clean": "rm -rf node_modules && rm -rf ./coverage && rm -rf ./package-lock.json && rm -rf ./test/testdb.sqlite && rm -rf ./dist"
|
|
15
17
|
},
|
|
16
18
|
"files": [
|
|
17
|
-
"
|
|
19
|
+
"dist"
|
|
18
20
|
],
|
|
19
21
|
"keywords": [
|
|
20
22
|
"HTTP",
|
|
@@ -29,28 +31,77 @@
|
|
|
29
31
|
"compliant"
|
|
30
32
|
],
|
|
31
33
|
"dependencies": {
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"normalize-url": "^6.0.1",
|
|
34
|
+
"get-stream": "^6.0.1",
|
|
35
|
+
"http-cache-semantics": "^4.1.1",
|
|
36
|
+
"keyv": "^4.5.2",
|
|
37
|
+
"mimic-response": "^3.1.0",
|
|
38
|
+
"normalize-url": "^6.0.3",
|
|
38
39
|
"responselike": "^2.0.0"
|
|
39
40
|
},
|
|
40
41
|
"devDependencies": {
|
|
41
|
-
"@
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"create-test-server": "3.0.
|
|
45
|
-
"delay": "^
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
"
|
|
51
|
-
"
|
|
42
|
+
"@babel/core": "^7.21.8",
|
|
43
|
+
"@babel/preset-env": "^7.21.5",
|
|
44
|
+
"@keyv/sqlite": "^3.6.5",
|
|
45
|
+
"@types/create-test-server": "^3.0.1",
|
|
46
|
+
"@types/delay": "^3.1.0",
|
|
47
|
+
"@types/get-stream": "^3.0.2",
|
|
48
|
+
"@types/http-cache-semantics": "^4.0.1",
|
|
49
|
+
"@types/jest": "^29.5.1",
|
|
50
|
+
"@types/node": "^18.0.0",
|
|
51
|
+
"@types/pify": "^5.0.1",
|
|
52
|
+
"@types/responselike": "^1.0.0",
|
|
53
|
+
"@types/sqlite3": "^3.1.8",
|
|
54
|
+
"create-test-server": "3.0.1",
|
|
55
|
+
"eslint-plugin-jest": "^27.2.1",
|
|
56
|
+
"delay": "^5.0.0",
|
|
57
|
+
"jest": "^29.5.0",
|
|
58
|
+
"pify": "^5.0.0",
|
|
59
|
+
"sqlite3": "^5.1.6",
|
|
60
|
+
"ts-jest": "^29.1.0",
|
|
61
|
+
"ts-node": "^10.9.1",
|
|
62
|
+
"typescript": "^4.7.4",
|
|
63
|
+
"xo": "^0.54.2"
|
|
64
|
+
},
|
|
65
|
+
"jest": {
|
|
66
|
+
"globals": {
|
|
67
|
+
"ts-jest": {
|
|
68
|
+
"tsconfig": "./tsconfig.json"
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
"moduleFileExtensions": [
|
|
72
|
+
"ts",
|
|
73
|
+
"js"
|
|
74
|
+
],
|
|
75
|
+
"transform": {
|
|
76
|
+
"^.+\\.(ts|tsx)$": "ts-jest"
|
|
77
|
+
},
|
|
78
|
+
"testMatch": [
|
|
79
|
+
"**/test/*.test.(ts)"
|
|
80
|
+
],
|
|
81
|
+
"testEnvironment": "node"
|
|
52
82
|
},
|
|
53
83
|
"xo": {
|
|
54
|
-
"
|
|
84
|
+
"plugins": [
|
|
85
|
+
"jest"
|
|
86
|
+
],
|
|
87
|
+
"extends": [
|
|
88
|
+
"plugin:jest/recommended"
|
|
89
|
+
],
|
|
90
|
+
"rules": {
|
|
91
|
+
"@typescript-eslint/object-curly-spacing": 0,
|
|
92
|
+
"@typescript-eslint/naming-convention": 0,
|
|
93
|
+
"@typescript-eslint/no-unsafe-assignment": 0,
|
|
94
|
+
"@typescript-eslint/no-unsafe-call": 0,
|
|
95
|
+
"@typescript-eslint/ban-types": 0,
|
|
96
|
+
"@typescript-eslint/restrict-template-expressions": 0,
|
|
97
|
+
"@typescript-eslint/no-unsafe-return": 0,
|
|
98
|
+
"new-cap": 0,
|
|
99
|
+
"n/prefer-global/url": 0,
|
|
100
|
+
"@typescript-eslint/no-empty-function": 0,
|
|
101
|
+
"@typescript-eslint/restrict-plus-operands": 0,
|
|
102
|
+
"@typescript-eslint/no-floating-promises": 0,
|
|
103
|
+
"@typescript-eslint/await-thenable": 0,
|
|
104
|
+
"@typescript-eslint/no-unsafe-argument": 0
|
|
105
|
+
}
|
|
55
106
|
}
|
|
56
107
|
}
|
package/src/index.js
DELETED
|
@@ -1,251 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const EventEmitter = require('events');
|
|
4
|
-
const urlLib = require('url');
|
|
5
|
-
const normalizeUrl = require('normalize-url');
|
|
6
|
-
const getStream = require('get-stream');
|
|
7
|
-
const CachePolicy = require('http-cache-semantics');
|
|
8
|
-
const Response = require('responselike');
|
|
9
|
-
const lowercaseKeys = require('lowercase-keys');
|
|
10
|
-
const cloneResponse = require('clone-response');
|
|
11
|
-
const Keyv = require('keyv');
|
|
12
|
-
|
|
13
|
-
class CacheableRequest {
|
|
14
|
-
constructor(request, cacheAdapter) {
|
|
15
|
-
if (typeof request !== 'function') {
|
|
16
|
-
throw new TypeError('Parameter `request` must be a function');
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
this.cache = new Keyv({
|
|
20
|
-
uri: typeof cacheAdapter === 'string' && cacheAdapter,
|
|
21
|
-
store: typeof cacheAdapter !== 'string' && cacheAdapter,
|
|
22
|
-
namespace: 'cacheable-request'
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
return this.createCacheableRequest(request);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
createCacheableRequest(request) {
|
|
29
|
-
return (opts, cb) => {
|
|
30
|
-
let url;
|
|
31
|
-
if (typeof opts === 'string') {
|
|
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 });
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
opts = {
|
|
46
|
-
headers: {},
|
|
47
|
-
method: 'GET',
|
|
48
|
-
cache: true,
|
|
49
|
-
strictTtl: false,
|
|
50
|
-
automaticFailover: false,
|
|
51
|
-
...opts,
|
|
52
|
-
...urlObjectToRequestOptions(url)
|
|
53
|
-
};
|
|
54
|
-
opts.headers = lowercaseKeys(opts.headers);
|
|
55
|
-
|
|
56
|
-
const ee = new EventEmitter();
|
|
57
|
-
const normalizedUrlString = normalizeUrl(
|
|
58
|
-
urlLib.format(url),
|
|
59
|
-
{
|
|
60
|
-
stripWWW: false,
|
|
61
|
-
removeTrailingSlash: false,
|
|
62
|
-
stripAuthentication: false
|
|
63
|
-
}
|
|
64
|
-
);
|
|
65
|
-
const key = `${opts.method}:${normalizedUrlString}`;
|
|
66
|
-
let revalidate = false;
|
|
67
|
-
let madeRequest = false;
|
|
68
|
-
|
|
69
|
-
const makeRequest = opts => {
|
|
70
|
-
madeRequest = true;
|
|
71
|
-
let requestErrored = false;
|
|
72
|
-
let requestErrorCallback;
|
|
73
|
-
|
|
74
|
-
const requestErrorPromise = new Promise(resolve => {
|
|
75
|
-
requestErrorCallback = () => {
|
|
76
|
-
if (!requestErrored) {
|
|
77
|
-
requestErrored = true;
|
|
78
|
-
resolve();
|
|
79
|
-
}
|
|
80
|
-
};
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
const handler = response => {
|
|
84
|
-
if (revalidate && !opts.forceRefresh) {
|
|
85
|
-
response.status = response.statusCode;
|
|
86
|
-
const revalidatedPolicy = CachePolicy.fromObject(revalidate.cachePolicy).revalidatedPolicy(opts, response);
|
|
87
|
-
if (!revalidatedPolicy.modified) {
|
|
88
|
-
const headers = revalidatedPolicy.policy.responseHeaders();
|
|
89
|
-
response = new Response(revalidate.statusCode, headers, revalidate.body, revalidate.url);
|
|
90
|
-
response.cachePolicy = revalidatedPolicy.policy;
|
|
91
|
-
response.fromCache = true;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (!response.fromCache) {
|
|
96
|
-
response.cachePolicy = new CachePolicy(opts, response, opts);
|
|
97
|
-
response.fromCache = false;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
let clonedResponse;
|
|
101
|
-
if (opts.cache && response.cachePolicy.storable()) {
|
|
102
|
-
clonedResponse = cloneResponse(response);
|
|
103
|
-
|
|
104
|
-
(async () => {
|
|
105
|
-
try {
|
|
106
|
-
const bodyPromise = getStream.buffer(response);
|
|
107
|
-
|
|
108
|
-
await Promise.race([
|
|
109
|
-
requestErrorPromise,
|
|
110
|
-
new Promise(resolve => response.once('end', resolve))
|
|
111
|
-
]);
|
|
112
|
-
|
|
113
|
-
if (requestErrored) {
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
const body = await bodyPromise;
|
|
118
|
-
|
|
119
|
-
const value = {
|
|
120
|
-
cachePolicy: response.cachePolicy.toObject(),
|
|
121
|
-
url: response.url,
|
|
122
|
-
statusCode: response.fromCache ? revalidate.statusCode : response.statusCode,
|
|
123
|
-
body
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
let ttl = opts.strictTtl ? response.cachePolicy.timeToLive() : undefined;
|
|
127
|
-
if (opts.maxTtl) {
|
|
128
|
-
ttl = ttl ? Math.min(ttl, opts.maxTtl) : opts.maxTtl;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
await this.cache.set(key, value, ttl);
|
|
132
|
-
} catch (error) {
|
|
133
|
-
ee.emit('error', new CacheableRequest.CacheError(error));
|
|
134
|
-
}
|
|
135
|
-
})();
|
|
136
|
-
} else if (opts.cache && revalidate) {
|
|
137
|
-
(async () => {
|
|
138
|
-
try {
|
|
139
|
-
await this.cache.delete(key);
|
|
140
|
-
} catch (error) {
|
|
141
|
-
ee.emit('error', new CacheableRequest.CacheError(error));
|
|
142
|
-
}
|
|
143
|
-
})();
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
ee.emit('response', clonedResponse || response);
|
|
147
|
-
if (typeof cb === 'function') {
|
|
148
|
-
cb(clonedResponse || response);
|
|
149
|
-
}
|
|
150
|
-
};
|
|
151
|
-
|
|
152
|
-
try {
|
|
153
|
-
const req = request(opts, handler);
|
|
154
|
-
req.once('error', requestErrorCallback);
|
|
155
|
-
req.once('abort', requestErrorCallback);
|
|
156
|
-
ee.emit('request', req);
|
|
157
|
-
} catch (error) {
|
|
158
|
-
ee.emit('error', new CacheableRequest.RequestError(error));
|
|
159
|
-
}
|
|
160
|
-
};
|
|
161
|
-
|
|
162
|
-
(async () => {
|
|
163
|
-
const get = async opts => {
|
|
164
|
-
await Promise.resolve();
|
|
165
|
-
|
|
166
|
-
const cacheEntry = opts.cache ? await this.cache.get(key) : undefined;
|
|
167
|
-
if (typeof cacheEntry === 'undefined') {
|
|
168
|
-
return makeRequest(opts);
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
const policy = CachePolicy.fromObject(cacheEntry.cachePolicy);
|
|
172
|
-
if (policy.satisfiesWithoutRevalidation(opts) && !opts.forceRefresh) {
|
|
173
|
-
const headers = policy.responseHeaders();
|
|
174
|
-
const response = new Response(cacheEntry.statusCode, headers, cacheEntry.body, cacheEntry.url);
|
|
175
|
-
response.cachePolicy = policy;
|
|
176
|
-
response.fromCache = true;
|
|
177
|
-
|
|
178
|
-
ee.emit('response', response);
|
|
179
|
-
if (typeof cb === 'function') {
|
|
180
|
-
cb(response);
|
|
181
|
-
}
|
|
182
|
-
} else {
|
|
183
|
-
revalidate = cacheEntry;
|
|
184
|
-
opts.headers = policy.revalidationHeaders(opts);
|
|
185
|
-
makeRequest(opts);
|
|
186
|
-
}
|
|
187
|
-
};
|
|
188
|
-
|
|
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));
|
|
192
|
-
|
|
193
|
-
try {
|
|
194
|
-
await get(opts);
|
|
195
|
-
} catch (error) {
|
|
196
|
-
if (opts.automaticFailover && !madeRequest) {
|
|
197
|
-
makeRequest(opts);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
ee.emit('error', new CacheableRequest.CacheError(error));
|
|
201
|
-
}
|
|
202
|
-
})();
|
|
203
|
-
|
|
204
|
-
return ee;
|
|
205
|
-
};
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
function urlObjectToRequestOptions(url) {
|
|
210
|
-
const options = { ...url };
|
|
211
|
-
options.path = `${url.pathname || '/'}${url.search || ''}`;
|
|
212
|
-
delete options.pathname;
|
|
213
|
-
delete options.search;
|
|
214
|
-
return options;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
function normalizeUrlObject(url) {
|
|
218
|
-
// If url was parsed by url.parse or new URL:
|
|
219
|
-
// - hostname will be set
|
|
220
|
-
// - host will be hostname[:port]
|
|
221
|
-
// - port will be set if it was explicit in the parsed string
|
|
222
|
-
// Otherwise, url was from request options:
|
|
223
|
-
// - hostname or host may be set
|
|
224
|
-
// - host shall not have port encoded
|
|
225
|
-
return {
|
|
226
|
-
protocol: url.protocol,
|
|
227
|
-
auth: url.auth,
|
|
228
|
-
hostname: url.hostname || url.host || 'localhost',
|
|
229
|
-
port: url.port,
|
|
230
|
-
pathname: url.pathname,
|
|
231
|
-
search: url.search
|
|
232
|
-
};
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
CacheableRequest.RequestError = class extends Error {
|
|
236
|
-
constructor(error) {
|
|
237
|
-
super(error.message);
|
|
238
|
-
this.name = 'RequestError';
|
|
239
|
-
Object.assign(this, error);
|
|
240
|
-
}
|
|
241
|
-
};
|
|
242
|
-
|
|
243
|
-
CacheableRequest.CacheError = class extends Error {
|
|
244
|
-
constructor(error) {
|
|
245
|
-
super(error.message);
|
|
246
|
-
this.name = 'CacheError';
|
|
247
|
-
Object.assign(this, error);
|
|
248
|
-
}
|
|
249
|
-
};
|
|
250
|
-
|
|
251
|
-
module.exports = CacheableRequest;
|