-
Notifications
You must be signed in to change notification settings - Fork 12
/
utils.js
114 lines (97 loc) · 2.93 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
'use strict';
const { normalize, isString, isPlainObject } = require('@sentry/utils');
const cookie = require('cookie');
/* istanbul ignore next */
function isAutoSessionTrackingEnabled(client) {
if (client === undefined) {
return false;
}
const clientOptions = client && client.getOptions();
if (clientOptions && clientOptions.autoSessionTracking !== undefined) {
return clientOptions.autoSessionTracking;
}
return false;
}
exports.isAutoSessionTrackingEnabled = isAutoSessionTrackingEnabled;
function tryToExtractBody(req) {
if (req.body !== undefined) {
return isString(req.body) ? req.body : JSON.stringify(normalize(req.body));
}
}
exports.tryToExtractBody = tryToExtractBody;
function extractRequestData(req, keys) {
const extracted = {};
for (const key of keys) {
switch (key) {
case 'headers':
extracted.headers = req.headers;
break;
case 'method':
extracted.method = req.method;
break;
case 'url':
{
const host = req.hostname;
extracted.url = `${req.protocol}://${host}${req.url}`;
}
break;
case 'cookies':
if (extracted.headers) {
extracted.cookies = cookie.parse(extracted.headers.cookie || '');
}
break;
case 'query_string':
extracted.query_string = req.query;
break;
case 'data':
if (req.method === 'GET' || req.method === 'HEAD') {
break;
}
if (req.body !== undefined) {
extracted.data = tryToExtractBody(req);
}
break;
}
}
return extracted;
}
exports.extractRequestData = extractRequestData;
/** Default user keys that'll be used to extract data from the request */
const DEFAULT_USER_KEYS = ['id', 'username', 'email'];
function extractUserData(request) {
if (!isPlainObject(request.user)) {
return {};
}
const extractedUser = {};
const user = request.user;
for (const key of DEFAULT_USER_KEYS) {
if (key in user) {
extractedUser[key] = user[key];
}
}
return extractedUser;
}
exports.extractUserData = extractUserData;
const getTransactionName = (request) => {
return `${request.method} ${request.routeOptions.url}`;
};
exports.getTransactionName = getTransactionName;
const extractPathForTransaction = (request, getName = getTransactionName) => {
const name = getName(request);
let source = 'url';
return [name, source];
};
exports.extractPathForTransaction = extractPathForTransaction;
const shouldHandleError = function (error, request, reply) {
return reply.statusCode >= 500;
};
exports.shouldHandleError = shouldHandleError;
const errorResponse = function (error, request, reply) {
// @fastify/sensible explicit internal errors support
if (reply.statusCode === 500 && error.explicitInternalServerError !== true) {
reply.send(new Error('Something went wrong'));
} else {
reply.send(error);
}
};
exports.errorResponse = errorResponse;