Merge pull request #617 from plebbit/master

Development
This commit is contained in:
Tom (plebeius.eth)
2024-12-12 16:30:50 +01:00
committed by GitHub
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) => {
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
+62 -54
View File
@@ -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();
+31 -29
View File
@@ -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();