From d608a46b00303dc2a5f0b7c9a4e40f62f6beaf1d Mon Sep 17 00:00:00 2001 From: Esteban Abaroa Date: Sat, 7 Dec 2024 21:13:23 +0000 Subject: [PATCH 1/2] fix(electron): ipfs proxy should have error status code --- electron/proxy-server.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/electron/proxy-server.js b/electron/proxy-server.js index 7c10141a..b6aa8d6f 100644 --- a/electron/proxy-server.js +++ b/electron/proxy-server.js @@ -23,7 +23,9 @@ proxy.on('proxyReq', function (proxyReq, req, res, options) { proxy.on('error', (e, req, res) => { console.error(e); // if not ended, will hang forever - res.end(); + res.statusCode = 502; + res.setHeader('Content-Type', 'text/plain'); + res.end(`502 Bad Gateway: ${e.message}`); }); // start server From b05c6ed94f61fa94fd233033c4be056a55bc1e23 Mon Sep 17 00:00:00 2001 From: Esteban Abaroa Date: Sat, 7 Dec 2024 22:57:25 +0000 Subject: [PATCH 2/2] fix(electron): auto restart script more reliable --- electron/start-ipfs.js | 116 ++++++++++++++++++---------------- electron/start-plebbit-rpc.js | 60 +++++++++--------- 2 files changed, 93 insertions(+), 83 deletions(-) diff --git a/electron/start-ipfs.js b/electron/start-ipfs.js index 2f1722a9..af6d56cb 100644 --- a/electron/start-ipfs.js +++ b/electron/start-ipfs.js @@ -76,67 +76,75 @@ const startIpfs = async () => { } await spawnAsync(ipfsPath, ['config', 'Addresses.API', apiAddress], { env, hideWindows: true }); - await new Promise((resolve, reject) => { - const ipfsProcess = spawn(ipfsPath, ['daemon', '--migrate', '--enable-pubsub-experiment', '--enable-namesys-pubsub'], { env, hideWindows: true }); - console.log(`ipfs daemon process started with pid ${ipfsProcess.pid}`); - let lastError; - ipfsProcess.stderr.on('data', (data) => { - lastError = data.toString(); - console.error(data.toString()); + const startIpfsDaemon = () => + new Promise((resolve, reject) => { + const ipfsProcess = spawn(ipfsPath, ['daemon', '--migrate', '--enable-pubsub-experiment', '--enable-namesys-pubsub'], { env, hideWindows: true }); + console.log(`ipfs daemon process started with pid ${ipfsProcess.pid}`); + let lastError; + ipfsProcess.stderr.on('data', (data) => { + lastError = data.toString(); + console.error(data.toString()); + }); + ipfsProcess.stdin.on('data', (data) => console.log(data.toString())); + ipfsProcess.stdout.on('data', (data) => { + data = data.toString(); + console.log(data); + if (data.includes('Daemon is ready')) { + resolve(); + } + }); + ipfsProcess.on('error', (data) => console.error(data.toString())); + ipfsProcess.on('exit', () => { + console.error(`ipfs process with pid ${ipfsProcess.pid} exited`); + reject(Error(lastError)); + }); + process.on('exit', () => { + try { + ps.kill(ipfsProcess.pid); + } catch (e) { + console.log(e); + } + try { + // sometimes ipfs doesnt exit unless we kill pid +1 + ps.kill(ipfsProcess.pid + 1); + } catch (e) { + console.log(e); + } + }); }); - ipfsProcess.stdin.on('data', (data) => console.log(data.toString())); - ipfsProcess.stdout.on('data', (data) => console.log(data.toString())); - ipfsProcess.on('error', (data) => console.error(data.toString())); - ipfsProcess.on('exit', () => { - console.error(`ipfs process with pid ${ipfsProcess.pid} exited`); - reject(Error(lastError)); - }); - process.on('exit', () => { - try { - ps.kill(ipfsProcess.pid); - } catch (e) { - console.log(e); - } - try { - // sometimes ipfs doesnt exit unless we kill pid +1 - ps.kill(ipfsProcess.pid + 1); - } catch (e) { - console.log(e); - } - }); - }); + await startIpfsDaemon(); }; const DefaultExport = {}; +export default DefaultExport; -let pendingStart = false; -const start = async () => { - if (pendingStart) { - return; - } - pendingStart = true; - try { - const started = await tcpPortUsed.check(isDev ? 50029 : 50019, '127.0.0.1'); - if (started) { +const startIpfsAutoRestart = async () => { + let pendingStart = false; + const start = async () => { + if (pendingStart) { return; } - await startIpfs(); - } catch (e) { - console.log('failed starting ipfs', e); + pendingStart = true; try { - // try to run exported onError callback, can be undefined - DefaultExport.onError(e)?.catch?.(console.log); - } catch (e) {} - } - pendingStart = false; -}; + const started = await tcpPortUsed.check(isDev ? 50029 : 50019, '127.0.0.1'); + if (!started) { + await startIpfs(); + } + } catch (e) { + console.log('failed starting ipfs', e); + try { + // try to run exported onError callback, can be undefined + DefaultExport.onError(e)?.catch?.(console.log); + } catch (e) {} + } + pendingStart = false; + }; -// retry starting ipfs every 1 second, -// in case it was started by another client that shut down and shut down ipfs with it -start(); -setInterval(() => { + // retry starting ipfs every 1 second, + // in case it was started by another client that shut down and shut down ipfs with it start(); -}, 1000); - -DefaultExport.start = start; -export default DefaultExport; + setInterval(() => { + start(); + }, 1000); +}; +startIpfsAutoRestart(); diff --git a/electron/start-plebbit-rpc.js b/electron/start-plebbit-rpc.js index 8a3e07b9..f01f0533 100644 --- a/electron/start-plebbit-rpc.js +++ b/electron/start-plebbit-rpc.js @@ -29,38 +29,40 @@ try { fs.writeFileSync(plebbitRpcAuthKeyPath, plebbitRpcAuthKey); } -let pendingStart = false; -const start = async () => { - if (pendingStart) { - return; - } - pendingStart = true; - try { - const started = await tcpPortUsed.check(port, '127.0.0.1'); - if (started) { +const startPlebbitRpcAutoRestart = async () => { + let pendingStart = false; + const start = async () => { + if (pendingStart) { return; } - const plebbitWebSocketServer = await PlebbitRpc.PlebbitWsServer({ port, plebbitOptions: defaultPlebbitOptions, authKey: plebbitRpcAuthKey }); - plebbitWebSocketServer.on('error', (e) => console.log('plebbit rpc error', e)); + pendingStart = true; + try { + const started = await tcpPortUsed.check(port, '127.0.0.1'); + if (!started) { + const plebbitWebSocketServer = await PlebbitRpc.PlebbitWsServer({ port, plebbitOptions: defaultPlebbitOptions, authKey: plebbitRpcAuthKey }); + plebbitWebSocketServer.on('error', (e) => console.log('plebbit rpc error', e)); - console.log(`plebbit rpc: listening on ws://localhost:${port} (local connections only)`); - console.log(`plebbit rpc: listening on ws://localhost:${port}/${plebbitRpcAuthKey} (secret auth key for remote connections)`); - plebbitWebSocketServer.ws.on('connection', (socket, request) => { - console.log('plebbit rpc: new connection'); - // debug raw JSON RPC messages in console - if (isDev) { - socket.on('message', (message) => console.log(`plebbit rpc: ${message.toString()}`)); + console.log(`plebbit rpc: listening on ws://localhost:${port} (local connections only)`); + console.log(`plebbit rpc: listening on ws://localhost:${port}/${plebbitRpcAuthKey} (secret auth key for remote connections)`); + plebbitWebSocketServer.ws.on('connection', (socket, request) => { + console.log('plebbit rpc: new connection'); + // debug raw JSON RPC messages in console + if (isDev) { + socket.on('message', (message) => console.log(`plebbit rpc: ${message.toString()}`)); + } + }); } - }); - } catch (e) { - console.log('failed starting plebbit rpc server', e); - } - pendingStart = false; -}; + } catch (e) { + console.log('failed starting plebbit rpc server', e); + } + pendingStart = false; + }; -// retry starting the plebbit rpc server every 1 second, -// in case it was started by another client that shut down and shut down the server with it -start(); -setInterval(() => { + // retry starting the plebbit rpc server every 1 second, + // in case it was started by another client that shut down and shut down the server with it start(); -}, 1000); + setInterval(() => { + start(); + }, 1000); +}; +startPlebbitRpcAutoRestart();