1 |
- {"version":3,"file":"index.js","sources":["../../../../src/integrations/hapi/index.ts"],"sourcesContent":["import {\n SDK_VERSION,\n captureException,\n continueTrace,\n convertIntegrationFnToClass,\n defineIntegration,\n getActiveTransaction,\n getCurrentScope,\n getDynamicSamplingContextFromSpan,\n setHttpStatus,\n spanToTraceHeader,\n startTransaction,\n} from '@sentry/core';\nimport type { IntegrationFn } from '@sentry/types';\nimport { dynamicSamplingContextToSentryBaggageHeader, fill } from '@sentry/utils';\n\nimport type { Boom, RequestEvent, ResponseObject, Server } from './types';\n\nfunction isResponseObject(response: ResponseObject | Boom): response is ResponseObject {\n return response && (response as ResponseObject).statusCode !== undefined;\n}\n\nfunction isBoomObject(response: ResponseObject | Boom): response is Boom {\n return response && (response as Boom).isBoom !== undefined;\n}\n\nfunction isErrorEvent(event: RequestEvent): event is RequestEvent {\n return event && (event as RequestEvent).error !== undefined;\n}\n\nfunction sendErrorToSentry(errorData: object): void {\n captureException(errorData, {\n mechanism: {\n type: 'hapi',\n handled: false,\n data: {\n function: 'hapiErrorPlugin',\n },\n },\n });\n}\n\nexport const hapiErrorPlugin = {\n name: 'SentryHapiErrorPlugin',\n version: SDK_VERSION,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n register: async function (serverArg: Record<any, any>) {\n const server = serverArg as unknown as Server;\n\n server.events.on('request', (request, event) => {\n // eslint-disable-next-line deprecation/deprecation\n const transaction = getActiveTransaction();\n\n if (request.response && isBoomObject(request.response)) {\n sendErrorToSentry(request.response);\n } else if (isErrorEvent(event)) {\n sendErrorToSentry(event.error);\n }\n\n if (transaction) {\n transaction.setStatus('internal_error');\n transaction.end();\n }\n });\n },\n};\n\nexport const hapiTracingPlugin = {\n name: 'SentryHapiTracingPlugin',\n version: SDK_VERSION,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n register: async function (serverArg: Record<any, any>) {\n const server = serverArg as unknown as Server;\n\n server.ext('onPreHandler', (request, h) => {\n const transaction = continueTrace(\n {\n sentryTrace: request.headers['sentry-trace'] || undefined,\n baggage: request.headers['baggage'] || undefined,\n },\n transactionContext => {\n // eslint-disable-next-line deprecation/deprecation\n return startTransaction({\n ...transactionContext,\n op: 'hapi.request',\n name: request.route.path,\n description: `${request.route.method} ${request.path}`,\n });\n },\n );\n\n // eslint-disable-next-line deprecation/deprecation\n getCurrentScope().setSpan(transaction);\n\n return h.continue;\n });\n\n server.ext('onPreResponse', (request, h) => {\n // eslint-disable-next-line deprecation/deprecation\n const transaction = getActiveTransaction();\n\n if (request.response && isResponseObject(request.response) && transaction) {\n const response = request.response as ResponseObject;\n response.header('sentry-trace', spanToTraceHeader(transaction));\n\n const dynamicSamplingContext = dynamicSamplingContextToSentryBaggageHeader(\n getDynamicSamplingContextFromSpan(transaction),\n );\n\n if (dynamicSamplingContext) {\n response.header('baggage', dynamicSamplingContext);\n }\n }\n\n return h.continue;\n });\n\n server.ext('onPostHandler', (request, h) => {\n // eslint-disable-next-line deprecation/deprecation\n const transaction = getActiveTransaction();\n\n if (transaction) {\n if (request.response && isResponseObject(request.response)) {\n setHttpStatus(transaction, request.response.statusCode);\n }\n\n transaction.end();\n }\n\n return h.continue;\n });\n },\n};\n\nexport type HapiOptions = {\n /** Hapi server instance */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n server?: Record<any, any>;\n};\n\nconst INTEGRATION_NAME = 'Hapi';\n\nconst _hapiIntegration = ((options: HapiOptions = {}) => {\n const server = options.server as undefined | Server;\n\n return {\n name: INTEGRATION_NAME,\n setupOnce() {\n if (!server) {\n return;\n }\n\n fill(server, 'start', (originalStart: () => void) => {\n return async function (this: Server) {\n await this.register(hapiTracingPlugin);\n await this.register(hapiErrorPlugin);\n const result = originalStart.apply(this);\n return result;\n };\n });\n },\n };\n}) satisfies IntegrationFn;\n\nexport const hapiIntegration = defineIntegration(_hapiIntegration);\n\n/**\n * Hapi Framework Integration.\n * @deprecated Use `hapiIntegration()` instead.\n */\n// eslint-disable-next-line deprecation/deprecation\nexport const Hapi = convertIntegrationFnToClass(INTEGRATION_NAME, hapiIntegration);\n\n// eslint-disable-next-line deprecation/deprecation\nexport type Hapi = typeof Hapi;\n"],"names":[],"mappings":";;;AAkBA,SAAS,gBAAgB,CAAC,QAAQ,EAAqD;AACvF,EAAE,OAAO,YAAY,CAAC,WAA4B,UAAW,KAAI,SAAS,CAAA;AAC1E,CAAA;AACA;AACA,SAAS,YAAY,CAAC,QAAQ,EAA2C;AACzE,EAAE,OAAO,YAAY,CAAC,WAAkB,MAAO,KAAI,SAAS,CAAA;AAC5D,CAAA;AACA;AACA,SAAS,YAAY,CAAC,KAAK,EAAuC;AAClE,EAAE,OAAO,SAAS,CAAC,QAAuB,KAAM,KAAI,SAAS,CAAA;AAC7D,CAAA;AACA;AACA,SAAS,iBAAiB,CAAC,SAAS,EAAgB;AACpD,EAAE,gBAAgB,CAAC,SAAS,EAAE;AAC9B,IAAI,SAAS,EAAE;AACf,MAAM,IAAI,EAAE,MAAM;AAClB,MAAM,OAAO,EAAE,KAAK;AACpB,MAAM,IAAI,EAAE;AACZ,QAAQ,QAAQ,EAAE,iBAAiB;AACnC,OAAO;AACP,KAAK;AACL,GAAG,CAAC,CAAA;AACJ,CAAA;AACA;AACO,MAAM,kBAAkB;AAC/B,EAAE,IAAI,EAAE,uBAAuB;AAC/B,EAAE,OAAO,EAAE,WAAW;AACtB;AACA,EAAE,QAAQ,EAAE,gBAAgB,SAAS,EAAoB;AACzD,IAAI,MAAM,MAAO,GAAE,SAAU,EAAA;AAC7B;AACA,IAAI,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,SAAS,EAAE,CAAC,OAAO,EAAE,KAAK,KAAK;AACpD;AACA,MAAM,MAAM,WAAA,GAAc,oBAAoB,EAAE,CAAA;AAChD;AACA,MAAM,IAAI,OAAO,CAAC,QAAS,IAAG,YAAY,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE;AAC9D,QAAQ,iBAAiB,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAA;AAC3C,O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