redweb 0.1.3 → 0.1.5
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/HttpServer.js +88 -0
- package/SocketServer.js +98 -0
- package/package.json +5 -2
- package/sslConfig.js +20 -0
package/HttpServer.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
const express = require('express');
|
|
2
|
+
const bodyParser = require('body-parser');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const https = require('https');
|
|
5
|
+
const loadSslConfig = require('./sslConfig');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @typedef {'json' | 'urlencoded'} RedWebEncoding
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* RedWeb options object.
|
|
13
|
+
* @typedef {Object} RedWebOptions
|
|
14
|
+
* @property {number} [port=80] - The port number to bind the server.
|
|
15
|
+
* @property {string} [bind='0.0.0.0'] - The bind address for the server.
|
|
16
|
+
* @property {string[]} [publicPaths=['./public']] - An array of paths to serve static files from.
|
|
17
|
+
* @property {Array<{serviceName: string, method: string, function: Function}>} [services=[]] - An array of services with their endpoints and handlers.
|
|
18
|
+
* @property {Function} [listenCallback] - Callback function to execute once the server starts listening.
|
|
19
|
+
* @property {RedWebEncoding} [encoding='json'] - The encoding type for the request bodies ('json' or 'urlencoded').
|
|
20
|
+
* @property {Object} [ssl] - SSL configuration for HTTPS server.
|
|
21
|
+
* @property {string} [ssl.key] - Path to the SSL key file.
|
|
22
|
+
* @property {string} [ssl.cert] - Path to the SSL certificate file.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
const ENCODINGS = { json: 'json', urlencoded: 'urlencoded' };
|
|
26
|
+
const METHODS = { POST: 'post', GET: 'get' };
|
|
27
|
+
|
|
28
|
+
const HTTP_OPTIONS = {
|
|
29
|
+
port: 80,
|
|
30
|
+
bind: '0.0.0.0',
|
|
31
|
+
publicPaths: ['./public'],
|
|
32
|
+
services: [],
|
|
33
|
+
listenCallback: undefined,
|
|
34
|
+
encoding: ENCODINGS.json,
|
|
35
|
+
ssl: null
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Base HTTP Server
|
|
40
|
+
* @param {RedWebOptions} options - Configuration options for RedWeb.
|
|
41
|
+
* @return {Object} Express application instance.
|
|
42
|
+
*/
|
|
43
|
+
function BaseHttpServer(options = {}) {
|
|
44
|
+
this.options = { ...HTTP_OPTIONS, ...options };
|
|
45
|
+
this.app = express();
|
|
46
|
+
Object.assign(this, this.options);
|
|
47
|
+
|
|
48
|
+
// Middleware to parse request bodies based on the specified encoding
|
|
49
|
+
if (this.encoding === ENCODINGS.json) {
|
|
50
|
+
this.app.use(bodyParser.json());
|
|
51
|
+
} else if (this.encoding === ENCODINGS.urlencoded) {
|
|
52
|
+
this.app.use(bodyParser.urlencoded({ extended: true }));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
this.services.forEach(service => this.app[service.method](service.serviceName, service.function));
|
|
56
|
+
this.publicPaths.forEach(public_path => this.app.use(express.static(path.join(process.cwd(), public_path))));
|
|
57
|
+
}
|
|
58
|
+
|
|
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
|
+
module.exports = {
|
|
83
|
+
HttpServer,
|
|
84
|
+
HttpsServer,
|
|
85
|
+
ENCODINGS,
|
|
86
|
+
METHODS,
|
|
87
|
+
HTTP_OPTIONS
|
|
88
|
+
};
|
package/SocketServer.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
const http = require('http');
|
|
2
|
+
const https = require('https');
|
|
3
|
+
const socketIo = require('socket.io');
|
|
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': (socket) => socket.send('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
|
+
* @return {Object} Socket.IO server instance.
|
|
34
|
+
*/
|
|
35
|
+
function BaseSocketServer(server, options = {}) {
|
|
36
|
+
this.io = socketIo(server);
|
|
37
|
+
Object.assign(this, { ...SOCKET_OPTIONS, ...options });
|
|
38
|
+
|
|
39
|
+
this.io.on('connection', socket => {
|
|
40
|
+
console.log('New client connected');
|
|
41
|
+
if (this.connectionOpenCallback) this.connectionOpenCallback(socket);
|
|
42
|
+
|
|
43
|
+
socket.on('message', (message) => {
|
|
44
|
+
try {
|
|
45
|
+
const parsedMessage = JSON.parse(message);
|
|
46
|
+
const { type, data } = parsedMessage;
|
|
47
|
+
|
|
48
|
+
if (this.messageHandlers[type]) {
|
|
49
|
+
this.messageHandlers[type](socket, data);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (this.messageCallback) this.messageCallback(socket, message);
|
|
53
|
+
} catch (error) {
|
|
54
|
+
console.error('Error handling message:', error);
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
socket.on('disconnect', () => {
|
|
59
|
+
console.log('Client disconnected');
|
|
60
|
+
if (this.connectionCloseCallback) this.connectionCloseCallback(socket);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
socket.on('error', (error) => {
|
|
64
|
+
console.error('Socket error:', error);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* WebSocket Server
|
|
71
|
+
* @param {SocketServerOptions} options - Configuration options for SocketServer.
|
|
72
|
+
* @return {Object} Socket.IO server instance.
|
|
73
|
+
*/
|
|
74
|
+
function SocketServer(options = {}) {
|
|
75
|
+
const server = http.createServer();
|
|
76
|
+
BaseSocketServer.call(this, server, options);
|
|
77
|
+
server.listen(this.port, () => console.log(`RedWeb SocketServer listening on port ${this.port}`));
|
|
78
|
+
return this.io;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Secure WebSocket Server
|
|
83
|
+
* @param {SocketServerOptions} options - Configuration options for SecureSocketServer.
|
|
84
|
+
* @return {Object} Socket.IO server instance.
|
|
85
|
+
*/
|
|
86
|
+
function SecureSocketServer(options = {}) {
|
|
87
|
+
const sslOptions = loadSslConfig(options.ssl);
|
|
88
|
+
const server = https.createServer(sslOptions);
|
|
89
|
+
BaseSocketServer.call(this, server, options);
|
|
90
|
+
server.listen(this.port, () => console.log(`RedWeb SecureSocketServer listening on port ${this.port}`));
|
|
91
|
+
return this.io;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
module.exports = {
|
|
95
|
+
SocketServer,
|
|
96
|
+
SecureSocketServer,
|
|
97
|
+
SOCKET_OPTIONS
|
|
98
|
+
};
|
package/package.json
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "redweb",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
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"
|
|
10
|
+
"index.js",
|
|
11
|
+
"HttpServer.js",
|
|
12
|
+
"SocketServer.js",
|
|
13
|
+
"sslConfig.js"
|
|
11
14
|
],
|
|
12
15
|
"keywords": [],
|
|
13
16
|
"author": "",
|
package/sslConfig.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Load SSL configuration
|
|
5
|
+
* @param {Object} sslOptions - The SSL options object containing the paths to key and cert files.
|
|
6
|
+
* @param {string} sslOptions.key - Path to the SSL key file.
|
|
7
|
+
* @param {string} sslOptions.cert - Path to the SSL certificate file.
|
|
8
|
+
* @return {Object} - The loaded SSL options containing key and cert.
|
|
9
|
+
*/
|
|
10
|
+
function loadSslConfig(sslOptions) {
|
|
11
|
+
if (!sslOptions || !sslOptions.key || !sslOptions.cert) {
|
|
12
|
+
throw new Error('SSL key and certificate paths must be provided');
|
|
13
|
+
}
|
|
14
|
+
return {
|
|
15
|
+
key: fs.readFileSync(sslOptions.key),
|
|
16
|
+
cert: fs.readFileSync(sslOptions.cert)
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
module.exports = loadSslConfig;
|