From c482ee869aa02979c248836b66aca7b4e8e5e13e Mon Sep 17 00:00:00 2001 From: codax69 Date: Sun, 8 Nov 2026 23:06:16 +0530 Subject: [PATCH] fix(query-redis-labs): support custom redis port and url --- node/query-redis-labs/README.md | 37 ++++++++++++++++++------ node/query-redis-labs/src/main.js | 4 ++- node/query-redis-labs/src/redis.js | 46 ++++++++++++++++++++++++++---- 3 files changed, 72 insertions(+), 15 deletions(-) diff --git a/node/query-redis-labs/README.md b/node/query-redis-labs/README.md index 80494283..e5569c27 100644 --- a/node/query-redis-labs/README.md +++ b/node/query-redis-labs/README.md @@ -33,18 +33,37 @@ Sample `200` Response: ### REDIS_HOST -The endpoint to connect to your Redis database. +The endpoint or hostname to connect to your Redis database. Required only when `REDIS_URL` is absent. -| Question | Answer | -| ------------ | ------------------------------ | -| Required | Yes | +| Question | Answer | +| ------------ | ------------------------------------- | +| Required | Yes (only when `REDIS_URL` is absent) | | Sample Value | `redis-13258.c35.eu-central-1-1.ec2.redns.redis-cloud.com` | +### REDIS_PORT + +The port to connect to your Redis database. + +| Question | Answer | +| ------------- | ------- | +| Required | No | +| Default Value | `6379` | +| Sample Value | `13258` | + ### REDIS_PASSWORD -Authentication password to access your Redis database. +Authentication password to access your Redis database. Required only when `REDIS_URL` is absent. + +| Question | Answer | +| ------------ | ------------------------------------- | +| Required | Yes (only when `REDIS_URL` is absent) | +| Sample Value | `efNNehiACfcZiwsTAjcK6xiwPyu6Dpdq` | + +### REDIS_URL + +A full connection URL to connect to your Redis database (optional alternative to host/port/password). -| Question | Answer | -| ------------ | ------------------------------ | -| Required | Yes | -| Sample Value | `efNNehiACfcZiwsTAjcK6xiwPyu6Dpdq` | +| Question | Answer | +| ------------ | ------------------------------------------------------------------------------------------------------------------- | +| Required | No | +| Sample Value | `redis://default:efNNehiACfcZiwsTAjcK6xiwPyu6Dpdq@redis-13258.c35.eu-central-1-1.ec2.redns.redis-cloud.com:13258` | diff --git a/node/query-redis-labs/src/main.js b/node/query-redis-labs/src/main.js index 04cb4d42..c1c7bee2 100644 --- a/node/query-redis-labs/src/main.js +++ b/node/query-redis-labs/src/main.js @@ -7,7 +7,9 @@ import { getClient, getWarehouse, insertTestData } from "./redis.js"; let client; export default async ({ req, res, log, error }) => { - throwIfMissing(process.env, ['REDIS_HOST', 'REDIS_PASSWORD']); + if (!process.env.REDIS_URL) { + throwIfMissing(process.env, ['REDIS_HOST', 'REDIS_PASSWORD']); + } if(req.method !== 'GET') { return res.text('Not found.', 404); diff --git a/node/query-redis-labs/src/redis.js b/node/query-redis-labs/src/redis.js index 260e3a0d..6176b553 100644 --- a/node/query-redis-labs/src/redis.js +++ b/node/query-redis-labs/src/redis.js @@ -1,18 +1,54 @@ import { createClient } from 'redis'; import { createHash } from 'node:crypto'; +import { isIP } from 'node:net'; /** * Throws an error if any of the keys are missing from the object */ export async function getClient() { - const { REDIS_PASSWORD, REDIS_HOST } = process.env; + const { REDIS_PASSWORD, REDIS_HOST, REDIS_PORT, REDIS_URL } = process.env; - const client = createClient({ + if (REDIS_URL) { + const client = createClient({ + url: REDIS_URL, + }); + + await client.connect(); + + return client; + } + + let host = REDIS_HOST; + let port = REDIS_PORT ? parseInt(REDIS_PORT, 10) : 6379; + + if (host) { + if (isIP(host) === 6) { + // Bare IPv6 address, keep host intact without extracting port + } else if (host.startsWith('[')) { + const match = host.match(/^\[([^\]]+)\](?::(\d+))?$/); + if (match) { + host = match[1]; + if (!REDIS_PORT && match[2]) { + port = parseInt(match[2], 10); + } + } + } else { + const match = host.match(/^([^:]+):(\d+)$/); + if (match) { + host = match[1]; + if (!REDIS_PORT && match[2]) { + port = parseInt(match[2], 10); + } + } + } + } + + const client = createClient({ password: REDIS_PASSWORD, socket: { - host: REDIS_HOST, - port: 14714 - } + host, + port, + }, }); await client.connect();