backendium 0.0.3 → 0.0.5
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 +2 -0
- package/dist/index.js +2 -0
- package/dist/router.js +1 -1
- package/dist/ws.d.ts +1 -1
- package/dist/ws.js +1 -0
- package/package.json +1 -1
- package/readme.md +36 -7
- package/src/index.ts +4 -1
- package/src/router.ts +2 -2
- package/src/ws.ts +2 -1
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/router.js
CHANGED
|
@@ -116,7 +116,7 @@ export class BackendiumRouter {
|
|
|
116
116
|
this.addHandler("unlink", ...args);
|
|
117
117
|
}
|
|
118
118
|
ws(route) {
|
|
119
|
-
|
|
119
|
+
let constructor = new WebSocketRouteConstructor;
|
|
120
120
|
this._handlers.push(["ws", route, [(app) => (request, response, next) => {
|
|
121
121
|
constructor._handle(request, response, next, app);
|
|
122
122
|
}]]);
|
package/dist/ws.d.ts
CHANGED
|
@@ -96,6 +96,6 @@ 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>):
|
|
99
|
+
requireInit<Type>(callback: (connection: WebSocket & WebSocketExtension, data: Type, app: Backendium) => null | InitDataType | Promise<null | InitDataType>, validator: Validator<Type>): WebSocketRouteConstructor<InitDataType>;
|
|
100
100
|
}
|
|
101
101
|
export {};
|
package/dist/ws.js
CHANGED
package/package.json
CHANGED
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
|
|
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:
|
|
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
|
|
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.
|
|
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
package/src/router.ts
CHANGED
|
@@ -200,8 +200,8 @@ export class BackendiumRouter<GlobalAuthType = undefined> {
|
|
|
200
200
|
this.addHandler("unlink", ...args);
|
|
201
201
|
}
|
|
202
202
|
|
|
203
|
-
ws<InitDataType>(route: string)
|
|
204
|
-
|
|
203
|
+
ws<InitDataType>(route: string) {
|
|
204
|
+
let constructor = new WebSocketRouteConstructor<InitDataType>;
|
|
205
205
|
this._handlers.push(["ws", route, [(app: Backendium) => (request: Request, response: WSResponse, next: NextFunction) => {
|
|
206
206
|
constructor._handle(request, response, next, app);
|
|
207
207
|
}]]);
|
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,6 +365,7 @@ export class WebSocketRouteConstructor<InitDataType> {
|
|
|
365
365
|
else app.logger.wsInitFailed(url);
|
|
366
366
|
}
|
|
367
367
|
});
|
|
368
|
+
return this;
|
|
368
369
|
}
|
|
369
370
|
}
|
|
370
371
|
|