Redis URL Parser (Simple)

1.0.2 · active · verified Tue Apr 21

parse-redis-url-simple is a focused JavaScript utility designed for parsing Redis connection URLs, including complex scenarios like Redis Sentinel configurations. It provides a simple, direct API for converting various URL formats into structured objects containing host, port, database, and password. Unlike several unmaintained or production-unsuitable prior art libraries mentioned in its README, this package aims to offer a robust and reliable solution for production environments. Currently at version 1.0.2, it appears to follow a stable release cadence typical of utility libraries, with updates primarily for bug fixes or minor enhancements. A key differentiator is its explicit support for ioredis-compatible Sentinel URLs, addressing a common need in distributed Redis setups.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to parse various Redis URL formats, including single instances, multiple comma-separated instances, URLs with passwords, and Redis Sentinel configurations using the `parseRedisUrl` function.

import parseRedisUrl from 'parse-redis-url-simple';

// Parse a basic Redis URL with a specific database
const basicUrl = 'redis://localhost:6379/1';
console.log('Basic URL:', parseRedisUrl(basicUrl));
// => [{host: 'localhost', port: 6379, database: '1', password: undefined}]

// Parse multiple Redis instances from a comma-separated string
const multipleUrls = 'redis://barhost.com:39143/,redis://foohost.com:39143/';
console.log('Multiple URLs:', parseRedisUrl(multipleUrls));
// => [{host: 'barhost.com', port: 39143, database: '0', password: undefined}, {host: 'foohost.com', port: 39143, database: '0', password: undefined}]

// Parse a Redis URL that includes a password (username is ignored)
const passwordUrl = 'redis://user:n9y25ah7@foohost.com:39143/';
console.log('Password URL:', parseRedisUrl(passwordUrl));
// => [{host: 'foohost.com', port: 39143, database: '0', password: 'n9y25ah7'}]

// Parse Redis Sentinel URLs by passing `true` as the second argument
const sentinelHosts = 'barhost.com:39143,foohost.com:39143,foobarhost.com:39143';
console.log('Sentinel Hosts:', parseRedisUrl(sentinelHosts, true));
// => [{host: 'barhost.com', port: 39143},{host: 'foohost.com', port: 39143},{host: 'foobarhost.com', port: 39143}]

view raw JSON →