@trpc/server 11.0.0-rc.530 → 11.0.0-rc.532

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 +1 @@
1
- {"version":3,"file":"ws.d.ts","sourceRoot":"","sources":["../../src/adapters/ws.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,MAAM,CAAC;AAC5C,OAAO,KAAK,EAAE,MAAM,IAAI,CAAC;AACzB,OAAO,KAAK,EACV,SAAS,EACT,qBAAqB,EACrB,kBAAkB,EACnB,MAAM,iBAAiB,CAAC;AASzB,OAAO,EAAS,KAAK,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AActE,OAAO,EAKL,KAAK,YAAY,EAClB,MAAM,gCAAgC,CAAC;AACxC,OAAO,KAAK,EAAE,8BAA8B,EAAE,MAAM,aAAa,CAAC;AAQlE;;GAEG;AACH,MAAM,MAAM,yBAAyB,GAAG,8BAA8B,CACpE,eAAe,EACf,EAAE,CAAC,SAAS,CACb,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,kBAAkB,CAAC,OAAO,SAAS,SAAS,IAAI,CAC1D,IAAI,EAAE,yBAAyB,KAC5B,YAAY,CAAC,kBAAkB,CAAC,OAAO,CAAC,CAAC,CAAC;AAE/C,MAAM,MAAM,0BAA0B,CAAC,OAAO,SAAS,SAAS,IAC9D,kBAAkB,CAAC,OAAO,EAAE,eAAe,CAAC,GAC1C,qBAAqB,CACnB,kBAAkB,CAAC,OAAO,CAAC,EAC3B,kBAAkB,CAAC,OAAO,CAAC,CAC5B,CAAC;AAEN;;GAEG;AACH,MAAM,MAAM,iBAAiB,CAAC,OAAO,SAAS,SAAS,IACrD,0BAA0B,CAAC,OAAO,CAAC,GAAG;IACpC,GAAG,EAAE,EAAE,CAAC,eAAe,CAAC;IACxB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE;QACV;;;WAGG;QACH,OAAO,EAAE,OAAO,CAAC;QACjB;;;WAGG;QACH,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB;;;WAGG;QACH,UAAU,CAAC,EAAE,MAAM,CAAC;KACrB,CAAC;IACF;;;;OAIG;IACH,sBAAsB,CAAC,EAAE,OAAO,CAAC;CAClC,CAAC;AAGJ,wBAAgB,sBAAsB,CAAC,OAAO,SAAS,SAAS,EAC9D,IAAI,EAAE,iBAAiB,CAAC,OAAO,CAAC,YAKV,EAAE,CAAC,SAAS,OAAO,eAAe,mBAoXzD;AAED;;GAEG;AACH,wBAAgB,eAAe,CAC7B,MAAM,EAAE,EAAE,CAAC,SAAS,EACpB,MAAM,SAAS,EACf,UAAU,SAAQ,QAuBnB;AAED,wBAAgB,eAAe,CAAC,OAAO,SAAS,SAAS,EACvD,IAAI,EAAE,iBAAiB,CAAC,OAAO,CAAC;;EAwCjC"}
1
+ {"version":3,"file":"ws.d.ts","sourceRoot":"","sources":["../../src/adapters/ws.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,MAAM,CAAC;AAC5C,OAAO,KAAK,EAAE,MAAM,IAAI,CAAC;AACzB,OAAO,KAAK,EACV,SAAS,EACT,qBAAqB,EACrB,kBAAkB,EACnB,MAAM,iBAAiB,CAAC;AASzB,OAAO,EAAS,KAAK,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AActE,OAAO,EAKL,KAAK,YAAY,EAClB,MAAM,gCAAgC,CAAC;AACxC,OAAO,KAAK,EAAE,8BAA8B,EAAE,MAAM,aAAa,CAAC;AAQlE;;GAEG;AACH,MAAM,MAAM,yBAAyB,GAAG,8BAA8B,CACpE,eAAe,EACf,EAAE,CAAC,SAAS,CACb,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,kBAAkB,CAAC,OAAO,SAAS,SAAS,IAAI,CAC1D,IAAI,EAAE,yBAAyB,KAC5B,YAAY,CAAC,kBAAkB,CAAC,OAAO,CAAC,CAAC,CAAC;AAE/C,MAAM,MAAM,0BAA0B,CAAC,OAAO,SAAS,SAAS,IAC9D,kBAAkB,CAAC,OAAO,EAAE,eAAe,CAAC,GAC1C,qBAAqB,CACnB,kBAAkB,CAAC,OAAO,CAAC,EAC3B,kBAAkB,CAAC,OAAO,CAAC,CAC5B,CAAC;AAEN;;GAEG;AACH,MAAM,MAAM,iBAAiB,CAAC,OAAO,SAAS,SAAS,IACrD,0BAA0B,CAAC,OAAO,CAAC,GAAG;IACpC,GAAG,EAAE,EAAE,CAAC,eAAe,CAAC;IACxB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE;QACV;;;WAGG;QACH,OAAO,EAAE,OAAO,CAAC;QACjB;;;WAGG;QACH,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB;;;WAGG;QACH,UAAU,CAAC,EAAE,MAAM,CAAC;KACrB,CAAC;IACF;;;;OAIG;IACH,sBAAsB,CAAC,EAAE,OAAO,CAAC;CAClC,CAAC;AAGJ,wBAAgB,sBAAsB,CAAC,OAAO,SAAS,SAAS,EAC9D,IAAI,EAAE,iBAAiB,CAAC,OAAO,CAAC,YAKV,EAAE,CAAC,SAAS,OAAO,eAAe,mBAsXzD;AAED;;GAEG;AACH,wBAAgB,eAAe,CAC7B,MAAM,EAAE,EAAE,CAAC,SAAS,EACpB,MAAM,SAAS,EACf,UAAU,SAAQ,QAiCnB;AAED,wBAAgB,eAAe,CAAC,OAAO,SAAS,SAAS,EACvD,IAAI,EAAE,iBAAiB,CAAC,OAAO,CAAC;;EAwCjC"}
@@ -281,13 +281,23 @@ function getWSConnectionHandler(opts) {
281
281
  });
282
282
  }
283
283
  }
284
- client.on('message', async (message)=>{
284
+ client.on('message', async (rawData)=>{
285
+ const msgStr = rawData.toString();
286
+ if (msgStr === 'PONG') {
287
+ return;
288
+ }
289
+ if (msgStr === 'PING') {
290
+ if (!opts.dangerouslyDisablePong) {
291
+ client.send('PONG');
292
+ }
293
+ return;
294
+ }
285
295
  if (ctxPromise === unsetContextPromiseSymbol) {
286
296
  // If the ctxPromise wasn't created immediately, we're expecting the first message to be a TRPCConnectionParamsMessage
287
297
  ctxPromise = createCtxPromise(()=>{
288
298
  let msg;
289
299
  try {
290
- msg = JSON.parse(message.toString());
300
+ msg = JSON.parse(msgStr);
291
301
  if (!utils.isObject(msg)) {
292
302
  throw new Error('Message was not an object');
293
303
  }
@@ -304,14 +314,7 @@ function getWSConnectionHandler(opts) {
304
314
  return;
305
315
  }
306
316
  try {
307
- const str = message.toString();
308
- if (str === 'PING') {
309
- if (!opts.dangerouslyDisablePong) {
310
- client.send('PONG');
311
- }
312
- return;
313
- }
314
- const msgJSON = JSON.parse(str);
317
+ const msgJSON = JSON.parse(msgStr);
315
318
  const msgs = Array.isArray(msgJSON) ? msgJSON : [
316
319
  msgJSON
317
320
  ];
@@ -364,27 +367,30 @@ function getWSConnectionHandler(opts) {
364
367
  /**
365
368
  * Handle WebSocket keep-alive messages
366
369
  */ function handleKeepAlive(client, pingMs = 30000, pongWaitMs = 5000) {
367
- let heartbeatTimeout;
368
- const heartbeatInterval = setInterval(()=>{
369
- if (client.readyState !== WEBSOCKET_OPEN) {
370
- return;
371
- }
372
- // First we send a ping message and wait for a pong
373
- client.ping();
374
- // We set a timeout to close the connection if the pong is not received
375
- heartbeatTimeout = setTimeout(()=>{
376
- client.terminate();
377
- clearInterval(heartbeatInterval);
378
- }, pongWaitMs);
379
- }, pingMs).unref();
380
- // When we receive a pong message, we clear the timeout
381
- client.on('pong', ()=>{
382
- heartbeatTimeout && clearTimeout(heartbeatTimeout);
383
- });
384
- // If the connection is closed, we clear the interval
370
+ let timeout = undefined;
371
+ let ping = undefined;
372
+ const schedulePing = ()=>{
373
+ const scheduleTimeout = ()=>{
374
+ timeout = setTimeout(()=>{
375
+ client.terminate();
376
+ }, pongWaitMs);
377
+ };
378
+ ping = setTimeout(()=>{
379
+ client.send('PING');
380
+ scheduleTimeout();
381
+ }, pingMs);
382
+ };
383
+ const onMessage = ()=>{
384
+ clearTimeout(ping);
385
+ clearTimeout(timeout);
386
+ schedulePing();
387
+ };
388
+ client.on('message', onMessage);
385
389
  client.on('close', ()=>{
386
- clearInterval(heartbeatInterval);
390
+ clearTimeout(ping);
391
+ clearTimeout(timeout);
387
392
  });
393
+ schedulePing();
388
394
  }
389
395
  function applyWSSHandler(opts) {
390
396
  const onConnection = getWSConnectionHandler(opts);
@@ -279,13 +279,23 @@ function getWSConnectionHandler(opts) {
279
279
  });
280
280
  }
281
281
  }
282
- client.on('message', async (message)=>{
282
+ client.on('message', async (rawData)=>{
283
+ const msgStr = rawData.toString();
284
+ if (msgStr === 'PONG') {
285
+ return;
286
+ }
287
+ if (msgStr === 'PING') {
288
+ if (!opts.dangerouslyDisablePong) {
289
+ client.send('PONG');
290
+ }
291
+ return;
292
+ }
283
293
  if (ctxPromise === unsetContextPromiseSymbol) {
284
294
  // If the ctxPromise wasn't created immediately, we're expecting the first message to be a TRPCConnectionParamsMessage
285
295
  ctxPromise = createCtxPromise(()=>{
286
296
  let msg;
287
297
  try {
288
- msg = JSON.parse(message.toString());
298
+ msg = JSON.parse(msgStr);
289
299
  if (!isObject(msg)) {
290
300
  throw new Error('Message was not an object');
291
301
  }
@@ -302,14 +312,7 @@ function getWSConnectionHandler(opts) {
302
312
  return;
303
313
  }
304
314
  try {
305
- const str = message.toString();
306
- if (str === 'PING') {
307
- if (!opts.dangerouslyDisablePong) {
308
- client.send('PONG');
309
- }
310
- return;
311
- }
312
- const msgJSON = JSON.parse(str);
315
+ const msgJSON = JSON.parse(msgStr);
313
316
  const msgs = Array.isArray(msgJSON) ? msgJSON : [
314
317
  msgJSON
315
318
  ];
@@ -362,27 +365,30 @@ function getWSConnectionHandler(opts) {
362
365
  /**
363
366
  * Handle WebSocket keep-alive messages
364
367
  */ function handleKeepAlive(client, pingMs = 30000, pongWaitMs = 5000) {
365
- let heartbeatTimeout;
366
- const heartbeatInterval = setInterval(()=>{
367
- if (client.readyState !== WEBSOCKET_OPEN) {
368
- return;
369
- }
370
- // First we send a ping message and wait for a pong
371
- client.ping();
372
- // We set a timeout to close the connection if the pong is not received
373
- heartbeatTimeout = setTimeout(()=>{
374
- client.terminate();
375
- clearInterval(heartbeatInterval);
376
- }, pongWaitMs);
377
- }, pingMs).unref();
378
- // When we receive a pong message, we clear the timeout
379
- client.on('pong', ()=>{
380
- heartbeatTimeout && clearTimeout(heartbeatTimeout);
381
- });
382
- // If the connection is closed, we clear the interval
368
+ let timeout = undefined;
369
+ let ping = undefined;
370
+ const schedulePing = ()=>{
371
+ const scheduleTimeout = ()=>{
372
+ timeout = setTimeout(()=>{
373
+ client.terminate();
374
+ }, pongWaitMs);
375
+ };
376
+ ping = setTimeout(()=>{
377
+ client.send('PING');
378
+ scheduleTimeout();
379
+ }, pingMs);
380
+ };
381
+ const onMessage = ()=>{
382
+ clearTimeout(ping);
383
+ clearTimeout(timeout);
384
+ schedulePing();
385
+ };
386
+ client.on('message', onMessage);
383
387
  client.on('close', ()=>{
384
- clearInterval(heartbeatInterval);
388
+ clearTimeout(ping);
389
+ clearTimeout(timeout);
385
390
  });
391
+ schedulePing();
386
392
  }
387
393
  function applyWSSHandler(opts) {
388
394
  const onConnection = getWSConnectionHandler(opts);
@@ -1,7 +1,7 @@
1
1
  {
2
- "bundleSize": 136257,
3
- "bundleOrigSize": 186007,
4
- "bundleReduction": 26.75,
2
+ "bundleSize": 136151,
3
+ "bundleOrigSize": 185917,
4
+ "bundleReduction": 26.77,
5
5
  "modules": [
6
6
  {
7
7
  "id": "/src/unstable-core-do-not-import/http/resolveResponse.ts",
@@ -14,7 +14,7 @@
14
14
  "dependents": [
15
15
  "/src/unstable-core-do-not-import.ts"
16
16
  ],
17
- "percent": 13.15,
17
+ "percent": 13.16,
18
18
  "reduction": 0
19
19
  },
20
20
  {
@@ -31,13 +31,13 @@
31
31
  "/src/unstable-core-do-not-import.ts",
32
32
  "/src/unstable-core-do-not-import/http/resolveResponse.ts"
33
33
  ],
34
- "percent": 12.89,
34
+ "percent": 12.9,
35
35
  "reduction": 4.31
36
36
  },
37
37
  {
38
38
  "id": "/src/adapters/ws.ts",
39
- "size": 16392,
40
- "origSize": 15346,
39
+ "size": 16286,
40
+ "origSize": 15256,
41
41
  "renderedExports": [
42
42
  "getWSConnectionHandler",
43
43
  "handleKeepAlive",
@@ -47,7 +47,7 @@
47
47
  "dependents": [
48
48
  "/src/adapters/fastify/fastifyTRPCPlugin.ts"
49
49
  ],
50
- "percent": 12.03,
50
+ "percent": 11.96,
51
51
  "reduction": 0
52
52
  },
53
53
  {
@@ -62,7 +62,7 @@
62
62
  "/src/unstable-core-do-not-import.ts",
63
63
  "/src/unstable-core-do-not-import/http/resolveResponse.ts"
64
64
  ],
65
- "percent": 5.64,
65
+ "percent": 5.65,
66
66
  "reduction": 0
67
67
  },
68
68
  {
@@ -80,7 +80,7 @@
80
80
  "/src/unstable-core-do-not-import.ts",
81
81
  "/src/unstable-core-do-not-import/initTRPC.ts"
82
82
  ],
83
- "percent": 4.71,
83
+ "percent": 4.72,
84
84
  "reduction": 40.18
85
85
  },
86
86
  {
@@ -112,7 +112,7 @@
112
112
  "/src/unstable-core-do-not-import.ts",
113
113
  "/src/unstable-core-do-not-import/initTRPC.ts"
114
114
  ],
115
- "percent": 4.25,
115
+ "percent": 4.26,
116
116
  "reduction": 63.05
117
117
  },
118
118
  {
@@ -160,7 +160,7 @@
160
160
  "dependents": [
161
161
  "/src/adapters/next-app-dir.ts"
162
162
  ],
163
- "percent": 2.36,
163
+ "percent": 2.37,
164
164
  "reduction": 22.57
165
165
  },
166
166
  {
@@ -210,7 +210,7 @@
210
210
  "/src/unstable-core-do-not-import/initTRPC.ts",
211
211
  "/src/unstable-core-do-not-import/router.ts"
212
212
  ],
213
- "percent": 2.01,
213
+ "percent": 2.02,
214
214
  "reduction": 45.94
215
215
  },
216
216
  {
@@ -224,7 +224,7 @@
224
224
  "dependents": [
225
225
  "/src/unstable-core-do-not-import.ts"
226
226
  ],
227
- "percent": 1.98,
227
+ "percent": 1.99,
228
228
  "reduction": 40.91
229
229
  },
230
230
  {
@@ -274,7 +274,7 @@
274
274
  "dependents": [
275
275
  "/src/adapters/fetch/index.ts"
276
276
  ],
277
- "percent": 1.65,
277
+ "percent": 1.66,
278
278
  "reduction": 2.17
279
279
  },
280
280
  {
@@ -303,7 +303,7 @@
303
303
  "dependents": [
304
304
  "/src/adapters/next-app-dir/nextAppDirCaller.ts"
305
305
  ],
306
- "percent": 1.5,
306
+ "percent": 1.51,
307
307
  "reduction": 5.79
308
308
  },
309
309
  {
@@ -432,13 +432,13 @@
432
432
  "/src/unstable-core-do-not-import/http/getHTTPStatusCode.ts",
433
433
  "/src/unstable-core-do-not-import/http/parseConnectionParams.ts",
434
434
  "/src/unstable-core-do-not-import/http/resolveResponse.ts",
435
+ "/src/unstable-core-do-not-import/rpc/parseTRPCMessage.ts",
435
436
  "/src/unstable-core-do-not-import/error/TRPCError.ts",
436
437
  "/src/unstable-core-do-not-import/transformer.ts",
437
438
  "/src/unstable-core-do-not-import/middleware.ts",
438
439
  "/src/unstable-core-do-not-import/router.ts",
439
440
  "/src/unstable-core-do-not-import/http/contentType.ts",
440
441
  "/src/unstable-core-do-not-import/procedureBuilder.ts",
441
- "/src/unstable-core-do-not-import/rpc/parseTRPCMessage.ts",
442
442
  "/src/unstable-core-do-not-import/stream/jsonl.ts",
443
443
  "/src/unstable-core-do-not-import/stream/sse.ts"
444
444
  ],
@@ -539,7 +539,7 @@
539
539
  "/src/unstable-core-do-not-import.ts",
540
540
  "/src/unstable-core-do-not-import/stream/sse.ts"
541
541
  ],
542
- "percent": 0.58,
542
+ "percent": 0.59,
543
543
  "reduction": 44.65
544
544
  },
545
545
  {
@@ -739,9 +739,9 @@
739
739
  "reduction": 100
740
740
  },
741
741
  {
742
- "id": "/src/index.ts",
742
+ "id": "/src/rpc.ts",
743
743
  "size": 0,
744
- "origSize": 32,
744
+ "origSize": 36,
745
745
  "renderedExports": [],
746
746
  "removedExports": [],
747
747
  "dependents": [],
@@ -749,22 +749,19 @@
749
749
  "reduction": 100
750
750
  },
751
751
  {
752
- "id": "/src/unstable-core-do-not-import.ts",
752
+ "id": "/src/index.ts",
753
753
  "size": 0,
754
- "origSize": 2306,
754
+ "origSize": 32,
755
755
  "renderedExports": [],
756
756
  "removedExports": [],
757
- "dependents": [
758
- "/src/adapters/ws.ts",
759
- "/src/adapters/next-app-dir/nextAppDirCaller.ts"
760
- ],
757
+ "dependents": [],
761
758
  "percent": 0,
762
759
  "reduction": 100
763
760
  },
764
761
  {
765
- "id": "/src/rpc.ts",
762
+ "id": "/src/shared.ts",
766
763
  "size": 0,
767
- "origSize": 36,
764
+ "origSize": 653,
768
765
  "renderedExports": [],
769
766
  "removedExports": [],
770
767
  "dependents": [],
@@ -772,12 +769,15 @@
772
769
  "reduction": 100
773
770
  },
774
771
  {
775
- "id": "/src/shared.ts",
772
+ "id": "/src/unstable-core-do-not-import.ts",
776
773
  "size": 0,
777
- "origSize": 653,
774
+ "origSize": 2306,
778
775
  "renderedExports": [],
779
776
  "removedExports": [],
780
- "dependents": [],
777
+ "dependents": [
778
+ "/src/adapters/ws.ts",
779
+ "/src/adapters/next-app-dir/nextAppDirCaller.ts"
780
+ ],
781
781
  "percent": 0,
782
782
  "reduction": 100
783
783
  },
@@ -831,8 +831,8 @@
831
831
  "removedExports": [],
832
832
  "dependents": [
833
833
  "/src/adapters/express.ts",
834
- "/src/adapters/standalone.ts",
835
834
  "/src/adapters/next.ts",
835
+ "/src/adapters/standalone.ts",
836
836
  "/src/adapters/fastify/fastifyRequestHandler.ts"
837
837
  ],
838
838
  "percent": 0,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trpc/server",
3
- "version": "11.0.0-rc.530+d1e8f33f6",
3
+ "version": "11.0.0-rc.532+476db0d8c",
4
4
  "description": "The tRPC server library",
5
5
  "author": "KATT",
6
6
  "license": "MIT",
@@ -149,5 +149,5 @@
149
149
  "funding": [
150
150
  "https://trpc.io/sponsor"
151
151
  ],
152
- "gitHead": "d1e8f33f6bc6104003f4a0d4030c6cf306e6dec2"
152
+ "gitHead": "476db0d8c42cd886182b117f6fd1ecbe0ae7cbd8"
153
153
  }
@@ -388,13 +388,23 @@ export function getWSConnectionHandler<TRouter extends AnyRouter>(
388
388
  });
389
389
  }
390
390
  }
391
- client.on('message', async (message) => {
391
+ client.on('message', async (rawData) => {
392
+ const msgStr = rawData.toString();
393
+ if (msgStr === 'PONG') {
394
+ return;
395
+ }
396
+ if (msgStr === 'PING') {
397
+ if (!opts.dangerouslyDisablePong) {
398
+ client.send('PONG');
399
+ }
400
+ return;
401
+ }
392
402
  if (ctxPromise === unsetContextPromiseSymbol) {
393
403
  // If the ctxPromise wasn't created immediately, we're expecting the first message to be a TRPCConnectionParamsMessage
394
404
  ctxPromise = createCtxPromise(() => {
395
405
  let msg;
396
406
  try {
397
- msg = JSON.parse(message.toString()) as TRPCConnectionParamsMessage;
407
+ msg = JSON.parse(msgStr) as TRPCConnectionParamsMessage;
398
408
 
399
409
  if (!isObject(msg)) {
400
410
  throw new Error('Message was not an object');
@@ -414,15 +424,7 @@ export function getWSConnectionHandler<TRouter extends AnyRouter>(
414
424
  return;
415
425
  }
416
426
  try {
417
- const str = message.toString();
418
- if (str === 'PING') {
419
- if (!opts.dangerouslyDisablePong) {
420
- client.send('PONG');
421
- }
422
-
423
- return;
424
- }
425
- const msgJSON: unknown = JSON.parse(str);
427
+ const msgJSON: unknown = JSON.parse(msgStr);
426
428
  const msgs: unknown[] = Array.isArray(msgJSON) ? msgJSON : [msgJSON];
427
429
  const promises = msgs
428
430
  .map((raw) => parseTRPCMessage(raw, transformer))
@@ -485,27 +487,37 @@ export function handleKeepAlive(
485
487
  pingMs = 30_000,
486
488
  pongWaitMs = 5_000,
487
489
  ) {
488
- let heartbeatTimeout: NodeJS.Timeout | undefined;
489
- const heartbeatInterval = setInterval(() => {
490
- if (client.readyState !== WEBSOCKET_OPEN) {
491
- return;
492
- }
493
- // First we send a ping message and wait for a pong
494
- client.ping();
495
- // We set a timeout to close the connection if the pong is not received
496
- heartbeatTimeout = setTimeout(() => {
497
- client.terminate();
498
- clearInterval(heartbeatInterval);
499
- }, pongWaitMs);
500
- }, pingMs).unref();
501
- // When we receive a pong message, we clear the timeout
502
- client.on('pong', () => {
503
- heartbeatTimeout && clearTimeout(heartbeatTimeout);
504
- });
505
- // If the connection is closed, we clear the interval
490
+ let timeout: NodeJS.Timeout | undefined = undefined;
491
+ let ping: NodeJS.Timeout | undefined = undefined;
492
+
493
+ const schedulePing = () => {
494
+ const scheduleTimeout = () => {
495
+ timeout = setTimeout(() => {
496
+ client.terminate();
497
+ }, pongWaitMs);
498
+ };
499
+ ping = setTimeout(() => {
500
+ client.send('PING');
501
+
502
+ scheduleTimeout();
503
+ }, pingMs);
504
+ };
505
+
506
+ const onMessage = () => {
507
+ clearTimeout(ping);
508
+ clearTimeout(timeout);
509
+
510
+ schedulePing();
511
+ };
512
+
513
+ client.on('message', onMessage);
514
+
506
515
  client.on('close', () => {
507
- clearInterval(heartbeatInterval);
516
+ clearTimeout(ping);
517
+ clearTimeout(timeout);
508
518
  });
519
+
520
+ schedulePing();
509
521
  }
510
522
 
511
523
  export function applyWSSHandler<TRouter extends AnyRouter>(