stream-chat 8.60.0 → 8.61.0

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/src/client.ts CHANGED
@@ -32,6 +32,7 @@ import {
32
32
  import {
33
33
  APIErrorResponse,
34
34
  APIResponse,
35
+ AppIdentifier,
35
36
  AppSettings,
36
37
  AppSettingsAPIResponse,
37
38
  BannedUsersFilters,
@@ -283,6 +284,8 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
283
284
  defaultWSTimeout: number;
284
285
  sdkIdentifier?: SdkIdentifier;
285
286
  deviceIdentifier?: DeviceIdentifier;
287
+ appIdentifier?: AppIdentifier;
288
+ private cachedUserAgent?: string;
286
289
  private nextRequestAbortController: AbortController | null = null;
287
290
 
288
291
  /**
@@ -2945,36 +2948,42 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
2945
2948
  );
2946
2949
  }
2947
2950
 
2948
- getUserAgent() {
2951
+ getUserAgent = (): string => {
2952
+ // An explicit override (deprecated `setUserAgent`) always wins and is never cached.
2949
2953
  if (this.userAgent) {
2950
2954
  return this.userAgent;
2951
2955
  }
2952
2956
 
2953
- const version = process.env.PKG_VERSION;
2954
- const clientBundle = process.env.CLIENT_BUNDLE;
2955
-
2956
- let userAgentString = '';
2957
- if (this.sdkIdentifier) {
2958
- userAgentString = `stream-chat-${this.sdkIdentifier.name}-v${this.sdkIdentifier.version}-llc-v${version}`;
2959
- } else {
2960
- userAgentString = `stream-chat-js-v${version}-${this.node ? 'node' : 'browser'}`;
2957
+ // Computed once, then memoized for the client's lifetime - inputs read on
2958
+ // the first call (sdk/app/device identifiers, build-time env) are not re-read.
2959
+ if (!this.cachedUserAgent) {
2960
+ const version = process.env.PKG_VERSION;
2961
+ const { name: sdkName, version: sdkVersion } = this.sdkIdentifier ?? {};
2962
+ const { name: appName, version: appVersion } = this.appIdentifier ?? {};
2963
+ const { os, model: deviceModel } = this.deviceIdentifier ?? {};
2964
+
2965
+ const head = sdkName
2966
+ ? `stream-chat-${sdkName}-v${sdkVersion}-llc-v${version}`
2967
+ : `stream-chat-js-v${version}-${this.node ? 'node' : 'browser'}`;
2968
+
2969
+ this.cachedUserAgent = [
2970
+ head,
2971
+ // reports the host app, the device OS, the device model, and the picked
2972
+ // exports bundle, each only when a value is present
2973
+ ...Object.entries({
2974
+ app: appName,
2975
+ app_version: appVersion,
2976
+ os,
2977
+ device_model: deviceModel,
2978
+ client_bundle: process.env.CLIENT_BUNDLE,
2979
+ })
2980
+ .filter(([, value]) => value && value.length > 0)
2981
+ .map(([key, value]) => `${key}=${value}`),
2982
+ ].join('|');
2961
2983
  }
2962
2984
 
2963
- const { os, model } = this.deviceIdentifier ?? {};
2964
-
2965
- return ([
2966
- // reports the device OS, if provided
2967
- ['os', os],
2968
- // reports the device model, if provided
2969
- ['device_model', model],
2970
- // reports which bundle is being picked from the exports
2971
- ['client_bundle', clientBundle],
2972
- ] as const).reduce(
2973
- (withArguments, [key, value]) =>
2974
- value && value.length > 0 ? withArguments.concat(`|${key}=${value}`) : withArguments,
2975
- userAgentString,
2976
- );
2977
- }
2985
+ return this.cachedUserAgent;
2986
+ };
2978
2987
 
2979
2988
  /**
2980
2989
  * @deprecated use sdkIdentifier instead
package/src/types.ts CHANGED
@@ -3861,6 +3861,13 @@ export type SdkIdentifier = { name: 'react' | 'react-native' | 'expo' | 'angular
3861
3861
  */
3862
3862
  export type DeviceIdentifier = { os: string; model?: string };
3863
3863
 
3864
+ /**
3865
+ * An identifier containing information about the downstream application integrating
3866
+ * stream-chat, if available. `name` is reported as `app` and `version` as `app_version`
3867
+ * in the user agent. Distinct from the SDK and device identifiers.
3868
+ */
3869
+ export type AppIdentifier = { name: string; version?: string };
3870
+
3864
3871
  export type DraftResponse<StreamChatGenerics extends ExtendableGenerics = DefaultGenerics> = {
3865
3872
  channel_cid: string;
3866
3873
  created_at: string;