diff --git a/lib/Server.js b/lib/Server.js index 5c8632ef34..65ca0a0e3f 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -552,6 +552,21 @@ class Server { searchParams.set("password", webSocketURL.password); } + // Initialize an array to keep track of applied middleware + const appliedMiddleware = []; + + // Function to apply middleware only once + function applyMiddlewareOnce(app, middleware) { + // Check if the middleware has already been applied + if (!appliedMiddleware.includes(middleware)) { + // Apply the middleware + app.use(middleware); + + // Add the middleware to the applied middleware array + appliedMiddleware.push(middleware); + } + } + /** @type {string} */ let hostname; diff --git a/test/server/open-option.test.js b/test/server/open-option.test.js index c31d8c0372..9288f4a9a0 100644 --- a/test/server/open-option.test.js +++ b/test/server/open-option.test.js @@ -959,3 +959,42 @@ describe('"open" option', () => { loggerWarnSpy.mockRestore(); }); }); + +// applyMiddlewareOnce-test + +const { applyMiddlewareOnce } = require('../../lib/Server'); + +describe('applyMiddlewareOnce', () => { + let app; + + beforeEach(() => { + // Mock Express app + app = { + use: jest.fn() + }; + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + it('should apply middleware only once', () => { + const middlewareFunction = jest.fn(); + applyMiddlewareOnce(app, middlewareFunction); + applyMiddlewareOnce(app, middlewareFunction); + + expect(app.use).toHaveBeenCalledTimes(1); + expect(app.use).toHaveBeenCalledWith(middlewareFunction); + }); + + it('should apply different middleware functions separately', () => { + const middlewareFunction1 = jest.fn(); + const middlewareFunction2 = jest.fn(); + applyMiddlewareOnce(app, middlewareFunction1); + applyMiddlewareOnce(app, middlewareFunction2); + + expect(app.use).toHaveBeenCalledTimes(2); + expect(app.use).toHaveBeenCalledWith(middlewareFunction1); + expect(app.use).toHaveBeenCalledWith(middlewareFunction2); + }); +}); diff --git a/test/server/proxy-option.test.js b/test/server/proxy-option.test.js index d894fdae08..f220091242 100644 --- a/test/server/proxy-option.test.js +++ b/test/server/proxy-option.test.js @@ -1035,3 +1035,43 @@ describe("proxy option", () => { }); }); }); + + +// applyMiddlewareOnce-test + +const { applyMiddlewareOnce } = require('../../lib/Server'); + +describe('applyMiddlewareOnce', () => { + let app; + + beforeEach(() => { + // Mock Express app + app = { + use: jest.fn() + }; + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + it('should apply middleware only once', () => { + const middlewareFunction = jest.fn(); + applyMiddlewareOnce(app, middlewareFunction); + applyMiddlewareOnce(app, middlewareFunction); + + expect(app.use).toHaveBeenCalledTimes(1); + expect(app.use).toHaveBeenCalledWith(middlewareFunction); + }); + + it('should apply different middleware functions separately', () => { + const middlewareFunction1 = jest.fn(); + const middlewareFunction2 = jest.fn(); + applyMiddlewareOnce(app, middlewareFunction1); + applyMiddlewareOnce(app, middlewareFunction2); + + expect(app.use).toHaveBeenCalledTimes(2); + expect(app.use).toHaveBeenCalledWith(middlewareFunction1); + expect(app.use).toHaveBeenCalledWith(middlewareFunction2); + }); +});