nicola-framework 1.0.1 → 1.0.2
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 +139 -494
- package/core/Core.js +85 -80
- package/core/Remote.js +7 -2
- package/database/Connection.js +2 -7
- package/database/Model.js +1 -1
- package/database/dialects/Postgres.js +8 -3
- package/dev-tools/LiveCurrent.js +2 -2
- package/middlewares/EasyCors.js +33 -14
- package/middlewares/Shadowgraph.js +2 -3
- package/package.json +6 -6
- package/security/Coherer.js +63 -38
- package/test/Coherer.test.js +73 -0
- package/test/Core.test.js +96 -0
- package/test/Router.test.js +76 -0
- package/utils/console.js +31 -0
- package/utils/expTime.js +30 -0
- package/utils/isPromise.js +7 -0
- package/database/dialects/Mysql.js +0 -0
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import request from 'supertest';
|
|
2
|
+
import { createServer } from 'http';
|
|
3
|
+
import Remote from '../core/Remote.js'; // Ajusta la ruta si es necesario
|
|
4
|
+
|
|
5
|
+
describe('🚦 Router System (Remote.js)', () => {
|
|
6
|
+
let router;
|
|
7
|
+
let server;
|
|
8
|
+
|
|
9
|
+
// Antes de todos los tests, configuramos una "App de Mentira"
|
|
10
|
+
beforeAll(() => {
|
|
11
|
+
router = new Remote();
|
|
12
|
+
|
|
13
|
+
// 1. Ruta Básica
|
|
14
|
+
router.get('/', (req, res) => {
|
|
15
|
+
res.statusCode = 200;
|
|
16
|
+
res.end('Hola Nicola');
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
// 2. Ruta con Parámetros
|
|
20
|
+
router.get('/user/:id', (req, res) => {
|
|
21
|
+
res.statusCode = 200;
|
|
22
|
+
res.setHeader('Content-Type', 'application/json');
|
|
23
|
+
res.end(JSON.stringify({ id: req.params.id }));
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
// 3. Ruta Suicida (Async Error) - Para probar tu arreglo P0
|
|
27
|
+
router.get('/crash', async (req, res) => {
|
|
28
|
+
throw new Error('Boom Async!');
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
// Creamos el servidor nativo que usa tu Router
|
|
32
|
+
server = createServer((req, res) => {
|
|
33
|
+
// Tu router recibe (req, res, done)
|
|
34
|
+
router.handle(req, res, (err) => {
|
|
35
|
+
// Este es el "Final Handler" (lo que sería tu BlackBox o default)
|
|
36
|
+
if (err) {
|
|
37
|
+
res.statusCode = 500;
|
|
38
|
+
res.end(err.message); // Devolvemos el error capturado
|
|
39
|
+
} else {
|
|
40
|
+
res.statusCode = 404;
|
|
41
|
+
res.end('Not Found');
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
describe('Rutas y Navegación', () => {
|
|
48
|
+
test('GET / debe responder 200 OK', async () => {
|
|
49
|
+
const response = await request(server).get('/');
|
|
50
|
+
expect(response.statusCode).toBe(200);
|
|
51
|
+
expect(response.text).toBe('Hola Nicola');
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test('GET /unknown debe responder 404', async () => {
|
|
55
|
+
const response = await request(server).get('/ruta-que-no-existe');
|
|
56
|
+
expect(response.statusCode).toBe(404);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test('GET /user/123 debe capturar parámetros', async () => {
|
|
60
|
+
const response = await request(server).get('/user/555');
|
|
61
|
+
expect(response.statusCode).toBe(200);
|
|
62
|
+
expect(response.body).toEqual({ id: '555' });
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
describe('🛡️ Blindaje de Errores (P0 Fix)', () => {
|
|
67
|
+
test('Debe capturar errores en handlers async y no colgarse', async () => {
|
|
68
|
+
// Si tu fix del principio funciona, esto devolverá 500.
|
|
69
|
+
// Si NO funciona, este test dará timeout porque el servidor se quedará colgado.
|
|
70
|
+
const response = await request(server).get('/crash');
|
|
71
|
+
|
|
72
|
+
expect(response.statusCode).toBe(500);
|
|
73
|
+
expect(response.text).toBe('Boom Async!');
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
});
|
package/utils/console.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
const CODE = {
|
|
2
|
+
reset: "\x1b[0m",
|
|
3
|
+
negrita: "\x1b[1m",
|
|
4
|
+
brigth: "\x1b[1m",
|
|
5
|
+
red: "\x1b[31m",
|
|
6
|
+
green: "\x1b[32m",
|
|
7
|
+
yellow: "\x1b[33m",
|
|
8
|
+
blue: "\x1b[34m",
|
|
9
|
+
cyan: "\x1b[36m",
|
|
10
|
+
magent: "\x1b[35m",
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
export const paint = (color, text) =>{
|
|
16
|
+
if(!process.stdout.isTTY) return text;
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
return `${CODE[color] || ''}${text}${CODE.reset}`
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
export const red = (text) => paint('red', text);
|
|
25
|
+
export const green = (text) => paint('green', text);
|
|
26
|
+
export const yellow = (text) => paint('yellow', text);
|
|
27
|
+
export const blue = (text) => paint('blue', text);
|
|
28
|
+
export const cyan = (text) => paint('cyan', text);
|
|
29
|
+
export const magent = (text) => paint('magent', text);
|
|
30
|
+
export const brigth = (text) => paint('brigth', text);
|
|
31
|
+
export const bold = (text) => paint('negrita', text);
|
package/utils/expTime.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
|
|
2
|
+
export const getExpTime = (durationStr) => {
|
|
3
|
+
|
|
4
|
+
const nowInSeconds = Math.floor(Date.now() / 1000);
|
|
5
|
+
|
|
6
|
+
if (!durationStr || typeof durationStr !== 'string') return null;
|
|
7
|
+
|
|
8
|
+
const match = durationStr.match(/^(\d+)([smhdy])$/);
|
|
9
|
+
|
|
10
|
+
if(!match){
|
|
11
|
+
throw new Error(`Invalid Format, use for example: 10h, 10s, 10m, 10d`)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const value = parseInt(match[1], 10);
|
|
15
|
+
const unit = match[2];
|
|
16
|
+
|
|
17
|
+
let multiplier = 1;
|
|
18
|
+
switch(unit){
|
|
19
|
+
case 'm' : multiplier = 60; break;
|
|
20
|
+
case 'h' : multiplier = 60 * 60; break;
|
|
21
|
+
case 'd' : multiplier = 24 * 60 * 60; break;
|
|
22
|
+
case 'y' : multiplier = 365 * 24 * 60 * 60; break;
|
|
23
|
+
default: multiplier = 1;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const secondsToadd = value * multiplier;
|
|
27
|
+
|
|
28
|
+
return nowInSeconds + secondsToadd;
|
|
29
|
+
|
|
30
|
+
}
|
|
File without changes
|