@trpc/server 10.42.0 → 10.43.0

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.
@@ -13,6 +13,7 @@ require('../../TRPCError-ae7b67e8.js');
13
13
  require('../../transformTRPCResponse-e65f34e9.js');
14
14
  require('../../contentType-90bad7bb.js');
15
15
  require('../../observable-464116ac.js');
16
+ require('../../parseTRPCMessage-1377f305.js');
16
17
 
17
18
  async function fastifyRequestHandler(opts) {
18
19
  const createContext = async function _createContext() {
@@ -9,6 +9,7 @@ import '../../TRPCError-816ff32e.mjs';
9
9
  import '../../transformTRPCResponse-1153b421.mjs';
10
10
  import '../../contentType-93515a46.mjs';
11
11
  import '../../observable-ade1bad8.mjs';
12
+ import '../../parseTRPCMessage-a0f17853.mjs';
12
13
 
13
14
  async function fastifyRequestHandler(opts) {
14
15
  const createContext = async function _createContext() {
@@ -4,10 +4,7 @@ import { IncomingMessage } from 'http';
4
4
  import ws from 'ws';
5
5
  import { AnyRouter } from '../core';
6
6
  import { BaseHandlerOptions } from '../internals/types';
7
- import { TRPCClientOutgoingMessage } from '../rpc';
8
- import { CombinedDataTransformer } from '../transformer';
9
7
  import { NodeHTTPCreateContextFnOptions, NodeHTTPCreateContextOption } from './node-http';
10
- export declare function parseMessage(obj: unknown, transformer: CombinedDataTransformer): TRPCClientOutgoingMessage;
11
8
  /**
12
9
  * Web socket server handler
13
10
  */
@@ -1 +1 @@
1
- {"version":3,"file":"ws.d.ts","sourceRoot":"","sources":["../../src/adapters/ws.ts"],"names":[],"mappings":";;AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,MAAM,CAAC;AACvC,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,EACL,SAAS,EAIV,MAAM,SAAS,CAAC;AAEjB,OAAO,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AAExD,OAAO,EAEL,yBAAyB,EAG1B,MAAM,QAAQ,CAAC;AAGhB,OAAO,EAAE,uBAAuB,EAAE,MAAM,gBAAgB,CAAC;AACzD,OAAO,EACL,8BAA8B,EAC9B,2BAA2B,EAC5B,MAAM,aAAa,CAAC;AAyCrB,wBAAgB,YAAY,CAC1B,GAAG,EAAE,OAAO,EACZ,WAAW,EAAE,uBAAuB,GACnC,yBAAyB,CA2B3B;AAED;;GAEG;AACH,MAAM,MAAM,iBAAiB,CAAC,OAAO,SAAS,SAAS,IAAI,kBAAkB,CAC3E,OAAO,EACP,eAAe,CAChB,GACC,2BAA2B,CAAC,OAAO,EAAE,eAAe,EAAE,EAAE,CAAC,GAAG;IAC1D,GAAG,EAAE,EAAE,CAAC,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC,OAAO,CAAC;CAC1B,CAAC;AAEJ,MAAM,MAAM,yBAAyB,GAAG,8BAA8B,CACpE,eAAe,EACf,EAAE,CACH,CAAC;AAEF,wBAAgB,eAAe,CAAC,OAAO,SAAS,SAAS,EACvD,IAAI,EAAE,iBAAiB,CAAC,OAAO,CAAC;;EAwQjC"}
1
+ {"version":3,"file":"ws.d.ts","sourceRoot":"","sources":["../../src/adapters/ws.ts"],"names":[],"mappings":";;AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,MAAM,CAAC;AACvC,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,EAAE,SAAS,EAAqC,MAAM,SAAS,CAAC;AAEvE,OAAO,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AAWxD,OAAO,EACL,8BAA8B,EAC9B,2BAA2B,EAC5B,MAAM,aAAa,CAAC;AAErB;;GAEG;AACH,MAAM,MAAM,iBAAiB,CAAC,OAAO,SAAS,SAAS,IAAI,kBAAkB,CAC3E,OAAO,EACP,eAAe,CAChB,GACC,2BAA2B,CAAC,OAAO,EAAE,eAAe,EAAE,EAAE,CAAC,GAAG;IAC1D,GAAG,EAAE,EAAE,CAAC,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC,OAAO,CAAC;CAC1B,CAAC;AAEJ,MAAM,MAAM,yBAAyB,GAAG,8BAA8B,CACpE,eAAe,EACf,EAAE,CACH,CAAC;AAEF,wBAAgB,eAAe,CAAC,OAAO,SAAS,SAAS,EACvD,IAAI,EAAE,iBAAiB,CAAC,OAAO,CAAC;;EAwQjC"}
@@ -5,62 +5,11 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var config = require('../config-3ab6b85e.js');
6
6
  var TRPCError = require('../TRPCError-ae7b67e8.js');
7
7
  var observable = require('../observable-464116ac.js');
8
+ require('../codes-87f6824b.js');
9
+ var parseTRPCMessage = require('../parseTRPCMessage-1377f305.js');
8
10
  var transformTRPCResponse = require('../transformTRPCResponse-e65f34e9.js');
9
11
  require('../index-784ff647.js');
10
- require('../codes-87f6824b.js');
11
12
 
12
- /* istanbul ignore next -- @preserve */ function assertIsObject(obj) {
13
- if (typeof obj !== 'object' || Array.isArray(obj) || !obj) {
14
- throw new Error('Not an object');
15
- }
16
- }
17
- /* istanbul ignore next -- @preserve */ function assertIsProcedureType(obj) {
18
- if (obj !== 'query' && obj !== 'subscription' && obj !== 'mutation') {
19
- throw new Error('Invalid procedure type');
20
- }
21
- }
22
- /* istanbul ignore next -- @preserve */ function assertIsRequestId(obj) {
23
- if (obj !== null && typeof obj === 'number' && isNaN(obj) && typeof obj !== 'string') {
24
- throw new Error('Invalid request id');
25
- }
26
- }
27
- /* istanbul ignore next -- @preserve */ function assertIsString(obj) {
28
- if (typeof obj !== 'string') {
29
- throw new Error('Invalid string');
30
- }
31
- }
32
- /* istanbul ignore next -- @preserve */ function assertIsJSONRPC2OrUndefined(obj) {
33
- if (typeof obj !== 'undefined' && obj !== '2.0') {
34
- throw new Error('Must be JSONRPC 2.0');
35
- }
36
- }
37
- function parseMessage(obj, transformer) {
38
- assertIsObject(obj);
39
- const { method , params , id , jsonrpc } = obj;
40
- assertIsRequestId(id);
41
- assertIsJSONRPC2OrUndefined(jsonrpc);
42
- if (method === 'subscription.stop') {
43
- return {
44
- id,
45
- jsonrpc,
46
- method
47
- };
48
- }
49
- assertIsProcedureType(method);
50
- assertIsObject(params);
51
- const { input: rawInput , path } = params;
52
- assertIsString(path);
53
- const input = transformer.input.deserialize(rawInput);
54
- return {
55
- id,
56
- jsonrpc,
57
- method,
58
- params: {
59
- input,
60
- path
61
- }
62
- };
63
- }
64
13
  function applyWSSHandler(opts) {
65
14
  const { wss , createContext , router } = opts;
66
15
  const { transformer } = router._def._config;
@@ -235,7 +184,7 @@ function applyWSSHandler(opts) {
235
184
  const msgs = Array.isArray(msgJSON) ? msgJSON : [
236
185
  msgJSON
237
186
  ];
238
- const promises = msgs.map((raw)=>parseMessage(raw, transformer)).map(handleRequest);
187
+ const promises = msgs.map((raw)=>parseTRPCMessage.parseTRPCMessage(raw, transformer)).map(handleRequest);
239
188
  await Promise.all(promises);
240
189
  } catch (cause) {
241
190
  const error = new TRPCError.TRPCError({
@@ -324,4 +273,3 @@ function applyWSSHandler(opts) {
324
273
  }
325
274
 
326
275
  exports.applyWSSHandler = applyWSSHandler;
327
- exports.parseMessage = parseMessage;
@@ -1,62 +1,11 @@
1
1
  import { b as callProcedure } from '../config-4c0f8e88.mjs';
2
2
  import { T as TRPCError, g as getTRPCErrorFromUnknown } from '../TRPCError-816ff32e.mjs';
3
3
  import { i as isObservable } from '../observable-ade1bad8.mjs';
4
+ import '../codes-c924c3db.mjs';
5
+ import { p as parseTRPCMessage } from '../parseTRPCMessage-a0f17853.mjs';
4
6
  import { g as getErrorShape, t as transformTRPCResponse } from '../transformTRPCResponse-1153b421.mjs';
5
7
  import '../index-f91d720c.mjs';
6
- import '../codes-c924c3db.mjs';
7
8
 
8
- /* istanbul ignore next -- @preserve */ function assertIsObject(obj) {
9
- if (typeof obj !== 'object' || Array.isArray(obj) || !obj) {
10
- throw new Error('Not an object');
11
- }
12
- }
13
- /* istanbul ignore next -- @preserve */ function assertIsProcedureType(obj) {
14
- if (obj !== 'query' && obj !== 'subscription' && obj !== 'mutation') {
15
- throw new Error('Invalid procedure type');
16
- }
17
- }
18
- /* istanbul ignore next -- @preserve */ function assertIsRequestId(obj) {
19
- if (obj !== null && typeof obj === 'number' && isNaN(obj) && typeof obj !== 'string') {
20
- throw new Error('Invalid request id');
21
- }
22
- }
23
- /* istanbul ignore next -- @preserve */ function assertIsString(obj) {
24
- if (typeof obj !== 'string') {
25
- throw new Error('Invalid string');
26
- }
27
- }
28
- /* istanbul ignore next -- @preserve */ function assertIsJSONRPC2OrUndefined(obj) {
29
- if (typeof obj !== 'undefined' && obj !== '2.0') {
30
- throw new Error('Must be JSONRPC 2.0');
31
- }
32
- }
33
- function parseMessage(obj, transformer) {
34
- assertIsObject(obj);
35
- const { method , params , id , jsonrpc } = obj;
36
- assertIsRequestId(id);
37
- assertIsJSONRPC2OrUndefined(jsonrpc);
38
- if (method === 'subscription.stop') {
39
- return {
40
- id,
41
- jsonrpc,
42
- method
43
- };
44
- }
45
- assertIsProcedureType(method);
46
- assertIsObject(params);
47
- const { input: rawInput , path } = params;
48
- assertIsString(path);
49
- const input = transformer.input.deserialize(rawInput);
50
- return {
51
- id,
52
- jsonrpc,
53
- method,
54
- params: {
55
- input,
56
- path
57
- }
58
- };
59
- }
60
9
  function applyWSSHandler(opts) {
61
10
  const { wss , createContext , router } = opts;
62
11
  const { transformer } = router._def._config;
@@ -231,7 +180,7 @@ function applyWSSHandler(opts) {
231
180
  const msgs = Array.isArray(msgJSON) ? msgJSON : [
232
181
  msgJSON
233
182
  ];
234
- const promises = msgs.map((raw)=>parseMessage(raw, transformer)).map(handleRequest);
183
+ const promises = msgs.map((raw)=>parseTRPCMessage(raw, transformer)).map(handleRequest);
235
184
  await Promise.all(promises);
236
185
  } catch (cause) {
237
186
  const error = new TRPCError({
@@ -319,4 +268,4 @@ function applyWSSHandler(opts) {
319
268
  };
320
269
  }
321
270
 
322
- export { applyWSSHandler, parseMessage };
271
+ export { applyWSSHandler };
@@ -0,0 +1,56 @@
1
+ 'use strict';
2
+
3
+ /* istanbul ignore next -- @preserve */ function assertIsObject(obj) {
4
+ if (typeof obj !== 'object' || Array.isArray(obj) || !obj) {
5
+ throw new Error('Not an object');
6
+ }
7
+ }
8
+ /* istanbul ignore next -- @preserve */ function assertIsProcedureType(obj) {
9
+ if (obj !== 'query' && obj !== 'subscription' && obj !== 'mutation') {
10
+ throw new Error('Invalid procedure type');
11
+ }
12
+ }
13
+ /* istanbul ignore next -- @preserve */ function assertIsRequestId(obj) {
14
+ if (obj !== null && typeof obj === 'number' && isNaN(obj) && typeof obj !== 'string') {
15
+ throw new Error('Invalid request id');
16
+ }
17
+ }
18
+ /* istanbul ignore next -- @preserve */ function assertIsString(obj) {
19
+ if (typeof obj !== 'string') {
20
+ throw new Error('Invalid string');
21
+ }
22
+ }
23
+ /* istanbul ignore next -- @preserve */ function assertIsJSONRPC2OrUndefined(obj) {
24
+ if (typeof obj !== 'undefined' && obj !== '2.0') {
25
+ throw new Error('Must be JSONRPC 2.0');
26
+ }
27
+ }
28
+ /** @public */ function parseTRPCMessage(obj, transformer) {
29
+ assertIsObject(obj);
30
+ const { id , jsonrpc , method , params } = obj;
31
+ assertIsRequestId(id);
32
+ assertIsJSONRPC2OrUndefined(jsonrpc);
33
+ if (method === 'subscription.stop') {
34
+ return {
35
+ id,
36
+ jsonrpc,
37
+ method
38
+ };
39
+ }
40
+ assertIsProcedureType(method);
41
+ assertIsObject(params);
42
+ const { input: rawInput , path } = params;
43
+ assertIsString(path);
44
+ const input = transformer.input.deserialize(rawInput);
45
+ return {
46
+ id,
47
+ jsonrpc,
48
+ method,
49
+ params: {
50
+ input,
51
+ path
52
+ }
53
+ };
54
+ }
55
+
56
+ exports.parseTRPCMessage = parseTRPCMessage;
@@ -0,0 +1,63 @@
1
+ /* istanbul ignore next -- @preserve */
2
+ function assertIsObject(obj) {
3
+ if (typeof obj !== 'object' || Array.isArray(obj) || !obj) {
4
+ throw new Error('Not an object');
5
+ }
6
+ }
7
+ /* istanbul ignore next -- @preserve */
8
+ function assertIsProcedureType(obj) {
9
+ if (obj !== 'query' && obj !== 'subscription' && obj !== 'mutation') {
10
+ throw new Error('Invalid procedure type');
11
+ }
12
+ }
13
+ /* istanbul ignore next -- @preserve */
14
+ function assertIsRequestId(obj) {
15
+ if (obj !== null &&
16
+ typeof obj === 'number' &&
17
+ isNaN(obj) &&
18
+ typeof obj !== 'string') {
19
+ throw new Error('Invalid request id');
20
+ }
21
+ }
22
+ /* istanbul ignore next -- @preserve */
23
+ function assertIsString(obj) {
24
+ if (typeof obj !== 'string') {
25
+ throw new Error('Invalid string');
26
+ }
27
+ }
28
+ /* istanbul ignore next -- @preserve */
29
+ function assertIsJSONRPC2OrUndefined(obj) {
30
+ if (typeof obj !== 'undefined' && obj !== '2.0') {
31
+ throw new Error('Must be JSONRPC 2.0');
32
+ }
33
+ }
34
+ /** @public */
35
+ function parseTRPCMessage(obj, transformer) {
36
+ assertIsObject(obj);
37
+ const { id, jsonrpc, method, params } = obj;
38
+ assertIsRequestId(id);
39
+ assertIsJSONRPC2OrUndefined(jsonrpc);
40
+ if (method === 'subscription.stop') {
41
+ return {
42
+ id,
43
+ jsonrpc,
44
+ method,
45
+ };
46
+ }
47
+ assertIsProcedureType(method);
48
+ assertIsObject(params);
49
+ const { input: rawInput, path } = params;
50
+ assertIsString(path);
51
+ const input = transformer.input.deserialize(rawInput);
52
+ return {
53
+ id,
54
+ jsonrpc,
55
+ method,
56
+ params: {
57
+ input,
58
+ path,
59
+ },
60
+ };
61
+ }
62
+
63
+ export { parseTRPCMessage as p };
@@ -0,0 +1,54 @@
1
+ /* istanbul ignore next -- @preserve */ function assertIsObject(obj) {
2
+ if (typeof obj !== 'object' || Array.isArray(obj) || !obj) {
3
+ throw new Error('Not an object');
4
+ }
5
+ }
6
+ /* istanbul ignore next -- @preserve */ function assertIsProcedureType(obj) {
7
+ if (obj !== 'query' && obj !== 'subscription' && obj !== 'mutation') {
8
+ throw new Error('Invalid procedure type');
9
+ }
10
+ }
11
+ /* istanbul ignore next -- @preserve */ function assertIsRequestId(obj) {
12
+ if (obj !== null && typeof obj === 'number' && isNaN(obj) && typeof obj !== 'string') {
13
+ throw new Error('Invalid request id');
14
+ }
15
+ }
16
+ /* istanbul ignore next -- @preserve */ function assertIsString(obj) {
17
+ if (typeof obj !== 'string') {
18
+ throw new Error('Invalid string');
19
+ }
20
+ }
21
+ /* istanbul ignore next -- @preserve */ function assertIsJSONRPC2OrUndefined(obj) {
22
+ if (typeof obj !== 'undefined' && obj !== '2.0') {
23
+ throw new Error('Must be JSONRPC 2.0');
24
+ }
25
+ }
26
+ /** @public */ function parseTRPCMessage(obj, transformer) {
27
+ assertIsObject(obj);
28
+ const { id , jsonrpc , method , params } = obj;
29
+ assertIsRequestId(id);
30
+ assertIsJSONRPC2OrUndefined(jsonrpc);
31
+ if (method === 'subscription.stop') {
32
+ return {
33
+ id,
34
+ jsonrpc,
35
+ method
36
+ };
37
+ }
38
+ assertIsProcedureType(method);
39
+ assertIsObject(params);
40
+ const { input: rawInput , path } = params;
41
+ assertIsString(path);
42
+ const input = transformer.input.deserialize(rawInput);
43
+ return {
44
+ id,
45
+ jsonrpc,
46
+ method,
47
+ params: {
48
+ input,
49
+ path
50
+ }
51
+ };
52
+ }
53
+
54
+ export { parseTRPCMessage as p };
@@ -1,3 +1,4 @@
1
1
  export * from './codes';
2
2
  export * from './envelopes';
3
+ export * from './parseTRPCMessage';
3
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/rpc/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/rpc/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,oBAAoB,CAAC"}
package/dist/rpc/index.js CHANGED
@@ -3,8 +3,10 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var codes = require('../codes-87f6824b.js');
6
+ var parseTRPCMessage = require('../parseTRPCMessage-1377f305.js');
6
7
 
7
8
 
8
9
 
9
10
  exports.TRPC_ERROR_CODES_BY_KEY = codes.TRPC_ERROR_CODES_BY_KEY;
10
11
  exports.TRPC_ERROR_CODES_BY_NUMBER = codes.TRPC_ERROR_CODES_BY_NUMBER;
12
+ exports.parseTRPCMessage = parseTRPCMessage.parseTRPCMessage;
@@ -1 +1,2 @@
1
1
  export { T as TRPC_ERROR_CODES_BY_KEY, a as TRPC_ERROR_CODES_BY_NUMBER } from '../codes-c924c3db.mjs';
2
+ export { p as parseTRPCMessage } from '../parseTRPCMessage-a0f17853.mjs';
@@ -0,0 +1,5 @@
1
+ import type { CombinedDataTransformer } from '../transformer';
2
+ import type { TRPCClientOutgoingMessage } from './envelopes';
3
+ /** @public */
4
+ export declare function parseTRPCMessage(obj: unknown, transformer: CombinedDataTransformer): TRPCClientOutgoingMessage;
5
+ //# sourceMappingURL=parseTRPCMessage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parseTRPCMessage.d.ts","sourceRoot":"","sources":["../../src/rpc/parseTRPCMessage.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,gBAAgB,CAAC;AAC9D,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,aAAa,CAAC;AA8C7D,cAAc;AACd,wBAAgB,gBAAgB,CAC9B,GAAG,EAAE,OAAO,EACZ,WAAW,EAAE,uBAAuB,GACnC,yBAAyB,CA+B3B"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trpc/server",
3
- "version": "10.42.0",
3
+ "version": "10.43.0",
4
4
  "description": "The tRPC server library",
5
5
  "author": "KATT",
6
6
  "license": "MIT",
@@ -173,5 +173,5 @@
173
173
  "funding": [
174
174
  "https://trpc.io/sponsor"
175
175
  ],
176
- "gitHead": "b6853a63b41c979ec0e4f1a318eec2887b7c58bf"
176
+ "gitHead": "0d505fd7415f537bd3e263971686b13a78584167"
177
177
  }
@@ -1,99 +1,23 @@
1
1
  import { IncomingMessage } from 'http';
2
2
  import ws from 'ws';
3
- import {
4
- AnyRouter,
5
- callProcedure,
6
- inferRouterContext,
7
- ProcedureType,
8
- } from '../core';
3
+ import { AnyRouter, callProcedure, inferRouterContext } from '../core';
9
4
  import { getTRPCErrorFromUnknown, TRPCError } from '../error/TRPCError';
10
5
  import { BaseHandlerOptions } from '../internals/types';
11
6
  import { isObservable, Unsubscribable } from '../observable';
12
7
  import {
13
8
  JSONRPC2,
9
+ parseTRPCMessage,
14
10
  TRPCClientOutgoingMessage,
15
11
  TRPCReconnectNotification,
16
12
  TRPCResponseMessage,
17
13
  } from '../rpc';
18
14
  import { getErrorShape } from '../shared/getErrorShape';
19
15
  import { transformTRPCResponse } from '../shared/transformTRPCResponse';
20
- import { CombinedDataTransformer } from '../transformer';
21
16
  import {
22
17
  NodeHTTPCreateContextFnOptions,
23
18
  NodeHTTPCreateContextOption,
24
19
  } from './node-http';
25
20
 
26
- /* istanbul ignore next -- @preserve */
27
- function assertIsObject(obj: unknown): asserts obj is Record<string, unknown> {
28
- if (typeof obj !== 'object' || Array.isArray(obj) || !obj) {
29
- throw new Error('Not an object');
30
- }
31
- }
32
- /* istanbul ignore next -- @preserve */
33
- function assertIsProcedureType(obj: unknown): asserts obj is ProcedureType {
34
- if (obj !== 'query' && obj !== 'subscription' && obj !== 'mutation') {
35
- throw new Error('Invalid procedure type');
36
- }
37
- }
38
- /* istanbul ignore next -- @preserve */
39
- function assertIsRequestId(
40
- obj: unknown,
41
- ): asserts obj is number | string | null {
42
- if (
43
- obj !== null &&
44
- typeof obj === 'number' &&
45
- isNaN(obj) &&
46
- typeof obj !== 'string'
47
- ) {
48
- throw new Error('Invalid request id');
49
- }
50
- }
51
- /* istanbul ignore next -- @preserve */
52
- function assertIsString(obj: unknown): asserts obj is string {
53
- if (typeof obj !== 'string') {
54
- throw new Error('Invalid string');
55
- }
56
- }
57
- /* istanbul ignore next -- @preserve */
58
- function assertIsJSONRPC2OrUndefined(
59
- obj: unknown,
60
- ): asserts obj is '2.0' | undefined {
61
- if (typeof obj !== 'undefined' && obj !== '2.0') {
62
- throw new Error('Must be JSONRPC 2.0');
63
- }
64
- }
65
- export function parseMessage(
66
- obj: unknown,
67
- transformer: CombinedDataTransformer,
68
- ): TRPCClientOutgoingMessage {
69
- assertIsObject(obj);
70
- const { method, params, id, jsonrpc } = obj;
71
- assertIsRequestId(id);
72
- assertIsJSONRPC2OrUndefined(jsonrpc);
73
- if (method === 'subscription.stop') {
74
- return {
75
- id,
76
- jsonrpc,
77
- method,
78
- };
79
- }
80
- assertIsProcedureType(method);
81
- assertIsObject(params);
82
-
83
- const { input: rawInput, path } = params;
84
- assertIsString(path);
85
- const input = transformer.input.deserialize(rawInput);
86
- return {
87
- id,
88
- jsonrpc,
89
- method,
90
- params: {
91
- input,
92
- path,
93
- },
94
- };
95
- }
96
-
97
21
  /**
98
22
  * Web socket server handler
99
23
  */
@@ -284,7 +208,7 @@ export function applyWSSHandler<TRouter extends AnyRouter>(
284
208
  const msgJSON: unknown = JSON.parse(message.toString());
285
209
  const msgs: unknown[] = Array.isArray(msgJSON) ? msgJSON : [msgJSON];
286
210
  const promises = msgs
287
- .map((raw) => parseMessage(raw, transformer))
211
+ .map((raw) => parseTRPCMessage(raw, transformer))
288
212
  .map(handleRequest);
289
213
  await Promise.all(promises);
290
214
  } catch (cause) {
package/src/rpc/index.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './codes';
2
2
  export * from './envelopes';
3
+ export * from './parseTRPCMessage';
@@ -0,0 +1,84 @@
1
+ import type { ProcedureType } from '../core';
2
+ import type { CombinedDataTransformer } from '../transformer';
3
+ import type { TRPCClientOutgoingMessage } from './envelopes';
4
+
5
+ /* istanbul ignore next -- @preserve */
6
+ function assertIsObject(obj: unknown): asserts obj is Record<string, unknown> {
7
+ if (typeof obj !== 'object' || Array.isArray(obj) || !obj) {
8
+ throw new Error('Not an object');
9
+ }
10
+ }
11
+
12
+ /* istanbul ignore next -- @preserve */
13
+ function assertIsProcedureType(obj: unknown): asserts obj is ProcedureType {
14
+ if (obj !== 'query' && obj !== 'subscription' && obj !== 'mutation') {
15
+ throw new Error('Invalid procedure type');
16
+ }
17
+ }
18
+
19
+ /* istanbul ignore next -- @preserve */
20
+ function assertIsRequestId(
21
+ obj: unknown,
22
+ ): asserts obj is number | string | null {
23
+ if (
24
+ obj !== null &&
25
+ typeof obj === 'number' &&
26
+ isNaN(obj) &&
27
+ typeof obj !== 'string'
28
+ ) {
29
+ throw new Error('Invalid request id');
30
+ }
31
+ }
32
+
33
+ /* istanbul ignore next -- @preserve */
34
+ function assertIsString(obj: unknown): asserts obj is string {
35
+ if (typeof obj !== 'string') {
36
+ throw new Error('Invalid string');
37
+ }
38
+ }
39
+
40
+ /* istanbul ignore next -- @preserve */
41
+ function assertIsJSONRPC2OrUndefined(
42
+ obj: unknown,
43
+ ): asserts obj is '2.0' | undefined {
44
+ if (typeof obj !== 'undefined' && obj !== '2.0') {
45
+ throw new Error('Must be JSONRPC 2.0');
46
+ }
47
+ }
48
+
49
+ /** @public */
50
+ export function parseTRPCMessage(
51
+ obj: unknown,
52
+ transformer: CombinedDataTransformer,
53
+ ): TRPCClientOutgoingMessage {
54
+ assertIsObject(obj);
55
+
56
+ const { id, jsonrpc, method, params } = obj;
57
+ assertIsRequestId(id);
58
+ assertIsJSONRPC2OrUndefined(jsonrpc);
59
+
60
+ if (method === 'subscription.stop') {
61
+ return {
62
+ id,
63
+ jsonrpc,
64
+ method,
65
+ };
66
+ }
67
+ assertIsProcedureType(method);
68
+ assertIsObject(params);
69
+ const { input: rawInput, path } = params;
70
+
71
+ assertIsString(path);
72
+
73
+ const input = transformer.input.deserialize(rawInput);
74
+
75
+ return {
76
+ id,
77
+ jsonrpc,
78
+ method,
79
+ params: {
80
+ input,
81
+ path,
82
+ },
83
+ };
84
+ }