@trpc/server 10.14.0 → 10.15.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/{TRPCError-e7333eae.mjs → TRPCError-226f5343.mjs} +5 -10
- package/dist/{TRPCError-ed16b3b7.js → TRPCError-92d93cfd.js} +5 -10
- package/dist/{TRPCError-4cbab0d0.js → TRPCError-98f26ad6.js} +5 -10
- package/dist/adapters/aws-lambda/index.js +7 -3
- package/dist/adapters/aws-lambda/index.mjs +7 -3
- package/dist/adapters/aws-lambda/utils.d.ts.map +1 -1
- package/dist/adapters/express.js +4 -4
- package/dist/adapters/express.mjs +4 -4
- package/dist/adapters/fastify/index.js +3 -3
- package/dist/adapters/fastify/index.mjs +3 -3
- package/dist/adapters/fetch/index.js +3 -3
- package/dist/adapters/fetch/index.mjs +3 -3
- package/dist/adapters/next.js +4 -4
- package/dist/adapters/next.mjs +4 -4
- package/dist/adapters/node-http/index.js +4 -4
- package/dist/adapters/node-http/index.mjs +4 -4
- package/dist/adapters/standalone.js +4 -4
- package/dist/adapters/standalone.mjs +4 -4
- package/dist/adapters/ws.js +2 -2
- package/dist/adapters/ws.mjs +2 -2
- package/dist/{config-bd759464.js → config-055ae8e4.js} +28 -5
- package/dist/{config-69ee2314.js → config-163772ca.js} +28 -4
- package/dist/{config-fb5f49a1.mjs → config-6e96a9bc.mjs} +28 -5
- package/dist/core/router.d.ts +2 -2
- package/dist/core/router.d.ts.map +1 -1
- package/dist/error/TRPCError.d.ts +1 -1
- package/dist/error/TRPCError.d.ts.map +1 -1
- package/dist/http/index.js +3 -3
- package/dist/http/index.mjs +3 -3
- package/dist/index.js +2 -2
- package/dist/index.mjs +4 -4
- package/dist/{nodeHTTPRequestHandler-7ebe1e44.js → nodeHTTPRequestHandler-11f3df04.js} +2 -2
- package/dist/{nodeHTTPRequestHandler-5a5141c3.js → nodeHTTPRequestHandler-16d7c6df.js} +2 -2
- package/dist/{nodeHTTPRequestHandler-8c27f394.mjs → nodeHTTPRequestHandler-c9ddbeeb.mjs} +2 -2
- package/dist/{resolveHTTPResponse-39258d19.js → resolveHTTPResponse-15c18b7a.js} +2 -2
- package/dist/{resolveHTTPResponse-cbfdc43d.js → resolveHTTPResponse-94b380d2.js} +2 -2
- package/dist/{resolveHTTPResponse-8e9dd846.mjs → resolveHTTPResponse-edf72fc7.mjs} +2 -2
- package/dist/shared/index.d.ts +1 -0
- package/dist/shared/index.d.ts.map +1 -1
- package/dist/shared/internal/serialize.d.ts +5 -2
- package/dist/shared/internal/serialize.d.ts.map +1 -1
- package/dist/subscription.js +1 -1
- package/dist/subscription.mjs +1 -1
- package/package.json +3 -2
- package/src/adapters/aws-lambda/utils.ts +4 -0
- package/src/core/router.ts +41 -6
- package/src/error/TRPCError.ts +8 -13
- package/src/shared/index.ts +3 -0
- package/src/shared/internal/serialize.ts +5 -2
- package/adapters/express/dist/trpc-server-adapters-express.cjs.d.ts +0 -1
- package/adapters/express/dist/trpc-server-adapters-express.cjs.dev.js +0 -23
- package/adapters/express/dist/trpc-server-adapters-express.cjs.js +0 -7
- package/adapters/express/dist/trpc-server-adapters-express.cjs.prod.js +0 -23
- package/adapters/express/dist/trpc-server-adapters-express.esm.js +0 -19
- package/adapters/lambda/dist/trpc-server-adapters-lambda.cjs.d.ts +0 -1
- package/adapters/lambda/dist/trpc-server-adapters-lambda.cjs.dev.js +0 -25
- package/adapters/lambda/dist/trpc-server-adapters-lambda.cjs.js +0 -7
- package/adapters/lambda/dist/trpc-server-adapters-lambda.cjs.prod.js +0 -25
- package/adapters/lambda/dist/trpc-server-adapters-lambda.esm.js +0 -21
- package/adapters/next/dist/trpc-server-adapters-next.cjs.d.ts +0 -1
- package/adapters/next/dist/trpc-server-adapters-next.cjs.dev.js +0 -56
- package/adapters/next/dist/trpc-server-adapters-next.cjs.js +0 -7
- package/adapters/next/dist/trpc-server-adapters-next.cjs.prod.js +0 -56
- package/adapters/next/dist/trpc-server-adapters-next.esm.js +0 -52
- package/adapters/node-http/dist/trpc-server-adapters-node-http.cjs.d.ts +0 -1
- package/adapters/node-http/dist/trpc-server-adapters-node-http.cjs.dev.js +0 -13
- package/adapters/node-http/dist/trpc-server-adapters-node-http.cjs.js +0 -7
- package/adapters/node-http/dist/trpc-server-adapters-node-http.cjs.prod.js +0 -13
- package/adapters/node-http/dist/trpc-server-adapters-node-http.esm.js +0 -5
- package/adapters/standalone/dist/trpc-server-adapters-standalone.cjs.d.ts +0 -1
- package/adapters/standalone/dist/trpc-server-adapters-standalone.cjs.dev.js +0 -46
- package/adapters/standalone/dist/trpc-server-adapters-standalone.cjs.js +0 -7
- package/adapters/standalone/dist/trpc-server-adapters-standalone.cjs.prod.js +0 -46
- package/adapters/standalone/dist/trpc-server-adapters-standalone.esm.js +0 -36
- package/adapters/ws/dist/trpc-server-adapters-ws.cjs.d.ts +0 -1
- package/adapters/ws/dist/trpc-server-adapters-ws.cjs.dev.js +0 -363
- package/adapters/ws/dist/trpc-server-adapters-ws.cjs.js +0 -7
- package/adapters/ws/dist/trpc-server-adapters-ws.cjs.prod.js +0 -363
- package/adapters/ws/dist/trpc-server-adapters-ws.esm.js +0 -359
- package/rpc/dist/trpc-server-rpc.cjs.d.ts +0 -1
- package/rpc/dist/trpc-server-rpc.cjs.dev.js +0 -10
- package/rpc/dist/trpc-server-rpc.cjs.js +0 -7
- package/rpc/dist/trpc-server-rpc.cjs.prod.js +0 -10
- package/rpc/dist/trpc-server-rpc.esm.js +0 -1
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var transformTRPCResponse = require('../../../dist/transformTRPCResponse-077b7539.cjs.prod.js');
|
|
6
|
-
var nodeHTTPRequestHandler = require('../../../dist/nodeHTTPRequestHandler-21707788.cjs.prod.js');
|
|
7
|
-
require('url');
|
|
8
|
-
require('../../../dist/resolveHTTPResponse-9812e803.cjs.prod.js');
|
|
9
|
-
require('../../../dist/codes-aff770a3.cjs.prod.js');
|
|
10
|
-
|
|
11
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
12
|
-
function createNextApiHandler(opts) {
|
|
13
|
-
return async (req, res) => {
|
|
14
|
-
function getPath() {
|
|
15
|
-
if (typeof req.query.trpc === 'string') {
|
|
16
|
-
return req.query.trpc;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
if (Array.isArray(req.query.trpc)) {
|
|
20
|
-
return req.query.trpc.join('/');
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return null;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const path = getPath();
|
|
27
|
-
|
|
28
|
-
if (path === null) {
|
|
29
|
-
const error = opts.router.getErrorShape({
|
|
30
|
-
error: new transformTRPCResponse.TRPCError({
|
|
31
|
-
message: 'Query "trpc" not found - is the file named `[trpc]`.ts or `[...trpc].ts`?',
|
|
32
|
-
code: 'INTERNAL_SERVER_ERROR'
|
|
33
|
-
}),
|
|
34
|
-
type: 'unknown',
|
|
35
|
-
ctx: undefined,
|
|
36
|
-
path: undefined,
|
|
37
|
-
input: undefined
|
|
38
|
-
});
|
|
39
|
-
const json = {
|
|
40
|
-
id: -1,
|
|
41
|
-
error
|
|
42
|
-
};
|
|
43
|
-
res.statusCode = 500;
|
|
44
|
-
res.json(json);
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
await nodeHTTPRequestHandler.nodeHTTPRequestHandler({ ...opts,
|
|
49
|
-
req,
|
|
50
|
-
res,
|
|
51
|
-
path
|
|
52
|
-
});
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
exports.createNextApiHandler = createNextApiHandler;
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { T as TRPCError } from '../../../dist/transformTRPCResponse-56464e52.esm.js';
|
|
2
|
-
import { n as nodeHTTPRequestHandler } from '../../../dist/nodeHTTPRequestHandler-4e09fc4c.esm.js';
|
|
3
|
-
import 'url';
|
|
4
|
-
import '../../../dist/resolveHTTPResponse-fcf14f5a.esm.js';
|
|
5
|
-
import '../../../dist/codes-5678cc97.esm.js';
|
|
6
|
-
|
|
7
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
8
|
-
function createNextApiHandler(opts) {
|
|
9
|
-
return async (req, res) => {
|
|
10
|
-
function getPath() {
|
|
11
|
-
if (typeof req.query.trpc === 'string') {
|
|
12
|
-
return req.query.trpc;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
if (Array.isArray(req.query.trpc)) {
|
|
16
|
-
return req.query.trpc.join('/');
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
return null;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const path = getPath();
|
|
23
|
-
|
|
24
|
-
if (path === null) {
|
|
25
|
-
const error = opts.router.getErrorShape({
|
|
26
|
-
error: new TRPCError({
|
|
27
|
-
message: 'Query "trpc" not found - is the file named `[trpc]`.ts or `[...trpc].ts`?',
|
|
28
|
-
code: 'INTERNAL_SERVER_ERROR'
|
|
29
|
-
}),
|
|
30
|
-
type: 'unknown',
|
|
31
|
-
ctx: undefined,
|
|
32
|
-
path: undefined,
|
|
33
|
-
input: undefined
|
|
34
|
-
});
|
|
35
|
-
const json = {
|
|
36
|
-
id: -1,
|
|
37
|
-
error
|
|
38
|
-
};
|
|
39
|
-
res.statusCode = 500;
|
|
40
|
-
res.json(json);
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
await nodeHTTPRequestHandler({ ...opts,
|
|
45
|
-
req,
|
|
46
|
-
res,
|
|
47
|
-
path
|
|
48
|
-
});
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export { createNextApiHandler };
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "../../../dist/declarations/src/adapters/node-http/index";
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var nodeHTTPRequestHandler = require('../../../dist/nodeHTTPRequestHandler-9a93c255.cjs.dev.js');
|
|
6
|
-
require('url');
|
|
7
|
-
require('../../../dist/resolveHTTPResponse-ab01e4b9.cjs.dev.js');
|
|
8
|
-
require('../../../dist/transformTRPCResponse-36a41418.cjs.dev.js');
|
|
9
|
-
require('../../../dist/codes-130e62df.cjs.dev.js');
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
exports.nodeHTTPRequestHandler = nodeHTTPRequestHandler.nodeHTTPRequestHandler;
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var nodeHTTPRequestHandler = require('../../../dist/nodeHTTPRequestHandler-21707788.cjs.prod.js');
|
|
6
|
-
require('url');
|
|
7
|
-
require('../../../dist/resolveHTTPResponse-9812e803.cjs.prod.js');
|
|
8
|
-
require('../../../dist/transformTRPCResponse-077b7539.cjs.prod.js');
|
|
9
|
-
require('../../../dist/codes-aff770a3.cjs.prod.js');
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
exports.nodeHTTPRequestHandler = nodeHTTPRequestHandler.nodeHTTPRequestHandler;
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
export { n as nodeHTTPRequestHandler } from '../../../dist/nodeHTTPRequestHandler-4e09fc4c.esm.js';
|
|
2
|
-
import 'url';
|
|
3
|
-
import '../../../dist/resolveHTTPResponse-fcf14f5a.esm.js';
|
|
4
|
-
import '../../../dist/transformTRPCResponse-56464e52.esm.js';
|
|
5
|
-
import '../../../dist/codes-5678cc97.esm.js';
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "../../../dist/declarations/src/adapters/standalone";
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var http = require('http');
|
|
6
|
-
var url = require('url');
|
|
7
|
-
var nodeHTTPRequestHandler = require('../../../dist/nodeHTTPRequestHandler-9a93c255.cjs.dev.js');
|
|
8
|
-
require('../../../dist/resolveHTTPResponse-ab01e4b9.cjs.dev.js');
|
|
9
|
-
require('../../../dist/transformTRPCResponse-36a41418.cjs.dev.js');
|
|
10
|
-
require('../../../dist/codes-130e62df.cjs.dev.js');
|
|
11
|
-
|
|
12
|
-
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
|
13
|
-
|
|
14
|
-
var http__default = /*#__PURE__*/_interopDefault(http);
|
|
15
|
-
var url__default = /*#__PURE__*/_interopDefault(url);
|
|
16
|
-
|
|
17
|
-
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
18
|
-
function createHTTPHandler(opts) {
|
|
19
|
-
return async (req, res) => {
|
|
20
|
-
const endpoint = url__default['default'].parse(req.url).pathname.slice(1);
|
|
21
|
-
await nodeHTTPRequestHandler.nodeHTTPRequestHandler({ ...opts,
|
|
22
|
-
req,
|
|
23
|
-
res,
|
|
24
|
-
path: endpoint
|
|
25
|
-
});
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
function createHTTPServer(opts) {
|
|
29
|
-
const handler = createHTTPHandler(opts);
|
|
30
|
-
const server = http__default['default'].createServer((req, res) => handler(req, res));
|
|
31
|
-
return {
|
|
32
|
-
server,
|
|
33
|
-
|
|
34
|
-
listen(port) {
|
|
35
|
-
server.listen(port);
|
|
36
|
-
const actualPort = port === 0 ? server.address().port : port;
|
|
37
|
-
return {
|
|
38
|
-
port: actualPort
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
exports.createHTTPHandler = createHTTPHandler;
|
|
46
|
-
exports.createHTTPServer = createHTTPServer;
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var http = require('http');
|
|
6
|
-
var url = require('url');
|
|
7
|
-
var nodeHTTPRequestHandler = require('../../../dist/nodeHTTPRequestHandler-21707788.cjs.prod.js');
|
|
8
|
-
require('../../../dist/resolveHTTPResponse-9812e803.cjs.prod.js');
|
|
9
|
-
require('../../../dist/transformTRPCResponse-077b7539.cjs.prod.js');
|
|
10
|
-
require('../../../dist/codes-aff770a3.cjs.prod.js');
|
|
11
|
-
|
|
12
|
-
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
|
13
|
-
|
|
14
|
-
var http__default = /*#__PURE__*/_interopDefault(http);
|
|
15
|
-
var url__default = /*#__PURE__*/_interopDefault(url);
|
|
16
|
-
|
|
17
|
-
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
18
|
-
function createHTTPHandler(opts) {
|
|
19
|
-
return async (req, res) => {
|
|
20
|
-
const endpoint = url__default['default'].parse(req.url).pathname.slice(1);
|
|
21
|
-
await nodeHTTPRequestHandler.nodeHTTPRequestHandler({ ...opts,
|
|
22
|
-
req,
|
|
23
|
-
res,
|
|
24
|
-
path: endpoint
|
|
25
|
-
});
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
function createHTTPServer(opts) {
|
|
29
|
-
const handler = createHTTPHandler(opts);
|
|
30
|
-
const server = http__default['default'].createServer((req, res) => handler(req, res));
|
|
31
|
-
return {
|
|
32
|
-
server,
|
|
33
|
-
|
|
34
|
-
listen(port) {
|
|
35
|
-
server.listen(port);
|
|
36
|
-
const actualPort = port === 0 ? server.address().port : port;
|
|
37
|
-
return {
|
|
38
|
-
port: actualPort
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
exports.createHTTPHandler = createHTTPHandler;
|
|
46
|
-
exports.createHTTPServer = createHTTPServer;
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import http from 'http';
|
|
2
|
-
import url from 'url';
|
|
3
|
-
import { n as nodeHTTPRequestHandler } from '../../../dist/nodeHTTPRequestHandler-4e09fc4c.esm.js';
|
|
4
|
-
import '../../../dist/resolveHTTPResponse-fcf14f5a.esm.js';
|
|
5
|
-
import '../../../dist/transformTRPCResponse-56464e52.esm.js';
|
|
6
|
-
import '../../../dist/codes-5678cc97.esm.js';
|
|
7
|
-
|
|
8
|
-
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
9
|
-
function createHTTPHandler(opts) {
|
|
10
|
-
return async (req, res) => {
|
|
11
|
-
const endpoint = url.parse(req.url).pathname.slice(1);
|
|
12
|
-
await nodeHTTPRequestHandler({ ...opts,
|
|
13
|
-
req,
|
|
14
|
-
res,
|
|
15
|
-
path: endpoint
|
|
16
|
-
});
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
function createHTTPServer(opts) {
|
|
20
|
-
const handler = createHTTPHandler(opts);
|
|
21
|
-
const server = http.createServer((req, res) => handler(req, res));
|
|
22
|
-
return {
|
|
23
|
-
server,
|
|
24
|
-
|
|
25
|
-
listen(port) {
|
|
26
|
-
server.listen(port);
|
|
27
|
-
const actualPort = port === 0 ? server.address().port : port;
|
|
28
|
-
return {
|
|
29
|
-
port: actualPort
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export { createHTTPHandler, createHTTPServer };
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "../../../dist/declarations/src/adapters/ws";
|
|
@@ -1,363 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var transformTRPCResponse = require('../../../dist/transformTRPCResponse-36a41418.cjs.dev.js');
|
|
6
|
-
var subscription = require('../../../dist/subscription-8bf61685.cjs.dev.js');
|
|
7
|
-
require('events');
|
|
8
|
-
|
|
9
|
-
/* istanbul ignore next */
|
|
10
|
-
function assertIsObject(obj) {
|
|
11
|
-
if (typeof obj !== 'object' || Array.isArray(obj) || !obj) {
|
|
12
|
-
throw new Error('Not an object');
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
/* istanbul ignore next */
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
function assertIsProcedureType(obj) {
|
|
19
|
-
if (obj !== 'query' && obj !== 'subscription' && obj !== 'mutation') {
|
|
20
|
-
throw new Error('Invalid procedure type');
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
/* istanbul ignore next */
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
function assertIsRequestId(obj) {
|
|
27
|
-
if (obj !== null && typeof obj === 'number' && isNaN(obj) && typeof obj !== 'string') {
|
|
28
|
-
throw new Error('Invalid request id');
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
/* istanbul ignore next */
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
function assertIsString(obj) {
|
|
35
|
-
if (typeof obj !== 'string') {
|
|
36
|
-
throw new Error('Invalid string');
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
/* istanbul ignore next */
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
function assertIsJSONRPC2OrUndefined(obj) {
|
|
43
|
-
if (typeof obj !== 'undefined' && obj !== '2.0') {
|
|
44
|
-
throw new Error('Must be JSONRPC 2.0');
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function parseMessage(obj, transformer) {
|
|
49
|
-
assertIsObject(obj);
|
|
50
|
-
const {
|
|
51
|
-
method,
|
|
52
|
-
params,
|
|
53
|
-
id,
|
|
54
|
-
jsonrpc
|
|
55
|
-
} = obj;
|
|
56
|
-
assertIsRequestId(id);
|
|
57
|
-
assertIsJSONRPC2OrUndefined(jsonrpc);
|
|
58
|
-
|
|
59
|
-
if (method === 'subscription.stop') {
|
|
60
|
-
return {
|
|
61
|
-
id,
|
|
62
|
-
method,
|
|
63
|
-
params: undefined
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
assertIsProcedureType(method);
|
|
68
|
-
assertIsObject(params);
|
|
69
|
-
const {
|
|
70
|
-
input: rawInput,
|
|
71
|
-
path
|
|
72
|
-
} = params;
|
|
73
|
-
assertIsString(path);
|
|
74
|
-
const input = transformer.input.deserialize(rawInput);
|
|
75
|
-
return {
|
|
76
|
-
jsonrpc,
|
|
77
|
-
id,
|
|
78
|
-
method,
|
|
79
|
-
params: {
|
|
80
|
-
input,
|
|
81
|
-
path
|
|
82
|
-
}
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Web socket server handler
|
|
87
|
-
*/
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
function applyWSSHandler(opts) {
|
|
91
|
-
const {
|
|
92
|
-
wss,
|
|
93
|
-
createContext,
|
|
94
|
-
router
|
|
95
|
-
} = opts;
|
|
96
|
-
const {
|
|
97
|
-
transformer
|
|
98
|
-
} = router._def;
|
|
99
|
-
wss.on('connection', async (client, req) => {
|
|
100
|
-
const clientSubscriptions = new Map();
|
|
101
|
-
|
|
102
|
-
function respond(untransformedJSON) {
|
|
103
|
-
client.send(JSON.stringify(transformTRPCResponse.transformTRPCResponse(router, untransformedJSON)));
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
const ctxPromise = createContext === null || createContext === void 0 ? void 0 : createContext({
|
|
107
|
-
req,
|
|
108
|
-
res: client
|
|
109
|
-
});
|
|
110
|
-
let ctx = undefined;
|
|
111
|
-
|
|
112
|
-
async function handleRequest(msg) {
|
|
113
|
-
const {
|
|
114
|
-
id
|
|
115
|
-
} = msg;
|
|
116
|
-
/* istanbul ignore next */
|
|
117
|
-
|
|
118
|
-
if (id === null) {
|
|
119
|
-
throw new transformTRPCResponse.TRPCError({
|
|
120
|
-
code: 'BAD_REQUEST',
|
|
121
|
-
message: '`id` is required'
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
if (msg.method === 'subscription.stop') {
|
|
126
|
-
const sub = clientSubscriptions.get(id);
|
|
127
|
-
|
|
128
|
-
if (sub) {
|
|
129
|
-
sub.destroy();
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
clientSubscriptions.delete(id);
|
|
133
|
-
return;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
const {
|
|
137
|
-
path,
|
|
138
|
-
input
|
|
139
|
-
} = msg.params;
|
|
140
|
-
const type = msg.method;
|
|
141
|
-
|
|
142
|
-
try {
|
|
143
|
-
await ctxPromise; // asserts context has been set
|
|
144
|
-
|
|
145
|
-
const result = await transformTRPCResponse.callProcedure({
|
|
146
|
-
path,
|
|
147
|
-
input,
|
|
148
|
-
type,
|
|
149
|
-
router,
|
|
150
|
-
ctx
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
if (!(result instanceof subscription.Subscription)) {
|
|
154
|
-
respond({
|
|
155
|
-
id,
|
|
156
|
-
result: {
|
|
157
|
-
type: 'data',
|
|
158
|
-
data: result
|
|
159
|
-
}
|
|
160
|
-
});
|
|
161
|
-
return;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
const sub = result;
|
|
165
|
-
/* istanbul ignore next */
|
|
166
|
-
|
|
167
|
-
if (client.readyState !== client.OPEN) {
|
|
168
|
-
// if the client got disconnected whilst initializing the subscription
|
|
169
|
-
sub.destroy();
|
|
170
|
-
return;
|
|
171
|
-
}
|
|
172
|
-
/* istanbul ignore next */
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
if (clientSubscriptions.has(id)) {
|
|
176
|
-
// duplicate request ids for client
|
|
177
|
-
sub.destroy();
|
|
178
|
-
throw new transformTRPCResponse.TRPCError({
|
|
179
|
-
message: `Duplicate id ${id}`,
|
|
180
|
-
code: 'BAD_REQUEST'
|
|
181
|
-
});
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
clientSubscriptions.set(id, sub);
|
|
185
|
-
sub.on('data', data => {
|
|
186
|
-
respond({
|
|
187
|
-
id,
|
|
188
|
-
result: {
|
|
189
|
-
type: 'data',
|
|
190
|
-
data
|
|
191
|
-
}
|
|
192
|
-
});
|
|
193
|
-
});
|
|
194
|
-
sub.on('error', _error => {
|
|
195
|
-
var _opts$onError;
|
|
196
|
-
|
|
197
|
-
const error = transformTRPCResponse.getErrorFromUnknown(_error);
|
|
198
|
-
const json = {
|
|
199
|
-
id,
|
|
200
|
-
error: router.getErrorShape({
|
|
201
|
-
error,
|
|
202
|
-
type,
|
|
203
|
-
path,
|
|
204
|
-
input,
|
|
205
|
-
ctx
|
|
206
|
-
})
|
|
207
|
-
};
|
|
208
|
-
(_opts$onError = opts.onError) === null || _opts$onError === void 0 ? void 0 : _opts$onError.call(opts, {
|
|
209
|
-
error,
|
|
210
|
-
path,
|
|
211
|
-
type,
|
|
212
|
-
ctx,
|
|
213
|
-
req,
|
|
214
|
-
input
|
|
215
|
-
});
|
|
216
|
-
respond(json);
|
|
217
|
-
});
|
|
218
|
-
sub.on('destroy', () => {
|
|
219
|
-
respond({
|
|
220
|
-
id,
|
|
221
|
-
result: {
|
|
222
|
-
type: 'stopped'
|
|
223
|
-
}
|
|
224
|
-
});
|
|
225
|
-
});
|
|
226
|
-
respond({
|
|
227
|
-
id,
|
|
228
|
-
result: {
|
|
229
|
-
type: 'started'
|
|
230
|
-
}
|
|
231
|
-
});
|
|
232
|
-
await sub.start();
|
|
233
|
-
} catch (cause)
|
|
234
|
-
/* istanbul ignore next */
|
|
235
|
-
{
|
|
236
|
-
var _opts$onError2;
|
|
237
|
-
|
|
238
|
-
// procedure threw an error
|
|
239
|
-
const error = transformTRPCResponse.getErrorFromUnknown(cause);
|
|
240
|
-
const json = router.getErrorShape({
|
|
241
|
-
error,
|
|
242
|
-
type,
|
|
243
|
-
path,
|
|
244
|
-
input,
|
|
245
|
-
ctx
|
|
246
|
-
});
|
|
247
|
-
(_opts$onError2 = opts.onError) === null || _opts$onError2 === void 0 ? void 0 : _opts$onError2.call(opts, {
|
|
248
|
-
error,
|
|
249
|
-
path,
|
|
250
|
-
type,
|
|
251
|
-
ctx,
|
|
252
|
-
req,
|
|
253
|
-
input
|
|
254
|
-
});
|
|
255
|
-
respond({
|
|
256
|
-
id,
|
|
257
|
-
error: json
|
|
258
|
-
});
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
client.on('message', async message => {
|
|
263
|
-
try {
|
|
264
|
-
const msgJSON = JSON.parse(message.toString());
|
|
265
|
-
const msgs = Array.isArray(msgJSON) ? msgJSON : [msgJSON];
|
|
266
|
-
const promises = msgs.map(raw => parseMessage(raw, transformer)).map(handleRequest);
|
|
267
|
-
await Promise.all(promises);
|
|
268
|
-
} catch (cause) {
|
|
269
|
-
const error = new transformTRPCResponse.TRPCError({
|
|
270
|
-
code: 'PARSE_ERROR',
|
|
271
|
-
cause
|
|
272
|
-
});
|
|
273
|
-
respond({
|
|
274
|
-
id: null,
|
|
275
|
-
error: router.getErrorShape({
|
|
276
|
-
error,
|
|
277
|
-
type: 'unknown',
|
|
278
|
-
path: undefined,
|
|
279
|
-
input: undefined,
|
|
280
|
-
ctx: undefined
|
|
281
|
-
})
|
|
282
|
-
});
|
|
283
|
-
}
|
|
284
|
-
}); // WebSocket errors should be handled, as otherwise unhandled exceptions will crash Node.js.
|
|
285
|
-
// This line was introduced after the following error brought down production systems:
|
|
286
|
-
// "RangeError: Invalid WebSocket frame: RSV2 and RSV3 must be clear"
|
|
287
|
-
// Here is the relevant discussion: https://github.com/websockets/ws/issues/1354#issuecomment-774616962
|
|
288
|
-
|
|
289
|
-
client.on('error', cause => {
|
|
290
|
-
var _opts$onError3;
|
|
291
|
-
|
|
292
|
-
(_opts$onError3 = opts.onError) === null || _opts$onError3 === void 0 ? void 0 : _opts$onError3.call(opts, {
|
|
293
|
-
ctx,
|
|
294
|
-
error: transformTRPCResponse.getErrorFromUnknown(cause),
|
|
295
|
-
input: undefined,
|
|
296
|
-
path: undefined,
|
|
297
|
-
type: 'unknown',
|
|
298
|
-
req
|
|
299
|
-
});
|
|
300
|
-
});
|
|
301
|
-
client.once('close', () => {
|
|
302
|
-
for (const sub of clientSubscriptions.values()) {
|
|
303
|
-
sub.destroy();
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
clientSubscriptions.clear();
|
|
307
|
-
});
|
|
308
|
-
|
|
309
|
-
async function createContextAsync() {
|
|
310
|
-
try {
|
|
311
|
-
ctx = await ctxPromise;
|
|
312
|
-
} catch (cause) {
|
|
313
|
-
var _opts$onError4, _global$setImmediate;
|
|
314
|
-
|
|
315
|
-
const error = transformTRPCResponse.getErrorFromUnknown(cause);
|
|
316
|
-
const json = {
|
|
317
|
-
id: null,
|
|
318
|
-
error: router.getErrorShape({
|
|
319
|
-
error,
|
|
320
|
-
type: 'unknown',
|
|
321
|
-
path: undefined,
|
|
322
|
-
input: undefined,
|
|
323
|
-
ctx
|
|
324
|
-
})
|
|
325
|
-
};
|
|
326
|
-
(_opts$onError4 = opts.onError) === null || _opts$onError4 === void 0 ? void 0 : _opts$onError4.call(opts, {
|
|
327
|
-
error,
|
|
328
|
-
path: undefined,
|
|
329
|
-
type: 'unknown',
|
|
330
|
-
ctx,
|
|
331
|
-
req,
|
|
332
|
-
input: undefined
|
|
333
|
-
});
|
|
334
|
-
respond(json); // close in next tick
|
|
335
|
-
|
|
336
|
-
((_global$setImmediate = global.setImmediate) !== null && _global$setImmediate !== void 0 ? _global$setImmediate : global.setTimeout)(() => {
|
|
337
|
-
client.close();
|
|
338
|
-
});
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
await createContextAsync();
|
|
343
|
-
});
|
|
344
|
-
return {
|
|
345
|
-
broadcastReconnectNotification: () => {
|
|
346
|
-
const response = {
|
|
347
|
-
id: null,
|
|
348
|
-
method: 'reconnect'
|
|
349
|
-
};
|
|
350
|
-
const data = JSON.stringify(response);
|
|
351
|
-
|
|
352
|
-
for (const client of wss.clients) {
|
|
353
|
-
if (client.readyState === 1
|
|
354
|
-
/* ws.OPEN */
|
|
355
|
-
) {
|
|
356
|
-
client.send(data);
|
|
357
|
-
}
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
};
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
exports.applyWSSHandler = applyWSSHandler;
|