cacheable-request 8.3.0 → 10.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 +51 -5
- package/package.json +28 -27
package/README.md
CHANGED
|
@@ -11,6 +11,28 @@
|
|
|
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
|
+
## Breaking Changes with v10.0.0
|
|
19
|
+
This release contains breaking changes. This is the new way to use this package.
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
import CacheableRequest from 'cacheable-request';
|
|
23
|
+
|
|
24
|
+
// Now You can do
|
|
25
|
+
const cacheableRequest = new CacheableRequest(http.request).createCacheableRequest();
|
|
26
|
+
const cacheReq = cacheableRequest('http://example.com', cb);
|
|
27
|
+
cacheReq.on('request', req => req.end());
|
|
28
|
+
// Future requests to 'example.com' will be returned from cache if still valid
|
|
29
|
+
|
|
30
|
+
// You pass in any other http.request API compatible method to be wrapped with cache support:
|
|
31
|
+
const cacheableRequest = new CacheableRequest(https.request).createCacheableRequest();
|
|
32
|
+
const cacheableRequest = new CacheableRequest(electron.net).createCacheableRequest();
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
|
|
14
36
|
## Features
|
|
15
37
|
|
|
16
38
|
- Only stores cacheable responses as defined by RFC 7234
|
|
@@ -43,14 +65,14 @@ const req = http.request('http://example.com', cb);
|
|
|
43
65
|
req.end();
|
|
44
66
|
|
|
45
67
|
// You can do
|
|
46
|
-
const cacheableRequest = new CacheableRequest(http.request);
|
|
68
|
+
const cacheableRequest = new CacheableRequest(http.request).createCacheableRequest();
|
|
47
69
|
const cacheReq = cacheableRequest('http://example.com', cb);
|
|
48
70
|
cacheReq.on('request', req => req.end());
|
|
49
71
|
// Future requests to 'example.com' will be returned from cache if still valid
|
|
50
72
|
|
|
51
73
|
// You pass in any other http.request API compatible method to be wrapped with cache support:
|
|
52
|
-
const cacheableRequest = new CacheableRequest(https.request);
|
|
53
|
-
const cacheableRequest = new CacheableRequest(electron.net);
|
|
74
|
+
const cacheableRequest = new CacheableRequest(https.request).createCacheableRequest();
|
|
75
|
+
const cacheableRequest = new CacheableRequest(electron.net).createCacheableRequest();
|
|
54
76
|
```
|
|
55
77
|
|
|
56
78
|
## Storage Adapters
|
|
@@ -66,7 +88,7 @@ npm install @keyv/redis
|
|
|
66
88
|
And then you can pass `CacheableRequest` your connection string:
|
|
67
89
|
|
|
68
90
|
```js
|
|
69
|
-
const cacheableRequest = new CacheableRequest(http.request, 'redis://user:pass@localhost:6379');
|
|
91
|
+
const cacheableRequest = new CacheableRequest(http.request, 'redis://user:pass@localhost:6379').createCacheableRequest();
|
|
70
92
|
```
|
|
71
93
|
|
|
72
94
|
[View all official Keyv storage adapters.](https://github.com/jaredwray/keyv#official-storage-adapters)
|
|
@@ -83,7 +105,7 @@ const storageAdapter = require('./my-storage-adapter');
|
|
|
83
105
|
const QuickLRU = require('quick-lru');
|
|
84
106
|
const storageAdapter = new QuickLRU({ maxSize: 1000 });
|
|
85
107
|
|
|
86
|
-
const cacheableRequest = new CacheableRequest(http.request, storageAdapter);
|
|
108
|
+
const cacheableRequest = new CacheableRequest(http.request, storageAdapter).createCacheableRequest();
|
|
87
109
|
```
|
|
88
110
|
|
|
89
111
|
View the [Keyv docs](https://github.com/jaredwray/keyv) for more information on how to use storage adapters.
|
|
@@ -199,6 +221,30 @@ cacheableRequest('example.com', cb)
|
|
|
199
221
|
});
|
|
200
222
|
```
|
|
201
223
|
|
|
224
|
+
## Add Hooks
|
|
225
|
+
|
|
226
|
+
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.
|
|
227
|
+
|
|
228
|
+
```js
|
|
229
|
+
import http from 'http';
|
|
230
|
+
import CacheableRequest from 'cacheable-request';
|
|
231
|
+
|
|
232
|
+
const cacheableRequest = new CacheableRequest(request, cache).createCacheableRequest();
|
|
233
|
+
cacheableRequest.addHook('response', async (response: any) => {
|
|
234
|
+
const buffer = await pm(gunzip)(response);
|
|
235
|
+
return buffer.toString();
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
## Remove Hooks
|
|
241
|
+
|
|
242
|
+
You can also remove hook by using below
|
|
243
|
+
|
|
244
|
+
```js
|
|
245
|
+
CacheableRequest.removeHook('response');
|
|
246
|
+
```
|
|
247
|
+
|
|
202
248
|
**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
249
|
|
|
204
250
|
## License
|
package/package.json
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cacheable-request",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "10.0.0",
|
|
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
|
+
"exports": "./dist/index.js",
|
|
9
10
|
"types": "./dist/index.d.ts",
|
|
10
11
|
"engines": {
|
|
11
12
|
"node": ">=14.16"
|
|
12
13
|
},
|
|
13
14
|
"scripts": {
|
|
14
|
-
"test": "xo && jest --coverage",
|
|
15
|
+
"test": "xo && NODE_OPTIONS=--experimental-vm-modules jest --coverage ",
|
|
15
16
|
"build": "tsc --project tsconfig.build.json",
|
|
16
17
|
"clean": "rm -rf node_modules && rm -rf ./coverage && rm -rf ./package-lock.json && rm -rf ./test/testdb.sqlite && rm -rf ./dist"
|
|
17
18
|
},
|
|
@@ -33,39 +34,41 @@
|
|
|
33
34
|
"dependencies": {
|
|
34
35
|
"get-stream": "^6.0.1",
|
|
35
36
|
"http-cache-semantics": "^4.1.0",
|
|
36
|
-
"keyv": "^4.
|
|
37
|
-
"mimic-response": "^
|
|
38
|
-
"normalize-url": "^
|
|
39
|
-
"responselike": "^
|
|
37
|
+
"keyv": "^4.4.1",
|
|
38
|
+
"mimic-response": "^4.0.0",
|
|
39
|
+
"normalize-url": "^7.0.3",
|
|
40
|
+
"responselike": "^3.0.0"
|
|
40
41
|
},
|
|
41
42
|
"devDependencies": {
|
|
42
|
-
"@
|
|
43
|
-
"@babel/preset-env": "^7.18.2",
|
|
44
|
-
"@keyv/sqlite": "^3.5.2",
|
|
43
|
+
"@keyv/sqlite": "^3.5.3",
|
|
45
44
|
"@types/create-test-server": "^3.0.1",
|
|
46
45
|
"@types/delay": "^3.1.0",
|
|
47
46
|
"@types/get-stream": "^3.0.2",
|
|
48
47
|
"@types/http-cache-semantics": "^4.0.1",
|
|
49
|
-
"@types/jest": "^28.1.
|
|
50
|
-
"@types/node": "^18.
|
|
51
|
-
"@types/pify": "^5.0.1",
|
|
48
|
+
"@types/jest": "^28.1.7",
|
|
49
|
+
"@types/node": "^18.7.9",
|
|
52
50
|
"@types/responselike": "^1.0.0",
|
|
53
51
|
"@types/sqlite3": "^3.1.8",
|
|
54
52
|
"create-test-server": "3.0.1",
|
|
55
|
-
"eslint-plugin-jest": "^26.5.3",
|
|
56
53
|
"delay": "^5.0.0",
|
|
57
|
-
"jest": "^
|
|
58
|
-
"
|
|
59
|
-
"sqlite3": "^5.0.
|
|
60
|
-
"ts-jest": "^28.0.
|
|
61
|
-
"ts-
|
|
54
|
+
"eslint-plugin-jest": "^26.8.7",
|
|
55
|
+
"jest": "^28.1.3",
|
|
56
|
+
"sqlite3": "^5.0.11",
|
|
57
|
+
"ts-jest": "^28.0.8",
|
|
58
|
+
"ts-jest-resolver": "^2.0.0",
|
|
59
|
+
"ts-node": "^10.9.1",
|
|
62
60
|
"typescript": "^4.7.4",
|
|
63
|
-
"xo": "^0.
|
|
61
|
+
"xo": "^0.51.0"
|
|
64
62
|
},
|
|
65
63
|
"jest": {
|
|
64
|
+
"extensionsToTreatAsEsm": [
|
|
65
|
+
".ts"
|
|
66
|
+
],
|
|
67
|
+
"resolver": "ts-jest-resolver",
|
|
66
68
|
"globals": {
|
|
67
69
|
"ts-jest": {
|
|
68
|
-
"tsconfig": "./tsconfig.json"
|
|
70
|
+
"tsconfig": "./tsconfig.build.json",
|
|
71
|
+
"useESM": true
|
|
69
72
|
}
|
|
70
73
|
},
|
|
71
74
|
"moduleFileExtensions": [
|
|
@@ -76,7 +79,7 @@
|
|
|
76
79
|
"^.+\\.(ts|tsx)$": "ts-jest"
|
|
77
80
|
},
|
|
78
81
|
"testMatch": [
|
|
79
|
-
"**/test/*.test.(ts)"
|
|
82
|
+
"**/test/*.test.(ts|js)"
|
|
80
83
|
],
|
|
81
84
|
"testEnvironment": "node"
|
|
82
85
|
},
|
|
@@ -88,18 +91,16 @@
|
|
|
88
91
|
"plugin:jest/recommended"
|
|
89
92
|
],
|
|
90
93
|
"rules": {
|
|
91
|
-
"@typescript-eslint/
|
|
92
|
-
"@typescript-eslint/
|
|
94
|
+
"@typescript-eslint/triple-slash-reference": 0,
|
|
95
|
+
"@typescript-eslint/no-namespace": 0,
|
|
93
96
|
"@typescript-eslint/no-unsafe-assignment": 0,
|
|
94
97
|
"@typescript-eslint/no-unsafe-call": 0,
|
|
95
98
|
"@typescript-eslint/ban-types": 0,
|
|
96
99
|
"@typescript-eslint/restrict-template-expressions": 0,
|
|
97
100
|
"@typescript-eslint/no-unsafe-return": 0,
|
|
98
101
|
"new-cap": 0,
|
|
99
|
-
"@typescript-eslint/no-empty-function": 0,
|
|
100
102
|
"@typescript-eslint/restrict-plus-operands": 0,
|
|
101
|
-
"@typescript-eslint/no-
|
|
102
|
-
"@typescript-eslint/await-thenable": 0
|
|
103
|
+
"@typescript-eslint/no-implicit-any-catch": 0
|
|
103
104
|
}
|
|
104
105
|
}
|
|
105
106
|
}
|