mirror of
https://github.com/logto-io/logto.git
synced 2024-12-16 20:26:19 -05:00
refactor(core): allow non-normative redis url
This commit is contained in:
parent
aa9b7aea5c
commit
c4c22a50fb
2 changed files with 22 additions and 7 deletions
|
@ -1,4 +1,7 @@
|
|||
import { createMockUtils } from '@logto/shared/esm';
|
||||
import Sinon from 'sinon';
|
||||
|
||||
import { EnvSet } from '#src/env-set/index.js';
|
||||
|
||||
const { jest } = import.meta;
|
||||
const { mockEsm } = createMockUtils(jest);
|
||||
|
@ -26,7 +29,7 @@ mockEsm('redis', () => ({
|
|||
}),
|
||||
}));
|
||||
|
||||
const { RedisCache, RedisClusterCache } = await import('./index.js');
|
||||
const { RedisCache, RedisClusterCache, redisCacheFactory } = await import('./index.js');
|
||||
|
||||
describe('RedisCache', () => {
|
||||
it('should successfully construct with no REDIS_URL', async () => {
|
||||
|
@ -46,8 +49,13 @@ describe('RedisCache', () => {
|
|||
it('should successfully construct with a Redis client', async () => {
|
||||
for (const url of ['1', 'redis://url']) {
|
||||
jest.clearAllMocks();
|
||||
const cache = new RedisCache(url);
|
||||
const stub = Sinon.stub(EnvSet, 'values').value({
|
||||
...EnvSet.values,
|
||||
redisUrl: url,
|
||||
});
|
||||
|
||||
const cache = redisCacheFactory();
|
||||
expect(cache instanceof RedisCache).toBeTruthy();
|
||||
expect(cache.client).toBeTruthy();
|
||||
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
|
@ -61,14 +69,20 @@ describe('RedisCache', () => {
|
|||
|
||||
// Do sanity check only
|
||||
expect(mockFunction).toBeCalledTimes(6);
|
||||
stub.restore();
|
||||
}
|
||||
});
|
||||
|
||||
it('should successfully construct with a Redis Cluster client', async () => {
|
||||
for (const url of ['redis://url', 'redis:?host=h1&host=h2&host=h3']) {
|
||||
for (const url of ['redis://url?cluster=1', 'redis:?host=h1&host=h2&host=h3&cluster=true']) {
|
||||
jest.clearAllMocks();
|
||||
const cache = new RedisClusterCache(new URL(url));
|
||||
const stub = Sinon.stub(EnvSet, 'values').value({
|
||||
...EnvSet.values,
|
||||
redisUrl: url,
|
||||
});
|
||||
|
||||
const cache = redisCacheFactory();
|
||||
expect(cache instanceof RedisClusterCache).toBeTruthy();
|
||||
expect(cache.client).toBeTruthy();
|
||||
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
|
@ -82,6 +96,7 @@ describe('RedisCache', () => {
|
|||
|
||||
// Do sanity check only
|
||||
expect(mockFunction).toBeCalledTimes(6);
|
||||
stub.restore();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
@ -135,12 +135,12 @@ export class RedisClusterCache extends RedisCacheBase {
|
|||
}
|
||||
}
|
||||
|
||||
const redisCacheFactory = (): RedisCacheBase => {
|
||||
export const redisCacheFactory = (): RedisCacheBase => {
|
||||
const { redisUrl } = EnvSet.values;
|
||||
|
||||
if (redisUrl) {
|
||||
const url = new URL(redisUrl);
|
||||
if (yes(url.searchParams.get('redis_cluster'))) {
|
||||
const url = trySafe(() => new URL(redisUrl));
|
||||
if (url && yes(url.searchParams.get('cluster'))) {
|
||||
return new RedisClusterCache(url);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue