fix(desktop): start PKC RPC with configured options

This commit is contained in:
Tommaso Casaburi
2026-05-14 17:26:45 +07:00
parent 2e619afd35
commit 9f2a95af50
3 changed files with 51 additions and 12 deletions
+16
View File
@@ -0,0 +1,16 @@
export const startPkcRpcServer = async ({ PKCRpcModule, port, pkcOptions, authKey, isDev = false, logger = console }) => {
const pkcWebSocketServer = await PKCRpcModule.PKCWsServer({ port, pkcOptions, authKey });
pkcWebSocketServer.on('error', (e) => logger.log('pkc rpc error', e));
logger.log(`pkc rpc: listening on ws://localhost:${port} (local connections only)`);
logger.log(`pkc rpc: listening on ws://localhost:${port}/${authKey} (secret auth key for remote connections)`);
pkcWebSocketServer.ws.on('connection', (socket) => {
logger.log('pkc rpc: new connection');
// debug raw JSON RPC messages in console
if (isDev) {
socket.on('message', (message) => logger.log(`pkc rpc: ${message.toString()}`));
}
});
return pkcWebSocketServer;
};
+33
View File
@@ -0,0 +1,33 @@
import { describe, expect, it, vi } from 'vitest';
import { startPkcRpcServer } from './start-pkc-rpc-core.js';
describe('startPkcRpcServer', () => {
it('passes the Electron data path through pkcOptions', async () => {
const pkcOptions = {
dataPath: '/tmp/5chan-pkc',
kuboRpcClientsOptions: [{ url: 'http://localhost:50019/api/v0' }],
};
const server = {
on: vi.fn(),
ws: { on: vi.fn() },
};
const PKCRpcModule = {
PKCWsServer: vi.fn(async () => server),
};
await startPkcRpcServer({
PKCRpcModule,
port: 9138,
pkcOptions,
authKey: 'test-auth-key',
logger: { log: vi.fn() },
});
expect(PKCRpcModule.PKCWsServer).toHaveBeenCalledWith({
port: 9138,
pkcOptions,
authKey: 'test-auth-key',
});
expect(PKCRpcModule.PKCWsServer.mock.calls[0][0]).not.toHaveProperty('pkc');
});
});
+2 -12
View File
@@ -6,6 +6,7 @@ import path from 'path';
import { fileURLToPath } from 'url';
import isDev from 'electron-is-dev';
import { getPkcDataPath } from './pkc-paths.js';
import { startPkcRpcServer } from './start-pkc-rpc-core.js';
const dirname = path.join(path.dirname(fileURLToPath(import.meta.url)));
const projectRoot = path.join(dirname, '..');
@@ -39,18 +40,7 @@ const startPkcRpcAutoRestart = async () => {
try {
const started = await tcpPortUsed.check(port, '127.0.0.1');
if (!started) {
const pkcWebSocketServer = await PKCRpc.PKCWsServer({ port, pkc: defaultPkcOptions, authKey: pkcRpcAuthKey });
pkcWebSocketServer.on('error', (e) => console.log('pkc rpc error', e));
console.log(`pkc rpc: listening on ws://localhost:${port} (local connections only)`);
console.log(`pkc rpc: listening on ws://localhost:${port}/${pkcRpcAuthKey} (secret auth key for remote connections)`);
pkcWebSocketServer.ws.on('connection', (socket, request) => {
console.log('pkc rpc: new connection');
// debug raw JSON RPC messages in console
if (isDev) {
socket.on('message', (message) => console.log(`pkc rpc: ${message.toString()}`));
}
});
await startPkcRpcServer({ PKCRpcModule: PKCRpc, port, pkcOptions: defaultPkcOptions, authKey: pkcRpcAuthKey, isDev });
}
} catch (e) {
console.log('failed starting pkc rpc server', e);