|
| 1 | +import * as fs from 'fs'; |
| 2 | +import { v4 as uuidv4 } from 'uuid'; |
| 3 | + |
| 4 | +const USERS_TO_GENERATE = 1000000; |
| 5 | +const USERS_PER_JSON = 10000; |
| 6 | + |
| 7 | +const n = Math.floor(USERS_TO_GENERATE / USERS_PER_JSON); |
| 8 | + |
| 9 | +const generatedEmails = new Set<string>(); |
| 10 | +const generatedPhoneNumbers = new Set<string>(); |
| 11 | +const generatedUserIds = new Set<string>(); |
| 12 | + |
| 13 | +interface LoginMethod { |
| 14 | + tenantIds: string[]; |
| 15 | + email: string; |
| 16 | + recipeId: string; |
| 17 | + passwordHash?: string; |
| 18 | + hashingAlgorithm?: string; |
| 19 | + thirdPartyId?: string; |
| 20 | + thirdPartyUserId?: string; |
| 21 | + phoneNumber?: string; |
| 22 | + isVerified: boolean; |
| 23 | + isPrimary: boolean; |
| 24 | + timeJoinedInMSSinceEpoch: number; |
| 25 | +} |
| 26 | + |
| 27 | +interface User { |
| 28 | + externalUserId: string; |
| 29 | + userRoles: Array<{ |
| 30 | + role: string; |
| 31 | + tenantIds: string[]; |
| 32 | + }>; |
| 33 | + loginMethods: LoginMethod[]; |
| 34 | +} |
| 35 | + |
| 36 | +function createEmailLoginMethod(email: string, tenantIds: string[]): LoginMethod { |
| 37 | + return { |
| 38 | + tenantIds, |
| 39 | + email, |
| 40 | + recipeId: "emailpassword", |
| 41 | + passwordHash: "$argon2d$v=19$m=12,t=3,p=1$aGI4enNvMmd0Zm0wMDAwMA$r6p7qbr6HD+8CD7sBi4HVw", |
| 42 | + hashingAlgorithm: "argon2", |
| 43 | + isVerified: true, |
| 44 | + isPrimary: false, |
| 45 | + timeJoinedInMSSinceEpoch: Math.floor(Math.random() * (Date.now() - (3 * 365 * 24 * 60 * 60 * 1000))) + (3 * 365 * 24 * 60 * 60 * 1000) |
| 46 | + }; |
| 47 | +} |
| 48 | + |
| 49 | +function createThirdPartyLoginMethod(email: string, tenantIds: string[]): LoginMethod { |
| 50 | + return { |
| 51 | + tenantIds, |
| 52 | + recipeId: "thirdparty", |
| 53 | + email, |
| 54 | + thirdPartyId: "google", |
| 55 | + thirdPartyUserId: String(hashCode(email)), |
| 56 | + isVerified: true, |
| 57 | + isPrimary: false, |
| 58 | + timeJoinedInMSSinceEpoch: Math.floor(Math.random() * (Date.now() - (3 * 365 * 24 * 60 * 60 * 1000))) + (3 * 365 * 24 * 60 * 60 * 1000) |
| 59 | + }; |
| 60 | +} |
| 61 | + |
| 62 | +function createPasswordlessLoginMethod(email: string, tenantIds: string[], phoneNumber: string): LoginMethod { |
| 63 | + return { |
| 64 | + tenantIds, |
| 65 | + email, |
| 66 | + recipeId: "passwordless", |
| 67 | + phoneNumber, |
| 68 | + isVerified: true, |
| 69 | + isPrimary: false, |
| 70 | + timeJoinedInMSSinceEpoch: Math.floor(Math.random() * (Date.now() - (3 * 365 * 24 * 60 * 60 * 1000))) + (3 * 365 * 24 * 60 * 60 * 1000) |
| 71 | + }; |
| 72 | +} |
| 73 | + |
| 74 | +function hashCode(str: string): number { |
| 75 | + let hash = 0; |
| 76 | + for (let i = 0; i < str.length; i++) { |
| 77 | + const char = str.charCodeAt(i); |
| 78 | + hash = ((hash << 5) - hash) + char; |
| 79 | + hash = hash & hash; |
| 80 | + } |
| 81 | + return hash; |
| 82 | +} |
| 83 | + |
| 84 | +function generateRandomString(length: number, chars: string): string { |
| 85 | + let result = ''; |
| 86 | + for (let i = 0; i < length; i++) { |
| 87 | + result += chars.charAt(Math.floor(Math.random() * chars.length)); |
| 88 | + } |
| 89 | + return result; |
| 90 | +} |
| 91 | + |
| 92 | +function generateRandomEmail(): string { |
| 93 | + return `${generateRandomString(24, 'abcdefghijklmnopqrstuvwxyz')}@example.com`; |
| 94 | +} |
| 95 | + |
| 96 | +function generateRandomPhoneNumber(): string { |
| 97 | + return `+91${generateRandomString(10, '0123456789')}`; |
| 98 | +} |
| 99 | + |
| 100 | +function genUser(): User { |
| 101 | + const user: User = { |
| 102 | + externalUserId: '', |
| 103 | + userRoles: [ |
| 104 | + { role: "role1", tenantIds: ["public"] }, |
| 105 | + { role: "role2", tenantIds: ["public"] } |
| 106 | + ], |
| 107 | + loginMethods: [] |
| 108 | + }; |
| 109 | + |
| 110 | + let userId = uuidv4(); |
| 111 | + while (generatedUserIds.has(userId)) { |
| 112 | + userId = uuidv4(); |
| 113 | + } |
| 114 | + generatedUserIds.add(userId); |
| 115 | + user.externalUserId = userId; |
| 116 | + |
| 117 | + const tenantIds = ["public"]; |
| 118 | + |
| 119 | + let email = generateRandomEmail(); |
| 120 | + while (generatedEmails.has(email)) { |
| 121 | + email = generateRandomEmail(); |
| 122 | + } |
| 123 | + generatedEmails.add(email); |
| 124 | + |
| 125 | + const loginMethods: LoginMethod[] = []; |
| 126 | + |
| 127 | + // Always add email login method |
| 128 | + loginMethods.push(createEmailLoginMethod(email, tenantIds)); |
| 129 | + |
| 130 | + // 50% chance to add third party login |
| 131 | + if (Math.random() < 0.5) { |
| 132 | + loginMethods.push(createThirdPartyLoginMethod(email, tenantIds)); |
| 133 | + } |
| 134 | + |
| 135 | + // 50% chance to add passwordless login |
| 136 | + if (Math.random() < 0.5) { |
| 137 | + let phoneNumber = generateRandomPhoneNumber(); |
| 138 | + while (generatedPhoneNumbers.has(phoneNumber)) { |
| 139 | + phoneNumber = generateRandomPhoneNumber(); |
| 140 | + } |
| 141 | + generatedPhoneNumbers.add(phoneNumber); |
| 142 | + loginMethods.push(createPasswordlessLoginMethod(email, tenantIds, phoneNumber)); |
| 143 | + } |
| 144 | + |
| 145 | + // If no methods were added, randomly add one |
| 146 | + if (loginMethods.length === 0) { |
| 147 | + const methodNumber = Math.floor(Math.random() * 3); |
| 148 | + if (methodNumber === 0) { |
| 149 | + loginMethods.push(createEmailLoginMethod(email, tenantIds)); |
| 150 | + } else if (methodNumber === 1) { |
| 151 | + loginMethods.push(createThirdPartyLoginMethod(email, tenantIds)); |
| 152 | + } else { |
| 153 | + let phoneNumber = generateRandomPhoneNumber(); |
| 154 | + while (generatedPhoneNumbers.has(phoneNumber)) { |
| 155 | + phoneNumber = generateRandomPhoneNumber(); |
| 156 | + } |
| 157 | + generatedPhoneNumbers.add(phoneNumber); |
| 158 | + loginMethods.push(createPasswordlessLoginMethod(email, tenantIds, phoneNumber)); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + loginMethods[Math.floor(Math.random() * loginMethods.length)].isPrimary = true; |
| 163 | + |
| 164 | + user.loginMethods = loginMethods; |
| 165 | + return user; |
| 166 | +} |
| 167 | + |
| 168 | +// Create users directory if it doesn't exist |
| 169 | +if (!fs.existsSync('users')) { |
| 170 | + fs.mkdirSync('users'); |
| 171 | +} |
| 172 | + |
| 173 | +for (let i = 0; i < n; i++) { |
| 174 | + console.log(`Generating ${USERS_PER_JSON} users for ${i}`); |
| 175 | + const users: User[] = []; |
| 176 | + for (let j = 0; j < USERS_PER_JSON; j++) { |
| 177 | + users.push(genUser()); |
| 178 | + } |
| 179 | + fs.writeFileSync(`users/users-${i.toString().padStart(4, '0')}.json`, JSON.stringify({ users }, null, 2)); |
| 180 | +} |
0 commit comments