Files
5chan/electron/start-plebbit-rpc.js
T

67 lines
2.5 KiB
JavaScript
Raw Normal View History

import tcpPortUsed from 'tcp-port-used';
import EnvPaths from 'env-paths';
import { randomBytes } from 'crypto';
import fs from 'fs-extra';
import PlebbitRpc from '@plebbit/plebbit-js/dist/node/rpc/src/index.js';
import path from 'path';
import { fileURLToPath } from 'url';
import isDev from 'electron-is-dev';
const dirname = path.join(path.dirname(fileURLToPath(import.meta.url)));
const envPaths = EnvPaths('plebbit', { suffix: false });
2023-12-11 00:49:36 +00:00
// PLEB, always run plebbit rpc on this port so all clients can use it
2023-12-16 16:49:26 +00:00
const port = 9138;
2023-12-11 00:49:36 +00:00
const defaultPlebbitOptions = {
// find the user's OS data path
dataPath: !isDev ? envPaths.data : path.join(dirname, '..', '.plebbit'),
2023-12-11 00:49:36 +00:00
ipfsHttpClientsOptions: ['http://localhost:5001/api/v0'],
// TODO: having to define pubsubHttpClientsOptions and ipfsHttpClientsOptions is a bug with plebbit-js
pubsubHttpClientsOptions: ['http://localhost:5001/api/v0'],
2023-12-16 16:49:26 +00:00
};
2023-12-11 00:49:36 +00:00
// generate plebbit rpc auth key if doesn't exist
2023-12-16 16:49:26 +00:00
const plebbitRpcAuthKeyPath = path.join(defaultPlebbitOptions.dataPath, 'auth-key');
let plebbitRpcAuthKey;
2023-12-11 00:49:36 +00:00
try {
2023-12-16 16:49:26 +00:00
plebbitRpcAuthKey = fs.readFileSync(plebbitRpcAuthKeyPath, 'utf8');
} catch (e) {
plebbitRpcAuthKey = randomBytes(32).toString('base64').replace(/[/+=]/g, '').substring(0, 40);
fs.ensureFileSync(plebbitRpcAuthKeyPath);
fs.writeFileSync(plebbitRpcAuthKeyPath, plebbitRpcAuthKey);
2023-12-11 00:49:36 +00:00
}
2023-12-16 16:49:26 +00:00
let pendingStart = false;
2023-12-11 00:49:36 +00:00
const start = async () => {
if (pendingStart) {
2023-12-16 16:49:26 +00:00
return;
2023-12-11 00:49:36 +00:00
}
2023-12-16 16:49:26 +00:00
pendingStart = true;
2023-12-11 00:49:36 +00:00
try {
2023-12-16 16:49:26 +00:00
const started = await tcpPortUsed.check(port, '127.0.0.1');
2023-12-11 00:49:36 +00:00
if (started) {
2023-12-16 16:49:26 +00:00
return;
2023-12-11 00:49:36 +00:00
}
const plebbitWebSocketServer = await PlebbitRpc.PlebbitWsServer({ port, plebbitOptions: defaultPlebbitOptions, authKey: plebbitRpcAuthKey });
2023-12-11 00:49:36 +00:00
2023-12-16 16:49:26 +00:00
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)`);
2023-12-11 00:49:36 +00:00
plebbitWebSocketServer.ws.on('connection', (socket, request) => {
2023-12-16 16:49:26 +00:00
console.log('plebbit rpc: new connection');
2023-12-11 00:49:36 +00:00
// debug raw JSON RPC messages in console
if (isDev) {
2023-12-16 16:49:26 +00:00
socket.on('message', (message) => console.log(`plebbit rpc: ${message.toString()}`));
2023-12-11 00:49:36 +00:00
}
2023-12-16 16:49:26 +00:00
});
} catch (e) {
console.log('failed starting plebbit rpc server', e);
2023-12-11 00:49:36 +00:00
}
2023-12-16 16:49:26 +00:00
pendingStart = false;
};
2023-12-11 00:49:36 +00:00
2023-12-16 16:49:26 +00:00
// retry starting the plebbit rpc server every 1 second,
2023-12-11 00:49:36 +00:00
// in case it was started by another client that shut down and shut down the server with it
2023-12-16 16:49:26 +00:00
start();
2023-12-11 00:49:36 +00:00
setInterval(() => {
2023-12-16 16:49:26 +00:00
start();
}, 1000);