Skip to content

Commit

Permalink
fix: compatible with lower node versions
Browse files Browse the repository at this point in the history
  • Loading branch information
cwj0417 committed Dec 22, 2023
1 parent 53e8edf commit 30aa7ad
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
6 changes: 4 additions & 2 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2295,9 +2295,11 @@ class Server {

if (newProxyConfig !== proxyConfig) {
proxyConfig = newProxyConfig;
const server = (req.socket ?? req.connection)?.server;
const socket = req.socket != null ? req.socket : req.connection;
// @ts-ignore
const server = socket != null ? socket.server : null;
if (server) {
server.removeAllListeners('close');
server.removeAllListeners("close");

Check warning on line 2302 in lib/Server.js

View check run for this annotation

Codecov / codecov/patch

lib/Server.js#L2302

Added line #L2302 was not covered by tests
}
proxyMiddleware =
/** @type {RequestHandler} */
Expand Down
14 changes: 10 additions & 4 deletions test/server/proxy-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,16 @@ let maxServerListeners = 0;
const proxyOptionOfArray = [
{ context: "/proxy1", target: proxyOption.target },
function proxy(req, res, next) {
const server = (req?.socket ?? req?.connection)?.server;
if (server) {
maxServerListeners = Math.max(maxServerListeners, server.listeners('close').length)
if (req != null) {
const socket = req.socket != null ? req.socket : req.connection;
// @ts-ignore
const server = socket != null ? socket.server : null;
if (server) {
maxServerListeners = Math.max(
maxServerListeners,
server.listeners("close").length
);
}
}
return {
context: "/api/proxy2",
Expand Down Expand Up @@ -445,7 +452,6 @@ describe("proxy option", () => {
it("should not exist multiple close events registered", async () => {
expect(maxServerListeners).toBeLessThanOrEqual(1);
});

});

describe("as an array without the `route` option", () => {
Expand Down

0 comments on commit 30aa7ad

Please sign in to comment.