@sap-ux/backend-proxy-middleware 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const dotenv_1 = __importDefault(require("dotenv"));
16
+ const logger_1 = require("@sap-ux/logger");
17
+ const express_1 = require("express");
18
+ const http_proxy_middleware_1 = require("http-proxy-middleware");
19
+ const proxy_1 = require("./base/proxy");
20
+ const bsp_1 = require("./ext/bsp");
21
+ /**
22
+ * Hides the proxy credentials for displaying the proxy configuration in the console.
23
+ *
24
+ * @param proxy - user's proxy server
25
+ * @returns proxy with hidden credentials for displaying in the console
26
+ */
27
+ function formatProxyForLogging(proxy) {
28
+ if (proxy) {
29
+ const forwardSlashIndex = proxy.indexOf('//');
30
+ const atIndex = proxy.indexOf('@');
31
+ if (forwardSlashIndex !== -1 && atIndex !== -1) {
32
+ proxy = proxy.replace(proxy.slice(forwardSlashIndex + 2, atIndex), '***:***');
33
+ }
34
+ }
35
+ return proxy || 'none';
36
+ }
37
+ /**
38
+ * UI5 middleware allowing to to proxy backends.
39
+ *
40
+ * @param params input parameters for UI5 middleware
41
+ * @param params.options configuration options
42
+ */
43
+ module.exports = ({ options }) => __awaiter(void 0, void 0, void 0, function* () {
44
+ var _a;
45
+ const logger = new logger_1.ToolsLogger({
46
+ transports: [new logger_1.UI5ToolingTransport({ moduleName: 'backend-proxy-middleware' })]
47
+ });
48
+ yield proxy_1.initI18n();
49
+ dotenv_1.default.config();
50
+ const router = express_1.Router();
51
+ const backend = options.configuration.backend;
52
+ const configOptions = (_a = options.configuration.options) !== null && _a !== void 0 ? _a : {};
53
+ configOptions.secure = configOptions.secure !== undefined ? !!configOptions.secure : true;
54
+ try {
55
+ const proxyOptions = yield proxy_1.generateProxyMiddlewareOptions(options.configuration.backend, configOptions, logger);
56
+ if (backend.bsp) {
57
+ bsp_1.addOptionsForEmbeddedBSP(backend.bsp, proxyOptions, logger);
58
+ }
59
+ router.use(backend.path, http_proxy_middleware_1.createProxyMiddleware(proxyOptions));
60
+ logger.info(`Starting backend-proxy-middleware using following configuration:\nbackend: ${JSON.stringify(Object.assign(Object.assign({}, backend), { proxy: formatProxyForLogging(backend.proxy) }))}\noptions: ${JSON.stringify(configOptions)}'`);
61
+ }
62
+ catch (e) {
63
+ const message = `Failed to register backend for ${backend.path}. Check configuration in yaml file. \n\t${e}`;
64
+ logger.error(message);
65
+ throw new Error(message);
66
+ }
67
+ return router;
68
+ });
69
+ //# sourceMappingURL=middleware.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"middleware.js","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,oDAA4B;AAC5B,2CAAkE;AAElE,qCAAiD;AACjD,iEAA8D;AAE9D,wCAAwE;AACxE,mCAAqD;AAErD;;;;;GAKG;AACH,SAAS,qBAAqB,CAAC,KAAyB;IACpD,IAAI,KAAK,EAAE;QACP,MAAM,iBAAiB,GAAG,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QAC9C,MAAM,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QAEnC,IAAI,iBAAiB,KAAK,CAAC,CAAC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AC,SAAS,iCACrF,OAAO,KACV,KAAK,EAAE,qBAAqB,CAAC,OAAO,CAAC,KAAK,CAAC,IAC7C,cAAc,IAAI,CAAC,SAAS,CAAC,aAAa,CAAC,GAAG,CACnD,CAAC;KACL;IAAC,OAAO,CAAC,EAAE;QACR,MAAM,OAAO,GAAG,kCAAkC,OAAO,CAAC,IAAI,2CAA2C,CAAC,EAAE,CAAC;QAC7G,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QACtB,MAAM,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC;KAC5B;IAED,OAAO,MAAM,CAAC;AAClB,CAAC,CAAA,CAAC"}
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "@sap-ux/backend-proxy-middleware",
3
+ "description": "SAP backend proxy middleware",
4
+ "repository": {
5
+ "type": "git",
6
+ "url": "https://github.com/SAP/open-ux-tools.git",
7
+ "directory": "packages/backend-proxy-middleware"
8
+ },
9
+ "bugs": {
10
+ "url": "https://github.com/SAP/open-ux-tools/issues?q=is%3Aopen+is%3Aissue+label%3Abug+label%3Abackend-proxy-middleware"
11
+ },
12
+ "version": "0.5.0",
13
+ "license": "Apache-2.0",
14
+ "main": "dist/index.js",
15
+ "files": [
16
+ "LICENSE",
17
+ "dist",
18
+ "ui5.yaml"
19
+ ],
20
+ "dependencies": {
21
+ "@sap-ux/axios-extension": "0.5.1",
22
+ "@sap-ux/btp-utils": "0.9.1",
23
+ "@sap-ux/logger": "0.1.5",
24
+ "@sap-ux/store": "0.1.4",
25
+ "chalk": "4.1.2",
26
+ "dotenv": "16.0.0",
27
+ "express": "4.17.2",
28
+ "http-proxy-middleware": "2.0.1",
29
+ "https-proxy-agent": "5.0.0",
30
+ "i18next": "20.3.2",
31
+ "prompts": "2.4.2"
32
+ },
33
+ "devDependencies": {
34
+ "@types/express": "4.17.13",
35
+ "@types/prompts": "2.0.14",
36
+ "nock": "13.2.4",
37
+ "supertest": "6.2.2",
38
+ "@types/supertest": "2.0.12",
39
+ "yaml": "2.0.0-10"
40
+ },
41
+ "ui5": {
42
+ "dependencies": []
43
+ },
44
+ "engines": {
45
+ "pnpm": ">=6.26.1",
46
+ "node": ">=12.22.5 < 13.0.0 || >= 14.16.0 < 15.0.0 || >=16.1.0 < 17.0.0"
47
+ },
48
+ "scripts": {
49
+ "build": "pnpm clean && tsc",
50
+ "clean": "rimraf dist",
51
+ "format": "prettier --write '**/*.{js,json,ts,yaml,yml}' --ignore-path ../../.prettierignore",
52
+ "lint": "eslint . --ext .ts",
53
+ "lint:fix": "eslint . --ext .ts --fix",
54
+ "test": "jest --ci --forceExit --detectOpenHandles --colors",
55
+ "watch": "tsc --watch"
56
+ },
57
+ "readme": "# `@sap-ux/backend-proxy-middleware`\n\nThe `@sap-ux/backend-proxy-middleware` is a [Custom UI5 Server Middleware](https://sap.github.io/ui5-tooling/pages/extensibility/CustomServerMiddleware) for proxying requests to backend servers. The middleware is agnostic to running in local environments and SAP Business Application Studio. Additionally, if credentials are maintained with the SAP Fiori tools then these will be used.\n\nIt can be used either with the `ui5 serve` or the `fiori run` commands.\n\n## Configuration Options\n\n### `backend`\n| Option | Type | Description |\n| ------------- | ------------- | ----------- |\n| `url` | `string` mandatory (local) | Mandatory URL pointing to the backend system. *Not required if destination is provided and the proxy is running SAP Business Application Studio |\n| `destination` | `string` mandatory (if no url) | Required if the backend system is available as destination in SAP Business Application Studio. |\n| `destinationInstance` | `string` optional | If a destination needs to be read by a specific instance of a destination service then you need to provide the id of the service as optional property `destinationInstance`.|\n| `path` | `string` mandatory | Path that is to be proxied |\n| `pathReplace` | `string` optional | If provided then the path will be replaced with this value before forwarding |\n| `client` | `string` optional | sap-client parameter |\n| `scp` | `boolean` optional | If set to true the proxy will execute the required OAuth routine for the ABAP environment on SAP BTP |\n| `apiHub` | `boolean` optional | If set to true then the proxy will connect to the SAP API Business Hub\n| `proxy` | `string` optional | If set then it will override the proxy settings from node.\n\nAdditional optional experimental property `bsp` (type `string`): The BSP property is only needed for the FLP Embedded Flow. The property refers to the BSP Application Name. In that case, we need to redirect the manifest.appdescr request to the local manifest.json in order to overwrite the deployed application with the local one.\n\n### `options`\nOptional object that can be used to directly set options of the used `http-proxy-middleware` modules (https://www.npmjs.com/package/http-proxy-middleware#http-proxy-middleware-options). Note: only declarative options (i.e. no functions) are supported.\n\n## Configuration Examples\n\n### [Connecting to a backend system](#connecting-to-a-backend-system)\n\nExecuting `ui5 serve` in your project with the configuration below in the `ui5.yaml` file would forward any request starting with the `path` parameter to the provided backend `url`.\n\n```\n- name: backend-proxy-middleware\n afterMiddleware: compression\n configuration:\n backend:\n path: /sap\n url: https://my.backend.example:1234\n```\n\n### [Connecting to a backend system with destination](#connecting-to-a-backend-system-with-destination)\n\nIf working in SAP Business Application Studio and the backend is configured as destination then you can also provide the `destination` in the configuration. \nIf a destination needs to be read by a specific instance of a destination service then you need to provide the id of the service as optional property `destinationInstance`.\n\n```\n- name: backend-proxy-middleware\n afterMiddleware: compression\n configuration:\n backend:\n path: /sap\n destination: my_example_destination\n```\n\n### [Connecting to a backend system with destination and principal propagation](#connecting-to-a-backend-system-with-destination-and-principal-propagation)\n\nIf the backend destination is configured to use principal propagation, then in some cases the requests might fail. If this occurs then you will need to set the optional property `xfwd` to `true`. This will add the x-forwared headers to the proxy requests.\n\n```\n- name: backend-proxy-middleware\n afterMiddleware: compression\n configuration:\n backend:\n path: /sap\n destination: my_example_destination\n options:\n xfwd: true\n```\n\n### [Connecting to the SAP Business Technology Platform](#connecting-to-the-sap-business-technology-platform)\nIf you want to connect to an ABAP Environment on SAP Business Technology Platform then you will need to set the optional property `scp` to `true`. For any other target, remove this property or set it to `false`.\n\n```\n- name: backend-proxy-middleware\n afterMiddleware: compression\n configuration:\n backend:\n path: /sap\n url: https://my.steampunk.example:1234\n scp: true\n```\n\n### [Connecting to the SAP API Business Hub](#connecting-to-the-sap-api-business-hub)\nIf you want to connect to the SAP API Business Hub then you will need to set the optional property `apiHub` to `true`, and set the corresponding `path` and `url`, e.g.\n\n```\n- name: backend-proxy-middleware\n afterMiddleware: compression\n configuration:\n backend:\n path: /s4hanacloud\n url: https://api.sap.com\n apiHub: true\n```\n\n### [Proxying WebSockets](#proxying-websockets)\nIf you want the proxy to handle also WebSockets, then you need to set the optional property `ws` to `true`, e.g.\n\n```\n- name: backend-proxy-middleware\n afterMiddleware: compression\n configuration:\n backend:\n path: /sap\n url: https://my.backend.example:1234\n options:\n ws: true\n```\n\n### [Changing the path to which a request is proxied](#changing-the-path-to-which-a-request-is-proxied)\nIf you want to configure the proxy to send requests from a certain path `/services/odata` to your backend (local url or destination) with a specified entry path `/my/entry/path`. Then you need to do the following:\n\n```\n- name: backend-proxy-middleware\n afterMiddleware: compression\n configuration:\n backend:\n - path: /services/odata\n pathReplace: /my/entry/path\n url: https://my.backend.example:1234\n destination: my_example_destination\n```\n\n### [Providing Proxy Configuration](#providing-proxy-configuration)\nBy default the `backend-proxy-middleware` will read the proxy configuration from the Node.js environment variables `proxy`, `https-proxy` and `noproxy`. If those variables are not set, then you can also provide the proxy configuration in the `ui5.yaml` file. \n**Please note: if you want to exclude any domains from the proxy then you will need to set the `noproxy` variable, e.g. `npm config set noproxy \"sap.com\"`**.\n\n```\n- name: backend-proxy-middleware\n afterMiddleware: compression\n configuration:\n proxy: https://myproxy.example:8443\n backend:\n - path: /sap\n url: https://my.backend.example:1234\n\n```\n## Programmatic Usage\nAlternatively you can only use the underlying proxy function, e.g. for the case when you want to use the `backend-proxy-middleware` functionality in your `express` server.\n\n```Typescript\nimport { createProxy } from '@sap-ux/backend-proxy-middleware';\nconst proxy = createProxy(backend, options);\n```\n- **config** - The backend configuration\n- **options** - Options of the [http-proxy-middleware](https://www.npmjs.com/package/http-proxy-middleware#options)\n\n## Keywords\n* Backend Proxy Middleware\n* Fiori tools\n* Fiori elements\n* SAP UI5"
58
+ }
package/ui5.yaml ADDED
@@ -0,0 +1,7 @@
1
+ specVersion: '2.6'
2
+ metadata:
3
+ name: backend-proxy-middleware
4
+ kind: extension
5
+ type: server-middleware
6
+ middleware:
7
+ path: dist/middleware.js