Skip to content

Refactor webpack-dev-server middleware application logic #5149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
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
15 changes: 15 additions & 0 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
39 changes: 39 additions & 0 deletions test/server/open-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
40 changes: 40 additions & 0 deletions test/server/proxy-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});