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-1086 Use voice-js DNS names directly #119

Merged
merged 4 commits into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*.sw*
.env
.idea
.nyc_output/
config.yaml
Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
2.2.0 (In Progress)
===================

Changes
-------

This release includes updated DNS names for [Twilio Edge Locations](https://www.twilio.com/docs/global-infrastructure/edge-locations). The Voice JS SDK uses these Edge Locations to connect to Twilio’s infrastructure via the parameter `Device.Options.edge`. The current usage of this parameter does not change as the SDK automatically maps the edge value to the new DNS names.

Additionally, you need to update your [Content Security Policies (CSP)](README.md#content-security-policy-csp) if you have it enabled for your application. You also need to update your network configuration such as firewalls, if necessary, to allow connections to the new [DNS names and IP addresses](https://www.twilio.com/docs/voice/sdks/network-connectivity-requirements).


2.1.1 (February 18, 2022)
=========================

Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,27 +137,27 @@ Use the following policy directives to enable [CSP](https://developer.mozilla.or
```
script-src https://media.twiliocdn.com https://sdk.twilio.com
media-src mediastream https://media.twiliocdn.com https://sdk.twilio.com
connect-src https://eventgw.twilio.com wss://chunderw-vpc-gll.twilio.com https://media.twiliocdn.com https://sdk.twilio.com
connect-src https://eventgw.twilio.com wss://voice-js.roaming.twilio.com https://media.twiliocdn.com https://sdk.twilio.com
```

If you are providing a non-default value for `Device.ConnectOptions.edge` parameter, you need to add the Signaling URI `wss://chunderw-vpc-gll-{regionId}.twilio.com` in your `connect-src` directive where `regionId` is the `Region ID` as defined in this [page](https://www.twilio.com/docs/global-infrastructure/edge-locations/legacy-regions). See examples below.
If you are providing a non-default value for `Device.ConnectOptions.edge` parameter, you need to add the Signaling URI `wss://voice-js.{edgeId}.twilio.com` in your `connect-src` directive where `edgeId` is the `Edge ID` as defined in this [page](https://www.twilio.com/docs/global-infrastructure/edge-locations). See examples below.

**If `Device.ConnectOptions.edge` is `ashburn`**

```
connect-src https://eventgw.twilio.com https://media.twiliocdn.com https://sdk.twilio.com wss://chunderw-vpc-gll-us1.twilio.com
connect-src https://eventgw.twilio.com https://media.twiliocdn.com https://sdk.twilio.com wss://voice-js.ashburn.twilio.com
```

**If `Device.ConnectOptions.edge` is `['ashburn', 'sydney', 'roaming']`**

```
connect-src https://eventgw.twilio.com https://media.twiliocdn.com https://sdk.twilio.com wss://chunderw-vpc-gll-us1.twilio.com wss://chunderw-vpc-gll-au1.twilio.com wss://chunderw-vpc-gll.twilio.com
connect-src https://eventgw.twilio.com https://media.twiliocdn.com https://sdk.twilio.com wss://voice-js.ashburn.twilio.com wss://voice-js.sydney.twilio.com wss://voice-js.roaming.twilio.com
```

If you are providing a home region grant into your [Twilio access token](https://www.twilio.com/docs/iam/access-tokens), you need to add the insights endpoint in your `connect-src` directive using `eventgw.{homeRegion}.twilio.com` format. Below is an example if your home region grant is `sg1`.

```
connect-src https://eventgw.sg1.twilio.com wss://chunderw-vpc-gll.twilio.com https://media.twiliocdn.com https://sdk.twilio.com
connect-src https://eventgw.sg1.twilio.com wss://voice-js.roaming.twilio.com https://media.twiliocdn.com https://sdk.twilio.com
```

License
Expand Down
15 changes: 0 additions & 15 deletions lib/twilio/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -922,17 +922,12 @@ class Call extends EventEmitter {
call_sid: this.parameters.CallSid,
dscp: !!this._options.dscp,
sdk_version: C.RELEASE_VERSION,
selected_region: this._options.selectedRegion,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we publish the selected edge instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

};

if (this._options.gateway) {
payload.gateway = this._options.gateway;
}

if (this._options.region) {
payload.region = this._options.region;
}

payload.direction = this._direction;
return payload;
}
Expand Down Expand Up @@ -1664,11 +1659,6 @@ namespace Call {
*/
reconnectToken?: string;

/**
* The Region currently connected to.
*/
region?: string;

/**
* An RTCConfiguration to pass to the RTCPeerConnection constructor.
*/
Expand All @@ -1680,11 +1670,6 @@ namespace Call {
*/
rtcConstraints?: MediaStreamConstraints;

/**
* The region passed to {@link Device} on setup.
*/
selectedRegion?: string;

/**
* Whether the disconnect sound should be played.
*/
Expand Down
12 changes: 2 additions & 10 deletions lib/twilio/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -736,11 +736,7 @@ class Device extends EventEmitter {
: Array.isArray(this._options.chunderw) && this._options.chunderw;

const newChunderURIs = this._chunderURIs = (
chunderw || getChunderURIs(
this._options.edge,
undefined,
this._log.warn.bind(this._log),
)
chunderw || getChunderURIs(this._options.edge)
).map(createSignalingEndpointURL);

let hasChunderURIsChanged =
Expand Down Expand Up @@ -1107,11 +1103,7 @@ class Device extends EventEmitter {
}
this._home = payload.home;

const preferredURIs = getChunderURIs(
this._edge as Edge,
undefined,
this._log.warn.bind(this._log),
);
const preferredURIs = getChunderURIs(this._edge as Edge);
if (preferredURIs.length > 0) {
const [preferredURI] = preferredURIs;
this._preferredURI = createSignalingEndpointURL(preferredURI);
Expand Down
179 changes: 5 additions & 174 deletions lib/twilio/regions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,6 @@
*/
import { InvalidArgumentError } from './errors';

/**
* Valid deprecated regions.
* @private
*/
export enum DeprecatedRegion {
Au = 'au',
Br = 'br',
Ie = 'ie',
Jp = 'jp',
Sg = 'sg',
UsOr = 'us-or',
UsVa = 'us-va',
}

/**
* Valid edges.
* @private
Expand Down Expand Up @@ -87,26 +73,6 @@ export enum Region {
Us2Tnx = 'us2-tnx',
}

/**
* All valid regions
* @private
*/
export type ValidRegion = Region | DeprecatedRegion;

/**
* Deprecated regions. Maps the deprecated region to its equivalent up-to-date region.
* @private
*/
export const deprecatedRegions: Record<DeprecatedRegion, Region> = {
[DeprecatedRegion.Au]: Region.Au1,
[DeprecatedRegion.Br]: Region.Br1,
[DeprecatedRegion.Ie]: Region.Ie1,
[DeprecatedRegion.Jp]: Region.Jp1,
[DeprecatedRegion.Sg]: Region.Sg1,
[DeprecatedRegion.UsOr]: Region.Us1,
[DeprecatedRegion.UsVa]: Region.Us1,
};

/**
* Region shortcodes. Maps the full region name from AWS to the Twilio shortcode.
* @private
Expand All @@ -122,60 +88,6 @@ export const regionShortcodes: { [index: string]: Region } = {
US_WEST_OREGON: Region.Us2,
};

/**
* Region URIs. Maps the Twilio shortcode to its Twilio endpoint URI.
* @private
*/
const regionURIs: Record<Region, string> = {
[Region.Au1]: 'chunderw-vpc-gll-au1.twilio.com',
[Region.Au1Ix]: 'chunderw-vpc-gll-au1-ix.twilio.com',
[Region.Br1]: 'chunderw-vpc-gll-br1.twilio.com',
[Region.De1]: 'chunderw-vpc-gll-de1.twilio.com',
[Region.De1Ix]: 'chunderw-vpc-gll-de1-ix.twilio.com',
[Region.Gll]: 'chunderw-vpc-gll.twilio.com',
[Region.Ie1]: 'chunderw-vpc-gll-ie1.twilio.com',
[Region.Ie1Ix]: 'chunderw-vpc-gll-ie1-ix.twilio.com',
[Region.Ie1Tnx]: 'chunderw-vpc-gll-ie1-tnx.twilio.com',
[Region.Jp1]: 'chunderw-vpc-gll-jp1.twilio.com',
[Region.Jp1Ix]: 'chunderw-vpc-gll-jp1-ix.twilio.com',
[Region.Sg1]: 'chunderw-vpc-gll-sg1.twilio.com',
[Region.Sg1Ix]: 'chunderw-vpc-gll-sg1-ix.twilio.com',
[Region.Sg1Tnx]: 'chunderw-vpc-gll-sg1-tnx.twilio.com',
[Region.Us1]: 'chunderw-vpc-gll-us1.twilio.com',
[Region.Us1Ix]: 'chunderw-vpc-gll-us1-ix.twilio.com',
[Region.Us1Tnx]: 'chunderw-vpc-gll-us1-tnx.twilio.com',
[Region.Us2]: 'chunderw-vpc-gll-us2.twilio.com',
[Region.Us2Ix]: 'chunderw-vpc-gll-us2-ix.twilio.com',
[Region.Us2Tnx]: 'chunderw-vpc-gll-us2-tnx.twilio.com',
};

/**
* Edge to region mapping, as part of Phase 1 Regional (CLIENT-7519).
* Temporary.
* @private
*/
export const edgeToRegion: Record<Edge, Region> = {
[Edge.Sydney]: Region.Au1,
[Edge.SaoPaulo]: Region.Br1,
[Edge.Dublin]: Region.Ie1,
[Edge.Frankfurt]: Region.De1,
[Edge.Tokyo]: Region.Jp1,
[Edge.Singapore]: Region.Sg1,
[Edge.Ashburn]: Region.Us1,
[Edge.Umatilla]: Region.Us2,
[Edge.Roaming]: Region.Gll,
/**
* Interconnect edges
*/
[Edge.AshburnIx]: Region.Us1Ix,
[Edge.SanJoseIx]: Region.Us2Ix,
[Edge.LondonIx]: Region.Ie1Ix,
[Edge.FrankfurtIx]: Region.De1Ix,
[Edge.SingaporeIx]: Region.Sg1Ix,
[Edge.SydneyIx]: Region.Au1Ix,
[Edge.TokyoIx]: Region.Jp1Ix,
};

/**
* Region to edge mapping, as part of Phase 1 Regional (CLIENT-7519).
* Temporary.
Expand Down Expand Up @@ -210,45 +122,20 @@ export const regionToEdge: Record<Region, Edge> = {
[Region.Sg1Tnx]: Edge.SingaporeIx,
};

/**
* The default region to connect to and create a chunder uri from if region is
* not defined.
* @constant
* @private
*/
export const defaultRegion: string = 'gll';

/**
* The default edge to connect to and create a chunder uri from, if the edge
* parameter is not specified during setup in `Device`.
* @constant
*/
export const defaultEdge: Edge = Edge.Roaming;

/**
* The default chunder URI to connect to, should map to region `gll`.
* @constant
* @private
*/
export const defaultChunderRegionURI: string = 'chunderw-vpc-gll.twilio.com';

/**
* The default event gateway URI to publish to.
* @constant
* @private
*/
const defaultEventGatewayURI: string = 'eventgw.twilio.com';

/**
* String template for a region chunder URI
* @param region - The region.
*/
function createChunderRegionURI(region: string): string {
return region === defaultRegion
? defaultChunderRegionURI
: `chunderw-vpc-gll-${region}.twilio.com`;
}

/**
* String template for an edge chunder URI
* @param edge - The edge.
Expand Down Expand Up @@ -276,81 +163,25 @@ export function createSignalingEndpointURL(uri: string): string {
}

/**
* Get the URI associated with the passed region or edge. If both are passed,
* then we want to fail `Device` setup, so we throw an error.
* As of CLIENT-7519, Regions are deprecated in favor of edges as part of
* Phase 1 Regional.
*
* Get the URI associated with the passed edge.
* @private
* @param edge - A string or an array of edge values
* @param region - The region shortcode.
* @param [onDeprecated] - A callback containing the deprecation message to be
* warned when the passed parameters are deprecated.
* @returns An array of chunder URIs
*/
export function getChunderURIs(
edge: string[] | string | undefined,
region: string | undefined,
onDeprecated?: (message: string) => void,
): string[] {
if (!!region && typeof region !== 'string') {
throw new InvalidArgumentError(
'If `region` is provided, it must be of type `string`.',
);
}

export function getChunderURIs(edge?: string[] | string): string[] {
if (!!edge && typeof edge !== 'string' && !Array.isArray(edge)) {
throw new InvalidArgumentError(
'If `edge` is provided, it must be of type `string` or an array of strings.',
);
}

const deprecatedMessages: string[] = [];
let uris: string[];

if (region && edge) {
throw new InvalidArgumentError(
'You cannot specify `region` when `edge` is specified in' +
'`Twilio.Device.Options`.',
);
} else if (region) {
let chunderRegion = region;

deprecatedMessages.push(
'Regions are deprecated in favor of edges. Please see this page for ' +
'documentation: https://www.twilio.com/docs/voice/client/edges.',
);

const isDeprecatedRegion: boolean =
(Object.values(DeprecatedRegion) as string[]).includes(chunderRegion);
if (isDeprecatedRegion) {
chunderRegion = deprecatedRegions[chunderRegion as DeprecatedRegion];
}

const isKnownRegion: boolean =
(Object.values(Region) as string[]).includes(chunderRegion);
if (isKnownRegion) {
const preferredEdge = regionToEdge[chunderRegion as Region];
deprecatedMessages.push(
`Region "${chunderRegion}" is deprecated, please use \`edge\` ` +
`"${preferredEdge}".`,
);
}

uris = [createChunderRegionURI(chunderRegion)];
} else if (edge) {
const edgeValues = Object.values(Edge) as string[];
if (edge) {
const edgeParams = Array.isArray(edge) ? edge : [edge];

uris = edgeParams.map((param: Edge) => edgeValues.includes(param)
? createChunderRegionURI(edgeToRegion[param])
: createChunderEdgeURI(param));
uris = edgeParams.map((param: Edge) => createChunderEdgeURI(param));
} else {
uris = [defaultChunderRegionURI];
}

if (onDeprecated && deprecatedMessages.length) {
setTimeout(() => onDeprecated(deprecatedMessages.join('\n')));
uris = [createChunderEdgeURI(defaultEdge)];
}

return uris;
Expand Down
Loading