@trpc/server 11.0.0-rc.345 → 11.0.0-rc.347
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/adapters/aws-lambda/content-type/json/index.js +2 -2
- package/dist/adapters/aws-lambda/content-type/json/index.mjs +2 -2
- package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.d.ts +8 -2
- package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.d.ts.map +1 -1
- package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.js +13 -2
- package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.mjs +13 -2
- package/dist/adapters/fastify/content-type/json/index.js +2 -2
- package/dist/adapters/fastify/content-type/json/index.mjs +2 -2
- package/dist/adapters/fetch/content-type/json/index.d.ts.map +1 -1
- package/dist/adapters/fetch/content-type/json/index.js +2 -2
- package/dist/adapters/fetch/content-type/json/index.mjs +2 -2
- package/dist/adapters/node-http/content-type/form-data/index.d.ts +0 -7
- 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 +16 -94
- package/dist/adapters/node-http/content-type/form-data/index.mjs +17 -75
- package/dist/adapters/node-http/content-type/json/index.js +2 -2
- package/dist/adapters/node-http/content-type/json/index.mjs +2 -2
- package/dist/adapters/node-http/content-type/octet/index.d.ts.map +1 -1
- package/dist/adapters/node-http/content-type/octet/index.js +2 -2
- package/dist/adapters/node-http/content-type/octet/index.mjs +2 -2
- package/dist/bundle-analysis.json +95 -160
- package/dist/unstable-core-do-not-import/http/contentType.d.ts +1 -1
- package/dist/unstable-core-do-not-import/http/contentType.d.ts.map +1 -1
- package/package.json +2 -3
- package/src/adapters/aws-lambda/content-type/json/index.ts +2 -2
- package/src/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.ts +29 -5
- package/src/adapters/fastify/content-type/json/index.ts +2 -2
- package/src/adapters/fetch/content-type/json/index.ts +2 -4
- package/src/adapters/node-http/content-type/form-data/index.ts +15 -119
- package/src/adapters/node-http/content-type/json/index.ts +2 -2
- package/src/adapters/node-http/content-type/octet/index.ts +4 -6
- package/src/unstable-core-do-not-import/http/contentType.ts +1 -1
- 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 -89
- package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.mjs +0 -86
- 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 -18
- package/dist/adapters/node-http/content-type/form-data/uploadHandler.mjs +0 -15
- 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
|
@@ -7,8 +7,8 @@ var utils = require('../../utils.js');
|
|
|
7
7
|
// @trpc/server
|
|
8
8
|
const getLambdaHTTPJSONContentTypeHandler = ()=>({
|
|
9
9
|
name: 'lambda-json',
|
|
10
|
-
isMatch (
|
|
11
|
-
return !!
|
|
10
|
+
isMatch: (headers)=>{
|
|
11
|
+
return !!headers.get('content-type')?.startsWith('application/json');
|
|
12
12
|
},
|
|
13
13
|
getInputs: async (opts, info)=>{
|
|
14
14
|
function getRawProcedureInputOrThrow() {
|
|
@@ -5,8 +5,8 @@ import { lambdaEventToHTTPBody } from '../../utils.mjs';
|
|
|
5
5
|
// @trpc/server
|
|
6
6
|
const getLambdaHTTPJSONContentTypeHandler = ()=>({
|
|
7
7
|
name: 'lambda-json',
|
|
8
|
-
isMatch (
|
|
9
|
-
return !!
|
|
8
|
+
isMatch: (headers)=>{
|
|
9
|
+
return !!headers.get('content-type')?.startsWith('application/json');
|
|
10
10
|
},
|
|
11
11
|
getInputs: async (opts, info)=>{
|
|
12
12
|
function getRawProcedureInputOrThrow() {
|
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import { TRPCError } from '../../@trpc/server';
|
|
2
|
-
import type { BaseContentTypeHandler } from '../../@trpc/server/http';
|
|
3
|
-
|
|
2
|
+
import type { BaseContentTypeHandler, HTTPHeaders } from '../../@trpc/server/http';
|
|
3
|
+
interface MinimalHandlerOpts {
|
|
4
|
+
req: {
|
|
5
|
+
headers: HTTPHeaders | Headers;
|
|
6
|
+
};
|
|
7
|
+
}
|
|
8
|
+
export declare function selectContentHandlerOrUnsupportedMediaType<THandlerOpts extends MinimalHandlerOpts, THandler extends BaseContentTypeHandler<THandlerOpts>>(handlers: THandler[], opts: THandlerOpts): readonly [undefined, TRPCError] | readonly [THandler];
|
|
9
|
+
export {};
|
|
4
10
|
//# sourceMappingURL=selectContentHandlerOrUnsupportedMediaType.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"selectContentHandlerOrUnsupportedMediaType.d.ts","sourceRoot":"","sources":["../../../src/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAC/C,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"selectContentHandlerOrUnsupportedMediaType.d.ts","sourceRoot":"","sources":["../../../src/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAC/C,OAAO,KAAK,EACV,sBAAsB,EACtB,WAAW,EACZ,MAAM,yBAAyB,CAAC;AAEjC,UAAU,kBAAkB;IAC1B,GAAG,EAAE;QAGH,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC;KAChC,CAAC;CACH;AAED,wBAAgB,0CAA0C,CACxD,YAAY,SAAS,kBAAkB,EACvC,QAAQ,SAAS,sBAAsB,CAAC,YAAY,CAAC,EACrD,QAAQ,EAAE,QAAQ,EAAE,EAAE,IAAI,EAAE,YAAY,yDA2BzC"}
|
|
@@ -4,13 +4,24 @@ var TRPCError = require('../../unstable-core-do-not-import/error/TRPCError.js');
|
|
|
4
4
|
require('../../unstable-core-do-not-import/rootConfig.js');
|
|
5
5
|
|
|
6
6
|
function selectContentHandlerOrUnsupportedMediaType(handlers, opts) {
|
|
7
|
-
const
|
|
7
|
+
const headers = new Headers(opts.req.headers);
|
|
8
|
+
const contentType = headers.get('content-type');
|
|
9
|
+
if (contentType === null) {
|
|
10
|
+
return [
|
|
11
|
+
undefined,
|
|
12
|
+
new TRPCError.TRPCError({
|
|
13
|
+
code: 'UNSUPPORTED_MEDIA_TYPE',
|
|
14
|
+
message: 'No Content-Type header detected on the incoming request. This request may not be supported by your tRPC Adapter, or possibly by tRPC at all'
|
|
15
|
+
})
|
|
16
|
+
];
|
|
17
|
+
}
|
|
18
|
+
const handler = handlers.find((handler)=>handler.isMatch(headers));
|
|
8
19
|
if (!handler) {
|
|
9
20
|
return [
|
|
10
21
|
undefined,
|
|
11
22
|
new TRPCError.TRPCError({
|
|
12
23
|
code: 'UNSUPPORTED_MEDIA_TYPE',
|
|
13
|
-
message:
|
|
24
|
+
message: `Invalid Content-Type header '${contentType}'. This request may not be supported by your tRPC Adapter, or possibly by tRPC at all`
|
|
14
25
|
})
|
|
15
26
|
];
|
|
16
27
|
}
|
|
@@ -2,13 +2,24 @@ import { TRPCError } from '../../unstable-core-do-not-import/error/TRPCError.mjs
|
|
|
2
2
|
import '../../unstable-core-do-not-import/rootConfig.mjs';
|
|
3
3
|
|
|
4
4
|
function selectContentHandlerOrUnsupportedMediaType(handlers, opts) {
|
|
5
|
-
const
|
|
5
|
+
const headers = new Headers(opts.req.headers);
|
|
6
|
+
const contentType = headers.get('content-type');
|
|
7
|
+
if (contentType === null) {
|
|
8
|
+
return [
|
|
9
|
+
undefined,
|
|
10
|
+
new TRPCError({
|
|
11
|
+
code: 'UNSUPPORTED_MEDIA_TYPE',
|
|
12
|
+
message: 'No Content-Type header detected on the incoming request. This request may not be supported by your tRPC Adapter, or possibly by tRPC at all'
|
|
13
|
+
})
|
|
14
|
+
];
|
|
15
|
+
}
|
|
16
|
+
const handler = handlers.find((handler)=>handler.isMatch(headers));
|
|
6
17
|
if (!handler) {
|
|
7
18
|
return [
|
|
8
19
|
undefined,
|
|
9
20
|
new TRPCError({
|
|
10
21
|
code: 'UNSUPPORTED_MEDIA_TYPE',
|
|
11
|
-
message:
|
|
22
|
+
message: `Invalid Content-Type header '${contentType}'. This request may not be supported by your tRPC Adapter, or possibly by tRPC at all`
|
|
12
23
|
})
|
|
13
24
|
];
|
|
14
25
|
}
|
|
@@ -6,8 +6,8 @@ require('../../../../unstable-core-do-not-import/rootConfig.js');
|
|
|
6
6
|
// @trpc/server
|
|
7
7
|
const getFastifyHTTPJSONContentTypeHandler = ()=>({
|
|
8
8
|
name: 'fastify-json',
|
|
9
|
-
isMatch (
|
|
10
|
-
return !!
|
|
9
|
+
isMatch: (headers)=>{
|
|
10
|
+
return !!headers.get('content-type')?.startsWith('application/json');
|
|
11
11
|
},
|
|
12
12
|
getInputs: async (opts, info)=>{
|
|
13
13
|
async function getRawProcedureInputOrThrow() {
|
|
@@ -4,8 +4,8 @@ import '../../../../unstable-core-do-not-import/rootConfig.mjs';
|
|
|
4
4
|
// @trpc/server
|
|
5
5
|
const getFastifyHTTPJSONContentTypeHandler = ()=>({
|
|
6
6
|
name: 'fastify-json',
|
|
7
|
-
isMatch (
|
|
8
|
-
return !!
|
|
7
|
+
isMatch: (headers)=>{
|
|
8
|
+
return !!headers.get('content-type')?.startsWith('application/json');
|
|
9
9
|
},
|
|
10
10
|
getInputs: async (opts, info)=>{
|
|
11
11
|
async function getRawProcedureInputOrThrow() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/adapters/fetch/content-type/json/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,SAAS,EAEV,MAAM,0BAA0B,CAAC;AAClC,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAC;AAC5E,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,aAAa,CAAC;AAE9D,MAAM,WAAW,2BAA2B,CAAC,OAAO,SAAS,SAAS,CACpE,SAAQ,sBAAsB,CAC5B,0BAA0B,CAAC,OAAO,CAAC,GAAG;IACpC,GAAG,EAAE,GAAG,CAAC;CACV,CACF;CAAG;AAEN,eAAO,MAAM,kCAAkC,EAAE,CAC/C,OAAO,SAAS,SAAS,OACpB,2BAA2B,CAAC,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/adapters/fetch/content-type/json/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,SAAS,EAEV,MAAM,0BAA0B,CAAC;AAClC,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAC;AAC5E,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,aAAa,CAAC;AAE9D,MAAM,WAAW,2BAA2B,CAAC,OAAO,SAAS,SAAS,CACpE,SAAQ,sBAAsB,CAC5B,0BAA0B,CAAC,OAAO,CAAC,GAAG;IACpC,GAAG,EAAE,GAAG,CAAC;CACV,CACF;CAAG;AAEN,eAAO,MAAM,kCAAkC,EAAE,CAC/C,OAAO,SAAS,SAAS,OACpB,2BAA2B,CAAC,OAAO,CA2ExC,CAAC"}
|
|
@@ -6,8 +6,8 @@ require('../../../../unstable-core-do-not-import/rootConfig.js');
|
|
|
6
6
|
// @trpc/server
|
|
7
7
|
const getFetchHTTPJSONContentTypeHandler = ()=>({
|
|
8
8
|
name: 'fetch-json',
|
|
9
|
-
isMatch (
|
|
10
|
-
return !!
|
|
9
|
+
isMatch: (headers)=>{
|
|
10
|
+
return !!headers.get('content-type')?.startsWith('application/json');
|
|
11
11
|
},
|
|
12
12
|
getInputs: async (opts, info)=>{
|
|
13
13
|
async function getRawProcedureInputOrThrow() {
|
|
@@ -4,8 +4,8 @@ import '../../../../unstable-core-do-not-import/rootConfig.mjs';
|
|
|
4
4
|
// @trpc/server
|
|
5
5
|
const getFetchHTTPJSONContentTypeHandler = ()=>({
|
|
6
6
|
name: 'fetch-json',
|
|
7
|
-
isMatch (
|
|
8
|
-
return !!
|
|
7
|
+
isMatch: (headers)=>{
|
|
8
|
+
return !!headers.get('content-type')?.startsWith('application/json');
|
|
9
9
|
},
|
|
10
10
|
getInputs: async (opts, info)=>{
|
|
11
11
|
async function getRawProcedureInputOrThrow() {
|
|
@@ -1,12 +1,5 @@
|
|
|
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
1
|
import type { AnyRouter } from '../../../../@trpc/server';
|
|
8
2
|
import type { NodeHTTPRequest, NodeHTTPResponse } from '../../types';
|
|
9
3
|
import type { NodeHTTPContentTypeHandler } from '../types';
|
|
10
4
|
export declare const getFormDataContentTypeHandler: <TRouter extends AnyRouter, TRequest extends NodeHTTPRequest, TResponse extends NodeHTTPResponse>() => NodeHTTPContentTypeHandler<TRouter, TRequest, TResponse>;
|
|
11
|
-
export { MaxBodySizeExceededError, MaxPartSizeExceededError, type UploadHandler, } from './uploadHandler';
|
|
12
5
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/adapters/node-http/content-type/form-data/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/adapters/node-http/content-type/form-data/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAC1D,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AACrE,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,UAAU,CAAC;AAE3D,eAAO,MAAM,6BAA6B,EAAE,CAC1C,OAAO,SAAS,SAAS,EACzB,QAAQ,SAAS,eAAe,EAChC,SAAS,SAAS,gBAAgB,OAC7B,0BAA0B,CAAC,OAAO,EAAE,QAAQ,EAAE,SAAS,CA0B5D,CAAC"}
|
|
@@ -1,110 +1,32 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var fs = require('node:fs/promises');
|
|
4
3
|
var node_stream = require('node:stream');
|
|
5
|
-
var multipartParser = require('@web3-storage/multipart-parser');
|
|
6
|
-
var fileUploadHandler = require('./fileUploadHandler.js');
|
|
7
|
-
var memoryUploadHandler = require('./memoryUploadHandler.js');
|
|
8
|
-
var uploadHandler = require('./uploadHandler.js');
|
|
9
4
|
|
|
10
|
-
function _interopNamespaceDefault(e) {
|
|
11
|
-
var n = Object.create(null);
|
|
12
|
-
if (e) {
|
|
13
|
-
Object.keys(e).forEach(function (k) {
|
|
14
|
-
if (k !== 'default') {
|
|
15
|
-
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
16
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
17
|
-
enumerable: true,
|
|
18
|
-
get: function () { return e[k]; }
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
n.default = e;
|
|
24
|
-
return Object.freeze(n);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
var fs__namespace = /*#__PURE__*/_interopNamespaceDefault(fs);
|
|
28
|
-
|
|
29
|
-
const utfTextDecoder = new TextDecoder('utf-8');
|
|
30
|
-
/**
|
|
31
|
-
* Allows you to handle multipart forms (file uploads) for your app.
|
|
32
|
-
* Request body parts with a 'filename' property will be treated as files.
|
|
33
|
-
* The rest will be treated as text.
|
|
34
|
-
* @param request The incoming Node HTTP request
|
|
35
|
-
* @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.
|
|
36
|
-
* @param maxBodySize The maximum size of the request body in bytes. Defaults to Infinity.
|
|
37
|
-
*
|
|
38
|
-
* @link https://remix.run/utils/parse-multipart-form-data
|
|
39
|
-
*/ async function parseMultipartFormData(request, uploadHandler$1, maxBodySize = Infinity) {
|
|
40
|
-
const contentType = request.headers['content-type'] ?? '';
|
|
41
|
-
const [type, boundary] = contentType.split(/\s*;\s*boundary=/);
|
|
42
|
-
if (!boundary || type !== 'multipart/form-data') {
|
|
43
|
-
throw new TypeError('Could not parse content as FormData.');
|
|
44
|
-
}
|
|
45
|
-
const formData = new FormData();
|
|
46
|
-
const parts = multipartParser.streamMultipart(node_stream.Readable.toWeb(request), boundary);
|
|
47
|
-
let currentBodySize = 0;
|
|
48
|
-
const nodeOnDiskFiles = [];
|
|
49
|
-
try {
|
|
50
|
-
for await (const part of parts){
|
|
51
|
-
if (part.done) break;
|
|
52
|
-
if (typeof part.filename === 'string') {
|
|
53
|
-
// This is a file, so the uploadHandler function will be called
|
|
54
|
-
// only pass basename as the multipart/form-data spec recommends
|
|
55
|
-
// https://datatracker.ietf.org/doc/html/rfc7578#section-4.2
|
|
56
|
-
part.filename = part.filename.split(/[/\\]/).pop();
|
|
57
|
-
const value = await uploadHandler$1(part);
|
|
58
|
-
if (typeof value === 'undefined' || value === null) {
|
|
59
|
-
continue;
|
|
60
|
-
}
|
|
61
|
-
// add to cleanup array in case of error
|
|
62
|
-
if (value instanceof fileUploadHandler.NodeOnDiskFile) {
|
|
63
|
-
nodeOnDiskFiles.push(value);
|
|
64
|
-
}
|
|
65
|
-
// if the combined size of the body exceeds the max size, throw an error
|
|
66
|
-
currentBodySize += value.size;
|
|
67
|
-
if (currentBodySize > maxBodySize) {
|
|
68
|
-
throw new uploadHandler.MaxBodySizeExceededError(maxBodySize);
|
|
69
|
-
}
|
|
70
|
-
// add the file to the form data
|
|
71
|
-
formData.append(part.name, value);
|
|
72
|
-
} else {
|
|
73
|
-
// This is text, so we'll decode it and add it to the form data
|
|
74
|
-
let textualPart = '';
|
|
75
|
-
for await (const chunk of part.data){
|
|
76
|
-
// if the combined size of the body exceeds the max size, throw an error
|
|
77
|
-
currentBodySize += chunk.length;
|
|
78
|
-
if (currentBodySize > maxBodySize) {
|
|
79
|
-
throw new uploadHandler.MaxBodySizeExceededError(maxBodySize);
|
|
80
|
-
}
|
|
81
|
-
textualPart += utfTextDecoder.decode(chunk);
|
|
82
|
-
}
|
|
83
|
-
// add the text to the form data
|
|
84
|
-
formData.append(part.name, textualPart);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
return formData;
|
|
88
|
-
} catch (e) {
|
|
89
|
-
// clean up any files that were uploaded to disk if an error occurs
|
|
90
|
-
await Promise.all(nodeOnDiskFiles.map((file)=>fs__namespace.unlink(file.getFilePath())));
|
|
91
|
-
throw e;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
5
|
const getFormDataContentTypeHandler = ()=>({
|
|
95
6
|
name: 'node-http-formdata',
|
|
96
|
-
isMatch (
|
|
97
|
-
return
|
|
7
|
+
isMatch: (headers)=>{
|
|
8
|
+
return !!headers.get('content-type')?.startsWith('multipart/form-data');
|
|
98
9
|
},
|
|
99
10
|
async getInputs (opts, inputOpts) {
|
|
100
11
|
if (inputOpts.isBatchCall) {
|
|
101
12
|
throw new Error('Batch calls not supported for form-data');
|
|
102
13
|
}
|
|
103
|
-
const
|
|
14
|
+
const contentType = opts.req.headers['content-type'];
|
|
15
|
+
if (!contentType) {
|
|
16
|
+
// Should be unreachable given the isMatch check
|
|
17
|
+
throw new Error('No content-type header found');
|
|
18
|
+
}
|
|
19
|
+
const form = await new Request('https://unused.com', {
|
|
20
|
+
method: 'POST',
|
|
21
|
+
headers: {
|
|
22
|
+
'content-type': contentType
|
|
23
|
+
},
|
|
24
|
+
body: node_stream.Readable.toWeb(opts.req),
|
|
25
|
+
// @ts-expect-error - outdated types? this exists
|
|
26
|
+
duplex: 'half'
|
|
27
|
+
}).formData();
|
|
104
28
|
return form;
|
|
105
29
|
}
|
|
106
30
|
});
|
|
107
31
|
|
|
108
|
-
exports.MaxBodySizeExceededError = uploadHandler.MaxBodySizeExceededError;
|
|
109
|
-
exports.MaxPartSizeExceededError = uploadHandler.MaxPartSizeExceededError;
|
|
110
32
|
exports.getFormDataContentTypeHandler = getFormDataContentTypeHandler;
|
|
@@ -1,88 +1,30 @@
|
|
|
1
|
-
import * as fs from 'node:fs/promises';
|
|
2
1
|
import { Readable } from 'node:stream';
|
|
3
|
-
import { streamMultipart } from '@web3-storage/multipart-parser';
|
|
4
|
-
import { NodeOnDiskFile } from './fileUploadHandler.mjs';
|
|
5
|
-
import { createMemoryUploadHandler } from './memoryUploadHandler.mjs';
|
|
6
|
-
import { MaxBodySizeExceededError } from './uploadHandler.mjs';
|
|
7
|
-
export { MaxPartSizeExceededError } from './uploadHandler.mjs';
|
|
8
2
|
|
|
9
|
-
const utfTextDecoder = new TextDecoder('utf-8');
|
|
10
|
-
/**
|
|
11
|
-
* Allows you to handle multipart forms (file uploads) for your app.
|
|
12
|
-
* Request body parts with a 'filename' property will be treated as files.
|
|
13
|
-
* The rest will be treated as text.
|
|
14
|
-
* @param request The incoming Node HTTP request
|
|
15
|
-
* @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.
|
|
16
|
-
* @param maxBodySize The maximum size of the request body in bytes. Defaults to Infinity.
|
|
17
|
-
*
|
|
18
|
-
* @link https://remix.run/utils/parse-multipart-form-data
|
|
19
|
-
*/ async function parseMultipartFormData(request, uploadHandler, maxBodySize = Infinity) {
|
|
20
|
-
const contentType = request.headers['content-type'] ?? '';
|
|
21
|
-
const [type, boundary] = contentType.split(/\s*;\s*boundary=/);
|
|
22
|
-
if (!boundary || type !== 'multipart/form-data') {
|
|
23
|
-
throw new TypeError('Could not parse content as FormData.');
|
|
24
|
-
}
|
|
25
|
-
const formData = new FormData();
|
|
26
|
-
const parts = streamMultipart(Readable.toWeb(request), boundary);
|
|
27
|
-
let currentBodySize = 0;
|
|
28
|
-
const nodeOnDiskFiles = [];
|
|
29
|
-
try {
|
|
30
|
-
for await (const part of parts){
|
|
31
|
-
if (part.done) break;
|
|
32
|
-
if (typeof part.filename === 'string') {
|
|
33
|
-
// This is a file, so the uploadHandler function will be called
|
|
34
|
-
// only pass basename as the multipart/form-data spec recommends
|
|
35
|
-
// https://datatracker.ietf.org/doc/html/rfc7578#section-4.2
|
|
36
|
-
part.filename = part.filename.split(/[/\\]/).pop();
|
|
37
|
-
const value = await uploadHandler(part);
|
|
38
|
-
if (typeof value === 'undefined' || value === null) {
|
|
39
|
-
continue;
|
|
40
|
-
}
|
|
41
|
-
// add to cleanup array in case of error
|
|
42
|
-
if (value instanceof NodeOnDiskFile) {
|
|
43
|
-
nodeOnDiskFiles.push(value);
|
|
44
|
-
}
|
|
45
|
-
// if the combined size of the body exceeds the max size, throw an error
|
|
46
|
-
currentBodySize += value.size;
|
|
47
|
-
if (currentBodySize > maxBodySize) {
|
|
48
|
-
throw new MaxBodySizeExceededError(maxBodySize);
|
|
49
|
-
}
|
|
50
|
-
// add the file to the form data
|
|
51
|
-
formData.append(part.name, value);
|
|
52
|
-
} else {
|
|
53
|
-
// This is text, so we'll decode it and add it to the form data
|
|
54
|
-
let textualPart = '';
|
|
55
|
-
for await (const chunk of part.data){
|
|
56
|
-
// if the combined size of the body exceeds the max size, throw an error
|
|
57
|
-
currentBodySize += chunk.length;
|
|
58
|
-
if (currentBodySize > maxBodySize) {
|
|
59
|
-
throw new MaxBodySizeExceededError(maxBodySize);
|
|
60
|
-
}
|
|
61
|
-
textualPart += utfTextDecoder.decode(chunk);
|
|
62
|
-
}
|
|
63
|
-
// add the text to the form data
|
|
64
|
-
formData.append(part.name, textualPart);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
return formData;
|
|
68
|
-
} catch (e) {
|
|
69
|
-
// clean up any files that were uploaded to disk if an error occurs
|
|
70
|
-
await Promise.all(nodeOnDiskFiles.map((file)=>fs.unlink(file.getFilePath())));
|
|
71
|
-
throw e;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
3
|
const getFormDataContentTypeHandler = ()=>({
|
|
75
4
|
name: 'node-http-formdata',
|
|
76
|
-
isMatch (
|
|
77
|
-
return
|
|
5
|
+
isMatch: (headers)=>{
|
|
6
|
+
return !!headers.get('content-type')?.startsWith('multipart/form-data');
|
|
78
7
|
},
|
|
79
8
|
async getInputs (opts, inputOpts) {
|
|
80
9
|
if (inputOpts.isBatchCall) {
|
|
81
10
|
throw new Error('Batch calls not supported for form-data');
|
|
82
11
|
}
|
|
83
|
-
const
|
|
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();
|
|
84
26
|
return form;
|
|
85
27
|
}
|
|
86
28
|
});
|
|
87
29
|
|
|
88
|
-
export {
|
|
30
|
+
export { getFormDataContentTypeHandler };
|
|
@@ -7,8 +7,8 @@ var getPostBody = require('./getPostBody.js');
|
|
|
7
7
|
// @trpc/server
|
|
8
8
|
const getNodeHTTPJSONContentTypeHandler = ()=>({
|
|
9
9
|
name: 'node-http-json',
|
|
10
|
-
isMatch (
|
|
11
|
-
return !!
|
|
10
|
+
isMatch: (headers)=>{
|
|
11
|
+
return !!headers.get('content-type')?.startsWith('application/json');
|
|
12
12
|
},
|
|
13
13
|
getInputs: async (opts, info)=>{
|
|
14
14
|
const bodyResult = await getPostBody.getPostBody(opts);
|
|
@@ -5,8 +5,8 @@ import { getPostBody } from './getPostBody.mjs';
|
|
|
5
5
|
// @trpc/server
|
|
6
6
|
const getNodeHTTPJSONContentTypeHandler = ()=>({
|
|
7
7
|
name: 'node-http-json',
|
|
8
|
-
isMatch (
|
|
9
|
-
return !!
|
|
8
|
+
isMatch: (headers)=>{
|
|
9
|
+
return !!headers.get('content-type')?.startsWith('application/json');
|
|
10
10
|
},
|
|
11
11
|
getInputs: async (opts, info)=>{
|
|
12
12
|
const bodyResult = await getPostBody(opts);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/adapters/node-http/content-type/octet/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAC1D,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AACrE,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,UAAU,CAAC;AAE3D,eAAO,MAAM,0BAA0B,EAAE,CACvC,OAAO,SAAS,SAAS,EACzB,QAAQ,SAAS,eAAe,EAChC,SAAS,SAAS,gBAAgB,OAC7B,0BAA0B,CAAC,OAAO,EAAE,QAAQ,EAAE,SAAS,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/adapters/node-http/content-type/octet/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAC1D,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AACrE,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,UAAU,CAAC;AAE3D,eAAO,MAAM,0BAA0B,EAAE,CACvC,OAAO,SAAS,SAAS,EACzB,QAAQ,SAAS,eAAe,EAChC,SAAS,SAAS,gBAAgB,OAC7B,0BAA0B,CAAC,OAAO,EAAE,QAAQ,EAAE,SAAS,CAgB5D,CAAC"}
|
|
@@ -4,8 +4,8 @@ var node_stream = require('node:stream');
|
|
|
4
4
|
|
|
5
5
|
const getOctetContentTypeHandler = ()=>({
|
|
6
6
|
name: 'node-http-octet',
|
|
7
|
-
isMatch (
|
|
8
|
-
return
|
|
7
|
+
isMatch: (headers)=>{
|
|
8
|
+
return !!headers.get('content-type')?.startsWith('application/octet-stream');
|
|
9
9
|
},
|
|
10
10
|
async getInputs (opts, inputOpts) {
|
|
11
11
|
if (inputOpts.isBatchCall) {
|
|
@@ -2,8 +2,8 @@ import { Stream } from 'node:stream';
|
|
|
2
2
|
|
|
3
3
|
const getOctetContentTypeHandler = ()=>({
|
|
4
4
|
name: 'node-http-octet',
|
|
5
|
-
isMatch (
|
|
6
|
-
return
|
|
5
|
+
isMatch: (headers)=>{
|
|
6
|
+
return !!headers.get('content-type')?.startsWith('application/octet-stream');
|
|
7
7
|
},
|
|
8
8
|
async getInputs (opts, inputOpts) {
|
|
9
9
|
if (inputOpts.isBatchCall) {
|