@trpc/client 11.1.2 → 11.1.3-alpha-tmp-tsdown.23

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.
Files changed (234) hide show
  1. package/dist/TRPCClientError.d.mts +33 -0
  2. package/dist/TRPCClientError.d.mts.map +1 -0
  3. package/dist/TRPCClientError.d.ts +29 -25
  4. package/dist/TRPCClientError.d.ts.map +1 -1
  5. package/dist/TRPCClientError.js +48 -72
  6. package/dist/TRPCClientError.mjs +48 -71
  7. package/dist/TRPCClientError.mjs.map +1 -0
  8. package/dist/_virtual/rolldown_runtime.js +39 -0
  9. package/dist/_virtual/rolldown_runtime.mjs +27 -0
  10. package/dist/createTRPCClient.d.mts +73 -0
  11. package/dist/createTRPCClient.d.mts.map +1 -0
  12. package/dist/createTRPCClient.d.ts +38 -34
  13. package/dist/createTRPCClient.d.ts.map +1 -1
  14. package/dist/createTRPCClient.js +34 -34
  15. package/dist/createTRPCClient.mjs +33 -31
  16. package/dist/createTRPCClient.mjs.map +1 -0
  17. package/dist/createTRPCUntypedClient.d.mts +9 -0
  18. package/dist/createTRPCUntypedClient.d.mts.map +1 -0
  19. package/dist/createTRPCUntypedClient.d.ts +8 -6
  20. package/dist/createTRPCUntypedClient.d.ts.map +1 -1
  21. package/dist/createTRPCUntypedClient.js +5 -6
  22. package/dist/createTRPCUntypedClient.mjs +6 -3
  23. package/dist/createTRPCUntypedClient.mjs.map +1 -0
  24. package/dist/getFetch.d.mts +10 -0
  25. package/dist/getFetch.d.mts.map +1 -0
  26. package/dist/getFetch.d.ts +9 -2
  27. package/dist/getFetch.d.ts.map +1 -1
  28. package/dist/getFetch.js +8 -13
  29. package/dist/getFetch.mjs +8 -11
  30. package/dist/getFetch.mjs.map +1 -0
  31. package/dist/index.d.mts +21 -0
  32. package/dist/index.d.ts +21 -16
  33. package/dist/index.js +41 -42
  34. package/dist/index.mjs +18 -15
  35. package/dist/internals/TRPCUntypedClient.d.mts +46 -0
  36. package/dist/internals/TRPCUntypedClient.d.mts.map +1 -0
  37. package/dist/internals/TRPCUntypedClient.d.ts +40 -33
  38. package/dist/internals/TRPCUntypedClient.d.ts.map +1 -1
  39. package/dist/internals/TRPCUntypedClient.js +94 -113
  40. package/dist/internals/TRPCUntypedClient.mjs +93 -110
  41. package/dist/internals/TRPCUntypedClient.mjs.map +1 -0
  42. package/dist/internals/dataLoader.js +101 -116
  43. package/dist/internals/dataLoader.mjs +101 -114
  44. package/dist/internals/dataLoader.mjs.map +1 -0
  45. package/dist/internals/inputWithTrackedEventId.js +9 -12
  46. package/dist/internals/inputWithTrackedEventId.mjs +9 -10
  47. package/dist/internals/inputWithTrackedEventId.mjs.map +1 -0
  48. package/dist/internals/signals.js +39 -40
  49. package/dist/internals/signals.mjs +39 -39
  50. package/dist/internals/signals.mjs.map +1 -0
  51. package/dist/internals/transformer.d.mts +47 -0
  52. package/dist/internals/transformer.d.mts.map +1 -0
  53. package/dist/internals/transformer.d.ts +28 -23
  54. package/dist/internals/transformer.d.ts.map +1 -1
  55. package/dist/internals/transformer.js +25 -26
  56. package/dist/internals/transformer.mjs +25 -24
  57. package/dist/internals/transformer.mjs.map +1 -0
  58. package/dist/internals/types.d.mts +85 -0
  59. package/dist/internals/types.d.mts.map +1 -0
  60. package/dist/internals/types.d.ts +45 -42
  61. package/dist/internals/types.d.ts.map +1 -1
  62. package/dist/links/HTTPBatchLinkOptions.d.mts +25 -0
  63. package/dist/links/HTTPBatchLinkOptions.d.mts.map +1 -0
  64. package/dist/links/HTTPBatchLinkOptions.d.ts +23 -18
  65. package/dist/links/HTTPBatchLinkOptions.d.ts.map +1 -1
  66. package/dist/links/httpBatchLink.d.mts +18 -0
  67. package/dist/links/httpBatchLink.d.mts.map +1 -0
  68. package/dist/links/httpBatchLink.d.ts +14 -4
  69. package/dist/links/httpBatchLink.d.ts.map +1 -1
  70. package/dist/links/httpBatchLink.js +90 -106
  71. package/dist/links/httpBatchLink.mjs +89 -103
  72. package/dist/links/httpBatchLink.mjs.map +1 -0
  73. package/dist/links/httpBatchStreamLink.d.mts +22 -0
  74. package/dist/links/httpBatchStreamLink.d.mts.map +1 -0
  75. package/dist/links/httpBatchStreamLink.d.ts +15 -5
  76. package/dist/links/httpBatchStreamLink.d.ts.map +1 -1
  77. package/dist/links/httpBatchStreamLink.js +120 -144
  78. package/dist/links/httpBatchStreamLink.mjs +119 -141
  79. package/dist/links/httpBatchStreamLink.mjs.map +1 -0
  80. package/dist/links/httpLink.d.mts +24 -0
  81. package/dist/links/httpLink.d.mts.map +1 -0
  82. package/dist/links/httpLink.d.ts +19 -12
  83. package/dist/links/httpLink.d.ts.map +1 -1
  84. package/dist/links/httpLink.js +74 -90
  85. package/dist/links/httpLink.mjs +73 -87
  86. package/dist/links/httpLink.mjs.map +1 -0
  87. package/dist/links/httpSubscriptionLink.d.mts +30 -0
  88. package/dist/links/httpSubscriptionLink.d.mts.map +1 -0
  89. package/dist/links/httpSubscriptionLink.d.ts +21 -17
  90. package/dist/links/httpSubscriptionLink.d.ts.map +1 -1
  91. package/dist/links/httpSubscriptionLink.js +134 -179
  92. package/dist/links/httpSubscriptionLink.mjs +133 -176
  93. package/dist/links/httpSubscriptionLink.mjs.map +1 -0
  94. package/dist/links/internals/contentTypes.d.mts +10 -0
  95. package/dist/links/internals/contentTypes.d.mts.map +1 -0
  96. package/dist/links/internals/contentTypes.d.ts +9 -3
  97. package/dist/links/internals/contentTypes.d.ts.map +1 -1
  98. package/dist/links/internals/contentTypes.js +6 -6
  99. package/dist/links/internals/contentTypes.mjs +6 -4
  100. package/dist/links/internals/contentTypes.mjs.map +1 -0
  101. package/dist/links/internals/createChain.js +23 -23
  102. package/dist/links/internals/createChain.mjs +22 -20
  103. package/dist/links/internals/createChain.mjs.map +1 -0
  104. package/dist/links/internals/httpUtils.d.mts +26 -0
  105. package/dist/links/internals/httpUtils.d.mts.map +1 -0
  106. package/dist/links/internals/httpUtils.d.ts +22 -64
  107. package/dist/links/internals/httpUtils.d.ts.map +1 -1
  108. package/dist/links/internals/httpUtils.js +93 -116
  109. package/dist/links/internals/httpUtils.mjs +94 -113
  110. package/dist/links/internals/httpUtils.mjs.map +1 -0
  111. package/dist/links/internals/subscriptions.d.mts +20 -0
  112. package/dist/links/internals/subscriptions.d.mts.map +1 -0
  113. package/dist/links/internals/subscriptions.d.ts +11 -8
  114. package/dist/links/internals/subscriptions.d.ts.map +1 -1
  115. package/dist/links/internals/urlWithConnectionParams.d.mts +27 -0
  116. package/dist/links/internals/urlWithConnectionParams.d.mts.map +1 -0
  117. package/dist/links/internals/urlWithConnectionParams.d.ts +22 -18
  118. package/dist/links/internals/urlWithConnectionParams.d.ts.map +1 -1
  119. package/dist/links/internals/urlWithConnectionParams.js +8 -6
  120. package/dist/links/internals/urlWithConnectionParams.mjs +8 -4
  121. package/dist/links/internals/urlWithConnectionParams.mjs.map +1 -0
  122. package/dist/links/localLink.d.mts +22 -0
  123. package/dist/links/localLink.d.mts.map +1 -0
  124. package/dist/links/localLink.d.ts +22 -0
  125. package/dist/links/localLink.d.ts.map +1 -0
  126. package/dist/links/localLink.js +192 -0
  127. package/dist/links/localLink.mjs +193 -0
  128. package/dist/links/localLink.mjs.map +1 -0
  129. package/dist/links/loggerLink.d.mts +56 -0
  130. package/dist/links/loggerLink.d.mts.map +1 -0
  131. package/dist/links/loggerLink.d.ts +40 -36
  132. package/dist/links/loggerLink.d.ts.map +1 -1
  133. package/dist/links/loggerLink.js +113 -166
  134. package/dist/links/loggerLink.mjs +112 -163
  135. package/dist/links/loggerLink.mjs.map +1 -0
  136. package/dist/links/retryLink.d.mts +35 -0
  137. package/dist/links/retryLink.d.mts.map +1 -0
  138. package/dist/links/retryLink.d.ts +27 -25
  139. package/dist/links/retryLink.d.ts.map +1 -1
  140. package/dist/links/retryLink.js +60 -68
  141. package/dist/links/retryLink.mjs +59 -65
  142. package/dist/links/retryLink.mjs.map +1 -0
  143. package/dist/links/splitLink.d.mts +21 -0
  144. package/dist/links/splitLink.d.mts.map +1 -0
  145. package/dist/links/splitLink.d.ts +19 -12
  146. package/dist/links/splitLink.d.ts.map +1 -1
  147. package/dist/links/splitLink.js +20 -21
  148. package/dist/links/splitLink.mjs +19 -18
  149. package/dist/links/splitLink.mjs.map +1 -0
  150. package/dist/links/types.d.mts +71 -0
  151. package/dist/links/types.d.mts.map +1 -0
  152. package/dist/links/types.d.ts +37 -29
  153. package/dist/links/types.d.ts.map +1 -1
  154. package/dist/links/wsLink/createWsClient.d.mts +10 -0
  155. package/dist/links/wsLink/createWsClient.d.mts.map +1 -0
  156. package/dist/links/wsLink/createWsClient.d.ts +9 -5
  157. package/dist/links/wsLink/createWsClient.d.ts.map +1 -1
  158. package/dist/links/wsLink/createWsClient.js +5 -5
  159. package/dist/links/wsLink/createWsClient.mjs +5 -2
  160. package/dist/links/wsLink/createWsClient.mjs.map +1 -0
  161. package/dist/links/wsLink/wsClient/options.d.mts +69 -0
  162. package/dist/links/wsLink/wsClient/options.d.mts.map +1 -0
  163. package/dist/links/wsLink/wsClient/options.d.ts +54 -64
  164. package/dist/links/wsLink/wsClient/options.d.ts.map +1 -1
  165. package/dist/links/wsLink/wsClient/options.js +14 -12
  166. package/dist/links/wsLink/wsClient/options.mjs +14 -10
  167. package/dist/links/wsLink/wsClient/options.mjs.map +1 -0
  168. package/dist/links/wsLink/wsClient/requestManager.js +133 -135
  169. package/dist/links/wsLink/wsClient/requestManager.mjs +133 -132
  170. package/dist/links/wsLink/wsClient/requestManager.mjs.map +1 -0
  171. package/dist/links/wsLink/wsClient/utils.js +64 -81
  172. package/dist/links/wsLink/wsClient/utils.mjs +64 -78
  173. package/dist/links/wsLink/wsClient/utils.mjs.map +1 -0
  174. package/dist/links/wsLink/wsClient/wsClient.d.mts +104 -0
  175. package/dist/links/wsLink/wsClient/wsClient.d.mts.map +1 -0
  176. package/dist/links/wsLink/wsClient/wsClient.d.ts +99 -79
  177. package/dist/links/wsLink/wsClient/wsClient.d.ts.map +1 -1
  178. package/dist/links/wsLink/wsClient/wsClient.js +273 -322
  179. package/dist/links/wsLink/wsClient/wsClient.mjs +272 -319
  180. package/dist/links/wsLink/wsClient/wsClient.mjs.map +1 -0
  181. package/dist/links/wsLink/wsClient/wsConnection.js +155 -178
  182. package/dist/links/wsLink/wsClient/wsConnection.mjs +154 -175
  183. package/dist/links/wsLink/wsClient/wsConnection.mjs.map +1 -0
  184. package/dist/links/wsLink/wsLink.d.mts +15 -0
  185. package/dist/links/wsLink/wsLink.d.mts.map +1 -0
  186. package/dist/links/wsLink/wsLink.d.ts +13 -9
  187. package/dist/links/wsLink/wsLink.d.ts.map +1 -1
  188. package/dist/links/wsLink/wsLink.js +30 -31
  189. package/dist/links/wsLink/wsLink.mjs +29 -28
  190. package/dist/links/wsLink/wsLink.mjs.map +1 -0
  191. package/dist/links.js +11 -0
  192. package/dist/links.mjs +11 -0
  193. package/dist/node_modules/.pnpm/@oxc-project_runtime@0.71.0/node_modules/@oxc-project/runtime/src/helpers/usingCtx.js +68 -0
  194. package/dist/node_modules/.pnpm/@oxc-project_runtime@0.71.0/node_modules/@oxc-project/runtime/src/helpers/usingCtx.mjs +66 -0
  195. package/dist/node_modules/.pnpm/@oxc-project_runtime@0.71.0/node_modules/@oxc-project/runtime/src/helpers/usingCtx.mjs.map +1 -0
  196. package/dist/unstable-internals.d.mts +3 -0
  197. package/dist/unstable-internals.d.ts +3 -3
  198. package/dist/unstable-internals.js +2 -6
  199. package/dist/unstable-internals.mjs +3 -1
  200. package/links/httpBatchLink/package.json +1 -0
  201. package/links/httpLink/package.json +1 -0
  202. package/links/loggerLink/package.json +1 -0
  203. package/links/splitLink/package.json +1 -0
  204. package/links/wsLink/wsLink/package.json +1 -0
  205. package/package.json +64 -29
  206. package/src/TRPCClientError.ts +4 -9
  207. package/src/internals/dataLoader.ts +2 -6
  208. package/src/internals/signals.ts +16 -0
  209. package/src/links/httpSubscriptionLink.ts +3 -18
  210. package/src/links/localLink.ts +277 -0
  211. package/src/links/wsLink/wsClient/requestManager.ts +9 -0
  212. package/src/links/wsLink/wsClient/wsClient.ts +1 -1
  213. package/src/links.ts +1 -0
  214. package/unstable-internals/package.json +1 -0
  215. package/dist/index.d.ts.map +0 -1
  216. package/dist/internals/dataLoader.d.ts +0 -13
  217. package/dist/internals/dataLoader.d.ts.map +0 -1
  218. package/dist/internals/inputWithTrackedEventId.d.ts +0 -2
  219. package/dist/internals/inputWithTrackedEventId.d.ts.map +0 -1
  220. package/dist/internals/signals.d.ts +0 -15
  221. package/dist/internals/signals.d.ts.map +0 -1
  222. package/dist/links/internals/createChain.d.ts +0 -8
  223. package/dist/links/internals/createChain.d.ts.map +0 -1
  224. package/dist/links/internals/dedupeLink.d.ts +0 -7
  225. package/dist/links/internals/dedupeLink.d.ts.map +0 -1
  226. package/dist/links/wsLink/wsClient/requestManager.d.ts +0 -102
  227. package/dist/links/wsLink/wsClient/requestManager.d.ts.map +0 -1
  228. package/dist/links/wsLink/wsClient/utils.d.ts +0 -38
  229. package/dist/links/wsLink/wsClient/utils.d.ts.map +0 -1
  230. package/dist/links/wsLink/wsClient/wsConnection.d.ts +0 -79
  231. package/dist/links/wsLink/wsClient/wsConnection.d.ts.map +0 -1
  232. package/dist/links.d.ts +0 -11
  233. package/dist/links.d.ts.map +0 -1
  234. package/dist/unstable-internals.d.ts.map +0 -1
@@ -1,325 +1,276 @@
1
- 'use strict';
1
+ const require_rolldown_runtime = require('../../../_virtual/rolldown_runtime.js');
2
+ const require__Users_julius_dev_trpc_packages_client_src_TRPCClientError = require('../../../TRPCClientError.js');
3
+ const require__Users_julius_dev_trpc_packages_client_src_links_wsLink_wsClient_options = require('./options.js');
4
+ const require__Users_julius_dev_trpc_packages_client_src_links_wsLink_wsClient_utils = require('./utils.js');
5
+ const require__Users_julius_dev_trpc_packages_client_src_links_wsLink_wsClient_requestManager = require('./requestManager.js');
6
+ const require__Users_julius_dev_trpc_packages_client_src_links_wsLink_wsClient_wsConnection = require('./wsConnection.js');
7
+ const __trpc_server_observable = require_rolldown_runtime.__toESM(require("@trpc/server/observable"));
8
+ const __trpc_server_unstable_core_do_not_import = require_rolldown_runtime.__toESM(require("@trpc/server/unstable-core-do-not-import"));
2
9
 
3
- var observable = require('@trpc/server/observable');
4
- var unstableCoreDoNotImport = require('@trpc/server/unstable-core-do-not-import');
5
- var TRPCClientError = require('../../../TRPCClientError.js');
6
- var options = require('./options.js');
7
- var requestManager = require('./requestManager.js');
8
- var utils = require('./utils.js');
9
- var wsConnection = require('./wsConnection.js');
10
-
11
- function _define_property(obj, key, value) {
12
- if (key in obj) {
13
- Object.defineProperty(obj, key, {
14
- value: value,
15
- enumerable: true,
16
- configurable: true,
17
- writable: true
18
- });
19
- } else {
20
- obj[key] = value;
21
- }
22
- return obj;
23
- }
10
+ //#region src/links/wsLink/wsClient/wsClient.ts
24
11
  /**
25
- * A WebSocket client for managing TRPC operations, supporting lazy initialization,
26
- * reconnection, keep-alive, and request management.
27
- */ class WsClient {
28
- /**
29
- * Opens the WebSocket connection. Handles reconnection attempts and updates
30
- * the connection state accordingly.
31
- */ async open() {
32
- this.allowReconnect = true;
33
- if (this.connectionState.get().state !== 'connecting') {
34
- this.connectionState.next({
35
- type: 'state',
36
- state: 'connecting',
37
- error: null
38
- });
39
- }
40
- try {
41
- await this.activeConnection.open();
42
- } catch (error) {
43
- this.reconnect(new utils.TRPCWebSocketClosedError({
44
- message: 'Initialization error',
45
- cause: error
46
- }));
47
- return this.reconnecting;
48
- }
49
- }
50
- /**
51
- * Closes the WebSocket connection and stops managing requests.
52
- * Ensures all outgoing and pending requests are properly finalized.
53
- */ async close() {
54
- this.allowReconnect = false;
55
- this.inactivityTimeout.stop();
56
- const requestsToAwait = [];
57
- for (const request of this.requestManager.getRequests()){
58
- if (request.message.method === 'subscription') {
59
- request.callbacks.complete();
60
- } else if (request.state === 'outgoing') {
61
- request.callbacks.error(TRPCClientError.TRPCClientError.from(new utils.TRPCWebSocketClosedError({
62
- message: 'Closed before connection was established'
63
- })));
64
- } else {
65
- requestsToAwait.push(request.end);
66
- }
67
- }
68
- await Promise.all(requestsToAwait).catch(()=>null);
69
- await this.activeConnection.close().catch(()=>null);
70
- this.connectionState.next({
71
- type: 'state',
72
- state: 'idle',
73
- error: null
74
- });
75
- }
76
- /**
77
- * Method to request the server.
78
- * Handles data transformation, batching of requests, and subscription lifecycle.
79
- *
80
- * @param op - The operation details including id, type, path, input and signal
81
- * @param transformer - Data transformer for serializing requests and deserializing responses
82
- * @param lastEventId - Optional ID of the last received event for subscriptions
83
- *
84
- * @returns An observable that emits operation results and handles cleanup
85
- */ request({ op: { id, type, path, input, signal }, transformer, lastEventId }) {
86
- return observable.observable((observer)=>{
87
- const abort = this.batchSend({
88
- id,
89
- method: type,
90
- params: {
91
- input: transformer.input.serialize(input),
92
- path,
93
- lastEventId
94
- }
95
- }, {
96
- ...observer,
97
- next (event) {
98
- const transformed = unstableCoreDoNotImport.transformResult(event, transformer.output);
99
- if (!transformed.ok) {
100
- observer.error(TRPCClientError.TRPCClientError.from(transformed.error));
101
- return;
102
- }
103
- observer.next({
104
- result: transformed.result
105
- });
106
- }
107
- });
108
- return ()=>{
109
- abort();
110
- if (type === 'subscription' && this.activeConnection.isOpen()) {
111
- this.send({
112
- id,
113
- method: 'subscription.stop'
114
- });
115
- }
116
- signal?.removeEventListener('abort', abort);
117
- };
118
- });
119
- }
120
- get connection() {
121
- return wsConnection.backwardCompatibility(this.activeConnection);
122
- }
123
- reconnect(closedError) {
124
- this.connectionState.next({
125
- type: 'state',
126
- state: 'connecting',
127
- error: TRPCClientError.TRPCClientError.from(closedError)
128
- });
129
- if (this.reconnecting) return;
130
- const tryReconnect = async (attemptIndex)=>{
131
- try {
132
- await unstableCoreDoNotImport.sleep(this.reconnectRetryDelay(attemptIndex));
133
- if (this.allowReconnect) {
134
- await this.activeConnection.close();
135
- await this.activeConnection.open();
136
- if (this.requestManager.hasPendingRequests()) {
137
- this.send(this.requestManager.getPendingRequests().map(({ message })=>message));
138
- }
139
- }
140
- this.reconnecting = null;
141
- } catch {
142
- await tryReconnect(attemptIndex + 1);
143
- }
144
- };
145
- this.reconnecting = tryReconnect(0);
146
- }
147
- setupWebSocketListeners(ws) {
148
- const handleCloseOrError = (cause)=>{
149
- const reqs = this.requestManager.getPendingRequests();
150
- for (const { message, callbacks } of reqs){
151
- if (message.method === 'subscription') continue;
152
- callbacks.error(TRPCClientError.TRPCClientError.from(cause ?? new utils.TRPCWebSocketClosedError({
153
- message: 'WebSocket closed',
154
- cause
155
- })));
156
- this.requestManager.delete(message.id);
157
- }
158
- };
159
- ws.addEventListener('open', ()=>{
160
- unstableCoreDoNotImport.run(async ()=>{
161
- if (this.lazyMode) {
162
- this.inactivityTimeout.start();
163
- }
164
- this.callbacks.onOpen?.();
165
- this.connectionState.next({
166
- type: 'state',
167
- state: 'pending',
168
- error: null
169
- });
170
- }).catch((error)=>{
171
- ws.close(3000);
172
- handleCloseOrError(error);
173
- });
174
- });
175
- ws.addEventListener('message', ({ data })=>{
176
- this.inactivityTimeout.reset();
177
- if (typeof data !== 'string' || [
178
- 'PING',
179
- 'PONG'
180
- ].includes(data)) return;
181
- const incomingMessage = JSON.parse(data);
182
- if ('method' in incomingMessage) {
183
- this.handleIncomingRequest(incomingMessage);
184
- return;
185
- }
186
- this.handleResponseMessage(incomingMessage);
187
- });
188
- ws.addEventListener('close', (event)=>{
189
- handleCloseOrError(event);
190
- this.callbacks.onClose?.(event);
191
- if (!this.lazyMode) {
192
- this.reconnect(new utils.TRPCWebSocketClosedError({
193
- message: 'WebSocket closed',
194
- cause: event
195
- }));
196
- }
197
- });
198
- ws.addEventListener('error', (event)=>{
199
- handleCloseOrError(event);
200
- this.callbacks.onError?.(event);
201
- this.reconnect(new utils.TRPCWebSocketClosedError({
202
- message: 'WebSocket closed',
203
- cause: event
204
- }));
205
- });
206
- }
207
- handleResponseMessage(message) {
208
- const request = this.requestManager.getPendingRequest(message.id);
209
- if (!request) return;
210
- request.callbacks.next(message);
211
- let completed = true;
212
- if ('result' in message && request.message.method === 'subscription') {
213
- if (message.result.type === 'data') {
214
- request.message.params.lastEventId = message.result.id;
215
- }
216
- if (message.result.type !== 'stopped') {
217
- completed = false;
218
- }
219
- }
220
- if (completed) {
221
- request.callbacks.complete();
222
- this.requestManager.delete(message.id);
223
- }
224
- }
225
- handleIncomingRequest(message) {
226
- if (message.method === 'reconnect') {
227
- this.reconnect(new utils.TRPCWebSocketClosedError({
228
- message: 'Server requested reconnect'
229
- }));
230
- }
231
- }
232
- /**
233
- * Sends a message or batch of messages directly to the server.
234
- */ send(messageOrMessages) {
235
- if (!this.activeConnection.isOpen()) {
236
- throw new Error('Active connection is not open');
237
- }
238
- const messages = messageOrMessages instanceof Array ? messageOrMessages : [
239
- messageOrMessages
240
- ];
241
- this.activeConnection.ws.send(JSON.stringify(messages.length === 1 ? messages[0] : messages));
242
- }
243
- /**
244
- * Groups requests for batch sending.
245
- *
246
- * @returns A function to abort the batched request.
247
- */ batchSend(message, callbacks) {
248
- this.inactivityTimeout.reset();
249
- unstableCoreDoNotImport.run(async ()=>{
250
- if (!this.activeConnection.isOpen()) {
251
- await this.open();
252
- }
253
- await unstableCoreDoNotImport.sleep(0);
254
- if (!this.requestManager.hasOutgoingRequests()) return;
255
- this.send(this.requestManager.flush().map(({ message })=>message));
256
- }).catch((err)=>{
257
- this.requestManager.delete(message.id);
258
- callbacks.error(TRPCClientError.TRPCClientError.from(err));
259
- });
260
- return this.requestManager.register(message, callbacks);
261
- }
262
- constructor(opts){
263
- /**
264
- * Observable tracking the current connection state, including errors.
265
- */ _define_property(this, "connectionState", void 0);
266
- _define_property(this, "allowReconnect", false);
267
- _define_property(this, "requestManager", new requestManager.RequestManager());
268
- _define_property(this, "activeConnection", void 0);
269
- _define_property(this, "reconnectRetryDelay", void 0);
270
- _define_property(this, "inactivityTimeout", void 0);
271
- _define_property(this, "callbacks", void 0);
272
- _define_property(this, "lazyMode", void 0);
273
- /**
274
- * Manages the reconnection process for the WebSocket using retry logic.
275
- * Ensures that only one reconnection attempt is active at a time by tracking the current
276
- * reconnection state in the `reconnecting` promise.
277
- */ _define_property(this, "reconnecting", null);
278
- // Initialize callbacks, connection parameters, and options.
279
- this.callbacks = {
280
- onOpen: opts.onOpen,
281
- onClose: opts.onClose,
282
- onError: opts.onError
283
- };
284
- const lazyOptions = {
285
- ...options.lazyDefaults,
286
- ...opts.lazy
287
- };
288
- // Set up inactivity timeout for lazy connections.
289
- this.inactivityTimeout = new utils.ResettableTimeout(()=>{
290
- if (this.requestManager.hasOutgoingRequests() || this.requestManager.hasPendingRequests()) {
291
- this.inactivityTimeout.reset();
292
- return;
293
- }
294
- this.close().catch(()=>null);
295
- }, lazyOptions.closeMs);
296
- // Initialize the WebSocket connection.
297
- this.activeConnection = new wsConnection.WsConnection({
298
- WebSocketPonyfill: opts.WebSocket,
299
- urlOptions: opts,
300
- keepAlive: {
301
- ...options.keepAliveDefaults,
302
- ...opts.keepAlive
303
- }
304
- });
305
- this.activeConnection.wsObservable.subscribe({
306
- next: (ws)=>{
307
- if (!ws) return;
308
- this.setupWebSocketListeners(ws);
309
- }
310
- });
311
- this.reconnectRetryDelay = opts.retryDelayMs ?? options.exponentialBackoff;
312
- this.lazyMode = lazyOptions.enabled;
313
- this.connectionState = observable.behaviorSubject({
314
- type: 'state',
315
- state: lazyOptions.enabled ? 'idle' : 'connecting',
316
- error: null
317
- });
318
- // Automatically open the connection if lazy mode is disabled.
319
- if (!this.lazyMode) {
320
- this.open().catch(()=>null);
321
- }
322
- }
323
- }
12
+ * A WebSocket client for managing TRPC operations, supporting lazy initialization,
13
+ * reconnection, keep-alive, and request management.
14
+ */
15
+ var WsClient = class {
16
+ /**
17
+ * Observable tracking the current connection state, including errors.
18
+ */
19
+ connectionState;
20
+ allowReconnect = false;
21
+ requestManager = new require__Users_julius_dev_trpc_packages_client_src_links_wsLink_wsClient_requestManager.RequestManager();
22
+ activeConnection;
23
+ reconnectRetryDelay;
24
+ inactivityTimeout;
25
+ callbacks;
26
+ lazyMode;
27
+ constructor(opts) {
28
+ this.callbacks = {
29
+ onOpen: opts.onOpen,
30
+ onClose: opts.onClose,
31
+ onError: opts.onError
32
+ };
33
+ const lazyOptions = {
34
+ ...require__Users_julius_dev_trpc_packages_client_src_links_wsLink_wsClient_options.lazyDefaults,
35
+ ...opts.lazy
36
+ };
37
+ this.inactivityTimeout = new require__Users_julius_dev_trpc_packages_client_src_links_wsLink_wsClient_utils.ResettableTimeout(() => {
38
+ if (this.requestManager.hasOutgoingRequests() || this.requestManager.hasPendingRequests()) {
39
+ this.inactivityTimeout.reset();
40
+ return;
41
+ }
42
+ this.close().catch(() => null);
43
+ }, lazyOptions.closeMs);
44
+ this.activeConnection = new require__Users_julius_dev_trpc_packages_client_src_links_wsLink_wsClient_wsConnection.WsConnection({
45
+ WebSocketPonyfill: opts.WebSocket,
46
+ urlOptions: opts,
47
+ keepAlive: {
48
+ ...require__Users_julius_dev_trpc_packages_client_src_links_wsLink_wsClient_options.keepAliveDefaults,
49
+ ...opts.keepAlive
50
+ }
51
+ });
52
+ this.activeConnection.wsObservable.subscribe({ next: (ws) => {
53
+ if (!ws) return;
54
+ this.setupWebSocketListeners(ws);
55
+ } });
56
+ this.reconnectRetryDelay = opts.retryDelayMs ?? require__Users_julius_dev_trpc_packages_client_src_links_wsLink_wsClient_options.exponentialBackoff;
57
+ this.lazyMode = lazyOptions.enabled;
58
+ this.connectionState = (0, __trpc_server_observable.behaviorSubject)({
59
+ type: "state",
60
+ state: lazyOptions.enabled ? "idle" : "connecting",
61
+ error: null
62
+ });
63
+ if (!this.lazyMode) this.open().catch(() => null);
64
+ }
65
+ /**
66
+ * Opens the WebSocket connection. Handles reconnection attempts and updates
67
+ * the connection state accordingly.
68
+ */
69
+ async open() {
70
+ this.allowReconnect = true;
71
+ if (this.connectionState.get().state !== "connecting") this.connectionState.next({
72
+ type: "state",
73
+ state: "connecting",
74
+ error: null
75
+ });
76
+ try {
77
+ await this.activeConnection.open();
78
+ } catch (error) {
79
+ this.reconnect(new require__Users_julius_dev_trpc_packages_client_src_links_wsLink_wsClient_utils.TRPCWebSocketClosedError({
80
+ message: "Initialization error",
81
+ cause: error
82
+ }));
83
+ return this.reconnecting;
84
+ }
85
+ }
86
+ /**
87
+ * Closes the WebSocket connection and stops managing requests.
88
+ * Ensures all outgoing and pending requests are properly finalized.
89
+ */
90
+ async close() {
91
+ this.allowReconnect = false;
92
+ this.inactivityTimeout.stop();
93
+ const requestsToAwait = [];
94
+ for (const request of this.requestManager.getRequests()) if (request.message.method === "subscription") request.callbacks.complete();
95
+ else if (request.state === "outgoing") request.callbacks.error(require__Users_julius_dev_trpc_packages_client_src_TRPCClientError.TRPCClientError.from(new require__Users_julius_dev_trpc_packages_client_src_links_wsLink_wsClient_utils.TRPCWebSocketClosedError({ message: "Closed before connection was established" })));
96
+ else requestsToAwait.push(request.end);
97
+ await Promise.all(requestsToAwait).catch(() => null);
98
+ await this.activeConnection.close().catch(() => null);
99
+ this.connectionState.next({
100
+ type: "state",
101
+ state: "idle",
102
+ error: null
103
+ });
104
+ }
105
+ /**
106
+ * Method to request the server.
107
+ * Handles data transformation, batching of requests, and subscription lifecycle.
108
+ *
109
+ * @param op - The operation details including id, type, path, input and signal
110
+ * @param transformer - Data transformer for serializing requests and deserializing responses
111
+ * @param lastEventId - Optional ID of the last received event for subscriptions
112
+ *
113
+ * @returns An observable that emits operation results and handles cleanup
114
+ */
115
+ request({ op: { id, type, path, input, signal }, transformer, lastEventId }) {
116
+ return (0, __trpc_server_observable.observable)((observer) => {
117
+ const abort = this.batchSend({
118
+ id,
119
+ method: type,
120
+ params: {
121
+ input: transformer.input.serialize(input),
122
+ path,
123
+ lastEventId
124
+ }
125
+ }, {
126
+ ...observer,
127
+ next(event) {
128
+ const transformed = (0, __trpc_server_unstable_core_do_not_import.transformResult)(event, transformer.output);
129
+ if (!transformed.ok) {
130
+ observer.error(require__Users_julius_dev_trpc_packages_client_src_TRPCClientError.TRPCClientError.from(transformed.error));
131
+ return;
132
+ }
133
+ observer.next({ result: transformed.result });
134
+ }
135
+ });
136
+ return () => {
137
+ abort();
138
+ if (type === "subscription" && this.activeConnection.isOpen()) this.send({
139
+ id,
140
+ method: "subscription.stop"
141
+ });
142
+ signal?.removeEventListener("abort", abort);
143
+ };
144
+ });
145
+ }
146
+ get connection() {
147
+ return require__Users_julius_dev_trpc_packages_client_src_links_wsLink_wsClient_wsConnection.backwardCompatibility(this.activeConnection);
148
+ }
149
+ /**
150
+ * Manages the reconnection process for the WebSocket using retry logic.
151
+ * Ensures that only one reconnection attempt is active at a time by tracking the current
152
+ * reconnection state in the `reconnecting` promise.
153
+ */
154
+ reconnecting = null;
155
+ reconnect(closedError) {
156
+ this.connectionState.next({
157
+ type: "state",
158
+ state: "connecting",
159
+ error: require__Users_julius_dev_trpc_packages_client_src_TRPCClientError.TRPCClientError.from(closedError)
160
+ });
161
+ if (this.reconnecting) return;
162
+ const tryReconnect = async (attemptIndex) => {
163
+ try {
164
+ await (0, __trpc_server_unstable_core_do_not_import.sleep)(this.reconnectRetryDelay(attemptIndex));
165
+ if (this.allowReconnect) {
166
+ await this.activeConnection.close();
167
+ await this.activeConnection.open();
168
+ if (this.requestManager.hasPendingRequests()) this.send(this.requestManager.getPendingRequests().map(({ message }) => message));
169
+ }
170
+ this.reconnecting = null;
171
+ } catch {
172
+ await tryReconnect(attemptIndex + 1);
173
+ }
174
+ };
175
+ this.reconnecting = tryReconnect(0);
176
+ }
177
+ setupWebSocketListeners(ws) {
178
+ const handleCloseOrError = (cause) => {
179
+ const reqs = this.requestManager.getPendingRequests();
180
+ for (const { message, callbacks } of reqs) {
181
+ if (message.method === "subscription") continue;
182
+ callbacks.error(require__Users_julius_dev_trpc_packages_client_src_TRPCClientError.TRPCClientError.from(cause ?? new require__Users_julius_dev_trpc_packages_client_src_links_wsLink_wsClient_utils.TRPCWebSocketClosedError({
183
+ message: "WebSocket closed",
184
+ cause
185
+ })));
186
+ this.requestManager.delete(message.id);
187
+ }
188
+ };
189
+ ws.addEventListener("open", () => {
190
+ (0, __trpc_server_unstable_core_do_not_import.run)(async () => {
191
+ if (this.lazyMode) this.inactivityTimeout.start();
192
+ this.callbacks.onOpen?.();
193
+ this.connectionState.next({
194
+ type: "state",
195
+ state: "pending",
196
+ error: null
197
+ });
198
+ }).catch((error) => {
199
+ ws.close(3e3);
200
+ handleCloseOrError(error);
201
+ });
202
+ });
203
+ ws.addEventListener("message", ({ data }) => {
204
+ this.inactivityTimeout.reset();
205
+ if (typeof data !== "string" || ["PING", "PONG"].includes(data)) return;
206
+ const incomingMessage = JSON.parse(data);
207
+ if ("method" in incomingMessage) {
208
+ this.handleIncomingRequest(incomingMessage);
209
+ return;
210
+ }
211
+ this.handleResponseMessage(incomingMessage);
212
+ });
213
+ ws.addEventListener("close", (event) => {
214
+ handleCloseOrError(event);
215
+ this.callbacks.onClose?.(event);
216
+ if (!this.lazyMode || this.requestManager.hasPendingSubscriptions()) this.reconnect(new require__Users_julius_dev_trpc_packages_client_src_links_wsLink_wsClient_utils.TRPCWebSocketClosedError({
217
+ message: "WebSocket closed",
218
+ cause: event
219
+ }));
220
+ });
221
+ ws.addEventListener("error", (event) => {
222
+ handleCloseOrError(event);
223
+ this.callbacks.onError?.(event);
224
+ this.reconnect(new require__Users_julius_dev_trpc_packages_client_src_links_wsLink_wsClient_utils.TRPCWebSocketClosedError({
225
+ message: "WebSocket closed",
226
+ cause: event
227
+ }));
228
+ });
229
+ }
230
+ handleResponseMessage(message) {
231
+ const request = this.requestManager.getPendingRequest(message.id);
232
+ if (!request) return;
233
+ request.callbacks.next(message);
234
+ let completed = true;
235
+ if ("result" in message && request.message.method === "subscription") {
236
+ if (message.result.type === "data") request.message.params.lastEventId = message.result.id;
237
+ if (message.result.type !== "stopped") completed = false;
238
+ }
239
+ if (completed) {
240
+ request.callbacks.complete();
241
+ this.requestManager.delete(message.id);
242
+ }
243
+ }
244
+ handleIncomingRequest(message) {
245
+ if (message.method === "reconnect") this.reconnect(new require__Users_julius_dev_trpc_packages_client_src_links_wsLink_wsClient_utils.TRPCWebSocketClosedError({ message: "Server requested reconnect" }));
246
+ }
247
+ /**
248
+ * Sends a message or batch of messages directly to the server.
249
+ */
250
+ send(messageOrMessages) {
251
+ if (!this.activeConnection.isOpen()) throw new Error("Active connection is not open");
252
+ const messages = messageOrMessages instanceof Array ? messageOrMessages : [messageOrMessages];
253
+ this.activeConnection.ws.send(JSON.stringify(messages.length === 1 ? messages[0] : messages));
254
+ }
255
+ /**
256
+ * Groups requests for batch sending.
257
+ *
258
+ * @returns A function to abort the batched request.
259
+ */
260
+ batchSend(message, callbacks) {
261
+ this.inactivityTimeout.reset();
262
+ (0, __trpc_server_unstable_core_do_not_import.run)(async () => {
263
+ if (!this.activeConnection.isOpen()) await this.open();
264
+ await (0, __trpc_server_unstable_core_do_not_import.sleep)(0);
265
+ if (!this.requestManager.hasOutgoingRequests()) return;
266
+ this.send(this.requestManager.flush().map(({ message: message$1 }) => message$1));
267
+ }).catch((err) => {
268
+ this.requestManager.delete(message.id);
269
+ callbacks.error(require__Users_julius_dev_trpc_packages_client_src_TRPCClientError.TRPCClientError.from(err));
270
+ });
271
+ return this.requestManager.register(message, callbacks);
272
+ }
273
+ };
324
274
 
325
- exports.WsClient = WsClient;
275
+ //#endregion
276
+ exports.WsClient = WsClient;