@webtypen/webframez-core 0.0.16 → 0.0.18
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/lib/Router/Router.d.ts +1 -1
- package/lib/Router/Router.js +47 -38
- package/package.json +1 -1
- package/src/Router/Router.ts +50 -43
package/lib/Router/Router.d.ts
CHANGED
|
@@ -103,7 +103,7 @@ declare class RouterFacade {
|
|
|
103
103
|
* @param res
|
|
104
104
|
*/
|
|
105
105
|
handleRequest(req: IncomingMessage, res: ServerResponse): Promise<void>;
|
|
106
|
-
requestConsoleLog(req: IncomingMessage): void;
|
|
106
|
+
requestConsoleLog(req: IncomingMessage, httpCode: number): void;
|
|
107
107
|
}
|
|
108
108
|
export declare const Router: RouterFacade;
|
|
109
109
|
export {};
|
package/lib/Router/Router.js
CHANGED
|
@@ -51,9 +51,7 @@ class RouterFacade {
|
|
|
51
51
|
this.basename = options && options.basename ? options.basename : null;
|
|
52
52
|
this.kernel = options && options.kernel ? options.kernel : null;
|
|
53
53
|
// Load routes
|
|
54
|
-
console.log("loadRoutes");
|
|
55
54
|
require(process.cwd() + "/app/routes");
|
|
56
|
-
console.log("finishRoutes");
|
|
57
55
|
}
|
|
58
56
|
/**
|
|
59
57
|
* Register a route
|
|
@@ -177,12 +175,43 @@ class RouterFacade {
|
|
|
177
175
|
*/
|
|
178
176
|
handleRequest(req, res) {
|
|
179
177
|
return __awaiter(this, void 0, void 0, function* () {
|
|
180
|
-
const route = this.dissolve(req);
|
|
181
|
-
const response = new Response_1.Response();
|
|
182
|
-
response.setServerResponse(res);
|
|
183
178
|
if (!this.kernel) {
|
|
179
|
+
res.end();
|
|
184
180
|
throw new Error("Missing Kernel in Router ...");
|
|
185
181
|
}
|
|
182
|
+
const route = this.dissolve(req);
|
|
183
|
+
const response = new Response_1.Response();
|
|
184
|
+
response.setServerResponse(res);
|
|
185
|
+
// Load Body
|
|
186
|
+
let body = [];
|
|
187
|
+
const bodyPlain = yield new Promise((resolve) => {
|
|
188
|
+
req
|
|
189
|
+
.on("data", (chunk) => {
|
|
190
|
+
body.push(chunk);
|
|
191
|
+
})
|
|
192
|
+
.on("end", () => {
|
|
193
|
+
resolve(Buffer.concat(body).toString());
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
const request = {
|
|
197
|
+
body: bodyPlain &&
|
|
198
|
+
req.headers["content-type"] === "application/json" &&
|
|
199
|
+
(bodyPlain.trim().substring(0, 1) === "[" ||
|
|
200
|
+
bodyPlain.trim().substring(0, 1) === "{")
|
|
201
|
+
? JSON.parse(bodyPlain)
|
|
202
|
+
: bodyPlain
|
|
203
|
+
? qs_1.default.parse(bodyPlain)
|
|
204
|
+
: {},
|
|
205
|
+
bodyPlain: bodyPlain,
|
|
206
|
+
params: route && route.params ? route.params : {},
|
|
207
|
+
httpVersionMajor: req.httpVersionMajor,
|
|
208
|
+
httpVersionMinor: req.httpVersionMinor,
|
|
209
|
+
httpVersion: req.httpVersion,
|
|
210
|
+
headers: req.headers,
|
|
211
|
+
rawHeaders: req.rawHeaders,
|
|
212
|
+
url: req.url,
|
|
213
|
+
method: req.method,
|
|
214
|
+
};
|
|
186
215
|
// Check-Middleware
|
|
187
216
|
if (route &&
|
|
188
217
|
route.options &&
|
|
@@ -191,6 +220,7 @@ class RouterFacade {
|
|
|
191
220
|
for (let middlewareKey of route.options.middleware) {
|
|
192
221
|
const middleware = this.kernel.middleware[middlewareKey];
|
|
193
222
|
if (!middleware) {
|
|
223
|
+
this.requestConsoleLog(req, 500);
|
|
194
224
|
return this.sendError(res, 500, "Unknown middleware `" + middlewareKey + "` ...");
|
|
195
225
|
}
|
|
196
226
|
try {
|
|
@@ -198,10 +228,11 @@ class RouterFacade {
|
|
|
198
228
|
try {
|
|
199
229
|
middleware(resolve, (status, data) => {
|
|
200
230
|
reject({ status: status, data: data });
|
|
201
|
-
},
|
|
231
|
+
}, request, response);
|
|
202
232
|
}
|
|
203
233
|
catch (e) {
|
|
204
234
|
console.error(e);
|
|
235
|
+
this.requestConsoleLog(req, 500);
|
|
205
236
|
return this.sendError(res, 500, "Error in middleware `" + middlewareKey + "` ...");
|
|
206
237
|
}
|
|
207
238
|
});
|
|
@@ -225,6 +256,7 @@ class RouterFacade {
|
|
|
225
256
|
res.statusCode = 200;
|
|
226
257
|
res.setHeader("Content-Length", "0");
|
|
227
258
|
res.end();
|
|
259
|
+
this.requestConsoleLog(req, 200);
|
|
228
260
|
return;
|
|
229
261
|
}
|
|
230
262
|
// Resolve route-component (handle controller definition)
|
|
@@ -241,50 +273,25 @@ class RouterFacade {
|
|
|
241
273
|
route.component = controllerInstance[split[1]];
|
|
242
274
|
}
|
|
243
275
|
if (route && route.component !== undefined) {
|
|
244
|
-
let body = [];
|
|
245
|
-
const bodyPlain = yield new Promise((resolve) => {
|
|
246
|
-
req
|
|
247
|
-
.on("data", (chunk) => {
|
|
248
|
-
body.push(chunk);
|
|
249
|
-
})
|
|
250
|
-
.on("end", () => {
|
|
251
|
-
resolve(Buffer.concat(body).toString());
|
|
252
|
-
});
|
|
253
|
-
});
|
|
254
276
|
try {
|
|
255
|
-
yield route.component(
|
|
256
|
-
body: bodyPlain &&
|
|
257
|
-
req.headers["content-type"] === "application/json" &&
|
|
258
|
-
(bodyPlain.trim().substring(0, 1) === "[" ||
|
|
259
|
-
bodyPlain.trim().substring(0, 1) === "{")
|
|
260
|
-
? JSON.parse(bodyPlain)
|
|
261
|
-
: bodyPlain
|
|
262
|
-
? qs_1.default.parse(bodyPlain)
|
|
263
|
-
: {},
|
|
264
|
-
bodyPlain: bodyPlain,
|
|
265
|
-
params: route.params,
|
|
266
|
-
httpVersionMajor: req.httpVersionMajor,
|
|
267
|
-
httpVersionMinor: req.httpVersionMinor,
|
|
268
|
-
httpVersion: req.httpVersion,
|
|
269
|
-
headers: req.headers,
|
|
270
|
-
rawHeaders: req.rawHeaders,
|
|
271
|
-
url: req.url,
|
|
272
|
-
method: req.method,
|
|
273
|
-
}, response);
|
|
277
|
+
yield route.component(request, response);
|
|
274
278
|
res.end();
|
|
279
|
+
this.requestConsoleLog(req, 200);
|
|
275
280
|
}
|
|
276
281
|
catch (e) {
|
|
277
282
|
console.error(e);
|
|
278
283
|
this.sendError(res, 500);
|
|
284
|
+
this.requestConsoleLog(req, 500);
|
|
279
285
|
}
|
|
280
286
|
}
|
|
281
287
|
else {
|
|
282
288
|
this.sendError(res, 404);
|
|
289
|
+
this.requestConsoleLog(req, 404);
|
|
283
290
|
}
|
|
284
291
|
});
|
|
285
292
|
}
|
|
286
|
-
requestConsoleLog(req) {
|
|
287
|
-
if (Config_1.Config.get("router.requests.logConsole") !== true) {
|
|
293
|
+
requestConsoleLog(req, httpCode) {
|
|
294
|
+
if (Config_1.Config.get("application.router.requests.logConsole") !== true) {
|
|
288
295
|
return;
|
|
289
296
|
}
|
|
290
297
|
const date = new Date();
|
|
@@ -316,7 +323,9 @@ class RouterFacade {
|
|
|
316
323
|
req.url +
|
|
317
324
|
'", ip: "' +
|
|
318
325
|
ip +
|
|
319
|
-
'"
|
|
326
|
+
'", httpcode: ' +
|
|
327
|
+
httpCode.toString() +
|
|
328
|
+
" }");
|
|
320
329
|
}
|
|
321
330
|
}
|
|
322
331
|
exports.Router = new RouterFacade();
|
package/package.json
CHANGED
package/src/Router/Router.ts
CHANGED
|
@@ -78,9 +78,7 @@ class RouterFacade {
|
|
|
78
78
|
this.kernel = options && options.kernel ? options.kernel : null;
|
|
79
79
|
|
|
80
80
|
// Load routes
|
|
81
|
-
console.log("loadRoutes");
|
|
82
81
|
require(process.cwd() + "/app/routes");
|
|
83
|
-
console.log("finishRoutes");
|
|
84
82
|
}
|
|
85
83
|
|
|
86
84
|
/**
|
|
@@ -207,13 +205,47 @@ class RouterFacade {
|
|
|
207
205
|
* @param res
|
|
208
206
|
*/
|
|
209
207
|
async handleRequest(req: IncomingMessage, res: ServerResponse) {
|
|
208
|
+
if (!this.kernel) {
|
|
209
|
+
res.end();
|
|
210
|
+
throw new Error("Missing Kernel in Router ...");
|
|
211
|
+
}
|
|
212
|
+
|
|
210
213
|
const route = this.dissolve(req);
|
|
211
214
|
const response = new Response();
|
|
212
215
|
response.setServerResponse(res);
|
|
213
216
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
+
// Load Body
|
|
218
|
+
let body: any = [];
|
|
219
|
+
const bodyPlain: any = await new Promise((resolve: Function) => {
|
|
220
|
+
req
|
|
221
|
+
.on("data", (chunk) => {
|
|
222
|
+
body.push(chunk);
|
|
223
|
+
})
|
|
224
|
+
.on("end", () => {
|
|
225
|
+
resolve(Buffer.concat(body).toString());
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
const request = {
|
|
230
|
+
body:
|
|
231
|
+
bodyPlain &&
|
|
232
|
+
req.headers["content-type"] === "application/json" &&
|
|
233
|
+
(bodyPlain.trim().substring(0, 1) === "[" ||
|
|
234
|
+
bodyPlain.trim().substring(0, 1) === "{")
|
|
235
|
+
? JSON.parse(bodyPlain)
|
|
236
|
+
: bodyPlain
|
|
237
|
+
? qs.parse(bodyPlain)
|
|
238
|
+
: {},
|
|
239
|
+
bodyPlain: bodyPlain,
|
|
240
|
+
params: route && route.params ? route.params : {},
|
|
241
|
+
httpVersionMajor: req.httpVersionMajor,
|
|
242
|
+
httpVersionMinor: req.httpVersionMinor,
|
|
243
|
+
httpVersion: req.httpVersion,
|
|
244
|
+
headers: req.headers,
|
|
245
|
+
rawHeaders: req.rawHeaders,
|
|
246
|
+
url: req.url,
|
|
247
|
+
method: req.method,
|
|
248
|
+
};
|
|
217
249
|
|
|
218
250
|
// Check-Middleware
|
|
219
251
|
if (
|
|
@@ -225,6 +257,7 @@ class RouterFacade {
|
|
|
225
257
|
for (let middlewareKey of route.options.middleware) {
|
|
226
258
|
const middleware = this.kernel.middleware[middlewareKey];
|
|
227
259
|
if (!middleware) {
|
|
260
|
+
this.requestConsoleLog(req, 500);
|
|
228
261
|
return this.sendError(
|
|
229
262
|
res,
|
|
230
263
|
500,
|
|
@@ -240,12 +273,13 @@ class RouterFacade {
|
|
|
240
273
|
(status: number, data: any) => {
|
|
241
274
|
reject({ status: status, data: data });
|
|
242
275
|
},
|
|
243
|
-
|
|
276
|
+
request,
|
|
244
277
|
response
|
|
245
278
|
);
|
|
246
279
|
} catch (e) {
|
|
247
280
|
console.error(e);
|
|
248
281
|
|
|
282
|
+
this.requestConsoleLog(req, 500);
|
|
249
283
|
return this.sendError(
|
|
250
284
|
res,
|
|
251
285
|
500,
|
|
@@ -278,6 +312,7 @@ class RouterFacade {
|
|
|
278
312
|
res.statusCode = 200;
|
|
279
313
|
res.setHeader("Content-Length", "0");
|
|
280
314
|
res.end();
|
|
315
|
+
this.requestConsoleLog(req, 200);
|
|
281
316
|
return;
|
|
282
317
|
}
|
|
283
318
|
|
|
@@ -297,53 +332,23 @@ class RouterFacade {
|
|
|
297
332
|
}
|
|
298
333
|
|
|
299
334
|
if (route && route.component !== undefined) {
|
|
300
|
-
let body: any = [];
|
|
301
|
-
const bodyPlain: any = await new Promise((resolve: Function) => {
|
|
302
|
-
req
|
|
303
|
-
.on("data", (chunk) => {
|
|
304
|
-
body.push(chunk);
|
|
305
|
-
})
|
|
306
|
-
.on("end", () => {
|
|
307
|
-
resolve(Buffer.concat(body).toString());
|
|
308
|
-
});
|
|
309
|
-
});
|
|
310
|
-
|
|
311
335
|
try {
|
|
312
|
-
await route.component(
|
|
313
|
-
{
|
|
314
|
-
body:
|
|
315
|
-
bodyPlain &&
|
|
316
|
-
req.headers["content-type"] === "application/json" &&
|
|
317
|
-
(bodyPlain.trim().substring(0, 1) === "[" ||
|
|
318
|
-
bodyPlain.trim().substring(0, 1) === "{")
|
|
319
|
-
? JSON.parse(bodyPlain)
|
|
320
|
-
: bodyPlain
|
|
321
|
-
? qs.parse(bodyPlain)
|
|
322
|
-
: {},
|
|
323
|
-
bodyPlain: bodyPlain,
|
|
324
|
-
params: route.params,
|
|
325
|
-
httpVersionMajor: req.httpVersionMajor,
|
|
326
|
-
httpVersionMinor: req.httpVersionMinor,
|
|
327
|
-
httpVersion: req.httpVersion,
|
|
328
|
-
headers: req.headers,
|
|
329
|
-
rawHeaders: req.rawHeaders,
|
|
330
|
-
url: req.url,
|
|
331
|
-
method: req.method,
|
|
332
|
-
},
|
|
333
|
-
response
|
|
334
|
-
);
|
|
336
|
+
await route.component(request, response);
|
|
335
337
|
res.end();
|
|
338
|
+
this.requestConsoleLog(req, 200);
|
|
336
339
|
} catch (e) {
|
|
337
340
|
console.error(e);
|
|
338
341
|
this.sendError(res, 500);
|
|
342
|
+
this.requestConsoleLog(req, 500);
|
|
339
343
|
}
|
|
340
344
|
} else {
|
|
341
345
|
this.sendError(res, 404);
|
|
346
|
+
this.requestConsoleLog(req, 404);
|
|
342
347
|
}
|
|
343
348
|
}
|
|
344
349
|
|
|
345
|
-
requestConsoleLog(req: IncomingMessage) {
|
|
346
|
-
if (Config.get("router.requests.logConsole") !== true) {
|
|
350
|
+
requestConsoleLog(req: IncomingMessage, httpCode: number) {
|
|
351
|
+
if (Config.get("application.router.requests.logConsole") !== true) {
|
|
347
352
|
return;
|
|
348
353
|
}
|
|
349
354
|
|
|
@@ -378,7 +383,9 @@ class RouterFacade {
|
|
|
378
383
|
req.url +
|
|
379
384
|
'", ip: "' +
|
|
380
385
|
ip +
|
|
381
|
-
'"
|
|
386
|
+
'", httpcode: ' +
|
|
387
|
+
httpCode.toString() +
|
|
388
|
+
" }"
|
|
382
389
|
);
|
|
383
390
|
}
|
|
384
391
|
}
|