Conversation
Summary of ChangesHello @ylxmf2005, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! 此拉取请求旨在解决跨域资源共享(CORS)预检请求在 Electron 和浏览器环境中遇到的认证和响应头处理问题。通过在路由层引入统一的 CORS 处理机制,确保 OPTIONS 请求能够正确响应,避免了不必要的 401 和 500 错误,同时不影响后续业务请求的鉴权和限流逻辑,从而提升了 API 的兼容性和稳定性。 Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
9ded55b to
6ebceb7
Compare
6ebceb7 to
9746478
Compare
问题背景
在 Obsidian(基于 Electron)或浏览器环境中调用
/v1beta/models/gemini-2.5-flash:streamGenerateContent?alt=sse时,客户端会先发送 OPTIONS 预检请求。由于预检请求不携带认证头,导致以下问题:headers不可变抛出异常解决方案
在路由层统一处理 CORS,避免预检请求进入业务守卫链。
新增
src/app/v1/_lib/cors.tsOrigin和Access-Control-Request-Headers204 No ContentAccess-Control-Expose-Headers暴露x-request-id、x-ratelimit-*等响应头append追加,避免覆盖已有值路由层集成
/v1和/v1beta路由统一调用registerCors(app)技术细节
为什么需要处理 OPTIONS?
Electron 应用和浏览器对跨域请求会自动发送 OPTIONS 预检。当请求携带自定义头(
Authorization、x-api-key、anthropic-version等)时触发。若预检被拒绝,实际业务请求不会发送。当前配置:
安全考虑
安全性未降低:
变更文件
src/app/v1/_lib/cors.tssrc/app/v1/[...route]/route.tssrc/app/v1beta/[...route]/route.ts验证
Checklist
devmain无直接冲突