Skip to content

Commit

Permalink
[BREAKING] Transform to native ESM (#501)
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
This package has been transformed to native ESM. Therefore it no longer provides a CommonJS export.
If your project uses CommonJS, it needs to be converted to ESM or use a dynamic import.

For more information see also:
- https://sap.github.io/ui5-tooling/updates/migrate-v3/
- https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

Co-authored-by: Florian Vogt <florian.vogt@sap.com>
Co-authored-by: Yavor Ivanov <yavor.ivanov@sap.com>
  • Loading branch information
3 people authored Oct 24, 2022
1 parent 8edc10c commit 05e3013
Show file tree
Hide file tree
Showing 39 changed files with 2,037 additions and 2,807 deletions.
6 changes: 5 additions & 1 deletion .eslintrc.js → .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module.exports = {
"parserOptions": {
"sourceType": "module",
},
"env": {
"node": true,
"es2021": true
Expand Down Expand Up @@ -73,7 +76,8 @@ module.exports = {
"settings": {
"jsdoc": {
"tagNamePreference": {
"return": "returns"
"return": "returns",
"augments": "extends"
}
}
},
Expand Down
35 changes: 0 additions & 35 deletions index.js

This file was deleted.

9 changes: 9 additions & 0 deletions jsdoc-plugin.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* This plugin fixes unexpected JSDoc behavior that prevents us from using types that start with an at-sign (@).
* JSDoc doesn't see "{@" as a valid type expression, probably as there's also {@link ...}.
*/
exports.handlers = {
jsdocCommentFound: function(e) {
e.comment = e.comment.replace(/{@ui5\//g, "{ @ui5/");
}
};
13 changes: 0 additions & 13 deletions jsdoc-plugin.js

This file was deleted.

4 changes: 2 additions & 2 deletions jsdoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
"allowUnknownTags": false
},
"source": {
"include": ["README.md", "index.js"],
"include": ["README.md"],
"includePattern": ".+\\.js$",
"excludePattern": "(node_modules(\\\\|/))"
},
"plugins": [
"./jsdoc-plugin"
"./jsdoc-plugin.cjs"
],
"opts": {
"encoding": "utf8",
Expand Down
59 changes: 47 additions & 12 deletions lib/middleware/MiddlewareManager.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
const middlewareRepository = require("./middlewareRepository");
const MiddlewareUtil = require("./MiddlewareUtil");
import middlewareRepository from "./middlewareRepository.js";
import MiddlewareUtil from "./MiddlewareUtil.js";
const hasOwn = Function.prototype.call.bind(Object.prototype.hasOwnProperty);

/**
*
* @typedef {object} middlewareResources
* @property {module:@ui5/fs.AbstractReader} all Reader or Collection to read resources of the
* @private
* @typedef {object} MiddlewareResources
* @property {@ui5/fs.AbstractReader} all Reader or Collection to read resources of the
* root project and its dependencies
* @property {module:@ui5/fs.AbstractReader} rootProject Reader or Collection to read resources of
* @property {@ui5/fs.AbstractReader} rootProject Reader or Collection to read resources of
* the project the server is started in
* @memberof @ui5/server/internal/MiddlewareManager
*/

/**
* The MiddlewareManager
*
*
* @memberof module:@ui5/server.middleware
* @private
* @class
* @alias @ui5/server/internal/MiddlewareManager
*/
class MiddlewareManager {
constructor({graph, resources, options = {
Expand All @@ -33,6 +36,14 @@ class MiddlewareManager {
this.middlewareUtil = new MiddlewareUtil();
}

/**
* Applies the middleware to
*
* @private
* @param {object} app The express application object
* @returns {Promise<Array<undefined>>} Promise resolving to an Array with a length of the number
* of added middlewares. The entries of the Array have a value of <code>undefined</code>.
*/
async applyMiddleware(app) {
await this.addStandardMiddleware();
await this.addCustomMiddleware();
Expand All @@ -43,6 +54,18 @@ class MiddlewareManager {
});
}

/**
* Adds the given middleware configuration
*
* @private
* @param {string} configuredMiddlewareName The name of the middleware
* @param {object} [options] The Options of the middleware
* @param {object} [options.customMiddleware] The custom middleware
* @param {Function} [options.wrapperCallback] Callback called when middleware is called
* @param {string} [options.mountPath="/"] The path hosting the middleware
* @param {string} [options.beforeMiddleware] The name of the middleware called before the added middleware
* @param {string} [options.afterMiddleware] The name of the middleware called after the added middleware
*/
async addMiddleware(configuredMiddlewareName, {
customMiddleware, wrapperCallback, mountPath = "/",
beforeMiddleware, afterMiddleware
Expand All @@ -51,7 +74,7 @@ class MiddlewareManager {
if (customMiddleware) {
middlewareCallback = customMiddleware;
} else {
const middlewareInfo = middlewareRepository.getMiddleware(configuredMiddlewareName);
const middlewareInfo = await middlewareRepository.getMiddleware(configuredMiddlewareName);
if (wrapperCallback) {
middlewareCallback = wrapperCallback(middlewareInfo);
} else {
Expand Down Expand Up @@ -98,6 +121,12 @@ class MiddlewareManager {
};
}

/**
* Adds all registered standard middlewares
*
* @private
* @returns {Promise} Resolving to <code>undefined</code> once all standard middlewares are added
*/
async addStandardMiddleware() {
await this.addMiddleware("csp", {
wrapperCallback: ({middleware: cspModule}) => {
Expand Down Expand Up @@ -214,6 +243,12 @@ class MiddlewareManager {
});
}

/**
* Adds all registered custom middlewares
*
* @private
* @returns {Promise} Resolving to <code>undefined</code> once all custom middlewares are added
*/
async addCustomMiddleware() {
const project = this.graph.getRoot();
const projectCustomMiddleware = project.getCustomMiddleware();
Expand All @@ -238,7 +273,7 @@ class MiddlewareManager {
`defines neither a "beforeMiddleware" nor an "afterMiddleware" parameter. One must be defined.`);
}
await this.addMiddleware(middlewareDef.name, {
customMiddleware: ({resources, middlewareUtil}) => {
customMiddleware: async ({resources, middlewareUtil}) => {
const customMiddleware = this.graph.getExtension(middlewareDef.name);
const specVersion = customMiddleware.getSpecVersion();
const options = {
Expand All @@ -254,7 +289,7 @@ class MiddlewareManager {
// Supply interface to MiddlewareUtil instance starting with specVersion 2.0
params.middlewareUtil = middlewareUtil.getInterface(specVersion);
}
return customMiddleware.getMiddleware()(params);
return (await customMiddleware.getMiddleware())(params);
},
mountPath: middlewareDef.mountPath,
beforeMiddleware: middlewareDef.beforeMiddleware,
Expand All @@ -264,4 +299,4 @@ class MiddlewareManager {
}
}

module.exports = MiddlewareManager;
export default MiddlewareManager;
15 changes: 9 additions & 6 deletions lib/middleware/MiddlewareUtil.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import parseurl from "parseurl";
import mime from "mime-types";

/**
* Convenience functions for UI5 Server middleware.
* An instance of this class is passed to every standard UI5 Server middleware.
Expand All @@ -8,7 +11,9 @@
* version defined for the extension.
*
* @public
* @memberof module:@ui5/server.middleware
* @class
* @alias @ui5/server/middleware/MiddlewareUtil
* @hideconstructor
*/
class MiddlewareUtil {
/**
Expand Down Expand Up @@ -54,7 +59,6 @@ class MiddlewareUtil {
* @public
*/
getPathname(req) {
const parseurl = require("parseurl");
let {pathname} = parseurl(req);
pathname = decodeURIComponent(pathname);
return pathname;
Expand All @@ -75,7 +79,7 @@ class MiddlewareUtil {
* @property {string} type Detected content-type for the given resource path
* @property {string} charset Default charset for the detected content-type
* @property {string} contentType Calculated content-type header value
* @memberof module:@ui5/server.middleware.MiddlewareUtil
* @memberof @ui5/server/middleware/MiddlewareUtil
*/
/**
* Returns MIME information derived from a given resource path.
Expand All @@ -84,11 +88,10 @@ class MiddlewareUtil {
* <b>Specification Version 2.0 and above</b>.
*
* @param {object} resourcePath
* @returns {module:@ui5/server.middleware.MiddlewareUtil.MimeInfo}
* @returns {@ui5/server/middleware/MiddlewareUtil.MimeInfo}
* @public
*/
getMimeInfo(resourcePath) {
const mime = require("mime-types");
const type = mime.lookup(resourcePath) || "application/octet-stream";
const charset = mime.charset(type);
return {
Expand All @@ -99,4 +102,4 @@ class MiddlewareUtil {
}
}

module.exports = MiddlewareUtil;
export default MiddlewareUtil;
4 changes: 2 additions & 2 deletions lib/middleware/connectUi5Proxy.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const ui5connect = require("connect-openui5");
import ui5connect from "connect-openui5";

function createMiddleware() {
return ui5connect.proxy({
secure: false
});
}

module.exports = createMiddleware;
export default createMiddleware;
13 changes: 7 additions & 6 deletions lib/middleware/csp.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const parseurl = require("parseurl");
const Router = require("router");
const querystring = require("querystring");
import parseurl from "parseurl";
import Router from "router";
import querystring from "node:querystring";
import logger from "@ui5/logger";
import bodyParser from "body-parser";

const log = require("@ui5/logger").getLogger("server:middleware:csp");
const log = logger.getLogger("server:middleware:csp");

const HEADER_CONTENT_SECURITY_POLICY = "Content-Security-Policy";
const HEADER_CONTENT_SECURITY_POLICY_REPORT_ONLY = "Content-Security-Policy-Report-Only";
Expand Down Expand Up @@ -77,7 +79,6 @@ function createMiddleware(sCspUrlParameterName, oConfig) {
// .csplog
// body parser is required to parse csp-report in body (json)
if (serveCSPReports) {
const bodyParser = require("body-parser");
router.post("/.ui5/csp/report.csplog", bodyParser.json({type: "application/csp-report"}));
}
router.post("/.ui5/csp/report.csplog", function(req, res, next) {
Expand Down Expand Up @@ -196,4 +197,4 @@ function createMiddleware(sCspUrlParameterName, oConfig) {
return router;
}

module.exports = createMiddleware;
export default createMiddleware;
2 changes: 1 addition & 1 deletion lib/middleware/discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,4 @@ function createMiddleware({resources}) {
};
}

module.exports = createMiddleware;
export default createMiddleware;
47 changes: 34 additions & 13 deletions lib/middleware/middlewareRepository.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,55 @@
const middlewareInfos = {
compression: {path: "compression"},
cors: {path: "cors"},
csp: {path: "./csp"},
serveResources: {path: "./serveResources"},
serveIndex: {path: "./serveIndex"},
discovery: {path: "./discovery"},
versionInfo: {path: "./versionInfo"},
connectUi5Proxy: {path: "./connectUi5Proxy"},
serveThemes: {path: "./serveThemes"},
testRunner: {path: "./testRunner"},
nonReadRequests: {path: "./nonReadRequests"}
csp: {path: "./csp.js"},
serveResources: {path: "./serveResources.js"},
serveIndex: {path: "./serveIndex.js"},
discovery: {path: "./discovery.js"},
versionInfo: {path: "./versionInfo.js"},
connectUi5Proxy: {path: "./connectUi5Proxy.js"},
serveThemes: {path: "./serveThemes.js"},
testRunner: {path: "./testRunner.js"},
nonReadRequests: {path: "./nonReadRequests.js"}
};

function getMiddleware(middlewareName) {
// see @ui5/server/internal/middlewareRepository#getMiddleware
async function getMiddleware(middlewareName) {
const middlewareInfo = middlewareInfos[middlewareName];

if (!middlewareInfo) {
throw new Error(`middlewareRepository: Unknown Middleware ${middlewareName}`);
}
try {
const middleware = require(middlewareInfo.path);
const {default: middleware} = await import(middlewareInfo.path);
return {
middleware
};
} catch (err) {
throw new Error(
`middlewareRepository: Failed to require middleware module for ${middlewareName}: ${err.message}`);
`middlewareRepository: Failed to require middleware module for ${middlewareName}:\n${err.stack}`);
}
}
/**
* @private
* @typedef {object} module:@ui5/server/internal/middlewareRepository~Middleware
* @property {object} middleware The middleware
*/

module.exports = {
/**
* @private
* @module @ui5/server/internal/middlewareRepository
* @borrows getMiddleware as getMiddleware
*/
export default {

/**
* Determines the desired middleware
*
* @private
* @static
* @function
* @param {string} middlewareName The name of the middleware
* @returns {module:@ui5/server/internal/middlewareRepository~Middleware} The middleware
*/
getMiddleware: getMiddleware
};
2 changes: 1 addition & 1 deletion lib/middleware/nonReadRequests.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ function createMiddleware() {
};
}

module.exports = createMiddleware;
export default createMiddleware;
Loading

0 comments on commit 05e3013

Please sign in to comment.