Skip to content

Commit

Permalink
refactor: import single-file 3rd party modules
Browse files Browse the repository at this point in the history
This commit allows to:

- provide an ESM version of those modules ([1])
- reduce the attack surface in case of supply chain attacks
- reduce the size of the bundle with tree-shaking

As a downside, we won't receive security updates for those modules
anymore.

[1]: #1536

Related: socketio/engine.io-client@df32277
  • Loading branch information
darrachequesne committed Apr 23, 2022
1 parent b862924 commit 6fdf3c9
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 13 deletions.
1 change: 1 addition & 0 deletions .prettierignore
@@ -0,0 +1 @@
lib/contrib/*
78 changes: 78 additions & 0 deletions lib/contrib/backo2.ts
@@ -0,0 +1,78 @@
/**
* Initialize backoff timer with `opts`.
*
* - `min` initial timeout in milliseconds [100]
* - `max` max timeout [10000]
* - `jitter` [0]
* - `factor` [2]
*
* @param {Object} opts
* @api public
*/

export function Backoff(opts) {
opts = opts || {};
this.ms = opts.min || 100;
this.max = opts.max || 10000;
this.factor = opts.factor || 2;
this.jitter = opts.jitter > 0 && opts.jitter <= 1 ? opts.jitter : 0;
this.attempts = 0;
}

/**
* Return the backoff duration.
*
* @return {Number}
* @api public
*/

Backoff.prototype.duration = function(){
var ms = this.ms * Math.pow(this.factor, this.attempts++);
if (this.jitter) {
var rand = Math.random();
var deviation = Math.floor(rand * this.jitter * ms);
ms = (Math.floor(rand * 10) & 1) == 0 ? ms - deviation : ms + deviation;
}
return Math.min(ms, this.max) | 0;
};

/**
* Reset the number of attempts.
*
* @api public
*/

Backoff.prototype.reset = function(){
this.attempts = 0;
};

/**
* Set the minimum duration
*
* @api public
*/

Backoff.prototype.setMin = function(min){
this.ms = min;
};

/**
* Set the maximum duration
*
* @api public
*/

Backoff.prototype.setMax = function(max){
this.max = max;
};

/**
* Set the jitter
*
* @api public
*/

Backoff.prototype.setJitter = function(jitter){
this.jitter = jitter;
};

3 changes: 2 additions & 1 deletion lib/manager.ts
Expand Up @@ -7,7 +7,7 @@ import { Socket, SocketOptions, DisconnectDescription } from "./socket.js";
import * as parser from "socket.io-parser";
import { Decoder, Encoder, Packet } from "socket.io-parser";
import { on } from "./on.js";
import Backoff from "backo2";
import { Backoff } from "./contrib/backo2.js";
import {
DefaultEventsMap,
EventsMap,
Expand Down Expand Up @@ -125,6 +125,7 @@ export class Manager<

private nsps: Record<string, Socket> = {};
private subs: Array<ReturnType<typeof on>> = [];
// @ts-ignore
private backoff: Backoff;
private setTimeoutFn: typeof setTimeout;
private _reconnection: boolean;
Expand Down
11 changes: 0 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Expand Up @@ -33,7 +33,6 @@
"types": "./build/esm/index.d.ts",
"dependencies": {
"@socket.io/component-emitter": "~3.1.0",
"backo2": "~1.0.2",
"debug": "~4.3.2",
"engine.io-client": "~6.2.1",
"socket.io-parser": "~4.2.0"
Expand Down

0 comments on commit 6fdf3c9

Please sign in to comment.