server/lib/middleware/MiddlewareUtil.js

  1. /**
  2. * Convenience functions for UI5 Server middleware.
  3. * An instance of this class is passed to every standard UI5 Server middleware.
  4. * Custom middleware that define a specification version >= 2.0 will also receive an instance
  5. * of this class as part of the parameters of their create-middleware function.
  6. *
  7. * The set of functions that can be accessed by a custom middleware depends on the specification
  8. * version defined for the extension.
  9. *
  10. * @public
  11. * @memberof module:@ui5/server.middleware
  12. */
  13. class MiddlewareUtil {
  14. /**
  15. * Get an interface to an instance of this class that only provides those functions
  16. * that are supported by the given custom middleware extension specification version.
  17. *
  18. * @param {string} specVersion Specification Version of custom middleware extension
  19. * @returns {object} An object with bound instance methods supported by the given specification version
  20. */
  21. getInterface(specVersion) {
  22. const baseInterface = {
  23. getPathname: this.getPathname.bind(this),
  24. getMimeInfo: this.getMimeInfo.bind(this)
  25. };
  26. switch (specVersion) {
  27. case "0.1":
  28. case "1.0":
  29. case "1.1":
  30. return undefined;
  31. case "2.0":
  32. case "2.1":
  33. case "2.2":
  34. case "2.3":
  35. case "2.4":
  36. case "2.5":
  37. case "2.6":
  38. return baseInterface;
  39. default:
  40. throw new Error(`MiddlewareUtil: Unknown or unsupported Specification Version ${specVersion}`);
  41. }
  42. }
  43. /**
  44. * Returns the [pathname]{@link https://developer.mozilla.org/en-US/docs/Web/API/URL/pathname}
  45. * of a given request. Any escape sequences will be decoded.
  46. * </br></br>
  47. * This method is only available to custom middleware extensions defining
  48. * <b>Specification Version 2.0 and above</b>.
  49. *
  50. * @param {object} req Request object
  51. * @returns {string} [Pathname]{@link https://developer.mozilla.org/en-US/docs/Web/API/URL/pathname}
  52. * of the given request
  53. * @public
  54. */
  55. getPathname(req) {
  56. const parseurl = require("parseurl");
  57. let {pathname} = parseurl(req);
  58. pathname = decodeURIComponent(pathname);
  59. return pathname;
  60. }
  61. /**
  62. * MIME Info
  63. *
  64. * @example
  65. * const mimeInfo = {
  66. * "type": "text/html",
  67. * "charset": "utf-8",
  68. * "contentType": "text/html; charset=utf-8"
  69. * };
  70. *
  71. * @public
  72. * @typedef {object} MimeInfo
  73. * @property {string} type Detected content-type for the given resource path
  74. * @property {string} charset Default charset for the detected content-type
  75. * @property {string} contentType Calculated content-type header value
  76. * @memberof module:@ui5/server.middleware.MiddlewareUtil
  77. */
  78. /**
  79. * Returns MIME information derived from a given resource path.
  80. * </br></br>
  81. * This method is only available to custom middleware extensions defining
  82. * <b>Specification Version 2.0 and above</b>.
  83. *
  84. * @param {object} resourcePath
  85. * @returns {module:@ui5/server.middleware.MiddlewareUtil.MimeInfo}
  86. * @public
  87. */
  88. getMimeInfo(resourcePath) {
  89. const mime = require("mime-types");
  90. const type = mime.lookup(resourcePath) || "application/octet-stream";
  91. const charset = mime.charset(type);
  92. return {
  93. type,
  94. charset,
  95. contentType: type + (charset ? "; charset=" + charset : "")
  96. };
  97. }
  98. }
  99. module.exports = MiddlewareUtil;