|
| 1 | +import mime from 'mime'; |
| 2 | +import type { ReadableStream as ReadableNodeStream } from 'node:stream/web'; |
| 3 | + |
| 4 | +type FormDataValue = string | Blob | ArrayBuffer | ReadableStream | ReadableNodeStream; |
| 5 | + |
| 6 | +class FileWithSize extends File { |
| 7 | + size: number = 0; |
| 8 | +} |
| 9 | + |
| 10 | +interface AppendOptions { |
| 11 | + contentLength?: number; |
| 12 | + contentType?: string; |
| 13 | +} |
| 14 | + |
| 15 | +export class FormDataService { |
| 16 | + formData: FormData; |
| 17 | + |
| 18 | + constructor() { |
| 19 | + this.formData = new FormData(); |
| 20 | + } |
| 21 | + |
| 22 | + async append(value: FormDataValue, filename: string, options: AppendOptions = {}) { |
| 23 | + const blobOptions = { |
| 24 | + type: options.contentType ?? mime.getType(filename) ?? undefined, |
| 25 | + }; |
| 26 | + |
| 27 | + if (typeof value === 'string') { |
| 28 | + this.formData.append('file', new Blob([value], blobOptions), filename); |
| 29 | + } else if (value instanceof Blob) { |
| 30 | + this.formData.append('file', new Blob([value], blobOptions), filename); |
| 31 | + } else if (ArrayBuffer.isView(value) || value instanceof ArrayBuffer) { |
| 32 | + this.formData.append('file', new Blob([value], blobOptions), filename); |
| 33 | + } else if (value instanceof ReadableStream) { |
| 34 | + const file = new FileWithSize([], filename, blobOptions); |
| 35 | + |
| 36 | + if (options.contentLength != undefined) { |
| 37 | + file.size = options.contentLength; |
| 38 | + file.stream = () => value as ReadableStream; |
| 39 | + } else { |
| 40 | + const [streamForSize, streamForContent] = value.tee(); |
| 41 | + |
| 42 | + file.size = await this.getStreamSize(streamForSize); |
| 43 | + file.stream = () => streamForContent as ReadableStream; |
| 44 | + } |
| 45 | + |
| 46 | + this.formData.append('file', file); |
| 47 | + } else { |
| 48 | + throw new Error('Invalid value'); // todo error handling |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private async getStreamSize(stream: ReadableStream | ReadableNodeStream): Promise<number> { |
| 53 | + let totalSize = 0; |
| 54 | + const reader = stream.getReader(); |
| 55 | + |
| 56 | + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition |
| 57 | + while (true) { |
| 58 | + const { done, value } = await reader.read(); |
| 59 | + if (done) break; |
| 60 | + |
| 61 | + if (value instanceof Uint8Array) { |
| 62 | + totalSize += value.length; |
| 63 | + } else if (typeof value === 'string') { |
| 64 | + totalSize += new TextEncoder().encode(value).length; |
| 65 | + } else if (value instanceof Blob) { |
| 66 | + totalSize += value.size; |
| 67 | + } else if (ArrayBuffer.isView(value) || value instanceof ArrayBuffer) { |
| 68 | + totalSize += value.byteLength; |
| 69 | + } else if (value === null || value === undefined) { |
| 70 | + continue; |
| 71 | + } else { |
| 72 | + throw new Error(`Unsupported value type: ${typeof value}`); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return totalSize; |
| 77 | + } |
| 78 | +} |
0 commit comments