mirror of
https://github.com/supabase/supabase.git
synced 2026-07-02 10:44:33 +08:00
* Add a generateDeterministicUuid function and tests for it. * Use the new function and generate an id automatically when creating a snippet. * Clean up extra code. * Don't pass in id when creating a snippet. * Add generateSnippetTitle function and use it instead of fixed string. * When SQL editor is open, generate an id form a generated snippet title. * Add id override for SQL editor to avoid flash when saving the snippet. * Merge the two generate functions to happen in the same useMemo block. * Save the snippet to the API when adding it. * Minor fixes from CodeRabbit review. * Hide new folder CTA in sql editor for self-hosted * Don't add the snippet for saving, just set the value. * UpsertContentPayload always has an id. --------- Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
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 Array.from(bytes)
|
|
}
|
|
|
|
// Generate UUID v4 using the deterministic RNG
|
|
return uuidv4({ rng })
|
|
}
|