Skip to content

Commit b03cefe

Browse files
committed
feat: rename from ConnectionCredentials -> TokenSource
1 parent 9dbf893 commit b03cefe

File tree

5 files changed

+47
-55
lines changed

5 files changed

+47
-55
lines changed

examples/demo/demo.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import type {
1111
} from '../../src/index';
1212
import {
1313
BackupCodecPolicy,
14-
ConnectionCredentials,
1514
ConnectionQuality,
1615
ConnectionState,
1716
DisconnectReason,
@@ -25,6 +24,7 @@ import {
2524
Room,
2625
RoomEvent,
2726
ScreenSharePresets,
27+
TokenSource,
2828
Track,
2929
TrackPublication,
3030
VideoPresets,
@@ -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) => {
@@ -413,7 +413,7 @@ const appActions = {
413413
reject(error);
414414
}
415415
});
416-
await Promise.all([room.connect(credentials, connectOptions), publishPromise]);
416+
await Promise.all([room.connect(tokenSource, connectOptions), publishPromise]);
417417
const elapsed = Date.now() - startTime;
418418
appendLog(
419419
`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
@@ -66,7 +66,7 @@ export * from './room/errors';
6666
export * from './room/events';
6767
export * from './room/track/Track';
6868
export * from './room/track/create';
69-
export { ConnectionCredentials } from './room/ConnectionCredentials';
69+
export { TokenSource } from './room/TokenSource';
7070
export { facingModeFromDeviceLabel, facingModeFromLocalTrack } from './room/track/facingMode';
7171
export * from './room/track/options';
7272
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 & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ import type {
4343
RoomOptions,
4444
} from '../options';
4545
import { getBrowser } from '../utils/browserParser';
46-
import { ConnectionCredentials } from './ConnectionCredentials';
4746
import DeviceManager from './DeviceManager';
4847
import RTCEngine from './RTCEngine';
4948
import { RegionUrlProvider } from './RegionUrlProvider';
49+
import { TokenSource } from './TokenSource';
5050
import IncomingDataStreamManager from './data-stream/incoming/IncomingDataStreamManager';
5151
import {
5252
type ByteStreamHandler,
@@ -171,7 +171,7 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
171171
/** future holding client initiated connection attempt */
172172
private connectFuture?: Future<void>;
173173

174-
private connectionCredentials?: ConnectionCredentials;
174+
private connectionCredentials?: TokenSource;
175175

176176
private disconnectLock: Mutex;
177177

@@ -591,31 +591,28 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
591591
* With LiveKit Cloud, it will also determine the best edge data center for
592592
* the current client to connect to if a token is provided.
593593
*/
594-
prepareConnection(connectionCredentials: ConnectionCredentials): Promise<void>;
594+
prepareConnection(connectionCredentials: TokenSource): Promise<void>;
595595
prepareConnection(url: string): Promise<void>;
596-
/** @deprecated Use room.prepareConnection(new ConnectionCredentials.Literal({ serverUrl: "url", participantToken: "token" })) instead */
596+
/** @deprecated Use room.prepareConnection(new TokenSource.Literal({ serverUrl: "url", participantToken: "token" })) instead */
597597
prepareConnection(url: string, token?: string): Promise<void>;
598598
async prepareConnection(
599-
urlOrConnectionCredentials: ConnectionCredentials | string,
599+
urlOrTokenSource: TokenSource | string,
600600
tokenOrUnknown?: string | undefined,
601601
) {
602602
let url, token;
603-
if (
604-
urlOrConnectionCredentials instanceof ConnectionCredentials &&
605-
typeof tokenOrUnknown !== 'string'
606-
) {
607-
const result = await urlOrConnectionCredentials.generate();
603+
if (urlOrTokenSource instanceof TokenSource && typeof tokenOrUnknown !== 'string') {
604+
const result = await urlOrTokenSource.generate();
608605
url = result.serverUrl;
609606
token = result.participantToken;
610607
} else if (
611-
typeof urlOrConnectionCredentials === 'string' &&
608+
typeof urlOrTokenSource === 'string' &&
612609
(typeof tokenOrUnknown === 'string' || typeof tokenOrUnknown === 'undefined')
613610
) {
614-
url = urlOrConnectionCredentials;
611+
url = urlOrTokenSource;
615612
token = tokenOrUnknown;
616613
} else {
617614
throw new Error(
618-
`Room.prepareConnection received invalid parameters - expected url, url/token or connectionCredentials, received ${urlOrConnectionCredentials}, ${tokenOrUnknown}`,
615+
`Room.prepareConnection received invalid parameters - expected url, url/token or connectionCredentials, received ${urlOrTokenSource}, ${tokenOrUnknown}`,
619616
);
620617
}
621618

@@ -643,26 +640,23 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
643640
}
644641

645642
connect: {
646-
(connectionCredentials: ConnectionCredentials, opts?: RoomConnectOptions): Promise<void>;
647-
/** @deprecated Use room.connect(new ConnectionCredentials.Literal({ serverUrl: "url", participantToken: "token" }), opts?: RoomConnectOptions) instead */
643+
(connectionCredentials: TokenSource, opts?: RoomConnectOptions): Promise<void>;
644+
/** @deprecated Use room.connect(new TokenSource.Literal({ serverUrl: "url", participantToken: "token" }), opts?: RoomConnectOptions) instead */
648645
(url: string, token: string, opts?: RoomConnectOptions): Promise<void>;
649-
} = async (urlOrConnectionCredentials, tokenOrOpts, optsOrUnset?: unknown): Promise<void> => {
646+
} = async (urlOrTokenSource, tokenOrOpts, optsOrUnset?: unknown): Promise<void> => {
650647
let opts: RoomConnectOptions = {};
651-
if (
652-
urlOrConnectionCredentials instanceof ConnectionCredentials &&
653-
typeof tokenOrOpts !== 'string'
654-
) {
655-
this.connectionCredentials = urlOrConnectionCredentials;
648+
if (urlOrTokenSource instanceof TokenSource && typeof tokenOrOpts !== 'string') {
649+
this.connectionCredentials = urlOrTokenSource;
656650
opts = tokenOrOpts ?? {};
657-
} else if (typeof urlOrConnectionCredentials === 'string' && typeof tokenOrOpts === 'string') {
658-
this.connectionCredentials = new ConnectionCredentials.Literal({
659-
serverUrl: urlOrConnectionCredentials,
651+
} else if (typeof urlOrTokenSource === 'string' && typeof tokenOrOpts === 'string') {
652+
this.connectionCredentials = new TokenSource.Literal({
653+
serverUrl: urlOrTokenSource,
660654
participantToken: tokenOrOpts,
661655
});
662656
opts = optsOrUnset ?? {};
663657
} else {
664658
throw new Error(
665-
`Room.connect received invalid parameters - expected url/token or connectionCredentials, received ${urlOrConnectionCredentials}, ${tokenOrOpts}, ${optsOrUnset}`,
659+
`Room.connect received invalid parameters - expected url/token or connectionCredentials, received ${urlOrTokenSource}, ${tokenOrOpts}, ${optsOrUnset}`,
666660
);
667661
}
668662

Lines changed: 22 additions & 24 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
}
@@ -175,36 +175,34 @@ export namespace ConnectionCredentials {
175175
}
176176
}
177177

178-
protected abstract fetch(
179-
request: ConnectionCredentials.Request,
180-
): Promise<ConnectionCredentials.Response>;
178+
protected abstract fetch(request: TokenSource.Request): Promise<TokenSource.Response>;
181179
}
182180

183-
/** ConnectionCredentials.Literal contains a single, literal set of credentials.
181+
/** TokenSource.Literal contains a single, literal set of credentials.
184182
* Note that refreshing credentials isn't implemented, because there is only one set provided.
185183
* */
186-
export class Literal extends ConnectionCredentials {
184+
export class Literal extends TokenSource {
187185
private log = log;
188186

189187
constructor(payload: Response) {
190188
super(payload);
191-
this.log = getLogger(LoggerNames.ConnectionCredentials);
189+
this.log = getLogger(LoggerNames.TokenSource);
192190
}
193191

194192
async generate() {
195193
if (this.isCachedResponseExpired()) {
196194
this.log.warn(
197-
'The credentials within ConnectionCredentials.Literal have expired, so any upcoming uses of them will likely fail.',
195+
'The credentials within TokenSource.Literal have expired, so any upcoming uses of them will likely fail.',
198196
);
199197
}
200198
return this.cachedResponse!;
201199
}
202200
}
203201

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

210208
constructor(handler: (request: Request) => Promise<Response>) {
@@ -221,14 +219,14 @@ export namespace ConnectionCredentials {
221219
baseUrl?: string;
222220
};
223221

224-
/** ConnectionCredentials.SandboxTokenServer queries a sandbox token server for credentials,
222+
/** TokenSource.SandboxTokenServer queries a sandbox token server for credentials,
225223
* which supports quick prototyping / getting started types of use cases.
226224
*
227225
* This token provider is INSECURE and should NOT be used in production.
228226
*
229227
* For more info:
230228
* @see https://cloud.livekit.io/projects/p_/sandbox/templates/token-server */
231-
export class SandboxTokenServer extends ConnectionCredentials.Refreshable {
229+
export class SandboxTokenServer extends TokenSource.Refreshable {
232230
protected options: SandboxTokenServerOptions;
233231

234232
constructor(options: SandboxTokenServerOptions) {

0 commit comments

Comments
 (0)