@webtypen/webframez-core 0.0.15 → 0.0.17

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.
@@ -1,4 +1,5 @@
1
1
  export type Request = {
2
+ [key: string]: any;
2
3
  body?: any;
3
4
  bodyPlain?: any;
4
5
  params: object;
@@ -177,12 +177,42 @@ class RouterFacade {
177
177
  */
178
178
  handleRequest(req, res) {
179
179
  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
180
  if (!this.kernel) {
184
181
  throw new Error("Missing Kernel in Router ...");
185
182
  }
183
+ const route = this.dissolve(req);
184
+ const response = new Response_1.Response();
185
+ response.setServerResponse(res);
186
+ // Load Body
187
+ let body = [];
188
+ const bodyPlain = yield new Promise((resolve) => {
189
+ req
190
+ .on("data", (chunk) => {
191
+ body.push(chunk);
192
+ })
193
+ .on("end", () => {
194
+ resolve(Buffer.concat(body).toString());
195
+ });
196
+ });
197
+ const request = {
198
+ body: bodyPlain &&
199
+ req.headers["content-type"] === "application/json" &&
200
+ (bodyPlain.trim().substring(0, 1) === "[" ||
201
+ bodyPlain.trim().substring(0, 1) === "{")
202
+ ? JSON.parse(bodyPlain)
203
+ : bodyPlain
204
+ ? qs_1.default.parse(bodyPlain)
205
+ : {},
206
+ bodyPlain: bodyPlain,
207
+ params: route && route.params ? route.params : {},
208
+ httpVersionMajor: req.httpVersionMajor,
209
+ httpVersionMinor: req.httpVersionMinor,
210
+ httpVersion: req.httpVersion,
211
+ headers: req.headers,
212
+ rawHeaders: req.rawHeaders,
213
+ url: req.url,
214
+ method: req.method,
215
+ };
186
216
  // Check-Middleware
187
217
  if (route &&
188
218
  route.options &&
@@ -198,7 +228,7 @@ class RouterFacade {
198
228
  try {
199
229
  middleware(resolve, (status, data) => {
200
230
  reject({ status: status, data: data });
201
- }, req, response);
231
+ }, request, response);
202
232
  }
203
233
  catch (e) {
204
234
  console.error(e);
@@ -241,36 +271,8 @@ class RouterFacade {
241
271
  route.component = controllerInstance[split[1]];
242
272
  }
243
273
  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
274
  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);
275
+ yield route.component(request, response);
274
276
  res.end();
275
277
  }
276
278
  catch (e) {
@@ -287,6 +289,36 @@ class RouterFacade {
287
289
  if (Config_1.Config.get("router.requests.logConsole") !== true) {
288
290
  return;
289
291
  }
292
+ const date = new Date();
293
+ const month = date.getMonth() + 1;
294
+ const ip = req.headers["x-forwarded-for"]
295
+ ? req.headers["x-forwarded-for"]
296
+ : req.headers["x-forwarded"]
297
+ ? req.headers["x-forwarded"]
298
+ : req.headers["forwarded-for"]
299
+ ? req.headers["forwarded-for"]
300
+ : req.socket && req.socket.remoteAddress
301
+ ? req.socket.remoteAddress
302
+ : null;
303
+ console.log('{ date: "' +
304
+ date.getFullYear() +
305
+ "-" +
306
+ (month < 10 ? "0" + month : month) +
307
+ "-" +
308
+ date.getDate() +
309
+ " " +
310
+ (date.getHours() + 1) +
311
+ ":" +
312
+ date.getMinutes() +
313
+ ":" +
314
+ date.getSeconds() +
315
+ '", method: "' +
316
+ req.method +
317
+ '", url: "' +
318
+ req.url +
319
+ '", ip: "' +
320
+ ip +
321
+ '" }');
290
322
  }
291
323
  }
292
324
  exports.Router = new RouterFacade();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webtypen/webframez-core",
3
- "version": "0.0.15",
3
+ "version": "0.0.17",
4
4
  "description": "The core package of the typescript based webframez (web-framework) by webtypen.de",
5
5
  "homepage": "https://webtypen.de/",
6
6
  "license": "MIT",
@@ -1,4 +1,5 @@
1
1
  export type Request = {
2
+ [key: string]: any;
2
3
  body?: any;
3
4
  bodyPlain?: any;
4
5
  params: object;
@@ -207,13 +207,46 @@ class RouterFacade {
207
207
  * @param res
208
208
  */
209
209
  async handleRequest(req: IncomingMessage, res: ServerResponse) {
210
+ if (!this.kernel) {
211
+ throw new Error("Missing Kernel in Router ...");
212
+ }
213
+
210
214
  const route = this.dissolve(req);
211
215
  const response = new Response();
212
216
  response.setServerResponse(res);
213
217
 
214
- if (!this.kernel) {
215
- throw new Error("Missing Kernel in Router ...");
216
- }
218
+ // Load Body
219
+ let body: any = [];
220
+ const bodyPlain: any = await new Promise((resolve: Function) => {
221
+ req
222
+ .on("data", (chunk) => {
223
+ body.push(chunk);
224
+ })
225
+ .on("end", () => {
226
+ resolve(Buffer.concat(body).toString());
227
+ });
228
+ });
229
+
230
+ const request = {
231
+ body:
232
+ bodyPlain &&
233
+ req.headers["content-type"] === "application/json" &&
234
+ (bodyPlain.trim().substring(0, 1) === "[" ||
235
+ bodyPlain.trim().substring(0, 1) === "{")
236
+ ? JSON.parse(bodyPlain)
237
+ : bodyPlain
238
+ ? qs.parse(bodyPlain)
239
+ : {},
240
+ bodyPlain: bodyPlain,
241
+ params: route && route.params ? route.params : {},
242
+ httpVersionMajor: req.httpVersionMajor,
243
+ httpVersionMinor: req.httpVersionMinor,
244
+ httpVersion: req.httpVersion,
245
+ headers: req.headers,
246
+ rawHeaders: req.rawHeaders,
247
+ url: req.url,
248
+ method: req.method,
249
+ };
217
250
 
218
251
  // Check-Middleware
219
252
  if (
@@ -240,7 +273,7 @@ class RouterFacade {
240
273
  (status: number, data: any) => {
241
274
  reject({ status: status, data: data });
242
275
  },
243
- req,
276
+ request,
244
277
  response
245
278
  );
246
279
  } catch (e) {
@@ -297,41 +330,8 @@ class RouterFacade {
297
330
  }
298
331
 
299
332
  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
333
  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
- );
334
+ await route.component(request, response);
335
335
  res.end();
336
336
  } catch (e) {
337
337
  console.error(e);
@@ -346,6 +346,40 @@ class RouterFacade {
346
346
  if (Config.get("router.requests.logConsole") !== true) {
347
347
  return;
348
348
  }
349
+
350
+ const date = new Date();
351
+ const month = date.getMonth() + 1;
352
+ const ip = req.headers["x-forwarded-for"]
353
+ ? req.headers["x-forwarded-for"]
354
+ : req.headers["x-forwarded"]
355
+ ? req.headers["x-forwarded"]
356
+ : req.headers["forwarded-for"]
357
+ ? req.headers["forwarded-for"]
358
+ : req.socket && req.socket.remoteAddress
359
+ ? req.socket.remoteAddress
360
+ : null;
361
+
362
+ console.log(
363
+ '{ date: "' +
364
+ date.getFullYear() +
365
+ "-" +
366
+ (month < 10 ? "0" + month : month) +
367
+ "-" +
368
+ date.getDate() +
369
+ " " +
370
+ (date.getHours() + 1) +
371
+ ":" +
372
+ date.getMinutes() +
373
+ ":" +
374
+ date.getSeconds() +
375
+ '", method: "' +
376
+ req.method +
377
+ '", url: "' +
378
+ req.url +
379
+ '", ip: "' +
380
+ ip +
381
+ '" }'
382
+ );
349
383
  }
350
384
  }
351
385