@scalemule/sdk 0.0.29 → 0.0.31

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.mts CHANGED
@@ -1502,6 +1502,7 @@ interface PresenceEvent {
1502
1502
  declare class RealtimeService extends ServiceModule {
1503
1503
  protected basePath: string;
1504
1504
  private ws;
1505
+ private usedTicketAuth;
1505
1506
  private subscriptions;
1506
1507
  private presenceCallbacks;
1507
1508
  private statusCallbacks;
@@ -1542,6 +1543,7 @@ declare class RealtimeService extends ServiceModule {
1542
1543
  /** Disconnect and clean up all subscriptions. */
1543
1544
  disconnect(): void;
1544
1545
  private connect;
1546
+ private fetchTicketAndConnect;
1545
1547
  private authenticate;
1546
1548
  private handleMessage;
1547
1549
  private dispatchMessage;
package/dist/index.d.ts CHANGED
@@ -1502,6 +1502,7 @@ interface PresenceEvent {
1502
1502
  declare class RealtimeService extends ServiceModule {
1503
1503
  protected basePath: string;
1504
1504
  private ws;
1505
+ private usedTicketAuth;
1505
1506
  private subscriptions;
1506
1507
  private presenceCallbacks;
1507
1508
  private statusCallbacks;
@@ -1542,6 +1543,7 @@ declare class RealtimeService extends ServiceModule {
1542
1543
  /** Disconnect and clean up all subscriptions. */
1543
1544
  disconnect(): void;
1544
1545
  private connect;
1546
+ private fetchTicketAndConnect;
1545
1547
  private authenticate;
1546
1548
  private handleMessage;
1547
1549
  private dispatchMessage;
package/dist/index.js CHANGED
@@ -3647,6 +3647,7 @@ var RealtimeService = class extends ServiceModule {
3647
3647
  super(...arguments);
3648
3648
  this.basePath = "/v1/realtime";
3649
3649
  this.ws = null;
3650
+ this.usedTicketAuth = false;
3650
3651
  this.subscriptions = /* @__PURE__ */ new Map();
3651
3652
  this.presenceCallbacks = /* @__PURE__ */ new Map();
3652
3653
  this.statusCallbacks = /* @__PURE__ */ new Set();
@@ -3775,10 +3776,31 @@ var RealtimeService = class extends ServiceModule {
3775
3776
  // --------------------------------------------------------------------------
3776
3777
  connect() {
3777
3778
  if (this._status === "connecting" || this._status === "connected") return;
3778
- const baseUrl = this.client.getBaseUrl();
3779
- const wsUrl = baseUrl.replace(/^http/, "ws") + "/v1/realtime/ws";
3780
3779
  this.setStatus(this.reconnectAttempt > 0 ? "reconnecting" : "connecting");
3780
+ this.fetchTicketAndConnect();
3781
+ }
3782
+ async fetchTicketAndConnect() {
3783
+ const baseUrl = this.client.getBaseUrl();
3781
3784
  try {
3785
+ const headers = { "Content-Type": "application/json" };
3786
+ const apiKey = this.client.getApiKey();
3787
+ if (apiKey) headers["x-api-key"] = apiKey;
3788
+ const token = this.client.getSessionToken();
3789
+ if (token) headers["Authorization"] = `Bearer ${token}`;
3790
+ const ticketRes = await fetch(`${baseUrl}/v1/realtime/ws/ticket`, {
3791
+ method: "POST",
3792
+ headers
3793
+ });
3794
+ let wsUrl;
3795
+ if (ticketRes.ok) {
3796
+ const ticketData = await ticketRes.json();
3797
+ const ticket = ticketData.ticket;
3798
+ wsUrl = baseUrl.replace(/^http/, "ws") + `/v1/realtime/ws?ticket=${encodeURIComponent(ticket)}`;
3799
+ this.usedTicketAuth = true;
3800
+ } else {
3801
+ wsUrl = baseUrl.replace(/^http/, "ws") + "/v1/realtime/ws";
3802
+ this.usedTicketAuth = false;
3803
+ }
3782
3804
  this.ws = new WebSocket(wsUrl);
3783
3805
  } catch {
3784
3806
  this.scheduleReconnect();
@@ -3786,8 +3808,21 @@ var RealtimeService = class extends ServiceModule {
3786
3808
  }
3787
3809
  this.ws.onopen = () => {
3788
3810
  this.reconnectAttempt = 0;
3789
- this.authenticate();
3811
+ if (!this.usedTicketAuth) {
3812
+ this.authenticate();
3813
+ }
3790
3814
  this.startHeartbeat();
3815
+ if (this.usedTicketAuth) {
3816
+ setTimeout(() => {
3817
+ if (!this.authenticated) {
3818
+ this.authenticated = true;
3819
+ this.setStatus("connected");
3820
+ for (const channel of this.subscriptions.keys()) {
3821
+ this.sendWs({ type: "subscribe", channel });
3822
+ }
3823
+ }
3824
+ }, 2e3);
3825
+ }
3791
3826
  };
3792
3827
  this.ws.onmessage = (event) => {
3793
3828
  try {
package/dist/index.mjs CHANGED
@@ -3276,6 +3276,7 @@ var RealtimeService = class extends ServiceModule {
3276
3276
  super(...arguments);
3277
3277
  this.basePath = "/v1/realtime";
3278
3278
  this.ws = null;
3279
+ this.usedTicketAuth = false;
3279
3280
  this.subscriptions = /* @__PURE__ */ new Map();
3280
3281
  this.presenceCallbacks = /* @__PURE__ */ new Map();
3281
3282
  this.statusCallbacks = /* @__PURE__ */ new Set();
@@ -3404,10 +3405,31 @@ var RealtimeService = class extends ServiceModule {
3404
3405
  // --------------------------------------------------------------------------
3405
3406
  connect() {
3406
3407
  if (this._status === "connecting" || this._status === "connected") return;
3407
- const baseUrl = this.client.getBaseUrl();
3408
- const wsUrl = baseUrl.replace(/^http/, "ws") + "/v1/realtime/ws";
3409
3408
  this.setStatus(this.reconnectAttempt > 0 ? "reconnecting" : "connecting");
3409
+ this.fetchTicketAndConnect();
3410
+ }
3411
+ async fetchTicketAndConnect() {
3412
+ const baseUrl = this.client.getBaseUrl();
3410
3413
  try {
3414
+ const headers = { "Content-Type": "application/json" };
3415
+ const apiKey = this.client.getApiKey();
3416
+ if (apiKey) headers["x-api-key"] = apiKey;
3417
+ const token = this.client.getSessionToken();
3418
+ if (token) headers["Authorization"] = `Bearer ${token}`;
3419
+ const ticketRes = await fetch(`${baseUrl}/v1/realtime/ws/ticket`, {
3420
+ method: "POST",
3421
+ headers
3422
+ });
3423
+ let wsUrl;
3424
+ if (ticketRes.ok) {
3425
+ const ticketData = await ticketRes.json();
3426
+ const ticket = ticketData.ticket;
3427
+ wsUrl = baseUrl.replace(/^http/, "ws") + `/v1/realtime/ws?ticket=${encodeURIComponent(ticket)}`;
3428
+ this.usedTicketAuth = true;
3429
+ } else {
3430
+ wsUrl = baseUrl.replace(/^http/, "ws") + "/v1/realtime/ws";
3431
+ this.usedTicketAuth = false;
3432
+ }
3411
3433
  this.ws = new WebSocket(wsUrl);
3412
3434
  } catch {
3413
3435
  this.scheduleReconnect();
@@ -3415,8 +3437,21 @@ var RealtimeService = class extends ServiceModule {
3415
3437
  }
3416
3438
  this.ws.onopen = () => {
3417
3439
  this.reconnectAttempt = 0;
3418
- this.authenticate();
3440
+ if (!this.usedTicketAuth) {
3441
+ this.authenticate();
3442
+ }
3419
3443
  this.startHeartbeat();
3444
+ if (this.usedTicketAuth) {
3445
+ setTimeout(() => {
3446
+ if (!this.authenticated) {
3447
+ this.authenticated = true;
3448
+ this.setStatus("connected");
3449
+ for (const channel of this.subscriptions.keys()) {
3450
+ this.sendWs({ type: "subscribe", channel });
3451
+ }
3452
+ }
3453
+ }, 2e3);
3454
+ }
3420
3455
  };
3421
3456
  this.ws.onmessage = (event) => {
3422
3457
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scalemule/sdk",
3
- "version": "0.0.29",
3
+ "version": "0.0.31",
4
4
  "description": "Official TypeScript/JavaScript SDK for ScaleMule Backend-as-a-Service",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",