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!

Get rid of nanoid in favour of a custom implementation

Blue Moused325356dadb6
diff --git a/backend/apps/cloud/src/common/utils.ts b/backend/apps/cloud/src/common/utils.ts
index ee48160a5..ddff2ca8d 100644
--- a/backend/apps/cloud/src/common/utils.ts
+++ b/backend/apps/cloud/src/common/utils.ts
@@ -1,6 +1,7 @@
import { xxh3 } from '@node-rs/xxhash'
import path from 'path'
import fs from 'fs'
+import crypto from 'crypto'
import { Reader, CityResponse } from 'maxmind'
import { HttpException } from '@nestjs/common'
import timezones from 'countries-and-timezones'
@@ -22,6 +23,17 @@ export const hash = (content: string): string => {
return xxh3.xxh128(content).toString(16)
}
+export const generateRandomId = (alphabet: string, size: number) => {
+ const bytes = crypto.randomBytes(size)
+ let id = ''
+
+ for (let i = 0; i < size; i++) {
+ id += alphabet[bytes[i] % alphabet.length]
+ }
+
+ return id
+}
+
const marketingTips = {
en: [
'Blog frequently and consistently to build search engine presence.\nTo get website visitors, you need lots and lots of content. Your prospects are using Google every day to search for what they want. One of the most reliable ways to show up in Google results is to blog about lots of topics your customers care about.',
diff --git a/backend/apps/cloud/src/project/project.controller.ts b/backend/apps/cloud/src/project/project.controller.ts
index ab4f1ed04..8bfcaac85 100644
--- a/backend/apps/cloud/src/project/project.controller.ts
+++ b/backend/apps/cloud/src/project/project.controller.ts
@@ -53,7 +53,8 @@ import {
ProjectService,
processProjectUser,
deleteProjectRedis,
- generateProjectId,
+ LEGAL_PID_CHARACTERS,
+ PID_LENGTH,
} from './project.service'
import { UserType, PlanCode } from '../user/entities/user.entity'
import { ActionTokenType } from '../action-tokens/action-token.entity'
@@ -77,7 +78,7 @@ import {
MAX_FUNNELS,
} from '../common/constants'
import { clickhouse } from '../common/integrations/clickhouse'
-import { generateRandomString } from '../common/utils'
+import { generateRandomId, generateRandomString } from '../common/utils'
import {
AddSubscriberParamsDto,
AddSubscriberBodyDto,
@@ -413,10 +414,10 @@ export class ProjectController {
this.projectService.validateProject(projectDTO as ProjectDTO, true)
- let pid = generateProjectId()
+ let pid = generateRandomId(LEGAL_PID_CHARACTERS, PID_LENGTH)
while (!(await this.projectService.isPIDUnique(pid))) {
- pid = generateProjectId()
+ pid = generateRandomId(LEGAL_PID_CHARACTERS, PID_LENGTH)
}
try {
diff --git a/backend/apps/cloud/src/project/project.service.ts b/backend/apps/cloud/src/project/project.service.ts
index 6bfabc515..c21073e73 100644
--- a/backend/apps/cloud/src/project/project.service.ts
+++ b/backend/apps/cloud/src/project/project.service.ts
@@ -17,7 +17,6 @@ import {
Brackets,
FindOptionsWhere,
} from 'typeorm'
-import { customAlphabet } from 'nanoid'
import handlebars from 'handlebars'
import puppeteer from 'puppeteer'
import CryptoJS from 'crypto-js'
@@ -86,24 +85,9 @@ import { OrganisationRole } from '../organisation/entity/organisation-member.ent
dayjs.extend(utc)
// A list of characters that can be used in a Project ID
-const LEGAL_PID_CHARACTERS =
+export const LEGAL_PID_CHARACTERS =
'1234567890QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm'
-export const generateProjectId = customAlphabet(LEGAL_PID_CHARACTERS, 12)
-
-// const updateProjectRedis = async (id: string, project: Project) => {
-// const key = getRedisProjectKey(id)
-
-// try {
-// await redis.set(
-// key,
-// JSON.stringify(project),
-// 'EX',
-// redisProjectCacheTimeout,
-// )
-// } catch {
-// await redis.del(key)
-// }
-// }
+export const PID_LENGTH = 12
const DEFAULT_USAGE_INFO = {
total: 0,
diff --git a/backend/apps/community/src/common/utils.ts b/backend/apps/community/src/common/utils.ts
index a0d354c2d..5ca7e42e1 100644
--- a/backend/apps/community/src/common/utils.ts
+++ b/backend/apps/community/src/common/utils.ts
@@ -1,7 +1,8 @@
import { NotFoundException, HttpException } from '@nestjs/common'
import { xxh3 } from '@node-rs/xxhash'
-import path from 'node:path'
-import fs from 'node:fs'
+import path from 'path'
+import fs from 'fs'
+import crypto from 'crypto'
import { CityResponse, Reader } from 'maxmind'
import timezones from 'countries-and-timezones'
import { v5 as uuidv5 } from 'uuid'
@@ -39,10 +40,24 @@ import { ClickhouseFunnel, ClickhouseSFUser } from './types'
dayjs.extend(utc)
+/*
+ Returns 128 bit (a string of 32 chars) hash of the provided string using xxHash algo.
+*/
export const hash = (content: string): string => {
return xxh3.xxh128(content).toString(16)
}
+export const generateRandomId = (alphabet: string, size: number) => {
+ const bytes = crypto.randomBytes(size)
+ let id = ''
+
+ for (let i = 0; i < size; i++) {
+ id += alphabet[bytes[i] % alphabet.length]
+ }
+
+ return id
+}
+
const RATE_LIMIT_REQUESTS_AMOUNT = 3
const RATE_LIMIT_TIMEOUT = 86400 // 24 hours
diff --git a/backend/apps/community/src/project/project.controller.ts b/backend/apps/community/src/project/project.controller.ts
index d92e2e621..fa062ecb4 100644
--- a/backend/apps/community/src/project/project.controller.ts
+++ b/backend/apps/community/src/project/project.controller.ts
@@ -43,9 +43,10 @@ import { JwtAccessTokenGuard } from '../auth/guards'
import { Auth } from '../auth/decorators'
import { isValidDate } from '../analytics/analytics.service'
import {
+ LEGAL_PID_CHARACTERS,
+ PID_LENGTH,
ProjectService,
deleteProjectRedis,
- generateProjectId,
} from './project.service'
import { RolesGuard } from '../auth/guards/roles.guard'
import { Pagination } from '../common/pagination/pagination'
@@ -78,6 +79,7 @@ import {
updateProjectViewClickhouse,
getProjectClickhouse,
getFunnelClickhouse,
+ generateRandomId,
} from '../common/utils'
import { Funnel } from './entity/funnel.entity'
import { ProjectViewEntity } from './entity/project-view.entity'
@@ -200,10 +202,10 @@ export class ProjectController {
this.projectService.validateProject(projectDTO as ProjectDTO, true)
- let pid = generateProjectId()
+ let pid = generateRandomId(LEGAL_PID_CHARACTERS, PID_LENGTH)
while (!this.projectService.isPIDUnique(projects, pid)) {
- pid = generateProjectId()
+ pid = generateRandomId(LEGAL_PID_CHARACTERS, PID_LENGTH)
}
const project = new Project()
diff --git a/backend/apps/community/src/project/project.service.ts b/backend/apps/community/src/project/project.service.ts
index e7b636194..785199333 100644
--- a/backend/apps/community/src/project/project.service.ts
+++ b/backend/apps/community/src/project/project.service.ts
@@ -7,7 +7,6 @@ import {
InternalServerErrorException,
ConflictException,
} from '@nestjs/common'
-import { customAlphabet } from 'nanoid'
import _isEmpty from 'lodash/isEmpty'
import _isString from 'lodash/isString'
import _size from 'lodash/size'
@@ -48,9 +47,9 @@ import { ProjectViewCustomEventEntity } from './entity/project-view-custom-event
import { ClickhouseFunnel } from '../common/types'
// A list of characters that can be used in a Project ID
-const LEGAL_PID_CHARACTERS =
+export const LEGAL_PID_CHARACTERS =
'1234567890QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm'
-export const generateProjectId = customAlphabet(LEGAL_PID_CHARACTERS, 12)
+export const PID_LENGTH = 12
export const deleteProjectRedis = async (id: string) => {
const key = getRedisProjectKey(id)
diff --git a/backend/package-lock.json b/backend/package-lock.json
index e57aca050..a29f8ea15 100644
--- a/backend/package-lock.json
+++ b/backend/package-lock.json
@@ -51,7 +51,6 @@
"lodash": "^4.17.21",
"maxmind": "^4.3.24",
"mysql2": "^3.14.0",
- "nanoid": "^3.3.7",
"nestjs-form-data": "^1.9.93",
"nestjs-i18n": "^10.5.1",
"nestjs-telegraf": "^2.8.1",
@@ -9450,24 +9449,6 @@
"node": ">=12.0.0"
}
},
- "node_modules/nanoid": {
- "version": "3.3.11",
- "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.11.tgz",
- "integrity": "sha512-N8SpfPUnUp1bK+PMYW8qSWdl9U+wwNWI4QKxOYDy9JAro3WMX7p2OeVRF9v+347pnakNevPmiHhNmZ2HbFA76w==",
- "funding": [
- {
- "type": "github",
- "url": "https://github.com/sponsors/ai"
- }
- ],
- "license": "MIT",
- "bin": {
- "nanoid": "bin/nanoid.cjs"
- },
- "engines": {
- "node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1"
- }
- },
"node_modules/natural-compare": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz",
diff --git a/backend/package.json b/backend/package.json
index 48ce37fd5..85849ffc2 100644
--- a/backend/package.json
+++ b/backend/package.json
@@ -70,7 +70,6 @@
"lodash": "^4.17.21",
"maxmind": "^4.3.24",
"mysql2": "^3.14.0",
- "nanoid": "^3.3.7",
"nestjs-form-data": "^1.9.93",
"nestjs-i18n": "^10.5.1",
"nestjs-telegraf": "^2.8.1",