forked from sugarcrm/thorn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.js
72 lines (68 loc) · 2.6 KB
/
debug.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
/*
* Copyright (c) 2017 SugarCRM Inc. Licensed by SugarCRM under the Apache 2.0 license.
*/
let _ = require('lodash');
let bulkDataCache = {};
const VERBOSE_FUNCTIONS = [
(type, data, r) => {
let msg;
switch (type) {
case 'request':
msg = `Request ${data.debugId} ${r.headers['X-Thorn']}: ${r.method} ${r.uri.pathname}`;
break;
case 'response':
msg = `Response ${data.debugId} ${r.headers['X-Thorn']}: ${r.method} ${r.uri.pathname} ${
data.statusCode}`;
break;
case 'redirect':
case 'auth':
msg = `Redirect: ${data.statusCode} ${data.uri}`;
break;
default:
msg = `Unidentified event ${type}`;
break;
}
console.info(msg);
},
(type, data, r) => {
let requests;
let responses;
if (r.method === 'POST' && r.uri.pathname.indexOf('bulk') >= 0) {
switch (type) {
case 'request':
// cache the method and URL of each part of the bulk request,
// because the response doesn't disclose that information
bulkDataCache[data.debugId] = [];
requests = JSON.parse(data.body).requests;
_.each(requests, (req, index) => {
bulkDataCache[data.debugId].push({method: req.method, url: req.url});
console.info(`\tBulk Request ${index + 1}: ${req.method} ${req.url}`);
});
break;
case 'response':
if (!data.body) {
console.warn('\tBulk Response: no body');
break;
}
responses = data.body;
if (typeof responses === 'string') {
responses = JSON.parse(responses);
}
if (!_.isArray(responses)) {
responses = [responses];
}
_.each(responses, (resp, index) => {
let req = bulkDataCache[data.debugId][index];
let msg = `\tBulk Response ${index + 1}: ${req.method} ${req.url} ${
resp.status}`;
console.info(msg);
});
delete bulkDataCache[data.debugId];
break;
default:
break;
}
}
},
];
export {VERBOSE_FUNCTIONS};