redweb 0.4.5 → 0.4.7
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/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
declare module 'redweb' {
|
|
2
|
-
import {
|
|
2
|
+
import { Request, Response } from 'express';
|
|
3
3
|
import { Server as HTTPServer } from 'http';
|
|
4
4
|
import { Server as HTTPSServer } from 'https';
|
|
5
5
|
import WebSocket from 'ws';
|
|
@@ -15,7 +15,7 @@ declare module 'redweb' {
|
|
|
15
15
|
export interface Service {
|
|
16
16
|
serviceName: string;
|
|
17
17
|
method: 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options' | 'head';
|
|
18
|
-
function: (req:
|
|
18
|
+
function: (req: Request, res: Response) => void;
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
/**
|
|
@@ -97,7 +97,7 @@ declare module 'redweb' {
|
|
|
97
97
|
|
|
98
98
|
constructor(server: HTTPServer | HTTPSServer, options?: SocketServerOptions);
|
|
99
99
|
|
|
100
|
-
handleConnection(socket: WebSocket, req:
|
|
100
|
+
handleConnection(socket: WebSocket, req: Request): void;
|
|
101
101
|
handleMessage(socket: WebSocket, message: string, ip: string): void;
|
|
102
102
|
handleClose(socket: WebSocket, ip: string): void;
|
|
103
103
|
handleError(socket: WebSocket, error: Error, ip: string): void;
|
package/package.json
CHANGED
|
@@ -18,6 +18,7 @@ const path = require('path');
|
|
|
18
18
|
* @property {Object} [ssl] - SSL configuration for HTTPS server.
|
|
19
19
|
* @property {string} [ssl.key] - Path to the SSL key file.
|
|
20
20
|
* @property {string} [ssl.cert] - Path to the SSL certificate file.
|
|
21
|
+
* @property {import('express').Application} [server] - Whether to automatically start listening.
|
|
21
22
|
*/
|
|
22
23
|
|
|
23
24
|
const ENCODINGS = { json: 'json', urlencoded: 'urlencoded' };
|
|
@@ -28,7 +29,8 @@ const HTTP_OPTIONS = {
|
|
|
28
29
|
services: [],
|
|
29
30
|
listenCallback: undefined,
|
|
30
31
|
encoding: ENCODINGS.json,
|
|
31
|
-
ssl: null
|
|
32
|
+
ssl: null,
|
|
33
|
+
server: undefined
|
|
32
34
|
};
|
|
33
35
|
|
|
34
36
|
/**
|
|
@@ -38,7 +40,7 @@ const HTTP_OPTIONS = {
|
|
|
38
40
|
*/
|
|
39
41
|
function BaseHttpServer(options = {}) {
|
|
40
42
|
this.options = { ...HTTP_OPTIONS, ...options };
|
|
41
|
-
this.app = express();
|
|
43
|
+
this.app = express() || this.options.server;
|
|
42
44
|
Object.assign(this, this.options);
|
|
43
45
|
|
|
44
46
|
// Middleware to parse request bodies based on the specified encoding
|
|
@@ -50,6 +52,7 @@ function BaseHttpServer(options = {}) {
|
|
|
50
52
|
|
|
51
53
|
this.services.forEach(service => this.app[service.method](service.serviceName, service.function));
|
|
52
54
|
this.publicPaths.forEach(public_path => this.app.use(express.static(path.join(process.cwd(), public_path))));
|
|
55
|
+
return this;
|
|
53
56
|
}
|
|
54
57
|
|
|
55
58
|
module.exports = {
|
package/src/http/HttpServer.js
CHANGED
|
@@ -7,7 +7,7 @@ const { BaseHttpServer } = require('./BaseHttpServer');
|
|
|
7
7
|
*/
|
|
8
8
|
function HttpServer(options = {}) {
|
|
9
9
|
BaseHttpServer.call(this, options);
|
|
10
|
-
this.app.listen(this.port, this.listenCallback ? this.listenCallback : () => console.log(`RedWeb HttpServer listening on port ${this.port}`));
|
|
10
|
+
this.server = this.app.listen(this.port, this.listenCallback ? this.listenCallback : () => console.log(`RedWeb HttpServer listening on port ${this.port}`));
|
|
11
11
|
return this;
|
|
12
12
|
}
|
|
13
13
|
|
|
@@ -2,6 +2,7 @@ const WebSocket = require('ws');
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* @typedef {Object} SocketServerOptions
|
|
5
|
+
* @property {any} [server] - The existing server instance.
|
|
5
6
|
* @property {number} [port=3000] - The port number to bind the socket server.
|
|
6
7
|
* @property {Function} [connectionOpenCallback] - Callback function to execute once a client connects.
|
|
7
8
|
* @property {Function} [connectionCloseCallback] - Callback function to execute once a client disconnects.
|
|
@@ -35,7 +36,6 @@ function broadcast(socketServer, message) {
|
|
|
35
36
|
class BaseSocketServer {
|
|
36
37
|
/**
|
|
37
38
|
* Base Socket Server
|
|
38
|
-
* @param {Object} server - The HTTP or HTTPS server instance.
|
|
39
39
|
* @param {SocketServerOptions} options - Configuration options for SocketServer.
|
|
40
40
|
*/
|
|
41
41
|
constructor(server, options = {}) {
|
package/src/ws/SocketServer.js
CHANGED
|
@@ -8,7 +8,7 @@ const { BaseSocketServer } = require('./BaseSocketServer');
|
|
|
8
8
|
*/
|
|
9
9
|
class SocketServer extends BaseSocketServer {
|
|
10
10
|
constructor(options = {}) {
|
|
11
|
-
const server = http.createServer();
|
|
11
|
+
const server = options?.server || http.createServer();
|
|
12
12
|
super(server, options);
|
|
13
13
|
server.listen(this.port, () => console.log(`RedWeb SocketServer listening on port ${this.port}`));
|
|
14
14
|
return this;
|