roster-server 1.5.0 → 1.5.4

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,14 @@
1
+ const path = require('path');
2
+ const file = path.join(__dirname, '..', '..', 'index.js');
3
+ const Roster = require(file);
4
+
5
+ const roster = new Roster({
6
+ email: 'admin@example.com',
7
+ });
8
+
9
+ roster.registerSite('zentara.group', (req, res) => {
10
+ res.writeHead(200);
11
+ res.end('Hello from example.com!');
12
+ });
13
+
14
+ roster.start();
@@ -23,8 +23,4 @@ const app = (httpsServer) => {
23
23
  };
24
24
  };
25
25
 
26
- app.config = {
27
- port: 3334,
28
- };
29
-
30
26
  module.exports = app;
package/index.js CHANGED
@@ -12,22 +12,30 @@ class Roster {
12
12
  this.cluster = options.cluster || false;
13
13
  this.domains = [];
14
14
  this.sites = {};
15
+ this.hostname = options.hostname || '0.0.0.0';
16
+ this.filename = options.filename || 'index';
15
17
 
16
18
  const port = options.port || 443;
17
19
  if (port === 80) {
18
- throw new Error('Port 80 is reserved for ACME challenge. Please use a different port.');
20
+ throw new Error('⚠️ Port 80 is reserved for ACME challenge. Please use a different port.');
19
21
  }
20
22
  this.port = port;
21
23
  }
22
24
 
23
25
  loadSites() {
26
+ // Check if wwwPath exists
27
+ if (!fs.existsSync(this.wwwPath)) {
28
+ console.warn(`⚠️ WWW path does not exist: ${this.wwwPath}`);
29
+ return;
30
+ }
31
+
24
32
  fs.readdirSync(this.wwwPath, { withFileTypes: true })
25
33
  .filter(dirent => dirent.isDirectory())
26
34
  .forEach((dirent) => {
27
35
  const domain = dirent.name;
28
36
  const domainPath = path.join(this.wwwPath, domain);
29
37
 
30
- const possibleIndexFiles = ['index.js', 'index.mjs', 'index.cjs'];
38
+ const possibleIndexFiles = ['js', 'mjs', 'cjs'].map(ext => `${this.filename}.${ext}`);
31
39
  let siteApp;
32
40
  let loadedFile;
33
41
 
@@ -47,9 +55,9 @@ class Roster {
47
55
  this.sites[d] = siteApp;
48
56
  });
49
57
 
50
- console.log(`✅ Loaded site: ${domain} (using ${loadedFile})`);
58
+ console.log(`✅ Loaded site: ${domain} (using ${loadedFile})`);
51
59
  } else {
52
- console.warn(`⚠️ No index file (js/mjs/cjs) found in ${domainPath}`);
60
+ console.warn(`⚠️ No index file (js/mjs/cjs) found in ${domainPath}`);
53
61
  }
54
62
  });
55
63
  }
@@ -127,16 +135,16 @@ class Roster {
127
135
  const currentConfigContentFormatted = JSON.stringify(currentConfig, null, 2);
128
136
 
129
137
  if (newConfigContent === currentConfigContentFormatted) {
130
- console.log('ℹ️ Configuration has not changed. config.json will not be overwritten.');
138
+ console.log('ℹ️ Configuration has not changed. config.json will not be overwritten.');
131
139
  return;
132
140
  }
133
- console.log('🔄 Configuration has changed. config.json will be updated.');
141
+ console.log('🔄 Configuration has changed. config.json will be updated.');
134
142
  } else {
135
- console.log('🆕 config.json does not exist. A new one will be created.');
143
+ console.log('🆕 config.json does not exist. A new one will be created.');
136
144
  }
137
145
 
138
146
  fs.writeFileSync(configPath, JSON.stringify(newConfig, null, 2));
139
- console.log(`📁 config.json generated at ${configPath}`);
147
+ console.log(`📁 config.json generated at ${configPath}`);
140
148
  }
141
149
 
142
150
  handleRequest(req, res) {
@@ -158,27 +166,25 @@ class Roster {
158
166
  }
159
167
  }
160
168
 
161
- initServers(glx) {
162
- const app = (req, res) => {
163
- this.handleRequest(req, res);
164
- };
169
+ registerSite(domain, requestHandler) {
170
+ if (!domain) {
171
+ throw new Error('Domain is required');
172
+ }
173
+ if (typeof requestHandler !== 'function') {
174
+ throw new Error('requestHandler must be a function');
175
+ }
165
176
 
166
- // Obtener los servidores sin iniciarlos
167
- const httpsServer = glx.httpsServer(null, app);
168
- const httpServer = glx.httpServer();
169
-
170
- // Inicializar las aplicaciones Socket.IO con el servidor HTTPS
171
- for (const [host, siteApp] of Object.entries(this.sites)) {
172
- if (!host.startsWith('www.')) {
173
- const appInstance = siteApp(httpsServer);
174
- this.sites[host] = appInstance;
175
- this.sites[`www.${host}`] = appInstance;
176
- console.log(`🔧 Initialized server for ${host}`);
177
- }
177
+ const domainEntries = [domain];
178
+ if ((domain.match(/\./g) || []).length < 2) {
179
+ domainEntries.push(`www.${domain}`);
178
180
  }
179
181
 
180
- // Retornar los servidores para iniciarlos después
181
- return { httpsServer, httpServer };
182
+ this.domains.push(...domainEntries);
183
+ domainEntries.forEach(d => {
184
+ this.sites[d] = requestHandler;
185
+ });
186
+
187
+ console.log(`✅ Manually registered site: ${domain}`);
182
188
  }
183
189
 
184
190
  start() {
@@ -193,26 +199,31 @@ class Roster {
193
199
  staging: this.staging
194
200
  });
195
201
 
196
- // Usar una promesa para manejar la inicialización
197
- return new Promise((resolve, reject) => {
198
- try {
199
- greenlock.ready((glx) => {
200
- const { httpsServer, httpServer } = this.initServers(glx);
201
-
202
- // Primero iniciar el servidor HTTPS
203
- httpsServer.listen(this.port, '0.0.0.0', () => {
204
- console.log('ℹ️ HTTPS server listening on port ' + this.port);
205
-
206
- // Después iniciar el servidor HTTP
207
- httpServer.listen(80, '0.0.0.0', () => {
208
- console.log('ℹ️ HTTP server listening on port 80');
209
- resolve({ httpsServer, httpServer });
210
- });
211
- });
212
- });
213
- } catch (error) {
214
- reject(error);
202
+ const app = (req, res) => {
203
+ this.handleRequest(req, res);
204
+ };
205
+
206
+ greenlock.ready(glx => {
207
+ // Obtener los servidores sin iniciarlos
208
+ const httpsServer = glx.httpsServer(null, app);
209
+ const httpServer = glx.httpServer();
210
+
211
+ for (const [host, siteApp] of Object.entries(this.sites)) {
212
+ if (!host.startsWith('www.')) {
213
+ const appInstance = siteApp(httpsServer);
214
+ this.sites[host] = appInstance;
215
+ this.sites[`www.${host}`] = appInstance;
216
+ console.log(`🔧 Initialized server for ${host}`);
217
+ }
215
218
  }
219
+
220
+ httpServer.listen(80, this.hostname, () => {
221
+ console.log('ℹ️ HTTP server listening on port 80');
222
+ });
223
+
224
+ httpsServer.listen(this.port, this.hostname, () => {
225
+ console.log('ℹ️ HTTPS server listening on port ' + this.port);
226
+ });
216
227
  });
217
228
  }
218
229
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "roster-server",
3
- "version": "1.5.0",
3
+ "version": "1.5.4",
4
4
  "description": "👾 RosterServer - A domain host router to host multiple HTTPS.",
5
5
  "main": "index.js",
6
6
  "scripts": {