zyket 1.0.2 → 1.0.3
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 +2 -2
- package/package.json +1 -1
- package/src/services/database/index.js +1 -1
- package/src/services/index.js +4 -3
- package/src/services/socketio/SocketIO.js +7 -29
- package/src/services/template-manager/index.js +67 -0
- package/src/templates/auth/src/handlers/auth/login.js +0 -0
- package/src/templates/auth/src/handlers/auth/register.js +0 -0
- package/src/templates/auth/src/middlewares/logged.js +0 -0
- package/src/templates/default/src/handlers/connection.js +7 -0
- package/src/templates/default/src/handlers/message.js +9 -0
- package/src/templates/default/src/middlewares/default.js +6 -0
package/README.md
CHANGED
|
@@ -30,7 +30,7 @@ const { Handler } = require("zyket");
|
|
|
30
30
|
module.exports = class MessageHandler extends Handler {
|
|
31
31
|
middlewares = ["default"];
|
|
32
32
|
|
|
33
|
-
async handle({ container, socket, data }) {
|
|
33
|
+
async handle({ container, socket, data, io }) {
|
|
34
34
|
container.get("logger").info("Message handler");
|
|
35
35
|
}
|
|
36
36
|
};
|
|
@@ -48,7 +48,7 @@ Middlewares allow you to process data before it reaches the handler. They can be
|
|
|
48
48
|
const { Middleware } = require("zyket");
|
|
49
49
|
|
|
50
50
|
module.exports = class DefaultMiddleware extends Middleware {
|
|
51
|
-
async handle({ container, socket }) {
|
|
51
|
+
async handle({ container, socket, io }) {
|
|
52
52
|
container.get("logger").info("Default middleware");
|
|
53
53
|
}
|
|
54
54
|
};
|
package/package.json
CHANGED
|
@@ -18,7 +18,7 @@ module.exports = class Database extends Service {
|
|
|
18
18
|
|
|
19
19
|
async boot() {
|
|
20
20
|
this.#createModelsFolder();
|
|
21
|
-
this.sequelize = new Sequelize(
|
|
21
|
+
this.sequelize = new Sequelize(this.#databaseUrl, {
|
|
22
22
|
dialect: process.env.DATABASE_DIALECT || 'mariadb',
|
|
23
23
|
logging: (msg) => this.#container.get('logger').debug(msg),
|
|
24
24
|
operatorsAliases: 0,
|
package/src/services/index.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
const Database = require("./
|
|
2
|
-
const Cache = require("./
|
|
3
|
-
const S3 = require("./
|
|
1
|
+
const Database = require("./database");
|
|
2
|
+
const Cache = require("./cache");
|
|
3
|
+
const S3 = require("./s3");
|
|
4
4
|
const { SocketIO } = require("./socketio");
|
|
5
5
|
|
|
6
6
|
module.exports = [
|
|
7
7
|
["logger", require("./Logger"), ["@service_container", process.env.LOG_DIRECTORY || `${process.cwd()}/logs`, process.env.DEBUG === "true"]],
|
|
8
|
+
["template-manager", require("./template-manager"), ["@service_container"]],
|
|
8
9
|
process.env.DATABASE_URL ? ["database", Database, ["@service_container", process.env.DATABASE_URL]] : null,
|
|
9
10
|
process.env.CACHE_URL ? ["cache", Cache, ["@service_container", process.env.CACHE_URL]] : null,
|
|
10
11
|
(process.env.S3_ENDPOINT && process.env.S3_ACCESS_KEY && process.env.S3_SECRET_KEY) ? ["s3", S3, ["@service_container", process.env.S3_ENDPOINT, process.env.S3_PORT, process.env.S3_USE_SSL === "true", process.env.S3_ACCESS_KEY, process.env.S3_SECRET_KEY]] : null,
|
|
@@ -40,9 +40,9 @@ module.exports = class SocketIO extends Service {
|
|
|
40
40
|
this.#container.get('logger').warn(`You are using a middleware that does not exist`);
|
|
41
41
|
continue;
|
|
42
42
|
}
|
|
43
|
-
middleware.handle({ container: this.#container, socket });
|
|
43
|
+
middleware.handle({ container: this.#container, socket, io: this.io });
|
|
44
44
|
}
|
|
45
|
-
connectionHandler.handle({ container: this.#container, socket });
|
|
45
|
+
connectionHandler.handle({ container: this.#container, socket, io: this.io });
|
|
46
46
|
handlers.forEach((handler) => {
|
|
47
47
|
const handlerMiddlewares = (handler?.middlewares || []).map(mdl => this.middlewares[mdl])
|
|
48
48
|
socket.on(handler.event, async (data) => {
|
|
@@ -52,9 +52,9 @@ module.exports = class SocketIO extends Service {
|
|
|
52
52
|
this.#container.get('logger').warn(`You are using a middleware that does not exist`);
|
|
53
53
|
continue;
|
|
54
54
|
}
|
|
55
|
-
await middleware.handle({ container: this.#container, socket });
|
|
55
|
+
await middleware.handle({ container: this.#container, socket, io: this.io });
|
|
56
56
|
}
|
|
57
|
-
return await handler.handle({ container: this.#container, socket, data });
|
|
57
|
+
return await handler.handle({ container: this.#container, socket, data, io: this.io });
|
|
58
58
|
});
|
|
59
59
|
});
|
|
60
60
|
});
|
|
@@ -65,15 +65,8 @@ module.exports = class SocketIO extends Service {
|
|
|
65
65
|
|
|
66
66
|
async #loadConnectionHandler() {
|
|
67
67
|
const connectionHandlerExists = fs.existsSync(path.join(process.cwd(), "src", "handlers", "connection.js"));
|
|
68
|
-
/* if exists require, otherwise create a default one */
|
|
69
68
|
if(!connectionHandlerExists) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
module.exports = class ConnectionHandler extends Handler {
|
|
73
|
-
async handle({ container, socket }) {
|
|
74
|
-
container.get("logger").info("New connection");
|
|
75
|
-
}
|
|
76
|
-
};`);
|
|
69
|
+
this.#container.get('template-manager').installFile('default/src/handlers/connection', path.join(process.cwd(), "src", "handlers", "connection.js"));
|
|
77
70
|
}
|
|
78
71
|
return require(path.join(process.cwd(), "src", "handlers", "connection.js"));
|
|
79
72
|
}
|
|
@@ -94,16 +87,7 @@ module.exports = class ConnectionHandler extends Handler {
|
|
|
94
87
|
if (fs.existsSync(handlersFolder) && !overwrite) return;
|
|
95
88
|
this.#container.get('logger').info(`Creating handlers folder at ${handlersFolder}`);
|
|
96
89
|
fs.mkdirSync(handlersFolder);
|
|
97
|
-
|
|
98
|
-
fs.writeFileSync(path.join(handlersFolder, "message.js"), `const { Handler } = require("zyket");
|
|
99
|
-
module.exports = class MessageHandler extends Handler {
|
|
100
|
-
event = "message";
|
|
101
|
-
middlewares = ["default"];
|
|
102
|
-
|
|
103
|
-
async handle({ container, socket, data }) {
|
|
104
|
-
container.get("logger").info("Message handler");
|
|
105
|
-
}
|
|
106
|
-
};`);
|
|
90
|
+
this.#container.get('template-manager').installFile('default/src/handlers/message', path.join(handlersFolder, "message.js"));
|
|
107
91
|
}
|
|
108
92
|
|
|
109
93
|
async #loadMiddlewaresFromFolder(middlewaresFolder) {
|
|
@@ -123,13 +107,7 @@ module.exports = class MessageHandler extends Handler {
|
|
|
123
107
|
if (fs.existsSync(middlewaresFolder) && !overwrite) return;
|
|
124
108
|
this.#container.get('logger').info(`Creating middlewares folder at ${middlewaresFolder}`);
|
|
125
109
|
fs.mkdirSync(middlewaresFolder);
|
|
126
|
-
|
|
127
|
-
fs.writeFileSync(path.join(middlewaresFolder, "default.js"), `const { Middleware } = require("zyket");
|
|
128
|
-
module.exports = class DefaultMiddleware extends Middleware {
|
|
129
|
-
async handle({ container, socket }) {
|
|
130
|
-
container.get("logger").info("Default middleware");
|
|
131
|
-
}
|
|
132
|
-
};`);
|
|
110
|
+
this.#container.get('template-manager').installFile('default/src/middlewares/default', path.join(middlewaresFolder, "default.js"));
|
|
133
111
|
}
|
|
134
112
|
|
|
135
113
|
stop() {
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
const fg = require('fast-glob');
|
|
2
|
+
const Service = require('../Service');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const fs = require('fs');
|
|
5
|
+
|
|
6
|
+
module.exports = class TemplateManager extends Service {
|
|
7
|
+
#container;
|
|
8
|
+
templates = {}
|
|
9
|
+
|
|
10
|
+
constructor(container) {
|
|
11
|
+
super('template-manager');
|
|
12
|
+
this.#container = container;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
async boot() {
|
|
16
|
+
const zyketTemplates = await fg(['**/*.js'], {
|
|
17
|
+
cwd: path.join(__dirname, '../../templates'),
|
|
18
|
+
});
|
|
19
|
+
for (const template of zyketTemplates) {
|
|
20
|
+
// need to copy full file and relation with the name on templates variable
|
|
21
|
+
const templatePath = path.join(__dirname, '../../templates', template);
|
|
22
|
+
const templateContent = fs.readFileSync(templatePath, 'utf-8');
|
|
23
|
+
this.templates[template.replace('.js', '')] = templateContent;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
this.#container.get('logger').info(`Loaded ${this.templates.length} templates`);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
installFile(fileName, location) {
|
|
30
|
+
const template = this.templates[fileName];
|
|
31
|
+
if (!template) throw new Error(`Template ${fileName} not found`);
|
|
32
|
+
return fs.writeFileSync(location, template);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
installTemplate(templateName) {
|
|
36
|
+
const template = this.getTemplates().find((t) => t === templateName);
|
|
37
|
+
if (!template) throw new Error(`Template ${templateName} not found`);
|
|
38
|
+
const files = this.getTemplate(templateName);
|
|
39
|
+
|
|
40
|
+
for (const file of files) {
|
|
41
|
+
const fileName = file.split('/').slice(1).join('/');
|
|
42
|
+
const fileLocation = path.join(process.cwd(), fileName);
|
|
43
|
+
if (fs.existsSync(fileLocation)) throw new Error(`File ${file} already exists`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
for (const file of files) {
|
|
47
|
+
const fileName = file.split('/').slice(1).join('/');
|
|
48
|
+
const template = this.templates[file];
|
|
49
|
+
const fileLocation = path.join(process.cwd(), fileName);
|
|
50
|
+
fs.writeFileSync(fileLocation, template);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
getTemplates() {
|
|
55
|
+
const uniqueTemplates = new Set();
|
|
56
|
+
for (const template of Object.keys(this.templates)) {
|
|
57
|
+
uniqueTemplates.add(template.split('/')[0]);
|
|
58
|
+
}
|
|
59
|
+
return Array.from(uniqueTemplates);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
getTemplate(templateName) {
|
|
63
|
+
const files = Object.keys(this.templates).filter((t) => t.startsWith(templateName));
|
|
64
|
+
if (files.length === 0) throw new Error(`Template ${templateName} not found`);
|
|
65
|
+
return files
|
|
66
|
+
}
|
|
67
|
+
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|