@trpc/server 11.0.0-rc.345 → 11.0.0-rc.346

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (27) hide show
  1. package/dist/adapters/node-http/content-type/form-data/index.d.ts +0 -7
  2. package/dist/adapters/node-http/content-type/form-data/index.d.ts.map +1 -1
  3. package/dist/adapters/node-http/content-type/form-data/index.js +14 -92
  4. package/dist/adapters/node-http/content-type/form-data/index.mjs +15 -73
  5. package/dist/bundle-analysis.json +77 -142
  6. package/package.json +2 -3
  7. package/src/adapters/node-http/content-type/form-data/index.ts +13 -114
  8. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.d.ts +0 -73
  9. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.d.ts.map +0 -1
  10. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.js +0 -89
  11. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.mjs +0 -86
  12. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.d.ts +0 -31
  13. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.d.ts.map +0 -1
  14. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.js +0 -29
  15. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.mjs +0 -27
  16. package/dist/adapters/node-http/content-type/form-data/streamSlice.d.ts +0 -16
  17. package/dist/adapters/node-http/content-type/form-data/streamSlice.d.ts.map +0 -1
  18. package/dist/adapters/node-http/content-type/form-data/streamSlice.js +0 -46
  19. package/dist/adapters/node-http/content-type/form-data/streamSlice.mjs +0 -44
  20. package/dist/adapters/node-http/content-type/form-data/uploadHandler.d.ts +0 -45
  21. package/dist/adapters/node-http/content-type/form-data/uploadHandler.d.ts.map +0 -1
  22. package/dist/adapters/node-http/content-type/form-data/uploadHandler.js +0 -18
  23. package/dist/adapters/node-http/content-type/form-data/uploadHandler.mjs +0 -15
  24. package/src/adapters/node-http/content-type/form-data/fileUploadHandler.ts +0 -277
  25. package/src/adapters/node-http/content-type/form-data/memoryUploadHandler.ts +0 -56
  26. package/src/adapters/node-http/content-type/form-data/streamSlice.ts +0 -56
  27. package/src/adapters/node-http/content-type/form-data/uploadHandler.ts +0 -89
@@ -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":"AAAA;;;;;GAKG;AAWH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAC1D,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAErE,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,UAAU,CAAC;AA2F3D,eAAO,MAAM,6BAA6B,EAAE,CAC1C,OAAO,SAAS,SAAS,EACzB,QAAQ,SAAS,eAAe,EAChC,SAAS,SAAS,gBAAgB,OAC7B,0BAA0B,CAAC,OAAO,EAAE,QAAQ,EAAE,SAAS,CAoB5D,CAAC;AAEH,OAAO,EACL,wBAAwB,EACxB,wBAAwB,EACxB,KAAK,aAAa,GACnB,MAAM,iBAAiB,CAAC"}
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,CA6B5D,CAAC"}
@@ -1,96 +1,7 @@
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
7
  isMatch (opts) {
@@ -100,11 +11,22 @@ const getFormDataContentTypeHandler = ()=>({
100
11
  if (inputOpts.isBatchCall) {
101
12
  throw new Error('Batch calls not supported for form-data');
102
13
  }
103
- const form = await parseMultipartFormData(opts.req, memoryUploadHandler.createMemoryUploadHandler());
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,76 +1,5 @@
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
5
  isMatch (opts) {
@@ -80,9 +9,22 @@ const getFormDataContentTypeHandler = ()=>({
80
9
  if (inputOpts.isBatchCall) {
81
10
  throw new Error('Batch calls not supported for form-data');
82
11
  }
83
- const form = await parseMultipartFormData(opts.req, createMemoryUploadHandler());
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 { MaxBodySizeExceededError, getFormDataContentTypeHandler };
30
+ export { getFormDataContentTypeHandler };