roster-server 1.5.2 → 1.5.6
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/demo/www/manual.js +16 -0
- package/demo/www/sio.example.com/index.js +0 -4
- package/index.js +36 -9
- package/package.json +1 -1
|
@@ -0,0 +1,16 @@
|
|
|
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('example.com', (httpsServer) => {
|
|
10
|
+
return (req, res) => {
|
|
11
|
+
res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
|
|
12
|
+
res.end('"Loco de pensar, queriendo entrar en razón, y el corazón tiene razones que la propia razón nunca entenderá."');
|
|
13
|
+
};
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
roster.start();
|
package/index.js
CHANGED
|
@@ -17,12 +17,18 @@ class Roster {
|
|
|
17
17
|
|
|
18
18
|
const port = options.port || 443;
|
|
19
19
|
if (port === 80) {
|
|
20
|
-
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.');
|
|
21
21
|
}
|
|
22
22
|
this.port = port;
|
|
23
23
|
}
|
|
24
24
|
|
|
25
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
|
+
|
|
26
32
|
fs.readdirSync(this.wwwPath, { withFileTypes: true })
|
|
27
33
|
.filter(dirent => dirent.isDirectory())
|
|
28
34
|
.forEach((dirent) => {
|
|
@@ -49,9 +55,9 @@ class Roster {
|
|
|
49
55
|
this.sites[d] = siteApp;
|
|
50
56
|
});
|
|
51
57
|
|
|
52
|
-
console.log(`✅
|
|
58
|
+
console.log(`✅ Loaded site: ${domain} (using ${loadedFile})`);
|
|
53
59
|
} else {
|
|
54
|
-
console.warn(`⚠️
|
|
60
|
+
console.warn(`⚠️ No index file (js/mjs/cjs) found in ${domainPath}`);
|
|
55
61
|
}
|
|
56
62
|
});
|
|
57
63
|
}
|
|
@@ -129,16 +135,16 @@ class Roster {
|
|
|
129
135
|
const currentConfigContentFormatted = JSON.stringify(currentConfig, null, 2);
|
|
130
136
|
|
|
131
137
|
if (newConfigContent === currentConfigContentFormatted) {
|
|
132
|
-
console.log('ℹ️
|
|
138
|
+
console.log('ℹ️ Configuration has not changed. config.json will not be overwritten.');
|
|
133
139
|
return;
|
|
134
140
|
}
|
|
135
|
-
console.log('🔄
|
|
141
|
+
console.log('🔄 Configuration has changed. config.json will be updated.');
|
|
136
142
|
} else {
|
|
137
|
-
console.log('🆕
|
|
143
|
+
console.log('🆕 config.json does not exist. A new one will be created.');
|
|
138
144
|
}
|
|
139
145
|
|
|
140
146
|
fs.writeFileSync(configPath, JSON.stringify(newConfig, null, 2));
|
|
141
|
-
console.log(`📁
|
|
147
|
+
console.log(`📁 config.json generated at ${configPath}`);
|
|
142
148
|
}
|
|
143
149
|
|
|
144
150
|
handleRequest(req, res) {
|
|
@@ -160,6 +166,27 @@ class Roster {
|
|
|
160
166
|
}
|
|
161
167
|
}
|
|
162
168
|
|
|
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
|
+
}
|
|
176
|
+
|
|
177
|
+
const domainEntries = [domain];
|
|
178
|
+
if ((domain.match(/\./g) || []).length < 2) {
|
|
179
|
+
domainEntries.push(`www.${domain}`);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
this.domains.push(...domainEntries);
|
|
183
|
+
domainEntries.forEach(d => {
|
|
184
|
+
this.sites[d] = requestHandler;
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
console.log(`✅ Manually registered site: ${domain}`);
|
|
188
|
+
}
|
|
189
|
+
|
|
163
190
|
start() {
|
|
164
191
|
this.loadSites();
|
|
165
192
|
this.generateConfigJson();
|
|
@@ -191,11 +218,11 @@ class Roster {
|
|
|
191
218
|
}
|
|
192
219
|
|
|
193
220
|
httpServer.listen(80, this.hostname, () => {
|
|
194
|
-
console.log('ℹ️
|
|
221
|
+
console.log('ℹ️ HTTP server listening on port 80');
|
|
195
222
|
});
|
|
196
223
|
|
|
197
224
|
httpsServer.listen(this.port, this.hostname, () => {
|
|
198
|
-
console.log('ℹ️
|
|
225
|
+
console.log('ℹ️ HTTPS server listening on port ' + this.port);
|
|
199
226
|
});
|
|
200
227
|
});
|
|
201
228
|
}
|