From d4837cedd1fd2bd2a29d99384a9e2ea1f54ad541 Mon Sep 17 00:00:00 2001 From: Gautam Kachru Date: Sat, 5 Oct 2024 03:10:58 +0530 Subject: [PATCH] fix: BinaryEncoding did not encode/decode the Clear message type (#38) --- .../src/bus/encoders/binary_encoder.ts | 16 ++++++++-- packages/bentocache/tests/bus/bus.spec.ts | 31 +++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/packages/bentocache/src/bus/encoders/binary_encoder.ts b/packages/bentocache/src/bus/encoders/binary_encoder.ts index 766b9df..d92f476 100644 --- a/packages/bentocache/src/bus/encoders/binary_encoder.ts +++ b/packages/bentocache/src/bus/encoders/binary_encoder.ts @@ -30,6 +30,18 @@ export class BinaryEncoder implements TransportEncoder { this.#busIdLength = busIdLength } + protected busMessageTypeToNum(type: CacheBusMessageType): number { + if (type === CacheBusMessageType.Set) return 0x01 + if (type === CacheBusMessageType.Clear) return 0x02 + return 0x03 + } + + protected numToBusMessageType(num: number): CacheBusMessageType { + if (num === 0x01) return CacheBusMessageType.Set + if (num === 0x02) return CacheBusMessageType.Clear + return CacheBusMessageType.Delete + } + /** * Encode the given message into a Buffer */ @@ -59,7 +71,7 @@ export class BinaryEncoder implements TransportEncoder { /** * 2. write the message type. 0x01 for 'Set' message, and 0x02 for a 'Delete' message */ - buffer.writeUInt8(payload.type === CacheBusMessageType.Set ? 0x01 : 0x02, this.#busIdLength) + buffer.writeUInt8(this.busMessageTypeToNum(payload.type), this.#busIdLength) /** * 3. Write the keys @@ -100,7 +112,7 @@ export class BinaryEncoder implements TransportEncoder { * Then comes the message type as a single byte */ const typeValue = buffer.readUInt8(offset++) - const type = typeValue === 0x01 ? CacheBusMessageType.Set : CacheBusMessageType.Delete + const type = this.numToBusMessageType(typeValue) /** * Finally, the keys diff --git a/packages/bentocache/tests/bus/bus.spec.ts b/packages/bentocache/tests/bus/bus.spec.ts index 0d9df72..977faa9 100644 --- a/packages/bentocache/tests/bus/bus.spec.ts +++ b/packages/bentocache/tests/bus/bus.spec.ts @@ -269,6 +269,37 @@ test.group('Bus synchronization', () => { }) .waitForDone() .disableTimeout() + + test('binary encoding/decoding using Clear should be fine', async ({ assert, cleanup }, done) => { + const bus1 = redisBusDriver({ connection: REDIS_CREDENTIALS }) + .factory(null as any) + .setId('foo') + + const bus2 = redisBusDriver({ connection: REDIS_CREDENTIALS }) + .factory(null as any) + .setId('bar') + + cleanup(async () => { + await bus1.disconnect() + await bus2.disconnect() + }) + + const data = { + keys: [], + type: CacheBusMessageType.Clear, + } + + bus1.subscribe('foo', (message: any) => { + assert.deepInclude(message, data) + done() + }) + + await setTimeout(200) + + await bus2.publish('foo', data) + }) + .waitForDone() + .disableTimeout() test('works with utf8 characters', async ({ assert }, done) => { const bus1 = redisBusDriver({ connection: REDIS_CREDENTIALS })