@trpc/server 10.41.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.
- package/dist/adapters/fastify/index.js +1 -0
- package/dist/adapters/fastify/index.mjs +1 -0
- package/dist/adapters/node-http/content-type/form-data/index.d.ts +8 -4
- package/dist/adapters/node-http/content-type/form-data/index.d.ts.map +1 -1
- package/dist/adapters/node-http/content-type/form-data/index.js +127 -66
- package/dist/adapters/node-http/content-type/form-data/index.mjs +103 -63
- package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.d.ts.map +1 -1
- package/dist/adapters/node-http/content-type/form-data/uploadHandler.d.ts +5 -1
- package/dist/adapters/node-http/content-type/form-data/uploadHandler.d.ts.map +1 -1
- package/dist/adapters/ws.d.ts +0 -3
- package/dist/adapters/ws.d.ts.map +1 -1
- package/dist/adapters/ws.js +3 -55
- package/dist/adapters/ws.mjs +4 -55
- package/dist/core/internals/procedureBuilder.d.ts +1 -1
- package/dist/core/internals/procedureBuilder.d.ts.map +1 -1
- package/dist/core/middleware.d.ts +3 -3
- package/dist/core/middleware.d.ts.map +1 -1
- package/dist/parseTRPCMessage-1377f305.js +56 -0
- package/dist/parseTRPCMessage-95955211.js +63 -0
- package/dist/parseTRPCMessage-a0f17853.mjs +54 -0
- package/dist/rpc/index.d.ts +1 -0
- package/dist/rpc/index.d.ts.map +1 -1
- package/dist/rpc/index.js +2 -0
- package/dist/rpc/index.mjs +1 -0
- package/dist/rpc/parseTRPCMessage.d.ts +5 -0
- package/dist/rpc/parseTRPCMessage.d.ts.map +1 -0
- package/package.json +2 -2
- package/src/adapters/node-http/content-type/form-data/index.ts +71 -16
- package/src/adapters/node-http/content-type/form-data/memoryUploadHandler.ts +1 -5
- package/src/adapters/node-http/content-type/form-data/uploadHandler.ts +9 -7
- package/src/adapters/ws.ts +3 -79
- package/src/core/internals/procedureBuilder.ts +3 -1
- package/src/core/middleware.ts +7 -2
- package/src/rpc/index.ts +1 -0
- package/src/rpc/parseTRPCMessage.ts +84 -0
package/src/adapters/ws.ts
CHANGED
|
@@ -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) =>
|
|
211
|
+
.map((raw) => parseTRPCMessage(raw, transformer))
|
|
288
212
|
.map(handleRequest);
|
|
289
213
|
await Promise.all(promises);
|
|
290
214
|
} catch (cause) {
|
|
@@ -37,7 +37,9 @@ type CreateProcedureReturnInput<
|
|
|
37
37
|
_meta: TPrev['_meta'];
|
|
38
38
|
_ctx_out: Overwrite<TPrev['_ctx_out'], TNext['_ctx_out']>;
|
|
39
39
|
_input_in: FallbackValue<TNext['_input_in'], TPrev['_input_in']>;
|
|
40
|
-
_input_out:
|
|
40
|
+
_input_out: UnsetMarker extends TNext['_input_out']
|
|
41
|
+
? TPrev['_input_out']
|
|
42
|
+
: Overwrite<TPrev['_input_out'], TNext['_input_out']>;
|
|
41
43
|
_output_in: FallbackValue<TNext['_output_in'], TPrev['_output_in']>;
|
|
42
44
|
_output_out: FallbackValue<TNext['_output_out'], TPrev['_output_out']>;
|
|
43
45
|
}>;
|
package/src/core/middleware.ts
CHANGED
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
DefaultValue as FallbackValue,
|
|
8
8
|
MiddlewareMarker,
|
|
9
9
|
Overwrite,
|
|
10
|
+
UnsetMarker,
|
|
10
11
|
} from './internals/utils';
|
|
11
12
|
import { ProcedureParams } from './procedure';
|
|
12
13
|
import { ProcedureType } from './types';
|
|
@@ -64,7 +65,9 @@ export interface MiddlewareBuilder<
|
|
|
64
65
|
_meta: TRoot['_meta'];
|
|
65
66
|
_ctx_out: Overwrite<TRoot['_ctx_out'], TNewParams['_ctx_out']>;
|
|
66
67
|
_input_in: FallbackValue<TRoot['_input_in'], TNewParams['_input_in']>;
|
|
67
|
-
_input_out:
|
|
68
|
+
_input_out: UnsetMarker extends TNewParams['_input_out']
|
|
69
|
+
? TRoot['_input_out']
|
|
70
|
+
: Overwrite<TRoot['_input_out'], TNewParams['_input_out']>;
|
|
68
71
|
_output_in: FallbackValue<TRoot['_output_in'], TNewParams['_output_in']>;
|
|
69
72
|
_output_out: FallbackValue<
|
|
70
73
|
TRoot['_output_out'],
|
|
@@ -102,7 +105,9 @@ type CreateMiddlewareReturnInput<
|
|
|
102
105
|
_meta: TPrev['_meta'];
|
|
103
106
|
_ctx_out: Overwrite<TPrev['_ctx_out'], TNext['_ctx_out']>;
|
|
104
107
|
_input_in: FallbackValue<TNext['_input_in'], TPrev['_input_in']>;
|
|
105
|
-
_input_out:
|
|
108
|
+
_input_out: UnsetMarker extends TNext['_input_out']
|
|
109
|
+
? TPrev['_input_out']
|
|
110
|
+
: Overwrite<TPrev['_input_out'], TNext['_input_out']>;
|
|
106
111
|
_output_in: FallbackValue<TNext['_output_in'], TPrev['_output_in']>;
|
|
107
112
|
_output_out: FallbackValue<TNext['_output_out'], TPrev['_output_out']>;
|
|
108
113
|
}
|
package/src/rpc/index.ts
CHANGED
|
@@ -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
|
+
}
|