Merge branch 'development' of https://github.com/plebbit/plebchan into development

This commit is contained in:
Tom (plebeius.eth)
2024-12-12 16:38:10 +01:00
3 changed files with 96 additions and 84 deletions
+3 -1
View File
@@ -23,7 +23,9 @@ proxy.on('proxyReq', function (proxyReq, req, res, options) {
proxy.on('error', (e, req, res) => { proxy.on('error', (e, req, res) => {
console.error(e); console.error(e);
// if not ended, will hang forever // 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 // start server
+24 -16
View File
@@ -76,7 +76,8 @@ const startIpfs = async () => {
} }
await spawnAsync(ipfsPath, ['config', 'Addresses.API', apiAddress], { env, hideWindows: true }); await spawnAsync(ipfsPath, ['config', 'Addresses.API', apiAddress], { env, hideWindows: true });
await new Promise((resolve, reject) => { const startIpfsDaemon = () =>
new Promise((resolve, reject) => {
const ipfsProcess = spawn(ipfsPath, ['daemon', '--migrate', '--enable-pubsub-experiment', '--enable-namesys-pubsub'], { env, hideWindows: true }); 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}`); console.log(`ipfs daemon process started with pid ${ipfsProcess.pid}`);
let lastError; let lastError;
@@ -85,7 +86,13 @@ const startIpfs = async () => {
console.error(data.toString()); console.error(data.toString());
}); });
ipfsProcess.stdin.on('data', (data) => console.log(data.toString())); ipfsProcess.stdin.on('data', (data) => console.log(data.toString()));
ipfsProcess.stdout.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('error', (data) => console.error(data.toString()));
ipfsProcess.on('exit', () => { ipfsProcess.on('exit', () => {
console.error(`ipfs process with pid ${ipfsProcess.pid} exited`); console.error(`ipfs process with pid ${ipfsProcess.pid} exited`);
@@ -105,22 +112,24 @@ const startIpfs = async () => {
} }
}); });
}); });
await startIpfsDaemon();
}; };
const DefaultExport = {}; const DefaultExport = {};
export default DefaultExport;
let pendingStart = false; const startIpfsAutoRestart = async () => {
const start = async () => { let pendingStart = false;
const start = async () => {
if (pendingStart) { if (pendingStart) {
return; return;
} }
pendingStart = true; pendingStart = true;
try { try {
const started = await tcpPortUsed.check(isDev ? 50029 : 50019, '127.0.0.1'); const started = await tcpPortUsed.check(isDev ? 50029 : 50019, '127.0.0.1');
if (started) { if (!started) {
return;
}
await startIpfs(); await startIpfs();
}
} catch (e) { } catch (e) {
console.log('failed starting ipfs', e); console.log('failed starting ipfs', e);
try { try {
@@ -129,14 +138,13 @@ const start = async () => {
} catch (e) {} } catch (e) {}
} }
pendingStart = false; pendingStart = false;
}; };
// retry starting ipfs every 1 second, // retry starting ipfs every 1 second,
// in case it was started by another client that shut down and shut down ipfs with it // in case it was started by another client that shut down and shut down ipfs with it
start();
setInterval(() => {
start(); start();
}, 1000); setInterval(() => {
start();
DefaultExport.start = start; }, 1000);
export default DefaultExport; };
startIpfsAutoRestart();
+13 -11
View File
@@ -29,17 +29,16 @@ try {
fs.writeFileSync(plebbitRpcAuthKeyPath, plebbitRpcAuthKey); fs.writeFileSync(plebbitRpcAuthKeyPath, plebbitRpcAuthKey);
} }
let pendingStart = false; const startPlebbitRpcAutoRestart = async () => {
const start = async () => { let pendingStart = false;
const start = async () => {
if (pendingStart) { if (pendingStart) {
return; return;
} }
pendingStart = true; pendingStart = true;
try { try {
const started = await tcpPortUsed.check(port, '127.0.0.1'); const started = await tcpPortUsed.check(port, '127.0.0.1');
if (started) { if (!started) {
return;
}
const plebbitWebSocketServer = await PlebbitRpc.PlebbitWsServer({ port, plebbitOptions: defaultPlebbitOptions, authKey: plebbitRpcAuthKey }); const plebbitWebSocketServer = await PlebbitRpc.PlebbitWsServer({ port, plebbitOptions: defaultPlebbitOptions, authKey: plebbitRpcAuthKey });
plebbitWebSocketServer.on('error', (e) => console.log('plebbit rpc error', e)); plebbitWebSocketServer.on('error', (e) => console.log('plebbit rpc error', e));
@@ -52,15 +51,18 @@ const start = async () => {
socket.on('message', (message) => console.log(`plebbit rpc: ${message.toString()}`)); socket.on('message', (message) => console.log(`plebbit rpc: ${message.toString()}`));
} }
}); });
}
} catch (e) { } catch (e) {
console.log('failed starting plebbit rpc server', e); console.log('failed starting plebbit rpc server', e);
} }
pendingStart = false; pendingStart = false;
}; };
// retry starting the plebbit rpc server every 1 second, // 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 // in case it was started by another client that shut down and shut down the server with it
start();
setInterval(() => {
start(); start();
}, 1000); setInterval(() => {
start();
}, 1000);
};
startPlebbitRpcAutoRestart();