@trpc/client 11.31.5 → 11.31.7

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 CHANGED
@@ -7,7 +7,7 @@ const require_httpLink = require("./httpLink-C7hAx__E.cjs");
7
7
  const require_httpBatchLink = require("./httpBatchLink-D5PwsSuY.cjs");
8
8
  const require_unstable_internals = require("./unstable-internals.cjs");
9
9
  const require_links_loggerLink = require("./links/loggerLink.cjs");
10
- const require_wsLink = require("./wsLink-vHwF4z7F.cjs");
10
+ const require_wsLink = require("./wsLink-CZyAXvMe.cjs");
11
11
  let _trpc_server_observable = require("@trpc/server/observable");
12
12
  let _trpc_server_unstable_core_do_not_import = require("@trpc/server/unstable-core-do-not-import");
13
13
  let _trpc_server = require("@trpc/server");
package/dist/index.d.cts CHANGED
@@ -4,7 +4,7 @@ import { n as HTTPBatchLinkOptions, t as httpBatchLink } from "./httpBatchLink-D
4
4
  import { HTTPLinkOptions, httpLink } from "./links/httpLink.cjs";
5
5
  import { LoggerLinkOptions, loggerLink } from "./links/loggerLink.cjs";
6
6
  import { splitLink } from "./links/splitLink.cjs";
7
- import { a as WebSocketClientOptions, c as jsonEncoder, i as createWSClient, n as wsLink, o as UrlOptionsWithConnectionParams, r as TRPCWebSocketClient, s as Encoder, t as WebSocketLinkOptions } from "./wsLink-CEooxn80.cjs";
7
+ import { a as WebSocketClientOptions, c as jsonEncoder, i as createWSClient, n as wsLink, o as UrlOptionsWithConnectionParams, r as TRPCWebSocketClient, s as Encoder, t as WebSocketLinkOptions } from "./wsLink-BlAMQb0i.cjs";
8
8
  import { TransformerOptions } from "./unstable-internals.cjs";
9
9
  import { t as HTTPLinkBaseOptions } from "./httpUtils-CxgZ3W-1.cjs";
10
10
  import { AnyClientTypes, AnyProcedure, AnyRouter, ErrorHandlerOptions, EventSourceLike, InferrableClientTypes, ProcedureType, RouterRecord, TypeError, inferAsyncIterableYield, inferClientTypes, inferProcedureErrorShape, inferProcedureInput, inferRouterContext, inferTransformedProcedureOutput } from "@trpc/server/unstable-core-do-not-import";
package/dist/index.d.mts CHANGED
@@ -4,7 +4,7 @@ import { n as HTTPBatchLinkOptions, t as httpBatchLink } from "./httpBatchLink-D
4
4
  import { n as httpLink, t as HTTPLinkOptions } from "./httpLink-BjZdBzr6.mjs";
5
5
  import { n as loggerLink, t as LoggerLinkOptions } from "./loggerLink-CBPx95zm.mjs";
6
6
  import { t as splitLink } from "./splitLink-BtPz7lCB.mjs";
7
- import { a as WebSocketClientOptions, c as jsonEncoder, i as createWSClient, n as wsLink, o as UrlOptionsWithConnectionParams, r as TRPCWebSocketClient, s as Encoder, t as WebSocketLinkOptions } from "./wsLink-CnVTvoqf.mjs";
7
+ import { a as WebSocketClientOptions, c as jsonEncoder, i as createWSClient, n as wsLink, o as UrlOptionsWithConnectionParams, r as TRPCWebSocketClient, s as Encoder, t as WebSocketLinkOptions } from "./wsLink-DU5wj6kH.mjs";
8
8
  import { n as TransformerOptions } from "./unstable-internals-CnrO49lh.mjs";
9
9
  import { t as HTTPLinkBaseOptions } from "./httpUtils-gZ7bAQ6T.mjs";
10
10
  import { Unsubscribable } from "@trpc/server/observable";
package/dist/index.mjs CHANGED
@@ -6,7 +6,7 @@ import { i as isOctetType, n as isFormData, r as isNonJsonSerializable, t as htt
6
6
  import { n as dataLoader, t as httpBatchLink } from "./httpBatchLink-BkVNtNND.mjs";
7
7
  import { getTransformer } from "./unstable-internals.mjs";
8
8
  import { loggerLink } from "./links/loggerLink.mjs";
9
- import { i as jsonEncoder, n as createWSClient, r as resultOf, t as wsLink } from "./wsLink-DSmHMirJ.mjs";
9
+ import { i as jsonEncoder, n as createWSClient, r as resultOf, t as wsLink } from "./wsLink-C_Zg2yXV.mjs";
10
10
  import { behaviorSubject, isObservable, observable, observableToAsyncIterable, observableToPromise, share } from "@trpc/server/observable";
11
11
  import { callProcedure, createFlatProxy, createRecursiveProxy, isAbortError, isAsyncIterable, isPlainObject, iteratorResource, jsonlStreamConsumer, makeResource, resolvePathSeparator, retryableRpcCodes, run, sseStreamConsumer } from "@trpc/server/unstable-core-do-not-import";
12
12
  import { getTRPCErrorFromUnknown, getTRPCErrorShape, isTrackedEnvelope } from "@trpc/server";
@@ -1,5 +1,5 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_wsLink = require("../../wsLink-vHwF4z7F.cjs");
2
+ const require_wsLink = require("../../wsLink-CZyAXvMe.cjs");
3
3
  exports.createWSClient = require_wsLink.createWSClient;
4
4
  exports.jsonEncoder = require_wsLink.jsonEncoder;
5
5
  exports.wsLink = require_wsLink.wsLink;
@@ -1,2 +1,2 @@
1
- import { a as WebSocketClientOptions, c as jsonEncoder, i as createWSClient, n as wsLink, r as TRPCWebSocketClient, s as Encoder, t as WebSocketLinkOptions } from "../../wsLink-CEooxn80.cjs";
1
+ import { a as WebSocketClientOptions, c as jsonEncoder, i as createWSClient, n as wsLink, r as TRPCWebSocketClient, s as Encoder, t as WebSocketLinkOptions } from "../../wsLink-BlAMQb0i.cjs";
2
2
  export { type Encoder, type TRPCWebSocketClient, type WebSocketClientOptions, WebSocketLinkOptions, createWSClient, jsonEncoder, wsLink };
@@ -1,2 +1,2 @@
1
- import { a as WebSocketClientOptions, c as jsonEncoder, i as createWSClient, n as wsLink, r as TRPCWebSocketClient, s as Encoder, t as WebSocketLinkOptions } from "../../wsLink-CnVTvoqf.mjs";
1
+ import { a as WebSocketClientOptions, c as jsonEncoder, i as createWSClient, n as wsLink, r as TRPCWebSocketClient, s as Encoder, t as WebSocketLinkOptions } from "../../wsLink-DU5wj6kH.mjs";
2
2
  export { type Encoder, type TRPCWebSocketClient, type WebSocketClientOptions, WebSocketLinkOptions, createWSClient, jsonEncoder, wsLink };
@@ -1 +1 @@
1
- export { n as createWSClient, i as jsonEncoder, t as wsLink } from "../../wsLink-DSmHMirJ.mjs";
1
+ export { n as createWSClient, i as jsonEncoder, t as wsLink } from "../../wsLink-C_Zg2yXV.mjs";
@@ -210,8 +210,9 @@ declare class WsClient {
210
210
  * error, e.g. because `createContext` threw. The server closes the socket
211
211
  * right after, so the reconnect that follows carries on with the backoff
212
212
  * instead of starting over at attempt 0. Cleared again when the socket
213
- * receives a result, which proves the connection was accepted. Also set
214
- * on a keepAlive pong timeout when the socket never received anything.
213
+ * receives a result, which proves the connection was accepted, and by
214
+ * `close()`. Also set on a keepAlive pong timeout when the socket never
215
+ * received anything.
215
216
  * @see https://github.com/trpc/trpc/issues/4774
216
217
  */
217
218
  private connectionRejected;
@@ -306,4 +307,4 @@ type WebSocketLinkOptions<TRouter extends AnyRouter> = {
306
307
  */
307
308
  declare function wsLink<TRouter extends AnyRouter>(opts: WebSocketLinkOptions<TRouter>): TRPCLink<TRouter>;
308
309
  export { WebSocketClientOptions as a, jsonEncoder as c, createWSClient as i, wsLink as n, UrlOptionsWithConnectionParams as o, TRPCWebSocketClient as r, Encoder as s, WebSocketLinkOptions as t };
309
- //# sourceMappingURL=wsLink-CEooxn80.d.cts.map
310
+ //# sourceMappingURL=wsLink-BlAMQb0i.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"wsLink-CEooxn80.d.cts","names":[],"sources":["../src/links/wsLink/wsClient/encoder.ts","../src/links/internals/urlWithConnectionParams.ts","../src/links/wsLink/wsClient/options.ts","../src/links/wsLink/wsClient/wsClient.ts","../src/links/wsLink/createWsClient.ts","../src/links/wsLink/wsLink.ts"],"mappings":";;;;;;;;;;cAOa,aAAa;;;;KCWd,gBAAgB,KAAK,WAAW,IAAI,QAAQ;UAEvC;;;;EAIf,KAAK;;;;;;;EAQL,mBAAmB,gBAAgB;;;;;;;UCxBpB,+BAA+B;;;;EAI9C,mBAAmB;;;;;EAKnB,gBAAgB;;;;EAIhB;;;;EAIA,WAAW,MAAM;;;;EAIjB,WAAW;IAAU;;;;;EAIrB;;;;;IAKE;;;;;IAKA;;;;;EAKF;;;;IAIE;;;;;IAKA;;;;;IAKA;;;;;;EAMF,uBAAuB;;;;;;cCWZ;;;;WAIK,iBAAiB,gBAC/B,oBAAoB,gBAAgB;UAG9B;UACA;;;;;;;UASA;mBACS;;;;;;UAMT;mBACS;UACT;;;;;UAKA;mBACS;mBAIA;mBACA;mBACA;EAEjB,YAAY,MAAM;;;;;;UA4DV;UAUA;;;;;UAeM;;;;;EAiCD,SAAK;;;;;;;;;;;EAiDX,UACL,MAAM,MAAM,MAAM,OAAO,UACzB,aACA;IAEA,IAAI,KAAK;IACT,aAAa;IACb;wCACD,WAAA,iCAAA,gBAAA,iBAAA,gBAAA;;;;MAwEU;;;;;;;;;;;;;;;;;;UAaH;;;;UAIA;;;;;UAKA;;;;;;UAMA;;;;UAIA;;;;;;;;;;UAUA;;mBAES;;;;;;;;;UAUT;;;;;;;;UAsFA;;;;;;;UA8CA;UAiBA;UASA;;;;UAaA;;;;;;UAWA;;;;;UAaA;UA0BA;UA4MA;UA+DA;;;;UASA;;;;;;UAqBA;;;;;UAWA;;;;;;;;iBC36BM,eAAe,MAAM,yBAAsB;;;;KAO/C,sBAAsB,kBAAkB;;;;;;KCIxC,qBAAqB,gBAAgB;;;;EAI/C,QAAQ;IACN,mBAAmB,iBAAiB;;;;;;iBAOxB,OAAO,gBAAgB,WACrC,MAAM,qBAAqB,WAC1B,SAAS"}
1
+ {"version":3,"file":"wsLink-BlAMQb0i.d.cts","names":[],"sources":["../src/links/wsLink/wsClient/encoder.ts","../src/links/internals/urlWithConnectionParams.ts","../src/links/wsLink/wsClient/options.ts","../src/links/wsLink/wsClient/wsClient.ts","../src/links/wsLink/createWsClient.ts","../src/links/wsLink/wsLink.ts"],"mappings":";;;;;;;;;;cAOa,aAAa;;;;KCWd,gBAAgB,KAAK,WAAW,IAAI,QAAQ;UAEvC;;;;EAIf,KAAK;;;;;;;EAQL,mBAAmB,gBAAgB;;;;;;;UCxBpB,+BAA+B;;;;EAI9C,mBAAmB;;;;;EAKnB,gBAAgB;;;;EAIhB;;;;EAIA,WAAW,MAAM;;;;EAIjB,WAAW;IAAU;;;;;EAIrB;;;;;IAKE;;;;;IAKA;;;;;EAKF;;;;IAIE;;;;;IAKA;;;;;IAKA;;;;;;EAMF,uBAAuB;;;;;;cCWZ;;;;WAIK,iBAAiB,gBAC/B,oBAAoB,gBAAgB;UAG9B;UACA;;;;;;;UASA;mBACS;;;;;;UAMT;mBACS;UACT;;;;;UAKA;mBACS;mBAIA;mBACA;mBACA;EAEjB,YAAY,MAAM;;;;;;UA4DV;UAUA;;;;;UAeM;;;;;EAmDD,SAAK;;;;;;;;;;;EAqDX,UACL,MAAM,MAAM,MAAM,OAAO,UACzB,aACA;IAEA,IAAI,KAAK;IACT,aAAa;IACb;wCACD,WAAA,iCAAA,gBAAA,iBAAA,gBAAA;;;;MAwEU;;;;;;;;;;;;;;;;;;UAaH;;;;UAIA;;;;;UAKA;;;;;;UAMA;;;;UAIA;;;;;;;;;;;UAWA;;mBAES;;;;;;;;;UAUT;;;;;;;;UA2FA;;;;;;;UA8CA;UAiBA;UASA;;;;UAaA;;;;;;UAWA;;;;;UAaA;UA0BA;UAqNA;UA+DA;;;;UASA;;;;;;UAqBA;;;;;UAWA;;;;;;;;iBCh9BM,eAAe,MAAM,yBAAsB;;;;KAO/C,sBAAsB,kBAAkB;;;;;;KCIxC,qBAAqB,gBAAgB;;;;EAI/C,QAAQ;IACN,mBAAmB,iBAAiB;;;;;;iBAOxB,OAAO,gBAAgB,WACrC,MAAM,qBAAqB,WAC1B,SAAS"}
@@ -613,6 +613,15 @@ var WsClient = class {
613
613
  async open() {
614
614
  this.allowReconnect = true;
615
615
  if (this.reconnecting) return this.reconnecting;
616
+ if (this.connectionRejected) {
617
+ if (this.connectionState.get().state !== "connecting") callUserCallback(() => this.connectionState.next({
618
+ type: "state",
619
+ state: "connecting",
620
+ error: null
621
+ }));
622
+ this.reconnect(null);
623
+ return this.reconnecting;
624
+ }
616
625
  this.connectionAttemptIndex = 0;
617
626
  if (this.connectionState.get().state === "idle") callUserCallback(() => this.connectionState.next({
618
627
  type: "state",
@@ -635,8 +644,12 @@ var WsClient = class {
635
644
  * Ensures all outgoing and pending requests are properly finalized.
636
645
  */
637
646
  async close() {
638
- var _this$nextConnection;
647
+ var _nextConnection$ws;
639
648
  this.allowReconnect = false;
649
+ this.connectionRejected = false;
650
+ this.reconnecting = null;
651
+ const nextConnection = this.nextConnection;
652
+ this.nextConnection = null;
640
653
  this.replacementError = null;
641
654
  this.retiredConnection = null;
642
655
  this.inactivityTimeoutEnabled = false;
@@ -646,7 +659,7 @@ var WsClient = class {
646
659
  else if (request.state === "outgoing") callUserCallback(() => request.callbacks.error(require_TRPCClientError.TRPCClientError.from(new TRPCWebSocketClosedError({ message: "Closed before connection was established" }))));
647
660
  else requestsToAwait.push(request.end);
648
661
  await Promise.all(requestsToAwait).catch(() => null);
649
- (_this$nextConnection = this.nextConnection) === null || _this$nextConnection === void 0 || (_this$nextConnection = _this$nextConnection.ws) === null || _this$nextConnection === void 0 || _this$nextConnection.close();
662
+ nextConnection === null || nextConnection === void 0 || (_nextConnection$ws = nextConnection.ws) === null || _nextConnection$ws === void 0 || _nextConnection$ws.close();
650
663
  await this.activeConnection.close().catch(() => null);
651
664
  callUserCallback(() => this.connectionState.next({
652
665
  type: "state",
@@ -714,8 +727,8 @@ var WsClient = class {
714
727
  * The current WebSocket and its state, or `null` when there is no socket.
715
728
  */
716
729
  get connection() {
717
- var _this$nextConnection2;
718
- return backwardCompatibility(this.activeConnection.ws ? this.activeConnection : (_this$nextConnection2 = this.nextConnection) !== null && _this$nextConnection2 !== void 0 ? _this$nextConnection2 : this.activeConnection);
730
+ var _this$nextConnection;
731
+ return backwardCompatibility(this.activeConnection.ws ? this.activeConnection : (_this$nextConnection = this.nextConnection) !== null && _this$nextConnection !== void 0 ? _this$nextConnection : this.activeConnection);
719
732
  }
720
733
  /**
721
734
  * Opens a new connection, retrying until one opens. While the current
@@ -738,10 +751,11 @@ var WsClient = class {
738
751
  }));
739
752
  }
740
753
  if (this.reconnecting) return;
754
+ const isCurrent = () => this.allowReconnect && this.reconnecting === reconnecting;
741
755
  const tryReconnect = async (attemptIndex) => {
742
756
  try {
743
757
  await (0, _trpc_server_unstable_core_do_not_import.sleep)(this.reconnectRetryDelay(attemptIndex));
744
- if (this.allowReconnect) {
758
+ if (isCurrent()) {
745
759
  const next = this.createConnection();
746
760
  this.nextConnection = next;
747
761
  try {
@@ -751,9 +765,9 @@ var WsClient = class {
751
765
  await next.close().catch(() => null);
752
766
  throw error;
753
767
  } finally {
754
- this.nextConnection = null;
768
+ if (this.nextConnection === next) this.nextConnection = null;
755
769
  }
756
- if (this.allowReconnect) {
770
+ if (isCurrent()) {
757
771
  const previous = this.activeConnection;
758
772
  this.activeConnection = next;
759
773
  this.retiredConnection = previous.isOpen() ? previous : null;
@@ -762,14 +776,14 @@ var WsClient = class {
762
776
  this.handOff(previous.ws);
763
777
  } else await next.close();
764
778
  }
765
- this.reconnecting = null;
779
+ if (this.reconnecting === reconnecting) this.reconnecting = null;
766
780
  } catch (error) {
767
781
  const closedError = new TRPCWebSocketClosedError({
768
782
  message: "Initialization error",
769
783
  cause: error
770
784
  });
771
- if (this.allowReconnect && this.activeConnection.isOpen()) this.replacementError = closedError;
772
- else if (this.allowReconnect) callUserCallback(() => this.connectionState.next({
785
+ if (isCurrent() && this.activeConnection.isOpen()) this.replacementError = closedError;
786
+ else if (isCurrent()) callUserCallback(() => this.connectionState.next({
773
787
  type: "state",
774
788
  state: "connecting",
775
789
  error: require_TRPCClientError.TRPCClientError.from(closedError)
@@ -777,7 +791,8 @@ var WsClient = class {
777
791
  await tryReconnect(attemptIndex + 1);
778
792
  }
779
793
  };
780
- this.reconnecting = tryReconnect(this.connectionRejected ? this.connectionAttemptIndex + 1 : 0);
794
+ const reconnecting = tryReconnect(this.connectionRejected ? this.connectionAttemptIndex + 1 : 0);
795
+ this.reconnecting = reconnecting;
781
796
  }
782
797
  /**
783
798
  * Moves the pending subscriptions to the active socket, which has just
@@ -877,7 +892,8 @@ var WsClient = class {
877
892
  return subscriptions;
878
893
  }
879
894
  setupWebSocketListeners(ws) {
880
- this.connectionRejected = false;
895
+ var _this$nextConnection2;
896
+ if (ws === this.activeConnection.ws || ws === ((_this$nextConnection2 = this.nextConnection) === null || _this$nextConnection2 === void 0 ? void 0 : _this$nextConnection2.ws)) this.connectionRejected = false;
881
897
  /**
882
898
  * Fails the queries and mutations sent on `ws`. Its subscriptions are
883
899
  * sent again on the active socket if that's open, or else by
@@ -922,13 +938,14 @@ var WsClient = class {
922
938
  };
923
939
  ws.addEventListener("open", () => {
924
940
  (0, _trpc_server_unstable_core_do_not_import.run)(async () => {
925
- var _this$callbacks$onOpe, _this$callbacks;
926
- if (this.lazyMode) {
941
+ var _this$nextConnection3, _this$callbacks$onOpe, _this$callbacks;
942
+ const isLive = ws === this.activeConnection.ws || ws === ((_this$nextConnection3 = this.nextConnection) === null || _this$nextConnection3 === void 0 ? void 0 : _this$nextConnection3.ws);
943
+ if (this.lazyMode && isLive) {
927
944
  this.inactivityTimeoutEnabled = true;
928
945
  this.startInactivityTimeout();
929
946
  }
930
947
  (_this$callbacks$onOpe = (_this$callbacks = this.callbacks).onOpen) === null || _this$callbacks$onOpe === void 0 || _this$callbacks$onOpe.call(_this$callbacks);
931
- if (this.connectionState.get().state === "pending" || ws !== this.activeConnection.ws && this.activeConnection.isOpen()) return;
948
+ if (!isLive || this.connectionState.get().state === "pending" || ws !== this.activeConnection.ws && this.activeConnection.isOpen()) return;
932
949
  callUserCallback(() => this.connectionState.next({
933
950
  type: "state",
934
951
  state: "pending",
@@ -960,7 +977,7 @@ var WsClient = class {
960
977
  };
961
978
  const abandonedEvent = { code: 1006 };
962
979
  const onMessage = ({ data }) => {
963
- var _this$nextConnection3;
980
+ var _this$nextConnection4;
964
981
  if (closed) return;
965
982
  answered = true;
966
983
  this.inactivityTimeout.reset();
@@ -980,7 +997,7 @@ var WsClient = class {
980
997
  if (ws === this.activeConnection.ws) this.handleIncomingRequest(incomingMessage);
981
998
  return;
982
999
  }
983
- const isRetired = ws !== this.activeConnection.ws && ws !== ((_this$nextConnection3 = this.nextConnection) === null || _this$nextConnection3 === void 0 ? void 0 : _this$nextConnection3.ws);
1000
+ const isRetired = ws !== this.activeConnection.ws && ws !== ((_this$nextConnection4 = this.nextConnection) === null || _this$nextConnection4 === void 0 ? void 0 : _this$nextConnection4.ws);
984
1001
  if (!isRetired && incomingMessage.id === null && "error" in incomingMessage) this.connectionRejected = true;
985
1002
  else if (!isRetired && "result" in incomingMessage) this.connectionRejected = false;
986
1003
  this.handleResponseMessage(incomingMessage, ws);
@@ -613,6 +613,15 @@ var WsClient = class {
613
613
  async open() {
614
614
  this.allowReconnect = true;
615
615
  if (this.reconnecting) return this.reconnecting;
616
+ if (this.connectionRejected) {
617
+ if (this.connectionState.get().state !== "connecting") callUserCallback(() => this.connectionState.next({
618
+ type: "state",
619
+ state: "connecting",
620
+ error: null
621
+ }));
622
+ this.reconnect(null);
623
+ return this.reconnecting;
624
+ }
616
625
  this.connectionAttemptIndex = 0;
617
626
  if (this.connectionState.get().state === "idle") callUserCallback(() => this.connectionState.next({
618
627
  type: "state",
@@ -635,8 +644,12 @@ var WsClient = class {
635
644
  * Ensures all outgoing and pending requests are properly finalized.
636
645
  */
637
646
  async close() {
638
- var _this$nextConnection;
647
+ var _nextConnection$ws;
639
648
  this.allowReconnect = false;
649
+ this.connectionRejected = false;
650
+ this.reconnecting = null;
651
+ const nextConnection = this.nextConnection;
652
+ this.nextConnection = null;
640
653
  this.replacementError = null;
641
654
  this.retiredConnection = null;
642
655
  this.inactivityTimeoutEnabled = false;
@@ -646,7 +659,7 @@ var WsClient = class {
646
659
  else if (request.state === "outgoing") callUserCallback(() => request.callbacks.error(TRPCClientError.from(new TRPCWebSocketClosedError({ message: "Closed before connection was established" }))));
647
660
  else requestsToAwait.push(request.end);
648
661
  await Promise.all(requestsToAwait).catch(() => null);
649
- (_this$nextConnection = this.nextConnection) === null || _this$nextConnection === void 0 || (_this$nextConnection = _this$nextConnection.ws) === null || _this$nextConnection === void 0 || _this$nextConnection.close();
662
+ nextConnection === null || nextConnection === void 0 || (_nextConnection$ws = nextConnection.ws) === null || _nextConnection$ws === void 0 || _nextConnection$ws.close();
650
663
  await this.activeConnection.close().catch(() => null);
651
664
  callUserCallback(() => this.connectionState.next({
652
665
  type: "state",
@@ -714,8 +727,8 @@ var WsClient = class {
714
727
  * The current WebSocket and its state, or `null` when there is no socket.
715
728
  */
716
729
  get connection() {
717
- var _this$nextConnection2;
718
- return backwardCompatibility(this.activeConnection.ws ? this.activeConnection : (_this$nextConnection2 = this.nextConnection) !== null && _this$nextConnection2 !== void 0 ? _this$nextConnection2 : this.activeConnection);
730
+ var _this$nextConnection;
731
+ return backwardCompatibility(this.activeConnection.ws ? this.activeConnection : (_this$nextConnection = this.nextConnection) !== null && _this$nextConnection !== void 0 ? _this$nextConnection : this.activeConnection);
719
732
  }
720
733
  /**
721
734
  * Opens a new connection, retrying until one opens. While the current
@@ -738,10 +751,11 @@ var WsClient = class {
738
751
  }));
739
752
  }
740
753
  if (this.reconnecting) return;
754
+ const isCurrent = () => this.allowReconnect && this.reconnecting === reconnecting;
741
755
  const tryReconnect = async (attemptIndex) => {
742
756
  try {
743
757
  await sleep(this.reconnectRetryDelay(attemptIndex));
744
- if (this.allowReconnect) {
758
+ if (isCurrent()) {
745
759
  const next = this.createConnection();
746
760
  this.nextConnection = next;
747
761
  try {
@@ -751,9 +765,9 @@ var WsClient = class {
751
765
  await next.close().catch(() => null);
752
766
  throw error;
753
767
  } finally {
754
- this.nextConnection = null;
768
+ if (this.nextConnection === next) this.nextConnection = null;
755
769
  }
756
- if (this.allowReconnect) {
770
+ if (isCurrent()) {
757
771
  const previous = this.activeConnection;
758
772
  this.activeConnection = next;
759
773
  this.retiredConnection = previous.isOpen() ? previous : null;
@@ -762,14 +776,14 @@ var WsClient = class {
762
776
  this.handOff(previous.ws);
763
777
  } else await next.close();
764
778
  }
765
- this.reconnecting = null;
779
+ if (this.reconnecting === reconnecting) this.reconnecting = null;
766
780
  } catch (error) {
767
781
  const closedError = new TRPCWebSocketClosedError({
768
782
  message: "Initialization error",
769
783
  cause: error
770
784
  });
771
- if (this.allowReconnect && this.activeConnection.isOpen()) this.replacementError = closedError;
772
- else if (this.allowReconnect) callUserCallback(() => this.connectionState.next({
785
+ if (isCurrent() && this.activeConnection.isOpen()) this.replacementError = closedError;
786
+ else if (isCurrent()) callUserCallback(() => this.connectionState.next({
773
787
  type: "state",
774
788
  state: "connecting",
775
789
  error: TRPCClientError.from(closedError)
@@ -777,7 +791,8 @@ var WsClient = class {
777
791
  await tryReconnect(attemptIndex + 1);
778
792
  }
779
793
  };
780
- this.reconnecting = tryReconnect(this.connectionRejected ? this.connectionAttemptIndex + 1 : 0);
794
+ const reconnecting = tryReconnect(this.connectionRejected ? this.connectionAttemptIndex + 1 : 0);
795
+ this.reconnecting = reconnecting;
781
796
  }
782
797
  /**
783
798
  * Moves the pending subscriptions to the active socket, which has just
@@ -877,7 +892,8 @@ var WsClient = class {
877
892
  return subscriptions;
878
893
  }
879
894
  setupWebSocketListeners(ws) {
880
- this.connectionRejected = false;
895
+ var _this$nextConnection2;
896
+ if (ws === this.activeConnection.ws || ws === ((_this$nextConnection2 = this.nextConnection) === null || _this$nextConnection2 === void 0 ? void 0 : _this$nextConnection2.ws)) this.connectionRejected = false;
881
897
  /**
882
898
  * Fails the queries and mutations sent on `ws`. Its subscriptions are
883
899
  * sent again on the active socket if that's open, or else by
@@ -922,13 +938,14 @@ var WsClient = class {
922
938
  };
923
939
  ws.addEventListener("open", () => {
924
940
  run(async () => {
925
- var _this$callbacks$onOpe, _this$callbacks;
926
- if (this.lazyMode) {
941
+ var _this$nextConnection3, _this$callbacks$onOpe, _this$callbacks;
942
+ const isLive = ws === this.activeConnection.ws || ws === ((_this$nextConnection3 = this.nextConnection) === null || _this$nextConnection3 === void 0 ? void 0 : _this$nextConnection3.ws);
943
+ if (this.lazyMode && isLive) {
927
944
  this.inactivityTimeoutEnabled = true;
928
945
  this.startInactivityTimeout();
929
946
  }
930
947
  (_this$callbacks$onOpe = (_this$callbacks = this.callbacks).onOpen) === null || _this$callbacks$onOpe === void 0 || _this$callbacks$onOpe.call(_this$callbacks);
931
- if (this.connectionState.get().state === "pending" || ws !== this.activeConnection.ws && this.activeConnection.isOpen()) return;
948
+ if (!isLive || this.connectionState.get().state === "pending" || ws !== this.activeConnection.ws && this.activeConnection.isOpen()) return;
932
949
  callUserCallback(() => this.connectionState.next({
933
950
  type: "state",
934
951
  state: "pending",
@@ -960,7 +977,7 @@ var WsClient = class {
960
977
  };
961
978
  const abandonedEvent = { code: 1006 };
962
979
  const onMessage = ({ data }) => {
963
- var _this$nextConnection3;
980
+ var _this$nextConnection4;
964
981
  if (closed) return;
965
982
  answered = true;
966
983
  this.inactivityTimeout.reset();
@@ -980,7 +997,7 @@ var WsClient = class {
980
997
  if (ws === this.activeConnection.ws) this.handleIncomingRequest(incomingMessage);
981
998
  return;
982
999
  }
983
- const isRetired = ws !== this.activeConnection.ws && ws !== ((_this$nextConnection3 = this.nextConnection) === null || _this$nextConnection3 === void 0 ? void 0 : _this$nextConnection3.ws);
1000
+ const isRetired = ws !== this.activeConnection.ws && ws !== ((_this$nextConnection4 = this.nextConnection) === null || _this$nextConnection4 === void 0 ? void 0 : _this$nextConnection4.ws);
984
1001
  if (!isRetired && incomingMessage.id === null && "error" in incomingMessage) this.connectionRejected = true;
985
1002
  else if (!isRetired && "result" in incomingMessage) this.connectionRejected = false;
986
1003
  this.handleResponseMessage(incomingMessage, ws);
@@ -1139,4 +1156,4 @@ function wsLink(opts) {
1139
1156
  }
1140
1157
  export { jsonEncoder as i, createWSClient as n, resultOf as r, wsLink as t };
1141
1158
 
1142
- //# sourceMappingURL=wsLink-DSmHMirJ.mjs.map
1159
+ //# sourceMappingURL=wsLink-C_Zg2yXV.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wsLink-C_Zg2yXV.mjs","names":[],"sources":["../src/links/wsLink/wsClient/encoder.ts","../src/links/wsLink/wsClient/options.ts","../src/links/internals/urlWithConnectionParams.ts","../src/links/wsLink/wsClient/utils.ts","../src/links/wsLink/wsClient/requestManager.ts","../src/links/wsLink/wsClient/wsConnection.ts","../src/links/wsLink/wsClient/wsClient.ts","../src/links/wsLink/createWsClient.ts","../src/links/wsLink/wsLink.ts"],"sourcesContent":["import type { Encoder } from '@trpc/server/unstable-core-do-not-import';\n\nexport type { Encoder };\n\n/**\n * The default WebSocket encoder. Sends messages as JSON text frames.\n */\nexport const jsonEncoder: Encoder = {\n encode: (data) => JSON.stringify(data),\n decode: (data) => {\n if (typeof data !== 'string') {\n throw new Error(\n 'jsonEncoder received binary data. JSON uses text frames. ' +\n 'Use a binary encoder for binary data.',\n );\n }\n return JSON.parse(data);\n },\n};\n","import type { UrlOptionsWithConnectionParams } from '../../internals/urlWithConnectionParams';\nimport type { Encoder } from './encoder';\n\n/**\n * Options for `createWSClient`.\n *\n * @see https://trpc.io/docs/client/links/wsLink#wslink--createwsclient-options\n */\nexport interface WebSocketClientOptions extends UrlOptionsWithConnectionParams {\n /**\n * The `WebSocket` class to use, for runtimes that don't have one.\n */\n WebSocket?: typeof WebSocket;\n /**\n * The number of milliseconds before a reconnect is attempted.\n * @default {@link exponentialBackoff}\n */\n retryDelayMs?: (attemptIndex: number) => number;\n /**\n * Triggered when a WebSocket connection is established\n */\n onOpen?: () => void;\n /**\n * Triggered when a WebSocket connection encounters an error\n */\n onError?: (evt?: Event) => void;\n /**\n * Triggered when a WebSocket connection is closed\n */\n onClose?: (cause?: { code?: number }) => void;\n /**\n * Lazy mode will close the WebSocket automatically after a period of inactivity (no messages sent or received and no pending requests)\n */\n lazy?: {\n /**\n * Enable lazy mode\n * @default false\n */\n enabled: boolean;\n /**\n * Close the WebSocket after this many milliseconds\n * @default 0\n */\n closeMs: number;\n };\n /**\n * Send ping messages to the server and kill the connection if no pong message is returned\n */\n keepAlive?: {\n /**\n * @default false\n */\n enabled: boolean;\n /**\n * Send a ping message every this many milliseconds\n * @default 5_000\n */\n intervalMs?: number;\n /**\n * Close the WebSocket after this many milliseconds if the server does not respond\n * @default 1_000\n */\n pongTimeoutMs?: number;\n };\n /**\n * Encodes and decodes messages on the wire, for example as a binary format.\n * @default jsonEncoder\n */\n experimental_encoder?: Encoder;\n}\n\n/**\n * Default options for lazy WebSocket connections.\n * Determines whether the connection should be established lazily and defines the delay before closure.\n */\nexport type LazyOptions = Required<NonNullable<WebSocketClientOptions['lazy']>>;\nexport const lazyDefaults: LazyOptions = {\n enabled: false,\n closeMs: 0,\n};\n\n/**\n * Default options for the WebSocket keep-alive mechanism.\n * Configures whether keep-alive is enabled and specifies the timeout and interval for ping-pong messages.\n */\nexport type KeepAliveOptions = Required<\n NonNullable<WebSocketClientOptions['keepAlive']>\n>;\nexport const keepAliveDefaults: KeepAliveOptions = {\n enabled: false,\n pongTimeoutMs: 1_000,\n intervalMs: 5_000,\n};\n\n/**\n * Calculates a delay for exponential backoff based on the retry attempt index.\n * The delay starts at 0 for the first attempt and doubles for each subsequent attempt,\n * capped at 30 seconds.\n */\nexport const exponentialBackoff = (attemptIndex: number) => {\n return attemptIndex === 0 ? 0 : Math.min(1000 * 2 ** attemptIndex, 30000);\n};\n","import { type TRPCRequestInfo } from '@trpc/server/http';\n\n/**\n * Get the result of a value or function that returns a value\n * It also optionally accepts typesafe arguments for the function\n */\nexport const resultOf = <T, TArgs extends any[]>(\n value: T | ((...args: TArgs) => T),\n ...args: TArgs\n): T => {\n return typeof value === 'function'\n ? (value as (...args: TArgs) => T)(...args)\n : value;\n};\n\n/**\n * A value that can be wrapped in callback\n */\nexport type CallbackOrValue<T> = T | (() => T | Promise<T>);\n\nexport interface UrlOptionsWithConnectionParams {\n /**\n * The URL to connect to (can be a function that returns a URL)\n */\n url: CallbackOrValue<string>;\n\n /**\n * Connection params that are available in `createContext()`\n * - For `wsLink`/`wsClient`, these are sent as the first message\n * - For `httpSubscriptionLink`, these are serialized as part of the URL under the `connectionParams` query\n * @see https://trpc.io/docs/server/websockets#connectionParams\n */\n connectionParams?: CallbackOrValue<TRPCRequestInfo['connectionParams']>;\n}\n","import type {\n TRPCConnectionParamsMessage,\n TRPCRequestInfo,\n} from '@trpc/server/unstable-core-do-not-import';\nimport type {\n CallbackOrValue,\n UrlOptionsWithConnectionParams,\n} from '../../internals/urlWithConnectionParams';\nimport { resultOf } from '../../internals/urlWithConnectionParams';\nimport type { Encoder } from './encoder';\n\nexport class TRPCWebSocketClosedError extends Error {\n constructor(opts: { message: string; cause?: unknown }) {\n super(opts.message, {\n cause: opts.cause,\n });\n this.name = 'TRPCWebSocketClosedError';\n Object.setPrototypeOf(this, TRPCWebSocketClosedError.prototype);\n }\n}\n\n/**\n * Calls a user callback, including `connectionState` subscribers. A throw\n * would skip the rest of the client's bookkeeping (e.g. end the reconnect\n * loop, or, with event-emitter based WebSockets such as `ws`, skip the other\n * listeners for that event), so it is rethrown asynchronously instead.\n */\nexport function callUserCallback(callback: () => void) {\n try {\n callback();\n } catch (error) {\n setTimeout(() => {\n throw error;\n });\n }\n}\n\n/**\n * Utility class for managing a timeout that can be started, stopped, and reset.\n * Useful for scenarios where the timeout duration is reset dynamically based on events.\n */\nexport class ResettableTimeout {\n private timeout: ReturnType<typeof setTimeout> | undefined;\n\n constructor(\n private readonly onTimeout: () => void,\n private readonly timeoutMs: number,\n ) {}\n\n /**\n * Resets the current timeout, restarting it with the same duration.\n * Does nothing if no timeout is active.\n */\n public reset() {\n if (!this.timeout) return;\n\n clearTimeout(this.timeout);\n this.timeout = setTimeout(this.onTimeout, this.timeoutMs);\n }\n\n public start() {\n clearTimeout(this.timeout);\n this.timeout = setTimeout(this.onTimeout, this.timeoutMs);\n }\n\n public stop() {\n clearTimeout(this.timeout);\n this.timeout = undefined;\n }\n}\n\n// Ponyfill for Promise.withResolvers https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/withResolvers\nexport function withResolvers<T>() {\n let resolve: (value: T | PromiseLike<T>) => void;\n let reject: (reason?: any) => void;\n const promise = new Promise<T>((res, rej) => {\n resolve = res;\n reject = rej;\n });\n\n // oxlint-disable-next-line typescript/no-non-null-assertion\n return { promise, resolve: resolve!, reject: reject! };\n}\n\n/**\n * Resolves a WebSocket URL and optionally appends connection parameters.\n *\n * If connectionParams are provided, appends 'connectionParams=1' query parameter.\n */\nexport async function prepareUrl(urlOptions: UrlOptionsWithConnectionParams) {\n const url = await resultOf(urlOptions.url);\n\n if (!urlOptions.connectionParams) return url;\n\n // append `?connectionParams=1` when connection params are used\n const prefix = url.includes('?') ? '&' : '?';\n const connectionParams = `${prefix}connectionParams=1`;\n\n return url + connectionParams;\n}\n\nexport async function buildConnectionMessage(\n connectionParams: CallbackOrValue<TRPCRequestInfo['connectionParams']>,\n encoder: Encoder,\n) {\n const message: TRPCConnectionParamsMessage = {\n method: 'connectionParams',\n data: await resultOf(connectionParams),\n };\n\n return encoder.encode(message);\n}\n","import type { AnyTRPCRouter, inferRouterError } from '@trpc/server';\nimport type { Observer } from '@trpc/server/observable';\nimport type {\n TRPCClientOutgoingMessage,\n TRPCResponseMessage,\n} from '@trpc/server/unstable-core-do-not-import';\nimport type { TRPCClientError } from '../../../TRPCClientError';\nimport { withResolvers } from './utils';\n\nexport type TCallbacks = Observer<\n TRPCResponseMessage<unknown, inferRouterError<AnyTRPCRouter>>,\n TRPCClientError<AnyTRPCRouter>\n>;\n\ntype MessageId = string;\ntype MessageIdLike = string | number | null;\n\n/**\n * Represents a WebSocket request managed by the RequestManager.\n * Combines the network message, a utility promise (`end`) that mirrors the lifecycle\n * handled by `callbacks`, and a set of state monitoring callbacks.\n */\ninterface Request {\n message: TRPCClientOutgoingMessage;\n end: Promise<void>;\n callbacks: TCallbacks;\n /**\n * The socket the request was sent on, or `null` while it's outgoing.\n */\n ws: WebSocket | null;\n /**\n * The socket a subscription is moving to. `ws` keeps delivering until this\n * one confirms the subscription with `started`.\n */\n successor: WebSocket | null;\n /**\n * The event ids `ws` delivered while the subscription was moving, which\n * its successor replays and are dropped from it.\n */\n overlapEventIds: Set<string> | null;\n /**\n * The sockets a subscription moved from, each until everything it sent\n * before the stop has arrived. Their events without ids are still\n * delivered, since the new socket may have started listening after they\n * were sent.\n */\n movedFrom: Set<WebSocket>;\n}\n\n/**\n * Manages WebSocket requests, tracking their lifecycle and providing utility methods\n * for handling outgoing and pending requests.\n *\n * - **Outgoing requests**: Requests that are queued and waiting to be sent.\n * - **Pending requests**: Requests that have been sent and are in flight awaiting a response.\n * For subscriptions, multiple responses may be received until the subscription is closed.\n */\nexport class RequestManager {\n constructor(\n private readonly opts: {\n /**\n * Called when a deletion leaves no outgoing or pending request.\n */\n onIdle: () => void;\n },\n ) {}\n\n /**\n * Stores requests that are outgoing, meaning they are registered but not yet sent over the WebSocket.\n */\n private outgoingRequests = new Array<Request & { id: MessageId }>();\n\n /**\n * Stores requests that are pending (in flight), meaning they have been sent over the WebSocket\n * and are awaiting responses. For subscriptions, this includes requests\n * that may receive multiple responses.\n */\n private pendingRequests: Record<MessageId, Request> = {};\n\n /**\n * Subscriptions sent again under a new id, by their earlier id, while\n * sockets they moved from may still send events under that one.\n */\n private drainingRequests: Record<MessageId, Request> = {};\n\n /**\n * Registers a new request by adding it to the outgoing queue and setting up\n * callbacks for lifecycle events such as completion or error.\n *\n * @param message - The outgoing message to be sent.\n * @param callbacks - Callback functions to observe the request's state.\n * @returns A cleanup function to manually remove the request.\n */\n public register(message: TRPCClientOutgoingMessage, callbacks: TCallbacks) {\n const { promise: end, resolve } = withResolvers<void>();\n\n this.outgoingRequests.push({\n id: String(message.id),\n message,\n end,\n ws: null,\n successor: null,\n overlapEventIds: null,\n movedFrom: new Set(),\n callbacks: {\n next: callbacks.next,\n complete: () => {\n callbacks.complete();\n resolve();\n },\n error: (e) => {\n try {\n callbacks.error(e);\n } finally {\n resolve();\n }\n },\n },\n });\n\n return () => {\n this.delete(message);\n callbacks.complete();\n resolve();\n };\n }\n\n /**\n * Deletes a request from both the outgoing and pending collections, if it exists.\n *\n * Matches the message itself rather than its id: a retry reuses the id, and\n * registers its request before the failed request with that id is deleted.\n */\n public delete(message: TRPCClientOutgoingMessage) {\n const wasInFlight = this.hasRequestsInFlight();\n this.outgoingRequests = this.outgoingRequests.filter(\n (request) => request.message !== message,\n );\n const id = String(message.id);\n if (this.pendingRequests[id]?.message === message) {\n delete this.pendingRequests[id];\n }\n for (const [drainingId, request] of Object.entries(this.drainingRequests)) {\n if (request.message === message) {\n delete this.drainingRequests[drainingId];\n }\n }\n if (wasInFlight && !this.hasRequestsInFlight()) {\n this.opts.onIdle();\n }\n }\n\n /**\n * Moves all outgoing requests to the pending state and clears the outgoing queue.\n *\n * The caller is expected to handle the actual sending of the requests\n * (e.g., sending them over the network) after this method is called.\n *\n * @param ws - The socket the requests are sent on.\n * @returns The list of requests that were transitioned to the pending state.\n */\n public flush(ws: WebSocket | null = null) {\n const requests = this.outgoingRequests;\n this.outgoingRequests = [];\n\n for (const request of requests) {\n request.ws = ws;\n request.successor = null;\n request.movedFrom = new Set();\n this.pendingRequests[request.id] = request;\n }\n return requests;\n }\n\n /**\n * Moves a pending request back to the outgoing queue, to be sent again\n * with `id`. Its earlier id then matches only the sockets it's still\n * draining; see `getDrainingRequest()`.\n */\n public requeue(\n message: TRPCClientOutgoingMessage,\n id: TRPCClientOutgoingMessage['id'],\n ) {\n const earlierId = String(message.id);\n const request = this.pendingRequests[earlierId];\n if (request?.message !== message) return;\n\n delete this.pendingRequests[earlierId];\n if (request.movedFrom.size) this.drainingRequests[earlierId] = request;\n message.id = id;\n this.outgoingRequests.push({\n ...request,\n id: String(id),\n ws: null,\n successor: null,\n overlapEventIds: null,\n movedFrom: new Set(),\n });\n }\n\n /**\n * Retrieves all currently pending requests, which are in flight awaiting responses\n * or handling ongoing subscriptions.\n */\n public getPendingRequests() {\n return Object.values(this.pendingRequests);\n }\n\n /**\n * Retrieves a specific pending request by its message ID.\n */\n public getPendingRequest(messageId: MessageIdLike) {\n if (messageId === null) return null;\n\n return this.pendingRequests[String(messageId)];\n }\n\n /**\n * Retrieves a subscription sent again under a new id by its earlier id,\n * while a socket it moved from may still send events under that one.\n */\n public getDrainingRequest(messageId: MessageIdLike) {\n if (messageId === null) return null;\n\n return this.drainingRequests[String(messageId)];\n }\n\n /**\n * Retrieves the subscriptions sent again under a new id whose earlier id\n * is still in use on a socket they moved from.\n */\n public getDrainingRequests() {\n return Object.values(this.drainingRequests);\n }\n\n /**\n * Forgets the earlier ids of subscriptions no longer draining any socket.\n */\n public deleteDrained() {\n for (const [id, request] of Object.entries(this.drainingRequests)) {\n if (!request.movedFrom.size) delete this.drainingRequests[id];\n }\n }\n\n /**\n * Retrieves all outgoing requests, which are waiting to be sent.\n */\n public getOutgoingRequests() {\n return this.outgoingRequests;\n }\n\n /**\n * Retrieves all requests, both outgoing and pending, with their respective states.\n *\n * @returns An array of all requests with their state (\"outgoing\" or \"pending\").\n */\n public getRequests() {\n return [\n ...this.getOutgoingRequests().map((request) => ({\n state: 'outgoing' as const,\n message: request.message,\n end: request.end,\n callbacks: request.callbacks,\n })),\n ...this.getPendingRequests().map((request) => ({\n state: 'pending' as const,\n message: request.message,\n end: request.end,\n callbacks: request.callbacks,\n })),\n ];\n }\n\n /**\n * Checks if there are any pending requests, including ongoing subscriptions.\n */\n public hasPendingRequests() {\n return this.getPendingRequests().length > 0;\n }\n\n /**\n * Checks if there are any pending subscriptions\n */\n public hasPendingSubscriptions() {\n return this.getPendingRequests().some(\n (request) => request.message.method === 'subscription',\n );\n }\n\n /**\n * Checks if a message is still waiting to be sent.\n */\n public isOutgoing(message: TRPCClientOutgoingMessage) {\n return this.outgoingRequests.some((request) => request.message === message);\n }\n\n /**\n * Checks if there are any outgoing requests waiting to be sent.\n */\n public hasOutgoingRequests() {\n return this.outgoingRequests.length > 0;\n }\n\n /**\n * Checks if any request is outgoing or pending, including subscriptions.\n */\n public hasRequestsInFlight() {\n return this.hasOutgoingRequests() || this.hasPendingRequests();\n }\n}\n","import { behaviorSubject } from '@trpc/server/observable';\nimport type { UrlOptionsWithConnectionParams } from '../../internals/urlWithConnectionParams';\nimport type { Encoder } from './encoder';\nimport {\n buildConnectionMessage,\n prepareUrl,\n TRPCWebSocketClosedError,\n withResolvers,\n} from './utils';\n\n/**\n * Opens a WebSocket connection asynchronously and returns a promise\n * that resolves when the connection is successfully established.\n * The promise rejects if an error occurs during the connection attempt.\n */\nfunction asyncWsOpen(ws: WebSocket) {\n const { promise, resolve, reject } = withResolvers<void>();\n\n ws.addEventListener('open', () => {\n ws.removeEventListener('error', reject);\n resolve();\n });\n ws.addEventListener('error', reject);\n\n return promise;\n}\n\nconst pongCallbacks = new WeakMap<WebSocket, ((() => void) | null)[]>();\n\n/**\n * Sends \"PING\" on `ws` and calls `onPong`, if given, once its \"PONG\"\n * arrives. The server answers in order, so by then everything it sent before\n * it read this \"PING\" has arrived too.\n */\nexport function sendPing(ws: WebSocket, onPong: (() => void) | null) {\n let callbacks = pongCallbacks.get(ws);\n if (!callbacks) {\n const queue: ((() => void) | null)[] = [];\n callbacks = queue;\n pongCallbacks.set(ws, queue);\n ws.addEventListener('message', ({ data }) => {\n if (data === 'PONG') queue.shift()?.();\n });\n }\n callbacks.push(onPong);\n ws.send('PING');\n}\n\ninterface PingPongOptions {\n /**\n * The interval (in milliseconds) between \"PING\" messages.\n */\n intervalMs: number;\n\n /**\n * The timeout (in milliseconds) to wait for a \"PONG\" response before closing the connection.\n */\n pongTimeoutMs: number;\n}\n\n/**\n * Sets up a periodic ping-pong mechanism to keep the WebSocket connection alive.\n *\n * - Sends \"PING\" messages at regular intervals defined by `intervalMs`.\n * - If a \"PONG\" response is not received within the `pongTimeoutMs`, the WebSocket is closed.\n * - The ping timer resets upon receiving any message to maintain activity.\n * - Automatically starts the ping process when the WebSocket connection is opened.\n * - Cleans up timers when the WebSocket is closed.\n *\n * @param ws - The WebSocket instance to manage.\n * @param options - Configuration options for ping-pong intervals and timeouts.\n */\nfunction setupPingInterval(\n ws: WebSocket,\n { intervalMs, pongTimeoutMs }: PingPongOptions,\n onPongTimeout: () => void,\n) {\n let pingTimeout: ReturnType<typeof setTimeout> | undefined;\n let pongTimeout: ReturnType<typeof setTimeout> | undefined;\n\n function start() {\n pingTimeout = setTimeout(() => {\n sendPing(ws, null);\n pongTimeout = setTimeout(() => {\n // A socket that is already closing was closed on purpose.\n if (ws.readyState !== ws.OPEN) return;\n ws.close();\n onPongTimeout();\n }, pongTimeoutMs);\n }, intervalMs);\n }\n\n function reset() {\n clearTimeout(pingTimeout);\n start();\n }\n\n function pong() {\n clearTimeout(pongTimeout);\n reset();\n }\n\n ws.addEventListener('open', start);\n ws.addEventListener('message', ({ data }) => {\n clearTimeout(pingTimeout);\n start();\n\n if (data === 'PONG') {\n pong();\n }\n });\n ws.addEventListener('close', () => {\n clearTimeout(pingTimeout);\n clearTimeout(pongTimeout);\n });\n}\n\nexport interface WebSocketConnectionOptions {\n WebSocketPonyfill?: typeof WebSocket;\n urlOptions: UrlOptionsWithConnectionParams;\n keepAlive: PingPongOptions & {\n enabled: boolean;\n };\n encoder: Encoder;\n /**\n * Called when a keepAlive pong times out, once the socket is closing. Its\n * `close` event can come much later: an unresponsive server never answers\n * the closing handshake.\n */\n onPongTimeout: (ws: WebSocket) => void;\n}\n\n/**\n * Manages a WebSocket connection with support for reconnection, keep-alive mechanisms,\n * and observable state tracking.\n */\nexport class WsConnection {\n static connectCount = 0;\n public id = ++WsConnection.connectCount;\n\n private readonly WebSocketPonyfill: typeof WebSocket;\n private readonly urlOptions: UrlOptionsWithConnectionParams;\n private readonly keepAliveOpts: WebSocketConnectionOptions['keepAlive'];\n private readonly encoder: Encoder;\n private readonly onPongTimeout: WebSocketConnectionOptions['onPongTimeout'];\n public readonly wsObservable = behaviorSubject<WebSocket | null>(null);\n\n constructor(opts: WebSocketConnectionOptions) {\n this.WebSocketPonyfill = opts.WebSocketPonyfill ?? WebSocket;\n if (!this.WebSocketPonyfill) {\n throw new Error(\n \"No WebSocket implementation found - you probably don't want to use this on the server, but if you do you need to pass a `WebSocket`-ponyfill\",\n );\n }\n\n this.urlOptions = opts.urlOptions;\n this.keepAliveOpts = opts.keepAlive;\n this.encoder = opts.encoder;\n this.onPongTimeout = opts.onPongTimeout;\n }\n\n public get ws() {\n return this.wsObservable.get();\n }\n\n private set ws(ws) {\n this.wsObservable.next(ws);\n }\n\n /**\n * Checks if the WebSocket connection is open and ready to communicate.\n */\n public isOpen(): this is { ws: WebSocket } {\n return (\n !!this.ws &&\n this.ws.readyState === this.WebSocketPonyfill.OPEN &&\n !this.openPromise\n );\n }\n\n /**\n * Checks if the WebSocket connection is closed or in the process of closing.\n */\n public isClosed(): this is { ws: WebSocket } {\n return (\n !!this.ws &&\n (this.ws.readyState === this.WebSocketPonyfill.CLOSING ||\n this.ws.readyState === this.WebSocketPonyfill.CLOSED)\n );\n }\n\n /**\n * Manages the WebSocket opening process, ensuring that only one open operation\n * occurs at a time. Tracks the ongoing operation with `openPromise` to avoid\n * redundant calls and ensure proper synchronization.\n *\n * Sets up the keep-alive mechanism and necessary event listeners for the connection.\n *\n * @returns A promise that resolves once the WebSocket connection is successfully opened.\n */\n private openPromise: Promise<void> | null = null;\n public async open() {\n if (this.openPromise) return this.openPromise;\n\n this.id = ++WsConnection.connectCount;\n const wsPromise = prepareUrl(this.urlOptions).then(\n (url) => new this.WebSocketPonyfill(url),\n );\n const openPromise = wsPromise.then(async (ws) => {\n this.ws = ws;\n\n // Set binaryType to handle both text and binary messages consistently\n ws.binaryType = 'arraybuffer';\n\n // Setup ping listener\n ws.addEventListener('message', function ({ data }) {\n if (data === 'PING') {\n this.send('PONG');\n }\n });\n\n const abandoned = withResolvers<never>();\n if (this.keepAliveOpts.enabled) {\n setupPingInterval(ws, this.keepAliveOpts, () => {\n // A pong timeout gives up on an attempt still waiting on its\n // `connectionParams`, so the reconnect doesn't wait on them. The\n // attempt stays in `openPromise` until it rejects, so an `open()`\n // meanwhile, e.g. from `onClose`, gets that rejection instead of\n // starting another attempt on this connection.\n if (this.openPromise === openPromise) {\n abandoned.reject(\n new TRPCWebSocketClosedError({\n message: 'Pong timeout before connectionParams were sent',\n }),\n );\n }\n this.onPongTimeout(ws);\n });\n }\n\n ws.addEventListener('close', () => {\n if (this.ws === ws) {\n this.ws = null;\n }\n });\n\n await asyncWsOpen(ws);\n\n if (this.urlOptions.connectionParams) {\n ws.send(\n await Promise.race([\n buildConnectionMessage(\n this.urlOptions.connectionParams,\n this.encoder,\n ),\n abandoned.promise,\n ]),\n );\n }\n });\n this.openPromise = openPromise;\n\n try {\n await openPromise;\n } finally {\n if (this.openPromise === openPromise) {\n this.openPromise = null;\n }\n }\n }\n\n /**\n * Closes the WebSocket connection gracefully.\n * Waits for any ongoing open operation to complete before closing.\n */\n public async close() {\n try {\n await this.openPromise;\n } finally {\n this.ws?.close();\n }\n }\n}\n\n/**\n * Provides a backward-compatible representation of the connection state.\n */\nexport function backwardCompatibility(connection: WsConnection) {\n if (connection.isOpen()) {\n return {\n id: connection.id,\n state: 'open',\n ws: connection.ws,\n } as const;\n }\n\n if (connection.isClosed()) {\n return {\n id: connection.id,\n state: 'closed',\n ws: connection.ws,\n } as const;\n }\n\n if (!connection.ws) {\n return null;\n }\n\n return {\n id: connection.id,\n state: 'connecting',\n ws: connection.ws,\n } as const;\n}\n","import type { AnyTRPCRouter } from '@trpc/server';\nimport type { BehaviorSubject } from '@trpc/server/observable';\nimport { behaviorSubject, observable } from '@trpc/server/observable';\nimport type {\n CombinedDataTransformer,\n TRPCClientIncomingMessage,\n TRPCClientIncomingRequest,\n TRPCClientOutgoingMessage,\n TRPCResponseMessage,\n} from '@trpc/server/unstable-core-do-not-import';\nimport {\n isObject,\n run,\n sleep,\n transformResult,\n} from '@trpc/server/unstable-core-do-not-import';\nimport { TRPCClientError } from '../../../TRPCClientError';\nimport type { TRPCConnectionState } from '../../internals/subscriptions';\nimport type { Operation, OperationResultEnvelope } from '../../types';\nimport type { Encoder } from './encoder';\nimport { jsonEncoder } from './encoder';\nimport type { WebSocketClientOptions } from './options';\nimport { exponentialBackoff, keepAliveDefaults, lazyDefaults } from './options';\nimport type { TCallbacks } from './requestManager';\nimport { RequestManager } from './requestManager';\nimport {\n callUserCallback,\n ResettableTimeout,\n TRPCWebSocketClosedError,\n} from './utils';\nimport type { WebSocketConnectionOptions } from './wsConnection';\nimport { backwardCompatibility, sendPing, WsConnection } from './wsConnection';\n\nfunction isRequestId(value: unknown) {\n return (\n value === null || typeof value === 'string' || typeof value === 'number'\n );\n}\n\n/** A result's `type`; a missing one means `'data'`. */\nfunction isResultType(value: unknown) {\n return (\n value === undefined ||\n value === 'data' ||\n value === 'started' ||\n value === 'stopped'\n );\n}\n\n/**\n * A `reconnect` request from the server, or a response: a string, number or\n * `null` `id` and either a `result` object of a known `type` or an `error`.\n * The output transformer may serialize an error to any value, so\n * `transformResult` validates it.\n */\nfunction isIncomingMessage(value: unknown): value is TRPCClientIncomingMessage {\n if (!isObject(value)) return false;\n if ('method' in value) return value['method'] === 'reconnect';\n if (!isRequestId(value['id'])) return false;\n const result = value['result'];\n if (isObject(result)) return isResultType(result['type']);\n return result === undefined && 'error' in value;\n}\n\nfunction decodeIncomingMessage(\n encoder: Encoder,\n data: string | ArrayBuffer | Uint8Array,\n) {\n const message = encoder.decode(data);\n if (!isIncomingMessage(message)) {\n throw new TypeError('Expected a tRPC message');\n }\n return message;\n}\n\n/**\n * A WebSocket client for managing TRPC operations, supporting lazy initialization,\n * reconnection, keep-alive, and request management.\n */\nexport class WsClient {\n /**\n * Observable tracking the current connection state, including errors.\n */\n public readonly connectionState: BehaviorSubject<\n TRPCConnectionState<TRPCClientError<AnyTRPCRouter>>\n >;\n\n private allowReconnect = false;\n private requestManager = new RequestManager({\n onIdle: () => this.startInactivityTimeout(),\n });\n /**\n * The last id sent on the wire. Each request gets its own instead of its\n * `op.id`: a retry reuses the failed attempt's `op.id`, and the server's\n * late frames for that attempt must not reach the retry. A subscription\n * sent again after its socket was lost gets a new one for the same reason.\n */\n private lastRequestId = 0;\n private readonly connectionOptions: WebSocketConnectionOptions;\n /**\n * The connection new requests are sent on. A reconnect replaces it only\n * once the new connection is fully open, `connectionParams` included, so\n * until then requests keep going to the old one.\n */\n private activeConnection: WsConnection;\n private readonly reconnectRetryDelay: (attemptIndex: number) => number;\n private inactivityTimeout: ResettableTimeout;\n /**\n * Whether lazy mode may close the connection once it's idle: from the\n * first `open` until `close()`.\n */\n private inactivityTimeoutEnabled = false;\n private readonly callbacks: Pick<\n WebSocketClientOptions,\n 'onOpen' | 'onClose' | 'onError'\n >;\n private readonly lazyMode: boolean;\n private readonly encoder: Encoder;\n private readonly closeOnPongTimeout = new WeakMap<WebSocket, () => void>();\n\n constructor(opts: WebSocketClientOptions) {\n this.encoder = opts.experimental_encoder ?? jsonEncoder;\n // Initialize callbacks, connection parameters, and options.\n this.callbacks = {\n onOpen: opts.onOpen,\n onClose: opts.onClose,\n onError: opts.onError,\n };\n\n const lazyOptions = {\n ...lazyDefaults,\n ...opts.lazy,\n };\n\n // Set up inactivity timeout for lazy connections. It only runs while no\n // request or subscription is in flight; see `startInactivityTimeout()`.\n this.inactivityTimeout = new ResettableTimeout(() => {\n if (this.requestManager.hasRequestsInFlight()) {\n this.inactivityTimeout.stop();\n return;\n }\n\n this.close().catch(() => null);\n }, lazyOptions.closeMs);\n\n // Initialize the WebSocket connection.\n this.connectionOptions = {\n WebSocketPonyfill: opts.WebSocket,\n urlOptions: opts,\n keepAlive: {\n ...keepAliveDefaults,\n ...opts.keepAlive,\n },\n encoder: this.encoder,\n onPongTimeout: (ws) => this.closeOnPongTimeout.get(ws)?.(),\n };\n this.activeConnection = this.createConnection();\n this.reconnectRetryDelay = opts.retryDelayMs ?? exponentialBackoff;\n\n this.lazyMode = lazyOptions.enabled;\n\n this.connectionState = behaviorSubject<\n TRPCConnectionState<TRPCClientError<AnyTRPCRouter>>\n >({\n type: 'state',\n state: lazyOptions.enabled ? 'idle' : 'connecting',\n error: null,\n });\n\n // Automatically open the connection if lazy mode is disabled.\n if (!this.lazyMode) {\n this.open().catch(() => null);\n }\n }\n\n /**\n * Starts counting down to closing a lazy connection, if nothing is in\n * flight. Called on `open` and whenever the last request or subscription\n * ends, so the timer never has to poll.\n */\n private startInactivityTimeout() {\n if (\n !this.inactivityTimeoutEnabled ||\n this.requestManager.hasRequestsInFlight()\n ) {\n return;\n }\n this.inactivityTimeout.start();\n }\n\n private createConnection() {\n const connection = new WsConnection(this.connectionOptions);\n connection.wsObservable.subscribe({\n next: (ws) => {\n if (!ws) return;\n this.setupWebSocketListeners(ws);\n },\n });\n return connection;\n }\n\n /**\n * Opens the WebSocket connection. Handles reconnection attempts and updates\n * the connection state accordingly.\n */\n private async open() {\n this.allowReconnect = true;\n if (this.reconnecting) return this.reconnecting;\n // In lazy mode nothing reconnects after the server rejects a connection,\n // so the next request, e.g. one sent again by `retryLink`, opens the next\n // one. It waits for the backoff too. `close()` forgets the rejection, so a\n // request made once the client is idle again connects at once.\n if (this.connectionRejected) {\n // No socket is open while the request waits.\n if (this.connectionState.get().state !== 'connecting') {\n callUserCallback(() =>\n this.connectionState.next({\n type: 'state',\n state: 'connecting',\n error: null,\n }),\n );\n }\n this.reconnect(null);\n return this.reconnecting;\n }\n this.connectionAttemptIndex = 0;\n if (this.connectionState.get().state === 'idle') {\n callUserCallback(() =>\n this.connectionState.next({\n type: 'state',\n state: 'connecting',\n error: null,\n }),\n );\n }\n\n try {\n await this.activeConnection.open();\n } catch (error) {\n this.reconnect(\n new TRPCWebSocketClosedError({\n message: 'Initialization error',\n cause: error,\n }),\n );\n // A socket whose `connectionParams` failed is open, but not usable.\n await this.activeConnection.close().catch(() => null);\n return this.reconnecting;\n }\n }\n\n /**\n * Closes the WebSocket connection and stops managing requests.\n * Ensures all outgoing and pending requests are properly finalized.\n */\n public async close() {\n this.allowReconnect = false;\n this.connectionRejected = false;\n this.reconnecting = null;\n const nextConnection = this.nextConnection;\n this.nextConnection = null;\n this.replacementError = null;\n this.retiredConnection = null;\n this.inactivityTimeoutEnabled = false;\n this.inactivityTimeout.stop();\n\n const requestsToAwait: Promise<void>[] = [];\n for (const request of this.requestManager.getRequests()) {\n if (request.message.method === 'subscription') {\n callUserCallback(() => request.callbacks.complete());\n } else if (request.state === 'outgoing') {\n callUserCallback(() =>\n request.callbacks.error(\n TRPCClientError.from(\n new TRPCWebSocketClosedError({\n message: 'Closed before connection was established',\n }),\n ),\n ),\n );\n } else {\n requestsToAwait.push(request.end);\n }\n }\n\n await Promise.all(requestsToAwait).catch(() => null);\n nextConnection?.ws?.close();\n await this.activeConnection.close().catch(() => null);\n\n callUserCallback(() =>\n this.connectionState.next({\n type: 'state',\n state: 'idle',\n error: null,\n }),\n );\n }\n\n /**\n * Method to request the server.\n * Handles data transformation, batching of requests, and subscription lifecycle.\n *\n * @param op - The operation details including type, path, input and signal. Each subscription to the returned observable is sent with a new id, not `op.id`.\n * @param transformer - Data transformer for serializing requests and deserializing responses\n * @param lastEventId - Optional ID of the last received event for subscriptions\n *\n * @returns An observable that emits operation results and handles cleanup\n */\n public request({\n op: { type, path, input, signal },\n transformer,\n lastEventId,\n }: {\n op: Pick<Operation, 'id' | 'type' | 'path' | 'input' | 'signal'>;\n transformer: CombinedDataTransformer;\n lastEventId?: string;\n }) {\n return observable<\n OperationResultEnvelope<unknown, TRPCClientError<AnyTRPCRouter>>,\n TRPCClientError<AnyTRPCRouter>\n >((observer) => {\n const message: TRPCClientOutgoingMessage = {\n id: ++this.lastRequestId,\n method: type,\n params: {\n input: transformer.input.serialize(input),\n path,\n lastEventId,\n },\n };\n const abort = this.batchSend(message, {\n ...observer,\n next(event) {\n let transformed: ReturnType<typeof transformResult>;\n try {\n transformed = transformResult(event, transformer.output);\n } catch (cause) {\n observer.error(TRPCClientError.from(cause as Error));\n return;\n }\n\n if (!transformed.ok) {\n observer.error(TRPCClientError.from(transformed.error));\n return;\n }\n\n observer.next({\n result: transformed.result,\n });\n },\n });\n\n const onAbort = () => observer.complete();\n if (signal?.aborted) {\n onAbort();\n } else {\n signal?.addEventListener('abort', onAbort, { once: true });\n }\n\n return () => {\n // A subscription sent again after its socket was lost has a new id.\n const { id } = message;\n const pending = this.requestManager.getPendingRequest(id);\n // Under its earlier ids, it may still be draining other sockets.\n const draining = this.requestManager\n .getDrainingRequests()\n .filter((request) => request.message === message);\n abort();\n\n if (type === 'subscription' && pending) {\n this.stopSubscription(id, pending.ws);\n this.stopSubscription(id, pending.successor);\n }\n this.closeIfDrained(pending?.ws);\n for (const request of [pending, ...draining]) {\n for (const earlier of request?.movedFrom ?? []) {\n this.closeIfDrained(earlier);\n }\n }\n\n signal?.removeEventListener('abort', onAbort);\n };\n });\n }\n\n /**\n * The current WebSocket and its state, or `null` when there is no socket.\n */\n public get connection() {\n return backwardCompatibility(\n this.activeConnection.ws\n ? this.activeConnection\n : (this.nextConnection ?? this.activeConnection),\n );\n }\n\n /**\n * Manages the reconnection process for the WebSocket using retry logic.\n * Ensures that only one reconnection attempt is active at a time by tracking the current\n * reconnection state in the `reconnecting` promise.\n */\n private reconnecting: Promise<void> | null = null;\n /**\n * The connection a reconnect is opening to replace the active one.\n */\n private nextConnection: WsConnection | null = null;\n /**\n * The connection the active one replaced, while it's open. If the active\n * one is lost first, this one serves every request again until the next.\n */\n private retiredConnection: WsConnection | null = null;\n /**\n * Why the latest attempt to replace a socket that's still open failed.\n * It's shown once that socket closes, since until then it serves every\n * request.\n */\n private replacementError: TRPCWebSocketClosedError | null = null;\n /**\n * The retry attempt that opened the current connection.\n */\n private connectionAttemptIndex = 0;\n /**\n * Set when the server rejects the current connection with an `id: null`\n * error, e.g. because `createContext` threw. The server closes the socket\n * right after, so the reconnect that follows carries on with the backoff\n * instead of starting over at attempt 0. Cleared again when the socket\n * receives a result, which proves the connection was accepted, and by\n * `close()`. Also set on a keepAlive pong timeout when the socket never\n * received anything.\n * @see https://github.com/trpc/trpc/issues/4774\n */\n private connectionRejected = false;\n /** Subscriptions whose subscriber has seen `started`. */\n private readonly started = new WeakSet<TRPCClientOutgoingMessage>();\n\n /**\n * Opens a new connection, retrying until one opens. While the current\n * socket is open, it keeps serving every request, new ones included, until\n * the new one is fully open, `connectionParams` included.\n *\n * @param closedError - Why the current socket was lost, or `null` when it's\n * still open and serving requests, e.g. on a server-requested reconnect.\n */\n private reconnect(closedError: TRPCWebSocketClosedError | null) {\n if (!this.allowReconnect) {\n return;\n }\n\n if (closedError) {\n const error = this.replacementError ?? closedError;\n this.replacementError = null;\n callUserCallback(() =>\n this.connectionState.next({\n type: 'state',\n state: 'connecting',\n error: TRPCClientError.from(error),\n }),\n );\n }\n if (this.reconnecting) return;\n\n // `close()` drops a reconnect that's still running, so a later `open()`\n // doesn't wait for it.\n const isCurrent = () =>\n this.allowReconnect && this.reconnecting === reconnecting;\n const tryReconnect = async (attemptIndex: number) => {\n try {\n await sleep(this.reconnectRetryDelay(attemptIndex));\n if (isCurrent()) {\n const next = this.createConnection();\n this.nextConnection = next;\n try {\n await next.open();\n // `open()` resolves even if the socket closed while its\n // `connectionParams` were pending.\n if (!next.isOpen()) {\n throw new Error('WebSocket closed before it was ready');\n }\n } catch (error) {\n await next.close().catch(() => null);\n throw error;\n } finally {\n if (this.nextConnection === next) this.nextConnection = null;\n }\n\n if (isCurrent()) {\n // Read only now: losing the active socket while `next` opened may\n // have restored the one it replaced.\n const previous = this.activeConnection;\n this.activeConnection = next;\n this.retiredConnection = previous.isOpen() ? previous : null;\n this.connectionAttemptIndex = attemptIndex;\n this.replacementError = null;\n this.handOff(previous.ws);\n } else {\n await next.close();\n }\n }\n if (this.reconnecting === reconnecting) this.reconnecting = null;\n } catch (error) {\n const closedError = new TRPCWebSocketClosedError({\n message: 'Initialization error',\n cause: error,\n });\n if (isCurrent() && this.activeConnection.isOpen()) {\n // The old socket keeps serving every request, so this is shown only\n // once it closes.\n this.replacementError = closedError;\n } else if (isCurrent()) {\n callUserCallback(() =>\n this.connectionState.next({\n type: 'state',\n state: 'connecting',\n error: TRPCClientError.from(closedError),\n }),\n );\n }\n await tryReconnect(attemptIndex + 1);\n }\n };\n\n const reconnecting = tryReconnect(\n this.connectionRejected ? this.connectionAttemptIndex + 1 : 0,\n );\n this.reconnecting = reconnecting;\n }\n\n /**\n * Moves the pending subscriptions to the active socket, which has just\n * opened: each is resubscribed with the id of the last event it delivered.\n * A subscription whose old socket is still open keeps delivering from it\n * until the new socket confirms it with `started`; see `confirmMove()`.\n * Closes the previous socket once nothing is left on it.\n */\n private handOff(previous: WebSocket | null) {\n if (\n this.connectionState.get().state !== 'pending' &&\n this.activeConnection.isOpen()\n ) {\n callUserCallback(() =>\n this.connectionState.next({\n type: 'state',\n state: 'pending',\n error: null,\n }),\n );\n }\n\n const ws = this.activeConnection.ws;\n const subscriptions = this.requestManager\n .getPendingRequests()\n .filter(\n (request) =>\n request.message.method === 'subscription' && request.ws !== ws,\n );\n if (subscriptions.length) {\n this.send(subscriptions.map(({ message }) => message));\n for (const request of subscriptions) {\n // The socket it was moving to hasn't started it, and won't be used.\n this.stopSubscription(request.message.id, request.successor);\n if (request.ws && request.ws.readyState === request.ws.OPEN) {\n request.successor = ws;\n request.overlapEventIds = new Set();\n } else {\n // Its socket was lost, so it starts again.\n this.started.delete(request.message);\n request.ws = ws;\n request.successor = null;\n }\n }\n }\n this.closeIfDrained(previous);\n }\n\n /**\n * Completes a subscription's move once its new socket has confirmed it:\n * stops it on the old socket, whose later messages for it are dropped\n * apart from events without ids, and closes that socket once everything it\n * sent before the stop has arrived and nothing else is left on it.\n */\n private confirmMove(request: {\n message: TRPCClientOutgoingMessage;\n ws: WebSocket | null;\n successor: WebSocket | null;\n movedFrom: Set<WebSocket>;\n }) {\n const previous = request.ws;\n request.ws = request.successor;\n request.successor = null;\n if (previous && this.stopSubscription(request.message.id, previous)) {\n request.movedFrom.add(previous);\n // `stopped` can wait on the procedure, but the \"PONG\" can't.\n sendPing(previous, () => this.releaseMovedFrom(request, previous));\n }\n this.closeIfDrained(previous);\n }\n\n private releaseMovedFrom(\n request: { movedFrom: Set<WebSocket> },\n ws: WebSocket,\n ) {\n if (!request.movedFrom.delete(ws)) return;\n this.requestManager.deleteDrained();\n this.closeIfDrained(ws);\n }\n\n private hasRequestsOn(ws: WebSocket | null) {\n return [\n ...this.requestManager.getPendingRequests(),\n ...this.requestManager.getDrainingRequests(),\n ].some(\n (request) =>\n request.ws === ws || (ws !== null && request.movedFrom.has(ws)),\n );\n }\n\n /**\n * Closes `ws` once it's been replaced and nothing is left on it.\n */\n private closeIfDrained(ws: WebSocket | null | undefined) {\n if (!ws || ws === this.activeConnection.ws) return;\n if (this.hasRequestsOn(ws)) return;\n ws.close();\n }\n\n /**\n * Stops a subscription on the socket it was sent on, if that's still open.\n *\n * @returns Whether the stop was sent.\n */\n private stopSubscription(\n id: TRPCClientOutgoingMessage['id'],\n ws: WebSocket | null,\n ) {\n if (!ws || ws.readyState !== ws.OPEN) return false;\n ws.send(this.encoder.encode({ id, method: 'subscription.stop' }));\n return true;\n }\n\n /**\n * Fails the pending queries and mutations sent on `ws`, and returns its\n * pending subscriptions, which are kept to be sent again.\n */\n private failPendingRequests(ws: WebSocket, cause: unknown) {\n const subscriptions = [];\n for (const request of this.requestManager.getPendingRequests()) {\n const { message, callbacks } = request;\n if (request.ws !== ws) continue;\n if (message.method === 'subscription') {\n subscriptions.push(request);\n continue;\n }\n\n callUserCallback(() =>\n callbacks.error(\n TRPCClientError.from(\n cause ??\n new TRPCWebSocketClosedError({\n message: 'WebSocket closed',\n cause,\n }),\n ),\n ),\n );\n this.requestManager.delete(message);\n }\n return subscriptions;\n }\n\n private setupWebSocketListeners(ws: WebSocket) {\n // A socket from a reconnect that `close()` abandoned says nothing about\n // the connection the client now uses.\n if (ws === this.activeConnection.ws || ws === this.nextConnection?.ws) {\n this.connectionRejected = false;\n }\n\n /**\n * Fails the queries and mutations sent on `ws`. Its subscriptions are\n * sent again on the active socket if that's open, or else by\n * `reconnect()` once the next one opens.\n */\n const handleCloseOrError = (cause: unknown) => {\n if (ws === this.retiredConnection?.ws) this.retiredConnection = null;\n const retired = this.retiredConnection;\n let restored = false;\n if (ws === this.activeConnection.ws && retired?.isOpen()) {\n restored = true;\n // The socket it replaced serves every request again, until the next\n // one opens.\n this.activeConnection = retired;\n this.retiredConnection = null;\n this.replacementError = new TRPCWebSocketClosedError({\n message: 'WebSocket closed',\n cause,\n });\n }\n for (const request of this.requestManager.getPendingRequests()) {\n if (request.successor === ws) {\n // Its old socket still has it.\n request.successor = null;\n request.overlapEventIds = null;\n }\n request.movedFrom.delete(ws);\n }\n for (const request of this.requestManager.getDrainingRequests()) {\n request.movedFrom.delete(ws);\n }\n this.requestManager.deleteDrained();\n const lost = this.failPendingRequests(ws, cause).filter((request) => {\n if (!request.successor) return true;\n // Its new socket has it already.\n request.ws = request.successor;\n request.successor = null;\n return false;\n });\n if (\n ws === this.activeConnection.ws ||\n (this.reconnecting && !this.activeConnection.isOpen())\n ) {\n return;\n }\n\n for (const { message, callbacks } of lost) {\n this.started.delete(message);\n // The active socket may still have its old id, e.g. while stopping it.\n this.requestManager.requeue(message, ++this.lastRequestId);\n this.flushOutgoing(message, callbacks);\n }\n if (restored) this.reconnect(null);\n };\n\n ws.addEventListener('open', () => {\n run(async () => {\n // A socket from a reconnect that `close()` abandoned changes neither\n // the state nor the inactivity timeout.\n const isLive =\n ws === this.activeConnection.ws || ws === this.nextConnection?.ws;\n if (this.lazyMode && isLive) {\n this.inactivityTimeoutEnabled = true;\n this.startInactivityTimeout();\n }\n\n this.callbacks.onOpen?.();\n\n // A socket opened to replace one that's still open changes nothing\n // the subscribers can see until `handOff()` puts it in use.\n if (\n !isLive ||\n this.connectionState.get().state === 'pending' ||\n (ws !== this.activeConnection.ws && this.activeConnection.isOpen())\n ) {\n return;\n }\n callUserCallback(() =>\n this.connectionState.next({\n type: 'state',\n state: 'pending',\n error: null,\n }),\n );\n }).catch((error) => {\n ws.close(3000);\n handleCloseOrError(error);\n });\n });\n\n // Set on the socket's `close` event, or earlier when the client gives up\n // on the socket (a keepAlive pong timeout or an undecodable frame) without\n // waiting for the closing handshake. The socket's later frames and events\n // must not reach the requests resent by then.\n let closed = false;\n let answered = false;\n\n /**\n * Runs the socket's close handling once: `cause` fails its queries and\n * mutations, and `event` goes to `onClose`.\n */\n const handleClose = (\n cause: unknown,\n event: CloseEvent | { code: number },\n ) => {\n if (closed) return;\n closed = true;\n handleCloseOrError(cause);\n callUserCallback(() => this.callbacks.onClose?.(event));\n\n if (\n ws === this.activeConnection.ws &&\n (!this.lazyMode || this.requestManager.hasPendingSubscriptions())\n ) {\n this.reconnect(\n new TRPCWebSocketClosedError({\n message: 'WebSocket closed',\n cause,\n }),\n );\n }\n };\n // What `close` reports when the closing handshake doesn't complete.\n const abandonedEvent = { code: 1006 };\n\n const onMessage = ({ data }: MessageEvent) => {\n if (closed) return;\n answered = true;\n this.inactivityTimeout.reset();\n\n // Handle PING/PONG as text regardless of encoder\n if (['PING', 'PONG'].includes(data)) return;\n\n let incomingMessage: TRPCClientIncomingMessage;\n try {\n incomingMessage = decodeIncomingMessage(this.encoder, data);\n } catch (cause) {\n // The frame may have carried a response, and its request would wait\n // forever, so this socket is treated as lost.\n ws.close();\n handleClose(\n new TRPCWebSocketClosedError({\n message: 'Received a message that could not be decoded',\n cause,\n }),\n abandonedEvent,\n );\n // Rethrown asynchronously by `callUserCallback()` below.\n throw cause;\n }\n if ('method' in incomingMessage) {\n if (ws === this.activeConnection.ws) {\n this.handleIncomingRequest(incomingMessage);\n }\n return;\n }\n\n // A replaced socket still answers its own queries and mutations, but\n // says nothing about the connection the client now uses.\n const isRetired =\n ws !== this.activeConnection.ws && ws !== this.nextConnection?.ws;\n if (\n !isRetired &&\n incomingMessage.id === null &&\n 'error' in incomingMessage\n ) {\n this.connectionRejected = true;\n } else if (!isRetired && 'result' in incomingMessage) {\n // The server only sends results once `createContext` has succeeded,\n // so an earlier `id: null` error on this socket was not a rejection,\n // e.g. the server's reply to a malformed frame.\n this.connectionRejected = false;\n }\n\n this.handleResponseMessage(incomingMessage, ws);\n };\n // With `ws`, a throw from a `message` listener stops the socket from\n // reading any further frames while it stays open.\n ws.addEventListener('message', (event) => {\n callUserCallback(() => onMessage(event));\n });\n\n ws.addEventListener('close', (event) => handleClose(event, event));\n this.closeOnPongTimeout.set(ws, () => {\n // A server that never answered, not even a ping, gets the same backoff\n // as one that rejected the connection.\n if (!answered) this.connectionRejected = true;\n handleClose(abandonedEvent, abandonedEvent);\n });\n\n ws.addEventListener('error', (event) => {\n if (closed) return;\n handleCloseOrError(event);\n callUserCallback(() => this.callbacks.onError?.(event));\n\n if (ws !== this.activeConnection.ws) return;\n\n this.reconnect(\n new TRPCWebSocketClosedError({\n message: 'WebSocket closed',\n cause: event,\n }),\n );\n });\n }\n\n private handleResponseMessage(message: TRPCResponseMessage, ws: WebSocket) {\n const request =\n this.requestManager.getPendingRequest(message.id) ??\n this.requestManager.getDrainingRequest(message.id);\n if (!request) return;\n const result = 'result' in message ? message.result : null;\n if (request.successor === ws) {\n this.confirmMove(request);\n } else if (request.ws !== ws) {\n // A subscription moved to a new socket ignores the old one's frames,\n // apart from events without ids until it's stopped there: those can't\n // be replayed, and the new socket may have started listening after\n // the old one sent them.\n if (request.movedFrom.has(ws)) {\n if (result?.type === 'stopped') {\n this.releaseMovedFrom(request, ws);\n } else if (result?.type === 'data' && result.id === undefined) {\n callUserCallback(() => request.callbacks.next(message));\n }\n }\n return;\n }\n // The subscriber doesn't see a move, even one its old socket didn't\n // live to finish.\n if (result?.type === 'started' && this.started.has(request.message)) {\n return;\n }\n\n if (result?.type === 'data' && result.id !== undefined) {\n const eventId = String(result.id);\n if (request.successor) {\n request.overlapEventIds?.add(eventId);\n } else if (request.overlapEventIds?.delete(eventId)) {\n // Already delivered by the old socket.\n return;\n }\n }\n\n callUserCallback(() => request.callbacks.next(message));\n\n let completed = true;\n if ('result' in message && request.message.method === 'subscription') {\n if (message.result.type === 'data') {\n // An event without an id keeps the last tracked id, so a reconnect\n // still resumes after it, as with `httpSubscriptionLink`.\n if (message.result.id !== undefined) {\n request.message.params.lastEventId = message.result.id;\n }\n } else if (message.result.type === 'started') {\n this.started.add(request.message);\n }\n\n if (message.result.type !== 'stopped') {\n completed = false;\n }\n }\n\n if (completed) {\n callUserCallback(() => request.callbacks.complete());\n this.requestManager.delete(request.message);\n }\n }\n\n private handleIncomingRequest(message: TRPCClientIncomingRequest) {\n if (message.method === 'reconnect') {\n this.reconnect(null);\n }\n }\n\n /**\n * Sends a message or batch of messages directly to the server.\n */\n private send(\n messageOrMessages: TRPCClientOutgoingMessage | TRPCClientOutgoingMessage[],\n ) {\n if (!this.activeConnection.isOpen()) {\n throw new Error('Active connection is not open');\n }\n\n const messages =\n messageOrMessages instanceof Array\n ? messageOrMessages\n : [messageOrMessages];\n this.activeConnection.ws.send(\n this.encoder.encode(messages.length === 1 ? messages[0] : messages),\n );\n }\n\n /**\n * Groups requests for batch sending.\n *\n * @returns A function to abort the batched request.\n */\n private batchSend(message: TRPCClientOutgoingMessage, callbacks: TCallbacks) {\n // Started again once the last request or subscription ends.\n this.inactivityTimeout.stop();\n this.flushOutgoing(message, callbacks);\n return this.requestManager.register(message, callbacks);\n }\n\n /**\n * Sends `message` with the other outgoing requests once the connection is\n * open, batching those queued in the same tick.\n */\n private flushOutgoing(\n message: TRPCClientOutgoingMessage,\n callbacks: TCallbacks,\n ) {\n run(async () => {\n // During a reconnect, requests go to the old socket while it's open.\n do {\n if (!this.activeConnection.isOpen()) {\n await this.open();\n }\n await sleep(0);\n } while (!this.activeConnection.isOpen() && this.reconnecting);\n\n if (!this.requestManager.hasOutgoingRequests()) return;\n // Another run already sent this request. Each queued request's own run\n // sends it once the new socket opens.\n if (\n !this.activeConnection.isOpen() &&\n !this.requestManager.isOutgoing(message)\n ) {\n return;\n }\n\n const requests = this.requestManager.flush(this.activeConnection.ws);\n try {\n this.send(requests.map((request) => request.message));\n } catch (err) {\n for (const request of requests) {\n this.requestManager.delete(request.message);\n callUserCallback(() =>\n request.callbacks.error(TRPCClientError.from(err as Error)),\n );\n }\n }\n }).catch((err) => {\n this.requestManager.delete(message);\n callUserCallback(() => callbacks.error(TRPCClientError.from(err)));\n });\n }\n}\n","import type { Encoder } from './wsClient/encoder';\nimport { jsonEncoder } from './wsClient/encoder';\nimport type { WebSocketClientOptions } from './wsClient/options';\nimport { WsClient } from './wsClient/wsClient';\n\n/**\n * Opens and manages a WebSocket connection to a tRPC server. Pass it to\n * `wsLink`.\n *\n * @see https://trpc.io/docs/client/links/wsLink#wslink--createwsclient-options\n */\nexport function createWSClient(opts: WebSocketClientOptions) {\n return new WsClient(opts);\n}\n\n/**\n * The client returned by {@link createWSClient}.\n */\nexport type TRPCWebSocketClient = ReturnType<typeof createWSClient>;\n\nexport { jsonEncoder, type Encoder, type WebSocketClientOptions };\n","import type { Unsubscribable } from '@trpc/server/observable';\nimport { observable } from '@trpc/server/observable';\nimport type {\n AnyRouter,\n inferClientTypes,\n} from '@trpc/server/unstable-core-do-not-import';\nimport type { TransformerOptions } from '../../unstable-internals';\nimport { getTransformer } from '../../unstable-internals';\nimport type { TRPCLink } from '../types';\nimport type {\n Encoder,\n TRPCWebSocketClient,\n WebSocketClientOptions,\n} from './createWsClient';\nimport { createWSClient, jsonEncoder } from './createWsClient';\nimport { callUserCallback } from './wsClient/utils';\n\n/**\n * Options for {@link wsLink}.\n *\n * @see https://trpc.io/docs/client/links/wsLink#wslink--createwsclient-options\n */\nexport type WebSocketLinkOptions<TRouter extends AnyRouter> = {\n /**\n * The client made with `createWSClient`.\n */\n client: TRPCWebSocketClient;\n} & TransformerOptions<inferClientTypes<TRouter>>;\n\n/**\n * A terminating link that sends calls over a WebSocket.\n *\n * @see https://trpc.io/docs/client/links/wsLink\n */\nexport function wsLink<TRouter extends AnyRouter>(\n opts: WebSocketLinkOptions<TRouter>,\n): TRPCLink<TRouter> {\n const { client } = opts;\n const transformer = getTransformer(opts.transformer);\n return () => {\n return ({ op }) => {\n return observable((observer) => {\n let connStateSubscription: Unsubscribable | null = null;\n if (op.type === 'subscription') {\n // The current state is sent during `subscribe`, and a throwing\n // `onConnectionStateChange` must not stop the request below from\n // being sent. The client guards the states it sends later.\n let isSubscribing = true;\n connStateSubscription = client.connectionState.subscribe({\n next(result) {\n const emit = () =>\n observer.next({\n result,\n context: op.context,\n });\n if (isSubscribing) {\n callUserCallback(emit);\n } else {\n emit();\n }\n },\n });\n isSubscribing = false;\n }\n\n const requestSubscription = client\n .request({\n op,\n transformer,\n })\n .subscribe(observer);\n\n return () => {\n requestSubscription.unsubscribe();\n connStateSubscription?.unsubscribe();\n };\n });\n };\n };\n}\n\nexport {\n createWSClient,\n jsonEncoder,\n type Encoder,\n type TRPCWebSocketClient,\n type WebSocketClientOptions,\n};\n"],"mappings":";;;;;;;;;AAOA,MAAa,cAAuB;CAClC,SAAS,SAAS,KAAK,UAAU,IAAI;CACrC,SAAS,SAAS;EAChB,IAAI,OAAO,SAAS,UAClB,MAAM,IAAI,MACR,gGAEF;EAEF,OAAO,KAAK,MAAM,IAAI;CACxB;AACF;;AC0DA,MAAa,eAA4B;CACvC,SAAS;CACT,SAAS;AACX;AASA,MAAa,oBAAsC;CACjD,SAAS;CACT,eAAe;CACf,YAAY;AACd;;;;;;AAOA,MAAa,sBAAsB,iBAAyB;CAC1D,OAAO,iBAAiB,IAAI,IAAI,KAAK,IAAI,MAAO,KAAK,cAAc,GAAK;AAC1E;;;;;;AC/FA,MAAa,YACX,OACA,GAAG,SACG;CACN,OAAO,OAAO,UAAU,aACnB,MAAgC,GAAG,IAAI,IACxC;AACN;;ACFA,IAAa,2BAAb,MAAa,iCAAiC,MAAM;CAClD,YAAY,MAA4C;EACtD,MAAM,KAAK,SAAS,EAClB,OAAO,KAAK,MACd,CAAC;EACD,KAAK,OAAO;EACZ,OAAO,eAAe,MAAM,yBAAyB,SAAS;CAChE;AACF;;;;;;;AAQA,SAAgB,iBAAiB,UAAsB;CACrD,IAAI;EACF,SAAS;CACX,SAAS,OAAO;EACd,iBAAiB;GACf,MAAM;EACR,CAAC;CACH;AACF;;;;;AAMA,IAAa,oBAAb,MAA+B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,MAAM,cAAc,eAAe,KAAK,WAAW;CACnD,aAAa;EACX,QAAQ,EAAE,SAAS;GACjB,OAAO,YAAY,aAAa;IAC9B,IAAI,wBAA+C;IACnD,IAAI,GAAG,SAAS,gBAAgB;KAI9B,IAAI,gBAAgB;KACpB,wBAAwB,OAAO,gBAAgB,UAAU,EACvD,KAAK,QAAQ;MACX,MAAM,aACJ,SAAS,KAAK;OACZ;OACA,SAAS,GAAG;MACd,CAAC;MACH,IAAI,eACF,iBAAiB,IAAI;WAErB,KAAK;KAET,EACF,CAAC;KACD,gBAAgB;IAClB;IAEA,MAAM,sBAAsB,OACzB,QAAQ;KACP;KACA;IACF,CAAC,CAAC,CACD,UAAU,QAAQ;IAErB,aAAa;KACX,oBAAoB,YAAY;KAChC,0BAAA,QAAA,0BAAA,KAAA,KAAA,sBAAuB,YAAY;IACrC;GACF,CAAC;EACH;CACF;AACF"}
@@ -210,8 +210,9 @@ declare class WsClient {
210
210
  * error, e.g. because `createContext` threw. The server closes the socket
211
211
  * right after, so the reconnect that follows carries on with the backoff
212
212
  * instead of starting over at attempt 0. Cleared again when the socket
213
- * receives a result, which proves the connection was accepted. Also set
214
- * on a keepAlive pong timeout when the socket never received anything.
213
+ * receives a result, which proves the connection was accepted, and by
214
+ * `close()`. Also set on a keepAlive pong timeout when the socket never
215
+ * received anything.
215
216
  * @see https://github.com/trpc/trpc/issues/4774
216
217
  */
217
218
  private connectionRejected;
@@ -306,4 +307,4 @@ type WebSocketLinkOptions<TRouter extends AnyRouter> = {
306
307
  */
307
308
  declare function wsLink<TRouter extends AnyRouter>(opts: WebSocketLinkOptions<TRouter>): TRPCLink<TRouter>;
308
309
  export { WebSocketClientOptions as a, jsonEncoder as c, createWSClient as i, wsLink as n, UrlOptionsWithConnectionParams as o, TRPCWebSocketClient as r, Encoder as s, WebSocketLinkOptions as t };
309
- //# sourceMappingURL=wsLink-CnVTvoqf.d.mts.map
310
+ //# sourceMappingURL=wsLink-DU5wj6kH.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"wsLink-CnVTvoqf.d.mts","names":[],"sources":["../src/links/wsLink/wsClient/encoder.ts","../src/links/internals/urlWithConnectionParams.ts","../src/links/wsLink/wsClient/options.ts","../src/links/wsLink/wsClient/wsClient.ts","../src/links/wsLink/createWsClient.ts","../src/links/wsLink/wsLink.ts"],"mappings":";;;;;;;;;;cAOa,aAAa;;;;KCWd,gBAAgB,KAAK,WAAW,IAAI,QAAQ;UAEvC;;;;EAIf,KAAK;;;;;;;EAQL,mBAAmB,gBAAgB;;;;;;;UCxBpB,+BAA+B;;;;EAI9C,mBAAmB;;;;;EAKnB,gBAAgB;;;;EAIhB;;;;EAIA,WAAW,MAAM;;;;EAIjB,WAAW;IAAU;;;;;EAIrB;;;;;IAKE;;;;;IAKA;;;;;EAKF;;;;IAIE;;;;;IAKA;;;;;IAKA;;;;;;EAMF,uBAAuB;;;;;;cCWZ;;;;WAIK,iBAAiB,gBAC/B,oBAAoB,gBAAgB;UAG9B;UACA;;;;;;;UASA;mBACS;;;;;;UAMT;mBACS;UACT;;;;;UAKA;mBACS;mBAIA;mBACA;mBACA;EAEjB,YAAY,MAAM;;;;;;UA4DV;UAUA;;;;;UAeM;;;;;EAiCD,SAAK;;;;;;;;;;;EAiDX,UACL,MAAM,MAAM,MAAM,OAAO,UACzB,aACA;IAEA,IAAI,KAAK;IACT,aAAa;IACb;wCACD,WAAA,iCAAA,gBAAA,iBAAA,gBAAA;;;;MAwEU;;;;;;;;;;;;;;;;;;UAaH;;;;UAIA;;;;;UAKA;;;;;;UAMA;;;;UAIA;;;;;;;;;;UAUA;;mBAES;;;;;;;;;UAUT;;;;;;;;UAsFA;;;;;;;UA8CA;UAiBA;UASA;;;;UAaA;;;;;;UAWA;;;;;UAaA;UA0BA;UA4MA;UA+DA;;;;UASA;;;;;;UAqBA;;;;;UAWA;;;;;;;;iBC36BM,eAAe,MAAM,yBAAsB;;;;KAO/C,sBAAsB,kBAAkB;;;;;;KCIxC,qBAAqB,gBAAgB;;;;EAI/C,QAAQ;IACN,mBAAmB,iBAAiB;;;;;;iBAOxB,OAAO,gBAAgB,WACrC,MAAM,qBAAqB,WAC1B,SAAS"}
1
+ {"version":3,"file":"wsLink-DU5wj6kH.d.mts","names":[],"sources":["../src/links/wsLink/wsClient/encoder.ts","../src/links/internals/urlWithConnectionParams.ts","../src/links/wsLink/wsClient/options.ts","../src/links/wsLink/wsClient/wsClient.ts","../src/links/wsLink/createWsClient.ts","../src/links/wsLink/wsLink.ts"],"mappings":";;;;;;;;;;cAOa,aAAa;;;;KCWd,gBAAgB,KAAK,WAAW,IAAI,QAAQ;UAEvC;;;;EAIf,KAAK;;;;;;;EAQL,mBAAmB,gBAAgB;;;;;;;UCxBpB,+BAA+B;;;;EAI9C,mBAAmB;;;;;EAKnB,gBAAgB;;;;EAIhB;;;;EAIA,WAAW,MAAM;;;;EAIjB,WAAW;IAAU;;;;;EAIrB;;;;;IAKE;;;;;IAKA;;;;;EAKF;;;;IAIE;;;;;IAKA;;;;;IAKA;;;;;;EAMF,uBAAuB;;;;;;cCWZ;;;;WAIK,iBAAiB,gBAC/B,oBAAoB,gBAAgB;UAG9B;UACA;;;;;;;UASA;mBACS;;;;;;UAMT;mBACS;UACT;;;;;UAKA;mBACS;mBAIA;mBACA;mBACA;EAEjB,YAAY,MAAM;;;;;;UA4DV;UAUA;;;;;UAeM;;;;;EAmDD,SAAK;;;;;;;;;;;EAqDX,UACL,MAAM,MAAM,MAAM,OAAO,UACzB,aACA;IAEA,IAAI,KAAK;IACT,aAAa;IACb;wCACD,WAAA,iCAAA,gBAAA,iBAAA,gBAAA;;;;MAwEU;;;;;;;;;;;;;;;;;;UAaH;;;;UAIA;;;;;UAKA;;;;;;UAMA;;;;UAIA;;;;;;;;;;;UAWA;;mBAES;;;;;;;;;UAUT;;;;;;;;UA2FA;;;;;;;UA8CA;UAiBA;UASA;;;;UAaA;;;;;;UAWA;;;;;UAaA;UA0BA;UAqNA;UA+DA;;;;UASA;;;;;;UAqBA;;;;;UAWA;;;;;;;;iBCh9BM,eAAe,MAAM,yBAAsB;;;;KAO/C,sBAAsB,kBAAkB;;;;;;KCIxC,qBAAqB,gBAAgB;;;;EAI/C,QAAQ;IACN,mBAAmB,iBAAiB;;;;;;iBAOxB,OAAO,gBAAgB,WACrC,MAAM,qBAAqB,WAC1B,SAAS"}
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@trpc/client",
3
3
  "type": "module",
4
4
  "sideEffects": false,
5
- "version": "11.31.5",
5
+ "version": "11.31.7",
6
6
  "description": "The tRPC client library",
7
7
  "author": "KATT",
8
8
  "license": "MIT",
@@ -100,12 +100,12 @@
100
100
  "skills"
101
101
  ],
102
102
  "peerDependencies": {
103
- "@trpc/server": "11.31.5",
103
+ "@trpc/server": "11.31.7",
104
104
  "typescript": ">=5.7.2"
105
105
  },
106
106
  "devDependencies": {
107
107
  "@tanstack/intent": "0.5.6",
108
- "@trpc/server": "11.31.5",
108
+ "@trpc/server": "11.31.7",
109
109
  "@types/isomorphic-fetch": "^0.0.39",
110
110
  "@types/node": "^24.19.2",
111
111
  "dataloader": "^2.2.3",
@@ -12,7 +12,7 @@ sources:
12
12
  metadata:
13
13
  type: core
14
14
  library: trpc
15
- library_version: 11.31.5
15
+ library_version: 11.31.7
16
16
  ---
17
17
 
18
18
  <!-- Generated from www/v11/docs/client/vanilla/setup.md by www/build/skills.ts. Edit the docs instead. -->
@@ -17,7 +17,7 @@ sources:
17
17
  metadata:
18
18
  type: core
19
19
  library: trpc
20
- library_version: 11.31.5
20
+ library_version: 11.31.7
21
21
  ---
22
22
 
23
23
  <!-- Generated from www/v11/docs/client/links.md by www/build/skills.ts. Edit the docs instead. -->
@@ -9,7 +9,7 @@ sources:
9
9
  metadata:
10
10
  type: composition
11
11
  library: trpc
12
- library_version: 11.31.5
12
+ library_version: 11.31.7
13
13
  ---
14
14
 
15
15
  <!-- Generated from www/v11/docs/server/data-transformers.md by www/build/skills.ts. Edit the docs instead. -->
@@ -206,6 +206,24 @@ export class WsClient {
206
206
  private async open() {
207
207
  this.allowReconnect = true;
208
208
  if (this.reconnecting) return this.reconnecting;
209
+ // In lazy mode nothing reconnects after the server rejects a connection,
210
+ // so the next request, e.g. one sent again by `retryLink`, opens the next
211
+ // one. It waits for the backoff too. `close()` forgets the rejection, so a
212
+ // request made once the client is idle again connects at once.
213
+ if (this.connectionRejected) {
214
+ // No socket is open while the request waits.
215
+ if (this.connectionState.get().state !== 'connecting') {
216
+ callUserCallback(() =>
217
+ this.connectionState.next({
218
+ type: 'state',
219
+ state: 'connecting',
220
+ error: null,
221
+ }),
222
+ );
223
+ }
224
+ this.reconnect(null);
225
+ return this.reconnecting;
226
+ }
209
227
  this.connectionAttemptIndex = 0;
210
228
  if (this.connectionState.get().state === 'idle') {
211
229
  callUserCallback(() =>
@@ -238,6 +256,10 @@ export class WsClient {
238
256
  */
239
257
  public async close() {
240
258
  this.allowReconnect = false;
259
+ this.connectionRejected = false;
260
+ this.reconnecting = null;
261
+ const nextConnection = this.nextConnection;
262
+ this.nextConnection = null;
241
263
  this.replacementError = null;
242
264
  this.retiredConnection = null;
243
265
  this.inactivityTimeoutEnabled = false;
@@ -263,7 +285,7 @@ export class WsClient {
263
285
  }
264
286
 
265
287
  await Promise.all(requestsToAwait).catch(() => null);
266
- this.nextConnection?.ws?.close();
288
+ nextConnection?.ws?.close();
267
289
  await this.activeConnection.close().catch(() => null);
268
290
 
269
291
  callUserCallback(() =>
@@ -403,8 +425,9 @@ export class WsClient {
403
425
  * error, e.g. because `createContext` threw. The server closes the socket
404
426
  * right after, so the reconnect that follows carries on with the backoff
405
427
  * instead of starting over at attempt 0. Cleared again when the socket
406
- * receives a result, which proves the connection was accepted. Also set
407
- * on a keepAlive pong timeout when the socket never received anything.
428
+ * receives a result, which proves the connection was accepted, and by
429
+ * `close()`. Also set on a keepAlive pong timeout when the socket never
430
+ * received anything.
408
431
  * @see https://github.com/trpc/trpc/issues/4774
409
432
  */
410
433
  private connectionRejected = false;
@@ -437,10 +460,14 @@ export class WsClient {
437
460
  }
438
461
  if (this.reconnecting) return;
439
462
 
463
+ // `close()` drops a reconnect that's still running, so a later `open()`
464
+ // doesn't wait for it.
465
+ const isCurrent = () =>
466
+ this.allowReconnect && this.reconnecting === reconnecting;
440
467
  const tryReconnect = async (attemptIndex: number) => {
441
468
  try {
442
469
  await sleep(this.reconnectRetryDelay(attemptIndex));
443
- if (this.allowReconnect) {
470
+ if (isCurrent()) {
444
471
  const next = this.createConnection();
445
472
  this.nextConnection = next;
446
473
  try {
@@ -454,10 +481,10 @@ export class WsClient {
454
481
  await next.close().catch(() => null);
455
482
  throw error;
456
483
  } finally {
457
- this.nextConnection = null;
484
+ if (this.nextConnection === next) this.nextConnection = null;
458
485
  }
459
486
 
460
- if (this.allowReconnect) {
487
+ if (isCurrent()) {
461
488
  // Read only now: losing the active socket while `next` opened may
462
489
  // have restored the one it replaced.
463
490
  const previous = this.activeConnection;
@@ -470,17 +497,17 @@ export class WsClient {
470
497
  await next.close();
471
498
  }
472
499
  }
473
- this.reconnecting = null;
500
+ if (this.reconnecting === reconnecting) this.reconnecting = null;
474
501
  } catch (error) {
475
502
  const closedError = new TRPCWebSocketClosedError({
476
503
  message: 'Initialization error',
477
504
  cause: error,
478
505
  });
479
- if (this.allowReconnect && this.activeConnection.isOpen()) {
506
+ if (isCurrent() && this.activeConnection.isOpen()) {
480
507
  // The old socket keeps serving every request, so this is shown only
481
508
  // once it closes.
482
509
  this.replacementError = closedError;
483
- } else if (this.allowReconnect) {
510
+ } else if (isCurrent()) {
484
511
  callUserCallback(() =>
485
512
  this.connectionState.next({
486
513
  type: 'state',
@@ -493,9 +520,10 @@ export class WsClient {
493
520
  }
494
521
  };
495
522
 
496
- this.reconnecting = tryReconnect(
523
+ const reconnecting = tryReconnect(
497
524
  this.connectionRejected ? this.connectionAttemptIndex + 1 : 0,
498
525
  );
526
+ this.reconnecting = reconnecting;
499
527
  }
500
528
 
501
529
  /**
@@ -641,7 +669,11 @@ export class WsClient {
641
669
  }
642
670
 
643
671
  private setupWebSocketListeners(ws: WebSocket) {
644
- this.connectionRejected = false;
672
+ // A socket from a reconnect that `close()` abandoned says nothing about
673
+ // the connection the client now uses.
674
+ if (ws === this.activeConnection.ws || ws === this.nextConnection?.ws) {
675
+ this.connectionRejected = false;
676
+ }
645
677
 
646
678
  /**
647
679
  * Fails the queries and mutations sent on `ws`. Its subscriptions are
@@ -700,7 +732,11 @@ export class WsClient {
700
732
 
701
733
  ws.addEventListener('open', () => {
702
734
  run(async () => {
703
- if (this.lazyMode) {
735
+ // A socket from a reconnect that `close()` abandoned changes neither
736
+ // the state nor the inactivity timeout.
737
+ const isLive =
738
+ ws === this.activeConnection.ws || ws === this.nextConnection?.ws;
739
+ if (this.lazyMode && isLive) {
704
740
  this.inactivityTimeoutEnabled = true;
705
741
  this.startInactivityTimeout();
706
742
  }
@@ -710,6 +746,7 @@ export class WsClient {
710
746
  // A socket opened to replace one that's still open changes nothing
711
747
  // the subscribers can see until `handOff()` puts it in use.
712
748
  if (
749
+ !isLive ||
713
750
  this.connectionState.get().state === 'pending' ||
714
751
  (ws !== this.activeConnection.ws && this.activeConnection.isOpen())
715
752
  ) {
@@ -1 +0,0 @@
1
- {"version":3,"file":"wsLink-DSmHMirJ.mjs","names":[],"sources":["../src/links/wsLink/wsClient/encoder.ts","../src/links/wsLink/wsClient/options.ts","../src/links/internals/urlWithConnectionParams.ts","../src/links/wsLink/wsClient/utils.ts","../src/links/wsLink/wsClient/requestManager.ts","../src/links/wsLink/wsClient/wsConnection.ts","../src/links/wsLink/wsClient/wsClient.ts","../src/links/wsLink/createWsClient.ts","../src/links/wsLink/wsLink.ts"],"sourcesContent":["import type { Encoder } from '@trpc/server/unstable-core-do-not-import';\n\nexport type { Encoder };\n\n/**\n * The default WebSocket encoder. Sends messages as JSON text frames.\n */\nexport const jsonEncoder: Encoder = {\n encode: (data) => JSON.stringify(data),\n decode: (data) => {\n if (typeof data !== 'string') {\n throw new Error(\n 'jsonEncoder received binary data. JSON uses text frames. ' +\n 'Use a binary encoder for binary data.',\n );\n }\n return JSON.parse(data);\n },\n};\n","import type { UrlOptionsWithConnectionParams } from '../../internals/urlWithConnectionParams';\nimport type { Encoder } from './encoder';\n\n/**\n * Options for `createWSClient`.\n *\n * @see https://trpc.io/docs/client/links/wsLink#wslink--createwsclient-options\n */\nexport interface WebSocketClientOptions extends UrlOptionsWithConnectionParams {\n /**\n * The `WebSocket` class to use, for runtimes that don't have one.\n */\n WebSocket?: typeof WebSocket;\n /**\n * The number of milliseconds before a reconnect is attempted.\n * @default {@link exponentialBackoff}\n */\n retryDelayMs?: (attemptIndex: number) => number;\n /**\n * Triggered when a WebSocket connection is established\n */\n onOpen?: () => void;\n /**\n * Triggered when a WebSocket connection encounters an error\n */\n onError?: (evt?: Event) => void;\n /**\n * Triggered when a WebSocket connection is closed\n */\n onClose?: (cause?: { code?: number }) => void;\n /**\n * Lazy mode will close the WebSocket automatically after a period of inactivity (no messages sent or received and no pending requests)\n */\n lazy?: {\n /**\n * Enable lazy mode\n * @default false\n */\n enabled: boolean;\n /**\n * Close the WebSocket after this many milliseconds\n * @default 0\n */\n closeMs: number;\n };\n /**\n * Send ping messages to the server and kill the connection if no pong message is returned\n */\n keepAlive?: {\n /**\n * @default false\n */\n enabled: boolean;\n /**\n * Send a ping message every this many milliseconds\n * @default 5_000\n */\n intervalMs?: number;\n /**\n * Close the WebSocket after this many milliseconds if the server does not respond\n * @default 1_000\n */\n pongTimeoutMs?: number;\n };\n /**\n * Encodes and decodes messages on the wire, for example as a binary format.\n * @default jsonEncoder\n */\n experimental_encoder?: Encoder;\n}\n\n/**\n * Default options for lazy WebSocket connections.\n * Determines whether the connection should be established lazily and defines the delay before closure.\n */\nexport type LazyOptions = Required<NonNullable<WebSocketClientOptions['lazy']>>;\nexport const lazyDefaults: LazyOptions = {\n enabled: false,\n closeMs: 0,\n};\n\n/**\n * Default options for the WebSocket keep-alive mechanism.\n * Configures whether keep-alive is enabled and specifies the timeout and interval for ping-pong messages.\n */\nexport type KeepAliveOptions = Required<\n NonNullable<WebSocketClientOptions['keepAlive']>\n>;\nexport const keepAliveDefaults: KeepAliveOptions = {\n enabled: false,\n pongTimeoutMs: 1_000,\n intervalMs: 5_000,\n};\n\n/**\n * Calculates a delay for exponential backoff based on the retry attempt index.\n * The delay starts at 0 for the first attempt and doubles for each subsequent attempt,\n * capped at 30 seconds.\n */\nexport const exponentialBackoff = (attemptIndex: number) => {\n return attemptIndex === 0 ? 0 : Math.min(1000 * 2 ** attemptIndex, 30000);\n};\n","import { type TRPCRequestInfo } from '@trpc/server/http';\n\n/**\n * Get the result of a value or function that returns a value\n * It also optionally accepts typesafe arguments for the function\n */\nexport const resultOf = <T, TArgs extends any[]>(\n value: T | ((...args: TArgs) => T),\n ...args: TArgs\n): T => {\n return typeof value === 'function'\n ? (value as (...args: TArgs) => T)(...args)\n : value;\n};\n\n/**\n * A value that can be wrapped in callback\n */\nexport type CallbackOrValue<T> = T | (() => T | Promise<T>);\n\nexport interface UrlOptionsWithConnectionParams {\n /**\n * The URL to connect to (can be a function that returns a URL)\n */\n url: CallbackOrValue<string>;\n\n /**\n * Connection params that are available in `createContext()`\n * - For `wsLink`/`wsClient`, these are sent as the first message\n * - For `httpSubscriptionLink`, these are serialized as part of the URL under the `connectionParams` query\n * @see https://trpc.io/docs/server/websockets#connectionParams\n */\n connectionParams?: CallbackOrValue<TRPCRequestInfo['connectionParams']>;\n}\n","import type {\n TRPCConnectionParamsMessage,\n TRPCRequestInfo,\n} from '@trpc/server/unstable-core-do-not-import';\nimport type {\n CallbackOrValue,\n UrlOptionsWithConnectionParams,\n} from '../../internals/urlWithConnectionParams';\nimport { resultOf } from '../../internals/urlWithConnectionParams';\nimport type { Encoder } from './encoder';\n\nexport class TRPCWebSocketClosedError extends Error {\n constructor(opts: { message: string; cause?: unknown }) {\n super(opts.message, {\n cause: opts.cause,\n });\n this.name = 'TRPCWebSocketClosedError';\n Object.setPrototypeOf(this, TRPCWebSocketClosedError.prototype);\n }\n}\n\n/**\n * Calls a user callback, including `connectionState` subscribers. A throw\n * would skip the rest of the client's bookkeeping (e.g. end the reconnect\n * loop, or, with event-emitter based WebSockets such as `ws`, skip the other\n * listeners for that event), so it is rethrown asynchronously instead.\n */\nexport function callUserCallback(callback: () => void) {\n try {\n callback();\n } catch (error) {\n setTimeout(() => {\n throw error;\n });\n }\n}\n\n/**\n * Utility class for managing a timeout that can be started, stopped, and reset.\n * Useful for scenarios where the timeout duration is reset dynamically based on events.\n */\nexport class ResettableTimeout {\n private timeout: ReturnType<typeof setTimeout> | undefined;\n\n constructor(\n private readonly onTimeout: () => void,\n private readonly timeoutMs: number,\n ) {}\n\n /**\n * Resets the current timeout, restarting it with the same duration.\n * Does nothing if no timeout is active.\n */\n public reset() {\n if (!this.timeout) return;\n\n clearTimeout(this.timeout);\n this.timeout = setTimeout(this.onTimeout, this.timeoutMs);\n }\n\n public start() {\n clearTimeout(this.timeout);\n this.timeout = setTimeout(this.onTimeout, this.timeoutMs);\n }\n\n public stop() {\n clearTimeout(this.timeout);\n this.timeout = undefined;\n }\n}\n\n// Ponyfill for Promise.withResolvers https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/withResolvers\nexport function withResolvers<T>() {\n let resolve: (value: T | PromiseLike<T>) => void;\n let reject: (reason?: any) => void;\n const promise = new Promise<T>((res, rej) => {\n resolve = res;\n reject = rej;\n });\n\n // oxlint-disable-next-line typescript/no-non-null-assertion\n return { promise, resolve: resolve!, reject: reject! };\n}\n\n/**\n * Resolves a WebSocket URL and optionally appends connection parameters.\n *\n * If connectionParams are provided, appends 'connectionParams=1' query parameter.\n */\nexport async function prepareUrl(urlOptions: UrlOptionsWithConnectionParams) {\n const url = await resultOf(urlOptions.url);\n\n if (!urlOptions.connectionParams) return url;\n\n // append `?connectionParams=1` when connection params are used\n const prefix = url.includes('?') ? '&' : '?';\n const connectionParams = `${prefix}connectionParams=1`;\n\n return url + connectionParams;\n}\n\nexport async function buildConnectionMessage(\n connectionParams: CallbackOrValue<TRPCRequestInfo['connectionParams']>,\n encoder: Encoder,\n) {\n const message: TRPCConnectionParamsMessage = {\n method: 'connectionParams',\n data: await resultOf(connectionParams),\n };\n\n return encoder.encode(message);\n}\n","import type { AnyTRPCRouter, inferRouterError } from '@trpc/server';\nimport type { Observer } from '@trpc/server/observable';\nimport type {\n TRPCClientOutgoingMessage,\n TRPCResponseMessage,\n} from '@trpc/server/unstable-core-do-not-import';\nimport type { TRPCClientError } from '../../../TRPCClientError';\nimport { withResolvers } from './utils';\n\nexport type TCallbacks = Observer<\n TRPCResponseMessage<unknown, inferRouterError<AnyTRPCRouter>>,\n TRPCClientError<AnyTRPCRouter>\n>;\n\ntype MessageId = string;\ntype MessageIdLike = string | number | null;\n\n/**\n * Represents a WebSocket request managed by the RequestManager.\n * Combines the network message, a utility promise (`end`) that mirrors the lifecycle\n * handled by `callbacks`, and a set of state monitoring callbacks.\n */\ninterface Request {\n message: TRPCClientOutgoingMessage;\n end: Promise<void>;\n callbacks: TCallbacks;\n /**\n * The socket the request was sent on, or `null` while it's outgoing.\n */\n ws: WebSocket | null;\n /**\n * The socket a subscription is moving to. `ws` keeps delivering until this\n * one confirms the subscription with `started`.\n */\n successor: WebSocket | null;\n /**\n * The event ids `ws` delivered while the subscription was moving, which\n * its successor replays and are dropped from it.\n */\n overlapEventIds: Set<string> | null;\n /**\n * The sockets a subscription moved from, each until everything it sent\n * before the stop has arrived. Their events without ids are still\n * delivered, since the new socket may have started listening after they\n * were sent.\n */\n movedFrom: Set<WebSocket>;\n}\n\n/**\n * Manages WebSocket requests, tracking their lifecycle and providing utility methods\n * for handling outgoing and pending requests.\n *\n * - **Outgoing requests**: Requests that are queued and waiting to be sent.\n * - **Pending requests**: Requests that have been sent and are in flight awaiting a response.\n * For subscriptions, multiple responses may be received until the subscription is closed.\n */\nexport class RequestManager {\n constructor(\n private readonly opts: {\n /**\n * Called when a deletion leaves no outgoing or pending request.\n */\n onIdle: () => void;\n },\n ) {}\n\n /**\n * Stores requests that are outgoing, meaning they are registered but not yet sent over the WebSocket.\n */\n private outgoingRequests = new Array<Request & { id: MessageId }>();\n\n /**\n * Stores requests that are pending (in flight), meaning they have been sent over the WebSocket\n * and are awaiting responses. For subscriptions, this includes requests\n * that may receive multiple responses.\n */\n private pendingRequests: Record<MessageId, Request> = {};\n\n /**\n * Subscriptions sent again under a new id, by their earlier id, while\n * sockets they moved from may still send events under that one.\n */\n private drainingRequests: Record<MessageId, Request> = {};\n\n /**\n * Registers a new request by adding it to the outgoing queue and setting up\n * callbacks for lifecycle events such as completion or error.\n *\n * @param message - The outgoing message to be sent.\n * @param callbacks - Callback functions to observe the request's state.\n * @returns A cleanup function to manually remove the request.\n */\n public register(message: TRPCClientOutgoingMessage, callbacks: TCallbacks) {\n const { promise: end, resolve } = withResolvers<void>();\n\n this.outgoingRequests.push({\n id: String(message.id),\n message,\n end,\n ws: null,\n successor: null,\n overlapEventIds: null,\n movedFrom: new Set(),\n callbacks: {\n next: callbacks.next,\n complete: () => {\n callbacks.complete();\n resolve();\n },\n error: (e) => {\n try {\n callbacks.error(e);\n } finally {\n resolve();\n }\n },\n },\n });\n\n return () => {\n this.delete(message);\n callbacks.complete();\n resolve();\n };\n }\n\n /**\n * Deletes a request from both the outgoing and pending collections, if it exists.\n *\n * Matches the message itself rather than its id: a retry reuses the id, and\n * registers its request before the failed request with that id is deleted.\n */\n public delete(message: TRPCClientOutgoingMessage) {\n const wasInFlight = this.hasRequestsInFlight();\n this.outgoingRequests = this.outgoingRequests.filter(\n (request) => request.message !== message,\n );\n const id = String(message.id);\n if (this.pendingRequests[id]?.message === message) {\n delete this.pendingRequests[id];\n }\n for (const [drainingId, request] of Object.entries(this.drainingRequests)) {\n if (request.message === message) {\n delete this.drainingRequests[drainingId];\n }\n }\n if (wasInFlight && !this.hasRequestsInFlight()) {\n this.opts.onIdle();\n }\n }\n\n /**\n * Moves all outgoing requests to the pending state and clears the outgoing queue.\n *\n * The caller is expected to handle the actual sending of the requests\n * (e.g., sending them over the network) after this method is called.\n *\n * @param ws - The socket the requests are sent on.\n * @returns The list of requests that were transitioned to the pending state.\n */\n public flush(ws: WebSocket | null = null) {\n const requests = this.outgoingRequests;\n this.outgoingRequests = [];\n\n for (const request of requests) {\n request.ws = ws;\n request.successor = null;\n request.movedFrom = new Set();\n this.pendingRequests[request.id] = request;\n }\n return requests;\n }\n\n /**\n * Moves a pending request back to the outgoing queue, to be sent again\n * with `id`. Its earlier id then matches only the sockets it's still\n * draining; see `getDrainingRequest()`.\n */\n public requeue(\n message: TRPCClientOutgoingMessage,\n id: TRPCClientOutgoingMessage['id'],\n ) {\n const earlierId = String(message.id);\n const request = this.pendingRequests[earlierId];\n if (request?.message !== message) return;\n\n delete this.pendingRequests[earlierId];\n if (request.movedFrom.size) this.drainingRequests[earlierId] = request;\n message.id = id;\n this.outgoingRequests.push({\n ...request,\n id: String(id),\n ws: null,\n successor: null,\n overlapEventIds: null,\n movedFrom: new Set(),\n });\n }\n\n /**\n * Retrieves all currently pending requests, which are in flight awaiting responses\n * or handling ongoing subscriptions.\n */\n public getPendingRequests() {\n return Object.values(this.pendingRequests);\n }\n\n /**\n * Retrieves a specific pending request by its message ID.\n */\n public getPendingRequest(messageId: MessageIdLike) {\n if (messageId === null) return null;\n\n return this.pendingRequests[String(messageId)];\n }\n\n /**\n * Retrieves a subscription sent again under a new id by its earlier id,\n * while a socket it moved from may still send events under that one.\n */\n public getDrainingRequest(messageId: MessageIdLike) {\n if (messageId === null) return null;\n\n return this.drainingRequests[String(messageId)];\n }\n\n /**\n * Retrieves the subscriptions sent again under a new id whose earlier id\n * is still in use on a socket they moved from.\n */\n public getDrainingRequests() {\n return Object.values(this.drainingRequests);\n }\n\n /**\n * Forgets the earlier ids of subscriptions no longer draining any socket.\n */\n public deleteDrained() {\n for (const [id, request] of Object.entries(this.drainingRequests)) {\n if (!request.movedFrom.size) delete this.drainingRequests[id];\n }\n }\n\n /**\n * Retrieves all outgoing requests, which are waiting to be sent.\n */\n public getOutgoingRequests() {\n return this.outgoingRequests;\n }\n\n /**\n * Retrieves all requests, both outgoing and pending, with their respective states.\n *\n * @returns An array of all requests with their state (\"outgoing\" or \"pending\").\n */\n public getRequests() {\n return [\n ...this.getOutgoingRequests().map((request) => ({\n state: 'outgoing' as const,\n message: request.message,\n end: request.end,\n callbacks: request.callbacks,\n })),\n ...this.getPendingRequests().map((request) => ({\n state: 'pending' as const,\n message: request.message,\n end: request.end,\n callbacks: request.callbacks,\n })),\n ];\n }\n\n /**\n * Checks if there are any pending requests, including ongoing subscriptions.\n */\n public hasPendingRequests() {\n return this.getPendingRequests().length > 0;\n }\n\n /**\n * Checks if there are any pending subscriptions\n */\n public hasPendingSubscriptions() {\n return this.getPendingRequests().some(\n (request) => request.message.method === 'subscription',\n );\n }\n\n /**\n * Checks if a message is still waiting to be sent.\n */\n public isOutgoing(message: TRPCClientOutgoingMessage) {\n return this.outgoingRequests.some((request) => request.message === message);\n }\n\n /**\n * Checks if there are any outgoing requests waiting to be sent.\n */\n public hasOutgoingRequests() {\n return this.outgoingRequests.length > 0;\n }\n\n /**\n * Checks if any request is outgoing or pending, including subscriptions.\n */\n public hasRequestsInFlight() {\n return this.hasOutgoingRequests() || this.hasPendingRequests();\n }\n}\n","import { behaviorSubject } from '@trpc/server/observable';\nimport type { UrlOptionsWithConnectionParams } from '../../internals/urlWithConnectionParams';\nimport type { Encoder } from './encoder';\nimport {\n buildConnectionMessage,\n prepareUrl,\n TRPCWebSocketClosedError,\n withResolvers,\n} from './utils';\n\n/**\n * Opens a WebSocket connection asynchronously and returns a promise\n * that resolves when the connection is successfully established.\n * The promise rejects if an error occurs during the connection attempt.\n */\nfunction asyncWsOpen(ws: WebSocket) {\n const { promise, resolve, reject } = withResolvers<void>();\n\n ws.addEventListener('open', () => {\n ws.removeEventListener('error', reject);\n resolve();\n });\n ws.addEventListener('error', reject);\n\n return promise;\n}\n\nconst pongCallbacks = new WeakMap<WebSocket, ((() => void) | null)[]>();\n\n/**\n * Sends \"PING\" on `ws` and calls `onPong`, if given, once its \"PONG\"\n * arrives. The server answers in order, so by then everything it sent before\n * it read this \"PING\" has arrived too.\n */\nexport function sendPing(ws: WebSocket, onPong: (() => void) | null) {\n let callbacks = pongCallbacks.get(ws);\n if (!callbacks) {\n const queue: ((() => void) | null)[] = [];\n callbacks = queue;\n pongCallbacks.set(ws, queue);\n ws.addEventListener('message', ({ data }) => {\n if (data === 'PONG') queue.shift()?.();\n });\n }\n callbacks.push(onPong);\n ws.send('PING');\n}\n\ninterface PingPongOptions {\n /**\n * The interval (in milliseconds) between \"PING\" messages.\n */\n intervalMs: number;\n\n /**\n * The timeout (in milliseconds) to wait for a \"PONG\" response before closing the connection.\n */\n pongTimeoutMs: number;\n}\n\n/**\n * Sets up a periodic ping-pong mechanism to keep the WebSocket connection alive.\n *\n * - Sends \"PING\" messages at regular intervals defined by `intervalMs`.\n * - If a \"PONG\" response is not received within the `pongTimeoutMs`, the WebSocket is closed.\n * - The ping timer resets upon receiving any message to maintain activity.\n * - Automatically starts the ping process when the WebSocket connection is opened.\n * - Cleans up timers when the WebSocket is closed.\n *\n * @param ws - The WebSocket instance to manage.\n * @param options - Configuration options for ping-pong intervals and timeouts.\n */\nfunction setupPingInterval(\n ws: WebSocket,\n { intervalMs, pongTimeoutMs }: PingPongOptions,\n onPongTimeout: () => void,\n) {\n let pingTimeout: ReturnType<typeof setTimeout> | undefined;\n let pongTimeout: ReturnType<typeof setTimeout> | undefined;\n\n function start() {\n pingTimeout = setTimeout(() => {\n sendPing(ws, null);\n pongTimeout = setTimeout(() => {\n // A socket that is already closing was closed on purpose.\n if (ws.readyState !== ws.OPEN) return;\n ws.close();\n onPongTimeout();\n }, pongTimeoutMs);\n }, intervalMs);\n }\n\n function reset() {\n clearTimeout(pingTimeout);\n start();\n }\n\n function pong() {\n clearTimeout(pongTimeout);\n reset();\n }\n\n ws.addEventListener('open', start);\n ws.addEventListener('message', ({ data }) => {\n clearTimeout(pingTimeout);\n start();\n\n if (data === 'PONG') {\n pong();\n }\n });\n ws.addEventListener('close', () => {\n clearTimeout(pingTimeout);\n clearTimeout(pongTimeout);\n });\n}\n\nexport interface WebSocketConnectionOptions {\n WebSocketPonyfill?: typeof WebSocket;\n urlOptions: UrlOptionsWithConnectionParams;\n keepAlive: PingPongOptions & {\n enabled: boolean;\n };\n encoder: Encoder;\n /**\n * Called when a keepAlive pong times out, once the socket is closing. Its\n * `close` event can come much later: an unresponsive server never answers\n * the closing handshake.\n */\n onPongTimeout: (ws: WebSocket) => void;\n}\n\n/**\n * Manages a WebSocket connection with support for reconnection, keep-alive mechanisms,\n * and observable state tracking.\n */\nexport class WsConnection {\n static connectCount = 0;\n public id = ++WsConnection.connectCount;\n\n private readonly WebSocketPonyfill: typeof WebSocket;\n private readonly urlOptions: UrlOptionsWithConnectionParams;\n private readonly keepAliveOpts: WebSocketConnectionOptions['keepAlive'];\n private readonly encoder: Encoder;\n private readonly onPongTimeout: WebSocketConnectionOptions['onPongTimeout'];\n public readonly wsObservable = behaviorSubject<WebSocket | null>(null);\n\n constructor(opts: WebSocketConnectionOptions) {\n this.WebSocketPonyfill = opts.WebSocketPonyfill ?? WebSocket;\n if (!this.WebSocketPonyfill) {\n throw new Error(\n \"No WebSocket implementation found - you probably don't want to use this on the server, but if you do you need to pass a `WebSocket`-ponyfill\",\n );\n }\n\n this.urlOptions = opts.urlOptions;\n this.keepAliveOpts = opts.keepAlive;\n this.encoder = opts.encoder;\n this.onPongTimeout = opts.onPongTimeout;\n }\n\n public get ws() {\n return this.wsObservable.get();\n }\n\n private set ws(ws) {\n this.wsObservable.next(ws);\n }\n\n /**\n * Checks if the WebSocket connection is open and ready to communicate.\n */\n public isOpen(): this is { ws: WebSocket } {\n return (\n !!this.ws &&\n this.ws.readyState === this.WebSocketPonyfill.OPEN &&\n !this.openPromise\n );\n }\n\n /**\n * Checks if the WebSocket connection is closed or in the process of closing.\n */\n public isClosed(): this is { ws: WebSocket } {\n return (\n !!this.ws &&\n (this.ws.readyState === this.WebSocketPonyfill.CLOSING ||\n this.ws.readyState === this.WebSocketPonyfill.CLOSED)\n );\n }\n\n /**\n * Manages the WebSocket opening process, ensuring that only one open operation\n * occurs at a time. Tracks the ongoing operation with `openPromise` to avoid\n * redundant calls and ensure proper synchronization.\n *\n * Sets up the keep-alive mechanism and necessary event listeners for the connection.\n *\n * @returns A promise that resolves once the WebSocket connection is successfully opened.\n */\n private openPromise: Promise<void> | null = null;\n public async open() {\n if (this.openPromise) return this.openPromise;\n\n this.id = ++WsConnection.connectCount;\n const wsPromise = prepareUrl(this.urlOptions).then(\n (url) => new this.WebSocketPonyfill(url),\n );\n const openPromise = wsPromise.then(async (ws) => {\n this.ws = ws;\n\n // Set binaryType to handle both text and binary messages consistently\n ws.binaryType = 'arraybuffer';\n\n // Setup ping listener\n ws.addEventListener('message', function ({ data }) {\n if (data === 'PING') {\n this.send('PONG');\n }\n });\n\n const abandoned = withResolvers<never>();\n if (this.keepAliveOpts.enabled) {\n setupPingInterval(ws, this.keepAliveOpts, () => {\n // A pong timeout gives up on an attempt still waiting on its\n // `connectionParams`, so the reconnect doesn't wait on them. The\n // attempt stays in `openPromise` until it rejects, so an `open()`\n // meanwhile, e.g. from `onClose`, gets that rejection instead of\n // starting another attempt on this connection.\n if (this.openPromise === openPromise) {\n abandoned.reject(\n new TRPCWebSocketClosedError({\n message: 'Pong timeout before connectionParams were sent',\n }),\n );\n }\n this.onPongTimeout(ws);\n });\n }\n\n ws.addEventListener('close', () => {\n if (this.ws === ws) {\n this.ws = null;\n }\n });\n\n await asyncWsOpen(ws);\n\n if (this.urlOptions.connectionParams) {\n ws.send(\n await Promise.race([\n buildConnectionMessage(\n this.urlOptions.connectionParams,\n this.encoder,\n ),\n abandoned.promise,\n ]),\n );\n }\n });\n this.openPromise = openPromise;\n\n try {\n await openPromise;\n } finally {\n if (this.openPromise === openPromise) {\n this.openPromise = null;\n }\n }\n }\n\n /**\n * Closes the WebSocket connection gracefully.\n * Waits for any ongoing open operation to complete before closing.\n */\n public async close() {\n try {\n await this.openPromise;\n } finally {\n this.ws?.close();\n }\n }\n}\n\n/**\n * Provides a backward-compatible representation of the connection state.\n */\nexport function backwardCompatibility(connection: WsConnection) {\n if (connection.isOpen()) {\n return {\n id: connection.id,\n state: 'open',\n ws: connection.ws,\n } as const;\n }\n\n if (connection.isClosed()) {\n return {\n id: connection.id,\n state: 'closed',\n ws: connection.ws,\n } as const;\n }\n\n if (!connection.ws) {\n return null;\n }\n\n return {\n id: connection.id,\n state: 'connecting',\n ws: connection.ws,\n } as const;\n}\n","import type { AnyTRPCRouter } from '@trpc/server';\nimport type { BehaviorSubject } from '@trpc/server/observable';\nimport { behaviorSubject, observable } from '@trpc/server/observable';\nimport type {\n CombinedDataTransformer,\n TRPCClientIncomingMessage,\n TRPCClientIncomingRequest,\n TRPCClientOutgoingMessage,\n TRPCResponseMessage,\n} from '@trpc/server/unstable-core-do-not-import';\nimport {\n isObject,\n run,\n sleep,\n transformResult,\n} from '@trpc/server/unstable-core-do-not-import';\nimport { TRPCClientError } from '../../../TRPCClientError';\nimport type { TRPCConnectionState } from '../../internals/subscriptions';\nimport type { Operation, OperationResultEnvelope } from '../../types';\nimport type { Encoder } from './encoder';\nimport { jsonEncoder } from './encoder';\nimport type { WebSocketClientOptions } from './options';\nimport { exponentialBackoff, keepAliveDefaults, lazyDefaults } from './options';\nimport type { TCallbacks } from './requestManager';\nimport { RequestManager } from './requestManager';\nimport {\n callUserCallback,\n ResettableTimeout,\n TRPCWebSocketClosedError,\n} from './utils';\nimport type { WebSocketConnectionOptions } from './wsConnection';\nimport { backwardCompatibility, sendPing, WsConnection } from './wsConnection';\n\nfunction isRequestId(value: unknown) {\n return (\n value === null || typeof value === 'string' || typeof value === 'number'\n );\n}\n\n/** A result's `type`; a missing one means `'data'`. */\nfunction isResultType(value: unknown) {\n return (\n value === undefined ||\n value === 'data' ||\n value === 'started' ||\n value === 'stopped'\n );\n}\n\n/**\n * A `reconnect` request from the server, or a response: a string, number or\n * `null` `id` and either a `result` object of a known `type` or an `error`.\n * The output transformer may serialize an error to any value, so\n * `transformResult` validates it.\n */\nfunction isIncomingMessage(value: unknown): value is TRPCClientIncomingMessage {\n if (!isObject(value)) return false;\n if ('method' in value) return value['method'] === 'reconnect';\n if (!isRequestId(value['id'])) return false;\n const result = value['result'];\n if (isObject(result)) return isResultType(result['type']);\n return result === undefined && 'error' in value;\n}\n\nfunction decodeIncomingMessage(\n encoder: Encoder,\n data: string | ArrayBuffer | Uint8Array,\n) {\n const message = encoder.decode(data);\n if (!isIncomingMessage(message)) {\n throw new TypeError('Expected a tRPC message');\n }\n return message;\n}\n\n/**\n * A WebSocket client for managing TRPC operations, supporting lazy initialization,\n * reconnection, keep-alive, and request management.\n */\nexport class WsClient {\n /**\n * Observable tracking the current connection state, including errors.\n */\n public readonly connectionState: BehaviorSubject<\n TRPCConnectionState<TRPCClientError<AnyTRPCRouter>>\n >;\n\n private allowReconnect = false;\n private requestManager = new RequestManager({\n onIdle: () => this.startInactivityTimeout(),\n });\n /**\n * The last id sent on the wire. Each request gets its own instead of its\n * `op.id`: a retry reuses the failed attempt's `op.id`, and the server's\n * late frames for that attempt must not reach the retry. A subscription\n * sent again after its socket was lost gets a new one for the same reason.\n */\n private lastRequestId = 0;\n private readonly connectionOptions: WebSocketConnectionOptions;\n /**\n * The connection new requests are sent on. A reconnect replaces it only\n * once the new connection is fully open, `connectionParams` included, so\n * until then requests keep going to the old one.\n */\n private activeConnection: WsConnection;\n private readonly reconnectRetryDelay: (attemptIndex: number) => number;\n private inactivityTimeout: ResettableTimeout;\n /**\n * Whether lazy mode may close the connection once it's idle: from the\n * first `open` until `close()`.\n */\n private inactivityTimeoutEnabled = false;\n private readonly callbacks: Pick<\n WebSocketClientOptions,\n 'onOpen' | 'onClose' | 'onError'\n >;\n private readonly lazyMode: boolean;\n private readonly encoder: Encoder;\n private readonly closeOnPongTimeout = new WeakMap<WebSocket, () => void>();\n\n constructor(opts: WebSocketClientOptions) {\n this.encoder = opts.experimental_encoder ?? jsonEncoder;\n // Initialize callbacks, connection parameters, and options.\n this.callbacks = {\n onOpen: opts.onOpen,\n onClose: opts.onClose,\n onError: opts.onError,\n };\n\n const lazyOptions = {\n ...lazyDefaults,\n ...opts.lazy,\n };\n\n // Set up inactivity timeout for lazy connections. It only runs while no\n // request or subscription is in flight; see `startInactivityTimeout()`.\n this.inactivityTimeout = new ResettableTimeout(() => {\n if (this.requestManager.hasRequestsInFlight()) {\n this.inactivityTimeout.stop();\n return;\n }\n\n this.close().catch(() => null);\n }, lazyOptions.closeMs);\n\n // Initialize the WebSocket connection.\n this.connectionOptions = {\n WebSocketPonyfill: opts.WebSocket,\n urlOptions: opts,\n keepAlive: {\n ...keepAliveDefaults,\n ...opts.keepAlive,\n },\n encoder: this.encoder,\n onPongTimeout: (ws) => this.closeOnPongTimeout.get(ws)?.(),\n };\n this.activeConnection = this.createConnection();\n this.reconnectRetryDelay = opts.retryDelayMs ?? exponentialBackoff;\n\n this.lazyMode = lazyOptions.enabled;\n\n this.connectionState = behaviorSubject<\n TRPCConnectionState<TRPCClientError<AnyTRPCRouter>>\n >({\n type: 'state',\n state: lazyOptions.enabled ? 'idle' : 'connecting',\n error: null,\n });\n\n // Automatically open the connection if lazy mode is disabled.\n if (!this.lazyMode) {\n this.open().catch(() => null);\n }\n }\n\n /**\n * Starts counting down to closing a lazy connection, if nothing is in\n * flight. Called on `open` and whenever the last request or subscription\n * ends, so the timer never has to poll.\n */\n private startInactivityTimeout() {\n if (\n !this.inactivityTimeoutEnabled ||\n this.requestManager.hasRequestsInFlight()\n ) {\n return;\n }\n this.inactivityTimeout.start();\n }\n\n private createConnection() {\n const connection = new WsConnection(this.connectionOptions);\n connection.wsObservable.subscribe({\n next: (ws) => {\n if (!ws) return;\n this.setupWebSocketListeners(ws);\n },\n });\n return connection;\n }\n\n /**\n * Opens the WebSocket connection. Handles reconnection attempts and updates\n * the connection state accordingly.\n */\n private async open() {\n this.allowReconnect = true;\n if (this.reconnecting) return this.reconnecting;\n this.connectionAttemptIndex = 0;\n if (this.connectionState.get().state === 'idle') {\n callUserCallback(() =>\n this.connectionState.next({\n type: 'state',\n state: 'connecting',\n error: null,\n }),\n );\n }\n\n try {\n await this.activeConnection.open();\n } catch (error) {\n this.reconnect(\n new TRPCWebSocketClosedError({\n message: 'Initialization error',\n cause: error,\n }),\n );\n // A socket whose `connectionParams` failed is open, but not usable.\n await this.activeConnection.close().catch(() => null);\n return this.reconnecting;\n }\n }\n\n /**\n * Closes the WebSocket connection and stops managing requests.\n * Ensures all outgoing and pending requests are properly finalized.\n */\n public async close() {\n this.allowReconnect = false;\n this.replacementError = null;\n this.retiredConnection = null;\n this.inactivityTimeoutEnabled = false;\n this.inactivityTimeout.stop();\n\n const requestsToAwait: Promise<void>[] = [];\n for (const request of this.requestManager.getRequests()) {\n if (request.message.method === 'subscription') {\n callUserCallback(() => request.callbacks.complete());\n } else if (request.state === 'outgoing') {\n callUserCallback(() =>\n request.callbacks.error(\n TRPCClientError.from(\n new TRPCWebSocketClosedError({\n message: 'Closed before connection was established',\n }),\n ),\n ),\n );\n } else {\n requestsToAwait.push(request.end);\n }\n }\n\n await Promise.all(requestsToAwait).catch(() => null);\n this.nextConnection?.ws?.close();\n await this.activeConnection.close().catch(() => null);\n\n callUserCallback(() =>\n this.connectionState.next({\n type: 'state',\n state: 'idle',\n error: null,\n }),\n );\n }\n\n /**\n * Method to request the server.\n * Handles data transformation, batching of requests, and subscription lifecycle.\n *\n * @param op - The operation details including type, path, input and signal. Each subscription to the returned observable is sent with a new id, not `op.id`.\n * @param transformer - Data transformer for serializing requests and deserializing responses\n * @param lastEventId - Optional ID of the last received event for subscriptions\n *\n * @returns An observable that emits operation results and handles cleanup\n */\n public request({\n op: { type, path, input, signal },\n transformer,\n lastEventId,\n }: {\n op: Pick<Operation, 'id' | 'type' | 'path' | 'input' | 'signal'>;\n transformer: CombinedDataTransformer;\n lastEventId?: string;\n }) {\n return observable<\n OperationResultEnvelope<unknown, TRPCClientError<AnyTRPCRouter>>,\n TRPCClientError<AnyTRPCRouter>\n >((observer) => {\n const message: TRPCClientOutgoingMessage = {\n id: ++this.lastRequestId,\n method: type,\n params: {\n input: transformer.input.serialize(input),\n path,\n lastEventId,\n },\n };\n const abort = this.batchSend(message, {\n ...observer,\n next(event) {\n let transformed: ReturnType<typeof transformResult>;\n try {\n transformed = transformResult(event, transformer.output);\n } catch (cause) {\n observer.error(TRPCClientError.from(cause as Error));\n return;\n }\n\n if (!transformed.ok) {\n observer.error(TRPCClientError.from(transformed.error));\n return;\n }\n\n observer.next({\n result: transformed.result,\n });\n },\n });\n\n const onAbort = () => observer.complete();\n if (signal?.aborted) {\n onAbort();\n } else {\n signal?.addEventListener('abort', onAbort, { once: true });\n }\n\n return () => {\n // A subscription sent again after its socket was lost has a new id.\n const { id } = message;\n const pending = this.requestManager.getPendingRequest(id);\n // Under its earlier ids, it may still be draining other sockets.\n const draining = this.requestManager\n .getDrainingRequests()\n .filter((request) => request.message === message);\n abort();\n\n if (type === 'subscription' && pending) {\n this.stopSubscription(id, pending.ws);\n this.stopSubscription(id, pending.successor);\n }\n this.closeIfDrained(pending?.ws);\n for (const request of [pending, ...draining]) {\n for (const earlier of request?.movedFrom ?? []) {\n this.closeIfDrained(earlier);\n }\n }\n\n signal?.removeEventListener('abort', onAbort);\n };\n });\n }\n\n /**\n * The current WebSocket and its state, or `null` when there is no socket.\n */\n public get connection() {\n return backwardCompatibility(\n this.activeConnection.ws\n ? this.activeConnection\n : (this.nextConnection ?? this.activeConnection),\n );\n }\n\n /**\n * Manages the reconnection process for the WebSocket using retry logic.\n * Ensures that only one reconnection attempt is active at a time by tracking the current\n * reconnection state in the `reconnecting` promise.\n */\n private reconnecting: Promise<void> | null = null;\n /**\n * The connection a reconnect is opening to replace the active one.\n */\n private nextConnection: WsConnection | null = null;\n /**\n * The connection the active one replaced, while it's open. If the active\n * one is lost first, this one serves every request again until the next.\n */\n private retiredConnection: WsConnection | null = null;\n /**\n * Why the latest attempt to replace a socket that's still open failed.\n * It's shown once that socket closes, since until then it serves every\n * request.\n */\n private replacementError: TRPCWebSocketClosedError | null = null;\n /**\n * The retry attempt that opened the current connection.\n */\n private connectionAttemptIndex = 0;\n /**\n * Set when the server rejects the current connection with an `id: null`\n * error, e.g. because `createContext` threw. The server closes the socket\n * right after, so the reconnect that follows carries on with the backoff\n * instead of starting over at attempt 0. Cleared again when the socket\n * receives a result, which proves the connection was accepted. Also set\n * on a keepAlive pong timeout when the socket never received anything.\n * @see https://github.com/trpc/trpc/issues/4774\n */\n private connectionRejected = false;\n /** Subscriptions whose subscriber has seen `started`. */\n private readonly started = new WeakSet<TRPCClientOutgoingMessage>();\n\n /**\n * Opens a new connection, retrying until one opens. While the current\n * socket is open, it keeps serving every request, new ones included, until\n * the new one is fully open, `connectionParams` included.\n *\n * @param closedError - Why the current socket was lost, or `null` when it's\n * still open and serving requests, e.g. on a server-requested reconnect.\n */\n private reconnect(closedError: TRPCWebSocketClosedError | null) {\n if (!this.allowReconnect) {\n return;\n }\n\n if (closedError) {\n const error = this.replacementError ?? closedError;\n this.replacementError = null;\n callUserCallback(() =>\n this.connectionState.next({\n type: 'state',\n state: 'connecting',\n error: TRPCClientError.from(error),\n }),\n );\n }\n if (this.reconnecting) return;\n\n const tryReconnect = async (attemptIndex: number) => {\n try {\n await sleep(this.reconnectRetryDelay(attemptIndex));\n if (this.allowReconnect) {\n const next = this.createConnection();\n this.nextConnection = next;\n try {\n await next.open();\n // `open()` resolves even if the socket closed while its\n // `connectionParams` were pending.\n if (!next.isOpen()) {\n throw new Error('WebSocket closed before it was ready');\n }\n } catch (error) {\n await next.close().catch(() => null);\n throw error;\n } finally {\n this.nextConnection = null;\n }\n\n if (this.allowReconnect) {\n // Read only now: losing the active socket while `next` opened may\n // have restored the one it replaced.\n const previous = this.activeConnection;\n this.activeConnection = next;\n this.retiredConnection = previous.isOpen() ? previous : null;\n this.connectionAttemptIndex = attemptIndex;\n this.replacementError = null;\n this.handOff(previous.ws);\n } else {\n await next.close();\n }\n }\n this.reconnecting = null;\n } catch (error) {\n const closedError = new TRPCWebSocketClosedError({\n message: 'Initialization error',\n cause: error,\n });\n if (this.allowReconnect && this.activeConnection.isOpen()) {\n // The old socket keeps serving every request, so this is shown only\n // once it closes.\n this.replacementError = closedError;\n } else if (this.allowReconnect) {\n callUserCallback(() =>\n this.connectionState.next({\n type: 'state',\n state: 'connecting',\n error: TRPCClientError.from(closedError),\n }),\n );\n }\n await tryReconnect(attemptIndex + 1);\n }\n };\n\n this.reconnecting = tryReconnect(\n this.connectionRejected ? this.connectionAttemptIndex + 1 : 0,\n );\n }\n\n /**\n * Moves the pending subscriptions to the active socket, which has just\n * opened: each is resubscribed with the id of the last event it delivered.\n * A subscription whose old socket is still open keeps delivering from it\n * until the new socket confirms it with `started`; see `confirmMove()`.\n * Closes the previous socket once nothing is left on it.\n */\n private handOff(previous: WebSocket | null) {\n if (\n this.connectionState.get().state !== 'pending' &&\n this.activeConnection.isOpen()\n ) {\n callUserCallback(() =>\n this.connectionState.next({\n type: 'state',\n state: 'pending',\n error: null,\n }),\n );\n }\n\n const ws = this.activeConnection.ws;\n const subscriptions = this.requestManager\n .getPendingRequests()\n .filter(\n (request) =>\n request.message.method === 'subscription' && request.ws !== ws,\n );\n if (subscriptions.length) {\n this.send(subscriptions.map(({ message }) => message));\n for (const request of subscriptions) {\n // The socket it was moving to hasn't started it, and won't be used.\n this.stopSubscription(request.message.id, request.successor);\n if (request.ws && request.ws.readyState === request.ws.OPEN) {\n request.successor = ws;\n request.overlapEventIds = new Set();\n } else {\n // Its socket was lost, so it starts again.\n this.started.delete(request.message);\n request.ws = ws;\n request.successor = null;\n }\n }\n }\n this.closeIfDrained(previous);\n }\n\n /**\n * Completes a subscription's move once its new socket has confirmed it:\n * stops it on the old socket, whose later messages for it are dropped\n * apart from events without ids, and closes that socket once everything it\n * sent before the stop has arrived and nothing else is left on it.\n */\n private confirmMove(request: {\n message: TRPCClientOutgoingMessage;\n ws: WebSocket | null;\n successor: WebSocket | null;\n movedFrom: Set<WebSocket>;\n }) {\n const previous = request.ws;\n request.ws = request.successor;\n request.successor = null;\n if (previous && this.stopSubscription(request.message.id, previous)) {\n request.movedFrom.add(previous);\n // `stopped` can wait on the procedure, but the \"PONG\" can't.\n sendPing(previous, () => this.releaseMovedFrom(request, previous));\n }\n this.closeIfDrained(previous);\n }\n\n private releaseMovedFrom(\n request: { movedFrom: Set<WebSocket> },\n ws: WebSocket,\n ) {\n if (!request.movedFrom.delete(ws)) return;\n this.requestManager.deleteDrained();\n this.closeIfDrained(ws);\n }\n\n private hasRequestsOn(ws: WebSocket | null) {\n return [\n ...this.requestManager.getPendingRequests(),\n ...this.requestManager.getDrainingRequests(),\n ].some(\n (request) =>\n request.ws === ws || (ws !== null && request.movedFrom.has(ws)),\n );\n }\n\n /**\n * Closes `ws` once it's been replaced and nothing is left on it.\n */\n private closeIfDrained(ws: WebSocket | null | undefined) {\n if (!ws || ws === this.activeConnection.ws) return;\n if (this.hasRequestsOn(ws)) return;\n ws.close();\n }\n\n /**\n * Stops a subscription on the socket it was sent on, if that's still open.\n *\n * @returns Whether the stop was sent.\n */\n private stopSubscription(\n id: TRPCClientOutgoingMessage['id'],\n ws: WebSocket | null,\n ) {\n if (!ws || ws.readyState !== ws.OPEN) return false;\n ws.send(this.encoder.encode({ id, method: 'subscription.stop' }));\n return true;\n }\n\n /**\n * Fails the pending queries and mutations sent on `ws`, and returns its\n * pending subscriptions, which are kept to be sent again.\n */\n private failPendingRequests(ws: WebSocket, cause: unknown) {\n const subscriptions = [];\n for (const request of this.requestManager.getPendingRequests()) {\n const { message, callbacks } = request;\n if (request.ws !== ws) continue;\n if (message.method === 'subscription') {\n subscriptions.push(request);\n continue;\n }\n\n callUserCallback(() =>\n callbacks.error(\n TRPCClientError.from(\n cause ??\n new TRPCWebSocketClosedError({\n message: 'WebSocket closed',\n cause,\n }),\n ),\n ),\n );\n this.requestManager.delete(message);\n }\n return subscriptions;\n }\n\n private setupWebSocketListeners(ws: WebSocket) {\n this.connectionRejected = false;\n\n /**\n * Fails the queries and mutations sent on `ws`. Its subscriptions are\n * sent again on the active socket if that's open, or else by\n * `reconnect()` once the next one opens.\n */\n const handleCloseOrError = (cause: unknown) => {\n if (ws === this.retiredConnection?.ws) this.retiredConnection = null;\n const retired = this.retiredConnection;\n let restored = false;\n if (ws === this.activeConnection.ws && retired?.isOpen()) {\n restored = true;\n // The socket it replaced serves every request again, until the next\n // one opens.\n this.activeConnection = retired;\n this.retiredConnection = null;\n this.replacementError = new TRPCWebSocketClosedError({\n message: 'WebSocket closed',\n cause,\n });\n }\n for (const request of this.requestManager.getPendingRequests()) {\n if (request.successor === ws) {\n // Its old socket still has it.\n request.successor = null;\n request.overlapEventIds = null;\n }\n request.movedFrom.delete(ws);\n }\n for (const request of this.requestManager.getDrainingRequests()) {\n request.movedFrom.delete(ws);\n }\n this.requestManager.deleteDrained();\n const lost = this.failPendingRequests(ws, cause).filter((request) => {\n if (!request.successor) return true;\n // Its new socket has it already.\n request.ws = request.successor;\n request.successor = null;\n return false;\n });\n if (\n ws === this.activeConnection.ws ||\n (this.reconnecting && !this.activeConnection.isOpen())\n ) {\n return;\n }\n\n for (const { message, callbacks } of lost) {\n this.started.delete(message);\n // The active socket may still have its old id, e.g. while stopping it.\n this.requestManager.requeue(message, ++this.lastRequestId);\n this.flushOutgoing(message, callbacks);\n }\n if (restored) this.reconnect(null);\n };\n\n ws.addEventListener('open', () => {\n run(async () => {\n if (this.lazyMode) {\n this.inactivityTimeoutEnabled = true;\n this.startInactivityTimeout();\n }\n\n this.callbacks.onOpen?.();\n\n // A socket opened to replace one that's still open changes nothing\n // the subscribers can see until `handOff()` puts it in use.\n if (\n this.connectionState.get().state === 'pending' ||\n (ws !== this.activeConnection.ws && this.activeConnection.isOpen())\n ) {\n return;\n }\n callUserCallback(() =>\n this.connectionState.next({\n type: 'state',\n state: 'pending',\n error: null,\n }),\n );\n }).catch((error) => {\n ws.close(3000);\n handleCloseOrError(error);\n });\n });\n\n // Set on the socket's `close` event, or earlier when the client gives up\n // on the socket (a keepAlive pong timeout or an undecodable frame) without\n // waiting for the closing handshake. The socket's later frames and events\n // must not reach the requests resent by then.\n let closed = false;\n let answered = false;\n\n /**\n * Runs the socket's close handling once: `cause` fails its queries and\n * mutations, and `event` goes to `onClose`.\n */\n const handleClose = (\n cause: unknown,\n event: CloseEvent | { code: number },\n ) => {\n if (closed) return;\n closed = true;\n handleCloseOrError(cause);\n callUserCallback(() => this.callbacks.onClose?.(event));\n\n if (\n ws === this.activeConnection.ws &&\n (!this.lazyMode || this.requestManager.hasPendingSubscriptions())\n ) {\n this.reconnect(\n new TRPCWebSocketClosedError({\n message: 'WebSocket closed',\n cause,\n }),\n );\n }\n };\n // What `close` reports when the closing handshake doesn't complete.\n const abandonedEvent = { code: 1006 };\n\n const onMessage = ({ data }: MessageEvent) => {\n if (closed) return;\n answered = true;\n this.inactivityTimeout.reset();\n\n // Handle PING/PONG as text regardless of encoder\n if (['PING', 'PONG'].includes(data)) return;\n\n let incomingMessage: TRPCClientIncomingMessage;\n try {\n incomingMessage = decodeIncomingMessage(this.encoder, data);\n } catch (cause) {\n // The frame may have carried a response, and its request would wait\n // forever, so this socket is treated as lost.\n ws.close();\n handleClose(\n new TRPCWebSocketClosedError({\n message: 'Received a message that could not be decoded',\n cause,\n }),\n abandonedEvent,\n );\n // Rethrown asynchronously by `callUserCallback()` below.\n throw cause;\n }\n if ('method' in incomingMessage) {\n if (ws === this.activeConnection.ws) {\n this.handleIncomingRequest(incomingMessage);\n }\n return;\n }\n\n // A replaced socket still answers its own queries and mutations, but\n // says nothing about the connection the client now uses.\n const isRetired =\n ws !== this.activeConnection.ws && ws !== this.nextConnection?.ws;\n if (\n !isRetired &&\n incomingMessage.id === null &&\n 'error' in incomingMessage\n ) {\n this.connectionRejected = true;\n } else if (!isRetired && 'result' in incomingMessage) {\n // The server only sends results once `createContext` has succeeded,\n // so an earlier `id: null` error on this socket was not a rejection,\n // e.g. the server's reply to a malformed frame.\n this.connectionRejected = false;\n }\n\n this.handleResponseMessage(incomingMessage, ws);\n };\n // With `ws`, a throw from a `message` listener stops the socket from\n // reading any further frames while it stays open.\n ws.addEventListener('message', (event) => {\n callUserCallback(() => onMessage(event));\n });\n\n ws.addEventListener('close', (event) => handleClose(event, event));\n this.closeOnPongTimeout.set(ws, () => {\n // A server that never answered, not even a ping, gets the same backoff\n // as one that rejected the connection.\n if (!answered) this.connectionRejected = true;\n handleClose(abandonedEvent, abandonedEvent);\n });\n\n ws.addEventListener('error', (event) => {\n if (closed) return;\n handleCloseOrError(event);\n callUserCallback(() => this.callbacks.onError?.(event));\n\n if (ws !== this.activeConnection.ws) return;\n\n this.reconnect(\n new TRPCWebSocketClosedError({\n message: 'WebSocket closed',\n cause: event,\n }),\n );\n });\n }\n\n private handleResponseMessage(message: TRPCResponseMessage, ws: WebSocket) {\n const request =\n this.requestManager.getPendingRequest(message.id) ??\n this.requestManager.getDrainingRequest(message.id);\n if (!request) return;\n const result = 'result' in message ? message.result : null;\n if (request.successor === ws) {\n this.confirmMove(request);\n } else if (request.ws !== ws) {\n // A subscription moved to a new socket ignores the old one's frames,\n // apart from events without ids until it's stopped there: those can't\n // be replayed, and the new socket may have started listening after\n // the old one sent them.\n if (request.movedFrom.has(ws)) {\n if (result?.type === 'stopped') {\n this.releaseMovedFrom(request, ws);\n } else if (result?.type === 'data' && result.id === undefined) {\n callUserCallback(() => request.callbacks.next(message));\n }\n }\n return;\n }\n // The subscriber doesn't see a move, even one its old socket didn't\n // live to finish.\n if (result?.type === 'started' && this.started.has(request.message)) {\n return;\n }\n\n if (result?.type === 'data' && result.id !== undefined) {\n const eventId = String(result.id);\n if (request.successor) {\n request.overlapEventIds?.add(eventId);\n } else if (request.overlapEventIds?.delete(eventId)) {\n // Already delivered by the old socket.\n return;\n }\n }\n\n callUserCallback(() => request.callbacks.next(message));\n\n let completed = true;\n if ('result' in message && request.message.method === 'subscription') {\n if (message.result.type === 'data') {\n // An event without an id keeps the last tracked id, so a reconnect\n // still resumes after it, as with `httpSubscriptionLink`.\n if (message.result.id !== undefined) {\n request.message.params.lastEventId = message.result.id;\n }\n } else if (message.result.type === 'started') {\n this.started.add(request.message);\n }\n\n if (message.result.type !== 'stopped') {\n completed = false;\n }\n }\n\n if (completed) {\n callUserCallback(() => request.callbacks.complete());\n this.requestManager.delete(request.message);\n }\n }\n\n private handleIncomingRequest(message: TRPCClientIncomingRequest) {\n if (message.method === 'reconnect') {\n this.reconnect(null);\n }\n }\n\n /**\n * Sends a message or batch of messages directly to the server.\n */\n private send(\n messageOrMessages: TRPCClientOutgoingMessage | TRPCClientOutgoingMessage[],\n ) {\n if (!this.activeConnection.isOpen()) {\n throw new Error('Active connection is not open');\n }\n\n const messages =\n messageOrMessages instanceof Array\n ? messageOrMessages\n : [messageOrMessages];\n this.activeConnection.ws.send(\n this.encoder.encode(messages.length === 1 ? messages[0] : messages),\n );\n }\n\n /**\n * Groups requests for batch sending.\n *\n * @returns A function to abort the batched request.\n */\n private batchSend(message: TRPCClientOutgoingMessage, callbacks: TCallbacks) {\n // Started again once the last request or subscription ends.\n this.inactivityTimeout.stop();\n this.flushOutgoing(message, callbacks);\n return this.requestManager.register(message, callbacks);\n }\n\n /**\n * Sends `message` with the other outgoing requests once the connection is\n * open, batching those queued in the same tick.\n */\n private flushOutgoing(\n message: TRPCClientOutgoingMessage,\n callbacks: TCallbacks,\n ) {\n run(async () => {\n // During a reconnect, requests go to the old socket while it's open.\n do {\n if (!this.activeConnection.isOpen()) {\n await this.open();\n }\n await sleep(0);\n } while (!this.activeConnection.isOpen() && this.reconnecting);\n\n if (!this.requestManager.hasOutgoingRequests()) return;\n // Another run already sent this request. Each queued request's own run\n // sends it once the new socket opens.\n if (\n !this.activeConnection.isOpen() &&\n !this.requestManager.isOutgoing(message)\n ) {\n return;\n }\n\n const requests = this.requestManager.flush(this.activeConnection.ws);\n try {\n this.send(requests.map((request) => request.message));\n } catch (err) {\n for (const request of requests) {\n this.requestManager.delete(request.message);\n callUserCallback(() =>\n request.callbacks.error(TRPCClientError.from(err as Error)),\n );\n }\n }\n }).catch((err) => {\n this.requestManager.delete(message);\n callUserCallback(() => callbacks.error(TRPCClientError.from(err)));\n });\n }\n}\n","import type { Encoder } from './wsClient/encoder';\nimport { jsonEncoder } from './wsClient/encoder';\nimport type { WebSocketClientOptions } from './wsClient/options';\nimport { WsClient } from './wsClient/wsClient';\n\n/**\n * Opens and manages a WebSocket connection to a tRPC server. Pass it to\n * `wsLink`.\n *\n * @see https://trpc.io/docs/client/links/wsLink#wslink--createwsclient-options\n */\nexport function createWSClient(opts: WebSocketClientOptions) {\n return new WsClient(opts);\n}\n\n/**\n * The client returned by {@link createWSClient}.\n */\nexport type TRPCWebSocketClient = ReturnType<typeof createWSClient>;\n\nexport { jsonEncoder, type Encoder, type WebSocketClientOptions };\n","import type { Unsubscribable } from '@trpc/server/observable';\nimport { observable } from '@trpc/server/observable';\nimport type {\n AnyRouter,\n inferClientTypes,\n} from '@trpc/server/unstable-core-do-not-import';\nimport type { TransformerOptions } from '../../unstable-internals';\nimport { getTransformer } from '../../unstable-internals';\nimport type { TRPCLink } from '../types';\nimport type {\n Encoder,\n TRPCWebSocketClient,\n WebSocketClientOptions,\n} from './createWsClient';\nimport { createWSClient, jsonEncoder } from './createWsClient';\nimport { callUserCallback } from './wsClient/utils';\n\n/**\n * Options for {@link wsLink}.\n *\n * @see https://trpc.io/docs/client/links/wsLink#wslink--createwsclient-options\n */\nexport type WebSocketLinkOptions<TRouter extends AnyRouter> = {\n /**\n * The client made with `createWSClient`.\n */\n client: TRPCWebSocketClient;\n} & TransformerOptions<inferClientTypes<TRouter>>;\n\n/**\n * A terminating link that sends calls over a WebSocket.\n *\n * @see https://trpc.io/docs/client/links/wsLink\n */\nexport function wsLink<TRouter extends AnyRouter>(\n opts: WebSocketLinkOptions<TRouter>,\n): TRPCLink<TRouter> {\n const { client } = opts;\n const transformer = getTransformer(opts.transformer);\n return () => {\n return ({ op }) => {\n return observable((observer) => {\n let connStateSubscription: Unsubscribable | null = null;\n if (op.type === 'subscription') {\n // The current state is sent during `subscribe`, and a throwing\n // `onConnectionStateChange` must not stop the request below from\n // being sent. The client guards the states it sends later.\n let isSubscribing = true;\n connStateSubscription = client.connectionState.subscribe({\n next(result) {\n const emit = () =>\n observer.next({\n result,\n context: op.context,\n });\n if (isSubscribing) {\n callUserCallback(emit);\n } else {\n emit();\n }\n },\n });\n isSubscribing = false;\n }\n\n const requestSubscription = client\n .request({\n op,\n transformer,\n })\n .subscribe(observer);\n\n return () => {\n requestSubscription.unsubscribe();\n connStateSubscription?.unsubscribe();\n };\n });\n };\n };\n}\n\nexport {\n createWSClient,\n jsonEncoder,\n type Encoder,\n type TRPCWebSocketClient,\n type WebSocketClientOptions,\n};\n"],"mappings":";;;;;;;;;AAOA,MAAa,cAAuB;CAClC,SAAS,SAAS,KAAK,UAAU,IAAI;CACrC,SAAS,SAAS;EAChB,IAAI,OAAO,SAAS,UAClB,MAAM,IAAI,MACR,gGAEF;EAEF,OAAO,KAAK,MAAM,IAAI;CACxB;AACF;;AC0DA,MAAa,eAA4B;CACvC,SAAS;CACT,SAAS;AACX;AASA,MAAa,oBAAsC;CACjD,SAAS;CACT,eAAe;CACf,YAAY;AACd;;;;;;AAOA,MAAa,sBAAsB,iBAAyB;CAC1D,OAAO,iBAAiB,IAAI,IAAI,KAAK,IAAI,MAAO,KAAK,cAAc,GAAK;AAC1E;;;;;;AC/FA,MAAa,YACX,OACA,GAAG,SACG;CACN,OAAO,OAAO,UAAU,aACnB,MAAgC,GAAG,IAAI,IACxC;AACN;;ACFA,IAAa,2BAAb,MAAa,iCAAiC,MAAM;CAClD,YAAY,MAA4C;EACtD,MAAM,KAAK,SAAS,EAClB,OAAO,KAAK,MACd,CAAC;EACD,KAAK,OAAO;EACZ,OAAO,eAAe,MAAM,yBAAyB,SAAS;CAChE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