|
| 1 | +import { File } from './shims/file.node.js'; |
| 2 | +import { BlobPart, getName, makeFile, isAsyncIterable } from './uploads'; |
| 3 | +import type { FilePropertyBag } from './builtin-types'; |
| 4 | + |
| 5 | +type BlobLikePart = string | ArrayBuffer | ArrayBufferView | BlobLike | DataView; |
| 6 | + |
| 7 | +/** |
| 8 | + * Intended to match DOM Blob, node-fetch Blob, node:buffer Blob, etc. |
| 9 | + * Don't add arrayBuffer here, node-fetch doesn't have it |
| 10 | + */ |
| 11 | +interface BlobLike { |
| 12 | + /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/size) */ |
| 13 | + readonly size: number; |
| 14 | + /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/type) */ |
| 15 | + readonly type: string; |
| 16 | + /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/text) */ |
| 17 | + text(): Promise<string>; |
| 18 | + /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/slice) */ |
| 19 | + slice(start?: number, end?: number): BlobLike; |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * This check adds the arrayBuffer() method type because it is available and used at runtime |
| 24 | + */ |
| 25 | +const isBlobLike = (value: any): value is BlobLike & { arrayBuffer(): Promise<ArrayBuffer> } => |
| 26 | + value != null && |
| 27 | + typeof value === 'object' && |
| 28 | + typeof value.size === 'number' && |
| 29 | + typeof value.type === 'string' && |
| 30 | + typeof value.text === 'function' && |
| 31 | + typeof value.slice === 'function' && |
| 32 | + typeof value.arrayBuffer === 'function'; |
| 33 | + |
| 34 | +/** |
| 35 | + * Intended to match DOM File, node:buffer File, undici File, etc. |
| 36 | + */ |
| 37 | +interface FileLike extends BlobLike { |
| 38 | + /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/lastModified) */ |
| 39 | + readonly lastModified: number; |
| 40 | + /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/name) */ |
| 41 | + readonly name?: string | undefined; |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * This check adds the arrayBuffer() method type because it is available and used at runtime |
| 46 | + */ |
| 47 | +const isFileLike = (value: any): value is FileLike & { arrayBuffer(): Promise<ArrayBuffer> } => |
| 48 | + value != null && |
| 49 | + typeof value === 'object' && |
| 50 | + typeof value.name === 'string' && |
| 51 | + typeof value.lastModified === 'number' && |
| 52 | + isBlobLike(value); |
| 53 | + |
| 54 | +/** |
| 55 | + * Intended to match DOM Response, node-fetch Response, undici Response, etc. |
| 56 | + */ |
| 57 | +export interface ResponseLike { |
| 58 | + url: string; |
| 59 | + blob(): Promise<BlobLike>; |
| 60 | +} |
| 61 | + |
| 62 | +const isResponseLike = (value: any): value is ResponseLike => |
| 63 | + value != null && |
| 64 | + typeof value === 'object' && |
| 65 | + typeof value.url === 'string' && |
| 66 | + typeof value.blob === 'function'; |
| 67 | + |
| 68 | +export type ToFileInput = |
| 69 | + | FileLike |
| 70 | + | ResponseLike |
| 71 | + | Exclude<BlobLikePart, string> |
| 72 | + | AsyncIterable<BlobLikePart>; |
| 73 | + |
| 74 | +/** |
| 75 | + * Helper for creating a {@link File} to pass to an SDK upload method from a variety of different data formats |
| 76 | + * @param value the raw content of the file. Can be an {@link Uploadable}, {@link BlobLikePart}, or {@link AsyncIterable} of {@link BlobLikePart}s |
| 77 | + * @param {string=} name the name of the file. If omitted, toFile will try to determine a file name from bits if possible |
| 78 | + * @param {Object=} options additional properties |
| 79 | + * @param {string=} options.type the MIME type of the content |
| 80 | + * @param {number=} options.lastModified the last modified timestamp |
| 81 | + * @returns a {@link File} with the given properties |
| 82 | + */ |
| 83 | +export async function toFile( |
| 84 | + value: ToFileInput | PromiseLike<ToFileInput>, |
| 85 | + name?: string | null | undefined, |
| 86 | + options?: FilePropertyBag | undefined, |
| 87 | +): Promise<File> { |
| 88 | + // If it's a promise, resolve it. |
| 89 | + value = await value; |
| 90 | + |
| 91 | + // If we've been given a `File` we don't need to do anything |
| 92 | + if (isFileLike(value)) { |
| 93 | + if (File && value instanceof File) { |
| 94 | + return value; |
| 95 | + } |
| 96 | + return makeFile([await value.arrayBuffer()], value.name); |
| 97 | + } |
| 98 | + |
| 99 | + if (isResponseLike(value)) { |
| 100 | + const blob = await value.blob(); |
| 101 | + name ||= new URL(value.url).pathname.split(/[\\/]/).pop(); |
| 102 | + |
| 103 | + return makeFile(await getBytes(blob), name, options); |
| 104 | + } |
| 105 | + |
| 106 | + const parts = await getBytes(value); |
| 107 | + |
| 108 | + name ||= getName(value); |
| 109 | + |
| 110 | + if (!options?.type) { |
| 111 | + const type = parts.find((part) => typeof part === 'object' && 'type' in part && part.type); |
| 112 | + if (typeof type === 'string') { |
| 113 | + options = { ...options, type }; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return makeFile(parts, name, options); |
| 118 | +} |
| 119 | + |
| 120 | +async function getBytes(value: BlobLikePart | AsyncIterable<BlobLikePart>): Promise<Array<BlobPart>> { |
| 121 | + let parts: Array<BlobPart> = []; |
| 122 | + if ( |
| 123 | + typeof value === 'string' || |
| 124 | + ArrayBuffer.isView(value) || // includes Uint8Array, Buffer, etc. |
| 125 | + value instanceof ArrayBuffer |
| 126 | + ) { |
| 127 | + parts.push(value); |
| 128 | + } else if (isBlobLike(value)) { |
| 129 | + parts.push(value instanceof Blob ? value : await value.arrayBuffer()); |
| 130 | + } else if ( |
| 131 | + isAsyncIterable(value) // includes Readable, ReadableStream, etc. |
| 132 | + ) { |
| 133 | + for await (const chunk of value) { |
| 134 | + parts.push(...(await getBytes(chunk as BlobLikePart))); // TODO, consider validating? |
| 135 | + } |
| 136 | + } else { |
| 137 | + const constructor = value?.constructor?.name; |
| 138 | + throw new Error( |
| 139 | + `Unexpected data type: ${typeof value}${ |
| 140 | + constructor ? `; constructor: ${constructor}` : '' |
| 141 | + }${propsForError(value)}`, |
| 142 | + ); |
| 143 | + } |
| 144 | + |
| 145 | + return parts; |
| 146 | +} |
| 147 | + |
| 148 | +function propsForError(value: unknown): string { |
| 149 | + if (typeof value !== 'object' || value === null) return ''; |
| 150 | + const props = Object.getOwnPropertyNames(value); |
| 151 | + return `; props: [${props.map((p) => `"${p}"`).join(', ')}]`; |
| 152 | +} |
0 commit comments