Skip to content

🌈 improve(health): reduce cognitive complexity in health check handler#9

Merged
UniquePixels merged 1 commit intodevelopfrom
fix/health-check-complexity
Mar 9, 2026
Merged

🌈 improve(health): reduce cognitive complexity in health check handler#9
UniquePixels merged 1 commit intodevelopfrom
fix/health-check-complexity

Conversation

@UniquePixels
Copy link
Owner

@UniquePixels UniquePixels commented Mar 9, 2026

Summary

  • Extract respond(), handleLiveness(), and handleReadiness() helpers from the nested closure in createHealthCheckHandler
  • Reduces cognitive complexity from 18 to within the Biome limit of 15
  • No behavior changes β€” all existing tests pass without modification

Test plan

  • bun biome check src/health-check.ts β€” zero findings
  • bun test β€” 608 tests pass, 100% coverage on health-check.ts
  • bun qa:tsc β€” passes clean

πŸ€– Generated with Claude Code

Summary by CodeRabbit

  • Refactor
    • Improved health check service implementation through better code organization and streamlined internal operations. Enhanced overall code quality and maintainability with consolidated response handling and improved security header management. All health check endpoints continue to operate as expected with no visible changes to the public API or functionality.

Extract respond(), handleLiveness(), and handleReadiness() helpers from
the nested closure to bring complexity from 18 to within the 15 limit.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Signed-off-by: Brian L. <brian@uniquepixels.xyz>
@coderabbitai
Copy link

coderabbitai bot commented Mar 9, 2026

No actionable comments were generated in the recent review. πŸŽ‰

ℹ️ Recent review info
βš™οΈ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 6f088e60-7928-4e9e-af60-f0298d18d69b

πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between b4448fc and c0dcf99.

πŸ“’ Files selected for processing (1)
  • src/health-check.ts
πŸ“œ Recent review details
🧰 Additional context used
πŸ““ Path-based instructions (1)
**

βš™οΈ CodeRabbit configuration file

**: General project rules:

  • Runtime is Bun, not Node.js. Do not suggest node/npm/yarn patterns.
  • Zod v4 is used. z.enum() handles native TS enums directly;
    z.nativeEnum() is deprecated. Do not flag z.enum(SomeEnum) as incorrect.
  • attempt() is the project's Result-type wrapper. All fallible calls in
    sparks must use it β€” never raw try/catch or uncaught promises.
  • import type is required for type-only imports (verbatimModuleSyntax).
  • Biome enforces kebab-case filenames and organized imports.

Files:

  • src/health-check.ts
πŸ”‡ Additional comments (4)
src/health-check.ts (4)

10-20: LGTM!

Clean helper that centralizes response construction with security headers. The conditional spread for extra headers is efficient.


22-25: LGTM!

Simple and focused liveness probe handler.


27-37: LGTM!

Correctly prioritizes shutdown state over client readiness, and the distinct response bodies ("Shutting Down" vs "Not Ready") aid operational debugging.


50-69: LGTM!

The refactored handler is cleaner and maintains identical behavior. The new URL(req.url) parsing is safe since Bun.serve() guarantees absolute URLs in the fetch handler, and all response paths now consistently apply security headers via respond().


πŸ“ Walkthrough

Walkthrough

Refactors health check handler to introduce internal helper functions (respond, handleLiveness, handleReadiness) that centralize header and response construction logic. Updates pathname extraction to use URL parsing instead of direct property access, while maintaining existing public API and functionality.

Changes

Cohort / File(s) Summary
Health Check Refactoring
src/health-check.ts
Introduces internal helpers to centralize response construction with security headers; refactors pathname extraction using URL parsing; consolidates response creation for liveness (200), readiness (200/503), method-not-allowed (405), and not-found (404) handlers.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

πŸš₯ Pre-merge checks | βœ… 3
βœ… Passed checks (3 passed)
Check name Status Explanation
Title check βœ… Passed The title clearly and specifically describes the main change: extracting helper functions to reduce cognitive complexity in the health check handler, which is the primary objective of the PR.
Description check βœ… Passed The description provides a clear summary of changes, rationale (complexity reduction), test coverage, and verification results. It matches the template intent and includes substantive details beyond the template requirement.
Docstring Coverage βœ… Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • πŸ“ Generate docstrings (stacked PR)
  • πŸ“ Generate docstrings (commit on current branch)
πŸ§ͺ Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/health-check-complexity

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❀️ Share

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

@sonarqubecloud
Copy link

sonarqubecloud bot commented Mar 9, 2026

@UniquePixels UniquePixels merged commit 531b56f into develop Mar 9, 2026
12 checks passed
@UniquePixels UniquePixels deleted the fix/health-check-complexity branch March 9, 2026 01:26
@UniquePixels UniquePixels changed the title Reduce health check handler cognitive complexity 🌈 improve(health): reduce cognitive complexity in health check handler Mar 9, 2026
UniquePixels added a commit that referenced this pull request Mar 9, 2026
## Release v0.1.1

### 🌈 Improvements

- reduce cognitive complexity in health check handler (#9)
- detect all Biome finding severities as failures (#8)
- revamp badge row (#21)
- add Renovate commit format and squash strategy (#10)
- consolidate test utilities (#6)

### 🧺 Chores

- update undici to v7
- update install-action digest

---

**After merging**, the tag-release job will automatically:
- Tag this commit as `v0.1.1`
- Create a GitHub Release with the changelog
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