cloudflare/cloudflare-typescript

Public

mirrored from https://github.com/cloudflare/cloudflare-typescriptAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v3.0.0-beta.2

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/uploads.ts

245lines · modeblame

2d51afdcstainless-app[bot]2 years ago1import { type RequestOptions } from './core';
2import {
3FormData,
4File,
5type Blob,
6type FilePropertyBag,
7getMultipartRequestOptions,
8type FsReadStream,
9isFsReadStream,
10} from './_shims/index';
11import { MultipartBody } from './_shims/MultipartBody';
12export { fileFromPath } from './_shims/index';
13
14type BlobLikePart = string | ArrayBuffer | ArrayBufferView | BlobLike | Uint8Array | DataView;
15export type BlobPart = string | ArrayBuffer | ArrayBufferView | Blob | Uint8Array | DataView;
16
17/**
18* Typically, this is a native "File" class.
19*
20* We provide the {@link toFile} utility to convert a variety of objects
21* into the File class.
22*
23* For convenience, you can also pass a fetch Response, or in Node,
24* the result of fs.createReadStream().
25*/
26export type Uploadable = FileLike | ResponseLike | FsReadStream;
27
28/**
29* Intended to match web.Blob, node.Blob, node-fetch.Blob, etc.
30*/
31export interface BlobLike {
32/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/size) */
33readonly size: number;
34/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/type) */
35readonly type: string;
36/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/text) */
37text(): Promise<string>;
38/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/slice) */
39slice(start?: number, end?: number): BlobLike;
40// unfortunately @types/node-fetch@^2.6.4 doesn't type the arrayBuffer method
41}
42
43/**
44* Intended to match web.File, node.File, node-fetch.File, etc.
45*/
46export interface FileLike extends BlobLike {
47/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/lastModified) */
48readonly lastModified: number;
49/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/name) */
50readonly name: string;
51}
52
53/**
54* Intended to match web.Response, node.Response, node-fetch.Response, etc.
55*/
56export interface ResponseLike {
57url: string;
58blob(): Promise<BlobLike>;
59}
60
61export const isResponseLike = (value: any): value is ResponseLike =>
62value != null &&
63typeof value === 'object' &&
64typeof value.url === 'string' &&
65typeof value.blob === 'function';
66
67export const isFileLike = (value: any): value is FileLike =>
68value != null &&
69typeof value === 'object' &&
70typeof value.name === 'string' &&
71typeof value.lastModified === 'number' &&
72isBlobLike(value);
73
74/**
75* The BlobLike type omits arrayBuffer() because @types/node-fetch@^2.6.4 lacks it; but this check
76* adds the arrayBuffer() method type because it is available and used at runtime
77*/
78export const isBlobLike = (value: any): value is BlobLike & { arrayBuffer(): Promise<ArrayBuffer> } =>
79value != null &&
80typeof value === 'object' &&
81typeof value.size === 'number' &&
82typeof value.type === 'string' &&
83typeof value.text === 'function' &&
84typeof value.slice === 'function' &&
85typeof value.arrayBuffer === 'function';
86
87export const isUploadable = (value: any): value is Uploadable => {
88return isFileLike(value) || isResponseLike(value) || isFsReadStream(value);
89};
90
91export type ToFileInput = Uploadable | Exclude<BlobLikePart, string> | AsyncIterable<BlobLikePart>;
92
93/**
94* Helper for creating a {@link File} to pass to an SDK upload method from a variety of different data formats
95* @param value the raw content of the file. Can be an {@link Uploadable}, {@link BlobLikePart}, or {@link AsyncIterable} of {@link BlobLikePart}s
96* @param {string=} name the name of the file. If omitted, toFile will try to determine a file name from bits if possible
97* @param {Object=} options additional properties
98* @param {string=} options.type the MIME type of the content
99* @param {number=} options.lastModified the last modified timestamp
100* @returns a {@link File} with the given properties
101*/
102export async function toFile(
103value: ToFileInput | PromiseLike<ToFileInput>,
104name?: string | null | undefined,
105options: FilePropertyBag | undefined = {},
106): Promise<FileLike> {
107// If it's a promise, resolve it.
108value = await value;
109
110if (isResponseLike(value)) {
111const blob = await value.blob();
112name ||= new URL(value.url).pathname.split(/[\\/]/).pop() ?? 'unknown_file';
113
114return new File([blob as any], name, options);
115}
116
117const bits = await getBytes(value);
118
119name ||= getName(value) ?? 'unknown_file';
120
121if (!options.type) {
122const type = (bits[0] as any)?.type;
123if (typeof type === 'string') {
124options = { ...options, type };
125}
126}
127
128return new File(bits, name, options);
129}
130
131async function getBytes(value: ToFileInput): Promise<Array<BlobPart>> {
132let parts: Array<BlobPart> = [];
133if (
134typeof value === 'string' ||
135ArrayBuffer.isView(value) || // includes Uint8Array, Buffer, etc.
136value instanceof ArrayBuffer
137) {
138parts.push(value);
139} else if (isBlobLike(value)) {
140parts.push(await value.arrayBuffer());
141} else if (
142isAsyncIterableIterator(value) // includes Readable, ReadableStream, etc.
143) {
144for await (const chunk of value) {
145parts.push(chunk as BlobPart); // TODO, consider validating?
146}
147} else {
148throw new Error(
149`Unexpected data type: ${typeof value}; constructor: ${value?.constructor
150?.name}; props: ${propsForError(value)}`,
151);
152}
153
154return parts;
155}
156
157function propsForError(value: any): string {
158const props = Object.getOwnPropertyNames(value);
159return `[${props.map((p) => `"${p}"`).join(', ')}]`;
160}
161
162function getName(value: any): string | undefined {
163return (
164getStringFromMaybeBuffer(value.name) ||
165getStringFromMaybeBuffer(value.filename) ||
166// For fs.ReadStream
167getStringFromMaybeBuffer(value.path)?.split(/[\\/]/).pop()
168);
169}
170
171const getStringFromMaybeBuffer = (x: string | Buffer | unknown): string | undefined => {
172if (typeof x === 'string') return x;
173if (typeof Buffer !== 'undefined' && x instanceof Buffer) return String(x);
174return undefined;
175};
176
177const isAsyncIterableIterator = (value: any): value is AsyncIterableIterator<unknown> =>
178value != null && typeof value === 'object' && typeof value[Symbol.asyncIterator] === 'function';
179
180export const isMultipartBody = (body: any): body is MultipartBody =>
181body && typeof body === 'object' && body.body && body[Symbol.toStringTag] === 'MultipartBody';
182
183/**
184* Returns a multipart/form-data request if any part of the given request body contains a File / Blob value.
185* Otherwise returns the request as is.
186*/
187export const maybeMultipartFormRequestOptions = async <T = Record<string, unknown>>(
188opts: RequestOptions<T>,
189): Promise<RequestOptions<T | MultipartBody>> => {
190if (!hasUploadableValue(opts.body)) return opts;
191
192const form = await createForm(opts.body);
193return getMultipartRequestOptions(form, opts);
194};
195
196export const multipartFormRequestOptions = async <T = Record<string, unknown>>(
197opts: RequestOptions<T>,
198): Promise<RequestOptions<T | MultipartBody>> => {
199const form = await createForm(opts.body);
200return getMultipartRequestOptions(form, opts);
201};
202
203export const createForm = async <T = Record<string, unknown>>(body: T | undefined): Promise<FormData> => {
204const form = new FormData();
205await Promise.all(Object.entries(body || {}).map(([key, value]) => addFormValue(form, key, value)));
206return form;
207};
208
209const hasUploadableValue = (value: unknown): boolean => {
210if (isUploadable(value)) return true;
211if (Array.isArray(value)) return value.some(hasUploadableValue);
212if (value && typeof value === 'object') {
213for (const k in value) {
214if (hasUploadableValue((value as any)[k])) return true;
215}
216}
217return false;
218};
219
220const addFormValue = async (form: FormData, key: string, value: unknown): Promise<void> => {
221if (value === undefined) return;
222if (value == null) {
223throw new TypeError(
224`Received null for "${key}"; to pass null in FormData, you must use the string 'null'`,
225);
226}
227
228// TODO: make nested formats configurable
229if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
230form.append(key, String(value));
231} else if (isUploadable(value)) {
232const file = await toFile(value);
233form.append(key, file as File);
234} else if (Array.isArray(value)) {
235await Promise.all(value.map((entry) => addFormValue(form, key + '[]', entry)));
236} else if (typeof value === 'object') {
237await Promise.all(
238Object.entries(value).map(([name, prop]) => addFormValue(form, `${key}[${name}]`, prop)),
239);
240} else {
241throw new TypeError(
242`Invalid value given to form, expected a string, number, boolean, object, Array, File or Blob but got ${value} instead`,
243);
244}
245};