@webtypen/webframez-core 0.0.28 → 0.0.30
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/.prettierrc +1 -0
- package/lib/LambdaApplication.d.ts +5 -7
- package/lib/LambdaApplication.js +2 -2
- package/lib/Router/Router.d.ts +5 -2
- package/lib/Router/Router.js +126 -68
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/package.json +1 -1
- package/src/LambdaApplication.ts +2 -5
- package/src/Router/Router.ts +129 -93
- package/src/index.ts +1 -0
package/.prettierrc
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{ "printWidth": 140 }
|
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
export declare class LambdaApplication {
|
|
2
|
-
private server;
|
|
3
2
|
/**
|
|
4
|
-
* Init the routes and
|
|
3
|
+
* Init the routes and handle the request
|
|
5
4
|
*/
|
|
6
|
-
boot(event: any, context: any, options?: any): {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
};
|
|
5
|
+
boot(event: any, context: any, options?: any): Promise<void | {
|
|
6
|
+
statusCode: number;
|
|
7
|
+
body: string;
|
|
8
|
+
}>;
|
|
11
9
|
}
|
package/lib/LambdaApplication.js
CHANGED
|
@@ -5,7 +5,7 @@ const Config_1 = require("./Config");
|
|
|
5
5
|
const Router_1 = require("./Router/Router");
|
|
6
6
|
class LambdaApplication {
|
|
7
7
|
/**
|
|
8
|
-
* Init the routes and
|
|
8
|
+
* Init the routes and handle the request
|
|
9
9
|
*/
|
|
10
10
|
boot(event, context, options) {
|
|
11
11
|
if (options && options.config) {
|
|
@@ -18,7 +18,7 @@ class LambdaApplication {
|
|
|
18
18
|
kernel: options && options.kernel ? options.kernel : null,
|
|
19
19
|
basename: options && options.basename ? options.basename : null,
|
|
20
20
|
});
|
|
21
|
-
return
|
|
21
|
+
return Router_1.Router.handleRequest(null, null, { event: event });
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
exports.LambdaApplication = LambdaApplication;
|
package/lib/Router/Router.d.ts
CHANGED
|
@@ -85,7 +85,7 @@ declare class RouterFacade {
|
|
|
85
85
|
* @param req
|
|
86
86
|
* @returns
|
|
87
87
|
*/
|
|
88
|
-
dissolve(req: IncomingMessage): RouteObject | null;
|
|
88
|
+
dissolve(req: IncomingMessage | any | null): RouteObject | null;
|
|
89
89
|
/**
|
|
90
90
|
* Dissolves the matching route based on the request-url and an object of routes
|
|
91
91
|
*
|
|
@@ -106,7 +106,10 @@ declare class RouterFacade {
|
|
|
106
106
|
* @param req
|
|
107
107
|
* @param res
|
|
108
108
|
*/
|
|
109
|
-
handleRequest(req: IncomingMessage, res: ServerResponse): Promise<void
|
|
109
|
+
handleRequest(req: null | IncomingMessage, res: null | ServerResponse, options?: any): Promise<void | {
|
|
110
|
+
statusCode: number;
|
|
111
|
+
body: string;
|
|
112
|
+
}>;
|
|
110
113
|
requestConsoleLog(req: IncomingMessage, httpCode: number): void;
|
|
111
114
|
}
|
|
112
115
|
export declare const Router: RouterFacade;
|
package/lib/Router/Router.js
CHANGED
|
@@ -93,7 +93,7 @@ class RouterFacade {
|
|
|
93
93
|
* @returns
|
|
94
94
|
*/
|
|
95
95
|
dissolve(req) {
|
|
96
|
-
if (!req.url) {
|
|
96
|
+
if (!req || !req.url) {
|
|
97
97
|
return null;
|
|
98
98
|
}
|
|
99
99
|
if (req.method === "GET") {
|
|
@@ -167,11 +167,7 @@ class RouterFacade {
|
|
|
167
167
|
*/
|
|
168
168
|
sendError(res, code, message) {
|
|
169
169
|
res.writeHead(code, { "Content-Type": "text/html" });
|
|
170
|
-
res.end(message
|
|
171
|
-
? message
|
|
172
|
-
: code === 404
|
|
173
|
-
? "Oops! Page not found ..."
|
|
174
|
-
: "Oops! There was an unexpected error ...");
|
|
170
|
+
res.end(message ? message : code === 404 ? "Oops! Page not found ..." : "Oops! There was an unexpected error ...");
|
|
175
171
|
}
|
|
176
172
|
/**
|
|
177
173
|
* Processes a request
|
|
@@ -179,55 +175,93 @@ class RouterFacade {
|
|
|
179
175
|
* @param req
|
|
180
176
|
* @param res
|
|
181
177
|
*/
|
|
182
|
-
handleRequest(req, res) {
|
|
178
|
+
handleRequest(req, res, options) {
|
|
183
179
|
return __awaiter(this, void 0, void 0, function* () {
|
|
180
|
+
let customRequest = null;
|
|
181
|
+
if (!req && this.mode === "aws-lambda" && options && options.event) {
|
|
182
|
+
customRequest = {
|
|
183
|
+
method: options.event.requestContext.http.method,
|
|
184
|
+
url: options.event.requestContext.http.path,
|
|
185
|
+
headers: options.event.requestContext.headers,
|
|
186
|
+
};
|
|
187
|
+
}
|
|
184
188
|
if (!this.kernel) {
|
|
185
|
-
res
|
|
189
|
+
if (res) {
|
|
190
|
+
res.end();
|
|
191
|
+
}
|
|
186
192
|
throw new Error("Missing Kernel in Router ...");
|
|
187
193
|
}
|
|
188
|
-
const route = this.dissolve(req);
|
|
194
|
+
const route = this.dissolve(customRequest !== null && this.mode === "aws-lambda" ? customRequest : req);
|
|
189
195
|
const response = new Response_1.Response({ mode: this.mode });
|
|
190
|
-
|
|
196
|
+
if (res) {
|
|
197
|
+
response.setServerResponse(res);
|
|
198
|
+
}
|
|
191
199
|
// Load Body
|
|
192
200
|
let body = [];
|
|
193
|
-
const bodyPlain =
|
|
194
|
-
|
|
195
|
-
.
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
201
|
+
const bodyPlain = this.mode === "aws-lambda"
|
|
202
|
+
? options && options.event && options.event.body
|
|
203
|
+
? Buffer.concat(options.event.body).toString()
|
|
204
|
+
: null
|
|
205
|
+
: req
|
|
206
|
+
? yield new Promise((resolve) => {
|
|
207
|
+
req
|
|
208
|
+
.on("data", (chunk) => {
|
|
209
|
+
body.push(chunk);
|
|
210
|
+
})
|
|
211
|
+
.on("end", () => {
|
|
212
|
+
resolve(Buffer.concat(body).toString());
|
|
213
|
+
});
|
|
214
|
+
})
|
|
215
|
+
: null;
|
|
216
|
+
const request = this.mode === "aws-lambda" && customRequest
|
|
217
|
+
? {
|
|
218
|
+
body: bodyPlain &&
|
|
219
|
+
customRequest.headers["content-type"] === "application/json" &&
|
|
220
|
+
(bodyPlain.trim().substring(0, 1) === "[" || bodyPlain.trim().substring(0, 1) === "{")
|
|
221
|
+
? JSON.parse(bodyPlain)
|
|
222
|
+
: bodyPlain
|
|
223
|
+
? qs_1.default.parse(bodyPlain)
|
|
224
|
+
: {},
|
|
225
|
+
bodyPlain: bodyPlain,
|
|
226
|
+
params: route && route.params ? route.params : {},
|
|
227
|
+
headers: customRequest.headers,
|
|
228
|
+
method: customRequest.method,
|
|
229
|
+
}
|
|
230
|
+
: req
|
|
231
|
+
? {
|
|
232
|
+
body: bodyPlain &&
|
|
233
|
+
req.headers["content-type"] === "application/json" &&
|
|
234
|
+
(bodyPlain.trim().substring(0, 1) === "[" || bodyPlain.trim().substring(0, 1) === "{")
|
|
235
|
+
? JSON.parse(bodyPlain)
|
|
236
|
+
: bodyPlain
|
|
237
|
+
? qs_1.default.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
|
+
}
|
|
249
|
+
: null;
|
|
221
250
|
// Check-Middleware
|
|
222
|
-
if (route &&
|
|
223
|
-
route.options &&
|
|
224
|
-
route.options.middleware &&
|
|
225
|
-
route.options.middleware.length > 0) {
|
|
251
|
+
if (route && route.options && route.options.middleware && route.options.middleware.length > 0) {
|
|
226
252
|
for (let middlewareKey of route.options.middleware) {
|
|
227
253
|
const middleware = this.kernel.middleware[middlewareKey];
|
|
228
254
|
if (!middleware) {
|
|
229
|
-
this.
|
|
230
|
-
|
|
255
|
+
if (this.mode === "aws-lambda") {
|
|
256
|
+
return {
|
|
257
|
+
statusCode: 500,
|
|
258
|
+
body: JSON.stringify({ status: "error", message: "Unknown middleware `" + middlewareKey + "` ..." }),
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
else if (req && res) {
|
|
262
|
+
this.requestConsoleLog(req, 500);
|
|
263
|
+
return this.sendError(res, 500, "Unknown middleware `" + middlewareKey + "` ...");
|
|
264
|
+
}
|
|
231
265
|
}
|
|
232
266
|
try {
|
|
233
267
|
yield new Promise((resolve, reject) => {
|
|
@@ -238,27 +272,34 @@ class RouterFacade {
|
|
|
238
272
|
}
|
|
239
273
|
catch (e) {
|
|
240
274
|
console.error(e);
|
|
241
|
-
this.
|
|
242
|
-
|
|
275
|
+
if (this.mode === "aws-lambda") {
|
|
276
|
+
return {
|
|
277
|
+
statusCode: 500,
|
|
278
|
+
body: JSON.stringify({ status: "error", message: "Middleware run error `" + middlewareKey + "` ..." }),
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
else if (req && res) {
|
|
282
|
+
this.requestConsoleLog(req, 500);
|
|
283
|
+
return this.sendError(res, 500, "Error in middleware `" + middlewareKey + "` ...");
|
|
284
|
+
}
|
|
243
285
|
}
|
|
244
286
|
});
|
|
245
287
|
}
|
|
246
288
|
catch (e) {
|
|
247
289
|
// Send Middleware-Abort
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
: e.data
|
|
254
|
-
|
|
255
|
-
: " ");
|
|
290
|
+
if (this.mode === "aws-lambda") {
|
|
291
|
+
return { statusCode: 500, body: JSON.stringify({ status: "error", message: e.data }) };
|
|
292
|
+
}
|
|
293
|
+
else if (res) {
|
|
294
|
+
res.writeHead(e.status, typeof e.data === "object" ? { "Content-Type": "application/json" } : { "Content-Type": "text/html" });
|
|
295
|
+
res.end(typeof e.data === "object" ? JSON.stringify(e.data) : e.data ? e.data : " ");
|
|
296
|
+
}
|
|
256
297
|
return;
|
|
257
298
|
}
|
|
258
299
|
}
|
|
259
300
|
}
|
|
260
301
|
// Handle OPTIONS-Request
|
|
261
|
-
if (req.method === "OPTIONS") {
|
|
302
|
+
if (req && res && req.method === "OPTIONS") {
|
|
262
303
|
res.statusCode = 200;
|
|
263
304
|
res.setHeader("Content-Length", "0");
|
|
264
305
|
res.end();
|
|
@@ -270,8 +311,13 @@ class RouterFacade {
|
|
|
270
311
|
const split = route.component.split("@");
|
|
271
312
|
const controller = this.kernel.controller[split[0]];
|
|
272
313
|
if (!controller) {
|
|
273
|
-
|
|
274
|
-
|
|
314
|
+
if (this.mode === "aws-lambda") {
|
|
315
|
+
return { statusCode: 500, body: JSON.stringify({ status: "error", message: "Controller `" + split[0] + "` not found ..." }) };
|
|
316
|
+
}
|
|
317
|
+
else if (res) {
|
|
318
|
+
res.writeHead(500, { "Content-Type": "text/html" });
|
|
319
|
+
res.end("Controller `" + split[0] + "` not found ...");
|
|
320
|
+
}
|
|
275
321
|
return;
|
|
276
322
|
}
|
|
277
323
|
// Set Controller method as route-component
|
|
@@ -281,24 +327,38 @@ class RouterFacade {
|
|
|
281
327
|
if (route && route.component !== undefined) {
|
|
282
328
|
try {
|
|
283
329
|
yield route.component(request, response);
|
|
284
|
-
|
|
285
|
-
|
|
330
|
+
if (this.mode === "aws-lambda") {
|
|
331
|
+
return { statusCode: 200, body: JSON.stringify(response.content) };
|
|
332
|
+
}
|
|
333
|
+
else if (res && req) {
|
|
334
|
+
res.end();
|
|
335
|
+
this.requestConsoleLog(req, 200);
|
|
336
|
+
}
|
|
286
337
|
}
|
|
287
338
|
catch (e) {
|
|
288
339
|
console.error(e);
|
|
289
|
-
this.
|
|
290
|
-
|
|
340
|
+
if (this.mode === "aws-lambda") {
|
|
341
|
+
return { statusCode: 500, body: JSON.stringify({ status: "error", message: "Error running the component ..." }) };
|
|
342
|
+
}
|
|
343
|
+
else if (res && req) {
|
|
344
|
+
this.sendError(res, 500);
|
|
345
|
+
this.requestConsoleLog(req, 500);
|
|
346
|
+
}
|
|
291
347
|
}
|
|
292
348
|
}
|
|
293
349
|
else {
|
|
294
|
-
this.
|
|
295
|
-
|
|
350
|
+
if (this.mode === "aws-lambda") {
|
|
351
|
+
return { statusCode: 500, body: JSON.stringify({ status: "error", message: "Route component not found ..." }) };
|
|
352
|
+
}
|
|
353
|
+
else if (res && req) {
|
|
354
|
+
this.sendError(res, 404);
|
|
355
|
+
this.requestConsoleLog(req, 404);
|
|
356
|
+
}
|
|
296
357
|
}
|
|
297
358
|
});
|
|
298
359
|
}
|
|
299
360
|
requestConsoleLog(req, httpCode) {
|
|
300
|
-
if (Config_1.Config.get("application.router.requests.logConsole") !== true &&
|
|
301
|
-
!Config_1.Config.get("application.router.requests.logFile")) {
|
|
361
|
+
if (Config_1.Config.get("application.router.requests.logConsole") !== true && !Config_1.Config.get("application.router.requests.logFile")) {
|
|
302
362
|
return;
|
|
303
363
|
}
|
|
304
364
|
const date = new Date();
|
|
@@ -340,9 +400,7 @@ class RouterFacade {
|
|
|
340
400
|
try {
|
|
341
401
|
const filepath = Config_1.Config.get("application.router.requests.logFile");
|
|
342
402
|
if (filepath && filepath.trim() !== "") {
|
|
343
|
-
fs_1.default.appendFileSync(filepath.trim().substring(0, 1) === "/"
|
|
344
|
-
? filepath
|
|
345
|
-
: process.cwd() + "/" + filepath, logString + "\n");
|
|
403
|
+
fs_1.default.appendFileSync(filepath.trim().substring(0, 1) === "/" ? filepath : process.cwd() + "/" + filepath, logString + "\n");
|
|
346
404
|
}
|
|
347
405
|
}
|
|
348
406
|
catch (e) {
|
package/lib/index.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export * from "./Controller/Controller";
|
|
|
7
7
|
export * from "./Database/Model";
|
|
8
8
|
export * from "./Database/DBConnection";
|
|
9
9
|
export * from "./ConsoleApplication";
|
|
10
|
+
export * from "./LambdaApplication";
|
|
10
11
|
export * from "./WebApplication";
|
|
11
12
|
export * from "./Router/Route";
|
|
12
13
|
export * from "./Router/Request";
|
package/lib/index.js
CHANGED
|
@@ -23,6 +23,7 @@ __exportStar(require("./Controller/Controller"), exports);
|
|
|
23
23
|
__exportStar(require("./Database/Model"), exports);
|
|
24
24
|
__exportStar(require("./Database/DBConnection"), exports);
|
|
25
25
|
__exportStar(require("./ConsoleApplication"), exports);
|
|
26
|
+
__exportStar(require("./LambdaApplication"), exports);
|
|
26
27
|
__exportStar(require("./WebApplication"), exports);
|
|
27
28
|
__exportStar(require("./Router/Route"), exports);
|
|
28
29
|
__exportStar(require("./Router/Request"), exports);
|
package/package.json
CHANGED
package/src/LambdaApplication.ts
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
import http, { Server } from "http";
|
|
2
1
|
import { Config } from "./Config";
|
|
3
2
|
import { Router } from "./Router/Router";
|
|
4
3
|
|
|
5
4
|
export class LambdaApplication {
|
|
6
|
-
private server!: Server;
|
|
7
|
-
|
|
8
5
|
/**
|
|
9
|
-
* Init the routes and
|
|
6
|
+
* Init the routes and handle the request
|
|
10
7
|
*/
|
|
11
8
|
boot(event: any, context: any, options?: any) {
|
|
12
9
|
if (options && options.config) {
|
|
@@ -21,6 +18,6 @@ export class LambdaApplication {
|
|
|
21
18
|
basename: options && options.basename ? options.basename : null,
|
|
22
19
|
});
|
|
23
20
|
|
|
24
|
-
return
|
|
21
|
+
return Router.handleRequest(null, null, { event: event });
|
|
25
22
|
}
|
|
26
23
|
}
|
package/src/Router/Router.ts
CHANGED
|
@@ -122,8 +122,8 @@ class RouterFacade {
|
|
|
122
122
|
* @param req
|
|
123
123
|
* @returns
|
|
124
124
|
*/
|
|
125
|
-
dissolve(req: IncomingMessage): RouteObject | null {
|
|
126
|
-
if (!req.url) {
|
|
125
|
+
dissolve(req: IncomingMessage | any | null): RouteObject | null {
|
|
126
|
+
if (!req || !req.url) {
|
|
127
127
|
return null;
|
|
128
128
|
}
|
|
129
129
|
|
|
@@ -196,13 +196,7 @@ class RouterFacade {
|
|
|
196
196
|
*/
|
|
197
197
|
sendError(res: ServerResponse, code: number, message?: string) {
|
|
198
198
|
res.writeHead(code, { "Content-Type": "text/html" });
|
|
199
|
-
res.end(
|
|
200
|
-
message
|
|
201
|
-
? message
|
|
202
|
-
: code === 404
|
|
203
|
-
? "Oops! Page not found ..."
|
|
204
|
-
: "Oops! There was an unexpected error ..."
|
|
205
|
-
);
|
|
199
|
+
res.end(message ? message : code === 404 ? "Oops! Page not found ..." : "Oops! There was an unexpected error ...");
|
|
206
200
|
}
|
|
207
201
|
|
|
208
202
|
/**
|
|
@@ -211,65 +205,100 @@ class RouterFacade {
|
|
|
211
205
|
* @param req
|
|
212
206
|
* @param res
|
|
213
207
|
*/
|
|
214
|
-
async handleRequest(req: IncomingMessage, res: ServerResponse) {
|
|
208
|
+
async handleRequest(req: null | IncomingMessage, res: null | ServerResponse, options?: any) {
|
|
209
|
+
let customRequest = null;
|
|
210
|
+
if (!req && this.mode === "aws-lambda" && options && options.event) {
|
|
211
|
+
customRequest = {
|
|
212
|
+
method: options.event.requestContext.http.method,
|
|
213
|
+
url: options.event.requestContext.http.path,
|
|
214
|
+
headers: options.event.requestContext.headers,
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
215
218
|
if (!this.kernel) {
|
|
216
|
-
res
|
|
219
|
+
if (res) {
|
|
220
|
+
res.end();
|
|
221
|
+
}
|
|
217
222
|
throw new Error("Missing Kernel in Router ...");
|
|
218
223
|
}
|
|
219
224
|
|
|
220
|
-
const route = this.dissolve(req);
|
|
225
|
+
const route = this.dissolve(customRequest !== null && this.mode === "aws-lambda" ? customRequest : req);
|
|
221
226
|
const response = new Response({ mode: this.mode });
|
|
222
|
-
|
|
227
|
+
if (res) {
|
|
228
|
+
response.setServerResponse(res);
|
|
229
|
+
}
|
|
223
230
|
|
|
224
231
|
// Load Body
|
|
225
232
|
let body: any = [];
|
|
226
|
-
const bodyPlain: any =
|
|
227
|
-
|
|
228
|
-
.
|
|
229
|
-
body.
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
233
|
+
const bodyPlain: any =
|
|
234
|
+
this.mode === "aws-lambda"
|
|
235
|
+
? options && options.event && options.event.body
|
|
236
|
+
? Buffer.concat(options.event.body).toString()
|
|
237
|
+
: null
|
|
238
|
+
: req
|
|
239
|
+
? await new Promise((resolve: Function) => {
|
|
240
|
+
req
|
|
241
|
+
.on("data", (chunk) => {
|
|
242
|
+
body.push(chunk);
|
|
243
|
+
})
|
|
244
|
+
.on("end", () => {
|
|
245
|
+
resolve(Buffer.concat(body).toString());
|
|
246
|
+
});
|
|
247
|
+
})
|
|
248
|
+
: null;
|
|
249
|
+
|
|
250
|
+
const request =
|
|
251
|
+
this.mode === "aws-lambda" && customRequest
|
|
252
|
+
? {
|
|
253
|
+
body:
|
|
254
|
+
bodyPlain &&
|
|
255
|
+
customRequest.headers["content-type"] === "application/json" &&
|
|
256
|
+
(bodyPlain.trim().substring(0, 1) === "[" || bodyPlain.trim().substring(0, 1) === "{")
|
|
257
|
+
? JSON.parse(bodyPlain)
|
|
258
|
+
: bodyPlain
|
|
259
|
+
? qs.parse(bodyPlain)
|
|
260
|
+
: {},
|
|
261
|
+
bodyPlain: bodyPlain,
|
|
262
|
+
params: route && route.params ? route.params : {},
|
|
263
|
+
headers: customRequest.headers,
|
|
264
|
+
method: customRequest.method,
|
|
265
|
+
}
|
|
266
|
+
: req
|
|
267
|
+
? {
|
|
268
|
+
body:
|
|
269
|
+
bodyPlain &&
|
|
270
|
+
req.headers["content-type"] === "application/json" &&
|
|
271
|
+
(bodyPlain.trim().substring(0, 1) === "[" || bodyPlain.trim().substring(0, 1) === "{")
|
|
272
|
+
? JSON.parse(bodyPlain)
|
|
273
|
+
: bodyPlain
|
|
274
|
+
? qs.parse(bodyPlain)
|
|
275
|
+
: {},
|
|
276
|
+
bodyPlain: bodyPlain,
|
|
277
|
+
params: route && route.params ? route.params : {},
|
|
278
|
+
httpVersionMajor: req.httpVersionMajor,
|
|
279
|
+
httpVersionMinor: req.httpVersionMinor,
|
|
280
|
+
httpVersion: req.httpVersion,
|
|
281
|
+
headers: req.headers,
|
|
282
|
+
rawHeaders: req.rawHeaders,
|
|
283
|
+
url: req.url,
|
|
284
|
+
method: req.method,
|
|
285
|
+
}
|
|
286
|
+
: null;
|
|
256
287
|
|
|
257
288
|
// Check-Middleware
|
|
258
|
-
if (
|
|
259
|
-
route &&
|
|
260
|
-
route.options &&
|
|
261
|
-
route.options.middleware &&
|
|
262
|
-
route.options.middleware.length > 0
|
|
263
|
-
) {
|
|
289
|
+
if (route && route.options && route.options.middleware && route.options.middleware.length > 0) {
|
|
264
290
|
for (let middlewareKey of route.options.middleware) {
|
|
265
291
|
const middleware = this.kernel.middleware[middlewareKey];
|
|
266
292
|
if (!middleware) {
|
|
267
|
-
this.
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
)
|
|
293
|
+
if (this.mode === "aws-lambda") {
|
|
294
|
+
return {
|
|
295
|
+
statusCode: 500,
|
|
296
|
+
body: JSON.stringify({ status: "error", message: "Unknown middleware `" + middlewareKey + "` ..." }),
|
|
297
|
+
};
|
|
298
|
+
} else if (req && res) {
|
|
299
|
+
this.requestConsoleLog(req, 500);
|
|
300
|
+
return this.sendError(res, 500, "Unknown middleware `" + middlewareKey + "` ...");
|
|
301
|
+
}
|
|
273
302
|
}
|
|
274
303
|
|
|
275
304
|
try {
|
|
@@ -286,36 +315,32 @@ class RouterFacade {
|
|
|
286
315
|
} catch (e) {
|
|
287
316
|
console.error(e);
|
|
288
317
|
|
|
289
|
-
this.
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
)
|
|
318
|
+
if (this.mode === "aws-lambda") {
|
|
319
|
+
return {
|
|
320
|
+
statusCode: 500,
|
|
321
|
+
body: JSON.stringify({ status: "error", message: "Middleware run error `" + middlewareKey + "` ..." }),
|
|
322
|
+
};
|
|
323
|
+
} else if (req && res) {
|
|
324
|
+
this.requestConsoleLog(req, 500);
|
|
325
|
+
return this.sendError(res, 500, "Error in middleware `" + middlewareKey + "` ...");
|
|
326
|
+
}
|
|
295
327
|
}
|
|
296
328
|
});
|
|
297
329
|
} catch (e: any) {
|
|
298
330
|
// Send Middleware-Abort
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
res.end(
|
|
306
|
-
typeof e.data === "object"
|
|
307
|
-
? JSON.stringify(e.data)
|
|
308
|
-
: e.data
|
|
309
|
-
? e.data
|
|
310
|
-
: " "
|
|
311
|
-
);
|
|
331
|
+
if (this.mode === "aws-lambda") {
|
|
332
|
+
return { statusCode: 500, body: JSON.stringify({ status: "error", message: e.data }) };
|
|
333
|
+
} else if (res) {
|
|
334
|
+
res.writeHead(e.status, typeof e.data === "object" ? { "Content-Type": "application/json" } : { "Content-Type": "text/html" });
|
|
335
|
+
res.end(typeof e.data === "object" ? JSON.stringify(e.data) : e.data ? e.data : " ");
|
|
336
|
+
}
|
|
312
337
|
return;
|
|
313
338
|
}
|
|
314
339
|
}
|
|
315
340
|
}
|
|
316
341
|
|
|
317
342
|
// Handle OPTIONS-Request
|
|
318
|
-
if (req.method === "OPTIONS") {
|
|
343
|
+
if (req && res && req.method === "OPTIONS") {
|
|
319
344
|
res.statusCode = 200;
|
|
320
345
|
res.setHeader("Content-Length", "0");
|
|
321
346
|
res.end();
|
|
@@ -328,8 +353,13 @@ class RouterFacade {
|
|
|
328
353
|
const split = route.component.split("@");
|
|
329
354
|
const controller = this.kernel.controller[split[0]];
|
|
330
355
|
if (!controller) {
|
|
331
|
-
|
|
332
|
-
|
|
356
|
+
if (this.mode === "aws-lambda") {
|
|
357
|
+
return { statusCode: 500, body: JSON.stringify({ status: "error", message: "Controller `" + split[0] + "` not found ..." }) };
|
|
358
|
+
} else if (res) {
|
|
359
|
+
res.writeHead(500, { "Content-Type": "text/html" });
|
|
360
|
+
res.end("Controller `" + split[0] + "` not found ...");
|
|
361
|
+
}
|
|
362
|
+
|
|
333
363
|
return;
|
|
334
364
|
}
|
|
335
365
|
|
|
@@ -341,24 +371,35 @@ class RouterFacade {
|
|
|
341
371
|
if (route && route.component !== undefined) {
|
|
342
372
|
try {
|
|
343
373
|
await route.component(request, response);
|
|
344
|
-
|
|
345
|
-
this.
|
|
374
|
+
|
|
375
|
+
if (this.mode === "aws-lambda") {
|
|
376
|
+
return { statusCode: 200, body: JSON.stringify(response.content) };
|
|
377
|
+
} else if (res && req) {
|
|
378
|
+
res.end();
|
|
379
|
+
this.requestConsoleLog(req, 200);
|
|
380
|
+
}
|
|
346
381
|
} catch (e) {
|
|
347
382
|
console.error(e);
|
|
348
|
-
|
|
349
|
-
this.
|
|
383
|
+
|
|
384
|
+
if (this.mode === "aws-lambda") {
|
|
385
|
+
return { statusCode: 500, body: JSON.stringify({ status: "error", message: "Error running the component ..." }) };
|
|
386
|
+
} else if (res && req) {
|
|
387
|
+
this.sendError(res, 500);
|
|
388
|
+
this.requestConsoleLog(req, 500);
|
|
389
|
+
}
|
|
350
390
|
}
|
|
351
391
|
} else {
|
|
352
|
-
this.
|
|
353
|
-
|
|
392
|
+
if (this.mode === "aws-lambda") {
|
|
393
|
+
return { statusCode: 500, body: JSON.stringify({ status: "error", message: "Route component not found ..." }) };
|
|
394
|
+
} else if (res && req) {
|
|
395
|
+
this.sendError(res, 404);
|
|
396
|
+
this.requestConsoleLog(req, 404);
|
|
397
|
+
}
|
|
354
398
|
}
|
|
355
399
|
}
|
|
356
400
|
|
|
357
401
|
requestConsoleLog(req: IncomingMessage, httpCode: number) {
|
|
358
|
-
if (
|
|
359
|
-
Config.get("application.router.requests.logConsole") !== true &&
|
|
360
|
-
!Config.get("application.router.requests.logFile")
|
|
361
|
-
) {
|
|
402
|
+
if (Config.get("application.router.requests.logConsole") !== true && !Config.get("application.router.requests.logFile")) {
|
|
362
403
|
return;
|
|
363
404
|
}
|
|
364
405
|
|
|
@@ -404,12 +445,7 @@ class RouterFacade {
|
|
|
404
445
|
try {
|
|
405
446
|
const filepath = Config.get("application.router.requests.logFile");
|
|
406
447
|
if (filepath && filepath.trim() !== "") {
|
|
407
|
-
fs.appendFileSync(
|
|
408
|
-
filepath.trim().substring(0, 1) === "/"
|
|
409
|
-
? filepath
|
|
410
|
-
: process.cwd() + "/" + filepath,
|
|
411
|
-
logString + "\n"
|
|
412
|
-
);
|
|
448
|
+
fs.appendFileSync(filepath.trim().substring(0, 1) === "/" ? filepath : process.cwd() + "/" + filepath, logString + "\n");
|
|
413
449
|
}
|
|
414
450
|
} catch (e) {
|
|
415
451
|
console.error(e);
|
package/src/index.ts
CHANGED
|
@@ -7,6 +7,7 @@ export * from "./Controller/Controller";
|
|
|
7
7
|
export * from "./Database/Model";
|
|
8
8
|
export * from "./Database/DBConnection";
|
|
9
9
|
export * from "./ConsoleApplication";
|
|
10
|
+
export * from "./LambdaApplication";
|
|
10
11
|
export * from "./WebApplication";
|
|
11
12
|
export * from "./Router/Route";
|
|
12
13
|
export * from "./Router/Request";
|