@trpc/server 11.0.0-rc.359 → 11.0.0-rc.362
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.
- package/dist/@trpc/server/http.d.ts +7 -6
- package/dist/@trpc/server/http.d.ts.map +1 -1
- package/dist/@trpc/server/index.d.ts +1 -1
- package/dist/@trpc/server/index.d.ts.map +1 -1
- package/dist/adapters/aws-lambda/getPlanner.d.ts +13 -0
- package/dist/adapters/aws-lambda/getPlanner.d.ts.map +1 -0
- package/dist/adapters/aws-lambda/getPlanner.js +143 -0
- package/dist/adapters/aws-lambda/getPlanner.mjs +141 -0
- package/dist/adapters/aws-lambda/index.d.ts +12 -9
- package/dist/adapters/aws-lambda/index.d.ts.map +1 -1
- package/dist/adapters/aws-lambda/index.js +6 -65
- package/dist/adapters/aws-lambda/index.mjs +8 -61
- package/dist/adapters/express.d.ts.map +1 -1
- package/dist/adapters/express.js +1 -1
- package/dist/adapters/express.mjs +1 -1
- package/dist/adapters/fastify/fastifyRequestHandler.d.ts +10 -1
- package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -1
- package/dist/adapters/fastify/fastifyRequestHandler.js +12 -55
- package/dist/adapters/fastify/fastifyRequestHandler.mjs +13 -56
- package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts +1 -1
- package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts.map +1 -1
- package/dist/adapters/fetch/fetchRequestHandler.d.ts +1 -5
- package/dist/adapters/fetch/fetchRequestHandler.d.ts.map +1 -1
- package/dist/adapters/fetch/fetchRequestHandler.js +28 -71
- package/dist/adapters/fetch/fetchRequestHandler.mjs +29 -72
- package/dist/adapters/fetch/types.d.ts +9 -12
- package/dist/adapters/fetch/types.d.ts.map +1 -1
- package/dist/adapters/next-app-dir/nextAppDirCaller.d.ts +2 -7
- package/dist/adapters/next-app-dir/nextAppDirCaller.d.ts.map +1 -1
- package/dist/adapters/next.d.ts.map +1 -1
- package/dist/adapters/next.js +1 -2
- package/dist/adapters/next.mjs +1 -2
- package/dist/adapters/node-http/incomingMessageToRequest.d.ts +18 -0
- package/dist/adapters/node-http/incomingMessageToRequest.d.ts.map +1 -0
- package/dist/adapters/node-http/incomingMessageToRequest.js +71 -0
- package/dist/adapters/node-http/incomingMessageToRequest.mjs +69 -0
- package/dist/adapters/node-http/index.d.ts +1 -0
- package/dist/adapters/node-http/index.d.ts.map +1 -1
- package/dist/adapters/node-http/index.js +2 -0
- package/dist/adapters/node-http/index.mjs +1 -0
- package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts +1 -1
- package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
- package/dist/adapters/node-http/nodeHTTPRequestHandler.js +27 -72
- package/dist/adapters/node-http/nodeHTTPRequestHandler.mjs +28 -73
- package/dist/adapters/node-http/types.d.ts +2 -14
- package/dist/adapters/node-http/types.d.ts.map +1 -1
- package/dist/adapters/standalone.d.ts.map +1 -1
- package/dist/adapters/standalone.js +0 -1
- package/dist/adapters/standalone.mjs +0 -1
- package/dist/adapters/ws.d.ts +2 -12
- package/dist/adapters/ws.d.ts.map +1 -1
- package/dist/adapters/ws.js +1 -1
- package/dist/adapters/ws.mjs +1 -1
- package/dist/bundle-analysis.json +173 -259
- package/dist/http.js +4 -4
- package/dist/http.mjs +2 -2
- package/dist/index.js +2 -2
- package/dist/index.mjs +1 -1
- package/dist/unstable-core-do-not-import/http/contentType.d.ts +8 -28
- package/dist/unstable-core-do-not-import/http/contentType.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/http/contentType.js +164 -38
- package/dist/unstable-core-do-not-import/http/contentType.mjs +164 -38
- package/dist/unstable-core-do-not-import/http/contentTypeParsers.d.ts +14 -0
- package/dist/unstable-core-do-not-import/http/contentTypeParsers.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/http/contentTypeParsers.js +14 -0
- package/dist/unstable-core-do-not-import/http/contentTypeParsers.mjs +12 -0
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.d.ts +1 -1
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts +7 -43
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.js +115 -124
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.mjs +116 -125
- package/dist/unstable-core-do-not-import/http/types.d.ts +25 -29
- package/dist/unstable-core-do-not-import/http/types.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/initTRPC.d.ts +1 -1
- package/dist/unstable-core-do-not-import/procedureBuilder.d.ts +1 -3
- package/dist/unstable-core-do-not-import/procedureBuilder.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/procedureBuilder.js +0 -2
- package/dist/unstable-core-do-not-import/procedureBuilder.mjs +1 -2
- package/dist/unstable-core-do-not-import/rootConfig.d.ts +12 -0
- package/dist/unstable-core-do-not-import/rootConfig.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/utils.d.ts +3 -0
- package/dist/unstable-core-do-not-import/utils.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/utils.js +2 -0
- package/dist/unstable-core-do-not-import/utils.mjs +2 -1
- package/dist/unstable-core-do-not-import.d.ts +9 -3
- package/dist/unstable-core-do-not-import.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import.js +11 -9
- package/dist/unstable-core-do-not-import.mjs +7 -6
- package/package.json +3 -14
- package/src/@trpc/server/http.ts +7 -13
- package/src/@trpc/server/index.ts +1 -0
- package/src/adapters/aws-lambda/getPlanner.ts +191 -0
- package/src/adapters/aws-lambda/index.ts +43 -92
- package/src/adapters/express.ts +1 -6
- package/src/adapters/fastify/fastifyRequestHandler.ts +22 -77
- package/src/adapters/fastify/fastifyTRPCPlugin.ts +1 -1
- package/src/adapters/fetch/fetchRequestHandler.ts +34 -98
- package/src/adapters/fetch/types.ts +24 -15
- package/src/adapters/next-app-dir/nextAppDirCaller.ts +2 -9
- package/src/adapters/next.ts +1 -6
- package/src/adapters/node-http/incomingMessageToRequest.ts +94 -0
- package/src/adapters/node-http/index.ts +1 -0
- package/src/adapters/node-http/nodeHTTPRequestHandler.ts +31 -110
- package/src/adapters/node-http/types.ts +9 -19
- package/src/adapters/standalone.ts +1 -2
- package/src/adapters/ws.ts +9 -14
- package/src/unstable-core-do-not-import/http/contentType.ts +199 -82
- package/src/unstable-core-do-not-import/http/contentTypeParsers.ts +29 -0
- package/src/unstable-core-do-not-import/http/getHTTPStatusCode.ts +2 -2
- package/src/unstable-core-do-not-import/http/resolveHTTPResponse.ts +133 -205
- package/src/unstable-core-do-not-import/http/types.ts +25 -32
- package/src/unstable-core-do-not-import/procedureBuilder.ts +2 -4
- package/src/unstable-core-do-not-import/rootConfig.ts +21 -0
- package/src/unstable-core-do-not-import/utils.ts +4 -0
- package/src/unstable-core-do-not-import.ts +9 -3
- package/adapters/node-http/content-type/form-data/index.d.ts +0 -1
- package/adapters/node-http/content-type/form-data/index.js +0 -1
- package/adapters/node-http/content-type/json/index.d.ts +0 -1
- package/adapters/node-http/content-type/json/index.js +0 -1
- package/dist/adapters/aws-lambda/utils.d.ts +0 -40
- package/dist/adapters/aws-lambda/utils.d.ts.map +0 -1
- package/dist/adapters/aws-lambda/utils.js +0 -100
- package/dist/adapters/aws-lambda/utils.mjs +0 -93
- package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.d.ts +0 -73
- package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.d.ts.map +0 -1
- package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.js +0 -161
- package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.mjs +0 -157
- package/dist/adapters/node-http/content-type/form-data/index.d.ts +0 -26
- package/dist/adapters/node-http/content-type/form-data/index.d.ts.map +0 -1
- package/dist/adapters/node-http/content-type/form-data/index.js +0 -135
- package/dist/adapters/node-http/content-type/form-data/index.mjs +0 -108
- package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.d.ts +0 -31
- package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.d.ts.map +0 -1
- package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.js +0 -29
- package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.mjs +0 -27
- package/dist/adapters/node-http/content-type/form-data/streamSlice.d.ts +0 -16
- package/dist/adapters/node-http/content-type/form-data/streamSlice.d.ts.map +0 -1
- package/dist/adapters/node-http/content-type/form-data/streamSlice.js +0 -46
- package/dist/adapters/node-http/content-type/form-data/streamSlice.mjs +0 -44
- package/dist/adapters/node-http/content-type/form-data/uploadHandler.d.ts +0 -45
- package/dist/adapters/node-http/content-type/form-data/uploadHandler.d.ts.map +0 -1
- package/dist/adapters/node-http/content-type/form-data/uploadHandler.js +0 -30
- package/dist/adapters/node-http/content-type/form-data/uploadHandler.mjs +0 -26
- package/dist/adapters/node-http/content-type/json/getPostBody.d.ts +0 -7
- package/dist/adapters/node-http/content-type/json/getPostBody.d.ts.map +0 -1
- package/dist/adapters/node-http/content-type/json/getPostBody.js +0 -53
- package/dist/adapters/node-http/content-type/json/getPostBody.mjs +0 -51
- package/dist/adapters/node-http/content-type/json/index.d.ts +0 -2
- package/dist/adapters/node-http/content-type/json/index.d.ts.map +0 -1
- package/dist/adapters/node-http/content-type/json/index.js +0 -16
- package/dist/adapters/node-http/content-type/json/index.mjs +0 -14
- package/dist/adapters/node-http/internals/contentType.d.ts +0 -9
- package/dist/adapters/node-http/internals/contentType.d.ts.map +0 -1
- package/dist/adapters/node-http/internals/contentType.js +0 -8
- package/dist/adapters/node-http/internals/contentType.mjs +0 -6
- package/dist/unstable-core-do-not-import/http/index.d.ts +0 -12
- package/dist/unstable-core-do-not-import/http/index.d.ts.map +0 -1
- package/src/adapters/aws-lambda/utils.ts +0 -164
- package/src/adapters/node-http/content-type/form-data/fileUploadHandler.ts +0 -277
- package/src/adapters/node-http/content-type/form-data/index.ts +0 -163
- package/src/adapters/node-http/content-type/form-data/memoryUploadHandler.ts +0 -56
- package/src/adapters/node-http/content-type/form-data/streamSlice.ts +0 -56
- package/src/adapters/node-http/content-type/form-data/uploadHandler.ts +0 -89
- package/src/adapters/node-http/content-type/json/getPostBody.ts +0 -58
- package/src/adapters/node-http/content-type/json/index.ts +0 -11
- package/src/adapters/node-http/internals/contentType.ts +0 -31
- package/src/unstable-core-do-not-import/http/index.ts +0 -29
|
@@ -1,163 +0,0 @@
|
|
|
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
|
-
/**
|
|
9
|
-
* @link https://github.com/remix-run/remix/blob/0bcb4a304dd2f08f6032c3bf0c3aa7eb5b976901/packages/remix-server-runtime/formData.ts
|
|
10
|
-
*/
|
|
11
|
-
import * as fs from 'fs/promises';
|
|
12
|
-
import { Readable } from 'node:stream';
|
|
13
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
14
|
-
// @ts-ignore the type definitions for this package are borked
|
|
15
|
-
import { streamMultipart } from '@web3-storage/multipart-parser';
|
|
16
|
-
// @trpc/server
|
|
17
|
-
import { createNodeHTTPContentTypeHandler } from '../../internals/contentType';
|
|
18
|
-
import type { NodeHTTPRequest } from '../../types';
|
|
19
|
-
import { NodeOnDiskFile } from './fileUploadHandler';
|
|
20
|
-
import type { UploadHandler, UploadHandlerPart } from './uploadHandler';
|
|
21
|
-
import { MaxBodySizeExceededError } from './uploadHandler';
|
|
22
|
-
|
|
23
|
-
const utfTextDecoder = new TextDecoder('utf-8');
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Allows you to handle multipart forms (file uploads) for your app.
|
|
27
|
-
* Request body parts with a 'filename' property will be treated as files.
|
|
28
|
-
* The rest will be treated as text.
|
|
29
|
-
* @param request The incoming Node HTTP request
|
|
30
|
-
* @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.
|
|
31
|
-
* @param maxBodySize The maximum size of the request body in bytes. Defaults to Infinity.
|
|
32
|
-
*
|
|
33
|
-
* @link https://remix.run/utils/parse-multipart-form-data
|
|
34
|
-
*/
|
|
35
|
-
async function parseMultipartFormData(
|
|
36
|
-
request: NodeHTTPRequest,
|
|
37
|
-
uploadHandler: UploadHandler,
|
|
38
|
-
maxBodySize = Infinity,
|
|
39
|
-
) {
|
|
40
|
-
const contentType = request.headers['content-type'] ?? '';
|
|
41
|
-
const [type, boundary] = contentType.split(/\s*;\s*boundary=/);
|
|
42
|
-
|
|
43
|
-
if (!boundary || type !== 'multipart/form-data') {
|
|
44
|
-
throw new TypeError('Could not parse content as FormData.');
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const formData = new FormData();
|
|
48
|
-
const parts: AsyncIterable<UploadHandlerPart & { done?: true }> =
|
|
49
|
-
streamMultipart(Readable.toWeb(request), boundary);
|
|
50
|
-
|
|
51
|
-
let currentBodySize = 0;
|
|
52
|
-
|
|
53
|
-
const nodeOnDiskFiles: NodeOnDiskFile[] = [];
|
|
54
|
-
|
|
55
|
-
try {
|
|
56
|
-
for await (const part of parts) {
|
|
57
|
-
if (part.done) break;
|
|
58
|
-
|
|
59
|
-
if (typeof part.filename === 'string') {
|
|
60
|
-
// This is a file, so the uploadHandler function will be called
|
|
61
|
-
|
|
62
|
-
// only pass basename as the multipart/form-data spec recommends
|
|
63
|
-
// https://datatracker.ietf.org/doc/html/rfc7578#section-4.2
|
|
64
|
-
part.filename = part.filename.split(/[/\\]/).pop();
|
|
65
|
-
const value = await uploadHandler(part as Required<typeof part>);
|
|
66
|
-
|
|
67
|
-
if (typeof value === 'undefined' || value === null) {
|
|
68
|
-
continue;
|
|
69
|
-
}
|
|
70
|
-
// add to cleanup array in case of error
|
|
71
|
-
if (value instanceof NodeOnDiskFile) {
|
|
72
|
-
nodeOnDiskFiles.push(value);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// if the combined size of the body exceeds the max size, throw an error
|
|
76
|
-
currentBodySize += value.size;
|
|
77
|
-
if (currentBodySize > maxBodySize) {
|
|
78
|
-
throw new MaxBodySizeExceededError(maxBodySize);
|
|
79
|
-
}
|
|
80
|
-
// add the file to the form data
|
|
81
|
-
formData.append(part.name, value as any);
|
|
82
|
-
} else {
|
|
83
|
-
// This is text, so we'll decode it and add it to the form data
|
|
84
|
-
let textualPart = '';
|
|
85
|
-
for await (const chunk of part.data) {
|
|
86
|
-
// if the combined size of the body exceeds the max size, throw an error
|
|
87
|
-
currentBodySize += chunk.length;
|
|
88
|
-
if (currentBodySize > maxBodySize) {
|
|
89
|
-
throw new MaxBodySizeExceededError(maxBodySize);
|
|
90
|
-
}
|
|
91
|
-
textualPart += utfTextDecoder.decode(chunk);
|
|
92
|
-
}
|
|
93
|
-
// add the text to the form data
|
|
94
|
-
formData.append(part.name, textualPart);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
return formData;
|
|
99
|
-
} catch (e) {
|
|
100
|
-
// clean up any files that were uploaded to disk if an error occurs
|
|
101
|
-
await Promise.all(
|
|
102
|
-
nodeOnDiskFiles.map((file) => fs.unlink(file.getFilePath())),
|
|
103
|
-
);
|
|
104
|
-
throw e;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
function isMultipartFormDataRequest(req: NodeHTTPRequest) {
|
|
109
|
-
const contentTypeHeader = req.headers['content-type'];
|
|
110
|
-
return (
|
|
111
|
-
contentTypeHeader?.startsWith('multipart/form-data') ??
|
|
112
|
-
contentTypeHeader === 'application/x-www-form-urlencoded'
|
|
113
|
-
);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
export const nodeHTTPFormDataContentTypeHandler =
|
|
117
|
-
createNodeHTTPContentTypeHandler({
|
|
118
|
-
isMatch(opts) {
|
|
119
|
-
return isMultipartFormDataRequest(opts.req);
|
|
120
|
-
},
|
|
121
|
-
async getBody(opts) {
|
|
122
|
-
const fields = Object.fromEntries(opts.query);
|
|
123
|
-
|
|
124
|
-
return {
|
|
125
|
-
ok: true,
|
|
126
|
-
data: fields,
|
|
127
|
-
preprocessed: false,
|
|
128
|
-
};
|
|
129
|
-
},
|
|
130
|
-
getInputs(opts) {
|
|
131
|
-
const req = opts.req;
|
|
132
|
-
const unparsedInput = req.query.get('input');
|
|
133
|
-
if (!unparsedInput) {
|
|
134
|
-
return {
|
|
135
|
-
0: undefined,
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
const transformer = opts.router._def._config.transformer;
|
|
139
|
-
|
|
140
|
-
const deserializedInput = transformer.input.deserialize(
|
|
141
|
-
JSON.parse(unparsedInput),
|
|
142
|
-
);
|
|
143
|
-
return {
|
|
144
|
-
0: deserializedInput,
|
|
145
|
-
};
|
|
146
|
-
},
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
export {
|
|
150
|
-
NodeOnDiskFile as experimental_NodeOnDiskFile,
|
|
151
|
-
createFileUploadHandler as experimental_createFileUploadHandler,
|
|
152
|
-
} from './fileUploadHandler';
|
|
153
|
-
export { createMemoryUploadHandler as experimental_createMemoryUploadHandler } from './memoryUploadHandler';
|
|
154
|
-
export {
|
|
155
|
-
MaxBodySizeExceededError,
|
|
156
|
-
MaxPartSizeExceededError,
|
|
157
|
-
composeUploadHandlers as experimental_composeUploadHandlers,
|
|
158
|
-
type UploadHandler,
|
|
159
|
-
} from './uploadHandler';
|
|
160
|
-
export {
|
|
161
|
-
isMultipartFormDataRequest as experimental_isMultipartFormDataRequest,
|
|
162
|
-
parseMultipartFormData as experimental_parseMultipartFormData,
|
|
163
|
-
};
|
|
@@ -1,56 +0,0 @@
|
|
|
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
|
-
/**
|
|
9
|
-
* @link https://github.com/remix-run/remix/blob/0bcb4a304dd2f08f6032c3bf0c3aa7eb5b976901/packages/remix-server-runtime/upload/memoryUploadHandler.ts
|
|
10
|
-
*/
|
|
11
|
-
import type { UploadHandler } from './uploadHandler';
|
|
12
|
-
import { MaxPartSizeExceededError } from './uploadHandler';
|
|
13
|
-
|
|
14
|
-
export type MemoryUploadHandlerFilterArgs = {
|
|
15
|
-
filename?: string;
|
|
16
|
-
contentType: string;
|
|
17
|
-
name: string;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export type MemoryUploadHandlerOptions = {
|
|
21
|
-
/**
|
|
22
|
-
* The maximum upload size allowed. If the size is exceeded an error will be thrown.
|
|
23
|
-
* Defaults to 3000000B (3MB).
|
|
24
|
-
*/
|
|
25
|
-
maxPartSize?: number;
|
|
26
|
-
/**
|
|
27
|
-
*
|
|
28
|
-
* @param filename
|
|
29
|
-
* @param mimetype
|
|
30
|
-
* @param encoding
|
|
31
|
-
*/
|
|
32
|
-
filter?(args: MemoryUploadHandlerFilterArgs): Promise<boolean> | boolean;
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
export function createMemoryUploadHandler({
|
|
36
|
-
filter,
|
|
37
|
-
maxPartSize = 3000000,
|
|
38
|
-
}: MemoryUploadHandlerOptions = {}): UploadHandler {
|
|
39
|
-
return async ({ filename, contentType, name, data }) => {
|
|
40
|
-
if (filter && !(await filter({ filename, contentType, name }))) {
|
|
41
|
-
return undefined;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
let size = 0;
|
|
45
|
-
const chunks = [];
|
|
46
|
-
for await (const chunk of data) {
|
|
47
|
-
size += chunk.byteLength;
|
|
48
|
-
if (size > maxPartSize) {
|
|
49
|
-
throw new MaxPartSizeExceededError(name, maxPartSize);
|
|
50
|
-
}
|
|
51
|
-
chunks.push(chunk);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return new File(chunks, filename, { type: contentType });
|
|
55
|
-
};
|
|
56
|
-
}
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import type { TransformCallback } from 'node:stream';
|
|
2
|
-
import { Transform } from 'node:stream';
|
|
3
|
-
|
|
4
|
-
class SliceStream extends Transform {
|
|
5
|
-
private indexOffset = 0;
|
|
6
|
-
private emitUp = false;
|
|
7
|
-
private emitDown = false;
|
|
8
|
-
|
|
9
|
-
constructor(private startIndex = 0, private endIndex = Infinity) {
|
|
10
|
-
super();
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
override _transform(
|
|
14
|
-
chunk: any,
|
|
15
|
-
_: BufferEncoding,
|
|
16
|
-
done: TransformCallback,
|
|
17
|
-
): void {
|
|
18
|
-
this.indexOffset += chunk.length;
|
|
19
|
-
|
|
20
|
-
if (!this.emitUp && this.indexOffset >= this.startIndex) {
|
|
21
|
-
this.emitUp = true;
|
|
22
|
-
const start = chunk.length - (this.indexOffset - this.startIndex);
|
|
23
|
-
|
|
24
|
-
if (this.indexOffset > this.endIndex) {
|
|
25
|
-
const end = chunk.length - (this.indexOffset - this.endIndex);
|
|
26
|
-
this.emitDown = true;
|
|
27
|
-
this.push(chunk.slice(start, end));
|
|
28
|
-
} else {
|
|
29
|
-
this.push(chunk.slice(start, chunk.length));
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
done();
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
if (this.emitUp && !this.emitDown) {
|
|
37
|
-
if (this.indexOffset >= this.endIndex) {
|
|
38
|
-
this.emitDown = true;
|
|
39
|
-
this.push(
|
|
40
|
-
chunk.slice(0, chunk.length - (this.indexOffset - this.endIndex)),
|
|
41
|
-
);
|
|
42
|
-
} else {
|
|
43
|
-
this.push(chunk);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
done();
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
done();
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export function streamSlice(startIndex = 0, endIndex = Infinity): SliceStream {
|
|
55
|
-
return new SliceStream(startIndex, endIndex);
|
|
56
|
-
}
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import type { NodeOnDiskFile } from './fileUploadHandler';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @link https://github.com/remix-run/remix/blob/0bcb4a304dd2f08f6032c3bf0c3aa7eb5b976901/packages/remix-server-runtime/formData.ts
|
|
5
|
-
*/
|
|
6
|
-
export type UploadHandlerPart = {
|
|
7
|
-
name: string;
|
|
8
|
-
filename?: string;
|
|
9
|
-
contentType: string;
|
|
10
|
-
data: AsyncIterable<Uint8Array>;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export type UploadHandler = (
|
|
14
|
-
part: Required<UploadHandlerPart>,
|
|
15
|
-
) => Promise<File | NodeOnDiskFile | null | undefined>;
|
|
16
|
-
|
|
17
|
-
export function composeUploadHandlers(
|
|
18
|
-
...handlers: UploadHandler[]
|
|
19
|
-
): UploadHandler {
|
|
20
|
-
return async (part) => {
|
|
21
|
-
for (const handler of handlers) {
|
|
22
|
-
const value = await handler(part);
|
|
23
|
-
if (typeof value !== 'undefined' && value !== null) {
|
|
24
|
-
return value;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return undefined;
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export class MaxPartSizeExceededError extends Error {
|
|
33
|
-
constructor(public field: string, public maxBytes: number) {
|
|
34
|
-
super(`Field "${field}" exceeded upload size of ${maxBytes} bytes.`);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export class MaxBodySizeExceededError extends Error {
|
|
39
|
-
constructor(public maxBytes: number) {
|
|
40
|
-
super(`Body exceeded upload size of ${maxBytes} bytes.`);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export type MemoryUploadHandlerFilterArgs = {
|
|
45
|
-
filename?: string;
|
|
46
|
-
contentType: string;
|
|
47
|
-
name: string;
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
export type MemoryUploadHandlerOptions = {
|
|
51
|
-
/**
|
|
52
|
-
* The maximum upload size allowed. If the size is exceeded an error will be thrown.
|
|
53
|
-
* Defaults to 3000000B (3MB).
|
|
54
|
-
*/
|
|
55
|
-
maxPartSize?: number;
|
|
56
|
-
/**
|
|
57
|
-
*
|
|
58
|
-
* @param filename
|
|
59
|
-
* @param mimetype
|
|
60
|
-
* @param encoding
|
|
61
|
-
*/
|
|
62
|
-
filter?(args: MemoryUploadHandlerFilterArgs): Promise<boolean> | boolean;
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* @see
|
|
67
|
-
*/
|
|
68
|
-
export function createMemoryUploadHandler({
|
|
69
|
-
filter,
|
|
70
|
-
maxPartSize = 3000000,
|
|
71
|
-
}: MemoryUploadHandlerOptions = {}): UploadHandler {
|
|
72
|
-
return async ({ filename, contentType, name, data }) => {
|
|
73
|
-
if (filter && !(await filter({ filename, contentType, name }))) {
|
|
74
|
-
return undefined;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
let size = 0;
|
|
78
|
-
const chunks = [];
|
|
79
|
-
for await (const chunk of data) {
|
|
80
|
-
size += chunk.byteLength;
|
|
81
|
-
if (size > maxPartSize) {
|
|
82
|
-
throw new MaxPartSizeExceededError(name, maxPartSize);
|
|
83
|
-
}
|
|
84
|
-
chunks.push(chunk);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
return new File(chunks, filename, { type: contentType });
|
|
88
|
-
};
|
|
89
|
-
}
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
// @trpc/server
|
|
2
|
-
import { TRPCError } from '../../../../@trpc/server';
|
|
3
|
-
import type { BodyResult } from '../../../../@trpc/server/http';
|
|
4
|
-
import type { NodeHTTPRequest } from '../../types';
|
|
5
|
-
|
|
6
|
-
export async function getPostBody(opts: {
|
|
7
|
-
req: NodeHTTPRequest;
|
|
8
|
-
maxBodySize?: number;
|
|
9
|
-
}): Promise<BodyResult> {
|
|
10
|
-
const { req, maxBodySize = Infinity } = opts;
|
|
11
|
-
return new Promise((resolve) => {
|
|
12
|
-
if (
|
|
13
|
-
!req.headers['content-type']?.startsWith('application/json') &&
|
|
14
|
-
(!req.method ||
|
|
15
|
-
(req.method !== 'GET' &&
|
|
16
|
-
req.method !== 'OPTIONS' &&
|
|
17
|
-
req.method !== 'HEAD'))
|
|
18
|
-
) {
|
|
19
|
-
resolve({
|
|
20
|
-
ok: false,
|
|
21
|
-
error: new TRPCError({
|
|
22
|
-
code: 'UNSUPPORTED_MEDIA_TYPE',
|
|
23
|
-
message: 'Invalid Content-Type header (expected application/json)',
|
|
24
|
-
}),
|
|
25
|
-
});
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
if ('body' in req) {
|
|
29
|
-
resolve({
|
|
30
|
-
ok: true,
|
|
31
|
-
data: req.body,
|
|
32
|
-
// If the request headers specifies a content-type, we assume that the body has been preprocessed
|
|
33
|
-
preprocessed:
|
|
34
|
-
!!req.headers['content-type']?.startsWith('application/json'),
|
|
35
|
-
});
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
let body = '';
|
|
39
|
-
let hasBody = false;
|
|
40
|
-
req.on('data', function (data) {
|
|
41
|
-
body += data;
|
|
42
|
-
hasBody = true;
|
|
43
|
-
if (body.length > maxBodySize) {
|
|
44
|
-
resolve({
|
|
45
|
-
ok: false,
|
|
46
|
-
error: new TRPCError({ code: 'PAYLOAD_TOO_LARGE' }),
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
req.on('end', () => {
|
|
51
|
-
resolve({
|
|
52
|
-
ok: true,
|
|
53
|
-
data: hasBody ? body : undefined,
|
|
54
|
-
preprocessed: false,
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
});
|
|
58
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { getJsonContentTypeInputs } from '../../../../@trpc/server/http';
|
|
2
|
-
import { createNodeHTTPContentTypeHandler } from '../../internals/contentType';
|
|
3
|
-
import { getPostBody } from './getPostBody';
|
|
4
|
-
|
|
5
|
-
export const nodeHTTPJSONContentTypeHandler = createNodeHTTPContentTypeHandler({
|
|
6
|
-
isMatch(opts) {
|
|
7
|
-
return !!opts.req.headers['content-type']?.startsWith('application/json');
|
|
8
|
-
},
|
|
9
|
-
getBody: getPostBody,
|
|
10
|
-
getInputs: getJsonContentTypeInputs,
|
|
11
|
-
});
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
// @trpc/server
|
|
2
|
-
import type { AnyRouter } from '../../../@trpc/server';
|
|
3
|
-
// @trpc/server/http
|
|
4
|
-
import type { BaseContentTypeHandler } from '../../../@trpc/server/http';
|
|
5
|
-
import type {
|
|
6
|
-
NodeHTTPRequest,
|
|
7
|
-
NodeHTTPRequestHandlerOptions,
|
|
8
|
-
NodeHTTPResponse,
|
|
9
|
-
} from '../types';
|
|
10
|
-
|
|
11
|
-
export interface NodeHTTPContentTypeHandler<
|
|
12
|
-
TRequest extends NodeHTTPRequest,
|
|
13
|
-
TResponse extends NodeHTTPResponse,
|
|
14
|
-
> extends BaseContentTypeHandler<
|
|
15
|
-
NodeHTTPRequestHandlerOptions<AnyRouter, TRequest, TResponse> & {
|
|
16
|
-
query: URLSearchParams;
|
|
17
|
-
}
|
|
18
|
-
> {}
|
|
19
|
-
|
|
20
|
-
export function createNodeHTTPContentTypeHandler(
|
|
21
|
-
contentTypeHandler: NodeHTTPContentTypeHandler<
|
|
22
|
-
NodeHTTPRequest,
|
|
23
|
-
NodeHTTPResponse
|
|
24
|
-
>,
|
|
25
|
-
) {
|
|
26
|
-
return <
|
|
27
|
-
TRequest extends NodeHTTPRequest,
|
|
28
|
-
TResponse extends NodeHTTPResponse,
|
|
29
|
-
>(): NodeHTTPContentTypeHandler<TRequest, TResponse> =>
|
|
30
|
-
contentTypeHandler as any;
|
|
31
|
-
}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
export {
|
|
2
|
-
getHTTPStatusCode,
|
|
3
|
-
getHTTPStatusCodeFromError,
|
|
4
|
-
} from './getHTTPStatusCode';
|
|
5
|
-
export { resolveHTTPResponse } from './resolveHTTPResponse';
|
|
6
|
-
export type {
|
|
7
|
-
BaseHandlerOptions,
|
|
8
|
-
HTTPBaseHandlerOptions,
|
|
9
|
-
HTTPHeaders,
|
|
10
|
-
HTTPRequest,
|
|
11
|
-
HTTPResponse,
|
|
12
|
-
/**
|
|
13
|
-
* @deprecated Use `HTTPErrorHandler` instead
|
|
14
|
-
*/
|
|
15
|
-
HTTPErrorHandler as OnErrorFunction,
|
|
16
|
-
HTTPErrorHandler,
|
|
17
|
-
ProcedureCall,
|
|
18
|
-
ResolveHTTPRequestOptionsContextFn,
|
|
19
|
-
ResponseChunk,
|
|
20
|
-
ResponseMeta,
|
|
21
|
-
ResponseMetaFn,
|
|
22
|
-
TRPCRequestInfo,
|
|
23
|
-
} from './types';
|
|
24
|
-
|
|
25
|
-
export { getBatchStreamFormatter } from './batchStreamFormatter';
|
|
26
|
-
export type { BaseContentTypeHandler, BodyResult } from './contentType';
|
|
27
|
-
export { getJsonContentTypeInputs } from './contentType';
|
|
28
|
-
|
|
29
|
-
export { toURL } from './toURL';
|