mirror of
https://github.com/supabase/supabase.git
synced 2026-05-07 17:30:25 -04:00
0dec08c96f
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Chores** * Upgraded the UUID library to a newer major version across apps and removed a now-unneeded dev dependency. * Pinned PostCSS to a workspace-specific version to stabilize builds. * **Refactor** * Improved internal identifier generation for more consistent behavior without changing outward functionality. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
48 lines
1.5 KiB
TypeScript
48 lines
1.5 KiB
TypeScript
import { IS_PLATFORM } from 'common'
|
|
import { compact } from 'lodash'
|
|
import { v4 as uuidv4 } from 'uuid'
|
|
|
|
/**
|
|
* Generates a UUID v4. If the platform is self-hosted, it will generate a deterministic UUID v4 from the inputs.
|
|
*/
|
|
export const generateUuid = (inputs: (string | undefined | null)[] = []) => {
|
|
const cleaned = compact(inputs)
|
|
if (!IS_PLATFORM && cleaned.length === 0) return uuidv4()
|
|
return IS_PLATFORM ? uuidv4() : generateDeterministicUuid(cleaned)
|
|
}
|
|
|
|
/**
|
|
* Generates a deterministic UUID v4 from a string input
|
|
* @param inputs - The array of strings to generate a UUID from
|
|
* @returns A deterministic UUID v4 string
|
|
*/
|
|
export function generateDeterministicUuid(inputs: (string | undefined | null)[]): string {
|
|
const simpleHash = (str: string): number => {
|
|
let hash = 0
|
|
if (str.length === 0) return hash
|
|
for (let i = 0; i < str.length; i++) {
|
|
const char = str.charCodeAt(i)
|
|
hash = (hash << 5) - hash + char
|
|
hash = hash & hash // Convert to 32-bit integer
|
|
}
|
|
return Math.abs(hash)
|
|
}
|
|
|
|
const input = compact(inputs).join('_')
|
|
|
|
// Create a deterministic random number generator using the hash as seed
|
|
let seed = simpleHash(input)
|
|
const rng = () => {
|
|
const bytes = new Uint8Array(16)
|
|
for (let i = 0; i < 16; i++) {
|
|
// Simple LCG (Linear Congruential Generator) for deterministic randomness
|
|
seed = (seed * 1103515245 + 12345) & 0x7fffffff
|
|
bytes[i] = (seed >>> 16) & 0xff
|
|
}
|
|
return bytes
|
|
}
|
|
|
|
// Generate UUID v4 using the deterministic RNG
|
|
return uuidv4({ rng })
|
|
}
|