UNPKG

@sap/adp-cf

Version:
152 lines 8.04 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const axios_1 = __importDefault(require("axios")); const adm_zip_1 = __importDefault(require("adm-zip")); const _1 = require("./"); const messages_1 = require("../i18n/messages"); class HTML5RepoUtils { /** * Get HTML5 repo credentials * @param {string} spaceGuid space guid * @param {CFUtil} cfUtil utility to communicate with CF * @returns {Promise<any>} credentials json object */ static getHtml5RepoCredentials(spaceGuid) { var _a, _b; return __awaiter(this, void 0, void 0, function* () { const INSTANCE_NAME = "html5-apps-repo-runtime"; try { let serviceKeys = yield _1.CFUtils.getServiceInstanceKeys({ spaceGuids: [spaceGuid], planNames: ["app-runtime"], names: [INSTANCE_NAME] }); if (serviceKeys === null || (serviceKeys === null || serviceKeys === void 0 ? void 0 : serviceKeys.credentials) === null || ((_a = serviceKeys === null || serviceKeys === void 0 ? void 0 : serviceKeys.credentials) === null || _a === void 0 ? void 0 : _a.length) === 0) { yield _1.CFUtils.createService(spaceGuid, "app-runtime", INSTANCE_NAME, ["html5-apps-repo-rt"]); serviceKeys = yield _1.CFUtils.getServiceInstanceKeys({ names: [INSTANCE_NAME] }); if (serviceKeys === null || (serviceKeys === null || serviceKeys === void 0 ? void 0 : serviceKeys.credentials) === null || ((_b = serviceKeys === null || serviceKeys === void 0 ? void 0 : serviceKeys.credentials) === null || _b === void 0 ? void 0 : _b.length) === 0) { throw new Error(messages_1.Messages.MISSING_HTML5_RUNTIME); } } return serviceKeys; } catch (error) { // log error: HTML5RepoUtils.ts=>getHtml5RepoCredentials(spaceGuid) throw new Error(messages_1.Messages.FAILED_TO_GET_HTML5_CREDENTIALS(spaceGuid, error.message)); } }); } /** * Download base app manifest.json and xs-app.json from HTML5 repository * @static * @param {*} spaceGuid current space guid * @param {IAppParams} parameters appName, appVersion, appHostId * @return {Promise<AdmZip.IZipEntry[]>} manifest.json and xs-app.json * @memberof HTML5RepoUtils */ static downloadAppContent(spaceGuid, parameters) { var _a; return __awaiter(this, void 0, void 0, function* () { const { appHostId, appName, appVersion } = parameters; const appNameVersion = `${appName}-${appVersion}`; try { const htmlRepoCredentials = yield this.getHtml5RepoCredentials(spaceGuid); if ((htmlRepoCredentials === null || htmlRepoCredentials === void 0 ? void 0 : htmlRepoCredentials.credentials) && (htmlRepoCredentials === null || htmlRepoCredentials === void 0 ? void 0 : htmlRepoCredentials.credentials.length) && ((_a = htmlRepoCredentials === null || htmlRepoCredentials === void 0 ? void 0 : htmlRepoCredentials.credentials[0]) === null || _a === void 0 ? void 0 : _a.uaa)) { const token = yield this.getToken(htmlRepoCredentials.credentials[0].uaa); const uri = `${htmlRepoCredentials.credentials[0].uri}/applications/content/${appNameVersion}?pathSuffixFilter=manifest.json,xs-app.json`; const zip = yield this.downloadZip(token, appHostId, uri); let admZip; try { admZip = new adm_zip_1.default(zip); } catch (error) { throw new Error(messages_1.Messages.FAILED_TO_PARSE_ZIP(error.message)); } if (!(admZip && admZip.getEntries().length)) { throw new Error(messages_1.Messages.NO_ZIP_CONTENT_PARSED); } const zipEntry = admZip.getEntries().find((zipEntry) => zipEntry.entryName === "manifest.json"); if (!zipEntry) { throw new Error(messages_1.Messages.FAILED_TO_FIND_MANIFEST_IN_APP); } try { const manifest = JSON.parse(zipEntry.getData().toString("utf8")); return { entries: admZip.getEntries(), serviceInstanceGuid: htmlRepoCredentials.serviceInstance.guid, manifest: manifest }; } catch (error) { throw new Error(messages_1.Messages.FAILED_TO_PARSE_MANIFEST_JSON); } } else { throw new Error(messages_1.Messages.NO_UAA_CREDENTIALS); } } catch (error) { // log error: HTML5RepoUtils.ts=>downloadAppContent(params) throw new Error(messages_1.Messages.FAILED_TO_DOWNLOAD_APP_CONTENT(spaceGuid, appHostId, appName, error.message)); } }); } /** * Download zip from HTML5 repository * @static * @param {string} token html5 reposiotry token * @param {string} appHostId appHostId where content is stored * @param {string} uri url with parameters * @return {Promise<Buffer>} file buffer content * @memberof HTML5RepoUtils */ static downloadZip(token, appHostId, uri) { return __awaiter(this, void 0, void 0, function* () { try { const response = yield axios_1.default.get(uri, { responseType: "arraybuffer", headers: { "Content-Type": "application/json", "Authorization": "Bearer " + token, "x-app-host-id": appHostId } }); return response.data; } catch (error) { // log error: HTML5RepoUtils.ts=>downloadZip(params) throw new Error(messages_1.Messages.FAILED_TO_DOWNLOAD_ZIP(error)); } }); } static getToken(uaa) { return __awaiter(this, void 0, void 0, function* () { const auth = Buffer.from(`${uaa.clientid}:${uaa.clientsecret}`); const options = { headers: { "Content-Type": "application/json", "Authorization": "Basic " + auth.toString("base64") } }; const uri = `${uaa.url}/oauth/token?grant_type=client_credentials`; try { const response = yield axios_1.default.get(uri, options); return response.data["access_token"]; } catch (error) { // log error: HTML5RepoUtils.ts=>getToken(params) throw new Error(messages_1.Messages.FAILED_TO_GET_OAUTH_TOKEN(error)); } }); } } exports.default = HTML5RepoUtils; //# sourceMappingURL=HTML5RepoUtils.js.map