fauxbase 0.5.1 → 0.5.2
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 +34 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +6 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +34 -2
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -268,6 +268,7 @@ var AuthService = class extends Service {
|
|
|
268
268
|
authState = null;
|
|
269
269
|
saveState = null;
|
|
270
270
|
httpDriver = null;
|
|
271
|
+
authChangeListeners = [];
|
|
271
272
|
/** @internal — called by createClient to wire persistence */
|
|
272
273
|
_initAuth(loadState, saveState) {
|
|
273
274
|
this.saveState = saveState;
|
|
@@ -423,10 +424,17 @@ var AuthService = class extends Service {
|
|
|
423
424
|
exp: Date.now() + 24 * 60 * 60 * 1e3
|
|
424
425
|
}));
|
|
425
426
|
}
|
|
427
|
+
/** @internal — called by createClient to listen for auth state changes */
|
|
428
|
+
_onAuthChange(listener) {
|
|
429
|
+
this.authChangeListeners.push(listener);
|
|
430
|
+
}
|
|
426
431
|
persistState() {
|
|
427
432
|
if (this.saveState) {
|
|
428
433
|
this.saveState(this.authState);
|
|
429
434
|
}
|
|
435
|
+
for (const listener of this.authChangeListeners) {
|
|
436
|
+
listener();
|
|
437
|
+
}
|
|
430
438
|
}
|
|
431
439
|
};
|
|
432
440
|
|
|
@@ -1447,6 +1455,10 @@ var SSESource = class {
|
|
|
1447
1455
|
});
|
|
1448
1456
|
}
|
|
1449
1457
|
}
|
|
1458
|
+
reconnect() {
|
|
1459
|
+
this.disconnect();
|
|
1460
|
+
this.connect();
|
|
1461
|
+
}
|
|
1450
1462
|
disconnect() {
|
|
1451
1463
|
if (this.eventSource) {
|
|
1452
1464
|
this.eventSource.close();
|
|
@@ -1481,6 +1493,10 @@ var STOMPSource = class {
|
|
|
1481
1493
|
connect() {
|
|
1482
1494
|
this.connectAsync();
|
|
1483
1495
|
}
|
|
1496
|
+
reconnect() {
|
|
1497
|
+
this.disconnect();
|
|
1498
|
+
this.connect();
|
|
1499
|
+
}
|
|
1484
1500
|
async connectAsync() {
|
|
1485
1501
|
let StompJs;
|
|
1486
1502
|
try {
|
|
@@ -1491,9 +1507,16 @@ var STOMPSource = class {
|
|
|
1491
1507
|
"STOMP source requires @stomp/stompjs. Install it: npm install @stomp/stompjs"
|
|
1492
1508
|
);
|
|
1493
1509
|
}
|
|
1510
|
+
const headers = { ...this.config.connectHeaders };
|
|
1511
|
+
if (this.config.getAuthToken) {
|
|
1512
|
+
const token = this.config.getAuthToken();
|
|
1513
|
+
if (token) {
|
|
1514
|
+
headers["Authorization"] = `Bearer ${token}`;
|
|
1515
|
+
}
|
|
1516
|
+
}
|
|
1494
1517
|
this.client = new StompJs.Client({
|
|
1495
1518
|
brokerURL: this.config.brokerUrl,
|
|
1496
|
-
connectHeaders:
|
|
1519
|
+
connectHeaders: headers,
|
|
1497
1520
|
onConnect: () => {
|
|
1498
1521
|
for (const [destination, resource] of Object.entries(this.config.subscriptions)) {
|
|
1499
1522
|
this.client.subscribe(destination, (message) => {
|
|
@@ -1647,10 +1670,19 @@ function createClient(config) {
|
|
|
1647
1670
|
if (eventsConfig.source.type === "sse") {
|
|
1648
1671
|
eventSource = new SSESource(eventsConfig.source, eventBus);
|
|
1649
1672
|
} else if (eventsConfig.source.type === "stomp") {
|
|
1650
|
-
|
|
1673
|
+
const stompConfig = { ...eventsConfig.source };
|
|
1674
|
+
if (config.auth && !stompConfig.getAuthToken) {
|
|
1675
|
+
stompConfig.getAuthToken = () => client.auth?.token ?? null;
|
|
1676
|
+
}
|
|
1677
|
+
eventSource = new STOMPSource(stompConfig, eventBus);
|
|
1651
1678
|
}
|
|
1652
1679
|
eventSource?.connect();
|
|
1653
1680
|
}
|
|
1681
|
+
if (eventSource && config.auth) {
|
|
1682
|
+
client.auth._onAuthChange(() => {
|
|
1683
|
+
eventSource.reconnect();
|
|
1684
|
+
});
|
|
1685
|
+
}
|
|
1654
1686
|
client.disconnect = () => {
|
|
1655
1687
|
eventSource?.disconnect();
|
|
1656
1688
|
eventBus.destroy();
|