UNPKG

@tririga/tri-pull

Version:

A tool for pulling the UX view source files from the TRIRIGA server.

178 lines (163 loc) 5.17 kB
"use strict"; const http = require("http"); const https = require("https"); const url = require("url"); const TriWebSignon = require("./tri-web-signon.js"); const GET_VIEW_DATA_PATH = "/p/components/views/"; const VIEW_COMPONENTS_PATH = "/p/components/tri_view_components/"; class TriWebComponent { constructor(target) { this.target = target; this.getViewDataUrl = new url.URL(this.target + GET_VIEW_DATA_PATH); this.viewComponentsUrl = new url.URL(this.target + VIEW_COMPONENTS_PATH); this.protocol = this.getViewDataUrl.protocol === "https:" ? https : http; } getViewData(viewName, polymerVersion, webapp) { return new Promise((resolve, reject) => { if (!TriWebSignon.isUserAuthenticated()) { reject(new Error("No authenticated user.")); } const options = this.createGetViewDataRequestOptions( viewName, polymerVersion, webapp ); const req = this.protocol.request(options, (resp) => { let content = ""; resp.on("data", (d) => { content += d; }); resp.on("end", () => { switch (resp.statusCode) { case 401: reject(new Error("GetViewData operation was not authorized.")); break; case 404: reject( new Error( `There is no ${ webapp ? "Web Application" : "view" } with exposed name equals to ${viewName}${ !webapp ? ` and polymer version equals to ${polymerVersion}` : "" }.` ) ); break; case 200: resolve(JSON.parse(content)); break; default: reject( new Error( "Unknown error occurred while running GetViewData operation for the view: " + viewName ) ); break; } }); }); req.on("error", (e) => { reject( new Error( `Unknown error occurred while running GetViewData operation for the view: ${viewName}, Error: ${e.message}` ) ); }); req.end(); }); } getViewFile(viewFilePath, viewName, polymerVersion, webapp) { return new Promise((resolve, reject) => { if (!TriWebSignon.isUserAuthenticated()) { reject(new Error("No authenticated user.")); } const options = this.createGetViewFileRequestOptions( viewFilePath, viewName, polymerVersion, webapp ); const req = this.protocol.request(options, (resp) => { let content = ""; resp.on("data", (d) => { content += d; }); resp.on("end", () => { switch (resp.statusCode) { case 401: reject(new Error("GetViewFile operation was not authorized.")); break; case 404: reject( new Error( `There is no view file at this path ${viewName}/${viewFilePath}.` ) ); break; case 200: resolve(content); break; default: reject( new Error( `Unknown error occurred while running GetViewFile operation for the view file at this path: ${viewName}/${viewFilePath}` ) ); break; } }); }); req.on("error", (e) => { reject( new Error( `Unknown error occurred while running GetViewFile operation for the view file at this path: ${viewName}/${viewFilePath}` ) ); }); req.end(); }); } createGetViewDataRequestOptions(viewName, polymerVersion, webapp) { let path = `${this.getViewDataUrl.pathname}${viewName}`; path += webapp ? "?customApp=true" : `?polymerVersion=${polymerVersion}`; const options = { hostname: this.getViewDataUrl.hostname, port: this.getViewDataUrl.port, path, method: "GET", headers: { Cookie: TriWebSignon.getAuthenticatedUser().cookies, }, }; if (TriWebSignon.getAuthenticatedUser().basicAuth) { options.auth = TriWebSignon.getAuthenticatedUser().basicAuth; } return options; } createGetViewFileRequestOptions( viewFilePath, viewName, polymerVersion, webapp ) { let path = `${this.viewComponentsUrl.pathname}${viewName}${viewFilePath}`; path += webapp ? "?customApp=true" : `?polymerVersion=${polymerVersion}`; const options = { hostname: this.getViewDataUrl.hostname, port: this.getViewDataUrl.port, path, method: "GET", headers: { Cookie: TriWebSignon.getAuthenticatedUser().cookies, }, }; if (TriWebSignon.getAuthenticatedUser().basicAuth) { options.auth = TriWebSignon.getAuthenticatedUser().basicAuth; } return options; } } module.exports = TriWebComponent;