{"version":3,"sources":["../../../../src/shared/lib/turbopack/compilation-events.ts"],"sourcesContent":["import type { Project } from '../../../build/swc/types'\nimport * as Log from '../../../build/output/log'\nimport { flushAllTraces, type Span } from '../../../trace'\nimport { traceMemoryUsage } from '../../../lib/memory/trace'\n\nconst MILLISECONDS_IN_NANOSECOND = BigInt(1_000_000)\nexport function msToNs(ms: number): bigint {\n  return BigInt(Math.floor(ms)) * MILLISECONDS_IN_NANOSECOND\n}\n\n/**\n * Subscribes to compilation events for `project` and prints them using the\n * `Log` library.\n *\n * When `parentSpan` is provided, `TraceEvent` compilation events are recorded\n * as trace spans in the `.next/trace` file.\n *\n * Returns a promise that resolves when the subscription ends.  Abort the\n * `signal` to close the underlying async iterator and settle the promise\n * promptly.  The iterator also closes automatically when the Rust side\n * drops the subscription (e.g. after project shutdown).\n */\nexport function backgroundLogCompilationEvents(\n  project: Project,\n  {\n    eventTypes,\n    signal,\n    parentSpan,\n  }: { eventTypes?: string[]; signal?: AbortSignal; parentSpan?: Span } = {}\n): Promise<void> {\n  const iterator = project.compilationEventsSubscribe(eventTypes)\n\n  // Close the iterator as soon as the signal fires so the for-await loop\n  // exits without waiting for the next compilation event.\n  signal?.addEventListener('abort', () => iterator.return?.(undefined as any), {\n    once: true,\n  })\n\n  const promise = (async function () {\n    for await (const event of iterator) {\n      // Record TraceEvent compilation events as trace spans in .next/trace.\n      if (parentSpan && event.typeName === 'TraceEvent' && event.eventJson) {\n        try {\n          const data = JSON.parse(event.eventJson)\n          parentSpan.manualTraceChild(\n            data.name,\n            msToNs(data.startTimeMs),\n            msToNs(data.endTimeMs),\n            Object.fromEntries(data.attributes ?? [])\n          )\n          traceMemoryUsage(data.name, parentSpan)\n          // We flush after each event to make sure it makes it to disk.  These events are rare and\n          // tend to happen at the very end of a build so to make sure they are logged we need to\n          // flush.\n          // NOTE: in a `next build` environment where we are reporting events to the parent thread, this is a no-op.\n          await flushAllTraces()\n        } catch {}\n        continue // don't log these events, they just go to the trace file\n      }\n\n      switch (event.severity) {\n        case 'EVENT':\n          Log.event(event.message)\n          break\n        case 'TRACE':\n          Log.trace(event.message)\n          break\n        case 'INFO':\n          Log.info(event.message)\n          break\n        case 'WARNING':\n          Log.warn(event.message)\n          break\n        case 'ERROR':\n          Log.error(event.message)\n          break\n        case 'FATAL':\n          Log.error(event.message)\n          break\n        default:\n          break\n      }\n    }\n  })()\n  // Prevent unhandled rejection if the subscription errors after the project shuts down.\n  promise.catch(() => {})\n  return promise\n}\n"],"names":["backgroundLogCompilationEvents","msToNs","MILLISECONDS_IN_NANOSECOND","BigInt","ms","Math","floor","project","eventTypes","signal","parentSpan","iterator","compilationEventsSubscribe","addEventListener","return","undefined","once","promise","event","typeName","eventJson","data","JSON","parse","manualTraceChild","name","startTimeMs","endTimeMs","Object","fromEntries","attributes","traceMemoryUsage","flushAllTraces","severity","Log","message","trace","info","warn","error","catch"],"mappings":";;;;;;;;;;;;;;;IAsBgBA,8BAA8B;eAA9BA;;IAhBAC,MAAM;eAANA;;;;+DALK;uBACqB;wBACT;AAEjC,MAAMC,6BAA6BC,OAAO;AACnC,SAASF,OAAOG,EAAU;IAC/B,OAAOD,OAAOE,KAAKC,KAAK,CAACF,OAAOF;AAClC;AAcO,SAASF,+BACdO,OAAgB,EAChB,EACEC,UAAU,EACVC,MAAM,EACNC,UAAU,EACyD,GAAG,CAAC,CAAC;IAE1E,MAAMC,WAAWJ,QAAQK,0BAA0B,CAACJ;IAEpD,uEAAuE;IACvE,wDAAwD;IACxDC,QAAQI,iBAAiB,SAAS,IAAMF,SAASG,MAAM,GAAGC,YAAmB;QAC3EC,MAAM;IACR;IAEA,MAAMC,UAAU,AAAC;QACf,WAAW,MAAMC,SAASP,SAAU;YAClC,sEAAsE;YACtE,IAAID,cAAcQ,MAAMC,QAAQ,KAAK,gBAAgBD,MAAME,SAAS,EAAE;gBACpE,IAAI;oBACF,MAAMC,OAAOC,KAAKC,KAAK,CAACL,MAAME,SAAS;oBACvCV,WAAWc,gBAAgB,CAC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