teckos-client 0.2.4 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +456 -5
- package/dist/index.min.js +1 -0
- package/es/index.js +459 -0
- package/es/index.mjs +1 -0
- package/lib/index.js +484 -0
- package/package.json +63 -44
- package/src/ITeckosClient.js +2 -0
- package/src/ITeckosClient.js.map +1 -0
- package/src/ITeckosClient.ts +3 -3
- package/src/TeckosClient.js +235 -0
- package/src/TeckosClient.js.map +1 -0
- package/src/TeckosClient.ts +40 -32
- package/src/TeckosClientWithJWT.js +63 -0
- package/src/TeckosClientWithJWT.js.map +1 -0
- package/src/TeckosClientWithJWT.ts +8 -10
- package/src/index.js +8 -0
- package/src/index.js.map +1 -0
- package/src/types/ConnectionState.js +9 -0
- package/src/types/ConnectionState.js.map +1 -0
- package/src/types/Options.js +2 -0
- package/src/types/Options.js.map +1 -0
- package/src/types/Packet.js +2 -0
- package/src/types/Packet.js.map +1 -0
- package/src/types/PacketType.js +7 -0
- package/src/types/PacketType.js.map +1 -0
- package/src/types/SocketEvent.js +2 -0
- package/src/types/SocketEvent.js.map +1 -0
- package/src/types/index.js +4 -0
- package/src/types/index.js.map +1 -0
- package/src/util/Converter.js +6 -0
- package/src/util/Converter.js.map +1 -0
- package/src/util/SocketEventEmitter.js +99 -0
- package/src/util/SocketEventEmitter.js.map +1 -0
- package/src/util/formatProdErrorMessage.ts +16 -0
- package/{dist → types}/ITeckosClient.d.ts +3 -3
- package/{dist → types}/TeckosClient.d.ts +6 -6
- package/{dist → types}/TeckosClientWithJWT.d.ts +0 -0
- package/types/index.d.ts +9 -0
- package/{dist → types}/types/ConnectionState.d.ts +0 -0
- package/{dist → types}/types/Options.d.ts +0 -0
- package/{dist → types}/types/Packet.d.ts +0 -0
- package/{dist → types}/types/PacketType.d.ts +0 -0
- package/{dist → types}/types/SocketEvent.d.ts +0 -0
- package/{dist → types}/types/index.d.ts +0 -0
- package/{dist → types}/util/Converter.d.ts +0 -0
- package/{dist → types}/util/SocketEventEmitter.d.ts +1 -1
- package/types/util/formatProdErrorMessage.d.ts +9 -0
- package/dist/index.d.ts +0 -9
- package/dist/teckos-client.cjs.development.js +0 -617
- package/dist/teckos-client.cjs.development.js.map +0 -1
- package/dist/teckos-client.cjs.production.min.js +0 -2
- package/dist/teckos-client.cjs.production.min.js.map +0 -1
- package/dist/teckos-client.esm.js +0 -614
- package/dist/teckos-client.esm.js.map +0 -1
@@ -1,2 +0,0 @@
|
|
1
|
-
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var n=e(require("debug")),t=e(require("isomorphic-ws"));function r(){return(r=Object.assign||function(e){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])}return e}).apply(this,arguments)}function o(e,n){e.prototype=Object.create(n.prototype),e.prototype.constructor=e,(Object.setPrototypeOf||function(e,n){return e.__proto__=n,e})(e,n)}var i,c,s=new TextEncoder,a=new TextDecoder;(i=exports.PacketType||(exports.PacketType={}))[i.EVENT=0]="EVENT",i[i.ACK=1]="ACK",(c=exports.ConnectionState||(exports.ConnectionState={})).DISCONNECTED="disconnected",c.CONNECTING="connecting",c.CONNECTED="connected",c.DISCONNECTING="disconnecting";var u=function(){var e=this;this.maxListeners=50,this.handlers={},this.addListener=function(n,t){if(Object.keys(e.handlers).length===e.maxListeners)throw new Error("Max listeners reached");if("function"!=typeof t)throw new Error("The given listener is not a function");return e.handlers[n]=e.handlers[n]||[],e.handlers[n].push(t),e},this.once=function(n,t){if(Object.keys(e.handlers).length===e.maxListeners)throw new Error("Max listeners reached");if("function"!=typeof t)throw new Error("The given listener is not a function");return e.handlers[n]=e.handlers[n]||[],e.handlers[n].push((function r(){t(),e.off(n,r)})),e},this.removeListener=function(n,t){return e.handlers[n]&&(e.handlers[n]=e.handlers[n].filter((function(e){return e!==t}))),e},this.off=function(n,t){return e.removeListener(n,t)},this.removeAllListeners=function(n){return n?delete e.handlers[n]:e.handlers={},e},this.setMaxListeners=function(n){return e.maxListeners=n,e},this.getMaxListeners=function(){return e.maxListeners},this.listeners=function(n){return e.handlers[n]?[].concat(e.handlers[n]):[]},this.rawListeners=function(n){return[].concat(e.handlers[n])},this.listenerCount=function(n){return e.handlers[n]?Object.keys(e.handlers[n]).length:0},this.prependListener=function(n,t){if(Object.keys(e.handlers).length===e.maxListeners)throw new Error("Max listeners reached");return e.handlers[n]=e.handlers[n]||[],e.handlers[n].unshift(t),e},this.prependOnceListener=function(n,t){if(Object.keys(e.handlers).length===e.maxListeners)throw new Error("Max listeners reached");return e.handlers[n]=e.handlers[n]||[],e.handlers[n].unshift((function r(){t(),e.off(n,r)})),e},this.eventNames=function(){return Object.keys(e.handlers)},this.on=function(n,t){return e.addListener(n,t)},this.emit=function(n){for(var t=arguments.length,r=new Array(t>1?t-1:0),o=1;o<t;o++)r[o-1]=arguments[o];var i=e.listeners(n);return i.length>0&&(i.forEach((function(e){e&&e(r)})),!0)}},l=n("teckos:client"),d={reconnection:!0,reconnectionDelay:1e3,reconnectionDelayMax:5e3,reconnectionAttempts:Infinity,randomizationFactor:.5,timeout:5e3,debug:!1},f=function(e){function n(n,o){var i;return(i=e.call(this)||this).currentReconnectionAttempts=0,i.acks=new Map,i.fnId=0,i.attachHandler=function(){i.ws&&(i.ws.onopen=i.handleOpen,i.ws.onerror=i.handleError,i.ws.onclose=i.handleClose,i.ws.onmessage=i.handleMessage)},i.connect=function(){i.options.debug&&l("Connecting to "+i.url+"..."),i.ws=new t(i.url),i.attachHandler(),i.connectionTimeout=setTimeout((function(){i.ws&&i.ws.readyState===t.CONNECTING&&i.ws.close()}),i.options.timeout)},i.reconnect=function(){i.listeners("reconnect_attempt").forEach((function(e){return e()})),i.connect()},i.emit=function(e){for(var n=arguments.length,t=new Array(n>1?n-1:0),r=1;r<n;r++)t[r-1]=arguments[r];t.unshift(e);var o={type:exports.PacketType.EVENT,data:t};return"function"==typeof t[t.length-1]&&(i.acks.set(i.fnId,t.pop()),o.id=i.fnId,i.fnId+=1),i.sendPackage(o)},i.send=function(){for(var e=arguments.length,n=new Array(e),t=0;t<e;t++)n[t]=arguments[t];return n.unshift("message"),i.sendPackage({type:exports.PacketType.EVENT,data:n})},i.sendPackage=function(e){if(void 0!==i.ws&&i.ws.readyState===t.OPEN){var n=function(e){return s.encode(JSON.stringify(e))}(e);return i.options.debug&&l("["+i.url+"] Send packet: "+JSON.stringify(e)),i.ws.send(n),!0}return!1},i.handleMessage=function(e){var n="string"==typeof e.data?JSON.parse(e.data):JSON.parse(a.decode(e.data).toString());if(i.options.debug&&l("["+i.url+"] Got packet: "+JSON.stringify(n)),n.type===exports.PacketType.EVENT){var t=n.data[0],r=n.data.slice(1);if(!t)throw new Error("[teckos-client@"+i.url+"] Got invalid event message: "+JSON.stringify(e.data));i.listeners(t).forEach((function(e){return e.apply(void 0,r)}))}else{if(n.type!==exports.PacketType.ACK||void 0===n.id)throw new Error("[teckos-client@"+i.url+"] Got invalid message type: "+n.type);var o=i.acks.get(n.id);"function"==typeof o&&(o.apply(function(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(i),n.data),i.acks.delete(n.id))}},i.handleOpen=function(){i.currentReconnectionAttempts>0&&(i.currentReconnectDelay=i.options.reconnectionDelay,i.currentReconnectionAttempts=0,i.options.debug&&l("["+i.url+"] Reconnected!"),i.listeners("reconnect").forEach((function(e){return e()}))),i.options.debug&&l("["+i.url+"] Connected!"),i.listeners("connect").forEach((function(e){return e()}))},i.handleError=function(e){i.handlers&&i.handlers.error&&(i.options.debug&&l("["+i.url+"] Got error from server: "+JSON.stringify(e)),i.handlers.error.forEach((function(n){return n(e)})))},i.handleClose=function(){if(i.connectionTimeout&&clearTimeout(i.connectionTimeout),i.reconnectionTimeout&&clearTimeout(i.reconnectionTimeout),i.currentReconnectionAttempts>0?(i.options.debug&&l("["+i.url+"] Reconnect #"+i.currentReconnectionAttempts+" failed!"),i.listeners("reconnect_error").forEach((function(e){e&&e()}))):(i.options.debug&&l("["+i.url+"] Disconnected!"),i.listeners("disconnect").forEach((function(e){e&&e()}))),i.options.reconnection)if(i.currentReconnectionAttempts+=1,Infinity===i.options.reconnectionAttempts||i.currentReconnectionAttempts<=i.options.reconnectionAttempts){var e=Math.min(i.options.reconnectionDelayMax,i.currentReconnectDelay);i.currentReconnectDelay=Math.round(i.currentReconnectDelay+i.currentReconnectDelay*i.options.randomizationFactor),i.options.debug&&l("["+i.url+"] Try reconnecting ("+i.currentReconnectionAttempts+"/"+i.options.reconnectionAttempts+") in "+e+"ms to "+i.url+"..."),i.reconnectionTimeout=setTimeout((function(){i.reconnect()}),e)}else i.options.debug&&l("["+i.url+"] Reconnection maximum of "+i.options.reconnectionAttempts+" reached"),i.listeners("reconnect_failed").forEach((function(e){return e()}))},i.close=function(){i.options.debug&&l("["+i.url+"] Closing connection (client-side)"),void 0!==i.ws&&(i.ws.onclose=function(){},i.ws.close(),i.listeners("disconnect").forEach((function(e){return e()})))},i.disconnect=function(){i.close()},i.options=r({},d,o),i.currentReconnectDelay=i.options.reconnectionDelay,i.url=n,i}var i,c;return o(n,e),n.prototype.getConnectionState=function(){if(this.ws)switch(this.ws.readyState){case t.OPEN:return exports.ConnectionState.CONNECTED;case t.CONNECTING:return exports.ConnectionState.CONNECTING;case t.CLOSING:return exports.ConnectionState.DISCONNECTING;default:return exports.ConnectionState.DISCONNECTED}return exports.ConnectionState.DISCONNECTED},i=n,(c=[{key:"webSocket",get:function(){return this.ws}},{key:"state",get:function(){return this.getConnectionState()}},{key:"connected",get:function(){return this.getConnectionState()===exports.ConnectionState.CONNECTED}},{key:"disconnected",get:function(){return this.getConnectionState()===exports.ConnectionState.DISCONNECTED}}])&&function(e,n){for(var t=0;t<n.length;t++){var r=n[t];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}(i.prototype,c),Object.defineProperty(i,"prototype",{writable:!1}),n}(u),h=n("teckos:client"),p=function(e){function n(n,t,o,i){var c;return(c=e.call(this,n,t)||this).receivedReady=!1,c.handleReadyEvent=function(){c.options.debug&&h("["+c.url+"] Connected!"),c.receivedReady=!0,c.currentReconnectionAttempts>0&&(c.options.debug&&h("["+c.url+"] Reconnected!"),c.listeners("reconnect").forEach((function(e){return e()})),c.currentReconnectDelay=c.options.reconnectionDelay,c.currentReconnectionAttempts=0),c.listeners("connect").forEach((function(e){return e()}))},c.handleOpen=function(){c.receivedReady=!1,c.once("ready",c.handleReadyEvent),c.options.debug&&h("Connection opened, sending token now"),c.emit("token",r({token:c.token},c.initialData))},c.token=o,c.initialData=i,c.on("disconnect",(function(){c.receivedReady=!1})),c}return o(n,e),n.prototype.getConnectionState=function(){if(this.ws)switch(this.ws.readyState){case t.OPEN:return this.receivedReady?exports.ConnectionState.CONNECTED:exports.ConnectionState.CONNECTING;case t.CONNECTING:return exports.ConnectionState.CONNECTING;case t.CLOSING:return exports.ConnectionState.DISCONNECTING;default:return exports.ConnectionState.DISCONNECTED}return exports.ConnectionState.DISCONNECTED},n}(f);exports.TeckosClient=f,exports.TeckosClientWithJWT=p;
|
2
|
-
//# sourceMappingURL=teckos-client.cjs.production.min.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"teckos-client.cjs.production.min.js","sources":["../src/util/Converter.ts","../src/types/PacketType.ts","../src/types/ConnectionState.ts","../src/util/SocketEventEmitter.ts","../src/TeckosClient.ts","../src/TeckosClientWithJWT.ts"],"sourcesContent":["import { Packet } from '../types'\n\nconst enc = new TextEncoder()\nconst dec = new TextDecoder()\n\nconst encodePacket = (packet: Packet): ArrayBufferLike => enc.encode(JSON.stringify(packet))\nconst decodePacket = (buffer: ArrayBuffer): Packet =>\n JSON.parse(dec.decode(buffer).toString()) as Packet\n\nexport { encodePacket, decodePacket }\n","enum PacketType {\n EVENT,\n ACK,\n}\nexport { PacketType }\n","enum ConnectionState {\n DISCONNECTED = 'disconnected',\n CONNECTING = 'connecting',\n CONNECTED = 'connected',\n DISCONNECTING = 'disconnecting',\n}\nexport { ConnectionState }\n","export type Listener = (...args: any[]) => void\n\nclass SocketEventEmitter<T extends string> {\n protected maxListeners = 50\n\n protected handlers: {\n [event: string]: ((...args: any[]) => void)[]\n } = {}\n\n public addListener = (event: T, listener: (...args: any[]) => void): this => {\n if (Object.keys(this.handlers).length === this.maxListeners) {\n throw new Error('Max listeners reached')\n }\n if (typeof listener !== 'function') {\n throw new Error('The given listener is not a function')\n }\n this.handlers[event] = this.handlers[event] || []\n this.handlers[event].push(listener)\n return this\n }\n\n public once = (event: T, listener: (...args: any[]) => void): this => {\n if (Object.keys(this.handlers).length === this.maxListeners) {\n throw new Error('Max listeners reached')\n }\n if (typeof listener !== 'function') {\n throw new Error('The given listener is not a function')\n }\n this.handlers[event] = this.handlers[event] || []\n const onceWrapper = () => {\n listener()\n this.off(event, onceWrapper)\n }\n this.handlers[event].push(onceWrapper)\n return this\n }\n\n public removeListener = (event: T, listener: (...args: any[]) => void): this => {\n if (this.handlers[event]) {\n this.handlers[event] = this.handlers[event].filter((handler) => handler !== listener)\n }\n return this\n }\n\n public off = (event: T, listener: (...args: any[]) => void): this =>\n this.removeListener(event, listener)\n\n public removeAllListeners = (event?: T): this => {\n if (event) {\n delete this.handlers[event]\n } else {\n this.handlers = {}\n }\n return this\n }\n\n public setMaxListeners = (n: number): this => {\n this.maxListeners = n\n return this\n }\n\n public getMaxListeners = (): number => this.maxListeners\n\n public listeners = (event: T): Listener[] => {\n if (this.handlers[event]) {\n return [...this.handlers[event]]\n }\n return []\n }\n\n public rawListeners = (event: T): Listener[] => [...this.handlers[event]]\n\n public listenerCount = (event: T): number => {\n if (this.handlers[event]) {\n return Object.keys(this.handlers[event]).length\n }\n return 0\n }\n\n public prependListener = (event: T, listener: (...args: any[]) => void): this => {\n if (Object.keys(this.handlers).length === this.maxListeners) {\n throw new Error('Max listeners reached')\n }\n this.handlers[event] = this.handlers[event] || []\n this.handlers[event].unshift(listener)\n return this\n }\n\n public prependOnceListener = (event: T, listener: (...args: any[]) => void): this => {\n if (Object.keys(this.handlers).length === this.maxListeners) {\n throw new Error('Max listeners reached')\n }\n this.handlers[event] = this.handlers[event] || []\n const onceWrapper = () => {\n listener()\n this.off(event, onceWrapper)\n }\n this.handlers[event].unshift(onceWrapper)\n return this\n }\n\n public eventNames = (): T[] => Object.keys(this.handlers) as T[]\n\n public on = (event: T, listener: (...args: any[]) => void): this =>\n this.addListener(event, listener)\n\n public emit = (event: T, ...args: any[]): boolean => {\n const listeners = this.listeners(event)\n if (listeners.length > 0) {\n listeners.forEach((listener) => {\n if (listener) listener(args)\n })\n return true\n }\n return false\n }\n}\n\nexport { SocketEventEmitter }\n","import debug from 'debug'\nimport WebSocket from 'isomorphic-ws'\nimport { decodePacket, encodePacket } from './util/Converter'\nimport { ConnectionState, OptionalOptions, Options, Packet, PacketType, SocketEvent } from './types'\nimport { ITeckosClient } from './ITeckosClient'\nimport { SocketEventEmitter } from './util/SocketEventEmitter'\n\nconst d = debug('teckos:client')\n\nconst DEFAULT_OPTIONS: Options = {\n reconnection: true,\n reconnectionDelay: 1000,\n reconnectionDelayMax: 5000,\n reconnectionAttempts: Infinity,\n randomizationFactor: 0.5,\n timeout: 5000,\n debug: false,\n}\n\nclass TeckosClient extends SocketEventEmitter<SocketEvent> implements ITeckosClient {\n protected readonly url: string\n\n protected readonly options: Options\n\n ws: WebSocket | undefined\n\n protected currentReconnectDelay: number\n\n protected currentReconnectionAttempts = 0\n\n protected acks: Map<number, (...args: any[]) => void> = new Map()\n\n protected fnId = 0\n\n protected connectionTimeout: any | undefined\n\n protected reconnectionTimeout: any | undefined\n\n constructor(url: string, options?: OptionalOptions) {\n super()\n this.options = {\n ...DEFAULT_OPTIONS,\n ...options,\n }\n this.currentReconnectDelay = this.options.reconnectionDelay\n this.url = url\n }\n\n protected attachHandler = (): void => {\n if (this.ws) {\n this.ws.onopen = this.handleOpen\n this.ws.onerror = this.handleError\n this.ws.onclose = this.handleClose\n this.ws.onmessage = this.handleMessage\n }\n }\n\n public get webSocket(): WebSocket | undefined {\n return this.ws\n }\n\n public connect = (): void => {\n if (this.options.debug) d(`Connecting to ${this.url}...`)\n\n // This will try to connect immediately\n this.ws = new WebSocket(this.url)\n // Attach handlers\n this.attachHandler()\n // Handle timeout\n this.connectionTimeout = setTimeout(() => {\n if (this.ws && this.ws.readyState === WebSocket.CONNECTING) {\n this.ws.close()\n }\n }, this.options.timeout)\n }\n\n protected reconnect = (): void => {\n this.listeners('reconnect_attempt').forEach((listener) => listener())\n this.connect()\n }\n\n protected getConnectionState(): ConnectionState {\n if (this.ws) {\n switch (this.ws.readyState) {\n case WebSocket.OPEN:\n return ConnectionState.CONNECTED\n case WebSocket.CONNECTING:\n return ConnectionState.CONNECTING\n case WebSocket.CLOSING:\n return ConnectionState.DISCONNECTING\n default:\n return ConnectionState.DISCONNECTED\n }\n }\n return ConnectionState.DISCONNECTED\n }\n\n public get state(): ConnectionState {\n return this.getConnectionState()\n }\n\n get connected(): boolean {\n return this.getConnectionState() === ConnectionState.CONNECTED\n }\n\n get disconnected(): boolean {\n return this.getConnectionState() === ConnectionState.DISCONNECTED\n }\n\n public emit = (event: SocketEvent, ...args: any[]): boolean => {\n args.unshift(event)\n\n const packet: Packet = {\n type: PacketType.EVENT,\n data: args,\n }\n\n if (typeof args[args.length - 1] === 'function') {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-argument\n this.acks.set(this.fnId, args.pop())\n packet.id = this.fnId\n this.fnId += 1\n }\n\n return this.sendPackage(packet)\n }\n\n public send = (...args: any[]): boolean => {\n args.unshift('message')\n return this.sendPackage({\n type: PacketType.EVENT,\n data: args,\n })\n }\n\n public sendPackage = (packet: Packet): boolean => {\n if (this.ws !== undefined && this.ws.readyState === WebSocket.OPEN) {\n const buffer = encodePacket(packet)\n if (this.options.debug) d(`[${this.url}] Send packet: ${JSON.stringify(packet)}`)\n this.ws.send(buffer)\n return true\n }\n return false\n }\n\n protected handleMessage = (msg: WebSocket.MessageEvent): void => {\n const packet =\n typeof msg.data === 'string'\n ? (JSON.parse(msg.data) as Packet)\n : decodePacket(msg.data as ArrayBuffer)\n if (this.options.debug) d(`[${this.url}] Got packet: ${JSON.stringify(packet)}`)\n if (packet.type === PacketType.EVENT) {\n const event = packet.data[0] as SocketEvent\n const args = packet.data.slice(1)\n if (event) {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-argument\n this.listeners(event).forEach((listener) => listener(...args))\n } else {\n throw new Error(\n `[teckos-client@${this.url}] Got invalid event message: ${JSON.stringify(\n msg.data\n )}`\n )\n }\n } else if (packet.type === PacketType.ACK && packet.id !== undefined) {\n // Call assigned function\n const ack = this.acks.get(packet.id)\n if (typeof ack === 'function') {\n ack.apply(this, packet.data)\n this.acks.delete(packet.id)\n }\n } else {\n throw new Error(`[teckos-client@${this.url}] Got invalid message type: ${packet.type}`)\n }\n }\n\n protected handleOpen = (): void => {\n if (this.currentReconnectionAttempts > 0) {\n // Reset reconnection settings to default\n this.currentReconnectDelay = this.options.reconnectionDelay\n this.currentReconnectionAttempts = 0\n\n // Inform listeners\n if (this.options.debug) d(`[${this.url}] Reconnected!`)\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n this.listeners('reconnect').forEach((listener) => listener())\n }\n // Inform listeners\n if (this.options.debug) d(`[${this.url}] Connected!`)\n this.listeners('connect').forEach((listener) => listener())\n }\n\n protected handleError = (error: WebSocket.ErrorEvent): void => {\n if (this.handlers && this.handlers.error) {\n if (this.options.debug)\n d(`[${this.url}] Got error from server: ${JSON.stringify(error)}`)\n this.handlers.error.forEach((listener) => listener(error))\n }\n }\n\n protected handleClose = (): void => {\n // Stop connection timeout\n if (this.connectionTimeout) {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-argument\n clearTimeout(this.connectionTimeout)\n }\n // Stop reconnection timeout\n if (this.reconnectionTimeout) {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-argument\n clearTimeout(this.reconnectionTimeout)\n }\n\n // Inform listeners\n if (this.currentReconnectionAttempts > 0) {\n if (this.options.debug)\n d(`[${this.url}] Reconnect #${this.currentReconnectionAttempts} failed!`)\n this.listeners('reconnect_error').forEach((listener) => {\n if (listener) listener()\n })\n } else {\n if (this.options.debug) d(`[${this.url}] Disconnected!`)\n this.listeners('disconnect').forEach((listener) => {\n if (listener) listener()\n })\n }\n\n if (this.options.reconnection) {\n // Apply reconnection logic\n this.currentReconnectionAttempts += 1\n\n if (\n this.options.reconnectionAttempts === Infinity ||\n this.currentReconnectionAttempts <= this.options.reconnectionAttempts\n ) {\n const timeout = Math.min(\n this.options.reconnectionDelayMax,\n this.currentReconnectDelay\n )\n // Increase reconnection delay\n this.currentReconnectDelay = Math.round(\n this.currentReconnectDelay +\n this.currentReconnectDelay * this.options.randomizationFactor\n )\n\n if (this.options.debug)\n d(\n `[${this.url}] Try reconnecting (${this.currentReconnectionAttempts}/${this.options.reconnectionAttempts}) in ${timeout}ms to ${this.url}...`\n )\n this.reconnectionTimeout = setTimeout(() => {\n this.reconnect()\n }, timeout)\n } else {\n if (this.options.debug)\n d(\n `[${this.url}] Reconnection maximum of ${this.options.reconnectionAttempts} reached`\n )\n this.listeners('reconnect_failed').forEach((listener) => listener())\n }\n }\n }\n\n public close = (): void => {\n if (this.options.debug) d(`[${this.url}] Closing connection (client-side)`)\n if (this.ws !== undefined) {\n this.ws.onclose = () => {}\n this.ws.close()\n this.listeners('disconnect').forEach((listener) => listener())\n }\n }\n\n public disconnect = (): void => {\n this.close()\n }\n}\n\nexport { TeckosClient }\n","import debug from 'debug'\nimport WebSocket from 'isomorphic-ws'\nimport { TeckosClient } from './TeckosClient'\nimport { OptionalOptions, ConnectionState } from './types'\n\nconst d = debug('teckos:client')\n\nclass TeckosClientWithJWT extends TeckosClient {\n protected readonly token: string\n\n protected readonly initialData: any\n\n protected receivedReady = false\n\n // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types\n constructor(url: string, options: OptionalOptions, token: string, initialData?: any) {\n super(url, options)\n this.token = token\n // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n this.initialData = initialData\n this.on('disconnect', () => {\n this.receivedReady = false\n })\n }\n\n protected getConnectionState(): ConnectionState {\n if (this.ws) {\n switch (this.ws.readyState) {\n case WebSocket.OPEN:\n if (this.receivedReady) {\n return ConnectionState.CONNECTED\n }\n return ConnectionState.CONNECTING\n case WebSocket.CONNECTING:\n return ConnectionState.CONNECTING\n case WebSocket.CLOSING:\n return ConnectionState.DISCONNECTING\n default:\n return ConnectionState.DISCONNECTED\n }\n }\n return ConnectionState.DISCONNECTED\n }\n\n protected handleReadyEvent = (): void => {\n if (this.options.debug) d(`[${this.url}] Connected!`)\n this.receivedReady = true\n if (this.currentReconnectionAttempts > 0) {\n if (this.options.debug) d(`[${this.url}] Reconnected!`)\n this.listeners('reconnect').forEach((listener) => listener())\n // Reset reconnection settings to default\n this.currentReconnectDelay = this.options.reconnectionDelay\n this.currentReconnectionAttempts = 0\n }\n this.listeners('connect').forEach((listener) => listener())\n }\n\n protected handleOpen = (): void => {\n this.receivedReady = false\n this.once('ready', this.handleReadyEvent)\n if (this.options.debug) d('Connection opened, sending token now')\n this.emit('token', {\n token: this.token,\n ...this.initialData,\n })\n }\n}\n\nexport { TeckosClientWithJWT }\n"],"names":["PacketType","ConnectionState","enc","TextEncoder","dec","TextDecoder","SocketEventEmitter","event","listener","Object","keys","_this","handlers","length","maxListeners","Error","push","onceWrapper","off","filter","handler","removeListener","n","unshift","addListener","args","listeners","forEach","d","debug","DEFAULT_OPTIONS","reconnection","reconnectionDelay","reconnectionDelayMax","reconnectionAttempts","Infinity","randomizationFactor","timeout","TeckosClient","url","options","Map","ws","onopen","handleOpen","onerror","handleError","onclose","handleClose","onmessage","handleMessage","WebSocket","attachHandler","connectionTimeout","setTimeout","readyState","CONNECTING","close","connect","packet","type","EVENT","data","acks","set","fnId","pop","id","sendPackage","undefined","OPEN","buffer","encode","JSON","stringify","encodePacket","send","msg","parse","decode","toString","slice","ACK","ack","get","apply","currentReconnectionAttempts","currentReconnectDelay","error","clearTimeout","reconnectionTimeout","Math","min","round","reconnect","getConnectionState","this","CONNECTED","CLOSING","DISCONNECTING","DISCONNECTED","TeckosClientWithJWT","token","initialData","receivedReady","once","handleReadyEvent","emit","on"],"mappings":"siBAEA,ICFKA,ECAAC,EFECC,EAAM,IAAIC,YACVC,EAAM,IAAIC,aCHXL,EAAAA,qBAAAA,2CAEDA,kBCFCC,EAAAA,0BAAAA,yDAEDA,0BACAA,wBACAA,oCCFEK,EAAN,wCAC6B,iBAIrB,oBAEiB,SAACC,EAAUC,MACxBC,OAAOC,KAAKC,EAAKC,UAAUC,SAAWF,EAAKG,mBACrC,IAAIC,MAAM,4BAEI,mBAAbP,QACD,IAAIO,MAAM,+CAEpBJ,EAAKC,SAASL,GAASI,EAAKC,SAASL,IAAU,GAC/CI,EAAKC,SAASL,GAAOS,KAAKR,GACnBG,aAGG,SAACJ,EAAUC,MACjBC,OAAOC,KAAKC,EAAKC,UAAUC,SAAWF,EAAKG,mBACrC,IAAIC,MAAM,4BAEI,mBAAbP,QACD,IAAIO,MAAM,+CAEpBJ,EAAKC,SAASL,GAASI,EAAKC,SAASL,IAAU,GAK/CI,EAAKC,SAASL,GAAOS,MAJD,SAAdC,IACFT,IACAG,EAAKO,IAAIX,EAAOU,MAGbN,uBAGa,SAACJ,EAAUC,UAC3BG,EAAKC,SAASL,KACdI,EAAKC,SAASL,GAASI,EAAKC,SAASL,GAAOY,QAAO,SAACC,UAAYA,IAAYZ,MAEzEG,YAGE,SAACJ,EAAUC,UACpBG,EAAKU,eAAed,EAAOC,4BAEH,SAACD,UACrBA,SACOI,EAAKC,SAASL,GAErBI,EAAKC,SAAW,GAEbD,wBAGc,SAACW,UACtBX,EAAKG,aAAeQ,EACbX,wBAGc,kBAAcA,EAAKG,6BAEzB,SAACP,UACZI,EAAKC,SAASL,aACHI,EAAKC,SAASL,IAE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AK4B,wBAC9Bb,UAAU,aAAaC,SAAQ,SAACnB,UAAaA,SAE7C+E,sBAAwB5E,EAAK6B,QAAQR,oBACrCsD,4BAA8B,KAElC5D,UAAU,WAAWC,SAAQ,SAACnB,UAAaA,qBAG7B,aACdgG,eAAgB,IAChBC,KAAK,QAAS9F,EAAK+F,kBACpB/F,EAAK6B,QAAQX,OAAOD,EAAE,0CACrB+E,KAAK,WACNL,MAAO3F,EAAK2F,OACT3F,EAAK4F,iBA9CPD,MAAQA,IAERC,YAAcA,IACdK,GAAG,cAAc,aACbJ,eAAgB,iCAInBT,mBAAA,cACFC,KAAKtD,UACGsD,KAAKtD,GAAGa,iBACPJ,EAAUmB,YACP0B,KAAKQ,cACEvG,wBAAgBgG,UAEpBhG,wBAAgBuD,gBACtBL,EAAUK,kBACJvD,wBAAgBuD,gBACtBL,EAAU+C,eACJjG,wBAAgBkG,6BAEhBlG,wBAAgBmG,oBAG5BnG,wBAAgBmG,iBAlCG9D"}
|
@@ -1,614 +0,0 @@
|
|
1
|
-
import debug from 'debug';
|
2
|
-
import WebSocket from 'isomorphic-ws';
|
3
|
-
|
4
|
-
function _defineProperties(target, props) {
|
5
|
-
for (var i = 0; i < props.length; i++) {
|
6
|
-
var descriptor = props[i];
|
7
|
-
descriptor.enumerable = descriptor.enumerable || false;
|
8
|
-
descriptor.configurable = true;
|
9
|
-
if ("value" in descriptor) descriptor.writable = true;
|
10
|
-
Object.defineProperty(target, descriptor.key, descriptor);
|
11
|
-
}
|
12
|
-
}
|
13
|
-
|
14
|
-
function _createClass(Constructor, protoProps, staticProps) {
|
15
|
-
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
16
|
-
if (staticProps) _defineProperties(Constructor, staticProps);
|
17
|
-
Object.defineProperty(Constructor, "prototype", {
|
18
|
-
writable: false
|
19
|
-
});
|
20
|
-
return Constructor;
|
21
|
-
}
|
22
|
-
|
23
|
-
function _extends() {
|
24
|
-
_extends = Object.assign || function (target) {
|
25
|
-
for (var i = 1; i < arguments.length; i++) {
|
26
|
-
var source = arguments[i];
|
27
|
-
|
28
|
-
for (var key in source) {
|
29
|
-
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
30
|
-
target[key] = source[key];
|
31
|
-
}
|
32
|
-
}
|
33
|
-
}
|
34
|
-
|
35
|
-
return target;
|
36
|
-
};
|
37
|
-
|
38
|
-
return _extends.apply(this, arguments);
|
39
|
-
}
|
40
|
-
|
41
|
-
function _inheritsLoose(subClass, superClass) {
|
42
|
-
subClass.prototype = Object.create(superClass.prototype);
|
43
|
-
subClass.prototype.constructor = subClass;
|
44
|
-
|
45
|
-
_setPrototypeOf(subClass, superClass);
|
46
|
-
}
|
47
|
-
|
48
|
-
function _setPrototypeOf(o, p) {
|
49
|
-
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
|
50
|
-
o.__proto__ = p;
|
51
|
-
return o;
|
52
|
-
};
|
53
|
-
|
54
|
-
return _setPrototypeOf(o, p);
|
55
|
-
}
|
56
|
-
|
57
|
-
function _assertThisInitialized(self) {
|
58
|
-
if (self === void 0) {
|
59
|
-
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
60
|
-
}
|
61
|
-
|
62
|
-
return self;
|
63
|
-
}
|
64
|
-
|
65
|
-
var enc = /*#__PURE__*/new TextEncoder();
|
66
|
-
var dec = /*#__PURE__*/new TextDecoder();
|
67
|
-
|
68
|
-
var encodePacket = function encodePacket(packet) {
|
69
|
-
return enc.encode(JSON.stringify(packet));
|
70
|
-
};
|
71
|
-
|
72
|
-
var decodePacket = function decodePacket(buffer) {
|
73
|
-
return JSON.parse(dec.decode(buffer).toString());
|
74
|
-
};
|
75
|
-
|
76
|
-
var PacketType;
|
77
|
-
|
78
|
-
(function (PacketType) {
|
79
|
-
PacketType[PacketType["EVENT"] = 0] = "EVENT";
|
80
|
-
PacketType[PacketType["ACK"] = 1] = "ACK";
|
81
|
-
})(PacketType || (PacketType = {}));
|
82
|
-
|
83
|
-
var ConnectionState;
|
84
|
-
|
85
|
-
(function (ConnectionState) {
|
86
|
-
ConnectionState["DISCONNECTED"] = "disconnected";
|
87
|
-
ConnectionState["CONNECTING"] = "connecting";
|
88
|
-
ConnectionState["CONNECTED"] = "connected";
|
89
|
-
ConnectionState["DISCONNECTING"] = "disconnecting";
|
90
|
-
})(ConnectionState || (ConnectionState = {}));
|
91
|
-
|
92
|
-
var SocketEventEmitter = function SocketEventEmitter() {
|
93
|
-
var _this = this;
|
94
|
-
|
95
|
-
this.maxListeners = 50;
|
96
|
-
this.handlers = {};
|
97
|
-
|
98
|
-
this.addListener = function (event, listener) {
|
99
|
-
if (Object.keys(_this.handlers).length === _this.maxListeners) {
|
100
|
-
throw new Error('Max listeners reached');
|
101
|
-
}
|
102
|
-
|
103
|
-
if (typeof listener !== 'function') {
|
104
|
-
throw new Error('The given listener is not a function');
|
105
|
-
}
|
106
|
-
|
107
|
-
_this.handlers[event] = _this.handlers[event] || [];
|
108
|
-
|
109
|
-
_this.handlers[event].push(listener);
|
110
|
-
|
111
|
-
return _this;
|
112
|
-
};
|
113
|
-
|
114
|
-
this.once = function (event, listener) {
|
115
|
-
if (Object.keys(_this.handlers).length === _this.maxListeners) {
|
116
|
-
throw new Error('Max listeners reached');
|
117
|
-
}
|
118
|
-
|
119
|
-
if (typeof listener !== 'function') {
|
120
|
-
throw new Error('The given listener is not a function');
|
121
|
-
}
|
122
|
-
|
123
|
-
_this.handlers[event] = _this.handlers[event] || [];
|
124
|
-
|
125
|
-
var onceWrapper = function onceWrapper() {
|
126
|
-
listener();
|
127
|
-
|
128
|
-
_this.off(event, onceWrapper);
|
129
|
-
};
|
130
|
-
|
131
|
-
_this.handlers[event].push(onceWrapper);
|
132
|
-
|
133
|
-
return _this;
|
134
|
-
};
|
135
|
-
|
136
|
-
this.removeListener = function (event, listener) {
|
137
|
-
if (_this.handlers[event]) {
|
138
|
-
_this.handlers[event] = _this.handlers[event].filter(function (handler) {
|
139
|
-
return handler !== listener;
|
140
|
-
});
|
141
|
-
}
|
142
|
-
|
143
|
-
return _this;
|
144
|
-
};
|
145
|
-
|
146
|
-
this.off = function (event, listener) {
|
147
|
-
return _this.removeListener(event, listener);
|
148
|
-
};
|
149
|
-
|
150
|
-
this.removeAllListeners = function (event) {
|
151
|
-
if (event) {
|
152
|
-
delete _this.handlers[event];
|
153
|
-
} else {
|
154
|
-
_this.handlers = {};
|
155
|
-
}
|
156
|
-
|
157
|
-
return _this;
|
158
|
-
};
|
159
|
-
|
160
|
-
this.setMaxListeners = function (n) {
|
161
|
-
_this.maxListeners = n;
|
162
|
-
return _this;
|
163
|
-
};
|
164
|
-
|
165
|
-
this.getMaxListeners = function () {
|
166
|
-
return _this.maxListeners;
|
167
|
-
};
|
168
|
-
|
169
|
-
this.listeners = function (event) {
|
170
|
-
if (_this.handlers[event]) {
|
171
|
-
return [].concat(_this.handlers[event]);
|
172
|
-
}
|
173
|
-
|
174
|
-
return [];
|
175
|
-
};
|
176
|
-
|
177
|
-
this.rawListeners = function (event) {
|
178
|
-
return [].concat(_this.handlers[event]);
|
179
|
-
};
|
180
|
-
|
181
|
-
this.listenerCount = function (event) {
|
182
|
-
if (_this.handlers[event]) {
|
183
|
-
return Object.keys(_this.handlers[event]).length;
|
184
|
-
}
|
185
|
-
|
186
|
-
return 0;
|
187
|
-
};
|
188
|
-
|
189
|
-
this.prependListener = function (event, listener) {
|
190
|
-
if (Object.keys(_this.handlers).length === _this.maxListeners) {
|
191
|
-
throw new Error('Max listeners reached');
|
192
|
-
}
|
193
|
-
|
194
|
-
_this.handlers[event] = _this.handlers[event] || [];
|
195
|
-
|
196
|
-
_this.handlers[event].unshift(listener);
|
197
|
-
|
198
|
-
return _this;
|
199
|
-
};
|
200
|
-
|
201
|
-
this.prependOnceListener = function (event, listener) {
|
202
|
-
if (Object.keys(_this.handlers).length === _this.maxListeners) {
|
203
|
-
throw new Error('Max listeners reached');
|
204
|
-
}
|
205
|
-
|
206
|
-
_this.handlers[event] = _this.handlers[event] || [];
|
207
|
-
|
208
|
-
var onceWrapper = function onceWrapper() {
|
209
|
-
listener();
|
210
|
-
|
211
|
-
_this.off(event, onceWrapper);
|
212
|
-
};
|
213
|
-
|
214
|
-
_this.handlers[event].unshift(onceWrapper);
|
215
|
-
|
216
|
-
return _this;
|
217
|
-
};
|
218
|
-
|
219
|
-
this.eventNames = function () {
|
220
|
-
return Object.keys(_this.handlers);
|
221
|
-
};
|
222
|
-
|
223
|
-
this.on = function (event, listener) {
|
224
|
-
return _this.addListener(event, listener);
|
225
|
-
};
|
226
|
-
|
227
|
-
this.emit = function (event) {
|
228
|
-
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
229
|
-
args[_key - 1] = arguments[_key];
|
230
|
-
}
|
231
|
-
|
232
|
-
var listeners = _this.listeners(event);
|
233
|
-
|
234
|
-
if (listeners.length > 0) {
|
235
|
-
listeners.forEach(function (listener) {
|
236
|
-
if (listener) listener(args);
|
237
|
-
});
|
238
|
-
return true;
|
239
|
-
}
|
240
|
-
|
241
|
-
return false;
|
242
|
-
};
|
243
|
-
};
|
244
|
-
|
245
|
-
var d = /*#__PURE__*/debug('teckos:client');
|
246
|
-
var DEFAULT_OPTIONS = {
|
247
|
-
reconnection: true,
|
248
|
-
reconnectionDelay: 1000,
|
249
|
-
reconnectionDelayMax: 5000,
|
250
|
-
reconnectionAttempts: Infinity,
|
251
|
-
randomizationFactor: 0.5,
|
252
|
-
timeout: 5000,
|
253
|
-
debug: false
|
254
|
-
};
|
255
|
-
|
256
|
-
var TeckosClient = /*#__PURE__*/function (_SocketEventEmitter) {
|
257
|
-
_inheritsLoose(TeckosClient, _SocketEventEmitter);
|
258
|
-
|
259
|
-
function TeckosClient(url, options) {
|
260
|
-
var _this;
|
261
|
-
|
262
|
-
_this = _SocketEventEmitter.call(this) || this;
|
263
|
-
_this.currentReconnectionAttempts = 0;
|
264
|
-
_this.acks = new Map();
|
265
|
-
_this.fnId = 0;
|
266
|
-
|
267
|
-
_this.attachHandler = function () {
|
268
|
-
if (_this.ws) {
|
269
|
-
_this.ws.onopen = _this.handleOpen;
|
270
|
-
_this.ws.onerror = _this.handleError;
|
271
|
-
_this.ws.onclose = _this.handleClose;
|
272
|
-
_this.ws.onmessage = _this.handleMessage;
|
273
|
-
}
|
274
|
-
};
|
275
|
-
|
276
|
-
_this.connect = function () {
|
277
|
-
if (_this.options.debug) d("Connecting to " + _this.url + "..."); // This will try to connect immediately
|
278
|
-
|
279
|
-
_this.ws = new WebSocket(_this.url); // Attach handlers
|
280
|
-
|
281
|
-
_this.attachHandler(); // Handle timeout
|
282
|
-
|
283
|
-
|
284
|
-
_this.connectionTimeout = setTimeout(function () {
|
285
|
-
if (_this.ws && _this.ws.readyState === WebSocket.CONNECTING) {
|
286
|
-
_this.ws.close();
|
287
|
-
}
|
288
|
-
}, _this.options.timeout);
|
289
|
-
};
|
290
|
-
|
291
|
-
_this.reconnect = function () {
|
292
|
-
_this.listeners('reconnect_attempt').forEach(function (listener) {
|
293
|
-
return listener();
|
294
|
-
});
|
295
|
-
|
296
|
-
_this.connect();
|
297
|
-
};
|
298
|
-
|
299
|
-
_this.emit = function (event) {
|
300
|
-
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
301
|
-
args[_key - 1] = arguments[_key];
|
302
|
-
}
|
303
|
-
|
304
|
-
args.unshift(event);
|
305
|
-
var packet = {
|
306
|
-
type: PacketType.EVENT,
|
307
|
-
data: args
|
308
|
-
};
|
309
|
-
|
310
|
-
if (typeof args[args.length - 1] === 'function') {
|
311
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
312
|
-
_this.acks.set(_this.fnId, args.pop());
|
313
|
-
|
314
|
-
packet.id = _this.fnId;
|
315
|
-
_this.fnId += 1;
|
316
|
-
}
|
317
|
-
|
318
|
-
return _this.sendPackage(packet);
|
319
|
-
};
|
320
|
-
|
321
|
-
_this.send = function () {
|
322
|
-
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
323
|
-
args[_key2] = arguments[_key2];
|
324
|
-
}
|
325
|
-
|
326
|
-
args.unshift('message');
|
327
|
-
return _this.sendPackage({
|
328
|
-
type: PacketType.EVENT,
|
329
|
-
data: args
|
330
|
-
});
|
331
|
-
};
|
332
|
-
|
333
|
-
_this.sendPackage = function (packet) {
|
334
|
-
if (_this.ws !== undefined && _this.ws.readyState === WebSocket.OPEN) {
|
335
|
-
var buffer = encodePacket(packet);
|
336
|
-
if (_this.options.debug) d("[" + _this.url + "] Send packet: " + JSON.stringify(packet));
|
337
|
-
|
338
|
-
_this.ws.send(buffer);
|
339
|
-
|
340
|
-
return true;
|
341
|
-
}
|
342
|
-
|
343
|
-
return false;
|
344
|
-
};
|
345
|
-
|
346
|
-
_this.handleMessage = function (msg) {
|
347
|
-
var packet = typeof msg.data === 'string' ? JSON.parse(msg.data) : decodePacket(msg.data);
|
348
|
-
if (_this.options.debug) d("[" + _this.url + "] Got packet: " + JSON.stringify(packet));
|
349
|
-
|
350
|
-
if (packet.type === PacketType.EVENT) {
|
351
|
-
var event = packet.data[0];
|
352
|
-
var args = packet.data.slice(1);
|
353
|
-
|
354
|
-
if (event) {
|
355
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
356
|
-
_this.listeners(event).forEach(function (listener) {
|
357
|
-
return listener.apply(void 0, args);
|
358
|
-
});
|
359
|
-
} else {
|
360
|
-
throw new Error("[teckos-client@" + _this.url + "] Got invalid event message: " + JSON.stringify(msg.data));
|
361
|
-
}
|
362
|
-
} else if (packet.type === PacketType.ACK && packet.id !== undefined) {
|
363
|
-
// Call assigned function
|
364
|
-
var ack = _this.acks.get(packet.id);
|
365
|
-
|
366
|
-
if (typeof ack === 'function') {
|
367
|
-
ack.apply(_assertThisInitialized(_this), packet.data);
|
368
|
-
|
369
|
-
_this.acks["delete"](packet.id);
|
370
|
-
}
|
371
|
-
} else {
|
372
|
-
throw new Error("[teckos-client@" + _this.url + "] Got invalid message type: " + packet.type);
|
373
|
-
}
|
374
|
-
};
|
375
|
-
|
376
|
-
_this.handleOpen = function () {
|
377
|
-
if (_this.currentReconnectionAttempts > 0) {
|
378
|
-
// Reset reconnection settings to default
|
379
|
-
_this.currentReconnectDelay = _this.options.reconnectionDelay;
|
380
|
-
_this.currentReconnectionAttempts = 0; // Inform listeners
|
381
|
-
|
382
|
-
if (_this.options.debug) d("[" + _this.url + "] Reconnected!"); // eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
383
|
-
|
384
|
-
_this.listeners('reconnect').forEach(function (listener) {
|
385
|
-
return listener();
|
386
|
-
});
|
387
|
-
} // Inform listeners
|
388
|
-
|
389
|
-
|
390
|
-
if (_this.options.debug) d("[" + _this.url + "] Connected!");
|
391
|
-
|
392
|
-
_this.listeners('connect').forEach(function (listener) {
|
393
|
-
return listener();
|
394
|
-
});
|
395
|
-
};
|
396
|
-
|
397
|
-
_this.handleError = function (error) {
|
398
|
-
if (_this.handlers && _this.handlers.error) {
|
399
|
-
if (_this.options.debug) d("[" + _this.url + "] Got error from server: " + JSON.stringify(error));
|
400
|
-
|
401
|
-
_this.handlers.error.forEach(function (listener) {
|
402
|
-
return listener(error);
|
403
|
-
});
|
404
|
-
}
|
405
|
-
};
|
406
|
-
|
407
|
-
_this.handleClose = function () {
|
408
|
-
// Stop connection timeout
|
409
|
-
if (_this.connectionTimeout) {
|
410
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
411
|
-
clearTimeout(_this.connectionTimeout);
|
412
|
-
} // Stop reconnection timeout
|
413
|
-
|
414
|
-
|
415
|
-
if (_this.reconnectionTimeout) {
|
416
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
417
|
-
clearTimeout(_this.reconnectionTimeout);
|
418
|
-
} // Inform listeners
|
419
|
-
|
420
|
-
|
421
|
-
if (_this.currentReconnectionAttempts > 0) {
|
422
|
-
if (_this.options.debug) d("[" + _this.url + "] Reconnect #" + _this.currentReconnectionAttempts + " failed!");
|
423
|
-
|
424
|
-
_this.listeners('reconnect_error').forEach(function (listener) {
|
425
|
-
if (listener) listener();
|
426
|
-
});
|
427
|
-
} else {
|
428
|
-
if (_this.options.debug) d("[" + _this.url + "] Disconnected!");
|
429
|
-
|
430
|
-
_this.listeners('disconnect').forEach(function (listener) {
|
431
|
-
if (listener) listener();
|
432
|
-
});
|
433
|
-
}
|
434
|
-
|
435
|
-
if (_this.options.reconnection) {
|
436
|
-
// Apply reconnection logic
|
437
|
-
_this.currentReconnectionAttempts += 1;
|
438
|
-
|
439
|
-
if (_this.options.reconnectionAttempts === Infinity || _this.currentReconnectionAttempts <= _this.options.reconnectionAttempts) {
|
440
|
-
var timeout = Math.min(_this.options.reconnectionDelayMax, _this.currentReconnectDelay); // Increase reconnection delay
|
441
|
-
|
442
|
-
_this.currentReconnectDelay = Math.round(_this.currentReconnectDelay + _this.currentReconnectDelay * _this.options.randomizationFactor);
|
443
|
-
if (_this.options.debug) d("[" + _this.url + "] Try reconnecting (" + _this.currentReconnectionAttempts + "/" + _this.options.reconnectionAttempts + ") in " + timeout + "ms to " + _this.url + "...");
|
444
|
-
_this.reconnectionTimeout = setTimeout(function () {
|
445
|
-
_this.reconnect();
|
446
|
-
}, timeout);
|
447
|
-
} else {
|
448
|
-
if (_this.options.debug) d("[" + _this.url + "] Reconnection maximum of " + _this.options.reconnectionAttempts + " reached");
|
449
|
-
|
450
|
-
_this.listeners('reconnect_failed').forEach(function (listener) {
|
451
|
-
return listener();
|
452
|
-
});
|
453
|
-
}
|
454
|
-
}
|
455
|
-
};
|
456
|
-
|
457
|
-
_this.close = function () {
|
458
|
-
if (_this.options.debug) d("[" + _this.url + "] Closing connection (client-side)");
|
459
|
-
|
460
|
-
if (_this.ws !== undefined) {
|
461
|
-
_this.ws.onclose = function () {};
|
462
|
-
|
463
|
-
_this.ws.close();
|
464
|
-
|
465
|
-
_this.listeners('disconnect').forEach(function (listener) {
|
466
|
-
return listener();
|
467
|
-
});
|
468
|
-
}
|
469
|
-
};
|
470
|
-
|
471
|
-
_this.disconnect = function () {
|
472
|
-
_this.close();
|
473
|
-
};
|
474
|
-
|
475
|
-
_this.options = _extends({}, DEFAULT_OPTIONS, options);
|
476
|
-
_this.currentReconnectDelay = _this.options.reconnectionDelay;
|
477
|
-
_this.url = url;
|
478
|
-
return _this;
|
479
|
-
}
|
480
|
-
|
481
|
-
var _proto = TeckosClient.prototype;
|
482
|
-
|
483
|
-
_proto.getConnectionState = function getConnectionState() {
|
484
|
-
if (this.ws) {
|
485
|
-
switch (this.ws.readyState) {
|
486
|
-
case WebSocket.OPEN:
|
487
|
-
return ConnectionState.CONNECTED;
|
488
|
-
|
489
|
-
case WebSocket.CONNECTING:
|
490
|
-
return ConnectionState.CONNECTING;
|
491
|
-
|
492
|
-
case WebSocket.CLOSING:
|
493
|
-
return ConnectionState.DISCONNECTING;
|
494
|
-
|
495
|
-
default:
|
496
|
-
return ConnectionState.DISCONNECTED;
|
497
|
-
}
|
498
|
-
}
|
499
|
-
|
500
|
-
return ConnectionState.DISCONNECTED;
|
501
|
-
};
|
502
|
-
|
503
|
-
_createClass(TeckosClient, [{
|
504
|
-
key: "webSocket",
|
505
|
-
get: function get() {
|
506
|
-
return this.ws;
|
507
|
-
}
|
508
|
-
}, {
|
509
|
-
key: "state",
|
510
|
-
get: function get() {
|
511
|
-
return this.getConnectionState();
|
512
|
-
}
|
513
|
-
}, {
|
514
|
-
key: "connected",
|
515
|
-
get: function get() {
|
516
|
-
return this.getConnectionState() === ConnectionState.CONNECTED;
|
517
|
-
}
|
518
|
-
}, {
|
519
|
-
key: "disconnected",
|
520
|
-
get: function get() {
|
521
|
-
return this.getConnectionState() === ConnectionState.DISCONNECTED;
|
522
|
-
}
|
523
|
-
}]);
|
524
|
-
|
525
|
-
return TeckosClient;
|
526
|
-
}(SocketEventEmitter);
|
527
|
-
|
528
|
-
var d$1 = /*#__PURE__*/debug('teckos:client');
|
529
|
-
|
530
|
-
var TeckosClientWithJWT = /*#__PURE__*/function (_TeckosClient) {
|
531
|
-
_inheritsLoose(TeckosClientWithJWT, _TeckosClient);
|
532
|
-
|
533
|
-
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
534
|
-
function TeckosClientWithJWT(url, options, token, initialData) {
|
535
|
-
var _this;
|
536
|
-
|
537
|
-
_this = _TeckosClient.call(this, url, options) || this;
|
538
|
-
_this.receivedReady = false;
|
539
|
-
|
540
|
-
_this.handleReadyEvent = function () {
|
541
|
-
if (_this.options.debug) d$1("[" + _this.url + "] Connected!");
|
542
|
-
_this.receivedReady = true;
|
543
|
-
|
544
|
-
if (_this.currentReconnectionAttempts > 0) {
|
545
|
-
if (_this.options.debug) d$1("[" + _this.url + "] Reconnected!");
|
546
|
-
|
547
|
-
_this.listeners('reconnect').forEach(function (listener) {
|
548
|
-
return listener();
|
549
|
-
}); // Reset reconnection settings to default
|
550
|
-
|
551
|
-
|
552
|
-
_this.currentReconnectDelay = _this.options.reconnectionDelay;
|
553
|
-
_this.currentReconnectionAttempts = 0;
|
554
|
-
}
|
555
|
-
|
556
|
-
_this.listeners('connect').forEach(function (listener) {
|
557
|
-
return listener();
|
558
|
-
});
|
559
|
-
};
|
560
|
-
|
561
|
-
_this.handleOpen = function () {
|
562
|
-
_this.receivedReady = false;
|
563
|
-
|
564
|
-
_this.once('ready', _this.handleReadyEvent);
|
565
|
-
|
566
|
-
if (_this.options.debug) d$1('Connection opened, sending token now');
|
567
|
-
|
568
|
-
_this.emit('token', _extends({
|
569
|
-
token: _this.token
|
570
|
-
}, _this.initialData));
|
571
|
-
};
|
572
|
-
|
573
|
-
_this.token = token; // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
574
|
-
|
575
|
-
_this.initialData = initialData;
|
576
|
-
|
577
|
-
_this.on('disconnect', function () {
|
578
|
-
_this.receivedReady = false;
|
579
|
-
});
|
580
|
-
|
581
|
-
return _this;
|
582
|
-
}
|
583
|
-
|
584
|
-
var _proto = TeckosClientWithJWT.prototype;
|
585
|
-
|
586
|
-
_proto.getConnectionState = function getConnectionState() {
|
587
|
-
if (this.ws) {
|
588
|
-
switch (this.ws.readyState) {
|
589
|
-
case WebSocket.OPEN:
|
590
|
-
if (this.receivedReady) {
|
591
|
-
return ConnectionState.CONNECTED;
|
592
|
-
}
|
593
|
-
|
594
|
-
return ConnectionState.CONNECTING;
|
595
|
-
|
596
|
-
case WebSocket.CONNECTING:
|
597
|
-
return ConnectionState.CONNECTING;
|
598
|
-
|
599
|
-
case WebSocket.CLOSING:
|
600
|
-
return ConnectionState.DISCONNECTING;
|
601
|
-
|
602
|
-
default:
|
603
|
-
return ConnectionState.DISCONNECTED;
|
604
|
-
}
|
605
|
-
}
|
606
|
-
|
607
|
-
return ConnectionState.DISCONNECTED;
|
608
|
-
};
|
609
|
-
|
610
|
-
return TeckosClientWithJWT;
|
611
|
-
}(TeckosClient);
|
612
|
-
|
613
|
-
export { ConnectionState, PacketType, TeckosClient, TeckosClientWithJWT };
|
614
|
-
//# sourceMappingURL=teckos-client.esm.js.map
|