Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Can't set response headers in Server.handleUpgrade

未关闭 适合新手
#5,497 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 2 天内回复

@MsfPablo 已经在做这个了。

开始于 2026年8月13日。

  • #5534 来自 @MsfPablo —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
72/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
冷清
技术栈
nodejs, typescript
领域
api, backend

调研方向

从 packages/engine.io/lib/server.ts 中唯一的 kResponseHeaders 定义以及 713–732 行附近 Server.handleUpgrade 对 response header 的处理开始。将其与 engine.io README 中显式请求的用法进行比较;完成标准是用户代码可以提供额外的 upgrade response header,并且文档中的 CORS 示例无需 cors middleware 即可正常工作。

由索引模型根据 Issue 内容生成。

描述

question

Describe the bug
It's not possible to specify headers for an upgrade response that is handled by Server.handleUpgrade.

To Reproduce
Follow the usage example where requests are passed in explicitly:
https://github.com/socketio/socket.io/blob/439a8f669c6788b512dd76011c4476830d67515b/packages/engine.io/README.md?plain=1#L40-L58

Using this example it's not possible to specify the upgrade response headers. Specifically, it's not possible to set the CORS headers without using the .cors "middleware" option. Since https://github.com/socketio/socket.io/commit/d1f5aa93722a7f1ed729b96f771daf92a3dfdaf7 using the .cors "middleware" option breaks webtransport, so the only way to make it work is to remove the .cors option and set them up manually.

Expected behavior
It should be possible to set any headers for the upgrade response that is sent out when upgrade is handled by engine.io.

Engine.IO currently uses the kResponseHeaders key on the request object to specify additional headers to send in the upgrade response:
https://github.com/socketio/socket.io/blob/439a8f669c6788b512dd76011c4476830d67515b/packages/engine.io/lib/server.ts#L713-L732

I expected it to be possible to set them up from my code in a similar way:

const kResponseHeaders = Symbol.for("responseHeaders");

server.on("upgrade", (req, socket, head) => {
    if (req.url?.startsWith("/engine.io/")) {
        req[kResponseHeaders] = {
            "Access-Control-Allow-Origin": req.headers["origin"] || "*",
            "Access-Control-Allow-Credentials": "true"
        };
        return eio.handleUpgrade(req, socket, head);
    }
    // ...
});

But it is currently defined as a unique Symbol, which prevents above code from working:
https://github.com/socketio/socket.io/blob/439a8f669c6788b512dd76011c4476830d67515b/packages/engine.io/lib/server.ts#L27

The new definition can use Symbol.for instead to allow user code to specify extra headers for the websocket upgrade response.

主要语言
TypeScript
星标
63.2k
派生
10.3k
平均合并
3 小时 24 分钟
30 天内合并 PR
1

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

socketio/socket.io 的其他 Issue

查看 socketio/socket.io 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。