-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
72 lines (63 loc) · 1.53 KB
/
utils.js
File metadata and controls
72 lines (63 loc) · 1.53 KB
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
const ESCAPE_DOTS_REGEPX = /\./g;
const conf = require("./conf");
module.exports = {
/**
* Creates bus subject from a HTTP request.
*
* Example:
*
* GET /foo/bar => http.get.foo.bar
*
*/
createSubject: req => {
const method = req.method.toLowerCase();
const path = req.path.replace(ESCAPE_DOTS_REGEPX, "{dot}").split("/");
let subject = ["http", method]
.concat(path)
.filter(function (val) {
return val;
})
.join(".");
if (conf.httpSubjectToLowerCase)
subject = subject.toLowerCase();
return subject;
},
/**
* Creates bus request message from a HTTP request.
*/
createRequest: (req, reqId, user) => {
let o = {
reqId: reqId,
method: req.method,
path: req.path,
query: req.query,
headers: req.headers,
data: req.body
};
if (user) {
o.user = user;
}
return o;
},
/**
* Strips data from response that should not be leaked to outside.
*/
sanitizeResponse: resp => {
var clone = Object.assign(resp);
delete clone.headers;
delete clone.user;
return clone;
},
/**
* Converts JSON object to a string that is appropriate to use
* in a HTTP header.
*
* Note that only ASCII chars are okay to use in HTTP headers. Emoji's and
* other unorthodox characters may break the request.
*/
convertJsonToHttpHeaderString: json => {
const stringifiedJson = JSON.stringify(json);
const stringWithOnlyAscii = stringifiedJson.replace(/[^\x00-\x7F]/g, ""); // ASCII filter magic taken from https://stackoverflow.com/a/20856346
return stringWithOnlyAscii;
}
};