Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: pladaria/reconnecting-websocket
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v4.3.0
Choose a base ref
...
head repository: pladaria/reconnecting-websocket
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
  • 15 commits
  • 16 files changed
  • 3 contributors

Commits on Feb 6, 2020

  1. Fix compatibility with WebSocket

    Łukasz Fudro committed Feb 6, 2020
    Copy the full SHA
    d7c312f View commit details

Commits on Feb 7, 2020

  1. Merge pull request #123 from Nodeigi/fix_compatibility_with_websocket

    Fix compatibility with WebSocket
    pladaria authored Feb 7, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    d264bad View commit details
  2. Update README.md

    pladaria authored Feb 7, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    164120e View commit details
  3. Update README.md

    pladaria authored Feb 7, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    85b945f View commit details
  4. start using jest

    pladaria committed Feb 7, 2020
    Copy the full SHA
    df624e3 View commit details
  5. wip: migrate to jest

    pladaria committed Feb 7, 2020
    Copy the full SHA
    e606d8c View commit details

Commits on Feb 8, 2020

  1. migrate tests to jest

    pladaria committed Feb 8, 2020
    Copy the full SHA
    9c1769d View commit details
  2. eslint setup for typescript

    pladaria committed Feb 8, 2020
    Copy the full SHA
    1258af0 View commit details
  3. Copy the full SHA
    da55cdf View commit details
  4. Copy the full SHA
    7d8e0bb View commit details
  5. update package.json

    pladaria committed Feb 8, 2020
    Copy the full SHA
    8a548ea View commit details
  6. Merge pull request #124 from pladaria/dev

    Migrate test from ava to jest
    pladaria authored Feb 8, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    748f109 View commit details
  7. Update README.md

    pladaria authored Feb 8, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    b7f7bfc View commit details
  8. Update CONTRIBUTING.md

    pladaria authored Feb 8, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    25b2386 View commit details
  9. update style

    pladaria committed Feb 8, 2020
    Copy the full SHA
    05a2f7c View commit details
Showing with 5,736 additions and 3,857 deletions.
  1. +1 −0 .eslintignore
  2. +26 −0 .eslintrc.yml
  3. +0 −15 .npmignore
  4. +0 −9 CHANGELOG.md
  5. +1 −1 CONTRIBUTING.md
  6. +2 −2 README.md
  7. +263 −279 test/test.js → __tests__/test.ts
  8. +1 −0 {test → __tests__}/unresponsive-server.js
  9. +1 −1 events.ts
  10. +9 −0 jest.config.js
  11. +5,384 −3,495 package-lock.json
  12. +21 −9 package.json
  13. +23 −15 reconnecting-websocket.ts
  14. +0 −8 test/jsconfig.json
  15. +4 −6 tsconfig.json
  16. +0 −17 tslint.json
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
26 changes: 26 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
env:
browser: true
es6: true
node: true
jest: true
extends:
- 'plugin:@typescript-eslint/eslint-recommended'
- 'plugin:@typescript-eslint/recommended'
globals:
Atomics: readonly
SharedArrayBuffer: readonly
parser: '@typescript-eslint/parser'
parserOptions:
ecmaVersion: 2018
sourceType: module
project: './tsconfig.json'
plugins:
- '@typescript-eslint'
- 'jest'
rules:
'@typescript-eslint/no-unused-vars-experimental': error
'@typescript-eslint/ban-ts-ignore': off
'@typescript-eslint/no-empty-function': off
'@typescript-eslint/explicit-function-return-type': off
'@typescript-eslint/no-explicit-any': off
'@typescript-eslint/no-non-null-assertion': off
15 changes: 0 additions & 15 deletions .npmignore

This file was deleted.

9 changes: 0 additions & 9 deletions CHANGELOG.md

This file was deleted.

2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ Hello, thank you for your interest in contributing to this repository
# Guidelines

* Use `npm` as package manager
* Test your feature/bugfix and keep full coverage. You can execute `npm test && npm run report` to
* Test your feature/bugfix and keep full coverage. You can execute `npm test` to
see the test coverage report
* Update `README.md` if needed
* Do not skip pre-commit hook
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Reconnecting WebSocket

[![Build Status](https://travis-ci.org/pladaria/reconnecting-websocket.svg?branch=master)](https://travis-ci.org/pladaria/reconnecting-websocket)
[![Coverage Status](https://coveralls.io/repos/github/pladaria/reconnecting-websocket/badge.svg?branch=master&v=1)](https://coveralls.io/github/pladaria/reconnecting-websocket?branch=master)
[![Build Status](https://travis-ci.org/pladaria/reconnecting-websocket.svg?branch=master&v=1)](https://travis-ci.org/pladaria/reconnecting-websocket)
[![Coverage Status](https://coveralls.io/repos/github/pladaria/reconnecting-websocket/badge.svg?branch=master&v=3)](https://coveralls.io/github/pladaria/reconnecting-websocket?branch=master)

WebSocket that will automatically reconnect if the connection is closed.

542 changes: 263 additions & 279 deletions test/test.js → __tests__/test.ts

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const net = require('net');

const port = process.argv[2] || 50001;
2 changes: 1 addition & 1 deletion events.ts
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ export class CloseEvent extends Event {
public code: number;
public reason: string;
public wasClean = true;
constructor(code: number = 1000, reason: string = '', target: any) {
constructor(code = 1000, reason = '', target: any) {
super('close', target);
this.code = code;
this.reason = reason;
9 changes: 9 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
roots: ['<rootDir>'],
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
testEnvironment: 'node',
};
8,879 changes: 5,384 additions & 3,495 deletions package-lock.json

Large diffs are not rendered by default.

30 changes: 21 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
{
"name": "reconnecting-websocket",
"version": "4.3.0",
"version": "4.4.0",
"description": "Reconnecting WebSocket",
"main": "./dist/reconnecting-websocket-cjs.js",
"module": "./dist/reconnecting-websocket-mjs.js",
"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",
"test": "jest --coverage",
"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",
"lint": "DEBUG=eslint:file-enumerator eslint __tests__/*.* ./*.ts",
"prepublishOnly": "npm run clean && npm run lint && npm run build && npm test"
},
"keywords": [
@@ -27,20 +26,28 @@
"automatic"
],
"author": "Pedro Ladaria <pedro.ladaria@gmail.com>",
"files": [
"dist/*"
],
"license": "MIT",
"devDependencies": {
"ava": "^2.3.0",
"coveralls": "^3.0.6",
"@types/jest": "^25.1.2",
"@typescript-eslint/eslint-plugin": "^2.19.0",
"@typescript-eslint/parser": "^2.19.0",
"coveralls": "^3.0.9",
"del-cli": "^1.1.0",
"eslint": "^6.8.0",
"eslint-plugin-jest": "^23.7.0",
"husky": "^1.3.1",
"jest": "^25.1.0",
"lint-staged": "^7.3.0",
"nyc": "^14.1.1",
"opn-cli": "^3.1.0",
"prettier": "^1.18.2",
"prettier": "^1.19.1",
"rollup": "^0.66.6",
"rollup-plugin-typescript2": "^0.23.0",
"ts-jest": "^25.2.0",
"tslib": "^1.10.0",
"tslint": "^5.19.0",
"tslint": "^5.20.1",
"typescript": "^3.7.5",
"uglify-es": "^3.3.10",
"ws": "^6.2.1"
@@ -67,5 +74,10 @@
"git add"
]
}
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
}
}
38 changes: 23 additions & 15 deletions reconnecting-websocket.ts
Original file line number Diff line number Diff line change
@@ -72,7 +72,7 @@ export default class ReconnectingWebSocket {
private _connectTimeout: any;
private _shouldReconnect = true;
private _connectLock = false;
private _binaryType = 'blob';
private _binaryType: BinaryType = 'blob';
private _closeCalled = false;
private _messageQueue: Message[] = [];

@@ -116,14 +116,13 @@ export default class ReconnectingWebSocket {
return ReconnectingWebSocket.CLOSED;
}

get binaryType(): string {
get binaryType() {
return this._ws ? this._ws.binaryType : this._binaryType;
}

set binaryType(value: string) {
set binaryType(value: BinaryType) {
this._binaryType = value;
if (this._ws) {
// @ts-ignore
this._ws.binaryType = value;
}
}
@@ -194,29 +193,29 @@ export default class ReconnectingWebSocket {
/**
* An event listener to be called when the WebSocket connection's readyState changes to CLOSED
*/
public onclose?: (event: Events.CloseEvent) => void = undefined;
public onclose: ((event: Events.CloseEvent) => void) | null = null;

/**
* An event listener to be called when an error occurs
*/
public onerror?: (event: Events.ErrorEvent) => void = undefined;
public onerror: ((event: Events.ErrorEvent) => void) | null = null;

/**
* An event listener to be called when a message is received from the server
*/
public onmessage?: (event: MessageEvent) => void = undefined;
public onmessage: ((event: MessageEvent) => void) | null = null;

/**
* An event listener to be called when the WebSocket connection's readyState changes to OPEN;
* this indicates that the connection is ready to send and receive data
*/
public onopen?: (event: Events.Event) => void = undefined;
public onopen: ((event: Event) => void) | null = null;

/**
* Closes the WebSocket connection or connection attempt, if any. If the connection is already
* CLOSED, this method does nothing
*/
public close(code: number = 1000, reason?: string) {
public close(code = 1000, reason?: string) {
this._closeCalled = true;
this._shouldReconnect = false;
this._clearTimeouts();
@@ -276,6 +275,16 @@ export default class ReconnectingWebSocket {
}
}

public dispatchEvent(event: Event) {
const listeners = this._listeners[event.type as keyof Events.WebSocketEventListenerMap];
if (listeners) {
for (const listener of listeners) {
this._callEventListener(event, listener);
}
}
return true;
}

/**
* Removes an event listener
*/
@@ -330,7 +339,8 @@ export default class ReconnectingWebSocket {
if (typeof url === 'string') {
return Promise.resolve(url);
}
if (!!url.then) {
// @ts-ignore redundant check
if (url.then) {
return url;
}
}
@@ -372,7 +382,6 @@ export default class ReconnectingWebSocket {
this._ws = this._protocols
? new WebSocket(url, this._protocols)
: new WebSocket(url);
// @ts-ignore
this._ws!.binaryType = this._binaryType;
this._connectLock = false;
this._addListeners();
@@ -386,7 +395,7 @@ export default class ReconnectingWebSocket {
this._handleError(new Events.ErrorEvent(Error('TIMEOUT'), this));
}

private _disconnect(code: number = 1000, reason?: string) {
private _disconnect(code = 1000, reason?: string) {
this._clearTimeouts();
if (!this._ws) {
return;
@@ -418,18 +427,17 @@ export default class ReconnectingWebSocket {
}
}

private _handleOpen = (event: Events.Event) => {
private _handleOpen = (event: Event) => {
this._debug('open event');
const {minUptime = DEFAULT.minUptime} = this._options;

clearTimeout(this._connectTimeout);
this._uptimeTimeout = setTimeout(() => this._acceptOpen(), minUptime);

// @ts-ignore
this._ws!.binaryType = this._binaryType;

// send enqueued messages (messages sent before websocket open event)
this._messageQueue.forEach(message => this._ws!.send(message));
this._messageQueue.forEach(message => this._ws?.send(message));
this._messageQueue = [];

if (this.onopen) {
8 changes: 0 additions & 8 deletions test/jsconfig.json

This file was deleted.

10 changes: 4 additions & 6 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -4,11 +4,9 @@
"module": "ESNext",
"strict": true,
"target": "es5",
"lib": [
"es6",
"dom"
],
"lib": ["es6", "dom"],
"declaration": true,
"downlevelIteration": true
"downlevelIteration": true,
"esModuleInterop": true
}
}
}
17 changes: 0 additions & 17 deletions tslint.json

This file was deleted.