import { mkdirp , pathExists , readdir , readFile , remove , rename , stat , writeFile } from "fs-extra" ; import { homedir } from "os" ; import { join } from "path" ; import { Readable } from "stream" ; import { v4 as uuidv4 } from "uuid" ; interface Blob { } /**
 * A simple persistent key-value store. Used to implement  Limits . cache * for  throttle .
 *  remarks public export class PersistentCache { private initialized : Promise < void > ; private async initialize ( dir ) { if ( ! ( await pathExists ( dir ) ) ) { await mkdirp ( dir ) ; } } /**
     * The directory on disk where cached values are stored.
     */ readonly dir : string ; /**
     * Construct a new persistent cache, typically used with  Limits as
     * part of the arguments to  throttle .
     *  param dirRelativeToHomeDir param expiration constructor ( /**
     * The directory under the user's home directory that will be used to
     * store cached values. The directory will be created if it doesn't
     * exist.
     */ readonly dirRelativeToHomeDir , /**
     * The age (in ms) after which a cached entry is invalid. Default:
     * `24*3600*1000` (1 day).
     */ readonly expiration = 24 * 3600 * 1000 ) { this . dir = join ( homedir ( ) , dirRelativeToHomeDir ) ; this . initialized = this . initialize ( this . dir ) ; } /**
     * Retrieves the value previously set for the given key, or undefined if the
     * key is not found.
     */ async get ( key ) { await this . initialized ; const entry = join ( this . dir , key ) ; const statEntry = await stat ( entry ) . catch ( _ => { } ) ; if ( statEntry ) { if ( Date . now ( ) - statEntry . mtimeMs > this . expiration ) { return undefined ; } return readFile ( entry ) . catch ( _ => undefined ) ; } return undefined ; } returns async set ( key , value ) { await this . initialized ; const entry = join ( this . dir , key ) ; const tmpEntry = join ( this . dir , uuidv4 ( ) ) ; await writeFile ( tmpEntry , value , { mode : 0o600 , encoding : "binary" } ) ; await rename ( tmpEntry , entry ) ; } /**
     * Retrieve all keys stored in the cache, including expired entries.
     */ entries ( ) { return readdir ( this . dir ) ; } param leaveEmptyDir async clear ( { leaveEmptyDir = true } = { } ) { await this . initialized ; await remove ( this . dir ) ; if ( leaveEmptyDir ) { await mkdirp ( this . dir ) ; } } } const days = 24 * 3600 * 1000 ; export const caches = { awsPrices : new PersistentCache ( ".faastjs/aws/pricing" , 1 * days ) , googlePrices : new PersistentCache ( ".faastjs/google/pricing" , 1 * days ) , awsGc : new PersistentCache ( ".faastjs/aws/gc" , 7 * days ) } ;