@tesana/sdk 1.3.0 → 1.3.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/README.md CHANGED
@@ -156,6 +156,16 @@ once (TES-2641). Types and properties are the backend registry's
156
156
  - **Nothing personal (TES-2639).** No device id: the SDK's device id can be
157
157
  traded for that guest's play token, so it never goes to analytics. The
158
158
  browser is named by its own random `anonymous_id`.
159
+ - **The account, named by the server (since 1.3.1, TES-2722).** The SDK never
160
+ sends a user id. A signed-in player's batch carries the site's play session
161
+ token (the bootstrap's `token`, the one the shop calls send) as `token` in the
162
+ body, since a beacon can't carry an Authorization header. The collector checks
163
+ it (signature, expiry, and that it is this game's) and stores the events with
164
+ the player's account; a guest's events, or a forged, expired or other game's
165
+ token, stay anonymous. A play service token (a JWT, all a guest holds) never
166
+ goes. The token goes only to Tesana's own collectors, never to one a host or
167
+ the game names (TES-2729), and a host answer without it (a sign-out) takes it
168
+ away at once, even one the session refuses to adopt (TES-2730).
159
169
  - **Delivery.** Batched every 30 s (at once at 50 events), and flushed by
160
170
  `navigator.sendBeacon` when the page is hidden or closed, as JSON labelled
161
171
  `text/plain`; a keepalive `fetch` when there is no beacon. Every event has its
@@ -50,7 +50,13 @@
50
50
  // 1.3.0 files a game's level events, its quits and a play heartbeat every 15 s
51
51
  // in Tesana's own event registry (`POST /api/e`, TES-2197), from Tesana's play
52
52
  // hosts only, and adds `tesana.gameplay.levelFail(levelId, reason?)`.
53
- const VERSION = "1.3.0";
53
+ //
54
+ // 1.3.2 (TES-2597): a room reconnect whose grant is past its `exp` asks for the
55
+ // room again first, so a drop after two hours in a room comes back without a
56
+ // refusal; if that fails, the held URL is offered once per drop (TES-2763), and
57
+ // a re-join that answers after the player left or moved rooms is dropped
58
+ // (TES-2764).
59
+ const VERSION = "1.3.2";
54
60
 
55
61
  const LOCAL_CACHE = "tesana.play.v1";
56
62
 
@@ -70,6 +76,10 @@ const MP_RECONNECT_MAX_ATTEMPTS = 8;
70
76
  //: up on that attempt. The answer is a token mint; a host that is slow must not
71
77
  //: hold a request open indefinitely.
72
78
  const MP_HOST_ASK_TIMEOUT_MS = 5000;
79
+ //: How close to its `exp` a room grant counts as expired for a reconnect. A
80
+ //: device clock that runs a little slow would otherwise offer a grant the room
81
+ //: host has just stopped taking; re-joining a little early costs one request.
82
+ const MP_GRANT_EXPIRY_MARGIN_MS = 30000;
73
83
  //: How long `create`/`quickJoin` will wait for a room that is being placed.
74
84
  //:
75
85
  //: Room creation on GameLift is asynchronous, so the service answers 202 with
@@ -459,9 +469,12 @@ function startListening() {
459
469
  // The site posts `{ type, payload }`; a baked or spliced bootstrap is the
460
470
  // payload itself. Accept both, so one SDK reads either host.
461
471
  if (data.type === "tesana-bootstrap") {
472
+ const next = data.payload && typeof data.payload === "object" ? data.payload : data;
473
+ // Whatever happens to the answer, events stop carrying a token it lacks.
474
+ eventsDropStaleToken(next);
462
475
  // `applyBootstrap` decides whether this answer may update a live session:
463
476
  // one the client asked for may, one it did not may not.
464
- applyBootstrap(data.payload && typeof data.payload === "object" ? data.payload : data);
477
+ applyBootstrap(next);
465
478
  return;
466
479
  }
467
480
  if (data.type === "tesana-wallet-changed" && bootstrap) {
@@ -590,6 +603,26 @@ function requestHostBootstrap() {
590
603
  return bootstrapAskPromise;
591
604
  }
592
605
 
606
+ /**
607
+ * Whether the player token a room URL carries has passed its own `exp`.
608
+ *
609
+ * Read without checking the signature: this only decides whether a reconnect
610
+ * asks for the room again before connecting. The room host still verifies
611
+ * every grant, so an expired token is refused there whatever this says. A URL
612
+ * whose token is not a JWT, or has no `exp`, is never called expired.
613
+ *
614
+ * @param {string} wsUrl
615
+ */
616
+ function roomGrantExpired(wsUrl) {
617
+ try {
618
+ const token = new URL(String(wsUrl)).searchParams.get("token");
619
+ const exp = Number(eventsClaims(token)?.exp);
620
+ return Number.isFinite(exp) && exp > 0 && exp * 1000 <= Date.now() + MP_GRANT_EXPIRY_MARGIN_MS;
621
+ } catch {
622
+ return false;
623
+ }
624
+ }
625
+
593
626
  function deviceId() {
594
627
  const cache = readCache();
595
628
  if (cache.deviceId) return cache.deviceId;
@@ -999,6 +1032,15 @@ const gameplay = Object.freeze({
999
1032
  // - No device id (TES-2639): the SDK's device id can be traded for that
1000
1033
  // guest's play token, so it never leaves for analytics. The browser is
1001
1034
  // named by its own random `anonymous_id`.
1035
+ // - No user id, ever (TES-2722). A signed-in player's batch carries the
1036
+ // site's play session token (the host's `token`, the one the shop calls
1037
+ // send), and the collector checks it and names the account itself; a
1038
+ // forged, expired or other game's token names no one. Only that token: a
1039
+ // play service token (a JWT, which is what a guest holds) never goes. In
1040
+ // the body, because a beacon can't carry an Authorization header. Only to
1041
+ // Tesana's own collectors (TES-2729): a collector a host or the game names
1042
+ // gets the events without it. And a host answer without it (a sign-out)
1043
+ // takes it away at once, even an answer the session refuses (TES-2730).
1002
1044
  //
1003
1045
  // A heartbeat marks 15 s of time in front (TES-2641): the clock runs only while
1004
1046
  // the page is visible and resumes where it stopped, so coming back to the tab
@@ -1016,6 +1058,8 @@ const EVENTS_COLLECTORS = new Map([
1016
1058
  ["play.tesana.ai", "https://api.tesana.ai/api/e"],
1017
1059
  ["play-staging.tesana.ai", "https://api-staging.onetap.build/api/e"],
1018
1060
  ]);
1061
+ //: Tesana's own collectors: the only ones a session token goes to (TES-2729).
1062
+ const EVENTS_OWN_COLLECTORS = new Set(EVENTS_COLLECTORS.values());
1019
1063
  //: A collector a host names: https, or plain http on this machine for a developer.
1020
1064
  const EVENTS_URL = /^(https:\/\/[^\s/]+|http:\/\/(127\.0\.0\.1|localhost)(:\d+)?)\/\S*$/i;
1021
1065
  const EVENTS_FLUSH_MS = 30000;
@@ -1138,6 +1182,38 @@ function eventsPlayPage(merged) {
1138
1182
  return "";
1139
1183
  }
1140
1184
 
1185
+ /**
1186
+ * The site's play session token, for the collector to name a signed-in player
1187
+ * by (TES-2722), or "". Tesana's own API issued it and only it can check it. A
1188
+ * play service token is a JWT and never goes: a guest's is all a guest holds.
1189
+ */
1190
+ function eventsSessionToken(merged) {
1191
+ const token = typeof merged.token === "string" ? merged.token.trim() : "";
1192
+ if (!token || token.length > 512 || /\s/.test(token)) return "";
1193
+ return token.split(".").length === 3 ? "" : token;
1194
+ }
1195
+
1196
+ /**
1197
+ * A host's answer that doesn't carry the session token the batches carry (a
1198
+ * sign-out, another account, a session that couldn't be minted) takes it away
1199
+ * at once (TES-2730). Run on every answer the host posts, before
1200
+ * `applyBootstrap`, which keeps a live session's identity and so refuses an
1201
+ * unasked guest answer: events after a sign-out must not carry the old
1202
+ * account. What was queued goes first, with the token it was played under.
1203
+ * Dropping a token only makes events anonymous, so any poster may cause it.
1204
+ */
1205
+ function eventsDropStaleToken(next) {
1206
+ try {
1207
+ const shared = eventsState.shared;
1208
+ if (!shared || !shared.token) return;
1209
+ if (eventsSessionToken(next && typeof next === "object" ? next : {}) === shared.token) return;
1210
+ eventsFlush();
1211
+ delete shared.token;
1212
+ } catch {
1213
+ /* never */
1214
+ }
1215
+ }
1216
+
1141
1217
  /** This browser's anonymous id: the collector's cookie wins when it has one. */
1142
1218
  function eventsAnonymousId() {
1143
1219
  const cached = readCache().anonymousId;
@@ -1168,6 +1244,13 @@ function eventsConfigure(merged) {
1168
1244
  const game = eventsGame(merged);
1169
1245
  if (!game || page !== "yes") return;
1170
1246
  const build = String(merged.build || merged.buildId || merged.buildVersion || "").slice(0, 64);
1247
+ const session = EVENTS_OWN_COLLECTORS.has(url) ? eventsSessionToken(merged) : "";
1248
+ // Rebuilt on every bootstrap the session accepts: a refreshed token
1249
+ // replaces the old one. What was queued goes first, under the token it was
1250
+ // played with, so a guest's play before signing in isn't stamped with the
1251
+ // account (a sign-out is eventsDropStaleToken's).
1252
+ const before = eventsState.shared;
1253
+ if (eventsState.state === "on" && before && (before.token || "") !== session) eventsFlush();
1171
1254
  eventsState.url = url;
1172
1255
  eventsState.shared = {
1173
1256
  surface: window.TesanaDesktop ? "desktop" : "sdk",
@@ -1175,6 +1258,7 @@ function eventsConfigure(merged) {
1175
1258
  ...(build ? { build_sha: build } : {}),
1176
1259
  anonymous_id: eventsAnonymousId(),
1177
1260
  ...game,
1261
+ ...(session ? { token: session } : {}),
1178
1262
  };
1179
1263
  eventsState.state = "on";
1180
1264
  eventsListen();
@@ -2042,6 +2126,10 @@ class TesanaClient {
2042
2126
  // expired, most likely — and a fresh one has to be minted before the next
2043
2127
  // reconnect can succeed. Cleared once a reconnect has used it.
2044
2128
  lastCloseCode: null,
2129
+ // Whether this drop has already offered a grant the device judged
2130
+ // expired, after a re-join failed (TES-2763). Once per drop, because the
2131
+ // device's clock may be wrong and the room host's check is the real one.
2132
+ expiredGrantOffered: false,
2045
2133
  reconnectExhausted: false,
2046
2134
  statusHandlers: [],
2047
2135
  // Overridable so a test (or a game with unusual tolerance) can shorten
@@ -2634,7 +2722,10 @@ class TesanaClient {
2634
2722
  let opened = false;
2635
2723
  const timer = setTimeout(() => {
2636
2724
  if (!opened) {
2637
- this._mpClose();
2725
+ // Only this attempt's own socket (TES-2764). If the game has since
2726
+ // entered another room, `this._mp.ws` is that room's, and closing it
2727
+ // here would throw the player out of where they chose to be.
2728
+ if (this._mp.ws === ws) this._mpClose();
2638
2729
  reject(new Error("multiplayer connect timeout"));
2639
2730
  }
2640
2731
  }, 8000);
@@ -2655,6 +2746,7 @@ class TesanaClient {
2655
2746
  this._mpEmitStatus({ status: "reconnected" });
2656
2747
  }
2657
2748
  this._mp.reconnectAttempts = 0;
2749
+ this._mp.expiredGrantOffered = false;
2658
2750
  resolve(msg);
2659
2751
  }
2660
2752
  for (const fn of this._mp.handlers) {
@@ -2708,6 +2800,10 @@ class TesanaClient {
2708
2800
  const mp = this._mp;
2709
2801
  if (!mp.wsUrl || mp.leaving) return;
2710
2802
  if (mp.reconnectTimer) return;
2803
+ // A socket open or opening means there is nothing to get back into: a
2804
+ // stale attempt that failed after the game entered another room must not
2805
+ // schedule a reconnect over the live one (TES-2764).
2806
+ if (mp.ws) return;
2711
2807
  const attempts = (mp.reconnectAttempts || 0) + 1;
2712
2808
  mp.reconnectAttempts = attempts;
2713
2809
  if (attempts > mp.reconnectMaxAttempts) {
@@ -2730,8 +2826,8 @@ class TesanaClient {
2730
2826
  }
2731
2827
 
2732
2828
  /**
2733
- * One reconnect: get a current grant if the last one was refused, then try the
2734
- * room again.
2829
+ * One reconnect: get a current grant if the last one was refused or has
2830
+ * expired, then try the room again.
2735
2831
  *
2736
2832
  * The grant the client holds is embedded in `wsUrl` and was minted when it
2737
2833
  * first joined. A game open longer than the token's life therefore cannot
@@ -2748,32 +2844,61 @@ class TesanaClient {
2748
2844
  */
2749
2845
  async _mpReconnectAttempt() {
2750
2846
  const mp = this._mp;
2751
- let wsUrl = mp.wsUrl;
2847
+ // What this attempt is for. The re-join below can take seconds (a host mint,
2848
+ // a placement wait), and the game may leave or enter another room meanwhile.
2849
+ const roomId = mp.roomId;
2850
+ const heldUrl = mp.wsUrl;
2851
+ let wsUrl = heldUrl;
2852
+ // A grant whose own `exp` has passed can only be refused (TES-2597). Offering
2853
+ // it anyway cost every reconnect after two hours in a room one refusal and
2854
+ // one backoff step, and when the re-join then failed, each later attempt
2855
+ // offered the same dead URL again, eight refusals and the player gone. So an
2856
+ // expired grant is treated as refused before it is offered.
2857
+ const expired = roomGrantExpired(wsUrl);
2752
2858
  // 4401 — the grant was refused, so the URL itself is dead and a fresh one is
2753
2859
  // required. 4409 — the room is being re-placed and has no address yet, so
2754
2860
  // the URL the client holds is not usable either. Both are fixed by asking
2755
2861
  // for the room again; 4403 (the game is denied rooms) and 4404 (the room is
2756
2862
  // gone) are not, and fall through to the URL we have so the attempt is still
2757
2863
  // counted and the backoff still applies.
2758
- if ((mp.lastCloseCode === 4401 || mp.lastCloseCode === 4409) && mp.roomId) {
2864
+ if ((expired || mp.lastCloseCode === 4401 || mp.lastCloseCode === 4409) && roomId) {
2865
+ let session = null;
2866
+ let failure = null;
2759
2867
  try {
2760
2868
  // `_mpAwaitPlacement` is harmless when the room is already up (it
2761
2869
  // returns the session untouched) and necessary when a reconnect lands
2762
2870
  // during a re-placement — a 4409 close, or a room swept and reopened,
2763
2871
  // answers with no `wsUrl`, and retrying the old URL would never work.
2764
- const session = await this._mpAwaitPlacement(
2765
- await this._mpRequest("POST", `/v1/mp/rooms/${enc(mp.roomId)}/join`),
2872
+ session = await this._mpAwaitPlacement(
2873
+ await this._mpRequest("POST", `/v1/mp/rooms/${enc(roomId)}/join`),
2766
2874
  );
2767
- if (session && session.wsUrl) {
2768
- wsUrl = session.wsUrl;
2769
- mp.wsUrl = wsUrl;
2770
- mp.code = session.room?.code ?? mp.code;
2875
+ } catch (err) {
2876
+ failure = err;
2877
+ }
2878
+ // The player left, or went into another room, while the re-join was out.
2879
+ // Its answer is for a room they are no longer in: drop it and do not
2880
+ // reconnect (TES-2764). Resolving, not throwing, so no retry is scheduled.
2881
+ if (mp.leaving || mp.roomId !== roomId || mp.wsUrl !== heldUrl) return null;
2882
+ if (session && session.wsUrl) {
2883
+ wsUrl = session.wsUrl;
2884
+ mp.wsUrl = wsUrl;
2885
+ mp.code = session.room?.code ?? mp.code;
2886
+ } else if (failure && expired) {
2887
+ // The device's clock says the grant is expired, but the clock may run
2888
+ // fast, and the room host's check is the one that counts (TES-2763). So
2889
+ // the held URL is still offered, once per drop: a grant that really has
2890
+ // expired costs one refusal, not eight, and one that has not gets the
2891
+ // player back in while the HTTP API is down. After that, the attempt
2892
+ // fails here and the backoff asks for the room again.
2893
+ if (mp.expiredGrantOffered) {
2894
+ mp.lastCloseCode = null;
2895
+ throw failure;
2771
2896
  }
2772
- } catch {
2773
- // The room is gone, or the fresh grant was refused too. Fall through to
2774
- // the URL we have: the room may have been replaced under the same id,
2775
- // and if it has not, the next attempt retries this same path.
2897
+ mp.expiredGrantOffered = true;
2776
2898
  }
2899
+ // Otherwise the room is gone, or the fresh grant was refused too. Fall
2900
+ // through to the URL we have: the room may have been replaced under the
2901
+ // same id, and if it has not, the next attempt retries this same path.
2777
2902
  }
2778
2903
  mp.lastCloseCode = null;
2779
2904
  return this._mpOpen(wsUrl);
@@ -2858,6 +2983,7 @@ class TesanaClient {
2858
2983
  this._mp.reconnectAttempts = 0;
2859
2984
  this._mp.reconnectExhausted = false;
2860
2985
  this._mp.lastCloseCode = null;
2986
+ this._mp.expiredGrantOffered = false;
2861
2987
  try {
2862
2988
  if (!roomId) return { ok: true, room: null };
2863
2989
  try {
@@ -1,4 +1,4 @@
1
- /* Tesana play-time SDK v1.3.0 */
1
+ /* Tesana play-time SDK v1.3.2 */
2
2
  (function (root) {
3
3
  /**
4
4
  * Tesana play-time SDK. Vanilla JS, no npm in games.
@@ -52,7 +52,13 @@
52
52
  // 1.3.0 files a game's level events, its quits and a play heartbeat every 15 s
53
53
  // in Tesana's own event registry (`POST /api/e`, TES-2197), from Tesana's play
54
54
  // hosts only, and adds `tesana.gameplay.levelFail(levelId, reason?)`.
55
- const VERSION = "1.3.0";
55
+ //
56
+ // 1.3.2 (TES-2597): a room reconnect whose grant is past its `exp` asks for the
57
+ // room again first, so a drop after two hours in a room comes back without a
58
+ // refusal; if that fails, the held URL is offered once per drop (TES-2763), and
59
+ // a re-join that answers after the player left or moved rooms is dropped
60
+ // (TES-2764).
61
+ const VERSION = "1.3.2";
56
62
 
57
63
  const LOCAL_CACHE = "tesana.play.v1";
58
64
 
@@ -72,6 +78,10 @@ const MP_RECONNECT_MAX_ATTEMPTS = 8;
72
78
  //: up on that attempt. The answer is a token mint; a host that is slow must not
73
79
  //: hold a request open indefinitely.
74
80
  const MP_HOST_ASK_TIMEOUT_MS = 5000;
81
+ //: How close to its `exp` a room grant counts as expired for a reconnect. A
82
+ //: device clock that runs a little slow would otherwise offer a grant the room
83
+ //: host has just stopped taking; re-joining a little early costs one request.
84
+ const MP_GRANT_EXPIRY_MARGIN_MS = 30000;
75
85
  //: How long `create`/`quickJoin` will wait for a room that is being placed.
76
86
  //:
77
87
  //: Room creation on GameLift is asynchronous, so the service answers 202 with
@@ -461,9 +471,12 @@ function startListening() {
461
471
  // The site posts `{ type, payload }`; a baked or spliced bootstrap is the
462
472
  // payload itself. Accept both, so one SDK reads either host.
463
473
  if (data.type === "tesana-bootstrap") {
474
+ const next = data.payload && typeof data.payload === "object" ? data.payload : data;
475
+ // Whatever happens to the answer, events stop carrying a token it lacks.
476
+ eventsDropStaleToken(next);
464
477
  // `applyBootstrap` decides whether this answer may update a live session:
465
478
  // one the client asked for may, one it did not may not.
466
- applyBootstrap(data.payload && typeof data.payload === "object" ? data.payload : data);
479
+ applyBootstrap(next);
467
480
  return;
468
481
  }
469
482
  if (data.type === "tesana-wallet-changed" && bootstrap) {
@@ -592,6 +605,26 @@ function requestHostBootstrap() {
592
605
  return bootstrapAskPromise;
593
606
  }
594
607
 
608
+ /**
609
+ * Whether the player token a room URL carries has passed its own `exp`.
610
+ *
611
+ * Read without checking the signature: this only decides whether a reconnect
612
+ * asks for the room again before connecting. The room host still verifies
613
+ * every grant, so an expired token is refused there whatever this says. A URL
614
+ * whose token is not a JWT, or has no `exp`, is never called expired.
615
+ *
616
+ * @param {string} wsUrl
617
+ */
618
+ function roomGrantExpired(wsUrl) {
619
+ try {
620
+ const token = new URL(String(wsUrl)).searchParams.get("token");
621
+ const exp = Number(eventsClaims(token)?.exp);
622
+ return Number.isFinite(exp) && exp > 0 && exp * 1000 <= Date.now() + MP_GRANT_EXPIRY_MARGIN_MS;
623
+ } catch {
624
+ return false;
625
+ }
626
+ }
627
+
595
628
  function deviceId() {
596
629
  const cache = readCache();
597
630
  if (cache.deviceId) return cache.deviceId;
@@ -1001,6 +1034,15 @@ const gameplay = Object.freeze({
1001
1034
  // - No device id (TES-2639): the SDK's device id can be traded for that
1002
1035
  // guest's play token, so it never leaves for analytics. The browser is
1003
1036
  // named by its own random `anonymous_id`.
1037
+ // - No user id, ever (TES-2722). A signed-in player's batch carries the
1038
+ // site's play session token (the host's `token`, the one the shop calls
1039
+ // send), and the collector checks it and names the account itself; a
1040
+ // forged, expired or other game's token names no one. Only that token: a
1041
+ // play service token (a JWT, which is what a guest holds) never goes. In
1042
+ // the body, because a beacon can't carry an Authorization header. Only to
1043
+ // Tesana's own collectors (TES-2729): a collector a host or the game names
1044
+ // gets the events without it. And a host answer without it (a sign-out)
1045
+ // takes it away at once, even an answer the session refuses (TES-2730).
1004
1046
  //
1005
1047
  // A heartbeat marks 15 s of time in front (TES-2641): the clock runs only while
1006
1048
  // the page is visible and resumes where it stopped, so coming back to the tab
@@ -1018,6 +1060,8 @@ const EVENTS_COLLECTORS = new Map([
1018
1060
  ["play.tesana.ai", "https://api.tesana.ai/api/e"],
1019
1061
  ["play-staging.tesana.ai", "https://api-staging.onetap.build/api/e"],
1020
1062
  ]);
1063
+ //: Tesana's own collectors: the only ones a session token goes to (TES-2729).
1064
+ const EVENTS_OWN_COLLECTORS = new Set(EVENTS_COLLECTORS.values());
1021
1065
  //: A collector a host names: https, or plain http on this machine for a developer.
1022
1066
  const EVENTS_URL = /^(https:\/\/[^\s/]+|http:\/\/(127\.0\.0\.1|localhost)(:\d+)?)\/\S*$/i;
1023
1067
  const EVENTS_FLUSH_MS = 30000;
@@ -1140,6 +1184,38 @@ function eventsPlayPage(merged) {
1140
1184
  return "";
1141
1185
  }
1142
1186
 
1187
+ /**
1188
+ * The site's play session token, for the collector to name a signed-in player
1189
+ * by (TES-2722), or "". Tesana's own API issued it and only it can check it. A
1190
+ * play service token is a JWT and never goes: a guest's is all a guest holds.
1191
+ */
1192
+ function eventsSessionToken(merged) {
1193
+ const token = typeof merged.token === "string" ? merged.token.trim() : "";
1194
+ if (!token || token.length > 512 || /\s/.test(token)) return "";
1195
+ return token.split(".").length === 3 ? "" : token;
1196
+ }
1197
+
1198
+ /**
1199
+ * A host's answer that doesn't carry the session token the batches carry (a
1200
+ * sign-out, another account, a session that couldn't be minted) takes it away
1201
+ * at once (TES-2730). Run on every answer the host posts, before
1202
+ * `applyBootstrap`, which keeps a live session's identity and so refuses an
1203
+ * unasked guest answer: events after a sign-out must not carry the old
1204
+ * account. What was queued goes first, with the token it was played under.
1205
+ * Dropping a token only makes events anonymous, so any poster may cause it.
1206
+ */
1207
+ function eventsDropStaleToken(next) {
1208
+ try {
1209
+ const shared = eventsState.shared;
1210
+ if (!shared || !shared.token) return;
1211
+ if (eventsSessionToken(next && typeof next === "object" ? next : {}) === shared.token) return;
1212
+ eventsFlush();
1213
+ delete shared.token;
1214
+ } catch {
1215
+ /* never */
1216
+ }
1217
+ }
1218
+
1143
1219
  /** This browser's anonymous id: the collector's cookie wins when it has one. */
1144
1220
  function eventsAnonymousId() {
1145
1221
  const cached = readCache().anonymousId;
@@ -1170,6 +1246,13 @@ function eventsConfigure(merged) {
1170
1246
  const game = eventsGame(merged);
1171
1247
  if (!game || page !== "yes") return;
1172
1248
  const build = String(merged.build || merged.buildId || merged.buildVersion || "").slice(0, 64);
1249
+ const session = EVENTS_OWN_COLLECTORS.has(url) ? eventsSessionToken(merged) : "";
1250
+ // Rebuilt on every bootstrap the session accepts: a refreshed token
1251
+ // replaces the old one. What was queued goes first, under the token it was
1252
+ // played with, so a guest's play before signing in isn't stamped with the
1253
+ // account (a sign-out is eventsDropStaleToken's).
1254
+ const before = eventsState.shared;
1255
+ if (eventsState.state === "on" && before && (before.token || "") !== session) eventsFlush();
1173
1256
  eventsState.url = url;
1174
1257
  eventsState.shared = {
1175
1258
  surface: window.TesanaDesktop ? "desktop" : "sdk",
@@ -1177,6 +1260,7 @@ function eventsConfigure(merged) {
1177
1260
  ...(build ? { build_sha: build } : {}),
1178
1261
  anonymous_id: eventsAnonymousId(),
1179
1262
  ...game,
1263
+ ...(session ? { token: session } : {}),
1180
1264
  };
1181
1265
  eventsState.state = "on";
1182
1266
  eventsListen();
@@ -2044,6 +2128,10 @@ class TesanaClient {
2044
2128
  // expired, most likely — and a fresh one has to be minted before the next
2045
2129
  // reconnect can succeed. Cleared once a reconnect has used it.
2046
2130
  lastCloseCode: null,
2131
+ // Whether this drop has already offered a grant the device judged
2132
+ // expired, after a re-join failed (TES-2763). Once per drop, because the
2133
+ // device's clock may be wrong and the room host's check is the real one.
2134
+ expiredGrantOffered: false,
2047
2135
  reconnectExhausted: false,
2048
2136
  statusHandlers: [],
2049
2137
  // Overridable so a test (or a game with unusual tolerance) can shorten
@@ -2636,7 +2724,10 @@ class TesanaClient {
2636
2724
  let opened = false;
2637
2725
  const timer = setTimeout(() => {
2638
2726
  if (!opened) {
2639
- this._mpClose();
2727
+ // Only this attempt's own socket (TES-2764). If the game has since
2728
+ // entered another room, `this._mp.ws` is that room's, and closing it
2729
+ // here would throw the player out of where they chose to be.
2730
+ if (this._mp.ws === ws) this._mpClose();
2640
2731
  reject(new Error("multiplayer connect timeout"));
2641
2732
  }
2642
2733
  }, 8000);
@@ -2657,6 +2748,7 @@ class TesanaClient {
2657
2748
  this._mpEmitStatus({ status: "reconnected" });
2658
2749
  }
2659
2750
  this._mp.reconnectAttempts = 0;
2751
+ this._mp.expiredGrantOffered = false;
2660
2752
  resolve(msg);
2661
2753
  }
2662
2754
  for (const fn of this._mp.handlers) {
@@ -2710,6 +2802,10 @@ class TesanaClient {
2710
2802
  const mp = this._mp;
2711
2803
  if (!mp.wsUrl || mp.leaving) return;
2712
2804
  if (mp.reconnectTimer) return;
2805
+ // A socket open or opening means there is nothing to get back into: a
2806
+ // stale attempt that failed after the game entered another room must not
2807
+ // schedule a reconnect over the live one (TES-2764).
2808
+ if (mp.ws) return;
2713
2809
  const attempts = (mp.reconnectAttempts || 0) + 1;
2714
2810
  mp.reconnectAttempts = attempts;
2715
2811
  if (attempts > mp.reconnectMaxAttempts) {
@@ -2732,8 +2828,8 @@ class TesanaClient {
2732
2828
  }
2733
2829
 
2734
2830
  /**
2735
- * One reconnect: get a current grant if the last one was refused, then try the
2736
- * room again.
2831
+ * One reconnect: get a current grant if the last one was refused or has
2832
+ * expired, then try the room again.
2737
2833
  *
2738
2834
  * The grant the client holds is embedded in `wsUrl` and was minted when it
2739
2835
  * first joined. A game open longer than the token's life therefore cannot
@@ -2750,32 +2846,61 @@ class TesanaClient {
2750
2846
  */
2751
2847
  async _mpReconnectAttempt() {
2752
2848
  const mp = this._mp;
2753
- let wsUrl = mp.wsUrl;
2849
+ // What this attempt is for. The re-join below can take seconds (a host mint,
2850
+ // a placement wait), and the game may leave or enter another room meanwhile.
2851
+ const roomId = mp.roomId;
2852
+ const heldUrl = mp.wsUrl;
2853
+ let wsUrl = heldUrl;
2854
+ // A grant whose own `exp` has passed can only be refused (TES-2597). Offering
2855
+ // it anyway cost every reconnect after two hours in a room one refusal and
2856
+ // one backoff step, and when the re-join then failed, each later attempt
2857
+ // offered the same dead URL again, eight refusals and the player gone. So an
2858
+ // expired grant is treated as refused before it is offered.
2859
+ const expired = roomGrantExpired(wsUrl);
2754
2860
  // 4401 — the grant was refused, so the URL itself is dead and a fresh one is
2755
2861
  // required. 4409 — the room is being re-placed and has no address yet, so
2756
2862
  // the URL the client holds is not usable either. Both are fixed by asking
2757
2863
  // for the room again; 4403 (the game is denied rooms) and 4404 (the room is
2758
2864
  // gone) are not, and fall through to the URL we have so the attempt is still
2759
2865
  // counted and the backoff still applies.
2760
- if ((mp.lastCloseCode === 4401 || mp.lastCloseCode === 4409) && mp.roomId) {
2866
+ if ((expired || mp.lastCloseCode === 4401 || mp.lastCloseCode === 4409) && roomId) {
2867
+ let session = null;
2868
+ let failure = null;
2761
2869
  try {
2762
2870
  // `_mpAwaitPlacement` is harmless when the room is already up (it
2763
2871
  // returns the session untouched) and necessary when a reconnect lands
2764
2872
  // during a re-placement — a 4409 close, or a room swept and reopened,
2765
2873
  // answers with no `wsUrl`, and retrying the old URL would never work.
2766
- const session = await this._mpAwaitPlacement(
2767
- await this._mpRequest("POST", `/v1/mp/rooms/${enc(mp.roomId)}/join`),
2874
+ session = await this._mpAwaitPlacement(
2875
+ await this._mpRequest("POST", `/v1/mp/rooms/${enc(roomId)}/join`),
2768
2876
  );
2769
- if (session && session.wsUrl) {
2770
- wsUrl = session.wsUrl;
2771
- mp.wsUrl = wsUrl;
2772
- mp.code = session.room?.code ?? mp.code;
2877
+ } catch (err) {
2878
+ failure = err;
2879
+ }
2880
+ // The player left, or went into another room, while the re-join was out.
2881
+ // Its answer is for a room they are no longer in: drop it and do not
2882
+ // reconnect (TES-2764). Resolving, not throwing, so no retry is scheduled.
2883
+ if (mp.leaving || mp.roomId !== roomId || mp.wsUrl !== heldUrl) return null;
2884
+ if (session && session.wsUrl) {
2885
+ wsUrl = session.wsUrl;
2886
+ mp.wsUrl = wsUrl;
2887
+ mp.code = session.room?.code ?? mp.code;
2888
+ } else if (failure && expired) {
2889
+ // The device's clock says the grant is expired, but the clock may run
2890
+ // fast, and the room host's check is the one that counts (TES-2763). So
2891
+ // the held URL is still offered, once per drop: a grant that really has
2892
+ // expired costs one refusal, not eight, and one that has not gets the
2893
+ // player back in while the HTTP API is down. After that, the attempt
2894
+ // fails here and the backoff asks for the room again.
2895
+ if (mp.expiredGrantOffered) {
2896
+ mp.lastCloseCode = null;
2897
+ throw failure;
2773
2898
  }
2774
- } catch {
2775
- // The room is gone, or the fresh grant was refused too. Fall through to
2776
- // the URL we have: the room may have been replaced under the same id,
2777
- // and if it has not, the next attempt retries this same path.
2899
+ mp.expiredGrantOffered = true;
2778
2900
  }
2901
+ // Otherwise the room is gone, or the fresh grant was refused too. Fall
2902
+ // through to the URL we have: the room may have been replaced under the
2903
+ // same id, and if it has not, the next attempt retries this same path.
2779
2904
  }
2780
2905
  mp.lastCloseCode = null;
2781
2906
  return this._mpOpen(wsUrl);
@@ -2860,6 +2985,7 @@ class TesanaClient {
2860
2985
  this._mp.reconnectAttempts = 0;
2861
2986
  this._mp.reconnectExhausted = false;
2862
2987
  this._mp.lastCloseCode = null;
2988
+ this._mp.expiredGrantOffered = false;
2863
2989
  try {
2864
2990
  if (!roomId) return { ok: true, room: null };
2865
2991
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tesana/sdk",
3
- "version": "1.3.0",
3
+ "version": "1.3.2",
4
4
  "description": "Tesana play-time SDK — players, cloud saves, leaderboards, achievements and the in-game coin shop for a web game.",
5
5
  "type": "module",
6
6
  "license": "UNLICENSED",
package/src/tesana.js CHANGED
@@ -50,7 +50,13 @@
50
50
  // 1.3.0 files a game's level events, its quits and a play heartbeat every 15 s
51
51
  // in Tesana's own event registry (`POST /api/e`, TES-2197), from Tesana's play
52
52
  // hosts only, and adds `tesana.gameplay.levelFail(levelId, reason?)`.
53
- const VERSION = "1.3.0";
53
+ //
54
+ // 1.3.2 (TES-2597): a room reconnect whose grant is past its `exp` asks for the
55
+ // room again first, so a drop after two hours in a room comes back without a
56
+ // refusal; if that fails, the held URL is offered once per drop (TES-2763), and
57
+ // a re-join that answers after the player left or moved rooms is dropped
58
+ // (TES-2764).
59
+ const VERSION = "1.3.2";
54
60
 
55
61
  const LOCAL_CACHE = "tesana.play.v1";
56
62
 
@@ -70,6 +76,10 @@ const MP_RECONNECT_MAX_ATTEMPTS = 8;
70
76
  //: up on that attempt. The answer is a token mint; a host that is slow must not
71
77
  //: hold a request open indefinitely.
72
78
  const MP_HOST_ASK_TIMEOUT_MS = 5000;
79
+ //: How close to its `exp` a room grant counts as expired for a reconnect. A
80
+ //: device clock that runs a little slow would otherwise offer a grant the room
81
+ //: host has just stopped taking; re-joining a little early costs one request.
82
+ const MP_GRANT_EXPIRY_MARGIN_MS = 30000;
73
83
  //: How long `create`/`quickJoin` will wait for a room that is being placed.
74
84
  //:
75
85
  //: Room creation on GameLift is asynchronous, so the service answers 202 with
@@ -459,9 +469,12 @@ function startListening() {
459
469
  // The site posts `{ type, payload }`; a baked or spliced bootstrap is the
460
470
  // payload itself. Accept both, so one SDK reads either host.
461
471
  if (data.type === "tesana-bootstrap") {
472
+ const next = data.payload && typeof data.payload === "object" ? data.payload : data;
473
+ // Whatever happens to the answer, events stop carrying a token it lacks.
474
+ eventsDropStaleToken(next);
462
475
  // `applyBootstrap` decides whether this answer may update a live session:
463
476
  // one the client asked for may, one it did not may not.
464
- applyBootstrap(data.payload && typeof data.payload === "object" ? data.payload : data);
477
+ applyBootstrap(next);
465
478
  return;
466
479
  }
467
480
  if (data.type === "tesana-wallet-changed" && bootstrap) {
@@ -590,6 +603,26 @@ function requestHostBootstrap() {
590
603
  return bootstrapAskPromise;
591
604
  }
592
605
 
606
+ /**
607
+ * Whether the player token a room URL carries has passed its own `exp`.
608
+ *
609
+ * Read without checking the signature: this only decides whether a reconnect
610
+ * asks for the room again before connecting. The room host still verifies
611
+ * every grant, so an expired token is refused there whatever this says. A URL
612
+ * whose token is not a JWT, or has no `exp`, is never called expired.
613
+ *
614
+ * @param {string} wsUrl
615
+ */
616
+ function roomGrantExpired(wsUrl) {
617
+ try {
618
+ const token = new URL(String(wsUrl)).searchParams.get("token");
619
+ const exp = Number(eventsClaims(token)?.exp);
620
+ return Number.isFinite(exp) && exp > 0 && exp * 1000 <= Date.now() + MP_GRANT_EXPIRY_MARGIN_MS;
621
+ } catch {
622
+ return false;
623
+ }
624
+ }
625
+
593
626
  function deviceId() {
594
627
  const cache = readCache();
595
628
  if (cache.deviceId) return cache.deviceId;
@@ -999,6 +1032,15 @@ const gameplay = Object.freeze({
999
1032
  // - No device id (TES-2639): the SDK's device id can be traded for that
1000
1033
  // guest's play token, so it never leaves for analytics. The browser is
1001
1034
  // named by its own random `anonymous_id`.
1035
+ // - No user id, ever (TES-2722). A signed-in player's batch carries the
1036
+ // site's play session token (the host's `token`, the one the shop calls
1037
+ // send), and the collector checks it and names the account itself; a
1038
+ // forged, expired or other game's token names no one. Only that token: a
1039
+ // play service token (a JWT, which is what a guest holds) never goes. In
1040
+ // the body, because a beacon can't carry an Authorization header. Only to
1041
+ // Tesana's own collectors (TES-2729): a collector a host or the game names
1042
+ // gets the events without it. And a host answer without it (a sign-out)
1043
+ // takes it away at once, even an answer the session refuses (TES-2730).
1002
1044
  //
1003
1045
  // A heartbeat marks 15 s of time in front (TES-2641): the clock runs only while
1004
1046
  // the page is visible and resumes where it stopped, so coming back to the tab
@@ -1016,6 +1058,8 @@ const EVENTS_COLLECTORS = new Map([
1016
1058
  ["play.tesana.ai", "https://api.tesana.ai/api/e"],
1017
1059
  ["play-staging.tesana.ai", "https://api-staging.onetap.build/api/e"],
1018
1060
  ]);
1061
+ //: Tesana's own collectors: the only ones a session token goes to (TES-2729).
1062
+ const EVENTS_OWN_COLLECTORS = new Set(EVENTS_COLLECTORS.values());
1019
1063
  //: A collector a host names: https, or plain http on this machine for a developer.
1020
1064
  const EVENTS_URL = /^(https:\/\/[^\s/]+|http:\/\/(127\.0\.0\.1|localhost)(:\d+)?)\/\S*$/i;
1021
1065
  const EVENTS_FLUSH_MS = 30000;
@@ -1138,6 +1182,38 @@ function eventsPlayPage(merged) {
1138
1182
  return "";
1139
1183
  }
1140
1184
 
1185
+ /**
1186
+ * The site's play session token, for the collector to name a signed-in player
1187
+ * by (TES-2722), or "". Tesana's own API issued it and only it can check it. A
1188
+ * play service token is a JWT and never goes: a guest's is all a guest holds.
1189
+ */
1190
+ function eventsSessionToken(merged) {
1191
+ const token = typeof merged.token === "string" ? merged.token.trim() : "";
1192
+ if (!token || token.length > 512 || /\s/.test(token)) return "";
1193
+ return token.split(".").length === 3 ? "" : token;
1194
+ }
1195
+
1196
+ /**
1197
+ * A host's answer that doesn't carry the session token the batches carry (a
1198
+ * sign-out, another account, a session that couldn't be minted) takes it away
1199
+ * at once (TES-2730). Run on every answer the host posts, before
1200
+ * `applyBootstrap`, which keeps a live session's identity and so refuses an
1201
+ * unasked guest answer: events after a sign-out must not carry the old
1202
+ * account. What was queued goes first, with the token it was played under.
1203
+ * Dropping a token only makes events anonymous, so any poster may cause it.
1204
+ */
1205
+ function eventsDropStaleToken(next) {
1206
+ try {
1207
+ const shared = eventsState.shared;
1208
+ if (!shared || !shared.token) return;
1209
+ if (eventsSessionToken(next && typeof next === "object" ? next : {}) === shared.token) return;
1210
+ eventsFlush();
1211
+ delete shared.token;
1212
+ } catch {
1213
+ /* never */
1214
+ }
1215
+ }
1216
+
1141
1217
  /** This browser's anonymous id: the collector's cookie wins when it has one. */
1142
1218
  function eventsAnonymousId() {
1143
1219
  const cached = readCache().anonymousId;
@@ -1168,6 +1244,13 @@ function eventsConfigure(merged) {
1168
1244
  const game = eventsGame(merged);
1169
1245
  if (!game || page !== "yes") return;
1170
1246
  const build = String(merged.build || merged.buildId || merged.buildVersion || "").slice(0, 64);
1247
+ const session = EVENTS_OWN_COLLECTORS.has(url) ? eventsSessionToken(merged) : "";
1248
+ // Rebuilt on every bootstrap the session accepts: a refreshed token
1249
+ // replaces the old one. What was queued goes first, under the token it was
1250
+ // played with, so a guest's play before signing in isn't stamped with the
1251
+ // account (a sign-out is eventsDropStaleToken's).
1252
+ const before = eventsState.shared;
1253
+ if (eventsState.state === "on" && before && (before.token || "") !== session) eventsFlush();
1171
1254
  eventsState.url = url;
1172
1255
  eventsState.shared = {
1173
1256
  surface: window.TesanaDesktop ? "desktop" : "sdk",
@@ -1175,6 +1258,7 @@ function eventsConfigure(merged) {
1175
1258
  ...(build ? { build_sha: build } : {}),
1176
1259
  anonymous_id: eventsAnonymousId(),
1177
1260
  ...game,
1261
+ ...(session ? { token: session } : {}),
1178
1262
  };
1179
1263
  eventsState.state = "on";
1180
1264
  eventsListen();
@@ -2042,6 +2126,10 @@ class TesanaClient {
2042
2126
  // expired, most likely — and a fresh one has to be minted before the next
2043
2127
  // reconnect can succeed. Cleared once a reconnect has used it.
2044
2128
  lastCloseCode: null,
2129
+ // Whether this drop has already offered a grant the device judged
2130
+ // expired, after a re-join failed (TES-2763). Once per drop, because the
2131
+ // device's clock may be wrong and the room host's check is the real one.
2132
+ expiredGrantOffered: false,
2045
2133
  reconnectExhausted: false,
2046
2134
  statusHandlers: [],
2047
2135
  // Overridable so a test (or a game with unusual tolerance) can shorten
@@ -2634,7 +2722,10 @@ class TesanaClient {
2634
2722
  let opened = false;
2635
2723
  const timer = setTimeout(() => {
2636
2724
  if (!opened) {
2637
- this._mpClose();
2725
+ // Only this attempt's own socket (TES-2764). If the game has since
2726
+ // entered another room, `this._mp.ws` is that room's, and closing it
2727
+ // here would throw the player out of where they chose to be.
2728
+ if (this._mp.ws === ws) this._mpClose();
2638
2729
  reject(new Error("multiplayer connect timeout"));
2639
2730
  }
2640
2731
  }, 8000);
@@ -2655,6 +2746,7 @@ class TesanaClient {
2655
2746
  this._mpEmitStatus({ status: "reconnected" });
2656
2747
  }
2657
2748
  this._mp.reconnectAttempts = 0;
2749
+ this._mp.expiredGrantOffered = false;
2658
2750
  resolve(msg);
2659
2751
  }
2660
2752
  for (const fn of this._mp.handlers) {
@@ -2708,6 +2800,10 @@ class TesanaClient {
2708
2800
  const mp = this._mp;
2709
2801
  if (!mp.wsUrl || mp.leaving) return;
2710
2802
  if (mp.reconnectTimer) return;
2803
+ // A socket open or opening means there is nothing to get back into: a
2804
+ // stale attempt that failed after the game entered another room must not
2805
+ // schedule a reconnect over the live one (TES-2764).
2806
+ if (mp.ws) return;
2711
2807
  const attempts = (mp.reconnectAttempts || 0) + 1;
2712
2808
  mp.reconnectAttempts = attempts;
2713
2809
  if (attempts > mp.reconnectMaxAttempts) {
@@ -2730,8 +2826,8 @@ class TesanaClient {
2730
2826
  }
2731
2827
 
2732
2828
  /**
2733
- * One reconnect: get a current grant if the last one was refused, then try the
2734
- * room again.
2829
+ * One reconnect: get a current grant if the last one was refused or has
2830
+ * expired, then try the room again.
2735
2831
  *
2736
2832
  * The grant the client holds is embedded in `wsUrl` and was minted when it
2737
2833
  * first joined. A game open longer than the token's life therefore cannot
@@ -2748,32 +2844,61 @@ class TesanaClient {
2748
2844
  */
2749
2845
  async _mpReconnectAttempt() {
2750
2846
  const mp = this._mp;
2751
- let wsUrl = mp.wsUrl;
2847
+ // What this attempt is for. The re-join below can take seconds (a host mint,
2848
+ // a placement wait), and the game may leave or enter another room meanwhile.
2849
+ const roomId = mp.roomId;
2850
+ const heldUrl = mp.wsUrl;
2851
+ let wsUrl = heldUrl;
2852
+ // A grant whose own `exp` has passed can only be refused (TES-2597). Offering
2853
+ // it anyway cost every reconnect after two hours in a room one refusal and
2854
+ // one backoff step, and when the re-join then failed, each later attempt
2855
+ // offered the same dead URL again, eight refusals and the player gone. So an
2856
+ // expired grant is treated as refused before it is offered.
2857
+ const expired = roomGrantExpired(wsUrl);
2752
2858
  // 4401 — the grant was refused, so the URL itself is dead and a fresh one is
2753
2859
  // required. 4409 — the room is being re-placed and has no address yet, so
2754
2860
  // the URL the client holds is not usable either. Both are fixed by asking
2755
2861
  // for the room again; 4403 (the game is denied rooms) and 4404 (the room is
2756
2862
  // gone) are not, and fall through to the URL we have so the attempt is still
2757
2863
  // counted and the backoff still applies.
2758
- if ((mp.lastCloseCode === 4401 || mp.lastCloseCode === 4409) && mp.roomId) {
2864
+ if ((expired || mp.lastCloseCode === 4401 || mp.lastCloseCode === 4409) && roomId) {
2865
+ let session = null;
2866
+ let failure = null;
2759
2867
  try {
2760
2868
  // `_mpAwaitPlacement` is harmless when the room is already up (it
2761
2869
  // returns the session untouched) and necessary when a reconnect lands
2762
2870
  // during a re-placement — a 4409 close, or a room swept and reopened,
2763
2871
  // answers with no `wsUrl`, and retrying the old URL would never work.
2764
- const session = await this._mpAwaitPlacement(
2765
- await this._mpRequest("POST", `/v1/mp/rooms/${enc(mp.roomId)}/join`),
2872
+ session = await this._mpAwaitPlacement(
2873
+ await this._mpRequest("POST", `/v1/mp/rooms/${enc(roomId)}/join`),
2766
2874
  );
2767
- if (session && session.wsUrl) {
2768
- wsUrl = session.wsUrl;
2769
- mp.wsUrl = wsUrl;
2770
- mp.code = session.room?.code ?? mp.code;
2875
+ } catch (err) {
2876
+ failure = err;
2877
+ }
2878
+ // The player left, or went into another room, while the re-join was out.
2879
+ // Its answer is for a room they are no longer in: drop it and do not
2880
+ // reconnect (TES-2764). Resolving, not throwing, so no retry is scheduled.
2881
+ if (mp.leaving || mp.roomId !== roomId || mp.wsUrl !== heldUrl) return null;
2882
+ if (session && session.wsUrl) {
2883
+ wsUrl = session.wsUrl;
2884
+ mp.wsUrl = wsUrl;
2885
+ mp.code = session.room?.code ?? mp.code;
2886
+ } else if (failure && expired) {
2887
+ // The device's clock says the grant is expired, but the clock may run
2888
+ // fast, and the room host's check is the one that counts (TES-2763). So
2889
+ // the held URL is still offered, once per drop: a grant that really has
2890
+ // expired costs one refusal, not eight, and one that has not gets the
2891
+ // player back in while the HTTP API is down. After that, the attempt
2892
+ // fails here and the backoff asks for the room again.
2893
+ if (mp.expiredGrantOffered) {
2894
+ mp.lastCloseCode = null;
2895
+ throw failure;
2771
2896
  }
2772
- } catch {
2773
- // The room is gone, or the fresh grant was refused too. Fall through to
2774
- // the URL we have: the room may have been replaced under the same id,
2775
- // and if it has not, the next attempt retries this same path.
2897
+ mp.expiredGrantOffered = true;
2776
2898
  }
2899
+ // Otherwise the room is gone, or the fresh grant was refused too. Fall
2900
+ // through to the URL we have: the room may have been replaced under the
2901
+ // same id, and if it has not, the next attempt retries this same path.
2777
2902
  }
2778
2903
  mp.lastCloseCode = null;
2779
2904
  return this._mpOpen(wsUrl);
@@ -2858,6 +2983,7 @@ class TesanaClient {
2858
2983
  this._mp.reconnectAttempts = 0;
2859
2984
  this._mp.reconnectExhausted = false;
2860
2985
  this._mp.lastCloseCode = null;
2986
+ this._mp.expiredGrantOffered = false;
2861
2987
  try {
2862
2988
  if (!roomId) return { ok: true, room: null };
2863
2989
  try {