{"version":3,"sources":["../src/hostTransport.ts"],"sourcesContent":["// The host transport resolver (SDK_PACKAGING_SPEC §4, expose-transport) — the ONE place\n// that answers \"how do I reach the host from here\".\n//\n// All host comms (sendMessage / protocolRequest / onMessage) go through this resolver so\n// the SDK is transport-agnostic: it works whether it was INJECTED into the bundler's\n// evaluation context (the current path — `module.evaluation.module.bundler.messageBus`) OR\n// fetched from npm as an ordinary dependency, in which case the sandbox runtime hands it\n// the transport via the §4 discovery global (`globalThis.__immediatelyRun__`).\n//\n// Dual-mode (§8): injection wins while it's active, so existing behaviour is byte-for-byte\n// preserved; the global is the fallback the npm-fetched SDK uses. When injection is removed\n// (phase 3), only the global path remains — `bundler.*` stops being API, which is the whole\n// point.\n//\n// WHY THIS IS ITS OWN MODULE (R3-307). It was the bottom half of `sandboxUtils.ts` until\n// the deadline machinery grew a dependency on the host-attention push channel. A push\n// channel needs the transport, and `withDeadline` needs the channel, so leaving both in one\n// module made `sandboxUtils → hostAttention → pushChannel → sandboxUtils` — a cycle, and\n// this package's import graph is CI-enforced acyclic (`check:circular`; a circular SDK\n// import once made the bundler infinite-loop). Splitting the *primitives* out puts\n// `sandboxUtils` at the TOP of the graph instead of in the middle of it. `sandboxUtils`\n// re-exports `sendMessage`/`addListener`, so every existing import site is unchanged and\n// the api-snapshot entry for that module keeps all four of its names.\nimport { getHostRuntime } from './hostRuntime';\n\nexport interface HostTransport {\n  sendMessage(type: string, data?: Record<string, any>): void;\n  protocolRequest(protocolName: string, method: string, params: Array<any>): Promise<any>;\n  onMessage(handler: (msg: any) => void): { dispose(): void };\n}\n\nexport function transport(): HostTransport {\n  // Injected bundler messageBus first — the current path, unchanged.\n  try {\n    // @ts-ignore - `module.evaluation` is injected by the sandbox runtime\n    const injected = module?.evaluation?.module?.bundler?.messageBus;\n    if (injected && typeof injected.sendMessage === 'function') return injected;\n  } catch {\n    /* no injection in this realm — fall through to the §4 global */\n  }\n  // §4 runtime-discovery transport (the npm-fetched SDK path).\n  const t = getHostRuntime()?.transport as HostTransport | undefined;\n  if (t && typeof t.sendMessage === 'function') return t;\n  throw new Error('immediately.run: no host transport (neither injected nor __immediatelyRun__)');\n}\n\nexport const sendMessage = (type: string, data: Record<string, any> = {}) => {\n  transport().sendMessage(type, data);\n};\n\nexport const addListener = (msgType: string, handler: (msg: any) => void, event?: any): (() => void) => {\n  const onMessage = event ?? transport().onMessage;\n  const disposable = onMessage((msg: any) => {\n    if (msg.type === msgType) {\n      handler(msg);\n    }\n  });\n  return () => disposable.dispose();\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAuBA,yBAA+B;AAQxB,SAAS,YAA2B;AAEzC,MAAI;AAEF,UAAM,WAAW,QAAQ,YAAY,QAAQ,SAAS;AACtD,QAAI,YAAY,OAAO,SAAS,gBAAgB,WAAY,QAAO;AAAA,EACrE,QAAQ;AAAA,EAER;AAEA,QAAM,QAAI,mCAAe,GAAG;AAC5B,MAAI,KAAK,OAAO,EAAE,gBAAgB,WAAY,QAAO;AACrD,QAAM,IAAI,MAAM,8EAA8E;AAChG;AAEO,MAAM,cAAc,CAAC,MAAc,OAA4B,CAAC,MAAM;AAC3E,YAAU,EAAE,YAAY,MAAM,IAAI;AACpC;AAEO,MAAM,cAAc,CAAC,SAAiB,SAA6B,UAA8B;AACtG,QAAM,YAAY,SAAS,UAAU,EAAE;AACvC,QAAM,aAAa,UAAU,CAAC,QAAa;AACzC,QAAI,IAAI,SAAS,SAAS;AACxB,cAAQ,GAAG;AAAA,IACb;AAAA,EACF,CAAC;AACD,SAAO,MAAM,WAAW,QAAQ;AAClC;","names":[]}