diff options
author | 2021-09-14 11:03:28 +0200 | |
---|---|---|
committer | 2021-09-14 11:03:28 +0200 | |
commit | 087113d8a1214ba4c7df03bfe66747d8d944280c (patch) | |
tree | 4d853a03057138dfa845cd6a7d91ccf63565a1a6 /src/electron/ipc-api/localServer.js | |
parent | chore: codebase improvements (#1930) (diff) | |
download | ferdium-app-087113d8a1214ba4c7df03bfe66747d8d944280c.tar.gz ferdium-app-087113d8a1214ba4c7df03bfe66747d8d944280c.tar.zst ferdium-app-087113d8a1214ba4c7df03bfe66747d8d944280c.zip |
chore: convert JS to TS (#1934)
Diffstat (limited to 'src/electron/ipc-api/localServer.js')
-rw-r--r-- | src/electron/ipc-api/localServer.js | 48 |
1 files changed, 0 insertions, 48 deletions
diff --git a/src/electron/ipc-api/localServer.js b/src/electron/ipc-api/localServer.js deleted file mode 100644 index 591e70504..000000000 --- a/src/electron/ipc-api/localServer.js +++ /dev/null | |||
@@ -1,48 +0,0 @@ | |||
1 | import { ipcMain } from 'electron'; | ||
2 | import net from 'net'; | ||
3 | import { LOCAL_HOSTNAME, LOCAL_PORT } from '../../config'; | ||
4 | import { userDataPath } from '../../environment'; | ||
5 | import startServer from '../../internal-server/start'; | ||
6 | |||
7 | const portInUse = function (port) { | ||
8 | return new Promise((resolve) => { | ||
9 | const server = net.createServer((socket) => { | ||
10 | socket.write('Echo server\r\n'); | ||
11 | socket.pipe(socket); | ||
12 | }); | ||
13 | |||
14 | server.listen(port, LOCAL_HOSTNAME); | ||
15 | server.on('error', () => { | ||
16 | resolve(true); | ||
17 | }); | ||
18 | server.on('listening', () => { | ||
19 | server.close(); | ||
20 | resolve(false); | ||
21 | }); | ||
22 | }); | ||
23 | }; | ||
24 | |||
25 | let localServerStarted = false; | ||
26 | |||
27 | export default (params) => { | ||
28 | ipcMain.on('startLocalServer', () => { | ||
29 | if (!localServerStarted) { | ||
30 | // Find next unused port for server | ||
31 | let port = LOCAL_PORT; | ||
32 | (async () => { | ||
33 | // eslint-disable-next-line no-await-in-loop | ||
34 | while ((await portInUse(port)) && port < LOCAL_PORT + 10) { | ||
35 | port += 1; | ||
36 | } | ||
37 | console.log('Starting local server on port', port); | ||
38 | |||
39 | startServer(userDataPath(), port); | ||
40 | |||
41 | params.mainWindow.webContents.send('localServerPort', { | ||
42 | port, | ||
43 | }); | ||
44 | })(); | ||
45 | localServerStarted = true; | ||
46 | } | ||
47 | }); | ||
48 | }; | ||