-
Notifications
You must be signed in to change notification settings - Fork 7
/
prepareOptions.mjs
114 lines (93 loc) · 2.96 KB
/
prepareOptions.mjs
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
//
const identifyUser = (req, res, env, ctx) => {
if (!req.headers) {
return undefined;
}
const token = req.headers.get('Authorization') || res.headers.get('Authorization');
if (token) {
const jwtToken = token.split(' ')[1]; // Assuming the token is in the format "Bearer <token>"
const decodedToken = atob(jwtToken.split('.')[1]);
const tokenPayload = JSON.parse(decodedToken);
return tokenPayload.sub; // Assuming the subject of the token is the user id
}
return undefined;
};
const identifyCompany = (req, res, env, ctx) => {
return undefined;
};
const getSessionToken = (req, res, env, ctx) => {
return undefined;
};
const getApiVersion = (req, res, env, ctx) => {
return undefined;
};
const getMetadata = (req, res, env, ctx) => {
return undefined;
};
const skip = (req, res, env, ctx) => {
return false;
};
const maskContent = (moesifEvent) => {
return moesifEvent;
};
const DEFAULT_OPTIONS = {
/*********************************
* MOESIF_INSTALL
* Set Your Moesif Application Id
*********************************/
applicationId: null, // required
// log request and response bodies.
// for bodies, the middleware splits stream by default to obtain an copy to read from
// if you do not read the original request body, you may get a warning that a split was wasted.
logBody: true,
// true or false
// This will mask any credit cards by checking for the Luhn algorithm
hideCreditCards: true,
// Set to true to prevent insertion of X-Moesif-Transaction-Id response header.
// X-Moesif-Transaction-Id is helpful for identifying transactions in Moesif.
disableTransactionId: false,
// Print debug messages to console.
// Enable to share debug logs with Moesif support staff for quicker debug.
debug: false,
// Fetch timeout in milliseconds so that Moesif can log the call even if origin server doesnt respond
fetchTimeoutMS: 120000,
// limit the size of request and response body send to moesif.
requestMaxBodySize: 100000, // 100k
responseMaxBodySize: 100000, // 100k
identifyUser,
identifyCompany,
getApiVersion,
getMetadata,
getSessionToken,
skip,
maskContent,
};
function makeAppIdUrlRegexArr(urlPatterns) {
const appIdUrlRegexArr = urlPatterns
.filter((x) => x && (x.regex || x.applicationId)) // appId / urlRegEx both empty
.map(({ regex, applicationId }) => {
try {
return {
regex: new RegExp(regex),
applicationId,
};
} catch (e) {
console.error(e);
}
})
.filter((x) => x && x.regex); // filter invalid regular expressions / blank entries
return appIdUrlRegexArr;
}
function prepareOptions(options) {
var INSTALL_TYPE = 'esm';
if (!options || !options.applicationId) {
throw new Error('no moesif application id found');
}
return {
INSTALL_TYPE,
...DEFAULT_OPTIONS,
...options,
appIdUrlRegexArr: makeAppIdUrlRegexArr(options.urlPatterns || []),
};
}
export default prepareOptions;