Skip to content

feat(shared,ui): promote the Protect check runner to a public @clerk/shared/protect-check entry - #9391

Open
mwickett wants to merge 1 commit into
mainfrom
mwickett/prot-855-extract-protect-check-runner
Open

feat(shared,ui): promote the Protect check runner to a public @clerk/shared/protect-check entry#9391
mwickett wants to merge 1 commit into
mainfrom
mwickett/prot-855-extract-protect-check-runner

Conversation

@mwickett

Copy link
Copy Markdown
Member

Description

Custom-flow (headless) apps can already see a Protect gate (signIn.protectCheck / signUp.protectCheck) and submit a proof token (submitProtectCheck()), but the piece that actually runs a challenge lives on the internal @clerk/shared/internal/clerk-js/protectCheck subpath, and the lifecycle handling around it is private to @clerk/ui's useProtectCheckRunner.

This PR is the extraction step that unblocks a public custom-flow challenge API, with no behavioral changes:

  • New public entry point @clerk/shared/protect-check exposing:
    • executeProtectCheck (moved verbatim; the internal subpath re-exports it as an alias so existing imports keep resolving)
    • executeProtectCheckWithTimeout — container ownership (clears stale widgets), the script timeout race, one-way caller-signal linking, and the zombie-script setWidgetVisible guard
    • submitProtectCheckProof — proof submission with protect_check_already_resolved treated as soft success (reload + continue)
    • isProtectCheckExpired, MAX_EXPIRED_RELOADS, PROTECT_CHECK_SCRIPT_TIMEOUT_MS, PROTECT_CHECK_ERROR_CODES
  • @clerk/ui's useProtectCheckRunner now drives its lifecycle through those shared helpers and keeps only the React orchestration (effect keying on the challenge token, mount/cancel races, flushSync visibility handshake, card error state, the no-RHC fail-closed guard — which must stay in the consumer layer since @clerk/shared compiles with __BUILD_DISABLE_RHC__ hard-coded false).
  • The shared module is still only loaded via the RHC-gated dynamic import(), so no-RHC bundles (chrome-extension, clerk.no-rhc.js) continue to dead-code-eliminate the loader entirely.

Follow-ups building on this entry: resolveProtectCheck() resource methods for imperative/legacy flows and a useProtectCheck() hook in @clerk/shared/react for the hooks API.

Checklist

  • pnpm test runs as expected.
  • pnpm build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

@changeset-bot

changeset-bot Bot commented Aug 11, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f6193d9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 23 packages
Name Type
@clerk/shared Minor
@clerk/ui Patch
@clerk/astro Patch
@clerk/backend Patch
@clerk/chrome-extension Patch
@clerk/clerk-js Patch
@clerk/electron Patch
@clerk/expo-passkeys Patch
@clerk/expo Patch
@clerk/express Patch
@clerk/fastify Patch
@clerk/headless Patch
@clerk/hono Patch
@clerk/localizations Patch
@clerk/msw Patch
@clerk/nextjs Patch
@clerk/nuxt Patch
@clerk/react-router Patch
@clerk/react Patch
@clerk/tanstack-react-start Patch
@clerk/testing Patch
@clerk/vue Patch
@clerk/swingset Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel

vercel Bot commented Aug 11, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
clerk-js-sandbox Ready Ready Preview Aug 11, 2026 2:22am
swingset Ready Ready Preview Aug 11, 2026 2:22am

Request Review

@pkg-pr-new

pkg-pr-new Bot commented Aug 11, 2026

Copy link
Copy Markdown

Open in StackBlitz

@clerk/astro

npm i https://pkg.pr.new/@clerk/astro@9391

@clerk/backend

npm i https://pkg.pr.new/@clerk/backend@9391

@clerk/chrome-extension

npm i https://pkg.pr.new/@clerk/chrome-extension@9391

@clerk/clerk-js

npm i https://pkg.pr.new/@clerk/clerk-js@9391

@clerk/electron

npm i https://pkg.pr.new/@clerk/electron@9391

@clerk/electron-passkeys

npm i https://pkg.pr.new/@clerk/electron-passkeys@9391

@clerk/eslint-plugin

npm i https://pkg.pr.new/@clerk/eslint-plugin@9391

@clerk/expo

npm i https://pkg.pr.new/@clerk/expo@9391

@clerk/expo-google-signin

npm i https://pkg.pr.new/@clerk/expo-google-signin@9391

@clerk/expo-passkeys

npm i https://pkg.pr.new/@clerk/expo-passkeys@9391

@clerk/express

npm i https://pkg.pr.new/@clerk/express@9391

@clerk/fastify

npm i https://pkg.pr.new/@clerk/fastify@9391

@clerk/hono

npm i https://pkg.pr.new/@clerk/hono@9391

@clerk/localizations

npm i https://pkg.pr.new/@clerk/localizations@9391

@clerk/nextjs

npm i https://pkg.pr.new/@clerk/nextjs@9391

@clerk/nuxt

npm i https://pkg.pr.new/@clerk/nuxt@9391

@clerk/react

npm i https://pkg.pr.new/@clerk/react@9391

@clerk/react-router

npm i https://pkg.pr.new/@clerk/react-router@9391

@clerk/shared

npm i https://pkg.pr.new/@clerk/shared@9391

@clerk/tanstack-react-start

npm i https://pkg.pr.new/@clerk/tanstack-react-start@9391

@clerk/testing

npm i https://pkg.pr.new/@clerk/testing@9391

@clerk/ui

npm i https://pkg.pr.new/@clerk/ui@9391

@clerk/upgrade

npm i https://pkg.pr.new/@clerk/upgrade@9391

@clerk/vue

npm i https://pkg.pr.new/@clerk/vue@9391

commit: f6193d9

@github-actions

Copy link
Copy Markdown
Contributor

API Changes Report

Generated by Break Check on 2026-08-11T02:23:58.977Z

Summary

Metric Count
Packages analyzed 19
Packages with changes 1
🔴 Breaking changes 0
🟡 Non-breaking changes 0
🟢 Additions 1

@clerk/shared

Current version: 4.28.1
Recommended bump: MINOR → 4.29.0

Subpath ./protect-check

🟢 Additions (1)

Added: ./protect-check

New subpath export ./protect-check (13 exported members)


Report generated by Break Check

Last ran on f6193d9.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f6193d98ba

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +1 to +2
// Moved to the public `@clerk/shared/protect-check` entry; this subpath is kept as an alias so
// existing imports keep resolving.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Remove the comment that restates the alias

This comment only narrates that the following re-export preserves the existing subpath, which is already evident from the code. Remove it rather than adding a multi-line restatement; repository guidance permits comments only for critical, non-obvious rationale and requires them to be terse.

AGENTS.md reference: AGENTS.md:L13-L13

Useful? React with 👍 / 👎.

@coderabbitai

coderabbitai Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The shared package adds a public @clerk/shared/protect-check entry point. It centralizes Protect Check execution, timeout handling, expiration checks, proof submission, abort handling, cleanup, and error codes. The previous internal path remains an alias. The UI runner now uses the shared lifecycle helpers. Tests cover execution and lifecycle behavior, and package build and release metadata include the new entry point.

Estimated code review effort: 4 (Complex) | ~45 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description check ✅ Passed The description clearly explains the new public entry point, shared lifecycle helpers, UI refactor, compatibility, and intended behavior.
Title check ✅ Passed The title clearly and concisely identifies the promotion of the Protect check runner to the public shared entry point.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (2)
packages/ui/src/components/SignIn/__tests__/SignInProtectCheck.test.tsx (1)

22-22: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Preserve the shared helper signature in both mocks.

Both tests cast executeProtectCheckWithTimeout to ReturnType<typeof vi.fn>. This removes compile-time validation of mock parameters and return values. Use vi.mocked(executeProtectCheckWithTimeout) or an equivalent MockedFunction type in both files.

  • packages/ui/src/components/SignIn/__tests__/SignInProtectCheck.test.tsx#L22-L22: Replace the broad cast with a typed Vitest mock.
  • packages/ui/src/components/SignUp/__tests__/SignUpProtectCheck.test.tsx#L23-L23: Replace the broad cast with a typed Vitest mock.

As per coding guidelines, use precise TypeScript types and avoid broad mock types when a narrower type is available.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/ui/src/components/SignIn/__tests__/SignInProtectCheck.test.tsx` at
line 22, Replace the broad ReturnType<typeof vi.fn> casts for
executeProtectCheckWithTimeout with vi.mocked or an equivalent precise
MockedFunction type in both
packages/ui/src/components/SignIn/__tests__/SignInProtectCheck.test.tsx:22-22
and
packages/ui/src/components/SignUp/__tests__/SignUpProtectCheck.test.tsx:23-23,
preserving compile-time validation of the shared helper’s parameters and return
value.

Source: Coding guidelines

packages/shared/src/protect-check/executeProtectCheck.ts (1)

59-72: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Use PROTECT_CHECK_ERROR_CODES for all thrown error codes in executeProtectCheck.ts. Replace the ten hardcoded literals at lines 60, 65, 70, 108, 121, 126, 131, 144, 148, and 155.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/shared/src/protect-check/executeProtectCheck.ts` around lines 59 -
72, Update all thrown error objects in executeProtectCheck to use the
corresponding constants from PROTECT_CHECK_ERROR_CODES instead of hardcoded
error-code string literals, including the validation and later failure paths
identified in the comment. Preserve each error’s existing semantic code while
centralizing the values through that shared constant.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@packages/shared/src/protect-check/executeProtectCheck.ts`:
- Around line 59-72: Update all thrown error objects in executeProtectCheck to
use the corresponding constants from PROTECT_CHECK_ERROR_CODES instead of
hardcoded error-code string literals, including the validation and later failure
paths identified in the comment. Preserve each error’s existing semantic code
while centralizing the values through that shared constant.

In `@packages/ui/src/components/SignIn/__tests__/SignInProtectCheck.test.tsx`:
- Line 22: Replace the broad ReturnType<typeof vi.fn> casts for
executeProtectCheckWithTimeout with vi.mocked or an equivalent precise
MockedFunction type in both
packages/ui/src/components/SignIn/__tests__/SignInProtectCheck.test.tsx:22-22
and
packages/ui/src/components/SignUp/__tests__/SignUpProtectCheck.test.tsx:23-23,
preserving compile-time validation of the shared helper’s parameters and return
value.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro Plus

Run ID: 307fd263-40d0-4c7f-9b07-63201917dc3d

📥 Commits

Reviewing files that changed from the base of the PR and between b9d25d7 and f6193d9.

📒 Files selected for processing (14)
  • .changeset/protect-check-public-entry.md
  • .changeset/protect-check-ui-refactor.md
  • packages/shared/package.json
  • packages/shared/src/internal/clerk-js/protectCheck.ts
  • packages/shared/src/protect-check/__tests__/executeProtectCheck.test.ts
  • packages/shared/src/protect-check/__tests__/lifecycle.test.ts
  • packages/shared/src/protect-check/errors.ts
  • packages/shared/src/protect-check/executeProtectCheck.ts
  • packages/shared/src/protect-check/index.ts
  • packages/shared/src/protect-check/lifecycle.ts
  • packages/shared/tsdown.config.mts
  • packages/ui/src/components/SignIn/__tests__/SignInProtectCheck.test.tsx
  • packages/ui/src/components/SignUp/__tests__/SignUpProtectCheck.test.tsx
  • packages/ui/src/hooks/useProtectCheckRunner.ts
🔗 Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

  • clerk/clerk_go (manual)
  • clerk/dashboard (manual)
  • clerk/accounts (manual)
  • clerk/backoffice (manual)
  • clerk/clerk (manual)
  • clerk/clerk-docs (manual)
  • clerk/cloudflare-workers (manual)
  • clerk/cli (auto-detected)
  • clerk/clerk-ios (auto-detected)
  • clerk/clerk-android (auto-detected)

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant