cacheable-request 8.0.1 → 9.0.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 +27 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +275 -0
- package/dist/index.js.map +1 -0
- package/package.json +67 -19
- package/src/index.js +0 -248
package/README.md
CHANGED
|
@@ -11,6 +11,11 @@
|
|
|
11
11
|
|
|
12
12
|
**Note:** This is a low level wrapper around the core HTTP modules, it's not a high level request library.
|
|
13
13
|
|
|
14
|
+
## ESM Support in version 9 and higher.
|
|
15
|
+
|
|
16
|
+
We are now using pure esm support in our package. If you need to use commonjs you can use v8 or lower. To learn more about using ESM please read this from `sindresorhus`: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
|
|
17
|
+
|
|
18
|
+
|
|
14
19
|
## Features
|
|
15
20
|
|
|
16
21
|
- Only stores cacheable responses as defined by RFC 7234
|
|
@@ -199,9 +204,31 @@ cacheableRequest('example.com', cb)
|
|
|
199
204
|
});
|
|
200
205
|
```
|
|
201
206
|
|
|
207
|
+
## Add Hooks
|
|
208
|
+
|
|
209
|
+
The hook will pre compute response right before saving it in cache. You can include many hooks and it will run in order you add hook on response object.
|
|
210
|
+
|
|
211
|
+
```js
|
|
212
|
+
CacheableRequest.addHook('response', async (response: any) => {
|
|
213
|
+
const buffer = await pm(gunzip)(response);
|
|
214
|
+
return buffer.toString();
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
const cacheableRequest = CacheableRequest(request, cache);
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
## Remove Hooks
|
|
221
|
+
|
|
222
|
+
You can also remove hook by using below
|
|
223
|
+
|
|
224
|
+
```js
|
|
225
|
+
CacheableRequest.removeHook('response');
|
|
226
|
+
```
|
|
227
|
+
|
|
202
228
|
**Note:** Database connection errors are emitted here, however `cacheable-request` will attempt to re-request the resource and bypass the cache on a connection error. Therefore a database connection error doesn't necessarily mean the request won't be fulfilled.
|
|
203
229
|
|
|
204
230
|
## License
|
|
205
231
|
|
|
206
232
|
MIT © Luke Childs 2017-2021
|
|
233
|
+
|
|
207
234
|
MIT © Jared Wray 2022
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
3
|
+
import EventEmitter from 'node:events';
|
|
4
|
+
declare const CacheableRequest: {
|
|
5
|
+
(request: Function, cacheAdapter?: any): (options: any, cb?: ((response: Record<string, unknown>) => void) | undefined) => EventEmitter;
|
|
6
|
+
addHook(name: string, fn: Function): void;
|
|
7
|
+
removeHook(name: string): boolean;
|
|
8
|
+
getHook(name: string): any;
|
|
9
|
+
runHook(name: string, response: any): Promise<any>;
|
|
10
|
+
RequestError: {
|
|
11
|
+
new (error: any): {
|
|
12
|
+
name: string;
|
|
13
|
+
message: string;
|
|
14
|
+
stack?: string | undefined;
|
|
15
|
+
};
|
|
16
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function | undefined): void;
|
|
17
|
+
prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined;
|
|
18
|
+
stackTraceLimit: number;
|
|
19
|
+
};
|
|
20
|
+
CacheError: {
|
|
21
|
+
new (error: any): {
|
|
22
|
+
name: string;
|
|
23
|
+
message: string;
|
|
24
|
+
stack?: string | undefined;
|
|
25
|
+
};
|
|
26
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function | undefined): void;
|
|
27
|
+
prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined;
|
|
28
|
+
stackTraceLimit: number;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
export default CacheableRequest;
|
|
32
|
+
//# 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;AAcvC,QAAA,MAAM,gBAAgB;cAAsB,QAAQ,iBAAiB,GAAG,aAgBtD,GAAG,mBAAkB,OAAO,MAAM,EAAE,OAAO,CAAC,KAAK,IAAI;kBA4LrC,MAAM,MAAM,QAAQ;qBAMjB,MAAM;kBAET,MAAM;kBAEA,MAAM,YAAY,GAAG;;oBAwDzC,GAAG;;;;;;;;;;oBAOH,GAAG;;;;;;;;;CAxQtB,CAAC;AA+QF,eAAe,gBAAgB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import EventEmitter from 'node:events';
|
|
2
|
+
import urlLib from 'node:url';
|
|
3
|
+
import crypto from 'node:crypto';
|
|
4
|
+
import stream, { PassThrough as PassThroughStream } from 'node:stream';
|
|
5
|
+
import normalizeUrl from 'normalize-url';
|
|
6
|
+
import getStream from 'get-stream';
|
|
7
|
+
import CachePolicy from 'http-cache-semantics';
|
|
8
|
+
import Response from 'responselike';
|
|
9
|
+
import Keyv from 'keyv';
|
|
10
|
+
import mimicResponse from 'mimic-response';
|
|
11
|
+
const hooks = new Map();
|
|
12
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
13
|
+
const CacheableRequest = function (request, cacheAdapter) {
|
|
14
|
+
let cache = {};
|
|
15
|
+
if (cacheAdapter instanceof Keyv) {
|
|
16
|
+
cache = cacheAdapter;
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
cache = new Keyv({
|
|
20
|
+
uri: (typeof cacheAdapter === 'string' && cacheAdapter) || '',
|
|
21
|
+
store: typeof cacheAdapter !== 'string' && cacheAdapter,
|
|
22
|
+
namespace: 'cacheable-request',
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return createCacheableRequest(request, cache);
|
|
26
|
+
};
|
|
27
|
+
function createCacheableRequest(request, cache) {
|
|
28
|
+
return (options, cb) => {
|
|
29
|
+
let url;
|
|
30
|
+
if (typeof options === 'string') {
|
|
31
|
+
url = normalizeUrlObject(urlLib.parse(options));
|
|
32
|
+
options = {};
|
|
33
|
+
}
|
|
34
|
+
else if (options instanceof urlLib.URL) {
|
|
35
|
+
url = normalizeUrlObject(urlLib.parse(options.toString()));
|
|
36
|
+
options = {};
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
const [pathname, ...searchParts] = (options.path || '').split('?');
|
|
40
|
+
const search = searchParts.length > 0
|
|
41
|
+
? `?${searchParts.join('?')}`
|
|
42
|
+
: '';
|
|
43
|
+
url = normalizeUrlObject({ ...options, pathname, search });
|
|
44
|
+
}
|
|
45
|
+
options = {
|
|
46
|
+
headers: {},
|
|
47
|
+
method: 'GET',
|
|
48
|
+
cache: true,
|
|
49
|
+
strictTtl: false,
|
|
50
|
+
automaticFailover: false,
|
|
51
|
+
...options,
|
|
52
|
+
...urlObjectToRequestOptions(url),
|
|
53
|
+
};
|
|
54
|
+
options.headers = Object.fromEntries(Object.entries(options.headers).map(([key, value]) => [key.toLowerCase(), value]));
|
|
55
|
+
const ee = new EventEmitter();
|
|
56
|
+
const normalizedUrlString = normalizeUrl(urlLib.format(url), {
|
|
57
|
+
stripWWW: false,
|
|
58
|
+
removeTrailingSlash: false,
|
|
59
|
+
stripAuthentication: false,
|
|
60
|
+
});
|
|
61
|
+
let key = `${options.method}:${normalizedUrlString}`;
|
|
62
|
+
// POST, PATCH, and PUT requests may be cached, depending on the response
|
|
63
|
+
// cache-control headers. As a result, the body of the request should be
|
|
64
|
+
// added to the cache key in order to avoid collisions.
|
|
65
|
+
if (options.body && ['POST', 'PATCH', 'PUT'].includes(options.method)) {
|
|
66
|
+
if (options.body instanceof stream.Readable) {
|
|
67
|
+
// Streamed bodies should completely skip the cache because they may
|
|
68
|
+
// or may not be hashable and in either case the stream would need to
|
|
69
|
+
// close before the cache key could be generated.
|
|
70
|
+
options.cache = false;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
key += `:${crypto.createHash('md5').update(options.body).digest('hex')}`;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
let revalidate = false;
|
|
77
|
+
let madeRequest = false;
|
|
78
|
+
const makeRequest = (options_) => {
|
|
79
|
+
madeRequest = true;
|
|
80
|
+
let requestErrored = false;
|
|
81
|
+
let requestErrorCallback;
|
|
82
|
+
const requestErrorPromise = new Promise(resolve => {
|
|
83
|
+
requestErrorCallback = () => {
|
|
84
|
+
if (!requestErrored) {
|
|
85
|
+
requestErrored = true;
|
|
86
|
+
resolve();
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
});
|
|
90
|
+
const handler = (response) => {
|
|
91
|
+
if (revalidate) {
|
|
92
|
+
response.status = response.statusCode;
|
|
93
|
+
const revalidatedPolicy = CachePolicy.fromObject(revalidate.cachePolicy).revalidatedPolicy(options_, response);
|
|
94
|
+
if (!revalidatedPolicy.modified) {
|
|
95
|
+
const headers = convertHeaders(revalidatedPolicy.policy.responseHeaders());
|
|
96
|
+
response = new Response({ statusCode: revalidate.statusCode, headers, body: revalidate.body, url: revalidate.url });
|
|
97
|
+
response.cachePolicy = revalidatedPolicy.policy;
|
|
98
|
+
response.fromCache = true;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (!response.fromCache) {
|
|
102
|
+
response.cachePolicy = new CachePolicy(options_, response, options_);
|
|
103
|
+
response.fromCache = false;
|
|
104
|
+
}
|
|
105
|
+
let clonedResponse;
|
|
106
|
+
if (options_.cache && response.cachePolicy.storable()) {
|
|
107
|
+
clonedResponse = cloneResponse(response);
|
|
108
|
+
(async () => {
|
|
109
|
+
try {
|
|
110
|
+
const bodyPromise = getStream.buffer(response);
|
|
111
|
+
await Promise.race([
|
|
112
|
+
requestErrorPromise,
|
|
113
|
+
new Promise(resolve => response.once('end', resolve)), // eslint-disable-line no-promise-executor-return
|
|
114
|
+
]);
|
|
115
|
+
const body = await bodyPromise;
|
|
116
|
+
const value = {
|
|
117
|
+
cachePolicy: response.cachePolicy.toObject(),
|
|
118
|
+
url: response.url,
|
|
119
|
+
statusCode: response.fromCache ? revalidate.statusCode : response.statusCode,
|
|
120
|
+
body,
|
|
121
|
+
};
|
|
122
|
+
let ttl = options_.strictTtl ? response.cachePolicy.timeToLive() : undefined;
|
|
123
|
+
if (options_.maxTtl) {
|
|
124
|
+
ttl = ttl ? Math.min(ttl, options_.maxTtl) : options_.maxTtl;
|
|
125
|
+
}
|
|
126
|
+
if (hooks.size > 0) {
|
|
127
|
+
/* eslint-disable no-await-in-loop */
|
|
128
|
+
for (const key_ of hooks.keys()) {
|
|
129
|
+
value.body = await CacheableRequest.runHook(key_, value.body);
|
|
130
|
+
}
|
|
131
|
+
/* eslint-enable no-await-in-loop */
|
|
132
|
+
}
|
|
133
|
+
await cache.set(key, value, ttl);
|
|
134
|
+
}
|
|
135
|
+
catch (error) {
|
|
136
|
+
ee.emit('error', new CacheableRequest.CacheError(error));
|
|
137
|
+
}
|
|
138
|
+
})();
|
|
139
|
+
}
|
|
140
|
+
else if (options_.cache && revalidate) {
|
|
141
|
+
(async () => {
|
|
142
|
+
try {
|
|
143
|
+
await cache.delete(key);
|
|
144
|
+
}
|
|
145
|
+
catch (error) {
|
|
146
|
+
ee.emit('error', new CacheableRequest.CacheError(error));
|
|
147
|
+
}
|
|
148
|
+
})();
|
|
149
|
+
}
|
|
150
|
+
ee.emit('response', clonedResponse || response);
|
|
151
|
+
if (typeof cb === 'function') {
|
|
152
|
+
cb(clonedResponse || response);
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
try {
|
|
156
|
+
const request_ = request(options_, handler);
|
|
157
|
+
request_.once('error', requestErrorCallback);
|
|
158
|
+
request_.once('abort', requestErrorCallback);
|
|
159
|
+
ee.emit('request', request_);
|
|
160
|
+
}
|
|
161
|
+
catch (error) {
|
|
162
|
+
ee.emit('error', new CacheableRequest.RequestError(error));
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
(async () => {
|
|
166
|
+
const get = async (options_) => {
|
|
167
|
+
await Promise.resolve();
|
|
168
|
+
const cacheEntry = options_.cache ? await cache.get(key) : undefined;
|
|
169
|
+
if (typeof cacheEntry === 'undefined' && !options_.forceRefresh) {
|
|
170
|
+
makeRequest(options_);
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
const policy = CachePolicy.fromObject(cacheEntry.cachePolicy);
|
|
174
|
+
if (policy.satisfiesWithoutRevalidation(options_) && !options_.forceRefresh) {
|
|
175
|
+
const headers = convertHeaders(policy.responseHeaders());
|
|
176
|
+
const response = new Response({ statusCode: cacheEntry.statusCode, headers, body: cacheEntry.body, url: cacheEntry.url });
|
|
177
|
+
response.cachePolicy = policy;
|
|
178
|
+
response.fromCache = true;
|
|
179
|
+
ee.emit('response', response);
|
|
180
|
+
if (typeof cb === 'function') {
|
|
181
|
+
cb(response);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
else if (policy.satisfiesWithoutRevalidation(options_) && Date.now() >= policy.timeToLive() && options_.forceRefresh) {
|
|
185
|
+
await cache.delete(key);
|
|
186
|
+
options_.headers = policy.revalidationHeaders(options_);
|
|
187
|
+
makeRequest(options_);
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
revalidate = cacheEntry;
|
|
191
|
+
options_.headers = policy.revalidationHeaders(options_);
|
|
192
|
+
makeRequest(options_);
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
const errorHandler = (error) => ee.emit('error', new CacheableRequest.CacheError(error));
|
|
196
|
+
cache.once('error', errorHandler);
|
|
197
|
+
ee.on('response', () => cache.removeListener('error', errorHandler));
|
|
198
|
+
try {
|
|
199
|
+
await get(options);
|
|
200
|
+
}
|
|
201
|
+
catch (error) {
|
|
202
|
+
if (options.automaticFailover && !madeRequest) {
|
|
203
|
+
makeRequest(options);
|
|
204
|
+
}
|
|
205
|
+
ee.emit('error', new CacheableRequest.CacheError(error));
|
|
206
|
+
}
|
|
207
|
+
})();
|
|
208
|
+
return ee;
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
CacheableRequest.addHook = (name, fn) => {
|
|
212
|
+
if (!hooks.has(name)) {
|
|
213
|
+
hooks.set(name, fn);
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
CacheableRequest.removeHook = (name) => hooks.delete(name);
|
|
217
|
+
CacheableRequest.getHook = (name) => hooks.get(name);
|
|
218
|
+
CacheableRequest.runHook = async (name, response) => {
|
|
219
|
+
if (!response) {
|
|
220
|
+
return new CacheableRequest.CacheError(new Error('runHooks requires response argument'));
|
|
221
|
+
}
|
|
222
|
+
return hooks.get(name)(response);
|
|
223
|
+
};
|
|
224
|
+
function cloneResponse(response) {
|
|
225
|
+
const clone = new PassThroughStream({ autoDestroy: false });
|
|
226
|
+
mimicResponse(response, clone);
|
|
227
|
+
return response.pipe(clone);
|
|
228
|
+
}
|
|
229
|
+
function urlObjectToRequestOptions(url) {
|
|
230
|
+
const options = { ...url };
|
|
231
|
+
options.path = `${url.pathname || '/'}${url.search || ''}`;
|
|
232
|
+
delete options.pathname;
|
|
233
|
+
delete options.search;
|
|
234
|
+
return options;
|
|
235
|
+
}
|
|
236
|
+
function normalizeUrlObject(url) {
|
|
237
|
+
// If url was parsed by url.parse or new URL:
|
|
238
|
+
// - hostname will be set
|
|
239
|
+
// - host will be hostname[:port]
|
|
240
|
+
// - port will be set if it was explicit in the parsed string
|
|
241
|
+
// Otherwise, url was from request options:
|
|
242
|
+
// - hostname or host may be set
|
|
243
|
+
// - host shall not have port encoded
|
|
244
|
+
return {
|
|
245
|
+
protocol: url.protocol,
|
|
246
|
+
auth: url.auth,
|
|
247
|
+
hostname: url.hostname || url.host || 'localhost',
|
|
248
|
+
port: url.port,
|
|
249
|
+
pathname: url.pathname,
|
|
250
|
+
search: url.search,
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
function convertHeaders(headers) {
|
|
254
|
+
const result = {};
|
|
255
|
+
for (const name of Object.keys(headers)) {
|
|
256
|
+
result[name.toLowerCase()] = headers[name];
|
|
257
|
+
}
|
|
258
|
+
return result;
|
|
259
|
+
}
|
|
260
|
+
CacheableRequest.RequestError = class extends Error {
|
|
261
|
+
constructor(error) {
|
|
262
|
+
super(error.message);
|
|
263
|
+
this.name = 'RequestError';
|
|
264
|
+
Object.assign(this, error);
|
|
265
|
+
}
|
|
266
|
+
};
|
|
267
|
+
CacheableRequest.CacheError = class extends Error {
|
|
268
|
+
constructor(error) {
|
|
269
|
+
super(error.message);
|
|
270
|
+
this.name = 'CacheError';
|
|
271
|
+
Object.assign(this, error);
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
export default CacheableRequest;
|
|
275
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,aAAa,CAAC;AACvC,OAAO,MAAM,MAAM,UAAU,CAAC;AAC9B,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,MAAM,EAAE,EAAC,WAAW,IAAI,iBAAiB,EAAC,MAAM,aAAa,CAAC;AACrE,OAAO,YAAY,MAAM,eAAe,CAAC;AACzC,OAAO,SAAS,MAAM,YAAY,CAAC;AACnC,OAAO,WAAW,MAAM,sBAAsB,CAAC;AAC/C,OAAO,QAAQ,MAAM,cAAc,CAAC;AACpC,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,aAAa,MAAM,gBAAgB,CAAC;AAE3C,MAAM,KAAK,GAAG,IAAI,GAAG,EAAe,CAAC;AAErC,gEAAgE;AAChE,MAAM,gBAAgB,GAAG,UAAU,OAAiB,EAAE,YAAkB;IACvE,IAAI,KAAK,GAAQ,EAAE,CAAC;IACpB,IAAI,YAAY,YAAY,IAAI,EAAE;QACjC,KAAK,GAAG,YAAY,CAAC;KACrB;SAAM;QACN,KAAK,GAAG,IAAI,IAAI,CAAC;YAChB,GAAG,EAAE,CAAC,O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4
4
|
"description": "Wrap native HTTP requests with RFC compliant cache support",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "jaredwray/cacheable-request",
|
|
7
7
|
"author": "Jared Wray <me@jaredwray.com> (http://jaredwray.com)",
|
|
8
8
|
"type": "module",
|
|
9
|
-
"
|
|
9
|
+
"exports": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
10
11
|
"engines": {
|
|
11
|
-
"node": ">=14"
|
|
12
|
+
"node": ">=14.16"
|
|
12
13
|
},
|
|
13
14
|
"scripts": {
|
|
14
|
-
"test": "xo &&
|
|
15
|
-
"
|
|
16
|
-
"clean": "rm -rf node_modules && rm -rf
|
|
15
|
+
"test": "xo && NODE_OPTIONS=--experimental-vm-modules jest --coverage ",
|
|
16
|
+
"build": "tsc --project tsconfig.build.json",
|
|
17
|
+
"clean": "rm -rf node_modules && rm -rf ./coverage && rm -rf ./package-lock.json && rm -rf ./test/testdb.sqlite && rm -rf ./dist"
|
|
17
18
|
},
|
|
18
19
|
"files": [
|
|
19
|
-
"
|
|
20
|
+
"dist"
|
|
20
21
|
],
|
|
21
22
|
"keywords": [
|
|
22
23
|
"HTTP",
|
|
@@ -31,27 +32,74 @@
|
|
|
31
32
|
"compliant"
|
|
32
33
|
],
|
|
33
34
|
"dependencies": {
|
|
34
|
-
"clone-response": "^1.0.2",
|
|
35
35
|
"get-stream": "^6.0.1",
|
|
36
36
|
"http-cache-semantics": "^4.1.0",
|
|
37
|
-
"keyv": "^4.3.
|
|
37
|
+
"keyv": "^4.3.3",
|
|
38
|
+
"mimic-response": "^4.0.0",
|
|
38
39
|
"normalize-url": "^7.0.3",
|
|
39
|
-
"responselike": "^
|
|
40
|
+
"responselike": "^3.0.0"
|
|
40
41
|
},
|
|
41
42
|
"devDependencies": {
|
|
42
|
-
"@
|
|
43
|
-
"@
|
|
44
|
-
"@
|
|
45
|
-
"
|
|
43
|
+
"@keyv/sqlite": "^3.5.3",
|
|
44
|
+
"@types/create-test-server": "^3.0.1",
|
|
45
|
+
"@types/delay": "^3.1.0",
|
|
46
|
+
"@types/get-stream": "^3.0.2",
|
|
47
|
+
"@types/http-cache-semantics": "^4.0.1",
|
|
48
|
+
"@types/jest": "^28.1.6",
|
|
49
|
+
"@types/node": "^18.7.3",
|
|
50
|
+
"@types/pify": "^5.0.1",
|
|
51
|
+
"@types/responselike": "^1.0.0",
|
|
52
|
+
"@types/sqlite3": "^3.1.8",
|
|
46
53
|
"create-test-server": "3.0.1",
|
|
47
54
|
"delay": "^5.0.0",
|
|
48
|
-
"eslint-
|
|
49
|
-
"
|
|
55
|
+
"eslint-plugin-jest": "^26.8.2",
|
|
56
|
+
"jest": "^28.1.3",
|
|
50
57
|
"pify": "^6.0.0",
|
|
51
|
-
"sqlite3": "^5.0.
|
|
52
|
-
"
|
|
58
|
+
"sqlite3": "^5.0.11",
|
|
59
|
+
"ts-jest": "^28.0.7",
|
|
60
|
+
"ts-jest-resolver": "^2.0.0",
|
|
61
|
+
"ts-node": "^10.9.1",
|
|
62
|
+
"typescript": "^4.7.4",
|
|
63
|
+
"xo": "^0.51.0"
|
|
64
|
+
},
|
|
65
|
+
"jest": {
|
|
66
|
+
"extensionsToTreatAsEsm": [
|
|
67
|
+
".ts"
|
|
68
|
+
],
|
|
69
|
+
"resolver": "ts-jest-resolver",
|
|
70
|
+
"globals": {
|
|
71
|
+
"ts-jest": {
|
|
72
|
+
"tsconfig": "./tsconfig.build.json",
|
|
73
|
+
"useESM": true
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
"moduleFileExtensions": [
|
|
77
|
+
"ts",
|
|
78
|
+
"js"
|
|
79
|
+
],
|
|
80
|
+
"transform": {
|
|
81
|
+
"^.+\\.(ts|tsx)$": "ts-jest"
|
|
82
|
+
},
|
|
83
|
+
"testMatch": [
|
|
84
|
+
"**/test/*.test.(ts)"
|
|
85
|
+
],
|
|
86
|
+
"testEnvironment": "node"
|
|
53
87
|
},
|
|
54
88
|
"xo": {
|
|
55
|
-
"
|
|
89
|
+
"plugins": [
|
|
90
|
+
"jest"
|
|
91
|
+
],
|
|
92
|
+
"extends": [
|
|
93
|
+
"plugin:jest/recommended"
|
|
94
|
+
],
|
|
95
|
+
"rules": {
|
|
96
|
+
"@typescript-eslint/no-unsafe-assignment": 0,
|
|
97
|
+
"@typescript-eslint/no-unsafe-call": 0,
|
|
98
|
+
"@typescript-eslint/ban-types": 0,
|
|
99
|
+
"@typescript-eslint/restrict-template-expressions": 0,
|
|
100
|
+
"@typescript-eslint/no-unsafe-return": 0,
|
|
101
|
+
"new-cap": 0,
|
|
102
|
+
"@typescript-eslint/restrict-plus-operands": 0
|
|
103
|
+
}
|
|
56
104
|
}
|
|
57
105
|
}
|
package/src/index.js
DELETED
|
@@ -1,248 +0,0 @@
|
|
|
1
|
-
import EventEmitter from 'node:events';
|
|
2
|
-
import urlLib from 'node:url';
|
|
3
|
-
import crypto from 'node:crypto';
|
|
4
|
-
import stream from 'node:stream';
|
|
5
|
-
import normalizeUrl from 'normalize-url';
|
|
6
|
-
import getStream from 'get-stream';
|
|
7
|
-
import CachePolicy from 'http-cache-semantics';
|
|
8
|
-
import Response from 'responselike';
|
|
9
|
-
import cloneResponse from 'clone-response';
|
|
10
|
-
import Keyv from 'keyv';
|
|
11
|
-
|
|
12
|
-
const { Readable } = stream;
|
|
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
|
-
if (cacheAdapter instanceof Keyv) {
|
|
20
|
-
this.cache = cacheAdapter;
|
|
21
|
-
} else {
|
|
22
|
-
this.cache = new Keyv({
|
|
23
|
-
uri: typeof cacheAdapter === 'string' && cacheAdapter,
|
|
24
|
-
store: typeof cacheAdapter !== 'string' && cacheAdapter,
|
|
25
|
-
namespace: 'cacheable-request',
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
return this.createCacheableRequest(request); // eslint-disable-line no-constructor-return
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
createCacheableRequest(request) {
|
|
33
|
-
return (options, cb) => {
|
|
34
|
-
let url;
|
|
35
|
-
if (typeof options === 'string') {
|
|
36
|
-
url = normalizeUrlObject(urlLib.parse(options));
|
|
37
|
-
options = {};
|
|
38
|
-
} else if (options instanceof urlLib.URL) {
|
|
39
|
-
url = normalizeUrlObject(urlLib.parse(options.toString()));
|
|
40
|
-
options = {};
|
|
41
|
-
} else {
|
|
42
|
-
const [pathname, ...searchParts] = (options.path || '').split('?');
|
|
43
|
-
const search = searchParts.length > 0
|
|
44
|
-
? `?${searchParts.join('?')}`
|
|
45
|
-
: '';
|
|
46
|
-
url = normalizeUrlObject({ ...options, pathname, search });
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
options = {
|
|
50
|
-
headers: {},
|
|
51
|
-
method: 'GET',
|
|
52
|
-
cache: true,
|
|
53
|
-
strictTtl: false,
|
|
54
|
-
automaticFailover: false,
|
|
55
|
-
...options,
|
|
56
|
-
...urlObjectToRequestOptions(url),
|
|
57
|
-
};
|
|
58
|
-
options.headers = Object.fromEntries(Object.entries(options.headers).map(([key, value]) => [key.toLowerCase(), value]));
|
|
59
|
-
const ee = new EventEmitter();
|
|
60
|
-
const normalizedUrlString = normalizeUrl(urlLib.format(url), {
|
|
61
|
-
stripWWW: false,
|
|
62
|
-
removeTrailingSlash: false,
|
|
63
|
-
stripAuthentication: false,
|
|
64
|
-
});
|
|
65
|
-
let key = `${options.method}:${normalizedUrlString}`;
|
|
66
|
-
// POST, PATCH, and PUT requests may be cached, depending on the response
|
|
67
|
-
// cache-control headers. As a result, the body of the request should be
|
|
68
|
-
// added to the cache key in order to avoid collisions.
|
|
69
|
-
if (options.body && ['POST', 'PATCH', 'PUT'].includes(options.method)) {
|
|
70
|
-
if (options.body instanceof Readable) {
|
|
71
|
-
// Streamed bodies should completely skip the cache because they may
|
|
72
|
-
// or may not be hashable and in either case the stream would need to
|
|
73
|
-
// close before the cache key could be generated.
|
|
74
|
-
options.cache = false;
|
|
75
|
-
} else {
|
|
76
|
-
key += `:${crypto.createHash('md5').update(options.body).digest('hex')}`;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
let revalidate = false;
|
|
81
|
-
let madeRequest = false;
|
|
82
|
-
const makeRequest = options_ => {
|
|
83
|
-
madeRequest = true;
|
|
84
|
-
let requestErrored = false;
|
|
85
|
-
let requestErrorCallback;
|
|
86
|
-
const requestErrorPromise = new Promise(resolve => {
|
|
87
|
-
requestErrorCallback = () => {
|
|
88
|
-
if (!requestErrored) {
|
|
89
|
-
requestErrored = true;
|
|
90
|
-
resolve();
|
|
91
|
-
}
|
|
92
|
-
};
|
|
93
|
-
});
|
|
94
|
-
const handler = response => {
|
|
95
|
-
if (revalidate && !options_.forceRefresh) {
|
|
96
|
-
response.status = response.statusCode;
|
|
97
|
-
const revalidatedPolicy = CachePolicy.fromObject(revalidate.cachePolicy).revalidatedPolicy(options_, response);
|
|
98
|
-
if (!revalidatedPolicy.modified) {
|
|
99
|
-
const headers = revalidatedPolicy.policy.responseHeaders();
|
|
100
|
-
response = new Response(revalidate.statusCode, headers, revalidate.body, revalidate.url);
|
|
101
|
-
response.cachePolicy = revalidatedPolicy.policy;
|
|
102
|
-
response.fromCache = true;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
if (!response.fromCache) {
|
|
107
|
-
response.cachePolicy = new CachePolicy(options_, response, options_);
|
|
108
|
-
response.fromCache = false;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
let clonedResponse;
|
|
112
|
-
if (options_.cache && response.cachePolicy.storable()) {
|
|
113
|
-
clonedResponse = cloneResponse(response);
|
|
114
|
-
(async () => {
|
|
115
|
-
try {
|
|
116
|
-
const bodyPromise = getStream.buffer(response);
|
|
117
|
-
await Promise.race([
|
|
118
|
-
requestErrorPromise,
|
|
119
|
-
new Promise(resolve => response.once('end', resolve)), // eslint-disable-line no-promise-executor-return
|
|
120
|
-
]);
|
|
121
|
-
const body = await bodyPromise;
|
|
122
|
-
const value = {
|
|
123
|
-
cachePolicy: response.cachePolicy.toObject(),
|
|
124
|
-
url: response.url,
|
|
125
|
-
statusCode: response.fromCache ? revalidate.statusCode : response.statusCode,
|
|
126
|
-
body,
|
|
127
|
-
};
|
|
128
|
-
let ttl = options_.strictTtl ? response.cachePolicy.timeToLive() : undefined;
|
|
129
|
-
if (options_.maxTtl) {
|
|
130
|
-
ttl = ttl ? Math.min(ttl, options_.maxTtl) : options_.maxTtl;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
await this.cache.set(key, value, ttl);
|
|
134
|
-
} catch (error) {
|
|
135
|
-
ee.emit('error', new CacheableRequest.CacheError(error));
|
|
136
|
-
}
|
|
137
|
-
})();
|
|
138
|
-
} else if (options_.cache && revalidate) {
|
|
139
|
-
(async () => {
|
|
140
|
-
try {
|
|
141
|
-
await this.cache.delete(key);
|
|
142
|
-
} catch (error) {
|
|
143
|
-
ee.emit('error', new CacheableRequest.CacheError(error));
|
|
144
|
-
}
|
|
145
|
-
})();
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
ee.emit('response', clonedResponse || response);
|
|
149
|
-
if (typeof cb === 'function') {
|
|
150
|
-
cb(clonedResponse || response);
|
|
151
|
-
}
|
|
152
|
-
};
|
|
153
|
-
|
|
154
|
-
try {
|
|
155
|
-
const request_ = request(options_, handler);
|
|
156
|
-
request_.once('error', requestErrorCallback);
|
|
157
|
-
request_.once('abort', requestErrorCallback);
|
|
158
|
-
ee.emit('request', request_);
|
|
159
|
-
} catch (error) {
|
|
160
|
-
ee.emit('error', new CacheableRequest.RequestError(error));
|
|
161
|
-
}
|
|
162
|
-
};
|
|
163
|
-
|
|
164
|
-
(async () => {
|
|
165
|
-
const get = async options_ => {
|
|
166
|
-
await Promise.resolve();
|
|
167
|
-
const cacheEntry = options_.cache ? await this.cache.get(key) : undefined;
|
|
168
|
-
if (typeof cacheEntry === 'undefined') {
|
|
169
|
-
return makeRequest(options_);
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
const policy = CachePolicy.fromObject(cacheEntry.cachePolicy);
|
|
173
|
-
if (policy.satisfiesWithoutRevalidation(options_) && !options_.forceRefresh) {
|
|
174
|
-
const headers = policy.responseHeaders();
|
|
175
|
-
const response = new Response(cacheEntry.statusCode, headers, cacheEntry.body, cacheEntry.url);
|
|
176
|
-
response.cachePolicy = policy;
|
|
177
|
-
response.fromCache = true;
|
|
178
|
-
ee.emit('response', response);
|
|
179
|
-
if (typeof cb === 'function') {
|
|
180
|
-
cb(response);
|
|
181
|
-
}
|
|
182
|
-
} else {
|
|
183
|
-
revalidate = cacheEntry;
|
|
184
|
-
options_.headers = policy.revalidationHeaders(options_);
|
|
185
|
-
makeRequest(options_);
|
|
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
|
-
try {
|
|
193
|
-
await get(options);
|
|
194
|
-
} catch (error) {
|
|
195
|
-
if (options.automaticFailover && !madeRequest) {
|
|
196
|
-
makeRequest(options);
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
ee.emit('error', new CacheableRequest.CacheError(error));
|
|
200
|
-
}
|
|
201
|
-
})();
|
|
202
|
-
|
|
203
|
-
return ee;
|
|
204
|
-
};
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
function urlObjectToRequestOptions(url) {
|
|
208
|
-
const options = { ...url };
|
|
209
|
-
options.path = `${url.pathname || '/'}${url.search || ''}`;
|
|
210
|
-
delete options.pathname;
|
|
211
|
-
delete options.search;
|
|
212
|
-
return options;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
function normalizeUrlObject(url) {
|
|
216
|
-
// If url was parsed by url.parse or new URL:
|
|
217
|
-
// - hostname will be set
|
|
218
|
-
// - host will be hostname[:port]
|
|
219
|
-
// - port will be set if it was explicit in the parsed string
|
|
220
|
-
// Otherwise, url was from request options:
|
|
221
|
-
// - hostname or host may be set
|
|
222
|
-
// - host shall not have port encoded
|
|
223
|
-
return {
|
|
224
|
-
protocol: url.protocol,
|
|
225
|
-
auth: url.auth,
|
|
226
|
-
hostname: url.hostname || url.host || 'localhost',
|
|
227
|
-
port: url.port,
|
|
228
|
-
pathname: url.pathname,
|
|
229
|
-
search: url.search,
|
|
230
|
-
};
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
CacheableRequest.RequestError = class extends Error {
|
|
234
|
-
constructor(error) {
|
|
235
|
-
super(error.message);
|
|
236
|
-
this.name = 'RequestError';
|
|
237
|
-
Object.assign(this, error);
|
|
238
|
-
}
|
|
239
|
-
};
|
|
240
|
-
CacheableRequest.CacheError = class extends Error {
|
|
241
|
-
constructor(error) {
|
|
242
|
-
super(error.message);
|
|
243
|
-
this.name = 'CacheError';
|
|
244
|
-
Object.assign(this, error);
|
|
245
|
-
}
|
|
246
|
-
};
|
|
247
|
-
|
|
248
|
-
export default CacheableRequest;
|