@@ -13,7 +13,9 @@ function reportError(error: Error | string) {
13
13
import { spawn , exec , ChildProcess } from 'child_process' ;
14
14
import * as os from 'os' ;
15
15
import * as path from 'path' ;
16
+ import * as querystring from 'querystring' ;
16
17
import { app , BrowserWindow , shell , Menu , dialog } from 'electron' ;
18
+ import * as uuid from 'uuid/v4' ;
17
19
18
20
import * as windowStateKeeper from 'electron-window-state' ;
19
21
@@ -30,6 +32,8 @@ const packageJson = require('../package.json');
30
32
const isWindows = os . platform ( ) === 'win32' ;
31
33
32
34
const APP_URL = process . env . APP_URL || 'https://app.httptoolkit.tech' ;
35
+ const AUTH_TOKEN = uuid ( ) ;
36
+ const DESKTOP_VERSION = packageJson . version ;
33
37
34
38
// Keep a global reference of the window object, if you don't, the window will
35
39
// be closed automatically when the JavaScript object is garbage collected.
@@ -72,7 +76,10 @@ const createWindow = () => {
72
76
73
77
windowState . manage ( window ) ;
74
78
75
- window . loadURL ( APP_URL ) ;
79
+ window . loadURL ( APP_URL + '?' + querystring . stringify ( {
80
+ authToken : AUTH_TOKEN ,
81
+ desktopVersion : DESKTOP_VERSION
82
+ } ) ) ;
76
83
77
84
window . on ( 'ready-to-show' , function ( ) {
78
85
window ! . show ( ) ;
@@ -152,14 +159,27 @@ if (!amMainInstance) {
152
159
} ) ;
153
160
154
161
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).
159
165
contents . executeJavaScript ( `
160
- window.httpToolkitDesktopVersion = '${ packageJson . version } ';
161
- window.postMessage({ httpToolkitDesktopVersion : window.httpToolkitDesktopVersion }, '*');
166
+ window.${ name } = '${ value } ';
167
+ window.postMessage({ ${ name } : window.${ name } }, '*');
162
168
` ) ;
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 ) ;
163
183
} ) ;
164
184
165
185
// Redirect all navigations & new windows to the system browser
@@ -192,7 +212,7 @@ if (!amMainInstance) {
192
212
const serverBinPath = path . join ( __dirname , '..' , 'httptoolkit-server' , 'bin' , binName ) ;
193
213
const serverBinCommand = isWindows ? `"${ serverBinPath } "` : serverBinPath ;
194
214
195
- server = spawn ( serverBinCommand , [ 'start' ] , {
215
+ server = spawn ( serverBinCommand , [ 'start' , '--token' , AUTH_TOKEN ] , {
196
216
windowsHide : true ,
197
217
stdio : [ 'inherit' , 'pipe' , 'pipe' ] ,
198
218
shell : isWindows , // Required to spawn a .cmd script
0 commit comments