@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.
Files changed (52) hide show
  1. package/dist/adapters/aws-lambda/content-type/json/index.js +2 -2
  2. package/dist/adapters/aws-lambda/content-type/json/index.mjs +2 -2
  3. package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.d.ts +8 -2
  4. package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.d.ts.map +1 -1
  5. package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.js +13 -2
  6. package/dist/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.mjs +13 -2
  7. package/dist/adapters/fastify/content-type/json/index.js +2 -2
  8. package/dist/adapters/fastify/content-type/json/index.mjs +2 -2
  9. package/dist/adapters/fetch/content-type/json/index.d.ts.map +1 -1
  10. package/dist/adapters/fetch/content-type/json/index.js +2 -2
  11. package/dist/adapters/fetch/content-type/json/index.mjs +2 -2
  12. package/dist/adapters/node-http/content-type/form-data/index.d.ts +0 -7
  13. package/dist/adapters/node-http/content-type/form-data/index.d.ts.map +1 -1
  14. package/dist/adapters/node-http/content-type/form-data/index.js +16 -94
  15. package/dist/adapters/node-http/content-type/form-data/index.mjs +17 -75
  16. package/dist/adapters/node-http/content-type/json/index.js +2 -2
  17. package/dist/adapters/node-http/content-type/json/index.mjs +2 -2
  18. package/dist/adapters/node-http/content-type/octet/index.d.ts.map +1 -1
  19. package/dist/adapters/node-http/content-type/octet/index.js +2 -2
  20. package/dist/adapters/node-http/content-type/octet/index.mjs +2 -2
  21. package/dist/bundle-analysis.json +95 -160
  22. package/dist/unstable-core-do-not-import/http/contentType.d.ts +1 -1
  23. package/dist/unstable-core-do-not-import/http/contentType.d.ts.map +1 -1
  24. package/package.json +2 -3
  25. package/src/adapters/aws-lambda/content-type/json/index.ts +2 -2
  26. package/src/adapters/content-handlers/selectContentHandlerOrUnsupportedMediaType.ts +29 -5
  27. package/src/adapters/fastify/content-type/json/index.ts +2 -2
  28. package/src/adapters/fetch/content-type/json/index.ts +2 -4
  29. package/src/adapters/node-http/content-type/form-data/index.ts +15 -119
  30. package/src/adapters/node-http/content-type/json/index.ts +2 -2
  31. package/src/adapters/node-http/content-type/octet/index.ts +4 -6
  32. package/src/unstable-core-do-not-import/http/contentType.ts +1 -1
  33. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.d.ts +0 -73
  34. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.d.ts.map +0 -1
  35. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.js +0 -89
  36. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.mjs +0 -86
  37. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.d.ts +0 -31
  38. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.d.ts.map +0 -1
  39. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.js +0 -29
  40. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.mjs +0 -27
  41. package/dist/adapters/node-http/content-type/form-data/streamSlice.d.ts +0 -16
  42. package/dist/adapters/node-http/content-type/form-data/streamSlice.d.ts.map +0 -1
  43. package/dist/adapters/node-http/content-type/form-data/streamSlice.js +0 -46
  44. package/dist/adapters/node-http/content-type/form-data/streamSlice.mjs +0 -44
  45. package/dist/adapters/node-http/content-type/form-data/uploadHandler.d.ts +0 -45
  46. package/dist/adapters/node-http/content-type/form-data/uploadHandler.d.ts.map +0 -1
  47. package/dist/adapters/node-http/content-type/form-data/uploadHandler.js +0 -18
  48. package/dist/adapters/node-http/content-type/form-data/uploadHandler.mjs +0 -15
  49. package/src/adapters/node-http/content-type/form-data/fileUploadHandler.ts +0 -277
  50. package/src/adapters/node-http/content-type/form-data/memoryUploadHandler.ts +0 -56
  51. package/src/adapters/node-http/content-type/form-data/streamSlice.ts +0 -56
  52. package/src/adapters/node-http/content-type/form-data/uploadHandler.ts +0 -89
@@ -1,44 +0,0 @@
1
- import { Transform } from 'node:stream';
2
-
3
- class SliceStream extends Transform {
4
- _transform(chunk, _, done) {
5
- this.indexOffset += chunk.length;
6
- if (!this.emitUp && this.indexOffset >= this.startIndex) {
7
- this.emitUp = true;
8
- const start = chunk.length - (this.indexOffset - this.startIndex);
9
- if (this.indexOffset > this.endIndex) {
10
- const end = chunk.length - (this.indexOffset - this.endIndex);
11
- this.emitDown = true;
12
- this.push(chunk.slice(start, end));
13
- } else {
14
- this.push(chunk.slice(start, chunk.length));
15
- }
16
- done();
17
- return;
18
- }
19
- if (this.emitUp && !this.emitDown) {
20
- if (this.indexOffset >= this.endIndex) {
21
- this.emitDown = true;
22
- this.push(chunk.slice(0, chunk.length - (this.indexOffset - this.endIndex)));
23
- } else {
24
- this.push(chunk);
25
- }
26
- done();
27
- return;
28
- }
29
- done();
30
- }
31
- constructor(startIndex = 0, endIndex = Infinity){
32
- super();
33
- this.startIndex = startIndex;
34
- this.endIndex = endIndex;
35
- this.indexOffset = 0;
36
- this.emitUp = false;
37
- this.emitDown = false;
38
- }
39
- }
40
- function streamSlice(startIndex = 0, endIndex = Infinity) {
41
- return new SliceStream(startIndex, endIndex);
42
- }
43
-
44
- export { streamSlice };
@@ -1,45 +0,0 @@
1
- import type { NodeOnDiskFile } from './fileUploadHandler';
2
- /**
3
- * @link https://github.com/remix-run/remix/blob/0bcb4a304dd2f08f6032c3bf0c3aa7eb5b976901/packages/remix-server-runtime/formData.ts
4
- */
5
- export type UploadHandlerPart = {
6
- name: string;
7
- filename?: string;
8
- contentType: string;
9
- data: AsyncIterable<Uint8Array>;
10
- };
11
- export type UploadHandler = (part: Required<UploadHandlerPart>) => Promise<File | NodeOnDiskFile | null | undefined>;
12
- export declare function composeUploadHandlers(...handlers: UploadHandler[]): UploadHandler;
13
- export declare class MaxPartSizeExceededError extends Error {
14
- field: string;
15
- maxBytes: number;
16
- constructor(field: string, maxBytes: number);
17
- }
18
- export declare class MaxBodySizeExceededError extends Error {
19
- maxBytes: number;
20
- constructor(maxBytes: number);
21
- }
22
- export type MemoryUploadHandlerFilterArgs = {
23
- filename?: string;
24
- contentType: string;
25
- name: string;
26
- };
27
- export type MemoryUploadHandlerOptions = {
28
- /**
29
- * The maximum upload size allowed. If the size is exceeded an error will be thrown.
30
- * Defaults to 3000000B (3MB).
31
- */
32
- maxPartSize?: number;
33
- /**
34
- *
35
- * @param filename
36
- * @param mimetype
37
- * @param encoding
38
- */
39
- filter?(args: MemoryUploadHandlerFilterArgs): Promise<boolean> | boolean;
40
- };
41
- /**
42
- * @see
43
- */
44
- export declare function createMemoryUploadHandler({ filter, maxPartSize, }?: MemoryUploadHandlerOptions): UploadHandler;
45
- //# sourceMappingURL=uploadHandler.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"uploadHandler.d.ts","sourceRoot":"","sources":["../../../../../src/adapters/node-http/content-type/form-data/uploadHandler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAE1D;;GAEG;AACH,MAAM,MAAM,iBAAiB,GAAG;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,IAAI,EAAE,aAAa,CAAC,UAAU,CAAC,CAAC;CACjC,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG,CAC1B,IAAI,EAAE,QAAQ,CAAC,iBAAiB,CAAC,KAC9B,OAAO,CAAC,IAAI,GAAG,cAAc,GAAG,IAAI,GAAG,SAAS,CAAC,CAAC;AAEvD,wBAAgB,qBAAqB,CACnC,GAAG,QAAQ,EAAE,aAAa,EAAE,GAC3B,aAAa,CAWf;AAED,qBAAa,wBAAyB,SAAQ,KAAK;IAC9B,KAAK,EAAE,MAAM;IAAS,QAAQ,EAAE,MAAM;gBAAtC,KAAK,EAAE,MAAM,EAAS,QAAQ,EAAE,MAAM;CAG1D;AAED,qBAAa,wBAAyB,SAAQ,KAAK;IAC9B,QAAQ,EAAE,MAAM;gBAAhB,QAAQ,EAAE,MAAM;CAGpC;AAED,MAAM,MAAM,6BAA6B,GAAG;IAC1C,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,0BAA0B,GAAG;IACvC;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;;;OAKG;IACH,MAAM,CAAC,CAAC,IAAI,EAAE,6BAA6B,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC;CAC1E,CAAC;AAEF;;GAEG;AACH,wBAAgB,yBAAyB,CAAC,EACxC,MAAM,EACN,WAAqB,GACtB,GAAE,0BAA+B,GAAG,aAAa,CAkBjD"}
@@ -1,18 +0,0 @@
1
- 'use strict';
2
-
3
- class MaxPartSizeExceededError extends Error {
4
- constructor(field, maxBytes){
5
- super(`Field "${field}" exceeded upload size of ${maxBytes} bytes.`);
6
- this.field = field;
7
- this.maxBytes = maxBytes;
8
- }
9
- }
10
- class MaxBodySizeExceededError extends Error {
11
- constructor(maxBytes){
12
- super(`Body exceeded upload size of ${maxBytes} bytes.`);
13
- this.maxBytes = maxBytes;
14
- }
15
- }
16
-
17
- exports.MaxBodySizeExceededError = MaxBodySizeExceededError;
18
- exports.MaxPartSizeExceededError = MaxPartSizeExceededError;
@@ -1,15 +0,0 @@
1
- class MaxPartSizeExceededError extends Error {
2
- constructor(field, maxBytes){
3
- super(`Field "${field}" exceeded upload size of ${maxBytes} bytes.`);
4
- this.field = field;
5
- this.maxBytes = maxBytes;
6
- }
7
- }
8
- class MaxBodySizeExceededError extends Error {
9
- constructor(maxBytes){
10
- super(`Body exceeded upload size of ${maxBytes} bytes.`);
11
- this.maxBytes = maxBytes;
12
- }
13
- }
14
-
15
- export { MaxBodySizeExceededError, MaxPartSizeExceededError };
@@ -1,277 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-empty-function */
2
-
3
- /**
4
- * Copyright 2021 Remix Software Inc.
5
- * 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:
6
- * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
- * 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.
8
- */
9
-
10
- /**
11
- * @link https://github.com/remix-run/remix/blob/0bcb4a304dd2f08f6032c3bf0c3aa7eb5b976901/packages/remix-node/upload/fileUploadHandler.ts
12
- */
13
- import { randomBytes } from 'crypto';
14
- import { createReadStream, createWriteStream, statSync } from 'fs';
15
- import { mkdir, rm, stat as statAsync, unlink } from 'fs/promises';
16
- import { tmpdir } from 'os';
17
- import { basename, dirname, extname, resolve as resolvePath } from 'path';
18
- import { finished, Readable } from 'stream';
19
- import { promisify } from 'util';
20
- import { streamSlice } from './streamSlice';
21
- import type { UploadHandler } from './uploadHandler';
22
- import { MaxPartSizeExceededError } from './uploadHandler';
23
-
24
- export async function readableStreamToString(
25
- stream: ReadableStream<Uint8Array>,
26
- encoding?: BufferEncoding,
27
- ) {
28
- const reader = stream.getReader();
29
- const chunks: Uint8Array[] = [];
30
-
31
- async function read() {
32
- const { done, value } = await reader.read();
33
-
34
- if (done) {
35
- return;
36
- } else if (value) {
37
- chunks.push(value);
38
- }
39
-
40
- await read();
41
- }
42
-
43
- await read();
44
-
45
- return Buffer.concat(chunks).toString(encoding);
46
- }
47
-
48
- export type FileUploadHandlerFilterArgs = {
49
- filename: string;
50
- contentType: string;
51
- name: string;
52
- };
53
-
54
- export type FileUploadHandlerPathResolverArgs = {
55
- filename: string;
56
- contentType: string;
57
- name: string;
58
- };
59
-
60
- /**
61
- * Chooses the path of the file to be uploaded. If a string is not
62
- * returned the file will not be written.
63
- */
64
- export type FileUploadHandlerPathResolver = (
65
- args: FileUploadHandlerPathResolverArgs,
66
- ) => string | undefined;
67
-
68
- export type FileUploadHandlerOptions = {
69
- /**
70
- * Avoid file conflicts by appending a count on the end of the filename
71
- * if it already exists on disk. Defaults to `true`.
72
- */
73
- avoidFileConflicts?: boolean;
74
- /**
75
- * The directory to write the upload.
76
- */
77
- directory?: FileUploadHandlerPathResolver | string;
78
- /**
79
- * The name of the file in the directory. Can be a relative path, the directory
80
- * structure will be created if it does not exist.
81
- */
82
- file?: FileUploadHandlerPathResolver;
83
- /**
84
- * The maximum upload size allowed. If the size is exceeded an error will be thrown.
85
- * Defaults to 3000000B (3MB).
86
- */
87
- maxPartSize?: number;
88
- /**
89
- *
90
- * @param filename
91
- * @param contentType
92
- * @param name
93
- */
94
- filter?(args: FileUploadHandlerFilterArgs): Promise<boolean> | boolean;
95
- };
96
-
97
- const defaultFilePathResolver: FileUploadHandlerPathResolver = ({
98
- filename,
99
- }) => {
100
- const ext = filename ? extname(filename) : '';
101
- return 'upload_' + randomBytes(4).readUInt32LE(0) + ext;
102
- };
103
-
104
- async function uniqueFile(filepath: string) {
105
- const ext = extname(filepath);
106
- let uniqueFilepath = filepath;
107
-
108
- for (
109
- let i = 1;
110
- await statAsync(uniqueFilepath)
111
- .then(() => true)
112
- .catch(() => false);
113
- i++
114
- ) {
115
- uniqueFilepath =
116
- (ext ? filepath.slice(0, -ext.length) : filepath) +
117
- `-${new Date().getTime()}${ext}`;
118
- }
119
-
120
- return uniqueFilepath;
121
- }
122
-
123
- export function createFileUploadHandler({
124
- directory = tmpdir(),
125
- avoidFileConflicts = true,
126
- file = defaultFilePathResolver,
127
- filter,
128
- maxPartSize = 3000000,
129
- }: FileUploadHandlerOptions = {}): UploadHandler {
130
- return async ({ name, filename, contentType, data }) => {
131
- if (
132
- !filename ||
133
- (filter && !(await filter({ name, filename, contentType })))
134
- ) {
135
- return undefined;
136
- }
137
-
138
- const dir =
139
- typeof directory === 'string'
140
- ? directory
141
- : directory({ name, filename, contentType });
142
-
143
- if (!dir) {
144
- return undefined;
145
- }
146
-
147
- const filedir = resolvePath(dir);
148
- const path =
149
- typeof file === 'string' ? file : file({ name, filename, contentType });
150
-
151
- if (!path) {
152
- return undefined;
153
- }
154
-
155
- let filepath = resolvePath(filedir, path);
156
-
157
- if (avoidFileConflicts) {
158
- filepath = await uniqueFile(filepath);
159
- }
160
-
161
- await mkdir(dirname(filepath), { recursive: true }).catch(() => {});
162
-
163
- const writeFileStream = createWriteStream(filepath);
164
- let size = 0;
165
- let deleteFile = false;
166
- try {
167
- for await (const chunk of data) {
168
- size += chunk.byteLength;
169
- if (size > maxPartSize) {
170
- deleteFile = true;
171
- throw new MaxPartSizeExceededError(name, maxPartSize);
172
- }
173
- writeFileStream.write(chunk);
174
- }
175
- } finally {
176
- writeFileStream.end();
177
- await promisify(finished)(writeFileStream);
178
-
179
- if (deleteFile) {
180
- await rm(filepath).catch(() => {});
181
- }
182
- }
183
-
184
- return new NodeOnDiskFile(filepath, contentType);
185
- };
186
- }
187
- type NodeJSReadableStreamEsque = {
188
- on(
189
- eventName: string | symbol,
190
- listener: (...args: any[]) => void,
191
- ): NodeJSReadableStreamEsque;
192
- };
193
-
194
- export class NodeOnDiskFile {
195
- name: string;
196
- lastModified = 0;
197
- webkitRelativePath = '';
198
-
199
- constructor(
200
- private filepath: string,
201
- public type: string,
202
- private slicer?: { start: number; end: number },
203
- ) {
204
- this.name = basename(filepath);
205
- }
206
-
207
- get size(): number {
208
- const stats = statSync(this.filepath);
209
-
210
- if (this.slicer) {
211
- const slice = this.slicer.end - this.slicer.start;
212
- return slice < 0 ? 0 : slice > stats.size ? stats.size : slice;
213
- }
214
-
215
- return stats.size;
216
- }
217
-
218
- slice(start?: number, end?: number, type?: string): NodeOnDiskFile {
219
- if (typeof start === 'number' && start < 0) start = this.size + start;
220
- if (typeof end === 'number' && end < 0) end = this.size + end;
221
-
222
- const startOffset = this.slicer?.start ?? 0;
223
-
224
- start = startOffset + (start ?? 0);
225
- end = startOffset + (end ?? this.size);
226
- return new NodeOnDiskFile(
227
- this.filepath,
228
- typeof type === 'string' ? type : this.type,
229
- {
230
- start,
231
- end,
232
- },
233
- );
234
- }
235
-
236
- async arrayBuffer(): Promise<ArrayBuffer> {
237
- let stream: Readable = createReadStream(this.filepath);
238
- if (this.slicer) {
239
- stream = stream.pipe(streamSlice(this.slicer.start, this.slicer.end));
240
- }
241
-
242
- return new Promise((resolve, reject) => {
243
- const buf: any[] = [];
244
- stream.on('data', (chunk) => buf.push(chunk));
245
- stream.on('end', () => {
246
- resolve(Buffer.concat(buf));
247
- });
248
- stream.on('error', (err) => {
249
- reject(err);
250
- });
251
- });
252
- }
253
-
254
- stream(): ReadableStream<any>;
255
- stream(): NodeJSReadableStreamEsque;
256
- stream(): NodeJSReadableStreamEsque | ReadableStream<any> {
257
- let stream: Readable = createReadStream(this.filepath);
258
- if (this.slicer) {
259
- stream = stream.pipe(streamSlice(this.slicer.start, this.slicer.end));
260
- }
261
-
262
- return Readable.toWeb(stream) as ReadableStream<Uint8Array>;
263
- }
264
-
265
- async text(): Promise<string> {
266
- return readableStreamToString(this.stream());
267
- }
268
-
269
- public readonly [Symbol.toStringTag] = 'File';
270
-
271
- remove(): Promise<void> {
272
- return unlink(this.filepath);
273
- }
274
- getFilePath(): string {
275
- return this.filepath;
276
- }
277
- }
@@ -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 'stream';
2
- import { Transform } from '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
- }