@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.
- package/dist/@trpc/server/http.d.ts +0 -1
- package/dist/@trpc/server/http.d.ts.map +1 -1
- package/dist/@trpc/server/index.d.ts +2 -1
- package/dist/@trpc/server/index.d.ts.map +1 -1
- package/dist/adapters/aws-lambda/content-type/json/index.d.ts +10 -0
- package/dist/adapters/aws-lambda/content-type/json/index.d.ts.map +1 -0
- package/dist/adapters/aws-lambda/content-type/json/index.js +62 -0
- package/dist/adapters/aws-lambda/content-type/json/index.mjs +60 -0
- package/dist/adapters/aws-lambda/index.d.ts.map +1 -1
- package/dist/adapters/aws-lambda/index.js +19 -9
- package/dist/adapters/aws-lambda/index.mjs +19 -9
- package/dist/adapters/aws-lambda/utils.d.ts +3 -12
- package/dist/adapters/aws-lambda/utils.d.ts.map +1 -1
- package/dist/adapters/aws-lambda/utils.js +12 -1
- package/dist/adapters/aws-lambda/utils.mjs +12 -2
- package/dist/adapters/content-handlers/concurrentCache.d.ts +7 -0
- package/dist/adapters/content-handlers/concurrentCache.d.ts.map +1 -0
- package/dist/adapters/content-handlers/concurrentCache.js +17 -0
- package/dist/adapters/content-handlers/concurrentCache.mjs +15 -0
- package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.d.ts +10 -0
- package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.d.ts.map +1 -0
- package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.js +33 -0
- package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.mjs +31 -0
- package/dist/adapters/express.d.ts.map +1 -1
- package/dist/adapters/express.js +0 -1
- package/dist/adapters/express.mjs +0 -1
- package/dist/adapters/fastify/content-type/json/index.d.ts +8 -0
- package/dist/adapters/fastify/content-type/json/index.d.ts.map +1 -0
- package/dist/adapters/fastify/content-type/json/index.js +62 -0
- package/dist/adapters/fastify/content-type/json/index.mjs +60 -0
- package/dist/adapters/fastify/fastifyRequestHandler.d.ts +1 -9
- package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -1
- package/dist/adapters/fastify/fastifyRequestHandler.js +10 -2
- package/dist/adapters/fastify/fastifyRequestHandler.mjs +10 -2
- package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts +1 -1
- package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts.map +1 -1
- package/dist/adapters/fastify/types.d.ts +11 -0
- package/dist/adapters/fastify/types.d.ts.map +1 -0
- package/dist/adapters/fetch/content-type/json/index.d.ts +9 -0
- package/dist/adapters/fetch/content-type/json/index.d.ts.map +1 -0
- package/dist/adapters/fetch/content-type/json/index.js +55 -0
- package/dist/adapters/fetch/content-type/json/index.mjs +53 -0
- 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 +16 -2
- package/dist/adapters/fetch/fetchRequestHandler.mjs +16 -2
- 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 +0 -1
- package/dist/adapters/next.mjs +0 -1
- package/dist/adapters/node-http/content-type/form-data/index.d.ts +4 -25
- package/dist/adapters/node-http/content-type/form-data/index.d.ts.map +1 -1
- package/dist/adapters/node-http/content-type/form-data/index.js +25 -128
- package/dist/adapters/node-http/content-type/form-data/index.mjs +25 -103
- package/dist/adapters/node-http/content-type/json/getPostBody.d.ts.map +1 -1
- package/dist/adapters/node-http/content-type/json/getPostBody.js +4 -12
- package/dist/adapters/node-http/content-type/json/getPostBody.mjs +4 -12
- package/dist/adapters/node-http/content-type/json/index.d.ts +4 -1
- package/dist/adapters/node-http/content-type/json/index.d.ts.map +1 -1
- package/dist/adapters/node-http/content-type/json/index.js +62 -10
- package/dist/adapters/node-http/content-type/json/index.mjs +62 -10
- package/dist/adapters/node-http/content-type/octet/index.d.ts +5 -0
- package/dist/adapters/node-http/content-type/octet/index.d.ts.map +1 -0
- package/dist/adapters/node-http/content-type/octet/index.js +19 -0
- package/dist/adapters/node-http/content-type/octet/index.mjs +17 -0
- package/dist/adapters/node-http/content-type/types.d.ts +8 -0
- package/dist/adapters/node-http/content-type/types.d.ts.map +1 -0
- package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
- package/dist/adapters/node-http/nodeHTTPRequestHandler.js +19 -21
- package/dist/adapters/node-http/nodeHTTPRequestHandler.mjs +20 -22
- package/dist/adapters/node-http/types.d.ts +8 -19
- 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/bundle-analysis.json +241 -230
- package/dist/http.js +0 -2
- package/dist/http.mjs +0 -1
- package/dist/index.js +2 -0
- package/dist/index.mjs +1 -0
- package/dist/unstable-core-do-not-import/contentTypeParsers.d.ts +16 -0
- package/dist/unstable-core-do-not-import/contentTypeParsers.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/contentTypeParsers.js +23 -0
- package/dist/unstable-core-do-not-import/contentTypeParsers.mjs +21 -0
- package/dist/unstable-core-do-not-import/http/contentType.d.ts +7 -15
- package/dist/unstable-core-do-not-import/http/contentType.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/http/index.d.ts +0 -1
- package/dist/unstable-core-do-not-import/http/index.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts +5 -5
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.js +26 -20
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.mjs +26 -20
- package/dist/unstable-core-do-not-import/http/types.d.ts +0 -2
- package/dist/unstable-core-do-not-import/http/types.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/rootConfig.d.ts +21 -0
- package/dist/unstable-core-do-not-import/rootConfig.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import.js +0 -2
- package/dist/unstable-core-do-not-import.mjs +0 -1
- package/package.json +6 -7
- package/src/@trpc/server/http.ts +0 -1
- package/src/@trpc/server/index.ts +4 -0
- package/src/adapters/aws-lambda/content-type/json/index.ts +108 -0
- package/src/adapters/aws-lambda/index.ts +24 -9
- package/src/adapters/aws-lambda/utils.ts +21 -15
- package/src/adapters/content-handlers/concurrentCache.ts +16 -0
- package/src/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.ts +45 -0
- package/src/adapters/express.ts +1 -6
- package/src/adapters/fastify/content-type/json/index.ts +106 -0
- package/src/adapters/fastify/fastifyRequestHandler.ts +15 -21
- package/src/adapters/fastify/fastifyTRPCPlugin.ts +1 -1
- package/src/adapters/fastify/types.ts +22 -0
- package/src/adapters/fetch/content-type/json/index.ts +90 -0
- package/src/adapters/fetch/fetchRequestHandler.ts +22 -10
- package/src/adapters/fetch/types.ts +22 -15
- package/src/adapters/next-app-dir/nextAppDirCaller.ts +2 -9
- package/src/adapters/next.ts +1 -6
- package/src/adapters/node-http/content-type/form-data/index.ts +33 -159
- package/src/adapters/node-http/content-type/json/getPostBody.ts +9 -18
- package/src/adapters/node-http/content-type/json/index.ts +98 -9
- package/src/adapters/node-http/content-type/octet/index.ts +27 -0
- package/src/adapters/node-http/{internals/contentType.ts → content-type/types.ts} +2 -14
- package/src/adapters/node-http/nodeHTTPRequestHandler.ts +22 -35
- package/src/adapters/node-http/types.ts +46 -46
- package/src/adapters/standalone.ts +1 -2
- package/src/adapters/ws.ts +9 -14
- package/src/unstable-core-do-not-import/contentTypeParsers.ts +37 -0
- package/src/unstable-core-do-not-import/http/contentType.ts +10 -85
- package/src/unstable-core-do-not-import/http/index.ts +0 -1
- package/src/unstable-core-do-not-import/http/resolveHTTPResponse.ts +30 -27
- package/src/unstable-core-do-not-import/http/types.ts +0 -2
- package/src/unstable-core-do-not-import/rootConfig.ts +31 -0
- 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/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/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/contentType.js +0 -54
- package/dist/unstable-core-do-not-import/http/contentType.mjs +0 -52
- package/src/adapters/node-http/content-type/form-data/fileUploadHandler.ts +0 -277
- 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
|
@@ -8,7 +8,12 @@
|
|
|
8
8
|
* ```
|
|
9
9
|
*/
|
|
10
10
|
// @trpc/server
|
|
11
|
-
import type {
|
|
11
|
+
import type {
|
|
12
|
+
AnyRouter,
|
|
13
|
+
CreateContextCallback,
|
|
14
|
+
inferRouterContext,
|
|
15
|
+
WrapCreateContext,
|
|
16
|
+
} from '../../@trpc/server';
|
|
12
17
|
// @trpc/server/http
|
|
13
18
|
import type {
|
|
14
19
|
HTTPBaseHandlerOptions,
|
|
@@ -26,19 +31,21 @@ export type FetchCreateContextFn<TRouter extends AnyRouter> = (
|
|
|
26
31
|
) => inferRouterContext<TRouter> | Promise<inferRouterContext<TRouter>>;
|
|
27
32
|
|
|
28
33
|
export type FetchCreateContextOption<TRouter extends AnyRouter> =
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
**/
|
|
34
|
-
createContext?: FetchCreateContextFn<TRouter>;
|
|
35
|
-
}
|
|
36
|
-
: {
|
|
37
|
-
/**
|
|
38
|
-
* @link https://trpc.io/docs/v11/context
|
|
39
|
-
**/
|
|
40
|
-
createContext: FetchCreateContextFn<TRouter>;
|
|
41
|
-
};
|
|
34
|
+
CreateContextCallback<
|
|
35
|
+
inferRouterContext<TRouter>,
|
|
36
|
+
FetchCreateContextFn<TRouter>
|
|
37
|
+
>;
|
|
42
38
|
|
|
43
39
|
export type FetchHandlerOptions<TRouter extends AnyRouter> =
|
|
44
|
-
FetchCreateContextOption<TRouter> &
|
|
40
|
+
FetchCreateContextOption<TRouter> &
|
|
41
|
+
HTTPBaseHandlerOptions<TRouter, Request> & {
|
|
42
|
+
req: Request;
|
|
43
|
+
endpoint: string;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export type FetchHandlerRequestOptions<TRouter extends AnyRouter> =
|
|
47
|
+
HTTPBaseHandlerOptions<TRouter, Request> &
|
|
48
|
+
WrapCreateContext<FetchCreateContextFn<TRouter>> & {
|
|
49
|
+
req: Request;
|
|
50
|
+
endpoint: string;
|
|
51
|
+
};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { CreateContextCallback } from '../../@trpc/server';
|
|
1
2
|
import { getTRPCErrorFromUnknown, TRPCError } from '../../@trpc/server';
|
|
2
3
|
// FIXME: fix lint rule, this is ok
|
|
3
4
|
// eslint-disable-next-line no-restricted-imports
|
|
@@ -15,14 +16,6 @@ import { formDataToObject } from './formDataToObject';
|
|
|
15
16
|
import { TRPCRedirectError } from './redirect';
|
|
16
17
|
import { rethrowNextErrors } from './rethrowNextErrors';
|
|
17
18
|
|
|
18
|
-
type ContextCallback<TContext> = object extends TContext
|
|
19
|
-
? {
|
|
20
|
-
createContext?: () => MaybePromise<TContext>;
|
|
21
|
-
}
|
|
22
|
-
: {
|
|
23
|
-
createContext: () => MaybePromise<TContext>;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
19
|
/**
|
|
27
20
|
* Create a caller that works with Next.js React Server Components & Server Actions
|
|
28
21
|
*/
|
|
@@ -38,7 +31,7 @@ export function nextAppDirCaller<TContext>(
|
|
|
38
31
|
* Called when an error occurs in the handler
|
|
39
32
|
*/
|
|
40
33
|
onError?: (opts: ErrorHandlerOptions<TContext>) => void;
|
|
41
|
-
} &
|
|
34
|
+
} & CreateContextCallback<TContext, () => MaybePromise<TContext>>
|
|
42
35
|
>,
|
|
43
36
|
): CallerOverride<TContext> {
|
|
44
37
|
const {
|
package/src/adapters/next.ts
CHANGED
|
@@ -65,12 +65,7 @@ export function createNextApiHandler<TRouter extends AnyRouter>(
|
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
await nodeHTTPRequestHandler({
|
|
68
|
-
|
|
69
|
-
...(opts as NodeHTTPHandlerOptions<
|
|
70
|
-
AnyRouter,
|
|
71
|
-
NextApiRequest,
|
|
72
|
-
NextApiResponse
|
|
73
|
-
>),
|
|
68
|
+
...opts,
|
|
74
69
|
req,
|
|
75
70
|
res,
|
|
76
71
|
path,
|
|
@@ -1,163 +1,37 @@
|
|
|
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';
|
|
1
|
+
import { Readable } from 'stream';
|
|
16
2
|
// @trpc/server
|
|
17
|
-
import {
|
|
18
|
-
import type { NodeHTTPRequest } from '../../types';
|
|
19
|
-
import {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
-
}
|
|
3
|
+
import type { AnyRouter } from '../../../../@trpc/server';
|
|
4
|
+
import type { NodeHTTPRequest, NodeHTTPResponse } from '../../types';
|
|
5
|
+
import type { NodeHTTPContentTypeHandler } from '../types';
|
|
6
|
+
|
|
7
|
+
export const getFormDataContentTypeHandler: <
|
|
8
|
+
TRouter extends AnyRouter,
|
|
9
|
+
TRequest extends NodeHTTPRequest,
|
|
10
|
+
TResponse extends NodeHTTPResponse,
|
|
11
|
+
>() => NodeHTTPContentTypeHandler<TRouter, TRequest, TResponse> = () => ({
|
|
12
|
+
name: 'node-http-formdata',
|
|
13
|
+
isMatch: (headers) => {
|
|
14
|
+
return !!headers.get('content-type')?.startsWith('multipart/form-data');
|
|
15
|
+
},
|
|
16
|
+
async getInputs(opts, inputOpts) {
|
|
17
|
+
if (inputOpts.isBatchCall) {
|
|
18
|
+
throw new Error('Batch calls not supported for form-data');
|
|
96
19
|
}
|
|
97
20
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
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
|
-
});
|
|
21
|
+
const contentType = opts.req.headers['content-type'];
|
|
22
|
+
if (!contentType) {
|
|
23
|
+
// Should be unreachable given the isMatch check
|
|
24
|
+
throw new Error('No content-type header found');
|
|
25
|
+
}
|
|
148
26
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
}
|
|
160
|
-
export {
|
|
161
|
-
isMultipartFormDataRequest as experimental_isMultipartFormDataRequest,
|
|
162
|
-
parseMultipartFormData as experimental_parseMultipartFormData,
|
|
163
|
-
};
|
|
27
|
+
const form = await new Request('https://unused.com', {
|
|
28
|
+
method: 'POST',
|
|
29
|
+
headers: { 'content-type': contentType },
|
|
30
|
+
body: Readable.toWeb(opts.req) as ReadableStream,
|
|
31
|
+
// @ts-expect-error - outdated types? this exists
|
|
32
|
+
duplex: 'half',
|
|
33
|
+
}).formData();
|
|
34
|
+
|
|
35
|
+
return form;
|
|
36
|
+
},
|
|
37
|
+
});
|
|
@@ -9,22 +9,6 @@ export async function getPostBody(opts: {
|
|
|
9
9
|
}): Promise<BodyResult> {
|
|
10
10
|
const { req, maxBodySize = Infinity } = opts;
|
|
11
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
12
|
if ('body' in req) {
|
|
29
13
|
resolve({
|
|
30
14
|
ok: true,
|
|
@@ -35,18 +19,25 @@ export async function getPostBody(opts: {
|
|
|
35
19
|
});
|
|
36
20
|
return;
|
|
37
21
|
}
|
|
22
|
+
|
|
38
23
|
let body = '';
|
|
39
24
|
let hasBody = false;
|
|
40
|
-
|
|
25
|
+
|
|
26
|
+
function onData(data: any) {
|
|
41
27
|
body += data;
|
|
42
28
|
hasBody = true;
|
|
43
29
|
if (body.length > maxBodySize) {
|
|
30
|
+
req.off('data', onData);
|
|
31
|
+
|
|
44
32
|
resolve({
|
|
45
33
|
ok: false,
|
|
46
34
|
error: new TRPCError({ code: 'PAYLOAD_TOO_LARGE' }),
|
|
47
35
|
});
|
|
48
36
|
}
|
|
49
|
-
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
req.on('data', onData);
|
|
40
|
+
|
|
50
41
|
req.on('end', () => {
|
|
51
42
|
resolve({
|
|
52
43
|
ok: true,
|
|
@@ -1,11 +1,100 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
1
|
+
// @trpc/server
|
|
2
|
+
import { TRPCError } from '../../../../@trpc/server';
|
|
3
|
+
import type {
|
|
4
|
+
AnyRouter,
|
|
5
|
+
CombinedDataTransformer,
|
|
6
|
+
} from '../../../../@trpc/server';
|
|
7
|
+
import { createConcurrentCache } from '../../../content-handlers/concurrentCache';
|
|
8
|
+
import type { NodeHTTPRequest, NodeHTTPResponse } from '../../types';
|
|
9
|
+
import type { NodeHTTPContentTypeHandler } from '../types';
|
|
3
10
|
import { getPostBody } from './getPostBody';
|
|
4
11
|
|
|
5
|
-
export const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
+
export const getNodeHTTPJSONContentTypeHandler: <
|
|
13
|
+
TRouter extends AnyRouter,
|
|
14
|
+
TRequest extends NodeHTTPRequest,
|
|
15
|
+
TResponse extends NodeHTTPResponse,
|
|
16
|
+
>() => NodeHTTPContentTypeHandler<TRouter, TRequest, TResponse> = () => {
|
|
17
|
+
const cache = createConcurrentCache();
|
|
18
|
+
|
|
19
|
+
return {
|
|
20
|
+
name: 'node-http-json',
|
|
21
|
+
isMatch: (headers) => {
|
|
22
|
+
return !!headers.get('content-type')?.startsWith('application/json');
|
|
23
|
+
},
|
|
24
|
+
getInputs: async (opts, info) => {
|
|
25
|
+
async function getRawProcedureInputOrThrow() {
|
|
26
|
+
const bodyResult = await getPostBody(opts);
|
|
27
|
+
if (!bodyResult.ok) {
|
|
28
|
+
throw bodyResult.error;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const preprocessedBody = bodyResult.preprocessed;
|
|
32
|
+
const body = bodyResult.data;
|
|
33
|
+
|
|
34
|
+
try {
|
|
35
|
+
if (opts.req.method === 'GET') {
|
|
36
|
+
const input = opts.query.get('input');
|
|
37
|
+
if (!input) {
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return JSON.parse(input);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (preprocessedBody || typeof body !== 'string') {
|
|
45
|
+
// Some tools like nextjs may parse json
|
|
46
|
+
// requests before they reach us. So we just use them as is
|
|
47
|
+
return body;
|
|
48
|
+
} else {
|
|
49
|
+
return JSON.parse(body);
|
|
50
|
+
}
|
|
51
|
+
} catch (cause) {
|
|
52
|
+
throw new TRPCError({
|
|
53
|
+
code: 'PARSE_ERROR',
|
|
54
|
+
cause,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const deserializeInputValue = (
|
|
60
|
+
rawValue: unknown,
|
|
61
|
+
transformer: CombinedDataTransformer,
|
|
62
|
+
) => {
|
|
63
|
+
return typeof rawValue !== 'undefined'
|
|
64
|
+
? transformer.input.deserialize(rawValue)
|
|
65
|
+
: rawValue;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const rawInput = await cache.concurrentSafeGet('rawInput', () =>
|
|
69
|
+
getRawProcedureInputOrThrow(),
|
|
70
|
+
);
|
|
71
|
+
if (rawInput === undefined) {
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const transformer = opts.router._def._config.transformer;
|
|
76
|
+
|
|
77
|
+
if (!info.isBatchCall) {
|
|
78
|
+
return cache.concurrentSafeGet('input', () =>
|
|
79
|
+
deserializeInputValue(rawInput, transformer),
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/* istanbul ignore if */
|
|
84
|
+
if (
|
|
85
|
+
rawInput == null ||
|
|
86
|
+
typeof rawInput !== 'object' ||
|
|
87
|
+
Array.isArray(rawInput)
|
|
88
|
+
) {
|
|
89
|
+
throw new TRPCError({
|
|
90
|
+
code: 'BAD_REQUEST',
|
|
91
|
+
message: '"input" needs to be an object when doing a batch call',
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return cache.concurrentSafeGet(String(info.batch), () =>
|
|
96
|
+
deserializeInputValue(rawInput[info.batch], transformer),
|
|
97
|
+
);
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Stream } from 'stream';
|
|
2
|
+
// @trpc/server
|
|
3
|
+
import type { AnyRouter } from '../../../../@trpc/server';
|
|
4
|
+
import type { NodeHTTPRequest, NodeHTTPResponse } from '../../types';
|
|
5
|
+
import type { NodeHTTPContentTypeHandler } from '../types';
|
|
6
|
+
|
|
7
|
+
export const getOctetContentTypeHandler: <
|
|
8
|
+
TRouter extends AnyRouter,
|
|
9
|
+
TRequest extends NodeHTTPRequest,
|
|
10
|
+
TResponse extends NodeHTTPResponse,
|
|
11
|
+
>() => NodeHTTPContentTypeHandler<TRouter, TRequest, TResponse> = () => ({
|
|
12
|
+
name: 'node-http-octet',
|
|
13
|
+
isMatch: (headers) => {
|
|
14
|
+
return !!headers
|
|
15
|
+
.get('content-type')
|
|
16
|
+
?.startsWith('application/octet-stream');
|
|
17
|
+
},
|
|
18
|
+
async getInputs(opts, inputOpts) {
|
|
19
|
+
if (inputOpts.isBatchCall) {
|
|
20
|
+
throw new Error('Batch calls not supported for octet-stream');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const stream = Stream.Readable.toWeb(opts.req);
|
|
24
|
+
|
|
25
|
+
return stream;
|
|
26
|
+
},
|
|
27
|
+
});
|
|
@@ -9,23 +9,11 @@ import type {
|
|
|
9
9
|
} from '../types';
|
|
10
10
|
|
|
11
11
|
export interface NodeHTTPContentTypeHandler<
|
|
12
|
+
TRouter extends AnyRouter,
|
|
12
13
|
TRequest extends NodeHTTPRequest,
|
|
13
14
|
TResponse extends NodeHTTPResponse,
|
|
14
15
|
> extends BaseContentTypeHandler<
|
|
15
|
-
NodeHTTPRequestHandlerOptions<
|
|
16
|
+
NodeHTTPRequestHandlerOptions<TRouter, TRequest, TResponse> & {
|
|
16
17
|
query: URLSearchParams;
|
|
17
18
|
}
|
|
18
19
|
> {}
|
|
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
|
-
}
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
* ```
|
|
9
9
|
*/
|
|
10
10
|
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
11
|
+
|
|
11
12
|
// @trpc/server
|
|
12
13
|
import type { AnyRouter } from '../../@trpc/server';
|
|
13
14
|
import type {
|
|
@@ -20,16 +21,16 @@ import {
|
|
|
20
21
|
getBatchStreamFormatter,
|
|
21
22
|
resolveHTTPResponse,
|
|
22
23
|
} from '../../@trpc/server/http';
|
|
23
|
-
import {
|
|
24
|
-
import
|
|
24
|
+
import { selectContentHandlerOrUnsupportedMediaType } from '../content-handlers/selectContentHandlerOrUnsupportedMediaType';
|
|
25
|
+
import { getFormDataContentTypeHandler } from './content-type/form-data';
|
|
26
|
+
import { getNodeHTTPJSONContentTypeHandler } from './content-type/json';
|
|
27
|
+
import { getOctetContentTypeHandler } from './content-type/octet';
|
|
25
28
|
import type {
|
|
26
29
|
NodeHTTPRequest,
|
|
27
30
|
NodeHTTPRequestHandlerOptions,
|
|
28
31
|
NodeHTTPResponse,
|
|
29
32
|
} from './types';
|
|
30
33
|
|
|
31
|
-
const defaultJSONContentTypeHandler = nodeHTTPJSONContentTypeHandler();
|
|
32
|
-
|
|
33
34
|
export async function nodeHTTPRequestHandler<
|
|
34
35
|
TRouter extends AnyRouter,
|
|
35
36
|
TRequest extends NodeHTTPRequest,
|
|
@@ -56,38 +57,10 @@ export async function nodeHTTPRequestHandler<
|
|
|
56
57
|
? new URLSearchParams(opts.req.query as any)
|
|
57
58
|
: new URLSearchParams(opts.req.url!.split('?')[1]);
|
|
58
59
|
|
|
59
|
-
const jsonContentTypeHandler =
|
|
60
|
-
defaultJSONContentTypeHandler as unknown as NodeHTTPContentTypeHandler<
|
|
61
|
-
TRequest,
|
|
62
|
-
TResponse
|
|
63
|
-
>;
|
|
64
|
-
|
|
65
|
-
const contentTypeHandlers = opts.experimental_contentTypeHandlers ?? [
|
|
66
|
-
jsonContentTypeHandler,
|
|
67
|
-
];
|
|
68
|
-
|
|
69
|
-
const contentTypeHandler =
|
|
70
|
-
contentTypeHandlers.find((handler) =>
|
|
71
|
-
handler.isMatch({
|
|
72
|
-
// FIXME: no typecasting should be needed here
|
|
73
|
-
...(opts as any),
|
|
74
|
-
query,
|
|
75
|
-
}),
|
|
76
|
-
) ??
|
|
77
|
-
// fallback to json
|
|
78
|
-
jsonContentTypeHandler;
|
|
79
|
-
|
|
80
|
-
const bodyResult = await contentTypeHandler.getBody({
|
|
81
|
-
// FIXME: no typecasting should be needed here
|
|
82
|
-
...(opts as any),
|
|
83
|
-
query,
|
|
84
|
-
});
|
|
85
|
-
|
|
86
60
|
const req: HTTPRequest = {
|
|
87
61
|
method: opts.req.method!,
|
|
88
62
|
headers: opts.req.headers,
|
|
89
63
|
query,
|
|
90
|
-
body: bodyResult.ok ? bodyResult.data : undefined,
|
|
91
64
|
};
|
|
92
65
|
|
|
93
66
|
let isStream = false;
|
|
@@ -133,19 +106,33 @@ export async function nodeHTTPRequestHandler<
|
|
|
133
106
|
}
|
|
134
107
|
};
|
|
135
108
|
|
|
109
|
+
const [contentTypeHandler, unsupportedMediaTypeError] =
|
|
110
|
+
selectContentHandlerOrUnsupportedMediaType(
|
|
111
|
+
[
|
|
112
|
+
getNodeHTTPJSONContentTypeHandler<TRouter, TRequest, TResponse>(),
|
|
113
|
+
getFormDataContentTypeHandler<TRouter, TRequest, TResponse>(),
|
|
114
|
+
getOctetContentTypeHandler<TRouter, TRequest, TResponse>(),
|
|
115
|
+
],
|
|
116
|
+
{ ...opts, query },
|
|
117
|
+
);
|
|
118
|
+
|
|
136
119
|
await resolveHTTPResponse<TRouter, HTTPRequest>({
|
|
137
120
|
...opts,
|
|
138
121
|
req,
|
|
139
122
|
createContext,
|
|
140
|
-
error:
|
|
141
|
-
preprocessedBody: bodyResult.ok ? bodyResult.preprocessed : false,
|
|
123
|
+
error: unsupportedMediaTypeError,
|
|
142
124
|
onError(o) {
|
|
143
125
|
opts?.onError?.({
|
|
144
126
|
...o,
|
|
145
127
|
req: opts.req,
|
|
146
128
|
});
|
|
147
129
|
},
|
|
148
|
-
|
|
130
|
+
async getInput(inputsOpts) {
|
|
131
|
+
return await contentTypeHandler?.getInputs(
|
|
132
|
+
{ ...opts, query },
|
|
133
|
+
inputsOpts,
|
|
134
|
+
);
|
|
135
|
+
},
|
|
149
136
|
unstable_onHead,
|
|
150
137
|
unstable_onChunk,
|
|
151
138
|
});
|