chore: Centralize env parsing, validation, defaults, and deprecation notices (#3487)

* chore: Centralize env parsing, defaults, deprecation

* wip

* test

* test

* tsc

* docs, more validation

* fix: Allow empty REDIS_URL (defaults to localhost)

* test

* fix: SLACK_MESSAGE_ACTIONS not bool

* fix: Add SMTP port validation
This commit is contained in:
Tom Moor
2022-05-19 08:05:11 -07:00
committed by GitHub
parent 51001cfac1
commit 3c002f82cc
66 changed files with 783 additions and 341 deletions

View File

@@ -1,5 +1,6 @@
import Redis from "ioredis";
import { defaults } from "lodash";
import env from "@server/env";
import Logger from "./logging/logger";
const defaultOptions = {
@@ -12,23 +13,21 @@ const defaultOptions = {
// support Heroku Redis, see:
// https://devcenter.heroku.com/articles/heroku-redis#ioredis-module
tls:
process.env.REDIS_URL && process.env.REDIS_URL.startsWith("rediss://")
? {
rejectUnauthorized: false,
}
: undefined,
tls: (env.REDIS_URL || "").startsWith("rediss://")
? {
rejectUnauthorized: false,
}
: undefined,
};
export default class RedisAdapter extends Redis {
constructor(url: string | undefined) {
if (!(url || "").startsWith("ioredis://")) {
super(process.env.REDIS_URL, defaultOptions);
if (!url || !url.startsWith("ioredis://")) {
super(env.REDIS_URL, defaultOptions);
} else {
let customOptions = {};
try {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const decodedString = Buffer.from(url!.slice(10), "base64").toString();
const decodedString = Buffer.from(url.slice(10), "base64").toString();
customOptions = JSON.parse(decodedString);
} catch (error) {
throw new Error(`Failed to decode redis adapter options: ${error}`);
@@ -52,12 +51,10 @@ export default class RedisAdapter extends Redis {
private static _subscriber: RedisAdapter;
public static get defaultClient(): RedisAdapter {
return this._client || (this._client = new this(process.env.REDIS_URL));
return this._client || (this._client = new this(env.REDIS_URL));
}
public static get defaultSubscriber(): RedisAdapter {
return (
this._subscriber || (this._subscriber = new this(process.env.REDIS_URL))
);
return this._subscriber || (this._subscriber = new this(env.REDIS_URL));
}
}