UNPKG

tiny-server-essentials

Version:

A good utility toolkit to unify Express v5 and Socket.IO v4 into a seamless development experience with modular helpers, server wrappers, and WebSocket tools.

61 lines (60 loc) 1.35 kB
import { readFileSync } from 'fs'; import { readFile } from 'fs/promises'; /** * @type {Map<string, string>} */ const fileCache = new Map(); /** * Clears a cached HTML file. * * @param {string} filePath * @returns {boolean} */ export function clearHtmlFileCache(filePath) { return fileCache.delete(filePath); } /** * Checks if a file is cached. * * @param {string} filePath * @returns {boolean} */ export function hasHtmlFileCache(filePath) { return fileCache.has(filePath); } /** * Loads an HTML file asynchronously and caches it. * * @param {string} filePath * @returns {Promise<string>} */ export async function getHtmlFile(filePath) { let file = fileCache.get(filePath); if (typeof file !== 'string') { file = await readFile(filePath, 'utf-8'); fileCache.set(filePath, file); } return file; } /** * Loads an HTML file synchronously and caches it. * * @param {string} filePath * @returns {string} */ export function getHtmlFileSync(filePath) { let file = fileCache.get(filePath); if (typeof file !== 'string') { file = readFileSync(filePath, 'utf-8'); fileCache.set(filePath, file); } return file; } /** * Gets the list of cached file paths. * * @returns {string[]} */ export function getHtmlFileCacheKeys() { return [...fileCache.keys()]; }