Skip to content

Commit 0a07961

Browse files
committed
feat: rename from ConnectionCredentials -> TokenSource
1 parent 2994946 commit 0a07961

File tree

5 files changed

+48
-48
lines changed

5 files changed

+48
-48
lines changed

examples/demo/demo.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type {
1111
} from '../../src/index';
1212
import {
1313
BackupCodecPolicy,
14-
ConnectionCredentials,
14+
TokenSource,
1515
ConnectionQuality,
1616
ConnectionState,
1717
DisconnectReason,
@@ -165,13 +165,13 @@ const appActions = {
165165
): Promise<Room | undefined> => {
166166
const room = new Room(roomOptions);
167167

168-
const credentials = new ConnectionCredentials.Literal({
168+
const tokenSource = new TokenSource.Literal({
169169
serverUrl: url,
170170
participantToken: token,
171171
});
172172

173173
startTime = Date.now();
174-
await room.prepareConnection(credentials);
174+
await room.prepareConnection(tokenSource);
175175
const prewarmTime = Date.now() - startTime;
176176
appendLog(`prewarmed connection in ${prewarmTime}ms`);
177177
room.localParticipant.on(ParticipantEvent.LocalTrackCpuConstrained, (track, publication) => {
@@ -412,7 +412,7 @@ const appActions = {
412412
reject(error);
413413
}
414414
});
415-
await Promise.all([room.connect(credentials, connectOptions), publishPromise]);
415+
await Promise.all([room.connect(tokenSource, connectOptions), publishPromise]);
416416
const elapsed = Date.now() - startTime;
417417
appendLog(
418418
`successfully connected to ${room.name} in ${Math.round(elapsed)}ms`,

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export * from './room/errors';
5858
export * from './room/events';
5959
export * from './room/track/Track';
6060
export * from './room/track/create';
61-
export { ConnectionCredentials } from './room/ConnectionCredentials';
61+
export { TokenSource } from './room/TokenSource';
6262
export { facingModeFromDeviceLabel, facingModeFromLocalTrack } from './room/track/facingMode';
6363
export * from './room/track/options';
6464
export * from './room/track/processor/types';

src/logger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export enum LogLevel {
1212
export enum LoggerNames {
1313
Default = 'livekit',
1414
Room = 'livekit-room',
15-
ConnectionCredentials = 'livekit-connection-credentials',
15+
TokenSource = 'livekit-connection-credentials',
1616
Participant = 'livekit-participant',
1717
Track = 'livekit-track',
1818
Publication = 'livekit-track-publication',

src/room/Room.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import type {
4242
RoomOptions,
4343
} from '../options';
4444
import { getBrowser } from '../utils/browserParser';
45-
import { ConnectionCredentials } from './ConnectionCredentials';
45+
import { TokenSource } from './TokenSource';
4646
import DeviceManager from './DeviceManager';
4747
import RTCEngine from './RTCEngine';
4848
import { RegionUrlProvider } from './RegionUrlProvider';
@@ -170,7 +170,7 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
170170
/** future holding client initiated connection attempt */
171171
private connectFuture?: Future<void>;
172172

173-
private connectionCredentials?: ConnectionCredentials;
173+
private connectionCredentials?: TokenSource;
174174

175175
private disconnectLock: Mutex;
176176

@@ -578,31 +578,31 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
578578
* With LiveKit Cloud, it will also determine the best edge data center for
579579
* the current client to connect to if a token is provided.
580580
*/
581-
prepareConnection(connectionCredentials: ConnectionCredentials): Promise<void>;
581+
prepareConnection(connectionCredentials: TokenSource): Promise<void>;
582582
prepareConnection(url: string): Promise<void>;
583-
/** @deprecated Use room.prepareConnection(new ConnectionCredentials.Literal({ serverUrl: "url", participantToken: "token" })) instead */
583+
/** @deprecated Use room.prepareConnection(new TokenSource.Literal({ serverUrl: "url", participantToken: "token" })) instead */
584584
prepareConnection(url: string, token?: string): Promise<void>;
585585
async prepareConnection(
586-
urlOrConnectionCredentials: ConnectionCredentials | string,
586+
urlOrTokenSource: TokenSource | string,
587587
tokenOrUnknown?: string | undefined,
588588
) {
589589
let url, token;
590590
if (
591-
urlOrConnectionCredentials instanceof ConnectionCredentials &&
591+
urlOrTokenSource instanceof TokenSource &&
592592
typeof tokenOrUnknown !== 'string'
593593
) {
594-
const result = await urlOrConnectionCredentials.generate();
594+
const result = await urlOrTokenSource.generate();
595595
url = result.serverUrl;
596596
token = result.participantToken;
597597
} else if (
598-
typeof urlOrConnectionCredentials === 'string' &&
598+
typeof urlOrTokenSource === 'string' &&
599599
(typeof tokenOrUnknown === 'string' || typeof tokenOrUnknown === 'undefined')
600600
) {
601-
url = urlOrConnectionCredentials;
601+
url = urlOrTokenSource;
602602
token = tokenOrUnknown;
603603
} else {
604604
throw new Error(
605-
`Room.prepareConnection received invalid parameters - expected url, url/token or connectionCredentials, received ${urlOrConnectionCredentials}, ${tokenOrUnknown}`,
605+
`Room.prepareConnection received invalid parameters - expected url, url/token or connectionCredentials, received ${urlOrTokenSource}, ${tokenOrUnknown}`,
606606
);
607607
}
608608

@@ -630,26 +630,26 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
630630
}
631631

632632
connect: {
633-
(connectionCredentials: ConnectionCredentials, opts?: RoomConnectOptions): Promise<void>;
634-
/** @deprecated Use room.connect(new ConnectionCredentials.Literal({ serverUrl: "url", participantToken: "token" }), opts?: RoomConnectOptions) instead */
633+
(connectionCredentials: TokenSource, opts?: RoomConnectOptions): Promise<void>;
634+
/** @deprecated Use room.connect(new TokenSource.Literal({ serverUrl: "url", participantToken: "token" }), opts?: RoomConnectOptions) instead */
635635
(url: string, token: string, opts?: RoomConnectOptions): Promise<void>;
636-
} = async (urlOrConnectionCredentials, tokenOrOpts, optsOrUnset?: unknown): Promise<void> => {
636+
} = async (urlOrTokenSource, tokenOrOpts, optsOrUnset?: unknown): Promise<void> => {
637637
let opts: RoomConnectOptions = {};
638638
if (
639-
urlOrConnectionCredentials instanceof ConnectionCredentials &&
639+
urlOrTokenSource instanceof TokenSource &&
640640
typeof tokenOrOpts !== 'string'
641641
) {
642-
this.connectionCredentials = urlOrConnectionCredentials;
642+
this.connectionCredentials = urlOrTokenSource;
643643
opts = tokenOrOpts ?? {};
644-
} else if (typeof urlOrConnectionCredentials === 'string' && typeof tokenOrOpts === 'string') {
645-
this.connectionCredentials = new ConnectionCredentials.Literal({
646-
serverUrl: urlOrConnectionCredentials,
644+
} else if (typeof urlOrTokenSource === 'string' && typeof tokenOrOpts === 'string') {
645+
this.connectionCredentials = new TokenSource.Literal({
646+
serverUrl: urlOrTokenSource,
647647
participantToken: tokenOrOpts,
648648
});
649649
opts = optsOrUnset ?? {};
650650
} else {
651651
throw new Error(
652-
`Room.connect received invalid parameters - expected url/token or connectionCredentials, received ${urlOrConnectionCredentials}, ${tokenOrOpts}, ${optsOrUnset}`,
652+
`Room.connect received invalid parameters - expected url/token or connectionCredentials, received ${urlOrTokenSource}, ${tokenOrOpts}, ${optsOrUnset}`,
653653
);
654654
}
655655

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import log, { LoggerNames, getLogger } from '../logger';
55
const ONE_SECOND_IN_MILLISECONDS = 1000;
66
const ONE_MINUTE_IN_MILLISECONDS = 60 * ONE_SECOND_IN_MILLISECONDS;
77

8-
/** ConnectionCredentials handles generating credentials for connecting to a new Room */
9-
export abstract class ConnectionCredentials {
10-
protected cachedResponse: ConnectionCredentials.Response | null = null;
8+
/** TokenSource handles generating credentials for connecting to a new Room */
9+
export abstract class TokenSource {
10+
protected cachedResponse: TokenSource.Response | null = null;
1111

12-
constructor(response: ConnectionCredentials.Response | null = null) {
12+
constructor(response: TokenSource.Response | null = null) {
1313
this.cachedResponse = response;
1414
}
1515

@@ -70,9 +70,9 @@ export abstract class ConnectionCredentials {
7070
return this.getCachedResponseJwtPayload()?.attributes ?? null;
7171
}
7272

73-
abstract generate(): Promise<ConnectionCredentials.Response>;
73+
abstract generate(): Promise<TokenSource.Response>;
7474
}
75-
export namespace ConnectionCredentials {
75+
export namespace TokenSource {
7676
export type Request = {
7777
/** The name of the room being requested when generating credentials */
7878
roomName?: string;
@@ -102,14 +102,14 @@ export namespace ConnectionCredentials {
102102
};
103103

104104
/**
105-
* ConnectionCredentials.Refreshable handles getting credentials for connecting to a new Room from
105+
* TokenSource.Refreshable handles getting credentials for connecting to a new Room from
106106
* an async source, caching them and auto refreshing them if they expire. */
107-
export abstract class Refreshable extends ConnectionCredentials {
108-
private request: ConnectionCredentials.Request = {};
107+
export abstract class Refreshable extends TokenSource {
108+
private request: TokenSource.Request = {};
109109

110-
private inProgressFetch: Promise<ConnectionCredentials.Response> | null = null;
110+
private inProgressFetch: Promise<TokenSource.Response> | null = null;
111111

112-
protected isSameAsCachedRequest(request: ConnectionCredentials.Request) {
112+
protected isSameAsCachedRequest(request: TokenSource.Request) {
113113
if (!this.request) {
114114
return false;
115115
}
@@ -140,8 +140,8 @@ export namespace ConnectionCredentials {
140140
/**
141141
* Store request metadata which will be provide explicitly when fetching new credentials.
142142
*
143-
* @example new ConnectionCredentials.Custom((request /* <= This value! *\/) => ({ serverUrl: "...", participantToken: "..." })) */
144-
setRequest(request: ConnectionCredentials.Request) {
143+
* @example new TokenSource.Custom((request /* <= This value! *\/) => ({ serverUrl: "...", participantToken: "..." })) */
144+
setRequest(request: TokenSource.Request) {
145145
if (!this.isSameAsCachedRequest(request)) {
146146
this.cachedResponse = null;
147147
}
@@ -176,35 +176,35 @@ export namespace ConnectionCredentials {
176176
}
177177

178178
protected abstract fetch(
179-
request: ConnectionCredentials.Request,
180-
): Promise<ConnectionCredentials.Response>;
179+
request: TokenSource.Request,
180+
): Promise<TokenSource.Response>;
181181
}
182182

183-
/** ConnectionCredentials.Literal contains a single, literal set of credentials.
183+
/** TokenSource.Literal contains a single, literal set of credentials.
184184
* Note that refreshing credentials isn't implemented, because there is only one set provided.
185185
* */
186-
export class Literal extends ConnectionCredentials {
186+
export class Literal extends TokenSource {
187187
private log = log;
188188

189189
constructor(payload: Response) {
190190
super(payload);
191-
this.log = getLogger(LoggerNames.ConnectionCredentials);
191+
this.log = getLogger(LoggerNames.TokenSource);
192192
}
193193

194194
async generate() {
195195
if (this.isCachedResponseExpired()) {
196196
this.log.warn(
197-
'The credentials within ConnectionCredentials.Literal have expired, so any upcoming uses of them will likely fail.',
197+
'The credentials within TokenSource.Literal have expired, so any upcoming uses of them will likely fail.',
198198
);
199199
}
200200
return this.cachedResponse!;
201201
}
202202
}
203203

204-
/** ConnectionCredentials.Custom allows a user to define a manual function which generates new
204+
/** TokenSource.Custom allows a user to define a manual function which generates new
205205
* {@link Response} values on demand. Use this to get credentials from custom backends / etc.
206206
* */
207-
export class Custom extends ConnectionCredentials.Refreshable {
207+
export class Custom extends TokenSource.Refreshable {
208208
protected fetch: (request: Request) => Promise<Response>;
209209

210210
constructor(handler: (request: Request) => Promise<Response>) {
@@ -221,14 +221,14 @@ export namespace ConnectionCredentials {
221221
baseUrl?: string;
222222
};
223223

224-
/** ConnectionCredentials.SandboxTokenServer queries a sandbox token server for credentials,
224+
/** TokenSource.SandboxTokenServer queries a sandbox token server for credentials,
225225
* which supports quick prototyping / getting started types of use cases.
226226
*
227227
* This token provider is INSECURE and should NOT be used in production.
228228
*
229229
* For more info:
230230
* @see https://cloud.livekit.io/projects/p_/sandbox/templates/token-server */
231-
export class SandboxTokenServer extends ConnectionCredentials.Refreshable {
231+
export class SandboxTokenServer extends TokenSource.Refreshable {
232232
protected options: SandboxTokenServerOptions;
233233

234234
constructor(options: SandboxTokenServerOptions) {

0 commit comments

Comments
 (0)