-
Notifications
You must be signed in to change notification settings - Fork 120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: should not throw when fetch a Request with post #563
Conversation
WalkthroughThe changes in this pull request include the addition of the Changes
Possibly related PRs
Suggested reviewers
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Outside diff range and nitpick comments (1)
test/fetch.test.ts (1)
114-122
: Consider enhancing the test coverageWhile the test verifies that the error is not thrown, it could be improved to ensure the POST request works correctly.
Consider expanding the test to verify:
- The request body is sent correctly
- The server receives the POST data
- The response is processed correctly
Example enhancement:
it('fetch request with post should work', async () => { - await assert.doesNotReject(async () => { - const request = new Request(_url, { - method: 'POST', - body: 'test-body', - }); - await fetch(request); - }, /Cannot construct a Request with a Request object that has already been used/); + const request = new Request(_url, { + method: 'POST', + body: 'test-body', + }); + const response = await fetch(request); + assert(response.ok); + const responseBody = await response.text(); + assert.equal(responseBody, 'test-body'); // Assuming echo server });
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (3)
.gitignore
(1 hunks)src/fetch.ts
(1 hunks)test/fetch.test.ts
(2 hunks)
✅ Files skipped from review due to trivial changes (1)
- .gitignore
🔇 Additional comments (2)
test/fetch.test.ts (1)
10-10
: LGTM: Import added for Request class
The import of Request
from 'undici' is correctly placed with other imports.
src/fetch.ts (1)
221-221
: LGTM: Fix prevents Request reuse error
The change correctly passes the Request object directly to UndiciFetch instead of creating a new Request object, which resolves the "already used" error for POST requests.
Let's verify there are no other places in the codebase where Request objects might be recreated:
✅ Verification successful
Request object creation is safe in current usage
The codebase shows only two instances of Request object creation:
- In test code (
test/fetch.test.ts
) which is expected and safe as it's creating test fixtures - In
src/fetch.ts
line 136, which is in a different code path than the fix we're reviewing (line 221)
The current implementation correctly prevents the "already used" error by directly passing the Request object to UndiciFetch at line 221, while the other Request creation at line 136 serves a different purpose in the request initialization flow.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Search for potential Request object creation patterns
ast-grep --pattern 'new Request($_, $_)'
Length of output: 300
Script:
#!/bin/bash
# Let's check the context around these Request creations to understand their usage
rg -B 3 -A 3 "new Request\(" test/fetch.test.ts src/fetch.ts
Length of output: 862
commit: |
src/fetch.ts
Outdated
@@ -218,7 +218,7 @@ export class FetchFactory { | |||
} as any as RawResponseWithMeta; | |||
try { | |||
await FetchFactory.#opaqueLocalStorage.run(internalOpaque, async () => { | |||
res = await UndiciFetch(input, init); | |||
res = await UndiciFetch(request, init); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
init 还需要吗?init 已经传到 request 里了。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
确实应该不用了,我改改
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
修复了提交一下,然后就可以合并了。
node v16 的报错可以不管,这个我会修复 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
[skip ci] ## [4.6.7](v4.6.6...v4.6.7) (2024-12-11) ### Bug Fixes * should not throw when fetch a Request with post ([#563](#563)) ([6f9f353](6f9f353))
现象
使用 post method fetch 一个 Request 对象时会出现
Cannot construct a Request with a Request object that has already been used.
错误。原因
Request 的 constructor 会把入参的 request.body.stream.locked 变为 true,urllib 的 fetch 中已经使用参数 input new 了一个 Request,再调用 undici fetch 时参数还是 input,就会报错。
Summary by CodeRabbit
New Features
Request
object for improved request handling.Request
class.Chores
.gitignore
to exclude the.idea/
directory.