backendium 0.0.3 → 0.0.4

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/index.d.ts CHANGED
@@ -50,3 +50,5 @@ export default class Backendium<GlobalAuthType = any> extends BackendiumRouter<G
50
50
  start(callback?: (server: Server) => void): Server;
51
51
  startAsync(): Promise<Server>;
52
52
  }
53
+ export * from "./ws";
54
+ export * from "./router";
package/dist/index.js CHANGED
@@ -85,3 +85,5 @@ export default class Backendium extends BackendiumRouter {
85
85
  });
86
86
  }
87
87
  }
88
+ export * from "./ws";
89
+ export * from "./router";
package/dist/router.d.ts CHANGED
@@ -52,7 +52,7 @@ export declare class BackendiumRouter<GlobalAuthType = undefined> {
52
52
  unlock<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>): void;
53
53
  link<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>): void;
54
54
  unlink<BodyType = Buffer, ParamsType = {}, QueryType = {}, AuthType = GlobalAuthType, HeadersType = {}>(...args: BackendiumMethodArgsType<BodyType, ParamsType, QueryType, AuthType, HeadersType, GlobalAuthType>): void;
55
- ws<InitDataType>(route: string): WebSocketRouteConstructor<InitDataType>;
55
+ ws<InitDataType>(route: string, constructor: WebSocketRouteConstructor<InitDataType>): void;
56
56
  protected static addPrefix([method, route, handler]: [MethodType, string | undefined, Array<(app: Backendium) => RequestHandler>], prefix: string): [MethodType, string, Array<(app: Backendium) => RequestHandler>];
57
57
  protected static addPrefix([method, route, handler]: ["ws", string, Array<(app: Backendium) => WSRequestHandler>], prefix: string): ["ws", string, Array<(app: Backendium) => WSRequestHandler>];
58
58
  router<AuthType>(router: BackendiumRouter<AuthType>, routePrefix?: string): void;
package/dist/router.js CHANGED
@@ -1,5 +1,4 @@
1
1
  import backendiumHandler from "./handler.js";
2
- import { WebSocketRouteConstructor } from "./ws.js";
3
2
  export class BackendiumRouter {
4
3
  constructor() {
5
4
  this._handlers = [];
@@ -115,12 +114,10 @@ export class BackendiumRouter {
115
114
  unlink(...args) {
116
115
  this.addHandler("unlink", ...args);
117
116
  }
118
- ws(route) {
119
- const constructor = new WebSocketRouteConstructor();
117
+ ws(route, constructor) {
120
118
  this._handlers.push(["ws", route, [(app) => (request, response, next) => {
121
119
  constructor._handle(request, response, next, app);
122
120
  }]]);
123
- return constructor;
124
121
  }
125
122
  static addPrefix([method, route, handler], prefix) {
126
123
  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];
package/dist/ws.d.ts CHANGED
@@ -96,6 +96,7 @@ export declare class WebSocketRouteConstructor<InitDataType> {
96
96
  on<E extends EventKey<BackendiumWebSocketEvents<InitDataType>>>(event: E, subscriber: (...args: BackendiumWebSocketEvents<InitDataType>[E]) => void): WebSocketRouteConstructor<InitDataType>;
97
97
  once<E extends EventKey<BackendiumWebSocketEvents<InitDataType>>>(event: E, subscriber: (...args: BackendiumWebSocketEvents<InitDataType>[E]) => void): WebSocketRouteConstructor<InitDataType>;
98
98
  off<E extends EventKey<BackendiumWebSocketEvents<InitDataType>>>(event: E, subscriber: (...args: BackendiumWebSocketEvents<InitDataType>[E]) => void): WebSocketRouteConstructor<InitDataType>;
99
- requireInit<Type>(callback: (connection: WebSocket & WebSocketExtension, data: Type, app: Backendium) => null | InitDataType | Promise<null | InitDataType>, validator: Validator<Type>): void;
99
+ requireInit<Type>(callback: (connection: WebSocket & WebSocketExtension, data: Type, app: Backendium) => null | InitDataType | Promise<null | InitDataType>, validator: Validator<Type>): WebSocketRouteConstructor<InitDataType>;
100
100
  }
101
+ export declare function websocketConstructor<InitDataType = undefined>(): WebSocketRouteConstructor<InitDataType>;
101
102
  export {};
package/dist/ws.js CHANGED
@@ -333,6 +333,10 @@ export class WebSocketRouteConstructor {
333
333
  app.logger.wsInitFailed(url);
334
334
  }
335
335
  }));
336
+ return this;
336
337
  }
337
338
  }
339
+ export function websocketConstructor() {
340
+ return new WebSocketRouteConstructor;
341
+ }
338
342
  // @TODO error handling +termination logging
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "backendium",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
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",
package/readme.md CHANGED
@@ -114,30 +114,38 @@ curl http://localhost:8080/validated/headers -H "n:2"
114
114
  ```
115
115
  # Authorization
116
116
  ```typescript
117
+ const USERS: {[key: number]: string} = {
118
+ 54: "sizoff"
119
+ }
120
+
117
121
  router.post<Buffer, {}, {}, string>("/auth", (request, response) => {
118
122
  console.log(request.auth); // type of request.auth is string
119
123
  response.end();
120
124
  }, {
121
125
  authChecker(request, response) {
122
- if (typeof request.headers.auth !== "string" || request.headers.auth !== "backendium") return null; // auth failed
123
- return request.headers.auth; // return auth data
126
+ if (typeof request.headers.auth !== "string" || !(Number(request.headers.auth) in USERS)) return null; // auth failed
127
+ return USERS[Number(request.headers.auth)]; // return auth data
124
128
  }
125
129
  });
126
130
  ```
127
131
  ```bash
128
- curl http://localhost:8080/auth -H "auth:backendium" -d ""
132
+ curl http://localhost:8080/auth -H "auth:54" -d ""
129
133
  ```
130
134
  ## Global (for router)
131
135
  ```typescript
132
136
  const router = new BackendiumRouter<string>;
133
137
 
138
+ const USERS: {[key: number]: string} = {
139
+ 54: "sizoff"
140
+ }
141
+
134
142
  router.setAuth((request, response) => {
135
- if (typeof request.headers.auth !== "string" || request.headers.auth !== "backendium") return null; // auth failed
136
- return request.headers.auth; // return auth data
143
+ if (typeof request.headers.auth !== "string" || !(Number(request.headers.auth) in USERS)) return null; // auth failed
144
+ return USERS[Number(request.headers.auth)]; // return auth data
137
145
  });
138
146
 
139
147
  router.post("/auth", (request, response) => {
140
- console.log(request.globalAuth); // type of request.globalAuth is string
148
+ console.log(request.globalAuth); // type of request.auth is string
141
149
  response.end();
142
150
  }, {
143
151
  auth: true
@@ -193,7 +201,7 @@ router.ws("/ws")
193
201
  .event<number>("sqrt", (data, socket) => {
194
202
  console.log(data);
195
203
  socket.emit("response", Math.sqrt(data));
196
- }, int())
204
+ }, int());
197
205
  ```
198
206
  only Backendium connect
199
207
  ```typescript
@@ -203,4 +211,25 @@ websocketRequest()
203
211
  .then(socket => {
204
212
  socket.emit("sqrt", 2);
205
213
  });
214
+ ```
215
+ ## Init
216
+ ```typescript
217
+ router.ws<string>("/ws/init")
218
+ .event("test", (data, socket) => {
219
+ socket.send(socket.initData);
220
+ })
221
+ .requireInit<number>((socket, data) => {
222
+ if (!(data in USERS)) return null; // auth failed
223
+ return USERS[data]; // return auth data
224
+ }, int());
225
+ ```
226
+ ```typescript
227
+ websocketRequest<number>()
228
+ .on("message", data => {
229
+ console.log(data.toString());
230
+ })
231
+ .send("ws://localhost:8080/ws/init", 54)
232
+ .then(socket => {
233
+ socket.emit("test");
234
+ });
206
235
  ```
package/src/index.ts CHANGED
@@ -106,4 +106,7 @@ export default class Backendium<GlobalAuthType = any> extends BackendiumRouter<G
106
106
  this.start(resolve);
107
107
  });
108
108
  }
109
- }
109
+ }
110
+
111
+ export * from "./ws";
112
+ export * from "./router";
package/src/router.ts CHANGED
@@ -200,12 +200,10 @@ export class BackendiumRouter<GlobalAuthType = undefined> {
200
200
  this.addHandler("unlink", ...args);
201
201
  }
202
202
 
203
- ws<InitDataType>(route: string): WebSocketRouteConstructor<InitDataType> {
204
- const constructor = new WebSocketRouteConstructor<InitDataType>();
203
+ ws<InitDataType>(route: string, constructor: WebSocketRouteConstructor<InitDataType>) {
205
204
  this._handlers.push(["ws", route, [(app: Backendium) => (request: Request, response: WSResponse, next: NextFunction) => {
206
205
  constructor._handle(request, response, next, app);
207
206
  }]]);
208
- return constructor;
209
207
  }
210
208
 
211
209
  protected static addPrefix([method, route, handler]: [MethodType, string | undefined, Array<(app: Backendium) => RequestHandler>], prefix: string): [MethodType, string, Array<(app: Backendium) => RequestHandler>];
package/src/ws.ts CHANGED
@@ -341,7 +341,7 @@ export class WebSocketRouteConstructor<InitDataType> {
341
341
  return this;
342
342
  }
343
343
 
344
- public requireInit<Type>(callback: (connection: WebSocket & WebSocketExtension, data: Type, app: Backendium) => null | InitDataType | Promise<null | InitDataType>, validator: Validator<Type>) {
344
+ public requireInit<Type>(callback: (connection: WebSocket & WebSocketExtension, data: Type, app: Backendium) => null | InitDataType | Promise<null | InitDataType>, validator: Validator<Type>): WebSocketRouteConstructor<InitDataType> {
345
345
  this.initRequired = true;
346
346
  this.on("init", async (socket, data, app, url) => {
347
347
  let [mainData, parsed] = validator ? parse(data, validator) : [data, true];
@@ -365,7 +365,12 @@ export class WebSocketRouteConstructor<InitDataType> {
365
365
  else app.logger.wsInitFailed(url);
366
366
  }
367
367
  });
368
+ return this;
368
369
  }
369
370
  }
370
371
 
372
+ export function websocketConstructor<InitDataType = undefined>() {
373
+ return new WebSocketRouteConstructor<InitDataType>;
374
+ }
375
+
371
376
  // @TODO error handling +termination logging