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IAAI,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;IAE1D,IAAM,MAAM,GAAG,oBAAa,EAAE,CAAC,SAAS,EAAc,CAAC;IAEvD,IAAI,MAAM,KAAK,SAAS,EAAE;QACxB,cAAM,CAAC,IAAI,CAAC,4DAA4D,CAAC,CAAC;QAC1E,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QACvB,OAAO;KACR;IAED,IAAM,OAAO,GAAG,MAAM,CAAC,UAAU,EAAE,CAAC;IACpC,IAAM,OAAO,GACX,CAAC,OAAO,IAAI,OAAO,CAAC,eAAe,IAAI,OAAO,CAAC,eAAe,GAAG,CAAC,IAAI,OAAO,CAAC,eAAe,CAAC;QAC9F,wBAAwB,CAAC;IAC3B,cAAM,CACJ,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,UAAC,MAAe;QACzC,IAAI,CAAC,MAAM,EAAE;YACX,cAAM,CAAC,IAAI,CAAC,4EAA4E,CAAC,CAAC;SAC3F;QACD,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACzB,CAAC,CAAC,CACH,CAAC;AACJ,CAAC;AAvBD,8CAuBC","sourcesContent":["import { Span } from '@sentry/apm';\nimport { captureException, getCurrentHub, withScope } from '@sentry/core';\nimport { Event } from '@sentry/types';\nimport { forget, isString, logger, normalize } from '@sentry/utils';\nimport * as cookie from 'cookie';\nimport * as domain from 'domain';\nimport * as http from 'http';\nimport * as os from 'os';\nimport * as url from 'url';\n\nimport { NodeClient } from './client';\nimport { flush } from './sdk';\n\nconst DEFAULT_SHUTDOWN_TIMEOUT = 2000;\n\n/**\n * Express compatible tracing handler.\n * @see Exposed as `Handlers.tracingHandler`\n */\nexport function tracingHandler(): (\n req: http.IncomingMessage,\n res: http.ServerResponse,\n next: (error?: any) => void,\n) => void {\n return function sentryTracingMiddleware(\n req: http.IncomingMessage,\n res: http.ServerResponse,\n next: (error?: any) => void,\n ): void {\n // TODO: At this point req.route.path we use in `extractTransaction` is not available\n // but `req.path` or `req.url` should do the job as well. We could unify this here.\n const reqMethod = (req.method || '').toUpperCase();\n const reqUrl = req.url;\n\n const hub = getCurrentHub();\n const transaction = hub.startSpan({\n transaction: `${reqMethod}|${reqUrl}`,\n });\n hub.configureScope(scope => {\n scope.setSpan(transaction);\n });\n res.once('finish', () => {\n transaction.setHttpStatus(res.statusCode);\n transaction.finish();\n });\n\n next();\n };\n}\n\ntype TransactionTypes = 'path' | 'methodPath' | 'handler';\n\n/** JSDoc */\nfunction extractTransaction(req: { [key: string]: any }, type: boolean | TransactionTypes): string | undefined {\n try {\n // Express.js shape\n const request = req as {\n method: string;\n route: {\n path: string;\n stack: [\n {\n name: string;\n }\n ];\n };\n };\n\n switch (type) {\n case 'path': {\n return request.route.path;\n }\n case 'handler': {\n return request.route.stack[0].name;\n }\n case 'methodPath':\n default: {\n const method = request.method.toUpperCase();\n const path = request.route.path;\n return `${method}|${path}`;\n }\n }\n } catch (_oO) {\n return undefined;\n }\n}\n\n/** Default request keys that'll be used to extract data from the request */\nconst DEFAULT_REQUEST_KEYS = ['cookies', 'data', 'headers', 'method', 'query_string', 'url'];\n\n/** JSDoc */\nfunction extractRequestData(req: { [key: string]: any }, keys: boolean | string[]): { [key: string]: string } {\n const request: { [key: string]: any } = {};\n const attributes = Array.isArray(keys) ? keys : DEFAULT_REQUEST_KEYS;\n\n // headers:\n // node, express: req.headers\n // koa: req.header\n const headers = (req.headers || req.header || {}) as {\n host?: string;\n cookie?: string;\n };\n // method:\n // node, express, koa: req.method\n const method = req.method;\n // host:\n // express: req.hostname in > 4 and req.host in < 4\n // koa: req.host\n // node: req.headers.host\n const host = req.hostname || req.host || headers.host || '<no host>';\n // protocol:\n // node: <n/a>\n // express, koa: req.protocol\n const protocol =\n req.protocol === 'https' || req.secure || ((req.socket || {}) as { encrypted?: boolean }).encrypted\n ? 'https'\n : 'http';\n // url (including path and query string):\n // node, express: req.originalUrl\n // koa: req.url\n const originalUrl = (req.originalUrl || req.url) as string;\n // absolute url\n const absoluteUrl = `${protocol}://${host}${originalUrl}`;\n\n attributes.forEach(key => {\n switch (key) {\n case 'headers':\n request.headers = headers;\n break;\n case 'method':\n request.method = method;\n break;\n case 'url':\n request.url = absoluteUrl;\n break;\n case 'cookies':\n // cookies:\n // node, express, koa: req.headers.cookie\n request.cookies = cookie.parse(headers.cookie || '');\n break;\n case 'query_string':\n // query string:\n // node: req.url (raw)\n // express, koa: req.query\n request.query_string = url.parse(originalUrl || '', false).query;\n break;\n case 'data':\n // body data:\n // node, express, koa: req.body\n let data = req.body;\n if (method === 'GET' || method === 'HEAD') {\n if (typeof data === 'undefined') {\n data = '<unavailable>';\n }\n }\n if (data && !isString(data)) {\n // Make sure the request body is a string\n data = JSON.stringify(normalize(data));\n }\n request.data = data;\n break;\n default:\n if ({}.hasOwnProperty.call(req, key)) {\n request[key] = (req as { [key: string]: any })[key];\n }\n }\n });\n\n return request;\n}\n\n/** Default user keys that'll be used to extract data from the request */\nconst DEFAULT_USER_KEYS = ['id', 'username', 'email'];\n\n/** JSDoc */\nfunction extractUserData(\n req: {\n ip?: string;\n connection?: {\n remoteAddress?: string;\n };\n user?: {\n [key: string]: any;\n };\n },\n keys: boolean | string[],\n): { [key: string]: any } {\n const user: { [key: string]: any } = {};\n const attributes = Array.isArray(keys) ? keys : DEFAULT_USER_KEYS;\n\n attributes.forEach(key => {\n if (req.user && key in req.user) {\n user[key] = req.user[key];\n }\n });\n\n // client ip:\n // node: req.connection.remoteAddress\n // express, koa: req.ip\n const ip = req.ip || (req.connection && req.connection.remoteAddress);\n\n if (ip) {\n user.ip_address = ip;\n }\n\n return user;\n}\n\n/**\n * Enriches passed event with request data.\n *\n *\n * @param event Will be mutated and enriched with req data\n * @param req Request object\n * @param options object containing flags to enable functionality\n * @hidden\n */\nexport function parseRequest(\n event: Event,\n req: {\n [key: string]: any;\n },\n options?: {\n request?: boolean | string[];\n serverName?: boolean;\n transaction?: boolean | TransactionTypes;\n user?: boolean | string[];\n version?: boolean;\n },\n): Event {\n // tslint:disable-next-line:no-parameter-reassignment\n options = {\n request: true,\n serverName: true,\n transaction: true,\n user: true,\n version: true,\n ...options,\n };\n\n if (options.version) {\n event.extra = {\n ...event.extra,\n node: global.process.version,\n };\n }\n\n if (options.request) {\n event.request = {\n ...event.request,\n ...extractRequestData(req, options.request),\n };\n }\n\n if (options.serverName) {\n event.server_name = global.process.env.SENTRY_NAME || os.hostname();\n }\n\n if (options.user && req.user) {\n event.user = {\n ...event.user,\n ...extractUserData(req, options.user),\n };\n }\n\n if (options.transaction && !event.transaction) {\n const transaction = extractTransaction(req, options.transaction);\n if (transaction) {\n event.transaction = transaction;\n }\n }\n\n return event;\n}\n\n/**\n * Express compatible request handler.\n * @see Exposed as `Handlers.requestHandler`\n */\nexport function requestHandler(options?: {\n request?: boolean;\n serverName?: boolean;\n transaction?: boolean | TransactionTypes;\n user?: boolean | string[];\n version?: boolean;\n flushTimeout?: number;\n}): (req: http.IncomingMessage, res: http.ServerResponse, next: (error?: any) => void) => void {\n return function sentryRequestMiddleware(\n req: http.IncomingMessage,\n res: http.ServerResponse,\n next: (error?: any) => void,\n ): void {\n if (options && options.flushTimeout && options.flushTimeout > 0) {\n // tslint:disable-next-line: no-unbound-method\n const _end = res.end;\n res.end = function(chunk?: any | (() => void), encoding?: string | (() => void), cb?: () => void): void {\n flush(options.flushTimeout)\n .then(() => {\n _end.call(this, chunk, encoding, cb);\n })\n .then(null, e => {\n logger.error(e);\n });\n };\n }\n const local = domain.create();\n local.add(req);\n local.add(res);\n local.on('error', next);\n local.run(() => {\n getCurrentHub().configureScope(scope =>\n scope.addEventProcessor((event: Event) => parseRequest(event, req, options)),\n );\n next();\n });\n };\n}\n\n/** JSDoc */\ninterface MiddlewareError extends Error {\n status?: number | string;\n statusCode?: number | string;\n status_code?: number | string;\n output?: {\n statusCode?: number | string;\n };\n}\n\n/** JSDoc */\nfunction getStatusCodeFromResponse(error: MiddlewareError): number {\n const statusCode = error.status || error.statusCode || error.status_code || (error.output && error.output.statusCode);\n return statusCode ? parseInt(statusCode as string, 10) : 500;\n}\n\n/** Returns true if response code is internal server error */\nfunction defaultShouldHandleError(error: MiddlewareError): boolean {\n const status = getStatusCodeFromResponse(error);\n return status >= 500;\n}\n\n/**\n * Express compatible error handler.\n * @see Exposed as `Handlers.errorHandler`\n */\nexport function errorHandler(options?: {\n /**\n * Callback method deciding whether error should be captured and sent to Sentry\n * @param error Captured middleware error\n */\n shouldHandleError?(error: MiddlewareError): boolean;\n}): (\n error: MiddlewareError,\n req: http.IncomingMessage,\n res: http.ServerResponse,\n next: (error: MiddlewareError) => void,\n) => void {\n return function sentryErrorMiddleware(\n error: MiddlewareError,\n req: http.IncomingMessage,\n res: http.ServerResponse,\n next: (error: MiddlewareError) => void,\n ): void {\n const shouldHandleError = (options && options.shouldHandleError) || defaultShouldHandleError;\n\n if (shouldHandleError(error)) {\n withScope(scope => {\n if (req.headers && isString(req.headers['sentry-trace'])) {\n const span = Span.fromTraceparent(req.headers['sentry-trace'] as string);\n scope.setSpan(span);\n }\n const eventId = captureException(error);\n (res as any).sentry = eventId;\n next(error);\n });\n\n return;\n }\n\n next(error);\n };\n}\n\n/**\n * @hidden\n */\nexport function logAndExitProcess(error: Error): void {\n console.error(error && error.stack ? error.stack : error);\n\n const client = getCurrentHub().getClient<NodeClient>();\n\n if (client === undefined) {\n logger.warn('No NodeClient was defined, we are exiting the process now.');\n global.process.exit(1);\n return;\n }\n\n const options = client.getOptions();\n const timeout =\n (options && options.shutdownTimeout && options.shutdownTimeout > 0 && options.shutdownTimeout) ||\n DEFAULT_SHUTDOWN_TIMEOUT;\n forget(\n client.close(timeout).then((result: boolean) => {\n if (!result) {\n logger.warn('We reached the timeout for emptying the request buffer, still exiting now!');\n }\n global.process.exit(1);\n }),\n );\n}\n"]}
\No newline at end of file