Skip to content

Commit 2a9d950

Browse files
vagruchiszuperaz
andauthored
feat: add createSRTCredetials method to StreamCall (#165)
Co-authored-by: Zita Szupera <[email protected]>
1 parent 0d98fe5 commit 2a9d950

File tree

6 files changed

+86
-4
lines changed

6 files changed

+86
-4
lines changed

__tests__/call.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,14 @@ describe('call API', () => {
204204
expect(response.call.settings.backstage.enabled).toBe(true);
205205
});
206206

207+
it('generate SRT credentials', () => {
208+
const creds = call.createSRTCredetials('john');
209+
210+
expect(creds).toBeDefined();
211+
expect(creds.address).toBeDefined();
212+
expect(creds.address).not.toBe('');
213+
});
214+
207215
it('go live', async () => {
208216
const response = await call.goLive();
209217

src/StreamCall.ts

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1-
import { GetOrCreateCallRequest, QueryCallMembersRequest } from './gen/models';
1+
import { VideoApi } from './gen-imports';
2+
import {
3+
CallResponse,
4+
GetOrCreateCallRequest,
5+
QueryCallMembersRequest,
6+
} from './gen/models';
27
import { CallApi } from './gen/video/CallApi';
8+
import { StreamClient } from './StreamClient';
39
import { OmitTypeId } from './types';
410

511
export class StreamCall extends CallApi {
12+
data?: CallResponse;
13+
14+
constructor(
15+
videoApi: VideoApi,
16+
readonly type: string,
17+
readonly id: string,
18+
private readonly streamClient: StreamClient,
19+
) {
20+
super(videoApi, type, id);
21+
}
22+
623
get cid() {
724
return `${this.type}:${this.id}`;
825
}
@@ -16,4 +33,42 @@ export class StreamCall extends CallApi {
1633
...(request ?? {}),
1734
});
1835
};
36+
37+
getOrCreate = async (request?: GetOrCreateCallRequest) => {
38+
const response = await super.getOrCreate(request);
39+
this.data = response.call;
40+
return response;
41+
};
42+
43+
get = async () => {
44+
const response = await super.get();
45+
this.data = response.call;
46+
return response;
47+
};
48+
49+
createSRTCredetials = (
50+
userID: string,
51+
): {
52+
address: string;
53+
} => {
54+
if (!this.data) {
55+
throw new Error(
56+
'Object is not initialized, call get() or getOrCreate() first',
57+
);
58+
}
59+
60+
const token = this.streamClient.generatePermanentUserToken({
61+
user_id: userID,
62+
});
63+
const segments = token.split('.');
64+
if (segments.length !== 3) {
65+
throw new Error('Invalid token format');
66+
}
67+
68+
return {
69+
address: this.data.ingress.srt.address
70+
.replace('{passphrase}', segments[2])
71+
.replace('{token}', token),
72+
};
73+
};
1974
}

src/StreamClient.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,24 @@ export class StreamClient extends CommonApi {
145145
return JWTUserToken(this.secret, payload as UserTokenPayload);
146146
};
147147

148+
/**
149+
*
150+
* @param payload
151+
* - user_id - the id of the user the token is for
152+
* - iat - issued at date of the token, unix timestamp in seconds, by default it's now
153+
*/
154+
generatePermanentUserToken = (
155+
payload: {
156+
user_id: string;
157+
iat?: number;
158+
} & Record<string, unknown>,
159+
) => {
160+
const defaultIat = Math.floor((Date.now() - 1000) / 1000);
161+
payload.iat = payload.iat ?? defaultIat;
162+
163+
return JWTUserToken(this.secret, payload as UserTokenPayload);
164+
};
165+
148166
/**
149167
*
150168
* @param payload

src/StreamVideoClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class StreamVideoClient extends VideoApi {
2525
}
2626

2727
call = (type: string, id: string) => {
28-
return new StreamCall(this, type, id);
28+
return new StreamCall(this, type, id, this.streamClient);
2929
};
3030

3131
connectOpenAi = async (options: {

src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export interface ApiConfig {
77
/** The timeout for requests in milliseconds. The default is 3000. */
88
timeout: number;
99
agent?: RequestInit['dispatcher'];
10+
secret?: string;
1011
}
1112

1213
export interface RequestMetadata {
@@ -39,7 +40,7 @@ export interface RateLimit {
3940

4041
interface BaseTokenPayload {
4142
user_id: string;
42-
exp: number;
43+
exp?: number;
4344
iat: number;
4445
call_cids?: string[];
4546
}

src/utils/create-token.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export function JWTUserToken(
44
apiSecret: Secret,
55
payload: {
66
user_id: string;
7-
exp: number;
7+
exp?: number;
88
iat: number;
99
call_cids?: string[];
1010
} & { [key: string]: any },

0 commit comments

Comments
 (0)