{"version":3,"sources":["../src/instrumentation.ts","../src/instrumentation-runtime.ts"],"sourcesContent":["import { build } from \"esbuild\";\nimport fs from \"node:fs\";\nimport path from \"node:path\";\nimport { pathToFileURL } from \"node:url\";\nimport type { FarmInstrumentationModule } from \"./instrumentation-runtime\";\n\nexport * from \"./instrumentation-runtime\";\n\nconst INSTRUMENTATION_EXTENSIONS = [\"ts\", \"mts\", \"js\", \"mjs\"] as const;\n\nexport function resolveFarmInstrumentationFile(root: string, srcDir = \"src\"): string | null {\n  const candidates = [\n    ...INSTRUMENTATION_EXTENSIONS.map((extension) =>\n      path.join(root, srcDir, `instrumentation.${extension}`),\n    ),\n    ...INSTRUMENTATION_EXTENSIONS.map((extension) =>\n      path.join(root, `instrumentation.${extension}`),\n    ),\n  ].filter((candidate) => fs.existsSync(candidate));\n\n  if (candidates.length > 1) {\n    throw new Error(\n      `Farm.js found multiple instrumentation files:\\n${candidates\n        .map((candidate) => `- ${path.relative(root, candidate)}`)\n        .join(\"\\n\")}\\nKeep only one instrumentation file in the project root or ${srcDir}/.`,\n    );\n  }\n\n  return candidates[0] ?? null;\n}\n\nexport async function loadFarmInstrumentation(\n  filePath: string | null,\n  root: string,\n): Promise<FarmInstrumentationModule | null> {\n  if (!filePath) return null;\n\n  const outputDirectory = path.join(root, \".farm\", \".instrumentation-loader\");\n  const outputFile = path.join(outputDirectory, \"instrumentation.mjs\");\n  await fs.promises.mkdir(outputDirectory, { recursive: true });\n  await build({\n    entryPoints: [filePath],\n    outfile: outputFile,\n    bundle: true,\n    format: \"esm\",\n    platform: \"node\",\n    target: \"node20\",\n    packages: \"external\",\n    sourcemap: \"inline\",\n  });\n\n  const url = pathToFileURL(outputFile);\n  url.searchParams.set(\"t\", String(Date.now()));\n  return (await import(url.href)) as FarmInstrumentationModule;\n}\n","export type FarmInstrumentationRuntime = \"nodejs\" | \"edge\" | \"bun\";\nexport type FarmInstrumentationMode = \"development\" | \"production\" | \"test\";\n\nexport interface FarmInstrumentationContext {\n  root: string;\n  mode: FarmInstrumentationMode;\n  runtime: FarmInstrumentationRuntime;\n}\n\nexport type FarmInstrumentationCleanup =\n  | void\n  | (() => void | Promise<void>)\n  | { shutdown: () => void | Promise<void> };\n\nexport interface FarmInstrumentationModule {\n  register?: (\n    context: FarmInstrumentationContext,\n  ) => FarmInstrumentationCleanup | Promise<FarmInstrumentationCleanup>;\n  shutdown?: () => void | Promise<void>;\n}\n\nexport interface FarmInstrumentationLifecycle {\n  start(): Promise<void>;\n  shutdown(): Promise<void>;\n}\n\nexport function createFarmInstrumentationLifecycle(\n  module: FarmInstrumentationModule | null | undefined,\n  context: FarmInstrumentationContext,\n): FarmInstrumentationLifecycle {\n  let startPromise: Promise<void> | undefined;\n  let shutdownPromise: Promise<void> | undefined;\n  let cleanup: Exclude<FarmInstrumentationCleanup, void> | undefined;\n\n  return {\n    start() {\n      if (!startPromise) {\n        startPromise = Promise.resolve(module?.register?.(context)).then((result) => {\n          cleanup = result || undefined;\n        });\n      }\n      return startPromise;\n    },\n    shutdown() {\n      if (!shutdownPromise) {\n        shutdownPromise = (async () => {\n          try {\n            await startPromise;\n          } catch {\n            // A partially initialized module may still need its exported cleanup.\n          }\n          try {\n            if (typeof cleanup === \"function\") {\n              await cleanup();\n            } else if (cleanup?.shutdown) {\n              await cleanup.shutdown();\n            }\n          } finally {\n            if (module?.shutdown) {\n              await module.shutdown();\n            }\n          }\n        })();\n      }\n      return shutdownPromise;\n    },\n  };\n}\n\nexport function resolveFarmInstrumentationRuntime(preset?: string): FarmInstrumentationRuntime {\n  const normalized = preset?.toLowerCase() ?? \"\";\n  if (\n    normalized.includes(\"cloudflare\") ||\n    normalized.includes(\"worker\") ||\n    normalized.includes(\"edge\")\n  ) {\n    return \"edge\";\n  }\n  if (normalized.includes(\"bun\")) return \"bun\";\n  return \"nodejs\";\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,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