backendium 0.0.6 → 0.0.8
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/dist/handler.d.ts +1 -1
- package/dist/handler.js +16 -38
- package/dist/index.js +13 -27
- package/dist/logger.js +3 -1
- package/dist/request.d.ts +1 -1
- package/dist/request.js +26 -51
- package/dist/response.js +4 -1
- package/dist/router.js +8 -7
- package/dist/ws.js +52 -64
- package/package.json +3 -2
- package/readme.md +1 -1
- package/src/handler.ts +1 -1
- package/src/request.ts +1 -1
- package/tsconfig.json +1 -1
package/dist/handler.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import BackendiumResponse from "./response.js";
|
|
|
2
2
|
import Backendium from "./index.js";
|
|
3
3
|
import { NextFunction, Request, RequestHandler } from "express";
|
|
4
4
|
import { BackendiumRequestOptionsType, BackendiumRequestType } from "./request.js";
|
|
5
|
-
import { BackendiumRouter } from "./router";
|
|
5
|
+
import { BackendiumRouter } from "./router.js";
|
|
6
6
|
import { ValidationError } from "checkeasy";
|
|
7
7
|
export type BackendiumHandlerReturnType = void | undefined | {
|
|
8
8
|
code?: number;
|
package/dist/handler.js
CHANGED
|
@@ -1,23 +1,3 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
11
|
-
var t = {};
|
|
12
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
13
|
-
t[p] = s[p];
|
|
14
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
15
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
16
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
17
|
-
t[p[i]] = s[p[i]];
|
|
18
|
-
}
|
|
19
|
-
return t;
|
|
20
|
-
};
|
|
21
1
|
import BackendiumResponse from "./response.js";
|
|
22
2
|
import parseRequest from "./request.js";
|
|
23
3
|
export function defaultAuthFailedHandler(request, response, app) {
|
|
@@ -26,25 +6,23 @@ export function defaultAuthFailedHandler(request, response, app) {
|
|
|
26
6
|
}
|
|
27
7
|
export function defaultErrorHandler(request, response, data, app, error, message) {
|
|
28
8
|
response.status(500);
|
|
29
|
-
response.end(message
|
|
9
|
+
response.end(message ?? "Internal Server Error");
|
|
30
10
|
app.logger.requestError(request.url, data, error);
|
|
31
11
|
}
|
|
32
12
|
export function defaultValidationErrorHandler(request, response, app, data, error, message) {
|
|
33
13
|
response.status(400);
|
|
34
|
-
response.end(message
|
|
14
|
+
response.end(message ?? "Validation failed"); // @TODO
|
|
35
15
|
}
|
|
36
|
-
export default function backendiumHandler(handler,
|
|
37
|
-
|
|
38
|
-
return (router) => (app) => ((request, response, next) => __awaiter(this, void 0, void 0, function* () {
|
|
39
|
-
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
16
|
+
export default function backendiumHandler(handler, { auth, authChecker, authFailed, errorHandler, errorMessage, validationErrorHandler, validationErrorMessage, ...options }) {
|
|
17
|
+
return (router) => (app) => (async (request, response, next) => {
|
|
40
18
|
let body;
|
|
41
19
|
try {
|
|
42
|
-
let req =
|
|
20
|
+
let req = await parseRequest(request, app, { ...options, auth, authChecker, authFailed, errorHandler, validationErrorHandler, errorMessage, validationErrorMessage });
|
|
43
21
|
let res = new BackendiumResponse(response, app);
|
|
44
22
|
if (Array.isArray(req)) {
|
|
45
23
|
let [data, error] = req;
|
|
46
|
-
(
|
|
47
|
-
if (
|
|
24
|
+
(validationErrorHandler ?? app.config.validationErrorHandler ?? defaultValidationErrorHandler)(request, res, app, data, error, validationErrorMessage ?? app.config.validationErrorMessage);
|
|
25
|
+
if (app.config.logging?.fullRequest)
|
|
48
26
|
app.logger.requestFull(request.url, res.lastStatus, data, res.lastResponse);
|
|
49
27
|
else
|
|
50
28
|
app.logger.request(request.url, res.lastStatus);
|
|
@@ -56,9 +34,9 @@ export default function backendiumHandler(handler, _a) {
|
|
|
56
34
|
if (authChecker) {
|
|
57
35
|
let ret = authChecker(request, res, app);
|
|
58
36
|
if (ret instanceof Promise)
|
|
59
|
-
ret =
|
|
37
|
+
ret = await ret;
|
|
60
38
|
if (ret === null) {
|
|
61
|
-
(
|
|
39
|
+
(authFailed ?? router.authFailed ?? app.authFailed ?? defaultAuthFailedHandler)(request, res, app);
|
|
62
40
|
return;
|
|
63
41
|
}
|
|
64
42
|
authData = ret;
|
|
@@ -68,16 +46,16 @@ export default function backendiumHandler(handler, _a) {
|
|
|
68
46
|
if (!authChecker && auth && router.authChecker) {
|
|
69
47
|
let ret = router.authChecker(request, res, app);
|
|
70
48
|
if (ret instanceof Promise)
|
|
71
|
-
ret =
|
|
49
|
+
ret = await ret;
|
|
72
50
|
if (ret === null) {
|
|
73
|
-
(
|
|
51
|
+
(authFailed ?? router.authFailed ?? app.authFailed ?? defaultAuthFailedHandler)(request, res, app);
|
|
74
52
|
return;
|
|
75
53
|
}
|
|
76
54
|
globalAuthData = ret;
|
|
77
55
|
}
|
|
78
|
-
let ret = handler(
|
|
56
|
+
let ret = handler({ ...req, auth: authData, globalAuth: globalAuthData }, res, app, next);
|
|
79
57
|
if (ret instanceof Promise)
|
|
80
|
-
ret =
|
|
58
|
+
ret = await ret;
|
|
81
59
|
if (!ret)
|
|
82
60
|
return;
|
|
83
61
|
let { code = 200, next: isNext = false } = ret;
|
|
@@ -92,13 +70,13 @@ export default function backendiumHandler(handler, _a) {
|
|
|
92
70
|
catch (error) {
|
|
93
71
|
return;
|
|
94
72
|
}
|
|
95
|
-
if (
|
|
73
|
+
if (app.config.logging?.fullRequest)
|
|
96
74
|
app.logger.requestFull(request.url, res.lastStatus, req.body, res.lastResponse);
|
|
97
75
|
else
|
|
98
76
|
app.logger.request(request.url, res.lastStatus);
|
|
99
77
|
}
|
|
100
78
|
catch (error) {
|
|
101
|
-
(
|
|
79
|
+
(errorHandler ?? app.config.errorHandler ?? defaultErrorHandler)(request, new BackendiumResponse(response, app), body, app, error, errorMessage ?? app.config.errorMessage);
|
|
102
80
|
}
|
|
103
|
-
})
|
|
81
|
+
});
|
|
104
82
|
}
|
package/dist/index.js
CHANGED
|
@@ -1,32 +1,22 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
1
|
import { BackendiumRouter } from "./router.js";
|
|
11
2
|
import { EventEmitter } from "event-emitter-typescript";
|
|
12
3
|
import { WebSocketExpress } from "websocket-express";
|
|
13
4
|
import Logger from "./logger";
|
|
14
5
|
export default class Backendium extends BackendiumRouter {
|
|
6
|
+
express = new WebSocketExpress;
|
|
7
|
+
eventEmitter = new EventEmitter;
|
|
8
|
+
logger = new Logger(console.log.bind(console));
|
|
9
|
+
config_ = {};
|
|
15
10
|
constructor(config = {}) {
|
|
16
11
|
super();
|
|
17
|
-
this.express = new WebSocketExpress;
|
|
18
|
-
this.eventEmitter = new EventEmitter;
|
|
19
|
-
this.logger = new Logger(console.log.bind(console));
|
|
20
|
-
this.config_ = {};
|
|
21
12
|
this.config = config;
|
|
22
13
|
}
|
|
23
14
|
get config() { return this.config_; }
|
|
24
15
|
set config(config) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
if ((_d = (_c = this.config_.logging) === null || _c === void 0 ? void 0 : _c.replaceConsoleLog) !== null && _d !== void 0 ? _d : true) {
|
|
16
|
+
this.config_ = { ...this.config_, ...config, logging: { ...this.config_.logging, ...config.logging } };
|
|
17
|
+
if (this.config_.logging?.path)
|
|
18
|
+
this.logger.path = this.config_.logging?.path;
|
|
19
|
+
if (this.config_.logging?.replaceConsoleLog ?? true) {
|
|
30
20
|
console.log = this.logger.message.bind(this.logger);
|
|
31
21
|
console.error = this.logger.error.bind(this.logger);
|
|
32
22
|
}
|
|
@@ -53,7 +43,6 @@ export default class Backendium extends BackendiumRouter {
|
|
|
53
43
|
}
|
|
54
44
|
;
|
|
55
45
|
start(callback) {
|
|
56
|
-
var _a, _b;
|
|
57
46
|
this.handlers.forEach(([method, route, handlers]) => {
|
|
58
47
|
if (method == "ws")
|
|
59
48
|
this.addWSHandler(route, handlers.map(handler => handler(this)));
|
|
@@ -68,20 +57,17 @@ export default class Backendium extends BackendiumRouter {
|
|
|
68
57
|
// if (callback) callback(server);
|
|
69
58
|
// this.eventEmitter.emit("start", [server]);
|
|
70
59
|
// });
|
|
71
|
-
const server = this.express.listen(
|
|
72
|
-
|
|
73
|
-
this.logger.initMessage((_a = this.config_.name) !== null && _a !== void 0 ? _a : "app", (_b = this.config_.version) !== null && _b !== void 0 ? _b : "0.0.0", (_c = this.config.port) !== null && _c !== void 0 ? _c : 8080, (_d = this.config_.host) !== null && _d !== void 0 ? _d : "localhost");
|
|
60
|
+
const server = this.express.listen(this.config_.port ?? 8080, this.config_.host ?? "localhost", () => {
|
|
61
|
+
this.logger.initMessage(this.config_.name ?? "app", this.config_.version ?? "0.0.0", this.config.port ?? 8080, this.config_.host ?? "localhost");
|
|
74
62
|
if (callback)
|
|
75
63
|
callback(server);
|
|
76
64
|
this.eventEmitter.emit("start", [server]);
|
|
77
65
|
});
|
|
78
66
|
return server;
|
|
79
67
|
}
|
|
80
|
-
startAsync() {
|
|
81
|
-
return
|
|
82
|
-
|
|
83
|
-
this.start(resolve);
|
|
84
|
-
});
|
|
68
|
+
async startAsync() {
|
|
69
|
+
return new Promise(resolve => {
|
|
70
|
+
this.start(resolve);
|
|
85
71
|
});
|
|
86
72
|
}
|
|
87
73
|
}
|
package/dist/logger.js
CHANGED
|
@@ -6,10 +6,12 @@ export function getDate() {
|
|
|
6
6
|
return `${date.getFullYear()}.${date.getMonth() + 1}.${date.getDate()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
|
|
7
7
|
}
|
|
8
8
|
export default class Logger {
|
|
9
|
+
log;
|
|
10
|
+
path_;
|
|
11
|
+
logData = "";
|
|
9
12
|
constructor(log, path_) {
|
|
10
13
|
this.log = log;
|
|
11
14
|
this.path_ = path_;
|
|
12
|
-
this.logData = "";
|
|
13
15
|
}
|
|
14
16
|
get path() { return this.path_; }
|
|
15
17
|
set path(path) {
|
package/dist/request.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Request } from "express";
|
|
2
2
|
import Backendium from "./index.js";
|
|
3
3
|
import { ValidationError, Validator } from "checkeasy";
|
|
4
|
-
import BackendiumResponse from "./response";
|
|
4
|
+
import BackendiumResponse from "./response.js";
|
|
5
5
|
export type ValidatorsType<BodyType, ParamsType, QueryType, HeadersType> = {
|
|
6
6
|
bodyValidator?: Validator<BodyType>;
|
|
7
7
|
paramsValidator?: Validator<ParamsType>;
|
package/dist/request.js
CHANGED
|
@@ -1,23 +1,3 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
11
|
-
var t = {};
|
|
12
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
13
|
-
t[p] = s[p];
|
|
14
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
15
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
16
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
17
|
-
t[p[i]] = s[p[i]];
|
|
18
|
-
}
|
|
19
|
-
return t;
|
|
20
|
-
};
|
|
21
1
|
import { ValidationError } from "checkeasy";
|
|
22
2
|
function parse(data, validator, root = "") {
|
|
23
3
|
if (!validator) {
|
|
@@ -41,38 +21,33 @@ function parse(data, validator, root = "") {
|
|
|
41
21
|
throw error0;
|
|
42
22
|
}
|
|
43
23
|
}
|
|
44
|
-
function getBody(request) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
request.
|
|
52
|
-
|
|
53
|
-
resolve(buffer);
|
|
54
|
-
});
|
|
24
|
+
async function getBody(request) {
|
|
25
|
+
if (request.body)
|
|
26
|
+
return request.body;
|
|
27
|
+
return new Promise(resolve => {
|
|
28
|
+
let buffer = Buffer.alloc(0);
|
|
29
|
+
request.on("data", chunk => buffer = Buffer.concat([buffer, chunk]));
|
|
30
|
+
request.on("end", () => {
|
|
31
|
+
request.body = buffer;
|
|
32
|
+
resolve(buffer);
|
|
55
33
|
});
|
|
56
34
|
});
|
|
57
35
|
}
|
|
58
|
-
export default function parseRequest(request, app,
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
let
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
throw error;
|
|
76
|
-
}
|
|
77
|
-
});
|
|
36
|
+
export default async function parseRequest(request, app, { bodyValidator, paramsValidator, queryValidator, headersValidator, ...other }) {
|
|
37
|
+
let bodyBuffer = await getBody(request);
|
|
38
|
+
try {
|
|
39
|
+
let body = parse(bodyBuffer, bodyValidator);
|
|
40
|
+
let params = parse(request.params, paramsValidator);
|
|
41
|
+
let query = parse(request.query, queryValidator);
|
|
42
|
+
let headers = parse(request.headers, headersValidator);
|
|
43
|
+
return {
|
|
44
|
+
expressRequest: request, body, params, query, headers, bodyBuffer, app,
|
|
45
|
+
options: { bodyValidator, paramsValidator, queryValidator, headersValidator, ...other }
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
if (error instanceof ValidationError)
|
|
50
|
+
return [bodyBuffer, error];
|
|
51
|
+
throw error;
|
|
52
|
+
}
|
|
78
53
|
}
|
package/dist/response.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
export default class BackendiumResponse {
|
|
2
|
+
expressResponse;
|
|
3
|
+
app;
|
|
4
|
+
lastStatus = 200;
|
|
5
|
+
lastResponse;
|
|
2
6
|
constructor(expressResponse, app) {
|
|
3
7
|
this.expressResponse = expressResponse;
|
|
4
8
|
this.app = app;
|
|
5
|
-
this.lastStatus = 200;
|
|
6
9
|
}
|
|
7
10
|
status(status) {
|
|
8
11
|
this.lastStatus = status;
|
package/dist/router.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import backendiumHandler from "./handler.js";
|
|
2
2
|
import { WebSocketRouteConstructor } from "./ws.js";
|
|
3
3
|
export class BackendiumRouter {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
_handlers = [];
|
|
5
|
+
authChecker;
|
|
6
|
+
authFailed;
|
|
7
|
+
constructor() { }
|
|
7
8
|
get handlers() { return this._handlers; }
|
|
8
9
|
parseArgs(args) {
|
|
9
10
|
let route = undefined, options = undefined;
|
|
@@ -16,7 +17,7 @@ export class BackendiumRouter {
|
|
|
16
17
|
else
|
|
17
18
|
options = elem;
|
|
18
19
|
}).filter(elem => typeof elem === "function");
|
|
19
|
-
return [route, handlers.map(handler => backendiumHandler(handler, options
|
|
20
|
+
return [route, handlers.map(handler => backendiumHandler(handler, options ?? { auth: false }))];
|
|
20
21
|
}
|
|
21
22
|
addHandler(method, ...args) {
|
|
22
23
|
let [route, handlers] = this.parseArgs(args);
|
|
@@ -123,7 +124,7 @@ export class BackendiumRouter {
|
|
|
123
124
|
return constructor;
|
|
124
125
|
}
|
|
125
126
|
static addPrefix([method, route, handler], prefix) {
|
|
126
|
-
return [method, route || !
|
|
127
|
+
return [method, route || !route?.startsWith("/") ? prefix + (route ?? "") : prefix + (route ?? ""), handler];
|
|
127
128
|
}
|
|
128
129
|
router(router, routePrefix = "") {
|
|
129
130
|
this._handlers.push(...router._handlers.map((handler) => {
|
|
@@ -132,7 +133,7 @@ export class BackendiumRouter {
|
|
|
132
133
|
}));
|
|
133
134
|
}
|
|
134
135
|
setAuth(checker, failHandler) {
|
|
135
|
-
this.authChecker = checker
|
|
136
|
-
this.authFailed = failHandler
|
|
136
|
+
this.authChecker = checker ?? this.authChecker;
|
|
137
|
+
this.authFailed = failHandler ?? this.authFailed;
|
|
137
138
|
}
|
|
138
139
|
}
|
package/dist/ws.js
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
1
|
import { EventEmitter } from "event-emitter-typescript";
|
|
11
2
|
import { ValidationError } from "checkeasy";
|
|
12
3
|
export class BackendiumWebSocket {
|
|
4
|
+
socket;
|
|
5
|
+
wsConstructor;
|
|
6
|
+
app;
|
|
7
|
+
initData;
|
|
8
|
+
url;
|
|
9
|
+
eventEmitter = new EventEmitter;
|
|
10
|
+
wsEventEmitter = new EventEmitter;
|
|
11
|
+
events = new Set;
|
|
12
|
+
operations = new EventEmitter;
|
|
13
|
+
useEvents = false;
|
|
13
14
|
static rawDataParse(data) {
|
|
14
15
|
return data instanceof Buffer ? data : data instanceof ArrayBuffer ? Buffer.from(data) : data.reduce((prev, cur) => Buffer.concat([prev, cur]), Buffer.alloc(0));
|
|
15
16
|
}
|
|
@@ -37,11 +38,10 @@ export class BackendiumWebSocket {
|
|
|
37
38
|
this.operations.emit(operation, [payload, operationConfig, socket, app]);
|
|
38
39
|
}
|
|
39
40
|
parseEventMessage(message, socket, app, isBinary) {
|
|
40
|
-
var _a, _b, _c, _d;
|
|
41
41
|
if (isBinary) {
|
|
42
42
|
this.eventEmitter.emit("notEventMessage", [message, socket, app, isBinary]);
|
|
43
43
|
this.wsConstructor.eventEmitter.emit("notEventMessage", [message, socket, app, isBinary]);
|
|
44
|
-
if (
|
|
44
|
+
if (app.config.logging?.fullWs)
|
|
45
45
|
app.logger.wsInputFull(this.url, message);
|
|
46
46
|
else
|
|
47
47
|
app.logger.wsInput(this.url);
|
|
@@ -51,7 +51,7 @@ export class BackendiumWebSocket {
|
|
|
51
51
|
let [head_, ...data] = message.toString().split("\n");
|
|
52
52
|
let payload = Buffer.from(data.join("\n")), head = this.parseEventHead(head_, message, socket, app);
|
|
53
53
|
if (!head) {
|
|
54
|
-
if (
|
|
54
|
+
if (app.config.logging?.fullWs)
|
|
55
55
|
app.logger.wsInputFull(this.url, message);
|
|
56
56
|
else
|
|
57
57
|
app.logger.wsInput(this.url);
|
|
@@ -59,7 +59,7 @@ export class BackendiumWebSocket {
|
|
|
59
59
|
}
|
|
60
60
|
if ("event" in head) {
|
|
61
61
|
this.emitIncomingEvent(head.event, payload, socket, app, head);
|
|
62
|
-
if (
|
|
62
|
+
if (app.config.logging?.fullWs)
|
|
63
63
|
app.logger.wsIncomingEventFull(this.url, head.event, payload.length ? payload : null);
|
|
64
64
|
else
|
|
65
65
|
app.logger.wsIncomingEvent(this.url, head.event);
|
|
@@ -70,7 +70,7 @@ export class BackendiumWebSocket {
|
|
|
70
70
|
catch (error) {
|
|
71
71
|
this.eventEmitter.emit("notEventMessage", [message, socket, app, isBinary]);
|
|
72
72
|
this.wsConstructor.eventEmitter.emit("notEventMessage", [message, socket, app, isBinary]);
|
|
73
|
-
if (
|
|
73
|
+
if (app.config.logging?.fullWs)
|
|
74
74
|
app.logger.wsInputFull(this.url, message);
|
|
75
75
|
else
|
|
76
76
|
app.logger.wsInput(this.url);
|
|
@@ -83,15 +83,9 @@ export class BackendiumWebSocket {
|
|
|
83
83
|
this.app = app;
|
|
84
84
|
this.initData = initData;
|
|
85
85
|
this.url = url;
|
|
86
|
-
this.eventEmitter = new EventEmitter;
|
|
87
|
-
this.wsEventEmitter = new EventEmitter;
|
|
88
|
-
this.events = new Set;
|
|
89
|
-
this.operations = new EventEmitter;
|
|
90
|
-
this.useEvents = false;
|
|
91
86
|
this.eventEmitter.emit("accept", [this, this.wsConstructor, app]);
|
|
92
87
|
this.wsConstructor.eventEmitter.emit("accept", [this, this.wsConstructor, app]);
|
|
93
88
|
socket.on("message", (data, isBinary) => {
|
|
94
|
-
var _a;
|
|
95
89
|
let buffer = BackendiumWebSocket.rawDataParse(data);
|
|
96
90
|
if (this.useEvents) {
|
|
97
91
|
this.eventEmitter.emit("messageBeforeEvents", [buffer, this, app, isBinary]);
|
|
@@ -101,7 +95,7 @@ export class BackendiumWebSocket {
|
|
|
101
95
|
else {
|
|
102
96
|
this.eventEmitter.emit("notEventMessage", [buffer, this, app, isBinary]);
|
|
103
97
|
this.wsConstructor.eventEmitter.emit("notEventMessage", [buffer, this, app, isBinary]);
|
|
104
|
-
if (
|
|
98
|
+
if (app.config.logging?.fullWs)
|
|
105
99
|
app.logger.wsInputFull(url, data);
|
|
106
100
|
else
|
|
107
101
|
app.logger.wsInput(url);
|
|
@@ -152,7 +146,7 @@ export class BackendiumWebSocket {
|
|
|
152
146
|
return;
|
|
153
147
|
}
|
|
154
148
|
// @ts-ignore
|
|
155
|
-
callback(mainData, socket, app, validator
|
|
149
|
+
callback(mainData, socket, app, validator ?? bufferValidator);
|
|
156
150
|
});
|
|
157
151
|
}
|
|
158
152
|
operation(event, subscriber) {
|
|
@@ -178,9 +172,8 @@ export class BackendiumWebSocket {
|
|
|
178
172
|
this.socket.send(data);
|
|
179
173
|
}
|
|
180
174
|
send(data) {
|
|
181
|
-
var _a;
|
|
182
175
|
this._send(data);
|
|
183
|
-
if (
|
|
176
|
+
if (this.app.config.logging?.fullWs)
|
|
184
177
|
this.app.logger.wsOutputFull(this.url, data);
|
|
185
178
|
else
|
|
186
179
|
this.app.logger.wsOutput(this.url);
|
|
@@ -189,10 +182,9 @@ export class BackendiumWebSocket {
|
|
|
189
182
|
return typeof data === "string" ? data : data instanceof Buffer ? data.toString() : data === undefined ? "undefined" : (typeof data === "number" && isNaN(data)) ? "NaN" : JSON.stringify(data);
|
|
190
183
|
}
|
|
191
184
|
emit(event, payload) {
|
|
192
|
-
var _a;
|
|
193
185
|
BackendiumWebSocket.eventNameCheck(event);
|
|
194
186
|
this._send(`$${event}\n${BackendiumWebSocket.AnyToString(payload)}`);
|
|
195
|
-
if (
|
|
187
|
+
if (this.app.config.logging?.fullWs)
|
|
196
188
|
this.app.logger.wsOutgoingEventFull(this.url, event, payload);
|
|
197
189
|
else
|
|
198
190
|
this.app.logger.wsOutgoingEvent(this.url, event);
|
|
@@ -226,50 +218,47 @@ function parse(data, validator) {
|
|
|
226
218
|
}
|
|
227
219
|
}
|
|
228
220
|
export class WebSocketRouteConstructor {
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
}
|
|
221
|
+
sockets = [];
|
|
222
|
+
eventEmitter = new EventEmitter;
|
|
223
|
+
acceptRejectFn;
|
|
224
|
+
eventHandlers = [];
|
|
225
|
+
operations = new EventEmitter;
|
|
226
|
+
initRequired = false;
|
|
236
227
|
_backendiumWebsocket(socket, app, initData, url) {
|
|
237
228
|
let backendiumSocket = new BackendiumWebSocket(socket, this, app, initData, url);
|
|
238
229
|
// @ts-ignore
|
|
239
230
|
this.eventHandlers.forEach(([event, socket, validator]) => backendiumSocket.event(event, socket, validator));
|
|
240
231
|
}
|
|
241
|
-
_handle(request, response, next, app) {
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
return;
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
let socket = yield response.accept();
|
|
253
|
-
app.logger.wsConnected(request.originalUrl);
|
|
254
|
-
if (this.initRequired) {
|
|
255
|
-
socket.once("message", (data) => {
|
|
256
|
-
this.eventEmitter.emit("init", [socket, BackendiumWebSocket.rawDataParse(data), app, request.originalUrl]);
|
|
257
|
-
});
|
|
232
|
+
async _handle(request, response, next, app) {
|
|
233
|
+
if (this.acceptRejectFn) {
|
|
234
|
+
let [flag, code, message] = await this.acceptRejectFn(request, response, app);
|
|
235
|
+
if (!flag) {
|
|
236
|
+
response.reject(code, message);
|
|
237
|
+
app.logger.wsRejected(request.originalUrl);
|
|
238
|
+
this.eventEmitter.emit("reject", [request, response, app, code, message]);
|
|
239
|
+
return;
|
|
258
240
|
}
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
241
|
+
}
|
|
242
|
+
let socket = await response.accept();
|
|
243
|
+
app.logger.wsConnected(request.originalUrl);
|
|
244
|
+
if (this.initRequired) {
|
|
245
|
+
socket.once("message", (data) => {
|
|
246
|
+
this.eventEmitter.emit("init", [socket, BackendiumWebSocket.rawDataParse(data), app, request.originalUrl]);
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
// @ts-ignore
|
|
250
|
+
else
|
|
251
|
+
this._backendiumWebsocket(socket, app, undefined, request.originalUrl);
|
|
263
252
|
}
|
|
264
253
|
acceptReject(callback) {
|
|
265
|
-
this.acceptRejectFn = (request, response, app) =>
|
|
254
|
+
this.acceptRejectFn = async (request, response, app) => {
|
|
266
255
|
let ans = callback(request, response, app), data;
|
|
267
256
|
if (ans instanceof Promise)
|
|
268
|
-
data =
|
|
257
|
+
data = await ans;
|
|
269
258
|
else
|
|
270
259
|
data = ans;
|
|
271
260
|
return typeof data === "boolean" ? [data, undefined, undefined] : [false, data[0], data[1]];
|
|
272
|
-
}
|
|
261
|
+
};
|
|
273
262
|
return this;
|
|
274
263
|
}
|
|
275
264
|
event(event, callback, validator) {
|
|
@@ -303,12 +292,11 @@ export class WebSocketRouteConstructor {
|
|
|
303
292
|
}
|
|
304
293
|
requireInit(callback, validator) {
|
|
305
294
|
this.initRequired = true;
|
|
306
|
-
this.on("init", (socket, data, app, url) =>
|
|
307
|
-
var _a, _b, _c;
|
|
295
|
+
this.on("init", async (socket, data, app, url) => {
|
|
308
296
|
let [mainData, parsed] = validator ? parse(data, validator) : [data, true];
|
|
309
297
|
if (!parsed || !mainData) {
|
|
310
298
|
this.eventEmitter.emit("initParsingFailed", [data, socket, app, validator]);
|
|
311
|
-
if (
|
|
299
|
+
if (app.config.logging?.fullWs)
|
|
312
300
|
app.logger.wsInitFailedFull(url, data);
|
|
313
301
|
else
|
|
314
302
|
app.logger.wsInitFailed(url);
|
|
@@ -317,9 +305,9 @@ export class WebSocketRouteConstructor {
|
|
|
317
305
|
// @ts-ignore
|
|
318
306
|
let ret = callback(socket, mainData, app);
|
|
319
307
|
if (ret instanceof Promise)
|
|
320
|
-
ret =
|
|
308
|
+
ret = await ret;
|
|
321
309
|
if (ret !== null) {
|
|
322
|
-
if (
|
|
310
|
+
if (app.config.logging?.fullWs)
|
|
323
311
|
app.logger.wsInitFull(url, mainData);
|
|
324
312
|
else
|
|
325
313
|
app.logger.wsInit(url);
|
|
@@ -327,12 +315,12 @@ export class WebSocketRouteConstructor {
|
|
|
327
315
|
}
|
|
328
316
|
else {
|
|
329
317
|
this.eventEmitter.emit("initFailed", [data, socket, app]);
|
|
330
|
-
if (
|
|
318
|
+
if (app.config.logging?.fullWs)
|
|
331
319
|
app.logger.wsInitFailedFull(url, mainData);
|
|
332
320
|
else
|
|
333
321
|
app.logger.wsInitFailed(url);
|
|
334
322
|
}
|
|
335
|
-
})
|
|
323
|
+
});
|
|
336
324
|
return this;
|
|
337
325
|
}
|
|
338
326
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "backendium",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.8",
|
|
4
4
|
"description": "Express-based javascript backend framework with websocket support and type-safe checkeasy validators",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -23,5 +23,6 @@
|
|
|
23
23
|
"@types/ws": "^8.5.12",
|
|
24
24
|
"typescript": "^5.5.3"
|
|
25
25
|
},
|
|
26
|
-
"files": ["src", "dist", "package.json", "readme.md", "tsconfig.json"]
|
|
26
|
+
"files": ["src", "dist", "package.json", "readme.md", "tsconfig.json"],
|
|
27
|
+
"type": "module"
|
|
27
28
|
}
|
package/readme.md
CHANGED
|
@@ -149,7 +149,7 @@ router.setAuth((request, response) => {
|
|
|
149
149
|
});
|
|
150
150
|
|
|
151
151
|
router.post("/auth", (request, response) => {
|
|
152
|
-
console.log(request.globalAuth); // type of request.
|
|
152
|
+
console.log(request.globalAuth); // type of request.globalAuth is string
|
|
153
153
|
response.end();
|
|
154
154
|
}, {
|
|
155
155
|
auth: true
|
package/src/handler.ts
CHANGED
|
@@ -2,7 +2,7 @@ import BackendiumResponse from "./response.js";
|
|
|
2
2
|
import Backendium from "./index.js";
|
|
3
3
|
import {NextFunction, Request, RequestHandler, Response} from "express";
|
|
4
4
|
import parseRequest, {BackendiumRequestOptionsType, BackendiumRequestType} from "./request.js";
|
|
5
|
-
import {BackendiumRouter} from "./router";
|
|
5
|
+
import {BackendiumRouter} from "./router.js";
|
|
6
6
|
import {ValidationError} from "checkeasy";
|
|
7
7
|
|
|
8
8
|
export type BackendiumHandlerReturnType = void | undefined | {code?: number, next?: boolean};
|
package/src/request.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {Request} from "express";
|
|
2
2
|
import Backendium from "./index.js";
|
|
3
3
|
import {ValidationError, Validator} from "checkeasy";
|
|
4
|
-
import BackendiumResponse from "./response";
|
|
4
|
+
import BackendiumResponse from "./response.js";
|
|
5
5
|
|
|
6
6
|
export type ValidatorsType<BodyType, ParamsType, QueryType, HeadersType> = {
|
|
7
7
|
bodyValidator?: Validator<BodyType>,
|