UNPKG

@tririga/tri-bundler

Version:

A tool for bundling Polymer 3 TRIRIGA UX views.

156 lines (131 loc) 3.6 kB
"use strict"; const http = require("http"); const https = require("https"); const url = require("url"); function Tririga(options) { this.url = options.url.endsWith("/") ? options.url.slice(0, options.length - 2) : options.url; this.user = options.user; this.password = options.password; this.basicAuth = options.basicuser && options.basicpassword ? options.basicuser + ":" + options.basicpassword : null; this.loggedIn = false; this.cookies = null; var parsedUrl = url.parse(this.url); this.http = parsedUrl.protocol === "https:" ? https : http; } Tririga.prototype.login = function(callback) { callback = callback || function(error) {}; var parsedUrl = url.parse(this.url + "/p/websignon/signon"); var postData = JSON.stringify({ userName: this.user, password: this.password, normal: false, api: true }); var options = { hostname: parsedUrl.hostname, port: parsedUrl.port, path: parsedUrl.path, method: "POST", headers: { "Content-Type": "application/json", "Content-Length": Buffer.byteLength(postData) } }; if (this.basicAuth) { options.auth = this.basicAuth; } var req = this.http.request(options, (resp) => { var content = ""; resp.on("data", (d) => { content += d; }); resp.on("end", (d) => { switch (resp.statusCode) { case 401: callback(new Error("Invalid username or password.")); break; case 200: this.cookies = resp.headers["set-cookie"]; this.loggedIn = true; callback(null); break; default: callback(new Error("Unknown error occurred while trying to login.")); break; } }); }); req.write(postData); req.end(); }; Tririga.prototype.logout = function(callback) { callback = callback || function(error) {}; if (!this.loggedIn) { callback(null); return; } var parsedUrl = url.parse(this.url + "/p/websignon/signout"); var protocol = parsedUrl.protocol; var options = { hostname: parsedUrl.hostname, port: parsedUrl.port, path: parsedUrl.path, method: "POST", headers: { "Cookie": this.cookies[0] } }; if (this.basicAuth) { options.auth = this.basicAuth; } this.http.request(options, (resp) => { if (resp.statusCode === 200) { this.loggedIn = false; this.cookies = null; callback(null); } else { callback(new Error("Error while logging out")); } }).end(); }; Tririga.prototype.getComponentResource = function(componentPath, callback) { callback = callback || function(error) {}; if (!this.loggedIn) { callback(new Error("Cannot call getComponentResource() without being " + "logged in.")); return; } var parsedUrl = url.parse(this.url + "/p/components/r/3/v/en-US/l" + componentPath); var options = { hostname: parsedUrl.hostname, port: parsedUrl.port, path: parsedUrl.path, method: "GET", headers: { "Cookie": this.cookies[0] } }; if (this.basicAuth) { options.auth = this.basicAuth; } this.http.request(options, (resp) => { if (resp.statusCode != 200) { callback(new Error("Get resource did not result in a 200.")); return; } var componentSource = ""; resp.on("data", (d) => { if (Buffer.isBuffer(d)) { componentSource += d.toString(); } else { componentSource += d; } }); resp.on("end", () => { callback(null, componentSource); }); }).end(); }; module.exports = Tririga;