@desaubv/quik 0.0.31 → 0.1.0
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 +804 -0
- package/dist/cron/index.cjs +330 -0
- package/dist/cron/index.cjs.map +1 -0
- package/dist/cron/index.d.cts +38 -0
- package/dist/cron/index.d.ts +38 -0
- package/dist/cron/index.js +291 -0
- package/dist/cron/index.js.map +1 -0
- package/dist/cron-BLi976po.d.cts +38 -0
- package/dist/cron-BLi976po.d.ts +38 -0
- package/dist/http/index.cjs +318 -0
- package/dist/http/index.cjs.map +1 -0
- package/dist/http/index.d.cts +121 -0
- package/dist/http/index.d.ts +121 -0
- package/dist/http/index.js +288 -0
- package/dist/http/index.js.map +1 -0
- package/dist/index-CmT_nxIe.d.cts +28 -0
- package/dist/index-CmT_nxIe.d.ts +28 -0
- package/dist/index.cjs +569 -24
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +91 -6
- package/dist/index.d.ts +91 -6
- package/dist/index.js +563 -21
- package/dist/index.js.map +1 -1
- package/dist/socket.io/index.cjs +110 -0
- package/dist/socket.io/index.cjs.map +1 -0
- package/dist/socket.io/index.d.cts +3 -0
- package/dist/socket.io/index.d.ts +3 -0
- package/dist/socket.io/index.js +85 -0
- package/dist/socket.io/index.js.map +1 -0
- package/dist/ws/index.cjs +111 -0
- package/dist/ws/index.cjs.map +1 -0
- package/dist/ws/index.d.cts +26 -0
- package/dist/ws/index.d.ts +26 -0
- package/dist/ws/index.js +86 -0
- package/dist/ws/index.js.map +1 -0
- package/package.json +75 -51
package/dist/index.cjs
CHANGED
|
@@ -28,12 +28,37 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
28
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
29
|
|
|
30
30
|
// src/index.ts
|
|
31
|
-
var
|
|
32
|
-
__export(
|
|
31
|
+
var src_exports = {};
|
|
32
|
+
__export(src_exports, {
|
|
33
|
+
QuikRouter: () => QuikRouter,
|
|
33
34
|
Router: () => Router,
|
|
35
|
+
Server: () => Server,
|
|
36
|
+
ServerInvalidRouteError: () => ServerInvalidRouteError,
|
|
34
37
|
logger: () => logger
|
|
35
38
|
});
|
|
36
|
-
module.exports = __toCommonJS(
|
|
39
|
+
module.exports = __toCommonJS(src_exports);
|
|
40
|
+
|
|
41
|
+
// src/core/errors/serverErrors.ts
|
|
42
|
+
var ServerInvalidRouteError = class extends Error {
|
|
43
|
+
constructor(path3) {
|
|
44
|
+
super(`"${path3}" is not a QuikRouter instance.`);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
var ServerInvalidCronError = class extends Error {
|
|
48
|
+
constructor() {
|
|
49
|
+
super(`Invalid object, must be a QuikCron instance.`);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
var ServerInvalidWebSocketError = class extends Error {
|
|
53
|
+
constructor() {
|
|
54
|
+
super(`Invalid object, must be a QuikWebSocket instance.`);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
var ServerInvalidSocketIOError = class extends Error {
|
|
58
|
+
constructor() {
|
|
59
|
+
super(`Invalid object, must be a QuikSocketIO instance.`);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
37
62
|
|
|
38
63
|
// src/core/logger.ts
|
|
39
64
|
var import_node_fs = __toESM(require("fs"), 1);
|
|
@@ -61,37 +86,128 @@ var colors = Object.fromEntries(
|
|
|
61
86
|
);
|
|
62
87
|
|
|
63
88
|
// src/core/time.ts
|
|
64
|
-
function dateToString(date, format) {
|
|
89
|
+
function dateToString(date, format, timeZone) {
|
|
90
|
+
const formatter = new Intl.DateTimeFormat("en-US", {
|
|
91
|
+
timeZone,
|
|
92
|
+
year: "numeric",
|
|
93
|
+
month: "2-digit",
|
|
94
|
+
day: "2-digit",
|
|
95
|
+
hour: "2-digit",
|
|
96
|
+
minute: "2-digit",
|
|
97
|
+
second: "2-digit",
|
|
98
|
+
hourCycle: "h23"
|
|
99
|
+
});
|
|
100
|
+
const parts = formatter.formatToParts(date);
|
|
101
|
+
const values = Object.fromEntries(
|
|
102
|
+
parts.filter(({ type }) => type !== "literal").map(({ type, value }) => [type, value])
|
|
103
|
+
);
|
|
65
104
|
return format.replace(/(YYYY|MM|DD|hh|mm|ss)/g, (match) => {
|
|
66
105
|
switch (match) {
|
|
67
106
|
case "YYYY":
|
|
68
|
-
return
|
|
107
|
+
return values.year || "";
|
|
69
108
|
case "MM":
|
|
70
|
-
return
|
|
109
|
+
return values.month || "";
|
|
71
110
|
case "DD":
|
|
72
|
-
return
|
|
111
|
+
return values.day || "";
|
|
73
112
|
case "hh":
|
|
74
|
-
return
|
|
113
|
+
return values.hour || "";
|
|
75
114
|
case "mm":
|
|
76
|
-
return
|
|
115
|
+
return values.minute || "";
|
|
77
116
|
case "ss":
|
|
78
|
-
return
|
|
117
|
+
return values.second || "";
|
|
79
118
|
default:
|
|
80
119
|
return match;
|
|
81
120
|
}
|
|
82
121
|
});
|
|
83
122
|
}
|
|
123
|
+
function isValidTimeZone(timeZone) {
|
|
124
|
+
try {
|
|
125
|
+
new Intl.DateTimeFormat("en-US", {
|
|
126
|
+
timeZone
|
|
127
|
+
});
|
|
128
|
+
return true;
|
|
129
|
+
} catch {
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// src/locales/en.json
|
|
135
|
+
var en_default = {
|
|
136
|
+
logger: {
|
|
137
|
+
levels: {
|
|
138
|
+
log: "log",
|
|
139
|
+
info: "info",
|
|
140
|
+
warning: "warning",
|
|
141
|
+
error: "error",
|
|
142
|
+
debug: "debug"
|
|
143
|
+
}
|
|
144
|
+
},
|
|
145
|
+
server: {
|
|
146
|
+
name: "server",
|
|
147
|
+
start: "Server running at",
|
|
148
|
+
routers_loaded: "routers loaded",
|
|
149
|
+
crons_loaded: "crons loaded",
|
|
150
|
+
socket_loaded: "socket loaded",
|
|
151
|
+
static_loaded: "static dirs loaded",
|
|
152
|
+
views_loaded: "views dirs loaded",
|
|
153
|
+
static_warn: "directory does not exist",
|
|
154
|
+
view_warn: "file don't not exist"
|
|
155
|
+
},
|
|
156
|
+
time: {
|
|
157
|
+
invalidTimeZone1: "Invalid time zone:",
|
|
158
|
+
invalidTimeZone2: "Using system default time zone."
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
// src/locales/es.json
|
|
163
|
+
var es_default = {
|
|
164
|
+
logger: {
|
|
165
|
+
levels: {
|
|
166
|
+
log: "log",
|
|
167
|
+
info: "info",
|
|
168
|
+
warning: "advertencia",
|
|
169
|
+
error: "error",
|
|
170
|
+
debug: "depuraci\xF3n"
|
|
171
|
+
}
|
|
172
|
+
},
|
|
173
|
+
server: {
|
|
174
|
+
name: "servidor",
|
|
175
|
+
start: "Servidor corriendo en",
|
|
176
|
+
routers_loaded: "rutas cargadas",
|
|
177
|
+
crons_loaded: "crons cargados",
|
|
178
|
+
socket_loaded: "socket cargado",
|
|
179
|
+
static_loaded: "carpetas estaticas cargadas",
|
|
180
|
+
views_loaded: "carpetas de vistas cargadas",
|
|
181
|
+
static_warn: "no existe el directorio",
|
|
182
|
+
view_warn: "no existe el archivo"
|
|
183
|
+
},
|
|
184
|
+
time: {
|
|
185
|
+
invalidTimeZone1: "Zona horaria inv\xE1lida:",
|
|
186
|
+
invalidTimeZone2: "Usando la zona horaria predeterminada del sistema."
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
// src/core/translator.ts
|
|
191
|
+
var locales = {
|
|
192
|
+
en: en_default,
|
|
193
|
+
es: es_default
|
|
194
|
+
};
|
|
195
|
+
function translate(language, key) {
|
|
196
|
+
return locales[language][key];
|
|
197
|
+
}
|
|
84
198
|
|
|
85
199
|
// src/core/logger.ts
|
|
86
200
|
var defaultConfig = {
|
|
201
|
+
language: "en",
|
|
87
202
|
save: {
|
|
88
203
|
logFilePath: null,
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
warningFilePath:
|
|
204
|
+
infoFilePath: null,
|
|
205
|
+
errorFilePath: "logs/error.log",
|
|
206
|
+
warningFilePath: "logs/warning.log"
|
|
92
207
|
},
|
|
93
208
|
color: {
|
|
94
209
|
logColor: "blue",
|
|
210
|
+
infoColor: "green",
|
|
95
211
|
errorColor: "red",
|
|
96
212
|
warningColor: "yellow",
|
|
97
213
|
debugColor: null
|
|
@@ -136,7 +252,17 @@ function write(level, color2, file, ...content) {
|
|
|
136
252
|
function parseContent(level, ...body) {
|
|
137
253
|
let header = "";
|
|
138
254
|
if (config.format.timestampFormat) {
|
|
139
|
-
const
|
|
255
|
+
const now = /* @__PURE__ */ new Date();
|
|
256
|
+
const timeZone = config.format.timeZone;
|
|
257
|
+
let tz = timeZone;
|
|
258
|
+
if (timeZone && !isValidTimeZone(timeZone)) {
|
|
259
|
+
tz = void 0;
|
|
260
|
+
}
|
|
261
|
+
const timestamp = dateToString(
|
|
262
|
+
now,
|
|
263
|
+
config.format.timestampFormat,
|
|
264
|
+
tz
|
|
265
|
+
);
|
|
140
266
|
header += `${timestamp} `;
|
|
141
267
|
}
|
|
142
268
|
switch (config.format.titleCase) {
|
|
@@ -168,6 +294,8 @@ function saveToFile(filePath, content) {
|
|
|
168
294
|
}
|
|
169
295
|
function mergeConfig(current, update) {
|
|
170
296
|
return {
|
|
297
|
+
...current,
|
|
298
|
+
...update,
|
|
171
299
|
save: {
|
|
172
300
|
...current.save,
|
|
173
301
|
...update.save
|
|
@@ -192,37 +320,53 @@ var logger = {
|
|
|
192
320
|
return Object.freeze(structuredClone(config));
|
|
193
321
|
},
|
|
194
322
|
log(...content) {
|
|
323
|
+
const messages = translate(config.language, "logger");
|
|
195
324
|
write(
|
|
196
|
-
|
|
325
|
+
messages.levels.log,
|
|
197
326
|
config.color.logColor,
|
|
198
327
|
config.save.logFilePath,
|
|
199
328
|
...content
|
|
200
329
|
);
|
|
201
330
|
},
|
|
331
|
+
info(...content) {
|
|
332
|
+
const messages = translate(config.language, "logger");
|
|
333
|
+
write(
|
|
334
|
+
messages.levels.info,
|
|
335
|
+
config.color.infoColor,
|
|
336
|
+
config.save.infoFilePath,
|
|
337
|
+
...content
|
|
338
|
+
);
|
|
339
|
+
},
|
|
202
340
|
error(...content) {
|
|
341
|
+
const messages = translate(config.language, "logger");
|
|
203
342
|
write(
|
|
204
|
-
|
|
343
|
+
messages.levels.error,
|
|
205
344
|
config.color.errorColor,
|
|
206
345
|
config.save.errorFilePath,
|
|
207
346
|
...content
|
|
208
347
|
);
|
|
209
348
|
},
|
|
210
349
|
warning(...content) {
|
|
350
|
+
const messages = translate(config.language, "logger");
|
|
211
351
|
write(
|
|
212
|
-
|
|
352
|
+
messages.levels.warning,
|
|
213
353
|
config.color.warningColor,
|
|
214
354
|
config.save.warningFilePath,
|
|
215
355
|
...content
|
|
216
356
|
);
|
|
217
357
|
},
|
|
218
358
|
debug(...content) {
|
|
359
|
+
const messages = translate(config.language, "logger");
|
|
219
360
|
write(
|
|
220
|
-
|
|
361
|
+
messages.levels.debug,
|
|
221
362
|
config.color.debugColor,
|
|
222
363
|
null,
|
|
223
364
|
...content
|
|
224
365
|
);
|
|
225
366
|
},
|
|
367
|
+
clear() {
|
|
368
|
+
console.clear();
|
|
369
|
+
},
|
|
226
370
|
custom(level, color2, file, ...content) {
|
|
227
371
|
write(level, color2, file, ...content);
|
|
228
372
|
}
|
|
@@ -230,10 +374,10 @@ var logger = {
|
|
|
230
374
|
|
|
231
375
|
// src/core/router.ts
|
|
232
376
|
var import_express = require("express");
|
|
233
|
-
var
|
|
377
|
+
var QuikRouter = class {
|
|
234
378
|
router;
|
|
235
379
|
pendingMiddleware = [];
|
|
236
|
-
|
|
380
|
+
currentPath = "";
|
|
237
381
|
constructor() {
|
|
238
382
|
this.router = (0, import_express.Router)();
|
|
239
383
|
}
|
|
@@ -241,13 +385,13 @@ var Router = class {
|
|
|
241
385
|
this.pendingMiddleware.push(...handlers);
|
|
242
386
|
return this;
|
|
243
387
|
}
|
|
244
|
-
path(
|
|
245
|
-
this.
|
|
388
|
+
path(path3) {
|
|
389
|
+
this.currentPath = path3;
|
|
246
390
|
return this;
|
|
247
391
|
}
|
|
248
392
|
register(method, handler) {
|
|
249
393
|
this.router[method](
|
|
250
|
-
this.
|
|
394
|
+
this.currentPath,
|
|
251
395
|
...this.pendingMiddleware,
|
|
252
396
|
async (req, res, next) => {
|
|
253
397
|
try {
|
|
@@ -258,7 +402,7 @@ var Router = class {
|
|
|
258
402
|
}
|
|
259
403
|
);
|
|
260
404
|
this.pendingMiddleware = [];
|
|
261
|
-
this.
|
|
405
|
+
this.currentPath = "";
|
|
262
406
|
return this;
|
|
263
407
|
}
|
|
264
408
|
get(handler) {
|
|
@@ -277,9 +421,410 @@ var Router = class {
|
|
|
277
421
|
return this.register("delete", handler);
|
|
278
422
|
}
|
|
279
423
|
};
|
|
424
|
+
function Router() {
|
|
425
|
+
return new QuikRouter();
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
// src/core/server.ts
|
|
429
|
+
var import_express2 = __toESM(require("express"), 1);
|
|
430
|
+
var import_node_http = __toESM(require("http"), 1);
|
|
431
|
+
var import_node_path2 = __toESM(require("path"), 1);
|
|
432
|
+
var import_node_fs2 = __toESM(require("fs"), 1);
|
|
433
|
+
|
|
434
|
+
// src/cron/cron.ts
|
|
435
|
+
var import_node_cron = __toESM(require("node-cron"), 1);
|
|
436
|
+
var import_node_cron2 = require("node-cron");
|
|
437
|
+
|
|
438
|
+
// src/http/status.ts
|
|
439
|
+
var Status = {
|
|
440
|
+
// 1xx Informational
|
|
441
|
+
CONTINUE: 100,
|
|
442
|
+
SWITCHING_PROTOCOLS: 101,
|
|
443
|
+
PROCESSING: 102,
|
|
444
|
+
EARLY_HINTS: 103,
|
|
445
|
+
// 2xx Success
|
|
446
|
+
OK: 200,
|
|
447
|
+
CREATED: 201,
|
|
448
|
+
ACCEPTED: 202,
|
|
449
|
+
NON_AUTHORITATIVE_INFORMATION: 203,
|
|
450
|
+
NO_CONTENT: 204,
|
|
451
|
+
RESET_CONTENT: 205,
|
|
452
|
+
PARTIAL_CONTENT: 206,
|
|
453
|
+
MULTI_STATUS: 207,
|
|
454
|
+
ALREADY_REPORTED: 208,
|
|
455
|
+
IM_USED: 226,
|
|
456
|
+
// 3xx Redirection
|
|
457
|
+
MULTIPLE_CHOICES: 300,
|
|
458
|
+
MOVED_PERMANENTLY: 301,
|
|
459
|
+
FOUND: 302,
|
|
460
|
+
SEE_OTHER: 303,
|
|
461
|
+
NOT_MODIFIED: 304,
|
|
462
|
+
TEMPORARY_REDIRECT: 307,
|
|
463
|
+
PERMANENT_REDIRECT: 308,
|
|
464
|
+
// 4xx Client Errors
|
|
465
|
+
BAD_REQUEST: 400,
|
|
466
|
+
UNAUTHORIZED: 401,
|
|
467
|
+
PAYMENT_REQUIRED: 402,
|
|
468
|
+
FORBIDDEN: 403,
|
|
469
|
+
NOT_FOUND: 404,
|
|
470
|
+
METHOD_NOT_ALLOWED: 405,
|
|
471
|
+
NOT_ACCEPTABLE: 406,
|
|
472
|
+
PROXY_AUTHENTICATION_REQUIRED: 407,
|
|
473
|
+
REQUEST_TIMEOUT: 408,
|
|
474
|
+
CONFLICT: 409,
|
|
475
|
+
GONE: 410,
|
|
476
|
+
LENGTH_REQUIRED: 411,
|
|
477
|
+
PRECONDITION_FAILED: 412,
|
|
478
|
+
PAYLOAD_TOO_LARGE: 413,
|
|
479
|
+
URI_TOO_LONG: 414,
|
|
480
|
+
UNSUPPORTED_MEDIA_TYPE: 415,
|
|
481
|
+
RANGE_NOT_SATISFIABLE: 416,
|
|
482
|
+
EXPECTATION_FAILED: 417,
|
|
483
|
+
IM_A_TEAPOT: 418,
|
|
484
|
+
MISDIRECTED_REQUEST: 421,
|
|
485
|
+
UNPROCESSABLE_CONTENT: 422,
|
|
486
|
+
LOCKED: 423,
|
|
487
|
+
FAILED_DEPENDENCY: 424,
|
|
488
|
+
UPGRADE_REQUIRED: 426,
|
|
489
|
+
PRECONDITION_REQUIRED: 428,
|
|
490
|
+
TOO_MANY_REQUESTS: 429,
|
|
491
|
+
REQUEST_HEADER_FIELDS_TOO_LARGE: 431,
|
|
492
|
+
UNAVAILABLE_FOR_LEGAL_REASONS: 451,
|
|
493
|
+
// 5xx Server Errors
|
|
494
|
+
INTERNAL_SERVER_ERROR: 500,
|
|
495
|
+
NOT_IMPLEMENTED: 501,
|
|
496
|
+
BAD_GATEWAY: 502,
|
|
497
|
+
SERVICE_UNAVAILABLE: 503,
|
|
498
|
+
GATEWAY_TIMEOUT: 504,
|
|
499
|
+
HTTP_VERSION_NOT_SUPPORTED: 505,
|
|
500
|
+
VARIANT_ALSO_NEGOTIATES: 506,
|
|
501
|
+
INSUFFICIENT_STORAGE: 507,
|
|
502
|
+
LOOP_DETECTED: 508,
|
|
503
|
+
NOT_EXTENDED: 510,
|
|
504
|
+
NETWORK_AUTHENTICATION_REQUIRED: 511
|
|
505
|
+
};
|
|
506
|
+
|
|
507
|
+
// src/core/handlers/notFoundHandler.ts
|
|
508
|
+
var QuikNotFoundHandler = (req, res) => {
|
|
509
|
+
res.status(Status.NOT_FOUND).json({
|
|
510
|
+
error: "Not Found",
|
|
511
|
+
message: `Route ${req.method} ${req.originalUrl} not found`
|
|
512
|
+
});
|
|
513
|
+
};
|
|
514
|
+
|
|
515
|
+
// src/core/handlers/httpErrorHandler.ts
|
|
516
|
+
var QuikErrorHandler = (err, req, res, next) => {
|
|
517
|
+
logger.error(err);
|
|
518
|
+
if (res.headersSent) {
|
|
519
|
+
return next(err);
|
|
520
|
+
}
|
|
521
|
+
res.status(Status.INTERNAL_SERVER_ERROR).json({
|
|
522
|
+
error: "Internal Server Error"
|
|
523
|
+
});
|
|
524
|
+
};
|
|
525
|
+
|
|
526
|
+
// src/core/server.ts
|
|
527
|
+
var defaultConfig2 = {
|
|
528
|
+
language: "en",
|
|
529
|
+
bodyParser: {
|
|
530
|
+
type: "json"
|
|
531
|
+
},
|
|
532
|
+
http: {
|
|
533
|
+
port: 8080
|
|
534
|
+
},
|
|
535
|
+
express: {}
|
|
536
|
+
};
|
|
537
|
+
var defaultNotFoundHandler = QuikNotFoundHandler;
|
|
538
|
+
var defaultErrorHandler = QuikErrorHandler;
|
|
539
|
+
var QuikServer = class {
|
|
540
|
+
app;
|
|
541
|
+
httpServer;
|
|
542
|
+
middlewares = [];
|
|
543
|
+
routes = {};
|
|
544
|
+
staticDirs = [];
|
|
545
|
+
crons = [];
|
|
546
|
+
ws;
|
|
547
|
+
io;
|
|
548
|
+
viewDirs = [];
|
|
549
|
+
notFoundHandler;
|
|
550
|
+
errorHandler;
|
|
551
|
+
config;
|
|
552
|
+
constructor() {
|
|
553
|
+
this.app = (0, import_express2.default)();
|
|
554
|
+
this.config = structuredClone(defaultConfig2);
|
|
555
|
+
this.httpServer = import_node_http.default.createServer(
|
|
556
|
+
this.app
|
|
557
|
+
);
|
|
558
|
+
this.io = void 0;
|
|
559
|
+
this.ws = void 0;
|
|
560
|
+
this.notFoundHandler = defaultNotFoundHandler;
|
|
561
|
+
this.errorHandler = defaultErrorHandler;
|
|
562
|
+
}
|
|
563
|
+
setConfig(config2) {
|
|
564
|
+
this.config = {
|
|
565
|
+
...this.config,
|
|
566
|
+
...config2,
|
|
567
|
+
http: {
|
|
568
|
+
...this.config.http,
|
|
569
|
+
...config2.http
|
|
570
|
+
},
|
|
571
|
+
express: {
|
|
572
|
+
...this.config.express,
|
|
573
|
+
...config2.express
|
|
574
|
+
}
|
|
575
|
+
};
|
|
576
|
+
if (config2.timeZone && !isValidTimeZone(config2.timeZone)) {
|
|
577
|
+
const messages = translate(this.config.language, "time");
|
|
578
|
+
logger.warning(
|
|
579
|
+
`${messages.invalidTimeZone1} ${config2.timeZone}. ${messages.invalidTimeZone2}`
|
|
580
|
+
);
|
|
581
|
+
}
|
|
582
|
+
return this;
|
|
583
|
+
}
|
|
584
|
+
setNotFoundHandler(handler) {
|
|
585
|
+
if (!handler) {
|
|
586
|
+
this.notFoundHandler = void 0;
|
|
587
|
+
return this;
|
|
588
|
+
}
|
|
589
|
+
this.notFoundHandler = handler;
|
|
590
|
+
return this;
|
|
591
|
+
}
|
|
592
|
+
setErrorHandler(handler) {
|
|
593
|
+
if (!handler) {
|
|
594
|
+
this.errorHandler = void 0;
|
|
595
|
+
return this;
|
|
596
|
+
}
|
|
597
|
+
this.errorHandler = handler;
|
|
598
|
+
return this;
|
|
599
|
+
}
|
|
600
|
+
addMiddleware(...middlewares) {
|
|
601
|
+
this.middlewares.push(
|
|
602
|
+
...middlewares
|
|
603
|
+
);
|
|
604
|
+
return this;
|
|
605
|
+
}
|
|
606
|
+
addRoute(path3, router) {
|
|
607
|
+
if (!(router instanceof QuikRouter)) {
|
|
608
|
+
throw new ServerInvalidRouteError(path3);
|
|
609
|
+
}
|
|
610
|
+
if (!path3.startsWith("/"))
|
|
611
|
+
path3 = "/" + path3;
|
|
612
|
+
this.routes[path3] = router;
|
|
613
|
+
return this;
|
|
614
|
+
}
|
|
615
|
+
addViewDir(...paths) {
|
|
616
|
+
for (const view of paths) {
|
|
617
|
+
const viewPath = import_node_path2.default.resolve(view);
|
|
618
|
+
if (!import_node_fs2.default.existsSync(viewPath)) {
|
|
619
|
+
const messages = translate(this.config.language, "server");
|
|
620
|
+
logger.warning(
|
|
621
|
+
`[ VIEW ] "${view}" ${messages.view_warn}`
|
|
622
|
+
);
|
|
623
|
+
continue;
|
|
624
|
+
}
|
|
625
|
+
this.viewDirs.push(viewPath);
|
|
626
|
+
}
|
|
627
|
+
return this;
|
|
628
|
+
}
|
|
629
|
+
addCron(cron2) {
|
|
630
|
+
if (cron2.constructor.name != "QuikCron") {
|
|
631
|
+
throw new ServerInvalidCronError();
|
|
632
|
+
}
|
|
633
|
+
this.crons.push(cron2);
|
|
634
|
+
return this;
|
|
635
|
+
}
|
|
636
|
+
useWebSocket(ws) {
|
|
637
|
+
if (ws.constructor.name != "QuikWebSocket") {
|
|
638
|
+
throw new ServerInvalidWebSocketError();
|
|
639
|
+
}
|
|
640
|
+
this.ws = ws;
|
|
641
|
+
return this;
|
|
642
|
+
}
|
|
643
|
+
useSocketIO(socket) {
|
|
644
|
+
if (socket?.constructor.name != "QuikSocketIO") {
|
|
645
|
+
throw new ServerInvalidSocketIOError();
|
|
646
|
+
}
|
|
647
|
+
this.io = socket;
|
|
648
|
+
return this;
|
|
649
|
+
}
|
|
650
|
+
addStaticDir(...paths) {
|
|
651
|
+
for (const dir of paths) {
|
|
652
|
+
const dirPath = import_node_path2.default.resolve(dir);
|
|
653
|
+
if (!import_node_fs2.default.existsSync(dirPath)) {
|
|
654
|
+
const messages = translate(this.config.language, "server");
|
|
655
|
+
logger.warning(
|
|
656
|
+
`[ STATIC ] "${dir}" ${messages.static_warn}`
|
|
657
|
+
);
|
|
658
|
+
continue;
|
|
659
|
+
}
|
|
660
|
+
this.staticDirs.push(dirPath);
|
|
661
|
+
}
|
|
662
|
+
return this;
|
|
663
|
+
}
|
|
664
|
+
#configureExpress() {
|
|
665
|
+
const config2 = this.config.express;
|
|
666
|
+
const { name, env, trustProxy, viewEngine } = config2;
|
|
667
|
+
if (trustProxy) {
|
|
668
|
+
this.app.set(
|
|
669
|
+
"trust proxy",
|
|
670
|
+
config2.trustProxy
|
|
671
|
+
);
|
|
672
|
+
}
|
|
673
|
+
if (env) {
|
|
674
|
+
this.app.set("env", config2.env);
|
|
675
|
+
}
|
|
676
|
+
if (name) {
|
|
677
|
+
this.app.set("name", name);
|
|
678
|
+
}
|
|
679
|
+
if (viewEngine) {
|
|
680
|
+
this.app.set("view engine", viewEngine);
|
|
681
|
+
}
|
|
682
|
+
if (this.viewDirs.length > 0) {
|
|
683
|
+
const messages = translate(this.config.language, "server");
|
|
684
|
+
logger.info(`[ VIEWS ] ${this.viewDirs.length} ${messages.views_loaded}.`);
|
|
685
|
+
this.app.set("views", this.viewDirs);
|
|
686
|
+
}
|
|
687
|
+
return this;
|
|
688
|
+
}
|
|
689
|
+
#configureMiddleware() {
|
|
690
|
+
const bodyParser = this.config.bodyParser;
|
|
691
|
+
switch (bodyParser.type) {
|
|
692
|
+
case "json":
|
|
693
|
+
this.app.use(
|
|
694
|
+
import_express2.default.json(bodyParser.options)
|
|
695
|
+
);
|
|
696
|
+
break;
|
|
697
|
+
case "urlencoded":
|
|
698
|
+
this.app.use(
|
|
699
|
+
import_express2.default.urlencoded(bodyParser.options)
|
|
700
|
+
);
|
|
701
|
+
break;
|
|
702
|
+
case "raw":
|
|
703
|
+
this.app.use(
|
|
704
|
+
import_express2.default.raw(bodyParser.options)
|
|
705
|
+
);
|
|
706
|
+
break;
|
|
707
|
+
case "text":
|
|
708
|
+
this.app.use(
|
|
709
|
+
import_express2.default.text(bodyParser.options)
|
|
710
|
+
);
|
|
711
|
+
break;
|
|
712
|
+
}
|
|
713
|
+
if (this.middlewares.length > 0)
|
|
714
|
+
this.app.use(...this.middlewares);
|
|
715
|
+
return this;
|
|
716
|
+
}
|
|
717
|
+
#loadRoutes() {
|
|
718
|
+
for (const [path3, router] of Object.entries(this.routes)) {
|
|
719
|
+
this.app.use(
|
|
720
|
+
path3,
|
|
721
|
+
router.router
|
|
722
|
+
);
|
|
723
|
+
}
|
|
724
|
+
const messages = translate(this.config.language, "server");
|
|
725
|
+
if (Object.entries(this.routes).length > 0) {
|
|
726
|
+
logger.info(`[ ROUTER ] ${Object.entries(this.routes).length} ${messages.routers_loaded}.`);
|
|
727
|
+
}
|
|
728
|
+
return this;
|
|
729
|
+
}
|
|
730
|
+
#loadCrons() {
|
|
731
|
+
for (const cron2 of this.crons) {
|
|
732
|
+
cron2.start();
|
|
733
|
+
}
|
|
734
|
+
const messages = translate(this.config.language, "server");
|
|
735
|
+
if (this.crons.length > 0) {
|
|
736
|
+
logger.info(`[ CRON ] ${this.crons.length} ${messages.crons_loaded}.`);
|
|
737
|
+
}
|
|
738
|
+
return this;
|
|
739
|
+
}
|
|
740
|
+
#loadWS() {
|
|
741
|
+
if (this.ws) {
|
|
742
|
+
this.ws.start(this.httpServer);
|
|
743
|
+
}
|
|
744
|
+
const messages = translate(this.config.language, "server");
|
|
745
|
+
if (this.ws) {
|
|
746
|
+
logger.info(`[ WS ] ${messages.socket_loaded}.`);
|
|
747
|
+
}
|
|
748
|
+
return this;
|
|
749
|
+
}
|
|
750
|
+
#loadIO() {
|
|
751
|
+
if (this.io) {
|
|
752
|
+
this.io.start(this.httpServer);
|
|
753
|
+
}
|
|
754
|
+
const messages = translate(this.config.language, "server");
|
|
755
|
+
if (this.io) {
|
|
756
|
+
logger.info(`[ SOCKET ] ${messages.socket_loaded}.`);
|
|
757
|
+
}
|
|
758
|
+
return this;
|
|
759
|
+
}
|
|
760
|
+
#loadStaticDirs() {
|
|
761
|
+
for (const dir of this.staticDirs) {
|
|
762
|
+
const dirPath = import_node_path2.default.resolve(dir);
|
|
763
|
+
this.app.use(import_express2.default.static(dirPath));
|
|
764
|
+
}
|
|
765
|
+
const messages = translate(this.config.language, "server");
|
|
766
|
+
if (this.staticDirs.length > 0) {
|
|
767
|
+
logger.info(`[ STATIC ] ${this.staticDirs.length} ${messages.static_loaded}.`);
|
|
768
|
+
}
|
|
769
|
+
return this;
|
|
770
|
+
}
|
|
771
|
+
#loadHandlers() {
|
|
772
|
+
if (this.notFoundHandler) {
|
|
773
|
+
this.app.use(
|
|
774
|
+
this.notFoundHandler
|
|
775
|
+
);
|
|
776
|
+
}
|
|
777
|
+
if (this.errorHandler) {
|
|
778
|
+
this.app.use(
|
|
779
|
+
this.errorHandler
|
|
780
|
+
);
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
#configureHTTP() {
|
|
784
|
+
const config2 = this.config.http;
|
|
785
|
+
if (config2.timeout)
|
|
786
|
+
this.httpServer.timeout = config2.timeout;
|
|
787
|
+
if (config2.keepAlive)
|
|
788
|
+
this.httpServer.keepAliveTimeout = config2.keepAlive;
|
|
789
|
+
if (config2.maxConnections)
|
|
790
|
+
this.httpServer.maxConnections = config2.maxConnections;
|
|
791
|
+
return this;
|
|
792
|
+
}
|
|
793
|
+
#listen() {
|
|
794
|
+
const { port, host } = this.config.http;
|
|
795
|
+
const messages = translate(this.config.language, "server");
|
|
796
|
+
this.httpServer.listen(
|
|
797
|
+
port,
|
|
798
|
+
host,
|
|
799
|
+
() => {
|
|
800
|
+
logger.info(`${messages.start} ${host ?? "localhost"}:${port}`);
|
|
801
|
+
}
|
|
802
|
+
);
|
|
803
|
+
return this;
|
|
804
|
+
}
|
|
805
|
+
start() {
|
|
806
|
+
this.#configureExpress();
|
|
807
|
+
this.#configureMiddleware();
|
|
808
|
+
this.#loadStaticDirs();
|
|
809
|
+
this.#loadRoutes();
|
|
810
|
+
this.#loadHandlers();
|
|
811
|
+
this.#loadCrons();
|
|
812
|
+
this.#loadWS();
|
|
813
|
+
this.#loadIO();
|
|
814
|
+
this.#configureHTTP();
|
|
815
|
+
this.#listen();
|
|
816
|
+
return this;
|
|
817
|
+
}
|
|
818
|
+
};
|
|
819
|
+
function Server() {
|
|
820
|
+
return new QuikServer();
|
|
821
|
+
}
|
|
280
822
|
// Annotate the CommonJS export names for ESM import in node:
|
|
281
823
|
0 && (module.exports = {
|
|
824
|
+
QuikRouter,
|
|
282
825
|
Router,
|
|
826
|
+
Server,
|
|
827
|
+
ServerInvalidRouteError,
|
|
283
828
|
logger
|
|
284
829
|
});
|
|
285
830
|
//# sourceMappingURL=index.cjs.map
|