import { Context , DatabaseSet } from "sqrl" ; import * as murmurJs from "murmurhash3.js" ; export interface RateLimitOptions { maxAmount : number ; refillTimeMs : number ; refillAmount : number ; take : number ; at : number ; strict : boolean ; } export interface RedisInterface { rateLimitFetch ( ctx , key , opt ) : Promise < number > ; sessionize ( ctx , key , opt ) : Promise < number > ; increment ( ctx , key , amount ? ) : Promise < number > ; get ( ctx , key ) : Promise < Buffer > ; del ( ctx , ... keys ) : Promise < number > ; set ( ctx , key , value , mode ? ) : Promise < boolean > ; getList ( ctx , key ) : Promise < string [ ] > ; listPush ( ctx , key , ... values ) : Promise < void > ; pfcount ( ctx , keys ) : Promise < number > ; pfadd ( ctx , key , values ) : Promise < void > ; expire ( ctx , key , seconds ) : Promise < void > ; mgetNumbers ( ctx , keys ) : Promise < number [ ] > ; } export function createRedisKey ( databaseSet , prefix , ... keys ) { const parts = [ databaseSet . getDatasetId ( ) , prefix , ... keys . map ( ( key ) => { if ( key instanceof Buffer ) { return key . toString ( "hex" ) ; } else { return key ; } } ) , ] ; const rv = murmurJs . x64 . hash128 ( JSON . stringify ( parts ) ) ; return Buffer . from ( rv ) ; }