roster-server 1.2.6 → 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,33 +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
- cors: {
9
- origin: "*",
10
- methods: ["GET", "POST"]
11
- },
12
- allowEIO3: true,
13
- transports: ['websocket', 'polling']
14
- });
15
-
16
- // Handle socket connections
17
- io.on('connection', (socket) => {
5
+ attach.on('connection', (socket) => {
18
6
  console.log('A user connected');
19
7
 
20
- // Handle chat messages
21
8
  socket.on('chat:message', (msg) => {
22
9
  console.log('Message received:', msg);
23
- // Broadcast the message to all connected clients
24
10
  io.emit('chat:message', msg);
25
11
  });
26
12
 
27
- // Handle disconnection
28
13
  socket.on('disconnect', () => {
29
14
  console.log('User disconnected');
30
15
  });
31
16
  });
32
17
 
33
- 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 {
@@ -184,9 +184,11 @@ class Roster {
184
184
  staging: this.staging,
185
185
  manager: { module: "@greenlock/manager" },
186
186
  approveDomains: (opts, certs, cb) => {
187
+ // If certs is defined, we already have a certificate and are renewing it
187
188
  if (certs) {
188
189
  opts.domains = certs.altnames;
189
190
  } else {
191
+ // If it's a new request, verify if the domain is in our list
190
192
  if (this.domains.includes(opts.domain)) {
191
193
  opts.email = this.maintainerEmail;
192
194
  opts.agreeTos = true;
@@ -204,15 +206,13 @@ class Roster {
204
206
  this.handleRequest(req, res);
205
207
  });
206
208
 
207
- // Agregar soporte para WebSocket
208
- httpsServer.on('upgrade', (req, socket, head) => {
209
- const host = req.headers.host?.split(':')[0] || '';
210
- const siteApp = this.sites[host];
211
-
212
- if (siteApp && siteApp.upgrade) {
213
- siteApp.upgrade(req, socket, head);
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
214
  }
215
- });
215
+ }
216
216
 
217
217
  httpsServer.listen(443, "0.0.0.0", () => {
218
218
  console.info("ℹ️ HTTPS Listening on", httpsServer.address());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "roster-server",
3
- "version": "1.2.6",
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": {