@stream-io/video-client 0.0.8 → 0.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs.js CHANGED
@@ -5696,6 +5696,19 @@ class Publisher {
5696
5696
  return this.notifyTrackMuteStateChanged(undefined, transceiver.sender.track, trackType, true);
5697
5697
  }
5698
5698
  });
5699
+ /**
5700
+ * Returns true if the given track type is currently being published to the SFU.
5701
+ *
5702
+ * @param trackType the track type to check.
5703
+ */
5704
+ this.isPublishing = (trackType) => {
5705
+ const transceiverForTrackType = this.transceiverRegistry[trackType];
5706
+ if (transceiverForTrackType && transceiverForTrackType.sender) {
5707
+ const sender = transceiverForTrackType.sender;
5708
+ return !!sender.track && sender.track.readyState === 'live';
5709
+ }
5710
+ return false;
5711
+ };
5699
5712
  this.notifyTrackMuteStateChanged = (mediaStream, track, trackType, isMuted) => __awaiter(this, void 0, void 0, function* () {
5700
5713
  yield this.sfuClient.updateMuteState(trackType, isMuted);
5701
5714
  const audioOrVideoOrScreenShareStream = trackTypeToParticipantStreamKey(trackType);
@@ -9295,20 +9308,22 @@ class Call {
9295
9308
  createSubscription(this.state.ownCapabilities$, (ownCapabilities) => {
9296
9309
  // update the permission context.
9297
9310
  this.permissionsContext.setPermissions(ownCapabilities);
9311
+ if (!this.publisher)
9312
+ return;
9298
9313
  // check if the user still has publishing permissions and stop publishing if not.
9299
9314
  const permissionToTrackType = {
9300
9315
  [OwnCapability.SEND_AUDIO]: TrackType.AUDIO,
9301
9316
  [OwnCapability.SEND_VIDEO]: TrackType.VIDEO,
9302
9317
  [OwnCapability.SCREENSHARE]: TrackType.SCREEN_SHARE,
9303
9318
  };
9304
- Object.entries(permissionToTrackType).forEach(([permission, type]) => {
9319
+ for (const [permission, trackType] of Object.entries(permissionToTrackType)) {
9305
9320
  const hasPermission = this.permissionsContext.hasPermission(permission);
9306
- if (!hasPermission) {
9307
- this.stopPublish(type).catch((err) => {
9308
- console.error('Error stopping publish', type, err);
9321
+ if (!hasPermission && this.publisher.isPublishing(trackType)) {
9322
+ this.stopPublish(trackType).catch((err) => {
9323
+ console.error('Error stopping publish', trackType, err);
9309
9324
  });
9310
9325
  }
9311
- });
9326
+ }
9312
9327
  }),
9313
9328
  // handles the case when the user is blocked by the call owner.
9314
9329
  createSubscription(this.state.metadata$, (metadata) => __awaiter(this, void 0, void 0, function* () {
@@ -10207,7 +10222,7 @@ class TokenManager {
10207
10222
  return;
10208
10223
  // Don't allow empty token for non-server side client.
10209
10224
  if (!this.secret && !tokenOrProvider) {
10210
- throw new Error('User token can not be empty');
10225
+ throw new Error('UserWithId token can not be empty');
10211
10226
  }
10212
10227
  if (tokenOrProvider &&
10213
10228
  typeof tokenOrProvider !== 'string' &&
@@ -10520,7 +10535,7 @@ class StreamClient {
10520
10535
  /**
10521
10536
  * connectUser - Set the current user and open a WebSocket connection
10522
10537
  *
10523
- * @param {OwnUserResponse} user Data about this user. IE {name: "john"}
10538
+ * @param user Data about this user. IE {name: "john"}
10524
10539
  * @param {TokenOrProvider} userTokenOrProvider Token or provider
10525
10540
  *
10526
10541
  * @return {ConnectAPIResponse} Returns a promise that resolves when the connection is setup
@@ -10597,7 +10612,7 @@ class StreamClient {
10597
10612
  this.openConnection = () => __awaiter(this, void 0, void 0, function* () {
10598
10613
  var _d, _e, _f;
10599
10614
  if (!this.userID) {
10600
- throw Error('User is not set on client, use client.connectUser or client.connectAnonymousUser instead');
10615
+ throw Error('UserWithId is not set on client, use client.connectUser or client.connectAnonymousUser instead');
10601
10616
  }
10602
10617
  if (((_d = this.wsConnection) === null || _d === void 0 ? void 0 : _d.isConnecting) && this.wsPromise) {
10603
10618
  this.logger('info', 'client:openConnection() - connection already in progress', {
@@ -10657,7 +10672,9 @@ class StreamClient {
10657
10672
  });
10658
10673
  this.doAxiosRequest = (type, url, data, options = {}) => __awaiter(this, void 0, void 0, function* () {
10659
10674
  var _g;
10660
- yield this.tokenManager.tokenReady();
10675
+ if (!options.publicEndpoint || this.user) {
10676
+ yield this.tokenManager.tokenReady();
10677
+ }
10661
10678
  const requestConfig = this._enrichAxiosOptions(options);
10662
10679
  try {
10663
10680
  let response;
@@ -11010,7 +11027,7 @@ class StreamClient {
11010
11027
  }
11011
11028
  getUserAgent() {
11012
11029
  return (this.userAgent ||
11013
- `stream-video-javascript-client-${this.node ? 'node' : 'browser'}-${"0.0.7"}`);
11030
+ `stream-video-javascript-client-${this.node ? 'node' : 'browser'}-${"0.0.9"}`);
11014
11031
  }
11015
11032
  setUserAgent(userAgent) {
11016
11033
  this.userAgent = userAgent;
@@ -11021,7 +11038,7 @@ class StreamClient {
11021
11038
  config: {},
11022
11039
  }) {
11023
11040
  var _a;
11024
- const token = this._getToken();
11041
+ const token = options.publicEndpoint && !this.user ? undefined : this._getToken();
11025
11042
  const authorization = token ? { Authorization: token } : undefined;
11026
11043
  let signal = null;
11027
11044
  if (this.nextRequestAbortController !== null) {
@@ -11031,7 +11048,9 @@ class StreamClient {
11031
11048
  if (!((_a = options.headers) === null || _a === void 0 ? void 0 : _a['x-client-request-id'])) {
11032
11049
  options.headers = Object.assign(Object.assign({}, options.headers), { 'x-client-request-id': randomId() });
11033
11050
  }
11034
- return Object.assign(Object.assign(Object.assign({ params: Object.assign({ user_id: this.userID, connection_id: this._getConnectionID(), api_key: this.key }, options.params), headers: Object.assign(Object.assign(Object.assign({}, authorization), { 'stream-auth-type': this.getAuthType(), 'X-Stream-Client': this.getUserAgent() }), options.headers) }, (signal ? { signal } : {})), options.config), this.options.axiosRequestConfig);
11051
+ return Object.assign(Object.assign(Object.assign({ params: Object.assign({ user_id: this.userID, connection_id: this._getConnectionID(), api_key: this.key }, options.params), headers: Object.assign(Object.assign(Object.assign({}, authorization), { 'stream-auth-type': options.publicEndpoint && !this.user
11052
+ ? 'anonymous'
11053
+ : this.getAuthType(), 'X-Stream-Client': this.getUserAgent() }), options.headers) }, (signal ? { signal } : {})), options.config), this.options.axiosRequestConfig);
11035
11054
  }
11036
11055
  _getToken() {
11037
11056
  if (!this.tokenManager)
@@ -11048,7 +11067,7 @@ class StreamClient {
11048
11067
  * createToken - Creates a token to authenticate this user. This function is used server side.
11049
11068
  * The resulting token should be passed to the client side when the users registers or logs in.
11050
11069
  *
11051
- * @param {string} userID The User ID
11070
+ * @param {string} userID The UserWithId ID
11052
11071
  * @param {number} [exp] The expiration time for the token expressed in the number of seconds since the epoch
11053
11072
  * @param call_cids for anonymous tokens you have to provide the call cids the use can join
11054
11073
  *
@@ -11083,108 +11102,6 @@ class StreamVideoClient {
11083
11102
  */
11084
11103
  constructor(apiKey, opts) {
11085
11104
  this.eventHandlersToUnregister = [];
11086
- /**
11087
- * Connects the given user to the client.
11088
- * Only one user can connect at a time, if you want to change users, call `disconnectUser` before connecting a new user.
11089
- * If the connection is successful, the connected user [state variable](#readonlystatestore) will be updated accordingly.
11090
- *
11091
- * @param user the user to connect.
11092
- * @param tokenOrProvider a token or a function that returns a token.
11093
- */
11094
- this.connectUser = (user, tokenOrProvider) => __awaiter(this, void 0, void 0, function* () {
11095
- var _a;
11096
- const connectUser = () => {
11097
- return this.streamClient.connectUser(
11098
- // @ts-expect-error
11099
- user, tokenOrProvider);
11100
- };
11101
- this.connectionPromise = this.disconnectionPromise
11102
- ? this.disconnectionPromise.then(() => connectUser())
11103
- : connectUser();
11104
- (_a = this.connectionPromise) === null || _a === void 0 ? void 0 : _a.finally(() => (this.connectionPromise = undefined));
11105
- const connectUserResponse = yield this.connectionPromise;
11106
- this.writeableStateStore.setConnectedUser(user);
11107
- this.eventHandlersToUnregister.push(this.on('connection.changed', (e) => {
11108
- const event = e;
11109
- if (event.online) {
11110
- const callsToReWatch = this.writeableStateStore.calls
11111
- .filter((call) => call.watching)
11112
- .map((call) => call.cid);
11113
- if (callsToReWatch.length > 0) {
11114
- this.queryCalls({
11115
- watch: true,
11116
- filter_conditions: {
11117
- cid: { $in: callsToReWatch },
11118
- },
11119
- sort: [{ field: 'cid', direction: 1 }],
11120
- }).catch((err) => {
11121
- console.warn('Failed to re-watch calls', err);
11122
- });
11123
- }
11124
- }
11125
- }));
11126
- this.eventHandlersToUnregister.push(this.on('call.created', (event) => {
11127
- if (event.type !== 'call.created')
11128
- return;
11129
- const { call, members } = event;
11130
- if (user.id === call.created_by.id) {
11131
- console.warn('Received `call.created` sent by the current user');
11132
- return;
11133
- }
11134
- this.writeableStateStore.registerCall(new Call({
11135
- streamClient: this.streamClient,
11136
- type: call.type,
11137
- id: call.id,
11138
- metadata: call,
11139
- members,
11140
- clientStore: this.writeableStateStore,
11141
- }));
11142
- }));
11143
- this.eventHandlersToUnregister.push(this.on('call.ring', (event) => __awaiter(this, void 0, void 0, function* () {
11144
- if (event.type !== 'call.ring')
11145
- return;
11146
- const { call, members } = event;
11147
- if (user.id === call.created_by.id) {
11148
- console.warn('Received `call.ring` sent by the current user');
11149
- return;
11150
- }
11151
- // The call might already be tracked by the client,
11152
- // if `call.created` was received before `call.ring`.
11153
- // In that case, we just reuse the already tracked call.
11154
- let theCall = this.writeableStateStore.findCall(call.type, call.id);
11155
- if (!theCall) {
11156
- // otherwise, we create a new call
11157
- theCall = new Call({
11158
- streamClient: this.streamClient,
11159
- type: call.type,
11160
- id: call.id,
11161
- members,
11162
- clientStore: this.writeableStateStore,
11163
- ringing: true,
11164
- });
11165
- }
11166
- // we fetch the latest metadata for the call from the server
11167
- yield theCall.get({ ring: true });
11168
- this.writeableStateStore.registerCall(theCall);
11169
- })));
11170
- return connectUserResponse;
11171
- });
11172
- /**
11173
- * Connects the given anonymous user to the client.
11174
- *
11175
- * @param user the user to connect.
11176
- * @param tokenOrProvider a token or a function that returns a token.
11177
- */
11178
- this.connectAnonymousUser = (user, tokenOrProvider) => __awaiter(this, void 0, void 0, function* () {
11179
- const connectAnonymousUser = () =>
11180
- // @ts-expect-error
11181
- this.streamClient.connectAnonymousUser(user, tokenOrProvider);
11182
- this.connectionPromise = this.disconnectionPromise
11183
- ? this.disconnectionPromise.then(() => connectAnonymousUser())
11184
- : connectAnonymousUser();
11185
- this.connectionPromise.finally(() => (this.connectionPromise = undefined));
11186
- return this.connectionPromise;
11187
- });
11188
11105
  /**
11189
11106
  * Disconnects the currently connected user from the client.
11190
11107
  *
@@ -11245,7 +11162,7 @@ class StreamVideoClient {
11245
11162
  * @param data the data for the guest user.
11246
11163
  */
11247
11164
  this.createGuestUser = (data) => __awaiter(this, void 0, void 0, function* () {
11248
- return this.streamClient.post('/guest', data);
11165
+ return this.streamClient.doAxiosRequest('post', '/guest', data, { publicEndpoint: true });
11249
11166
  });
11250
11167
  /**
11251
11168
  * Will query the API for calls matching the given filters.
@@ -11326,10 +11243,123 @@ class StreamVideoClient {
11326
11243
  this.removeDevice = (id, userID) => __awaiter(this, void 0, void 0, function* () {
11327
11244
  return yield this.streamClient.delete('/devices', Object.assign({ id }, (userID ? { user_id: userID } : {})));
11328
11245
  });
11246
+ /**
11247
+ * Connects the given anonymous user to the client.
11248
+ *
11249
+ * @param user the user to connect.
11250
+ * @param tokenOrProvider a token or a function that returns a token.
11251
+ */
11252
+ this.connectAnonymousUser = (user, tokenOrProvider) => __awaiter(this, void 0, void 0, function* () {
11253
+ const connectAnonymousUser = () => this.streamClient.connectAnonymousUser(user, tokenOrProvider);
11254
+ this.connectionPromise = this.disconnectionPromise
11255
+ ? this.disconnectionPromise.then(() => connectAnonymousUser())
11256
+ : connectAnonymousUser();
11257
+ this.connectionPromise.finally(() => (this.connectionPromise = undefined));
11258
+ return this.connectionPromise;
11259
+ });
11329
11260
  this.streamClient = new StreamClient(apiKey, Object.assign({ persistUserOnConnectionFailure: true }, opts));
11330
11261
  this.writeableStateStore = new StreamVideoWriteableStateStore();
11331
11262
  this.readOnlyStateStore = new StreamVideoReadOnlyStateStore(this.writeableStateStore);
11332
11263
  }
11264
+ /**
11265
+ * Connects the given user to the client.
11266
+ * Only one user can connect at a time, if you want to change users, call `disconnectUser` before connecting a new user.
11267
+ * If the connection is successful, the connected user [state variable](#readonlystatestore) will be updated accordingly.
11268
+ *
11269
+ * @param user the user to connect.
11270
+ * @param tokenOrProvider a token or a function that returns a token.
11271
+ */
11272
+ connectUser(user, token) {
11273
+ var _a;
11274
+ return __awaiter(this, void 0, void 0, function* () {
11275
+ if (user.type === 'anonymous') {
11276
+ user.id = '!anon';
11277
+ return this.connectAnonymousUser(user, token);
11278
+ }
11279
+ if (user.type === 'guest') {
11280
+ const response = yield this.createGuestUser({
11281
+ user: Object.assign(Object.assign({}, user), { role: 'guest' }),
11282
+ });
11283
+ return this.connectUser(response.user, response.access_token);
11284
+ }
11285
+ const connectUser = () => {
11286
+ return this.streamClient.connectUser(user, token);
11287
+ };
11288
+ this.connectionPromise = this.disconnectionPromise
11289
+ ? this.disconnectionPromise.then(() => connectUser())
11290
+ : connectUser();
11291
+ (_a = this.connectionPromise) === null || _a === void 0 ? void 0 : _a.finally(() => (this.connectionPromise = undefined));
11292
+ const connectUserResponse = yield this.connectionPromise;
11293
+ // connectUserResponse will be void if connectUser called twice for the same user
11294
+ if (connectUserResponse === null || connectUserResponse === void 0 ? void 0 : connectUserResponse.me) {
11295
+ this.writeableStateStore.setConnectedUser(connectUserResponse.me);
11296
+ }
11297
+ this.eventHandlersToUnregister.push(this.on('connection.changed', (e) => {
11298
+ const event = e;
11299
+ if (event.online) {
11300
+ const callsToReWatch = this.writeableStateStore.calls
11301
+ .filter((call) => call.watching)
11302
+ .map((call) => call.cid);
11303
+ if (callsToReWatch.length > 0) {
11304
+ this.queryCalls({
11305
+ watch: true,
11306
+ filter_conditions: {
11307
+ cid: { $in: callsToReWatch },
11308
+ },
11309
+ sort: [{ field: 'cid', direction: 1 }],
11310
+ }).catch((err) => {
11311
+ console.warn('Failed to re-watch calls', err);
11312
+ });
11313
+ }
11314
+ }
11315
+ }));
11316
+ this.eventHandlersToUnregister.push(this.on('call.created', (event) => {
11317
+ if (event.type !== 'call.created')
11318
+ return;
11319
+ const { call, members } = event;
11320
+ if (user.id === call.created_by.id) {
11321
+ console.warn('Received `call.created` sent by the current user');
11322
+ return;
11323
+ }
11324
+ this.writeableStateStore.registerCall(new Call({
11325
+ streamClient: this.streamClient,
11326
+ type: call.type,
11327
+ id: call.id,
11328
+ metadata: call,
11329
+ members,
11330
+ clientStore: this.writeableStateStore,
11331
+ }));
11332
+ }));
11333
+ this.eventHandlersToUnregister.push(this.on('call.ring', (event) => __awaiter(this, void 0, void 0, function* () {
11334
+ if (event.type !== 'call.ring')
11335
+ return;
11336
+ const { call, members } = event;
11337
+ if (user.id === call.created_by.id) {
11338
+ console.warn('Received `call.ring` sent by the current user');
11339
+ return;
11340
+ }
11341
+ // The call might already be tracked by the client,
11342
+ // if `call.created` was received before `call.ring`.
11343
+ // In that case, we just reuse the already tracked call.
11344
+ let theCall = this.writeableStateStore.findCall(call.type, call.id);
11345
+ if (!theCall) {
11346
+ // otherwise, we create a new call
11347
+ theCall = new Call({
11348
+ streamClient: this.streamClient,
11349
+ type: call.type,
11350
+ id: call.id,
11351
+ members,
11352
+ clientStore: this.writeableStateStore,
11353
+ ringing: true,
11354
+ });
11355
+ }
11356
+ // we fetch the latest metadata for the call from the server
11357
+ yield theCall.get({ ring: true });
11358
+ this.writeableStateStore.registerCall(theCall);
11359
+ })));
11360
+ return connectUserResponse;
11361
+ });
11362
+ }
11333
11363
  /**
11334
11364
  * addDevice - Adds a push device for a user.
11335
11365
  *