Files
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

16 lines
641 B
TypeScript

const randomByte = () => crypto.getRandomValues(new Uint8Array(1))[0]
/** Uniform random integer in [0, max) for max <= 256, using rejection sampling to avoid modulo bias. */
const randomIndex = (max: number) => {
const limit = 256 - (256 % max)
let value = randomByte()
while (value >= limit) value = randomByte()
return value % max
}
export const generateRandomString = (length: number, charset: string) =>
Array.from({ length }, () => charset[randomIndex(charset.length)]).join('')
export const generateRandomBase64 = (byteLength: number) =>
btoa(String.fromCharCode(...crypto.getRandomValues(new Uint8Array(byteLength))))