|
1 | | -export type CacheMode = "disk" | "memory" | "none"; |
| 1 | +import { type Static, Type } from "@sinclair/typebox"; |
| 2 | +import { Value } from "@sinclair/typebox/value"; |
2 | 3 |
|
3 | | -function parseCacheMode(value: string | undefined): CacheMode { |
4 | | - const mode = (value || "disk").toLowerCase(); |
5 | | - if (mode === "memory" || mode === "none" || mode === "disk") { |
6 | | - return mode; |
7 | | - } |
8 | | - return "disk"; |
9 | | -} |
| 4 | +// Schema definitions |
| 5 | +const CacheModeSchema = Type.Union([ |
| 6 | + Type.Literal("disk"), |
| 7 | + Type.Literal("memory"), |
| 8 | + Type.Literal("hybrid"), |
| 9 | + Type.Literal("none"), |
| 10 | +]); |
10 | 11 |
|
11 | | -export const config = { |
12 | | - port: parseInt(process.env.PORT || "3000", 10), |
| 12 | +const ImageFormatSchema = Type.Union([ |
| 13 | + Type.Literal("webp"), |
| 14 | + Type.Literal("avif"), |
| 15 | + Type.Literal("png"), |
| 16 | + Type.Literal("jpg"), |
| 17 | + Type.Literal("jpeg"), |
| 18 | + Type.Literal("gif"), |
| 19 | +]); |
| 20 | + |
| 21 | +const ConfigSchema = Type.Object({ |
| 22 | + // Server |
| 23 | + port: Type.Number({ default: 3000, minimum: 1, maximum: 65535 }), |
13 | 24 |
|
14 | 25 | // Cache settings |
15 | | - cacheMode: parseCacheMode(process.env.CACHE_MODE), |
16 | | - cacheDir: process.env.CACHE_DIR || "./cache", |
17 | | - cacheTTL: parseInt(process.env.CACHE_TTL || "86400", 10), // 24 hours in seconds |
18 | | - maxCacheSize: parseInt(process.env.MAX_CACHE_SIZE || "1073741824", 10), // 1GB |
19 | | - maxMemoryCacheItems: parseInt( |
20 | | - process.env.MAX_MEMORY_CACHE_ITEMS || "1000", |
21 | | - 10, |
22 | | - ), // Max items in memory cache |
| 26 | + cacheMode: Type.Optional(CacheModeSchema), |
| 27 | + cacheDir: Type.String({ default: "./cache" }), |
| 28 | + cacheTTL: Type.Number({ default: 86400, minimum: 0 }), // 24 hours in seconds |
| 29 | + maxCacheSize: Type.Number({ default: 1073741824, minimum: 0 }), // 1GB |
| 30 | + maxMemoryCacheItems: Type.Number({ default: 1000, minimum: 1 }), |
23 | 31 |
|
24 | 32 | // Security |
25 | | - allowedDomains: (process.env.ALLOWED_DOMAINS || "") |
26 | | - .split(",") |
27 | | - .map((d) => d.trim().toLowerCase()) |
28 | | - .filter(Boolean), |
29 | | - blockedDomains: ["localhost", "127.0.0.1", "0.0.0.0", "::1"], |
30 | | - maxImageSize: parseInt(process.env.MAX_IMAGE_SIZE || "10485760", 10), // 10MB |
31 | | - requestTimeout: parseInt(process.env.REQUEST_TIMEOUT || "30000", 10), // 30s |
| 33 | + allowedDomains: Type.Array(Type.String(), { default: [] }), |
| 34 | + allowedOrigins: Type.Array(Type.String(), { default: [] }), |
| 35 | + blockedDomains: Type.Array(Type.String(), { |
| 36 | + default: ["localhost", "127.0.0.1", "0.0.0.0", "::1"], |
| 37 | + }), |
| 38 | + maxImageSize: Type.Number({ default: 10485760, minimum: 0 }), // 10MB |
| 39 | + requestTimeout: Type.Number({ default: 30000, minimum: 0 }), // 30s |
32 | 40 |
|
33 | 41 | // Image defaults |
34 | | - defaultQuality: 80, |
35 | | - defaultFormat: "webp" as const, |
36 | | - maxWidth: 4096, |
37 | | - maxHeight: 4096, |
| 42 | + defaultQuality: Type.Number({ default: 80, minimum: 1, maximum: 100 }), |
| 43 | + defaultFormat: Type.Optional(ImageFormatSchema), |
| 44 | + maxWidth: Type.Number({ default: 4096, minimum: 1 }), |
| 45 | + maxHeight: Type.Number({ default: 4096, minimum: 1 }), |
38 | 46 |
|
39 | 47 | // Cache headers |
40 | | - browserCacheTTL: parseInt(process.env.BROWSER_CACHE_TTL || "31536000", 10), // 1 year |
| 48 | + browserCacheTTL: Type.Number({ default: 31536000, minimum: 0 }), // 1 year |
41 | 49 |
|
42 | 50 | // OG image defaults |
| 51 | + ogDefaultWidth: Type.Number({ default: 1200, minimum: 1 }), |
| 52 | + ogDefaultHeight: Type.Number({ default: 630, minimum: 1 }), |
| 53 | + ogDefaultBg: Type.String({ default: "1a1a2e" }), |
| 54 | + ogDefaultFg: Type.String({ default: "ffffff" }), |
| 55 | + |
| 56 | + // Custom templates directory |
| 57 | + templatesDir: Type.String({ default: "./templates" }), |
| 58 | +}); |
| 59 | + |
| 60 | +// Helper to parse comma-separated list |
| 61 | +function parseList(value: string | undefined): string[] { |
| 62 | + return (value || "") |
| 63 | + .split(",") |
| 64 | + .map((s) => s.trim()) |
| 65 | + .filter(Boolean); |
| 66 | +} |
| 67 | + |
| 68 | +// Helper to parse comma-separated list with lowercase |
| 69 | +function parseListLower(value: string | undefined): string[] { |
| 70 | + return (value || "") |
| 71 | + .split(",") |
| 72 | + .map((s) => s.trim().toLowerCase()) |
| 73 | + .filter(Boolean); |
| 74 | +} |
| 75 | + |
| 76 | +// Parse environment variables into raw config object |
| 77 | +const rawConfig = { |
| 78 | + port: parseInt(process.env.PORT || "3000", 10), |
| 79 | + cacheMode: (process.env.CACHE_MODE || "disk").toLowerCase(), |
| 80 | + cacheDir: process.env.CACHE_DIR || "./cache", |
| 81 | + cacheTTL: parseInt(process.env.CACHE_TTL || "86400", 10), |
| 82 | + maxCacheSize: parseInt(process.env.MAX_CACHE_SIZE || "1073741824", 10), |
| 83 | + maxMemoryCacheItems: parseInt( |
| 84 | + process.env.MAX_MEMORY_CACHE_ITEMS || "1000", |
| 85 | + 10, |
| 86 | + ), |
| 87 | + allowedDomains: parseListLower(process.env.ALLOWED_DOMAINS), |
| 88 | + allowedOrigins: parseList(process.env.ALLOWED_ORIGINS), |
| 89 | + blockedDomains: ["localhost", "127.0.0.1", "0.0.0.0", "::1"], |
| 90 | + maxImageSize: parseInt(process.env.MAX_IMAGE_SIZE || "10485760", 10), |
| 91 | + requestTimeout: parseInt(process.env.REQUEST_TIMEOUT || "30000", 10), |
| 92 | + defaultQuality: 80, |
| 93 | + defaultFormat: "webp", |
| 94 | + maxWidth: 4096, |
| 95 | + maxHeight: 4096, |
| 96 | + browserCacheTTL: parseInt(process.env.BROWSER_CACHE_TTL || "31536000", 10), |
43 | 97 | ogDefaultWidth: 1200, |
44 | 98 | ogDefaultHeight: 630, |
45 | 99 | ogDefaultBg: "1a1a2e", |
46 | 100 | ogDefaultFg: "ffffff", |
47 | | - |
48 | | - // Custom templates directory |
49 | 101 | templatesDir: process.env.TEMPLATES_DIR || "./templates", |
50 | | -} as const; |
| 102 | +}; |
| 103 | + |
| 104 | +// Validate and apply defaults |
| 105 | +if (!Value.Check(ConfigSchema, rawConfig)) { |
| 106 | + const errors = [...Value.Errors(ConfigSchema, rawConfig)]; |
| 107 | + console.error("Invalid configuration:"); |
| 108 | + for (const error of errors) { |
| 109 | + console.error(` ${error.path}: ${error.message}`); |
| 110 | + } |
| 111 | + process.exit(1); |
| 112 | +} |
| 113 | + |
| 114 | +// Export validated config |
| 115 | +export const config = rawConfig as Static<typeof ConfigSchema> & { |
| 116 | + cacheMode: CacheMode; |
| 117 | + defaultFormat: "webp" | "avif" | "png" | "jpg" | "jpeg" | "gif"; |
| 118 | +}; |
51 | 119 |
|
| 120 | +// Export types |
| 121 | +export type CacheMode = Static<typeof CacheModeSchema>; |
52 | 122 | export type Config = typeof config; |
0 commit comments