Skip to content

Commit 0c8090d

Browse files
committed
fix: allow pass in redis url as argument
1 parent c449a02 commit 0c8090d

File tree

6 files changed

+25
-21
lines changed

6 files changed

+25
-21
lines changed

bin/worker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ if (s3Endpoint) {
2727
store = createMemoryStorage()
2828
}
2929

30-
api.createWorker(store, redisPrefix)
30+
api.createWorker(store, { redisPrefix })

src/api.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ const decodeRedisRoomStreamName = (rediskey, expectedPrefix) => {
8686

8787
/**
8888
* @param {import('./storage.js').AbstractStorage} store
89-
* @param {string} redisPrefix
89+
* @param {{ redisPrefix?: string, redisUrl?: string }} opts
9090
*/
91-
export const createApiClient = async (store, redisPrefix) => {
92-
const a = new Api(store, redisPrefix)
91+
export const createApiClient = async (store, { redisPrefix, redisUrl }) => {
92+
const a = new Api(store, redisPrefix, redisUrl)
9393
await a.redis.connect()
9494
try {
9595
await a.redis.xGroupCreate(a.redisWorkerStreamName, a.redisWorkerGroupName, '0', { MKSTREAM: true })
@@ -100,9 +100,10 @@ export const createApiClient = async (store, redisPrefix) => {
100100
export class Api {
101101
/**
102102
* @param {import('./storage.js').AbstractStorage} store
103-
* @param {string} prefix
103+
* @param {string=} prefix
104+
* @param {string=} url
104105
*/
105-
constructor (store, prefix) {
106+
constructor (store, prefix = 'y', url = redisUrl) {
106107
this.store = store
107108
this.prefix = prefix
108109
this.consumername = random.uuidv4()
@@ -119,7 +120,7 @@ export class Api {
119120
this.redisWorkerGroupName = this.prefix + ':worker'
120121
this._destroyed = false
121122
this.redis = redis.createClient({
122-
url: redisUrl,
123+
url,
123124
// scripting: https://github.com/redis/node-redis/#lua-scripts
124125
scripts: {
125126
addMessage: redis.defineScript({
@@ -333,10 +334,10 @@ export class Api {
333334

334335
/**
335336
* @param {import('./storage.js').AbstractStorage} store
336-
* @param {string} redisPrefix
337+
* @param {{ redisPrefix?: string, redisUrl?: string }} opts
337338
*/
338-
export const createWorker = async (store, redisPrefix) => {
339-
const a = await createApiClient(store, redisPrefix)
339+
export const createWorker = async (store, opts) => {
340+
const a = await createApiClient(store, opts)
340341
return new Worker(a)
341342
}
342343

src/subscriber.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ const run = async subscriber => {
3232

3333
/**
3434
* @param {import('./storage.js').AbstractStorage} store
35-
* @param {string} redisPrefix
35+
* @param {{ redisPrefix?: string, redisUrl?: string }} opts
3636
*/
37-
export const createSubscriber = async (store, redisPrefix) => {
38-
const client = await api.createApiClient(store, redisPrefix)
37+
export const createSubscriber = async (store, opts) => {
38+
const client = await api.createApiClient(store, opts)
3939
return new Subscriber(client)
4040
}
4141

src/y-socket-io/y-socket-io.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,13 @@ export class YSocketIO {
109109
*
110110
* It also starts socket connection listeners.
111111
* @param {import('../storage.js').AbstractStorage} store
112-
* @param {{ redisPrefix?: string }=} redisPrefix
112+
* @param {{ redisPrefix?: string, redisUrl?: string }=} opts
113113
* @public
114114
*/
115-
async initialize (store, { redisPrefix = 'y' } = {}) {
115+
async initialize (store, { redisUrl, redisPrefix = 'y' } = {}) {
116116
const [client, subscriber] = await promise.all([
117-
api.createApiClient(store, redisPrefix),
118-
createSubscriber(store, redisPrefix)
117+
api.createApiClient(store, { redisUrl, redisPrefix }),
118+
createSubscriber(store, { redisUrl, redisPrefix })
119119
])
120120
this.client = client
121121
this.subscriber = subscriber

tests/api.tests.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const createTestCase = async tc => {
2020
const keysToDelete = await redisClient.keys(redisPrefix + ':*')
2121
keysToDelete.length > 0 && await redisClient.del(keysToDelete)
2222
await redisClient.quit()
23-
const client = await api.createApiClient(store, redisPrefix)
23+
const client = await api.createApiClient(store, { redisPrefix })
2424
prevClients.push(client)
2525
const room = tc.testName
2626
const docid = 'main'
@@ -44,7 +44,7 @@ const createTestCase = async tc => {
4444
}
4545

4646
const createWorker = async () => {
47-
const worker = await api.createWorker(store, redisPrefix)
47+
const worker = await api.createWorker(store, { redisPrefix })
4848
worker.client.redisMinMessageLifetime = 200
4949
worker.client.redisWorkerTimeout = 50
5050
prevClients.push(worker.client)

tests/socketio.tests.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ const createWsClient = (tc, room) => {
3434
}
3535

3636
const createWorker = async () => {
37-
const worker = await api.createWorker(utils.store, utils.redisPrefix)
37+
const worker = await api.createWorker(
38+
utils.store,
39+
{ redisPrefix: utils.redisPrefix }
40+
)
3841
worker.client.redisMinMessageLifetime = 500
3942
worker.client.redisWorkerTimeout = 100
4043
utils.prevClients.push(worker.client)
@@ -52,7 +55,7 @@ const createServer = async () => {
5255
}
5356

5457
const createApiClient = async () => {
55-
const client = await api.createApiClient(utils.store, utils.redisPrefix)
58+
const client = await api.createApiClient(utils.store, { redisPrefix: utils.redisPrefix })
5659
utils.prevClients.push(client)
5760
return client
5861
}

0 commit comments

Comments
 (0)