@squarecloud/blob
Version:
Official Square Cloud Blob SDK for NodeJS
1 lines • 8.88 kB
Source Map (JSON)
{"version":3,"sources":["../../src/utils/mimetype/mimetypes.ts","../../src/utils/mimetype/index.ts","../../src/structures/error.ts","../../src/utils/object-url.ts","../../src/structures/object.ts"],"sourcesContent":["export const mimeTypesWithExtension = {\n\t\"video/mp4\": [\"mp4\"],\n\t\"video/mpeg\": [\"mpeg\"],\n\t\"video/webm\": [\"webm\"],\n\t\"video/x-flv\": [\"flv\"],\n\t\"video/x-m4v\": [\"m4v\"],\n\t\"image/jpeg\": [\"jpg\", \"jpeg\"],\n\t\"image/png\": [\"png\"],\n\t\"image/apng\": [\"apng\"],\n\t\"image/tiff\": [\"tiff\"],\n\t\"image/gif\": [\"gif\"],\n\t\"image/webp\": [\"webp\"],\n\t\"image/bmp\": [\"bmp\"],\n\t\"image/svg+xml\": [\"svg\"],\n\t\"image/x-icon\": [\"ico\"],\n\t\"image/ico\": [\"ico\"],\n\t\"image/cur\": [\"cur\"],\n\t\"image/heic\": [\"heic\"],\n\t\"image/heif\": [\"heif\"],\n\t\"audio/wav\": [\"wav\"],\n\t\"audio/ogg\": [\"ogg\"],\n\t\"audio/opus\": [\"opus\"],\n\t\"audio/mp4\": [\"mp4\"],\n\t\"audio/mpeg\": [\"mp3\"],\n\t\"audio/aac\": [\"aac\"],\n\t\"text/plain\": [\"txt\"],\n\t\"text/html\": [\"html\"],\n\t\"text/css\": [\"css\"],\n\t\"text/csv\": [\"csv\"],\n\t\"text/x-sql\": [\"sql\"],\n\t\"application/xml\": [\"xml\"],\n\t\"application/sql\": [\"sql\"],\n\t\"application/x-sql\": [\"sql\"],\n\t\"application/x-sqlite3\": [\"sqlite3\"],\n\t\"application/x-pkcs12\": [\"pfx\"],\n\t\"application/pdf\": [\"pdf\"],\n\t\"application/json\": [\"json\"],\n\t\"application/javascript\": [\"js\"],\n};\n\nexport const mimeTypes = Object.keys(mimeTypesWithExtension) as [\n\tMimeType,\n\t...MimeType[],\n];\n\nexport type MimeType = keyof typeof mimeTypesWithExtension;\n","import { type MimeType, mimeTypes, mimeTypesWithExtension } from \"./mimetypes\";\n\n// biome-ignore lint/complexity/noStaticOnlyClass: organization\nexport class MimeTypeUtil {\n\t/** Supported mime types with their extensions */\n\tstatic mimeTypesWithExtension = mimeTypesWithExtension;\n\t/** All supported mime types */\n\tstatic mimeTypes = mimeTypes;\n\n\t/**\n\t * Returns the corresponding MIME type for a given file extension.\n\t *\n\t * @param extension - The file extension to search for.\n\t * @return The MIME type associated with the extension, or \"text/plain\" if not found.\n\t *\n\t * @example\n\t * ```js\n\t * \tMimeTypeUtil.fromExtension(\"jpeg\") // \"image/jpeg\" | Supported\n\t * \tMimeTypeUtil.fromExtension(\"json\") // \"application/json\" | Supported\n\t * \tMimeTypeUtil.fromExtension(\"potato\") // \"text/plain\" | Unsupported, defaults to text/plain\n\t * ```\n\t */\n\tstatic fromExtension(extension: string): MimeType {\n\t\tconst entries = Object.entries(mimeTypesWithExtension);\n\t\tconst mimeType = entries.find(([, extensions]) =>\n\t\t\textensions.includes(extension),\n\t\t)?.[0];\n\n\t\treturn (mimeType as MimeType) || \"text/plain\";\n\t}\n}\n\nexport * from \"./enum\";\nexport { MimeType } from \"./mimetypes\";\n","export class SquareCloudBlobError extends Error {\n\tconstructor(code: string, message?: string, cause?: unknown) {\n\t\tsuper(message, { cause });\n\n\t\tthis.name = SquareCloudBlobError.name;\n\t\tthis.message = this.getMessage(code);\n\t}\n\n\tprivate getMessage(rawCode: string) {\n\t\tconst code = rawCode\n\t\t\t.replaceAll(\"_\", \" \")\n\t\t\t.toLowerCase()\n\t\t\t.replace(/(^|\\s)\\S/g, (L) => L.toUpperCase());\n\t\tconst message = this.message ? `: ${this.message}` : \"\";\n\n\t\treturn `${code}${message}`;\n\t}\n}\n\nexport class SquareCloudValidationError extends SquareCloudBlobError {\n\tconstructor(...args: ConstructorParameters<typeof SquareCloudBlobError>) {\n\t\tsuper(...args);\n\t\tthis.name = SquareCloudValidationError.name;\n\t}\n}\n","import { SquareCloudBlobError } from \"../structures/error\";\n\nconst objectUrlRegex =\n\t/^(?<url>https:\\/\\/public-blob\\.squarecloud\\.dev)?\\/?(?<userId>\\d+\\/)(?<prefix>[\\w\\d-_]+\\/)?(?<name>[\\w\\d_]+)-(?<hash>[\\w\\d]+)\\.(?<extension>\\w+)$/;\n\n/**\n * Parses the object URL to extract id, userId, prefix, name, hash and extension.\n *\n * @param url - The object URL to parse.\n */\nexport function parseObjectUrl(url: string) {\n\tconst match = url.match(objectUrlRegex);\n\n\tif (!match?.groups) {\n\t\tthrow new SquareCloudBlobError(\"Invalid object URL\");\n\t}\n\n\tconst payload = {\n\t\tuserId: match.groups.userId.replace(\"/\", \"\"),\n\t\tprefix: match.groups.prefix?.replace(\"/\", \"\"),\n\t\tname: match.groups.name,\n\t\thash: match.groups.hash,\n\t\textension: match.groups.extension,\n\t};\n\n\treturn {\n\t\tid: `${payload.userId}/${payload.prefix ? `${payload.prefix}/` : \"\"}${\n\t\t\tpayload.name\n\t\t}-${payload.hash}.${payload.extension}`,\n\t\t...payload,\n\t};\n}\n","import type { BlobObjectData } from \"../types/object\";\nimport { type MimeType, MimeTypeUtil } from \"../utils/mimetype\";\nimport { parseObjectUrl } from \"../utils/object-url\";\n\nexport class BlobObject {\n\t/** The id of the object */\n\tid: string;\n\t/** The url to view or download the object */\n\turl: string;\n\t/** The name of the object */\n\tname: string;\n\t/** The prefix of the object (Optional) */\n\tprefix?: string;\n\t/** The hash of the object */\n\thash: string;\n\t/** The id of the user who created the object */\n\tuserId: string;\n\t/** The file extension of the object */\n\textension: string;\n\t/** The MIME type of the object */\n\tmimeType: MimeType;\n\t/** The size of the object in bytes */\n\tsize: number;\n\t/** The expiration date of the object (Only available using `objects.list`) */\n\texpiresAt?: Date;\n\t/** The creation date of the object (Only available using `objects.list`) */\n\tcreatedAt?: Date;\n\n\tconstructor(data: BlobObjectData) {\n\t\tconst { id, name, prefix, hash, userId, extension } = parseObjectUrl(\n\t\t\tdata.idOrUrl,\n\t\t);\n\n\t\tthis.id = id;\n\t\tthis.url = `https://public-blob.squarecloud.dev/${id}`;\n\t\tthis.name = name;\n\t\tthis.prefix = prefix;\n\t\tthis.hash = hash;\n\t\tthis.userId = userId;\n\t\tthis.extension = extension;\n\t\tthis.mimeType = MimeTypeUtil.fromExtension(extension);\n\t\tthis.size = data.size;\n\t\tthis.expiresAt = data.expiresAt;\n\t\tthis.createdAt = data.createdAt;\n\t}\n}\n"],"mappings":";;;;;;;;AAAO,IAAM,yBAAyB;AAAA,EACrC,aAAa,CAAC,KAAK;AAAA,EACnB,cAAc,CAAC,MAAM;AAAA,EACrB,cAAc,CAAC,MAAM;AAAA,EACrB,eAAe,CAAC,KAAK;AAAA,EACrB,eAAe,CAAC,KAAK;AAAA,EACrB,cAAc,CAAC,OAAO,MAAM;AAAA,EAC5B,aAAa,CAAC,KAAK;AAAA,EACnB,cAAc,CAAC,MAAM;AAAA,EACrB,cAAc,CAAC,MAAM;AAAA,EACrB,aAAa,CAAC,KAAK;AAAA,EACnB,cAAc,CAAC,MAAM;AAAA,EACrB,aAAa,CAAC,KAAK;AAAA,EACnB,iBAAiB,CAAC,KAAK;AAAA,EACvB,gBAAgB,CAAC,KAAK;AAAA,EACtB,aAAa,CAAC,KAAK;AAAA,EACnB,aAAa,CAAC,KAAK;AAAA,EACnB,cAAc,CAAC,MAAM;AAAA,EACrB,cAAc,CAAC,MAAM;AAAA,EACrB,aAAa,CAAC,KAAK;AAAA,EACnB,aAAa,CAAC,KAAK;AAAA,EACnB,cAAc,CAAC,MAAM;AAAA,EACrB,aAAa,CAAC,KAAK;AAAA,EACnB,cAAc,CAAC,KAAK;AAAA,EACpB,aAAa,CAAC,KAAK;AAAA,EACnB,cAAc,CAAC,KAAK;AAAA,EACpB,aAAa,CAAC,MAAM;AAAA,EACpB,YAAY,CAAC,KAAK;AAAA,EAClB,YAAY,CAAC,KAAK;AAAA,EAClB,cAAc,CAAC,KAAK;AAAA,E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