roster-server 1.2.4 → 1.3.0

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.
package/README.md CHANGED
@@ -30,7 +30,7 @@ Your project should look something like this:
30
30
  └── www/
31
31
  ├── example.com/
32
32
  │ └── index.js
33
- └── anotherdomain.com/
33
+ └── subdomain.example.com/
34
34
  └── index.js
35
35
  ```
36
36
 
@@ -1,26 +1,18 @@
1
- const https = require('https');
2
1
  const { Server } = require('socket.io');
3
2
 
4
- const server = https.createServer();
3
+ const attach = new Server();
5
4
 
6
- // Initialize Socket.IO with the HTTPS server
7
- const io = new Server(server);
8
-
9
- // Handle socket connections
10
- io.on('connection', (socket) => {
5
+ attach.on('connection', (socket) => {
11
6
  console.log('A user connected');
12
7
 
13
- // Handle chat messages
14
8
  socket.on('chat:message', (msg) => {
15
9
  console.log('Message received:', msg);
16
- // Broadcast the message to all connected clients
17
10
  io.emit('chat:message', msg);
18
11
  });
19
12
 
20
- // Handle disconnection
21
13
  socket.on('disconnect', () => {
22
14
  console.log('User disconnected');
23
15
  });
24
16
  });
25
17
 
26
- module.exports = server;
18
+ module.exports = { attach };
package/index.js CHANGED
@@ -20,7 +20,6 @@ class Roster {
20
20
  const domain = dirent.name;
21
21
  const domainPath = path.join(this.wwwPath, domain);
22
22
 
23
- // Check for different module file extensions
24
23
  const possibleIndexFiles = ['index.js', 'index.mjs', 'index.cjs'];
25
24
  let siteApp;
26
25
  let loadedFile;
@@ -29,16 +28,13 @@ class Roster {
29
28
  const indexPath = path.join(domainPath, indexFile);
30
29
  if (fs.existsSync(indexPath)) {
31
30
  const module = require(indexPath);
32
- // Si el módulo es una función o un servidor, usarlo directamente
33
- siteApp = typeof module === 'function' ? module :
34
- (module.handleRequest || module.handle || module);
31
+ siteApp = typeof module === 'function' ? module() : module;
35
32
  loadedFile = indexFile;
36
33
  break;
37
34
  }
38
35
  }
39
36
 
40
37
  if (siteApp) {
41
- // Add the main domain and 'www' subdomain by default
42
38
  const domainEntries = [domain, `www.${domain}`];
43
39
  this.domains.push(...domainEntries);
44
40
  domainEntries.forEach(d => {
@@ -46,6 +42,9 @@ class Roster {
46
42
  });
47
43
 
48
44
  console.log(`✅ Loaded site: ${domain} (using ${loadedFile})`);
45
+ if (siteApp.attach) {
46
+ console.log(`🔌 Attachable server detected for ${domain}`);
47
+ }
49
48
  } else {
50
49
  console.warn(`⚠️ No index file (js/mjs/cjs) found in ${domainPath}`);
51
50
  }
@@ -150,23 +149,24 @@ class Roster {
150
149
  handleRequest(req, res) {
151
150
  const host = req.headers.host || '';
152
151
 
153
- // Eliminar el puerto del host si está presente
154
- const cleanHost = host.split(':')[0];
155
-
156
- // Handle www redirect
157
- if (cleanHost.startsWith('www.')) {
158
- const newHost = cleanHost.slice(4);
152
+ if (host.startsWith('www.')) {
153
+ const newHost = host.slice(4);
159
154
  res.writeHead(301, { Location: `https://${newHost}${req.url}` });
160
155
  res.end();
161
156
  return;
162
157
  }
163
158
 
164
- // Find and execute the appropriate site handler
165
- const siteApp = this.sites[cleanHost];
159
+ const siteApp = this.sites[host];
166
160
  if (siteApp) {
167
- if (siteApp.emit) {
161
+ if (siteApp.attach) {
162
+ // Para servidores attachables (como Socket.IO)
163
+ res.writeHead(200);
164
+ res.end('Server running');
165
+ } else if (siteApp.emit) {
166
+ // Para servidores http/https (como subdomain.example.com)
168
167
  siteApp.emit('request', req, res);
169
168
  } else {
169
+ // Para funciones de manejo directo (como example.com)
170
170
  siteApp(req, res);
171
171
  }
172
172
  } else {
@@ -206,6 +206,14 @@ class Roster {
206
206
  this.handleRequest(req, res);
207
207
  });
208
208
 
209
+ // Attach any servers to the HTTPS server
210
+ for (const [host, siteApp] of Object.entries(this.sites)) {
211
+ if (siteApp.attach) {
212
+ siteApp.attach.attach(httpsServer);
213
+ console.log(`🔌 Server attached for ${host}`);
214
+ }
215
+ }
216
+
209
217
  httpsServer.listen(443, "0.0.0.0", () => {
210
218
  console.info("ℹ️ HTTPS Listening on", httpsServer.address());
211
219
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "roster-server",
3
- "version": "1.2.4",
3
+ "version": "1.3.0",
4
4
  "description": "👾 RosterServer - A domain host router to host multiple HTTPS.",
5
5
  "main": "index.js",
6
6
  "scripts": {