MCPcopy
hub / github.com/webpack/webpack / createServer

Function createServer

test/configCases/asset-modules/http-url/server/index.js:11–82  ·  view source on GitHub ↗

* @returns {import("http").Server} server instance

()

Source from the content-addressed store, hash-verified

9 * @returns {import("http").Server} server instance
10 */
11function createServer() {
12 const server = http.createServer((req, res) => {
13 let file;
14 const url = /** @type {string} */ (req.url);
15 const query = url.includes("?") ? url.slice(url.indexOf("?") + 1) : "";
16 // simulate a dropped connection to exercise the fetch error path
17 if (query === "error") {
18 /** @type {import("net").Socket} */ (res.socket).destroy();
19 return;
20 }
21 // must-revalidate redirect with a stable etag, to exercise the unchanged-redirect path
22 if (query === "redirect") {
23 res.statusCode = 301;
24 res.setHeader("Location", "/resolve.js");
25 res.setHeader("ETag", '"stable-redirect"');
26 res.setHeader("Cache-Control", "public, must-revalidate");
27 res.end();
28 return;
29 }
30 const pathname = "." + url.replace(/\?.*$/, "");
31 if (url.endsWith("?no-cache")) {
32 res.setHeader("Cache-Control", "no-cache, max-age=60");
33 } else {
34 res.setHeader("Cache-Control", "public, immutable, max-age=600");
35 }
36 try {
37 file = fs
38 .readFileSync(path.resolve(__dirname, pathname))
39 .toString()
40 .replace(/\r\n?/g, "\n")
41 .trim();
42 } catch (e) {
43 if (fs.existsSync(path.resolve(__dirname, pathname + ".js"))) {
44 res.statusCode = 301;
45 res.setHeader("Location", pathname.slice(1) + ".js");
46 res.end();
47 return;
48 }
49 res.statusCode = 404;
50 res.end();
51 return;
52 }
53 res.setHeader(
54 "Content-Type",
55 pathname.endsWith(".js")
56 ? "text/javascript"
57 : pathname.endsWith("LICENSE")
58 ? "text/plain"
59 : "text/css"
60 );
61 // serve compressed responses to exercise the decompression branches
62 const encoding =
63 query === "gzip" || query === "br" || query === "deflate"
64 ? query
65 : undefined;
66 if (encoding) {
67 res.setHeader("Content-Encoding", encoding);
68 const buffer = Buffer.from(file);

Callers 2

applyMethod · 0.85

Calls 5

sliceMethod · 0.80
destroyMethod · 0.80
resolveMethod · 0.65
replaceMethod · 0.45
toStringMethod · 0.45

Tested by

no test coverage detected