Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
b862924
commit 6fdf3c9
Showing
5 changed files
with
81 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
lib/contrib/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters