import crypto from 'crypto' ; import { readFile , writeFile } from 'fs/promises' ; import { ensureDir } from 'fs-extra' ; import { resolve } from 'path' ; import type { Observable } from 'rxjs' ; import { defer , of } from 'rxjs' ; import { catchError , mapTo } from 'rxjs/operators' ; const CACHE_DIR = resolve ( __dirname , '../../build/.cache' ) ; param url returns export function get ( url ) { const id = crypto . createHash ( 'md5' ) . update ( url ) . digest ( 'hex' ) ; const filepath = resolve ( CACHE_DIR , id ) ; return defer ( ( ) => readFile ( filepath , 'utf-8' ) ) ; } param url param content returns export function save ( url , content ) { const id = crypto . createHash ( 'md5' ) . update ( url ) . digest ( 'hex' ) ; const filepath = resolve ( CACHE_DIR , id ) ; return defer ( async ( ) => { await ensureDir ( CACHE_DIR ) ; await writeFile ( filepath , content ) ; } ) . pipe ( mapTo ( content ) , catchError ( ( _ ) => of ( content ) ) ) ; }