{"version":3,"file":"iso-ws.cjs","names":["WS: any"],"sources":["../../src/network/iso-ws.ts"],"sourcesContent":["export const WS_STATES = {\n  CONNECTING: 0,\n  OPEN: 1,\n  CLOSING: 2,\n  CLOSED: 3,\n} as const\n\nexport type ErrorEvent = Error\nexport type CloseEvent = {\n  /**\n   * Returns the WebSocket connection close code provided by the server.\n   *\n   * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CloseEvent/code)\n   */\n  readonly code: number\n  /**\n   * Returns the WebSocket connection close reason provided by the server.\n   *\n   * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CloseEvent/reason)\n   */\n  readonly reason: string\n}\nexport type MessageEvent = {\n  data: string | ArrayBuffer\n}\n\nexport interface IsoWS {\n  readonly url: string\n  readonly readyState:\n    | typeof WS_STATES.CONNECTING\n    | typeof WS_STATES.OPEN\n    | typeof WS_STATES.CLOSING\n    | typeof WS_STATES.CLOSED\n  onopen: (() => void) | null\n  onerror: ((error: ErrorEvent) => void) | null\n  onclose: ((event: CloseEvent) => void) | null\n  onmessage: ((event: MessageEvent) => void) | null\n  send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void\n  close(code: number): void\n}\n\nfunction getNativeWebSocket() {\n  if (typeof WebSocket !== 'undefined') {\n    return WebSocket\n  }\n  if (typeof global !== 'undefined' && typeof global.WebSocket !== 'undefined') {\n    return global.WebSocket\n  }\n  if (typeof globalThis !== 'undefined' && typeof globalThis.WebSocket !== 'undefined') {\n    return globalThis.WebSocket\n  }\n  // @ts-expect-error unknown variable in NodeJS but can be defined in other environment\n  if (typeof window !== 'undefined' && typeof window.WebSocket !== 'undefined') {\n    // @ts-expect-error unknown variable in NodeJS but can be defined in other environment\n    return window.WebSocket\n  }\n  // @ts-expect-error unknown variable in NodeJS but can be defined in other environment\n  if (typeof window !== 'undefined' && typeof window.WebSocket !== 'undefined') {\n    // @ts-expect-error unknown variable in NodeJS but can be defined in other environment\n    return window.WebSocket\n  }\n  // @ts-expect-error unknown variable in NodeJS but can be defined in other environment\n  if (typeof self !== 'undefined' && typeof self.WebSocket !== 'undefined') {\n    // @ts-expect-error unknown variable in NodeJS but can be defined in other environment\n    return self.WebSocket\n  }\n}\n\nexport async function newWebSocket(url: string | URL): Promise<IsoWS> {\n  let WS: any\n  try {\n    WS = getNativeWebSocket() ?? (await import('ws')).WebSocket\n  } catch {\n    // noop\n  }\n  if (!WS) {\n    throw new Error(\n      'WebSocket is not supported in this environment and the `ws` package is not installed'\n    )\n  }\n  return new WS(url)\n}\n"],"mappings":";;AAAA,MAAa,YAAY;CACvB,YAAY;CACZ,MAAM;CACN,SAAS;CACT,QAAQ;CACT;AAoCD,SAAS,qBAAqB;AAC5B,KAAI,OAAO,cAAc,YACvB,QAAO;AAET,KAAI,OAAO,WAAW,eAAe,OAAO,OAAO,cAAc,YAC/D,QAAO,OAAO;AAEhB,KAAI,OAAO,eAAe,eAAe,OAAO,WAAW,cAAc,YACvE,QAAO,WAAW;AAGpB,KAAI,OAAO,WAAW,eAAe,OAAO,OAAO,cAAc,YAE/D,QAAO,OAAO;AAGhB,KAAI,OAAO,WAAW,eAAe,OAAO,OAAO,cAAc,YAE/D,QAAO,OAAO;AAGhB,KAAI,OAAO,SAAS,eAAe,OAAO,KAAK,cAAc,YAE3D,QAAO,KAAK;;AAIhB,eAAsB,aAAa,KAAmC;CACpE,IAAIA;AACJ,KAAI;AACF,OAAK,oBAAoB,KAAK,MAAM,OAAO,OAAO;SAC5C;AAGR,KAAI,CAAC,GACH,OAAM,IAAI,MACR,uFACD;AAEH,QAAO,IAAI,GAAG,IAAI"}