@trpc/server 10.13.2 → 10.14.1

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 (54) hide show
  1. package/LICENSE +2 -2
  2. package/adapters/zodFileSchema/index.d.ts +1 -0
  3. package/adapters/zodFileSchema/index.js +1 -0
  4. package/dist/{TRPCError-e7333eae.mjs → TRPCError-226f5343.mjs} +5 -10
  5. package/dist/{TRPCError-ed16b3b7.js → TRPCError-92d93cfd.js} +5 -10
  6. package/dist/{TRPCError-4cbab0d0.js → TRPCError-98f26ad6.js} +5 -10
  7. package/dist/adapters/aws-lambda/index.js +7 -3
  8. package/dist/adapters/aws-lambda/index.mjs +7 -3
  9. package/dist/adapters/aws-lambda/utils.d.ts.map +1 -1
  10. package/dist/adapters/express.js +4 -4
  11. package/dist/adapters/express.mjs +4 -4
  12. package/dist/adapters/fastify/index.js +3 -3
  13. package/dist/adapters/fastify/index.mjs +3 -3
  14. package/dist/adapters/fetch/index.js +3 -3
  15. package/dist/adapters/fetch/index.mjs +3 -3
  16. package/dist/adapters/next.js +4 -4
  17. package/dist/adapters/next.mjs +4 -4
  18. package/dist/adapters/node-http/index.js +4 -4
  19. package/dist/adapters/node-http/index.mjs +4 -4
  20. package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
  21. package/dist/adapters/node-http/types.d.ts +23 -0
  22. package/dist/adapters/node-http/types.d.ts.map +1 -1
  23. package/dist/adapters/standalone.js +4 -4
  24. package/dist/adapters/standalone.mjs +4 -4
  25. package/dist/adapters/ws.js +2 -2
  26. package/dist/adapters/ws.mjs +2 -2
  27. package/dist/{config-bd759464.js → config-046e82fc.js} +1 -1
  28. package/dist/{config-fb5f49a1.mjs → config-61d48858.mjs} +1 -1
  29. package/dist/{config-69ee2314.js → config-cac4535e.js} +1 -1
  30. package/dist/core/index.d.ts +1 -1
  31. package/dist/core/index.d.ts.map +1 -1
  32. package/dist/error/TRPCError.d.ts +1 -1
  33. package/dist/error/TRPCError.d.ts.map +1 -1
  34. package/dist/http/index.js +3 -3
  35. package/dist/http/index.mjs +3 -3
  36. package/dist/index.js +2 -2
  37. package/dist/index.mjs +4 -4
  38. package/dist/nodeHTTPRequestHandler-0f439ec3.js +90 -0
  39. package/dist/nodeHTTPRequestHandler-41accae1.mjs +92 -0
  40. package/dist/nodeHTTPRequestHandler-4f981ca6.js +94 -0
  41. package/dist/{resolveHTTPResponse-cbfdc43d.js → resolveHTTPResponse-4798ae79.js} +2 -2
  42. package/dist/{resolveHTTPResponse-8e9dd846.mjs → resolveHTTPResponse-5c43d189.mjs} +2 -2
  43. package/dist/{resolveHTTPResponse-39258d19.js → resolveHTTPResponse-ef105097.js} +2 -2
  44. package/dist/subscription.js +1 -1
  45. package/dist/subscription.mjs +1 -1
  46. package/package.json +3 -2
  47. package/src/adapters/aws-lambda/utils.ts +4 -0
  48. package/src/adapters/node-http/nodeHTTPRequestHandler.ts +64 -41
  49. package/src/adapters/node-http/types.ts +27 -0
  50. package/src/core/index.ts +1 -0
  51. package/src/error/TRPCError.ts +8 -13
  52. package/dist/nodeHTTPRequestHandler-40bc6a18.js +0 -81
  53. package/dist/nodeHTTPRequestHandler-4314fa32.mjs +0 -79
  54. package/dist/nodeHTTPRequestHandler-93528c16.js +0 -76
@@ -0,0 +1,92 @@
1
+ import { r as resolveHTTPResponse } from './resolveHTTPResponse-5c43d189.mjs';
2
+ import { T as TRPCError } from './TRPCError-226f5343.mjs';
3
+
4
+ async function getPostBody(opts) {
5
+ const { req , maxBodySize =Infinity } = opts;
6
+ return new Promise((resolve)=>{
7
+ if ('body' in req) {
8
+ resolve({
9
+ ok: true,
10
+ data: req.body
11
+ });
12
+ return;
13
+ }
14
+ let body = '';
15
+ let hasBody = false;
16
+ req.on('data', function(data) {
17
+ body += data;
18
+ hasBody = true;
19
+ if (body.length > maxBodySize) {
20
+ resolve({
21
+ ok: false,
22
+ error: new TRPCError({
23
+ code: 'PAYLOAD_TOO_LARGE'
24
+ })
25
+ });
26
+ req.socket.destroy();
27
+ }
28
+ });
29
+ req.on('end', ()=>{
30
+ resolve({
31
+ ok: true,
32
+ data: hasBody ? body : undefined
33
+ });
34
+ });
35
+ });
36
+ }
37
+
38
+ async function nodeHTTPRequestHandler(opts) {
39
+ const handleViaMiddleware = opts.middleware ?? ((_req, _res, next)=>{
40
+ return next();
41
+ });
42
+ return handleViaMiddleware(opts.req, opts.res, async (err)=>{
43
+ if (err) {
44
+ throw err;
45
+ }
46
+ //
47
+ // Build tRPC dependencies
48
+ async function createContext() {
49
+ return await opts.createContext?.(opts);
50
+ }
51
+ const bodyResult = await getPostBody(opts);
52
+ const query = opts.req.query ? new URLSearchParams(opts.req.query) : new URLSearchParams(opts.req.url.split('?')[1]);
53
+ const req = {
54
+ method: opts.req.method,
55
+ headers: opts.req.headers,
56
+ query,
57
+ body: bodyResult.ok ? bodyResult.data : undefined
58
+ };
59
+ //
60
+ // Invoke tRPC
61
+ const result = await resolveHTTPResponse({
62
+ batching: opts.batching,
63
+ responseMeta: opts.responseMeta,
64
+ path: opts.path,
65
+ createContext,
66
+ router: opts.router,
67
+ req,
68
+ error: bodyResult.ok ? null : bodyResult.error,
69
+ onError (o) {
70
+ opts?.onError?.({
71
+ ...o,
72
+ req: opts.req
73
+ });
74
+ }
75
+ });
76
+ //
77
+ // Handle result
78
+ const { res } = opts;
79
+ if ('status' in result && (!res.statusCode || res.statusCode === 200)) {
80
+ res.statusCode = result.status;
81
+ }
82
+ for (const [key, value] of Object.entries(result.headers ?? {})){
83
+ if (typeof value === 'undefined') {
84
+ continue;
85
+ }
86
+ res.setHeader(key, value);
87
+ }
88
+ res.end(result.body);
89
+ });
90
+ }
91
+
92
+ export { nodeHTTPRequestHandler as n };
@@ -0,0 +1,94 @@
1
+ 'use strict';
2
+
3
+ var resolveHTTPResponse = require('./resolveHTTPResponse-4798ae79.js');
4
+ var TRPCError = require('./TRPCError-98f26ad6.js');
5
+
6
+ async function getPostBody(opts) {
7
+ const { req , maxBodySize =Infinity } = opts;
8
+ return new Promise((resolve)=>{
9
+ if ('body' in req) {
10
+ resolve({
11
+ ok: true,
12
+ data: req.body
13
+ });
14
+ return;
15
+ }
16
+ let body = '';
17
+ let hasBody = false;
18
+ req.on('data', function(data) {
19
+ body += data;
20
+ hasBody = true;
21
+ if (body.length > maxBodySize) {
22
+ resolve({
23
+ ok: false,
24
+ error: new TRPCError.TRPCError({
25
+ code: 'PAYLOAD_TOO_LARGE'
26
+ })
27
+ });
28
+ req.socket.destroy();
29
+ }
30
+ });
31
+ req.on('end', ()=>{
32
+ resolve({
33
+ ok: true,
34
+ data: hasBody ? body : undefined
35
+ });
36
+ });
37
+ });
38
+ }
39
+
40
+ async function nodeHTTPRequestHandler(opts) {
41
+ const handleViaMiddleware = opts.middleware ?? ((_req, _res, next)=>{
42
+ return next();
43
+ });
44
+ return handleViaMiddleware(opts.req, opts.res, async (err)=>{
45
+ if (err) {
46
+ throw err;
47
+ }
48
+ //
49
+ // Build tRPC dependencies
50
+ async function createContext() {
51
+ return await opts.createContext?.(opts);
52
+ }
53
+ const bodyResult = await getPostBody(opts);
54
+ const query = opts.req.query ? new URLSearchParams(opts.req.query) : new URLSearchParams(opts.req.url.split('?')[1]);
55
+ const req = {
56
+ method: opts.req.method,
57
+ headers: opts.req.headers,
58
+ query,
59
+ body: bodyResult.ok ? bodyResult.data : undefined
60
+ };
61
+ //
62
+ // Invoke tRPC
63
+ const result = await resolveHTTPResponse.resolveHTTPResponse({
64
+ batching: opts.batching,
65
+ responseMeta: opts.responseMeta,
66
+ path: opts.path,
67
+ createContext,
68
+ router: opts.router,
69
+ req,
70
+ error: bodyResult.ok ? null : bodyResult.error,
71
+ onError (o) {
72
+ opts?.onError?.({
73
+ ...o,
74
+ req: opts.req
75
+ });
76
+ }
77
+ });
78
+ //
79
+ // Handle result
80
+ const { res } = opts;
81
+ if ('status' in result && (!res.statusCode || res.statusCode === 200)) {
82
+ res.statusCode = result.status;
83
+ }
84
+ for (const [key, value] of Object.entries(result.headers ?? {})){
85
+ if (typeof value === 'undefined') {
86
+ continue;
87
+ }
88
+ res.setHeader(key, value);
89
+ }
90
+ res.end(result.body);
91
+ });
92
+ }
93
+
94
+ exports.nodeHTTPRequestHandler = nodeHTTPRequestHandler;
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
- var config = require('./config-bd759464.js');
4
- var TRPCError = require('./TRPCError-4cbab0d0.js');
3
+ var config = require('./config-046e82fc.js');
4
+ var TRPCError = require('./TRPCError-98f26ad6.js');
5
5
  var transformTRPCResponse = require('./transformTRPCResponse-d2700b72.js');
6
6
 
7
7
  const HTTP_METHOD_PROCEDURE_TYPE_MAP = {
@@ -1,5 +1,5 @@
1
- import { e as callProcedure, f as getHTTPStatusCode } from './config-fb5f49a1.mjs';
2
- import { T as TRPCError, a as getTRPCErrorFromUnknown, g as getCauseFromUnknown } from './TRPCError-e7333eae.mjs';
1
+ import { e as callProcedure, f as getHTTPStatusCode } from './config-61d48858.mjs';
2
+ import { T as TRPCError, a as getTRPCErrorFromUnknown, g as getCauseFromUnknown } from './TRPCError-226f5343.mjs';
3
3
  import { t as transformTRPCResponse } from './transformTRPCResponse-7a73a2df.mjs';
4
4
 
5
5
  const HTTP_METHOD_PROCEDURE_TYPE_MAP = {
@@ -1,5 +1,5 @@
1
- import { e as callProcedure, f as getHTTPStatusCode } from './config-69ee2314.js';
2
- import { T as TRPCError, a as getTRPCErrorFromUnknown, g as getCauseFromUnknown } from './TRPCError-ed16b3b7.js';
1
+ import { e as callProcedure, f as getHTTPStatusCode } from './config-cac4535e.js';
2
+ import { T as TRPCError, a as getTRPCErrorFromUnknown, g as getCauseFromUnknown } from './TRPCError-92d93cfd.js';
3
3
  import { t as transformTRPCResponse } from './transformTRPCResponse-896669e0.js';
4
4
 
5
5
  /* eslint-disable @typescript-eslint/no-non-null-assertion */
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var TRPCError = require('./TRPCError-4cbab0d0.js');
5
+ var TRPCError = require('./TRPCError-98f26ad6.js');
6
6
  var observable = require('./observable-464116ac.js');
7
7
 
8
8
  /**
@@ -1,4 +1,4 @@
1
- import { a as getTRPCErrorFromUnknown } from './TRPCError-e7333eae.mjs';
1
+ import { a as getTRPCErrorFromUnknown } from './TRPCError-226f5343.mjs';
2
2
  import { o as observable } from './observable-ade1bad8.mjs';
3
3
 
4
4
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trpc/server",
3
- "version": "10.13.2",
3
+ "version": "10.14.1",
4
4
  "description": "tRPC Server",
5
5
  "author": "KATT",
6
6
  "license": "MIT",
@@ -133,6 +133,7 @@
133
133
  "@types/hash-sum": "^1.0.0",
134
134
  "@types/node": "^18.7.20",
135
135
  "@types/react": "^18.0.9",
136
+ "@types/react-dom": "^18.0.5",
136
137
  "@types/ws": "^8.2.0",
137
138
  "aws-lambda": "^1.0.7",
138
139
  "devalue": "^4.0.0",
@@ -156,5 +157,5 @@
156
157
  "yup": "^1.0.0",
157
158
  "zod": "^3.0.0"
158
159
  },
159
- "gitHead": "5f4fb9641e1f43125f977b3f84268ac7e5884715"
160
+ "gitHead": "c20c66596798ea772f7684f0319b0dffdde4af2a"
160
161
  }
@@ -99,6 +99,10 @@ export function getHTTPMethod(event: APIGatewayEvent) {
99
99
 
100
100
  export function getPath(event: APIGatewayEvent) {
101
101
  if (isPayloadV1(event)) {
102
+ if (!event.pathParameters) {
103
+ // Then this event was not triggered by a resource denoted with {proxy+}
104
+ return event.path.split('/').pop() || '';
105
+ }
102
106
  const matches = event.resource.matchAll(/\{(.*?)\}/g);
103
107
  for (const match of matches) {
104
108
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
@@ -25,49 +25,72 @@ export async function nodeHTTPRequestHandler<
25
25
  TRequest extends NodeHTTPRequest,
26
26
  TResponse extends NodeHTTPResponse,
27
27
  >(opts: NodeHTTPRequestHandlerOptions<TRouter, TRequest, TResponse>) {
28
- const createContext = async function _createContext(): Promise<
29
- inferRouterContext<TRouter>
30
- > {
31
- return await opts.createContext?.(opts);
32
- };
33
- const { path, router } = opts;
28
+ const handleViaMiddleware =
29
+ opts.middleware ??
30
+ ((_req, _res, next) => {
31
+ return next();
32
+ });
34
33
 
35
- const bodyResult = await getPostBody(opts);
34
+ return handleViaMiddleware(opts.req, opts.res, async (err) => {
35
+ if (err) {
36
+ throw err;
37
+ }
36
38
 
37
- const query = opts.req.query
38
- ? new URLSearchParams(opts.req.query as any)
39
- : new URLSearchParams(opts.req.url!.split('?')[1]);
40
- const req: HTTPRequest = {
41
- method: opts.req.method!,
42
- headers: opts.req.headers,
43
- query,
44
- body: bodyResult.ok ? bodyResult.data : undefined,
45
- };
46
- const result = await resolveHTTPResponse({
47
- batching: opts.batching,
48
- responseMeta: opts.responseMeta,
49
- path,
50
- createContext,
51
- router,
52
- req,
53
- error: bodyResult.ok ? null : bodyResult.error,
54
- onError(o) {
55
- opts?.onError?.({
56
- ...o,
57
- req: opts.req,
58
- });
59
- },
60
- });
39
+ //
40
+ // Build tRPC dependencies
61
41
 
62
- const { res } = opts;
63
- if ('status' in result && (!res.statusCode || res.statusCode === 200)) {
64
- res.statusCode = result.status;
65
- }
66
- for (const [key, value] of Object.entries(result.headers ?? {})) {
67
- if (typeof value === 'undefined') {
68
- continue;
42
+ async function createContext(): Promise<inferRouterContext<TRouter>> {
43
+ return await opts.createContext?.(opts);
69
44
  }
70
- res.setHeader(key, value);
71
- }
72
- res.end(result.body);
45
+
46
+ const bodyResult = await getPostBody(opts);
47
+
48
+ const query = opts.req.query
49
+ ? new URLSearchParams(opts.req.query as any)
50
+ : new URLSearchParams(opts.req.url!.split('?')[1]);
51
+
52
+ const req: HTTPRequest = {
53
+ method: opts.req.method!,
54
+ headers: opts.req.headers,
55
+ query,
56
+ body: bodyResult.ok ? bodyResult.data : undefined,
57
+ };
58
+
59
+ //
60
+ // Invoke tRPC
61
+
62
+ const result = await resolveHTTPResponse({
63
+ batching: opts.batching,
64
+ responseMeta: opts.responseMeta,
65
+ path: opts.path,
66
+ createContext,
67
+ router: opts.router,
68
+ req,
69
+ error: bodyResult.ok ? null : bodyResult.error,
70
+ onError(o) {
71
+ opts?.onError?.({
72
+ ...o,
73
+ req: opts.req,
74
+ });
75
+ },
76
+ });
77
+
78
+ //
79
+ // Handle result
80
+
81
+ const { res } = opts;
82
+ if ('status' in result && (!res.statusCode || res.statusCode === 200)) {
83
+ res.statusCode = result.status;
84
+ }
85
+
86
+ for (const [key, value] of Object.entries(result.headers ?? {})) {
87
+ if (typeof value === 'undefined') {
88
+ continue;
89
+ }
90
+
91
+ res.setHeader(key, value);
92
+ }
93
+
94
+ res.end(result.body);
95
+ });
73
96
  }
@@ -31,11 +31,38 @@ export type NodeHTTPCreateContextOption<
31
31
  createContext: NodeHTTPCreateContextFn<TRouter, TRequest, TResponse>;
32
32
  };
33
33
 
34
+ /**
35
+ * @internal
36
+ */
37
+ interface ConnectMiddleware<
38
+ TRequest extends NodeHTTPRequest = NodeHTTPRequest,
39
+ TResponse extends NodeHTTPResponse = NodeHTTPResponse,
40
+ > {
41
+ (req: TRequest, res: TResponse, next: (err?: any) => any): void;
42
+ }
43
+
34
44
  export type NodeHTTPHandlerOptions<
35
45
  TRouter extends AnyRouter,
36
46
  TRequest extends NodeHTTPRequest,
37
47
  TResponse extends NodeHTTPResponse,
38
48
  > = HTTPBaseHandlerOptions<TRouter, TRequest> & {
49
+ /**
50
+ * By default, http `OPTIONS` requests are not handled, and CORS headers are not returned.
51
+ *
52
+ * This can be used to handle them manually or via the `cors` npm package: https://www.npmjs.com/package/cors
53
+ *
54
+ * ```ts
55
+ * import cors from 'cors'
56
+ *
57
+ * nodeHTTPRequestHandler({
58
+ * cors: cors()
59
+ * })
60
+ * ```
61
+ *
62
+ * You can also use it for other needs which a connect/node.js compatible middleware can solve,
63
+ * though you might wish to consider an alternative solution like the Express adapter if your needs are complex.
64
+ */
65
+ middleware?: ConnectMiddleware;
39
66
  maxBodySize?: number;
40
67
  } & NodeHTTPCreateContextOption<TRouter, TRequest, TResponse>;
41
68
 
package/src/core/index.ts CHANGED
@@ -3,6 +3,7 @@ export type {
3
3
  ProcedureRecord,
4
4
  ProcedureRouterRecord,
5
5
  CreateRouterInner,
6
+ Router,
6
7
  } from './router';
7
8
  export { callProcedure } from './router';
8
9
  export type {
@@ -6,10 +6,9 @@ import { TRPC_ERROR_CODE_KEY } from '../rpc/codes';
6
6
 
7
7
  export function getTRPCErrorFromUnknown(cause: unknown): TRPCError {
8
8
  const error = getErrorFromUnknown(cause);
9
- // this should ideally be an `instanceof TRPCError` but for some reason that isn't working
10
- // ref https://github.com/trpc/trpc/issues/331
11
- if (error.name === 'TRPCError') {
12
- return cause as TRPCError;
9
+
10
+ if (error instanceof TRPCError) {
11
+ return error;
13
12
  }
14
13
 
15
14
  const trpcError = new TRPCError({
@@ -25,7 +24,7 @@ export function getTRPCErrorFromUnknown(cause: unknown): TRPCError {
25
24
  }
26
25
 
27
26
  export class TRPCError extends Error {
28
- public readonly cause?;
27
+ public readonly cause?: Error;
29
28
  public readonly code;
30
29
 
31
30
  constructor(opts: {
@@ -33,20 +32,16 @@ export class TRPCError extends Error {
33
32
  code: TRPC_ERROR_CODE_KEY;
34
33
  cause?: unknown;
35
34
  }) {
36
- const code = opts.code;
37
35
  const message =
38
- opts.message ?? getMessageFromUnknownError(opts.cause, code);
39
- const cause: Error | undefined =
36
+ opts.message ?? getMessageFromUnknownError(opts.cause, opts.code);
37
+ const cause =
40
38
  opts.cause !== undefined ? getErrorFromUnknown(opts.cause) : undefined;
41
39
 
42
40
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
43
41
  // @ts-ignore https://github.com/tc39/proposal-error-cause
44
42
  super(message, { cause });
45
43
 
46
- this.code = code;
47
- this.cause = cause;
48
- this.name = 'TRPCError';
49
-
50
- Object.setPrototypeOf(this, new.target.prototype);
44
+ this.code = opts.code;
45
+ this.name = this.constructor.name;
51
46
  }
52
47
  }
@@ -1,81 +0,0 @@
1
- 'use strict';
2
-
3
- var resolveHTTPResponse = require('./resolveHTTPResponse-cbfdc43d.js');
4
- var TRPCError = require('./TRPCError-4cbab0d0.js');
5
-
6
- async function getPostBody(opts) {
7
- const { req , maxBodySize =Infinity } = opts;
8
- return new Promise((resolve)=>{
9
- if ('body' in req) {
10
- resolve({
11
- ok: true,
12
- data: req.body
13
- });
14
- return;
15
- }
16
- let body = '';
17
- let hasBody = false;
18
- req.on('data', function(data) {
19
- body += data;
20
- hasBody = true;
21
- if (body.length > maxBodySize) {
22
- resolve({
23
- ok: false,
24
- error: new TRPCError.TRPCError({
25
- code: 'PAYLOAD_TOO_LARGE'
26
- })
27
- });
28
- req.socket.destroy();
29
- }
30
- });
31
- req.on('end', ()=>{
32
- resolve({
33
- ok: true,
34
- data: hasBody ? body : undefined
35
- });
36
- });
37
- });
38
- }
39
-
40
- async function nodeHTTPRequestHandler(opts) {
41
- const createContext = async function _createContext() {
42
- return await opts.createContext?.(opts);
43
- };
44
- const { path , router } = opts;
45
- const bodyResult = await getPostBody(opts);
46
- const query = opts.req.query ? new URLSearchParams(opts.req.query) : new URLSearchParams(opts.req.url.split('?')[1]);
47
- const req = {
48
- method: opts.req.method,
49
- headers: opts.req.headers,
50
- query,
51
- body: bodyResult.ok ? bodyResult.data : undefined
52
- };
53
- const result = await resolveHTTPResponse.resolveHTTPResponse({
54
- batching: opts.batching,
55
- responseMeta: opts.responseMeta,
56
- path,
57
- createContext,
58
- router,
59
- req,
60
- error: bodyResult.ok ? null : bodyResult.error,
61
- onError (o) {
62
- opts?.onError?.({
63
- ...o,
64
- req: opts.req
65
- });
66
- }
67
- });
68
- const { res } = opts;
69
- if ('status' in result && (!res.statusCode || res.statusCode === 200)) {
70
- res.statusCode = result.status;
71
- }
72
- for (const [key, value] of Object.entries(result.headers ?? {})){
73
- if (typeof value === 'undefined') {
74
- continue;
75
- }
76
- res.setHeader(key, value);
77
- }
78
- res.end(result.body);
79
- }
80
-
81
- exports.nodeHTTPRequestHandler = nodeHTTPRequestHandler;
@@ -1,79 +0,0 @@
1
- import { r as resolveHTTPResponse } from './resolveHTTPResponse-8e9dd846.mjs';
2
- import { T as TRPCError } from './TRPCError-e7333eae.mjs';
3
-
4
- async function getPostBody(opts) {
5
- const { req , maxBodySize =Infinity } = opts;
6
- return new Promise((resolve)=>{
7
- if ('body' in req) {
8
- resolve({
9
- ok: true,
10
- data: req.body
11
- });
12
- return;
13
- }
14
- let body = '';
15
- let hasBody = false;
16
- req.on('data', function(data) {
17
- body += data;
18
- hasBody = true;
19
- if (body.length > maxBodySize) {
20
- resolve({
21
- ok: false,
22
- error: new TRPCError({
23
- code: 'PAYLOAD_TOO_LARGE'
24
- })
25
- });
26
- req.socket.destroy();
27
- }
28
- });
29
- req.on('end', ()=>{
30
- resolve({
31
- ok: true,
32
- data: hasBody ? body : undefined
33
- });
34
- });
35
- });
36
- }
37
-
38
- async function nodeHTTPRequestHandler(opts) {
39
- const createContext = async function _createContext() {
40
- return await opts.createContext?.(opts);
41
- };
42
- const { path , router } = opts;
43
- const bodyResult = await getPostBody(opts);
44
- const query = opts.req.query ? new URLSearchParams(opts.req.query) : new URLSearchParams(opts.req.url.split('?')[1]);
45
- const req = {
46
- method: opts.req.method,
47
- headers: opts.req.headers,
48
- query,
49
- body: bodyResult.ok ? bodyResult.data : undefined
50
- };
51
- const result = await resolveHTTPResponse({
52
- batching: opts.batching,
53
- responseMeta: opts.responseMeta,
54
- path,
55
- createContext,
56
- router,
57
- req,
58
- error: bodyResult.ok ? null : bodyResult.error,
59
- onError (o) {
60
- opts?.onError?.({
61
- ...o,
62
- req: opts.req
63
- });
64
- }
65
- });
66
- const { res } = opts;
67
- if ('status' in result && (!res.statusCode || res.statusCode === 200)) {
68
- res.statusCode = result.status;
69
- }
70
- for (const [key, value] of Object.entries(result.headers ?? {})){
71
- if (typeof value === 'undefined') {
72
- continue;
73
- }
74
- res.setHeader(key, value);
75
- }
76
- res.end(result.body);
77
- }
78
-
79
- export { nodeHTTPRequestHandler as n };