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

VBLOCKS-1718 | Remove default node modules #167

Merged
merged 13 commits into from
May 31, 2023
11 changes: 10 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
.circleci
.deepsource.toml
.env
.github
.idea
.nyc_output
.nycrc
.release.json
BUILD.md
build.yaml
config.example.yaml
config.yaml
coverage
docs
extension
lib
karma.conf.ts
karma.network.conf.ts
node_modules
nodemon.json
PREFLIGHT.md
scripts
server.js
templates
tests
tslint.json
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
2.6.0 (In Progress)
===================

Changes (TODO)
Changes
-------

- Removed usage of NodeJS modules from the SDK and some dependencies. With this change, the SDK should now work with some of the latest frameworks that use the latest versions of bundlers such as Vite and Webpack.
- Removed unnecessary files from the generated npm package.
- Links to source maps are now included in the generated npm package.

2.5.0 (May 9, 2023)
===================

Expand Down
74 changes: 74 additions & 0 deletions lib/twilio/backoff.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* @packageDocumentation
* @internalapi
*/
// @ts-nocheck
// NOTE (csantos): This file was taken directly from twilio-video and has been renamed from JS to TS only.
// It needs to be re-written as part of the overall updating of the files to TS.
import { EventEmitter } from 'events';

class Backoff extends EventEmitter {
/**
* 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) {
super();
Object.defineProperties(this, {
_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);
// tslint:disable-next-line
ms = (Math.floor(rand * 10) & 1) === 0 ? ms - deviation : ms + deviation;
}
// tslint:disable-next-line
return Math.min(ms, this._max) | 0;
},
},
_factor: { value: options.factor || 2 },
_jitter: { value: options.jitter > 0 && options.jitter <= 1 ? options.jitter : 0 },
_max: { value: options.max || 10000 },
_min: { value: options.min || 100 },
_timeoutID: {
value: null,
writable: true,
},
});
}

backoff() {
const duration = this._duration;
if (this._timeoutID) {
clearTimeout(this._timeoutID);
this._timeoutID = null;
}

this.emit('backoff', this._attempts, duration);
this._timeoutID = setTimeout(() => {
this.emit('ready', this._attempts, duration);
this._attempts++;
}, duration);
}

reset() {
this._attempts = 0;
if (this._timeoutID) {
clearTimeout(this._timeoutID);
this._timeoutID = null;
}
}
}

export default Backoff;
12 changes: 6 additions & 6 deletions lib/twilio/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* @internal
*/
import { EventEmitter } from 'events';
import Backoff from './backoff';
import Device from './device';
import DialtonePlayer from './dialtonePlayer';
import {
Expand All @@ -26,7 +27,6 @@ import StatsMonitor from './statsMonitor';
import { isChrome } from './util';
import { generateVoiceEventSid } from './uuid';

const Backoff = require('backoff');
const { RELEASE_VERSION } = require('./constants');

// Placeholders until we convert the respective files to TypeScript.
Expand All @@ -53,9 +53,9 @@ export type ISound = any;

const BACKOFF_CONFIG = {
factor: 1.1,
initialDelay: 1,
maxDelay: 30000,
randomisationFactor: 0.5,
jitter: 0.5,
max: 30000,
min: 1,
};

const DTMF_INTER_TONE_GAP: number = 70;
Expand Down Expand Up @@ -345,7 +345,7 @@ class Call extends EventEmitter {
this.callerInfo = null;
}

this._mediaReconnectBackoff = Backoff.exponential(BACKOFF_CONFIG);
this._mediaReconnectBackoff = new Backoff(BACKOFF_CONFIG);
this._mediaReconnectBackoff.on('ready', () => this._mediaHandler.iceRestart());

// temporary call sid to be used for outgoing calls
Expand Down Expand Up @@ -1242,7 +1242,7 @@ class Call extends EventEmitter {
if (isEndOfIceCycle) {

// We already exceeded max retry time.
if (Date.now() - this._mediaReconnectStartTime > BACKOFF_CONFIG.maxDelay) {
if (Date.now() - this._mediaReconnectStartTime > BACKOFF_CONFIG.max) {
this._log.info('Exceeded max ICE retries');
return this._mediaHandler.onerror(MEDIA_DISCONNECT_ERROR);
}
Expand Down
19 changes: 9 additions & 10 deletions lib/twilio/wstransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@

import { EventEmitter } from 'events';
import * as WebSocket from 'ws';
import Backoff from './backoff';
import { SignalingErrors } from './errors';
import Log from './log';

// tslint:disable-next-line
const Backoff = require('backoff');

const CONNECT_SUCCESS_TIMEOUT = 10000;
const CONNECT_TIMEOUT = 5000;
const HEARTBEAT_TIMEOUT = 15000;
Expand Down Expand Up @@ -521,11 +519,12 @@ export default class WSTransport extends EventEmitter {
private _setupBackoffs(): typeof WSTransport.prototype._backoff {
const preferredBackoffConfig = {
factor: 2.0,
maxDelay: this._options.maxPreferredDelayMs,
randomisationFactor: 0.40,
jitter: 0.40,
max: this._options.maxPreferredDelayMs,
min: 100,
};
this._log.info('Initializing preferred transport backoff using config: ', preferredBackoffConfig);
const preferredBackoff = Backoff.exponential(preferredBackoffConfig);
const preferredBackoff = new Backoff(preferredBackoffConfig);

preferredBackoff.on('backoff', (attempt: number, delay: number) => {
if (this.state === WSTransportState.Closed) {
Expand Down Expand Up @@ -565,16 +564,16 @@ export default class WSTransport extends EventEmitter {

const primaryBackoffConfig = {
factor: 2.0,
jitter: 0.40,
max: this._options.maxPrimaryDelayMs,
// We only want a random initial delay if there are any fallback edges
// Initial delay between 1s and 5s both inclusive
initialDelay: this._uris && this._uris.length > 1
min: this._uris && this._uris.length > 1
? Math.floor(Math.random() * (5000 - 1000 + 1)) + 1000
: 100,
maxDelay: this._options.maxPrimaryDelayMs,
randomisationFactor: 0.40,
};
this._log.info('Initializing primary transport backoff using config: ', primaryBackoffConfig);
const primaryBackoff = Backoff.exponential(primaryBackoffConfig);
const primaryBackoff = new Backoff(primaryBackoffConfig);

primaryBackoff.on('backoff', (attempt: number, delay: number) => {
if (this.state === WSTransportState.Closed) {
Expand Down
82 changes: 25 additions & 57 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
"@twilio/audioplayer": "1.0.6",
"@twilio/voice-errors": "1.3.1",
"@types/md5": "2.3.2",
"backoff": "2.5.0",
"events": "3.3.0",
"loglevel": "1.6.7",
"md5": "2.3.0",
"rtcpeerconnection-shim": "1.2.8",
Expand Down
1 change: 1 addition & 0 deletions tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ require('./sound');
require('./sdp');

require('./unit/asyncQueue');
require('./unit/backoff');
require('./unit/icecandidate');
require('./unit/call');
require('./unit/device');
Expand Down
Loading