@arena-im/buzz-client 1.4.0 → 1.5.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/lib/config.ts CHANGED
@@ -25,15 +25,11 @@ const DEFAULTS: Record<BuzzEnvironment, BuzzEnvironmentConfig> = {
25
25
  websocketUrl:
26
26
  BUZZ_WEBSOCKET_URL_DEV && BUZZ_WEBSOCKET_URL_DEV.length > 0
27
27
  ? BUZZ_WEBSOCKET_URL_DEV
28
- : BUZZ_WEBSOCKET_URL && BUZZ_WEBSOCKET_URL.length > 0
29
- ? BUZZ_WEBSOCKET_URL
30
- : "wss://buzz-prd.arena.im/ws",
28
+ : "wss://buzz-dev.arena.im/ws",
31
29
  identityUrl:
32
30
  IDENTITY_SERVICE_URL_DEV && IDENTITY_SERVICE_URL_DEV.length > 0
33
31
  ? IDENTITY_SERVICE_URL_DEV
34
- : IDENTITY_SERVICE_URL && IDENTITY_SERVICE_URL.length > 0
35
- ? IDENTITY_SERVICE_URL
36
- : "https://token-service-prd.arena.im",
32
+ : "https://token-service-dev.arena.im",
37
33
  },
38
34
  };
39
35
 
package/src/lib/index.ts CHANGED
@@ -7,7 +7,10 @@ import { TokenManager } from "./token-manager";
7
7
 
8
8
  import { Topic } from "./topic";
9
9
  import { BuzzEnvironment, resolveBuzzConfig } from "./config";
10
- import { TOKEN_UPDATED_EVENT, TokenUpdateEvent } from "./utils/dom-events";
10
+ import {
11
+ dispatchTokenUpdatedEvent,
12
+ TokenType,
13
+ } from "./utils/dom-events";
11
14
 
12
15
  // These should match buzz' throttling constants
13
16
  const throttleWindowDuration = 5000;
@@ -38,6 +41,7 @@ interface Message {
38
41
  topic?: string;
39
42
  reason?: string;
40
43
  }
44
+
41
45
  type ClientOptions = {
42
46
  commandTimeout?: number;
43
47
  websocket?: WebsocketOptions;
@@ -57,7 +61,6 @@ export class BuzzClient {
57
61
  eventListeners: Record<string, EventCallbackRegistry> = {};
58
62
  queue;
59
63
  private subscribedTopics = new Set<string>();
60
- private unsubscribeTokenChange: (() => void) | null = null;
61
64
  private readonly websocketUrl: string;
62
65
  private readonly tokenManager: TokenManager;
63
66
  private readonly namespace: string;
@@ -98,19 +101,6 @@ export class BuzzClient {
98
101
 
99
102
  this.namespace = namespace;
100
103
  this.options = options;
101
- this.setupIdTokenListener();
102
- }
103
-
104
- private setupIdTokenListener(): void {
105
- // Only listen to browser events if running in a browser environment
106
- if (typeof window !== "undefined") {
107
- document.addEventListener(TOKEN_UPDATED_EVENT, ((
108
- event: CustomEvent<TokenUpdateEvent["detail"]>,
109
- ) => {
110
- const { idToken } = event.detail;
111
- this.setToken(idToken);
112
- }) as EventListener);
113
- }
114
104
  }
115
105
 
116
106
  private async getWebsocketUrl(): Promise<string> {
@@ -134,9 +124,16 @@ export class BuzzClient {
134
124
 
135
125
  async setTemporaryUser(username: string): Promise<string> {
136
126
  const buzzToken = await this.tokenManager.setTemporaryUser(username);
127
+
128
+ const idToken = this.tokenManager.getIdToken();
129
+ if (idToken) {
130
+ dispatchTokenUpdatedEvent(TokenType.GUEST, { idToken });
131
+ }
132
+
137
133
  if (this.ws) this.ws.close();
138
134
  this.ws = await this.createSocket();
139
135
  await this.ws.whenOpen();
136
+
140
137
  return buzzToken;
141
138
  }
142
139
 
@@ -261,10 +258,6 @@ export class BuzzClient {
261
258
  this.ws.close();
262
259
  this.ws = null;
263
260
  }
264
- if (this.unsubscribeTokenChange) {
265
- this.unsubscribeTokenChange();
266
- this.unsubscribeTokenChange = null;
267
- }
268
261
  this.queue.destroy();
269
262
  this.pendingCommands = {};
270
263
  this.eventListeners = {};
@@ -72,13 +72,13 @@ export class TokenManager {
72
72
  throw new Error("getTemporaryUserToken failed: missing token");
73
73
  }
74
74
  if (typeof parsed === "string" && parsed.trim().length > 0) {
75
- return parsed;
75
+ return parsed.trim();
76
76
  }
77
77
  if (parsed && typeof parsed === "object") {
78
78
  const maybeToken =
79
79
  (parsed as { token?: string }).token ??
80
80
  (parsed as { access_token?: string }).access_token;
81
- if (maybeToken && maybeToken.length > 0) return maybeToken;
81
+ if (maybeToken && maybeToken.length > 0) return maybeToken.trim();
82
82
  }
83
83
  throw new Error("getTemporaryUserToken failed: missing token");
84
84
  }
@@ -95,6 +95,10 @@ export class TokenManager {
95
95
  return this.exchange();
96
96
  }
97
97
 
98
+ public getIdToken(): string | null {
99
+ return this.currentIdToken ?? null;
100
+ }
101
+
98
102
  public async setTemporaryUser(username: string): Promise<string> {
99
103
  const tempToken = await this.getTemporaryUserToken(username);
100
104
  this.currentIdToken = tempToken;
@@ -15,3 +15,24 @@ export interface TokenUpdateEvent {
15
15
  source?: string;
16
16
  };
17
17
  }
18
+
19
+ interface TokenEventParams {
20
+ idToken: string | null;
21
+ refreshToken?: string | null;
22
+ source?: string;
23
+ }
24
+
25
+ export function dispatchTokenUpdatedEvent(
26
+ type: TokenType,
27
+ params: TokenEventParams,
28
+ ) {
29
+ if (typeof document === "undefined") {
30
+ return;
31
+ }
32
+
33
+ document.dispatchEvent(
34
+ new CustomEvent(TOKEN_UPDATED_EVENT, {
35
+ detail: { ...params, type },
36
+ }),
37
+ );
38
+ }