Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions node/query-redis-labs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
4 changes: 3 additions & 1 deletion node/query-redis-labs/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
46 changes: 41 additions & 5 deletions node/query-redis-labs/src/redis.js
Original file line number Diff line number Diff line change
@@ -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();
Expand Down