@stream-io/video-client 0.0.46 → 0.0.47

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.
@@ -18,6 +18,7 @@ export declare class StreamClient {
18
18
  key: string;
19
19
  listeners: Record<string, Array<(event: StreamVideoEvent) => void>>;
20
20
  logger: Logger;
21
+ private locationHint;
21
22
  node: boolean;
22
23
  options: StreamClientOptions;
23
24
  secret?: string;
@@ -56,6 +57,7 @@ export declare class StreamClient {
56
57
  devToken: (userID: string) => string;
57
58
  getAuthType: () => "anonymous" | "jwt";
58
59
  setBaseURL: (baseURL: string) => void;
60
+ getLocationHint: (hintUrl?: string, timeout?: number) => Promise<string>;
59
61
  _getConnectionID: () => string | undefined;
60
62
  _hasConnectionID: () => boolean;
61
63
  /**
@@ -0,0 +1 @@
1
+ export declare const getLocationHint: (hintUrl?: string, timeout?: number) => Promise<string>;
@@ -77,6 +77,14 @@ export type StreamClientOptions = Partial<AxiosRequestConfig> & {
77
77
  enableWSFallback?: boolean;
78
78
  logger?: Logger;
79
79
  logLevel?: LogLevel;
80
+ /**
81
+ * The URL to use for the location hint.
82
+ */
83
+ locationHintUrl?: string;
84
+ /**
85
+ * The default timeout for requesting a location hint.
86
+ */
87
+ locationHintTimeout?: number;
80
88
  /**
81
89
  * When true, user will be persisted on client. Otherwise if `connectUser` call fails, then you need to
82
90
  * call `connectUser` again to retry.
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const version = "0.0.46";
1
+ export declare const version = "0.0.47";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stream-io/video-client",
3
- "version": "0.0.46",
3
+ "version": "0.0.47",
4
4
  "packageManager": "yarn@3.2.4",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.es.js",
@@ -34,6 +34,7 @@ import {
34
34
  } from './types';
35
35
  import { InsightMetrics, postInsights } from './insights';
36
36
  import { version } from '../../../version';
37
+ import { getLocationHint } from './location';
37
38
 
38
39
  export class StreamClient {
39
40
  _user?: UserWithId;
@@ -48,6 +49,8 @@ export class StreamClient {
48
49
  listeners: Record<string, Array<(event: StreamVideoEvent) => void>>;
49
50
  logger: Logger;
50
51
 
52
+ private locationHint: Promise<string> | undefined;
53
+
51
54
  node: boolean;
52
55
  options: StreamClientOptions;
53
56
  secret?: string;
@@ -101,6 +104,13 @@ export class StreamClient {
101
104
  this.browser = inputOptions.browser || typeof window !== 'undefined';
102
105
  this.node = !this.browser;
103
106
 
107
+ if (this.browser) {
108
+ this.locationHint = getLocationHint(
109
+ options?.locationHintUrl,
110
+ options?.locationHintTimeout,
111
+ );
112
+ }
113
+
104
114
  this.options = {
105
115
  timeout: 5000,
106
116
  withCredentials: false, // making sure cookies are not sent
@@ -178,6 +188,21 @@ export class StreamClient {
178
188
  .replace(':3030', ':8800');
179
189
  };
180
190
 
191
+ getLocationHint = async (
192
+ hintUrl?: string,
193
+ timeout?: number,
194
+ ): Promise<string> => {
195
+ const hint = await this.locationHint;
196
+ if (!hint || hint === 'ERR') {
197
+ this.locationHint = getLocationHint(
198
+ hintUrl ?? this.options.locationHintUrl,
199
+ timeout ?? this.options.locationHintTimeout,
200
+ );
201
+ return this.locationHint;
202
+ }
203
+ return hint;
204
+ };
205
+
181
206
  _getConnectionID = () =>
182
207
  this.wsConnection?.connectionID || this.wsFallback?.connectionID;
183
208
 
@@ -0,0 +1,26 @@
1
+ import { getLogger } from '../../logger';
2
+
3
+ const logger = getLogger(['location']);
4
+ const HINT_URL = `https://hint.stream-io-video.com/`;
5
+
6
+ export const getLocationHint = async (
7
+ hintUrl: string = HINT_URL,
8
+ timeout: number = 1500,
9
+ ) => {
10
+ const abortController = new AbortController();
11
+ const timeoutId = setTimeout(() => abortController.abort(), timeout);
12
+ try {
13
+ const response = await fetch(HINT_URL, {
14
+ method: 'HEAD',
15
+ signal: abortController.signal,
16
+ });
17
+ const awsPop = response.headers.get('x-amz-cf-pop') || 'ERR';
18
+ logger('debug', `Location header: ${awsPop}`);
19
+ return awsPop.substring(0, 3); // AMS1-P2 -> AMS
20
+ } catch (e) {
21
+ logger('error', `Failed to get location hint from ${HINT_URL}`, e);
22
+ return 'ERR';
23
+ } finally {
24
+ clearTimeout(timeoutId);
25
+ }
26
+ };
@@ -102,6 +102,14 @@ export type StreamClientOptions = Partial<AxiosRequestConfig> & {
102
102
  enableWSFallback?: boolean;
103
103
  logger?: Logger;
104
104
  logLevel?: LogLevel;
105
+ /**
106
+ * The URL to use for the location hint.
107
+ */
108
+ locationHintUrl?: string;
109
+ /**
110
+ * The default timeout for requesting a location hint.
111
+ */
112
+ locationHintTimeout?: number;
105
113
  /**
106
114
  * When true, user will be persisted on client. Otherwise if `connectUser` call fails, then you need to
107
115
  * call `connectUser` again to retry.
@@ -10,7 +10,6 @@ import {
10
10
  StreamVideoParticipant,
11
11
  } from '../../types';
12
12
  import { StreamClient } from '../../coordinator/connection/client';
13
- import { getLogger } from '../../logger';
14
13
 
15
14
  /**
16
15
  * Collects all necessary information to join a call, talks to the coordinator
@@ -45,7 +44,7 @@ const doJoin = async (
45
44
  id: string,
46
45
  data?: JoinCallData,
47
46
  ) => {
48
- const location = await getLocationHint();
47
+ const location = await httpClient.getLocationHint();
49
48
  const request: JoinCallRequest = {
50
49
  ...data,
51
50
  location,
@@ -75,27 +74,6 @@ const doJoin = async (
75
74
  );
76
75
  };
77
76
 
78
- const getLocationHint = async () => {
79
- const hintURL = `https://hint.stream-io-video.com/`;
80
- const abortController = new AbortController();
81
- const timeoutId = setTimeout(() => abortController.abort(), 1000);
82
- const logger = getLogger(['call']);
83
- try {
84
- const response = await fetch(hintURL, {
85
- method: 'HEAD',
86
- signal: abortController.signal,
87
- });
88
- const awsPop = response.headers.get('x-amz-cf-pop') || 'ERR';
89
- logger?.('info', `Location header: ${awsPop}`);
90
- return awsPop.substring(0, 3); // AMS1-P2 -> AMS
91
- } catch (e) {
92
- logger?.('error', `Failed to get location hint from ${hintURL}`, e);
93
- return 'ERR';
94
- } finally {
95
- clearTimeout(timeoutId);
96
- }
97
- };
98
-
99
77
  const toRtcConfiguration = (config?: ICEServer[]) => {
100
78
  if (!config || config.length === 0) return undefined;
101
79
  const rtcConfig: RTCConfiguration = {