-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
createClient url+tls invariant violation check #2835
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -157,21 +157,45 @@ export default class RedisClient< | |
return new (RedisClient.extend(options))(options); | ||
} | ||
|
||
static parseURL(url: string): RedisClientOptions { | ||
static parseOptions<O extends RedisClientOptions>(options: O): O { | ||
if (options?.url) { | ||
const parsed = RedisClient.parseURL(options.url); | ||
if (options.socket) { | ||
if (options.socket.tls !== undefined && options.socket.tls !== parsed.socket.tls) { | ||
throw new TypeError(`tls socket option is set to ${options.socket.tls} which is mismatch with protocol or the URL ${options.url} passed`) | ||
} | ||
parsed.socket = Object.assign(options.socket, parsed.socket); | ||
} | ||
|
||
Object.assign(options, parsed); | ||
} | ||
return options; | ||
} | ||
|
||
static parseURL(url: string): RedisClientOptions & { | ||
socket: Exclude<RedisClientOptions['socket'], undefined> & { | ||
tls: boolean | ||
} | ||
} { | ||
// https://www.iana.org/assignments/uri-schemes/prov/redis | ||
const { hostname, port, protocol, username, password, pathname } = new URL(url), | ||
parsed: RedisClientOptions = { | ||
parsed: RedisClientOptions & { | ||
socket: Exclude<RedisClientOptions['socket'], undefined> & { | ||
tls: boolean | ||
} | ||
} = { | ||
socket: { | ||
host: hostname | ||
host: hostname, | ||
tls: false | ||
} | ||
}; | ||
|
||
if (protocol === 'rediss:') { | ||
(parsed.socket as RedisTlsSocketOptions).tls = true; | ||
} else if (protocol !== 'redis:') { | ||
if (protocol !== 'redis:' && protocol !== 'rediss:') { | ||
throw new TypeError('Invalid protocol'); | ||
} | ||
|
||
parsed.socket.tls = protocol === 'rediss:'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "DRY enough": at this point |
||
|
||
if (port) { | ||
(parsed.socket as TcpSocketConnectOpts).port = Number(port); | ||
} | ||
|
@@ -239,19 +263,14 @@ export default class RedisClient< | |
} | ||
|
||
#initiateOptions(options?: RedisClientOptions<M, F, S>): RedisClientOptions<M, F, S> | undefined { | ||
if (options?.url) { | ||
const parsed = RedisClient.parseURL(options.url); | ||
if (options.socket) { | ||
parsed.socket = Object.assign(options.socket, parsed.socket); | ||
} | ||
|
||
Object.assign(options, parsed); | ||
} | ||
|
||
if (options?.database) { | ||
this.#selectedDB = options.database; | ||
} | ||
|
||
if (options) { | ||
return RedisClient.parseOptions(options); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe better to put before |
||
} | ||
|
||
return options; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
union is a bit wordy, please guide me where you'd like to define an interface if you think it's not ok to inline that