@trpc/server 11.0.0-rc.342 → 11.0.0-rc.346

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 (157) hide show
  1. package/dist/@trpc/server/http.d.ts +0 -1
  2. package/dist/@trpc/server/http.d.ts.map +1 -1
  3. package/dist/@trpc/server/index.d.ts +2 -1
  4. package/dist/@trpc/server/index.d.ts.map +1 -1
  5. package/dist/adapters/aws-lambda/content-type/json/index.d.ts +10 -0
  6. package/dist/adapters/aws-lambda/content-type/json/index.d.ts.map +1 -0
  7. package/dist/adapters/aws-lambda/content-type/json/index.js +59 -0
  8. package/dist/adapters/aws-lambda/content-type/json/index.mjs +57 -0
  9. package/dist/adapters/aws-lambda/index.d.ts.map +1 -1
  10. package/dist/adapters/aws-lambda/index.js +19 -9
  11. package/dist/adapters/aws-lambda/index.mjs +19 -9
  12. package/dist/adapters/aws-lambda/utils.d.ts +3 -12
  13. package/dist/adapters/aws-lambda/utils.d.ts.map +1 -1
  14. package/dist/adapters/aws-lambda/utils.js +12 -1
  15. package/dist/adapters/aws-lambda/utils.mjs +12 -2
  16. package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.d.ts +4 -0
  17. package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.d.ts.map +1 -0
  18. package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.js +22 -0
  19. package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.mjs +20 -0
  20. package/dist/adapters/express.d.ts.map +1 -1
  21. package/dist/adapters/express.js +0 -1
  22. package/dist/adapters/express.mjs +0 -1
  23. package/dist/adapters/fastify/content-type/json/index.d.ts +8 -0
  24. package/dist/adapters/fastify/content-type/json/index.d.ts.map +1 -0
  25. package/dist/adapters/fastify/content-type/json/index.js +59 -0
  26. package/dist/adapters/fastify/content-type/json/index.mjs +57 -0
  27. package/dist/adapters/fastify/fastifyRequestHandler.d.ts +1 -9
  28. package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -1
  29. package/dist/adapters/fastify/fastifyRequestHandler.js +10 -2
  30. package/dist/adapters/fastify/fastifyRequestHandler.mjs +10 -2
  31. package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts +1 -1
  32. package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts.map +1 -1
  33. package/dist/adapters/fastify/types.d.ts +11 -0
  34. package/dist/adapters/fastify/types.d.ts.map +1 -0
  35. package/dist/adapters/fetch/content-type/json/index.d.ts +9 -0
  36. package/dist/adapters/fetch/content-type/json/index.d.ts.map +1 -0
  37. package/dist/adapters/fetch/content-type/json/index.js +58 -0
  38. package/dist/adapters/fetch/content-type/json/index.mjs +56 -0
  39. package/dist/adapters/fetch/fetchRequestHandler.d.ts +1 -5
  40. package/dist/adapters/fetch/fetchRequestHandler.d.ts.map +1 -1
  41. package/dist/adapters/fetch/fetchRequestHandler.js +16 -2
  42. package/dist/adapters/fetch/fetchRequestHandler.mjs +16 -2
  43. package/dist/adapters/fetch/types.d.ts +9 -12
  44. package/dist/adapters/fetch/types.d.ts.map +1 -1
  45. package/dist/adapters/next-app-dir/nextAppDirCaller.d.ts +2 -7
  46. package/dist/adapters/next-app-dir/nextAppDirCaller.d.ts.map +1 -1
  47. package/dist/adapters/next.d.ts.map +1 -1
  48. package/dist/adapters/next.js +0 -1
  49. package/dist/adapters/next.mjs +0 -1
  50. package/dist/adapters/node-http/content-type/form-data/index.d.ts +4 -25
  51. package/dist/adapters/node-http/content-type/form-data/index.d.ts.map +1 -1
  52. package/dist/adapters/node-http/content-type/form-data/index.js +25 -128
  53. package/dist/adapters/node-http/content-type/form-data/index.mjs +25 -103
  54. package/dist/adapters/node-http/content-type/json/getPostBody.d.ts.map +1 -1
  55. package/dist/adapters/node-http/content-type/json/getPostBody.js +4 -12
  56. package/dist/adapters/node-http/content-type/json/getPostBody.mjs +4 -12
  57. package/dist/adapters/node-http/content-type/json/index.d.ts +4 -1
  58. package/dist/adapters/node-http/content-type/json/index.d.ts.map +1 -1
  59. package/dist/adapters/node-http/content-type/json/index.js +59 -10
  60. package/dist/adapters/node-http/content-type/json/index.mjs +59 -10
  61. package/dist/adapters/node-http/content-type/octet/index.d.ts +5 -0
  62. package/dist/adapters/node-http/content-type/octet/index.d.ts.map +1 -0
  63. package/dist/adapters/node-http/content-type/octet/index.js +19 -0
  64. package/dist/adapters/node-http/content-type/octet/index.mjs +17 -0
  65. package/dist/adapters/node-http/content-type/types.d.ts +8 -0
  66. package/dist/adapters/node-http/content-type/types.d.ts.map +1 -0
  67. package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
  68. package/dist/adapters/node-http/nodeHTTPRequestHandler.js +19 -21
  69. package/dist/adapters/node-http/nodeHTTPRequestHandler.mjs +20 -22
  70. package/dist/adapters/node-http/types.d.ts +8 -19
  71. package/dist/adapters/node-http/types.d.ts.map +1 -1
  72. package/dist/adapters/standalone.d.ts.map +1 -1
  73. package/dist/adapters/standalone.js +0 -1
  74. package/dist/adapters/standalone.mjs +0 -1
  75. package/dist/adapters/ws.d.ts +2 -12
  76. package/dist/adapters/ws.d.ts.map +1 -1
  77. package/dist/bundle-analysis.json +229 -235
  78. package/dist/http.js +0 -2
  79. package/dist/http.mjs +0 -1
  80. package/dist/index.js +2 -0
  81. package/dist/index.mjs +1 -0
  82. package/dist/unstable-core-do-not-import/contentTypeParsers.d.ts +16 -0
  83. package/dist/unstable-core-do-not-import/contentTypeParsers.d.ts.map +1 -0
  84. package/dist/unstable-core-do-not-import/contentTypeParsers.js +23 -0
  85. package/dist/unstable-core-do-not-import/contentTypeParsers.mjs +21 -0
  86. package/dist/unstable-core-do-not-import/http/contentType.d.ts +6 -14
  87. package/dist/unstable-core-do-not-import/http/contentType.d.ts.map +1 -1
  88. package/dist/unstable-core-do-not-import/http/index.d.ts +0 -1
  89. package/dist/unstable-core-do-not-import/http/index.d.ts.map +1 -1
  90. package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts +5 -5
  91. package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts.map +1 -1
  92. package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.js +25 -22
  93. package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.mjs +25 -22
  94. package/dist/unstable-core-do-not-import/http/types.d.ts +0 -2
  95. package/dist/unstable-core-do-not-import/http/types.d.ts.map +1 -1
  96. package/dist/unstable-core-do-not-import/rootConfig.d.ts +21 -0
  97. package/dist/unstable-core-do-not-import/rootConfig.d.ts.map +1 -1
  98. package/dist/unstable-core-do-not-import.js +0 -2
  99. package/dist/unstable-core-do-not-import.mjs +0 -1
  100. package/package.json +3 -4
  101. package/src/@trpc/server/http.ts +0 -1
  102. package/src/@trpc/server/index.ts +4 -0
  103. package/src/adapters/aws-lambda/content-type/json/index.ts +99 -0
  104. package/src/adapters/aws-lambda/index.ts +24 -9
  105. package/src/adapters/aws-lambda/utils.ts +21 -15
  106. package/src/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.ts +21 -0
  107. package/src/adapters/express.ts +1 -6
  108. package/src/adapters/fastify/content-type/json/index.ts +97 -0
  109. package/src/adapters/fastify/fastifyRequestHandler.ts +15 -21
  110. package/src/adapters/fastify/fastifyTRPCPlugin.ts +1 -1
  111. package/src/adapters/fastify/types.ts +22 -0
  112. package/src/adapters/fetch/content-type/json/index.ts +96 -0
  113. package/src/adapters/fetch/fetchRequestHandler.ts +22 -10
  114. package/src/adapters/fetch/types.ts +22 -15
  115. package/src/adapters/next-app-dir/nextAppDirCaller.ts +2 -9
  116. package/src/adapters/next.ts +1 -6
  117. package/src/adapters/node-http/content-type/form-data/index.ts +36 -159
  118. package/src/adapters/node-http/content-type/json/getPostBody.ts +9 -18
  119. package/src/adapters/node-http/content-type/json/index.ts +85 -5
  120. package/src/adapters/node-http/content-type/octet/index.ts +29 -0
  121. package/src/adapters/node-http/{internals/contentType.ts → content-type/types.ts} +2 -14
  122. package/src/adapters/node-http/nodeHTTPRequestHandler.ts +22 -35
  123. package/src/adapters/node-http/types.ts +46 -46
  124. package/src/adapters/standalone.ts +1 -2
  125. package/src/adapters/ws.ts +9 -14
  126. package/src/unstable-core-do-not-import/contentTypeParsers.ts +37 -0
  127. package/src/unstable-core-do-not-import/http/contentType.ts +9 -84
  128. package/src/unstable-core-do-not-import/http/index.ts +0 -1
  129. package/src/unstable-core-do-not-import/http/resolveHTTPResponse.ts +29 -28
  130. package/src/unstable-core-do-not-import/http/types.ts +0 -2
  131. package/src/unstable-core-do-not-import/rootConfig.ts +31 -0
  132. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.d.ts +0 -73
  133. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.d.ts.map +0 -1
  134. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.js +0 -161
  135. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.mjs +0 -157
  136. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.d.ts +0 -31
  137. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.d.ts.map +0 -1
  138. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.js +0 -29
  139. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.mjs +0 -27
  140. package/dist/adapters/node-http/content-type/form-data/streamSlice.d.ts +0 -16
  141. package/dist/adapters/node-http/content-type/form-data/streamSlice.d.ts.map +0 -1
  142. package/dist/adapters/node-http/content-type/form-data/streamSlice.js +0 -46
  143. package/dist/adapters/node-http/content-type/form-data/streamSlice.mjs +0 -44
  144. package/dist/adapters/node-http/content-type/form-data/uploadHandler.d.ts +0 -45
  145. package/dist/adapters/node-http/content-type/form-data/uploadHandler.d.ts.map +0 -1
  146. package/dist/adapters/node-http/content-type/form-data/uploadHandler.js +0 -30
  147. package/dist/adapters/node-http/content-type/form-data/uploadHandler.mjs +0 -26
  148. package/dist/adapters/node-http/internals/contentType.d.ts +0 -9
  149. package/dist/adapters/node-http/internals/contentType.d.ts.map +0 -1
  150. package/dist/adapters/node-http/internals/contentType.js +0 -8
  151. package/dist/adapters/node-http/internals/contentType.mjs +0 -6
  152. package/dist/unstable-core-do-not-import/http/contentType.js +0 -54
  153. package/dist/unstable-core-do-not-import/http/contentType.mjs +0 -52
  154. package/src/adapters/node-http/content-type/form-data/fileUploadHandler.ts +0 -277
  155. package/src/adapters/node-http/content-type/form-data/memoryUploadHandler.ts +0 -56
  156. package/src/adapters/node-http/content-type/form-data/streamSlice.ts +0 -56
  157. package/src/adapters/node-http/content-type/form-data/uploadHandler.ts +0 -89
@@ -1,135 +1,32 @@
1
1
  'use strict';
2
2
 
3
- var fs = require('node:fs/promises');
4
3
  var node_stream = require('node:stream');
5
- var multipartParser = require('@web3-storage/multipart-parser');
6
- var contentType = require('../../internals/contentType.js');
7
- var fileUploadHandler = require('./fileUploadHandler.js');
8
- var uploadHandler = require('./uploadHandler.js');
9
- var memoryUploadHandler = require('./memoryUploadHandler.js');
10
4
 
11
- function _interopNamespaceDefault(e) {
12
- var n = Object.create(null);
13
- if (e) {
14
- Object.keys(e).forEach(function (k) {
15
- if (k !== 'default') {
16
- var d = Object.getOwnPropertyDescriptor(e, k);
17
- Object.defineProperty(n, k, d.get ? d : {
18
- enumerable: true,
19
- get: function () { return e[k]; }
20
- });
21
- }
22
- });
23
- }
24
- n.default = e;
25
- return Object.freeze(n);
26
- }
27
-
28
- var fs__namespace = /*#__PURE__*/_interopNamespaceDefault(fs);
29
-
30
- const utfTextDecoder = new TextDecoder('utf-8');
31
- /**
32
- * Allows you to handle multipart forms (file uploads) for your app.
33
- * Request body parts with a 'filename' property will be treated as files.
34
- * The rest will be treated as text.
35
- * @param request The incoming Node HTTP request
36
- * @param uploadHandler A function that handles file uploads and returns a value to be used in the request body. If uploaded to disk, the returned value is a NodeOnDiskFile. If uploaded to memory, the returned value is a File.
37
- * @param maxBodySize The maximum size of the request body in bytes. Defaults to Infinity.
38
- *
39
- * @link https://remix.run/utils/parse-multipart-form-data
40
- */ async function parseMultipartFormData(request, uploadHandler$1, maxBodySize = Infinity) {
41
- const contentType = request.headers['content-type'] ?? '';
42
- const [type, boundary] = contentType.split(/\s*;\s*boundary=/);
43
- if (!boundary || type !== 'multipart/form-data') {
44
- throw new TypeError('Could not parse content as FormData.');
45
- }
46
- const formData = new FormData();
47
- const parts = multipartParser.streamMultipart(node_stream.Readable.toWeb(request), boundary);
48
- let currentBodySize = 0;
49
- const nodeOnDiskFiles = [];
50
- try {
51
- for await (const part of parts){
52
- if (part.done) break;
53
- if (typeof part.filename === 'string') {
54
- // This is a file, so the uploadHandler function will be called
55
- // only pass basename as the multipart/form-data spec recommends
56
- // https://datatracker.ietf.org/doc/html/rfc7578#section-4.2
57
- part.filename = part.filename.split(/[/\\]/).pop();
58
- const value = await uploadHandler$1(part);
59
- if (typeof value === 'undefined' || value === null) {
60
- continue;
61
- }
62
- // add to cleanup array in case of error
63
- if (value instanceof fileUploadHandler.NodeOnDiskFile) {
64
- nodeOnDiskFiles.push(value);
65
- }
66
- // if the combined size of the body exceeds the max size, throw an error
67
- currentBodySize += value.size;
68
- if (currentBodySize > maxBodySize) {
69
- throw new uploadHandler.MaxBodySizeExceededError(maxBodySize);
70
- }
71
- // add the file to the form data
72
- formData.append(part.name, value);
73
- } else {
74
- // This is text, so we'll decode it and add it to the form data
75
- let textualPart = '';
76
- for await (const chunk of part.data){
77
- // if the combined size of the body exceeds the max size, throw an error
78
- currentBodySize += chunk.length;
79
- if (currentBodySize > maxBodySize) {
80
- throw new uploadHandler.MaxBodySizeExceededError(maxBodySize);
81
- }
82
- textualPart += utfTextDecoder.decode(chunk);
83
- }
84
- // add the text to the form data
85
- formData.append(part.name, textualPart);
5
+ const getFormDataContentTypeHandler = ()=>({
6
+ name: 'node-http-formdata',
7
+ isMatch (opts) {
8
+ return opts.req.headers['content-type']?.startsWith('multipart/form-data') ?? false;
9
+ },
10
+ async getInputs (opts, inputOpts) {
11
+ if (inputOpts.isBatchCall) {
12
+ throw new Error('Batch calls not supported for form-data');
86
13
  }
14
+ const contentType = opts.req.headers['content-type'];
15
+ if (!contentType) {
16
+ // Should be unreachable given the isMatch check
17
+ throw new Error('No content-type header found');
18
+ }
19
+ const form = await new Request('https://unused.com', {
20
+ method: 'POST',
21
+ headers: {
22
+ 'content-type': contentType
23
+ },
24
+ body: node_stream.Readable.toWeb(opts.req),
25
+ // @ts-expect-error - outdated types? this exists
26
+ duplex: 'half'
27
+ }).formData();
28
+ return form;
87
29
  }
88
- return formData;
89
- } catch (e) {
90
- // clean up any files that were uploaded to disk if an error occurs
91
- await Promise.all(nodeOnDiskFiles.map((file)=>fs__namespace.unlink(file.getFilePath())));
92
- throw e;
93
- }
94
- }
95
- function isMultipartFormDataRequest(req) {
96
- const contentTypeHeader = req.headers['content-type'];
97
- return contentTypeHeader?.startsWith('multipart/form-data') ?? contentTypeHeader === 'application/x-www-form-urlencoded';
98
- }
99
- const nodeHTTPFormDataContentTypeHandler = contentType.createNodeHTTPContentTypeHandler({
100
- isMatch (opts) {
101
- return isMultipartFormDataRequest(opts.req);
102
- },
103
- async getBody (opts) {
104
- const fields = Object.fromEntries(opts.query);
105
- return {
106
- ok: true,
107
- data: fields,
108
- preprocessed: false
109
- };
110
- },
111
- getInputs (opts) {
112
- const req = opts.req;
113
- const unparsedInput = req.query.get('input');
114
- if (!unparsedInput) {
115
- return {
116
- 0: undefined
117
- };
118
- }
119
- const transformer = opts.router._def._config.transformer;
120
- const deserializedInput = transformer.input.deserialize(JSON.parse(unparsedInput));
121
- return {
122
- 0: deserializedInput
123
- };
124
- }
125
- });
30
+ });
126
31
 
127
- exports.experimental_NodeOnDiskFile = fileUploadHandler.NodeOnDiskFile;
128
- exports.experimental_createFileUploadHandler = fileUploadHandler.createFileUploadHandler;
129
- exports.MaxBodySizeExceededError = uploadHandler.MaxBodySizeExceededError;
130
- exports.MaxPartSizeExceededError = uploadHandler.MaxPartSizeExceededError;
131
- exports.experimental_composeUploadHandlers = uploadHandler.composeUploadHandlers;
132
- exports.experimental_createMemoryUploadHandler = memoryUploadHandler.createMemoryUploadHandler;
133
- exports.experimental_isMultipartFormDataRequest = isMultipartFormDataRequest;
134
- exports.experimental_parseMultipartFormData = parseMultipartFormData;
135
- exports.nodeHTTPFormDataContentTypeHandler = nodeHTTPFormDataContentTypeHandler;
32
+ exports.getFormDataContentTypeHandler = getFormDataContentTypeHandler;
@@ -1,108 +1,30 @@
1
- import * as fs from 'node:fs/promises';
2
1
  import { Readable } from 'node:stream';
3
- import { streamMultipart } from '@web3-storage/multipart-parser';
4
- import { createNodeHTTPContentTypeHandler } from '../../internals/contentType.mjs';
5
- import { NodeOnDiskFile } from './fileUploadHandler.mjs';
6
- export { createFileUploadHandler as experimental_createFileUploadHandler } from './fileUploadHandler.mjs';
7
- import { MaxBodySizeExceededError } from './uploadHandler.mjs';
8
- export { MaxPartSizeExceededError, composeUploadHandlers as experimental_composeUploadHandlers } from './uploadHandler.mjs';
9
- export { createMemoryUploadHandler as experimental_createMemoryUploadHandler } from './memoryUploadHandler.mjs';
10
2
 
11
- const utfTextDecoder = new TextDecoder('utf-8');
12
- /**
13
- * Allows you to handle multipart forms (file uploads) for your app.
14
- * Request body parts with a 'filename' property will be treated as files.
15
- * The rest will be treated as text.
16
- * @param request The incoming Node HTTP request
17
- * @param uploadHandler A function that handles file uploads and returns a value to be used in the request body. If uploaded to disk, the returned value is a NodeOnDiskFile. If uploaded to memory, the returned value is a File.
18
- * @param maxBodySize The maximum size of the request body in bytes. Defaults to Infinity.
19
- *
20
- * @link https://remix.run/utils/parse-multipart-form-data
21
- */ async function parseMultipartFormData(request, uploadHandler, maxBodySize = Infinity) {
22
- const contentType = request.headers['content-type'] ?? '';
23
- const [type, boundary] = contentType.split(/\s*;\s*boundary=/);
24
- if (!boundary || type !== 'multipart/form-data') {
25
- throw new TypeError('Could not parse content as FormData.');
26
- }
27
- const formData = new FormData();
28
- const parts = streamMultipart(Readable.toWeb(request), boundary);
29
- let currentBodySize = 0;
30
- const nodeOnDiskFiles = [];
31
- try {
32
- for await (const part of parts){
33
- if (part.done) break;
34
- if (typeof part.filename === 'string') {
35
- // This is a file, so the uploadHandler function will be called
36
- // only pass basename as the multipart/form-data spec recommends
37
- // https://datatracker.ietf.org/doc/html/rfc7578#section-4.2
38
- part.filename = part.filename.split(/[/\\]/).pop();
39
- const value = await uploadHandler(part);
40
- if (typeof value === 'undefined' || value === null) {
41
- continue;
42
- }
43
- // add to cleanup array in case of error
44
- if (value instanceof NodeOnDiskFile) {
45
- nodeOnDiskFiles.push(value);
46
- }
47
- // if the combined size of the body exceeds the max size, throw an error
48
- currentBodySize += value.size;
49
- if (currentBodySize > maxBodySize) {
50
- throw new MaxBodySizeExceededError(maxBodySize);
51
- }
52
- // add the file to the form data
53
- formData.append(part.name, value);
54
- } else {
55
- // This is text, so we'll decode it and add it to the form data
56
- let textualPart = '';
57
- for await (const chunk of part.data){
58
- // if the combined size of the body exceeds the max size, throw an error
59
- currentBodySize += chunk.length;
60
- if (currentBodySize > maxBodySize) {
61
- throw new MaxBodySizeExceededError(maxBodySize);
62
- }
63
- textualPart += utfTextDecoder.decode(chunk);
64
- }
65
- // add the text to the form data
66
- formData.append(part.name, textualPart);
3
+ const getFormDataContentTypeHandler = ()=>({
4
+ name: 'node-http-formdata',
5
+ isMatch (opts) {
6
+ return opts.req.headers['content-type']?.startsWith('multipart/form-data') ?? false;
7
+ },
8
+ async getInputs (opts, inputOpts) {
9
+ if (inputOpts.isBatchCall) {
10
+ throw new Error('Batch calls not supported for form-data');
67
11
  }
12
+ const contentType = opts.req.headers['content-type'];
13
+ if (!contentType) {
14
+ // Should be unreachable given the isMatch check
15
+ throw new Error('No content-type header found');
16
+ }
17
+ const form = await new Request('https://unused.com', {
18
+ method: 'POST',
19
+ headers: {
20
+ 'content-type': contentType
21
+ },
22
+ body: Readable.toWeb(opts.req),
23
+ // @ts-expect-error - outdated types? this exists
24
+ duplex: 'half'
25
+ }).formData();
26
+ return form;
68
27
  }
69
- return formData;
70
- } catch (e) {
71
- // clean up any files that were uploaded to disk if an error occurs
72
- await Promise.all(nodeOnDiskFiles.map((file)=>fs.unlink(file.getFilePath())));
73
- throw e;
74
- }
75
- }
76
- function isMultipartFormDataRequest(req) {
77
- const contentTypeHeader = req.headers['content-type'];
78
- return contentTypeHeader?.startsWith('multipart/form-data') ?? contentTypeHeader === 'application/x-www-form-urlencoded';
79
- }
80
- const nodeHTTPFormDataContentTypeHandler = createNodeHTTPContentTypeHandler({
81
- isMatch (opts) {
82
- return isMultipartFormDataRequest(opts.req);
83
- },
84
- async getBody (opts) {
85
- const fields = Object.fromEntries(opts.query);
86
- return {
87
- ok: true,
88
- data: fields,
89
- preprocessed: false
90
- };
91
- },
92
- getInputs (opts) {
93
- const req = opts.req;
94
- const unparsedInput = req.query.get('input');
95
- if (!unparsedInput) {
96
- return {
97
- 0: undefined
98
- };
99
- }
100
- const transformer = opts.router._def._config.transformer;
101
- const deserializedInput = transformer.input.deserialize(JSON.parse(unparsedInput));
102
- return {
103
- 0: deserializedInput
104
- };
105
- }
106
- });
28
+ });
107
29
 
108
- export { MaxBodySizeExceededError, NodeOnDiskFile as experimental_NodeOnDiskFile, isMultipartFormDataRequest as experimental_isMultipartFormDataRequest, parseMultipartFormData as experimental_parseMultipartFormData, nodeHTTPFormDataContentTypeHandler };
30
+ export { getFormDataContentTypeHandler };
@@ -1 +1 @@
1
- {"version":3,"file":"getPostBody.d.ts","sourceRoot":"","sources":["../../../../../src/adapters/node-http/content-type/json/getPostBody.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,+BAA+B,CAAC;AAChE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAEnD,wBAAsB,WAAW,CAAC,IAAI,EAAE;IACtC,GAAG,EAAE,eAAe,CAAC;IACrB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,GAAG,OAAO,CAAC,UAAU,CAAC,CAiDtB"}
1
+ {"version":3,"file":"getPostBody.d.ts","sourceRoot":"","sources":["../../../../../src/adapters/node-http/content-type/json/getPostBody.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,+BAA+B,CAAC;AAChE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAEnD,wBAAsB,WAAW,CAAC,IAAI,EAAE;IACtC,GAAG,EAAE,eAAe,CAAC;IACrB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,GAAG,OAAO,CAAC,UAAU,CAAC,CAwCtB"}
@@ -7,16 +7,6 @@ require('../../../../unstable-core-do-not-import/rootConfig.js');
7
7
  async function getPostBody(opts) {
8
8
  const { req , maxBodySize =Infinity } = opts;
9
9
  return new Promise((resolve)=>{
10
- if (!req.headers['content-type']?.startsWith('application/json') && (!req.method || req.method !== 'GET' && req.method !== 'OPTIONS' && req.method !== 'HEAD')) {
11
- resolve({
12
- ok: false,
13
- error: new TRPCError.TRPCError({
14
- code: 'UNSUPPORTED_MEDIA_TYPE',
15
- message: 'Invalid Content-Type header (expected application/json)'
16
- })
17
- });
18
- return;
19
- }
20
10
  if ('body' in req) {
21
11
  resolve({
22
12
  ok: true,
@@ -28,10 +18,11 @@ async function getPostBody(opts) {
28
18
  }
29
19
  let body = '';
30
20
  let hasBody = false;
31
- req.on('data', function(data) {
21
+ function onData(data) {
32
22
  body += data;
33
23
  hasBody = true;
34
24
  if (body.length > maxBodySize) {
25
+ req.off('data', onData);
35
26
  resolve({
36
27
  ok: false,
37
28
  error: new TRPCError.TRPCError({
@@ -39,7 +30,8 @@ async function getPostBody(opts) {
39
30
  })
40
31
  });
41
32
  }
42
- });
33
+ }
34
+ req.on('data', onData);
43
35
  req.on('end', ()=>{
44
36
  resolve({
45
37
  ok: true,
@@ -5,16 +5,6 @@ import '../../../../unstable-core-do-not-import/rootConfig.mjs';
5
5
  async function getPostBody(opts) {
6
6
  const { req , maxBodySize =Infinity } = opts;
7
7
  return new Promise((resolve)=>{
8
- if (!req.headers['content-type']?.startsWith('application/json') && (!req.method || req.method !== 'GET' && req.method !== 'OPTIONS' && req.method !== 'HEAD')) {
9
- resolve({
10
- ok: false,
11
- error: new TRPCError({
12
- code: 'UNSUPPORTED_MEDIA_TYPE',
13
- message: 'Invalid Content-Type header (expected application/json)'
14
- })
15
- });
16
- return;
17
- }
18
8
  if ('body' in req) {
19
9
  resolve({
20
10
  ok: true,
@@ -26,10 +16,11 @@ async function getPostBody(opts) {
26
16
  }
27
17
  let body = '';
28
18
  let hasBody = false;
29
- req.on('data', function(data) {
19
+ function onData(data) {
30
20
  body += data;
31
21
  hasBody = true;
32
22
  if (body.length > maxBodySize) {
23
+ req.off('data', onData);
33
24
  resolve({
34
25
  ok: false,
35
26
  error: new TRPCError({
@@ -37,7 +28,8 @@ async function getPostBody(opts) {
37
28
  })
38
29
  });
39
30
  }
40
- });
31
+ }
32
+ req.on('data', onData);
41
33
  req.on('end', ()=>{
42
34
  resolve({
43
35
  ok: true,
@@ -1,2 +1,5 @@
1
- export declare const nodeHTTPJSONContentTypeHandler: <TRequest extends import("../..").NodeHTTPRequest, TResponse extends import("../..").NodeHTTPResponse>() => import("../../internals/contentType").NodeHTTPContentTypeHandler<TRequest, TResponse>;
1
+ import type { AnyRouter } from '../../../../@trpc/server';
2
+ import type { NodeHTTPRequest, NodeHTTPResponse } from '../../types';
3
+ import type { NodeHTTPContentTypeHandler } from '../types';
4
+ export declare const getNodeHTTPJSONContentTypeHandler: <TRouter extends AnyRouter, TRequest extends NodeHTTPRequest, TResponse extends NodeHTTPResponse>() => NodeHTTPContentTypeHandler<TRouter, TRequest, TResponse>;
2
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/adapters/node-http/content-type/json/index.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,8BAA8B,mMAMzC,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/adapters/node-http/content-type/json/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,SAAS,EAEV,MAAM,0BAA0B,CAAC;AAClC,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AACrE,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,UAAU,CAAC;AAG3D,eAAO,MAAM,iCAAiC,EAAE,CAC9C,OAAO,SAAS,SAAS,EACzB,QAAQ,SAAS,eAAe,EAChC,SAAS,SAAS,gBAAgB,OAC7B,0BAA0B,CAAC,OAAO,EAAE,QAAQ,EAAE,SAAS,CA4E5D,CAAC"}
@@ -1,16 +1,65 @@
1
1
  'use strict';
2
2
 
3
- var contentType$1 = require('../../../../unstable-core-do-not-import/http/contentType.js');
3
+ var TRPCError = require('../../../../unstable-core-do-not-import/error/TRPCError.js');
4
4
  require('../../../../unstable-core-do-not-import/rootConfig.js');
5
- var contentType = require('../../internals/contentType.js');
6
5
  var getPostBody = require('./getPostBody.js');
7
6
 
8
- const nodeHTTPJSONContentTypeHandler = contentType.createNodeHTTPContentTypeHandler({
9
- isMatch (opts) {
10
- return !!opts.req.headers['content-type']?.startsWith('application/json');
11
- },
12
- getBody: getPostBody.getPostBody,
13
- getInputs: contentType$1.getJsonContentTypeInputs
14
- });
7
+ // @trpc/server
8
+ const getNodeHTTPJSONContentTypeHandler = ()=>({
9
+ name: 'node-http-json',
10
+ isMatch (opts) {
11
+ return !!opts.req.headers['content-type']?.startsWith('application/json');
12
+ },
13
+ getInputs: async (opts, info)=>{
14
+ const bodyResult = await getPostBody.getPostBody(opts);
15
+ if (!bodyResult.ok) {
16
+ throw bodyResult.error;
17
+ }
18
+ const preprocessedBody = bodyResult.preprocessed;
19
+ const body = bodyResult.data;
20
+ function getRawProcedureInputOrThrow() {
21
+ try {
22
+ if (opts.req.method === 'GET') {
23
+ const input = opts.query.get('input');
24
+ if (!input) {
25
+ return undefined;
26
+ }
27
+ return JSON.parse(input);
28
+ }
29
+ if (preprocessedBody || typeof body !== 'string') {
30
+ // Some tools like nextjs may parse json
31
+ // requests before they reach us. So we just use them as is
32
+ return body;
33
+ } else {
34
+ return JSON.parse(body);
35
+ }
36
+ } catch (cause) {
37
+ throw new TRPCError.TRPCError({
38
+ code: 'PARSE_ERROR',
39
+ cause
40
+ });
41
+ }
42
+ }
43
+ const deserializeInputValue = (rawValue, transformer)=>{
44
+ return typeof rawValue !== 'undefined' ? transformer.input.deserialize(rawValue) : rawValue;
45
+ };
46
+ const rawInput = getRawProcedureInputOrThrow();
47
+ if (rawInput === undefined) {
48
+ return undefined;
49
+ }
50
+ const transformer = opts.router._def._config.transformer;
51
+ if (!info.isBatchCall) {
52
+ return deserializeInputValue(rawInput, transformer);
53
+ }
54
+ /* istanbul ignore if */ if (rawInput == null || typeof rawInput !== 'object' || Array.isArray(rawInput)) {
55
+ throw new TRPCError.TRPCError({
56
+ code: 'BAD_REQUEST',
57
+ message: '"input" needs to be an object when doing a batch call'
58
+ });
59
+ }
60
+ const rawValue = rawInput[info.batch];
61
+ return deserializeInputValue(rawValue, transformer);
62
+ }
63
+ });
15
64
 
16
- exports.nodeHTTPJSONContentTypeHandler = nodeHTTPJSONContentTypeHandler;
65
+ exports.getNodeHTTPJSONContentTypeHandler = getNodeHTTPJSONContentTypeHandler;
@@ -1,14 +1,63 @@
1
- import { getJsonContentTypeInputs } from '../../../../unstable-core-do-not-import/http/contentType.mjs';
1
+ import { TRPCError } from '../../../../unstable-core-do-not-import/error/TRPCError.mjs';
2
2
  import '../../../../unstable-core-do-not-import/rootConfig.mjs';
3
- import { createNodeHTTPContentTypeHandler } from '../../internals/contentType.mjs';
4
3
  import { getPostBody } from './getPostBody.mjs';
5
4
 
6
- const nodeHTTPJSONContentTypeHandler = createNodeHTTPContentTypeHandler({
7
- isMatch (opts) {
8
- return !!opts.req.headers['content-type']?.startsWith('application/json');
9
- },
10
- getBody: getPostBody,
11
- getInputs: getJsonContentTypeInputs
12
- });
5
+ // @trpc/server
6
+ const getNodeHTTPJSONContentTypeHandler = ()=>({
7
+ name: 'node-http-json',
8
+ isMatch (opts) {
9
+ return !!opts.req.headers['content-type']?.startsWith('application/json');
10
+ },
11
+ getInputs: async (opts, info)=>{
12
+ const bodyResult = await getPostBody(opts);
13
+ if (!bodyResult.ok) {
14
+ throw bodyResult.error;
15
+ }
16
+ const preprocessedBody = bodyResult.preprocessed;
17
+ const body = bodyResult.data;
18
+ function getRawProcedureInputOrThrow() {
19
+ try {
20
+ if (opts.req.method === 'GET') {
21
+ const input = opts.query.get('input');
22
+ if (!input) {
23
+ return undefined;
24
+ }
25
+ return JSON.parse(input);
26
+ }
27
+ if (preprocessedBody || typeof body !== 'string') {
28
+ // Some tools like nextjs may parse json
29
+ // requests before they reach us. So we just use them as is
30
+ return body;
31
+ } else {
32
+ return JSON.parse(body);
33
+ }
34
+ } catch (cause) {
35
+ throw new TRPCError({
36
+ code: 'PARSE_ERROR',
37
+ cause
38
+ });
39
+ }
40
+ }
41
+ const deserializeInputValue = (rawValue, transformer)=>{
42
+ return typeof rawValue !== 'undefined' ? transformer.input.deserialize(rawValue) : rawValue;
43
+ };
44
+ const rawInput = getRawProcedureInputOrThrow();
45
+ if (rawInput === undefined) {
46
+ return undefined;
47
+ }
48
+ const transformer = opts.router._def._config.transformer;
49
+ if (!info.isBatchCall) {
50
+ return deserializeInputValue(rawInput, transformer);
51
+ }
52
+ /* istanbul ignore if */ if (rawInput == null || typeof rawInput !== 'object' || Array.isArray(rawInput)) {
53
+ throw new TRPCError({
54
+ code: 'BAD_REQUEST',
55
+ message: '"input" needs to be an object when doing a batch call'
56
+ });
57
+ }
58
+ const rawValue = rawInput[info.batch];
59
+ return deserializeInputValue(rawValue, transformer);
60
+ }
61
+ });
13
62
 
14
- export { nodeHTTPJSONContentTypeHandler };
63
+ export { getNodeHTTPJSONContentTypeHandler };
@@ -0,0 +1,5 @@
1
+ import type { AnyRouter } from '../../../../@trpc/server';
2
+ import type { NodeHTTPRequest, NodeHTTPResponse } from '../../types';
3
+ import type { NodeHTTPContentTypeHandler } from '../types';
4
+ export declare const getOctetContentTypeHandler: <TRouter extends AnyRouter, TRequest extends NodeHTTPRequest, TResponse extends NodeHTTPResponse>() => NodeHTTPContentTypeHandler<TRouter, TRequest, TResponse>;
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/adapters/node-http/content-type/octet/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAC1D,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AACrE,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,UAAU,CAAC;AAE3D,eAAO,MAAM,0BAA0B,EAAE,CACvC,OAAO,SAAS,SAAS,EACzB,QAAQ,SAAS,eAAe,EAChC,SAAS,SAAS,gBAAgB,OAC7B,0BAA0B,CAAC,OAAO,EAAE,QAAQ,EAAE,SAAS,CAkB5D,CAAC"}
@@ -0,0 +1,19 @@
1
+ 'use strict';
2
+
3
+ var node_stream = require('node:stream');
4
+
5
+ const getOctetContentTypeHandler = ()=>({
6
+ name: 'node-http-octet',
7
+ isMatch (opts) {
8
+ return opts.req.headers['content-type']?.startsWith('application/octet-stream') ?? false;
9
+ },
10
+ async getInputs (opts, inputOpts) {
11
+ if (inputOpts.isBatchCall) {
12
+ throw new Error('Batch calls not supported for octet-stream');
13
+ }
14
+ const stream = node_stream.Stream.Readable.toWeb(opts.req);
15
+ return stream;
16
+ }
17
+ });
18
+
19
+ exports.getOctetContentTypeHandler = getOctetContentTypeHandler;
@@ -0,0 +1,17 @@
1
+ import { Stream } from 'node:stream';
2
+
3
+ const getOctetContentTypeHandler = ()=>({
4
+ name: 'node-http-octet',
5
+ isMatch (opts) {
6
+ return opts.req.headers['content-type']?.startsWith('application/octet-stream') ?? false;
7
+ },
8
+ async getInputs (opts, inputOpts) {
9
+ if (inputOpts.isBatchCall) {
10
+ throw new Error('Batch calls not supported for octet-stream');
11
+ }
12
+ const stream = Stream.Readable.toWeb(opts.req);
13
+ return stream;
14
+ }
15
+ });
16
+
17
+ export { getOctetContentTypeHandler };
@@ -0,0 +1,8 @@
1
+ import type { AnyRouter } from '../../../@trpc/server';
2
+ import type { BaseContentTypeHandler } from '../../../@trpc/server/http';
3
+ import type { NodeHTTPRequest, NodeHTTPRequestHandlerOptions, NodeHTTPResponse } from '../types';
4
+ export interface NodeHTTPContentTypeHandler<TRouter extends AnyRouter, TRequest extends NodeHTTPRequest, TResponse extends NodeHTTPResponse> extends BaseContentTypeHandler<NodeHTTPRequestHandlerOptions<TRouter, TRequest, TResponse> & {
5
+ query: URLSearchParams;
6
+ }> {
7
+ }
8
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../src/adapters/node-http/content-type/types.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAEvD,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,4BAA4B,CAAC;AACzE,OAAO,KAAK,EACV,eAAe,EACf,6BAA6B,EAC7B,gBAAgB,EACjB,MAAM,UAAU,CAAC;AAElB,MAAM,WAAW,0BAA0B,CACzC,OAAO,SAAS,SAAS,EACzB,QAAQ,SAAS,eAAe,EAChC,SAAS,SAAS,gBAAgB,CAClC,SAAQ,sBAAsB,CAC5B,6BAA6B,CAAC,OAAO,EAAE,QAAQ,EAAE,SAAS,CAAC,GAAG;IAC5D,KAAK,EAAE,eAAe,CAAC;CACxB,CACF;CAAG"}
@@ -1 +1 @@
1
- {"version":3,"file":"nodeHTTPRequestHandler.d.ts","sourceRoot":"","sources":["../../../src/adapters/node-http/nodeHTTPRequestHandler.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAapD,OAAO,KAAK,EACV,eAAe,EACf,6BAA6B,EAC7B,gBAAgB,EACjB,MAAM,SAAS,CAAC;AAIjB,wBAAsB,sBAAsB,CAC1C,OAAO,SAAS,SAAS,EACzB,QAAQ,SAAS,eAAe,EAChC,SAAS,SAAS,gBAAgB,EAClC,IAAI,EAAE,6BAA6B,CAAC,OAAO,EAAE,QAAQ,EAAE,SAAS,CAAC,iBA2HlE"}
1
+ {"version":3,"file":"nodeHTTPRequestHandler.d.ts","sourceRoot":"","sources":["../../../src/adapters/node-http/nodeHTTPRequestHandler.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAepD,OAAO,KAAK,EACV,eAAe,EACf,6BAA6B,EAC7B,gBAAgB,EACjB,MAAM,SAAS,CAAC;AAEjB,wBAAsB,sBAAsB,CAC1C,OAAO,SAAS,SAAS,EACzB,QAAQ,SAAS,eAAe,EAChC,SAAS,SAAS,gBAAgB,EAClC,IAAI,EAAE,6BAA6B,CAAC,OAAO,EAAE,QAAQ,EAAE,SAAS,CAAC,iBA6GlE"}