baraqex 1.0.67 → 1.0.69

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.
@@ -1,8 +1,3 @@
1
- import { createServer, AuthService, Database } from 'baraqex/server';
2
- import helmet from 'helmet';
3
- import compression from 'compression';
4
- import rateLimit from 'express-rate-limit';
5
- import morgan from 'morgan';
6
1
  import dotenv from 'dotenv';
7
2
 
8
3
  // Load environment variables
@@ -31,70 +26,170 @@ const config = {
31
26
  }
32
27
  };
33
28
 
34
- // Create server instance
35
- const server = createServer(config);
29
+ // Wait for baraqex to be ready
30
+ async function waitForBaraqex() {
31
+ const maxWait = 10000;
32
+ const interval = 100;
33
+ let waited = 0;
34
+
35
+ while (waited < maxWait) {
36
+ try {
37
+ const baraqex = await import('baraqex');
38
+ if (baraqex.createServer && typeof baraqex.createServer === 'function') {
39
+ return baraqex;
40
+ }
41
+ } catch (error) {
42
+ // Continue waiting
43
+ }
44
+
45
+ await new Promise(resolve => setTimeout(resolve, interval));
46
+ waited += interval;
47
+ }
48
+
49
+ throw new Error('Baraqex failed to initialize within timeout period');
50
+ }
36
51
 
37
- // Get Express app for additional middleware
38
- const app = server.getExpressApp();
52
+ // Async function to start server
53
+ async function startServer() {
54
+ try {
55
+ console.log('šŸ”„ Waiting for Baraqex to initialize...');
56
+
57
+ // Wait for baraqex to be ready
58
+ const baraqex = await waitForBaraqex();
59
+ const { createServer } = baraqex;
60
+
61
+ console.log('āœ… Baraqex initialized successfully');
62
+
63
+ if (!createServer || typeof createServer !== 'function') {
64
+ throw new Error('createServer function not available from baraqex package');
65
+ }
39
66
 
40
- // Security middleware
41
- app.use(helmet({
42
- contentSecurityPolicy: {
43
- directives: {
44
- defaultSrc: ["'self'"],
45
- styleSrc: ["'self'", "'unsafe-inline'"],
46
- scriptSrc: ["'self'"],
47
- imgSrc: ["'self'", "data:", "https:"],
48
- },
49
- },
50
- }));
67
+ // Create server instance
68
+ const server = createServer(config);
69
+
70
+ // Wait for server initialization
71
+ await new Promise(resolve => setTimeout(resolve, 1000));
51
72
 
52
- // Compression middleware
53
- app.use(compression());
73
+ // Start the server first
74
+ console.log('šŸš€ Starting server...');
75
+ await server.start();
76
+
77
+ // Then try to set up additional endpoints after server is running
78
+ try {
79
+ if (typeof server.getExpressApp === 'function') {
80
+ const app = server.getExpressApp();
81
+
82
+ if (app) {
83
+ // Health check endpoint
84
+ app.get('/health', (req, res) => {
85
+ res.json({
86
+ status: 'ok',
87
+ timestamp: new Date().toISOString(),
88
+ version: process.env.npm_package_version || '1.0.0',
89
+ environment: process.env.NODE_ENV || 'development'
90
+ });
91
+ });
92
+
93
+ // API status endpoint
94
+ app.get('/api/status', (req, res) => {
95
+ let db = null;
96
+ let auth = null;
97
+
98
+ try {
99
+ if (typeof server.getDatabase === 'function') {
100
+ db = server.getDatabase();
101
+ }
102
+ } catch (e) {
103
+ // Database method not available
104
+ }
105
+
106
+ try {
107
+ if (typeof server.getAuth === 'function') {
108
+ auth = server.getAuth();
109
+ }
110
+ } catch (e) {
111
+ // Auth method not available
112
+ }
113
+
114
+ res.json({
115
+ api: 'online',
116
+ database: db ? 'connected' : 'not configured',
117
+ authentication: auth ? 'enabled' : 'disabled',
118
+ timestamp: new Date().toISOString()
119
+ });
120
+ });
121
+ }
122
+ }
123
+ } catch (setupError) {
124
+ console.warn('āš ļø Could not set up additional endpoints:', setupError.message);
125
+ }
54
126
 
55
- // Request logging
56
- if (process.env.NODE_ENV !== 'test') {
57
- app.use(morgan(process.env.NODE_ENV === 'production' ? 'combined' : 'dev'));
127
+ // Database connection (if configured)
128
+ await connectDatabase(server);
129
+
130
+ console.log(`šŸš€ Fullstack server running on http://localhost:${config.port}`);
131
+ console.log(`šŸ“Š Environment: ${process.env.NODE_ENV || 'development'}`);
132
+
133
+ // Safely check auth status
134
+ let authStatus = 'disabled';
135
+ try {
136
+ if (typeof server.getAuth === 'function') {
137
+ authStatus = server.getAuth() ? 'enabled' : 'disabled';
138
+ }
139
+ } catch (e) {
140
+ // Auth check failed
141
+ }
142
+ console.log(`šŸ”‘ Auth: ${authStatus}`);
143
+
144
+ // Safely check database status
145
+ let dbStatus = 'not configured';
146
+ try {
147
+ if (typeof server.getDatabase === 'function') {
148
+ dbStatus = server.getDatabase() ? 'connected' : 'not configured';
149
+ }
150
+ } catch (e) {
151
+ // Database check failed
152
+ }
153
+ console.log(`šŸ’¾ Database: ${dbStatus}`);
154
+
155
+ console.log(`🌐 API docs: http://localhost:${config.port}/api-docs`);
156
+ console.log(`ā¤ļø Health check: http://localhost:${config.port}/health`);
157
+
158
+ return server;
159
+
160
+ } catch (error) {
161
+ console.error('āŒ Failed to start server:', error);
162
+
163
+ if (error.message.includes('Express not installed')) {
164
+ console.log('\nšŸ’” To fix this, run:');
165
+ console.log(' npm install express @types/express');
166
+ } else if (error.message.includes('createServer function not available')) {
167
+ console.log('\nšŸ’” To fix this, make sure baraqex is properly installed:');
168
+ console.log(' npm install baraqex');
169
+ console.log(' npm install express @types/express');
170
+ } else if (error.message.includes('timeout')) {
171
+ console.log('\nšŸ’” Baraqex is taking longer than expected to initialize.');
172
+ console.log(' This might be due to missing dependencies.');
173
+ console.log(' Try installing: npm install express @types/express');
174
+ }
175
+
176
+ process.exit(1);
177
+ }
58
178
  }
59
179
 
60
- // Rate limiting
61
- const limiter = rateLimit({
62
- windowMs: 15 * 60 * 1000, // 15 minutes
63
- max: process.env.NODE_ENV === 'production' ? 100 : 1000, // limit each IP to 100 requests per windowMs in production
64
- message: {
65
- error: 'Too many requests from this IP, please try again later.'
180
+ // Database connection helper
181
+ async function connectDatabase(server) {
182
+ let db = null;
183
+ try {
184
+ if (typeof server.getDatabase === 'function') {
185
+ db = server.getDatabase();
186
+ }
187
+ } catch (e) {
188
+ console.log('āš ļø Database functionality not available');
189
+ return;
66
190
  }
67
- });
68
- app.use('/api', limiter);
69
-
70
- // Health check endpoint
71
- app.get('/health', (req, res) => {
72
- res.json({
73
- status: 'ok',
74
- timestamp: new Date().toISOString(),
75
- version: process.env.npm_package_version || '1.0.0',
76
- environment: process.env.NODE_ENV || 'development'
77
- });
78
- });
79
-
80
- // API status endpoint
81
- app.get('/api/status', (req, res) => {
82
- const db = server.getDatabase();
83
- res.json({
84
- api: 'online',
85
- database: db ? 'connected' : 'not configured',
86
- authentication: server.getAuth() ? 'enabled' : 'disabled',
87
- timestamp: new Date().toISOString()
88
- });
89
- });
90
-
91
- // Enable SSR with hydration
92
- server.enableSSR({ hydratable: true });
93
-
94
- // Database connection
95
- async function connectDatabase() {
96
- const db = server.getDatabase();
97
- if (db) {
191
+
192
+ if (db && typeof db.connect === 'function') {
98
193
  try {
99
194
  await db.connect();
100
195
  console.log('āœ… Database connected successfully');
@@ -112,42 +207,13 @@ async function connectDatabase() {
112
207
  // Graceful shutdown
113
208
  process.on('SIGTERM', async () => {
114
209
  console.log('šŸ”„ SIGTERM received, shutting down gracefully...');
115
-
116
- try {
117
- await server.stop();
118
- const db = server.getDatabase();
119
- if (db) {
120
- await db.disconnect();
121
- }
122
- console.log('āœ… Server shut down complete');
123
- process.exit(0);
124
- } catch (error) {
125
- console.error('āŒ Error during shutdown:', error);
126
- process.exit(1);
127
- }
210
+ process.exit(0);
128
211
  });
129
212
 
130
- // Start server
131
- async function startServer() {
132
- try {
133
- // Connect to database first
134
- await connectDatabase();
135
-
136
- // Start the server
137
- await server.start();
138
-
139
- console.log(`šŸš€ Fullstack server running on http://localhost:${config.port}`);
140
- console.log(`šŸ“Š Environment: ${process.env.NODE_ENV || 'development'}`);
141
- console.log(`šŸ”‘ Auth: ${server.getAuth() ? 'enabled' : 'disabled'}`);
142
- console.log(`šŸ’¾ Database: ${server.getDatabase() ? 'connected' : 'not configured'}`);
143
- console.log(`🌐 API docs: http://localhost:${config.port}/api-docs`);
144
- console.log(`ā¤ļø Health check: http://localhost:${config.port}/health`);
145
-
146
- } catch (error) {
147
- console.error('āŒ Failed to start server:', error);
148
- process.exit(1);
149
- }
150
- }
213
+ process.on('SIGINT', async () => {
214
+ console.log('šŸ”„ SIGINT received, shutting down gracefully...');
215
+ process.exit(0);
216
+ });
151
217
 
152
218
  // Handle unhandled promise rejections
153
219
  process.on('unhandledRejection', (reason, promise) => {
@@ -163,5 +229,3 @@ process.on('uncaughtException', (error) => {
163
229
 
164
230
  // Start the server
165
231
  startServer();
166
-
167
- export default server;
@@ -0,0 +1,30 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "module": "ESNext",
5
+ "moduleResolution": "node",
6
+ "allowSyntheticDefaultImports": true,
7
+ "esModuleInterop": true,
8
+ "allowJs": true,
9
+ "skipLibCheck": true,
10
+ "strict": true,
11
+ "forceConsistentCasingInFileNames": true,
12
+ "declaration": true,
13
+ "outDir": "./dist",
14
+ "rootDir": "./src",
15
+ "resolveJsonModule": true,
16
+ "isolatedModules": true,
17
+ "noEmit": false,
18
+ "jsx": "react-jsx",
19
+ "lib": ["ES2022", "DOM", "DOM.Iterable"]
20
+ },
21
+ "include": [
22
+ "src/**/*"
23
+ ],
24
+ "exclude": [
25
+ "node_modules",
26
+ "dist",
27
+ "**/*.test.ts",
28
+ "**/*.spec.ts"
29
+ ]
30
+ }