Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const node_path = require('node:path'); const agentClass = require('./agentClass.js'); const instrumentFile = require('./instrumentFile.js'); const transform = require('./transform.js'); const wranglerConfig = require('./wranglerConfig.js'); function normalizePath(path) { return path.replace(/\\/g, "/"); } const JS_EXTENSION_REGEX = /\.[cm]?[jt]sx?$/; function sentryCloudflareAutoInstrumentPlugin(options = {}) { let wranglerConfig$1; let entryFilePath; let optionsFn = instrumentFile.ENV_FALLBACK_OPTIONS_FN; let optionsImport; return { name: "sentry-cloudflare-auto-instrument", configResolved(config) { const result = wranglerConfig.resolveWranglerConfig(config.root, options.wranglerConfigPath); if (!result) { config.logger?.warn( options.wranglerConfigPath ? `[sentry] Could not find or parse the wrangler config "${node_path.basename(options.wranglerConfigPath)}" (resolved against the Vite root) \u2014 auto-instrumentation disabled.` : "[sentry] No parseable wrangler config found \u2014 auto-instrumentation disabled. Set `wranglerConfigPath` if your config uses a custom name." ); return; } wranglerConfig$1 = result.config; if (wranglerConfig$1.main) { entryFilePath = normalizePath(wranglerConfig$1.main); } if (entryFilePath) { const instrumentFilePath = instrumentFile.resolveInstrumentFile(entryFilePath); if (instrumentFilePath) { const built = instrumentFile.buildOptionsImport(entryFilePath, instrumentFilePath); optionsFn = built.optionsFn; optionsImport = built.importStmt; } } }, async transform(code, id) { if (!wranglerConfig$1 || !entryFilePath) return void 0; if (this.environment?.name === "client") return void 0; const normalizedId = normalizePath(id.replace(/[?#].*$/, "")); if (normalizedId !== entryFilePath) { if (!JS_EXTENSION_REGEX.test(normalizedId) || !JS_EXTENSION_REGEX.test(entryFilePath)) return void 0; if (normalizedId.replace(JS_EXTENSION_REGEX, "") !== entryFilePath.replace(JS_EXTENSION_REGEX, "")) { return void 0; } } let ast; try { ast = this.parse(code); } catch { return void 0; } const classWrappers = /* @__PURE__ */ new Map(); for (const { className } of wranglerConfig$1.durableObjects) { classWrappers.set(className, "durableObject"); } for (const { className } of wranglerConfig$1.workflows) { classWrappers.set(className, "workflow"); } for (const className of wranglerConfig$1.workerEntrypoints) { classWrappers.set(className, "workerEntrypoint"); } const agentCandidates = agentClass.collectAgentCandidates( ast, [...classWrappers].filter(([, kind]) => kind === "durableObject").map(([name]) => name) ); const agentClasses = agentCandidates.size > 0 ? await agentClass.detectAgentClasses(ast, normalizedId, agentCandidates, { parse: (code2) => this.parse(code2), resolve: this.resolve ? (source, importer) => this.resolve(source, importer) : void 0 }) : void 0; const result = transform.applyAutoInstrumentTransforms(code, ast, { classWrappers, agentClasses, optionsFn, optionsImport, sameWorkerBindings: wranglerConfig$1.sameWorkerBindings }); const wrappedClasses = result?.wrappedClasses ?? /* @__PURE__ */ new Set(); const missing = [...classWrappers.keys()].filter((name) => !wrappedClasses.has(name)); if (missing.length > 0) { this.warn?.( `[sentry] Could not auto-instrument ${missing.join(", ")}.The worker entry has no export matching them. Star re-exports (\`export * from "./do"\`) are not matched. Export them by name or wrap them manually with corresponding \`instrument*WithSentry\` helpers.` ); } return result ?? void 0; } }; } exports.sentryCloudflareAutoInstrumentPlugin = sentryCloudflareAutoInstrumentPlugin; //# sourceMappingURL=autoInstrument.js.map