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EACL,KAAK,EACH,MAAM,MAAM,2CAEN,KAAK,SAaf,MAAM,4BAA6B,KAAK,SACxC,KAAK,OAAS,IAAI,OAAO,UAAU,KAAK,SACxC,KAAK,OAAO,iBAAiB,OAAQ,KAdrC,MAAM,oCACN,QAAQ,KAAK,CAAC,KAAK,UAAW,KAAK,aAAc,KAAK,MAAM,MACzD,MAAK,IAEJ,GADA,MAAM,qBAAsB,IACxB,EAIF,MAAM,MAAM,iBAHX,KAAK,QAAS,KAAK,QAAU,EAC9B,GAGF,IAOR,EAGF,OAAO,QAAQ,OAAS","file":"/npm/webgram@0.4.1/browser_client.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/webgram@0.4.1/browser_client.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","'use strict'\n\nconst SharedClient = require('./shared_client.js').Client\nconst debug = require('debug')('webgram-client')\n\nclass Client extends SharedClient {\n  constructor (address, options) {\n    if (!address) address = window.serverAddress\n    super(address, options)\n  }\n\n  tryRoot () {\n    return new Promise((resolve) => {\n      const a = document.location.origin.replace(/^http/, 'ws')\n      debug('# computed my call-home address as', a)\n      try {\n        const s = new window.WebSocket(a)\n        s.addEventListener('open', () => {\n          resolve([a, s, 'direct'])\n        })\n      } catch (e) { }\n    })\n  }\n\n  tryViaConf () {\n    return new Promise((resolve) => {\n      window.fetch('/.well-known/webgram.json')\n        .then(response => {\n          response.json()\n            .then(conf => {\n              debug('# fetched json ', JSON.stringify(conf))\n              const a = conf.wsAddress\n              debug('# learned my call-home address is', a)\n              const s = new window.WebSocket(a)\n              s.addEventListener('open', () => {\n                debug('ws open 1!')\n              })\n              s.addEventListener('open', () => {\n                debug('ws open 2! resolving!')\n                resolve([a, s, 'indirect'])\n              })\n              s.addEventListener('open', () => {\n                debug('ws open 3!')\n              })\n            })\n            .catch(err => {\n              debug('# couldnt parse json response', err)\n            })\n        })\n        .catch(err => {\n          debug('# couldnt fetch webgram.json', err)\n        })\n    })\n  }\n\n  sleep (n) {\n    return new Promise((resolve) => {\n      window.setTimeout(resolve, n)\n    })\n  }\n\n  connect () {\n    const onOpen = () => this.onOpen()\n    if (this.socket) {\n      switch (this.socket.readyState) {\n        case 0:\n          this.socket.addEventListener('open', onOpen)\n          break\n        case 1:\n          onOpen()\n          break\n        case 2:\n        case 3:\n          throw Error('passed socket thats already closed')\n      }\n    } else if (!this.address) {\n      debug('no address given, lets be clever')\n      Promise.race([this.tryRoot(), this.tryViaConf(), this.sleep(10)])\n        .then(arg => {\n          debug('# race resolved %o', arg)\n          if (arg) {\n            [this.address, this.socket] = arg\n            onOpen()\n          } else {\n            throw Error('cant connect')\n          }\n        })\n    } else {\n      debug('using provided address %s', this.address)\n      this.socket = new window.WebSocket(this.address)\n      this.socket.addEventListener('open', onOpen)\n    }\n  }\n}\n\nmodule.exports.Client = Client\n\n/*\n\n  connect (buffer) {\n    this.socket = new window.WebSocket(this.address)\n\n    this.socket.addEventListener('message', messageRaw => {\n      this.connected = true\n      messageRaw = messageRaw.data // IN BROWSER\n      // debug('client sees new message', messageRaw)\n      let message\n      try {\n        message = JSON.parse(messageRaw)\n      } catch (e) {\n        console.error('badly formatted message ignored', messageRaw)\n        return\n      }\n      // debug('emitting', message[0], ...message.slice(1))\n      this.emit(...message)\n    })\n    this.socket.addEventListener('open', () => {\n      debug('$online')\n      this.emit('$online')\n      while (true) {\n        let item = this.buffer.shift()\n        if (!item) break\n        this.socket.send(item)\n      }\n      this.connected = true\n    })\n    this.socket.addEventListener('close', () => {\n      this.connected = false\n      debug('websocket closed; retrying ...')\n      window.setTimeout(() => { this.connect(this.buffer) }, 5000)\n      // indicate upstream that we're offline?\n      this.emit('$offline')\n      debug('$offline')\n    })\n    this.socket.addEventListener('error', (e) => {\n      this.connected = false\n      debug('websocket error; retrying in 1s', e)\n      // window.setTimeout(() => { this.connect() }, 1000)\n    })\n  }\n\n  send (...args) {\n    if (this.connected) {\n      this.socket.send(JSON.stringify(args))\n    } else {\n      this.buffer.push(JSON.stringify(args))\n    }\n  }\n\n  close () {\n    this.connected = false\n    this.socket.close()\n  }\n}\n\n*/\n"]}