Skip to content

Commit 7690bc2

Browse files
committed
fix: bus utf8 characters handling
Close #19
1 parent 35263ec commit 7690bc2

File tree

5 files changed

+51
-13
lines changed

5 files changed

+51
-13
lines changed

packages/bentocache/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
}
7373
},
7474
"dependencies": {
75-
"@boringnode/bus": "^0.5.0",
75+
"@boringnode/bus": "^0.6.0",
7676
"@poppinss/utils": "^6.7.3",
7777
"async-mutex": "^0.5.0",
7878
"chunkify": "^5.0.0",

packages/bentocache/src/bus/encoders/binary_encoder.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export class BinaryEncoder implements TransportEncoder {
8686
/**
8787
* Decode the given Buffer into a CacheBusMessage
8888
*/
89-
decode(data: string): any {
89+
decode(data: string | Buffer): any {
9090
let offset = 0
9191
const buffer = Buffer.isBuffer(data) ? data : Buffer.from(data, 'binary')
9292

packages/bentocache/src/drivers/redis.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ export function redisBusDriver(
2828
): CreateBusDriverResult {
2929
return {
3030
options,
31-
factory: () => new RedisTransport(options.connection, new BinaryEncoder()),
31+
factory: () => {
32+
return new RedisTransport(
33+
{ ...options.connection, useMessageBuffer: true },
34+
new BinaryEncoder(),
35+
)
36+
},
3237
}
3338
}
3439

packages/bentocache/tests/bus/bus.spec.ts

+38-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import { test } from '@japa/runner'
22
import { setTimeout } from 'node:timers/promises'
3-
import { RedisTransport } from '@boringnode/bus/transports/redis'
43
import { MemoryTransport } from '@boringnode/bus/transports/memory'
54

6-
import { RedisDriver } from '../../src/drivers/redis.js'
75
import { ChaosBus } from '../helpers/chaos/chaos_bus.js'
86
import { ChaosCache } from '../helpers/chaos/chaos_cache.js'
97
import { CacheBusMessageType } from '../../src/types/bus.js'
108
import { CacheFactory } from '../../factories/cache_factory.js'
9+
import { RedisDriver, redisBusDriver } from '../../src/drivers/redis.js'
1110
import { REDIS_CREDENTIALS, throwingFactory } from '../helpers/index.js'
12-
import { BinaryEncoder } from '../../src/bus/encoders/binary_encoder.js'
1311

1412
test.group('Bus synchronization', () => {
1513
test('synchronize multiple cache', async ({ assert }) => {
@@ -186,8 +184,13 @@ test.group('Bus synchronization', () => {
186184
})
187185

188186
test('binary encoding/decoding should works fine', async ({ assert, cleanup }, done) => {
189-
const bus1 = new RedisTransport(REDIS_CREDENTIALS, new BinaryEncoder()).setId('foo')
190-
const bus2 = new RedisTransport(REDIS_CREDENTIALS, new BinaryEncoder()).setId('bar')
187+
const bus1 = redisBusDriver({ connection: REDIS_CREDENTIALS })
188+
.factory(null as any)
189+
.setId('foo')
190+
191+
const bus2 = redisBusDriver({ connection: REDIS_CREDENTIALS })
192+
.factory(null as any)
193+
.setId('bar')
191194

192195
cleanup(async () => {
193196
await bus1.disconnect()
@@ -210,4 +213,34 @@ test.group('Bus synchronization', () => {
210213
})
211214
.waitForDone()
212215
.disableTimeout()
216+
217+
test('works with utf8 characters', async ({ assert }, done) => {
218+
const bus1 = redisBusDriver({ connection: REDIS_CREDENTIALS })
219+
.factory(null as any)
220+
.setId('foo')
221+
222+
const bus2 = redisBusDriver({ connection: REDIS_CREDENTIALS })
223+
.factory(null as any)
224+
.setId('bar')
225+
226+
const data = {
227+
keys: ['foo', '1', '2', 'bar', 'key::test', '🚀'],
228+
type: CacheBusMessageType.Set,
229+
}
230+
231+
bus1.subscribe('foo', (message: any) => {
232+
assert.deepInclude(message, data)
233+
done()
234+
})
235+
236+
await setTimeout(200)
237+
238+
await bus2.publish('foo', data)
239+
240+
await bus1.disconnect()
241+
242+
await bus2.disconnect()
243+
244+
await setTimeout(200)
245+
}).waitForDone()
213246
})

pnpm-lock.yaml

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)