source: trip-planner-front/node_modules/webpack-dev-middleware/dist/middleware.js

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.3 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = wrapper;
7
8var _path = _interopRequireDefault(require("path"));
9
10var _mimeTypes = _interopRequireDefault(require("mime-types"));
11
12var _getFilenameFromUrl = _interopRequireDefault(require("./utils/getFilenameFromUrl"));
13
14var _handleRangeHeaders = _interopRequireDefault(require("./utils/handleRangeHeaders"));
15
16var _ready = _interopRequireDefault(require("./utils/ready"));
17
18function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
20function wrapper(context) {
21 return async function middleware(req, res, next) {
22 const acceptedMethods = context.options.methods || ["GET", "HEAD"]; // fixes #282. credit @cexoso. in certain edge situations res.locals is undefined.
23 // eslint-disable-next-line no-param-reassign
24
25 res.locals = res.locals || {};
26
27 if (!acceptedMethods.includes(req.method)) {
28 await goNext();
29 return;
30 }
31
32 (0, _ready.default)(context, processRequest, req);
33
34 async function goNext() {
35 if (!context.options.serverSideRender) {
36 return next();
37 }
38
39 return new Promise(resolve => {
40 (0, _ready.default)(context, () => {
41 // eslint-disable-next-line no-param-reassign
42 res.locals.webpack = {
43 devMiddleware: context
44 };
45 resolve(next());
46 }, req);
47 });
48 }
49
50 async function processRequest() {
51 const filename = (0, _getFilenameFromUrl.default)(context, req.url);
52 let {
53 headers
54 } = context.options;
55
56 if (typeof headers === "function") {
57 headers = headers(req, res, context);
58 }
59
60 let content;
61
62 if (!filename) {
63 await goNext();
64 return;
65 }
66
67 try {
68 content = context.outputFileSystem.readFileSync(filename);
69 } catch (_ignoreError) {
70 await goNext();
71 return;
72 }
73
74 const contentTypeHeader = res.get ? res.get("Content-Type") : res.getHeader("Content-Type");
75
76 if (!contentTypeHeader) {
77 // content-type name(like application/javascript; charset=utf-8) or false
78 const contentType = _mimeTypes.default.contentType(_path.default.extname(filename)); // Only set content-type header if media type is known
79 // https://tools.ietf.org/html/rfc7231#section-3.1.1.5
80
81
82 if (contentType) {
83 // Express API
84 if (res.set) {
85 res.set("Content-Type", contentType);
86 } // Node.js API
87 else {
88 res.setHeader("Content-Type", contentType);
89 }
90 }
91 }
92
93 if (headers) {
94 const names = Object.keys(headers);
95
96 for (const name of names) {
97 // Express API
98 if (res.set) {
99 res.set(name, headers[name]);
100 } // Node.js API
101 else {
102 res.setHeader(name, headers[name]);
103 }
104 }
105 } // Buffer
106
107
108 content = (0, _handleRangeHeaders.default)(context, content, req, res); // Express API
109
110 if (res.send) {
111 res.send(content);
112 } // Node.js API
113 else {
114 res.setHeader("Content-Length", content.length);
115
116 if (req.method === "HEAD") {
117 res.end();
118 } else {
119 res.end(content);
120 }
121 }
122 }
123 };
124}
Note: See TracBrowser for help on using the repository browser.