redweb 0.2.8 → 0.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
@@ -1,3 +1,8 @@
1
+ You're right, let's provide more detailed instructions for setting up an HTTP server, including details about the `publicPaths` property and providing a sample HTML file.
2
+
3
+ ### `README.md`
4
+
5
+ ```markdown
1
6
  # RedWeb
2
7
 
3
8
  RedWeb is a simple and flexible Node.js framework built on top of Express.js and WebSocket. It allows you to quickly set up web servers and WebSocket servers with customizable options.
@@ -88,7 +93,7 @@ const options = {
88
93
  console.log('WebSocket client disconnected');
89
94
  },
90
95
  messageHandlers: {
91
- 'msg': () => (socket, data) => {
96
+ 'msg': (socket, data) => {
92
97
  console.log(data);
93
98
  socket.send('guuuuuurl');
94
99
  }
@@ -116,7 +121,7 @@ const options = {
116
121
  console.log('WebSocket client disconnected');
117
122
  },
118
123
  messageHandlers: {
119
- 'msg': () (socket, data) => {
124
+ 'msg': (socket, data) => {
120
125
  console.log(data);
121
126
  socket.send('guuuuuurl');
122
127
  }
@@ -142,7 +147,7 @@ const socketServer = new SocketServer({
142
147
  console.log('WebSocket client disconnected');
143
148
  },
144
149
  messageHandlers: {
145
- 'msg': () => (socket, data) {
150
+ 'msg': (socket, data) => {
146
151
  console.log(data);
147
152
  socket.send('guuuuuurl');
148
153
  }
@@ -174,6 +179,85 @@ console.log(socketServer.clients); // Map of clients by their IP addresses
174
179
  - **messageHandlers**: Object containing message handlers based on message type.
175
180
  - **ssl**: SSL configuration for SecureSocketServer (`{ key: './path/to/key.pem', cert: './path/to/cert.pem' }`).
176
181
 
182
+ ## HowTo
183
+
184
+ ### Setting Up an HTTP Server
185
+
186
+ 1. **Install RedWeb**: Run `npm install redweb`.
187
+ 2. **Create a new directory** (e.g., `public`) to store your static files and add an `index.html` file:
188
+
189
+ ```html
190
+ <!DOCTYPE html>
191
+ <html lang="en">
192
+ <head>
193
+ <meta charset="UTF-8">
194
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
195
+ <title>RedWeb HTTP Server</title>
196
+ </head>
197
+ <body>
198
+ <h1>Welcome to RedWeb HTTP Server!</h1>
199
+ </body>
200
+ </html>
201
+ ```
202
+
203
+ 3. **Create a new file** (e.g., `server.js`) and add the following code:
204
+
205
+ ```javascript
206
+ const { HttpServer } = require('redweb');
207
+
208
+ const services = [
209
+ {
210
+ serviceName: '/api/hello',
211
+ method: 'get',
212
+ function: (req, res) => {
213
+ res.send('Hello World!');
214
+ }
215
+ }
216
+ ];
217
+
218
+ const options = {
219
+ port: 3000,
220
+ publicPaths: ['./public'],
221
+ services: services
222
+ };
223
+
224
+ const app = new HttpServer(options);
225
+ ```
226
+
227
+ 4. **Run your server**: Execute `node server.js`.
228
+
229
+ ### Setting Up a WebSocket Server
230
+
231
+ 1. **Install RedWeb**: Run `npm install redweb`.
232
+ 2. **Create a new file** (e.g., `socketServer.js`) and add the following code:
233
+
234
+ ```javascript
235
+ const { SocketServer } = require('redweb');
236
+
237
+ const options = {
238
+ port: 3000,
239
+ connectionOpenCallback: (socket) => {
240
+ console.log('WebSocket client connected');
241
+ },
242
+ connectionCloseCallback: (socket) => {
243
+ console.log('WebSocket client disconnected');
244
+ },
245
+ messageHandlers: {
246
+ 'msg': (socket, data) => {
247
+ console.log(data);
248
+ socket.send('guuuuuurl');
249
+ }
250
+ }
251
+ };
252
+
253
+ const socketServer = new SocketServer(options);
254
+ ```
255
+
256
+ 3. **Run your server**: Execute `node socketServer.js`.
257
+
177
258
  ## License
178
259
 
179
- MIT License
260
+ MIT License
261
+ ```
262
+
263
+ This `README.md` now includes more detailed instructions for setting up an HTTP server, including the creation of a `public` directory and a sample `index.html` file.
package/index.js CHANGED
@@ -1,13 +1,9 @@
1
- const { HttpServer, HttpsServer, ENCODINGS, METHODS, HTTP_OPTIONS } = require('./HttpServer');
2
- const { SocketServer, SecureSocketServer, SOCKET_OPTIONS } = require('./SocketServer');
1
+ const SocketServer = require('./SocketServer');
2
+ const SecureSocketServer = require('./SecureSocketServer');
3
+ const { SOCKET_OPTIONS } = require('./BaseSocketServer');
3
4
 
4
5
  module.exports = {
5
- HttpServer,
6
- HttpsServer,
7
6
  SocketServer,
8
7
  SecureSocketServer,
9
- ENCODINGS,
10
- METHODS,
11
- HTTP_OPTIONS,
12
8
  SOCKET_OPTIONS
13
- };
9
+ };
package/package.json CHANGED
@@ -1,16 +1,13 @@
1
1
  {
2
2
  "name": "redweb",
3
- "version": "0.2.8",
3
+ "version": "0.3.0",
4
4
  "description": "A way to quickly set up an express server",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
7
  "test": "jest"
8
8
  },
9
9
  "files": [
10
- "index.js",
11
- "HttpServer.js",
12
- "SocketServer.js",
13
- "sslConfig.js"
10
+ "src/*"
14
11
  ],
15
12
  "keywords": [],
16
13
  "author": "",
@@ -1,8 +1,6 @@
1
1
  const express = require('express');
2
2
  const bodyParser = require('body-parser');
3
3
  const path = require('path');
4
- const https = require('https');
5
- const loadSslConfig = require('./sslConfig');
6
4
 
7
5
  /**
8
6
  * @typedef {'json' | 'urlencoded'} RedWebEncoding
@@ -23,8 +21,6 @@ const loadSslConfig = require('./sslConfig');
23
21
  */
24
22
 
25
23
  const ENCODINGS = { json: 'json', urlencoded: 'urlencoded' };
26
- const METHODS = { POST: 'post', GET: 'get' };
27
-
28
24
  const HTTP_OPTIONS = {
29
25
  port: 80,
30
26
  bind: '0.0.0.0',
@@ -56,33 +52,8 @@ function BaseHttpServer(options = {}) {
56
52
  this.publicPaths.forEach(public_path => this.app.use(express.static(path.join(process.cwd(), public_path))));
57
53
  }
58
54
 
59
- /**
60
- * HTTP Server
61
- * @param {RedWebOptions} options - Configuration options for RedWeb.
62
- * @return {Object} Express application instance.
63
- */
64
- function HttpServer(options = {}) {
65
- BaseHttpServer.call(this, options);
66
- this.app.listen(this.port, this.listenCallback ? this.listenCallback : () => console.log(`RedWeb HttpServer listening on port ${this.port}`));
67
- return this;
68
- }
69
-
70
- /**
71
- * HTTPS Server
72
- * @param {RedWebOptions} options - Configuration options for RedWeb.
73
- * @return {Object} Express application instance.
74
- */
75
- function HttpsServer(options = {}) {
76
- BaseHttpServer.call(this, options);
77
- const sslOptions = loadSslConfig(this.ssl);
78
- https.createServer(sslOptions, this.app).listen(this.port, this.listenCallback ? this.listenCallback : () => console.log(`RedWeb HttpsServer listening on port ${this.port}`));
79
- return this;
80
- }
81
-
82
55
  module.exports = {
83
- HttpServer,
84
- HttpsServer,
56
+ BaseHttpServer,
85
57
  ENCODINGS,
86
- METHODS,
87
58
  HTTP_OPTIONS
88
59
  };
@@ -0,0 +1,14 @@
1
+ const { BaseHttpServer } = require('./BaseHttpServer');
2
+
3
+ /**
4
+ * HTTP Server
5
+ * @param {RedWebOptions} options - Configuration options for RedWeb.
6
+ * @return {Object} Express application instance.
7
+ */
8
+ function HttpServer(options = {}) {
9
+ BaseHttpServer.call(this, options);
10
+ this.app.listen(this.port, this.listenCallback ? this.listenCallback : () => console.log(`RedWeb HttpServer listening on port ${this.port}`));
11
+ return this;
12
+ }
13
+
14
+ module.exports = HttpServer;
@@ -0,0 +1,17 @@
1
+ const https = require('https');
2
+ const { BaseHttpServer } = require('./BaseHttpServer');
3
+ const loadSslConfig = require('./sslConfig');
4
+
5
+ /**
6
+ * HTTPS Server
7
+ * @param {RedWebOptions} options - Configuration options for RedWeb.
8
+ * @return {Object} Express application instance.
9
+ */
10
+ function HttpsServer(options = {}) {
11
+ BaseHttpServer.call(this, options);
12
+ const sslOptions = loadSslConfig(this.ssl);
13
+ https.createServer(sslOptions, this.app).listen(this.port, this.listenCallback ? this.listenCallback : () => console.log(`RedWeb HttpsServer listening on port ${this.port}`));
14
+ return this;
15
+ }
16
+
17
+ module.exports = HttpsServer;
@@ -0,0 +1,75 @@
1
+ const WebSocket = require('ws');
2
+
3
+ /**
4
+ * @typedef {Object} SocketServerOptions
5
+ * @property {number} [port=3000] - The port number to bind the socket server.
6
+ * @property {Function} [connectionOpenCallback] - Callback function to execute once a client connects.
7
+ * @property {Function} [connectionCloseCallback] - Callback function to execute once a client disconnects.
8
+ * @property {Function} [messageCallback] - Callback function to execute for every message received.
9
+ * @property {Object} [messageHandlers] - Object containing message handlers based on message type.
10
+ * @property {Object} [ssl] - SSL configuration for SecureSocketServer.
11
+ * @property {string} [ssl.key] - Path to the SSL key file.
12
+ * @property {string} [ssl.cert] - Path to the SSL certificate file.
13
+ */
14
+
15
+ const SOCKET_OPTIONS = {
16
+ port: 3000,
17
+ connectionOpenCallback: undefined,
18
+ connectionCloseCallback: undefined,
19
+ messageCallback: undefined,
20
+ messageHandlers: {
21
+ ping: (socket, data) => socket.send(JSON.stringify({ type: 'pong' }))
22
+ },
23
+ ssl: null
24
+ };
25
+
26
+ /**
27
+ * Base Socket Server
28
+ * @param {Object} server - The HTTP or HTTPS server instance.
29
+ * @param {SocketServerOptions} options - Configuration options for SocketServer.
30
+ */
31
+ function BaseSocketServer(server, options = {}) {
32
+ this.wss = new WebSocket.Server({ server });
33
+ this.clients = new Map(); // Use a Map to store clients by their IP addresses
34
+ Object.assign(this, { ...SOCKET_OPTIONS, ...options });
35
+
36
+ this.wss.on('connection', this.handleConnection.bind(this));
37
+ }
38
+
39
+ BaseSocketServer.prototype.handleConnection = function (socket, req) {
40
+ const ip = req.socket.remoteAddress;
41
+ console.log(`New client connected: ${ip}`);
42
+ this.clients.set(ip, socket);
43
+
44
+ if (this.connectionOpenCallback) this.connectionOpenCallback(socket);
45
+
46
+ socket.on('message', (message) => this.handleMessage(socket, message, ip));
47
+ socket.on('close', () => this.handleClose(socket, ip));
48
+ socket.on('error', (error) => this.handleError(socket, error, ip));
49
+ };
50
+
51
+ BaseSocketServer.prototype.handleMessage = function (socket, message, ip) {
52
+ try {
53
+ console.info(`Received message from ${ip}: ${message}`);
54
+ if (this.messageCallback) this.messageCallback(socket, message);
55
+
56
+ const { type, data } = JSON.parse(message);
57
+ if (this.messageHandlers[type]) {
58
+ this.messageHandlers[type](socket, data);
59
+ }
60
+ } catch (error) {
61
+ console.error(`Error handling message from ${ip}:`, error);
62
+ }
63
+ };
64
+
65
+ BaseSocketServer.prototype.handleClose = function (socket, ip) {
66
+ console.log(`Client disconnected: ${ip}`);
67
+ this.clients.delete(ip);
68
+ if (this.connectionCloseCallback) this.connectionCloseCallback(socket);
69
+ };
70
+
71
+ BaseSocketServer.prototype.handleError = function (socket, error, ip) {
72
+ console.error(`Socket error from ${ip}:`, error);
73
+ };
74
+
75
+ module.exports = BaseSocketServer;
@@ -0,0 +1,18 @@
1
+ const https = require('https');
2
+ const loadSslConfig = require('../sslConfig');
3
+ const BaseSocketServer = require('./BaseSocketServer');
4
+
5
+ /**
6
+ * Secure WebSocket Server
7
+ * @param {SocketServerOptions} options - Configuration options for SecureSocketServer.
8
+ * @return {Object} WebSocket server instance.
9
+ */
10
+ function SecureSocketServer(options = {}) {
11
+ const sslOptions = loadSslConfig(options.ssl);
12
+ const server = https.createServer(sslOptions);
13
+ BaseSocketServer.call(this, server, options);
14
+ server.listen(this.port, () => console.log(`RedWeb SecureSocketServer listening on port ${this.port}`));
15
+ return this;
16
+ }
17
+
18
+ module.exports = SecureSocketServer;
@@ -0,0 +1,16 @@
1
+ const http = require('http');
2
+ const BaseSocketServer = require('./BaseSocketServer');
3
+
4
+ /**
5
+ * WebSocket Server
6
+ * @param {SocketServerOptions} options - Configuration options for SocketServer.
7
+ * @return {Object} WebSocket server instance.
8
+ */
9
+ function SocketServer(options = {}) {
10
+ const server = http.createServer();
11
+ BaseSocketServer.call(this, server, options);
12
+ server.listen(this.port, () => console.log(`RedWeb SocketServer listening on port ${this.port}`));
13
+ return this;
14
+ }
15
+
16
+ module.exports = SocketServer;
package/SocketServer.js DELETED
@@ -1,104 +0,0 @@
1
- const http = require('http');
2
- const https = require('https');
3
- const WebSocket = require('ws');
4
- const loadSslConfig = require('./sslConfig');
5
-
6
- /**
7
- * @typedef {Object} SocketServerOptions
8
- * @property {number} [port=3000] - The port number to bind the socket server.
9
- * @property {Function} [connectionOpenCallback] - Callback function to execute once a client connects.
10
- * @property {Function} [connectionCloseCallback] - Callback function to execute once a client disconnects.
11
- * @property {Function} [messageCallback] - Callback function to execute for every message received.
12
- * @property {Object} [messageHandlers] - Object containing message handlers based on message type.
13
- * @property {Object} [ssl] - SSL configuration for SecureSocketServer.
14
- * @property {string} [ssl.key] - Path to the SSL key file.
15
- * @property {string} [ssl.cert] - Path to the SSL certificate file.
16
- */
17
-
18
- const SOCKET_OPTIONS = {
19
- port: 3000,
20
- connectionOpenCallback: undefined,
21
- connectionCloseCallback: undefined,
22
- messageCallback: undefined,
23
- messageHandlers: {
24
- 'ping': function(socket, data) { socket.send(JSON.stringify({ type: 'pong' })); }
25
- },
26
- ssl: null
27
- };
28
-
29
- /**
30
- * Base Socket Server
31
- * @param {Object} server - The HTTP or HTTPS server instance.
32
- * @param {SocketServerOptions} options - Configuration options for SocketServer.
33
- */
34
- function BaseSocketServer(server, options = {}) {
35
- this.wss = new WebSocket.Server({ server });
36
- this.clients = new Map(); // Use a Map to store clients by their IP addresses
37
- Object.assign(this, { ...SOCKET_OPTIONS, ...options });
38
-
39
- this.wss.on('connection', (socket, req) => {
40
- const ip = req.socket.remoteAddress;
41
- console.log(`New client connected: ${ip}`);
42
- this.clients.set(ip, socket);
43
-
44
- if (this.connectionOpenCallback) this.connectionOpenCallback(socket);
45
-
46
- socket.on('message', (message) => {
47
- try {
48
- console.info(`Received message from ${ip}: ${message}`);
49
- if (this.messageCallback) this.messageCallback(socket, message);
50
- const parsedMessage = JSON.parse(message);
51
- const { type, data } = parsedMessage;
52
-
53
- if (this.messageHandlers[type]) {
54
- this.messageHandlers[type](socket, data);
55
- }
56
- } catch (error) {
57
- console.error(`Error handling message from ${ip}:`, error);
58
- }
59
- });
60
-
61
- socket.on('close', () => {
62
- console.log(`Client disconnected: ${ip}`);
63
- this.clients.delete(ip);
64
- if (this.connectionCloseCallback) this.connectionCloseCallback(socket);
65
- });
66
-
67
- socket.on('error', (error) => {
68
- console.error(`Socket error from ${ip}:`, error);
69
- });
70
- });
71
-
72
- return this;
73
- }
74
-
75
- /**
76
- * WebSocket Server
77
- * @param {SocketServerOptions} options - Configuration options for SocketServer.
78
- * @return {Object} WebSocket server instance.
79
- */
80
- function SocketServer(options = {}) {
81
- const server = http.createServer();
82
- BaseSocketServer.call(this, server, options);
83
- server.listen(this.port, () => console.log(`RedWeb SocketServer listening on port ${this.port}`));
84
- return this;
85
- }
86
-
87
- /**
88
- * Secure WebSocket Server
89
- * @param {SocketServerOptions} options - Configuration options for SecureSocketServer.
90
- * @return {Object} WebSocket server instance.
91
- */
92
- function SecureSocketServer(options = {}) {
93
- const sslOptions = loadSslConfig(options.ssl);
94
- const server = https.createServer(sslOptions);
95
- BaseSocketServer.call(this, server, options);
96
- server.listen(this.port, () => console.log(`RedWeb SecureSocketServer listening on port ${this.port}`));
97
- return this;
98
- }
99
-
100
- module.exports = {
101
- SocketServer,
102
- SecureSocketServer,
103
- SOCKET_OPTIONS
104
- };
File without changes