Add fallback ID generator for environments without crypto.randomUUID#12
Add fallback ID generator for environments without crypto.randomUUID#12shiye515 wants to merge 1 commit into
Conversation
|
@shiye515 is attempting to deploy a commit to the bytebase Team on Vercel. A member of the Team first needs to authorize it. |
Greptile SummaryThis PR adds a shared browser-safe ID helper for code paths that previously called
Confidence Score: 4/5This is close, but the fallback should be hardened before merging.
Important Files Changed
Reviews (1): Last reviewed commit: "Add fallback ID generator for environmen..." | Re-trigger Greptile |
| return globalThis.crypto.randomUUID() | ||
| } | ||
|
|
||
| return `id-${Date.now()}-${Math.random().toString(36).slice(2, 11)}` |
There was a problem hiding this comment.
Fallback IDs can collide The fallback ID only combines the current millisecond with
Math.random(). When two IDs are created in the same millisecond and Math.random() repeats or has weak entropy, callers receive duplicate IDs. Result tabs use these IDs as React keys and as keys into displayRowsMap, so duplicate IDs can make two result tabs share row state or make tab selection point at the wrong result. Staged changes also remove entries by matching ID, so a duplicate can remove more than one change.
| return `id-${Date.now()}-${Math.random().toString(36).slice(2, 11)}` | |
| const random = Math.random().toString(36).slice(2, 11) | |
| return `id-${Date.now()}-${random}-${createId.counter++}` |
There was a problem hiding this comment.
The program runs on the client side, and the scenario described above is very unlikely to happen. If you really want to avoid this situation completely, you can use the third-party library uuid.
|
When a web page is opened using a non‑HTTPS link, the crypto.randomUUID() function is unavailable. |
Summary
Replace direct
crypto.randomUUID()usage in browser-executed code with a shared ID helper that falls back whenglobalThis.crypto?.randomUUIDis unavailable.Changes
src/lib/create-id.tsuseEditorTabs.tsstaged-changes.tsWhy
Some users running the app via Docker hit a browser-side runtime error:
This change avoids relying on
crypto.randomUUID()being present in all client environments.