Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion packages/client/lib/client/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ describe('Client', () => {
{
socket: {
host: 'localhost',
port: 6379
port: 6379,
tls: false
},
username: 'user',
password: 'secret',
Expand Down Expand Up @@ -103,12 +104,58 @@ describe('Client', () => {
{
socket: {
host: 'localhost',
tls: false
}
}
);
});
});

describe('parseOptions', () => {
it('should throw error if tls socket option is set to true and the url protocol is "redis:"', () => {
assert.throws(
() => RedisClient.parseOptions({
url: 'redis://localhost',
socket: {
tls: true
}
}),
TypeError
);
});
it('should throw error if tls socket option is set to false and the url protocol is "rediss:"', () => {
assert.throws(
() => RedisClient.parseOptions({
url: 'rediss://localhost',
socket: {
tls: false
}
}),
TypeError
);
});
it('should not throw when tls socket option and url protocol matches"', () => {
assert.equal(
RedisClient.parseOptions({
url: 'rediss://localhost',
socket: {
tls: true
}
}).socket.tls,
true
);
assert.equal(
RedisClient.parseOptions({
url: 'redis://localhost',
socket: {
tls: false
}
}).socket.tls,
false
);
});
});

describe('connect', () => {
testUtils.testWithClient('connect should return the client instance', async client => {
try {
Expand Down
49 changes: 34 additions & 15 deletions packages/client/lib/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> & {
Copy link
Author

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

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:';
Copy link
Author

@dearlordylord dearlordylord Sep 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"DRY enough": at this point protocol is a literal union and won't accept anything except those two literals


if (port) {
(parsed.socket as TcpSocketConnectOpts).port = Number(port);
}
Expand Down Expand Up @@ -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);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe better to put before this.#selectedDB = options.database but it's a private method use in a constructor anyways?

}

return options;
}

Expand Down