Skip to content

Commit 42b7c11

Browse files
authored
Connecting via URI
* feat: connection via uri with refactored reference for options * chore: README.md
1 parent e3abeaa commit 42b7c11

4 files changed

Lines changed: 40 additions & 20 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,7 @@ await client.lpush('list', 'item-1', 'item-2');
155155
```typescript
156156
// Create client (with lazy connect)
157157
const client = new SolidisClient({
158-
host: '127.0.0.1',
159-
port: 6379,
158+
uri: 'redis://127.0.0.1:6379',
160159
lazyConnect: true
161160
}).extend({ get, set });
162161

@@ -248,6 +247,7 @@ Solidis provides extensive configuration options:
248247
```typescript
249248
const client = new SolidisClient({
250249
// Connection
250+
uri: 'redis://localhost:6379',
251251
host: '127.0.0.1',
252252
port: 6379,
253253
useTLS: false,

sources/common/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export const SolidisDefaultOptions: SolidisClientFrozenOptions = {
2323
debugMaxEntries: KB * 10,
2424
enableReadyCheck: true,
2525
host: '127.0.0.1',
26+
uri: false,
2627
lazyConnect: false,
2728
maxConnectionRetries: 20,
2829
maxCommandsPerPipeline: 300,

sources/modules/connection.ts

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export class SolidisConnection extends EventEmitter {
6868
return await this.#connectLock;
6969
}
7070

71-
this.#connectLock = this.#tryConnectWithRetry(options);
71+
this.#connectLock = this.#tryConnectWithRetry();
7272

7373
try {
7474
await this.#connectLock;
@@ -102,39 +102,37 @@ export class SolidisConnection extends EventEmitter {
102102
this.emit('end');
103103
}
104104

105-
async #tryConnectWithRetry(options: SolidisClientFrozenOptions) {
105+
async #tryConnectWithRetry() {
106106
let attemptIndex = 0;
107107

108-
const maxConnectionRetries = options.maxConnectionRetries;
108+
const maxConnectionRetries = this.#options.maxConnectionRetries;
109109

110110
while (true) {
111111
attemptIndex += 1;
112112

113113
try {
114-
await this.#tryConnect(options);
114+
await this.#tryConnect();
115115
return;
116116
} catch (error) {
117117
if (this.#isQuitted) {
118118
throw wrapWithSolidisConnectionError(error);
119119
}
120120

121121
if (attemptIndex > maxConnectionRetries) {
122-
throw wrapWithSolidisConnectionError(
123-
new SolidisConnectionError(
124-
`SolidisClient connection failed after ${maxConnectionRetries} retries.`,
125-
error,
126-
),
122+
throw new SolidisConnectionError(
123+
`SolidisClient connection failed after ${maxConnectionRetries} retries.`,
124+
error,
127125
);
128126
}
129127

130128
await new Promise<void>((resolve) =>
131-
setTimeout(resolve, options.connectionRetryDelay),
129+
setTimeout(resolve, this.#options.connectionRetryDelay),
132130
);
133131
}
134132
}
135133
}
136134

137-
async #tryConnect(options: SolidisClientFrozenOptions) {
135+
async #tryConnect() {
138136
if (this.#isQuitted) {
139137
throw new SolidisConnectionError(
140138
'Cannot connect: user quit the connection.',
@@ -155,7 +153,7 @@ export class SolidisConnection extends EventEmitter {
155153
};
156154

157155
const timeoutHandle = this.#setupConnectionTimeout(
158-
options.connectionTimeout,
156+
this.#options.connectionTimeout,
159157
(reason?: unknown) => onFail(reason),
160158
);
161159

@@ -176,18 +174,20 @@ export class SolidisConnection extends EventEmitter {
176174
this.emit('connect');
177175
};
178176

177+
const { host, port, useTLS } = this.#parseSocketOptions();
178+
179179
const socket = this.#createSocket({
180-
host: options.host,
181-
port: options.port,
182-
useTLS: options.useTLS,
180+
host,
181+
port,
182+
useTLS,
183183
onConnect,
184184
});
185185

186186
this.#setupSocketErrorHandler(socket);
187187
this.#setupSocketCloseHandler(socket, onFail);
188188

189-
socket.setMaxListeners(options.maxEventListenersForSocket);
190-
this.setMaxListeners(options.maxEventListenersForClient);
189+
socket.setMaxListeners(this.#options.maxEventListenersForSocket);
190+
this.setMaxListeners(this.#options.maxEventListenersForClient);
191191

192192
this.#socket = socket;
193193
});
@@ -202,7 +202,7 @@ export class SolidisConnection extends EventEmitter {
202202
return await this.#connectLock;
203203
}
204204

205-
this.#connectLock = this.#tryConnectWithRetry(this.#options);
205+
this.#connectLock = this.#tryConnectWithRetry();
206206

207207
try {
208208
await this.#connectLock;
@@ -302,4 +302,22 @@ export class SolidisConnection extends EventEmitter {
302302

303303
return timer;
304304
}
305+
306+
#parseSocketOptions() {
307+
if (this.#options.uri) {
308+
const url = new URL(this.#options.uri);
309+
310+
return {
311+
host: url.hostname,
312+
port: url.port ? Number.parseInt(url.port) : 6379,
313+
useTLS: this.#options.useTLS || url.protocol === 'rediss:',
314+
};
315+
}
316+
317+
return {
318+
host: this.#options.host,
319+
port: this.#options.port,
320+
useTLS: this.#options.useTLS,
321+
};
322+
}
305323
}

sources/types/solidis.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export interface SolidisClientOptions {
5151
debugMaxEntries?: number;
5252
enableReadyCheck?: boolean;
5353
host?: string;
54+
uri?: string | URL | false;
5455
lazyConnect?: boolean;
5556
maxConnectionRetries?: number;
5657
maxCommandsPerPipeline?: number;

0 commit comments

Comments
 (0)