mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
fix(desktop): start PKC RPC with configured options
This commit is contained in:
@@ -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;
|
||||
};
|
||||
@@ -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');
|
||||
});
|
||||
});
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user