@scayle/storefront-nuxt
Version:
Nuxt integration for the SCAYLE Commerce Engine and Storefront API
40 lines (39 loc) • 1.99 kB
JavaScript
import { UnstorageCache } from "@scayle/storefront-core";
import { UnstorageSessionStore } from "@scayle/h3-session";
import { useStorage } from "nitropack/runtime/storage";
export const STORAGE_MOUNT_BASE = "storefront";
export const STORAGE_MOUNT_CACHE = `${STORAGE_MOUNT_BASE}-cache`;
export const STORAGE_MOUNT_SESSION = `${STORAGE_MOUNT_BASE}-session`;
function checkInMemoryProviderAndShowWarning(driver, mountpoint, log) {
if (!driver || driver.name !== "memory" && driver.name !== "compression" || driver.name === "compression" && driver.options.passthroughDriver.name !== "memory") {
return;
}
log.warn(
`Using in-memory cache for mountpoint ${mountpoint}. In-memory storage should not be used in production`
);
}
export function useCacheStorage(prefix, shopId, log) {
const storageBase = STORAGE_MOUNT_CACHE;
const mountPoint = `${storageBase}:${shopId}`;
const storage = useStorage(mountPoint);
const driver = storage.getMount(mountPoint)?.driver;
checkInMemoryProviderAndShowWarning(driver, mountPoint, log);
const driverName = driver.name === "compression" ? `'${driver?.options?.passthroughDriver?.name}' with compression` : `'${driver.name}'`;
log.debug(
`Using ${driverName} on mount '${mountPoint}' as cache storage driver`
);
return new UnstorageCache(storage, prefix);
}
export function useSessionStorage(sessionConfig, shopId, log) {
const storageBase = STORAGE_MOUNT_SESSION;
const mountPoint = `${storageBase}:${shopId}`;
const storage = useStorage(mountPoint);
const driver = storage.getMount(mountPoint)?.driver;
const ttl = sessionConfig?.maxAge ?? 60 * 60 * 24;
checkInMemoryProviderAndShowWarning(driver, mountPoint, log);
const driverName = driver.name === "compression" ? `'${driver?.options?.passthroughDriver?.name}' with compression` : `'${driver?.name}'`;
log.debug(
`Using ${driverName} on mount '${mountPoint}' as session storage driver`
);
return new UnstorageSessionStore(storage, { ttl });
}