Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VIDEO-9282 - Migrate SDK from using node.js util. #1752

Merged
merged 45 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
3e1f58e
Replacing util.inherits
PikaJoyce Apr 1, 2022
4f0b622
Changing global.window to window object
PikaJoyce Apr 1, 2022
94b400b
Fixing toplevel declaration
PikaJoyce Apr 4, 2022
310b68b
Fixing toplevel global variable
PikaJoyce Apr 4, 2022
e228d1c
Add Changelog and license
PikaJoyce Apr 5, 2022
2a73778
Replacing Backoff with Backo module
PikaJoyce Apr 21, 2022
eeadc7e
Changing backoff in twilioconnectiontransport
PikaJoyce Apr 22, 2022
36e9ca5
Removing old reconnectBackoffConfig
PikaJoyce Apr 22, 2022
d305b4b
Adding max timeout retry to test unit tests
PikaJoyce Apr 22, 2022
251f5e6
Trying to fix unit tests
PikaJoyce Apr 25, 2022
80d32f7
Increasing no output timeout
PikaJoyce Apr 25, 2022
e5523b9
Changing toplevel declaration to globalThis
PikaJoyce Apr 25, 2022
b9e9953
reverting timeout in config
PikaJoyce Apr 25, 2022
6fadf85
Fixing constant
PikaJoyce Apr 27, 2022
517c440
Mock backoff in twilioconnectiontransport unit test
PikaJoyce Apr 28, 2022
2492d84
Removing an options.backoff
PikaJoyce Apr 28, 2022
6b5f520
Test circle without modified unit tests
PikaJoyce Apr 28, 2022
f206bb2
merge from develop
PikaJoyce May 3, 2022
4767dcd
Reverting Mocha
PikaJoyce May 3, 2022
a0d1547
Adding affected unit tests back
PikaJoyce May 3, 2022
9350df4
Add clearTimeout
PikaJoyce May 5, 2022
6722640
Fixing unit tests on clearTimeout
PikaJoyce May 5, 2022
72893c9
Adding unit tests for inherits and backoff
PikaJoyce May 6, 2022
c2e6617
Adding backoff.start and unit test
PikaJoyce May 7, 2022
64b99ea
New Backoff class
PikaJoyce May 10, 2022
69757b4
Backoff with unit tests
PikaJoyce May 10, 2022
d89a256
Adding a few more unit tests
PikaJoyce May 10, 2022
c226641
Moving passing of function into start method and cleaning up unit tests
PikaJoyce May 10, 2022
8115a62
Merge branch 'develop' of https://github.com/twilio/twilio-video.js i…
PikaJoyce May 10, 2022
4b7d2e2
TwilioConnectionTransport tests and adding constants
PikaJoyce May 10, 2022
b57a36c
Refactoring Backoff and adding more unit tests
PikaJoyce May 10, 2022
0b09825
Allow backoff to take in function.
PikaJoyce May 11, 2022
bd15cde
Mock Backoff in unit tests
PikaJoyce May 11, 2022
e769544
Removing old Backoff replacing with new
PikaJoyce May 12, 2022
32b4168
Replacing backoff in pc and tct
PikaJoyce May 12, 2022
77730f7
Update pc and tct unit tests
PikaJoyce May 12, 2022
f696f9d
Running unit tests without backoff ut
PikaJoyce May 12, 2022
e61eca0
Fix documentvisibilitymonitor unit tests
PikaJoyce May 12, 2022
7e396ac
Incorporating some of the requested changes
PikaJoyce May 12, 2022
963dc18
Removing previous change to documentvisibilitymonitor unit tests
PikaJoyce May 12, 2022
938596f
test framework + max test
makarandp0 May 13, 2022
9afc438
Merge branch 'VIDEO-9282-Migrate-from-util' of https://github.com/twi…
PikaJoyce May 13, 2022
cd811aa
Adding a few more unit tests against backoff options
PikaJoyce May 13, 2022
516099a
Addressing Makarands suggestions
PikaJoyce May 16, 2022
23d8d17
More requested changes
PikaJoyce May 16, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
],
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
"SharedArrayBuffer": "readonly",
"globalThis": false
},
"parserOptions": {
"ecmaVersion": 2018,
Expand Down
3 changes: 1 addition & 2 deletions lib/media/track/es5/localaudiotrack.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
// only in place so that we can support ES6 classes without requiring `new`.
'use strict';

const { inherits } = require('util');

const inherits = require('../../../vendor/inherits');
const LocalAudioTrackClass = require('../localaudiotrack');

function LocalAudioTrack(mediaStreamTrack, options) {
Expand Down
2 changes: 1 addition & 1 deletion lib/media/track/es5/localdatatrack.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// only in place so that we can support ES6 classes without requiring `new`.
'use strict';

const { inherits } = require('util');
const inherits = require('../../../vendor/inherits');

const LocalDataTrackClass = require('../localdatatrack');

Expand Down
2 changes: 1 addition & 1 deletion lib/media/track/es5/localvideotrack.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// only in place so that we can support ES6 classes without requiring `new`.
'use strict';

const { inherits } = require('util');
const inherits = require('../../../vendor/inherits');

const LocalVideoTrackClass = require('../localvideotrack');

Expand Down
9 changes: 4 additions & 5 deletions lib/signaling/v2/peerconnection.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const DefaultBackoff = require('backoff');

const DefaultBackoff = require('../../util/backoff');
const {
RTCIceCandidate: DefaultRTCIceCandidate,
RTCPeerConnection: DefaultRTCPeerConnection,
Expand Down Expand Up @@ -191,7 +190,8 @@ class PeerConnectionV2 extends StateMachine {
false)
},
_iceRestartBackoff: {
value: options.Backoff.exponential(iceRestartBackoffConfig)
// eslint-disable-next-line new-cap
value: new options.Backoff(null, iceRestartBackoffConfig)
},
_instanceId: {
value: ++nInstances
Expand Down Expand Up @@ -377,7 +377,6 @@ class PeerConnectionV2 extends StateMachine {
peerConnection.addEventListener('icegatheringstatechange', this._handleIceGatheringStateChange.bind(this));
peerConnection.addEventListener('signalingstatechange', this._handleSignalingStateChange.bind(this));
peerConnection.addEventListener('track', this._handleTrackEvent.bind(this));
this._iceRestartBackoff.on('ready', () => this._initiateIceRestart());

const self = this;
this.on('stateChanged', function stateChanged(state) {
Expand Down Expand Up @@ -960,7 +959,7 @@ class PeerConnectionV2 extends StateMachine {
}
this._log.warn('An ICE restart has been scheduled');
this._isIceRestartBackoffInProgress = true;
this._iceRestartBackoff.backoff();
this._iceRestartBackoff.backoff(this._initiateIceRestart.bind(this));
}

/**
Expand Down
8 changes: 3 additions & 5 deletions lib/signaling/v2/twilioconnectiontransport.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const StateMachine = require('../../statemachine');
const TwilioConnection = require('../../twilioconnection');
const DefaultBackoff = require('backoff');
const DefaultBackoff = require('../../util/backoff');
const { reconnectBackoffConfig } = require('../../util/constants');
const Timeout = require('../../util/timeout');
const { SDK_NAME, SDK_VERSION, SDP_FORMAT } = require('../../util/constants');
Expand Down Expand Up @@ -147,7 +147,7 @@ class TwilioConnectionTransport extends StateMachine {
writable: true
},
_reconnectBackoff: {
value: options.Backoff.exponential(reconnectBackoffConfig)
value: new options.Backoff(null, reconnectBackoffConfig)
},
_session: {
value: null,
Expand Down Expand Up @@ -179,7 +179,6 @@ class TwilioConnectionTransport extends StateMachine {


setupTransport(this);

}

/**
Expand Down Expand Up @@ -377,8 +376,7 @@ class TwilioConnectionTransport extends StateMachine {

// return promise that waits with exponential backoff.
return new Promise(resolve => {
this._reconnectBackoff.once('ready', resolve);
this._reconnectBackoff.backoff();
this._reconnectBackoff.backoff(resolve);
});
}

Expand Down
2 changes: 1 addition & 1 deletion lib/twilioconnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const WS_CLOSE_BUSY_WAIT = 3005;
const WS_CLOSE_SERVER_BUSY = 3006;
const WS_CLOSE_OPEN_TIMEOUT = 3007;

const toplevel = global.window || global;
const toplevel = globalThis;
const WebSocket = toplevel.WebSocket ? toplevel.WebSocket : require('ws');

const CloseReason = {
Expand Down
84 changes: 84 additions & 0 deletions lib/util/backoff.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@

/**
* Expose `Backoff`.
*/

class Backoff {
/**
* Construct a {@link Backoff}.
* @param {object} options
* @property {number} min - Initial timeout in milliseconds [100]
* @property {number} max - Max timeout [10000]
* @property {boolean} jitter - Apply jitter [0]
* @property {number} factor - Multiplication factor for Backoff operation [2]
*/
constructor(options) {
Object.defineProperties(this, {
_min: {
value: options.min || 100
},
_max: {
value: options.max || 10000
},
_jitter: {
value: options.jitter > 0 && options.jitter <= 1 ? options.jitter : 0
},
_factor: {
value: options.factor || 2
},
_attempts: {
value: 0,
writable: true
},
_duration: {
enumerable: false,
get() {
let ms = this._min * Math.pow(this._factor, this._attempts);
if (this._jitter) {
const rand = Math.random();
const 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;
}
},
_timeoutID: {
value: null,
writable: true
}
});
}

/**
* Start the backoff operation.
* @param {function} fn - Function to call
* @return {void}
* @api public
*/
backoff(fn) {
let duration = this._duration;
if (this._timeoutID) {
clearTimeout(this._timeoutID);
this._timeoutID = null;
}
this._timeoutID = setTimeout(() => {
fn();
}, duration);
}

/**
* Reset the number of attempts and clear the timer.
*
* @return {void}
* @api public
*/
reset() {
this._attempts = 0;
if (this._timeoutID) {
clearTimeout(this._timeoutID);
this._timeoutID = null;
}
}
}

module.exports = Backoff;
10 changes: 5 additions & 5 deletions lib/util/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ module.exports.ICE_INACTIVITY_THRESHOLD_MS = 3000;

module.exports.iceRestartBackoffConfig = {
factor: 1.1,
initialDelay: 1,
maxDelay: module.exports.DEFAULT_SESSION_TIMEOUT_SEC * 1000,
randomisationFactor: 0.5
min: 1,
max: module.exports.DEFAULT_SESSION_TIMEOUT_SEC * 1000,
jitter: 1
};

module.exports.reconnectBackoffConfig = {
factor: 1.5,
initialDelay: 80,
randomisationFactor: 0.5
min: 80,
jitter: 1
};

module.exports.subscriptionMode = {
Expand Down
2 changes: 1 addition & 1 deletion lib/util/insightspublisher/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const MAX_RECONNECT_ATTEMPTS = 5;
const RECONNECT_INTERVAL_MS = 50;
const WS_CLOSE_NORMAL = 1000;

const toplevel = global.window || global;
const toplevel = globalThis;
const WebSocket = toplevel.WebSocket ? toplevel.WebSocket : require('ws');
const util = require('../../util');

Expand Down
31 changes: 31 additions & 0 deletions lib/vendor/inherits.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright (c) 2011-2022 Isaac Z. Schlueter
* Licensed under the ISC License.
*
* Copied from https://github.com/isaacs/inherits (2.0.4)
*/

module.exports = function inherits(ctor, superCtor) {
if (ctor && superCtor) {
ctor.super_ = superCtor;
if (typeof Object.create === 'function') {
// implementation from standard node.js 'util' module
ctor.prototype = Object.create(superCtor.prototype, {
constructor: {
value: ctor,
enumerable: false,
writable: true,
configurable: true
}
});
} else {
// old school shim for old browsers
class TempCtor {
constructor() { }
}
TempCtor.prototype = superCtor.prototype;
ctor.prototype = new TempCtor();
ctor.prototype.constructor = ctor;
}
}
};
2 changes: 1 addition & 1 deletion lib/webrtc/rtcpeerconnection/chrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

var ChromeRTCSessionDescription = require('../rtcsessiondescription/chrome');
var EventTarget = require('../util/eventtarget');
var inherits = require('util').inherits;
var inherits = require('../../vendor/inherits');
var Latch = require('../util/latch');
var MediaStream = require('../mediastream');
var RTCRtpSenderShim = require('../rtcrtpsender');
Expand Down
2 changes: 1 addition & 1 deletion lib/webrtc/rtcpeerconnection/firefox.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

var EventTarget = require('../util/eventtarget');
var FirefoxRTCSessionDescription = require('../rtcsessiondescription/firefox');
var inherits = require('util').inherits;
var inherits = require('../../vendor/inherits');
var updateTracksToSSRCs = require('../util/sdp').updateUnifiedPlanTrackIdsToSSRCs;
var util = require('../util');

Expand Down
2 changes: 1 addition & 1 deletion lib/webrtc/rtcpeerconnection/safari.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
'use strict';

var EventTarget = require('../util/eventtarget');
var inherits = require('util').inherits;
var inherits = require('../../vendor/inherits');
var Latch = require('../util/latch');
var sdpUtils = require('../util/sdp');
var util = require('../util');
Expand Down
17 changes: 8 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
"babel-cli": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"browserify": "^17.0.0",
"cheerio": "^0.22.0",
"cheerio": "^1.0.0-rc.10",
"cors": "^2.8.5",
"electron": "^9.1.0",
"electron": "^17.2.0",
"envify": "^4.0.0",
"eslint": "^6.2.1",
"eslint-config-standard": "^14.0.0",
Expand All @@ -47,21 +47,21 @@
"eslint-plugin-standard": "^4.0.1",
"express": "^4.16.2",
"glob": "^7.1.7",
"ink-docstrap": "^1.3.2",
"ink-docstrap": "^0.5.4",
"inquirer": "^7.0.0",
"is-docker": "^2.0.0",
"jsdoc": "^3.5.5",
"jsdoc-babel": "^0.5.0",
"json-loader": "^0.5.7",
"karma": "^5.0.2",
"karma": "^6.3.17",
"karma-browserify": "^8.0.0",
"karma-chrome-launcher": "^2.0.0",
"karma-edgium-launcher": "^4.0.0-0",
"karma-electron": "^6.1.0",
"karma-firefox-launcher": "^1.3.0",
"karma-htmlfile-reporter": "^0.3.8",
"karma-junit-reporter": "^1.2.0",
"karma-mocha": "^1.3.0",
"karma-mocha": "^2.0.1",
"karma-safari-launcher": "^1.0.0",
"karma-spec-reporter": "0.0.32",
"karma-typescript": "^5.5.1",
Expand All @@ -74,17 +74,17 @@
"nyc": "^15.1.0",
"requirejs": "^2.3.3",
"rimraf": "^2.6.1",
"simple-git": "^1.126.0",
"simple-git": "^3.4.0",
"sinon": "^4.0.1",
"ts-node": "4.0.1",
"tslint": "5.8.0",
"twilio": "^3.49.0",
"twilio-release-tool": "^1.0.2",
"typescript": "4.2.2",
"uglify-js": "^2.8.22",
"vinyl-fs": "^2.4.4",
"vinyl-fs": "^2.4.1",
"vinyl-source-stream": "^1.1.0",
"watchify": "^3.11.1",
"watchify": "^4.0.0",
"webrtc-adapter": "^7.7.1"
},
"engines": {
Expand Down Expand Up @@ -144,7 +144,6 @@
"clean": "rimraf ./coverage ./es5 ./dist"
},
"dependencies": {
"backoff": "^2.5.0",
"events": "^3.3.0",
"util": "^0.12.4",
"ws": "^7.4.6",
Expand Down
2 changes: 1 addition & 1 deletion test/lib/webrtc/fakemediastream.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var inherits = require('util').inherits;
var inherits = require('../../../lib/vendor/inherits');
var randomName = require('./util').randomName;
var EventTarget = require('../../../lib/webrtc/util/eventtarget');

Expand Down
3 changes: 3 additions & 0 deletions test/unit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ require('./spec/signaling/v2/twilioconnectiontransport');

require('./spec/util');
require('./spec/util/asyncvar');
// require('./spec/util/backoff');
require('./spec/util/browserdetection');
require('./spec/util/eventobserver');
require('./spec/util/documentvisibilitymonitor');
Expand Down Expand Up @@ -96,6 +97,8 @@ require('./spec/stats/remotetrackstats');
require('./spec/stats/remoteaudiotrackstats');
require('./spec/stats/remotevideotrackstats');

require('./spec/vendor/inherits');

require('./spec/webrtc/getstats');
require('./spec/webrtc/getusermedia');

Expand Down
Loading