{"version":3,"file":"node-schedule.js","sources":["../../../src/cron/node-schedule.ts"],"sourcesContent":["import { withMonitor } from '@sentry/core';\nimport { replaceCronNames } from './common';\n\nexport interface NodeSchedule {\n  scheduleJob(\n    nameOrExpression: string | Date | object,\n    expressionOrCallback: string | Date | object | (() => void),\n    callback?: () => void,\n  ): unknown;\n}\n\n/**\n * Instruments the `node-schedule` library to send a check-in event to Sentry for each job execution.\n *\n * ```ts\n * import * as Sentry from '@sentry/node';\n * import * as schedule from 'node-schedule';\n *\n * const scheduleWithCheckIn = Sentry.cron.instrumentNodeSchedule(schedule);\n *\n * const job = scheduleWithCheckIn.scheduleJob('my-cron-job', '* * * * *', () => {\n *  console.log('You will see this message every minute');\n * });\n * ```\n */\nexport function instrumentNodeSchedule<T>(lib: T & NodeSchedule): T {\n  return new Proxy(lib, {\n    get(target, prop: keyof NodeSchedule) {\n      if (prop === 'scheduleJob') {\n        // eslint-disable-next-line @typescript-eslint/unbound-method\n        return new Proxy(target.scheduleJob, {\n          apply(target, thisArg, argArray: Parameters<NodeSchedule['scheduleJob']>) {\n            const [nameOrExpression, expressionOrCallback, callback] = argArray;\n\n            if (\n              typeof nameOrExpression !== 'string' ||\n              typeof expressionOrCallback !== 'string' ||\n              typeof callback !== 'function'\n            ) {\n              throw new Error(\n                \"Automatic instrumentation of 'node-schedule' requires the first parameter of 'scheduleJob' to be a job name string and the second parameter to be a crontab string\",\n              );\n            }\n\n            const monitorSlug = nameOrExpression;\n            const expression = expressionOrCallback;\n\n            async function monitoredCallback(): Promise<void> {\n              return withMonitor(\n                monitorSlug,\n                async () => {\n                  // oxlint-disable-next-line typescript/await-thenable -- callback may be async at runtime\n                  await callback?.();\n                },\n                {\n                  schedule: { type: 'crontab', value: replaceCronNames(expression) },\n                },\n              );\n            }\n\n            return target.apply(thisArg, [monitorSlug, expression, monitoredCallback]);\n          },\n        });\n      }\n\n      return target[prop];\n    },\n  });\n}\n"],"names":[],"mappings":";;;AAWA;AACA;AACA;AACA;AACA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