Skip to content

Commit 24f0757

Browse files
committed
#RI-5376 - fix pr comments
1 parent a3c26ae commit 24f0757

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

redisinsight/ui/src/utils/parseRedisUrl.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Maybe, Nullable } from 'uiSrc/utils/types'
22

33
/*
4-
redis[s]:// - Protocol (redis or rediss)
4+
[redis[s]://] - Optional Protocol (redis or rediss)
55
[username][:password]@ - Optional username and password
66
host - Hostname or IP address
77
[:port] - Optional port
@@ -17,7 +17,22 @@ interface ParsedRedisUrl {
1717
dbNumber: Maybe<number>
1818
}
1919

20-
const parseRedisUrl = (urlString: string): Nullable<ParsedRedisUrl> => {
20+
const parseRedisUrl = (urlString: string = ''): Nullable<ParsedRedisUrl> => {
21+
const pureUrlPattern = /^([^:]+):(\d+)$/
22+
const pureMatch = urlString.match(pureUrlPattern)
23+
24+
if (pureMatch) {
25+
const [, host, port] = pureMatch
26+
return {
27+
protocol: 'redis',
28+
username: '',
29+
password: '',
30+
host,
31+
port: port ? parseInt(port, 10) : undefined,
32+
dbNumber: undefined
33+
}
34+
}
35+
2136
// eslint-disable-next-line no-useless-escape
2237
const redisUrlPattern = /^(redis[s]?):\/\/(?:([^:@]+)?(?::([^@]+))?@)?([^:\/]+)(?::(\d+))?(?:\/(\d+))?$/
2338
const match = urlString.match(redisUrlPattern)
@@ -29,7 +44,7 @@ const parseRedisUrl = (urlString: string): Nullable<ParsedRedisUrl> => {
2944
const [, protocol, username, password, host, port, dbNumber] = match
3045

3146
return {
32-
protocol,
47+
protocol: protocol || 'redis',
3348
username: username || '',
3449
password: password || '',
3550
host,

redisinsight/ui/src/utils/tests/parseRedisUrl.spec.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { parseRedisUrl } from 'uiSrc/utils/parseRedisUrl'
22

33
const defaultRedisParams = {
4+
protocol: 'redis',
45
username: '',
56
password: '',
67
port: undefined,
@@ -20,29 +21,37 @@ const parseRedisUrlTests: Array<[string, any]> = [
2021
'redis://us@er:pass@localhost:6380',
2122
null
2223
],
24+
[
25+
'localhost',
26+
null
27+
],
28+
[
29+
'localhost:6379',
30+
{ ...defaultRedisParams, host: 'localhost', port: 6379 }
31+
],
2332
[
2433
'redis://localhost',
25-
{ ...defaultRedisParams, protocol: 'redis', host: 'localhost' }
34+
{ ...defaultRedisParams, host: 'localhost' }
2635
],
2736
[
2837
'redis://localhost:6380',
29-
{ ...defaultRedisParams, protocol: 'redis', host: 'localhost', port: 6380 }
38+
{ ...defaultRedisParams, host: 'localhost', port: 6380 }
3039
],
3140
[
3241
'redis://@localhost:6380',
33-
{ ...defaultRedisParams, protocol: 'redis', host: 'localhost', port: 6380 }
42+
{ ...defaultRedisParams, host: 'localhost', port: 6380 }
3443
],
3544
[
3645
'redis://user@localhost:6380',
37-
{ ...defaultRedisParams, username: 'user', protocol: 'redis', host: 'localhost', port: 6380 }
46+
{ ...defaultRedisParams, username: 'user', host: 'localhost', port: 6380 }
3847
],
3948
[
4049
'redis://:pass@localhost:6380',
41-
{ ...defaultRedisParams, protocol: 'redis', password: 'pass', host: 'localhost', port: 6380 }
50+
{ ...defaultRedisParams, password: 'pass', host: 'localhost', port: 6380 }
4251
],
4352
[
4453
'redis://user:pass@localhost:6380',
45-
{ ...defaultRedisParams, protocol: 'redis', username: 'user', password: 'pass', host: 'localhost', port: 6380 }
54+
{ ...defaultRedisParams, username: 'user', password: 'pass', host: 'localhost', port: 6380 }
4655
],
4756
[
4857
'rediss://user:pa%712ss@localhost:6380',

0 commit comments

Comments
 (0)