import type { CustomTransform } from '../apmTypes'; import { parse } from 'meriyah'; import { subscriberExportForModule } from '../config/channel-integration-definitions'; import { MODULE_REGISTRATION_TRANSFORM } from '../config/registration-only'; import { stringifyForCodeInjection } from '@sentry/core'; // Tracks Program nodes we already injected into, so a package with several // instrumented files (or several configs pointing at one file) is injected only // once per file. A `WeakSet` keyed by the node avoids mutating the emitted AST. const injectedPrograms = new WeakSet(); interface ProgramNode { type: string; body: Array<{ type: string; directive?: string }>; } // Where the injected snippet imports from: the `orchestrionModuleInjected` // helper and the module's channel-subscriber factory both live on the main // `@sentry/server-utils` entry, so a single import serves both. The ESM barrel // tree-shakes to just the helper and the factories actually referenced. const DEFAULT_IMPORT_SPECIFIER = '@sentry/server-utils'; /** * Assignment target that keeps the injected call from being tree-shaken. See * {@link moduleInjectedSnippet}. The value written is always `undefined`; only * the assignment matters. */ const MODULE_INJECTED_SINK = 'globalThis.__SENTRY_ORCHESTRION_INJECT__'; /** * Entry-chunk banner that marks "the bundler plugin ran" for * `detectOrchestrionSetup()`. Merge-only (`g.bundler = g.bundler || new Set()`) * so it can never clobber module names already recorded by an injected snippet * that happened to run first; the names themselves arrive per module, when each * transformed module is evaluated and its snippet calls * `orchestrionModuleInjected`. */ export const ORCHESTRION_BUNDLER_MARKER_BANNER = ';(function(){var g=globalThis.__SENTRY_ORCHESTRION__=globalThis.__SENTRY_ORCHESTRION__||{};g.bundler=g.bundler||new Set();})();'; /** * Snippet injected into each instrumented module. It imports the * `orchestrionModuleInjected` helper — plus the module's channel-subscriber * factory, when it has one — from `@sentry/server-utils` and calls the helper * with the module's real name. The helper records the module on the global * marker, stores the factory, and emits the `orchestrion.module-injected` client * event, so it runs exactly when the module is evaluated — the moment its * channels can start publishing. That per-module timing is what keeps * subscriptions lazy (Node caps diagnostics channels in use at 1024). * * Importing the single named factory (rather than a central dispatch that pulls * in every subscriber) is what makes this tree-shake: a bundle carries only the * subscriber code for packages actually transformed into it. The helper is * generic (references no factory), so importing it alongside doesn't pull * siblings. * * The specifier is parameterized for bundlers where the bare import emitted * inside a transformed `node_modules` file can't resolve (Turbopack under * isolated installs); it's embedded via `stringifyForCodeInjection` so * absolute Windows paths survive. * * The call result is assigned to a global rather than discarded. The helper * returns `void` and `@sentry/server-utils` is `sideEffects: false`, so a bare * call statement is something a bundler can prove droppable: rollup >= 4.63.0 * does exactly that and removes the whole registration, leaving the module * instrumented but unsubscribed. Writing to a property of `globalThis` is a * side effect no bundler can shake out, so the call survives. */ function moduleInjectedSnippet( moduleName: string, exportName: string | undefined, esm: boolean, importSpecifier: string, ): string { const bindings = exportName ? `orchestrionModuleInjected, ${exportName}` : 'orchestrionModuleInjected'; const importStmt = esm ? `import { ${bindings} } from ${stringifyForCodeInjection(importSpecifier)};` : `const { ${bindings} } = require(${stringifyForCodeInjection(importSpecifier)});`; // The arrow defers reading `exportName` to `init()`. `@sentry/*/vite` imports some packages into // their own integration module (e.g. Mastra's), and in that cycle a direct read would hit the TDZ. const args = exportName ? `${stringifyForCodeInjection(moduleName)}, () => ${exportName}()` : stringifyForCodeInjection(moduleName); return `${importStmt}\n${MODULE_INJECTED_SINK} = orchestrionModuleInjected(${args});`; } /** * Publishes the module's own namespace on `providedModules`. For `@flue/runtime`'s `dist/index.mjs`, * this prepends roughly: * * ```js * import * as __SENTRY_SELF_PROVIDED__ from "./index.mjs"; * globalThis.__SENTRY_ORCHESTRION__.providedModules["@flue/runtime"] = __SENTRY_SELF_PROVIDED__; // as a getter * ``` * * A self-import adds no edge to the module graph; importing the package into a Sentry module would * close a cycle through every instrumented dependency, since those all import `@sentry/server-utils`. */ function selfProvidedSnippet(moduleName: string, filePath: string): string { const marker = 'globalThis.__SENTRY_ORCHESTRION__'; const self = `./${filePath.split('/').pop()}`; return ( `import * as __SENTRY_SELF_PROVIDED__ from ${stringifyForCodeInjection(self)};\n` + `(${marker} = ${marker} || {});\n` + `(${marker}.providedModules = ${marker}.providedModules || {});\n` + `Object.defineProperty(${marker}.providedModules, ${stringifyForCodeInjection(moduleName)}, ` + '{ configurable: true, enumerable: true, get() { return __SENTRY_SELF_PROVIDED__; } });' ); } export interface ModuleInjectedTransformsOptions { /** Packages whose ESM entry publishes its own namespace on `providedModules`. */ selfProvidedModules?: readonly string[]; } /** * The unified `customTransforms` every orchestrion bundler plugin (and the * webpack/Turbopack loader) applies: an override for orchestrion's built-in * `tracingChannelImport` transform, which runs (via `tracingChannelDeclaration`) * for every file that gets a channel wrapped — the one hook that reaches every * instrumented module without any extra instrumentation configs. It chains the * default (which splices the `diagnostics_channel` import and bails when it is * already present), then splices the module-injected snippet in after any * `'use strict'` directive. * * Invoked once per wrapped channel, so the `WeakSet` keeps the snippet to one * per file. Requires `@apm-js-collab/code-transformer` >= 0.18.1, where * built-ins dispatch through the override map and expose the originals on * `state.transforms.defaults`. * * Also carries the registration-only operator, which splices the same snippet * without any channel injection — for library versions whose tracing channels * are native. */ export function moduleInjectedTransforms( // A function is read per injected file — the webpack/Turbopack loader uses it // to supply a per-file relative specifier (Turbopack supports neither // absolute-path imports nor bare specifiers that don't resolve from the // importing file's location). importSpecifier?: string | (() => string | undefined), { selfProvidedModules = [] }: ModuleInjectedTransformsOptions = {}, ): Record { const spliceModuleInjected = (state: unknown, program: unknown): void => { const { moduleType, module } = state as { moduleType?: string; module?: { name?: string; filePath?: string | RegExp }; }; const node = program as ProgramNode; if (injectedPrograms.has(node)) { return; } const moduleName = module?.name; if (!moduleName) { return; } injectedPrograms.add(node); const specifier = (typeof importSpecifier === 'function' ? importSpecifier() : importSpecifier) ?? DEFAULT_IMPORT_SPECIFIER; const exportName = subscriberExportForModule(moduleName); const esm = moduleType === 'esm'; const selfProvided = esm && typeof module?.filePath === 'string' && selfProvidedModules.includes(moduleName) ? `${selfProvidedSnippet(moduleName, module.filePath)}\n` : ''; const statements = parse(selfProvided + moduleInjectedSnippet(moduleName, exportName, esm, specifier), { module: esm, next: true, }).body as ProgramNode['body']; const directiveIndex = node.body.findIndex(n => n.type === 'ExpressionStatement' && n.directive === 'use strict'); node.body.splice(directiveIndex + 1, 0, ...statements); }; const injectModuleInjected: CustomTransform = (state, program, parent, ancestry) => { const { transforms } = state as { transforms: { defaults: { tracingChannelImport: CustomTransform } }; }; transforms.defaults.tracingChannelImport(state, program, parent, ancestry); spliceModuleInjected(state, program); }; // Operator for registration-only configs (`transform` field, see // `config/registration-only.ts`): dispatched INSTEAD of `traceSync`, so no // channel is declared and no function is wrapped. Their `astQuery: 'Program'` // matches only the file root, so `node` is the Program itself. const injectRegistrationOnly: CustomTransform = (state, node) => { spliceModuleInjected(state, node); }; return { tracingChannelImport: injectModuleInjected, [MODULE_REGISTRATION_TRANSFORM]: injectRegistrationOnly, }; }