1 | Object.defineProperty(exports, "__esModule", { value: true });
|
2 | var tslib_1 = require("tslib");
|
3 | var apm_1 = require("@sentry/apm");
|
4 | var core_1 = require("@sentry/core");
|
5 | var utils_1 = require("@sentry/utils");
|
6 | var cookie = require("cookie");
|
7 | var domain = require("domain");
|
8 | var os = require("os");
|
9 | var url = require("url");
|
10 | var sdk_1 = require("./sdk");
|
11 | var DEFAULT_SHUTDOWN_TIMEOUT = 2000;
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | function tracingHandler() {
|
17 | return function sentryTracingMiddleware(req, res, next) {
|
18 |
|
19 |
|
20 | var reqMethod = (req.method || '').toUpperCase();
|
21 | var reqUrl = req.url;
|
22 | var hub = core_1.getCurrentHub();
|
23 | var transaction = hub.startSpan({
|
24 | transaction: reqMethod + "|" + reqUrl,
|
25 | });
|
26 | hub.configureScope(function (scope) {
|
27 | scope.setSpan(transaction);
|
28 | });
|
29 | res.once('finish', function () {
|
30 | transaction.setHttpStatus(res.statusCode);
|
31 | transaction.finish();
|
32 | });
|
33 | next();
|
34 | };
|
35 | }
|
36 | exports.tracingHandler = tracingHandler;
|
37 |
|
38 | function extractTransaction(req, type) {
|
39 | try {
|
40 |
|
41 | var request = req;
|
42 | switch (type) {
|
43 | case 'path': {
|
44 | return request.route.path;
|
45 | }
|
46 | case 'handler': {
|
47 | return request.route.stack[0].name;
|
48 | }
|
49 | case 'methodPath':
|
50 | default: {
|
51 | var method = request.method.toUpperCase();
|
52 | var path = request.route.path;
|
53 | return method + "|" + path;
|
54 | }
|
55 | }
|
56 | }
|
57 | catch (_oO) {
|
58 | return undefined;
|
59 | }
|
60 | }
|
61 |
|
62 | var DEFAULT_REQUEST_KEYS = ['cookies', 'data', 'headers', 'method', 'query_string', 'url'];
|
63 |
|
64 | function extractRequestData(req, keys) {
|
65 | var request = {};
|
66 | var attributes = Array.isArray(keys) ? keys : DEFAULT_REQUEST_KEYS;
|
67 |
|
68 |
|
69 |
|
70 | var headers = (req.headers || req.header || {});
|
71 |
|
72 |
|
73 | var method = req.method;
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | var host = req.hostname || req.host || headers.host || '<no host>';
|
79 |
|
80 |
|
81 |
|
82 | var protocol = req.protocol === 'https' || req.secure || (req.socket || {}).encrypted
|
83 | ? 'https'
|
84 | : 'http';
|
85 |
|
86 |
|
87 |
|
88 | var originalUrl = (req.originalUrl || req.url);
|
89 |
|
90 | var absoluteUrl = protocol + "://" + host + originalUrl;
|
91 | attributes.forEach(function (key) {
|
92 | switch (key) {
|
93 | case 'headers':
|
94 | request.headers = headers;
|
95 | break;
|
96 | case 'method':
|
97 | request.method = method;
|
98 | break;
|
99 | case 'url':
|
100 | request.url = absoluteUrl;
|
101 | break;
|
102 | case 'cookies':
|
103 |
|
104 |
|
105 | request.cookies = cookie.parse(headers.cookie || '');
|
106 | break;
|
107 | case 'query_string':
|
108 |
|
109 |
|
110 |
|
111 | request.query_string = url.parse(originalUrl || '', false).query;
|
112 | break;
|
113 | case 'data':
|
114 |
|
115 |
|
116 | var data = req.body;
|
117 | if (method === 'GET' || method === 'HEAD') {
|
118 | if (typeof data === 'undefined') {
|
119 | data = '<unavailable>';
|
120 | }
|
121 | }
|
122 | if (data && !utils_1.isString(data)) {
|
123 |
|
124 | data = JSON.stringify(utils_1.normalize(data));
|
125 | }
|
126 | request.data = data;
|
127 | break;
|
128 | default:
|
129 | if ({}.hasOwnProperty.call(req, key)) {
|
130 | request[key] = req[key];
|
131 | }
|
132 | }
|
133 | });
|
134 | return request;
|
135 | }
|
136 |
|
137 | var DEFAULT_USER_KEYS = ['id', 'username', 'email'];
|
138 |
|
139 | function extractUserData(user, keys) {
|
140 | var extractedUser = {};
|
141 | var attributes = Array.isArray(keys) ? keys : DEFAULT_USER_KEYS;
|
142 | attributes.forEach(function (key) {
|
143 | if (user && key in user) {
|
144 | extractedUser[key] = user[key];
|
145 | }
|
146 | });
|
147 | return extractedUser;
|
148 | }
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 | function parseRequest(event, req, options) {
|
158 |
|
159 | options = tslib_1.__assign({ ip: false, request: true, serverName: true, transaction: true, user: true, version: true }, options);
|
160 | if (options.version) {
|
161 | event.extra = tslib_1.__assign({}, event.extra, { node: global.process.version });
|
162 | }
|
163 | if (options.request) {
|
164 | event.request = tslib_1.__assign({}, event.request, extractRequestData(req, options.request));
|
165 | }
|
166 | if (options.serverName && !event.server_name) {
|
167 | event.server_name = global.process.env.SENTRY_NAME || os.hostname();
|
168 | }
|
169 | if (options.user) {
|
170 | var extractedUser = req.user ? extractUserData(req.user, options.user) : {};
|
171 | if (Object.keys(extractedUser)) {
|
172 | event.user = tslib_1.__assign({}, event.user, extractedUser);
|
173 | }
|
174 | }
|
175 |
|
176 |
|
177 |
|
178 | if (options.ip) {
|
179 | var ip = req.ip || (req.connection && req.connection.remoteAddress);
|
180 | if (ip) {
|
181 | event.user = tslib_1.__assign({}, event.user, { ip_address: ip });
|
182 | }
|
183 | }
|
184 | if (options.transaction && !event.transaction) {
|
185 | var transaction = extractTransaction(req, options.transaction);
|
186 | if (transaction) {
|
187 | event.transaction = transaction;
|
188 | }
|
189 | }
|
190 | return event;
|
191 | }
|
192 | exports.parseRequest = parseRequest;
|
193 |
|
194 |
|
195 |
|
196 |
|
197 | function requestHandler(options) {
|
198 | return function sentryRequestMiddleware(req, res, next) {
|
199 | if (options && options.flushTimeout && options.flushTimeout > 0) {
|
200 |
|
201 | var _end_1 = res.end;
|
202 | res.end = function (chunk, encoding, cb) {
|
203 | var _this = this;
|
204 | sdk_1.flush(options.flushTimeout)
|
205 | .then(function () {
|
206 | _end_1.call(_this, chunk, encoding, cb);
|
207 | })
|
208 | .then(null, function (e) {
|
209 | utils_1.logger.error(e);
|
210 | });
|
211 | };
|
212 | }
|
213 | var local = domain.create();
|
214 | local.add(req);
|
215 | local.add(res);
|
216 | local.on('error', next);
|
217 | local.run(function () {
|
218 | core_1.getCurrentHub().configureScope(function (scope) {
|
219 | return scope.addEventProcessor(function (event) { return parseRequest(event, req, options); });
|
220 | });
|
221 | next();
|
222 | });
|
223 | };
|
224 | }
|
225 | exports.requestHandler = requestHandler;
|
226 |
|
227 | function getStatusCodeFromResponse(error) {
|
228 | var statusCode = error.status || error.statusCode || error.status_code || (error.output && error.output.statusCode);
|
229 | return statusCode ? parseInt(statusCode, 10) : 500;
|
230 | }
|
231 |
|
232 | function defaultShouldHandleError(error) {
|
233 | var status = getStatusCodeFromResponse(error);
|
234 | return status >= 500;
|
235 | }
|
236 |
|
237 |
|
238 |
|
239 |
|
240 | function errorHandler(options) {
|
241 | return function sentryErrorMiddleware(error, req, res, next) {
|
242 | var shouldHandleError = (options && options.shouldHandleError) || defaultShouldHandleError;
|
243 | if (shouldHandleError(error)) {
|
244 | core_1.withScope(function (scope) {
|
245 | if (req.headers && utils_1.isString(req.headers['sentry-trace'])) {
|
246 | var span = apm_1.Span.fromTraceparent(req.headers['sentry-trace']);
|
247 | scope.setSpan(span);
|
248 | }
|
249 | var eventId = core_1.captureException(error);
|
250 | res.sentry = eventId;
|
251 | next(error);
|
252 | });
|
253 | return;
|
254 | }
|
255 | next(error);
|
256 | };
|
257 | }
|
258 | exports.errorHandler = errorHandler;
|
259 |
|
260 |
|
261 |
|
262 | function logAndExitProcess(error) {
|
263 | console.error(error && error.stack ? error.stack : error);
|
264 | var client = core_1.getCurrentHub().getClient();
|
265 | if (client === undefined) {
|
266 | utils_1.logger.warn('No NodeClient was defined, we are exiting the process now.');
|
267 | global.process.exit(1);
|
268 | return;
|
269 | }
|
270 | var options = client.getOptions();
|
271 | var timeout = (options && options.shutdownTimeout && options.shutdownTimeout > 0 && options.shutdownTimeout) ||
|
272 | DEFAULT_SHUTDOWN_TIMEOUT;
|
273 | utils_1.forget(client.close(timeout).then(function (result) {
|
274 | if (!result) {
|
275 | utils_1.logger.warn('We reached the timeout for emptying the request buffer, still exiting now!');
|
276 | }
|
277 | global.process.exit(1);
|
278 | }));
|
279 | }
|
280 | exports.logAndExitProcess = logAndExitProcess;
|
281 |
|
\ | No newline at end of file |