Skip to content

Commit 6fdf3c9

Browse files
refactor: import single-file 3rd party modules
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
1 parent b862924 commit 6fdf3c9

File tree

5 files changed

+81
-13
lines changed

5 files changed

+81
-13
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lib/contrib/*

lib/contrib/backo2.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* Initialize backoff timer with `opts`.
3+
*
4+
* - `min` initial timeout in milliseconds [100]
5+
* - `max` max timeout [10000]
6+
* - `jitter` [0]
7+
* - `factor` [2]
8+
*
9+
* @param {Object} opts
10+
* @api public
11+
*/
12+
13+
export function Backoff(opts) {
14+
opts = opts || {};
15+
this.ms = opts.min || 100;
16+
this.max = opts.max || 10000;
17+
this.factor = opts.factor || 2;
18+
this.jitter = opts.jitter > 0 && opts.jitter <= 1 ? opts.jitter : 0;
19+
this.attempts = 0;
20+
}
21+
22+
/**
23+
* Return the backoff duration.
24+
*
25+
* @return {Number}
26+
* @api public
27+
*/
28+
29+
Backoff.prototype.duration = function(){
30+
var ms = this.ms * Math.pow(this.factor, this.attempts++);
31+
if (this.jitter) {
32+
var rand = Math.random();
33+
var deviation = Math.floor(rand * this.jitter * ms);
34+
ms = (Math.floor(rand * 10) & 1) == 0 ? ms - deviation : ms + deviation;
35+
}
36+
return Math.min(ms, this.max) | 0;
37+
};
38+
39+
/**
40+
* Reset the number of attempts.
41+
*
42+
* @api public
43+
*/
44+
45+
Backoff.prototype.reset = function(){
46+
this.attempts = 0;
47+
};
48+
49+
/**
50+
* Set the minimum duration
51+
*
52+
* @api public
53+
*/
54+
55+
Backoff.prototype.setMin = function(min){
56+
this.ms = min;
57+
};
58+
59+
/**
60+
* Set the maximum duration
61+
*
62+
* @api public
63+
*/
64+
65+
Backoff.prototype.setMax = function(max){
66+
this.max = max;
67+
};
68+
69+
/**
70+
* Set the jitter
71+
*
72+
* @api public
73+
*/
74+
75+
Backoff.prototype.setJitter = function(jitter){
76+
this.jitter = jitter;
77+
};
78+

lib/manager.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Socket, SocketOptions, DisconnectDescription } from "./socket.js";
77
import * as parser from "socket.io-parser";
88
import { Decoder, Encoder, Packet } from "socket.io-parser";
99
import { on } from "./on.js";
10-
import Backoff from "backo2";
10+
import { Backoff } from "./contrib/backo2.js";
1111
import {
1212
DefaultEventsMap,
1313
EventsMap,
@@ -125,6 +125,7 @@ export class Manager<
125125

126126
private nsps: Record<string, Socket> = {};
127127
private subs: Array<ReturnType<typeof on>> = [];
128+
// @ts-ignore
128129
private backoff: Backoff;
129130
private setTimeoutFn: typeof setTimeout;
130131
private _reconnection: boolean;

package-lock.json

Lines changed: 0 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
"types": "./build/esm/index.d.ts",
3434
"dependencies": {
3535
"@socket.io/component-emitter": "~3.1.0",
36-
"backo2": "~1.0.2",
3736
"debug": "~4.3.2",
3837
"engine.io-client": "~6.2.1",
3938
"socket.io-parser": "~4.2.0"

0 commit comments

Comments
 (0)