Files
supabase/apps/studio/lib/random.test.ts
653dfb7104 Fix client crash when landing on new project page (#53206)
## Why it's happening

Opening `/new/[slug]` on the TanStack/Vite dev server crashes with
`TypeError: Cannot read properties of undefined (reading 'from')`. It is
thrown from `.vite/deps/randombytes.js`, inside a `<Lazy>` boundary.

From what I understand it's happening as:
- `lib/project.tsx` imports `generate-password-browser`, which imports
`randombytes`, which imports `safe-buffer`.
- `safe-buffer` calls `require('buffer').Buffer.from`.
- In dev, Vite swaps the Node `buffer` built-in for an empty stub, so
`Buffer` is `undefined`.

Fix is to alias `buffer` to the `buffer` npm package in
`vite.config.ts`, and add `buffer` as a direct Studio dependency.

It's oddly only happening on local but not staging/prod which I'm not
too sure why honestly. Afaict from investigating with Claude, prod
builds already bundle the real `buffer` package, so `Buffer.from` exists
there, but not exactly sure how the build finds it, since nothing in the
repo declared it.

## To test
Only on local:
- [ ] First verify if the client crashes when landing on `/new/[slug]`
- Please let me know if this isn't happening on your end in which case
it might be something on my machine then perhaps
- [ ] Then checkout this branch and verify that client doesnt crash when
landing on `/new/[slug]`

---------

Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Co-authored-by: Claude <noreply@anthropic.com>
2026-11-10 15:10:26 +02:00

43 lines
1.5 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest'
import { generateRandomBase64, generateRandomString } from './random'
describe('generateRandomString', () => {
it('returns a string of the requested length using only the charset', () => {
const result = generateRandomString(64, 'abc')
expect(result).toHaveLength(64)
expect(result).toMatch(/^[abc]+$/)
})
it('returns an empty string for length 0', () => {
expect(generateRandomString(0, 'abc')).toBe('')
})
it('rejects random bytes that would introduce modulo bias', () => {
// With a charset of 62, bytes >= 248 must be discarded
const bytes = [255, 248, 62, 0]
const spy = vi.spyOn(crypto, 'getRandomValues').mockImplementation(((array: Uint8Array) => {
array[0] = bytes.shift()!
return array
}) as typeof crypto.getRandomValues)
const charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
// 62 % 62 = 0 -> 'a', then 0 -> 'a'
expect(generateRandomString(2, charset)).toBe('aa')
spy.mockRestore()
})
it('produces different values across calls', () => {
const charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
expect(generateRandomString(16, charset)).not.toBe(generateRandomString(16, charset))
})
})
describe('generateRandomBase64', () => {
it('returns base64 that decodes to the requested number of bytes', () => {
const result = generateRandomBase64(60)
expect(result).toMatch(/^[A-Za-z0-9+/]+={0,2}$/)
expect(atob(result)).toHaveLength(60)
})
})