Spaces:
Paused
Paused
Download src/middleware/cacheBuster.js from Haay/haasillytavern: direct link, hf CLI and curl.
- Browser
- Download file 855 Bytes
-
https://huggingface.co/spaces/Haay/haasillytavern/resolve/main/src/middleware/cacheBuster.js
- Command line
-
hf download hf://spaces/Haay/haasillytavern/src/middleware/cacheBuster.js
-
curl -L -o cacheBuster.js https://huggingface.co/spaces/Haay/haasillytavern/resolve/main/src/middleware/cacheBuster.js
855 Bytes
| import crypto from 'node:crypto'; | |
| import { DEFAULT_USER } from '../constants.js'; | |
| /** | |
| * Middleware to bust the browser cache for the current user. | |
| * @returns {import('express').RequestHandler} | |
| */ | |
| export default function getCacheBusterMiddleware() { | |
| /** | |
| * @type {Set<string>} Handles/User-Agents that have already been busted. | |
| */ | |
| const keys = new Set(); | |
| return (request, response, next) => { | |
| const handle = request.user?.profile?.handle || DEFAULT_USER.handle; | |
| const userAgent = request.headers['user-agent'] || ''; | |
| const hash = crypto.createHash('sha256').update(userAgent).digest('hex'); | |
| const key = `${handle}-${hash}`; | |
| if (keys.has(key)) { | |
| return next(); | |
| } | |
| keys.add(key); | |
| response.setHeader('Clear-Site-Data', '"cache"'); | |
| next(); | |
| }; | |
| } | |