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.
Files changed (3) hide show
  1. package/README.md +3 -2
  2. package/package.json +54 -55
  3. package/src/index.js +83 -23
package/README.md CHANGED
@@ -4,6 +4,7 @@
4
4
 
5
5
  [![Build Status](https://travis-ci.org/lukechilds/cacheable-request.svg?branch=master)](https://travis-ci.org/lukechilds/cacheable-request)
6
6
  [![Coverage Status](https://coveralls.io/repos/github/lukechilds/cacheable-request/badge.svg?branch=master)](https://coveralls.io/github/lukechilds/cacheable-request?branch=master)
7
+ [![npm](https://img.shields.io/npm/dm/cacheable-request.svg)](https://www.npmjs.com/package/cacheable-request)
7
8
  [![npm](https://img.shields.io/npm/v/cacheable-request.svg)](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 --save cacheable-request
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 --save @keyv/redis
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
- "name": "cacheable-request",
3
- "version": "2.1.3",
4
- "description": "Wrap native HTTP requests with RFC compliant cache support",
5
- "main": "src/index.js",
6
- "scripts": {
7
- "test": "xo && nyc ava",
8
- "coverage": "nyc report --reporter=text-lcov | coveralls"
9
- },
10
- "xo": {
11
- "extends": "xo-lukechilds"
12
- },
13
- "repository": {
14
- "type": "git",
15
- "url": "git+https://github.com/lukechilds/cacheable-request.git"
16
- },
17
- "keywords": [
18
- "HTTP",
19
- "HTTPS",
20
- "cache",
21
- "caching",
22
- "layer",
23
- "cacheable",
24
- "RFC 7234",
25
- "RFC",
26
- "7234",
27
- "compliant"
28
- ],
29
- "author": "Luke Childs <lukechilds123@gmail.com> (http://lukechilds.co.uk)",
30
- "license": "MIT",
31
- "bugs": {
32
- "url": "https://github.com/lukechilds/cacheable-request/issues"
33
- },
34
- "homepage": "https://github.com/lukechilds/cacheable-request",
35
- "dependencies": {
36
- "clone-response": "1.0.2",
37
- "get-stream": "3.0.0",
38
- "http-cache-semantics": "3.8.1",
39
- "keyv": "3.0.0",
40
- "lowercase-keys": "1.0.0",
41
- "normalize-url": "2.0.0",
42
- "responselike": "1.0.2"
43
- },
44
- "devDependencies": {
45
- "@keyv/sqlite": "^1.2.6",
46
- "ava": "^0.24.0",
47
- "coveralls": "^3.0.0",
48
- "create-test-server": "^2.0.0",
49
- "delay": "^2.0.0",
50
- "eslint-config-xo-lukechilds": "^1.0.0",
51
- "nyc": "^11.0.2",
52
- "pify": "^3.0.0",
53
- "sqlite3": "^3.1.9",
54
- "this": "^1.0.2",
55
- "xo": "^0.19.0"
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
- opts = urlLib.parse(opts);
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 = Object.assign({
44
+ opts = {
34
45
  headers: {},
35
46
  method: 'GET',
36
47
  cache: true,
37
48
  strictTtl: false,
38
- automaticFailover: false
39
- }, opts);
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 url = normalizeUrl(urlLib.format(opts));
44
- const key = `${opts.method}:${url}`;
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
- getStream.buffer(response)
70
- .then(body => {
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
- return this.cache.set(key, value, ttl);
79
- })
80
- .catch(err => ee.emit('error', new CacheableRequest.CacheError(err)));
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
- this.cache.delete(key)
83
- .catch(err => ee.emit('error', new CacheableRequest.CacheError(err)));
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
- const get = opts => Promise.resolve()
101
- .then(() => opts.cache ? this.cache.get(key) : undefined)
102
- .then(cacheEntry => {
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
- this.cache.on('error', err => ee.emit('error', new CacheableRequest.CacheError(err)));
156
+ this.cache.on('error', err => ee.emit('error', new CacheableRequest.CacheError(err)));
126
157
 
127
- get(opts).catch(err => {
128
- if (opts.automaticFailover && !madeRequest) {
129
- makeRequest(opts);
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
- ee.emit('error', new CacheableRequest.CacheError(err));
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);