Skip to content

Unable to connect to literal IPv6 addresses via the url option #3175

@TimWolla

Description

@TimWolla

Description

When using the url option one cannot connect to IPv6 addresses:

let redis = require('redis');
let c = redis.createClient({ url: 'redis://[::1]:6379', socket: { reconnectStrategy: false } });

c.connect();

results in:

Error: getaddrinfo ENOTFOUND [::1]
    at GetAddrInfoReqWrap.onlookupall [as oncomplete] (node:dns:122:26)

It works fine when passing the IPv6 address within the hostname:

let redis = require('redis');
let c = redis.createClient({ socket: { reconnectStrategy: false, hostname: '::1', port: 6379 } });

c.connect();

It worked fine with node-redis 3.x, but no longer works with node-redis 4.x. The issue is that the parse function from the url module stripped the square brackets, but the global URL constructor does not:

> require('url').parse('redis://[::1]:6379');
Url {
  protocol: 'redis:',
  slashes: true,
  auth: null,
  host: '[::1]:6379',
  port: '6379',
  hostname: '::1',
  hash: null,
  search: null,
  query: null,
  pathname: '/',
  path: '/',
  href: 'redis://[::1]:6379/'
}
> new URL('redis://[::1]:6379')
URL {
  href: 'redis://[::1]:6379',
  origin: 'null',
  protocol: 'redis:',
  username: '',
  password: '',
  host: '[::1]:6379',
  hostname: '[::1]',
  port: '6379',
  pathname: '',
  search: '',
  searchParams: URLSearchParams {},
  hash: ''
}

This is a new version of #2734, which I am unable to reopen.

Node.js Version

v24.13.0

Redis Server Version

No response

Node Redis Version

5.10.0

Platform

Linux

Logs

node:internal/process/promises:394
    triggerUncaughtException(err, true /* fromPromise */);
    ^

Error: getaddrinfo ENOTFOUND [::1]
    at GetAddrInfoReqWrap.onlookupall [as oncomplete] (node:dns:122:26)
Emitted 'error' event on Class instance at:
    at RedisSocket.<anonymous> (/pwd/node_modules/@redis/client/dist/lib/client/index.js:445:18)
    at RedisSocket.emit (node:events:508:28)
    at #shouldReconnect (/pwd/node_modules/@redis/client/dist/lib/client/socket.js:132:18)
    at #connect (/pwd/node_modules/@redis/client/dist/lib/client/socket.js:178:54)
    at process.processTicksAndRejections (node:internal/process/task_queues:103:5)
    at async Class.connect (/pwd/node_modules/@redis/client/dist/lib/client/index.js:556:9) {
  errno: -3008,
  code: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: '[::1]'
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions