check number 5000

This commit is contained in:
Michael Freno
2025-12-19 16:20:44 -05:00
parent 7a3dc3fc17
commit e64fd84cba
2 changed files with 44 additions and 200 deletions

View File

@@ -159,8 +159,32 @@ function AppLayout(props: { children: any }) {
return ( return (
<> <>
<div class="flex max-w-screen flex-row"> <Show when={!barsInitialized()}>
<Suspense fallback={<TerminalSplash />}>{props.children}</Suspense> <div class="bg-base fixed inset-0 z-100">
<TerminalSplash />
</div>
</Show>
<div
class="flex max-w-screen flex-row"
style={{
opacity: barsInitialized() ? "1" : "0",
transition: "opacity 0.3s ease-in-out"
}}
>
<LeftBar />
<div
class="relative min-h-screen rounded-t-lg shadow-2xl"
style={{
width: `${centerWidth()}px`,
"margin-left": `${leftBarSize()}px`
}}
>
<Show when={barsInitialized()} fallback={<TerminalSplash />}>
<Suspense fallback={<TerminalSplash />}>{props.children}</Suspense>
</Show>
</div>
<RightBar />
</div> </div>
</> </>
); );

216
src/env/server.ts vendored
View File

@@ -1,8 +1,5 @@
import { z } from "zod"; import { z } from "zod";
// Check if we're running in a server context
const isServer = typeof process !== "undefined" && process.env !== undefined;
const serverEnvSchema = z.object({ const serverEnvSchema = z.object({
// Server-side environment variables // Server-side environment variables
NODE_ENV: z.enum(["development", "test", "production"]), NODE_ENV: z.enum(["development", "test", "production"]),
@@ -27,46 +24,11 @@ const serverEnvSchema = z.object({
LINEAGE_OFFLINE_SERIALIZATION_SECRET: z.string().min(1), LINEAGE_OFFLINE_SERIALIZATION_SECRET: z.string().min(1),
GITEA_URL: z.string().min(1), GITEA_URL: z.string().min(1),
GITEA_TOKEN: z.string().min(1), GITEA_TOKEN: z.string().min(1),
GITHUB_API_TOKEN: z.string().min(1), GITHUB_API_TOKEN: z.string().min(1)
// Client-side variables accessible on server
VITE_DOMAIN: z.string().min(1).optional(),
VITE_AWS_BUCKET_STRING: z.string().min(1).optional(),
VITE_GOOGLE_CLIENT_ID: z.string().min(1).optional(),
VITE_GOOGLE_CLIENT_ID_MAGIC_DELVE: z.string().min(1).optional(),
VITE_GITHUB_CLIENT_ID: z.string().min(1).optional(),
VITE_WEBSOCKET: z.string().min(1).optional()
});
const clientEnvSchema = z.object({
// Client-side environment variables (using VITE_ prefix for SolidStart)
VITE_DOMAIN: z.string().min(1),
VITE_AWS_BUCKET_STRING: z.string().min(1),
VITE_GOOGLE_CLIENT_ID: z.string().min(1),
VITE_GOOGLE_CLIENT_ID_MAGIC_DELVE: z.string().min(1),
VITE_GITHUB_CLIENT_ID: z.string().min(1),
VITE_WEBSOCKET: z.string().min(1)
});
// Combined environment schema
export const envSchema = z.object({
server: serverEnvSchema,
client: clientEnvSchema
}); });
// Type inference // Type inference
export type ServerEnv = z.infer<typeof serverEnvSchema>; export type ServerEnv = z.infer<typeof serverEnvSchema>;
export type ClientEnv = z.infer<typeof clientEnvSchema>;
// Custom error class for better error handling
class EnvironmentError extends Error {
constructor(
message: string,
public errors?: z.ZodFormattedError<any>
) {
super(message);
this.name = "EnvironmentError";
}
}
// Validation function for server-side with detailed error messages // Validation function for server-side with detailed error messages
export const validateServerEnv = ( export const validateServerEnv = (
@@ -103,9 +65,7 @@ export const validateServerEnv = (
let errorMessage = "Environment validation failed:\n"; let errorMessage = "Environment validation failed:\n";
if (missingVars.length > 0) { if (missingVars.length > 0) {
errorMessage += `Missing required variables: ${missingVars.join( errorMessage += `Missing required variables: ${missingVars.join(", ")}\n`;
", "
)}\n`;
} }
if (invalidVars.length > 0) { if (invalidVars.length > 0) {
@@ -115,162 +75,36 @@ export const validateServerEnv = (
}); });
} }
throw new EnvironmentError(errorMessage, formattedErrors); console.error(errorMessage);
throw new Error(errorMessage);
} }
throw new EnvironmentError( console.error("Environment validation failed with unknown error:", error);
"Environment validation failed with unknown error", throw new Error("Environment validation failed with unknown error");
undefined
);
} }
}; };
// Validation function for client-side (runtime) with detailed error messages // Validate and export environment variables directly
export const validateClientEnv = ( // This happens once at module load time on the server
envVars: Record<string, string | undefined> const validateAndExportEnv = (): ServerEnv => {
): ClientEnv => {
try { try {
return clientEnvSchema.parse(envVars); const validated = validateServerEnv(process.env);
console.log("✅ Environment validation successful");
return validated;
} catch (error) { } catch (error) {
if (error instanceof z.ZodError) { console.error("❌ Environment validation failed:", error);
const formattedErrors = error.format(); throw error;
const missingVars = Object.entries(formattedErrors)
.filter(
([key, value]) =>
key !== "_errors" &&
typeof value === "object" &&
value._errors?.length > 0 &&
value._errors[0] === "Required"
)
.map(([key, _]) => key);
const invalidVars = Object.entries(formattedErrors)
.filter(
([key, value]) =>
key !== "_errors" &&
typeof value === "object" &&
value._errors?.length > 0 &&
value._errors[0] !== "Required"
)
.map(([key, value]) => ({
key,
error: value._errors[0]
}));
let errorMessage = "Client environment validation failed:\n";
if (missingVars.length > 0) {
errorMessage += `Missing required variables: ${missingVars.join(
", "
)}\n`;
}
if (invalidVars.length > 0) {
errorMessage += "Invalid values:\n";
invalidVars.forEach(({ key, error }) => {
errorMessage += ` ${key}: ${error}\n`;
});
}
throw new EnvironmentError(errorMessage, formattedErrors);
}
throw new EnvironmentError(
"Client environment validation failed with unknown error",
undefined
);
} }
}; };
// Lazy environment validation - only validates when accessed export const env = validateAndExportEnv();
let _cachedEnv: ServerEnv | null = null;
let _validationAttempted = false;
const validateEnvLazy = (): ServerEnv => {
if (!_validationAttempted) {
_validationAttempted = true;
// If not on server, return empty object (will throw on access)
if (!isServer) {
console.warn("⚠️ Server environment accessed in browser context");
_cachedEnv = {} as ServerEnv;
return _cachedEnv;
}
try {
// Validate server environment variables using process.env
_cachedEnv = validateServerEnv(process.env);
console.log("✅ Environment validation successful");
} catch (error) {
if (error instanceof EnvironmentError) {
console.error("❌ Environment validation failed:", error.message);
if (error.errors) {
console.error(
"Detailed errors:",
JSON.stringify(error.errors, null, 2)
);
}
throw new Error(`Environment validation failed: ${error.message}`);
}
console.error("❌ Unexpected environment validation error:", error);
throw error;
}
}
if (!_cachedEnv) {
throw new Error("Environment validation failed");
}
return _cachedEnv;
};
export const env = new Proxy({} as ServerEnv, {
get(_target, prop: string) {
if (!isServer) {
throw new Error(
`Cannot access server environment variable "${prop}" in browser context. This is a development error - server-only code is being bundled into the client.`
);
}
const validatedEnv = validateEnvLazy();
return validatedEnv[prop as keyof ServerEnv];
},
has(_target, prop: string) {
if (!isServer) return false;
try {
const validatedEnv = validateEnvLazy();
return prop in validatedEnv;
} catch {
return false;
}
}
});
// For client-side validation (useful in components)
export const getClientEnvValidation = () => {
try {
return validateClientEnv(import.meta.env);
} catch (error) {
if (error instanceof EnvironmentError) {
console.error("❌ Client environment validation failed:", error.message);
throw new Error(`Client environment validation failed: ${error.message}`);
}
throw new Error("Client environment validation failed with unknown error");
}
};
// Helper function to check if a variable is missing // Helper function to check if a variable is missing
export const isMissingEnvVar = (varName: string): boolean => { export const isMissingEnvVar = (varName: string): boolean => {
return !process.env[varName] || process.env[varName]?.trim() === ""; return !process.env[varName] || process.env[varName]?.trim() === "";
}; };
// Helper function to check if a client variable is missing // Helper function to get all missing server environment variables
export const isMissingClientEnvVar = (varName: string): boolean => { export const getMissingEnvVars = (): string[] => {
return !import.meta.env[varName] || import.meta.env[varName]?.trim() === "";
};
// Helper function to get all missing environment variables
export const getMissingEnvVars = (): {
server: string[];
client: string[];
} => {
const requiredServerVars = [ const requiredServerVars = [
"NODE_ENV", "NODE_ENV",
"ADMIN_EMAIL", "ADMIN_EMAIL",
@@ -297,19 +131,5 @@ export const getMissingEnvVars = (): {
"GITHUB_API_TOKEN" "GITHUB_API_TOKEN"
]; ];
const requiredClientVars = [ return requiredServerVars.filter((varName) => isMissingEnvVar(varName));
"VITE_DOMAIN",
"VITE_AWS_BUCKET_STRING",
"VITE_GOOGLE_CLIENT_ID",
"VITE_GOOGLE_CLIENT_ID_MAGIC_DELVE",
"VITE_GITHUB_CLIENT_ID",
"VITE_WEBSOCKET"
];
return {
server: requiredServerVars.filter((varName) => isMissingEnvVar(varName)),
client: requiredClientVars.filter((varName) =>
isMissingClientEnvVar(varName)
)
};
}; };