backendium 0.0.2 → 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/logger.js CHANGED
@@ -137,7 +137,7 @@ export default class Logger {
137
137
  }
138
138
  wsInit(url) {
139
139
  let prefix = this.getPrefix("wsInit");
140
- this.logSeparately([chalk.green(prefix), chalk.green("Websocket init on url"), chalk.cyan(url), chalk.red("done")], [prefix, "Websocket init on url", url, "done"]);
140
+ this.logSeparately([chalk.green(prefix), chalk.green("Websocket init on url"), chalk.cyan(url), chalk.green("done")], [prefix, "Websocket init on url", url, "done"]);
141
141
  }
142
142
  wsInitFailed(url) {
143
143
  let prefix = this.getPrefix("wsInitFailed");
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.2",
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
@@ -166,11 +174,62 @@ router.ws("/ws")
166
174
  socket.send(data);
167
175
  });
168
176
  ```
169
- request:
177
+ ### js build-in websockets:
170
178
  ```javascript
171
179
  const connection = new WebSocket("ws://localhost:8080/ws");
172
180
  connection.send("test");
173
181
  connection.onmessage = (message) => {
174
182
  console.log(message.data);
175
183
  };
184
+ ```
185
+ ### [Backendium connect](https://github.com/vssizoff/backendiumConnect)
186
+ ```typescript
187
+ import {websocketRequest} from "backendium-connect";
188
+
189
+ websocketRequest()
190
+ .on("message", (data, socket) => {
191
+ console.log(data); // data is Buffer
192
+ })
193
+ .send("ws://localhost:8080/ws")
194
+ .then(socket => {
195
+ socket.send("test");
196
+ });
197
+ ```
198
+ ## Events
199
+ ```typescript
200
+ router.ws("/ws")
201
+ .event<number>("sqrt", (data, socket) => {
202
+ console.log(data);
203
+ socket.emit("response", Math.sqrt(data));
204
+ }, int());
205
+ ```
206
+ only Backendium connect
207
+ ```typescript
208
+ websocketRequest()
209
+ .event<number>("response", data => console.log(data), float())
210
+ .send("ws://localhost:8080/ws")
211
+ .then(socket => {
212
+ socket.emit("sqrt", 2);
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
+ });
176
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/logger.ts CHANGED
@@ -164,7 +164,7 @@ export default class Logger {
164
164
 
165
165
  wsInit(url: string) {
166
166
  let prefix = this.getPrefix("wsInit");
167
- this.logSeparately([chalk.green(prefix), chalk.green("Websocket init on url"), chalk.cyan(url), chalk.red("done")],
167
+ this.logSeparately([chalk.green(prefix), chalk.green("Websocket init on url"), chalk.cyan(url), chalk.green("done")],
168
168
  [prefix, "Websocket init on url", url, "done"]);
169
169
  }
170
170
 
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