Skip to content

fix: pass Koa response to setHeaders#156

Open
vibhor-aggr wants to merge 1 commit into
koajs:masterfrom
vibhor-aggr:fix/setheaders-koa-response
Open

fix: pass Koa response to setHeaders#156
vibhor-aggr wants to merge 1 commit into
koajs:masterfrom
vibhor-aggr:fix/setheaders-koa-response

Conversation

@vibhor-aggr

@vibhor-aggr vibhor-aggr commented Jul 12, 2026

Copy link
Copy Markdown

Fixes #130.

Summary

  • Pass Koa's ctx.response object to the setHeaders callback instead of the raw Node response.
  • Update the SetHeaders type and README wording to describe the Koa response argument.
  • Update the existing setHeaders regression test to use Koa response methods such as response.set() and response.get().

Root Cause

setHeaders was receiving ctx.res, which requires callers to bypass Koa's response abstraction and use raw Node response methods. Koa documents that bypassing Koa's response handling is unsupported, and callers commonly need Koa response helpers when deciding whether to set cache headers.

Validation

  • npx jest test/index.test.ts --runInBand --testNamePattern='setHeaders'
  • npm test
  • npm run lint (passes with the existing send complexity warning)
  • npm run build
  • git diff --check

Summary by Sourcery

Route Koa send header injection through the Koa response abstraction instead of the raw Node response, and align types, docs, and tests with the updated callback signature.

Bug Fixes:

  • Ensure the setHeaders callback receives Koa's response object rather than the underlying Node response to avoid unsupported response handling.

Enhancements:

  • Update the SetHeaders type to reference ParameterizedContext['response'] and rename its first parameter for clarity.

Documentation:

  • Clarify README setHeaders documentation to describe the callback as receiving the Koa response object and update argument naming accordingly.

Tests:

  • Adjust the setHeaders regression test to use Koa response helpers (set/get) and assert that the callback receives ctx.response.

Summary by CodeRabbit

  • Bug Fixes

    • Updated the setHeaders callback to provide Koa’s response object, enabling standard response methods such as set() and get().
    • Preserved existing header protections and behavior, including preventing Content-Length overrides.
  • Documentation

    • Clarified the setHeaders callback signature and response parameter in the documentation.

@sourcery-ai

sourcery-ai Bot commented Jul 12, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Passes Koa's high-level response object into the setHeaders callback instead of the raw Node response, and aligns types, tests, and documentation with this behavior.

Sequence diagram for updated setHeaders invocation in send

sequenceDiagram
  participant KoaMiddleware
  participant send
  participant SetHeaders
  participant KoaResponse

  KoaMiddleware->>send: send(ctx, root, options)
  send->>SetHeaders: setHeaders(ctx.response, filePath, stats)
  send->>KoaResponse: set(Content-Length, stats.size)
  send->>KoaResponse: set(Last-Modified, stats.mtime.toUTCString())
Loading

File-Level Changes

Change Details Files
Route setHeaders to use Koa's ctx.response abstraction instead of the raw Node response.
  • Change send implementation to call setHeaders with ctx.response rather than ctx.res.
  • Update SetHeaders type signature to accept ParameterizedContext['response'].
  • Ensure header setting and retrieval logic uses Koa response helpers rather than Node response APIs.
src/send.ts
src/send.types.ts
Align tests and README with the new Koa response-based setHeaders contract.
  • Update the regression test to assert it receives ctx.response and uses response.set()/response.get() instead of res.setHeader().
  • Adjust README examples and wording to document fn(response, path, stats) and describe response as the Koa response object.
test/index.test.ts
README.md

Assessment against linked issues

Issue Objective Addressed Explanation
#130 Change the first parameter passed to the setHeaders option from the raw Node response (ctx.res) to the Koa response (ctx.response) in the implementation and types.
#130 Update tests and documentation to reflect that setHeaders now receives the Koa response object and demonstrate using Koa response methods.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@coderabbitai

coderabbitai Bot commented Jul 12, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: b351b01b-9509-490e-940a-041c1876a222

📥 Commits

Reviewing files that changed from the base of the PR and between 09657fc and b280945.

📒 Files selected for processing (4)
  • README.md
  • src/send.ts
  • src/send.types.ts
  • test/index.test.ts

📝 Walkthrough

Walkthrough

The setHeaders callback now receives ctx.response instead of ctx.res. Its internal type references Koa’s response property, tests use response.set() and response.get(), and documentation describes the updated callback signature. Existing header behavior, including protection of Content-Length, remains covered.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: passing Koa's response object to setHeaders.
Linked Issues check ✅ Passed The PR satisfies #130 by changing setHeaders to receive Koa's response object and updating types, docs, and tests accordingly.
Out of Scope Changes check ✅ Passed The changes are tightly scoped to the setHeaders response-object update and its supporting documentation and tests.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

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.

@sourcery-ai sourcery-ai 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.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

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.

Set the first parameter of the setHeaders option to Koa Response

1 participant