Skip to content

📈 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!

Replace some libs with native implementation

Blue Mouse7acbf4ce5f2e
diff --git a/backend/apps/cloud/src/analytics/analytics.service.ts b/backend/apps/cloud/src/analytics/analytics.service.ts
index 94c00d430..7e53b16f0 100644
--- a/backend/apps/cloud/src/analytics/analytics.service.ts
+++ b/backend/apps/cloud/src/analytics/analytics.service.ts
@@ -26,7 +26,6 @@ import utc from 'dayjs/plugin/utc'
import dayjsTimezone from 'dayjs/plugin/timezone'
import isSameOrBefore from 'dayjs/plugin/isSameOrBefore'
import ipRangeCheck from 'ip-range-check'
-import validator from 'validator'
import {
Injectable,
BadRequestException,
@@ -110,6 +109,15 @@ dayjs.extend(utc)
dayjs.extend(dayjsTimezone)
dayjs.extend(isSameOrBefore)
+const isValidLocale = (lc: string): boolean => {
+ try {
+ Intl.getCanonicalLocales(lc.replace(/_/g, '-'))
+ return true
+ } catch {
+ return false
+ }
+}
+
// 2 minutes
const LIVE_SESSION_THRESHOLD_SECONDS = 120
@@ -465,7 +473,7 @@ export class AnalyticsService {
// validate locale ('lc' param)
if (!_isEmpty(lc)) {
- if (validator.isLocale(lc)) {
+ if (isValidLocale(lc)) {
// uppercase the locale after '-' char, so for example both 'en-gb' and 'en-GB' in result will be 'en-GB'
const lcParted = _split(lc, '-')
diff --git a/backend/apps/cloud/src/common/utils.ts b/backend/apps/cloud/src/common/utils.ts
index b40b5903e..3d8c327e3 100644
--- a/backend/apps/cloud/src/common/utils.ts
+++ b/backend/apps/cloud/src/common/utils.ts
@@ -5,7 +5,7 @@ import net from 'net'
import { Reader, CityResponse } from 'maxmind'
import { HttpException } from '@nestjs/common'
import timezones from 'countries-and-timezones'
-import randomstring from 'randomstring'
+
import _sample from 'lodash/sample'
import _toNumber from 'lodash/toNumber'
import _replace from 'lodash/replace'
@@ -179,18 +179,18 @@ export const calculateRelativePercentage = (
return _round((1 - newVal / oldVal) * -100, round)
}
-export const generateRecoveryCode = () =>
- randomstring.generate({
- length: 30,
- charset: 'alphabetic',
- capitalization: 'uppercase',
- })
+const ALPHA_UPPER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
+
+export const generateRecoveryCode = () => generateRandomId(ALPHA_UPPER, 30)
export const millisecondsToSeconds = (milliseconds: number) =>
milliseconds / 1000
+const ALPHANUMERIC =
+ 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
+
export const generateRandomString = (length: number): string =>
- randomstring.generate(length)
+ generateRandomId(ALPHANUMERIC, length)
/**
* This is used to determine if the current node is the primary node.
diff --git a/backend/apps/cloud/src/main.ts b/backend/apps/cloud/src/main.ts
index cbdf29575..47d577cd6 100644
--- a/backend/apps/cloud/src/main.ts
+++ b/backend/apps/cloud/src/main.ts
@@ -3,7 +3,7 @@ import './instrument'
import { NestFactory } from '@nestjs/core'
import { ValidationPipe, VersioningType } from '@nestjs/common'
import cookieParser from 'cookie-parser'
-import bodyParser from 'body-parser'
+import express from 'express'
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger'
import { isDevelopment } from './common/constants'
@@ -60,10 +60,10 @@ async function bootstrap() {
next()
})
- app.use('/webhook', bodyParser.raw({ type: 'application/json' }))
+ app.use('/webhook', express.raw({ type: 'application/json' }))
- app.use('/webhook/sns', bodyParser.raw())
- app.use('/webhook/sns', bodyParser.text())
+ app.use('/webhook/sns', express.raw())
+ app.use('/webhook/sns', express.text())
app.enableShutdownHooks()
diff --git a/backend/apps/community/src/analytics/analytics.service.ts b/backend/apps/community/src/analytics/analytics.service.ts
index e66eb2939..606aee5b5 100644
--- a/backend/apps/community/src/analytics/analytics.service.ts
+++ b/backend/apps/community/src/analytics/analytics.service.ts
@@ -26,7 +26,6 @@ import utc from 'dayjs/plugin/utc'
import dayjsTimezone from 'dayjs/plugin/timezone'
import isSameOrBefore from 'dayjs/plugin/isSameOrBefore'
import ipRangeCheck from 'ip-range-check'
-import validator from 'validator'
import {
Injectable,
BadRequestException,
@@ -103,6 +102,15 @@ dayjs.extend(utc)
dayjs.extend(dayjsTimezone)
dayjs.extend(isSameOrBefore)
+const isValidLocale = (lc: string): boolean => {
+ try {
+ Intl.getCanonicalLocales(lc.replace(/_/g, '-'))
+ return true
+ } catch {
+ return false
+ }
+}
+
// 2 minutes
const LIVE_SESSION_THRESHOLD_SECONDS = 120
@@ -440,7 +448,7 @@ export class AnalyticsService {
// validate locale ('lc' param)
if (!_isEmpty(lc)) {
- if (validator.isLocale(lc)) {
+ if (isValidLocale(lc)) {
// uppercase the locale after '-' char, so for example both 'en-gb' and 'en-GB' in result will be 'en-GB'
const lcParted = _split(lc, '-')
diff --git a/backend/apps/community/src/common/utils.ts b/backend/apps/community/src/common/utils.ts
index 6c6c7b5bc..40ebba218 100644
--- a/backend/apps/community/src/common/utils.ts
+++ b/backend/apps/community/src/common/utils.ts
@@ -3,7 +3,7 @@ import path from 'path'
import fs from 'fs'
import crypto from 'crypto'
import net from 'net'
-import randomstring from 'randomstring'
+
import { CityResponse, Reader } from 'maxmind'
import timezones from 'countries-and-timezones'
import _join from 'lodash/join'
@@ -106,14 +106,15 @@ export const generateRandomId = (alphabet: string, size: number) => {
return id
}
+const ALPHANUMERIC =
+ 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
+
export const generateRandomString = (length: number): string =>
- randomstring.generate(length)
+ generateRandomId(ALPHANUMERIC, length)
-export const generateRecoveryCode = () =>
- randomstring.generate({
- length: 30,
- charset: 'alphabetic',
- })
+const ALPHA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
+
+export const generateRecoveryCode = () => generateRandomId(ALPHA, 30)
const RATE_LIMIT_REQUESTS_AMOUNT = 3
const RATE_LIMIT_TIMEOUT = 86400 // 24 hours
diff --git a/backend/package-lock.json b/backend/package-lock.json
index bff73d887..31865177f 100644
--- a/backend/package-lock.json
+++ b/backend/package-lock.json
@@ -17,7 +17,6 @@
"@nestjs/core": "^11.1.18",
"@nestjs/jwt": "^11.0.2",
"@nestjs/mapped-types": "^2.1.1",
- "@nestjs/microservices": "^11.1.18",
"@nestjs/passport": "^11.0.5",
"@nestjs/platform-express": "^11.1.18",
"@nestjs/platform-socket.io": "^11.1.18",
@@ -33,7 +32,6 @@
"@ua-parser-js/pro-business": "^2.0.9",
"ai": "^6.0.153",
"bcrypt": "^6.0.0",
- "body-parser": "^2.2.2",
"bullmq": "^5.73.1",
"class-transformer": "^0.5.1",
"class-validator": "^0.15.1",
@@ -66,7 +64,6 @@
"passport-headerapikey": "^1.2.2",
"passport-jwt": "^4.0.1",
"php-serialize": "^5.1.3",
- "randomstring": "^1.3.1",
"reflect-metadata": "^0.2.2",
"rimraf": "^6.1.3",
"rxjs": "^7.8.2",
@@ -75,14 +72,12 @@
"telegraf": "^4.16.3",
"tldts": "^7.0.28",
"typeorm": "^0.3.28",
- "validator": "^13.15.35",
"zod": "^4.3.6"
},
"devDependencies": {
"@nestjs/cli": "^11.0.18",
"@nestjs/schematics": "^11.0.10",
"@types/bcrypt": "^6.0.0",
- "@types/body-parser": "^1.19.6",
"@types/cookie-parser": "^1.4.10",
"@types/express": "^5.0.6",
"@types/ioredis": "^4.28.10",
@@ -91,7 +86,6 @@
"@types/node": "^25.5.2",
"@types/nodemailer": "^8.0.0",
"@types/passport-jwt": "^4.0.1",
- "@types/randomstring": "^1.3.0",
"knip": "^6.3.1",
"oxfmt": "^0.44.0",
"oxlint": "^1.59.0",
@@ -1808,64 +1802,6 @@
}
}
},
- "node_modules/@nestjs/microservices": {
- "version": "11.1.18",
- "resolved": "https://registry.npmjs.org/@nestjs/microservices/-/microservices-11.1.18.tgz",
- "integrity": "sha512-sodzBqqKAcOv5GctTbb0j3vzgx+X3/IxmJ8pBKL9hEmsarsYPkig6dcOcVFmM2Pknm+o6Cxaqv1YZZETISNi+w==",
- "license": "MIT",
- "dependencies": {
- "iterare": "1.2.1",
- "tslib": "2.8.1"
- },
- "funding": {
- "type": "opencollective",
- "url": "https://opencollective.com/nest"
- },
- "peerDependencies": {
- "@grpc/grpc-js": "*",
- "@nestjs/common": "^11.0.0",
- "@nestjs/core": "^11.0.0",
- "@nestjs/websockets": "^11.0.0",
- "amqp-connection-manager": "*",
- "amqplib": "*",
- "cache-manager": "*",
- "ioredis": "*",
- "kafkajs": "*",
- "mqtt": "*",
- "nats": "*",
- "reflect-metadata": "^0.1.12 || ^0.2.0",
- "rxjs": "^7.1.0"
- },
- "peerDependenciesMeta": {
- "@grpc/grpc-js": {
- "optional": true
- },
- "@nestjs/websockets": {
- "optional": true
- },
- "amqp-connection-manager": {
- "optional": true
- },
- "amqplib": {
- "optional": true
- },
- "cache-manager": {
- "optional": true
- },
- "ioredis": {
- "optional": true
- },
- "kafkajs": {
- "optional": true
- },
- "mqtt": {
- "optional": true
- },
- "nats": {
- "optional": true
- }
- }
- },
"node_modules/@nestjs/passport": {
"version": "11.0.5",
"resolved": "https://registry.npmjs.org/@nestjs/passport/-/passport-11.0.5.tgz",
@@ -4896,13 +4832,6 @@
"dev": true,
"license": "MIT"
},
- "node_modules/@types/randomstring": {
- "version": "1.3.0",
- "resolved": "https://registry.npmjs.org/@types/randomstring/-/randomstring-1.3.0.tgz",
- "integrity": "sha512-kCP61wludjY7oNUeFiMxfswHB3Wn/aC03Cu82oQsNTO6OCuhVN/rCbBs68Cq6Nkgjmp2Sh3Js6HearJPkk7KQA==",
- "dev": true,
- "license": "MIT"
- },
"node_modules/@types/range-parser": {
"version": "1.2.7",
"resolved": "https://registry.npmjs.org/@types/range-parser/-/range-parser-1.2.7.tgz",
@@ -12317,30 +12246,6 @@
],
"license": "MIT"
},
- "node_modules/randombytes": {
- "version": "2.1.0",
- "resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz",
- "integrity": "sha512-vYl3iOX+4CKUWuxGi9Ukhie6fsqXqS9FE2Zaic4tNFD2N2QQaXOMFbuKK4QmDHC0JO6B1Zp41J0LpT0oR68amQ==",
- "license": "MIT",
- "dependencies": {
- "safe-buffer": "^5.1.0"
- }
- },
- "node_modules/randomstring": {
- "version": "1.3.1",
- "resolved": "https://registry.npmjs.org/randomstring/-/randomstring-1.3.1.tgz",
- "integrity": "sha512-lgXZa80MUkjWdE7g2+PZ1xDLzc7/RokXVEQOv5NN2UOTChW1I8A9gha5a9xYBOqgaSoI6uJikDmCU8PyRdArRQ==",
- "license": "MIT",
- "dependencies": {
- "randombytes": "2.1.0"
- },
- "bin": {
- "randomstring": "bin/randomstring"
- },
- "engines": {
- "node": "*"
- }
- },
"node_modules/range-parser": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz",
diff --git a/backend/package.json b/backend/package.json
index c6258fa9b..f4de4618f 100644
--- a/backend/package.json
+++ b/backend/package.json
@@ -32,14 +32,12 @@
"@faker-js/faker": "^10.4.0",
"@googleapis/searchconsole": "^6.0.1",
"@nestjs-modules/mailer": "^2.3.4",
-
"@nestjs/bullmq": "^11.0.4",
"@nestjs/common": "^11.1.18",
"@nestjs/config": "^4.0.3",
"@nestjs/core": "^11.1.18",
"@nestjs/jwt": "^11.0.2",
"@nestjs/mapped-types": "^2.1.1",
- "@nestjs/microservices": "^11.1.18",
"@nestjs/passport": "^11.0.5",
"@nestjs/platform-express": "^11.1.18",
"@nestjs/platform-socket.io": "^11.1.18",
@@ -54,9 +52,7 @@
"@swetrix/node": "^3.1.1",
"@ua-parser-js/pro-business": "^2.0.9",
"ai": "^6.0.153",
-
"bcrypt": "^6.0.0",
- "body-parser": "^2.2.2",
"bullmq": "^5.73.1",
"class-transformer": "^0.5.1",
"class-validator": "^0.15.1",
@@ -89,7 +85,6 @@
"passport-headerapikey": "^1.2.2",
"passport-jwt": "^4.0.1",
"php-serialize": "^5.1.3",
- "randomstring": "^1.3.1",
"reflect-metadata": "^0.2.2",
"rimraf": "^6.1.3",
"rxjs": "^7.8.2",
@@ -98,14 +93,12 @@
"telegraf": "^4.16.3",
"tldts": "^7.0.28",
"typeorm": "^0.3.28",
- "validator": "^13.15.35",
"zod": "^4.3.6"
},
"devDependencies": {
"@nestjs/cli": "^11.0.18",
"@nestjs/schematics": "^11.0.10",
"@types/bcrypt": "^6.0.0",
- "@types/body-parser": "^1.19.6",
"@types/cookie-parser": "^1.4.10",
"@types/express": "^5.0.6",
"@types/ioredis": "^4.28.10",
@@ -114,7 +107,6 @@
"@types/node": "^25.5.2",
"@types/nodemailer": "^8.0.0",
"@types/passport-jwt": "^4.0.1",
- "@types/randomstring": "^1.3.0",
"knip": "^6.3.1",
"oxfmt": "^0.44.0",
"oxlint": "^1.59.0",