diff --git a/src/common/constants/TransportName.ts b/src/common/constants/TransportName.ts index 8c49a76cee..29137c7f4c 100644 --- a/src/common/constants/TransportName.ts +++ b/src/common/constants/TransportName.ts @@ -1,8 +1,14 @@ -enum TransportName { - WebSocket = 'web_socket', - Comet = 'comet', - XhrStreaming = 'xhr_streaming', - XhrPolling = 'xhr_polling', +export namespace TransportNames { + export const WebSocket = 'web_socket' as const; + export const Comet = 'comet' as const; + export const XhrStreaming = 'xhr_streaming' as const; + export const XhrPolling = 'xhr_polling' as const; } +type TransportName = + | typeof TransportNames.WebSocket + | typeof TransportNames.Comet + | typeof TransportNames.XhrStreaming + | typeof TransportNames.XhrPolling; + export default TransportName; diff --git a/src/common/lib/transport/connectionmanager.ts b/src/common/lib/transport/connectionmanager.ts index 53f4860ffc..cc6b1d6637 100644 --- a/src/common/lib/transport/connectionmanager.ts +++ b/src/common/lib/transport/connectionmanager.ts @@ -19,6 +19,7 @@ import { ErrCallback } from 'common/types/utils'; import HttpStatusCodes from 'common/constants/HttpStatusCodes'; import BaseRealtime from '../client/baserealtime'; import { NormalisedClientOptions } from 'common/types/ClientOptions'; +import TransportName from 'common/constants/TransportName'; let globalObject = typeof global !== 'undefined' ? global : typeof window !== 'undefined' ? window : self; @@ -98,7 +99,7 @@ function decodeRecoveryKey(recoveryKey: NormalisedClientOptions['recover']): Rec } } -const supportedTransports: Partial> = {}; +const supportedTransports: Partial> = {}; export class TransportParams { options: NormalisedClientOptions; @@ -201,9 +202,9 @@ class ConnectionManager extends EventEmitter { connectionKey?: string; connectionStateTtl: number; maxIdleInterval: number | null; - transports: string[]; - baseTransport: string; - upgradeTransports: string[]; + transports: TransportName[]; + baseTransport: TransportName; + upgradeTransports: TransportName[]; transportPreference: string | null; httpHosts: string[]; activeProtocol: null | Protocol; @@ -481,7 +482,7 @@ class ConnectionManager extends EventEmitter { * @param candidate, the transport to try * @param callback */ - tryATransport(transportParams: TransportParams, candidate: string, callback: Function): void { + tryATransport(transportParams: TransportParams, candidate: TransportName, callback: Function): void { Logger.logAction(Logger.LOG_MICRO, 'ConnectionManager.tryATransport()', 'trying ' + candidate); Transport.tryConnect( @@ -1677,13 +1678,13 @@ class ConnectionManager extends EventEmitter { this.tryATransport(transportParams, this.baseTransport, hostAttemptCb); } - getUpgradePossibilities(): string[] { + getUpgradePossibilities(): TransportName[] { /* returns the subset of upgradeTransports to the right of the current * transport in upgradeTransports (if it's in there - if not, currentSerial * will be -1, so return upgradeTransports.slice(0) == upgradeTransports */ const current = (this.activeProtocol as Protocol).getTransport().shortName; const currentSerial = Utils.arrIndexOf(this.upgradeTransports, current); - return this.upgradeTransports.slice(currentSerial + 1) as string[]; + return this.upgradeTransports.slice(currentSerial + 1); } upgradeIfNeeded(transportParams: Record): void { @@ -1698,7 +1699,7 @@ class ConnectionManager extends EventEmitter { return; } - Utils.arrForEach(upgradePossibilities, (upgradeTransport: string) => { + Utils.arrForEach(upgradePossibilities, (upgradeTransport: TransportName) => { /* Note: the transport may mutate the params, so give each transport a fresh one */ const upgradeTransportParams = this.createTransportParams(transportParams.host, 'upgrade'); this.tryATransport(upgradeTransportParams, upgradeTransport, noop); @@ -2101,7 +2102,7 @@ class ConnectionManager extends EventEmitter { this.proposedTransports.push(transport); } - getTransportPreference(): string { + getTransportPreference(): TransportName { return this.transportPreference || (haveWebStorage() && Platform.WebStorage?.get?.(transportPreferenceName)); } diff --git a/src/common/lib/transport/websockettransport.ts b/src/common/lib/transport/websockettransport.ts index 90a93e2bc5..2324fc3021 100644 --- a/src/common/lib/transport/websockettransport.ts +++ b/src/common/lib/transport/websockettransport.ts @@ -8,9 +8,9 @@ import ErrorInfo from '../types/errorinfo'; import NodeWebSocket from 'ws'; import ConnectionManager, { TransportParams } from './connectionmanager'; import Auth from '../client/auth'; -import TransportName from 'common/constants/TransportName'; +import { TransportNames } from 'common/constants/TransportName'; -const shortName = TransportName.WebSocket; +const shortName = TransportNames.WebSocket; function isNodeWebSocket(ws: WebSocket | NodeWebSocket): ws is NodeWebSocket { return !!(ws as NodeWebSocket).on; diff --git a/src/common/lib/util/utils.ts b/src/common/lib/util/utils.ts index d3aca59d2f..a8bf3c4241 100644 --- a/src/common/lib/util/utils.ts +++ b/src/common/lib/util/utils.ts @@ -155,7 +155,7 @@ export function containsValue(ob: Record, val: unknown): boolea return false; } -export function intersect(arr: Array, ob: string[] | Record): string[] { +export function intersect(arr: Array, ob: K[] | Partial>): K[] { return isArray(ob) ? arrIntersect(arr, ob) : arrIntersectOb(arr, ob); } @@ -168,7 +168,7 @@ export function arrIntersect(arr1: Array, arr2: Array): Array { return result; } -export function arrIntersectOb(arr: Array, ob: Record): string[] { +export function arrIntersectOb(arr: Array, ob: Partial>): K[] { const result = []; for (let i = 0; i < arr.length; i++) { const member = arr[i]; diff --git a/src/platform/nodejs/lib/transport/index.ts b/src/platform/nodejs/lib/transport/index.ts index c5db7c0429..084b0cc955 100644 --- a/src/platform/nodejs/lib/transport/index.ts +++ b/src/platform/nodejs/lib/transport/index.ts @@ -1,7 +1,7 @@ -import TransportName from 'common/constants/TransportName'; +import { TransportNames } from 'common/constants/TransportName'; import initialiseNodeCometTransport from './nodecomettransport'; export default { - order: [TransportName.Comet], - implementations: { [TransportName.Comet]: initialiseNodeCometTransport }, + order: [TransportNames.Comet], + implementations: { [TransportNames.Comet]: initialiseNodeCometTransport }, }; diff --git a/src/platform/nodejs/lib/transport/nodecomettransport.js b/src/platform/nodejs/lib/transport/nodecomettransport.js index f215c97a74..6e6655bb0d 100644 --- a/src/platform/nodejs/lib/transport/nodecomettransport.js +++ b/src/platform/nodejs/lib/transport/nodecomettransport.js @@ -10,11 +10,11 @@ import http from 'http'; import https from 'https'; import url from 'url'; import util from 'util'; -import TransportName from '../../../../common/constants/TransportName'; +import { TransportNames } from '../../../../common/constants/TransportName'; var NodeCometTransport = function (connectionManager) { var noop = function () {}; - var shortName = TransportName.Comet; + var shortName = TransportNames.Comet; /* * A transport to use with nodejs diff --git a/src/platform/nodejs/lib/util/defaults.ts b/src/platform/nodejs/lib/util/defaults.ts index e6b55f18f8..8b29953cbd 100644 --- a/src/platform/nodejs/lib/util/defaults.ts +++ b/src/platform/nodejs/lib/util/defaults.ts @@ -1,14 +1,14 @@ import IDefaults from '../../../../common/types/IDefaults'; -import TransportName from '../../../../common/constants/TransportName'; +import { TransportNames } from '../../../../common/constants/TransportName'; const Defaults: IDefaults = { connectivityCheckUrl: 'https://internet-up.ably-realtime.com/is-the-internet-up.txt', /* Note: order matters here: the base transport is the leftmost one in the * intersection of baseTransportOrder and the transports clientOption that's supported. */ - defaultTransports: [TransportName.WebSocket], - baseTransportOrder: [TransportName.Comet, TransportName.WebSocket], - transportPreferenceOrder: [TransportName.Comet, TransportName.WebSocket], - upgradeTransports: [TransportName.WebSocket], + defaultTransports: [TransportNames.WebSocket], + baseTransportOrder: [TransportNames.Comet, TransportNames.WebSocket], + transportPreferenceOrder: [TransportNames.Comet, TransportNames.WebSocket], + upgradeTransports: [TransportNames.WebSocket], restAgentOptions: { maxSockets: 40, keepAlive: true }, }; diff --git a/src/platform/web/lib/transport/index.ts b/src/platform/web/lib/transport/index.ts index 98674dd0e9..8fb1447186 100644 --- a/src/platform/web/lib/transport/index.ts +++ b/src/platform/web/lib/transport/index.ts @@ -1,11 +1,11 @@ -import TransportName from 'common/constants/TransportName'; +import { TransportNames } from 'common/constants/TransportName'; import initialiseXHRPollingTransport from './xhrpollingtransport'; import initialiseXHRStreamingTransport from './xhrstreamingtransport'; export default { - order: [TransportName.XhrPolling, TransportName.XhrStreaming], + order: [TransportNames.XhrPolling, TransportNames.XhrStreaming], implementations: { - [TransportName.XhrPolling]: initialiseXHRPollingTransport, - [TransportName.XhrStreaming]: initialiseXHRStreamingTransport, + [TransportNames.XhrPolling]: initialiseXHRPollingTransport, + [TransportNames.XhrStreaming]: initialiseXHRStreamingTransport, }, }; diff --git a/src/platform/web/lib/transport/xhrpollingtransport.ts b/src/platform/web/lib/transport/xhrpollingtransport.ts index 5a7dfafd1c..c063397198 100644 --- a/src/platform/web/lib/transport/xhrpollingtransport.ts +++ b/src/platform/web/lib/transport/xhrpollingtransport.ts @@ -4,9 +4,9 @@ import XHRRequest from './xhrrequest'; import ConnectionManager, { TransportParams } from 'common/lib/transport/connectionmanager'; import Auth from 'common/lib/client/auth'; import { RequestParams } from 'common/types/http'; -import TransportName from 'common/constants/TransportName'; +import { TransportNames } from 'common/constants/TransportName'; -var shortName = TransportName.XhrPolling; +var shortName = TransportNames.XhrPolling; class XHRPollingTransport extends CometTransport { shortName = shortName; constructor(connectionManager: ConnectionManager, auth: Auth, params: TransportParams) { diff --git a/src/platform/web/lib/transport/xhrstreamingtransport.ts b/src/platform/web/lib/transport/xhrstreamingtransport.ts index 8640eec37e..57cf30e7b7 100644 --- a/src/platform/web/lib/transport/xhrstreamingtransport.ts +++ b/src/platform/web/lib/transport/xhrstreamingtransport.ts @@ -4,9 +4,9 @@ import XHRRequest from './xhrrequest'; import ConnectionManager, { TransportParams } from 'common/lib/transport/connectionmanager'; import Auth from 'common/lib/client/auth'; import { RequestParams } from 'common/types/http'; -import TransportName from 'common/constants/TransportName'; +import { TransportNames } from 'common/constants/TransportName'; -const shortName = TransportName.XhrStreaming; +const shortName = TransportNames.XhrStreaming; class XHRStreamingTransport extends CometTransport { shortName = shortName; constructor(connectionManager: ConnectionManager, auth: Auth, params: TransportParams) { diff --git a/src/platform/web/lib/util/defaults.ts b/src/platform/web/lib/util/defaults.ts index b182008c18..3dbd549733 100644 --- a/src/platform/web/lib/util/defaults.ts +++ b/src/platform/web/lib/util/defaults.ts @@ -1,15 +1,15 @@ import IDefaults from 'common/types/IDefaults'; -import TransportName from 'common/constants/TransportName'; +import { TransportNames } from 'common/constants/TransportName'; const Defaults: IDefaults = { connectivityCheckUrl: 'https://internet-up.ably-realtime.com/is-the-internet-up.txt', /* Order matters here: the base transport is the leftmost one in the * intersection of baseTransportOrder and the transports clientOption that's * supported. */ - defaultTransports: [TransportName.XhrPolling, TransportName.XhrStreaming, TransportName.WebSocket], - baseTransportOrder: [TransportName.XhrPolling, TransportName.XhrStreaming, TransportName.WebSocket], - transportPreferenceOrder: [TransportName.XhrPolling, TransportName.XhrStreaming, TransportName.WebSocket], - upgradeTransports: [TransportName.XhrStreaming, TransportName.WebSocket], + defaultTransports: [TransportNames.XhrPolling, TransportNames.XhrStreaming, TransportNames.WebSocket], + baseTransportOrder: [TransportNames.XhrPolling, TransportNames.XhrStreaming, TransportNames.WebSocket], + transportPreferenceOrder: [TransportNames.XhrPolling, TransportNames.XhrStreaming, TransportNames.WebSocket], + upgradeTransports: [TransportNames.XhrStreaming, TransportNames.WebSocket], }; export default Defaults;