zyket 1.2.11 → 1.2.12
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/.github/workflows/publish.yml +37 -37
- package/README.md +279 -279
- package/bin/cli.js +201 -201
- package/index.js +32 -32
- package/package.json +54 -50
- package/src/Middleware.js +3 -3
- package/src/extensions/Extension.js +10 -10
- package/src/extensions/bullboard/index.js +38 -38
- package/src/extensions/interactive-storage/index.js +162 -162
- package/src/extensions/interactive-storage/middlewares/MulterMiddleware.js +31 -31
- package/src/extensions/interactive-storage/routes/browse.js +31 -31
- package/src/extensions/interactive-storage/routes/create-folder.js +37 -37
- package/src/extensions/interactive-storage/routes/delete-folder.js +57 -57
- package/src/extensions/interactive-storage/routes/delete.js +41 -41
- package/src/extensions/interactive-storage/routes/download.js +47 -47
- package/src/extensions/interactive-storage/routes/info.js +37 -37
- package/src/extensions/interactive-storage/routes/upload.js +46 -46
- package/src/kernel/HTTPServer.js +31 -31
- package/src/kernel/index.js +78 -78
- package/src/services/Service.js +10 -10
- package/src/services/auth/auth.js +7 -7
- package/src/services/auth/index.js +199 -199
- package/src/services/bullmq/Worker.js +7 -7
- package/src/services/bullmq/index.js +92 -92
- package/src/services/cache/index.js +96 -96
- package/src/services/database/index.js +127 -127
- package/src/services/events/Event.js +6 -6
- package/src/services/events/index.js +59 -59
- package/src/services/express/Express.js +248 -248
- package/src/services/express/Middleware.js +7 -7
- package/src/services/express/RedirectResponse.js +8 -8
- package/src/services/express/Route.js +6 -6
- package/src/services/express/index.js +4 -4
- package/src/services/index.js +29 -29
- package/src/services/logger/index.js +80 -80
- package/src/services/s3/index.js +82 -82
- package/src/services/scheduler/Schedule.js +6 -6
- package/src/services/scheduler/index.js +47 -47
- package/src/services/socketio/Guard.js +10 -10
- package/src/services/socketio/Handler.js +10 -10
- package/src/services/socketio/SocketIO.js +159 -132
- package/src/services/socketio/index.js +4 -4
- package/src/services/template-manager/index.js +73 -73
- package/src/templates/default/config/cors.js +4 -4
- package/src/templates/default/config/swagger.js +15 -15
- package/src/templates/default/frontend/main.jsx +15 -15
- package/src/templates/default/frontend/src/hooks/useAuth.jsx +51 -51
- package/src/templates/default/frontend/src/hooks/useLayout.jsx +18 -18
- package/src/templates/default/frontend/src/layouts/auth/index.jsx +45 -45
- package/src/templates/default/frontend/src/layouts/auth/routes.js +17 -17
- package/src/templates/default/frontend/src/layouts/landing/index.jsx +61 -61
- package/src/templates/default/frontend/src/layouts/landing/routes.js +10 -10
- package/src/templates/default/frontend/src/layouts/panel/index.jsx +115 -115
- package/src/templates/default/frontend/src/layouts/panel/routes.js +10 -10
- package/src/templates/default/frontend/src/middlewares/LoggedMiddleware.jsx +21 -21
- package/src/templates/default/frontend/src/middlewares/NotLoggedMiddleware.jsx +14 -14
- package/src/templates/default/frontend/src/store/index.jsx +5 -5
- package/src/templates/default/frontend/src/store/storeAuth.jsx +14 -14
- package/src/templates/default/frontend/src/views/auth/index.jsx +4 -4
- package/src/templates/default/frontend/src/views/auth/register/index.jsx +4 -4
- package/src/templates/default/frontend/src/views/landing/index.jsx +4 -4
- package/src/templates/default/frontend/src/views/panel/dashboard/index.jsx +4 -4
- package/src/templates/default/frontend/styles.css +1 -1
- package/src/templates/default/src/guards/default.js +6 -6
- package/src/templates/default/src/handlers/connection.js +6 -6
- package/src/templates/default/src/handlers/message.js +8 -8
- package/src/templates/default/src/middlewares/default.js +7 -7
- package/src/templates/default/src/routes/[test]/message.js +26 -26
- package/src/templates/default/src/routes/index.js +22 -22
- package/src/templates/default/src/services/auth/auth.js +7 -7
- package/src/templates/default/src/services/auth/index.js +32 -32
- package/src/utils/EnvManager.js +65 -65
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
name: Publish Package to npmjs
|
|
2
|
-
on:
|
|
3
|
-
push:
|
|
4
|
-
branches:
|
|
5
|
-
- main
|
|
6
|
-
jobs:
|
|
7
|
-
build:
|
|
8
|
-
runs-on: ubuntu-latest
|
|
9
|
-
permissions:
|
|
10
|
-
contents: write
|
|
11
|
-
packages: write
|
|
12
|
-
attestations: write
|
|
13
|
-
id-token: write
|
|
14
|
-
steps:
|
|
15
|
-
- name: Checkout code
|
|
16
|
-
uses: actions/checkout@v2
|
|
17
|
-
|
|
18
|
-
- name: Setup Node.js
|
|
19
|
-
uses: actions/setup-node@v4
|
|
20
|
-
with:
|
|
21
|
-
node-version: '20.x'
|
|
22
|
-
registry-url: 'https://registry.npmjs.org'
|
|
23
|
-
|
|
24
|
-
- name: Install dependencies
|
|
25
|
-
run: npm install
|
|
26
|
-
|
|
27
|
-
- name: 'Bump version'
|
|
28
|
-
id: version-bump
|
|
29
|
-
uses: 'phips28/gh-action-bump-version@master'
|
|
30
|
-
env:
|
|
31
|
-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
32
|
-
with:
|
|
33
|
-
commit-message: 'CI: bumps version to {{version}} [skip ci]'
|
|
34
|
-
|
|
35
|
-
- name: Publish to npm
|
|
36
|
-
run: npm publish
|
|
37
|
-
env:
|
|
1
|
+
name: Publish Package to npmjs
|
|
2
|
+
on:
|
|
3
|
+
push:
|
|
4
|
+
branches:
|
|
5
|
+
- main
|
|
6
|
+
jobs:
|
|
7
|
+
build:
|
|
8
|
+
runs-on: ubuntu-latest
|
|
9
|
+
permissions:
|
|
10
|
+
contents: write
|
|
11
|
+
packages: write
|
|
12
|
+
attestations: write
|
|
13
|
+
id-token: write
|
|
14
|
+
steps:
|
|
15
|
+
- name: Checkout code
|
|
16
|
+
uses: actions/checkout@v2
|
|
17
|
+
|
|
18
|
+
- name: Setup Node.js
|
|
19
|
+
uses: actions/setup-node@v4
|
|
20
|
+
with:
|
|
21
|
+
node-version: '20.x'
|
|
22
|
+
registry-url: 'https://registry.npmjs.org'
|
|
23
|
+
|
|
24
|
+
- name: Install dependencies
|
|
25
|
+
run: npm install
|
|
26
|
+
|
|
27
|
+
- name: 'Bump version'
|
|
28
|
+
id: version-bump
|
|
29
|
+
uses: 'phips28/gh-action-bump-version@master'
|
|
30
|
+
env:
|
|
31
|
+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
32
|
+
with:
|
|
33
|
+
commit-message: 'CI: bumps version to {{version}} [skip ci]'
|
|
34
|
+
|
|
35
|
+
- name: Publish to npm
|
|
36
|
+
run: npm publish
|
|
37
|
+
env:
|
|
38
38
|
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
package/README.md
CHANGED
|
@@ -1,279 +1,279 @@
|
|
|
1
|
-
# Zyket
|
|
2
|
-
|
|
3
|
-
Zyket is a Node.js framework designed to simplify the development of real-time applications with Socket.IO and Express. Inspired by the structured approach of frameworks like Symfony, Zyket provides a robust, service-oriented architecture for building scalable and maintainable server-side applications.
|
|
4
|
-
|
|
5
|
-
Upon initial boot, Zyket automatically scaffolds a default project structure, including handlers, routes, and configuration files, allowing you to get started immediately.
|
|
6
|
-
|
|
7
|
-
## Getting Started
|
|
8
|
-
|
|
9
|
-
### Quick Start (Recommended)
|
|
10
|
-
|
|
11
|
-
The easiest way to get started with Zyket is to use the CLI initialization command:
|
|
12
|
-
|
|
13
|
-
```bash
|
|
14
|
-
# Create a new directory for your project
|
|
15
|
-
mkdir my-zyket-app
|
|
16
|
-
cd my-zyket-app
|
|
17
|
-
|
|
18
|
-
# Install zyket
|
|
19
|
-
npm install zyket
|
|
20
|
-
|
|
21
|
-
# Initialize the project (one command!)
|
|
22
|
-
npx zyket init
|
|
23
|
-
|
|
24
|
-
# Start your application
|
|
25
|
-
npm run dev
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
The `init` command will:
|
|
29
|
-
- Create an `index.js` file with the kernel boilerplate
|
|
30
|
-
- Generate a `.env` file with sensible defaults
|
|
31
|
-
- Create a `package.json` if one doesn't exist
|
|
32
|
-
- Set up your project ready to run
|
|
33
|
-
|
|
34
|
-
You can also run `npx zyket` without arguments for an interactive menu with more options.
|
|
35
|
-
|
|
36
|
-
### Manual Setup (Alternative)
|
|
37
|
-
|
|
38
|
-
If you prefer to set up manually, install Zyket in your project:
|
|
39
|
-
|
|
40
|
-
```bash
|
|
41
|
-
npm i zyket
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
Then, create an `index.js` file and boot the Zyket Kernel:
|
|
45
|
-
|
|
46
|
-
```javascript
|
|
47
|
-
// index.js
|
|
48
|
-
const { Kernel } = require("zyket");
|
|
49
|
-
|
|
50
|
-
// Instantiate the kernel
|
|
51
|
-
const kernel = new Kernel();
|
|
52
|
-
|
|
53
|
-
// Boot the kernel to start all services
|
|
54
|
-
kernel.boot().then(() => {
|
|
55
|
-
console.log("Kernel booted successfully!");
|
|
56
|
-
});
|
|
57
|
-
```
|
|
58
|
-
|
|
59
|
-
When you run this for the first time, Zyket will create a default `.env` file and a `src` directory containing boilerplate for routes, handlers, guards, and middlewares.
|
|
60
|
-
|
|
61
|
-
## Core Concepts
|
|
62
|
-
|
|
63
|
-
Zyket is built around a few key architectural concepts:
|
|
64
|
-
|
|
65
|
-
* **Socket.IO Handlers & Guards**: For managing real-time WebSocket events and their authorization.
|
|
66
|
-
* **Express Routes & Middlewares**: For handling traditional RESTful API endpoints.
|
|
67
|
-
* **Services**: Reusable components that are managed by a dependency injection container.
|
|
68
|
-
* **CLI**: A command-line tool to scaffold features from templates.
|
|
69
|
-
|
|
70
|
-
### Socket.IO Development
|
|
71
|
-
|
|
72
|
-
#### Handlers
|
|
73
|
-
|
|
74
|
-
Handlers are classes that process incoming Socket.IO events. The name of the handler file (e.g., `message.js`) determines the event it listens to (`message`).
|
|
75
|
-
|
|
76
|
-
```javascript
|
|
77
|
-
// src/handlers/message.js
|
|
78
|
-
const { Handler } = require("zyket");
|
|
79
|
-
|
|
80
|
-
module.exports = class MessageHandler extends Handler {
|
|
81
|
-
// Array of guard names to execute before the handler
|
|
82
|
-
guards = ["default"];
|
|
83
|
-
|
|
84
|
-
async handle({ container, socket, data, io }) {
|
|
85
|
-
const logger = container.get("logger");
|
|
86
|
-
logger.info(`Received message: ${JSON.stringify(data)}`);
|
|
87
|
-
socket.emit("response", { received: data });
|
|
88
|
-
}
|
|
89
|
-
};
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
#### Guards
|
|
93
|
-
|
|
94
|
-
Guards are used to protect Socket.IO handlers or the initial connection. They run before the handler's `handle` method and are ideal for authorization logic.
|
|
95
|
-
|
|
96
|
-
```javascript
|
|
97
|
-
// src/guards/default.js
|
|
98
|
-
const { Guard } = require("zyket");
|
|
99
|
-
|
|
100
|
-
module.exports = class DefaultGuard extends Guard {
|
|
101
|
-
async handle({ container, socket, io }) {
|
|
102
|
-
container.get("logger").info(`Executing default guard for socket ${socket.id}`);
|
|
103
|
-
|
|
104
|
-
// Example: Check for an auth token. If invalid, disconnect the user.
|
|
105
|
-
// if (!socket.token) {
|
|
106
|
-
// socket.disconnect();
|
|
107
|
-
// }
|
|
108
|
-
}
|
|
109
|
-
};
|
|
110
|
-
```
|
|
111
|
-
|
|
112
|
-
### Express API Development
|
|
113
|
-
|
|
114
|
-
#### Routes
|
|
115
|
-
|
|
116
|
-
Routes handle HTTP requests. The file path maps directly to the URL endpoint. For example, `src/routes/index.js` handles requests to `/`, and `src/routes/[test]/message.js` handles requests to `/:test/message`.
|
|
117
|
-
|
|
118
|
-
```javascript
|
|
119
|
-
// src/routes/index.js
|
|
120
|
-
const { Route } = require("zyket");
|
|
121
|
-
const DefaultMiddleware = require("../middlewares/default");
|
|
122
|
-
|
|
123
|
-
module.exports = class DefaultRoute extends Route {
|
|
124
|
-
// Apply middlewares to specific HTTP methods
|
|
125
|
-
middlewares = {
|
|
126
|
-
get: [ new DefaultMiddleware() ],
|
|
127
|
-
post: [ new DefaultMiddleware() ]
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
async get({ container, request }) {
|
|
131
|
-
container.get("logger").info("Default route GET");
|
|
132
|
-
return { test: "Hello World GET!" };
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
async post({ container, request }) {
|
|
136
|
-
container.get("logger").info("Default route POST");
|
|
137
|
-
return { test: "Hello World POST!" };
|
|
138
|
-
}
|
|
139
|
-
};
|
|
140
|
-
```
|
|
141
|
-
|
|
142
|
-
#### Middlewares
|
|
143
|
-
|
|
144
|
-
Middlewares process the request before it reaches the route handler. They follow the standard Express middleware pattern.
|
|
145
|
-
|
|
146
|
-
```javascript
|
|
147
|
-
// src/middlewares/default.js
|
|
148
|
-
const { Middleware } = require("zyket");
|
|
149
|
-
|
|
150
|
-
module.exports = class DefaultMiddleware extends Middleware {
|
|
151
|
-
async handle({ container, request, response, next }) {
|
|
152
|
-
container.get("logger").info("Default Express middleware executing");
|
|
153
|
-
next(); // Pass control to the next middleware or route handler
|
|
154
|
-
}
|
|
155
|
-
};
|
|
156
|
-
```
|
|
157
|
-
|
|
158
|
-
## Services
|
|
159
|
-
|
|
160
|
-
Services are the cornerstone of Zyket's architecture, providing reusable functionality across your application. Zyket includes several default services and allows you to register your own.
|
|
161
|
-
|
|
162
|
-
### Custom Services
|
|
163
|
-
|
|
164
|
-
You can create your own services by extending the `Service` class and registering it with the Kernel.
|
|
165
|
-
|
|
166
|
-
```javascript
|
|
167
|
-
// src/services/MyCustomService.js
|
|
168
|
-
const { Service } = require("zyket");
|
|
169
|
-
|
|
170
|
-
module.exports = class MyCustomService extends Service {
|
|
171
|
-
constructor() {
|
|
172
|
-
super("my-custom-service");
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
async boot() {
|
|
176
|
-
console.log("MyCustomService has been booted!");
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
doSomething() {
|
|
180
|
-
return "Something was done.";
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
```
|
|
184
|
-
|
|
185
|
-
Register the service in your main `index.js` file:
|
|
186
|
-
|
|
187
|
-
```javascript
|
|
188
|
-
// index.js
|
|
189
|
-
const { Kernel } = require("zyket");
|
|
190
|
-
const MyCustomService = require("./src/services/MyCustomService");
|
|
191
|
-
|
|
192
|
-
const kernel = new Kernel({
|
|
193
|
-
services: [
|
|
194
|
-
// [name, class, [constructor_args]]
|
|
195
|
-
["my-service", MyCustomService, []]
|
|
196
|
-
]
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
kernel.boot();
|
|
200
|
-
```
|
|
201
|
-
Services are reusable components specified in the kernel configuration. Each service must include a boot() function that is executed when the kernel starts.
|
|
202
|
-
|
|
203
|
-
```javascript
|
|
204
|
-
module.exports = class LoggerService {
|
|
205
|
-
this.#container;
|
|
206
|
-
|
|
207
|
-
boot(container, enableLogging = true) {
|
|
208
|
-
this.#container = container;
|
|
209
|
-
console.log("LoggerService Booted");
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
info(message) {
|
|
213
|
-
if(!enableLogging) return;
|
|
214
|
-
console.log(`[INFO]: ${message}`);
|
|
215
|
-
}
|
|
216
|
-
};
|
|
217
|
-
```
|
|
218
|
-
|
|
219
|
-
Then, when booting the kernel, specify the service:
|
|
220
|
-
|
|
221
|
-
```javascript
|
|
222
|
-
const { Kernel } = require("zyket");
|
|
223
|
-
const LoggerService = require("./LoggerService");
|
|
224
|
-
|
|
225
|
-
const kernel = new Kernel({
|
|
226
|
-
services: [["logger", LoggerService, ['@service_container', true]],
|
|
227
|
-
});
|
|
228
|
-
|
|
229
|
-
kernel.boot().then(() => console.log(`Kernel Booted`));
|
|
230
|
-
```
|
|
231
|
-
|
|
232
|
-
## Default Services
|
|
233
|
-
Zyket includes some default services that provide essential functionality. These services can be overridden or extended if needed.
|
|
234
|
-
|
|
235
|
-
#### Cache Services
|
|
236
|
-
- **Name** `cache`
|
|
237
|
-
- **Description** Provides caching functionality using a Redis adapter.
|
|
238
|
-
- **Configuration** Add `CACHE_URL` in your `.env` file to activate caching.
|
|
239
|
-
|
|
240
|
-
#### Database Service
|
|
241
|
-
- **Name** `database`
|
|
242
|
-
- **Description** Manages database connections using a MariaDB/Sequelize adapter.
|
|
243
|
-
- **Configuration** Add `DATABASE_URL` in your `.env` file to enable the database connection.
|
|
244
|
-
|
|
245
|
-
#### S3 Service
|
|
246
|
-
- **Name** `s3`
|
|
247
|
-
- **Description** Provides S3-compatible object storage using MinIO.
|
|
248
|
-
- **Configuration** Add the following variables in your `.env` file to enable the service
|
|
249
|
-
- `S3_ENDPOINT`
|
|
250
|
-
- `S3_PORT`
|
|
251
|
-
- `S3_USE_SSL`
|
|
252
|
-
- `S3_ACCESS_KEY`
|
|
253
|
-
- `S3_SECRET_KEY`
|
|
254
|
-
|
|
255
|
-
#### Logger Service
|
|
256
|
-
- **Name** `logger`
|
|
257
|
-
- **Description** Handles logging for the application.
|
|
258
|
-
- **Configuration**
|
|
259
|
-
- Change `LOG_DIRECTORY` in `.env` file to set a custom log directory.
|
|
260
|
-
- Set `DEBUG` in `.env` file to enable or disable debug logging.
|
|
261
|
-
|
|
262
|
-
#### Socket.io Service
|
|
263
|
-
- **Name** `socketio`
|
|
264
|
-
- **Description** Manages the WebSocket server.
|
|
265
|
-
- **Configuration** Add `PORT` in your `.env` file to define the listening port for Socket.io.
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
## Contributing
|
|
269
|
-
|
|
270
|
-
We welcome contributions from the community! If you'd like to improve Zyket, feel free to:
|
|
271
|
-
|
|
272
|
-
- Report issues and suggest features on GitHub Issues
|
|
273
|
-
|
|
274
|
-
- Submit pull requests with bug fixes or enhancements
|
|
275
|
-
|
|
276
|
-
- Improve the documentation
|
|
277
|
-
|
|
278
|
-
Let's build a better framework together! 🚀
|
|
279
|
-
|
|
1
|
+
# Zyket
|
|
2
|
+
|
|
3
|
+
Zyket is a Node.js framework designed to simplify the development of real-time applications with Socket.IO and Express. Inspired by the structured approach of frameworks like Symfony, Zyket provides a robust, service-oriented architecture for building scalable and maintainable server-side applications.
|
|
4
|
+
|
|
5
|
+
Upon initial boot, Zyket automatically scaffolds a default project structure, including handlers, routes, and configuration files, allowing you to get started immediately.
|
|
6
|
+
|
|
7
|
+
## Getting Started
|
|
8
|
+
|
|
9
|
+
### Quick Start (Recommended)
|
|
10
|
+
|
|
11
|
+
The easiest way to get started with Zyket is to use the CLI initialization command:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
# Create a new directory for your project
|
|
15
|
+
mkdir my-zyket-app
|
|
16
|
+
cd my-zyket-app
|
|
17
|
+
|
|
18
|
+
# Install zyket
|
|
19
|
+
npm install zyket
|
|
20
|
+
|
|
21
|
+
# Initialize the project (one command!)
|
|
22
|
+
npx zyket init
|
|
23
|
+
|
|
24
|
+
# Start your application
|
|
25
|
+
npm run dev
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
The `init` command will:
|
|
29
|
+
- Create an `index.js` file with the kernel boilerplate
|
|
30
|
+
- Generate a `.env` file with sensible defaults
|
|
31
|
+
- Create a `package.json` if one doesn't exist
|
|
32
|
+
- Set up your project ready to run
|
|
33
|
+
|
|
34
|
+
You can also run `npx zyket` without arguments for an interactive menu with more options.
|
|
35
|
+
|
|
36
|
+
### Manual Setup (Alternative)
|
|
37
|
+
|
|
38
|
+
If you prefer to set up manually, install Zyket in your project:
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
npm i zyket
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Then, create an `index.js` file and boot the Zyket Kernel:
|
|
45
|
+
|
|
46
|
+
```javascript
|
|
47
|
+
// index.js
|
|
48
|
+
const { Kernel } = require("zyket");
|
|
49
|
+
|
|
50
|
+
// Instantiate the kernel
|
|
51
|
+
const kernel = new Kernel();
|
|
52
|
+
|
|
53
|
+
// Boot the kernel to start all services
|
|
54
|
+
kernel.boot().then(() => {
|
|
55
|
+
console.log("Kernel booted successfully!");
|
|
56
|
+
});
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
When you run this for the first time, Zyket will create a default `.env` file and a `src` directory containing boilerplate for routes, handlers, guards, and middlewares.
|
|
60
|
+
|
|
61
|
+
## Core Concepts
|
|
62
|
+
|
|
63
|
+
Zyket is built around a few key architectural concepts:
|
|
64
|
+
|
|
65
|
+
* **Socket.IO Handlers & Guards**: For managing real-time WebSocket events and their authorization.
|
|
66
|
+
* **Express Routes & Middlewares**: For handling traditional RESTful API endpoints.
|
|
67
|
+
* **Services**: Reusable components that are managed by a dependency injection container.
|
|
68
|
+
* **CLI**: A command-line tool to scaffold features from templates.
|
|
69
|
+
|
|
70
|
+
### Socket.IO Development
|
|
71
|
+
|
|
72
|
+
#### Handlers
|
|
73
|
+
|
|
74
|
+
Handlers are classes that process incoming Socket.IO events. The name of the handler file (e.g., `message.js`) determines the event it listens to (`message`).
|
|
75
|
+
|
|
76
|
+
```javascript
|
|
77
|
+
// src/handlers/message.js
|
|
78
|
+
const { Handler } = require("zyket");
|
|
79
|
+
|
|
80
|
+
module.exports = class MessageHandler extends Handler {
|
|
81
|
+
// Array of guard names to execute before the handler
|
|
82
|
+
guards = ["default"];
|
|
83
|
+
|
|
84
|
+
async handle({ container, socket, data, io }) {
|
|
85
|
+
const logger = container.get("logger");
|
|
86
|
+
logger.info(`Received message: ${JSON.stringify(data)}`);
|
|
87
|
+
socket.emit("response", { received: data });
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
#### Guards
|
|
93
|
+
|
|
94
|
+
Guards are used to protect Socket.IO handlers or the initial connection. They run before the handler's `handle` method and are ideal for authorization logic.
|
|
95
|
+
|
|
96
|
+
```javascript
|
|
97
|
+
// src/guards/default.js
|
|
98
|
+
const { Guard } = require("zyket");
|
|
99
|
+
|
|
100
|
+
module.exports = class DefaultGuard extends Guard {
|
|
101
|
+
async handle({ container, socket, io }) {
|
|
102
|
+
container.get("logger").info(`Executing default guard for socket ${socket.id}`);
|
|
103
|
+
|
|
104
|
+
// Example: Check for an auth token. If invalid, disconnect the user.
|
|
105
|
+
// if (!socket.token) {
|
|
106
|
+
// socket.disconnect();
|
|
107
|
+
// }
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### Express API Development
|
|
113
|
+
|
|
114
|
+
#### Routes
|
|
115
|
+
|
|
116
|
+
Routes handle HTTP requests. The file path maps directly to the URL endpoint. For example, `src/routes/index.js` handles requests to `/`, and `src/routes/[test]/message.js` handles requests to `/:test/message`.
|
|
117
|
+
|
|
118
|
+
```javascript
|
|
119
|
+
// src/routes/index.js
|
|
120
|
+
const { Route } = require("zyket");
|
|
121
|
+
const DefaultMiddleware = require("../middlewares/default");
|
|
122
|
+
|
|
123
|
+
module.exports = class DefaultRoute extends Route {
|
|
124
|
+
// Apply middlewares to specific HTTP methods
|
|
125
|
+
middlewares = {
|
|
126
|
+
get: [ new DefaultMiddleware() ],
|
|
127
|
+
post: [ new DefaultMiddleware() ]
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async get({ container, request }) {
|
|
131
|
+
container.get("logger").info("Default route GET");
|
|
132
|
+
return { test: "Hello World GET!" };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
async post({ container, request }) {
|
|
136
|
+
container.get("logger").info("Default route POST");
|
|
137
|
+
return { test: "Hello World POST!" };
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
#### Middlewares
|
|
143
|
+
|
|
144
|
+
Middlewares process the request before it reaches the route handler. They follow the standard Express middleware pattern.
|
|
145
|
+
|
|
146
|
+
```javascript
|
|
147
|
+
// src/middlewares/default.js
|
|
148
|
+
const { Middleware } = require("zyket");
|
|
149
|
+
|
|
150
|
+
module.exports = class DefaultMiddleware extends Middleware {
|
|
151
|
+
async handle({ container, request, response, next }) {
|
|
152
|
+
container.get("logger").info("Default Express middleware executing");
|
|
153
|
+
next(); // Pass control to the next middleware or route handler
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## Services
|
|
159
|
+
|
|
160
|
+
Services are the cornerstone of Zyket's architecture, providing reusable functionality across your application. Zyket includes several default services and allows you to register your own.
|
|
161
|
+
|
|
162
|
+
### Custom Services
|
|
163
|
+
|
|
164
|
+
You can create your own services by extending the `Service` class and registering it with the Kernel.
|
|
165
|
+
|
|
166
|
+
```javascript
|
|
167
|
+
// src/services/MyCustomService.js
|
|
168
|
+
const { Service } = require("zyket");
|
|
169
|
+
|
|
170
|
+
module.exports = class MyCustomService extends Service {
|
|
171
|
+
constructor() {
|
|
172
|
+
super("my-custom-service");
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
async boot() {
|
|
176
|
+
console.log("MyCustomService has been booted!");
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
doSomething() {
|
|
180
|
+
return "Something was done.";
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
Register the service in your main `index.js` file:
|
|
186
|
+
|
|
187
|
+
```javascript
|
|
188
|
+
// index.js
|
|
189
|
+
const { Kernel } = require("zyket");
|
|
190
|
+
const MyCustomService = require("./src/services/MyCustomService");
|
|
191
|
+
|
|
192
|
+
const kernel = new Kernel({
|
|
193
|
+
services: [
|
|
194
|
+
// [name, class, [constructor_args]]
|
|
195
|
+
["my-service", MyCustomService, []]
|
|
196
|
+
]
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
kernel.boot();
|
|
200
|
+
```
|
|
201
|
+
Services are reusable components specified in the kernel configuration. Each service must include a boot() function that is executed when the kernel starts.
|
|
202
|
+
|
|
203
|
+
```javascript
|
|
204
|
+
module.exports = class LoggerService {
|
|
205
|
+
this.#container;
|
|
206
|
+
|
|
207
|
+
boot(container, enableLogging = true) {
|
|
208
|
+
this.#container = container;
|
|
209
|
+
console.log("LoggerService Booted");
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
info(message) {
|
|
213
|
+
if(!enableLogging) return;
|
|
214
|
+
console.log(`[INFO]: ${message}`);
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
Then, when booting the kernel, specify the service:
|
|
220
|
+
|
|
221
|
+
```javascript
|
|
222
|
+
const { Kernel } = require("zyket");
|
|
223
|
+
const LoggerService = require("./LoggerService");
|
|
224
|
+
|
|
225
|
+
const kernel = new Kernel({
|
|
226
|
+
services: [["logger", LoggerService, ['@service_container', true]],
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
kernel.boot().then(() => console.log(`Kernel Booted`));
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
## Default Services
|
|
233
|
+
Zyket includes some default services that provide essential functionality. These services can be overridden or extended if needed.
|
|
234
|
+
|
|
235
|
+
#### Cache Services
|
|
236
|
+
- **Name** `cache`
|
|
237
|
+
- **Description** Provides caching functionality using a Redis adapter.
|
|
238
|
+
- **Configuration** Add `CACHE_URL` in your `.env` file to activate caching.
|
|
239
|
+
|
|
240
|
+
#### Database Service
|
|
241
|
+
- **Name** `database`
|
|
242
|
+
- **Description** Manages database connections using a MariaDB/Sequelize adapter.
|
|
243
|
+
- **Configuration** Add `DATABASE_URL` in your `.env` file to enable the database connection.
|
|
244
|
+
|
|
245
|
+
#### S3 Service
|
|
246
|
+
- **Name** `s3`
|
|
247
|
+
- **Description** Provides S3-compatible object storage using MinIO.
|
|
248
|
+
- **Configuration** Add the following variables in your `.env` file to enable the service
|
|
249
|
+
- `S3_ENDPOINT`
|
|
250
|
+
- `S3_PORT`
|
|
251
|
+
- `S3_USE_SSL`
|
|
252
|
+
- `S3_ACCESS_KEY`
|
|
253
|
+
- `S3_SECRET_KEY`
|
|
254
|
+
|
|
255
|
+
#### Logger Service
|
|
256
|
+
- **Name** `logger`
|
|
257
|
+
- **Description** Handles logging for the application.
|
|
258
|
+
- **Configuration**
|
|
259
|
+
- Change `LOG_DIRECTORY` in `.env` file to set a custom log directory.
|
|
260
|
+
- Set `DEBUG` in `.env` file to enable or disable debug logging.
|
|
261
|
+
|
|
262
|
+
#### Socket.io Service
|
|
263
|
+
- **Name** `socketio`
|
|
264
|
+
- **Description** Manages the WebSocket server.
|
|
265
|
+
- **Configuration** Add `PORT` in your `.env` file to define the listening port for Socket.io.
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
## Contributing
|
|
269
|
+
|
|
270
|
+
We welcome contributions from the community! If you'd like to improve Zyket, feel free to:
|
|
271
|
+
|
|
272
|
+
- Report issues and suggest features on GitHub Issues
|
|
273
|
+
|
|
274
|
+
- Submit pull requests with bug fixes or enhancements
|
|
275
|
+
|
|
276
|
+
- Improve the documentation
|
|
277
|
+
|
|
278
|
+
Let's build a better framework together! 🚀
|
|
279
|
+
|