@momsfriendlydevco/cowboy 1.6.0 → 1.6.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.
|
@@ -13,7 +13,7 @@ function sortKeys(o) {
|
|
|
13
13
|
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
|
-
* Cowboy middleware which will return a 304 if the incoming eTag matches the hash of the last identical response
|
|
16
|
+
* Cowboy middleware which will return a 304 if the incoming eTag matches the hash of the last identical payload response
|
|
17
17
|
* If hitting the same endpoints over and over this can significantly improve response times
|
|
18
18
|
*
|
|
19
19
|
* @param {Object} [options] Additional options to mutate behaviour
|
|
@@ -35,17 +35,16 @@ export default function CowboyEtagCaching(options) {
|
|
|
35
35
|
);
|
|
36
36
|
},
|
|
37
37
|
payload(req, res, settings) { // eslint-disable-line no-unused-vars
|
|
38
|
-
|
|
38
|
+
return {
|
|
39
39
|
method: req.method,
|
|
40
40
|
query: req.query,
|
|
41
41
|
url: req.path,
|
|
42
42
|
body: res.body,
|
|
43
43
|
};
|
|
44
|
-
return payload;
|
|
45
44
|
},
|
|
46
45
|
textEncoder: new TextEncoder(),
|
|
47
46
|
async hasher(obj, settings) {
|
|
48
|
-
let text = JSON.stringify(sortKeys(obj));
|
|
47
|
+
let text = typeof obj == 'string' ? obj : JSON.stringify(sortKeys(obj));
|
|
49
48
|
let data = settings.textEncoder.encode(text);
|
|
50
49
|
let hashBuffer = await crypto.subtle.digest('SHA-256', data);
|
|
51
50
|
let hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
@@ -72,10 +71,11 @@ export default function CowboyEtagCaching(options) {
|
|
|
72
71
|
|
|
73
72
|
settings.debug('Enabled for request', req.path);
|
|
74
73
|
|
|
75
|
-
// Queue up interceptor
|
|
74
|
+
// Queue up interceptor to handle the eventual middleware chain output
|
|
76
75
|
res.beforeServe(async ()=> {
|
|
77
|
-
|
|
78
|
-
|
|
76
|
+
let dataType = typeof res.body;
|
|
77
|
+
if (!['object', 'string'].includes(dataType)) {
|
|
78
|
+
settings.debug(`Refusing to cache - res.body data type is "${dataType}" and not a POJO or plain String`);
|
|
79
79
|
return;
|
|
80
80
|
}
|
|
81
81
|
|