Conversation
Fixes fastify#269 Signed-off-by: Liang Xu <lx3133584@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Adds reply APIs for inspecting pending response cookies before onSend.
Changes:
- Adds
reply.getCookie()andreply.getCookies(). - Adds TypeScript declarations and type assertions.
- Tests basic retrieval of pending cookies.
Reviewed changes
Copilot reviewed 3 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
index.js |
Implements and decorates cookie getters. |
test/cookie.test.js |
Tests basic getter behavior. |
types/index.d.ts |
Declares the new APIs. |
types/index.tst.ts |
Verifies inferred return types. |
Suppressed comments (1)
index.js:87
- A valid cookie name such as
__proto__invokes the inherited prototype setter here instead of creating an own property. The cookie is then absent fromObject.keys()/normal lookup semantics, and the returned object's prototype is unexpectedly replaced. Define the property explicitly (or otherwise use a prototype-safe container).
result[c.name] = Object.assign({ value: c.value }, c.opts)
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+85
to
+87
| const result = {} | ||
| for (const c of reply[kReplySetCookies].values()) { | ||
| result[c.name] = Object.assign({ value: c.value }, c.opts) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When cookies are set on the reply via
reply.setCookie, there was no method to inspect or read the pending response cookies inside the route handler or early lifecycle hooks.Root Cause
Pending cookies are accumulated in an internal
reply[kReplySetCookies]map until theonSendhook writes them to theSet-Cookieheader.FastifyReplylacked getter methods to query this map.Fix
fastifyCookieGetCookieandfastifyCookieGetCookieshelpers.FastifyReplywithreply.getCookie(name)andreply.getCookies().types/index.d.tsandtypes/index.tst.ts.Testing
test/cookie.test.jsassertingreply.getCookieandreply.getCookiesreturn correct values beforeonSend.