diff --git a/electron/start-pkc-rpc-core.js b/electron/start-pkc-rpc-core.js new file mode 100644 index 00000000..15149b88 --- /dev/null +++ b/electron/start-pkc-rpc-core.js @@ -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; +}; diff --git a/electron/start-pkc-rpc-core.test.js b/electron/start-pkc-rpc-core.test.js new file mode 100644 index 00000000..53d216db --- /dev/null +++ b/electron/start-pkc-rpc-core.test.js @@ -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'); + }); +}); diff --git a/electron/start-pkc-rpc.js b/electron/start-pkc-rpc.js index c80bd600..9a0c763a 100644 --- a/electron/start-pkc-rpc.js +++ b/electron/start-pkc-rpc.js @@ -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);