MCPcopy
hub / github.com/redis/node-redis / parseURL

Method parseURL

packages/client/lib/client/index.ts:417–481  ·  view source on GitHub ↗
(url: string)

Source from the content-addressed store, hash-verified

415 }
416
417 static parseURL(url: string): AnyRedisClientOptions & {
418 socket: Exclude<AnyRedisClientOptions['socket'], undefined> & {
419 tls: boolean
420 }
421 } {
422 // unix:// URIs use a non-special scheme; WHATWG URL refuses to parse an
423 // authority (e.g. `user:pass@`) without a host, so handle it separately.
424 if (url.startsWith('unix:')) {
425 return RedisClient.#parseUnixURL(url);
426 }
427
428 // https://www.iana.org/assignments/uri-schemes/prov/redis
429 const { hostname, port, protocol, username, password, pathname } = new URL(url),
430 parsed: AnyRedisClientOptions & {
431 socket: Exclude<AnyRedisClientOptions['socket'], undefined> & {
432 tls: boolean
433 }
434 } = {
435 socket: {
436 // Use net.SocketAddress.parse() once supported.
437 host: hostname.replace(/^\[([0-9a-f:]+)\]$/, '$1'),
438 tls: false
439 }
440 };
441
442 if (protocol !== 'redis:' && protocol !== 'rediss:') {
443 throw new TypeError('Invalid protocol');
444 }
445
446 parsed.socket.tls = protocol === 'rediss:';
447
448 if (port) {
449 (parsed.socket as TcpSocketConnectOpts).port = Number(port);
450 }
451
452 if (username) {
453 parsed.username = decodeURIComponent(username);
454 }
455
456 if (password) {
457 parsed.password = decodeURIComponent(password);
458 }
459
460 if (username || password) {
461 parsed.credentialsProvider = {
462 type: 'async-credentials-provider',
463 credentials: async () => (
464 {
465 username: username ? decodeURIComponent(username) : undefined,
466 password: password ? decodeURIComponent(password) : undefined
467 })
468 };
469 }
470
471 if (pathname.length > 1) {
472 const database = Number(pathname.substring(1));
473 if (isNaN(database)) {
474 throw new TypeError('Invalid pathname');

Callers 2

parseOptionsMethod · 0.80
index.spec.tsFile · 0.80

Calls 1

#parseUnixURLMethod · 0.80

Tested by

no test coverage detected