{"version":3,"sources":["websocket-client-interface.ts"],"sourcesContent":["import { Endpoint } from \"../../types/addressing.ts\";\nimport { client_type } from \"../../utils/constants.ts\";\nimport { InterfaceDirection } from \"../communication-interface.ts\";\nimport { InterfaceProperties } from \"../communication-interface.ts\";\nimport { WebSocketInterface, WebSocketInterfaceSocket } from \"./websocket-interface.ts\";\n\n/**\n * WebSocket client interface for connecting to a WebSocket server\n */\nexport class WebSocketClientInterface extends WebSocketInterface {\n\n    public properties: InterfaceProperties = {\n        type: \"websocket-client\",\n        direction: InterfaceDirection.IN_OUT,\n        latency: 40,\n        bandwidth: 50_000\n    }\n\n    public origin:URL\n    #initialEndpoint?: Endpoint\n    \n    constructor(origin: string|URL, initialEndpoint?: Endpoint) {\n        super()\n\n        // normalize origin\n        if (typeof origin === \"string\") {\n            if (!origin.match(/^\\w+?:\\/\\//)) {\n                origin = 'wss://' + origin\n            }\n            origin = new URL(origin)\n        }\n        if (origin.protocol === \"https:\") origin.protocol = \"wss:\"\n        if (origin.protocol === \"http:\") origin.protocol = \"ws:\"\n        if (origin.protocol !== \"wss:\" && origin.protocol !== \"ws:\") {\n            throw new Error(\"Invalid protocol for WebSocketClientInterface\")\n        }\n\n        this.origin = origin;\n        this.#initialEndpoint = initialEndpoint;\n        this.properties.name = origin.toString();\n    }\n\n    \n    connect() {\n        if (client_type == \"browser\" && !navigator.onLine) {\n            this.logger.error(\"Cannot connect (offline)\")\n            return false;\n        }\n\n        const webSocket = new WebSocket(this.origin);\n        return this.initWebSocket(webSocket)\n    }\n\n    protected addSocket(socket: WebSocketInterfaceSocket) {\n        // set initial endpoint if already known\n        if (this.#initialEndpoint) socket.endpoint = this.#initialEndpoint;\n        return super.addSocket(socket);\n    }\n\n    onWebSocketOpened(_webSocket: WebSocket) {\n        if (this.origin.protocol === 'ws:' && !this.origin.host.match(/(localhost|127\\.0\\.0\\.1)(:\\d+)?/)) \n            this.logger.warn(`unsecure websocket connection to ${this.origin.host}`)\n    }\n\n    onWebSocketClosed(_socket: WebSocketInterfaceSocket) {\n        // only one websocket exists, so we handle a interface connection error here and try to reconnect\n        this.onConnectionError();\n    }\n\n}"],"names":["client_type","InterfaceDirection","WebSocketInterface","WebSocketClientInterface","properties","type","direction","IN_OUT","latency","bandwidth","origin","constructor","initialEndpoint","match","URL","protocol","Error","name","toString","connect","navigator","onLine","logger","error","webSocket","WebSocket","initWebSocket","addSocket","socket","endpoint","onWebSocketOpened","_webSocket","host","warn","onWebSocketClosed","_socket","onConnectionError"],"mappings":"AACA,SAASA,WAAW,QAAQ,2BAA2B;AACvD,SAASC,kBAAkB,QAAQ,gCAAgC;AAEnE,SAASC,kBAAkB,QAAkC,2BAA2B;AAKxF,OAAO,MAAMC,iCAAiCD;IAEnCE,aAAkC;QACrCC,MAAM;QACNC,WAAWL,mBAAmBM,MAAM;QACpCC,SAAS;QACTC,WAAW;IACf,EAAC;IAEMC,OAAU;IACjB,CAAA,eAAgB,CAAW;IAE3BC,YAAYD,MAAkB,EAAEE,eAA0B,CAAE;QAYxD,IAXA,KAAK,IAGiB,YAAlB,OAAOF,WACFA,OAAOG,KAAK,CAAC,iBACdH,CAAAA,SAAS,WAAWA,MAAK,GAE7BA,SAAS,IAAII,IAAIJ,UAEG,aAApBA,OAAOK,QAAQ,IAAeL,CAAAA,OAAOK,QAAQ,GAAG,MAAK,GACjC,YAApBL,OAAOK,QAAQ,IAAcL,CAAAA,OAAOK,QAAQ,GAAG,KAAI,GACnDL,AAAoB,WAApBA,OAAOK,QAAQ,IAAeL,AAAoB,UAApBA,OAAOK,QAAQ,EAC7C,MAAM,AAAIC,MAAM;QAGpB,IAAI,CAACN,MAAM,GAAGA,QACd,IAAI,CAAC,CAAA,eAAgB,GAAGE,iBACxB,IAAI,CAACR,UAAU,CAACa,IAAI,GAAGP,OAAOQ,QAAQ;IAC1C;IAGAC,UAAU;QACN,IAAInB,AAAe,aAAfA,eAA4B,CAACoB,UAAUC,MAAM,EAE7C,OADA,IAAI,CAACC,MAAM,CAACC,K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