backendium 0.0.9 → 0.0.11

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/request.js CHANGED
@@ -1,3 +1,23 @@
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
+ };
1
21
  import { ValidationError } from "checkeasy";
2
22
  function parse(data, validator, root = "") {
3
23
  if (!validator) {
@@ -21,33 +41,38 @@ function parse(data, validator, root = "") {
21
41
  throw error0;
22
42
  }
23
43
  }
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);
44
+ function getBody(request) {
45
+ return __awaiter(this, void 0, void 0, function* () {
46
+ if (request.body)
47
+ return request.body;
48
+ return new Promise(resolve => {
49
+ let buffer = Buffer.alloc(0);
50
+ request.on("data", chunk => buffer = Buffer.concat([buffer, chunk]));
51
+ request.on("end", () => {
52
+ request.body = buffer;
53
+ resolve(buffer);
54
+ });
33
55
  });
34
56
  });
35
57
  }
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
- }
58
+ export default function parseRequest(request, app, _a) {
59
+ return __awaiter(this, void 0, void 0, function* () {
60
+ var { bodyValidator, paramsValidator, queryValidator, headersValidator } = _a, other = __rest(_a, ["bodyValidator", "paramsValidator", "queryValidator", "headersValidator"]);
61
+ let bodyBuffer = yield getBody(request);
62
+ try {
63
+ let body = parse(bodyBuffer, bodyValidator, "body");
64
+ let params = parse(request.params, paramsValidator, "params");
65
+ let query = parse(request.query, queryValidator, "query");
66
+ let headers = parse(request.headers, headersValidator, "headers");
67
+ return {
68
+ expressRequest: request, body, params, query, headers, bodyBuffer, app,
69
+ options: Object.assign({ bodyValidator, paramsValidator, queryValidator, headersValidator }, other)
70
+ };
71
+ }
72
+ catch (error) {
73
+ if (error instanceof ValidationError)
74
+ return [bodyBuffer, error];
75
+ throw error;
76
+ }
77
+ });
53
78
  }
package/dist/response.js CHANGED
@@ -1,11 +1,8 @@
1
1
  export default class BackendiumResponse {
2
- expressResponse;
3
- app;
4
- lastStatus = 200;
5
- lastResponse;
6
2
  constructor(expressResponse, app) {
7
3
  this.expressResponse = expressResponse;
8
4
  this.app = app;
5
+ this.lastStatus = 200;
9
6
  }
10
7
  status(status) {
11
8
  this.lastStatus = status;
package/dist/router.d.ts CHANGED
@@ -4,54 +4,13 @@ import { RequestHandler } from "express";
4
4
  import Backendium from "./index.js";
5
5
  import { WebSocketRouteConstructor } from "./ws.js";
6
6
  import { WSRequestHandler } from "websocket-express";
7
- export type MethodType = "use" | "all" | "get" | "post" | "put" | "delete" | "patch" | "options" | "head" | "checkout" | "connect" | "copy" | "lock" | "merge" | "mkactivity" | "mkcol" | "move" | "m-search" | "notify" | "propfind" | "proppatch" | "purge" | "report" | "search" | "subscribe" | "unsubscribe" | "trace" | "unlock" | "link" | "unlink" | "useHTTP";
8
- export type BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType> = Array<BackendiumHandlerType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>> | [string, ...Array<BackendiumHandlerType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>>] | [
9
- ...Array<BackendiumHandlerType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>>,
10
- BackendiumRequestOptionsType<BodyType, ParamsType, QueryType, AuthType, HeadersType>
11
- ] | [
12
- string,
13
- ...Array<BackendiumHandlerType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>>,
14
- BackendiumRequestOptionsType<BodyType, ParamsType, QueryType, AuthType, HeadersType>
15
- ];
16
- export declare class BackendiumRouter<GlobalAuthType = undefined> {
7
+ import { BackendiumHttpRouter, type MethodType } from "./httpRouter.js";
8
+ export declare class BackendiumRouter<GlobalAuthType = undefined> extends BackendiumHttpRouter<GlobalAuthType> {
17
9
  protected _handlers: Array<[MethodType, string | undefined, Array<ReturnType<RawHandlerType<GlobalAuthType>>>] | ["ws", string, Array<(app: Backendium) => WSRequestHandler>]>;
18
10
  authChecker: AuthCheckerType<GlobalAuthType> | undefined;
19
11
  authFailed: AuthFailedType | undefined;
20
- constructor();
12
+ protected pushHandlers<BodyType, ParamsType, QueryType, AuthType, HeadersType>(method: MethodType, path: string | undefined, options: BackendiumRequestOptionsType<BodyType, ParamsType, QueryType, AuthType, HeadersType> | undefined, handlers: Array<BackendiumHandlerType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>>): void;
21
13
  get handlers(): ([MethodType, string | undefined, ((app: Backendium) => RequestHandler)[]] | ["ws", string, ((app: Backendium) => WSRequestHandler)[]])[];
22
- protected parseArgs<BodyType, ParamsType, QueryType, AuthType, HeadersType>(args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>): [string | undefined, Array<RawHandlerType<GlobalAuthType>>];
23
- addHandler<BodyType, ParamsType, QueryType, AuthType, HeadersType>(method: MethodType, ...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>): void;
24
- use<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>): void;
25
- useHTTP<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>): void;
26
- all<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>): void;
27
- get<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>): void;
28
- post<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>): void;
29
- put<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>): void;
30
- delete<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>): void;
31
- patch<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>): void;
32
- options<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>): void;
33
- head<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>): void;
34
- checkout<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>): void;
35
- connect<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>): void;
36
- copy<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>): void;
37
- lock<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>): void;
38
- merge<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>): void;
39
- mkactivity<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>): void;
40
- mkcol<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>): void;
41
- move<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>): void;
42
- "m-search"<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>): void;
43
- notify<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>): void;
44
- propfind<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>): void;
45
- proppatch<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>): void;
46
- purge<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>): void;
47
- report<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>): void;
48
- search<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>): void;
49
- subscribe<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>): void;
50
- unsubscribe<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>): void;
51
- trace<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>): void;
52
- unlock<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>): void;
53
- link<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>): void;
54
- unlink<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>): void;
55
14
  ws<InitDataType>(route: string): WebSocketRouteConstructor<InitDataType>;
56
15
  protected static addPrefix([method, route, handler]: [MethodType, string | undefined, Array<(app: Backendium) => RequestHandler>], prefix: string): [MethodType, string, Array<(app: Backendium) => RequestHandler>];
57
16
  protected static addPrefix([method, route, handler]: ["ws", string, Array<(app: Backendium) => WSRequestHandler>], prefix: string): ["ws", string, Array<(app: Backendium) => WSRequestHandler>];
package/dist/router.js CHANGED
@@ -1,121 +1,15 @@
1
1
  import backendiumHandler from "./handler.js";
2
2
  import { WebSocketRouteConstructor } from "./ws.js";
3
- export class BackendiumRouter {
4
- _handlers = [];
5
- authChecker;
6
- authFailed;
7
- constructor() { }
8
- get handlers() { return this._handlers; }
9
- parseArgs(args) {
10
- let route = undefined, options = undefined;
11
- let handlers = args.map(elem => {
12
- if (typeof elem === "string")
13
- route = elem;
14
- if (typeof elem === "function")
15
- return elem;
16
- // @ts-ignore
17
- else
18
- options = elem;
19
- }).filter(elem => typeof elem === "function");
20
- return [route, handlers.map(handler => backendiumHandler(handler, options ?? { auth: false }))];
21
- }
22
- addHandler(method, ...args) {
23
- let [route, handlers] = this.parseArgs(args);
24
- this._handlers.push([method, route, handlers.map(handler => handler(this))]);
25
- }
26
- use(...args) {
27
- this.addHandler("use", ...args);
28
- }
29
- useHTTP(...args) {
30
- this.addHandler("useHTTP", ...args);
31
- }
32
- all(...args) {
33
- this.addHandler("all", ...args);
34
- }
35
- get(...args) {
36
- this.addHandler("get", ...args);
37
- }
38
- post(...args) {
39
- this.addHandler("post", ...args);
40
- }
41
- put(...args) {
42
- this.addHandler("put", ...args);
43
- }
44
- delete(...args) {
45
- this.addHandler("delete", ...args);
46
- }
47
- patch(...args) {
48
- this.addHandler("patch", ...args);
49
- }
50
- options(...args) {
51
- this.addHandler("options", ...args);
52
- }
53
- head(...args) {
54
- this.addHandler("head", ...args);
55
- }
56
- checkout(...args) {
57
- this.addHandler("checkout", ...args);
58
- }
59
- connect(...args) {
60
- this.addHandler("connect", ...args);
61
- }
62
- copy(...args) {
63
- this.addHandler("copy", ...args);
64
- }
65
- lock(...args) {
66
- this.addHandler("lock", ...args);
3
+ import { BackendiumHttpRouter } from "./httpRouter.js";
4
+ export class BackendiumRouter extends BackendiumHttpRouter {
5
+ constructor() {
6
+ super(...arguments);
7
+ this._handlers = [];
67
8
  }
68
- merge(...args) {
69
- this.addHandler("merge", ...args);
70
- }
71
- mkactivity(...args) {
72
- this.addHandler("mkactivity", ...args);
73
- }
74
- mkcol(...args) {
75
- this.addHandler("mkcol", ...args);
76
- }
77
- move(...args) {
78
- this.addHandler("move", ...args);
79
- }
80
- "m-search"(...args) {
81
- this.addHandler("m-search", ...args);
82
- }
83
- notify(...args) {
84
- this.addHandler("notify", ...args);
85
- }
86
- propfind(...args) {
87
- this.addHandler("propfind", ...args);
88
- }
89
- proppatch(...args) {
90
- this.addHandler("proppatch", ...args);
91
- }
92
- purge(...args) {
93
- this.addHandler("purge", ...args);
94
- }
95
- report(...args) {
96
- this.addHandler("report", ...args);
97
- }
98
- search(...args) {
99
- this.addHandler("search", ...args);
100
- }
101
- subscribe(...args) {
102
- this.addHandler("subscribe", ...args);
103
- }
104
- unsubscribe(...args) {
105
- this.addHandler("unsubscribe", ...args);
106
- }
107
- trace(...args) {
108
- this.addHandler("trace", ...args);
109
- }
110
- unlock(...args) {
111
- this.addHandler("unlock", ...args);
112
- }
113
- link(...args) {
114
- this.addHandler("link", ...args);
115
- }
116
- unlink(...args) {
117
- this.addHandler("unlink", ...args);
9
+ pushHandlers(method, path, options, handlers) {
10
+ this._handlers.push([method, path, handlers.map(handler => backendiumHandler(handler, options !== null && options !== void 0 ? options : { auth: false })(this))]);
118
11
  }
12
+ get handlers() { return this._handlers; }
119
13
  ws(route) {
120
14
  let constructor = new WebSocketRouteConstructor;
121
15
  this._handlers.push(["ws", route, [(app) => (request, response, next) => {
@@ -124,7 +18,7 @@ export class BackendiumRouter {
124
18
  return constructor;
125
19
  }
126
20
  static addPrefix([method, route, handler], prefix) {
127
- return [method, route || !route?.startsWith("/") ? prefix + (route ?? "") : prefix + (route ?? ""), handler];
21
+ return [method, route || !(route === null || route === void 0 ? void 0 : route.startsWith("/")) ? prefix + (route !== null && route !== void 0 ? route : "") : prefix + (route !== null && route !== void 0 ? route : ""), handler];
128
22
  }
129
23
  router(router, routePrefix = "") {
130
24
  this._handlers.push(...router._handlers.map((handler) => {
@@ -133,7 +27,7 @@ export class BackendiumRouter {
133
27
  }));
134
28
  }
135
29
  setAuth(checker, failHandler) {
136
- this.authChecker = checker ?? this.authChecker;
137
- this.authFailed = failHandler ?? this.authFailed;
30
+ this.authChecker = checker !== null && checker !== void 0 ? checker : this.authChecker;
31
+ this.authFailed = failHandler !== null && failHandler !== void 0 ? failHandler : this.authFailed;
138
32
  }
139
33
  }
package/dist/ws.js CHANGED
@@ -1,16 +1,15 @@
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
+ };
1
10
  import { EventEmitter } from "event-emitter-typescript";
2
11
  import { ValidationError } from "checkeasy";
3
12
  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;
14
13
  static rawDataParse(data) {
15
14
  return data instanceof Buffer ? data : data instanceof ArrayBuffer ? Buffer.from(data) : data.reduce((prev, cur) => Buffer.concat([prev, cur]), Buffer.alloc(0));
16
15
  }
@@ -38,10 +37,11 @@ export class BackendiumWebSocket {
38
37
  this.operations.emit(operation, [payload, operationConfig, socket, app]);
39
38
  }
40
39
  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 (app.config.logging?.fullWs)
44
+ if ((_a = app.config.logging) === null || _a === void 0 ? void 0 : _a.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 (app.config.logging?.fullWs)
54
+ if ((_b = app.config.logging) === null || _b === void 0 ? void 0 : _b.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 (app.config.logging?.fullWs)
62
+ if ((_c = app.config.logging) === null || _c === void 0 ? void 0 : _c.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 (app.config.logging?.fullWs)
73
+ if ((_d = app.config.logging) === null || _d === void 0 ? void 0 : _d.fullWs)
74
74
  app.logger.wsInputFull(this.url, message);
75
75
  else
76
76
  app.logger.wsInput(this.url);
@@ -83,9 +83,15 @@ 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;
86
91
  this.eventEmitter.emit("accept", [this, this.wsConstructor, app]);
87
92
  this.wsConstructor.eventEmitter.emit("accept", [this, this.wsConstructor, app]);
88
93
  socket.on("message", (data, isBinary) => {
94
+ var _a;
89
95
  let buffer = BackendiumWebSocket.rawDataParse(data);
90
96
  if (this.useEvents) {
91
97
  this.eventEmitter.emit("messageBeforeEvents", [buffer, this, app, isBinary]);
@@ -95,7 +101,7 @@ export class BackendiumWebSocket {
95
101
  else {
96
102
  this.eventEmitter.emit("notEventMessage", [buffer, this, app, isBinary]);
97
103
  this.wsConstructor.eventEmitter.emit("notEventMessage", [buffer, this, app, isBinary]);
98
- if (app.config.logging?.fullWs)
104
+ if ((_a = app.config.logging) === null || _a === void 0 ? void 0 : _a.fullWs)
99
105
  app.logger.wsInputFull(url, data);
100
106
  else
101
107
  app.logger.wsInput(url);
@@ -146,7 +152,7 @@ export class BackendiumWebSocket {
146
152
  return;
147
153
  }
148
154
  // @ts-ignore
149
- callback(mainData, socket, app, validator ?? bufferValidator);
155
+ callback(mainData, socket, app, validator !== null && validator !== void 0 ? validator : bufferValidator);
150
156
  });
151
157
  }
152
158
  operation(event, subscriber) {
@@ -172,8 +178,9 @@ export class BackendiumWebSocket {
172
178
  this.socket.send(data);
173
179
  }
174
180
  send(data) {
181
+ var _a;
175
182
  this._send(data);
176
- if (this.app.config.logging?.fullWs)
183
+ if ((_a = this.app.config.logging) === null || _a === void 0 ? void 0 : _a.fullWs)
177
184
  this.app.logger.wsOutputFull(this.url, data);
178
185
  else
179
186
  this.app.logger.wsOutput(this.url);
@@ -182,9 +189,10 @@ export class BackendiumWebSocket {
182
189
  return typeof data === "string" ? data : data instanceof Buffer ? data.toString() : data === undefined ? "undefined" : (typeof data === "number" && isNaN(data)) ? "NaN" : JSON.stringify(data);
183
190
  }
184
191
  emit(event, payload) {
192
+ var _a;
185
193
  BackendiumWebSocket.eventNameCheck(event);
186
194
  this._send(`$${event}\n${BackendiumWebSocket.AnyToString(payload)}`);
187
- if (this.app.config.logging?.fullWs)
195
+ if ((_a = this.app.config.logging) === null || _a === void 0 ? void 0 : _a.fullWs)
188
196
  this.app.logger.wsOutgoingEventFull(this.url, event, payload);
189
197
  else
190
198
  this.app.logger.wsOutgoingEvent(this.url, event);
@@ -218,47 +226,50 @@ function parse(data, validator) {
218
226
  }
219
227
  }
220
228
  export class WebSocketRouteConstructor {
221
- sockets = [];
222
- eventEmitter = new EventEmitter;
223
- acceptRejectFn;
224
- eventHandlers = [];
225
- operations = new EventEmitter;
226
- initRequired = false;
229
+ constructor() {
230
+ this.sockets = [];
231
+ this.eventEmitter = new EventEmitter;
232
+ this.eventHandlers = [];
233
+ this.operations = new EventEmitter;
234
+ this.initRequired = false;
235
+ }
227
236
  _backendiumWebsocket(socket, app, initData, url) {
228
237
  let backendiumSocket = new BackendiumWebSocket(socket, this, app, initData, url);
229
238
  // @ts-ignore
230
239
  this.eventHandlers.forEach(([event, socket, validator]) => backendiumSocket.event(event, socket, validator));
231
240
  }
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;
241
+ _handle(request, response, next, app) {
242
+ return __awaiter(this, void 0, void 0, function* () {
243
+ if (this.acceptRejectFn) {
244
+ let [flag, code, message] = yield this.acceptRejectFn(request, response, app);
245
+ if (!flag) {
246
+ response.reject(code, message);
247
+ app.logger.wsRejected(request.originalUrl);
248
+ this.eventEmitter.emit("reject", [request, response, app, code, message]);
249
+ return;
250
+ }
240
251
  }
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);
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
+ });
258
+ }
259
+ // @ts-ignore
260
+ else
261
+ this._backendiumWebsocket(socket, app, undefined, request.originalUrl);
262
+ });
252
263
  }
253
264
  acceptReject(callback) {
254
- this.acceptRejectFn = async (request, response, app) => {
265
+ this.acceptRejectFn = (request, response, app) => __awaiter(this, void 0, void 0, function* () {
255
266
  let ans = callback(request, response, app), data;
256
267
  if (ans instanceof Promise)
257
- data = await ans;
268
+ data = yield ans;
258
269
  else
259
270
  data = ans;
260
271
  return typeof data === "boolean" ? [data, undefined, undefined] : [false, data[0], data[1]];
261
- };
272
+ });
262
273
  return this;
263
274
  }
264
275
  event(event, callback, validator) {
@@ -292,11 +303,12 @@ export class WebSocketRouteConstructor {
292
303
  }
293
304
  requireInit(callback, validator) {
294
305
  this.initRequired = true;
295
- this.on("init", async (socket, data, app, url) => {
306
+ this.on("init", (socket, data, app, url) => __awaiter(this, void 0, void 0, function* () {
307
+ var _a, _b, _c;
296
308
  let [mainData, parsed] = validator ? parse(data, validator) : [data, true];
297
309
  if (!parsed || !mainData) {
298
310
  this.eventEmitter.emit("initParsingFailed", [data, socket, app, validator]);
299
- if (app.config.logging?.fullWs)
311
+ if ((_a = app.config.logging) === null || _a === void 0 ? void 0 : _a.fullWs)
300
312
  app.logger.wsInitFailedFull(url, data);
301
313
  else
302
314
  app.logger.wsInitFailed(url);
@@ -305,9 +317,9 @@ export class WebSocketRouteConstructor {
305
317
  // @ts-ignore
306
318
  let ret = callback(socket, mainData, app);
307
319
  if (ret instanceof Promise)
308
- ret = await ret;
320
+ ret = yield ret;
309
321
  if (ret !== null) {
310
- if (app.config.logging?.fullWs)
322
+ if ((_b = app.config.logging) === null || _b === void 0 ? void 0 : _b.fullWs)
311
323
  app.logger.wsInitFull(url, mainData);
312
324
  else
313
325
  app.logger.wsInit(url);
@@ -315,12 +327,12 @@ export class WebSocketRouteConstructor {
315
327
  }
316
328
  else {
317
329
  this.eventEmitter.emit("initFailed", [data, socket, app]);
318
- if (app.config.logging?.fullWs)
330
+ if ((_c = app.config.logging) === null || _c === void 0 ? void 0 : _c.fullWs)
319
331
  app.logger.wsInitFailedFull(url, mainData);
320
332
  else
321
333
  app.logger.wsInitFailed(url);
322
334
  }
323
- });
335
+ }));
324
336
  return this;
325
337
  }
326
338
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "backendium",
3
- "version": "0.0.9",
3
+ "version": "0.0.11",
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,6 +23,5 @@
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"],
27
- "type": "module"
26
+ "files": ["src", "dist", "package.json", "readme.md", "tsconfig.json"]
28
27
  }
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.globalAuth is string
152
+ console.log(request.globalAuth); // type of request.auth is string
153
153
  response.end();
154
154
  }, {
155
155
  auth: true
package/src/handler.ts CHANGED
@@ -23,7 +23,7 @@ export function defaultErrorHandler(request: Request, response: BackendiumRespon
23
23
 
24
24
  export function defaultValidationErrorHandler(request: Request, response: BackendiumResponse, app: Backendium, data: Buffer, error: ValidationError, message: string) {
25
25
  response.status(400);
26
- response.end(message ?? "Validation failed"); // @TODO
26
+ response.end(message ?? "Validation failed: " + error.message);
27
27
  }
28
28
 
29
29
  export default function backendiumHandler<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>(handler: BackendiumHandlerType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>,
@@ -49,6 +49,8 @@ export default function backendiumHandler<BodyType, ParamsType, QueryType, AuthT
49
49
  if (ret instanceof Promise) ret = await ret;
50
50
  if (ret === null) {
51
51
  (authFailed ?? router.authFailed ?? app.authFailed ?? defaultAuthFailedHandler)(request, res, app);
52
+ if (app.config.logging?.fullRequest) app.logger.requestFull(request.url, res.lastStatus, req.body, res.lastResponse);
53
+ else app.logger.request(request.url, res.lastStatus);
52
54
  return;
53
55
  }
54
56
  authData = ret;
@@ -60,13 +62,19 @@ export default function backendiumHandler<BodyType, ParamsType, QueryType, AuthT
60
62
  if (ret instanceof Promise) ret = await ret;
61
63
  if (ret === null) {
62
64
  (authFailed ?? router.authFailed ?? app.authFailed ?? defaultAuthFailedHandler)(request, res, app);
65
+ if (app.config.logging?.fullRequest) app.logger.requestFull(request.url, res.lastStatus, req.body, res.lastResponse);
66
+ else app.logger.request(request.url, res.lastStatus);
63
67
  return;
64
68
  }
65
69
  globalAuthData = ret;
66
70
  }
67
71
  let ret = handler({...req, auth: authData, globalAuth: globalAuthData}, res, app, next);
68
72
  if (ret instanceof Promise) ret = await ret;
69
- if (!ret) return;
73
+ if (!ret) {
74
+ if (app.config.logging?.fullRequest) app.logger.requestFull(request.url, res.lastStatus, req.body, res.lastResponse);
75
+ else app.logger.request(request.url, res.lastStatus);
76
+ return;
77
+ }
70
78
  let {code = 200, next: isNext = false} = ret;
71
79
  response.status(code);
72
80
  if (isNext) {
@@ -74,7 +82,7 @@ export default function backendiumHandler<BodyType, ParamsType, QueryType, AuthT
74
82
  return;
75
83
  }
76
84
  try {response.end();}
77
- catch (error) {return;}
85
+ catch (error) {}
78
86
  if (app.config.logging?.fullRequest) app.logger.requestFull(request.url, res.lastStatus, req.body, res.lastResponse);
79
87
  else app.logger.request(request.url, res.lastStatus);
80
88
  }