fauxbase 0.5.1 → 0.5.3

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.d.cts CHANGED
@@ -157,6 +157,7 @@ type EventHandler<T = any> = (event: FauxbaseEvent<T>) => void;
157
157
  interface EventSourceAdapter {
158
158
  connect(): void;
159
159
  disconnect(): void;
160
+ reconnect(): void;
160
161
  }
161
162
  interface SSEConfig {
162
163
  type: 'sse';
@@ -169,6 +170,8 @@ interface STOMPConfig {
169
170
  brokerUrl: string;
170
171
  subscriptions: Record<string, string>;
171
172
  connectHeaders?: Record<string, string>;
173
+ /** @internal — injected by createClient when auth is configured */
174
+ getAuthToken?: () => string | null;
172
175
  }
173
176
  type EventSourceConfig = SSEConfig | STOMPConfig;
174
177
  interface EventHandlersConfig {
@@ -291,6 +294,7 @@ declare abstract class AuthService<T extends Entity> extends Service<T> {
291
294
  private authState;
292
295
  private saveState;
293
296
  private httpDriver;
297
+ private authChangeListeners;
294
298
  /** @internal — called by createClient to wire persistence */
295
299
  _initAuth(loadState: () => AuthState | null, saveState: (state: AuthState | null) => void): void;
296
300
  /** @internal — called by createClient when using HttpDriver */
@@ -308,6 +312,8 @@ declare abstract class AuthService<T extends Entity> extends Service<T> {
308
312
  private httpLogin;
309
313
  private httpRegister;
310
314
  private generateToken;
315
+ /** @internal — called by createClient to listen for auth state changes */
316
+ _onAuthChange(listener: () => void): void;
311
317
  private persistState;
312
318
  }
313
319
 
package/dist/index.d.ts CHANGED
@@ -157,6 +157,7 @@ type EventHandler<T = any> = (event: FauxbaseEvent<T>) => void;
157
157
  interface EventSourceAdapter {
158
158
  connect(): void;
159
159
  disconnect(): void;
160
+ reconnect(): void;
160
161
  }
161
162
  interface SSEConfig {
162
163
  type: 'sse';
@@ -169,6 +170,8 @@ interface STOMPConfig {
169
170
  brokerUrl: string;
170
171
  subscriptions: Record<string, string>;
171
172
  connectHeaders?: Record<string, string>;
173
+ /** @internal — injected by createClient when auth is configured */
174
+ getAuthToken?: () => string | null;
172
175
  }
173
176
  type EventSourceConfig = SSEConfig | STOMPConfig;
174
177
  interface EventHandlersConfig {
@@ -291,6 +294,7 @@ declare abstract class AuthService<T extends Entity> extends Service<T> {
291
294
  private authState;
292
295
  private saveState;
293
296
  private httpDriver;
297
+ private authChangeListeners;
294
298
  /** @internal — called by createClient to wire persistence */
295
299
  _initAuth(loadState: () => AuthState | null, saveState: (state: AuthState | null) => void): void;
296
300
  /** @internal — called by createClient when using HttpDriver */
@@ -308,6 +312,8 @@ declare abstract class AuthService<T extends Entity> extends Service<T> {
308
312
  private httpLogin;
309
313
  private httpRegister;
310
314
  private generateToken;
315
+ /** @internal — called by createClient to listen for auth state changes */
316
+ _onAuthChange(listener: () => void): void;
311
317
  private persistState;
312
318
  }
313
319
 
package/dist/index.js CHANGED
@@ -266,6 +266,7 @@ var AuthService = class extends Service {
266
266
  authState = null;
267
267
  saveState = null;
268
268
  httpDriver = null;
269
+ authChangeListeners = [];
269
270
  /** @internal — called by createClient to wire persistence */
270
271
  _initAuth(loadState, saveState) {
271
272
  this.saveState = saveState;
@@ -421,10 +422,17 @@ var AuthService = class extends Service {
421
422
  exp: Date.now() + 24 * 60 * 60 * 1e3
422
423
  }));
423
424
  }
425
+ /** @internal — called by createClient to listen for auth state changes */
426
+ _onAuthChange(listener) {
427
+ this.authChangeListeners.push(listener);
428
+ }
424
429
  persistState() {
425
430
  if (this.saveState) {
426
431
  this.saveState(this.authState);
427
432
  }
433
+ for (const listener of this.authChangeListeners) {
434
+ listener();
435
+ }
428
436
  }
429
437
  };
430
438
 
@@ -1445,6 +1453,10 @@ var SSESource = class {
1445
1453
  });
1446
1454
  }
1447
1455
  }
1456
+ reconnect() {
1457
+ this.disconnect();
1458
+ this.connect();
1459
+ }
1448
1460
  disconnect() {
1449
1461
  if (this.eventSource) {
1450
1462
  this.eventSource.close();
@@ -1479,6 +1491,10 @@ var STOMPSource = class {
1479
1491
  connect() {
1480
1492
  this.connectAsync();
1481
1493
  }
1494
+ reconnect() {
1495
+ this.disconnect();
1496
+ this.connect();
1497
+ }
1482
1498
  async connectAsync() {
1483
1499
  let StompJs;
1484
1500
  try {
@@ -1489,9 +1505,16 @@ var STOMPSource = class {
1489
1505
  "STOMP source requires @stomp/stompjs. Install it: npm install @stomp/stompjs"
1490
1506
  );
1491
1507
  }
1508
+ const headers = { ...this.config.connectHeaders };
1509
+ if (this.config.getAuthToken) {
1510
+ const token = this.config.getAuthToken();
1511
+ if (token) {
1512
+ headers["Authorization"] = `Bearer ${token}`;
1513
+ }
1514
+ }
1492
1515
  this.client = new StompJs.Client({
1493
1516
  brokerURL: this.config.brokerUrl,
1494
- connectHeaders: this.config.connectHeaders,
1517
+ connectHeaders: headers,
1495
1518
  onConnect: () => {
1496
1519
  for (const [destination, resource] of Object.entries(this.config.subscriptions)) {
1497
1520
  this.client.subscribe(destination, (message) => {
@@ -1645,10 +1668,19 @@ function createClient(config) {
1645
1668
  if (eventsConfig.source.type === "sse") {
1646
1669
  eventSource = new SSESource(eventsConfig.source, eventBus);
1647
1670
  } else if (eventsConfig.source.type === "stomp") {
1648
- eventSource = new STOMPSource(eventsConfig.source, eventBus);
1671
+ const stompConfig = { ...eventsConfig.source };
1672
+ if (config.auth && !stompConfig.getAuthToken) {
1673
+ stompConfig.getAuthToken = () => client.auth?.token ?? null;
1674
+ }
1675
+ eventSource = new STOMPSource(stompConfig, eventBus);
1649
1676
  }
1650
1677
  eventSource?.connect();
1651
1678
  }
1679
+ if (eventSource && config.auth) {
1680
+ client.auth._onAuthChange(() => {
1681
+ eventSource.reconnect();
1682
+ });
1683
+ }
1652
1684
  client.disconnect = () => {
1653
1685
  eventSource?.disconnect();
1654
1686
  eventBus.destroy();