-
Notifications
You must be signed in to change notification settings - Fork 11
/
electron-main.js
112 lines (93 loc) · 3.39 KB
/
electron-main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
'use strict'
const electron = require('electron')
const { isWindows, isMac, isLinux } = require('which-runtime')
const { command } = require('paparam')
const State = require('./state')
const GUI = require('./gui')
const crasher = require('./lib/crasher')
const tryboot = require('./lib/tryboot')
const { SWAP, SOCKET_PATH, CONNECT_TIMEOUT } = require('./constants')
const runDefinition = require('./def/run')
const argv = (process.argv.length > 1 && process.argv[1][0] === '-') ? process.argv.slice(1) : process.argv.slice(2)
const runix = argv.indexOf('--run')
if (runix > -1) argv.splice(runix, 1)
configureElectron()
crasher('electron-main', SWAP)
const run = command('run', ...runDefinition, electronMain)
run.parse(argv)
run.running?.catch(console.error)
async function electronMain (cmd) {
const state = new State({
link: cmd.args.link.replace('_||', '://'), // for Windows
flags: cmd.flags,
args: cmd.rest
})
State.storage(state)
if (state.error) {
console.error(state.error)
electron.app.quit(1)
return
}
const gui = new GUI({
socketPath: SOCKET_PATH,
connectTimeout: CONNECT_TIMEOUT,
tryboot,
state
})
await gui.ready()
// note: would be unhandled rejection on failure, but should never fail:
if (await gui.ipc.wakeup(state.link, state.storage, state.key === null ? state.dir : null, state.link?.startsWith('pear://dev'))) {
electron.app.quit(0)
return
}
electron.ipcMain.on('send-to', (e, id, channel, message) => { electron.webContents.fromId(id)?.send(channel, message) })
const app = await gui.app()
app.unloading().then(async () => {
await app.close()
}) // note: would be unhandled rejection on failure, but should never fail
}
function configureElectron () {
const appName = applingName()
if (appName) {
process.title = appName
electron.app.on('ready', () => { process.title = appName })
}
process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = 'true'
/* c8 ignore start */
const inspix = process.argv.indexOf('--inspector-port')
if (inspix > -1) {
electron.app.commandLine.appendSwitch('remote-debugging-port', inspix + 1)
}
/* c8 ignore stop */
electron.protocol.registerSchemesAsPrivileged([
{ scheme: 'file', privileges: { secure: true, bypassCSP: true, corsEnabled: true, supportFetchAPI: true, allowServiceWorkers: true } }
])
// TODO: Remove when issue https://github.com/electron/electron/issues/29458 is resolved.
electron.app.commandLine.appendSwitch('disable-features', 'WindowCaptureMacV2')
// Needed for running fully-local WebRTC proxies
electron.app.commandLine.appendSwitch('allow-loopback-in-peer-connection')
if (isLinux && process.env.XDG_SESSION_TYPE === 'wayland') {
electron.app.commandLine.appendSwitch('enable-features', 'WebRTCPipeWireCapturer,WaylandWindowDecorations')
electron.app.commandLine.appendSwitch('ozone-platform-hint', 'auto')
}
}
function applingPath () {
const i = process.argv.indexOf('--appling')
if (i === -1 || process.argv.length <= i + 1) return null
return process.argv[i + 1]
}
function applingName () {
const a = applingPath()
if (!a) return null
if (isMac) {
const end = a.indexOf('.app')
if (end === -1) return null
const start = a.lastIndexOf('/', end) + 1
return a.slice(start, end)
}
if (isWindows) {
const name = a.slice(a.lastIndexOf('\\') + 1).replace(/\.exe$/i, '')
return name || null
}
return null
}