@trpc/server 11.0.0-next-beta.303 → 11.0.0-next-beta.304

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":"fastifyTRPCPlugin.d.ts","sourceRoot":"","sources":["../../../src/adapters/fastify/fastifyTRPCPlugin.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAC7E,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,GAAG,CAAC;AAE/C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,KAAK,EAAE,8BAA8B,EAAE,MAAM,cAAc,CAAC;AAKnE,MAAM,WAAW,wBAAwB,CAAC,OAAO,SAAS,SAAS;IACjE,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,WAAW,EAAE,qBAAqB,CAAC,OAAO,EAAE,cAAc,EAAE,YAAY,CAAC,CAAC;CAC3E;AAED,MAAM,MAAM,2BAA2B,GAAG,8BAA8B,CACtE,cAAc,EACd,YAAY,CACb,CAAC;AAEF,wBAAgB,iBAAiB,CAAC,OAAO,SAAS,SAAS,EACzD,OAAO,EAAE,eAAe,EACxB,IAAI,EAAE,wBAAwB,CAAC,OAAO,CAAC,EACvC,IAAI,EAAE,CAAC,GAAG,CAAC,EAAE,KAAK,KAAK,IAAI,QAoC5B"}
1
+ {"version":3,"file":"fastifyTRPCPlugin.d.ts","sourceRoot":"","sources":["../../../src/adapters/fastify/fastifyTRPCPlugin.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAC7E,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,GAAG,CAAC;AAE/C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,KAAK,EAAE,8BAA8B,EAAE,MAAM,cAAc,CAAC;AAKnE,MAAM,WAAW,wBAAwB,CAAC,OAAO,SAAS,SAAS;IACjE,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,WAAW,EAAE,qBAAqB,CAAC,OAAO,EAAE,cAAc,EAAE,YAAY,CAAC,CAAC;CAC3E;AAED,MAAM,MAAM,2BAA2B,GAAG,8BAA8B,CACtE,cAAc,EACd,YAAY,CACb,CAAC;AAEF,wBAAgB,iBAAiB,CAAC,OAAO,SAAS,SAAS,EACzD,OAAO,EAAE,eAAe,EACxB,IAAI,EAAE,wBAAwB,CAAC,OAAO,CAAC,EACvC,IAAI,EAAE,CAAC,GAAG,CAAC,EAAE,KAAK,KAAK,IAAI,QAmC5B"}
@@ -20,7 +20,6 @@ function fastifyTRPCPlugin(fastify, opts, done) {
20
20
  _done(null, body);
21
21
  });
22
22
  let prefix = opts.prefix ?? '';
23
- const websocketPrefix = prefix;
24
23
  // https://github.com/fastify/fastify-plugin/blob/fe079bef6557a83794bf437e14b9b9edb8a74104/plugin.js#L11
25
24
  // @ts-expect-error property 'default' does not exists on type ...
26
25
  if (typeof fastifyTRPCPlugin.default !== 'function') {
@@ -36,15 +35,12 @@ function fastifyTRPCPlugin(fastify, opts, done) {
36
35
  });
37
36
  });
38
37
  if (opts.useWSS) {
39
- ws.applyWSSHandler({
40
- ...opts.trpcOptions,
41
- prefix: websocketPrefix,
42
- wss: fastify.websocketServer
38
+ const onConnection = ws.getWSConnectionHandler({
39
+ ...opts.trpcOptions
43
40
  });
44
- // eslint-disable-next-line @typescript-eslint/no-empty-function
45
41
  fastify.get(prefix ?? '/', {
46
42
  websocket: true
47
- }, ()=>{});
43
+ }, ({ socket }, req)=>onConnection(socket, req.raw));
48
44
  }
49
45
  done();
50
46
  }
@@ -1,4 +1,4 @@
1
- import { applyWSSHandler } from '../ws.mjs';
1
+ import { getWSConnectionHandler } from '../ws.mjs';
2
2
  import { fastifyRequestHandler } from './fastifyRequestHandler.mjs';
3
3
 
4
4
  /**
@@ -18,7 +18,6 @@ function fastifyTRPCPlugin(fastify, opts, done) {
18
18
  _done(null, body);
19
19
  });
20
20
  let prefix = opts.prefix ?? '';
21
- const websocketPrefix = prefix;
22
21
  // https://github.com/fastify/fastify-plugin/blob/fe079bef6557a83794bf437e14b9b9edb8a74104/plugin.js#L11
23
22
  // @ts-expect-error property 'default' does not exists on type ...
24
23
  if (typeof fastifyTRPCPlugin.default !== 'function') {
@@ -34,15 +33,12 @@ function fastifyTRPCPlugin(fastify, opts, done) {
34
33
  });
35
34
  });
36
35
  if (opts.useWSS) {
37
- applyWSSHandler({
38
- ...opts.trpcOptions,
39
- prefix: websocketPrefix,
40
- wss: fastify.websocketServer
36
+ const onConnection = getWSConnectionHandler({
37
+ ...opts.trpcOptions
41
38
  });
42
- // eslint-disable-next-line @typescript-eslint/no-empty-function
43
39
  fastify.get(prefix ?? '/', {
44
40
  websocket: true
45
- }, ()=>{});
41
+ }, ({ socket }, req)=>onConnection(socket, req.raw));
46
42
  }
47
43
  done();
48
44
  }
@@ -15,10 +15,7 @@ export type CreateWSSContextFnOptions = Omit<NodeHTTPCreateContextFnOptions<Inco
15
15
  * @public
16
16
  */
17
17
  export type CreateWSSContextFn<TRouter extends AnyRouter> = (opts: CreateWSSContextFnOptions) => MaybePromise<inferRouterContext<TRouter>>;
18
- /**
19
- * Web socket server handler
20
- */
21
- export type WSSHandlerOptions<TRouter extends AnyRouter> = BaseHandlerOptions<TRouter, IncomingMessage> & (object extends inferRouterContext<TRouter> ? {
18
+ export type WSConnectionHandlerOptions<TRouter extends AnyRouter> = BaseHandlerOptions<TRouter, IncomingMessage> & (object extends inferRouterContext<TRouter> ? {
22
19
  /**
23
20
  * @link https://trpc.io/docs/v11/context
24
21
  **/
@@ -28,11 +25,16 @@ export type WSSHandlerOptions<TRouter extends AnyRouter> = BaseHandlerOptions<TR
28
25
  * @link https://trpc.io/docs/v11/context
29
26
  **/
30
27
  createContext: CreateWSSContextFn<TRouter>;
31
- }) & {
28
+ });
29
+ /**
30
+ * Web socket server handler
31
+ */
32
+ export type WSSHandlerOptions<TRouter extends AnyRouter> = WSConnectionHandlerOptions<TRouter> & {
32
33
  wss: ws.WebSocketServer;
33
34
  process?: NodeJS.Process;
34
35
  prefix?: string;
35
36
  };
37
+ export declare function getWSConnectionHandler<TRouter extends AnyRouter>(opts: WSConnectionHandlerOptions<TRouter>): (client: ws.WebSocket, req: IncomingMessage) => Promise<void>;
36
38
  export declare function applyWSSHandler<TRouter extends AnyRouter>(opts: WSSHandlerOptions<TRouter>): {
37
39
  broadcastReconnectNotification: () => void;
38
40
  };
@@ -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,EAAE,SAAS,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAQrE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAY/D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gCAAgC,CAAC;AACnE,OAAO,KAAK,EAAE,8BAA8B,EAAE,MAAM,aAAa,CAAC;AAQlE;;GAEG;AACH,MAAM,MAAM,yBAAyB,GAAG,IAAI,CAC1C,8BAA8B,CAAC,eAAe,EAAE,EAAE,CAAC,SAAS,CAAC,EAC7D,MAAM,CACP,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;;GAEG;AACH,MAAM,MAAM,iBAAiB,CAAC,OAAO,SAAS,SAAS,IAAI,kBAAkB,CAC3E,OAAO,EACP,eAAe,CAChB,GACC,CAAC,MAAM,SAAS,kBAAkB,CAAC,OAAO,CAAC,GACvC;IACE;;QAEI;IACJ,aAAa,CAAC,EAAE,kBAAkB,CAAC,OAAO,CAAC,CAAC;CAC7C,GACD;IACE;;QAEI;IACJ,aAAa,EAAE,kBAAkB,CAAC,OAAO,CAAC,CAAC;CAC5C,CAAC,GAAG;IACP,GAAG,EAAE,EAAE,CAAC,eAAe,CAAC;IACxB,OAAO,CAAC,EAAE,MAAM,CAAC,OAAO,CAAC;IACzB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEJ,wBAAgB,eAAe,CAAC,OAAO,SAAS,SAAS,EACvD,IAAI,EAAE,iBAAiB,CAAC,OAAO,CAAC;;EA4QjC"}
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,EAAE,SAAS,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAQrE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAY/D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gCAAgC,CAAC;AACnE,OAAO,KAAK,EAAE,8BAA8B,EAAE,MAAM,aAAa,CAAC;AAQlE;;GAEG;AACH,MAAM,MAAM,yBAAyB,GAAG,IAAI,CAC1C,8BAA8B,CAAC,eAAe,EAAE,EAAE,CAAC,SAAS,CAAC,EAC7D,MAAM,CACP,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,CAAC,MAAM,SAAS,kBAAkB,CAAC,OAAO,CAAC,GACvC;IACE;;QAEI;IACJ,aAAa,CAAC,EAAE,kBAAkB,CAAC,OAAO,CAAC,CAAC;CAC7C,GACD;IACE;;QAEI;IACJ,aAAa,EAAE,kBAAkB,CAAC,OAAO,CAAC,CAAC;CAC5C,CAAC,CAAC;AAEX;;GAEG;AACH,MAAM,MAAM,iBAAiB,CAAC,OAAO,SAAS,SAAS,IACrD,0BAA0B,CAAC,OAAO,CAAC,GAAG;IACpC,GAAG,EAAE,EAAE,CAAC,eAAe,CAAC;IACxB,OAAO,CAAC,EAAE,MAAM,CAAC,OAAO,CAAC;IACzB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEJ,wBAAgB,sBAAsB,CAAC,OAAO,SAAS,SAAS,EAC9D,IAAI,EAAE,0BAA0B,CAAC,OAAO,CAAC,YAKnB,YAAY,OAAO,eAAe,mBAoPzD;AAED,wBAAgB,eAAe,CAAC,OAAO,SAAS,SAAS,EACvD,IAAI,EAAE,iBAAiB,CAAC,OAAO,CAAC;;EA2BjC"}
@@ -12,13 +12,10 @@ var observable = require('../observable/observable.js');
12
12
  * Importing ws causes a build error
13
13
  * @link https://github.com/trpc/trpc/pull/5279
14
14
  */ const WEBSOCKET_OPEN = 1; /* ws.WebSocket.OPEN */
15
- function applyWSSHandler(opts) {
16
- const { wss , createContext , router: router$1 , prefix } = opts;
15
+ function getWSConnectionHandler(opts) {
16
+ const { createContext , router: router$1 } = opts;
17
17
  const { transformer: transformer$1 } = router$1._def._config;
18
- wss.on('connection', async (client, req)=>{
19
- if (prefix && !req.url?.startsWith(prefix)) {
20
- return;
21
- }
18
+ return async (client, req)=>{
22
19
  const clientSubscriptions = new Map();
23
20
  function respond(untransformedJSON) {
24
21
  client.send(JSON.stringify(transformer.transformTRPCResponse(router$1._def._config, untransformedJSON)));
@@ -260,6 +257,16 @@ function applyWSSHandler(opts) {
260
257
  }
261
258
  }
262
259
  await createContextAsync();
260
+ };
261
+ }
262
+ function applyWSSHandler(opts) {
263
+ const { wss , prefix } = opts;
264
+ const onConnection = getWSConnectionHandler(opts);
265
+ wss.on('connection', async (client, req)=>{
266
+ if (prefix && !req.url?.startsWith(prefix)) {
267
+ return;
268
+ }
269
+ await onConnection(client, req);
263
270
  });
264
271
  return {
265
272
  broadcastReconnectNotification: ()=>{
@@ -278,3 +285,4 @@ function applyWSSHandler(opts) {
278
285
  }
279
286
 
280
287
  exports.applyWSSHandler = applyWSSHandler;
288
+ exports.getWSConnectionHandler = getWSConnectionHandler;
@@ -10,13 +10,10 @@ import { isObservable } from '../observable/observable.mjs';
10
10
  * Importing ws causes a build error
11
11
  * @link https://github.com/trpc/trpc/pull/5279
12
12
  */ const WEBSOCKET_OPEN = 1; /* ws.WebSocket.OPEN */
13
- function applyWSSHandler(opts) {
14
- const { wss , createContext , router , prefix } = opts;
13
+ function getWSConnectionHandler(opts) {
14
+ const { createContext , router } = opts;
15
15
  const { transformer } = router._def._config;
16
- wss.on('connection', async (client, req)=>{
17
- if (prefix && !req.url?.startsWith(prefix)) {
18
- return;
19
- }
16
+ return async (client, req)=>{
20
17
  const clientSubscriptions = new Map();
21
18
  function respond(untransformedJSON) {
22
19
  client.send(JSON.stringify(transformTRPCResponse(router._def._config, untransformedJSON)));
@@ -258,6 +255,16 @@ function applyWSSHandler(opts) {
258
255
  }
259
256
  }
260
257
  await createContextAsync();
258
+ };
259
+ }
260
+ function applyWSSHandler(opts) {
261
+ const { wss , prefix } = opts;
262
+ const onConnection = getWSConnectionHandler(opts);
263
+ wss.on('connection', async (client, req)=>{
264
+ if (prefix && !req.url?.startsWith(prefix)) {
265
+ return;
266
+ }
267
+ await onConnection(client, req);
261
268
  });
262
269
  return {
263
270
  broadcastReconnectNotification: ()=>{
@@ -275,4 +282,4 @@ function applyWSSHandler(opts) {
275
282
  };
276
283
  }
277
284
 
278
- export { applyWSSHandler };
285
+ export { applyWSSHandler, getWSConnectionHandler };
@@ -1,20 +1,21 @@
1
1
  {
2
- "bundleSize": 93108,
3
- "bundleOrigSize": 148167,
4
- "bundleReduction": 37.16,
2
+ "bundleSize": 93191,
3
+ "bundleOrigSize": 148516,
4
+ "bundleReduction": 37.25,
5
5
  "modules": [
6
6
  {
7
7
  "id": "/src/adapters/ws.ts",
8
- "size": 9970,
9
- "origSize": 9269,
8
+ "size": 10170,
9
+ "origSize": 9698,
10
10
  "renderedExports": [
11
+ "getWSConnectionHandler",
11
12
  "applyWSSHandler"
12
13
  ],
13
14
  "removedExports": [],
14
15
  "dependents": [
15
16
  "/src/adapters/fastify/fastifyTRPCPlugin.ts"
16
17
  ],
17
- "percent": 10.71,
18
+ "percent": 10.91,
18
19
  "reduction": 0
19
20
  },
20
21
  {
@@ -26,7 +27,7 @@
26
27
  ],
27
28
  "removedExports": [],
28
29
  "dependents": [],
29
- "percent": 10.08,
30
+ "percent": 10.07,
30
31
  "reduction": 21.23
31
32
  },
32
33
  {
@@ -45,7 +46,7 @@
45
46
  "/src/unstable-core-do-not-import/http/resolveHTTPResponse.ts",
46
47
  "/src/unstable-core-do-not-import/initTRPC.ts"
47
48
  ],
48
- "percent": 6.52,
49
+ "percent": 6.51,
49
50
  "reduction": 38.58
50
51
  },
51
52
  {
@@ -61,7 +62,7 @@
61
62
  "/src/unstable-core-do-not-import.ts",
62
63
  "/src/unstable-core-do-not-import/initTRPC.ts"
63
64
  ],
64
- "percent": 6.13,
65
+ "percent": 6.12,
65
66
  "reduction": 58.57
66
67
  },
67
68
  {
@@ -77,7 +78,7 @@
77
78
  "dependents": [
78
79
  "/src/adapters/node-http/content-type/form-data/index.ts"
79
80
  ],
80
- "percent": 5.35,
81
+ "percent": 5.34,
81
82
  "reduction": 36.77
82
83
  },
83
84
  {
@@ -91,7 +92,7 @@
91
92
  ],
92
93
  "removedExports": [],
93
94
  "dependents": [],
94
- "percent": 4.75,
95
+ "percent": 4.74,
95
96
  "reduction": 32.71
96
97
  },
97
98
  {
@@ -119,7 +120,7 @@
119
120
  "dependents": [
120
121
  "/src/adapters/fetch/index.ts"
121
122
  ],
122
- "percent": 3.82,
123
+ "percent": 3.81,
123
124
  "reduction": 9.5
124
125
  },
125
126
  {
@@ -190,7 +191,7 @@
190
191
  "/src/unstable-core-do-not-import/router.ts",
191
192
  "/src/unstable-core-do-not-import/initTRPC.ts"
192
193
  ],
193
- "percent": 2.95,
194
+ "percent": 2.94,
194
195
  "reduction": 45.94
195
196
  },
196
197
  {
@@ -289,20 +290,6 @@
289
290
  "percent": 1.76,
290
291
  "reduction": 34.79
291
292
  },
292
- {
293
- "id": "/src/adapters/fastify/fastifyTRPCPlugin.ts",
294
- "size": 1603,
295
- "origSize": 2343,
296
- "renderedExports": [
297
- "fastifyTRPCPlugin"
298
- ],
299
- "removedExports": [],
300
- "dependents": [
301
- "/src/adapters/fastify/index.ts"
302
- ],
303
- "percent": 1.72,
304
- "reduction": 31.58
305
- },
306
293
  {
307
294
  "id": "/src/adapters/next.ts",
308
295
  "size": 1602,
@@ -371,6 +358,20 @@
371
358
  "percent": 1.62,
372
359
  "reduction": 5.58
373
360
  },
361
+ {
362
+ "id": "/src/adapters/fastify/fastifyTRPCPlugin.ts",
363
+ "size": 1486,
364
+ "origSize": 2263,
365
+ "renderedExports": [
366
+ "fastifyTRPCPlugin"
367
+ ],
368
+ "removedExports": [],
369
+ "dependents": [
370
+ "/src/adapters/fastify/index.ts"
371
+ ],
372
+ "percent": 1.59,
373
+ "reduction": 34.33
374
+ },
374
375
  {
375
376
  "id": "/src/adapters/node-http/content-type/form-data/streamSlice.ts",
376
377
  "size": 1393,
@@ -382,7 +383,7 @@
382
383
  "dependents": [
383
384
  "/src/adapters/node-http/content-type/form-data/fileUploadHandler.ts"
384
385
  ],
385
- "percent": 1.5,
386
+ "percent": 1.49,
386
387
  "reduction": 0
387
388
  },
388
389
  {
@@ -413,7 +414,7 @@
413
414
  "/src/unstable-core-do-not-import.ts",
414
415
  "/src/unstable-core-do-not-import/procedureBuilder.ts"
415
416
  ],
416
- "percent": 1.07,
417
+ "percent": 1.06,
417
418
  "reduction": 56.22
418
419
  },
419
420
  {
@@ -435,7 +436,7 @@
435
436
  "/src/unstable-core-do-not-import/rpc/parseTRPCMessage.ts",
436
437
  "/src/unstable-core-do-not-import/procedureBuilder.ts"
437
438
  ],
438
- "percent": 0.88,
439
+ "percent": 0.87,
439
440
  "reduction": 19.78
440
441
  },
441
442
  {
@@ -656,9 +657,9 @@
656
657
  "reduction": 100
657
658
  },
658
659
  {
659
- "id": "/src/shared.ts",
660
+ "id": "/src/unstable-core-do-not-import.ts",
660
661
  "size": 0,
661
- "origSize": 653,
662
+ "origSize": 1552,
662
663
  "renderedExports": [],
663
664
  "removedExports": [],
664
665
  "dependents": [],
@@ -666,9 +667,9 @@
666
667
  "reduction": 100
667
668
  },
668
669
  {
669
- "id": "/src/unstable-core-do-not-import.ts",
670
+ "id": "/src/shared.ts",
670
671
  "size": 0,
671
- "origSize": 1552,
672
+ "origSize": 653,
672
673
  "renderedExports": [],
673
674
  "removedExports": [],
674
675
  "dependents": [],
@@ -714,8 +715,8 @@
714
715
  "renderedExports": [],
715
716
  "removedExports": [],
716
717
  "dependents": [
717
- "/src/adapters/express.ts",
718
718
  "/src/adapters/next.ts",
719
+ "/src/adapters/express.ts",
719
720
  "/src/adapters/standalone.ts"
720
721
  ],
721
722
  "percent": 0,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trpc/server",
3
- "version": "11.0.0-next-beta.303+cdf215127",
3
+ "version": "11.0.0-next-beta.304+1724978f1",
4
4
  "description": "The tRPC server library",
5
5
  "author": "KATT",
6
6
  "license": "MIT",
@@ -157,5 +157,5 @@
157
157
  "funding": [
158
158
  "https://trpc.io/sponsor"
159
159
  ],
160
- "gitHead": "cdf215127a172e497468dec04acb8ea12e840e84"
160
+ "gitHead": "1724978f1aa05be40413cd7ca0859b74d6971b68"
161
161
  }
@@ -14,7 +14,7 @@ import type { FastifyHandlerOptions } from '.';
14
14
  import type { AnyRouter } from '../../@trpc/server';
15
15
  import type { NodeHTTPCreateContextFnOptions } from '../node-http';
16
16
  import type { WSSHandlerOptions } from '../ws';
17
- import { applyWSSHandler } from '../ws';
17
+ import { getWSConnectionHandler } from '../ws';
18
18
  import { fastifyRequestHandler } from './fastifyRequestHandler';
19
19
 
20
20
  export interface FastifyTRPCPluginOptions<TRouter extends AnyRouter> {
@@ -43,7 +43,6 @@ export function fastifyTRPCPlugin<TRouter extends AnyRouter>(
43
43
  );
44
44
 
45
45
  let prefix = opts.prefix ?? '';
46
- const websocketPrefix = prefix;
47
46
 
48
47
  // https://github.com/fastify/fastify-plugin/blob/fe079bef6557a83794bf437e14b9b9edb8a74104/plugin.js#L11
49
48
  // @ts-expect-error property 'default' does not exists on type ...
@@ -57,13 +56,13 @@ export function fastifyTRPCPlugin<TRouter extends AnyRouter>(
57
56
  });
58
57
 
59
58
  if (opts.useWSS) {
60
- applyWSSHandler<TRouter>({
59
+ const onConnection = getWSConnectionHandler<TRouter>({
61
60
  ...(opts.trpcOptions as unknown as WSSHandlerOptions<TRouter>),
62
- prefix: websocketPrefix,
63
- wss: fastify.websocketServer,
64
61
  });
65
- // eslint-disable-next-line @typescript-eslint/no-empty-function
66
- fastify.get(prefix ?? '/', { websocket: true }, () => {});
62
+
63
+ fastify.get(prefix ?? '/', { websocket: true }, ({ socket }, req) =>
64
+ onConnection(socket, req.raw),
65
+ );
67
66
  }
68
67
 
69
68
  done();
@@ -44,42 +44,39 @@ export type CreateWSSContextFn<TRouter extends AnyRouter> = (
44
44
  opts: CreateWSSContextFnOptions,
45
45
  ) => MaybePromise<inferRouterContext<TRouter>>;
46
46
 
47
+ export type WSConnectionHandlerOptions<TRouter extends AnyRouter> =
48
+ BaseHandlerOptions<TRouter, IncomingMessage> &
49
+ (object extends inferRouterContext<TRouter>
50
+ ? {
51
+ /**
52
+ * @link https://trpc.io/docs/v11/context
53
+ **/
54
+ createContext?: CreateWSSContextFn<TRouter>;
55
+ }
56
+ : {
57
+ /**
58
+ * @link https://trpc.io/docs/v11/context
59
+ **/
60
+ createContext: CreateWSSContextFn<TRouter>;
61
+ });
62
+
47
63
  /**
48
64
  * Web socket server handler
49
65
  */
50
- export type WSSHandlerOptions<TRouter extends AnyRouter> = BaseHandlerOptions<
51
- TRouter,
52
- IncomingMessage
53
- > &
54
- (object extends inferRouterContext<TRouter>
55
- ? {
56
- /**
57
- * @link https://trpc.io/docs/v11/context
58
- **/
59
- createContext?: CreateWSSContextFn<TRouter>;
60
- }
61
- : {
62
- /**
63
- * @link https://trpc.io/docs/v11/context
64
- **/
65
- createContext: CreateWSSContextFn<TRouter>;
66
- }) & {
66
+ export type WSSHandlerOptions<TRouter extends AnyRouter> =
67
+ WSConnectionHandlerOptions<TRouter> & {
67
68
  wss: ws.WebSocketServer;
68
69
  process?: NodeJS.Process;
69
70
  prefix?: string;
70
71
  };
71
72
 
72
- export function applyWSSHandler<TRouter extends AnyRouter>(
73
- opts: WSSHandlerOptions<TRouter>,
73
+ export function getWSConnectionHandler<TRouter extends AnyRouter>(
74
+ opts: WSConnectionHandlerOptions<TRouter>,
74
75
  ) {
75
- const { wss, createContext, router, prefix } = opts;
76
-
76
+ const { createContext, router } = opts;
77
77
  const { transformer } = router._def._config;
78
- wss.on('connection', async (client, req) => {
79
- if (prefix && !req.url?.startsWith(prefix)) {
80
- return;
81
- }
82
78
 
79
+ return async (client: ws.WebSocket, req: IncomingMessage) => {
83
80
  const clientSubscriptions = new Map<number | string, Unsubscribable>();
84
81
 
85
82
  function respond(untransformedJSON: TRPCResponseMessage) {
@@ -322,6 +319,21 @@ export function applyWSSHandler<TRouter extends AnyRouter>(
322
319
  }
323
320
  }
324
321
  await createContextAsync();
322
+ };
323
+ }
324
+
325
+ export function applyWSSHandler<TRouter extends AnyRouter>(
326
+ opts: WSSHandlerOptions<TRouter>,
327
+ ) {
328
+ const { wss, prefix } = opts;
329
+
330
+ const onConnection = getWSConnectionHandler(opts);
331
+ wss.on('connection', async (client, req) => {
332
+ if (prefix && !req.url?.startsWith(prefix)) {
333
+ return;
334
+ }
335
+
336
+ await onConnection(client, req);
325
337
  });
326
338
 
327
339
  return {