UNPKG

@squarecloud/blob

Version:
370 lines (356 loc) 11.4 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => { __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); return value; }; // src/utils/mimetype/mimetypes.ts var mimeTypesWithExtension = { "video/mp4": ["mp4"], "video/mpeg": ["mpeg"], "video/webm": ["webm"], "video/x-flv": ["flv"], "video/x-m4v": ["m4v"], "image/jpeg": ["jpg", "jpeg"], "image/png": ["png"], "image/apng": ["apng"], "image/tiff": ["tiff"], "image/gif": ["gif"], "image/webp": ["webp"], "image/bmp": ["bmp"], "image/svg+xml": ["svg"], "image/x-icon": ["ico"], "image/ico": ["ico"], "image/cur": ["cur"], "image/heic": ["heic"], "image/heif": ["heif"], "audio/wav": ["wav"], "audio/ogg": ["ogg"], "audio/opus": ["opus"], "audio/mp4": ["mp4"], "audio/mpeg": ["mp3"], "audio/aac": ["aac"], "text/plain": ["txt"], "text/html": ["html"], "text/css": ["css"], "text/csv": ["csv"], "text/x-sql": ["sql"], "application/xml": ["xml"], "application/sql": ["sql"], "application/x-sql": ["sql"], "application/x-sqlite3": ["sqlite3"], "application/x-pkcs12": ["pfx"], "application/pdf": ["pdf"], "application/json": ["json"], "application/javascript": ["js"] }; var mimeTypes = Object.keys(mimeTypesWithExtension); // src/utils/mimetype/index.ts var MimeTypeUtil = class { /** * Returns the corresponding MIME type for a given file extension. * * @param extension - The file extension to search for. * @return The MIME type associated with the extension, or "text/plain" if not found. * * @example * ```js * MimeTypeUtil.fromExtension("jpeg") // "image/jpeg" | Supported * MimeTypeUtil.fromExtension("json") // "application/json" | Supported * MimeTypeUtil.fromExtension("potato") // "text/plain" | Unsupported, defaults to text/plain * ``` */ static fromExtension(extension) { const entries = Object.entries(mimeTypesWithExtension); const mimeType = entries.find( ([, extensions]) => extensions.includes(extension) )?.[0]; return mimeType || "text/plain"; } }; /** Supported mime types with their extensions */ __publicField(MimeTypeUtil, "mimeTypesWithExtension", mimeTypesWithExtension); /** All supported mime types */ __publicField(MimeTypeUtil, "mimeTypes", mimeTypes); // src/structures/error.ts var SquareCloudBlobError = class _SquareCloudBlobError extends Error { constructor(code, message, cause) { super(message, { cause }); this.name = _SquareCloudBlobError.name; this.message = this.getMessage(code); } getMessage(rawCode) { const code = rawCode.replaceAll("_", " ").toLowerCase().replace(/(^|\s)\S/g, (L) => L.toUpperCase()); const message = this.message ? `: ${this.message}` : ""; return `${code}${message}`; } }; // src/utils/object-url.ts var objectUrlRegex = /^(?<url>https:\/\/public-blob\.squarecloud\.dev)?\/?(?<userId>\d+\/)(?<prefix>[\w\d-_]+\/)?(?<name>[\w\d_]+)-(?<hash>[\w\d]+)\.(?<extension>\w+)$/; function parseObjectUrl(url) { const match = url.match(objectUrlRegex); if (!match?.groups) { throw new SquareCloudBlobError("Invalid object URL"); } const payload = { userId: match.groups.userId.replace("/", ""), prefix: match.groups.prefix?.replace("/", ""), name: match.groups.name, hash: match.groups.hash, extension: match.groups.extension }; return { id: `${payload.userId}/${payload.prefix ? `${payload.prefix}/` : ""}${payload.name}-${payload.hash}.${payload.extension}`, ...payload }; } // src/structures/object.ts var BlobObject = class { constructor(data) { /** The id of the object */ __publicField(this, "id"); /** The url to view or download the object */ __publicField(this, "url"); /** The name of the object */ __publicField(this, "name"); /** The prefix of the object (Optional) */ __publicField(this, "prefix"); /** The hash of the object */ __publicField(this, "hash"); /** The id of the user who created the object */ __publicField(this, "userId"); /** The file extension of the object */ __publicField(this, "extension"); /** The MIME type of the object */ __publicField(this, "mimeType"); /** The size of the object in bytes */ __publicField(this, "size"); /** The expiration date of the object (Only available using `objects.list`) */ __publicField(this, "expiresAt"); /** The creation date of the object (Only available using `objects.list`) */ __publicField(this, "createdAt"); const { id, name, prefix, hash, userId, extension } = parseObjectUrl( data.idOrUrl ); this.id = id; this.url = `https://public-blob.squarecloud.dev/${id}`; this.name = name; this.prefix = prefix; this.hash = hash; this.userId = userId; this.extension = extension; this.mimeType = MimeTypeUtil.fromExtension(extension); this.size = data.size; this.expiresAt = data.expiresAt; this.createdAt = data.createdAt; } }; // src/utils/path-like.ts import { readFile } from "fs/promises"; async function parsePathLike(pathLike) { if (typeof pathLike === "string") { const fileBuffer = await readFile(pathLike).catch(() => void 0); if (!fileBuffer) { throw new SquareCloudBlobError("INVALID_FILE", "File not found"); } return fileBuffer; } return pathLike; } // src/validation/schemas/create.ts import { z as z2 } from "zod"; // src/validation/schemas/common.ts import { z } from "zod"; var stringSchema = z.string(); var nameLikeSchema = z.string().min(3).max(32).regex(/^[a-zA-Z0-9_]{3,32}$/, { message: "Name must contain only letters, numbers and _" }); // src/validation/schemas/create.ts var createObjectSchema = z2.object({ /** A string representing the name for the file. */ name: nameLikeSchema, /** Use absolute path, Buffer or Blob */ file: z2.string().or(z2.instanceof(Buffer)), /** A string representing the MIME type of the file. */ mimeType: z2.enum(MimeTypeUtil.mimeTypes).optional(), /** A string representing the prefix for the file. */ prefix: nameLikeSchema.optional(), /** A number indicating the expiration period of the file, ranging from 1 to 365 days. */ expiresIn: z2.number().min(1).max(365).optional(), /** Set to true if a security hash is required. */ securityHash: z2.boolean().optional(), /** Set to true if the file should be set for automatic download. */ autoDownload: z2.boolean().optional() }).refine(({ file, mimeType }) => !(file instanceof Buffer && !mimeType), { message: "mimeType is required if file is a Buffer", path: ["mimeType"] }); var createObjectPayloadSchema = createObjectSchema.transform( ({ file, securityHash, autoDownload, expiresIn, ...rest }) => ({ file, mimeType: typeof file === "string" ? MimeTypeUtil.fromExtension(file.split(".")[1]) : void 0, params: { ...rest, expire: expiresIn, security_hash: securityHash, auto_download: autoDownload } }) ); var createObjectResponseSchema = z2.object({ /** The id of the uploaded file. */ id: z2.string(), /** The name of the uploaded file. */ name: z2.string(), /** The prefix of the uploaded file. */ prefix: z2.string().optional(), /** The size of the uploaded file. */ size: z2.number(), /** The URL of the uploaded file. (File distributed in Square Cloud CDN) */ url: z2.string() }); // src/validation/assertions/handlers.ts function handleAPIObjectAssertion({ schema, value, code }) { const name = code.toLowerCase().replaceAll("_", " "); try { return schema.parse(value); } catch (err) { const cause = err.errors?.map((err2) => ({ ...err2, path: err2.path.join(" > ") })); throw new SquareCloudBlobError( `INVALID_API_${code}`, `Invalid ${name} object received from API`, { cause } ); } } // src/validation/assertions/create.ts function assertCreateObjectResponse(value) { return handleAPIObjectAssertion({ schema: createObjectResponseSchema, code: "CREATE_OBJECT", value }); } // src/validation/schemas/list.ts import { z as z3 } from "zod"; var listObjectsSchema = z3.object({ /** Filter by prefix */ prefix: z3.string().optional(), /** Return objects after this token */ continuationToken: z3.string().optional() }); var listObjectsPayloadSchema = listObjectsSchema.optional().transform((params) => ({ params })); var listObjectResponseSchema = z3.object({ id: z3.string(), size: z3.number(), created_at: z3.coerce.date(), expires_at: z3.coerce.date().optional() }); var listObjectsResponseSchema = z3.object({ objects: z3.array(listObjectResponseSchema).default([]) }); // src/validation/assertions/list.ts function assertListObjectsResponse(value) { return handleAPIObjectAssertion({ schema: listObjectsResponseSchema, code: "LIST_OBJECTS", value }); } // src/managers/objects.ts var ObjectsManager = class { constructor(client) { this.client = client; } /** * Lists all objects in the storage. * * @example * ```js * blob.objects.list(); * ``` */ async list(options) { const payload = listObjectsPayloadSchema.parse(options); const { response } = await this.client.api.request( "objects", { params: payload.params } ); const { objects } = assertListObjectsResponse(response); return objects.map((objectData) => { const createdAt = new Date(objectData.created_at); const expiresAt = objectData.expires_at ? new Date(objectData.expires_at) : void 0; return new BlobObject({ idOrUrl: objectData.id, size: objectData.size, createdAt, expiresAt }); }); } /** * Uploads an object to the storage. * * @param object - An object to upload * * @example * ```js * // Basic usage with absolute path * blob.objects.create({ * file: "path/to/file.jpeg", * name: "my_image" * }); * * // Advanced usage with Buffer * blob.objects.create({ * file: Buffer.from("content"), * name: "my_image", * mimeType: "image/jpeg" * }) * ``` */ async create(object) { const payload = createObjectPayloadSchema.parse(object); const file = await parsePathLike(payload.file); const type = payload.mimeType || object.mimeType; const formData = new FormData(); formData.append("file", new Blob([file], { type })); const { response } = await this.client.api.request( "objects", { method: "POST", body: formData, params: payload.params } ); const objectData = assertCreateObjectResponse(response); return new BlobObject({ idOrUrl: objectData.id, size: objectData.size }); } /** * Delete an object from the storage. * * @param object - An array of object IDs * * @example * ```js * blob.object.delete("userId/prefix/name1_xxx-xxx.mp4"); * ``` */ async delete(object) { const { status } = await this.client.api.request("objects", { method: "DELETE", body: { object } }); return status === "success"; } }; export { ObjectsManager }; //# sourceMappingURL=objects.mjs.map