Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/engine.io/lib/userver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,23 @@ export class uServer extends BaseServer {
}
}

// emit headers events for WebSocket upgrades
const additionalHeaders = {};
const isInitialRequest = !id;

if (isInitialRequest) {
this.emit("initial_headers", additionalHeaders, req);
}

this.emit("headers", additionalHeaders, req);

// calling writeStatus() triggers the flushing of any header added in a middleware
req.res.writeStatus("101 Switching Protocols");

Object.keys(additionalHeaders).forEach((key) => {
req.res.writeHeader(key, additionalHeaders[key]);
});

res.upgrade(
{
transport,
Expand Down
10 changes: 2 additions & 8 deletions packages/engine.io/test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3598,10 +3598,7 @@ describe("server", () => {
});

it("should emit a 'initial_headers' event (websocket)", function (done) {
if (
process.env.EIO_WS_ENGINE === "eiows" ||
process.env.EIO_WS_ENGINE === "uws"
) {
if (process.env.EIO_WS_ENGINE === "eiows") {
return this.skip();
}
const partialDone = createPartialDone(done, 2);
Expand Down Expand Up @@ -3644,10 +3641,7 @@ describe("server", () => {
});

it("should emit several 'headers' events per connection", function (done) {
if (
process.env.EIO_WS_ENGINE === "eiows" ||
process.env.EIO_WS_ENGINE === "uws"
) {
if (process.env.EIO_WS_ENGINE === "eiows") {
return this.skip();
}
const partialDone = createPartialDone(done, 4);
Expand Down