This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
forked from jpodwys/superagent-cache-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
320 lines (306 loc) · 10.3 KB
/
utils.js
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
const CachePolicy = require('http-cache-semantics');
module.exports = {
/**
* Generate a cache key unique to this query
* @param {superagent} agent
* @param {object} reg
* @param {object} cProps
*/
keygen: function(req, props){
var cleanParams = null;
var cleanOptions = null;
var params = this.getQueryParams(req);
var options = this.getHeaderOptions(req);
// prune headers together with revalidation headers added internally by superagent
// and optional 'bypassHeaders' which are likely changing per request and should not
// be used to calculate the cache key.
props.pruneHeader = ['if-none-match', 'if-modified-since']
.concat(props.pruneHeader || [], props.bypassHeaders || []);
if(props.pruneQuery || props.pruneHeader){
cleanParams = (props.pruneQuery) ? this.pruneObj(this.cloneObject(params), props.pruneQuery) : params;
cleanOptions = (props.pruneHeader) ? this.pruneObj(this.cloneObject(options), props.pruneHeader, true) : options;
}
return JSON.stringify({
method: req.method,
uri: req.url,
params: cleanParams || params || null,
options: cleanOptions || options || null
});
},
/**
* Find and extract query params
* @param {object} reg
*/
getQueryParams: function(req){
if(req && req.qs && !this.isEmpty(req.qs)){
return req.qs;
}
else if(req && req.qsRaw){
return this.arrayToObj(req.qsRaw);
}
else if(req && req.req){
return this.stringToObj(req.req.path);
}
else if(req && req._query){
return this.stringToObj(req._query.join('&'));
}
return null;
},
/**
* Find and extract headers
* @param {object} reg
*/
getHeaderOptions: function(req){
// I have to remove the User-Agent header ever since superagent 1.7.0
// The cache-control header must also be removed.
// Clone the request first, as we don't want to remove the headers from the original one, do we?
const _req = req ? JSON.parse(JSON.stringify(req)) : req;
const headersToPrune = ['user-agent', 'cache-control'];
if(_req && _req.headers){
return this.pruneObj(_req.headers, headersToPrune);
}
else if(_req && _req._header){
return this.pruneObj(_req._header, headersToPrune);
}
else if(_req && _req.req && _req.req._headers){
return this.pruneObj(_req.req._headers, headersToPrune);
}
else if(_req && _req.header){
return this.pruneObj(_req.header, headersToPrune);
}
return null;
},
/**
* Convert an array to an object
* @param {array} arr
*/
arrayToObj: function(arr){
if(arr && arr.length){
var obj = {};
for(var i = 0; i < arr.length; i++){
var str = arr[i];
var kvArray = str.split('&');
for(var j = 0; j < kvArray.length; j++){
var kvString = kvArray[j].split('=');
obj[kvString[0]] = kvString[1];
}
}
return obj;
}
return null;
},
/**
* Convert a string to an object
* @param {string} str
*/
stringToObj: function(str){
if(str){
var obj = {};
if(~str.indexOf('?')){
var strs = str.split('?');
str = strs[1];
}
var kvArray = str.split('&');
for(var i = 0; i < kvArray.length; i++){
var kvString = kvArray[i].split('=');
obj[kvString[0]] = kvString[1];
}
return obj;
}
return null;
},
/**
* Remove properties from an object
* @param {object} obj
* @param {array} props
* @param {boolean} isOptions
*/
pruneObj: function(obj, props, isOptions){
const lowerCasedProps = props.map(function (item) {
return item.toLowerCase();
});
Object.keys(obj).forEach(function (key) {
if (lowerCasedProps.indexOf(key.toLowerCase()) !== -1) {
if(isOptions){
delete obj[key.toLowerCase()];
}
delete obj[key];
}
});
return obj;
},
/**
* Simplify superagent's http response object
* @param {object} r - The response.
* @param {object} Request - The superagent's Request instance.
* @param {object} props - The request properties.
*/
gutResponse: function(r, Request, props){
var newResponse = {};
newResponse.req = Request.toJSON();
newResponse.body = r.body;
newResponse.text = r.text;
newResponse.header = r.header;
newResponse.headers = r.header;
newResponse.statusCode = r.statusCode;
newResponse.status = r.status;
newResponse.ok = r.ok;
return newResponse;
},
/**
* Determine whether a value is considered empty
* @param {*} val
*/
isEmpty: function(val){
return (val === false || val === null || (typeof val == 'object' && Object.keys(val).length == 0));
},
/**
* Return a clone of an object
* @param {object} obj
*/
cloneObject: function(obj){
var newObj = {};
for(var attr in obj) {
if (obj.hasOwnProperty(attr)){
newObj[attr] = obj[attr];
}
}
return newObj;
},
/**
* Reset superagent-cache's default query properties using the defaults object
* @param {object} d
*/
resetProps: function(d){
return {
doQuery: (typeof d.doQuery === 'boolean') ? d.doQuery : true,
cacheWhenEmpty: (typeof d.cacheWhenEmpty === 'boolean') ? d.cacheWhenEmpty : true,
prune: d.prune,
pruneQuery: d.pruneQuery,
pruneHeader: d.pruneHeader,
responseProp: d.responseProp,
expiration: d.expiration,
forceUpdate: d.forceUpdate,
preventDuplicateCalls: d.preventDuplicateCalls,
backgroundRefresh: d.backgroundRefresh,
bypassHeaders: d.bypassHeaders
};
},
/**
* Handle the varying number of callback output params
* @param {function} cb
* @param {object} err
* @param {object} response
* @param {string} key
* @param {object} [Request] - Superagent Request instance. When provided it will emit the events.
* @param {object} props - The request internal properties.
*/
callbackExecutor: function(cb, err, response, key, Request){
if (response) {
// Superagent response should bear only the 'header' attribute, this was only needed for the policy.
delete response.headers;
if (Request) {
Request.emit('request', Request);
if (err) {
Request.emit('error', err);
} else {
Request.emit('response', response);
}
}
}
if(cb.length === 1){
cb(response);
}
else if(cb.length > 1){
cb(err, response, key);
}
else{
throw new Error('UnsupportedCallbackException: Your .end() callback must pass at least one argument.');
}
},
/**
* Handles the request cache headers and eventually modifies the per request properties affecting caching.
* This method is called in early stage, before any attempt to execute the request against the HTTP server.
*
* @param {object} req - The request object.
* @param {object} props - The request-basis properties, which affect cache behavior.
* @returns {object} The modified properties.
*/
handleReqCacheHeaders: function (req, props) {
const cacheControl = req.get('cache-control');
if (typeof cacheControl === 'string') {
if (cacheControl.toLowerCase().indexOf('only-if-cached') !== -1) {
props.doQuery = false;
}
// the expiration can also be set via the Request header.
const maxAgeMatch = cacheControl.toLowerCase().match(/^(.*max-age=)(\d*).*$/);
if (maxAgeMatch && maxAgeMatch.length > 2 && maxAgeMatch[2] !== '') {
props.expiration = parseInt(maxAgeMatch[2]);
}
}
// We cheat the policy a bit here, giving the request instead of response (we don't have it at this stage),
// as we want to parse the request headers for the caching control related values,
// which could override the 'props' values.
const policy = new CachePolicy(req.toJSON(), req.toJSON());
// The 'no-store' will be checked here.
// Note: The default 'policy.timeToLive()' is '0' (means when there's no Expires or max-age specified).
// The legacy method 'expiration()' will set the policy TTL value via the Cache-Control max-age value,
// so no conflicts here.
props.expiration = policy.storable() ? Math.round(policy.timeToLive() / 1000) : 0;
},
/**
* Returns the `expiration` (TTL) value calculated as a minimum of the `props.expiration` and `policy.timeToLive`.
* The resulting value is multiplied by `2`due to enable further handling of the stale cache entries,
* (when the policy allows for that).
* The resulting value is to be used for underlying cache implementation.
*
* @param {object} props - The request-basis properties, which affect cache behavior.
* @param {object} policy - The cache policy.
* @returns {number} The expiration (TTL) time in seconds.
*/
getExpiration: function (props, policy) {
return Math.min(props.expiration * 2 || Number.MAX_VALUE, Math.round(policy.timeToLive() * 2 / 1000));
},
/**
* Sets the response header value.
*
* @param {object} response - The response instance.
* @param {string} name - The header name.
* @param {string} value - The header value.
* @returns {object} The incoming modified response.
*/
setResponseHeader: function (response, name, value) {
// both need to be checked as someone could do strange things with 'prune' or 'responseProp'.
if (response) {
if (response.header) {
response.header[name] = value;
}
if (response.headers) {
response.headers[name] = value;
}
}
return response;
},
/**
* Copies the header values declared with the `bypassHeaders` option from current request
* to a cached response headers and its bound request headers.
*
* @param {object} response - The response instance.
* @param {object} req - The request object.
* @param {object} props - The request-basis properties, which affect cache behavior.
* @returns {object} The incoming modified response.
*/
copyBypassHeaders: function (response, req, props) {
const self = this;
if (props.bypassHeaders && props.bypassHeaders.forEach) {
props.bypassHeaders.forEach(function (name) {
const value = req.get(name);
self.setResponseHeader(response, name, value);
if (response.req && response.req.headers) {
response.req.headers[name] = value;
}
});
}
return response;
}
}