Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

reconnecting-websocket

Package Overview
Dependencies
Maintainers
1
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

reconnecting-websocket - npm Package Compare versions

Comparing version 4.0.0-rc4 to 4.0.0-rc5

8

dist/events.d.ts

@@ -17,2 +17,8 @@ export declare class Event {

}
export declare type EventListener = (event: Event) => void;
export interface WebSocketEventMap {
close: CloseEvent;
error: ErrorEvent;
message: MessageEvent;
open: Event;
}
export declare type EventListener = (event: Event | CloseEvent | MessageEvent) => void;

12

dist/reconnecting-websocket-amd.js

@@ -52,3 +52,8 @@ define(function () { 'use strict';

constructor(url, protocols, options = {}) {
this._listeners = {};
this._listeners = {
error: [],
message: [],
open: [],
close: [],
};
this._retryCount = -1;

@@ -84,5 +89,2 @@ this._shouldReconnect = true;

this._options = options;
for (const [type] of this.eventToHandler) {
this._listeners[type] = [];
}
this._connect();

@@ -203,2 +205,3 @@ }

if (this._listeners[type]) {
// @ts-ignore
this._listeners[type].push(listener);

@@ -212,2 +215,3 @@ }

if (this._listeners[type]) {
// @ts-ignore
this._listeners[type] = this._listeners[type].filter(l => l !== listener);

@@ -214,0 +218,0 @@ }

@@ -52,3 +52,8 @@ 'use strict';

constructor(url, protocols, options = {}) {
this._listeners = {};
this._listeners = {
error: [],
message: [],
open: [],
close: [],
};
this._retryCount = -1;

@@ -84,5 +89,2 @@ this._shouldReconnect = true;

this._options = options;
for (const [type] of this.eventToHandler) {
this._listeners[type] = [];
}
this._connect();

@@ -203,2 +205,3 @@ }

if (this._listeners[type]) {
// @ts-ignore
this._listeners[type].push(listener);

@@ -212,2 +215,3 @@ }

if (this._listeners[type]) {
// @ts-ignore
this._listeners[type] = this._listeners[type].filter(l => l !== listener);

@@ -214,0 +218,0 @@ }

@@ -53,3 +53,8 @@ var ReconnectingWebSocket = (function () {

constructor(url, protocols, options = {}) {
this._listeners = {};
this._listeners = {
error: [],
message: [],
open: [],
close: [],
};
this._retryCount = -1;

@@ -85,5 +90,2 @@ this._shouldReconnect = true;

this._options = options;
for (const [type] of this.eventToHandler) {
this._listeners[type] = [];
}
this._connect();

@@ -204,2 +206,3 @@ }

if (this._listeners[type]) {
// @ts-ignore
this._listeners[type].push(listener);

@@ -213,2 +216,3 @@ }

if (this._listeners[type]) {
// @ts-ignore
this._listeners[type] = this._listeners[type].filter(l => l !== listener);

@@ -215,0 +219,0 @@ }

@@ -1,1 +0,1 @@

var ReconnectingWebSocket=function(){"use strict";class e{constructor(e,t){this.target=t,this.type=e}}class t extends e{constructor(e,t){super("error",t),this.message=e.message,this.error=e}}class s extends e{constructor(e=1e3,t="",s){super("close",s),this.wasClean=!0,this.code=e,this.reason=t}}const i=()=>{if("undefined"!=typeof WebSocket)return WebSocket},n=e=>"function"==typeof e&&2===e.CLOSING,o={maxReconnectionDelay:1e4,minReconnectionDelay:1e3+4e3*Math.random(),minUptime:5e3,reconnectionDelayGrowFactor:1.3,connectionTimeout:4e3,maxRetries:1/0,debug:!1};class r{constructor(e,t,s={}){this._listeners={},this._retryCount=-1,this._shouldReconnect=!0,this._connectLock=!1,this._binaryType="blob",this.eventToHandler=new Map([["open",this._handleOpen.bind(this)],["close",this._handleClose.bind(this)],["error",this._handleError.bind(this)],["message",this._handleMessage.bind(this)]]),this.onclose=void 0,this.onerror=void 0,this.onmessage=void 0,this.onopen=void 0,this._url=e,this._protocols=t,this._options=s;for(const[e]of this.eventToHandler)this._listeners[e]=[];this._connect()}static get CONNECTING(){return 0}static get OPEN(){return 1}static get CLOSING(){return 2}static get CLOSED(){return 3}get CONNECTING(){return r.CONNECTING}get OPEN(){return r.OPEN}get CLOSING(){return r.CLOSING}get CLOSED(){return r.CLOSED}get binaryType(){return this._ws?this._ws.binaryType:this._binaryType}set binaryType(e){this._binaryType=e,this._ws&&(this._ws.binaryType=e)}get retryCount(){return Math.max(this._retryCount,0)}get bufferedAmount(){return this._ws?this._ws.bufferedAmount:0}get extensions(){return this._ws?this._ws.extensions:""}get protocol(){return this._ws?this._ws.protocol:""}get readyState(){return this._ws?this._ws.readyState:r.CONNECTING}get url(){return this._ws?this._ws.url:""}close(e,t){this._shouldReconnect=!1,this._ws&&this._ws.readyState!==this.CLOSED&&this._ws.close(e,t)}reconnect(e,t){this._shouldReconnect=!0,this._retryCount=-1,this._ws&&this._ws.readyState!==this.CLOSED||this._connect(),this._disconnect(e,t),this._connect()}send(e){this._ws&&this._ws.send(e)}addEventListener(e,t){this._listeners[e]&&this._listeners[e].push(t)}removeEventListener(e,t){this._listeners[e]&&(this._listeners[e]=this._listeners[e].filter(e=>e!==t))}_debug(...e){this._options.debug&&console.log("RWS>",...e)}_getNextDelay(){let e=0;if(this._retryCount>0){const{reconnectionDelayGrowFactor:t=o.reconnectionDelayGrowFactor,minReconnectionDelay:s=o.minReconnectionDelay,maxReconnectionDelay:i=o.maxReconnectionDelay}=this._options;(e=s+Math.pow(this._retryCount-1,t))>i&&(e=i)}return this._debug("next delay",e),e}_wait(){return new Promise(e=>{setTimeout(e,this._getNextDelay())})}_getNextUrl(e){if("string"==typeof e)return Promise.resolve(e);if("function"==typeof e){const t=e();if("string"==typeof t)return Promise.resolve(t);if(t.then)return t}throw Error("Invalid URL")}_connect(){if(this._connectLock)return;this._connectLock=!0;const{maxRetries:e=o.maxRetries,connectionTimeout:t=o.connectionTimeout,WebSocket:s=i()}=this._options;if(this._retryCount>=e)this._debug("max retries reached",this._retryCount,">=",e);else{if(this._retryCount++,this._debug("connect",this._retryCount),this._removeListeners(),!n(s))throw Error("No valid WebSocket class provided");this._wait().then(()=>this._getNextUrl(this._url)).then(e=>{this._debug("connect",{url:e,protocols:this._protocols}),this._ws=new s(e,this._protocols),this._ws.binaryType=this._binaryType,this._connectLock=!1,this._addListeners(),this._connectTimeout=setTimeout(()=>this._handleTimeout(),t)})}}_handleTimeout(){this._debug("timeout event"),this._handleError(new t(Error("TIMEOUT"),this))}_disconnect(e,t){if(clearTimeout(this._connectTimeout),this._ws){this._removeListeners();try{this._ws.close(e,t),this._handleClose(new s(e,t,this))}catch(e){}}}_acceptOpen(){this._retryCount=0}_handleOpen(e){this._debug("open event");const{minUptime:t=o.minUptime}=this._options;clearTimeout(this._connectTimeout),this._uptimeTimeout=setTimeout(()=>this._acceptOpen(),t),this._debug("assign binary type"),this._ws.binaryType=this._binaryType,this.onopen&&this.onopen(e),this._listeners.open.forEach(t=>t(e))}_handleMessage(e){this._debug("message event"),this.onmessage&&this.onmessage(e),this._listeners.message.forEach(t=>t(e))}_handleError(e){this._debug("error event",e.message),this._disconnect(void 0,"TIMEOUT"===e.message?"timeout":void 0),this.onerror&&this.onerror(e),this._debug("exec error listeners"),this._listeners.error.forEach(t=>t(e)),this._connect()}_handleClose(e){this._debug("close event"),this.onclose&&this.onclose(e),this._listeners.close.forEach(t=>t(e))}_removeListeners(){if(this._ws){this._debug("removeListeners");for(const[e,t]of this.eventToHandler)this._ws.removeEventListener(e,t)}}_addListeners(){this._debug("addListeners");for(const[e,t]of this.eventToHandler)this._ws.addEventListener(e,t)}}return r}();
var ReconnectingWebSocket=function(){"use strict";class e{constructor(e,t){this.target=t,this.type=e}}class t extends e{constructor(e,t){super("error",t),this.message=e.message,this.error=e}}class s extends e{constructor(e=1e3,t="",s){super("close",s),this.wasClean=!0,this.code=e,this.reason=t}}const i=()=>{if("undefined"!=typeof WebSocket)return WebSocket},n=e=>"function"==typeof e&&2===e.CLOSING,o={maxReconnectionDelay:1e4,minReconnectionDelay:1e3+4e3*Math.random(),minUptime:5e3,reconnectionDelayGrowFactor:1.3,connectionTimeout:4e3,maxRetries:1/0,debug:!1};class r{constructor(e,t,s={}){this._listeners={error:[],message:[],open:[],close:[]},this._retryCount=-1,this._shouldReconnect=!0,this._connectLock=!1,this._binaryType="blob",this.eventToHandler=new Map([["open",this._handleOpen.bind(this)],["close",this._handleClose.bind(this)],["error",this._handleError.bind(this)],["message",this._handleMessage.bind(this)]]),this.onclose=void 0,this.onerror=void 0,this.onmessage=void 0,this.onopen=void 0,this._url=e,this._protocols=t,this._options=s,this._connect()}static get CONNECTING(){return 0}static get OPEN(){return 1}static get CLOSING(){return 2}static get CLOSED(){return 3}get CONNECTING(){return r.CONNECTING}get OPEN(){return r.OPEN}get CLOSING(){return r.CLOSING}get CLOSED(){return r.CLOSED}get binaryType(){return this._ws?this._ws.binaryType:this._binaryType}set binaryType(e){this._binaryType=e,this._ws&&(this._ws.binaryType=e)}get retryCount(){return Math.max(this._retryCount,0)}get bufferedAmount(){return this._ws?this._ws.bufferedAmount:0}get extensions(){return this._ws?this._ws.extensions:""}get protocol(){return this._ws?this._ws.protocol:""}get readyState(){return this._ws?this._ws.readyState:r.CONNECTING}get url(){return this._ws?this._ws.url:""}close(e,t){this._shouldReconnect=!1,this._ws&&this._ws.readyState!==this.CLOSED&&this._ws.close(e,t)}reconnect(e,t){this._shouldReconnect=!0,this._retryCount=-1,this._ws&&this._ws.readyState!==this.CLOSED||this._connect(),this._disconnect(e,t),this._connect()}send(e){this._ws&&this._ws.send(e)}addEventListener(e,t){this._listeners[e]&&this._listeners[e].push(t)}removeEventListener(e,t){this._listeners[e]&&(this._listeners[e]=this._listeners[e].filter(e=>e!==t))}_debug(...e){this._options.debug&&console.log("RWS>",...e)}_getNextDelay(){let e=0;if(this._retryCount>0){const{reconnectionDelayGrowFactor:t=o.reconnectionDelayGrowFactor,minReconnectionDelay:s=o.minReconnectionDelay,maxReconnectionDelay:i=o.maxReconnectionDelay}=this._options;(e=s+Math.pow(this._retryCount-1,t))>i&&(e=i)}return this._debug("next delay",e),e}_wait(){return new Promise(e=>{setTimeout(e,this._getNextDelay())})}_getNextUrl(e){if("string"==typeof e)return Promise.resolve(e);if("function"==typeof e){const t=e();if("string"==typeof t)return Promise.resolve(t);if(t.then)return t}throw Error("Invalid URL")}_connect(){if(this._connectLock)return;this._connectLock=!0;const{maxRetries:e=o.maxRetries,connectionTimeout:t=o.connectionTimeout,WebSocket:s=i()}=this._options;if(this._retryCount>=e)this._debug("max retries reached",this._retryCount,">=",e);else{if(this._retryCount++,this._debug("connect",this._retryCount),this._removeListeners(),!n(s))throw Error("No valid WebSocket class provided");this._wait().then(()=>this._getNextUrl(this._url)).then(e=>{this._debug("connect",{url:e,protocols:this._protocols}),this._ws=new s(e,this._protocols),this._ws.binaryType=this._binaryType,this._connectLock=!1,this._addListeners(),this._connectTimeout=setTimeout(()=>this._handleTimeout(),t)})}}_handleTimeout(){this._debug("timeout event"),this._handleError(new t(Error("TIMEOUT"),this))}_disconnect(e,t){if(clearTimeout(this._connectTimeout),this._ws){this._removeListeners();try{this._ws.close(e,t),this._handleClose(new s(e,t,this))}catch(e){}}}_acceptOpen(){this._retryCount=0}_handleOpen(e){this._debug("open event");const{minUptime:t=o.minUptime}=this._options;clearTimeout(this._connectTimeout),this._uptimeTimeout=setTimeout(()=>this._acceptOpen(),t),this._debug("assign binary type"),this._ws.binaryType=this._binaryType,this.onopen&&this.onopen(e),this._listeners.open.forEach(t=>t(e))}_handleMessage(e){this._debug("message event"),this.onmessage&&this.onmessage(e),this._listeners.message.forEach(t=>t(e))}_handleError(e){this._debug("error event",e.message),this._disconnect(void 0,"TIMEOUT"===e.message?"timeout":void 0),this.onerror&&this.onerror(e),this._debug("exec error listeners"),this._listeners.error.forEach(t=>t(e)),this._connect()}_handleClose(e){this._debug("close event"),this.onclose&&this.onclose(e),this._listeners.close.forEach(t=>t(e))}_removeListeners(){if(this._ws){this._debug("removeListeners");for(const[e,t]of this.eventToHandler)this._ws.removeEventListener(e,t)}}_addListeners(){this._debug("addListeners");for(const[e,t]of this.eventToHandler)this._ws.addEventListener(e,t)}}return r}();

@@ -1,2 +0,2 @@

import { CloseEvent, Event, EventListener } from './events';
import { CloseEvent, ErrorEvent, Event, WebSocketEventMap } from './events';
export declare type Options = {

@@ -13,2 +13,8 @@ WebSocket?: any;

export declare type UrlProvider = string | (() => string) | (() => Promise<string>);
export declare type ListenersMap = {
error: Array<((event: ErrorEvent) => void)>;
message: Array<((event: MessageEvent) => void)>;
open: Array<((event: Event) => void)>;
close: Array<((event: CloseEvent) => void)>;
};
export default class ReconnectingWebSocket {

@@ -101,7 +107,7 @@ private _ws?;

*/
addEventListener(type: keyof WebSocketEventMap, listener: EventListener): void;
addEventListener<K extends keyof WebSocketEventMap>(type: K, listener: ((event: WebSocketEventMap[K]) => void)): void;
/**
* Removes an event listener
*/
removeEventListener(type: keyof WebSocketEventMap, listener: EventListener): void;
removeEventListener<K extends keyof WebSocketEventMap>(type: K, listener: ((event: WebSocketEventMap[K]) => void)): void;
private _debug(...params);

@@ -108,0 +114,0 @@ private _getNextDelay();

@@ -30,3 +30,9 @@ export class Event {

}
export interface WebSocketEventMap {
close: CloseEvent;
error: ErrorEvent;
message: MessageEvent;
open: Event;
}
export type EventListener = (event: Event) => void;
export type EventListener = (event: Event | CloseEvent | MessageEvent) => void;
{
"name": "reconnecting-websocket",
"version": "4.0.0-rc4",
"description": "Reconnecting WebSocket",
"main": "./dist/reconnecting-websocket-cjs.js",
"module": "./dist/reconnecting-websocket.mjs",
"types": "./dist/reconnecting-websocket.d.ts",
"scripts": {
"build": "npm run clean && rollup -c && uglifyjs --compress --mangle -o dist/reconnecting-websocket-iife.min.js dist/reconnecting-websocket-iife.js",
"test": "nyc --reporter=text-summary --reporter=lcov ava --verbose --serial test/test.js",
"clean": "del dist && del coverage && del .nyc_output",
"report": "nyc report --reporter=html && opn coverage/index.html",
"coveralls": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls",
"lint": "tslint *.ts",
"precommit": "lint-staged",
"prepublishOnly": "npm run clean && npm run lint && npm run build && npm test"
},
"keywords": [
"websocket",
"client",
"reconnecting",
"reconnection",
"reconnect",
"forever",
"persistent",
"forever",
"automatic"
],
"author": "Pedro Ladaria <pedro.ladaria@gmail.com>",
"license": "MIT",
"devDependencies": {
"ava": "^0.25.0",
"coveralls": "^3.0.0",
"del-cli": "^1.1.0",
"husky": "^0.14.3",
"lint-staged": "^7.1.0",
"nyc": "^11.8.0",
"opn-cli": "^3.1.0",
"prettier": "^1.12.1",
"rollup": "^0.59.1",
"rollup-plugin-typescript2": "^0.14.0",
"tslint": "^5.10.0",
"typescript": "^2.8.3",
"uglify-es": "^3.3.9",
"ws": "^5.1.1"
},
"dependencies": {},
"directories": {
"test": "test"
},
"repository": {
"type": "git",
"url": "git+https://github.com/pladaria/reconnecting-websocket.git"
},
"bugs": {
"url": "https://github.com/pladaria/reconnecting-websocket/issues"
},
"publishConfig": {
"registry": "https://registry.npmjs.org"
},
"homepage": "https://github.com/pladaria/reconnecting-websocket#readme",
"lint-staged": {
"linters": {
"*.{js,md,ts}, !dist/*": [
"prettier --write",
"git add"
]
"name": "reconnecting-websocket",
"version": "4.0.0-rc5",
"description": "Reconnecting WebSocket",
"main": "./dist/reconnecting-websocket-cjs.js",
"module": "./dist/reconnecting-websocket.mjs",
"types": "./dist/reconnecting-websocket.d.ts",
"scripts": {
"build":
"npm run clean && rollup -c && uglifyjs --compress --mangle -o dist/reconnecting-websocket-iife.min.js dist/reconnecting-websocket-iife.js",
"test": "nyc --reporter=text-summary --reporter=lcov ava --verbose --serial test/test.js",
"clean": "del dist && del coverage && del .nyc_output",
"report": "nyc report --reporter=html && opn coverage/index.html",
"coveralls": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls",
"lint": "tslint *.ts",
"precommit": "lint-staged",
"prepublishOnly": "npm run clean && npm run lint && npm run build && npm test"
},
"keywords": [
"websocket",
"client",
"reconnecting",
"reconnection",
"reconnect",
"forever",
"persistent",
"forever",
"automatic"
],
"author": "Pedro Ladaria <pedro.ladaria@gmail.com>",
"license": "MIT",
"devDependencies": {
"ava": "^0.25.0",
"coveralls": "^3.0.0",
"del-cli": "^1.1.0",
"husky": "^0.14.3",
"lint-staged": "^7.1.0",
"nyc": "^11.8.0",
"opn-cli": "^3.1.0",
"prettier": "^1.12.1",
"rollup": "^0.59.1",
"rollup-plugin-typescript2": "^0.14.0",
"tslint": "^5.10.0",
"typescript": "^2.8.3",
"uglify-es": "^3.3.9",
"ws": "^5.1.1"
},
"dependencies": {},
"directories": {
"test": "test"
},
"repository": {
"type": "git",
"url": "git+https://github.com/pladaria/reconnecting-websocket.git"
},
"bugs": {
"url": "https://github.com/pladaria/reconnecting-websocket/issues"
},
"publishConfig": {
"registry": "https://registry.npmjs.org"
},
"homepage": "https://github.com/pladaria/reconnecting-websocket#readme",
"lint-staged": {
"linters": {
"*.{js,md,ts}, !dist/*": ["prettier --write", "git add"]
}
}
}
}

@@ -7,3 +7,3 @@ /*!

*/
import {CloseEvent, ErrorEvent, Event, EventListener} from './events';
import {CloseEvent, ErrorEvent, Event, EventListener, WebSocketEventMap} from './events';

@@ -45,5 +45,16 @@ const getGlobalWebSocket = (): WebSocket | undefined => {

export type ListenersMap = {
error: Array<((event: ErrorEvent) => void)>;
message: Array<((event: MessageEvent) => void)>;
open: Array<((event: Event) => void)>;
close: Array<((event: CloseEvent) => void)>;
};
export default class ReconnectingWebSocket {
private _ws?: WebSocket;
private _listeners: {[type: string]: EventListener[]} = {};
private _listeners: ListenersMap = {
error: [],
message: [],
open: [],
close: [],
};
private _retryCount = -1;

@@ -71,5 +82,2 @@ private _uptimeTimeout: any;

this._options = options;
for (const [type] of this.eventToHandler) {
this._listeners[type] = [];
}
this._connect();

@@ -223,4 +231,8 @@ }

*/
public addEventListener(type: keyof WebSocketEventMap, listener: EventListener) {
public addEventListener<K extends keyof WebSocketEventMap>(
type: K,
listener: ((event: WebSocketEventMap[K]) => void),
): void {
if (this._listeners[type]) {
// @ts-ignore
this._listeners[type].push(listener);

@@ -233,4 +245,8 @@ }

*/
public removeEventListener(type: keyof WebSocketEventMap, listener: EventListener) {
public removeEventListener<K extends keyof WebSocketEventMap>(
type: K,
listener: ((event: WebSocketEventMap[K]) => void),
): void {
if (this._listeners[type]) {
// @ts-ignore
this._listeners[type] = this._listeners[type].filter(l => l !== listener);

@@ -237,0 +253,0 @@ }

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc