diff --git a/src/tachyonClient.test.ts b/src/tachyonClient.test.ts index 2c43878..f87485e 100644 --- a/src/tachyonClient.test.ts +++ b/src/tachyonClient.test.ts @@ -5,7 +5,7 @@ import { once } from 'node:events'; import { TachyonClient } from './tachyonClient.js'; import { createTachyonServer } from './tachyonServer.fake.js'; import { deepEqual } from 'node:assert'; -import { TachyonMessage } from './tachyonTypes.js'; +import { TachyonMessage, TachyonResponseOk } from './tachyonTypes.js'; // Let's reuse the same server for all tests to make them quicker. const server = await createTachyonServer({ clientId: 'c', clientSecret: 's' }); @@ -14,6 +14,13 @@ const port = server.fastifyServer.addresses()[0].port; after(() => server.close()); afterEach(() => server.removeAllListeners()); +const connectionParams = { + clientId: 'c', + clientSecret: 's', + hostname: 'localhost', + port, +}; + test('simple full example', async () => { server.on('connection', (conn) => { conn.on('message', (msg) => { @@ -29,12 +36,7 @@ test('simple full example', async () => { }); }); - const client = new TachyonClient({ - clientId: 'c', - clientSecret: 's', - hostname: 'localhost', - port, - }); + const client = new TachyonClient(connectionParams); await once(client, 'connected'); client.send({ type: 'request', @@ -52,4 +54,28 @@ test('simple full example', async () => { client.close(); }); +test("doesn't emit bad tachyon messages", async () => { + server.on('connection', (conn) => { + conn.on('message', () => { + conn.send({ + type: 'asdasdasd', + } as unknown as TachyonResponseOk); + }); + }); + const client = new TachyonClient(connectionParams); + await once(client, 'connected'); + client.send({ + type: 'request', + commandId: 'test/command', + messageId: 'test-message1', + data: { test: 'test' }, + }); + let gotMessages = 0; + client.on('message', () => { + ++gotMessages; + }); + await once(client, 'close'); + equal(gotMessages, 0); +}); + // TODO: Add more tests then only a simple happy path. diff --git a/src/tachyonClient.ts b/src/tachyonClient.ts index e25c93f..67ab8da 100644 --- a/src/tachyonClient.ts +++ b/src/tachyonClient.ts @@ -6,7 +6,7 @@ * and the client can send messages to the server. */ import { TypedEmitter } from 'tiny-typed-emitter'; -import { TachyonMessage, TACHYON_PROTOCOL_VERSION } from './tachyonTypes.js'; +import { parseTachyonMessage, TachyonMessage, TACHYON_PROTOCOL_VERSION } from './tachyonTypes.js'; import { getAccessToken } from './oauth2Client.js'; import WebSocket from 'ws'; @@ -95,14 +95,15 @@ export class TachyonClient extends TypedEmitter<{ this.close(); return; } + let tachyonMsg: TachyonMessage; try { - const tachyonMsg = JSON.parse(msg.toString('utf-8')); - this.emit('message', tachyonMsg); + tachyonMsg = parseTachyonMessage(msg.toString('utf-8')); } catch (e) { ws.close(1008, 'Failed to parse base tachyon message'); this.close(); return; } + this.emit('message', tachyonMsg); }); }