📈 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
code clean-up
Blue Mousea8fe861ebcec
diff --git a/apps/production/src/analytics/analytics.service.ts b/apps/production/src/analytics/analytics.service.tsindex 313be460a..8e42f23d4 100644--- a/apps/production/src/analytics/analytics.service.ts+++ b/apps/production/src/analytics/analytics.service.ts@@ -144,7 +144,7 @@ const isValidTimezone = (timezone: string): boolean => {}}-export const isValidDate = (date: string, format = 'YYYY-MM-DD'): boolean => {+const isValidDate = (date: string, format = 'YYYY-MM-DD'): boolean => {if (_isEmpty(date)) {return false}diff --git a/apps/production/src/common/constants.ts b/apps/production/src/common/constants.tsindex 04de34c05..49462ac45 100644--- a/apps/production/src/common/constants.ts+++ b/apps/production/src/common/constants.ts@@ -47,8 +47,6 @@ const isNewRelicEnabled = Boolean(process.env.USE_NEW_RELIC)const isDevelopment = process.env.NODE_ENV === 'development'const PRODUCTION_ORIGIN = process.env.CLIENT_URL || 'https://swetrix.com'-const UUIDV5_NAMESPACE = '912c64c1-73fd-42b6-859f-785f839a9f68'-const { TWO_FACTOR_AUTHENTICATION_APP_NAME } = process.envconst ORIGINS_REGEX =@@ -117,7 +115,6 @@ export {REDIS_LOG_CUSTOM_CACHE_KEY,REDIS_SESSION_SALT_KEY,HEARTBEAT_SID_LIFE_TIME,- UUIDV5_NAMESPACE,REDIS_USERS_COUNT_KEY,REDIS_PROJECTS_COUNT_KEY,REDIS_PAGEVIEWS_COUNT_KEY,diff --git a/apps/selfhosted/src/analytics/analytics.controller.ts b/apps/selfhosted/src/analytics/analytics.controller.tsindex ea1d6ec4b..70352fc91 100644--- a/apps/selfhosted/src/analytics/analytics.controller.ts+++ b/apps/selfhosted/src/analytics/analytics.controller.ts@@ -179,7 +179,7 @@ const customLogDTO = (]}-export const getElValue = el => {+const getElValue = el => {if (el === undefined || el === null || el === 'NULL') return 'NULL'return mysql.escape(el)}diff --git a/apps/selfhosted/src/auth/decorators/index.ts b/apps/selfhosted/src/auth/decorators/index.tsindex 377aac6a6..ad7f2c823 100644--- a/apps/selfhosted/src/auth/decorators/index.ts+++ b/apps/selfhosted/src/auth/decorators/index.ts@@ -1,6 +1,5 @@export * from './current-user-id.decorator'export * from './public.decorator'export * from './current-user.decorator'-export * from './two-fa-not-required.decorator'export * from './roles.decorator'export * from './auth.decorator'diff --git a/apps/selfhosted/src/auth/decorators/two-fa-not-required.decorator.ts b/apps/selfhosted/src/auth/decorators/two-fa-not-required.decorator.tsdeleted file mode 100644index 7f0839d32..000000000--- a/apps/selfhosted/src/auth/decorators/two-fa-not-required.decorator.ts+++ /dev/null@@ -1,5 +0,0 @@-import { SetMetadata } from '@nestjs/common'--export const IS_TWO_FA_NOT_REQUIRED_KEY = 'isTwoFaNotRequired'-export const TwoFaNotRequired = () =>- SetMetadata(IS_TWO_FA_NOT_REQUIRED_KEY, true)diff --git a/apps/selfhosted/src/auth/dtos/change-password.dto.ts b/apps/selfhosted/src/auth/dtos/change-password.dto.tsdeleted file mode 100644index 2f8e41311..000000000--- a/apps/selfhosted/src/auth/dtos/change-password.dto.ts+++ /dev/null@@ -1,24 +0,0 @@-import { ApiProperty } from '@nestjs/swagger'-import { MaxLength, MinLength } from 'class-validator'--export class ChangePasswordDto {- @ApiProperty({- description: 'User password',- example: '%d7*c4W45p',- maxLength: 72,- minLength: 8,- })- @MaxLength(50, { message: 'Max length is $constraint1 characters' })- @MinLength(8, { message: 'Min length is $constraint1 characters' })- public readonly oldPassword: string-- @ApiProperty({- description: 'User password',- example: '%d7*c4W45p',- maxLength: 72,- minLength: 8,- })- @MaxLength(50, { message: 'Max length is $constraint1 characters' })- @MinLength(8, { message: 'Min length is $constraint1 characters' })- public readonly newPassword: string-}diff --git a/apps/selfhosted/src/auth/dtos/confirm-change-email.dto.ts b/apps/selfhosted/src/auth/dtos/confirm-change-email.dto.tsdeleted file mode 100644index 978d62050..000000000--- a/apps/selfhosted/src/auth/dtos/confirm-change-email.dto.ts+++ /dev/null@@ -1,11 +0,0 @@-import { ApiProperty } from '@nestjs/swagger'-import { IsUUID } from 'class-validator'--export class ConfirmChangeEmailDto {- @ApiProperty({- description: 'The token to confirm the change email',- example: 'a1b2c3d4-e5f6-g7h8-i9j0-k1l2m3n4o5p6',- })- @IsUUID('4', { message: 'Please enter the valid UUID.' })- public readonly token: string-}diff --git a/apps/selfhosted/src/auth/dtos/confirm-reset-password.dto.ts b/apps/selfhosted/src/auth/dtos/confirm-reset-password.dto.tsdeleted file mode 100644index 4d367f1f6..000000000--- a/apps/selfhosted/src/auth/dtos/confirm-reset-password.dto.ts+++ /dev/null@@ -1,11 +0,0 @@-import { ApiProperty } from '@nestjs/swagger'-import { IsUUID } from 'class-validator'--export class ConfirmResetPasswordDto {- @ApiProperty({- description: 'The token to confirm the reset password',- example: 'a1b2c3d4-e5f6-g7h8-i9j0-k1l2m3n4o5p6',- })- @IsUUID('4', { message: 'Please enter the valid UUID.' })- public readonly token: string-}diff --git a/apps/selfhosted/src/auth/dtos/index.ts b/apps/selfhosted/src/auth/dtos/index.tsindex 7afca67e8..84f02512a 100644--- a/apps/selfhosted/src/auth/dtos/index.ts+++ b/apps/selfhosted/src/auth/dtos/index.ts@@ -1,14 +1 @@-export * from './register.dto'export * from './login.dto'-export * from './verify-email.dto'-export * from './request-reset-password.dto'-export * from './confirm-reset-password.dto'-export * from './reset-password.dto'-export * from './change-password.dto'-export * from './request-change-email.dto'-export * from './confirm-change-email.dto'-export * from './process-sso-code.dto'-export * from './sso-generate.dto'-export * from './sso-get-jwt-by-hash.dto'-export * from './sso-link.dto'-export * from './sso-unlink.dto'diff --git a/apps/selfhosted/src/auth/dtos/process-sso-code.dto.ts b/apps/selfhosted/src/auth/dtos/process-sso-code.dto.tsdeleted file mode 100644index cc05d0935..000000000--- a/apps/selfhosted/src/auth/dtos/process-sso-code.dto.ts+++ /dev/null@@ -1,18 +0,0 @@-import { ApiProperty } from '@nestjs/swagger'-import { IsString } from 'class-validator'--export class ProcessSSOCodeDto {- @ApiProperty({- description: 'Google auth token',- example: '4/0AVHE...',- })- @IsString()- token: string-- @ApiProperty({- description: 'SSO session identifier',- example: '123e4567-e89b-12d3-a456-426614174000',- })- @IsString()- hash: string-}diff --git a/apps/selfhosted/src/auth/dtos/register.dto.ts b/apps/selfhosted/src/auth/dtos/register.dto.tsdeleted file mode 100644index 05c4d0e39..000000000--- a/apps/selfhosted/src/auth/dtos/register.dto.ts+++ /dev/null@@ -1,54 +0,0 @@-import { ApiProperty } from '@nestjs/swagger'-import {- IsEmail,- IsNotEmpty,- IsBoolean,- MaxLength,- MinLength,-} from 'class-validator'--export class RegisterRequestDto {- @ApiProperty({- description: 'User email',- example: '[email protected]',- maxLength: 254,- minLength: 6,- })- @IsEmail({}, { message: 'Please enter the valid email.' })- public readonly email: string-- @ApiProperty({- description: 'User password',- example: '%d7*c4W45p',- maxLength: 72,- minLength: 8,- })- @MaxLength(50, { message: 'Max length is $constraint1 characters' })- @MinLength(8, { message: 'Min length is $constraint1 characters' })- public readonly password: string-- @ApiProperty({- description: 'Check if password is leaked',- example: true,- })- @IsNotEmpty({ message: 'This field is required.' })- @IsBoolean({ message: 'Please enter the valid value (true or false).' })- public readonly checkIfLeaked: boolean-}--export class RegisterResponseDto {- @ApiProperty({- description: 'Access token (JWT)',- })- public readonly accessToken: string-- @ApiProperty({- description: 'Refresh token (JWT)',- })- public readonly refreshToken: string-- @ApiProperty({- description: 'User entity',- })- user: object-}diff --git a/apps/selfhosted/src/auth/dtos/request-change-email.dto.ts b/apps/selfhosted/src/auth/dtos/request-change-email.dto.tsdeleted file mode 100644index 6c1bde5db..000000000--- a/apps/selfhosted/src/auth/dtos/request-change-email.dto.ts+++ /dev/null@@ -1,23 +0,0 @@-import { ApiProperty } from '@nestjs/swagger'-import { IsEmail, MaxLength, MinLength } from 'class-validator'--export class RequestChangeEmailDto {- @ApiProperty({- description: 'User email',- example: '[email protected]',- maxLength: 254,- minLength: 6,- })- @IsEmail({}, { message: 'Please enter the valid email.' })- public readonly newEmail: string-- @ApiProperty({- description: 'User password',- example: '%d7*c4W45p',- maxLength: 72,- minLength: 8,- })- @MaxLength(50, { message: 'Max length is $constraint1 characters' })- @MinLength(8, { message: 'Min length is $constraint1 characters' })- public readonly password: string-}diff --git a/apps/selfhosted/src/auth/dtos/request-reset-password.dto.ts b/apps/selfhosted/src/auth/dtos/request-reset-password.dto.tsdeleted file mode 100644index a2a09a383..000000000--- a/apps/selfhosted/src/auth/dtos/request-reset-password.dto.ts+++ /dev/null@@ -1,13 +0,0 @@-import { ApiProperty } from '@nestjs/swagger'-import { IsEmail } from 'class-validator'--export class RequestResetPasswordDto {- @ApiProperty({- description: 'User email',- example: '[email protected]',- maxLength: 254,- minLength: 6,- })- @IsEmail({}, { message: 'Please enter the valid email.' })- public readonly email: string-}diff --git a/apps/selfhosted/src/auth/dtos/reset-password.dto.ts b/apps/selfhosted/src/auth/dtos/reset-password.dto.tsdeleted file mode 100644index 14df63e68..000000000--- a/apps/selfhosted/src/auth/dtos/reset-password.dto.ts+++ /dev/null@@ -1,14 +0,0 @@-import { ApiProperty } from '@nestjs/swagger'-import { MaxLength, MinLength } from 'class-validator'--export class ResetPasswordDto {- @ApiProperty({- description: 'User password',- example: '%d7*c4W45p',- maxLength: 72,- minLength: 8,- })- @MaxLength(50, { message: 'Max length is $constraint1 characters' })- @MinLength(8, { message: 'Min length is $constraint1 characters' })- public readonly newPassword: string-}diff --git a/apps/selfhosted/src/auth/dtos/sso-generate.dto.ts b/apps/selfhosted/src/auth/dtos/sso-generate.dto.tsdeleted file mode 100644index c0e43ab99..000000000--- a/apps/selfhosted/src/auth/dtos/sso-generate.dto.ts+++ /dev/null@@ -1,16 +0,0 @@-import { ApiProperty } from '@nestjs/swagger'-import { IsEnum } from 'class-validator'--export enum SSOProviders {- GOOGLE = 'google',- GITHUB = 'github',-}--export class SSOGenerateDto {- @ApiProperty({- description: 'SSO provider name',- enum: SSOProviders,- })- @IsEnum(SSOProviders)- provider: SSOProviders-}diff --git a/apps/selfhosted/src/auth/dtos/sso-get-jwt-by-hash.dto.ts b/apps/selfhosted/src/auth/dtos/sso-get-jwt-by-hash.dto.tsdeleted file mode 100644index cc58875d2..000000000--- a/apps/selfhosted/src/auth/dtos/sso-get-jwt-by-hash.dto.ts+++ /dev/null@@ -1,20 +0,0 @@-import { ApiProperty } from '@nestjs/swagger'-import { IsEnum, IsString } from 'class-validator'--import { SSOProviders } from './sso-generate.dto'--export class SSOGetJWTByHashDto {- @ApiProperty({- description: 'SSO session identifier',- example: '123e4567-e89b-12d3-a456-426614174000',- })- @IsString()- hash: string-- @ApiProperty({- description: 'SSO provider name',- enum: SSOProviders,- })- @IsEnum(SSOProviders)- provider: SSOProviders-}diff --git a/apps/selfhosted/src/auth/dtos/sso-link.dto.ts b/apps/selfhosted/src/auth/dtos/sso-link.dto.tsdeleted file mode 100644index 5e244573e..000000000--- a/apps/selfhosted/src/auth/dtos/sso-link.dto.ts+++ /dev/null@@ -1,20 +0,0 @@-import { ApiProperty } from '@nestjs/swagger'-import { IsEnum, IsString } from 'class-validator'--import { SSOProviders } from './sso-generate.dto'--export class SSOLinkDto {- @ApiProperty({- description: 'SSO session identifier',- example: '123e4567-e89b-12d3-a456-426614174000',- })- @IsString()- hash: string-- @ApiProperty({- description: 'SSO provider name',- enum: SSOProviders,- })- @IsEnum(SSOProviders)- provider: SSOProviders-}diff --git a/apps/selfhosted/src/auth/dtos/sso-unlink.dto.ts b/apps/selfhosted/src/auth/dtos/sso-unlink.dto.tsdeleted file mode 100644index f45697a9b..000000000--- a/apps/selfhosted/src/auth/dtos/sso-unlink.dto.ts+++ /dev/null@@ -1,13 +0,0 @@-import { ApiProperty } from '@nestjs/swagger'-import { IsEnum } from 'class-validator'--import { SSOProviders } from './sso-generate.dto'--export class SSOUnlinkDto {- @ApiProperty({- description: 'SSO provider name',- enum: SSOProviders,- })- @IsEnum(SSOProviders)- provider: SSOProviders-}diff --git a/apps/selfhosted/src/auth/dtos/verify-email.dto.ts b/apps/selfhosted/src/auth/dtos/verify-email.dto.tsdeleted file mode 100644index e03420979..000000000--- a/apps/selfhosted/src/auth/dtos/verify-email.dto.ts+++ /dev/null@@ -1,11 +0,0 @@-import { ApiProperty } from '@nestjs/swagger'-import { IsUUID } from 'class-validator'--export class VerifyEmailDto {- @ApiProperty({- description: 'The token to verify the email',- example: 'a1b2c3d4-e5f6-g7h8-i9j0-k1l2m3n4o5p6',- })- @IsUUID('4', { message: 'Please enter the valid UUID.' })- public readonly token: string-}diff --git a/apps/selfhosted/src/auth/guards/api-key.guard.ts b/apps/selfhosted/src/auth/guards/api-key.guard.tsdeleted file mode 100644index 43e58ce2d..000000000--- a/apps/selfhosted/src/auth/guards/api-key.guard.ts+++ /dev/null@@ -1,5 +0,0 @@-import { Injectable } from '@nestjs/common'-import { AuthGuard } from '@nestjs/passport'--@Injectable()-export class ApiKeyGuard extends AuthGuard('api-key') {}diff --git a/apps/selfhosted/src/common/constants.ts b/apps/selfhosted/src/common/constants.tsindex b0b7607cd..8a89bc674 100644--- a/apps/selfhosted/src/common/constants.ts+++ b/apps/selfhosted/src/common/constants.ts@@ -45,9 +45,7 @@ const clickhouse = new ClickHouse({})const { JWT_ACCESS_TOKEN_SECRET } = process.env-const isNewRelicEnabled = Boolean(process.env.USE_NEW_RELIC)const isDevelopment = process.env.NODE_ENV === 'development'-const PRODUCTION_ORIGIN = process.env.CLIENT_URL || 'https://swetrix.com'const SELFHOSTED_EMAIL = process.env.EMAILconst SELFHOSTED_PASSWORD = process.env.PASSWORD@@ -56,8 +54,6 @@ const DEFAULT_SELFHOSTED_UUID = 'deadbeef-dead-beef-dead-beefdeadbeef'const SELFHOSTED_UUID = getSelfhostedUUID()-const { TWO_FACTOR_AUTHENTICATION_APP_NAME } = process.env-const ORIGINS_REGEX =/^(?=.{1,255}$)[0-9A-Za-z:](?:(?:[0-9A-Za-z:]|-){0,61}[0-9A-Za-z])?(?:\.[0-9A-Za-z:](?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?)*\.?$/const IP_REGEX =@@ -67,8 +63,6 @@ const isValidPID = (pid: string) => PID_REGEX.test(pid)// redis keysconst getRedisProjectKey = (pid: string) => `pid_${pid}`-const getRedisUserCountKey = (uid: string) => `user_c_${uid}`-const getRedisCaptchaKey = (token: string) => `captcha_${hash(token)}`const REDIS_LOG_DATA_CACHE_KEY = 'log_cache'const REDIS_LOG_CAPTCHA_CACHE_KEY = 'log:captcha'@@ -79,36 +73,16 @@ const REDIS_USERS_COUNT_KEY = 'stats:users_count'const REDIS_PROJECTS_COUNT_KEY = 'stats:projects_count'const REDIS_PAGEVIEWS_COUNT_KEY = 'stats:pageviews'const REDIS_PERFORMANCE_COUNT_KEY = 'stats:performance'-const REDIS_SSO_UUID = 'sso:uuid'--// Captcha service-const { CAPTCHA_SALT } = process.env-const { CAPTCHA_ENCRYPTION_KEY } = process.env// 3600 sec -> 1 hourconst redisProjectCacheTimeout = 3600-// 15 minutes-const redisProjectCountCacheTimeout = 900-// 30 minues -> the amount of time analytics requests within one session are counted as non-uniqueconst UNIQUE_SESSION_LIFE_TIME = 1800// 35 secondsconst HEARTBEAT_SID_LIFE_TIME = 35-// how often can user request a fresh GDPR export of their data; in days.-const GDPR_EXPORT_TIMEFRAME = 14--// send email warning when 85% of events in tier are used-const SEND_WARNING_AT_PERC = 85--const PROJECT_INVITE_EXPIRE = 48--const CAPTCHA_COOKIE_KEY = 'swetrix-captcha-token'-const CAPTCHA_TOKEN_LIFETIME = 300 // seconds (5 minutes).-const CAPTCHA_SECRET_KEY_LENGTH = 50-export {clickhouse,redis,@@ -118,9 +92,6 @@ export {UNIQUE_SESSION_LIFE_TIME,REDIS_LOG_DATA_CACHE_KEY,REDIS_LOG_CAPTCHA_CACHE_KEY,- GDPR_EXPORT_TIMEFRAME,- getRedisUserCountKey,- redisProjectCountCacheTimeout,REDIS_LOG_CUSTOM_CACHE_KEY,REDIS_SESSION_SALT_KEY,HEARTBEAT_SID_LIFE_TIME,@@ -131,23 +102,11 @@ export {REDIS_USERS_COUNT_KEY,REDIS_PROJECTS_COUNT_KEY,REDIS_PAGEVIEWS_COUNT_KEY,- SEND_WARNING_AT_PERC,- PROJECT_INVITE_EXPIRE,- TWO_FACTOR_AUTHENTICATION_APP_NAME,IP_REGEX,- isNewRelicEnabled,ORIGINS_REGEX,REDIS_LOG_PERF_CACHE_KEY,REDIS_PERFORMANCE_COUNT_KEY,- CAPTCHA_SALT,- CAPTCHA_ENCRYPTION_KEY,isDevelopment,- getRedisCaptchaKey,- CAPTCHA_COOKIE_KEY,- CAPTCHA_TOKEN_LIFETIME,- CAPTCHA_SECRET_KEY_LENGTH,- PRODUCTION_ORIGIN,DEFAULT_SELFHOSTED_UUID,- REDIS_SSO_UUID,JWT_ACCESS_TOKEN_SECRET,}diff --git a/apps/selfhosted/src/common/utils.ts b/apps/selfhosted/src/common/utils.tsindex 7ce4eda7d..215f9b3b2 100644--- a/apps/selfhosted/src/common/utils.ts+++ b/apps/selfhosted/src/common/utils.ts@@ -62,21 +62,6 @@ const checkRateLimit = async (await redis.set(rlHash, 1 + rlCount, 'EX', reqTimeout)}-const RATE_LIMIT_FOR_API_KEY_TIMEOUT = 60 * 60 // 1 hour-export const checkRateLimitForApiKey = async (- apiKey: string,- reqAmount: number,-): Promise<boolean> => {- const rlHash = getRateLimitHash(apiKey)- const rlCount: number = _toNumber(await redis.get(rlHash)) || 0-- if (rlCount >= reqAmount) {- throw new HttpException('Too many requests, please try again later', 429)- }- await redis.set(rlHash, 1 + rlCount, 'EX', RATE_LIMIT_FOR_API_KEY_TIMEOUT)- return true-}-const getProjectsClickhouse = async (id = null) => {if (!id) {const query = 'SELECT * FROM project;'@@ -203,18 +188,8 @@ const deleteRefreshTokenClickhouse = async (return clickhouse.query(query, paramsData).toPromise()}-const generateRecoveryCode = () =>- randomstring.generate({- length: 30,- charset: 'alphabetic',- capitalization: 'uppercase',- })-const millisecondsToSeconds = (milliseconds: number) => milliseconds / 1000-const generateRandomString = (length: number): string =>- randomstring.generate(length)-const getSelfhostedUUID = (): string => {try {return uuidv5(SELFHOSTED_EMAIL, UUIDV5_NAMESPACE)@@ -229,10 +204,8 @@ export {getProjectsClickhouse,updateProjectClickhouse,deleteProjectClickhouse,- generateRecoveryCode,calculateRelativePercentage,millisecondsToSeconds,- generateRandomString,getSelfhostedUUID,saveRefreshTokenClickhouse,findRefreshTokenClickhouse,diff --git a/apps/selfhosted/src/user/entities/user.entity.ts b/apps/selfhosted/src/user/entities/user.entity.tsindex 217cb53b2..01daf3b8c 100644--- a/apps/selfhosted/src/user/entities/user.entity.ts+++ b/apps/selfhosted/src/user/entities/user.entity.ts@@ -10,12 +10,8 @@ export enum TimeFormat {'24-hour' = '24-hour',}-export const MAX_EMAIL_REQUESTS = 4 // 1 confirmation email on sign up + 3 additional ones-export const DEFAULT_TIMEZONE = 'Etc/GMT'-export const TRIAL_DURATION = 14 // days-export interface SelfhostedUser {id: stringemail: string