-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathelectron-main.js
More file actions
140 lines (120 loc) · 3.63 KB
/
electron-main.js
File metadata and controls
140 lines (120 loc) · 3.63 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
'use strict'
const electron = require('electron')
const { isWindows, isMac, isLinux } = require('which-runtime')
const { command } = require('paparam')
const { SWAP, SOCKET_PATH, CONNECT_TIMEOUT } = require('pear-constants')
const API = require('pear-api')
const crasher = require('pear-crasher')
const tryboot = require('pear-tryboot')
const rundef = require('pear-cmd/run')
const State = require('pear-state')
const GUI = require('./gui')
const argv = process.argv.slice(2) // ['path-to-runtime', 'path-to-bundle', ...args]
configureElectron()
crasher('electron-main', SWAP, argv.indexOf('--log') > -1)
const run = command('run', ...rundef, electronMain)
run.parse(argv)
run.running?.catch(console.error)
async function electronMain(cmd) {
const state = new State({
startId: global.Pear.constructor.RTI.startId,
dir: global.Pear.constructor.RTI.dir,
link: cmd.args.link.replace('_||', '://'), // for Windows
flags: cmd.flags,
args: cmd.rest
})
if (state.error) {
console.error(state.error)
electron.app.quit(1)
return
}
const gui = new GUI({
socketPath: SOCKET_PATH,
connectTimeout: CONNECT_TIMEOUT,
tryboot,
state
})
global.Pear = new API(gui.ipc, state)
await gui.ready()
// note: would be unhandled rejection on failure, but should never fail:
const wakeup = await gui.ipc.wakeup(
state.link,
state.storage,
state.key === null ? state.dir : null,
true
)
if (wakeup) {
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
await app.cutover()
}
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 = 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')
}
}
if (isLinux && process.arch === 'x64') {
electron.app.disableHardwareAcceleration()
}
function applingPath() {
const i = argv.indexOf('--appling')
if (i === -1 || argv.length <= i + 1) return null
return 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
}