@trpc/server 11.0.0-rc.354 → 11.0.0-rc.361

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 (162) 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 +62 -0
  8. package/dist/adapters/aws-lambda/content-type/json/index.mjs +60 -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/concurrentCache.d.ts +7 -0
  17. package/dist/adapters/content-handlers/concurrentCache.d.ts.map +1 -0
  18. package/dist/adapters/content-handlers/concurrentCache.js +17 -0
  19. package/dist/adapters/content-handlers/concurrentCache.mjs +15 -0
  20. package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.d.ts +10 -0
  21. package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.d.ts.map +1 -0
  22. package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.js +33 -0
  23. package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.mjs +31 -0
  24. package/dist/adapters/express.d.ts.map +1 -1
  25. package/dist/adapters/express.js +0 -1
  26. package/dist/adapters/express.mjs +0 -1
  27. package/dist/adapters/fastify/content-type/json/index.d.ts +8 -0
  28. package/dist/adapters/fastify/content-type/json/index.d.ts.map +1 -0
  29. package/dist/adapters/fastify/content-type/json/index.js +62 -0
  30. package/dist/adapters/fastify/content-type/json/index.mjs +60 -0
  31. package/dist/adapters/fastify/fastifyRequestHandler.d.ts +1 -9
  32. package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -1
  33. package/dist/adapters/fastify/fastifyRequestHandler.js +10 -2
  34. package/dist/adapters/fastify/fastifyRequestHandler.mjs +10 -2
  35. package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts +1 -1
  36. package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts.map +1 -1
  37. package/dist/adapters/fastify/types.d.ts +11 -0
  38. package/dist/adapters/fastify/types.d.ts.map +1 -0
  39. package/dist/adapters/fetch/content-type/json/index.d.ts +9 -0
  40. package/dist/adapters/fetch/content-type/json/index.d.ts.map +1 -0
  41. package/dist/adapters/fetch/content-type/json/index.js +55 -0
  42. package/dist/adapters/fetch/content-type/json/index.mjs +53 -0
  43. package/dist/adapters/fetch/fetchRequestHandler.d.ts +1 -5
  44. package/dist/adapters/fetch/fetchRequestHandler.d.ts.map +1 -1
  45. package/dist/adapters/fetch/fetchRequestHandler.js +16 -2
  46. package/dist/adapters/fetch/fetchRequestHandler.mjs +16 -2
  47. package/dist/adapters/fetch/types.d.ts +9 -12
  48. package/dist/adapters/fetch/types.d.ts.map +1 -1
  49. package/dist/adapters/next-app-dir/nextAppDirCaller.d.ts +2 -7
  50. package/dist/adapters/next-app-dir/nextAppDirCaller.d.ts.map +1 -1
  51. package/dist/adapters/next.d.ts.map +1 -1
  52. package/dist/adapters/next.js +0 -1
  53. package/dist/adapters/next.mjs +0 -1
  54. package/dist/adapters/node-http/content-type/form-data/index.d.ts +4 -25
  55. package/dist/adapters/node-http/content-type/form-data/index.d.ts.map +1 -1
  56. package/dist/adapters/node-http/content-type/form-data/index.js +25 -128
  57. package/dist/adapters/node-http/content-type/form-data/index.mjs +25 -103
  58. package/dist/adapters/node-http/content-type/json/getPostBody.d.ts.map +1 -1
  59. package/dist/adapters/node-http/content-type/json/getPostBody.js +4 -12
  60. package/dist/adapters/node-http/content-type/json/getPostBody.mjs +4 -12
  61. package/dist/adapters/node-http/content-type/json/index.d.ts +4 -1
  62. package/dist/adapters/node-http/content-type/json/index.d.ts.map +1 -1
  63. package/dist/adapters/node-http/content-type/json/index.js +62 -10
  64. package/dist/adapters/node-http/content-type/json/index.mjs +62 -10
  65. package/dist/adapters/node-http/content-type/octet/index.d.ts +5 -0
  66. package/dist/adapters/node-http/content-type/octet/index.d.ts.map +1 -0
  67. package/dist/adapters/node-http/content-type/octet/index.js +19 -0
  68. package/dist/adapters/node-http/content-type/octet/index.mjs +17 -0
  69. package/dist/adapters/node-http/content-type/types.d.ts +8 -0
  70. package/dist/adapters/node-http/content-type/types.d.ts.map +1 -0
  71. package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
  72. package/dist/adapters/node-http/nodeHTTPRequestHandler.js +19 -21
  73. package/dist/adapters/node-http/nodeHTTPRequestHandler.mjs +20 -22
  74. package/dist/adapters/node-http/types.d.ts +8 -19
  75. package/dist/adapters/node-http/types.d.ts.map +1 -1
  76. package/dist/adapters/standalone.d.ts.map +1 -1
  77. package/dist/adapters/standalone.js +0 -1
  78. package/dist/adapters/standalone.mjs +0 -1
  79. package/dist/adapters/ws.d.ts +2 -12
  80. package/dist/adapters/ws.d.ts.map +1 -1
  81. package/dist/bundle-analysis.json +241 -230
  82. package/dist/http.js +0 -2
  83. package/dist/http.mjs +0 -1
  84. package/dist/index.js +2 -0
  85. package/dist/index.mjs +1 -0
  86. package/dist/unstable-core-do-not-import/contentTypeParsers.d.ts +16 -0
  87. package/dist/unstable-core-do-not-import/contentTypeParsers.d.ts.map +1 -0
  88. package/dist/unstable-core-do-not-import/contentTypeParsers.js +23 -0
  89. package/dist/unstable-core-do-not-import/contentTypeParsers.mjs +21 -0
  90. package/dist/unstable-core-do-not-import/http/contentType.d.ts +7 -15
  91. package/dist/unstable-core-do-not-import/http/contentType.d.ts.map +1 -1
  92. package/dist/unstable-core-do-not-import/http/index.d.ts +0 -1
  93. package/dist/unstable-core-do-not-import/http/index.d.ts.map +1 -1
  94. package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts +5 -5
  95. package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts.map +1 -1
  96. package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.js +26 -20
  97. package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.mjs +26 -20
  98. package/dist/unstable-core-do-not-import/http/types.d.ts +0 -2
  99. package/dist/unstable-core-do-not-import/http/types.d.ts.map +1 -1
  100. package/dist/unstable-core-do-not-import/rootConfig.d.ts +21 -0
  101. package/dist/unstable-core-do-not-import/rootConfig.d.ts.map +1 -1
  102. package/dist/unstable-core-do-not-import.js +0 -2
  103. package/dist/unstable-core-do-not-import.mjs +0 -1
  104. package/package.json +6 -7
  105. package/src/@trpc/server/http.ts +0 -1
  106. package/src/@trpc/server/index.ts +4 -0
  107. package/src/adapters/aws-lambda/content-type/json/index.ts +108 -0
  108. package/src/adapters/aws-lambda/index.ts +24 -9
  109. package/src/adapters/aws-lambda/utils.ts +21 -15
  110. package/src/adapters/content-handlers/concurrentCache.ts +16 -0
  111. package/src/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.ts +45 -0
  112. package/src/adapters/express.ts +1 -6
  113. package/src/adapters/fastify/content-type/json/index.ts +106 -0
  114. package/src/adapters/fastify/fastifyRequestHandler.ts +15 -21
  115. package/src/adapters/fastify/fastifyTRPCPlugin.ts +1 -1
  116. package/src/adapters/fastify/types.ts +22 -0
  117. package/src/adapters/fetch/content-type/json/index.ts +90 -0
  118. package/src/adapters/fetch/fetchRequestHandler.ts +22 -10
  119. package/src/adapters/fetch/types.ts +22 -15
  120. package/src/adapters/next-app-dir/nextAppDirCaller.ts +2 -9
  121. package/src/adapters/next.ts +1 -6
  122. package/src/adapters/node-http/content-type/form-data/index.ts +33 -159
  123. package/src/adapters/node-http/content-type/json/getPostBody.ts +9 -18
  124. package/src/adapters/node-http/content-type/json/index.ts +98 -9
  125. package/src/adapters/node-http/content-type/octet/index.ts +27 -0
  126. package/src/adapters/node-http/{internals/contentType.ts → content-type/types.ts} +2 -14
  127. package/src/adapters/node-http/nodeHTTPRequestHandler.ts +22 -35
  128. package/src/adapters/node-http/types.ts +46 -46
  129. package/src/adapters/standalone.ts +1 -2
  130. package/src/adapters/ws.ts +9 -14
  131. package/src/unstable-core-do-not-import/contentTypeParsers.ts +37 -0
  132. package/src/unstable-core-do-not-import/http/contentType.ts +10 -85
  133. package/src/unstable-core-do-not-import/http/index.ts +0 -1
  134. package/src/unstable-core-do-not-import/http/resolveHTTPResponse.ts +30 -27
  135. package/src/unstable-core-do-not-import/http/types.ts +0 -2
  136. package/src/unstable-core-do-not-import/rootConfig.ts +31 -0
  137. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.d.ts +0 -73
  138. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.d.ts.map +0 -1
  139. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.js +0 -161
  140. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.mjs +0 -157
  141. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.d.ts +0 -31
  142. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.d.ts.map +0 -1
  143. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.js +0 -29
  144. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.mjs +0 -27
  145. package/dist/adapters/node-http/content-type/form-data/streamSlice.d.ts +0 -16
  146. package/dist/adapters/node-http/content-type/form-data/streamSlice.d.ts.map +0 -1
  147. package/dist/adapters/node-http/content-type/form-data/streamSlice.js +0 -46
  148. package/dist/adapters/node-http/content-type/form-data/streamSlice.mjs +0 -44
  149. package/dist/adapters/node-http/content-type/form-data/uploadHandler.d.ts +0 -45
  150. package/dist/adapters/node-http/content-type/form-data/uploadHandler.d.ts.map +0 -1
  151. package/dist/adapters/node-http/content-type/form-data/uploadHandler.js +0 -30
  152. package/dist/adapters/node-http/content-type/form-data/uploadHandler.mjs +0 -26
  153. package/dist/adapters/node-http/internals/contentType.d.ts +0 -9
  154. package/dist/adapters/node-http/internals/contentType.d.ts.map +0 -1
  155. package/dist/adapters/node-http/internals/contentType.js +0 -8
  156. package/dist/adapters/node-http/internals/contentType.mjs +0 -6
  157. package/dist/unstable-core-do-not-import/http/contentType.js +0 -54
  158. package/dist/unstable-core-do-not-import/http/contentType.mjs +0 -52
  159. package/src/adapters/node-http/content-type/form-data/fileUploadHandler.ts +0 -277
  160. package/src/adapters/node-http/content-type/form-data/memoryUploadHandler.ts +0 -56
  161. package/src/adapters/node-http/content-type/form-data/streamSlice.ts +0 -56
  162. 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: (headers)=>{
8
+ return !!headers.get('content-type')?.startsWith('multipart/form-data');
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: (headers)=>{
6
+ return !!headers.get('content-type')?.startsWith('multipart/form-data');
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;AAElC,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,CAoF7D,CAAC"}
@@ -1,16 +1,68 @@
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');
5
+ var concurrentCache = require('../../../content-handlers/concurrentCache.js');
6
6
  var getPostBody = require('./getPostBody.js');
7
7
 
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
- });
8
+ // @trpc/server
9
+ const getNodeHTTPJSONContentTypeHandler = ()=>{
10
+ const cache = concurrentCache.createConcurrentCache();
11
+ return {
12
+ name: 'node-http-json',
13
+ isMatch: (headers)=>{
14
+ return !!headers.get('content-type')?.startsWith('application/json');
15
+ },
16
+ getInputs: async (opts, info)=>{
17
+ async function getRawProcedureInputOrThrow() {
18
+ const bodyResult = await getPostBody.getPostBody(opts);
19
+ if (!bodyResult.ok) {
20
+ throw bodyResult.error;
21
+ }
22
+ const preprocessedBody = bodyResult.preprocessed;
23
+ const body = bodyResult.data;
24
+ try {
25
+ if (opts.req.method === 'GET') {
26
+ const input = opts.query.get('input');
27
+ if (!input) {
28
+ return undefined;
29
+ }
30
+ return JSON.parse(input);
31
+ }
32
+ if (preprocessedBody || typeof body !== 'string') {
33
+ // Some tools like nextjs may parse json
34
+ // requests before they reach us. So we just use them as is
35
+ return body;
36
+ } else {
37
+ return JSON.parse(body);
38
+ }
39
+ } catch (cause) {
40
+ throw new TRPCError.TRPCError({
41
+ code: 'PARSE_ERROR',
42
+ cause
43
+ });
44
+ }
45
+ }
46
+ const deserializeInputValue = (rawValue, transformer)=>{
47
+ return typeof rawValue !== 'undefined' ? transformer.input.deserialize(rawValue) : rawValue;
48
+ };
49
+ const rawInput = await cache.concurrentSafeGet('rawInput', ()=>getRawProcedureInputOrThrow());
50
+ if (rawInput === undefined) {
51
+ return undefined;
52
+ }
53
+ const transformer = opts.router._def._config.transformer;
54
+ if (!info.isBatchCall) {
55
+ return cache.concurrentSafeGet('input', ()=>deserializeInputValue(rawInput, transformer));
56
+ }
57
+ /* istanbul ignore if */ if (rawInput == null || typeof rawInput !== 'object' || Array.isArray(rawInput)) {
58
+ throw new TRPCError.TRPCError({
59
+ code: 'BAD_REQUEST',
60
+ message: '"input" needs to be an object when doing a batch call'
61
+ });
62
+ }
63
+ return cache.concurrentSafeGet(String(info.batch), ()=>deserializeInputValue(rawInput[info.batch], transformer));
64
+ }
65
+ };
66
+ };
15
67
 
16
- exports.nodeHTTPJSONContentTypeHandler = nodeHTTPJSONContentTypeHandler;
68
+ exports.getNodeHTTPJSONContentTypeHandler = getNodeHTTPJSONContentTypeHandler;
@@ -1,14 +1,66 @@
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';
3
+ import { createConcurrentCache } from '../../../content-handlers/concurrentCache.mjs';
4
4
  import { getPostBody } from './getPostBody.mjs';
5
5
 
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
- });
6
+ // @trpc/server
7
+ const getNodeHTTPJSONContentTypeHandler = ()=>{
8
+ const cache = createConcurrentCache();
9
+ return {
10
+ name: 'node-http-json',
11
+ isMatch: (headers)=>{
12
+ return !!headers.get('content-type')?.startsWith('application/json');
13
+ },
14
+ getInputs: async (opts, info)=>{
15
+ async function getRawProcedureInputOrThrow() {
16
+ const bodyResult = await getPostBody(opts);
17
+ if (!bodyResult.ok) {
18
+ throw bodyResult.error;
19
+ }
20
+ const preprocessedBody = bodyResult.preprocessed;
21
+ const body = bodyResult.data;
22
+ try {
23
+ if (opts.req.method === 'GET') {
24
+ const input = opts.query.get('input');
25
+ if (!input) {
26
+ return undefined;
27
+ }
28
+ return JSON.parse(input);
29
+ }
30
+ if (preprocessedBody || typeof body !== 'string') {
31
+ // Some tools like nextjs may parse json
32
+ // requests before they reach us. So we just use them as is
33
+ return body;
34
+ } else {
35
+ return JSON.parse(body);
36
+ }
37
+ } catch (cause) {
38
+ throw new TRPCError({
39
+ code: 'PARSE_ERROR',
40
+ cause
41
+ });
42
+ }
43
+ }
44
+ const deserializeInputValue = (rawValue, transformer)=>{
45
+ return typeof rawValue !== 'undefined' ? transformer.input.deserialize(rawValue) : rawValue;
46
+ };
47
+ const rawInput = await cache.concurrentSafeGet('rawInput', ()=>getRawProcedureInputOrThrow());
48
+ if (rawInput === undefined) {
49
+ return undefined;
50
+ }
51
+ const transformer = opts.router._def._config.transformer;
52
+ if (!info.isBatchCall) {
53
+ return cache.concurrentSafeGet('input', ()=>deserializeInputValue(rawInput, transformer));
54
+ }
55
+ /* istanbul ignore if */ if (rawInput == null || typeof rawInput !== 'object' || Array.isArray(rawInput)) {
56
+ throw new TRPCError({
57
+ code: 'BAD_REQUEST',
58
+ message: '"input" needs to be an object when doing a batch call'
59
+ });
60
+ }
61
+ return cache.concurrentSafeGet(String(info.batch), ()=>deserializeInputValue(rawInput[info.batch], transformer));
62
+ }
63
+ };
64
+ };
13
65
 
14
- export { nodeHTTPJSONContentTypeHandler };
66
+ 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,CAgB5D,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: (headers)=>{
8
+ return !!headers.get('content-type')?.startsWith('application/octet-stream');
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: (headers)=>{
6
+ return !!headers.get('content-type')?.startsWith('application/octet-stream');
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"}