@webtypen/webframez-core 0.0.43 → 0.0.44

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.
@@ -96,30 +96,35 @@ class RouterFacade {
96
96
  if (!req || !req.url) {
97
97
  return null;
98
98
  }
99
+ const cleanUrl = req.url.indexOf("?") > 0
100
+ ? req.url.substring(0, req.url.indexOf("?"))
101
+ : req.url.indexOf("#") > 0
102
+ ? req.url.substring(0, req.url.indexOf("#"))
103
+ : req.url;
99
104
  if (req.method === "GET") {
100
- return this.dissolveRoute(this.routesGET, req.url);
105
+ return this.dissolveRoute(this.routesGET, cleanUrl);
101
106
  }
102
107
  else if (req.method === "POST") {
103
- return this.dissolveRoute(this.routesPOST, req.url);
108
+ return this.dissolveRoute(this.routesPOST, cleanUrl);
104
109
  }
105
110
  else if (req.method === "PUT") {
106
- return this.dissolveRoute(this.routesPUT, req.url);
111
+ return this.dissolveRoute(this.routesPUT, cleanUrl);
107
112
  }
108
113
  else if (req.method === "DELETE") {
109
- return this.dissolveRoute(this.routesDELETE, req.url);
114
+ return this.dissolveRoute(this.routesDELETE, cleanUrl);
110
115
  }
111
116
  else if (req.method === "OPTIONS") {
112
117
  if (req.headers["access-control-request-method"] === "GET") {
113
- return this.dissolveRoute(this.routesGET, req.url);
118
+ return this.dissolveRoute(this.routesGET, cleanUrl);
114
119
  }
115
120
  else if (req.headers["access-control-request-method"] === "POST") {
116
- return this.dissolveRoute(this.routesPOST, req.url);
121
+ return this.dissolveRoute(this.routesPOST, cleanUrl);
117
122
  }
118
123
  else if (req.headers["access-control-request-method"] === "PUT") {
119
- return this.dissolveRoute(this.routesPUT, req.url);
124
+ return this.dissolveRoute(this.routesPUT, cleanUrl);
120
125
  }
121
126
  else if (req.headers["access-control-request-method"] === "DELETE") {
122
- return this.dissolveRoute(this.routesDELETE, req.url);
127
+ return this.dissolveRoute(this.routesDELETE, cleanUrl);
123
128
  }
124
129
  }
125
130
  return null;
@@ -167,7 +172,11 @@ class RouterFacade {
167
172
  */
168
173
  sendError(res, code, message) {
169
174
  res.writeHead(code, { "Content-Type": "text/html" });
170
- res.end(message ? message : code === 404 ? "Oops! Page not found ..." : "Oops! There was an unexpected error ...");
175
+ res.end(message
176
+ ? message
177
+ : code === 404
178
+ ? "Oops! Page not found ..."
179
+ : "Oops! There was an unexpected error ...");
171
180
  }
172
181
  /**
173
182
  * Processes a request
@@ -217,7 +226,8 @@ class RouterFacade {
217
226
  ? {
218
227
  body: bodyPlain &&
219
228
  customRequest.headers["content-type"] === "application/json" &&
220
- (bodyPlain.trim().substring(0, 1) === "[" || bodyPlain.trim().substring(0, 1) === "{")
229
+ (bodyPlain.trim().substring(0, 1) === "[" ||
230
+ bodyPlain.trim().substring(0, 1) === "{")
221
231
  ? JSON.parse(bodyPlain)
222
232
  : bodyPlain
223
233
  ? qs_1.default.parse(options && options.event && options.event.isBase64Encoded
@@ -233,7 +243,8 @@ class RouterFacade {
233
243
  ? {
234
244
  body: bodyPlain &&
235
245
  req.headers["content-type"] === "application/json" &&
236
- (bodyPlain.trim().substring(0, 1) === "[" || bodyPlain.trim().substring(0, 1) === "{")
246
+ (bodyPlain.trim().substring(0, 1) === "[" ||
247
+ bodyPlain.trim().substring(0, 1) === "{")
237
248
  ? JSON.parse(bodyPlain)
238
249
  : bodyPlain
239
250
  ? qs_1.default.parse(bodyPlain)
@@ -251,14 +262,20 @@ class RouterFacade {
251
262
  }
252
263
  : null;
253
264
  // Check-Middleware
254
- if (route && route.options && route.options.middleware && route.options.middleware.length > 0) {
265
+ if (route &&
266
+ route.options &&
267
+ route.options.middleware &&
268
+ route.options.middleware.length > 0) {
255
269
  for (let middlewareKey of route.options.middleware) {
256
270
  const middleware = this.kernel.middleware[middlewareKey];
257
271
  if (!middleware) {
258
272
  if (this.mode === "aws-lambda") {
259
273
  return {
260
274
  statusCode: 500,
261
- body: JSON.stringify({ status: "error", message: "Unknown middleware `" + middlewareKey + "` ..." }),
275
+ body: JSON.stringify({
276
+ status: "error",
277
+ message: "Unknown middleware `" + middlewareKey + "` ...",
278
+ }),
262
279
  headers: response.headers,
263
280
  };
264
281
  }
@@ -279,7 +296,10 @@ class RouterFacade {
279
296
  if (this.mode === "aws-lambda") {
280
297
  return {
281
298
  statusCode: 500,
282
- body: JSON.stringify({ status: "error", message: "Middleware run error `" + middlewareKey + "` ..." }),
299
+ body: JSON.stringify({
300
+ status: "error",
301
+ message: "Middleware run error `" + middlewareKey + "` ...",
302
+ }),
283
303
  headers: response.headers,
284
304
  };
285
305
  }
@@ -293,11 +313,21 @@ class RouterFacade {
293
313
  catch (e) {
294
314
  // Send Middleware-Abort
295
315
  if (this.mode === "aws-lambda") {
296
- return { statusCode: 500, body: JSON.stringify({ status: "error", message: e.data }), headers: response.headers };
316
+ return {
317
+ statusCode: 500,
318
+ body: JSON.stringify({ status: "error", message: e.data }),
319
+ headers: response.headers,
320
+ };
297
321
  }
298
322
  else if (res) {
299
- res.writeHead(e.status, typeof e.data === "object" ? { "Content-Type": "application/json" } : { "Content-Type": "text/html" });
300
- res.end(typeof e.data === "object" ? JSON.stringify(e.data) : e.data ? e.data : " ");
323
+ res.writeHead(e.status, typeof e.data === "object"
324
+ ? { "Content-Type": "application/json" }
325
+ : { "Content-Type": "text/html" });
326
+ res.end(typeof e.data === "object"
327
+ ? JSON.stringify(e.data)
328
+ : e.data
329
+ ? e.data
330
+ : " ");
301
331
  }
302
332
  return;
303
333
  }
@@ -328,7 +358,10 @@ class RouterFacade {
328
358
  if (this.mode === "aws-lambda") {
329
359
  return {
330
360
  statusCode: 500,
331
- body: JSON.stringify({ status: "error", message: "Controller `" + split[0] + "` not found ..." }),
361
+ body: JSON.stringify({
362
+ status: "error",
363
+ message: "Controller `" + split[0] + "` not found ...",
364
+ }),
332
365
  headers: response.headers,
333
366
  };
334
367
  }
@@ -346,7 +379,11 @@ class RouterFacade {
346
379
  try {
347
380
  yield route.component(request, response);
348
381
  if (this.mode === "aws-lambda") {
349
- return { statusCode: response.statusCode, body: JSON.stringify(response.content), headers: response.headers };
382
+ return {
383
+ statusCode: response.statusCode,
384
+ body: JSON.stringify(response.content),
385
+ headers: response.headers,
386
+ };
350
387
  }
351
388
  else if (res && req) {
352
389
  res.end();
@@ -357,7 +394,10 @@ class RouterFacade {
357
394
  if (this.mode === "aws-lambda") {
358
395
  return {
359
396
  statusCode: 500,
360
- body: JSON.stringify({ status: "error", message: "Error running the component ..." }),
397
+ body: JSON.stringify({
398
+ status: "error",
399
+ message: "Error running the component ...",
400
+ }),
361
401
  headers: response.headers,
362
402
  };
363
403
  }
@@ -371,7 +411,10 @@ class RouterFacade {
371
411
  if (this.mode === "aws-lambda") {
372
412
  return {
373
413
  statusCode: 500,
374
- body: JSON.stringify({ status: "error", message: "Route component not found ..." }),
414
+ body: JSON.stringify({
415
+ status: "error",
416
+ message: "Route component not found ...",
417
+ }),
375
418
  headers: response.headers,
376
419
  };
377
420
  }
@@ -383,7 +426,8 @@ class RouterFacade {
383
426
  });
384
427
  }
385
428
  requestConsoleLog(req, httpCode) {
386
- if (Config_1.Config.get("application.router.requests.logConsole") !== true && !Config_1.Config.get("application.router.requests.logFile")) {
429
+ if (Config_1.Config.get("application.router.requests.logConsole") !== true &&
430
+ !Config_1.Config.get("application.router.requests.logFile")) {
387
431
  return;
388
432
  }
389
433
  const date = new Date();
@@ -425,7 +469,9 @@ class RouterFacade {
425
469
  try {
426
470
  const filepath = Config_1.Config.get("application.router.requests.logFile");
427
471
  if (filepath && filepath.trim() !== "") {
428
- fs_1.default.appendFileSync(filepath.trim().substring(0, 1) === "/" ? filepath : process.cwd() + "/" + filepath, logString + "\n");
472
+ fs_1.default.appendFileSync(filepath.trim().substring(0, 1) === "/"
473
+ ? filepath
474
+ : process.cwd() + "/" + filepath, logString + "\n");
429
475
  }
430
476
  }
431
477
  catch (e) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webtypen/webframez-core",
3
- "version": "0.0.43",
3
+ "version": "0.0.44",
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",
@@ -127,23 +127,30 @@ class RouterFacade {
127
127
  return null;
128
128
  }
129
129
 
130
+ const cleanUrl =
131
+ req.url.indexOf("?") > 0
132
+ ? req.url.substring(0, req.url.indexOf("?"))
133
+ : req.url.indexOf("#") > 0
134
+ ? req.url.substring(0, req.url.indexOf("#"))
135
+ : req.url;
136
+
130
137
  if (req.method === "GET") {
131
- return this.dissolveRoute(this.routesGET, req.url);
138
+ return this.dissolveRoute(this.routesGET, cleanUrl);
132
139
  } else if (req.method === "POST") {
133
- return this.dissolveRoute(this.routesPOST, req.url);
140
+ return this.dissolveRoute(this.routesPOST, cleanUrl);
134
141
  } else if (req.method === "PUT") {
135
- return this.dissolveRoute(this.routesPUT, req.url);
142
+ return this.dissolveRoute(this.routesPUT, cleanUrl);
136
143
  } else if (req.method === "DELETE") {
137
- return this.dissolveRoute(this.routesDELETE, req.url);
144
+ return this.dissolveRoute(this.routesDELETE, cleanUrl);
138
145
  } else if (req.method === "OPTIONS") {
139
146
  if (req.headers["access-control-request-method"] === "GET") {
140
- return this.dissolveRoute(this.routesGET, req.url);
147
+ return this.dissolveRoute(this.routesGET, cleanUrl);
141
148
  } else if (req.headers["access-control-request-method"] === "POST") {
142
- return this.dissolveRoute(this.routesPOST, req.url);
149
+ return this.dissolveRoute(this.routesPOST, cleanUrl);
143
150
  } else if (req.headers["access-control-request-method"] === "PUT") {
144
- return this.dissolveRoute(this.routesPUT, req.url);
151
+ return this.dissolveRoute(this.routesPUT, cleanUrl);
145
152
  } else if (req.headers["access-control-request-method"] === "DELETE") {
146
- return this.dissolveRoute(this.routesDELETE, req.url);
153
+ return this.dissolveRoute(this.routesDELETE, cleanUrl);
147
154
  }
148
155
  }
149
156
  return null;
@@ -196,7 +203,13 @@ class RouterFacade {
196
203
  */
197
204
  sendError(res: ServerResponse, code: number, message?: string) {
198
205
  res.writeHead(code, { "Content-Type": "text/html" });
199
- res.end(message ? message : code === 404 ? "Oops! Page not found ..." : "Oops! There was an unexpected error ...");
206
+ res.end(
207
+ message
208
+ ? message
209
+ : code === 404
210
+ ? "Oops! Page not found ..."
211
+ : "Oops! There was an unexpected error ..."
212
+ );
200
213
  }
201
214
 
202
215
  /**
@@ -205,7 +218,11 @@ class RouterFacade {
205
218
  * @param req
206
219
  * @param res
207
220
  */
208
- async handleRequest(req: null | IncomingMessage, res: null | ServerResponse, options?: any) {
221
+ async handleRequest(
222
+ req: null | IncomingMessage,
223
+ res: null | ServerResponse,
224
+ options?: any
225
+ ) {
209
226
  let customRequest = null;
210
227
  if (!req && this.mode === "aws-lambda" && options && options.event) {
211
228
  customRequest = {
@@ -222,7 +239,9 @@ class RouterFacade {
222
239
  throw new Error("Missing Kernel in Router ...");
223
240
  }
224
241
 
225
- const route = this.dissolve(customRequest !== null && this.mode === "aws-lambda" ? customRequest : req);
242
+ const route = this.dissolve(
243
+ customRequest !== null && this.mode === "aws-lambda" ? customRequest : req
244
+ );
226
245
  const response = new Response({ mode: this.mode });
227
246
  if (res) {
228
247
  response.setServerResponse(res);
@@ -253,7 +272,8 @@ class RouterFacade {
253
272
  body:
254
273
  bodyPlain &&
255
274
  customRequest.headers["content-type"] === "application/json" &&
256
- (bodyPlain.trim().substring(0, 1) === "[" || bodyPlain.trim().substring(0, 1) === "{")
275
+ (bodyPlain.trim().substring(0, 1) === "[" ||
276
+ bodyPlain.trim().substring(0, 1) === "{")
257
277
  ? JSON.parse(bodyPlain)
258
278
  : bodyPlain
259
279
  ? qs.parse(
@@ -273,7 +293,8 @@ class RouterFacade {
273
293
  body:
274
294
  bodyPlain &&
275
295
  req.headers["content-type"] === "application/json" &&
276
- (bodyPlain.trim().substring(0, 1) === "[" || bodyPlain.trim().substring(0, 1) === "{")
296
+ (bodyPlain.trim().substring(0, 1) === "[" ||
297
+ bodyPlain.trim().substring(0, 1) === "{")
277
298
  ? JSON.parse(bodyPlain)
278
299
  : bodyPlain
279
300
  ? qs.parse(bodyPlain)
@@ -292,19 +313,31 @@ class RouterFacade {
292
313
  : null;
293
314
 
294
315
  // Check-Middleware
295
- if (route && route.options && route.options.middleware && route.options.middleware.length > 0) {
316
+ if (
317
+ route &&
318
+ route.options &&
319
+ route.options.middleware &&
320
+ route.options.middleware.length > 0
321
+ ) {
296
322
  for (let middlewareKey of route.options.middleware) {
297
323
  const middleware = this.kernel.middleware[middlewareKey];
298
324
  if (!middleware) {
299
325
  if (this.mode === "aws-lambda") {
300
326
  return {
301
327
  statusCode: 500,
302
- body: JSON.stringify({ status: "error", message: "Unknown middleware `" + middlewareKey + "` ..." }),
328
+ body: JSON.stringify({
329
+ status: "error",
330
+ message: "Unknown middleware `" + middlewareKey + "` ...",
331
+ }),
303
332
  headers: response.headers,
304
333
  };
305
334
  } else if (req && res) {
306
335
  this.requestConsoleLog(req, 500);
307
- return this.sendError(res, 500, "Unknown middleware `" + middlewareKey + "` ...");
336
+ return this.sendError(
337
+ res,
338
+ 500,
339
+ "Unknown middleware `" + middlewareKey + "` ..."
340
+ );
308
341
  }
309
342
  }
310
343
 
@@ -325,22 +358,44 @@ class RouterFacade {
325
358
  if (this.mode === "aws-lambda") {
326
359
  return {
327
360
  statusCode: 500,
328
- body: JSON.stringify({ status: "error", message: "Middleware run error `" + middlewareKey + "` ..." }),
361
+ body: JSON.stringify({
362
+ status: "error",
363
+ message: "Middleware run error `" + middlewareKey + "` ...",
364
+ }),
329
365
  headers: response.headers,
330
366
  };
331
367
  } else if (req && res) {
332
368
  this.requestConsoleLog(req, 500);
333
- return this.sendError(res, 500, "Error in middleware `" + middlewareKey + "` ...");
369
+ return this.sendError(
370
+ res,
371
+ 500,
372
+ "Error in middleware `" + middlewareKey + "` ..."
373
+ );
334
374
  }
335
375
  }
336
376
  });
337
377
  } catch (e: any) {
338
378
  // Send Middleware-Abort
339
379
  if (this.mode === "aws-lambda") {
340
- return { statusCode: 500, body: JSON.stringify({ status: "error", message: e.data }), headers: response.headers };
380
+ return {
381
+ statusCode: 500,
382
+ body: JSON.stringify({ status: "error", message: e.data }),
383
+ headers: response.headers,
384
+ };
341
385
  } else if (res) {
342
- res.writeHead(e.status, typeof e.data === "object" ? { "Content-Type": "application/json" } : { "Content-Type": "text/html" });
343
- res.end(typeof e.data === "object" ? JSON.stringify(e.data) : e.data ? e.data : " ");
386
+ res.writeHead(
387
+ e.status,
388
+ typeof e.data === "object"
389
+ ? { "Content-Type": "application/json" }
390
+ : { "Content-Type": "text/html" }
391
+ );
392
+ res.end(
393
+ typeof e.data === "object"
394
+ ? JSON.stringify(e.data)
395
+ : e.data
396
+ ? e.data
397
+ : " "
398
+ );
344
399
  }
345
400
  return;
346
401
  }
@@ -372,7 +427,10 @@ class RouterFacade {
372
427
  if (this.mode === "aws-lambda") {
373
428
  return {
374
429
  statusCode: 500,
375
- body: JSON.stringify({ status: "error", message: "Controller `" + split[0] + "` not found ..." }),
430
+ body: JSON.stringify({
431
+ status: "error",
432
+ message: "Controller `" + split[0] + "` not found ...",
433
+ }),
376
434
  headers: response.headers,
377
435
  };
378
436
  } else if (res) {
@@ -393,7 +451,11 @@ class RouterFacade {
393
451
  await route.component(request, response);
394
452
 
395
453
  if (this.mode === "aws-lambda") {
396
- return { statusCode: response.statusCode, body: JSON.stringify(response.content), headers: response.headers };
454
+ return {
455
+ statusCode: response.statusCode,
456
+ body: JSON.stringify(response.content),
457
+ headers: response.headers,
458
+ };
397
459
  } else if (res && req) {
398
460
  res.end();
399
461
  this.requestConsoleLog(req, response.statusCode);
@@ -402,7 +464,10 @@ class RouterFacade {
402
464
  if (this.mode === "aws-lambda") {
403
465
  return {
404
466
  statusCode: 500,
405
- body: JSON.stringify({ status: "error", message: "Error running the component ..." }),
467
+ body: JSON.stringify({
468
+ status: "error",
469
+ message: "Error running the component ...",
470
+ }),
406
471
  headers: response.headers,
407
472
  };
408
473
  } else if (res && req) {
@@ -414,7 +479,10 @@ class RouterFacade {
414
479
  if (this.mode === "aws-lambda") {
415
480
  return {
416
481
  statusCode: 500,
417
- body: JSON.stringify({ status: "error", message: "Route component not found ..." }),
482
+ body: JSON.stringify({
483
+ status: "error",
484
+ message: "Route component not found ...",
485
+ }),
418
486
  headers: response.headers,
419
487
  };
420
488
  } else if (res && req) {
@@ -425,7 +493,10 @@ class RouterFacade {
425
493
  }
426
494
 
427
495
  requestConsoleLog(req: IncomingMessage, httpCode: number) {
428
- if (Config.get("application.router.requests.logConsole") !== true && !Config.get("application.router.requests.logFile")) {
496
+ if (
497
+ Config.get("application.router.requests.logConsole") !== true &&
498
+ !Config.get("application.router.requests.logFile")
499
+ ) {
429
500
  return;
430
501
  }
431
502
 
@@ -471,7 +542,12 @@ class RouterFacade {
471
542
  try {
472
543
  const filepath = Config.get("application.router.requests.logFile");
473
544
  if (filepath && filepath.trim() !== "") {
474
- fs.appendFileSync(filepath.trim().substring(0, 1) === "/" ? filepath : process.cwd() + "/" + filepath, logString + "\n");
545
+ fs.appendFileSync(
546
+ filepath.trim().substring(0, 1) === "/"
547
+ ? filepath
548
+ : process.cwd() + "/" + filepath,
549
+ logString + "\n"
550
+ );
475
551
  }
476
552
  } catch (e) {
477
553
  console.error(e);