@minejs/server 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,511 @@
1
+ import { DB } from '@je-es/sdb';
2
+ export { ColumnDefinition, ColumnType, DB, QueryBuilder, SqlValue, TableSchema, WhereCondition, blob, column, defaultValue, index, integer, notNull, numeric, primaryKey, real, references, table, text, unique } from '@minejs/db';
3
+
4
+ declare class I18nManager {
5
+ private translations;
6
+ private currentLanguage;
7
+ private defaultLanguage;
8
+ private supportedLanguages;
9
+ private cachePath;
10
+ constructor(config?: I18nConfig);
11
+ /**
12
+ * Load translations for a specific language
13
+ * @param lang Language code (e.g., 'en', 'ar', 'fr')
14
+ * @param translations Translation object (can be nested)
15
+ */
16
+ loadLanguage(lang: string, translations: Record<string, any>): void;
17
+ /**
18
+ * Flatten nested object into dot notation
19
+ * @param obj Nested object
20
+ * @param prefix Current prefix
21
+ * @returns Flattened object with dot notation keys
22
+ */
23
+ private flattenObject;
24
+ /**
25
+ * Load all translations from static files
26
+ * @param translations Object with language codes as keys and translation objects as values
27
+ */
28
+ loadTranslations(translations: Record<string, Record<string, any>>): void;
29
+ /**
30
+ * Set the current language
31
+ * @param lang Language code
32
+ */
33
+ setLanguage(lang: string): void;
34
+ /**
35
+ * Get the current language
36
+ */
37
+ getLanguage(): string;
38
+ /**
39
+ * Get all supported languages
40
+ */
41
+ getSupportedLanguages(): string[];
42
+ /**
43
+ * Translate a key with smart parameter replacement
44
+ * Supports nested translation keys as parameter values
45
+ *
46
+ * @example
47
+ * // Simple translation
48
+ * t('button.login') // => "Login" or "دخـول"
49
+ *
50
+ * @example
51
+ * // With parameters
52
+ * t('nav.credits', { count: '100' })
53
+ * // => "Available Credits: 100"
54
+ *
55
+ * @example
56
+ * // With nested translation keys as parameters
57
+ * t('language.switching_to', { language: 'button.login' })
58
+ * // => "Switching to Login..."
59
+ *
60
+ * @param key Translation key (dot-notation for nested keys)
61
+ * @param params Optional parameters for replacement
62
+ * @param defaultValue Optional default value
63
+ * @returns Translated string with replaced parameters
64
+ */
65
+ t(key: string, params?: Record<string, string>, defaultValue?: string): string;
66
+ private getTranslation;
67
+ /**
68
+ * Translate with a specific language (overrides current language temporarily)
69
+ *
70
+ * @param key Translation key
71
+ * @param lang Language code
72
+ * @param params Optional parameters
73
+ * @returns Translated string
74
+ */
75
+ tLang(key: string, lang: string, params?: Record<string, string>): string;
76
+ /**
77
+ * Get all translations for current language
78
+ */
79
+ getTranslations(): Record<string, string>;
80
+ /**
81
+ * Check if a translation key exists
82
+ * @param key Translation key
83
+ * @returns true if key exists in current or default language
84
+ */
85
+ hasKey(key: string): boolean;
86
+ }
87
+ /**
88
+ * Initialize the i18n manager
89
+ * @param config I18n configuration
90
+ * @returns I18nManager instance
91
+ */
92
+ declare function initI18n(config?: I18nConfig): I18nManager;
93
+ /**
94
+ * Get the global i18n instance
95
+ */
96
+ declare function getI18n(): I18nManager;
97
+ /**
98
+ * Global translation function
99
+ * @param key Translation key (supports dot notation for nested keys)
100
+ * @param params Optional parameters
101
+ * @param defaultValue Optional default value
102
+ * @returns Translated string
103
+ */
104
+ declare function t(key: string, params?: Record<string, string>, defaultValue?: string): string;
105
+ /**
106
+ * Set the current language globally
107
+ * @param lang Language code
108
+ */
109
+ declare function setLanguage(lang: string): void;
110
+ /**
111
+ * Get the current language
112
+ */
113
+ declare function getCurrentLanguage(): string;
114
+ /**
115
+ * Get all supported languages
116
+ */
117
+ declare function getSupportedLanguages(): string[];
118
+
119
+ // src/types.d.ts
120
+ //
121
+ // Developed with ❤️ by Maysara.
122
+
123
+
124
+
125
+ // ╚══════════════════════════════════════════════════════════════════════════════════════╝
126
+
127
+
128
+
129
+ // ╔════════════════════════════════════════ TYPE ════════════════════════════════════════╗
130
+
131
+
132
+ type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS' | 'HEAD';
133
+ type RouteHandler$1 = (c: AppContext) => Response | Promise<Response>;
134
+ type AppMiddleware = (c: AppContext, next: () => Promise<void>) => void | Promise<void>;
135
+
136
+ interface AppContext {
137
+ ip : string;
138
+ request : Request;
139
+ params : Record<string, string>;
140
+ query : Record<string, string>;
141
+
142
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
143
+ body : any;
144
+ headers : Headers;
145
+ db : DB | undefined;
146
+ logger : Logger$1 | null;
147
+ i18n : I18nManager | null;
148
+ lang? : string;
149
+ user? : unknown;
150
+ requestId : string;
151
+
152
+ state : Record<string, unknown>;
153
+
154
+ // Response methods
155
+ json (data: unknown, status?: number): Response;
156
+ text (data: string, status?: number): Response;
157
+ html (data: string, status?: number): Response;
158
+ redirect (url: string, status?: number): Response;
159
+ file (path: string, contentType?: string): Response;
160
+
161
+ // Cookie methods
162
+ setCookie (name: string, value: string, options?: CookieOptions): AppContext;
163
+ getCookie (name: string): string | undefined;
164
+ deleteCookie (name: string, options?: Partial<CookieOptions>): AppContext;
165
+
166
+ // Header methods
167
+ setHeader(key: string, value: string): AppContext;
168
+ getHeader(key: string): string | undefined;
169
+
170
+ // Status code
171
+ status(code: number): AppContext;
172
+ statusCode: number;
173
+
174
+ // Internal helper
175
+ _setCookieHeaders(): Record<string, string | string[]>;
176
+ }
177
+
178
+ interface StaticConfig$1 {
179
+ path : string; // URL path prefix (e.g., '/public' or '/static')
180
+ directory : string; // Local directory to serve from
181
+ maxAge? : number; // Cache control in seconds (default: 3600)
182
+ index? : string[]; // Index files (default: ['index.html'])
183
+ dotfiles? : 'allow' | 'deny' | 'ignore'; // How to handle dotfiles (default: 'deny')
184
+ etag? : boolean; // Enable ETag headers (default: true)
185
+ lastModified? : boolean; // Enable Last-Modified headers (default: true)
186
+ immutable? : boolean; // Add immutable to cache-control (default: false)
187
+ extensions? : string[]; // Try these extensions if file not found (e.g., ['html', 'htm'])
188
+ fallthrough? : boolean; // Continue to next handler if file not found (default: false)
189
+ setHeaders? : (ctx: AppContext, path: string) => void; // Custom header setter
190
+ }
191
+
192
+ interface CookieOptions {
193
+ maxAge? : number;
194
+ expires? : Date;
195
+ path? : string;
196
+ domain? : string;
197
+ secure? : boolean;
198
+ httpOnly? : boolean;
199
+ sameSite? : 'Strict' | 'Lax' | 'None';
200
+ }
201
+
202
+ interface ValidationSchema {
203
+ body?: unknown;
204
+ query?: unknown;
205
+ params?: unknown;
206
+ }
207
+
208
+ interface RouteDefinition {
209
+ method : HttpMethod | HttpMethod[];
210
+ path : string;
211
+ handler : RouteHandler$1;
212
+ validate? : ValidationSchema;
213
+ middlewares? : AppMiddleware[];
214
+ timeout? : number;
215
+ rateLimit? : { max: number; windowMs: number };
216
+ cache? : number;
217
+ tags? : string[];
218
+ }
219
+
220
+ // Database types
221
+ interface DatabaseConfig {
222
+ name? : string;
223
+ connection : string; // File path or ':memory:'
224
+ schema? : Record<string, unknown>;
225
+ timeout? : number;
226
+ }
227
+
228
+ interface SecurityConfig {
229
+ cors? : boolean | CorsConfig;
230
+ rateLimit? : boolean | RateLimitConfig;
231
+ csrf? : boolean | CsrfConfig;
232
+ helmet? : boolean | HelmetConfig;
233
+ auth? : boolean | AuthConfig;
234
+ validation? : boolean;
235
+ sanitize? : boolean;
236
+ }
237
+
238
+ interface CorsConfig {
239
+ origin? : string | string[] | ((origin: string) => boolean);
240
+ methods? : HttpMethod[];
241
+ allowedHeaders? : string[];
242
+ credentials? : boolean;
243
+ maxAge? : number;
244
+ }
245
+
246
+ interface RateLimitConfig {
247
+ windowMs? : number;
248
+ max? : number;
249
+ keyGenerator? : (c: AppContext) => string;
250
+ message? : string;
251
+ }
252
+
253
+ interface CsrfConfig {
254
+ secret? : string;
255
+ headerName? : string;
256
+ tokenTTL? : number;
257
+ }
258
+
259
+ interface HelmetConfig {
260
+ contentSecurityPolicy? : Record<string, string[]> | boolean;
261
+ hsts? : boolean | { maxAge?: number; includeSubDomains?: boolean; preload?: boolean };
262
+ frameguard? : boolean | { action: 'deny' | 'sameorigin' };
263
+ noSniff? : boolean;
264
+ xssFilter? : boolean;
265
+ referrerPolicy? : string | boolean;
266
+ }
267
+
268
+ interface AuthConfig {
269
+ jwt? : boolean | { secret: string; expiresIn?: string };
270
+ apiKey? : boolean | { header?: string };
271
+ bearer? : boolean;
272
+ }
273
+
274
+ type LogLevel = 'debug' | 'info' | 'warn' | 'error';
275
+
276
+ interface LoggingConfig {
277
+ level?: LogLevel;
278
+ pretty?: boolean;
279
+ }
280
+
281
+ interface I18nConfig {
282
+ defaultLanguage? : string;
283
+ supportedLanguages? : string[];
284
+ staticPath? : string; // Path to static i18n files
285
+ }
286
+
287
+ type TranslationSet = Record<string, Record<string, string>>;
288
+
289
+ interface ServerConfig {
290
+ port? : number | string;
291
+ hostname? : string;
292
+ requestTimeout? : number;
293
+ maxRequestSize? : number;
294
+ maxJsonSize? : number;
295
+
296
+ database? : DatabaseConfig | DatabaseConfig[];
297
+
298
+ security? : boolean | SecurityConfig;
299
+
300
+ compression? : boolean | { threshold?: number };
301
+
302
+ logging? : boolean | LoggingConfig;
303
+
304
+ // Internationalization (i18n)
305
+ i18n? : boolean | I18nConfig;
306
+
307
+ // Static file serving
308
+ static? : StaticConfig$1 | StaticConfig$1[];
309
+
310
+ routes? : RouteDefinition[];
311
+ middlewares? : AppMiddleware[];
312
+
313
+ errorHandler? : (error: Error, context: AppContext) => void | Promise<void>;
314
+ // Error page handler - returns Response for custom error pages
315
+ onError? : (statusCode: number, path: string, method: string) => Response | Promise<Response>;
316
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
317
+ onStartup? : (app: any) => void | Promise<void>;
318
+ onReady? : (app: ServerInstance, db: Map<string, DB>) => void | Promise<void>;
319
+ onShutdown? : () => void | Promise<void>;
320
+
321
+ apiPrefix? : string;
322
+ apiVersion? : string;
323
+
324
+ gracefulShutdownTimeout?: number;
325
+ }
326
+
327
+ interface ServerInstance {
328
+ app : unknown;
329
+ logger : Logger$1 | null;
330
+ db : Map<string, unknown>;
331
+ bunServer : unknown;
332
+ start : () => Promise<void>;
333
+ stop : () => Promise<void>;
334
+ addRoute : (route: RouteDefinition) => void;
335
+ addRoutes : (routes: RouteDefinition[]) => void;
336
+ getRoutes : () => RouteDefinition[];
337
+ }
338
+
339
+ interface Logger$1 {
340
+ debug (data: unknown, msg?: string): void;
341
+ info (data: unknown, msg?: string): void;
342
+ warn (data: unknown, msg?: string): void;
343
+ error (data: unknown, msg?: string): void;
344
+ fatal (data: unknown, msg?: string): void;
345
+ }
346
+
347
+ declare class AppError extends Error {
348
+ constructor(public message: string, public statusCode: number = 500, public code?: string) {
349
+ super(message);
350
+ this.name = 'AppError';
351
+ }
352
+ }
353
+
354
+ declare class ValidationError extends AppError {
355
+ constructor(message: string, public issues?: unknown) {
356
+ super(message, 400, 'VALIDATION_ERROR');
357
+ this.name = 'ValidationError';
358
+ }
359
+ }
360
+
361
+ declare class DatabaseError extends AppError {
362
+ constructor(message: string) {
363
+ super(message, 500, 'DATABASE_ERROR');
364
+ this.name = 'DatabaseError';
365
+ }
366
+ }
367
+
368
+ declare class TimeoutError extends AppError {
369
+ constructor(message = 'Request timeout') {
370
+ super(message, 408, 'TIMEOUT_ERROR');
371
+ this.name = 'TimeoutError';
372
+ }
373
+ }
374
+
375
+ declare class RateLimitError extends AppError {
376
+ constructor(message = 'Too many requests') {
377
+ super(message, 429, 'RATE_LIMIT_ERROR');
378
+ this.name = 'RateLimitError';
379
+ }
380
+ }
381
+
382
+ type RouteHandler = (ctx: AppContext) => Response | Promise<Response>;
383
+ interface RouteMatch {
384
+ handler: RouteHandler;
385
+ params: Record<string, string>;
386
+ metadata?: unknown;
387
+ }
388
+ interface RouteInfo {
389
+ method: string;
390
+ path: string;
391
+ handler: RouteHandler;
392
+ }
393
+ declare class Router {
394
+ private routes;
395
+ private regexRoutes;
396
+ match(method: string, path: string): RouteMatch | null;
397
+ getAll(): RouteInfo[];
398
+ clear(): void;
399
+ remove(method: string, path: string): boolean;
400
+ register(method: string, path: string, handler: RouteHandler, metadata?: unknown): void;
401
+ private pathToRegex;
402
+ }
403
+
404
+ interface RequestLogEntry {
405
+ timestamp: string;
406
+ method: string;
407
+ path: string;
408
+ ip: string;
409
+ status: number;
410
+ duration: number;
411
+ }
412
+ interface SecurityStats {
413
+ rateLimitEntries: number;
414
+ csrfTokens: number;
415
+ requestLogs: number;
416
+ }
417
+ declare class SecurityManager {
418
+ private rateLimitStore;
419
+ private csrfTokens;
420
+ private requestLog;
421
+ private readonly MAX_REQUEST_LOG_SIZE;
422
+ checkRateLimit(key: string, max: number, windowMs: number): boolean;
423
+ cleanupRateLimit(): void;
424
+ generateCsrfToken(sessionId: string, ttl?: number): string;
425
+ validateCsrfToken(token: string, sessionId: string): boolean;
426
+ cleanupCsrfTokens(): void;
427
+ sanitizeHtml(html: string): string;
428
+ sanitizeSql(input: string): string;
429
+ logRequest(id: string, method: string, path: string, ip: string, status: number, duration: number): void;
430
+ getRequestLog(id: string): RequestLogEntry | undefined;
431
+ getAllRequestLogs(): RequestLogEntry[];
432
+ clearAll(): void;
433
+ getStats(): SecurityStats;
434
+ }
435
+
436
+ declare class Logger {
437
+ private level;
438
+ private pretty;
439
+ private prefix;
440
+ private levels;
441
+ private colors;
442
+ constructor(level?: 'debug' | 'info' | 'warn' | 'error', pretty?: boolean, prefix?: string);
443
+ debug(data: unknown, msg?: string): void;
444
+ info(data: unknown, msg?: string): void;
445
+ warn(data: unknown, msg?: string): void;
446
+ error(data: unknown, msg?: string): void;
447
+ fatal(data: unknown, msg?: string): void;
448
+ child(prefix: string): Logger;
449
+ private log;
450
+ private prettyLog;
451
+ private colorizeMethod;
452
+ private colorizeStatus;
453
+ private getLevelIcon;
454
+ private getLevelColor;
455
+ }
456
+
457
+ interface StaticConfig {
458
+ path: string;
459
+ directory: string;
460
+ maxAge?: number;
461
+ index?: string[];
462
+ dotfiles?: 'allow' | 'deny' | 'ignore';
463
+ etag?: boolean;
464
+ lastModified?: boolean;
465
+ immutable?: boolean;
466
+ extensions?: string[];
467
+ fallthrough?: boolean;
468
+ setHeaders?: (ctx: AppContext, path: string) => void;
469
+ }
470
+ declare class StaticFileServer {
471
+ private config;
472
+ private resolvedDir;
473
+ private fileCache;
474
+ private readonly CACHE_MAX_SIZE;
475
+ constructor(config: StaticConfig);
476
+ /**
477
+ * Create request handler for static files
478
+ */
479
+ handler(): (ctx: AppContext) => Promise<Response>;
480
+ /**
481
+ * Get URL path pattern for router
482
+ */
483
+ getPathPattern(): string;
484
+ private resolveFilePath;
485
+ private isPathSafe;
486
+ private serveDirectory;
487
+ private serveFile;
488
+ private buildHeaders;
489
+ private generateEtag;
490
+ private getMimeType;
491
+ private handleNotFound;
492
+ /**
493
+ * Clear file cache
494
+ */
495
+ clearCache(): void;
496
+ /**
497
+ * Get cache statistics
498
+ */
499
+ getCacheStats(): {
500
+ entries: number;
501
+ maxSize: number;
502
+ };
503
+ }
504
+ /**
505
+ * Helper function to create static file server
506
+ */
507
+ declare function createStatic(config: StaticConfig): StaticFileServer;
508
+
509
+ declare function server(config?: ServerConfig): ServerInstance;
510
+
511
+ export { type AppContext, AppError, type AppMiddleware, type AuthConfig, type CookieOptions, type CorsConfig, type CsrfConfig, type DatabaseConfig, DatabaseError, type HelmetConfig, type HttpMethod, type I18nConfig, I18nManager, type LogLevel, Logger, type LoggingConfig, type RateLimitConfig, RateLimitError, type RouteDefinition, type RouteHandler$1 as RouteHandler, Router, type SecurityConfig, SecurityManager, type ServerConfig, type ServerInstance, type StaticConfig, StaticFileServer, TimeoutError, type TranslationSet, ValidationError, type ValidationSchema, createStatic, server as default, getCurrentLanguage, getI18n, getSupportedLanguages, initI18n, server, setLanguage, t };