@trpc/server 10.0.0-alpha.16 → 10.0.0-alpha.19

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.
Files changed (90) hide show
  1. package/adapters/express/dist/trpc-server-adapters-express.cjs.d.ts +1 -11
  2. package/adapters/express/dist/trpc-server-adapters-express.cjs.dev.js +23 -0
  3. package/adapters/express/dist/trpc-server-adapters-express.cjs.js +6 -15
  4. package/adapters/express/dist/trpc-server-adapters-express.cjs.prod.js +23 -0
  5. package/adapters/express/dist/trpc-server-adapters-express.esm.js +19 -0
  6. package/adapters/fastify/dist/trpc-server-adapters-fastify.cjs.d.ts +1 -11
  7. package/adapters/fastify/dist/trpc-server-adapters-fastify.cjs.dev.js +102 -0
  8. package/adapters/fastify/dist/trpc-server-adapters-fastify.cjs.js +6 -15
  9. package/adapters/fastify/dist/trpc-server-adapters-fastify.cjs.prod.js +102 -0
  10. package/adapters/fastify/dist/trpc-server-adapters-fastify.esm.js +97 -0
  11. package/adapters/next/dist/trpc-server-adapters-next.cjs.d.ts +1 -11
  12. package/adapters/next/dist/trpc-server-adapters-next.cjs.dev.js +56 -0
  13. package/adapters/next/dist/trpc-server-adapters-next.cjs.js +6 -15
  14. package/adapters/next/dist/trpc-server-adapters-next.cjs.prod.js +56 -0
  15. package/adapters/next/dist/trpc-server-adapters-next.esm.js +52 -0
  16. package/adapters/node-http/dist/trpc-server-adapters-node-http.cjs.d.ts +1 -11
  17. package/adapters/node-http/dist/trpc-server-adapters-node-http.cjs.dev.js +13 -0
  18. package/adapters/node-http/dist/trpc-server-adapters-node-http.cjs.js +6 -15
  19. package/adapters/node-http/dist/trpc-server-adapters-node-http.cjs.prod.js +13 -0
  20. package/adapters/node-http/dist/trpc-server-adapters-node-http.esm.js +5 -0
  21. package/adapters/standalone/dist/trpc-server-adapters-standalone.cjs.d.ts +1 -11
  22. package/adapters/standalone/dist/trpc-server-adapters-standalone.cjs.dev.js +46 -0
  23. package/adapters/standalone/dist/trpc-server-adapters-standalone.cjs.js +6 -15
  24. package/adapters/standalone/dist/trpc-server-adapters-standalone.cjs.prod.js +46 -0
  25. package/adapters/standalone/dist/trpc-server-adapters-standalone.esm.js +36 -0
  26. package/adapters/ws/dist/trpc-server-adapters-ws.cjs.d.ts +1 -11
  27. package/adapters/ws/dist/trpc-server-adapters-ws.cjs.dev.js +347 -0
  28. package/adapters/ws/dist/trpc-server-adapters-ws.cjs.js +6 -15
  29. package/adapters/ws/dist/trpc-server-adapters-ws.cjs.prod.js +347 -0
  30. package/adapters/ws/dist/trpc-server-adapters-ws.esm.js +343 -0
  31. package/dist/TRPCError.d.ts +0 -8
  32. package/dist/TRPCError.d.ts.map +1 -1
  33. package/dist/adapters/express.js +3 -3
  34. package/dist/adapters/express.mjs +3 -3
  35. package/dist/adapters/fastify/index.js +3 -3
  36. package/dist/adapters/fastify/index.mjs +3 -3
  37. package/dist/adapters/next.js +3 -3
  38. package/dist/adapters/next.mjs +3 -3
  39. package/dist/adapters/node-http/index.js +3 -3
  40. package/dist/adapters/node-http/index.mjs +3 -3
  41. package/dist/adapters/standalone.js +3 -3
  42. package/dist/adapters/standalone.mjs +3 -3
  43. package/dist/adapters/ws.js +2 -2
  44. package/dist/adapters/ws.mjs +2 -2
  45. package/dist/index.d.ts +1 -3
  46. package/dist/index.d.ts.map +1 -1
  47. package/dist/index.js +4 -42
  48. package/dist/index.mjs +6 -40
  49. package/dist/{nodeHTTPRequestHandler-17b64c0e.js → nodeHTTPRequestHandler-e02d6e5d.js} +2 -2
  50. package/dist/{nodeHTTPRequestHandler-17a5e0dd.mjs → nodeHTTPRequestHandler-f9b7cd2d.mjs} +2 -2
  51. package/dist/{resolveHTTPResponse-9604d47d.mjs → resolveHTTPResponse-ca8bc869.mjs} +1 -1
  52. package/dist/{resolveHTTPResponse-087ce86a.js → resolveHTTPResponse-d4da195d.js} +1 -1
  53. package/dist/router.d.ts +0 -7
  54. package/dist/router.d.ts.map +1 -1
  55. package/dist/{subscription-e1d42e69.mjs → subscription-7ec5884e.mjs} +1 -1
  56. package/dist/{subscription-5b7e1eba.js → subscription-f36af4f8.js} +1 -1
  57. package/dist/{transformTRPCResponse-bd24f548.js → transformTRPCResponse-eeeb5300.js} +3 -7
  58. package/dist/{transformTRPCResponse-76e0c91e.mjs → transformTRPCResponse-f9337285.mjs} +3 -7
  59. package/dist/types.d.ts +0 -8
  60. package/dist/types.d.ts.map +1 -1
  61. package/package.json +3 -8
  62. package/rpc/dist/trpc-server-rpc.cjs.d.ts +1 -11
  63. package/rpc/dist/trpc-server-rpc.cjs.dev.js +10 -0
  64. package/rpc/dist/trpc-server-rpc.cjs.js +6 -15
  65. package/rpc/dist/trpc-server-rpc.cjs.prod.js +10 -0
  66. package/rpc/dist/trpc-server-rpc.esm.js +1 -0
  67. package/src/TRPCError.ts +2 -10
  68. package/src/index.ts +1 -5
  69. package/src/router.ts +0 -25
  70. package/src/types.ts +0 -9
  71. package/ws/dist/trpc-server-ws.cjs.d.ts +1 -11
  72. package/ws/dist/trpc-server-ws.cjs.dev.js +15 -0
  73. package/ws/dist/trpc-server-ws.cjs.js +6 -15
  74. package/ws/dist/trpc-server-ws.cjs.prod.js +15 -0
  75. package/ws/dist/trpc-server-ws.esm.js +11 -0
  76. package/dist/deprecated/LegacyRouter.d.ts +0 -3
  77. package/dist/deprecated/LegacyRouter.d.ts.map +0 -1
  78. package/dist/deprecated/createHttpServer.d.ts +0 -19
  79. package/dist/deprecated/createHttpServer.d.ts.map +0 -1
  80. package/dist/deprecated/requestHandler.d.ts +0 -15
  81. package/dist/deprecated/requestHandler.d.ts.map +0 -1
  82. package/dist/ws/index.d.ts +0 -7
  83. package/dist/ws/index.d.ts.map +0 -1
  84. package/dist/ws/index.js +0 -15
  85. package/dist/ws/index.mjs +0 -11
  86. package/dist/ws/index.ts.js +0 -1
  87. package/src/deprecated/LegacyRouter.ts +0 -3
  88. package/src/deprecated/createHttpServer.ts +0 -29
  89. package/src/deprecated/requestHandler.ts +0 -26
  90. package/src/ws/index.ts +0 -8
@@ -0,0 +1,13 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var nodeHTTPRequestHandler = require('../../../dist/nodeHTTPRequestHandler-2cc444d8.cjs.dev.js');
6
+ require('url');
7
+ require('../../../dist/resolveHTTPResponse-3867844c.cjs.dev.js');
8
+ require('../../../dist/transformTRPCResponse-8248515e.cjs.dev.js');
9
+ require('../../../dist/codes-33cef953.cjs.dev.js');
10
+
11
+
12
+
13
+ exports.nodeHTTPRequestHandler = nodeHTTPRequestHandler.nodeHTTPRequestHandler;
@@ -1,16 +1,7 @@
1
- "use strict";
2
- // this file might look strange and you might be wondering what it's for
3
- // it's lets you import your source files by importing this entrypoint
4
- // as you would import it if it was built with preconstruct build
5
- // this file is slightly different to some others though
6
- // it has a require hook which compiles your code with Babel
7
- // this means that you don't have to set up @babel/register or anything like that
8
- // but you can still require this module and it'll be compiled
1
+ 'use strict';
9
2
 
10
- // this bit of code imports the require hook and registers it
11
- let unregister = require("../../../../../node_modules/@preconstruct/hook").___internalHook(typeof __dirname === 'undefined' ? undefined : __dirname, "../../../../..", "../../..");
12
-
13
- // this re-exports the source file
14
- module.exports = require("../../../src/adapters/node-http/index.ts");
15
-
16
- unregister();
3
+ if (process.env.NODE_ENV === "production") {
4
+ module.exports = require("./trpc-server-adapters-node-http.cjs.prod.js");
5
+ } else {
6
+ module.exports = require("./trpc-server-adapters-node-http.cjs.dev.js");
7
+ }
@@ -0,0 +1,13 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var nodeHTTPRequestHandler = require('../../../dist/nodeHTTPRequestHandler-8ddacc19.cjs.prod.js');
6
+ require('url');
7
+ require('../../../dist/resolveHTTPResponse-562c3d62.cjs.prod.js');
8
+ require('../../../dist/transformTRPCResponse-dcbf66c5.cjs.prod.js');
9
+ require('../../../dist/codes-e0df67c4.cjs.prod.js');
10
+
11
+
12
+
13
+ exports.nodeHTTPRequestHandler = nodeHTTPRequestHandler.nodeHTTPRequestHandler;
@@ -0,0 +1,5 @@
1
+ export { n as nodeHTTPRequestHandler } from '../../../dist/nodeHTTPRequestHandler-d00c1126.esm.js';
2
+ import 'url';
3
+ import '../../../dist/resolveHTTPResponse-85f3f5df.esm.js';
4
+ import '../../../dist/transformTRPCResponse-f73dc4a2.esm.js';
5
+ import '../../../dist/codes-fe07bf82.esm.js';
@@ -1,11 +1 @@
1
- // are you seeing an error that a default export doesn't exist but your source file has a default export?
2
- // you should run `yarn` or `yarn preconstruct dev` if preconstruct dev isn't in your postinstall hook
3
-
4
- // curious why you need to?
5
- // this file exists so that you can import from the entrypoint normally
6
- // except that it points to your source file and you don't need to run build constantly
7
- // which means we need to re-export all of the modules from your source file
8
- // and since export * doesn't include default exports, we need to read your source file
9
- // to check for a default export and re-export it if it exists
10
- // it's not ideal, but it works pretty well ¯\_(ツ)_/¯
11
- export * from "../../../src/adapters/standalone";
1
+ export * from "../../../dist/declarations/src/adapters/standalone";
@@ -0,0 +1,46 @@
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-2cc444d8.cjs.dev.js');
8
+ require('../../../dist/resolveHTTPResponse-3867844c.cjs.dev.js');
9
+ require('../../../dist/transformTRPCResponse-8248515e.cjs.dev.js');
10
+ require('../../../dist/codes-33cef953.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,16 +1,7 @@
1
- "use strict";
2
- // this file might look strange and you might be wondering what it's for
3
- // it's lets you import your source files by importing this entrypoint
4
- // as you would import it if it was built with preconstruct build
5
- // this file is slightly different to some others though
6
- // it has a require hook which compiles your code with Babel
7
- // this means that you don't have to set up @babel/register or anything like that
8
- // but you can still require this module and it'll be compiled
1
+ 'use strict';
9
2
 
10
- // this bit of code imports the require hook and registers it
11
- let unregister = require("../../../../../node_modules/@preconstruct/hook").___internalHook(typeof __dirname === 'undefined' ? undefined : __dirname, "../../../../..", "../../..");
12
-
13
- // this re-exports the source file
14
- module.exports = require("../../../src/adapters/standalone.ts");
15
-
16
- unregister();
3
+ if (process.env.NODE_ENV === "production") {
4
+ module.exports = require("./trpc-server-adapters-standalone.cjs.prod.js");
5
+ } else {
6
+ module.exports = require("./trpc-server-adapters-standalone.cjs.dev.js");
7
+ }
@@ -0,0 +1,46 @@
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-8ddacc19.cjs.prod.js');
8
+ require('../../../dist/resolveHTTPResponse-562c3d62.cjs.prod.js');
9
+ require('../../../dist/transformTRPCResponse-dcbf66c5.cjs.prod.js');
10
+ require('../../../dist/codes-e0df67c4.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;
@@ -0,0 +1,36 @@
1
+ import http from 'http';
2
+ import url from 'url';
3
+ import { n as nodeHTTPRequestHandler } from '../../../dist/nodeHTTPRequestHandler-d00c1126.esm.js';
4
+ import '../../../dist/resolveHTTPResponse-85f3f5df.esm.js';
5
+ import '../../../dist/transformTRPCResponse-f73dc4a2.esm.js';
6
+ import '../../../dist/codes-fe07bf82.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,11 +1 @@
1
- // are you seeing an error that a default export doesn't exist but your source file has a default export?
2
- // you should run `yarn` or `yarn preconstruct dev` if preconstruct dev isn't in your postinstall hook
3
-
4
- // curious why you need to?
5
- // this file exists so that you can import from the entrypoint normally
6
- // except that it points to your source file and you don't need to run build constantly
7
- // which means we need to re-export all of the modules from your source file
8
- // and since export * doesn't include default exports, we need to read your source file
9
- // to check for a default export and re-export it if it exists
10
- // it's not ideal, but it works pretty well ¯\_(ツ)_/¯
11
- export * from "../../../src/adapters/ws";
1
+ export * from "../../../dist/declarations/src/adapters/ws";
@@ -0,0 +1,347 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var transformTRPCResponse = require('../../../dist/transformTRPCResponse-8248515e.cjs.dev.js');
6
+ var subscription = require('../../../dist/subscription-b8831081.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', (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
+ });
285
+ client.once('close', () => {
286
+ for (const sub of clientSubscriptions.values()) {
287
+ sub.destroy();
288
+ }
289
+
290
+ clientSubscriptions.clear();
291
+ });
292
+
293
+ async function createContextAsync() {
294
+ try {
295
+ ctx = await ctxPromise;
296
+ } catch (cause) {
297
+ var _opts$onError3, _global$setImmediate;
298
+
299
+ const error = transformTRPCResponse.getErrorFromUnknown(cause);
300
+ const json = {
301
+ id: null,
302
+ error: router.getErrorShape({
303
+ error,
304
+ type: 'unknown',
305
+ path: undefined,
306
+ input: undefined,
307
+ ctx
308
+ })
309
+ };
310
+ (_opts$onError3 = opts.onError) === null || _opts$onError3 === void 0 ? void 0 : _opts$onError3.call(opts, {
311
+ error,
312
+ path: undefined,
313
+ type: 'unknown',
314
+ ctx,
315
+ req,
316
+ input: undefined
317
+ });
318
+ respond(json); // close in next tick
319
+
320
+ ((_global$setImmediate = global.setImmediate) !== null && _global$setImmediate !== void 0 ? _global$setImmediate : global.setTimeout)(() => {
321
+ client.close();
322
+ });
323
+ }
324
+ }
325
+
326
+ createContextAsync();
327
+ });
328
+ return {
329
+ broadcastReconnectNotification: () => {
330
+ const response = {
331
+ id: null,
332
+ method: 'reconnect'
333
+ };
334
+ const data = JSON.stringify(response);
335
+
336
+ for (const client of wss.clients) {
337
+ if (client.readyState === 1
338
+ /* ws.OPEN */
339
+ ) {
340
+ client.send(data);
341
+ }
342
+ }
343
+ }
344
+ };
345
+ }
346
+
347
+ exports.applyWSSHandler = applyWSSHandler;
@@ -1,16 +1,7 @@
1
- "use strict";
2
- // this file might look strange and you might be wondering what it's for
3
- // it's lets you import your source files by importing this entrypoint
4
- // as you would import it if it was built with preconstruct build
5
- // this file is slightly different to some others though
6
- // it has a require hook which compiles your code with Babel
7
- // this means that you don't have to set up @babel/register or anything like that
8
- // but you can still require this module and it'll be compiled
1
+ 'use strict';
9
2
 
10
- // this bit of code imports the require hook and registers it
11
- let unregister = require("../../../../../node_modules/@preconstruct/hook").___internalHook(typeof __dirname === 'undefined' ? undefined : __dirname, "../../../../..", "../../..");
12
-
13
- // this re-exports the source file
14
- module.exports = require("../../../src/adapters/ws.ts");
15
-
16
- unregister();
3
+ if (process.env.NODE_ENV === "production") {
4
+ module.exports = require("./trpc-server-adapters-ws.cjs.prod.js");
5
+ } else {
6
+ module.exports = require("./trpc-server-adapters-ws.cjs.dev.js");
7
+ }