Skip to content

Add fallback ID generator for environments without crypto.randomUUID#12

Open
shiye515 wants to merge 1 commit into
pgplex:mainfrom
shiye515:fix-randomuuid-fallback
Open

Add fallback ID generator for environments without crypto.randomUUID#12
shiye515 wants to merge 1 commit into
pgplex:mainfrom
shiye515:fix-randomuuid-fallback

Conversation

@shiye515
Copy link
Copy Markdown

Summary

Replace direct crypto.randomUUID() usage in browser-executed code with a shared ID helper that falls back when globalThis.crypto?.randomUUID is unavailable.

Changes

  • add src/lib/create-id.ts
  • replace direct UUID generation in useEditorTabs.ts
  • replace direct UUID generation in staged-changes.ts

Why

Some users running the app via Docker hit a browser-side runtime error:

index-03aNhJ-r.js:1468 Uncaught (in promise) TypeError: crypto.randomUUID is not a function
    at index-03aNhJ-r.js:1468:15414
    at Array.map (<anonymous>)
    at index-03aNhJ-r.js:1468:15214
    at handleExecute (index-03aNhJ-r.js:1460:723)

This change avoids relying on crypto.randomUUID() being present in all client environments.

Copilot AI review requested due to automatic review settings May 23, 2026 12:52
@vercel
Copy link
Copy Markdown

vercel Bot commented May 23, 2026

@shiye515 is attempting to deploy a commit to the bytebase Team on Vercel.

A member of the Team first needs to authorize it.

@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented May 23, 2026

Greptile Summary

This PR adds a shared browser-safe ID helper for code paths that previously called crypto.randomUUID() directly. The main changes are:

  • Added src/lib/create-id.ts with a randomUUID path and fallback path.
  • Switched SQL result tab ID creation to use the helper.
  • Switched staged delete, insert, and update change IDs to use the helper.

Confidence Score: 4/5

This is close, but the fallback should be hardened before merging.

  • The direct crypto.randomUUID() crash path is addressed.

  • The fallback can still produce duplicate IDs in the same millisecond.

  • Duplicate IDs can confuse result tab state and staged change removal.

  • src/lib/create-id.ts should make fallback IDs unique within the running app.

Important Files Changed

Filename Overview
src/lib/create-id.ts Introduces the shared ID helper and fallback path used by the changed callers.
src/components/sql-editor/hooks/useEditorTabs.ts Uses the new helper for result tab IDs, which are later used as UI and state keys.
src/lib/staged-changes.ts Uses the new helper for staged change IDs used by staged-change operations.

Reviews (1): Last reviewed commit: "Add fallback ID generator for environmen..." | Re-trigger Greptile

Comment thread src/lib/create-id.ts
return globalThis.crypto.randomUUID()
}

return `id-${Date.now()}-${Math.random().toString(36).slice(2, 11)}`
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 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.

Suggested 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++}`

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@shiye515
Copy link
Copy Markdown
Author

When a web page is opened using a non‑HTTPS link, the crypto.randomUUID() function is unavailable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant