middleware.js 3.3 KB

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