Skip to content

Commit

Permalink
Tighten up type in places where transport name is used
Browse files Browse the repository at this point in the history
  • Loading branch information
lawrence-forooghian committed Sep 14, 2023
1 parent dbdead7 commit 8a9faaa
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 41 deletions.
16 changes: 11 additions & 5 deletions src/common/constants/TransportName.ts
Original file line number Diff line number Diff line change
@@ -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;
19 changes: 10 additions & 9 deletions src/common/lib/transport/connectionmanager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -98,7 +99,7 @@ function decodeRecoveryKey(recoveryKey: NormalisedClientOptions['recover']): Rec
}
}

const supportedTransports: Partial<Record<string, TransportCtor>> = {};
const supportedTransports: Partial<Record<TransportName, TransportCtor>> = {};

export class TransportParams {
options: NormalisedClientOptions;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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<string, any>): void {
Expand All @@ -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);
Expand Down Expand Up @@ -2101,7 +2102,7 @@ class ConnectionManager extends EventEmitter {
this.proposedTransports.push(transport);
}

getTransportPreference(): string {
getTransportPreference(): TransportName {
return this.transportPreference || (haveWebStorage() && Platform.WebStorage?.get?.(transportPreferenceName));
}

Expand Down
4 changes: 2 additions & 2 deletions src/common/lib/transport/websockettransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/common/lib/util/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export function containsValue(ob: Record<string, unknown>, val: unknown): boolea
return false;
}

export function intersect<T>(arr: Array<string>, ob: string[] | Record<string, T>): string[] {
export function intersect<K extends string, T>(arr: Array<K>, ob: K[] | Partial<Record<K, T>>): K[] {
return isArray(ob) ? arrIntersect(arr, ob) : arrIntersectOb(arr, ob);
}

Expand All @@ -168,7 +168,7 @@ export function arrIntersect<T>(arr1: Array<T>, arr2: Array<T>): Array<T> {
return result;
}

export function arrIntersectOb(arr: Array<string>, ob: Record<string, unknown>): string[] {
export function arrIntersectOb<K extends string>(arr: Array<K>, ob: Partial<Record<K, unknown>>): K[] {
const result = [];
for (let i = 0; i < arr.length; i++) {
const member = arr[i];
Expand Down
6 changes: 3 additions & 3 deletions src/platform/nodejs/lib/transport/index.ts
Original file line number Diff line number Diff line change
@@ -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 },
};
4 changes: 2 additions & 2 deletions src/platform/nodejs/lib/transport/nodecomettransport.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/platform/nodejs/lib/util/defaults.ts
Original file line number Diff line number Diff line change
@@ -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 },
};

Expand Down
8 changes: 4 additions & 4 deletions src/platform/web/lib/transport/index.ts
Original file line number Diff line number Diff line change
@@ -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,
},
};
4 changes: 2 additions & 2 deletions src/platform/web/lib/transport/xhrpollingtransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/platform/web/lib/transport/xhrstreamingtransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions src/platform/web/lib/util/defaults.ts
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 8a9faaa

Please sign in to comment.