(init: Init)
| 32 | } |
| 33 | |
| 34 | export async function startLogger(init: Init): Promise<void> { |
| 35 | const state: State = { |
| 36 | window: undefined, |
| 37 | url: path.join(__dirname, '../window/images/icon.png'), |
| 38 | entry: init.rest, |
| 39 | backHost: init.args['connect-remote'] ? new URL('ws://'+init.args['connect-remote']) : new URL('ws://localhost'), |
| 40 | socket: new SocketClient(WebSocket), |
| 41 | _secret: '', |
| 42 | mainFolderPath: createErrorProxy('mainFolderPath'), |
| 43 | config: createErrorProxy('config'), |
| 44 | prefs: createErrorProxy('prefs'), |
| 45 | isQuitting: false |
| 46 | }; |
| 47 | |
| 48 | function onInit(event: IpcMainEvent) { |
| 49 | const data: InitRendererData = { |
| 50 | isBackRemote: true, |
| 51 | installed: false, |
| 52 | version: 0, |
| 53 | host: state.backHost.href, |
| 54 | secret: state._secret, |
| 55 | }; |
| 56 | event.returnValue = data; |
| 57 | } |
| 58 | |
| 59 | await startup(); |
| 60 | |
| 61 | // -- Functions -- |
| 62 | |
| 63 | async function startup() { |
| 64 | app.once('ready', onAppReady); |
| 65 | app.once('window-all-closed', onAppWindowAllClosed); |
| 66 | app.once('web-contents-created', onAppWebContentsCreated); |
| 67 | app.on('activate', onAppActivate); |
| 68 | |
| 69 | state.backHost.port = '12001'; |
| 70 | |
| 71 | state.mainFolderPath = getMainFolderPath(); |
| 72 | state.config = ConfigFile.readOrCreateFileSync(path.join(state.mainFolderPath, CONFIG_FILENAME)); |
| 73 | state.prefs = PreferencesFile.readOrCreateFileSync(path.join(state.config.flashpointPath, PREFERENCES_FILENAME)); |
| 74 | |
| 75 | // Get secret to connect to backend |
| 76 | const secretFilePath = path.join(state.mainFolderPath, 'secret.dat'); |
| 77 | try { |
| 78 | state._secret = await fs.readFile(secretFilePath, { encoding: 'utf8' }); |
| 79 | } catch (e) { |
| 80 | state._secret = randomBytes(2048).toString('hex'); |
| 81 | try { |
| 82 | await fs.writeFile(secretFilePath, state._secret, { encoding: 'utf8' }); |
| 83 | } catch (e) { |
| 84 | console.warn(`Failed to save new secret to disk.\n${e}`); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // Connect to back process |
| 89 | await timeout<WebSocket>(new Promise((resolve, reject) => { |
| 90 | console.log('secret: ' + state._secret); |
| 91 | const sock = new WebSocket(state.backHost.href); |
no test coverage detected