{"version":3,"sources":["../src/instrumentation-runtime.ts"],"sourcesContent":["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":";;;;;AA0BO,SAAS,mCACd,QACA,SAC8B;AAC9B,MAAI;AACJ,MAAI;AACJ,MAAI;AAEJ,SAAO;AAAA,IACL,QAAQ;AACN,UAAI,CAAC,cAAc;AACjB,uBAAe,QAAQ,QAAQ,QAAQ,WAAW,OAAO,CAAC,EAAE,KAAK,CAAC,WAAW;AAC3E,oBAAU,UAAU;AAAA,QACtB,CAAC;AAAA,MACH;AACA,aAAO;AAAA,IACT;AAAA,IACA,WAAW;AACT,UAAI,CAAC,iBAAiB;AACpB,2BAAmB,YAAY;AAC7B,cAAI;AACF,kBAAM;AAAA,UACR,QAAQ;AAAA,UAER;AACA,cAAI;AACF,gBAAI,OAAO,YAAY,YAAY;AACjC,oBAAM,QAAQ;AAAA,YAChB,WAAW,SAAS,UAAU;AAC5B,oBAAM,QAAQ,SAAS;AAAA,YACzB;AAAA,UACF,UAAE;AACA,gBAAI,QAAQ,UAAU;AACpB,oBAAM,OAAO,SAAS;AAAA,YACxB;AAAA,UACF;AAAA,QACF,GAAG;AAAA,MACL;AACA,aAAO;AAAA,IACT;AAAA,EACF;AACF;AAzCgB;AA2CT,SAAS,kCAAkC,QAA6C;AAC7F,QAAM,aAAa,QAAQ,YAAY,KAAK;AAC5C,MACE,WAAW,SAAS,YAAY,KAChC,WAAW,SAAS,QAAQ,KAC5B,WAAW,SAAS,MAAM,GAC1B;AACA,WAAO;AAAA,EACT;AACA,MAAI,WAAW,SAAS,KAAK,EAAG,QAAO;AACvC,SAAO;AACT;AAXgB;","names":[]}