fix(validator): corrections to no leak like crazy

This commit is contained in:
2025-09-02 17:56:00 -05:00
parent 8fe1bcaef5
commit 80c0e1ec30
10 changed files with 102 additions and 127 deletions

View File

@@ -1,7 +1,8 @@
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import { env } from "../utils/envValidator.js";
import { validateEnv } from "../utils/envValidator.js";
const env = validateEnv(process.env);
const dbURL = `postgres://${env.DATABASE_USER}:${env.DATABASE_PASSWORD}@${env.DATABASE_HOST}:${env.DATABASE_PORT}/${env.DATABASE_DB}`;
const queryClient = postgres(dbURL, {

View File

@@ -1,4 +1,11 @@
import { boolean, pgTable, text, timestamp, uuid } from "drizzle-orm/pg-core";
import {
boolean,
jsonb,
pgTable,
text,
timestamp,
uuid,
} from "drizzle-orm/pg-core";
import { createInsertSchema, createSelectSchema } from "drizzle-zod";
import { z } from "zod";
@@ -8,7 +15,7 @@ export const logs = pgTable("logs", {
module: text("module").notNull(),
subModule: text("subModule"),
message: text("message").notNull(),
stack: text("stack"),
stack: jsonb("stack").default([]),
checked: boolean("checked").default(false),
hostname: text("hostname"),
createdAt: timestamp("createdAt").defaultNow(),

View File

@@ -1,7 +1,7 @@
import build from "pino-abstract-transport";
import { db } from "../db/db.js";
import { logs, type Log } from "../db/schema/logs.js";
import { checkENV } from "../utils/envValidator.js";
import { tryCatch } from "../utils/tryCatch.js";
const pinoLogLevels: any = {
10: "trace",
@@ -19,14 +19,21 @@ export default async function (log: Log) {
for await (let obj of source) {
// convert to the name to make it more easy to find later :P
const levelName = pinoLogLevels[obj.level] || "unknown";
await db.insert(logs).values({
level: levelName,
module: obj?.module.toLowerCase(),
subModule: obj?.subModule.toLowerCase(),
hostname: obj?.hostname.toLowerCase(),
message: obj.msg,
stack: obj?.stack,
});
const res = await tryCatch(
db.insert(logs).values({
level: levelName,
module: obj?.module?.toLowerCase(),
subModule: obj?.subModule?.toLowerCase(),
hostname: obj?.hostname?.toLowerCase(),
message: obj.msg,
stack: obj?.stack,
})
);
if (res.error) {
console.log(res.error);
}
}
});
} catch (err) {

View File

@@ -1,5 +1,4 @@
import pino, { type Logger } from "pino";
import { env } from "../utils/envValidator.js";
export let logLevel = process.env.LOG_LEVEL || "info";

View File

@@ -1,7 +1,8 @@
import build from "pino-abstract-transport";
import { db } from "../db/db.js";
import { logs, type Log } from "../db/schema/logs.js";
import { env } from "../utils/envValidator.js";
import { type Log } from "../db/schema/logs.js";
import { validateEnv } from "../utils/envValidator.js";
import { sendNotify } from "../utils/notify.js";
const env = validateEnv(process.env);
const pinoLogLevels: any = {
10: "trace",
@@ -12,49 +13,6 @@ const pinoLogLevels: any = {
60: "fatal",
};
// discord function
async function sendFatal(log: Log) {
const webhookUrl = process.env.WEBHOOK_URL!;
let payload = {
embeds: [
{
title: `🚨 ${env.PROD_PLANT_TOKEN}: encounter a critical error `,
description: `Where was the error: ${log.module}${
log.subModule ? `-${log.subModule}` : ""
}`,
color: 0xff0000, // red
fields: [
{
name: "Message",
value: log.message,
inline: false,
},
{
name: "Hostname",
value: log.hostname,
inline: false,
},
{
name: "Stack",
value:
"```" +
(log.stack?.slice(0, 1000) ?? "no stack") +
"```",
},
],
footer: {
text: "LST Logger 💀",
},
timestamp: new Date().toISOString(),
},
],
};
await fetch(webhookUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload),
});
}
export default async function (log: Log) {
//const {username, service, level, msg, ...extra} = log;
@@ -76,14 +34,15 @@ export default async function (log: Log) {
? String(obj.hostname).toLowerCase()
: undefined,
message: obj.msg,
stack: obj.stack ? obj.stack : undefined,
};
if (!process.env.WEBHOOK_URL) {
console.log("webhook missing?");
console.log("WebHook is missing we wont move foward.");
return;
}
if (obj.level >= 60 && obj.notify) {
sendFatal(newlog as Log);
sendNotify(newlog as Log);
}
}
});

View File

@@ -1,7 +1,8 @@
import { env } from "../utils/envValidator.js";
import { returnFunc } from "../utils/return.js";
import { connected, pool } from "./prodSqlConnect.js";
import { validateEnv } from "../utils/envValidator.js";
const env = validateEnv(process.env);
/**
* Run a prod query
* just pass over the query as a string and the name of the query.

View File

@@ -1,5 +1,7 @@
import sql from "mssql";
import { env } from "../utils/envValidator.js";
import { validateEnv } from "../utils/envValidator.js";
const env = validateEnv(process.env);
export const sqlConfig: sql.config = {
server: env.PROD_SERVER,
database: `AlplaPROD_${env.PROD_PLANT_TOKEN}_cus`,

View File

@@ -1,9 +1,11 @@
import sql from "mssql";
import { checkHostnamePort } from "../utils/checkHostNamePort.js";
import { sqlConfig } from "./prodSqlConfig.js";
import { env } from "../utils/envValidator.js";
import { createLogger } from "../logger/logger.js";
import { returnFunc } from "../utils/return.js";
import { validateEnv } from "../utils/envValidator.js";
const env = validateEnv(process.env);
export let pool: any;
export let connected: boolean = false;

View File

@@ -1,55 +1,37 @@
import { z } from "zod";
import { createLogger } from "../logger/logger.js";
/**
* This is where we will validate the required ENV parapmeters.
*
*/
const envSchema = z.object({
//Server stuff
// server stuff
VITE_PORT: z.string().default("4200"),
LOG_LEVEL: z.string().default("info"),
// app db stuff
// db stuff
DATABASE_HOST: z.string(),
DATABASE_PORT: z.string(),
DATABASE_USER: z.string(),
DATABASE_PASSWORD: z.string(),
DATABASE_DB: z.string().default("lst"),
// prod server checks
// prod stuff
PROD_SERVER: z.string(),
PROD_PLANT_TOKEN: z.string(),
PROD_USER: z.string(),
PROD_PASSWORD: z.string(),
// docker specifc
RUNNING_IN_DOCKER: z.string().default("false"),
});
// use safeParse instead of parse
const parsed = envSchema.safeParse(process.env);
export type Env = z.infer<typeof envSchema>;
export const checkENV = () => {
return envSchema.safeParse(process.env);
};
const log = createLogger({ module: "envValidation" });
if (!parsed.success) {
log.fatal(
`Environment validation failed: Missing: ${parsed.error.issues
.map((e) => {
return e.path[0];
})
.join(", ")}`
);
// 🔔 Send a notification (e.g., email, webhook, Slack)
// sendNotification(parsed.error.format());
// gracefully exit if in production
//process.exit(1);
throw Error(
`Environment validation failed: Missing: ${parsed.error.issues
.map((e) => {
return e.path[0];
})
.join(", ")}`
);
export function validateEnv(raw: NodeJS.ProcessEnv): Env {
const parsed = envSchema.safeParse(raw);
if (!parsed.success) {
throw new Error(
`Environment validation failed. Missing: ${parsed.error.issues
.map((e) => e.path[0])
.join(", ")}`
);
}
return parsed.data;
}
export const env = parsed.data;