UNPKG

@restnfeel/agentc-starter-kit

Version:

한국어 기업용 CMS 모듈 - Task Master AI와 함께 빠르게 웹사이트를 구현할 수 있는 재사용 가능한 컴포넌트 시스템

104 lines (101 loc) 4.34 kB
import './_shims/index.js'; import { File } from './_shims/registry.js'; export { fileFromPath } from './_shims/registry.js'; const isResponseLike = (value) => value != null && typeof value === 'object' && typeof value.url === 'string' && typeof value.blob === 'function'; const isFileLike = (value) => value != null && typeof value === 'object' && typeof value.name === 'string' && typeof value.lastModified === 'number' && isBlobLike(value); /** * The BlobLike type omits arrayBuffer() because @types/node-fetch@^2.6.4 lacks it; but this check * adds the arrayBuffer() method type because it is available and used at runtime */ const isBlobLike = (value) => value != null && typeof value === 'object' && typeof value.size === 'number' && typeof value.type === 'string' && typeof value.text === 'function' && typeof value.slice === 'function' && typeof value.arrayBuffer === 'function'; /** * Helper for creating a {@link File} to pass to an SDK upload method from a variety of different data formats * @param value the raw content of the file. Can be an {@link Uploadable}, {@link BlobLikePart}, or {@link AsyncIterable} of {@link BlobLikePart}s * @param {string=} name the name of the file. If omitted, toFile will try to determine a file name from bits if possible * @param {Object=} options additional properties * @param {string=} options.type the MIME type of the content * @param {number=} options.lastModified the last modified timestamp * @returns a {@link File} with the given properties */ async function toFile(value, name, options) { // If it's a promise, resolve it. value = await value; // If we've been given a `File` we don't need to do anything if (isFileLike(value)) { return value; } if (isResponseLike(value)) { const blob = await value.blob(); name || (name = new URL(value.url).pathname.split(/[\\/]/).pop() ?? 'unknown_file'); // we need to convert the `Blob` into an array buffer because the `Blob` class // that `node-fetch` defines is incompatible with the web standard which results // in `new File` interpreting it as a string instead of binary data. const data = isBlobLike(blob) ? [(await blob.arrayBuffer())] : [blob]; return new File(data, name, options); } const bits = await getBytes(value); name || (name = getName(value) ?? 'unknown_file'); if (!options?.type) { const type = bits[0]?.type; if (typeof type === 'string') { options = { ...options, type }; } } return new File(bits, name, options); } async function getBytes(value) { let parts = []; if (typeof value === 'string' || ArrayBuffer.isView(value) || // includes Uint8Array, Buffer, etc. value instanceof ArrayBuffer) { parts.push(value); } else if (isBlobLike(value)) { parts.push(await value.arrayBuffer()); } else if (isAsyncIterableIterator(value) // includes Readable, ReadableStream, etc. ) { for await (const chunk of value) { parts.push(chunk); // TODO, consider validating? } } else { throw new Error(`Unexpected data type: ${typeof value}; constructor: ${value?.constructor ?.name}; props: ${propsForError(value)}`); } return parts; } function propsForError(value) { const props = Object.getOwnPropertyNames(value); return `[${props.map((p) => `"${p}"`).join(', ')}]`; } function getName(value) { return (getStringFromMaybeBuffer(value.name) || getStringFromMaybeBuffer(value.filename) || // For fs.ReadStream getStringFromMaybeBuffer(value.path)?.split(/[\\/]/).pop()); } const getStringFromMaybeBuffer = (x) => { if (typeof x === 'string') return x; if (typeof Buffer !== 'undefined' && x instanceof Buffer) return String(x); return undefined; }; const isAsyncIterableIterator = (value) => value != null && typeof value === 'object' && typeof value[Symbol.asyncIterator] === 'function'; const isMultipartBody = (body) => body && typeof body === 'object' && body.body && body[Symbol.toStringTag] === 'MultipartBody'; export { isBlobLike, isFileLike, isMultipartBody, isResponseLike, toFile }; //# sourceMappingURL=uploads.js.map