UNPKG

@ui5/task-adaptation

Version:

Custom task for ui5-builder which allows building UI5 Flexibility Adaptation Projects for SAP BTP, Cloud Foundry environment

87 lines 3.74 kB
import CFUtil from "./../util/cfUtil.js"; import RequestUtil from "./../util/requestUtil.js"; import { getLogger } from "@ui5/logger"; import { unzipZipEntries } from "./../util/zipUtil.js"; const log = getLogger("@ui5/task-adaptation::HTML5RepoManager"); export default class HTML5RepoManager { static async getBaseAppFiles(configuration) { const { token, baseUri } = await this.getHtml5RepoInfo(configuration); return this.getBaseAppZipEntries(configuration, baseUri, token); } static async getMetadata(configuration) { const { token, baseUri } = await this.getHtml5RepoInfo(configuration); return this.requestMetadata(configuration, baseUri, token); } static async getHtml5RepoInfo(configuration) { const spaceGuid = await CFUtil.getSpaceGuid(configuration?.spaceGuid); const credentials = await this.getHTML5Credentials(spaceGuid); const token = await this.getToken(credentials); return { token, baseUri: credentials.uri }; } static async getHTML5Credentials(spaceGuid) { log.verbose("Getting HTML5 Repo Runtime credentials from space " + spaceGuid); const PLAN_NAME = "app-runtime"; const SERVIСE_INSTANCE_NAME = "html5-apps-repo-runtime"; const getParams = { spaceGuids: [spaceGuid], planNames: [PLAN_NAME], names: [SERVIСE_INSTANCE_NAME] }; const createParams = { spaceGuid, planName: PLAN_NAME, serviceName: SERVIСE_INSTANCE_NAME, tags: ["html5-apps-repo-rt"] }; const serviceKeys = await CFUtil.getServiceInstanceKeys(getParams, createParams); return serviceKeys.credentials; } static async getToken({ uaa }) { log.verbose("Getting HTML5 Repo token"); 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`; return RequestUtil.get(uri, options).then((json) => json["access_token"]); } static async requestMetadata(options, html5RepoBaseUri, token) { const { appHostId, appName, appVersion } = options; const uri = `${html5RepoBaseUri}/applications/metadata/`; const requestOptions = { headers: { "Content-Type": "application/json", "Authorization": "Bearer " + token, "x-app-host-id": appHostId } }; const metadata = await RequestUtil.get(uri, requestOptions); return metadata.find((item) => item.appHostId === appHostId && item.applicationName === appName && item.applicationVersion === appVersion); } static async getBaseAppZipEntries(options, html5RepoBaseUri, token) { const { appHostId, appName, appVersion } = options; const uri = `${html5RepoBaseUri}/applications/content/${appName}-${appVersion}/`; const zip = await this.download(token, appHostId, uri); return unzipZipEntries(zip); } static async download(token, appHostId, uri) { if (!token) { throw new Error("HTML5 token is undefined"); } return RequestUtil.get(uri, { responseType: "arraybuffer", headers: { "Content-Type": "application/json", "Authorization": "Bearer " + token, "x-app-host-id": appHostId } }); } } //# sourceMappingURL=html5RepoManager.js.map