UNPKG

@safaricom-mxl/nextjs-turbo-redis-cache

Version:

Next.js redis cache handler

36 lines (35 loc) 1.03 kB
// eslint-disable-next-line @typescript-eslint/no-explicit-any export function bufferReviver(_: string, value: any): any { if (value && typeof value === "object" && typeof value.$binary === "string") { try { // Check if it's valid base64 by attempting to decode it const base64String = value.$binary; // This regex checks for valid base64 format if (/^[A-Za-z0-9+/]*={0,2}$/.test(base64String)) { return Buffer.from(base64String, "base64"); } } catch { // If decoding fails, return the original object } } return value; } // eslint-disable-next-line @typescript-eslint/no-explicit-any export function bufferReplacer(_: string, value: any): any { if (Buffer.isBuffer(value)) { return { $binary: value.toString("base64"), }; } if ( value && typeof value === "object" && value?.type === "Buffer" && Array.isArray(value.data) ) { return { $binary: Buffer.from(value.data).toString("base64"), }; } return value; }