@trpc/client 11.0.0-rc.806 → 11.0.0-rc.807

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.
@@ -1,12 +1,12 @@
1
1
  {
2
- "bundleSize": 75585,
3
- "bundleOrigSize": 94308,
2
+ "bundleSize": 75612,
3
+ "bundleOrigSize": 94335,
4
4
  "bundleReduction": 19.85,
5
5
  "modules": [
6
6
  {
7
7
  "id": "/src/links/wsLink/wsClient/wsClient.ts",
8
- "size": 12353,
9
- "origSize": 12459,
8
+ "size": 12067,
9
+ "origSize": 12199,
10
10
  "renderedExports": [
11
11
  "WsClient"
12
12
  ],
@@ -14,8 +14,8 @@
14
14
  "dependents": [
15
15
  "/src/links/wsLink/createWsClient.ts"
16
16
  ],
17
- "percent": 16.34,
18
- "reduction": 0.85
17
+ "percent": 15.96,
18
+ "reduction": 1.08
19
19
  },
20
20
  {
21
21
  "id": "/src/links/httpSubscriptionLink.ts",
@@ -29,6 +29,21 @@
29
29
  "percent": 10.54,
30
30
  "reduction": 0
31
31
  },
32
+ {
33
+ "id": "/src/links/wsLink/wsClient/wsConnection.ts",
34
+ "size": 6174,
35
+ "origSize": 6492,
36
+ "renderedExports": [
37
+ "WsConnection",
38
+ "backwardCompatibility"
39
+ ],
40
+ "removedExports": [],
41
+ "dependents": [
42
+ "/src/links/wsLink/wsClient/wsClient.ts"
43
+ ],
44
+ "percent": 8.17,
45
+ "reduction": 4.9
46
+ },
32
47
  {
33
48
  "id": "/src/links/httpBatchStreamLink.ts",
34
49
  "size": 6017,
@@ -41,21 +56,6 @@
41
56
  "percent": 7.96,
42
57
  "reduction": 4.42
43
58
  },
44
- {
45
- "id": "/src/links/wsLink/wsClient/wsConnection.ts",
46
- "size": 5861,
47
- "origSize": 6205,
48
- "renderedExports": [
49
- "WsConnection",
50
- "backwardCompatibility"
51
- ],
52
- "removedExports": [],
53
- "dependents": [
54
- "/src/links/wsLink/wsClient/wsClient.ts"
55
- ],
56
- "percent": 7.75,
57
- "reduction": 5.54
58
- },
59
59
  {
60
60
  "id": "/src/links/loggerLink.ts",
61
61
  "size": 5596,
@@ -141,7 +141,7 @@
141
141
  ],
142
142
  "removedExports": [],
143
143
  "dependents": [],
144
- "percent": 4.36,
144
+ "percent": 4.35,
145
145
  "reduction": 12.56
146
146
  },
147
147
  {
@@ -402,16 +402,6 @@
402
402
  "percent": 0.1,
403
403
  "reduction": 76.83
404
404
  },
405
- {
406
- "id": "/src/index.ts",
407
- "size": 0,
408
- "origSize": 540,
409
- "renderedExports": [],
410
- "removedExports": [],
411
- "dependents": [],
412
- "percent": 0,
413
- "reduction": 100
414
- },
415
405
  {
416
406
  "id": "/src/unstable-internals.ts",
417
407
  "size": 0,
@@ -425,6 +415,16 @@
425
415
  ],
426
416
  "percent": 0,
427
417
  "reduction": 100
418
+ },
419
+ {
420
+ "id": "/src/index.ts",
421
+ "size": 0,
422
+ "origSize": 540,
423
+ "renderedExports": [],
424
+ "removedExports": [],
425
+ "dependents": [],
426
+ "percent": 0,
427
+ "reduction": 100
428
428
  }
429
429
  ],
430
430
  "moduleCount": 29
@@ -20,7 +20,6 @@ export declare class WsClient {
20
20
  private readonly reconnectRetryDelay;
21
21
  private inactivityTimeout;
22
22
  private readonly callbacks;
23
- private readonly connectionParams;
24
23
  private readonly lazyMode;
25
24
  constructor(opts: WebSocketClientOptions);
26
25
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"wsClient.d.ts","sourceRoot":"","sources":["../../../../src/links/wsLink/wsClient/wsClient.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAE/D,OAAO,KAAK,EACV,uBAAuB,EAKxB,MAAM,0CAA0C,CAAC;AAMlD,OAAO,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAC3D,OAAO,KAAK,EAAE,mBAAmB,EAAE,MA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WAAW,GACZ,EAAE;QACD,EAAE,EAAE,IAAI,CAAC,SAAS,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,QAAQ,CAAC,CAAC;QACjE,WAAW,EAAE,uBAAuB,CAAC;QACrC,WAAW,CAAC,EAAE,MAAM,CAAC;KACtB;IA+CD,IAAW,UAAU;;;;;;;;;;;;aAEpB;IAED;;;;OAIG;IACH,OAAO,CAAC,YAAY,CAA8B;IAClD,OAAO,CAAC,SAAS;IAgCjB,OAAO,CAAC,uBAAuB;IA+E/B,OAAO,CAAC,qBAAqB;IAuB7B,OAAO,CAAC,qBAAqB;IAU7B;;OAEG;IACH,OAAO,CAAC,IAAI;IAgBZ;;;;OAIG;IACH,OAAO,CAAC,SAAS;CAmBlB"}
@@ -133,6 +133,9 @@ function _define_property(obj, key, value) {
133
133
  if (this.allowReconnect) {
134
134
  await this.activeConnection.close();
135
135
  await this.activeConnection.open();
136
+ if (this.requestManager.hasPendingRequests()) {
137
+ this.send(this.requestManager.getPendingRequests().map(({ message })=>message));
138
+ }
136
139
  }
137
140
  this.reconnecting = null;
138
141
  } catch {
@@ -158,19 +161,12 @@ function _define_property(obj, key, value) {
158
161
  if (this.lazyMode) {
159
162
  this.inactivityTimeout.start();
160
163
  }
161
- if (this.connectionParams) {
162
- ws.send(await utils.buildConnectionMessage(this.connectionParams));
163
- }
164
164
  this.callbacks.onOpen?.();
165
165
  this.connectionState.next({
166
166
  type: 'state',
167
167
  state: 'pending',
168
168
  error: null
169
169
  });
170
- const messages = this.requestManager.getPendingRequests().map(({ message })=>message);
171
- if (messages.length) {
172
- ws.send(JSON.stringify(messages));
173
- }
174
170
  }).catch((error)=>{
175
171
  ws.close(3000);
176
172
  handleCloseOrError(error);
@@ -273,7 +269,6 @@ function _define_property(obj, key, value) {
273
269
  _define_property(this, "reconnectRetryDelay", void 0);
274
270
  _define_property(this, "inactivityTimeout", void 0);
275
271
  _define_property(this, "callbacks", void 0);
276
- _define_property(this, "connectionParams", void 0);
277
272
  _define_property(this, "lazyMode", void 0);
278
273
  /**
279
274
  * Manages the reconnection process for the WebSocket using retry logic.
@@ -286,7 +281,6 @@ function _define_property(obj, key, value) {
286
281
  onClose: opts.onClose,
287
282
  onError: opts.onError
288
283
  };
289
- this.connectionParams = opts.connectionParams;
290
284
  const lazyOptions = {
291
285
  ...options.lazyDefaults,
292
286
  ...opts.lazy
@@ -3,7 +3,7 @@ import { transformResult, run, sleep } from '@trpc/server/unstable-core-do-not-i
3
3
  import { TRPCClientError } from '../../../TRPCClientError.mjs';
4
4
  import { lazyDefaults, keepAliveDefaults, exponentialBackoff } from './options.mjs';
5
5
  import { RequestManager } from './requestManager.mjs';
6
- import { TRPCWebSocketClosedError, buildConnectionMessage, ResettableTimeout } from './utils.mjs';
6
+ import { TRPCWebSocketClosedError, ResettableTimeout } from './utils.mjs';
7
7
  import { backwardCompatibility, WsConnection } from './wsConnection.mjs';
8
8
 
9
9
  function _define_property(obj, key, value) {
@@ -131,6 +131,9 @@ function _define_property(obj, key, value) {
131
131
  if (this.allowReconnect) {
132
132
  await this.activeConnection.close();
133
133
  await this.activeConnection.open();
134
+ if (this.requestManager.hasPendingRequests()) {
135
+ this.send(this.requestManager.getPendingRequests().map(({ message })=>message));
136
+ }
134
137
  }
135
138
  this.reconnecting = null;
136
139
  } catch {
@@ -156,19 +159,12 @@ function _define_property(obj, key, value) {
156
159
  if (this.lazyMode) {
157
160
  this.inactivityTimeout.start();
158
161
  }
159
- if (this.connectionParams) {
160
- ws.send(await buildConnectionMessage(this.connectionParams));
161
- }
162
162
  this.callbacks.onOpen?.();
163
163
  this.connectionState.next({
164
164
  type: 'state',
165
165
  state: 'pending',
166
166
  error: null
167
167
  });
168
- const messages = this.requestManager.getPendingRequests().map(({ message })=>message);
169
- if (messages.length) {
170
- ws.send(JSON.stringify(messages));
171
- }
172
168
  }).catch((error)=>{
173
169
  ws.close(3000);
174
170
  handleCloseOrError(error);
@@ -271,7 +267,6 @@ function _define_property(obj, key, value) {
271
267
  _define_property(this, "reconnectRetryDelay", void 0);
272
268
  _define_property(this, "inactivityTimeout", void 0);
273
269
  _define_property(this, "callbacks", void 0);
274
- _define_property(this, "connectionParams", void 0);
275
270
  _define_property(this, "lazyMode", void 0);
276
271
  /**
277
272
  * Manages the reconnection process for the WebSocket using retry logic.
@@ -284,7 +279,6 @@ function _define_property(obj, key, value) {
284
279
  onClose: opts.onClose,
285
280
  onError: opts.onError
286
281
  };
287
- this.connectionParams = opts.connectionParams;
288
282
  const lazyOptions = {
289
283
  ...lazyDefaults,
290
284
  ...opts.lazy
@@ -1 +1 @@
1
- {"version":3,"file":"wsConnection.d.ts","sourceRoot":"","sources":["../../../../src/links/wsLink/wsClient/wsConnection.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,8BAA8B,EAAE,MAAM,yCAAyC,CAAC;AAoB9F,UAAU,eAAe;IACvB;;OAEG;IACH,UAAU,EAAE,MAAM,CAAC;IAEnB;;OAEG;IACH,aAAa,EAAE,MAAM,CAAC;CACvB;AAuDD,MAAM,WAAW,0BAA0B;IACzC,iBAAiB,CAAC,EAAE,OAAO,SAAS,CAAC;IACrC,UAAU,EAAE,8BAA8B,CAAC;IAC3C,SAAS,EAAE,eAAe,GAAG;QAC3B,OAAO,EAAE,OAAO,CAAC;KAClB,CAAC;CACH;AAED;;;GAGG;AACH,qBAAa,YAAY;IACvB,MAAM,CAAC,YAAY,SAAK;IACjB,EAAE,SAA+B;IAExC,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAmB;IACrD,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAiC;IAC5D,OAAO,CAAC,QAAQ,CAAC,aAAa,CAA0C;IACxE,SAAgB,YAAY,sEAA2C;gBAE3D,IAAI,EAAE,0BAA0B;IAY5C,IAAW,EAAE,qBAEZ;IAED,OAAO,KAAK,EAAE,QAEb;IAED;;OAEG;IACI,MAAM,IAAI,IAAI,IAAI;QAAE,EAAE,EAAE,SAAS,CAAA;K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AAO,CAAC,WAAW,CAA8B;IACpC,IAAI;IAyCjB;;;OAGG;IACU,KAAK;CAOnB;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,UAAU,EAAE,YAAY;;;;;;;;;;;;SA0B7D"}
@@ -85,7 +85,7 @@ function _define_property(obj, key, value) {
85
85
  /**
86
86
  * Checks if the WebSocket connection is open and ready to communicate.
87
87
  */ isOpen() {
88
- return !!this.ws && this.ws.readyState === this.WebSocketPonyfill.OPEN;
88
+ return !!this.ws && this.ws.readyState === this.WebSocketPonyfill.OPEN && !this.openPromise;
89
89
  }
90
90
  /**
91
91
  * Checks if the WebSocket connection is closed or in the process of closing.
@@ -96,19 +96,26 @@ function _define_property(obj, key, value) {
96
96
  if (this.openPromise) return this.openPromise;
97
97
  this.id = ++WsConnection.connectCount;
98
98
  const wsPromise = utils.prepareUrl(this.urlOptions).then((url)=>new this.WebSocketPonyfill(url));
99
- this.openPromise = wsPromise.then(asyncWsOpen);
100
- this.ws = await wsPromise;
101
- // Setup ping listener
102
- this.ws.addEventListener('message', function({ data }) {
103
- if (data === 'PING') {
104
- this.send('PONG');
99
+ this.openPromise = wsPromise.then(async (ws)=>{
100
+ this.ws = ws;
101
+ // Setup ping listener
102
+ ws.addEventListener('message', function({ data }) {
103
+ if (data === 'PING') {
104
+ this.send('PONG');
105
+ }
106
+ });
107
+ if (this.keepAliveOpts.enabled) {
108
+ setupPingInterval(ws, this.keepAliveOpts);
109
+ }
110
+ ws.addEventListener('close', ()=>{
111
+ if (this.ws === ws) {
112
+ this.ws = null;
113
+ }
114
+ });
115
+ await asyncWsOpen(ws);
116
+ if (this.urlOptions.connectionParams) {
117
+ ws.send(await utils.buildConnectionMessage(this.urlOptions.connectionParams));
105
118
  }
106
- });
107
- if (this.keepAliveOpts.enabled) {
108
- setupPingInterval(this.ws, this.keepAliveOpts);
109
- }
110
- this.ws.addEventListener('close', ()=>{
111
- this.ws = null;
112
119
  });
113
120
  try {
114
121
  await this.openPromise;
@@ -1,5 +1,5 @@
1
1
  import { behaviorSubject } from '@trpc/server/observable';
2
- import { prepareUrl, withResolvers } from './utils.mjs';
2
+ import { prepareUrl, buildConnectionMessage, withResolvers } from './utils.mjs';
3
3
 
4
4
  function _define_property(obj, key, value) {
5
5
  if (key in obj) {
@@ -83,7 +83,7 @@ function _define_property(obj, key, value) {
83
83
  /**
84
84
  * Checks if the WebSocket connection is open and ready to communicate.
85
85
  */ isOpen() {
86
- return !!this.ws && this.ws.readyState === this.WebSocketPonyfill.OPEN;
86
+ return !!this.ws && this.ws.readyState === this.WebSocketPonyfill.OPEN && !this.openPromise;
87
87
  }
88
88
  /**
89
89
  * Checks if the WebSocket connection is closed or in the process of closing.
@@ -94,19 +94,26 @@ function _define_property(obj, key, value) {
94
94
  if (this.openPromise) return this.openPromise;
95
95
  this.id = ++WsConnection.connectCount;
96
96
  const wsPromise = prepareUrl(this.urlOptions).then((url)=>new this.WebSocketPonyfill(url));
97
- this.openPromise = wsPromise.then(asyncWsOpen);
98
- this.ws = await wsPromise;
99
- // Setup ping listener
100
- this.ws.addEventListener('message', function({ data }) {
101
- if (data === 'PING') {
102
- this.send('PONG');
97
+ this.openPromise = wsPromise.then(async (ws)=>{
98
+ this.ws = ws;
99
+ // Setup ping listener
100
+ ws.addEventListener('message', function({ data }) {
101
+ if (data === 'PING') {
102
+ this.send('PONG');
103
+ }
104
+ });
105
+ if (this.keepAliveOpts.enabled) {
106
+ setupPingInterval(ws, this.keepAliveOpts);
107
+ }
108
+ ws.addEventListener('close', ()=>{
109
+ if (this.ws === ws) {
110
+ this.ws = null;
111
+ }
112
+ });
113
+ await asyncWsOpen(ws);
114
+ if (this.urlOptions.connectionParams) {
115
+ ws.send(await buildConnectionMessage(this.urlOptions.connectionParams));
103
116
  }
104
- });
105
- if (this.keepAliveOpts.enabled) {
106
- setupPingInterval(this.ws, this.keepAliveOpts);
107
- }
108
- this.ws.addEventListener('close', ()=>{
109
- this.ws = null;
110
117
  });
111
118
  try {
112
119
  await this.openPromise;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trpc/client",
3
- "version": "11.0.0-rc.806+c16467cd1",
3
+ "version": "11.0.0-rc.807+be14f29e3",
4
4
  "description": "The tRPC client library",
5
5
  "author": "KATT",
6
6
  "license": "MIT",
@@ -77,11 +77,11 @@
77
77
  "!**/__tests__"
78
78
  ],
79
79
  "peerDependencies": {
80
- "@trpc/server": "11.0.0-rc.806+c16467cd1",
80
+ "@trpc/server": "11.0.0-rc.807+be14f29e3",
81
81
  "typescript": ">=5.7.2"
82
82
  },
83
83
  "devDependencies": {
84
- "@trpc/server": "11.0.0-rc.806+c16467cd1",
84
+ "@trpc/server": "11.0.0-rc.807+be14f29e3",
85
85
  "@types/isomorphic-fetch": "^0.0.39",
86
86
  "@types/node": "^22.13.5",
87
87
  "eslint": "^9.21.0",
@@ -100,5 +100,5 @@
100
100
  "funding": [
101
101
  "https://trpc.io/sponsor"
102
102
  ],
103
- "gitHead": "c16467cd1b5156dfa8a3ad4acace14f3306c66be"
103
+ "gitHead": "be14f29e3f0667a2670f89e2e9e6d017797502d8"
104
104
  }
@@ -20,11 +20,7 @@ import type { WebSocketClientOptions } from './options';
20
20
  import { exponentialBackoff, keepAliveDefaults, lazyDefaults } from './options';
21
21
  import type { TCallbacks } from './requestManager';
22
22
  import { RequestManager } from './requestManager';
23
- import {
24
- buildConnectionMessage,
25
- ResettableTimeout,
26
- TRPCWebSocketClosedError,
27
- } from './utils';
23
+ import { ResettableTimeout, TRPCWebSocketClosedError } from './utils';
28
24
  import { backwardCompatibility, WsConnection } from './wsConnection';
29
25
 
30
26
  /**
@@ -48,7 +44,6 @@ export class WsClient {
48
44
  WebSocketClientOptions,
49
45
  'onOpen' | 'onClose' | 'onError'
50
46
  >;
51
- private readonly connectionParams: WebSocketClientOptions['connectionParams'];
52
47
  private readonly lazyMode: boolean;
53
48
 
54
49
  constructor(opts: WebSocketClientOptions) {
@@ -58,7 +53,6 @@ export class WsClient {
58
53
  onClose: opts.onClose,
59
54
  onError: opts.onError,
60
55
  };
61
- this.connectionParams = opts.connectionParams;
62
56
 
63
57
  const lazyOptions = {
64
58
  ...lazyDefaults,
@@ -262,6 +256,14 @@ export class WsClient {
262
256
  if (this.allowReconnect) {
263
257
  await this.activeConnection.close();
264
258
  await this.activeConnection.open();
259
+
260
+ if (this.requestManager.hasPendingRequests()) {
261
+ this.send(
262
+ this.requestManager
263
+ .getPendingRequests()
264
+ .map(({ message }) => message),
265
+ );
266
+ }
265
267
  }
266
268
  this.reconnecting = null;
267
269
  } catch {
@@ -297,10 +299,6 @@ export class WsClient {
297
299
  this.inactivityTimeout.start();
298
300
  }
299
301
 
300
- if (this.connectionParams) {
301
- ws.send(await buildConnectionMessage(this.connectionParams));
302
- }
303
-
304
302
  this.callbacks.onOpen?.();
305
303
 
306
304
  this.connectionState.next({
@@ -308,13 +306,6 @@ export class WsClient {
308
306
  state: 'pending',
309
307
  error: null,
310
308
  });
311
-
312
- const messages = this.requestManager
313
- .getPendingRequests()
314
- .map(({ message }) => message);
315
- if (messages.length) {
316
- ws.send(JSON.stringify(messages));
317
- }
318
309
  }).catch((error) => {
319
310
  ws.close(3000);
320
311
  handleCloseOrError(error);
@@ -1,6 +1,6 @@
1
1
  import { behaviorSubject } from '@trpc/server/observable';
2
2
  import type { UrlOptionsWithConnectionParams } from '../../internals/urlWithConnectionParams';
3
- import { prepareUrl, withResolvers } from './utils';
3
+ import { buildConnectionMessage, prepareUrl, withResolvers } from './utils';
4
4
 
5
5
  /**
6
6
  * Opens a WebSocket connection asynchronously and returns a promise
@@ -129,7 +129,11 @@ export class WsConnection {
129
129
  * Checks if the WebSocket connection is open and ready to communicate.
130
130
  */
131
131
  public isOpen(): this is { ws: WebSocket } {
132
- return !!this.ws && this.ws.readyState === this.WebSocketPonyfill.OPEN;
132
+ return (
133
+ !!this.ws &&
134
+ this.ws.readyState === this.WebSocketPonyfill.OPEN &&
135
+ !this.openPromise
136
+ );
133
137
  }
134
138
 
135
139
  /**
@@ -160,22 +164,31 @@ export class WsConnection {
160
164
  const wsPromise = prepareUrl(this.urlOptions).then(
161
165
  (url) => new this.WebSocketPonyfill(url),
162
166
  );
163
- this.openPromise = wsPromise.then(asyncWsOpen);
164
- this.ws = await wsPromise;
165
-
166
- // Setup ping listener
167
- this.ws.addEventListener('message', function ({ data }) {
168
- if (data === 'PING') {
169
- this.send('PONG');
167
+ this.openPromise = wsPromise.then(async (ws) => {
168
+ this.ws = ws;
169
+
170
+ // Setup ping listener
171
+ ws.addEventListener('message', function ({ data }) {
172
+ if (data === 'PING') {
173
+ this.send('PONG');
174
+ }
175
+ });
176
+
177
+ if (this.keepAliveOpts.enabled) {
178
+ setupPingInterval(ws, this.keepAliveOpts);
170
179
  }
171
- });
172
180
 
173
- if (this.keepAliveOpts.enabled) {
174
- setupPingInterval(this.ws, this.keepAliveOpts);
175
- }
181
+ ws.addEventListener('close', () => {
182
+ if (this.ws === ws) {
183
+ this.ws = null;
184
+ }
185
+ });
176
186
 
177
- this.ws.addEventListener('close', () => {
178
- this.ws = null;
187
+ await asyncWsOpen(ws);
188
+
189
+ if (this.urlOptions.connectionParams) {
190
+ ws.send(await buildConnectionMessage(this.urlOptions.connectionParams));
191
+ }
179
192
  });
180
193
 
181
194
  try {