function wrapToPromise(value) { if (!value || typeof value.then !== "function") { return Promise.resolve(value); } return value; } function asyncCall(function_, ...arguments_) { try { return wrapToPromise(function_(...arguments_)); } catch (error) { return Promise.reject(error); } } function isPrimitive(value) { const type = typeof value; return value === null || type !== "object" && type !== "function"; } function isPureObject(value) { const proto = Object.getPrototypeOf(value); return !proto || proto.isPrototypeOf(Object); } function stringify(value) { if (isPrimitive(value)) { return String(value); } if (isPureObject(value) || Array.isArray(value)) { return JSON.stringify(value); } if (typeof value.toJSON === "function") { return stringify(value.toJSON()); } throw new Error("[unstorage] Cannot stringify value!"); } const BASE64_PREFIX = "base64:"; function serializeRaw(value) { if (typeof value === "string") { return value; } return BASE64_PREFIX + base64Encode(value); } function deserializeRaw(value) { if (typeof value !== "string") { return value; } if (!value.startsWith(BASE64_PREFIX)) { return value; } return base64Decode(value.slice(BASE64_PREFIX.length)); } function base64Decode(input) { if (globalThis.Buffer) { return Buffer.from(input, "base64"); } return Uint8Array.from( globalThis.atob(input), (c) => c.codePointAt(0) ); } function base64Encode(input) { if (globalThis.Buffer) { return Buffer.from(input).toString("base64"); } return globalThis.btoa(String.fromCodePoint(...input)); } const storageKeyProperties = [ "has", "hasItem", "get", "getItem", "getItemRaw", "set", "setItem", "setItemRaw", "del", "remove", "removeItem", "getMeta", "setMeta", "removeMeta", "getKeys", "clear", "mount", "unmount" ]; const nonItemKeyProperties = /* @__PURE__ */ new Set([ "getKeys", "clear", "mount", "unmount" ]); function prefixStorage(storage, base) { if (base?.includes("?")) { throw new Error(`[unstorage] Invalid base: ${JSON.stringify(base)}`); } base = normalizeBaseKey(base); if (!base) { return storage; } const itemKey = (key) => { if (!normalizeKey(key)) { throw new Error(`[unstorage] Invalid key: ${JSON.stringify(key)}`); } return base + key; }; const nsStorage = { ...storage }; for (const property of storageKeyProperties) { nsStorage[property] = nonItemKeyProperties.has(property) ? (key = "", ...args) => ( // @ts-ignore storage[property](base + key, ...args) ) : async (key = "", ...args) => ( // @ts-ignore storage[property](itemKey(key), ...args) ); } nsStorage.getKeys = (key = "", ...arguments_) => storage.getKeys(base + key, ...arguments_).then((keys) => keys.map((key2) => key2.slice(base.length))); nsStorage.keys = nsStorage.getKeys; nsStorage.getItems = async (items, commonOptions) => { const prefixedItems = items.map( (item) => typeof item === "string" ? itemKey(item) : { ...item, key: itemKey(item.key) } ); const results = await storage.getItems(prefixedItems, commonOptions); return results.map((entry) => ({ key: entry.key.slice(base.length), value: entry.value })); }; nsStorage.setItems = async (items, commonOptions) => { const prefixedItems = items.map((item) => ({ key: itemKey(item.key), value: item.value, options: item.options })); return storage.setItems(prefixedItems, commonOptions); }; return nsStorage; } function normalizeKey(key) { if (!key) { return ""; } return key.split("?")[0]?.replace(/[/\\]/g, ":").replace(/:+/g, ":").replace(/^:|:$/g, "") || ""; } function hasDotSegment(key) { return key.split(":").some((segment) => segment === "." || segment === ".."); } function joinKeys(...keys) { return normalizeKey(keys.join(":")); } function normalizeBaseKey(base) { base = normalizeKey(base); return base ? base + ":" : ""; } function filterKeyByDepth(key, depth) { if (depth === void 0) { return true; } let substrCount = 0; let index = key.indexOf(":"); while (index > -1) { substrCount++; index = key.indexOf(":", index + 1); } return substrCount <= depth; } function filterKeyByBase(key, base) { if (base) { return key.startsWith(base) && key[key.length - 1] !== "$"; } return key[key.length - 1] !== "$"; } export { normalizeKey as a, asyncCall as b, filterKeyByBase as c, stringify as d, deserializeRaw as e, filterKeyByDepth as f, hasDotSegment as h, joinKeys as j, normalizeBaseKey as n, prefixStorage as p, serializeRaw as s };