Repository URL to install this package:
|
Version:
1.2.6 ▾
|
/**
* @file @todo split
*/
import { Request, Response } from 'express'
import { remove } from 'fs-jetpack'
import { read, write, exists } from 'flipfile'
import { fromRequestToHash } from 'uxui-modules/oneRequest/deps/fromRequestToHash'
import { resolveToRoot } from '@skava/bs'
import { NextFunction } from 'express'
import {
fromStringToSanitizedFileName,
truncate,
} from '../deps/fromStringToSanitizedFileName'
// @todo should be in routeParams but nobody should edit this file and muck encoding
// unless they have a mac or are careful
const emoji = {
moneyWithWings: '💸',
moneyBags: '💰',
}
const cacheAddressMap = {
moneyWithWings: encodeURI(emoji.moneyWithWings),
moneyBags: encodeURI(emoji.moneyBags),
}
Object.freeze(emoji)
Object.freeze(cacheAddressMap)
/**
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
*/
function respondFromCache(
req: Request,
res: Response,
cachedContents?: string
) {
res.setHeader('cached', true)
res.status(200)
res.send(`<!DOCTYPE html>${cachedContents}`)
res.end()
}
// @lint - logs
// eslint-disable-next-line
function fromReqResToRespondFromCacheExistence(req: Request, res: Response) {
const hash = fromRequestToHash(res, '/react/') + req.oneUrl.full
const sanitizedHash = fromStringToSanitizedFileName(hash)
const cachedHashedPath = resolveToRoot(
process.env.CACHE_SSR_OUTPUT_DIRECTORY + sanitizedHash
)
const shortHashedPath = truncate(cachedHashedPath, 255)
req.cachedHashedPath = shortHashedPath
console.info('[SSR] SHOULD_CACHE_SSR for: ', shortHashedPath)
if (exists(shortHashedPath) === true) {
console.info('[SSR] hash exists')
const cachedContents = read(shortHashedPath)
respondFromCache(req, res, cachedContents)
return true
}
return false
}
/**
* @throws hopefully not, but safety first
*/
function deleteCache() {
try {
remove(process.env.CACHE_SSR_OUTPUT_DIRECTORY)
} catch (safetyFirstException) {
console.error(
'[ERROR_NOTIFY] [CACHE] remove exception!!! ',
safetyFirstException
)
}
}
/**
* @todo !!! add security to `knownParams` not just any random params to bypass cache
* @todo avoid writing too many caches
* @todo limit total cache size setting
* @todo @security jwt
*/
function respondCash(req: Request, res: Response) {
// res.status(205)
const html = `<iframe width="560" height="315" src="https://www.youtube.com/embed/VTpNFBp3vbg?autoplay=1" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>`
res.status(200).send(`<!DOCTYPE html>${html}`)
res.end()
}
/**
* @description handle cachings
*/
function cacheMiddleware(req: Request, res: Response, next: NextFunction) {
const url = req.oneUrl.full
const isCacheRequest =
url.includes('cacheclear') ||
url.includes(cacheAddressMap.moneyWithWings) ||
url.includes(emoji.moneyWithWings)
if (isCacheRequest) {
// .debug
console.log('[CACHE] isCacheRequest')
deleteCache()
respondCash(req, res)
return
}
const SHOULD_CACHE_SSR =
url.includes('cashmoney') ||
url.includes(emoji.moneyBags) ||
url.includes(cacheAddressMap.moneyBags) ||
process.env.SHOULD_CACHE_SSR
req.SHOULD_CACHE_SSR = SHOULD_CACHE_SSR
next()
}
/**
* @todo iterate through routes, render, output
*/
function cacheIfNeeded(req: Request, res: Response, html: string) {
if (req.SHOULD_CACHE_SSR) {
console.info('[SSR] cacheIfNeeded: caching')
write(req.cachedHashedPath, html)
}
}
/**
* @description iterate routes, call them, output cache...
*/
function precacheRoutes() {
console.log('@todo read description, @see precache')
}
/**
* @todo https://github.com/addyosmani/critical/blob/master/README.md
*/
function optimizeCache() {
//
}
export {
precacheRoutes,
respondFromCache,
cacheIfNeeded,
fromReqResToRespondFromCacheExistence,
cacheMiddleware,
}