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CAAC;gBAClC,MAAM,SAAS,GAAa,EAAE,CAAC;gBAE/B,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;oBACzB,IAAI,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,CAAC;wBAC9B,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;oBACnC,CAAC;yBAAM,IAAI,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE,CAAC;wBACpC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC;oBACvC,CAAC;gBACH,CAAC;gBAED,IAAI,SAAS,KAAK,OAAO,EAAE,CAAC;oBAC1B,MAAM,IAAI,KAAK,CAAC,qBAAqB,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;gBAC/D,CAAC;gBACD,IAAI,SAAS,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;oBACzB,MAAM,cAAc,GAAG,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;oBACxD,MAAM,SAAS,CAAC,cAAc,EAAE,SAAS,CAAC,CAAC;gBAC7C,CAAC;YACH,CAAC;QACH,CAAC;QACD,MAAM,OAAO,EAAE,EAAE,CAAC;IACpB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,EAAE,CAAC,KAAK,CAAC,CAAC;IACnB,CAAC;YAAS,CAAC;QACT,IAAI,CAAC;YACH,MAAM,MAAM,EAAE,MAAM,EAAE,CAAC;QACzB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,CAAC,KAAK,CAAC,iCAAiC,EAAE,KAAK,CAAC,CAAC;QAC1D,CAAC;IACH,CAAC;AACH,CAAC,CAAC","sourcesContent":["/**\n * Streams server-sent events from the given URL\n *\n * @param url - The URL to stream events from\n * @param options - The options for the SSE stream\n * @param options.onMessage - The function to call when a message is received\n * @param options.onError - The function to call when an error occurs\n * @param options.onClose - The function to call when the stream finishes successfully\n * @param options.fetchFn - The function to use to fetch the events. Consumers need to provide a fetch function that supports server-sent events.\n */\nexport const fetchServerEvents = async (\n  url: string,\n  {\n    onMessage,\n    onError,\n    onClose,\n    fetchFn,\n    ...requestOptions\n  }: RequestInit & {\n    onMessage: (\n      data: Record<string, unknown>,\n      eventName?: string,\n    ) => Promise<void>;\n    onError?: (err: unknown) => void;\n    onClose?: () => void | Promise<void>;\n    fetchFn: typeof fetch;\n  },\n) => {\n  let reader: ReadableStreamDefaultReader<Uint8Array> | undefined;\n  try {\n    const response = await fetchFn(url, requestOptions);\n    if (!response.ok || !response.body) {\n      throw new Error(`${response.status}`);\n    }\n\n    reader = response.body.getReader();\n    const decoder = new TextDecoder('utf-8');\n    let buffer = '';\n\n    while (true) {\n      const { done, value } = await reader.read();\n\n      if (done) {\n        break;\n      }\n\n      buffer += decoder.decode(value, { stream: true });\n\n      // Split SSE messages at double newlines\n      const parts = buffer.split('\\n\\n');\n      buffer = parts.pop() || '';\n\n      // Split chunks into lines and parse the data\n      for (const chunk of parts) {\n        const lines = chunk.split('\\n');\n        let eventName: string | undefined;\n        const dataLines: string[] = [];\n\n        for (const line of lines) {\n          if (line.startsWith('event:')) {\n            eventName = line.slice(6).trim();\n          } else if (line.startsWith('data:')) {\n            dataLines.push(line.slice(5).trim());\n          }\n        }\n\n        if (eventName === 'error') {\n          throw new Error(`Bridge-api error: ${dataLines.join('\\n')}`);\n        }\n        if (dataLines.length > 0) {\n          const parsedJSONData = JSON.parse(dataLines.join('\\n'));\n          await onMessage(parsedJSONData, eventName);\n        }\n      }\n    }\n    await onClose?.();\n  } catch (error) {\n    onError?.(error);\n  } finally {\n    try {\n      await reader?.cancel();\n    } catch (error) {\n      console.error('Error cleaning up stream reader', error);\n    }\n  }\n};\n"]}