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
4 changes: 2 additions & 2 deletions packages/engine.io/lib/transports-uws/polling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ export class Polling extends Transport {
// remove the reference to the ServerResponse object (as the first request of the session is kept in memory by default)
req.res = null;

if (req.getMethod() === "get") {
if (req.method === "GET") {
this.onPollRequest(req, res);
} else if (req.getMethod() === "post") {
} else if (req.method === "POST") {
this.onDataRequest(req, res);
} else {
res.writeStatus("500 Internal Server Error");
Expand Down
35 changes: 21 additions & 14 deletions packages/engine.io/lib/userver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ export class uServer extends BaseServer {

req.res = res;

// the uWS.HttpRequest object cannot be accessed once the handler has returned (e.g. with an asynchronous middleware or allowRequest() function)
const secWebSocketKey = req.getHeader("sec-websocket-key");
const secWebSocketProtocol = req.getHeader("sec-websocket-protocol");
const secWebSocketExtensions = req.getHeader("sec-websocket-extensions");

const callback = async (errorCode, errorContext) => {
if (errorCode !== undefined) {
this.emit("connection_error", {
Expand Down Expand Up @@ -237,22 +242,24 @@ export class uServer extends BaseServer {

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

// calling writeStatus() triggers the flushing of any header added in a middleware
req.res.writeStatus("101 Switching Protocols");
res.cork(() => {
// 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]);
});
Object.keys(additionalHeaders).forEach((key) => {
req.res.writeHeader(key, additionalHeaders[key]);
});

res.upgrade(
{
transport,
},
req.getHeader("sec-websocket-key"),
req.getHeader("sec-websocket-protocol"),
req.getHeader("sec-websocket-extensions"),
context,
);
res.upgrade(
{
transport,
},
secWebSocketKey,
secWebSocketProtocol,
secWebSocketExtensions,
context,
);
});
};

this._applyMiddlewares(req, res, (err) => {
Expand Down
49 changes: 49 additions & 0 deletions packages/engine.io/test/middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,55 @@ describe("middlewares", () => {
});
});

it("should apply an asynchronous middleware (polling)", (done) => {
const engine = listen((port) => {
engine.use((req, res, next) => {
res.setHeader("foo", "bar");
setTimeout(next, 10);
});

request
.get(`http://localhost:${port}/engine.io/`)
.query({ EIO: 4, transport: "polling" })
.end((err, res) => {
expect(err).to.be(null);
expect(res.status).to.eql(200);
expect(res.headers["foo"]).to.eql("bar");

if (engine.httpServer) {
engine.httpServer.close();
}
done();
});
});
});

it("should apply an asynchronous middleware (websocket)", (done) => {
const engine = listen((port) => {
engine.use((req, res, next) => {
res.setHeader("foo", "bar");
setTimeout(next, 10);
});

const socket = new WebSocket(
`ws://localhost:${port}/engine.io/?EIO=4&transport=websocket`,
);

socket.on("upgrade", (res) => {
expect(res.headers["foo"]).to.eql("bar");

if (engine.httpServer) {
engine.httpServer.close();
}
done();
});

socket.on("open", () => {
socket.close();
});
});
});

it("should apply all middlewares in order", (done) => {
const engine = listen((port) => {
let count = 0;
Expand Down