UNPKG

@squarecloud/blob

Version:
63 lines (60 loc) 2.34 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/utils/object-url.ts var object_url_exports = {}; __export(object_url_exports, { parseObjectUrl: () => parseObjectUrl }); module.exports = __toCommonJS(object_url_exports); // 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 }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { parseObjectUrl }); //# sourceMappingURL=object-url.js.map