@trpc/server 10.0.0-alpha.45 → 10.0.0-alpha.48
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/adapters/aws-lambda/index.js +1 -1
- package/adapters/fastify/index.js +1 -1
- package/adapters/fetch/index.js +1 -1
- package/adapters/node-http/index.js +1 -1
- package/dist/TRPCError-6f61762c.mjs +54 -0
- package/dist/TRPCError-9c7830b4.js +58 -0
- package/dist/adapters/aws-lambda/index.d.ts.map +1 -1
- package/dist/adapters/aws-lambda/index.js +119 -156
- package/dist/adapters/aws-lambda/index.mjs +119 -156
- package/dist/adapters/aws-lambda/utils.d.ts.map +1 -1
- package/dist/adapters/express.d.ts.map +1 -1
- package/dist/adapters/express.js +14 -14
- package/dist/adapters/express.mjs +14 -14
- package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -1
- package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts.map +1 -1
- package/dist/adapters/fastify/index.d.ts.map +1 -1
- package/dist/adapters/fastify/index.js +68 -89
- package/dist/adapters/fastify/index.mjs +68 -89
- package/dist/adapters/fetch/fetchRequestHandler.d.ts.map +1 -1
- package/dist/adapters/fetch/index.d.ts.map +1 -1
- package/dist/adapters/fetch/index.js +45 -57
- package/dist/adapters/fetch/index.mjs +45 -57
- package/dist/adapters/fetch/types.d.ts.map +1 -1
- package/dist/adapters/lambda/index.d.ts.map +1 -1
- package/dist/adapters/next.d.ts.map +1 -1
- package/dist/adapters/next.js +42 -46
- package/dist/adapters/next.mjs +42 -46
- package/dist/adapters/node-http/index.d.ts.map +1 -1
- package/dist/adapters/node-http/index.js +5 -5
- package/dist/adapters/node-http/index.mjs +5 -5
- package/dist/adapters/node-http/internals/getPostBody.d.ts.map +1 -1
- package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
- package/dist/adapters/node-http/types.d.ts.map +1 -1
- package/dist/adapters/standalone.d.ts.map +1 -1
- package/dist/adapters/standalone.js +30 -32
- package/dist/adapters/standalone.mjs +30 -32
- package/dist/adapters/ws.d.ts.map +1 -1
- package/dist/adapters/ws.js +272 -352
- package/dist/adapters/ws.mjs +272 -352
- package/dist/codes-1c24d919.js +47 -0
- package/dist/codes-aaa7d554.mjs +44 -0
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/initTRPC.d.ts.map +1 -1
- package/dist/core/internals/__generated__/mergeRoutersGeneric.d.ts.map +1 -1
- package/dist/core/internals/config.d.ts.map +1 -1
- package/dist/core/internals/getParseFn.d.ts.map +1 -1
- package/dist/core/internals/mergeRouters.d.ts.map +1 -1
- package/dist/core/internals/mergeWithoutOverrides.d.ts.map +1 -1
- package/dist/core/internals/omitPrototype.d.ts.map +1 -1
- package/dist/core/internals/prefixObjectKeys.d.ts.map +1 -1
- package/dist/core/internals/procedureBuilder.d.ts.map +1 -1
- package/dist/core/internals/utils.d.ts.map +1 -1
- package/dist/core/middleware.d.ts.map +1 -1
- package/dist/core/parser.d.ts.map +1 -1
- package/dist/core/procedure.d.ts.map +1 -1
- package/dist/core/router.d.ts.map +1 -1
- package/dist/core/types.d.ts.map +1 -1
- package/dist/deprecated/internals/callProcedure.d.ts.map +1 -1
- package/dist/deprecated/internals/middlewares.d.ts.map +1 -1
- package/dist/deprecated/internals/procedure.d.ts.map +1 -1
- package/dist/deprecated/interop.d.ts.map +1 -1
- package/dist/deprecated/router.d.ts.map +1 -1
- package/dist/error/TRPCError.d.ts.map +1 -1
- package/dist/error/formatter.d.ts.map +1 -1
- package/dist/error/utils.d.ts.map +1 -1
- package/dist/http/index.d.ts.map +1 -1
- package/dist/http/internals/getHTTPStatusCode.d.ts.map +1 -1
- package/dist/http/internals/types.d.ts.map +1 -1
- package/dist/http/resolveHTTPResponse.d.ts.map +1 -1
- package/dist/http/types.d.ts.map +1 -1
- package/dist/index-47817162.mjs +26 -0
- package/dist/index-9d687d33.js +28 -0
- package/dist/index.js +59 -95
- package/dist/index.mjs +60 -96
- package/dist/internals/invert.d.ts.map +1 -1
- package/dist/internals/transformTRPCResponse.d.ts.map +1 -1
- package/dist/internals/types.d.ts.map +1 -1
- package/dist/nodeHTTPRequestHandler-7c2a3037.js +82 -0
- package/dist/nodeHTTPRequestHandler-b37609f7.mjs +80 -0
- package/dist/observable/index.d.ts.map +1 -1
- package/dist/observable/index.js +125 -172
- package/dist/observable/index.mjs +125 -172
- package/dist/observable/internals/identity.d.ts.map +1 -1
- package/dist/observable/internals/observableToPromise.d.ts.map +1 -1
- package/dist/observable/internals/pipe.d.ts.map +1 -1
- package/dist/observable/observable.d.ts.map +1 -1
- package/dist/observable/operators/index.d.ts.map +1 -1
- package/dist/observable/operators/map.d.ts.map +1 -1
- package/dist/observable/operators/share.d.ts.map +1 -1
- package/dist/observable/operators/tap.d.ts.map +1 -1
- package/dist/observable/types.d.ts.map +1 -1
- package/dist/observable-b651bf11.js +84 -0
- package/dist/observable-e129ac9d.mjs +81 -0
- package/dist/resolveHTTPResponse-83703ee8.mjs +251 -0
- package/dist/resolveHTTPResponse-b486996d.js +255 -0
- package/dist/router-8372703e.js +886 -0
- package/dist/router-f21b6d06.mjs +876 -0
- package/dist/rpc/codes.d.ts.map +1 -1
- package/dist/rpc/envelopes.d.ts.map +1 -1
- package/dist/rpc/index.d.ts.map +1 -1
- package/dist/rpc/index.js +3 -7
- package/dist/rpc/index.mjs +1 -1
- package/dist/rpc/internals/invert.d.ts.map +1 -1
- package/dist/shared/createProxy/index.d.ts.map +1 -1
- package/dist/shared/index.d.ts.map +1 -1
- package/dist/shared/index.js +1 -1
- package/dist/shared/index.mjs +1 -1
- package/dist/subscription.js +24 -34
- package/dist/subscription.mjs +24 -34
- package/dist/transformTRPCResponse-11ac69cc.js +27 -0
- package/dist/transformTRPCResponse-c8138d5f.mjs +25 -0
- package/observable/index.js +1 -1
- package/package.json +12 -16
- package/rpc/index.js +1 -1
- package/shared/index.js +1 -1
- package/subscription/index.d.ts +1 -0
- package/subscription/index.js +1 -0
- package/dist/TRPCError-9050a68d.js +0 -69
- package/dist/TRPCError-97212592.mjs +0 -65
- package/dist/adapters/aws-lambda/index.ts.js +0 -1
- package/dist/adapters/express.ts.js +0 -1
- package/dist/adapters/fastify/index.ts.js +0 -1
- package/dist/adapters/fetch/index.ts.js +0 -1
- package/dist/adapters/next.ts.js +0 -1
- package/dist/adapters/node-http/index.ts.js +0 -1
- package/dist/adapters/standalone.ts.js +0 -1
- package/dist/adapters/ws.ts.js +0 -1
- package/dist/envelopes-2ae27c9f.js +0 -85
- package/dist/envelopes-b9f49d4d.mjs +0 -82
- package/dist/index-1a25ccbd.js +0 -33
- package/dist/index-a3bd54c7.mjs +0 -31
- package/dist/index.ts.js +0 -1
- package/dist/nodeHTTPRequestHandler-bee7aacc.js +0 -107
- package/dist/nodeHTTPRequestHandler-f1656eba.mjs +0 -105
- package/dist/observable/index.ts.js +0 -1
- package/dist/observable-341a0506.js +0 -109
- package/dist/observable-cfedfa30.mjs +0 -106
- package/dist/resolveHTTPResponse-455f1e51.mjs +0 -293
- package/dist/resolveHTTPResponse-5d90abc5.js +0 -297
- package/dist/router-4e50a6ac.js +0 -1104
- package/dist/router-a3db8f3a.mjs +0 -1094
- package/dist/rpc/index.ts.js +0 -1
- package/dist/shared/index.ts.js +0 -1
- package/dist/subscription.ts.js +0 -1
- package/dist/transformTRPCResponse-0c261655.js +0 -29
- package/dist/transformTRPCResponse-747b9a68.mjs +0 -27
package/dist/adapters/ws.mjs
CHANGED
|
@@ -1,375 +1,295 @@
|
|
|
1
|
-
import { T as TRPCError, a as getCauseFromUnknown, g as getErrorFromUnknown } from '../TRPCError-
|
|
2
|
-
import { t as transformTRPCResponse } from '../transformTRPCResponse-
|
|
3
|
-
import { i as isObservable } from '../observable-
|
|
1
|
+
import { T as TRPCError, a as getCauseFromUnknown, g as getErrorFromUnknown } from '../TRPCError-6f61762c.mjs';
|
|
2
|
+
import { t as transformTRPCResponse } from '../transformTRPCResponse-c8138d5f.mjs';
|
|
3
|
+
import { i as isObservable } from '../observable-e129ac9d.mjs';
|
|
4
4
|
|
|
5
|
-
/* istanbul ignore next */
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
/* istanbul ignore next */
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
function assertIsProcedureType(obj) {
|
|
15
|
-
if (obj !== 'query' && obj !== 'subscription' && obj !== 'mutation') {
|
|
16
|
-
throw new Error('Invalid procedure type');
|
|
17
|
-
}
|
|
5
|
+
/* istanbul ignore next */ function assertIsObject(obj) {
|
|
6
|
+
if (typeof obj !== 'object' || Array.isArray(obj) || !obj) {
|
|
7
|
+
throw new Error('Not an object');
|
|
8
|
+
}
|
|
18
9
|
}
|
|
19
|
-
/* istanbul ignore next */
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
if (obj !== null && typeof obj === 'number' && isNaN(obj) && typeof obj !== 'string') {
|
|
24
|
-
throw new Error('Invalid request id');
|
|
25
|
-
}
|
|
10
|
+
/* istanbul ignore next */ function assertIsProcedureType(obj) {
|
|
11
|
+
if (obj !== 'query' && obj !== 'subscription' && obj !== 'mutation') {
|
|
12
|
+
throw new Error('Invalid procedure type');
|
|
13
|
+
}
|
|
26
14
|
}
|
|
27
|
-
/* istanbul ignore next */
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
if (typeof obj !== 'string') {
|
|
32
|
-
throw new Error('Invalid string');
|
|
33
|
-
}
|
|
15
|
+
/* istanbul ignore next */ function assertIsRequestId(obj) {
|
|
16
|
+
if (obj !== null && typeof obj === 'number' && isNaN(obj) && typeof obj !== 'string') {
|
|
17
|
+
throw new Error('Invalid request id');
|
|
18
|
+
}
|
|
34
19
|
}
|
|
35
|
-
/* istanbul ignore next */
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
if (typeof obj !== 'undefined' && obj !== '2.0') {
|
|
40
|
-
throw new Error('Must be JSONRPC 2.0');
|
|
41
|
-
}
|
|
20
|
+
/* istanbul ignore next */ function assertIsString(obj) {
|
|
21
|
+
if (typeof obj !== 'string') {
|
|
22
|
+
throw new Error('Invalid string');
|
|
23
|
+
}
|
|
42
24
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const {
|
|
47
|
-
method,
|
|
48
|
-
params,
|
|
49
|
-
id,
|
|
50
|
-
jsonrpc
|
|
51
|
-
} = obj;
|
|
52
|
-
assertIsRequestId(id);
|
|
53
|
-
assertIsJSONRPC2OrUndefined(jsonrpc);
|
|
54
|
-
|
|
55
|
-
if (method === 'subscription.stop') {
|
|
56
|
-
return {
|
|
57
|
-
id,
|
|
58
|
-
jsonrpc,
|
|
59
|
-
method
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
assertIsProcedureType(method);
|
|
64
|
-
assertIsObject(params);
|
|
65
|
-
const {
|
|
66
|
-
input: rawInput,
|
|
67
|
-
path
|
|
68
|
-
} = params;
|
|
69
|
-
assertIsString(path);
|
|
70
|
-
const input = transformer.input.deserialize(rawInput);
|
|
71
|
-
return {
|
|
72
|
-
id,
|
|
73
|
-
jsonrpc,
|
|
74
|
-
method,
|
|
75
|
-
params: {
|
|
76
|
-
input,
|
|
77
|
-
path
|
|
25
|
+
/* istanbul ignore next */ function assertIsJSONRPC2OrUndefined(obj) {
|
|
26
|
+
if (typeof obj !== 'undefined' && obj !== '2.0') {
|
|
27
|
+
throw new Error('Must be JSONRPC 2.0');
|
|
78
28
|
}
|
|
79
|
-
};
|
|
80
29
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
const {
|
|
93
|
-
transformer
|
|
94
|
-
} = router._def;
|
|
95
|
-
wss.on('connection', async (client, req) => {
|
|
96
|
-
const clientSubscriptions = new Map();
|
|
97
|
-
|
|
98
|
-
function respond(untransformedJSON) {
|
|
99
|
-
client.send(JSON.stringify(transformTRPCResponse(router, untransformedJSON)));
|
|
30
|
+
function parseMessage(obj, transformer) {
|
|
31
|
+
assertIsObject(obj);
|
|
32
|
+
const { method , params , id , jsonrpc } = obj;
|
|
33
|
+
assertIsRequestId(id);
|
|
34
|
+
assertIsJSONRPC2OrUndefined(jsonrpc);
|
|
35
|
+
if (method === 'subscription.stop') {
|
|
36
|
+
return {
|
|
37
|
+
id,
|
|
38
|
+
jsonrpc,
|
|
39
|
+
method
|
|
40
|
+
};
|
|
100
41
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
respond({
|
|
42
|
+
assertIsProcedureType(method);
|
|
43
|
+
assertIsObject(params);
|
|
44
|
+
const { input: rawInput , path } = params;
|
|
45
|
+
assertIsString(path);
|
|
46
|
+
const input = transformer.input.deserialize(rawInput);
|
|
47
|
+
return {
|
|
108
48
|
id,
|
|
109
49
|
jsonrpc,
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const ctxPromise = createContext === null || createContext === void 0 ? void 0 : createContext({
|
|
117
|
-
req,
|
|
118
|
-
res: client
|
|
119
|
-
});
|
|
120
|
-
let ctx = undefined;
|
|
121
|
-
|
|
122
|
-
async function handleRequest(msg) {
|
|
123
|
-
const {
|
|
124
|
-
id,
|
|
125
|
-
jsonrpc
|
|
126
|
-
} = msg;
|
|
127
|
-
/* istanbul ignore next */
|
|
128
|
-
|
|
129
|
-
if (id === null) {
|
|
130
|
-
throw new TRPCError({
|
|
131
|
-
code: 'BAD_REQUEST',
|
|
132
|
-
message: '`id` is required'
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
if (msg.method === 'subscription.stop') {
|
|
137
|
-
const sub = clientSubscriptions.get(id);
|
|
138
|
-
|
|
139
|
-
if (sub) {
|
|
140
|
-
stopSubscription(sub, {
|
|
141
|
-
id,
|
|
142
|
-
jsonrpc
|
|
143
|
-
});
|
|
50
|
+
method,
|
|
51
|
+
params: {
|
|
52
|
+
input,
|
|
53
|
+
path
|
|
144
54
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
const type = msg.method;
|
|
155
|
-
|
|
156
|
-
try {
|
|
157
|
-
await ctxPromise; // asserts context has been set
|
|
158
|
-
|
|
159
|
-
const caller = router.createCaller(ctx);
|
|
160
|
-
const result = await caller[type](path, input);
|
|
161
|
-
|
|
162
|
-
if (type === 'subscription') {
|
|
163
|
-
if (!isObservable(result)) {
|
|
164
|
-
throw new TRPCError({
|
|
165
|
-
message: `Subscription ${path} did not return an observable`,
|
|
166
|
-
code: 'INTERNAL_SERVER_ERROR'
|
|
167
|
-
});
|
|
168
|
-
}
|
|
169
|
-
} else {
|
|
170
|
-
// send the value as data if the method is not a subscription
|
|
171
|
-
respond({
|
|
172
|
-
id,
|
|
173
|
-
jsonrpc,
|
|
174
|
-
result: {
|
|
175
|
-
type: 'data',
|
|
176
|
-
data: result
|
|
177
|
-
}
|
|
178
|
-
});
|
|
179
|
-
return;
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
function applyWSSHandler(opts) {
|
|
58
|
+
const { wss , createContext , router } = opts;
|
|
59
|
+
const { transformer } = router._def;
|
|
60
|
+
wss.on('connection', async (client, req)=>{
|
|
61
|
+
const clientSubscriptions = new Map();
|
|
62
|
+
function respond(untransformedJSON) {
|
|
63
|
+
client.send(JSON.stringify(transformTRPCResponse(router, untransformedJSON)));
|
|
180
64
|
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
const sub = observable.subscribe({
|
|
184
|
-
next(data) {
|
|
185
|
-
respond({
|
|
186
|
-
id,
|
|
187
|
-
jsonrpc,
|
|
188
|
-
result: {
|
|
189
|
-
type: 'data',
|
|
190
|
-
data
|
|
191
|
-
}
|
|
192
|
-
});
|
|
193
|
-
},
|
|
194
|
-
|
|
195
|
-
error(err) {
|
|
196
|
-
var _opts$onError;
|
|
197
|
-
|
|
198
|
-
const error = getErrorFromUnknown(err);
|
|
199
|
-
(_opts$onError = opts.onError) === null || _opts$onError === void 0 ? void 0 : _opts$onError.call(opts, {
|
|
200
|
-
error,
|
|
201
|
-
path,
|
|
202
|
-
type,
|
|
203
|
-
ctx,
|
|
204
|
-
req,
|
|
205
|
-
input
|
|
206
|
-
});
|
|
207
|
-
respond({
|
|
208
|
-
id,
|
|
209
|
-
jsonrpc,
|
|
210
|
-
error: router.getErrorShape({
|
|
211
|
-
error,
|
|
212
|
-
type,
|
|
213
|
-
path,
|
|
214
|
-
input,
|
|
215
|
-
ctx
|
|
216
|
-
})
|
|
217
|
-
});
|
|
218
|
-
},
|
|
219
|
-
|
|
220
|
-
complete() {
|
|
65
|
+
function stopSubscription(subscription, { id , jsonrpc }) {
|
|
66
|
+
subscription.unsubscribe();
|
|
221
67
|
respond({
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
68
|
+
id,
|
|
69
|
+
jsonrpc,
|
|
70
|
+
result: {
|
|
71
|
+
type: 'stopped'
|
|
72
|
+
}
|
|
227
73
|
});
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
});
|
|
231
|
-
/* istanbul ignore next */
|
|
232
|
-
|
|
233
|
-
if (client.readyState !== client.OPEN) {
|
|
234
|
-
// if the client got disconnected whilst initializing the subscription
|
|
235
|
-
// no need to send stopped message if the client is disconnected
|
|
236
|
-
sub.unsubscribe();
|
|
237
|
-
return;
|
|
238
|
-
}
|
|
239
|
-
/* istanbul ignore next */
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
if (clientSubscriptions.has(id)) {
|
|
243
|
-
// duplicate request ids for client
|
|
244
|
-
stopSubscription(sub, {
|
|
245
|
-
id,
|
|
246
|
-
jsonrpc
|
|
247
|
-
});
|
|
248
|
-
throw new TRPCError({
|
|
249
|
-
message: `Duplicate id ${id}`,
|
|
250
|
-
code: 'BAD_REQUEST'
|
|
251
|
-
});
|
|
252
74
|
}
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
id,
|
|
257
|
-
jsonrpc,
|
|
258
|
-
result: {
|
|
259
|
-
type: 'started'
|
|
260
|
-
}
|
|
261
|
-
});
|
|
262
|
-
} catch (cause)
|
|
263
|
-
/* istanbul ignore next */
|
|
264
|
-
{
|
|
265
|
-
var _opts$onError2;
|
|
266
|
-
|
|
267
|
-
// procedure threw an error
|
|
268
|
-
const error = getErrorFromUnknown(cause);
|
|
269
|
-
(_opts$onError2 = opts.onError) === null || _opts$onError2 === void 0 ? void 0 : _opts$onError2.call(opts, {
|
|
270
|
-
error,
|
|
271
|
-
path,
|
|
272
|
-
type,
|
|
273
|
-
ctx,
|
|
274
|
-
req,
|
|
275
|
-
input
|
|
276
|
-
});
|
|
277
|
-
respond({
|
|
278
|
-
id,
|
|
279
|
-
jsonrpc,
|
|
280
|
-
error: router.getErrorShape({
|
|
281
|
-
error,
|
|
282
|
-
type,
|
|
283
|
-
path,
|
|
284
|
-
input,
|
|
285
|
-
ctx
|
|
286
|
-
})
|
|
75
|
+
const ctxPromise = createContext?.({
|
|
76
|
+
req,
|
|
77
|
+
res: client
|
|
287
78
|
});
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
79
|
+
let ctx = undefined;
|
|
80
|
+
async function handleRequest(msg) {
|
|
81
|
+
const { id , jsonrpc } = msg;
|
|
82
|
+
/* istanbul ignore next */ if (id === null) {
|
|
83
|
+
throw new TRPCError({
|
|
84
|
+
code: 'BAD_REQUEST',
|
|
85
|
+
message: '`id` is required'
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
if (msg.method === 'subscription.stop') {
|
|
89
|
+
const sub = clientSubscriptions.get(id);
|
|
90
|
+
if (sub) {
|
|
91
|
+
stopSubscription(sub, {
|
|
92
|
+
id,
|
|
93
|
+
jsonrpc
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
clientSubscriptions.delete(id);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
const { path , input } = msg.params;
|
|
100
|
+
const type = msg.method;
|
|
101
|
+
try {
|
|
102
|
+
await ctxPromise; // asserts context has been set
|
|
103
|
+
const caller = router.createCaller(ctx);
|
|
104
|
+
const result = await caller[type](path, input);
|
|
105
|
+
if (type === 'subscription') {
|
|
106
|
+
if (!isObservable(result)) {
|
|
107
|
+
throw new TRPCError({
|
|
108
|
+
message: `Subscription ${path} did not return an observable`,
|
|
109
|
+
code: 'INTERNAL_SERVER_ERROR'
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
} else {
|
|
113
|
+
// send the value as data if the method is not a subscription
|
|
114
|
+
respond({
|
|
115
|
+
id,
|
|
116
|
+
jsonrpc,
|
|
117
|
+
result: {
|
|
118
|
+
type: 'data',
|
|
119
|
+
data: result
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
const observable = result;
|
|
125
|
+
const sub1 = observable.subscribe({
|
|
126
|
+
next (data) {
|
|
127
|
+
respond({
|
|
128
|
+
id,
|
|
129
|
+
jsonrpc,
|
|
130
|
+
result: {
|
|
131
|
+
type: 'data',
|
|
132
|
+
data
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
},
|
|
136
|
+
error (err) {
|
|
137
|
+
const error = getErrorFromUnknown(err);
|
|
138
|
+
opts.onError?.({
|
|
139
|
+
error,
|
|
140
|
+
path,
|
|
141
|
+
type,
|
|
142
|
+
ctx,
|
|
143
|
+
req,
|
|
144
|
+
input
|
|
145
|
+
});
|
|
146
|
+
respond({
|
|
147
|
+
id,
|
|
148
|
+
jsonrpc,
|
|
149
|
+
error: router.getErrorShape({
|
|
150
|
+
error,
|
|
151
|
+
type,
|
|
152
|
+
path,
|
|
153
|
+
input,
|
|
154
|
+
ctx
|
|
155
|
+
})
|
|
156
|
+
});
|
|
157
|
+
},
|
|
158
|
+
complete () {
|
|
159
|
+
respond({
|
|
160
|
+
id,
|
|
161
|
+
jsonrpc,
|
|
162
|
+
result: {
|
|
163
|
+
type: 'stopped'
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
/* istanbul ignore next */ if (client.readyState !== client.OPEN) {
|
|
169
|
+
// if the client got disconnected whilst initializing the subscription
|
|
170
|
+
// no need to send stopped message if the client is disconnected
|
|
171
|
+
sub1.unsubscribe();
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
/* istanbul ignore next */ if (clientSubscriptions.has(id)) {
|
|
175
|
+
// duplicate request ids for client
|
|
176
|
+
stopSubscription(sub1, {
|
|
177
|
+
id,
|
|
178
|
+
jsonrpc
|
|
179
|
+
});
|
|
180
|
+
throw new TRPCError({
|
|
181
|
+
message: `Duplicate id ${id}`,
|
|
182
|
+
code: 'BAD_REQUEST'
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
clientSubscriptions.set(id, sub1);
|
|
186
|
+
respond({
|
|
187
|
+
id,
|
|
188
|
+
jsonrpc,
|
|
189
|
+
result: {
|
|
190
|
+
type: 'started'
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
} catch (cause) /* istanbul ignore next */ {
|
|
194
|
+
// procedure threw an error
|
|
195
|
+
const error = getErrorFromUnknown(cause);
|
|
196
|
+
opts.onError?.({
|
|
197
|
+
error,
|
|
198
|
+
path,
|
|
199
|
+
type,
|
|
200
|
+
ctx,
|
|
201
|
+
req,
|
|
202
|
+
input
|
|
203
|
+
});
|
|
204
|
+
respond({
|
|
205
|
+
id,
|
|
206
|
+
jsonrpc,
|
|
207
|
+
error: router.getErrorShape({
|
|
208
|
+
error,
|
|
209
|
+
type,
|
|
210
|
+
path,
|
|
211
|
+
input,
|
|
212
|
+
ctx
|
|
213
|
+
})
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
client.on('message', async (message)=>{
|
|
218
|
+
try {
|
|
219
|
+
const msgJSON = JSON.parse(message.toString());
|
|
220
|
+
const msgs = Array.isArray(msgJSON) ? msgJSON : [
|
|
221
|
+
msgJSON
|
|
222
|
+
];
|
|
223
|
+
const promises = msgs.map((raw)=>parseMessage(raw, transformer)).map(handleRequest);
|
|
224
|
+
await Promise.all(promises);
|
|
225
|
+
} catch (cause) {
|
|
226
|
+
const error = new TRPCError({
|
|
227
|
+
code: 'PARSE_ERROR',
|
|
228
|
+
cause: getCauseFromUnknown(cause)
|
|
229
|
+
});
|
|
230
|
+
respond({
|
|
231
|
+
id: null,
|
|
232
|
+
error: router.getErrorShape({
|
|
233
|
+
error,
|
|
234
|
+
type: 'unknown',
|
|
235
|
+
path: undefined,
|
|
236
|
+
input: undefined,
|
|
237
|
+
ctx: undefined
|
|
238
|
+
})
|
|
239
|
+
});
|
|
240
|
+
}
|
|
301
241
|
});
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
path: undefined,
|
|
308
|
-
input: undefined,
|
|
309
|
-
ctx: undefined
|
|
310
|
-
})
|
|
242
|
+
client.once('close', ()=>{
|
|
243
|
+
for (const sub of clientSubscriptions.values()){
|
|
244
|
+
sub.unsubscribe();
|
|
245
|
+
}
|
|
246
|
+
clientSubscriptions.clear();
|
|
311
247
|
});
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
248
|
+
async function createContextAsync() {
|
|
249
|
+
try {
|
|
250
|
+
ctx = await ctxPromise;
|
|
251
|
+
} catch (cause) {
|
|
252
|
+
const error = getErrorFromUnknown(cause);
|
|
253
|
+
opts.onError?.({
|
|
254
|
+
error,
|
|
255
|
+
path: undefined,
|
|
256
|
+
type: 'unknown',
|
|
257
|
+
ctx,
|
|
258
|
+
req,
|
|
259
|
+
input: undefined
|
|
260
|
+
});
|
|
261
|
+
respond({
|
|
262
|
+
id: null,
|
|
263
|
+
error: router.getErrorShape({
|
|
264
|
+
error,
|
|
265
|
+
type: 'unknown',
|
|
266
|
+
path: undefined,
|
|
267
|
+
input: undefined,
|
|
268
|
+
ctx
|
|
269
|
+
})
|
|
270
|
+
});
|
|
271
|
+
// close in next tick
|
|
272
|
+
(global.setImmediate ?? global.setTimeout)(()=>{
|
|
273
|
+
client.close();
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
await createContextAsync();
|
|
320
278
|
});
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
ctx,
|
|
334
|
-
req,
|
|
335
|
-
input: undefined
|
|
336
|
-
});
|
|
337
|
-
respond({
|
|
338
|
-
id: null,
|
|
339
|
-
error: router.getErrorShape({
|
|
340
|
-
error,
|
|
341
|
-
type: 'unknown',
|
|
342
|
-
path: undefined,
|
|
343
|
-
input: undefined,
|
|
344
|
-
ctx
|
|
345
|
-
})
|
|
346
|
-
}); // close in next tick
|
|
347
|
-
|
|
348
|
-
((_global$setImmediate = global.setImmediate) !== null && _global$setImmediate !== void 0 ? _global$setImmediate : global.setTimeout)(() => {
|
|
349
|
-
client.close();
|
|
350
|
-
});
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
await createContextAsync();
|
|
355
|
-
});
|
|
356
|
-
return {
|
|
357
|
-
broadcastReconnectNotification: () => {
|
|
358
|
-
const response = {
|
|
359
|
-
id: null,
|
|
360
|
-
method: 'reconnect'
|
|
361
|
-
};
|
|
362
|
-
const data = JSON.stringify(response);
|
|
363
|
-
|
|
364
|
-
for (const client of wss.clients) {
|
|
365
|
-
if (client.readyState === 1
|
|
366
|
-
/* ws.OPEN */
|
|
367
|
-
) {
|
|
368
|
-
client.send(data);
|
|
279
|
+
return {
|
|
280
|
+
broadcastReconnectNotification: ()=>{
|
|
281
|
+
const response = {
|
|
282
|
+
id: null,
|
|
283
|
+
method: 'reconnect'
|
|
284
|
+
};
|
|
285
|
+
const data = JSON.stringify(response);
|
|
286
|
+
for (const client of wss.clients){
|
|
287
|
+
if (client.readyState === 1 /* ws.OPEN */ ) {
|
|
288
|
+
client.send(data);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
369
291
|
}
|
|
370
|
-
|
|
371
|
-
}
|
|
372
|
-
};
|
|
292
|
+
};
|
|
373
293
|
}
|
|
374
294
|
|
|
375
295
|
export { applyWSSHandler };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @internal
|
|
5
|
+
*/ /**
|
|
6
|
+
* @internal
|
|
7
|
+
*/ function invert(obj) {
|
|
8
|
+
const newObj = Object.create(null);
|
|
9
|
+
for(const key in obj){
|
|
10
|
+
const v = obj[key];
|
|
11
|
+
newObj[v] = key;
|
|
12
|
+
}
|
|
13
|
+
return newObj;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// reference: https://www.jsonrpc.org/specification
|
|
17
|
+
/**
|
|
18
|
+
* JSON-RPC 2.0 Error codes
|
|
19
|
+
*
|
|
20
|
+
* `-32000` to `-32099` are reserved for implementation-defined server-errors.
|
|
21
|
+
* For tRPC we're copying the last digits of HTTP 4XX errors.
|
|
22
|
+
*/ const TRPC_ERROR_CODES_BY_KEY = {
|
|
23
|
+
/**
|
|
24
|
+
* Invalid JSON was received by the server.
|
|
25
|
+
* An error occurred on the server while parsing the JSON text.
|
|
26
|
+
*/ PARSE_ERROR: -32700,
|
|
27
|
+
/**
|
|
28
|
+
* The JSON sent is not a valid Request object.
|
|
29
|
+
*/ BAD_REQUEST: -32600,
|
|
30
|
+
/**
|
|
31
|
+
* Internal JSON-RPC error.
|
|
32
|
+
*/ INTERNAL_SERVER_ERROR: -32603,
|
|
33
|
+
// Implementation specific errors
|
|
34
|
+
UNAUTHORIZED: -32001,
|
|
35
|
+
FORBIDDEN: -32003,
|
|
36
|
+
NOT_FOUND: -32004,
|
|
37
|
+
METHOD_NOT_SUPPORTED: -32005,
|
|
38
|
+
TIMEOUT: -32008,
|
|
39
|
+
CONFLICT: -32009,
|
|
40
|
+
PRECONDITION_FAILED: -32012,
|
|
41
|
+
PAYLOAD_TOO_LARGE: -32013,
|
|
42
|
+
CLIENT_CLOSED_REQUEST: -32099
|
|
43
|
+
};
|
|
44
|
+
const TRPC_ERROR_CODES_BY_NUMBER = invert(TRPC_ERROR_CODES_BY_KEY);
|
|
45
|
+
|
|
46
|
+
exports.TRPC_ERROR_CODES_BY_KEY = TRPC_ERROR_CODES_BY_KEY;
|
|
47
|
+
exports.TRPC_ERROR_CODES_BY_NUMBER = TRPC_ERROR_CODES_BY_NUMBER;
|