Environment Variable Validation (#122)
* Update: env validation and discord token validation * Add: IPv4 Address Validation * Update: version increment
This commit is contained in:
@@ -7,7 +7,7 @@ services:
|
|||||||
build: ./ # find docker file in designated path
|
build: ./ # find docker file in designated path
|
||||||
container_name: discord
|
container_name: discord
|
||||||
restart: always # rebuild container always
|
restart: always # rebuild container always
|
||||||
image: kevinthedang/discord-ollama:0.6.0
|
image: kevinthedang/discord-ollama:0.6.1
|
||||||
environment:
|
environment:
|
||||||
CLIENT_TOKEN: ${CLIENT_TOKEN}
|
CLIENT_TOKEN: ${CLIENT_TOKEN}
|
||||||
MODEL: ${MODEL}
|
MODEL: ${MODEL}
|
||||||
|
|||||||
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "discord-ollama",
|
"name": "discord-ollama",
|
||||||
"version": "0.6.0",
|
"version": "0.6.1",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "discord-ollama",
|
"name": "discord-ollama",
|
||||||
"version": "0.6.0",
|
"version": "0.6.1",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"discord.js": "^14.15.3",
|
"discord.js": "^14.15.3",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "discord-ollama",
|
"name": "discord-ollama",
|
||||||
"version": "0.6.0",
|
"version": "0.6.1",
|
||||||
"description": "Ollama Integration into discord",
|
"description": "Ollama Integration into discord",
|
||||||
"main": "build/index.js",
|
"main": "build/index.js",
|
||||||
"exports": "./build/index.js",
|
"exports": "./build/index.js",
|
||||||
|
|||||||
@@ -3,16 +3,33 @@ import { config } from 'dotenv'
|
|||||||
|
|
||||||
// resolve config file
|
// resolve config file
|
||||||
const envFilePath = resolve(process.cwd(), '.env')
|
const envFilePath = resolve(process.cwd(), '.env')
|
||||||
|
const ipValidate: RegExp = /^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$/
|
||||||
|
|
||||||
// set current environment variable file
|
// set current environment variable file
|
||||||
config({ path: envFilePath })
|
config({ path: envFilePath })
|
||||||
|
|
||||||
// Getter for environment variables
|
/**
|
||||||
|
* Method to validate if environment variables found in file utils/env.ts
|
||||||
|
*
|
||||||
|
* @param name Name of the environment variable in .env
|
||||||
|
* @param fallback fallback value to set if environment variable is not set (used manually in src/keys.ts)
|
||||||
|
* @returns environment variable value
|
||||||
|
*/
|
||||||
export function getEnvVar(name: string, fallback?: string): string {
|
export function getEnvVar(name: string, fallback?: string): string {
|
||||||
const value = process.env[name] ?? fallback
|
const value = process.env[name] ?? fallback
|
||||||
if (value == undefined)
|
if (!value)
|
||||||
throw new Error(`Environment variable ${name} is not set.`)
|
throw new Error(`Environment variable ${name} is not set.`)
|
||||||
|
|
||||||
|
// validate User-Generated Discord Application Tokens
|
||||||
|
if (name === "CLIENT_TOKEN")
|
||||||
|
if (value.length < 72) throw new Error(`The "CLIENT_TOKEN" provided is not of at least length 72.
|
||||||
|
This is probably an invalid token unless Discord updated their token policy. Please provide a valid token.`)
|
||||||
|
|
||||||
|
// validate IPv4 address found in environment variables
|
||||||
|
if (name.endsWith("_IP") || name.endsWith("_ADDRESS"))
|
||||||
|
if (!ipValidate.test(value))
|
||||||
|
throw new Error(`Environment variable ${name} does not follow IPv4 formatting.`)
|
||||||
|
|
||||||
// return env variable
|
// return env variable
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user