Skip to content

Commit

Permalink
perf: remove headers option
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan committed Aug 20, 2024
1 parent d065b3e commit 0c13253
Show file tree
Hide file tree
Showing 5 changed files with 0 additions and 345 deletions.
65 changes: 0 additions & 65 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ See [below](#other-servers) for an example of use with fastify.
| Name | Type | Default | Description |
| :---------------------------------------------: | :---------------------------: | :-------------------------------------------: | :------------------------------------------------------------------------------------------------------------------- |
| **[`methods`](#methods)** | `Array` | `[ 'GET', 'HEAD' ]` | Allows to pass the list of HTTP request methods accepted by the middleware |
| **[`headers`](#headers)** | `Array\|Object\|Function` | `undefined` | Allows to pass custom HTTP headers on each request. |
| **[`index`](#index)** | `Boolean\|String` | `index.html` | If `false` (but not `undefined`), the server will not respond to requests to the root URL. |
| **[`etag`](#tag)** | `boolean\| "weak"\| "strong"` | `undefined` | Enable or disable etag generation. |
| **[`publicPath`](#publicpath)** | `String` | `output.publicPath` (from a configuration) | The public path that the middleware is bound to. |
Expand All @@ -81,70 +80,6 @@ Default: `[ 'GET', 'HEAD' ]`

This property allows a user to pass the list of HTTP request methods accepted by the middleware\*\*.

### headers

Type: `Array|Object|Function`
Default: `undefined`

This property allows a user to pass custom HTTP headers on each request.
eg. `{ "X-Custom-Header": "yes" }`

or

```js
webpackDevMiddleware(compiler, {
headers: () => {
return {
"Last-Modified": new Date(),
};
},
});
```

or

```js
webpackDevMiddleware(compiler, {
headers: (req, res, context) => {
res.setHeader("Last-Modified", new Date());
},
});
```

or

```js
webpackDevMiddleware(compiler, {
headers: [
{
key: "X-custom-header",
value: "foo",
},
{
key: "Y-custom-header",
value: "bar",
},
],
});
```

or

```js
webpackDevMiddleware(compiler, {
headers: () => [
{
key: "X-custom-header",
value: "foo",
},
{
key: "Y-custom-header",
value: "bar",
},
],
});
```

### index

Type: `Boolean|String`
Expand Down
1 change: 0 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ const noop = () => {};
* @typedef {Object} Options
* @property {boolean | ((targetPath: string) => boolean)} [writeToDisk]
* @property {string[]} [methods]
* @property {Headers<RequestInternal, ResponseInternal>} [headers]
* @property {NonNullable<Configuration["output"]>["publicPath"]} [publicPath]
* @property {Configuration["stats"]} [stats]
* @property {boolean} [serverSideRender]
Expand Down
27 changes: 0 additions & 27 deletions src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,33 +447,6 @@ function wrapper(context) {
let len = size;
let offset = 0;

// Send logic
let { headers } = context.options;

if (typeof headers === "function") {
headers = /** @type {NormalizedHeaders} */ (headers(req, res, context));
}

/**
* @type {{key: string, value: string | number}[]}
*/
const allHeaders = [];

if (typeof headers !== "undefined") {
if (!Array.isArray(headers)) {
// eslint-disable-next-line guard-for-in
for (const name in headers) {
allHeaders.push({ key: name, value: headers[name] });
}

headers = allHeaders;
}

headers.forEach((header) => {
res.setHeader(header.key, header.value);
});
}

if (!res.getHeader("Content-Type")) {
// content-type name(like application/javascript; charset=utf-8) or false
const contentType = mime.contentType(path.extname(filename));
Expand Down
250 changes: 0 additions & 250 deletions test/middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,25 +153,6 @@ function get404ContentTypeHeader(name) {
}
}

function applyTestMiddleware(name, middlewares) {
middlewares.push({
route: "/file.jpg",
fn: (req, res) => {
// Express API
if (res.send) {
res.send("welcome");
}
// Connect API
else {
res.setHeader("Content-Type", "text/html");
res.end("welcome");
}
},
});

return middlewares;
}

function parseHttpDate(date) {
const timestamp = date && Date.parse(date);

Expand Down Expand Up @@ -3519,237 +3500,6 @@ describe.each([
});
});

describe("headers option", () => {
describe("works with object", () => {
beforeEach(async () => {
const compiler = getCompiler(webpackConfig);

[server, req, instance] = await frameworkFactory(
name,
framework,
compiler,
{
headers: { "X-nonsense-1": "yes", "X-nonsense-2": "no" },
},
{
setupMiddlewares: (middlewares) => {
applyTestMiddleware(name, middlewares);

return middlewares;
},
},
);
});

afterEach(async () => {
await close(server, instance);
});

it('should return the "200" code for the "GET" request to the bundle file and return headers', async () => {
const response = await req.get(`/bundle.js`);

expect(response.statusCode).toEqual(200);
expect(response.headers["x-nonsense-1"]).toEqual("yes");
expect(response.headers["x-nonsense-2"]).toEqual("no");
});

it('should return the "200" code for the "GET" request to path not in outputFileSystem but not return headers', async () => {
const res = await req.get("/file.jpg");
expect(res.statusCode).toEqual(200);
expect(res.headers["X-nonsense-1"]).toBeUndefined();
expect(res.headers["X-nonsense-2"]).toBeUndefined();
});
});

describe("works with array of objects", () => {
beforeEach(async () => {
const compiler = getCompiler(webpackConfig);

[server, req, instance] = await frameworkFactory(
name,
framework,
compiler,
{
headers: [
{
key: "X-Foo",
value: "value1",
},
{
key: "X-Bar",
value: "value2",
},
],
},
{
setupMiddlewares: (middlewares) => {
applyTestMiddleware(name, middlewares);

return middlewares;
},
},
);
});

afterEach(async () => {
await close(server, instance);
});

it('should return the "200" code for the "GET" request to the bundle file and return headers', async () => {
const response = await req.get(`/bundle.js`);

expect(response.statusCode).toEqual(200);
expect(response.headers["x-foo"]).toEqual("value1");
expect(response.headers["x-bar"]).toEqual("value2");
});

it('should return the "200" code for the "GET" request to path not in outputFileSystem but not return headers', async () => {
const res = await req.get("/file.jpg");
expect(res.statusCode).toEqual(200);
expect(res.headers["x-foo"]).toBeUndefined();
expect(res.headers["x-bar"]).toBeUndefined();
});
});

describe("works with function", () => {
beforeEach(async () => {
const compiler = getCompiler(webpackConfig);

[server, req, instance] = await frameworkFactory(
name,
framework,
compiler,
{
headers: () => {
return { "X-nonsense-1": "yes", "X-nonsense-2": "no" };
},
},
{
setupMiddlewares: (middlewares) => {
applyTestMiddleware(name, middlewares);

return middlewares;
},
},
);
});

afterEach(async () => {
await close(server, instance);
});

it('should return the "200" code for the "GET" request to the bundle file and return headers', async () => {
const response = await req.get(`/bundle.js`);

expect(response.statusCode).toEqual(200);
expect(response.headers["x-nonsense-1"]).toEqual("yes");
expect(response.headers["x-nonsense-2"]).toEqual("no");
});

it('should return the "200" code for the "GET" request to path not in outputFileSystem but not return headers', async () => {
const res = await req.get("/file.jpg");
expect(res.statusCode).toEqual(200);
expect(res.headers["X-nonsense-1"]).toBeUndefined();
expect(res.headers["X-nonsense-2"]).toBeUndefined();
});
});

describe("works with function returning an array", () => {
beforeEach(async () => {
const compiler = getCompiler(webpackConfig);

[server, req, instance] = await frameworkFactory(
name,
framework,
compiler,
{
headers: () => [
{
key: "X-Foo",
value: "value1",
},
{
key: "X-Bar",
value: "value2",
},
],
},
{
setupMiddlewares: (middlewares) => {
applyTestMiddleware(name, middlewares);

return middlewares;
},
},
);
});

afterEach(async () => {
await close(server, instance);
});

it('should return the "200" code for the "GET" request to the bundle file and return headers', async () => {
const response = await req.get(`/bundle.js`);

expect(response.statusCode).toEqual(200);
expect(response.headers["x-foo"]).toEqual("value1");
expect(response.headers["x-bar"]).toEqual("value2");
});

it('should return the "200" code for the "GET" request to path not in outputFileSystem but not return headers', async () => {
const res = await req.get("/file.jpg");
expect(res.statusCode).toEqual(200);
expect(res.headers["x-foo"]).toBeUndefined();
expect(res.headers["x-bar"]).toBeUndefined();
});
});

describe("works with headers function with params", () => {
beforeEach(async () => {
const compiler = getCompiler(webpackConfig);

[server, req, instance] = await frameworkFactory(
name,
framework,
compiler,
{
// eslint-disable-next-line no-unused-vars, no-shadow
headers: (req, res, context) => {
res.setHeader("X-nonsense-1", "yes");
res.setHeader("X-nonsense-2", "no");
},
},
{
setupMiddlewares: (middlewares) => {
applyTestMiddleware(name, middlewares);

return middlewares;
},
},
);
});

afterEach(async () => {
await close(server, instance);
});

it('should return the "200" code for the "GET" request to the bundle file and return headers', async () => {
const response = await req.get(`/bundle.js`);

expect(response.statusCode).toEqual(200);
expect(response.headers["x-nonsense-1"]).toEqual("yes");
expect(response.headers["x-nonsense-2"]).toEqual("no");
});

it('should return the "200" code for the "GET" request to path not in outputFileSystem but not return headers', async () => {
const res = await req.get("/file.jpg");
expect(res.statusCode).toEqual(200);
expect(res.headers["X-nonsense-1"]).toBeUndefined();
expect(res.headers["X-nonsense-2"]).toBeUndefined();
});
});
});

describe("publicPath option", () => {
describe('should work with "string" value', () => {
beforeAll(async () => {
Expand Down
Loading

0 comments on commit 0c13253

Please sign in to comment.