123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- Object.defineProperty(exports, '__esModule', { value: true });
- const cookie = require('./cookie.js');
- const debugBuild = require('./debug-build.js');
- const is = require('./is.js');
- const logger = require('./logger.js');
- const normalize = require('./normalize.js');
- const url = require('./url.js');
- const DEFAULT_INCLUDES = {
- ip: false,
- request: true,
- transaction: true,
- user: true,
- };
- const DEFAULT_REQUEST_INCLUDES = ['cookies', 'data', 'headers', 'method', 'query_string', 'url'];
- const DEFAULT_USER_INCLUDES = ['id', 'username', 'email'];
- function addRequestDataToTransaction(
- transaction,
- req,
- deps,
- ) {
- if (!transaction) return;
-
- if (!transaction.metadata.source || transaction.metadata.source === 'url') {
-
- const [name, source] = extractPathForTransaction(req, { path: true, method: true });
- transaction.updateName(name);
-
-
- transaction.setMetadata({ source });
- }
- transaction.setAttribute('url', req.originalUrl || req.url);
- if (req.baseUrl) {
- transaction.setAttribute('baseUrl', req.baseUrl);
- }
-
-
- transaction.setData('query', extractQueryParams(req, deps));
- }
- function extractPathForTransaction(
- req,
- options = {},
- ) {
- const method = req.method && req.method.toUpperCase();
- let path = '';
- let source = 'url';
-
- if (options.customRoute || req.route) {
- path = options.customRoute || `${req.baseUrl || ''}${req.route && req.route.path}`;
- source = 'route';
- }
-
- else if (req.originalUrl || req.url) {
- path = url.stripUrlQueryAndFragment(req.originalUrl || req.url || '');
- }
- let name = '';
- if (options.method && method) {
- name += method;
- }
- if (options.method && options.path) {
- name += ' ';
- }
- if (options.path && path) {
- name += path;
- }
- return [name, source];
- }
- function extractTransaction(req, type) {
- switch (type) {
- case 'path': {
- return extractPathForTransaction(req, { path: true })[0];
- }
- case 'handler': {
- return (req.route && req.route.stack && req.route.stack[0] && req.route.stack[0].name) || '<anonymous>';
- }
- case 'methodPath':
- default: {
-
- const customRoute = req._reconstructedRoute ? req._reconstructedRoute : undefined;
- return extractPathForTransaction(req, { path: true, method: true, customRoute })[0];
- }
- }
- }
- function extractUserData(
- user
- ,
- keys,
- ) {
- const extractedUser = {};
- const attributes = Array.isArray(keys) ? keys : DEFAULT_USER_INCLUDES;
- attributes.forEach(key => {
- if (user && key in user) {
- extractedUser[key] = user[key];
- }
- });
- return extractedUser;
- }
- function extractRequestData(
- req,
- options
- ,
- ) {
- const { include = DEFAULT_REQUEST_INCLUDES, deps } = options || {};
-
- const requestData = {};
-
-
- const headers = (req.headers || {})
- ;
-
-
- const method = req.method;
-
-
-
-
-
-
-
- const host = headers.host || req.hostname || req.host || '<no host>';
-
-
-
- const protocol = req.protocol === 'https' || (req.socket && req.socket.encrypted) ? 'https' : 'http';
-
-
-
- const originalUrl = req.originalUrl || req.url || '';
-
- const absoluteUrl = originalUrl.startsWith(protocol) ? originalUrl : `${protocol}://${host}${originalUrl}`;
- include.forEach(key => {
- switch (key) {
- case 'headers': {
- requestData.headers = headers;
-
- if (!include.includes('cookies')) {
- delete (requestData.headers ).cookie;
- }
- break;
- }
- case 'method': {
- requestData.method = method;
- break;
- }
- case 'url': {
- requestData.url = absoluteUrl;
- break;
- }
- case 'cookies': {
-
-
-
- requestData.cookies =
-
-
- req.cookies || (headers.cookie && cookie.parseCookie(headers.cookie)) || {};
- break;
- }
- case 'query_string': {
-
-
-
-
- requestData.query_string = extractQueryParams(req, deps);
- break;
- }
- case 'data': {
- if (method === 'GET' || method === 'HEAD') {
- break;
- }
-
-
-
-
-
-
- if (req.body !== undefined) {
- requestData.data = is.isString(req.body) ? req.body : JSON.stringify(normalize.normalize(req.body));
- }
- break;
- }
- default: {
- if ({}.hasOwnProperty.call(req, key)) {
- requestData[key] = (req )[key];
- }
- }
- }
- });
- return requestData;
- }
- function addRequestDataToEvent(
- event,
- req,
- options,
- ) {
- const include = {
- ...DEFAULT_INCLUDES,
- ...(options && options.include),
- };
- if (include.request) {
- const extractedRequestData = Array.isArray(include.request)
- ? extractRequestData(req, { include: include.request, deps: options && options.deps })
- : extractRequestData(req, { deps: options && options.deps });
- event.request = {
- ...event.request,
- ...extractedRequestData,
- };
- }
- if (include.user) {
- const extractedUser = req.user && is.isPlainObject(req.user) ? extractUserData(req.user, include.user) : {};
- if (Object.keys(extractedUser).length) {
- event.user = {
- ...event.user,
- ...extractedUser,
- };
- }
- }
-
-
-
- if (include.ip) {
- const ip = req.ip || (req.socket && req.socket.remoteAddress);
- if (ip) {
- event.user = {
- ...event.user,
- ip_address: ip,
- };
- }
- }
- if (include.transaction && !event.transaction) {
-
-
- event.transaction = extractTransaction(req, include.transaction);
- }
- return event;
- }
- function extractQueryParams(
- req,
- deps,
- ) {
-
-
-
- let originalUrl = req.originalUrl || req.url || '';
- if (!originalUrl) {
- return;
- }
-
-
- if (originalUrl.startsWith('/')) {
- originalUrl = `http://dogs.are.great${originalUrl}`;
- }
- try {
- return (
- req.query ||
- (typeof URL !== 'undefined' && new URL(originalUrl).search.slice(1)) ||
-
- (deps && deps.url && deps.url.parse(originalUrl).query) ||
- undefined
- );
- } catch (e2) {
- return undefined;
- }
- }
- function winterCGHeadersToDict(winterCGHeaders) {
- const headers = {};
- try {
- winterCGHeaders.forEach((value, key) => {
- if (typeof value === 'string') {
-
- headers[key] = value;
- }
- });
- } catch (e) {
- debugBuild.DEBUG_BUILD &&
- logger.logger.warn('Sentry failed extracting headers from a request object. If you see this, please file an issue.');
- }
- return headers;
- }
- function winterCGRequestToRequestData(req) {
- const headers = winterCGHeadersToDict(req.headers);
- return {
- method: req.method,
- url: req.url,
- headers,
- };
- }
- exports.DEFAULT_USER_INCLUDES = DEFAULT_USER_INCLUDES;
- exports.addRequestDataToEvent = addRequestDataToEvent;
- exports.addRequestDataToTransaction = addRequestDataToTransaction;
- exports.extractPathForTransaction = extractPathForTransaction;
- exports.extractRequestData = extractRequestData;
- exports.winterCGHeadersToDict = winterCGHeadersToDict;
- exports.winterCGRequestToRequestData = winterCGRequestToRequestData;
|