Secure UUID v4 Generator (lib/random-id.ts)

DeskcommCRM · lib/random-id.ts · typescript · MIT
integration, security, lang:typescript, tag:crypto, tag:uuid, tag:fallback

Generates UUID v4 identifiers using crypto.getRandomValues with a fallback for insecure HTTP contexts where randomUUID is unavailable.

// Source: https://github.com/melgarafael/DeskcommCRM | License: MIT | Copyright 2026 Rafael Melgaço
// Provided via justclaw.it.com; keep this notice with the code.
/**
 * UUID v4 que funciona FORA de secure context (achado nº 9 do deploy VPS):
 * em `http://IP` (self-host sem TLS) o browser NÃO expõe `crypto.randomUUID`
 * (é restrito a secure context — localhost/https), e o apiClient morria em
 * TypeError ANTES do fetch, derrubando toda tela que fala com a API.
 * `crypto.getRandomValues` existe em QUALQUER contexto; o fallback monta o
 * UUID v4 a partir dele (RFC 4122: version nibble 4, variant 10).
 *
 * Código client-side NUNCA chama `crypto.randomUUID()` cru — sempre este
 * helper (teste-régua em lib/random-id.test.ts).
 */
export function randomId(): string {
  if (typeof crypto

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key