@trpc/server 11.0.0-rc.348 → 11.0.0-rc.351

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 +1 -0
  2. package/dist/@trpc/server/http.d.ts.map +1 -1
  3. package/dist/@trpc/server/index.d.ts +1 -2
  4. package/dist/@trpc/server/index.d.ts.map +1 -1
  5. package/dist/adapters/aws-lambda/index.d.ts.map +1 -1
  6. package/dist/adapters/aws-lambda/index.js +9 -19
  7. package/dist/adapters/aws-lambda/index.mjs +9 -19
  8. package/dist/adapters/aws-lambda/utils.d.ts +12 -3
  9. package/dist/adapters/aws-lambda/utils.d.ts.map +1 -1
  10. package/dist/adapters/aws-lambda/utils.js +1 -12
  11. package/dist/adapters/aws-lambda/utils.mjs +2 -12
  12. package/dist/adapters/express.d.ts.map +1 -1
  13. package/dist/adapters/express.js +1 -0
  14. package/dist/adapters/express.mjs +1 -0
  15. package/dist/adapters/fastify/fastifyRequestHandler.d.ts +9 -1
  16. package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -1
  17. package/dist/adapters/fastify/fastifyRequestHandler.js +2 -10
  18. package/dist/adapters/fastify/fastifyRequestHandler.mjs +2 -10
  19. package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts +1 -1
  20. package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts.map +1 -1
  21. package/dist/adapters/fetch/fetchRequestHandler.d.ts +5 -1
  22. package/dist/adapters/fetch/fetchRequestHandler.d.ts.map +1 -1
  23. package/dist/adapters/fetch/fetchRequestHandler.js +2 -16
  24. package/dist/adapters/fetch/fetchRequestHandler.mjs +2 -16
  25. package/dist/adapters/fetch/types.d.ts +12 -9
  26. package/dist/adapters/fetch/types.d.ts.map +1 -1
  27. package/dist/adapters/next-app-dir/nextAppDirCaller.d.ts +7 -2
  28. package/dist/adapters/next-app-dir/nextAppDirCaller.d.ts.map +1 -1
  29. package/dist/adapters/next.d.ts.map +1 -1
  30. package/dist/adapters/next.js +1 -0
  31. package/dist/adapters/next.mjs +1 -0
  32. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.d.ts +73 -0
  33. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.d.ts.map +1 -0
  34. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.js +161 -0
  35. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.mjs +157 -0
  36. package/dist/adapters/node-http/content-type/form-data/index.d.ts +25 -4
  37. package/dist/adapters/node-http/content-type/form-data/index.d.ts.map +1 -1
  38. package/dist/adapters/node-http/content-type/form-data/index.js +128 -25
  39. package/dist/adapters/node-http/content-type/form-data/index.mjs +103 -25
  40. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.d.ts +31 -0
  41. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.d.ts.map +1 -0
  42. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.js +29 -0
  43. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.mjs +27 -0
  44. package/dist/adapters/node-http/content-type/form-data/streamSlice.d.ts +16 -0
  45. package/dist/adapters/node-http/content-type/form-data/streamSlice.d.ts.map +1 -0
  46. package/dist/adapters/node-http/content-type/form-data/streamSlice.js +46 -0
  47. package/dist/adapters/node-http/content-type/form-data/streamSlice.mjs +44 -0
  48. package/dist/adapters/node-http/content-type/form-data/uploadHandler.d.ts +45 -0
  49. package/dist/adapters/node-http/content-type/form-data/uploadHandler.d.ts.map +1 -0
  50. package/dist/adapters/node-http/content-type/form-data/uploadHandler.js +30 -0
  51. package/dist/adapters/node-http/content-type/form-data/uploadHandler.mjs +26 -0
  52. package/dist/adapters/node-http/content-type/json/getPostBody.d.ts.map +1 -1
  53. package/dist/adapters/node-http/content-type/json/getPostBody.js +12 -4
  54. package/dist/adapters/node-http/content-type/json/getPostBody.mjs +12 -4
  55. package/dist/adapters/node-http/content-type/json/index.d.ts +1 -4
  56. package/dist/adapters/node-http/content-type/json/index.d.ts.map +1 -1
  57. package/dist/adapters/node-http/content-type/json/index.js +10 -59
  58. package/dist/adapters/node-http/content-type/json/index.mjs +10 -59
  59. package/dist/adapters/node-http/internals/contentType.d.ts +9 -0
  60. package/dist/adapters/node-http/internals/contentType.d.ts.map +1 -0
  61. package/dist/adapters/node-http/internals/contentType.js +8 -0
  62. package/dist/adapters/node-http/internals/contentType.mjs +6 -0
  63. package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
  64. package/dist/adapters/node-http/nodeHTTPRequestHandler.js +21 -19
  65. package/dist/adapters/node-http/nodeHTTPRequestHandler.mjs +22 -20
  66. package/dist/adapters/node-http/types.d.ts +19 -8
  67. package/dist/adapters/node-http/types.d.ts.map +1 -1
  68. package/dist/adapters/standalone.d.ts.map +1 -1
  69. package/dist/adapters/standalone.js +1 -0
  70. package/dist/adapters/standalone.mjs +1 -0
  71. package/dist/adapters/ws.d.ts +12 -2
  72. package/dist/adapters/ws.d.ts.map +1 -1
  73. package/dist/bundle-analysis.json +233 -227
  74. package/dist/http.js +2 -0
  75. package/dist/http.mjs +1 -0
  76. package/dist/index.js +0 -2
  77. package/dist/index.mjs +0 -1
  78. package/dist/unstable-core-do-not-import/http/contentType.d.ts +15 -7
  79. package/dist/unstable-core-do-not-import/http/contentType.d.ts.map +1 -1
  80. package/dist/unstable-core-do-not-import/http/contentType.js +54 -0
  81. package/dist/unstable-core-do-not-import/http/contentType.mjs +52 -0
  82. package/dist/unstable-core-do-not-import/http/index.d.ts +1 -0
  83. package/dist/unstable-core-do-not-import/http/index.d.ts.map +1 -1
  84. package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts +5 -5
  85. package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts.map +1 -1
  86. package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.js +22 -25
  87. package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.mjs +22 -25
  88. package/dist/unstable-core-do-not-import/http/types.d.ts +2 -0
  89. package/dist/unstable-core-do-not-import/http/types.d.ts.map +1 -1
  90. package/dist/unstable-core-do-not-import/rootConfig.d.ts +0 -21
  91. package/dist/unstable-core-do-not-import/rootConfig.d.ts.map +1 -1
  92. package/dist/unstable-core-do-not-import.js +2 -0
  93. package/dist/unstable-core-do-not-import.mjs +1 -0
  94. package/package.json +3 -2
  95. package/src/@trpc/server/http.ts +1 -0
  96. package/src/@trpc/server/index.ts +0 -4
  97. package/src/adapters/aws-lambda/index.ts +9 -24
  98. package/src/adapters/aws-lambda/utils.ts +15 -21
  99. package/src/adapters/express.ts +6 -1
  100. package/src/adapters/fastify/fastifyRequestHandler.ts +21 -15
  101. package/src/adapters/fastify/fastifyTRPCPlugin.ts +1 -1
  102. package/src/adapters/fetch/fetchRequestHandler.ts +10 -22
  103. package/src/adapters/fetch/types.ts +15 -22
  104. package/src/adapters/next-app-dir/nextAppDirCaller.ts +9 -2
  105. package/src/adapters/next.ts +6 -1
  106. package/src/adapters/node-http/content-type/form-data/fileUploadHandler.ts +277 -0
  107. package/src/adapters/node-http/content-type/form-data/index.ts +159 -33
  108. package/src/adapters/node-http/content-type/form-data/memoryUploadHandler.ts +56 -0
  109. package/src/adapters/node-http/content-type/form-data/streamSlice.ts +56 -0
  110. package/src/adapters/node-http/content-type/form-data/uploadHandler.ts +89 -0
  111. package/src/adapters/node-http/content-type/json/getPostBody.ts +18 -9
  112. package/src/adapters/node-http/content-type/json/index.ts +7 -87
  113. package/src/adapters/node-http/{content-type/types.ts → internals/contentType.ts} +14 -2
  114. package/src/adapters/node-http/nodeHTTPRequestHandler.ts +35 -22
  115. package/src/adapters/node-http/types.ts +46 -46
  116. package/src/adapters/standalone.ts +2 -1
  117. package/src/adapters/ws.ts +14 -9
  118. package/src/unstable-core-do-not-import/http/contentType.ts +85 -10
  119. package/src/unstable-core-do-not-import/http/index.ts +1 -0
  120. package/src/unstable-core-do-not-import/http/resolveHTTPResponse.ts +28 -29
  121. package/src/unstable-core-do-not-import/http/types.ts +2 -0
  122. package/src/unstable-core-do-not-import/rootConfig.ts +0 -31
  123. package/dist/adapters/aws-lambda/content-type/json/index.d.ts +0 -10
  124. package/dist/adapters/aws-lambda/content-type/json/index.d.ts.map +0 -1
  125. package/dist/adapters/aws-lambda/content-type/json/index.js +0 -59
  126. package/dist/adapters/aws-lambda/content-type/json/index.mjs +0 -57
  127. package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.d.ts +0 -10
  128. package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.d.ts.map +0 -1
  129. package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.js +0 -33
  130. package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.mjs +0 -31
  131. package/dist/adapters/fastify/content-type/json/index.d.ts +0 -8
  132. package/dist/adapters/fastify/content-type/json/index.d.ts.map +0 -1
  133. package/dist/adapters/fastify/content-type/json/index.js +0 -59
  134. package/dist/adapters/fastify/content-type/json/index.mjs +0 -57
  135. package/dist/adapters/fastify/types.d.ts +0 -11
  136. package/dist/adapters/fastify/types.d.ts.map +0 -1
  137. package/dist/adapters/fetch/content-type/json/index.d.ts +0 -9
  138. package/dist/adapters/fetch/content-type/json/index.d.ts.map +0 -1
  139. package/dist/adapters/fetch/content-type/json/index.js +0 -58
  140. package/dist/adapters/fetch/content-type/json/index.mjs +0 -56
  141. package/dist/adapters/node-http/content-type/octet/index.d.ts +0 -5
  142. package/dist/adapters/node-http/content-type/octet/index.d.ts.map +0 -1
  143. package/dist/adapters/node-http/content-type/octet/index.js +0 -19
  144. package/dist/adapters/node-http/content-type/octet/index.mjs +0 -17
  145. package/dist/adapters/node-http/content-type/types.d.ts +0 -8
  146. package/dist/adapters/node-http/content-type/types.d.ts.map +0 -1
  147. package/dist/unstable-core-do-not-import/contentTypeParsers.d.ts +0 -16
  148. package/dist/unstable-core-do-not-import/contentTypeParsers.d.ts.map +0 -1
  149. package/dist/unstable-core-do-not-import/contentTypeParsers.js +0 -23
  150. package/dist/unstable-core-do-not-import/contentTypeParsers.mjs +0 -21
  151. package/src/adapters/aws-lambda/content-type/json/index.ts +0 -99
  152. package/src/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.ts +0 -45
  153. package/src/adapters/fastify/content-type/json/index.ts +0 -97
  154. package/src/adapters/fastify/types.ts +0 -22
  155. package/src/adapters/fetch/content-type/json/index.ts +0 -94
  156. package/src/adapters/node-http/content-type/octet/index.ts +0 -27
  157. package/src/unstable-core-do-not-import/contentTypeParsers.ts +0 -37
@@ -1,30 +1,108 @@
1
+ import * as fs from 'node:fs/promises';
1
2
  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';
2
10
 
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');
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);
11
67
  }
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;
27
68
  }
28
- });
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
+ });
29
107
 
30
- export { getFormDataContentTypeHandler };
108
+ export { MaxBodySizeExceededError, NodeOnDiskFile as experimental_NodeOnDiskFile, isMultipartFormDataRequest as experimental_isMultipartFormDataRequest, parseMultipartFormData as experimental_parseMultipartFormData, nodeHTTPFormDataContentTypeHandler };
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Copyright 2021 Remix Software Inc.
3
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+ * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
5
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
6
+ */
7
+ /**
8
+ * @link https://github.com/remix-run/remix/blob/0bcb4a304dd2f08f6032c3bf0c3aa7eb5b976901/packages/remix-server-runtime/upload/memoryUploadHandler.ts
9
+ */
10
+ import type { UploadHandler } from './uploadHandler';
11
+ export type MemoryUploadHandlerFilterArgs = {
12
+ filename?: string;
13
+ contentType: string;
14
+ name: string;
15
+ };
16
+ export type MemoryUploadHandlerOptions = {
17
+ /**
18
+ * The maximum upload size allowed. If the size is exceeded an error will be thrown.
19
+ * Defaults to 3000000B (3MB).
20
+ */
21
+ maxPartSize?: number;
22
+ /**
23
+ *
24
+ * @param filename
25
+ * @param mimetype
26
+ * @param encoding
27
+ */
28
+ filter?(args: MemoryUploadHandlerFilterArgs): Promise<boolean> | boolean;
29
+ };
30
+ export declare function createMemoryUploadHandler({ filter, maxPartSize, }?: MemoryUploadHandlerOptions): UploadHandler;
31
+ //# sourceMappingURL=memoryUploadHandler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"memoryUploadHandler.d.ts","sourceRoot":"","sources":["../../../../../src/adapters/node-http/content-type/form-data/memoryUploadHandler.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;GAEG;AACH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAGrD,MAAM,MAAM,6BAA6B,GAAG;IAC1C,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,0BAA0B,GAAG;IACvC;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;;;OAKG;IACH,MAAM,CAAC,CAAC,IAAI,EAAE,6BAA6B,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC;CAC1E,CAAC;AAEF,wBAAgB,yBAAyB,CAAC,EACxC,MAAM,EACN,WAAqB,GACtB,GAAE,0BAA+B,GAAG,aAAa,CAkBjD"}
@@ -0,0 +1,29 @@
1
+ 'use strict';
2
+
3
+ var uploadHandler = require('./uploadHandler.js');
4
+
5
+ function createMemoryUploadHandler({ filter , maxPartSize =3000000 } = {}) {
6
+ return async ({ filename , contentType , name , data })=>{
7
+ if (filter && !await filter({
8
+ filename,
9
+ contentType,
10
+ name
11
+ })) {
12
+ return undefined;
13
+ }
14
+ let size = 0;
15
+ const chunks = [];
16
+ for await (const chunk of data){
17
+ size += chunk.byteLength;
18
+ if (size > maxPartSize) {
19
+ throw new uploadHandler.MaxPartSizeExceededError(name, maxPartSize);
20
+ }
21
+ chunks.push(chunk);
22
+ }
23
+ return new File(chunks, filename, {
24
+ type: contentType
25
+ });
26
+ };
27
+ }
28
+
29
+ exports.createMemoryUploadHandler = createMemoryUploadHandler;
@@ -0,0 +1,27 @@
1
+ import { MaxPartSizeExceededError } from './uploadHandler.mjs';
2
+
3
+ function createMemoryUploadHandler({ filter , maxPartSize =3000000 } = {}) {
4
+ return async ({ filename , contentType , name , data })=>{
5
+ if (filter && !await filter({
6
+ filename,
7
+ contentType,
8
+ name
9
+ })) {
10
+ return undefined;
11
+ }
12
+ let size = 0;
13
+ const chunks = [];
14
+ for await (const chunk of data){
15
+ size += chunk.byteLength;
16
+ if (size > maxPartSize) {
17
+ throw new MaxPartSizeExceededError(name, maxPartSize);
18
+ }
19
+ chunks.push(chunk);
20
+ }
21
+ return new File(chunks, filename, {
22
+ type: contentType
23
+ });
24
+ };
25
+ }
26
+
27
+ export { createMemoryUploadHandler };
@@ -0,0 +1,16 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ import type { TransformCallback } from 'node:stream';
4
+ import { Transform } from 'node:stream';
5
+ declare class SliceStream extends Transform {
6
+ private startIndex;
7
+ private endIndex;
8
+ private indexOffset;
9
+ private emitUp;
10
+ private emitDown;
11
+ constructor(startIndex?: number, endIndex?: number);
12
+ _transform(chunk: any, _: BufferEncoding, done: TransformCallback): void;
13
+ }
14
+ export declare function streamSlice(startIndex?: number, endIndex?: number): SliceStream;
15
+ export {};
16
+ //# sourceMappingURL=streamSlice.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"streamSlice.d.ts","sourceRoot":"","sources":["../../../../../src/adapters/node-http/content-type/form-data/streamSlice.ts"],"names":[],"mappings":";;AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AACrD,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,cAAM,WAAY,SAAQ,SAAS;IAKrB,OAAO,CAAC,UAAU;IAAM,OAAO,CAAC,QAAQ;IAJpD,OAAO,CAAC,WAAW,CAAK;IACxB,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,QAAQ,CAAS;gBAEL,UAAU,SAAI,EAAU,QAAQ,SAAW;IAItD,UAAU,CACjB,KAAK,EAAE,GAAG,EACV,CAAC,EAAE,cAAc,EACjB,IAAI,EAAE,iBAAiB,GACtB,IAAI;CAmCR;AAED,wBAAgB,WAAW,CAAC,UAAU,SAAI,EAAE,QAAQ,SAAW,GAAG,WAAW,CAE5E"}
@@ -0,0 +1,46 @@
1
+ 'use strict';
2
+
3
+ var node_stream = require('node:stream');
4
+
5
+ class SliceStream extends node_stream.Transform {
6
+ _transform(chunk, _, done) {
7
+ this.indexOffset += chunk.length;
8
+ if (!this.emitUp && this.indexOffset >= this.startIndex) {
9
+ this.emitUp = true;
10
+ const start = chunk.length - (this.indexOffset - this.startIndex);
11
+ if (this.indexOffset > this.endIndex) {
12
+ const end = chunk.length - (this.indexOffset - this.endIndex);
13
+ this.emitDown = true;
14
+ this.push(chunk.slice(start, end));
15
+ } else {
16
+ this.push(chunk.slice(start, chunk.length));
17
+ }
18
+ done();
19
+ return;
20
+ }
21
+ if (this.emitUp && !this.emitDown) {
22
+ if (this.indexOffset >= this.endIndex) {
23
+ this.emitDown = true;
24
+ this.push(chunk.slice(0, chunk.length - (this.indexOffset - this.endIndex)));
25
+ } else {
26
+ this.push(chunk);
27
+ }
28
+ done();
29
+ return;
30
+ }
31
+ done();
32
+ }
33
+ constructor(startIndex = 0, endIndex = Infinity){
34
+ super();
35
+ this.startIndex = startIndex;
36
+ this.endIndex = endIndex;
37
+ this.indexOffset = 0;
38
+ this.emitUp = false;
39
+ this.emitDown = false;
40
+ }
41
+ }
42
+ function streamSlice(startIndex = 0, endIndex = Infinity) {
43
+ return new SliceStream(startIndex, endIndex);
44
+ }
45
+
46
+ exports.streamSlice = streamSlice;
@@ -0,0 +1,44 @@
1
+ import { Transform } from 'node:stream';
2
+
3
+ class SliceStream extends Transform {
4
+ _transform(chunk, _, done) {
5
+ this.indexOffset += chunk.length;
6
+ if (!this.emitUp && this.indexOffset >= this.startIndex) {
7
+ this.emitUp = true;
8
+ const start = chunk.length - (this.indexOffset - this.startIndex);
9
+ if (this.indexOffset > this.endIndex) {
10
+ const end = chunk.length - (this.indexOffset - this.endIndex);
11
+ this.emitDown = true;
12
+ this.push(chunk.slice(start, end));
13
+ } else {
14
+ this.push(chunk.slice(start, chunk.length));
15
+ }
16
+ done();
17
+ return;
18
+ }
19
+ if (this.emitUp && !this.emitDown) {
20
+ if (this.indexOffset >= this.endIndex) {
21
+ this.emitDown = true;
22
+ this.push(chunk.slice(0, chunk.length - (this.indexOffset - this.endIndex)));
23
+ } else {
24
+ this.push(chunk);
25
+ }
26
+ done();
27
+ return;
28
+ }
29
+ done();
30
+ }
31
+ constructor(startIndex = 0, endIndex = Infinity){
32
+ super();
33
+ this.startIndex = startIndex;
34
+ this.endIndex = endIndex;
35
+ this.indexOffset = 0;
36
+ this.emitUp = false;
37
+ this.emitDown = false;
38
+ }
39
+ }
40
+ function streamSlice(startIndex = 0, endIndex = Infinity) {
41
+ return new SliceStream(startIndex, endIndex);
42
+ }
43
+
44
+ export { streamSlice };
@@ -0,0 +1,45 @@
1
+ import type { NodeOnDiskFile } from './fileUploadHandler';
2
+ /**
3
+ * @link https://github.com/remix-run/remix/blob/0bcb4a304dd2f08f6032c3bf0c3aa7eb5b976901/packages/remix-server-runtime/formData.ts
4
+ */
5
+ export type UploadHandlerPart = {
6
+ name: string;
7
+ filename?: string;
8
+ contentType: string;
9
+ data: AsyncIterable<Uint8Array>;
10
+ };
11
+ export type UploadHandler = (part: Required<UploadHandlerPart>) => Promise<File | NodeOnDiskFile | null | undefined>;
12
+ export declare function composeUploadHandlers(...handlers: UploadHandler[]): UploadHandler;
13
+ export declare class MaxPartSizeExceededError extends Error {
14
+ field: string;
15
+ maxBytes: number;
16
+ constructor(field: string, maxBytes: number);
17
+ }
18
+ export declare class MaxBodySizeExceededError extends Error {
19
+ maxBytes: number;
20
+ constructor(maxBytes: number);
21
+ }
22
+ export type MemoryUploadHandlerFilterArgs = {
23
+ filename?: string;
24
+ contentType: string;
25
+ name: string;
26
+ };
27
+ export type MemoryUploadHandlerOptions = {
28
+ /**
29
+ * The maximum upload size allowed. If the size is exceeded an error will be thrown.
30
+ * Defaults to 3000000B (3MB).
31
+ */
32
+ maxPartSize?: number;
33
+ /**
34
+ *
35
+ * @param filename
36
+ * @param mimetype
37
+ * @param encoding
38
+ */
39
+ filter?(args: MemoryUploadHandlerFilterArgs): Promise<boolean> | boolean;
40
+ };
41
+ /**
42
+ * @see
43
+ */
44
+ export declare function createMemoryUploadHandler({ filter, maxPartSize, }?: MemoryUploadHandlerOptions): UploadHandler;
45
+ //# sourceMappingURL=uploadHandler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uploadHandler.d.ts","sourceRoot":"","sources":["../../../../../src/adapters/node-http/content-type/form-data/uploadHandler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAE1D;;GAEG;AACH,MAAM,MAAM,iBAAiB,GAAG;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,IAAI,EAAE,aAAa,CAAC,UAAU,CAAC,CAAC;CACjC,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG,CAC1B,IAAI,EAAE,QAAQ,CAAC,iBAAiB,CAAC,KAC9B,OAAO,CAAC,IAAI,GAAG,cAAc,GAAG,IAAI,GAAG,SAAS,CAAC,CAAC;AAEvD,wBAAgB,qBAAqB,CACnC,GAAG,QAAQ,EAAE,aAAa,EAAE,GAC3B,aAAa,CAWf;AAED,qBAAa,wBAAyB,SAAQ,KAAK;IAC9B,KAAK,EAAE,MAAM;IAAS,QAAQ,EAAE,MAAM;gBAAtC,KAAK,EAAE,MAAM,EAAS,QAAQ,EAAE,MAAM;CAG1D;AAED,qBAAa,wBAAyB,SAAQ,KAAK;IAC9B,QAAQ,EAAE,MAAM;gBAAhB,QAAQ,EAAE,MAAM;CAGpC;AAED,MAAM,MAAM,6BAA6B,GAAG;IAC1C,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,0BAA0B,GAAG;IACvC;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;;;OAKG;IACH,MAAM,CAAC,CAAC,IAAI,EAAE,6BAA6B,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC;CAC1E,CAAC;AAEF;;GAEG;AACH,wBAAgB,yBAAyB,CAAC,EACxC,MAAM,EACN,WAAqB,GACtB,GAAE,0BAA+B,GAAG,aAAa,CAkBjD"}
@@ -0,0 +1,30 @@
1
+ 'use strict';
2
+
3
+ function composeUploadHandlers(...handlers) {
4
+ return async (part)=>{
5
+ for (const handler of handlers){
6
+ const value = await handler(part);
7
+ if (typeof value !== 'undefined' && value !== null) {
8
+ return value;
9
+ }
10
+ }
11
+ return undefined;
12
+ };
13
+ }
14
+ class MaxPartSizeExceededError extends Error {
15
+ constructor(field, maxBytes){
16
+ super(`Field "${field}" exceeded upload size of ${maxBytes} bytes.`);
17
+ this.field = field;
18
+ this.maxBytes = maxBytes;
19
+ }
20
+ }
21
+ class MaxBodySizeExceededError extends Error {
22
+ constructor(maxBytes){
23
+ super(`Body exceeded upload size of ${maxBytes} bytes.`);
24
+ this.maxBytes = maxBytes;
25
+ }
26
+ }
27
+
28
+ exports.MaxBodySizeExceededError = MaxBodySizeExceededError;
29
+ exports.MaxPartSizeExceededError = MaxPartSizeExceededError;
30
+ exports.composeUploadHandlers = composeUploadHandlers;
@@ -0,0 +1,26 @@
1
+ function composeUploadHandlers(...handlers) {
2
+ return async (part)=>{
3
+ for (const handler of handlers){
4
+ const value = await handler(part);
5
+ if (typeof value !== 'undefined' && value !== null) {
6
+ return value;
7
+ }
8
+ }
9
+ return undefined;
10
+ };
11
+ }
12
+ class MaxPartSizeExceededError extends Error {
13
+ constructor(field, maxBytes){
14
+ super(`Field "${field}" exceeded upload size of ${maxBytes} bytes.`);
15
+ this.field = field;
16
+ this.maxBytes = maxBytes;
17
+ }
18
+ }
19
+ class MaxBodySizeExceededError extends Error {
20
+ constructor(maxBytes){
21
+ super(`Body exceeded upload size of ${maxBytes} bytes.`);
22
+ this.maxBytes = maxBytes;
23
+ }
24
+ }
25
+
26
+ export { MaxBodySizeExceededError, MaxPartSizeExceededError, composeUploadHandlers };
@@ -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,CAwCtB"}
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"}
@@ -7,6 +7,16 @@ 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
+ }
10
20
  if ('body' in req) {
11
21
  resolve({
12
22
  ok: true,
@@ -18,11 +28,10 @@ async function getPostBody(opts) {
18
28
  }
19
29
  let body = '';
20
30
  let hasBody = false;
21
- function onData(data) {
31
+ req.on('data', function(data) {
22
32
  body += data;
23
33
  hasBody = true;
24
34
  if (body.length > maxBodySize) {
25
- req.off('data', onData);
26
35
  resolve({
27
36
  ok: false,
28
37
  error: new TRPCError.TRPCError({
@@ -30,8 +39,7 @@ async function getPostBody(opts) {
30
39
  })
31
40
  });
32
41
  }
33
- }
34
- req.on('data', onData);
42
+ });
35
43
  req.on('end', ()=>{
36
44
  resolve({
37
45
  ok: true,
@@ -5,6 +5,16 @@ 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
+ }
8
18
  if ('body' in req) {
9
19
  resolve({
10
20
  ok: true,
@@ -16,11 +26,10 @@ async function getPostBody(opts) {
16
26
  }
17
27
  let body = '';
18
28
  let hasBody = false;
19
- function onData(data) {
29
+ req.on('data', function(data) {
20
30
  body += data;
21
31
  hasBody = true;
22
32
  if (body.length > maxBodySize) {
23
- req.off('data', onData);
24
33
  resolve({
25
34
  ok: false,
26
35
  error: new TRPCError({
@@ -28,8 +37,7 @@ async function getPostBody(opts) {
28
37
  })
29
38
  });
30
39
  }
31
- }
32
- req.on('data', onData);
40
+ });
33
41
  req.on('end', ()=>{
34
42
  resolve({
35
43
  ok: true,
@@ -1,5 +1,2 @@
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>;
1
+ export declare const nodeHTTPJSONContentTypeHandler: <TRequest extends import("../..").NodeHTTPRequest, TResponse extends import("../..").NodeHTTPResponse>() => import("../../internals/contentType").NodeHTTPContentTypeHandler<TRequest, TResponse>;
5
2
  //# 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":"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
+ {"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,65 +1,16 @@
1
1
  'use strict';
2
2
 
3
- var TRPCError = require('../../../../unstable-core-do-not-import/error/TRPCError.js');
3
+ var contentType$1 = require('../../../../unstable-core-do-not-import/http/contentType.js');
4
4
  require('../../../../unstable-core-do-not-import/rootConfig.js');
5
+ var contentType = require('../../internals/contentType.js');
5
6
  var getPostBody = require('./getPostBody.js');
6
7
 
7
- // @trpc/server
8
- const getNodeHTTPJSONContentTypeHandler = ()=>({
9
- name: 'node-http-json',
10
- isMatch: (headers)=>{
11
- return !!headers.get('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
- });
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
+ });
64
15
 
65
- exports.getNodeHTTPJSONContentTypeHandler = getNodeHTTPJSONContentTypeHandler;
16
+ exports.nodeHTTPJSONContentTypeHandler = nodeHTTPJSONContentTypeHandler;