import { getCurrentHub , initAndBind , Integrations as CoreIntegrations } from '@sentry/core' ; import { getMainCarrier , setHubOnCarrier } from '@sentry/hub' ; import { SessionStatus } from '@sentry/types' ; import { getGlobalObject , logger } from '@sentry/utils' ; import * as domain from 'domain' ; import { NodeClient } from './client' ; import { Console , Http , LinkedErrors , OnUncaughtException , OnUnhandledRejection } from './integrations' ; import { NodeOptions } from './types' ; export const defaultIntegrations = [ new CoreIntegrations . InboundFilters ( ) , new CoreIntegrations . FunctionToString ( ) , new Console ( ) , new Http ( ) , new OnUncaughtException ( ) , new OnUnhandledRejection ( ) , new LinkedErrors ( ) , ] ; /**
 * The Sentry Node SDK Client.
 *
 * To use this SDK, call the  init function as early as possible in the
 * main entry module. To set context information or send manual events, use the
 * provided methods.
 *
 *  example example example example see NodeOptions for documentation on configuration options.
  export function init ( options = { } ) { const carrier = getMainCarrier ( ) ; const autoloadedIntegrations = carrier . __SENTRY__ ?. integrations || [ ] ; options . defaultIntegrations = options . defaultIntegrations === false ? [ ] : [ ... ( Array . isArray ( options . defaultIntegrations ) ? options . defaultIntegrations : defaultIntegrations ) , ... autoloadedIntegrations , ] ; if ( options . dsn === undefined && process . env . SENTRY_DSN ) { options . dsn = process . env . SENTRY_DSN ; } if ( options . tracesSampleRate === undefined && process . env . SENTRY_TRACES_SAMPLE_RATE ) { const tracesSampleRate = parseFloat ( process . env . SENTRY_TRACES_SAMPLE_RATE ) ; if ( isFinite ( tracesSampleRate ) ) { options . tracesSampleRate = tracesSampleRate ; } } if ( options . release === undefined ) { const detectedRelease = getSentryRelease ( ) ; if ( detectedRelease !== undefined ) { options . release = detectedRelease ; } else { options . autoSessionTracking = false ; } } if ( options . environment === undefined && process . env . SENTRY_ENVIRONMENT ) { options . environment = process . env . SENTRY_ENVIRONMENT ; } if ( options . autoSessionTracking === undefined && options . dsn !== undefined ) { options . autoSessionTracking = true ; } if ( ( domain as any ) . active ) { setHubOnCarrier ( carrier , getCurrentHub ( ) ) ; } initAndBind ( NodeClient , options ) ; if ( options . autoSessionTracking ) { startSessionTracking ( ) ; } } returns export function lastEventId ( ) { return getCurrentHub ( ) . lastEventId ( ) ; } /**
 * Call `flush()` on the current client, if there is one. See  Client . flush .
 *
 *  param timeout returns export async function flush ( timeout ? ) { const client = getCurrentHub ( ) . getClient < NodeClient > ( ) ; if ( client ) { return client . flush ( timeout ) ; } logger . warn ( 'Cannot flush events. No client defined.' ) ; return Promise . resolve ( false ) ; } /**
 * Call `close()` on the current client, if there is one. See  Client . close .
 *
 *  param timeout returns export async function close ( timeout ? ) { const client = getCurrentHub ( ) . getClient < NodeClient > ( ) ; if ( client ) { return client . close ( timeout ) ; } logger . warn ( 'Cannot flush events and disable SDK. No client defined.' ) ; return Promise . resolve ( false ) ; } /**
 * Function that takes an instance of NodeClient and checks if autoSessionTracking option is enabled for that client
 */ export function isAutoSessionTrackingEnabled ( client ? ) { if ( client === undefined ) { return false ; } const clientOptions = client && client . getOptions ( ) ; if ( clientOptions && clientOptions . autoSessionTracking !== undefined ) { return clientOptions . autoSessionTracking ; } return false ; } /**
 * Returns a release dynamically from environment variables.
 */ export function getSentryRelease ( fallback ? ) { if ( process . env . SENTRY_RELEASE ) { return process . env . SENTRY_RELEASE ; } const global = getGlobalObject ( ) ; if ( global . SENTRY_RELEASE && global . SENTRY_RELEASE . id ) { return global . SENTRY_RELEASE . id ; } return ( process . env . GITHUB_SHA || process . env . COMMIT_REF || process . env . VERCEL_GIT_COMMIT_SHA || process . env . VERCEL_GITHUB_COMMIT_SHA || process . env . VERCEL_GITLAB_COMMIT_SHA || process . env . VERCEL_BITBUCKET_COMMIT_SHA || process . env . ZEIT_GITHUB_COMMIT_SHA || process . env . ZEIT_GITLAB_COMMIT_SHA || process . env . ZEIT_BITBUCKET_COMMIT_SHA || fallback ) ; } /**
 * Enable automatic Session Tracking for the node process.
 */ function startSessionTracking ( ) { const hub = getCurrentHub ( ) ; hub . startSession ( ) ; process . on ( 'beforeExit' , ( ) => { const session = hub . getScope ( ) ?. getSession ( ) ; const terminalStates = [ 'exited' , 'crashed' ] ; if ( session && ! terminalStates . includes ( session . status ) ) hub . endSession ( ) ; } ) ; }