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
+62 -54
View File
@@ -76,67 +76,75 @@ 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 = () =>
const ipfsProcess = spawn(ipfsPath, ['daemon', '--migrate', '--enable-pubsub-experiment', '--enable-namesys-pubsub'], { env, hideWindows: true }); new Promise((resolve, reject) => {
console.log(`ipfs daemon process started with pid ${ipfsProcess.pid}`); const ipfsProcess = spawn(ipfsPath, ['daemon', '--migrate', '--enable-pubsub-experiment', '--enable-namesys-pubsub'], { env, hideWindows: true });
let lastError; console.log(`ipfs daemon process started with pid ${ipfsProcess.pid}`);
ipfsProcess.stderr.on('data', (data) => { let lastError;
lastError = data.toString(); ipfsProcess.stderr.on('data', (data) => {
console.error(data.toString()); 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())); await startIpfsDaemon();
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);
}
});
});
}; };
const DefaultExport = {}; const DefaultExport = {};
export default DefaultExport;
let pendingStart = false; const startIpfsAutoRestart = async () => {
const start = async () => { let pendingStart = false;
if (pendingStart) { const start = async () => {
return; if (pendingStart) {
}
pendingStart = true;
try {
const started = await tcpPortUsed.check(isDev ? 50029 : 50019, '127.0.0.1');
if (started) {
return; return;
} }
await startIpfs(); pendingStart = true;
} catch (e) {
console.log('failed starting ipfs', e);
try { try {
// try to run exported onError callback, can be undefined const started = await tcpPortUsed.check(isDev ? 50029 : 50019, '127.0.0.1');
DefaultExport.onError(e)?.catch?.(console.log); if (!started) {
} catch (e) {} await startIpfs();
} }
pendingStart = false; } 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, // 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();
+31 -29
View File
@@ -29,38 +29,40 @@ try {
fs.writeFileSync(plebbitRpcAuthKeyPath, plebbitRpcAuthKey); fs.writeFileSync(plebbitRpcAuthKeyPath, plebbitRpcAuthKey);
} }
let pendingStart = false; const startPlebbitRpcAutoRestart = async () => {
const start = async () => { let pendingStart = false;
if (pendingStart) { const start = async () => {
return; if (pendingStart) {
}
pendingStart = true;
try {
const started = await tcpPortUsed.check(port, '127.0.0.1');
if (started) {
return; return;
} }
const plebbitWebSocketServer = await PlebbitRpc.PlebbitWsServer({ port, plebbitOptions: defaultPlebbitOptions, authKey: plebbitRpcAuthKey }); pendingStart = true;
plebbitWebSocketServer.on('error', (e) => console.log('plebbit rpc error', e)); 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} (local connections only)`);
console.log(`plebbit rpc: listening on ws://localhost:${port}/${plebbitRpcAuthKey} (secret auth key for remote connections)`); console.log(`plebbit rpc: listening on ws://localhost:${port}/${plebbitRpcAuthKey} (secret auth key for remote connections)`);
plebbitWebSocketServer.ws.on('connection', (socket, request) => { plebbitWebSocketServer.ws.on('connection', (socket, request) => {
console.log('plebbit rpc: new connection'); console.log('plebbit rpc: new connection');
// debug raw JSON RPC messages in console // debug raw JSON RPC messages in console
if (isDev) { if (isDev) {
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();