Skip to content
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

fix: types #5057

Merged
merged 1 commit into from Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
162 changes: 70 additions & 92 deletions lib/Server.js
Expand Up @@ -18,8 +18,6 @@
/** @typedef {import("webpack").Stats} Stats */
/** @typedef {import("webpack").MultiStats} MultiStats */
/** @typedef {import("os").NetworkInterfaceInfo} NetworkInterfaceInfo */
/** @typedef {import("express").Request} Request */
/** @typedef {import("express").Response} Response */
/** @typedef {import("express").NextFunction} NextFunction */
/** @typedef {import("express").RequestHandler} ExpressRequestHandler */
/** @typedef {import("express").ErrorRequestHandler} ExpressErrorRequestHandler */
Expand All @@ -42,14 +40,19 @@

/** @typedef {import("https").ServerOptions & { spdy?: { plain?: boolean | undefined, ssl?: boolean | undefined, 'x-forwarded-for'?: string | undefined, protocol?: string | undefined, protocols?: string[] | undefined }}} ServerOptions */

/** @typedef {import("express").Request} Request */
/** @typedef {import("express").Response} Response */

/**
* @template Request, Response
* @typedef {import("webpack-dev-middleware").Options<IncomingMessage, ServerResponse>} DevMiddlewareOptions
* @template {Request} T
* @template {Response} U
* @typedef {import("webpack-dev-middleware").Options<T, U>} DevMiddlewareOptions
*/

/**
* @template Request, Response
* @typedef {import("webpack-dev-middleware").Context<IncomingMessage, ServerResponse>} DevMiddlewareContext
* @template {Request} T
* @template {Response} U
* @typedef {import("webpack-dev-middleware").Context<T, U>} DevMiddlewareContext
*/

/**
Expand Down Expand Up @@ -1841,36 +1844,20 @@
const { app, middleware } = this;

/** @type {import("express").Application} */
(app).get(
"/__webpack_dev_server__/sockjs.bundle.js",
/**
* @param {Request} req
* @param {Response} res
* @returns {void}
*/
(req, res) => {
res.setHeader("Content-Type", "application/javascript");
(app).get("/__webpack_dev_server__/sockjs.bundle.js", (req, res) => {
res.setHeader("Content-Type", "application/javascript");

Check warning on line 1848 in lib/Server.js

View check run for this annotation

Codecov / codecov/patch

lib/Server.js#L1848

Added line #L1848 was not covered by tests

const clientPath = path.join(__dirname, "..", "client");
const clientPath = path.join(__dirname, "..", "client");

Check warning on line 1850 in lib/Server.js

View check run for this annotation

Codecov / codecov/patch

lib/Server.js#L1850

Added line #L1850 was not covered by tests

res.sendFile(path.join(clientPath, "modules/sockjs-client/index.js"));
},
);
res.sendFile(path.join(clientPath, "modules/sockjs-client/index.js"));

Check warning on line 1852 in lib/Server.js

View check run for this annotation

Codecov / codecov/patch

lib/Server.js#L1852

Added line #L1852 was not covered by tests
});

/** @type {import("express").Application} */
(app).get(
"/webpack-dev-server/invalidate",
/**
* @param {Request} _req
* @param {Response} res
* @returns {void}
*/
(_req, res) => {
this.invalidate();
(app).get("/webpack-dev-server/invalidate", (_req, res) => {
this.invalidate();

Check warning on line 1857 in lib/Server.js

View check run for this annotation

Codecov / codecov/patch

lib/Server.js#L1857

Added line #L1857 was not covered by tests

res.end();
},
);
res.end();

Check warning on line 1859 in lib/Server.js

View check run for this annotation

Codecov / codecov/patch

lib/Server.js#L1859

Added line #L1859 was not covered by tests
});

/** @type {import("express").Application} */
(app).get("/webpack-dev-server/open-editor", (req, res) => {
Expand All @@ -1886,74 +1873,65 @@
});

/** @type {import("express").Application} */
(app).get(
"/webpack-dev-server",
/**
* @param {Request} req
* @param {Response} res
* @returns {void}
*/
(req, res) => {
/** @type {import("webpack-dev-middleware").API<Request, Response>}*/
(middleware).waitUntilValid((stats) => {
res.setHeader("Content-Type", "text/html");
// HEAD requests should not return body content
if (req.method === "HEAD") {
res.end();
return;
}
res.write(
'<!DOCTYPE html><html><head><meta charset="utf-8"/></head><body>',
);
(app).get("/webpack-dev-server", (req, res) => {
/** @type {import("webpack-dev-middleware").API<Request, Response>}*/
(middleware).waitUntilValid((stats) => {
res.setHeader("Content-Type", "text/html");

Check warning on line 1879 in lib/Server.js

View check run for this annotation

Codecov / codecov/patch

lib/Server.js#L1878-L1879

Added lines #L1878 - L1879 were not covered by tests
// HEAD requests should not return body content
if (req.method === "HEAD") {
res.end();
return;

Check warning on line 1883 in lib/Server.js

View check run for this annotation

Codecov / codecov/patch

lib/Server.js#L1882-L1883

Added lines #L1882 - L1883 were not covered by tests
}
res.write(

Check warning on line 1885 in lib/Server.js

View check run for this annotation

Codecov / codecov/patch

lib/Server.js#L1885

Added line #L1885 was not covered by tests
'<!DOCTYPE html><html><head><meta charset="utf-8"/></head><body>',
);

const statsForPrint =
typeof (/** @type {MultiStats} */ (stats).stats) !== "undefined"
? /** @type {MultiStats} */ (stats).toJson().children
: [/** @type {Stats} */ (stats).toJson()];
const statsForPrint =
typeof (/** @type {MultiStats} */ (stats).stats) !== "undefined"
? /** @type {MultiStats} */ (stats).toJson().children
: [/** @type {Stats} */ (stats).toJson()];

Check warning on line 1892 in lib/Server.js

View check run for this annotation

Codecov / codecov/patch

lib/Server.js#L1891-L1892

Added lines #L1891 - L1892 were not covered by tests

res.write(`<h1>Assets Report:</h1>`);
res.write(`<h1>Assets Report:</h1>`);

Check warning on line 1894 in lib/Server.js

View check run for this annotation

Codecov / codecov/patch

lib/Server.js#L1894

Added line #L1894 was not covered by tests

/**
* @type {StatsCompilation[]}
*/
(statsForPrint).forEach((item, index) => {
res.write("<div>");

const name =
// eslint-disable-next-line no-nested-ternary
typeof item.name !== "undefined"
? item.name
: /** @type {MultiStats} */ (stats).stats
? `unnamed[${index}]`
: "unnamed";

res.write(`<h2>Compilation: ${name}</h2>`);
res.write("<ul>");

const publicPath =
item.publicPath === "auto" ? "" : item.publicPath;

for (const asset of /** @type {NonNullable<StatsCompilation["assets"]>} */ (
item.assets
)) {
const assetName = asset.name;
const assetURL = `${publicPath}${assetName}`;

res.write(
`<li>
/**
* @type {StatsCompilation[]}
*/
(statsForPrint).forEach((item, index) => {
res.write("<div>");

Check warning on line 1900 in lib/Server.js

View check run for this annotation

Codecov / codecov/patch

lib/Server.js#L1899-L1900

Added lines #L1899 - L1900 were not covered by tests

const name =
// eslint-disable-next-line no-nested-ternary
typeof item.name !== "undefined"
? item.name
: /** @type {MultiStats} */ (stats).stats
? `unnamed[${index}]`
: "unnamed";

Check warning on line 1908 in lib/Server.js

View check run for this annotation

Codecov / codecov/patch

lib/Server.js#L1905-L1908

Added lines #L1905 - L1908 were not covered by tests

res.write(`<h2>Compilation: ${name}</h2>`);
res.write("<ul>");

Check warning on line 1911 in lib/Server.js

View check run for this annotation

Codecov / codecov/patch

lib/Server.js#L1910-L1911

Added lines #L1910 - L1911 were not covered by tests

const publicPath = item.publicPath === "auto" ? "" : item.publicPath;

for (const asset of /** @type {NonNullable<StatsCompilation["assets"]>} */ (

Check warning on line 1915 in lib/Server.js

View check run for this annotation

Codecov / codecov/patch

lib/Server.js#L1915

Added line #L1915 was not covered by tests
item.assets
)) {
const assetName = asset.name;
const assetURL = `${publicPath}${assetName}`;

Check warning on line 1919 in lib/Server.js

View check run for this annotation

Codecov / codecov/patch

lib/Server.js#L1918-L1919

Added lines #L1918 - L1919 were not covered by tests

res.write(

Check warning on line 1921 in lib/Server.js

View check run for this annotation

Codecov / codecov/patch

lib/Server.js#L1921

Added line #L1921 was not covered by tests
`<li>
<strong><a href="${assetURL}" target="_blank">${assetName}</a></strong>
</li>`,
);
}

res.write("</ul>");
res.write("</div>");
});
);
}

res.end("</body></html>");
res.write("</ul>");
res.write("</div>");

Check warning on line 1929 in lib/Server.js

View check run for this annotation

Codecov / codecov/patch

lib/Server.js#L1928-L1929

Added lines #L1928 - L1929 were not covered by tests
});
},
);

res.end("</body></html>");

Check warning on line 1932 in lib/Server.js

View check run for this annotation

Codecov / codecov/patch

lib/Server.js#L1932

Added line #L1932 was not covered by tests
});
});
}

/**
Expand Down