📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
import { Injectable, Logger } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { Repository } from 'typeorm'
import * as bcrypt from 'bcrypt'
import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc'
import { redis } from '../common/constants'
import { Salt } from './entities/salt.entity'
dayjs.extend(utc)
const REDIS_SALT_DAILY = 'salt:daily'
const REDIS_SALT_MONTHLY = 'salt:monthly'
const REDIS_CACHE_TTL = 600 // 10 minutes in seconds
export type SaltRotationType = 'daily' | 'monthly'
const getEndOfPeriod = (rotation: SaltRotationType): dayjs.Dayjs => {
const now = dayjs.utc()
switch (rotation) {
case 'monthly':
return now.endOf('month')
case 'daily':
default:
return now.endOf('day')
}
}
const getRedisKeyForRotation = (rotation: SaltRotationType): string => {
return rotation === 'monthly' ? REDIS_SALT_MONTHLY : REDIS_SALT_DAILY
}
@Injectable()
export class SaltService {
private readonly logger = new Logger(SaltService.name)
constructor(
@InjectRepository(Salt)
private saltRepository: Repository<Salt>,
) {}
private async generateSalt(): Promise<string> {
return bcrypt.genSalt(10)
}
async getSaltForSession(): Promise<string> {
return this.getGlobalSalt('daily')
}
async getSaltForProfile(): Promise<string> {
return this.getGlobalSalt('monthly')
}
/** @deprecated Use getSaltForSession() instead */
async getSaltForProject(_pid: string): Promise<string> {
return this.getSaltForSession()
}
async getGlobalSalt(rotation: SaltRotationType = 'daily'): Promise<string> {
const redisKey = getRedisKeyForRotation(rotation)
// Check Redis cache first
const cachedSalt = await redis.get(redisKey)
if (cachedSalt) {
return cachedSalt
}
// Check MySQL
const now = dayjs.utc().toDate()
const existingSalt = await this.saltRepository.findOne({
where: { rotation },
})
if (existingSalt && existingSalt.expiresAt > now) {
// Cache in Redis with 10-minute TTL
await redis.set(redisKey, existingSalt.salt, 'EX', REDIS_CACHE_TTL)
return existingSalt.salt
}
// Generate new salt
const salt = await this.generateSalt()
const expiresAt = getEndOfPeriod(rotation).toDate()
// Save to MySQL (upsert)
await this.saltRepository.save({
rotation,
salt,
expiresAt,
})
// Cache in Redis with 10-minute TTL
await redis.set(redisKey, salt, 'EX', REDIS_CACHE_TTL)
this.logger.log(
`Generated new ${rotation} salt, expires at ${expiresAt.toISOString()}`,
)
return salt
}
async regenerateExpiredSalts(): Promise<void> {
const rotations: SaltRotationType[] = ['daily', 'monthly']
const now = dayjs.utc().toDate()
for (const rotation of rotations) {
const existingSalt = await this.saltRepository.findOne({
where: { rotation },
})
if (!existingSalt || existingSalt.expiresAt <= now) {
const salt = await this.generateSalt()
const expiresAt = getEndOfPeriod(rotation).toDate()
await this.saltRepository.save({
rotation,
salt,
expiresAt,
})
// Cache in Redis with 10-minute TTL
const redisKey = getRedisKeyForRotation(rotation)
await redis.set(redisKey, salt, 'EX', REDIS_CACHE_TTL)
this.logger.log(
`Regenerated ${rotation} salt, expires at ${expiresAt.toISOString()}`,
)
}
}
}
}