Skip to content

Commit

Permalink
fix: bus utf8 characters handling
Browse files Browse the repository at this point in the history
Close #19
  • Loading branch information
Julien-R44 committed May 17, 2024
1 parent 35263ec commit 7690bc2
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/bentocache/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
}
},
"dependencies": {
"@boringnode/bus": "^0.5.0",
"@boringnode/bus": "^0.6.0",
"@poppinss/utils": "^6.7.3",
"async-mutex": "^0.5.0",
"chunkify": "^5.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/bentocache/src/bus/encoders/binary_encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class BinaryEncoder implements TransportEncoder {
/**
* Decode the given Buffer into a CacheBusMessage
*/
decode(data: string): any {
decode(data: string | Buffer): any {
let offset = 0
const buffer = Buffer.isBuffer(data) ? data : Buffer.from(data, 'binary')

Expand Down
7 changes: 6 additions & 1 deletion packages/bentocache/src/drivers/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ export function redisBusDriver(
): CreateBusDriverResult {
return {
options,
factory: () => new RedisTransport(options.connection, new BinaryEncoder()),
factory: () => {
return new RedisTransport(
{ ...options.connection, useMessageBuffer: true },
new BinaryEncoder(),
)
},
}
}

Expand Down
43 changes: 38 additions & 5 deletions packages/bentocache/tests/bus/bus.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { test } from '@japa/runner'
import { setTimeout } from 'node:timers/promises'
import { RedisTransport } from '@boringnode/bus/transports/redis'
import { MemoryTransport } from '@boringnode/bus/transports/memory'

import { RedisDriver } from '../../src/drivers/redis.js'
import { ChaosBus } from '../helpers/chaos/chaos_bus.js'
import { ChaosCache } from '../helpers/chaos/chaos_cache.js'
import { CacheBusMessageType } from '../../src/types/bus.js'
import { CacheFactory } from '../../factories/cache_factory.js'
import { RedisDriver, redisBusDriver } from '../../src/drivers/redis.js'
import { REDIS_CREDENTIALS, throwingFactory } from '../helpers/index.js'
import { BinaryEncoder } from '../../src/bus/encoders/binary_encoder.js'

test.group('Bus synchronization', () => {
test('synchronize multiple cache', async ({ assert }) => {
Expand Down Expand Up @@ -186,8 +184,13 @@ test.group('Bus synchronization', () => {
})

test('binary encoding/decoding should works fine', async ({ assert, cleanup }, done) => {
const bus1 = new RedisTransport(REDIS_CREDENTIALS, new BinaryEncoder()).setId('foo')
const bus2 = new RedisTransport(REDIS_CREDENTIALS, new BinaryEncoder()).setId('bar')
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()
Expand All @@ -210,4 +213,34 @@ test.group('Bus synchronization', () => {
})
.waitForDone()
.disableTimeout()

test('works with utf8 characters', async ({ assert }, 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')

const data = {
keys: ['foo', '1', '2', 'bar', 'key::test', '🚀'],
type: CacheBusMessageType.Set,
}

bus1.subscribe('foo', (message: any) => {
assert.deepInclude(message, data)
done()
})

await setTimeout(200)

await bus2.publish('foo', data)

await bus1.disconnect()

await bus2.disconnect()

await setTimeout(200)
}).waitForDone()
})
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7690bc2

Please sign in to comment.