UNPKG

@donmahallem/trapeze-client-desktop

Version:

An electron app to be used with trapeze endpoints

99 lines 3.42 kB
"use strict"; /*! * Source https://github.com/donmahallem/trapeze */ Object.defineProperty(exports, "__esModule", { value: true }); const crypto = require("crypto"); const electron_1 = require("electron"); const api_server_1 = require("./api-server"); class TrapezeApp { /** * Trapeze Client APP * @param config Config to be setup */ constructor(config) { this.config = config; this.secureToken = this.createSecureToken(); this.apiServer = new api_server_1.ApiServer({ endpoint: config.endpoint.href, port: config.port, secret: this.secureToken, }); } /** * creates a random string */ createSecureToken() { return crypto.randomBytes(64).toString('hex'); } /** * Inits the client and starts up the app */ init() { return this.apiServer .start() .then(() => { electron_1.app.on('ready', this.createWindow.bind(this)); electron_1.app.on('window-all-closed', () => { if (process.platform !== 'darwin') { electron_1.app.quit(); this.apiServer.stop(); } }); electron_1.app.on('activate', () => { if (this.mainWindow === null) { this.createWindow(); } }); }); } setupNetworkInterceptors(session) { const filter = { urls: [ '*://localhost/*', ], }; session.webRequest .onBeforeSendHeaders(filter, (details, callback) => { // tslint:disable-next-line:no-string-literal details.requestHeaders['Authorization'] = 'Bearer ' + this.secureToken; callback({ cancel: false, requestHeaders: details.requestHeaders }); }); } createWindow() { // create the browser window. const browserConfig = { height: 600, icon: __dirname + '/../icon.png', minHeight: 480, minWidth: 640, title: 'TrapezeClient', webPreferences: { allowRunningInsecureContent: true, javascript: true, nodeIntegration: false, }, width: 800, }; this.mainWindow = new electron_1.BrowserWindow(browserConfig); // register Token Interceptor this.setupNetworkInterceptors(this.mainWindow.webContents.session); // tslint:disable-next-line:no-null-keyword this.mainWindow.autoHideMenuBar = true; this.mainWindow.loadURL('http://localhost:' + this.config.port + '/index.html'); if (this.config.dev) { this.mainWindow.webContents.openDevTools({ mode: 'right', }); } // emitted when the window is closed. this.mainWindow.on('closed', () => { // dereference the window object, usually you would store windows // in an array if your app supports multi windows, this is the time // when you should delete the corresponding element. this.mainWindow = undefined; }); } } exports.TrapezeApp = TrapezeApp; //# sourceMappingURL=app.js.map