rittenhop-dev/versions/5.94.2/node_modules/@sentry/node/esm/integrations/context.js.map

1 line
67 KiB
Plaintext
Raw Normal View History

2024-09-23 19:40:12 -04:00
{"version":3,"file":"context.js","sources":["../../../src/integrations/context.ts"],"sourcesContent":["/* eslint-disable max-lines */\nimport { execFile } from 'child_process';\nimport { readFile, readdir } from 'fs';\nimport * as os from 'os';\nimport { join } from 'path';\nimport { promisify } from 'util';\nimport { convertIntegrationFnToClass, defineIntegration } from '@sentry/core';\nimport type {\n AppContext,\n CloudResourceContext,\n Contexts,\n CultureContext,\n DeviceContext,\n Event,\n Integration,\n IntegrationClass,\n IntegrationFn,\n OsContext,\n} from '@sentry/types';\n\n// TODO: Required until we drop support for Node v8\nexport const readFileAsync = promisify(readFile);\nexport const readDirAsync = promisify(readdir);\n\nconst INTEGRATION_NAME = 'Context';\n\ninterface DeviceContextOptions {\n cpu?: boolean;\n memory?: boolean;\n}\n\ninterface ContextOptions {\n app?: boolean;\n os?: boolean;\n device?: DeviceContextOptions | boolean;\n culture?: boolean;\n cloudResource?: boolean;\n}\n\nconst _nodeContextIntegration = ((options: ContextOptions = {}) => {\n let cachedContext: Promise<Contexts> | undefined;\n\n const _options = {\n app: true,\n os: true,\n device: true,\n culture: true,\n cloudResource: true,\n ...options,\n };\n\n /** Add contexts to the event. Caches the context so we only look it up once. */\n async function addContext(event: Event): Promise<Event> {\n if (cachedContext === undefined) {\n cachedContext = _getContexts();\n }\n\n const updatedContext = _updateContext(await cachedContext);\n\n event.contexts = {\n ...event.contexts,\n app: { ...updatedContext.app, ...event.contexts?.app },\n os: { ...updatedContext.os, ...event.contexts?.os },\n device: { ...updatedContext.device, ...event.contexts?.device },\n culture: { ...updatedContext.culture, ...event.contexts?.culture },\n cloud_resource: { ...updatedContext.cloud_resource, ...event.contexts?.cloud_resource },\n };\n\n return event;\n }\n\n /** Get the contexts from node. */\n async function _getContexts(): Promise<Contexts> {\n const contexts: Contexts = {};\n\n if (_options.os) {\n contexts.os = await getOsContext();\n }\n\n if (_options.app) {\n contexts.app = getAppContext();\n }\n\n if (_options.device) {\n contexts.device = getDeviceContext(_options.device);\n }\n\n if (_options.culture) {\n const culture = getCultureContext();\n\n if (culture) {\n contexts.culture = culture;\n }\n }\n\n if (_options.cloudResource) {\n contexts.cloud_resource = getCloudResourceContext();\n }\n\n return contexts;\n }\n\n return {\n name: INTEGRATION_NAME,\n // TODO v8: Remove this\n setupOnce() {}, // eslint-disable-line @typescript-eslint/no-empty-function\n processEvent(event) {\n return addContext(event);\n },\n };\n}) satisfies IntegrationFn;\n\nexport const nodeContextIntegration = defineIntegration(_nodeContextIntegration);\n\n/**\n * Add node modules / packages to the event.\n * @deprecated Use `nodeContextIntegration()` instead.\n */\n// eslint-disable-next-line deprecation/deprecation\nexport const Context = convertIntegrationFnToClass(INTEGRATION_NAME, nodeContextIntegration) as IntegrationClass<\n Integration & { processEvent: (event: Event) => Promise<Event> }\n> & {\n new (options?: {\n app?: boolean;\n os?: boolean;\n device?: { cpu?: boolean; memory?: boolean } | boolean;\n culture?: boolean;\n cloudResource?: boolean;\n }): Integration;\n};\n\n// eslint-disable-next-line deprecation/deprecation\nexport type Context = typeof Context;\n\n/**\n * Updates the context with dynamic values that can change\n */\nfunction _updateContext(contexts: Contexts): Contexts {\n // Only update properties if they exist\n if (contexts?.app?.app_memory) {\n contexts.app.app_memory = process.memoryUsage().rss;\n }\n\n if (contexts?.device?.free_memory) {\n contexts.device.free_memory = os.freemem();\n }\n\n return contexts;\n}