@@ -13,7 +13,9 @@ function reportError(error: Error | string) {
1313import { spawn , exec , ChildProcess } from 'child_process' ;
1414import * as os from 'os' ;
1515import * as path from 'path' ;
16+ import * as querystring from 'querystring' ;
1617import { app , BrowserWindow , shell , Menu , dialog } from 'electron' ;
18+ import * as uuid from 'uuid/v4' ;
1719
1820import * as windowStateKeeper from 'electron-window-state' ;
1921
@@ -30,6 +32,8 @@ const packageJson = require('../package.json');
3032const isWindows = os . platform ( ) === 'win32' ;
3133
3234const APP_URL = process . env . APP_URL || 'https://app.httptoolkit.tech' ;
35+ const AUTH_TOKEN = uuid ( ) ;
36+ const DESKTOP_VERSION = packageJson . version ;
3337
3438// Keep a global reference of the window object, if you don't, the window will
3539// be closed automatically when the JavaScript object is garbage collected.
@@ -72,7 +76,10 @@ const createWindow = () => {
7276
7377 windowState . manage ( window ) ;
7478
75- window . loadURL ( APP_URL ) ;
79+ window . loadURL ( APP_URL + '?' + querystring . stringify ( {
80+ authToken : AUTH_TOKEN ,
81+ desktopVersion : DESKTOP_VERSION
82+ } ) ) ;
7683
7784 window . on ( 'ready-to-show' , function ( ) {
7885 window ! . show ( ) ;
@@ -152,14 +159,27 @@ if (!amMainInstance) {
152159 } ) ;
153160
154161 app . on ( 'web-contents-created' , ( _event , contents ) => {
155- contents . on ( 'dom-ready' , ( ) => {
156- // Define & announce the desktop shell version to the app
157- // Not used now, intended to allow us to detect and prompt for updates
158- // in future, if a certain desktop shell version is required.
162+ function injectValue ( name : string , value : string ) {
163+ // Set a variable globally, and self-postmessage it too (to ping
164+ // anybody who's explicitly waiting for it).
159165 contents . executeJavaScript ( `
160- window.httpToolkitDesktopVersion = '${ packageJson . version } ';
161- window.postMessage({ httpToolkitDesktopVersion : window.httpToolkitDesktopVersion }, '*');
166+ window.${ name } = '${ value } ';
167+ window.postMessage({ ${ name } : window.${ name } }, '*');
162168 ` ) ;
169+ }
170+
171+ contents . on ( 'dom-ready' , ( ) => {
172+ // Define & announce config values to the app.
173+
174+ // Desktop version isn't used yet. Intended to allow us to detect
175+ // and prompt for updates in future if a certain desktop version
176+ // is required, and for error reporting context when things go wrong.
177+ injectValue ( 'httpToolkitDesktopVersion' , DESKTOP_VERSION ) ;
178+
179+ // Auth token is also injected into query string, but query string
180+ // gets replaced on first navigation (immediately), whilst global
181+ // vars like this are forever.
182+ injectValue ( 'httpToolkitAuthToken' , AUTH_TOKEN ) ;
163183 } ) ;
164184
165185 // Redirect all navigations & new windows to the system browser
@@ -192,7 +212,7 @@ if (!amMainInstance) {
192212 const serverBinPath = path . join ( __dirname , '..' , 'httptoolkit-server' , 'bin' , binName ) ;
193213 const serverBinCommand = isWindows ? `"${ serverBinPath } "` : serverBinPath ;
194214
195- server = spawn ( serverBinCommand , [ 'start' ] , {
215+ server = spawn ( serverBinCommand , [ 'start' , '--token' , AUTH_TOKEN ] , {
196216 windowsHide : true ,
197217 stdio : [ 'inherit' , 'pipe' , 'pipe' ] ,
198218 shell : isWindows , // Required to spawn a .cmd script
0 commit comments