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
+16 -8
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,10 +112,13 @@ const startIpfs = async () => {
} }
}); });
}); });
await startIpfsDaemon();
}; };
const DefaultExport = {}; const DefaultExport = {};
export default DefaultExport;
const startIpfsAutoRestart = async () => {
let pendingStart = false; let pendingStart = false;
const start = async () => { const start = async () => {
if (pendingStart) { if (pendingStart) {
@@ -117,10 +127,9 @@ const start = async () => {
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 {
@@ -137,6 +146,5 @@ start();
setInterval(() => { setInterval(() => {
start(); start();
}, 1000); }, 1000);
};
DefaultExport.start = start; startIpfsAutoRestart();
export default DefaultExport;
+5 -3
View File
@@ -29,6 +29,7 @@ try {
fs.writeFileSync(plebbitRpcAuthKeyPath, plebbitRpcAuthKey); fs.writeFileSync(plebbitRpcAuthKeyPath, plebbitRpcAuthKey);
} }
const startPlebbitRpcAutoRestart = async () => {
let pendingStart = false; let pendingStart = false;
const start = async () => { const start = async () => {
if (pendingStart) { if (pendingStart) {
@@ -37,9 +38,7 @@ const start = async () => {
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,6 +51,7 @@ 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);
} }
@@ -64,3 +64,5 @@ start();
setInterval(() => { setInterval(() => {
start(); start();
}, 1000); }, 1000);
};
startPlebbitRpcAutoRestart();