@ikonintegration/ikapi 5.0.10 → 5.0.11
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 +17 -0
- package/dist/index.js +28 -0
- package/dist/index.js.map +1 -0
- package/dist/package-lock.json +13811 -0
- package/dist/package.json +83 -0
- package/dist/src/API/Request.d.ts +125 -0
- package/dist/src/API/Request.js +185 -0
- package/dist/src/API/Request.js.map +1 -0
- package/dist/src/API/Response.d.ts +188 -0
- package/dist/src/API/Response.js +270 -0
- package/dist/src/API/Response.js.map +1 -0
- package/dist/src/BaseEvent/DynamoTransaction.d.ts +70 -0
- package/dist/src/BaseEvent/DynamoTransaction.js +104 -0
- package/dist/src/BaseEvent/DynamoTransaction.js.map +1 -0
- package/dist/src/BaseEvent/EventProcessor.d.ts +58 -0
- package/dist/src/BaseEvent/EventProcessor.js +101 -0
- package/dist/src/BaseEvent/EventProcessor.js.map +1 -0
- package/dist/src/BaseEvent/Launchable/ContainerExec.d.ts +42 -0
- package/dist/src/BaseEvent/Launchable/ContainerExec.js +104 -0
- package/dist/src/BaseEvent/Launchable/ContainerExec.js.map +1 -0
- package/dist/src/BaseEvent/Launchable/index.d.ts +34 -0
- package/dist/src/BaseEvent/Launchable/index.js +43 -0
- package/dist/src/BaseEvent/Launchable/index.js.map +1 -0
- package/dist/src/BaseEvent/Launchable/types.d.ts +10 -0
- package/dist/src/BaseEvent/Launchable/types.js +2 -0
- package/dist/src/BaseEvent/Launchable/types.js.map +1 -0
- package/dist/src/BaseEvent/Process.d.ts +50 -0
- package/dist/src/BaseEvent/Process.js +64 -0
- package/dist/src/BaseEvent/Process.js.map +1 -0
- package/dist/src/BaseEvent/StepTransaction.d.ts +23 -0
- package/dist/src/BaseEvent/StepTransaction.js +27 -0
- package/dist/src/BaseEvent/StepTransaction.js.map +1 -0
- package/dist/src/BaseEvent/Transaction.d.ts +149 -0
- package/dist/src/BaseEvent/Transaction.js +224 -0
- package/dist/src/BaseEvent/Transaction.js.map +1 -0
- package/dist/src/Cache/Redis.d.ts +29 -0
- package/dist/src/Cache/Redis.js +80 -0
- package/dist/src/Cache/Redis.js.map +1 -0
- package/dist/src/Cache/types.d.ts +31 -0
- package/dist/src/Cache/types.js +2 -0
- package/dist/src/Cache/types.js.map +1 -0
- package/dist/src/Config/Configuration.d.ts +123 -0
- package/dist/src/Config/Configuration.js +109 -0
- package/dist/src/Config/Configuration.js.map +1 -0
- package/dist/src/Config/EnvironmentVar.d.ts +74 -0
- package/dist/src/Config/EnvironmentVar.js +138 -0
- package/dist/src/Config/EnvironmentVar.js.map +1 -0
- package/dist/src/Crypto/Crypto.d.ts +45 -0
- package/dist/src/Crypto/Crypto.js +72 -0
- package/dist/src/Crypto/Crypto.js.map +1 -0
- package/dist/src/Database/Database.d.ts +21 -0
- package/dist/src/Database/Database.js +15 -0
- package/dist/src/Database/Database.js.map +1 -0
- package/dist/src/Database/DatabaseManager.d.ts +47 -0
- package/dist/src/Database/DatabaseManager.js +60 -0
- package/dist/src/Database/DatabaseManager.js.map +1 -0
- package/dist/src/Database/DatabaseTransaction.d.ts +101 -0
- package/dist/src/Database/DatabaseTransaction.js +126 -0
- package/dist/src/Database/DatabaseTransaction.js.map +1 -0
- package/dist/src/Database/index.d.ts +10 -0
- package/dist/src/Database/index.js +15 -0
- package/dist/src/Database/index.js.map +1 -0
- package/dist/src/Database/integrations/dynamo/DynamoDatabase.d.ts +35 -0
- package/dist/src/Database/integrations/dynamo/DynamoDatabase.js +59 -0
- package/dist/src/Database/integrations/dynamo/DynamoDatabase.js.map +1 -0
- package/dist/src/Database/integrations/kysely/KyselyDatabase.d.ts +66 -0
- package/dist/src/Database/integrations/kysely/KyselyDatabase.js +86 -0
- package/dist/src/Database/integrations/kysely/KyselyDatabase.js.map +1 -0
- package/dist/src/Database/integrations/kysely/KyselyTransaction.d.ts +70 -0
- package/dist/src/Database/integrations/kysely/KyselyTransaction.js +118 -0
- package/dist/src/Database/integrations/kysely/KyselyTransaction.js.map +1 -0
- package/dist/src/Database/integrations/pgsql/PostgresDatabase.d.ts +36 -0
- package/dist/src/Database/integrations/pgsql/PostgresDatabase.js +54 -0
- package/dist/src/Database/integrations/pgsql/PostgresDatabase.js.map +1 -0
- package/dist/src/Database/integrations/pgsql/PostgresTransaction.d.ts +63 -0
- package/dist/src/Database/integrations/pgsql/PostgresTransaction.js +61 -0
- package/dist/src/Database/integrations/pgsql/PostgresTransaction.js.map +1 -0
- package/dist/src/Database/types.d.ts +76 -0
- package/dist/src/Database/types.js +2 -0
- package/dist/src/Database/types.js.map +1 -0
- package/dist/src/Globals.d.ts +93 -0
- package/dist/src/Globals.js +99 -0
- package/dist/src/Globals.js.map +1 -0
- package/dist/src/Logger/Logger.d.ts +161 -0
- package/dist/src/Logger/Logger.js +299 -0
- package/dist/src/Logger/Logger.js.map +1 -0
- package/dist/src/Mailer/Mailer.d.ts +79 -0
- package/dist/src/Mailer/Mailer.js +184 -0
- package/dist/src/Mailer/Mailer.js.map +1 -0
- package/dist/src/Publisher/Publisher.d.ts +54 -0
- package/dist/src/Publisher/Publisher.js +136 -0
- package/dist/src/Publisher/Publisher.js.map +1 -0
- package/dist/src/Server/RouteResolver.d.ts +33 -0
- package/dist/src/Server/RouteResolver.js +100 -0
- package/dist/src/Server/RouteResolver.js.map +1 -0
- package/dist/src/Server/Router.d.ts +157 -0
- package/dist/src/Server/Router.js +32 -0
- package/dist/src/Server/Router.js.map +1 -0
- package/dist/src/Server/lib/ContainerServer.d.ts +42 -0
- package/dist/src/Server/lib/ContainerServer.js +66 -0
- package/dist/src/Server/lib/ContainerServer.js.map +1 -0
- package/dist/src/Server/lib/Server.d.ts +45 -0
- package/dist/src/Server/lib/Server.js +93 -0
- package/dist/src/Server/lib/Server.js.map +1 -0
- package/dist/src/Server/lib/container/GenericHandler.d.ts +9 -0
- package/dist/src/Server/lib/container/GenericHandler.js +82 -0
- package/dist/src/Server/lib/container/GenericHandler.js.map +1 -0
- package/dist/src/Server/lib/container/GenericHandlerEvent.d.ts +52 -0
- package/dist/src/Server/lib/container/GenericHandlerEvent.js +132 -0
- package/dist/src/Server/lib/container/GenericHandlerEvent.js.map +1 -0
- package/dist/src/Server/lib/container/HealthHandler.d.ts +9 -0
- package/dist/src/Server/lib/container/HealthHandler.js +19 -0
- package/dist/src/Server/lib/container/HealthHandler.js.map +1 -0
- package/dist/src/Server/lib/container/Proxy.d.ts +67 -0
- package/dist/src/Server/lib/container/Proxy.js +143 -0
- package/dist/src/Server/lib/container/Proxy.js.map +1 -0
- package/dist/src/Server/lib/container/Utils.d.ts +14 -0
- package/dist/src/Server/lib/container/Utils.js +37 -0
- package/dist/src/Server/lib/container/Utils.js.map +1 -0
- package/dist/src/Util/AsyncSingleton.d.ts +31 -0
- package/dist/src/Util/AsyncSingleton.js +83 -0
- package/dist/src/Util/AsyncSingleton.js.map +1 -0
- package/dist/src/Util/Utils.d.ts +61 -0
- package/dist/src/Util/Utils.js +147 -0
- package/dist/src/Util/Utils.js.map +1 -0
- package/dist/src/Validation/Validator.d.ts +17 -0
- package/dist/src/Validation/Validator.js +39 -0
- package/dist/src/Validation/Validator.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +1 -1
- package/src/Mailer/Mailer.ts +1 -1
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { pathToRegexp } from 'path-to-regexp';
|
|
11
|
+
import Response from '../../API/Response.js';
|
|
12
|
+
import Transaction from '../../BaseEvent/Transaction.js';
|
|
13
|
+
import Validator from '../../Validation/Validator.js';
|
|
14
|
+
import RouteResolver from '../RouteResolver.js';
|
|
15
|
+
/**
|
|
16
|
+
* Represents a server that handles serverless events and routes them to appropriate handlers.
|
|
17
|
+
*/
|
|
18
|
+
export default class Server {
|
|
19
|
+
/**
|
|
20
|
+
* Constructs a new instance of the Router class.
|
|
21
|
+
* @param {RouterConfig} config - The configuration object for the router.
|
|
22
|
+
* @returns None
|
|
23
|
+
*/
|
|
24
|
+
constructor(config) {
|
|
25
|
+
this.config = config;
|
|
26
|
+
this.routeResolver = new RouteResolver(config);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Returns a callable function that is bound to the `handleServerlessEvent` method of the current object.
|
|
30
|
+
* @returns {CallableFunction} - A callable function that is bound to the `handleServerlessEvent` method.
|
|
31
|
+
*/
|
|
32
|
+
getExport() {
|
|
33
|
+
return this.handleServerlessEvent.bind(this);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Handles a serverless event by executing a transaction and resolving the route based on the event.
|
|
37
|
+
* @param {APIGatewayProxyEvent} event - The serverless event object.
|
|
38
|
+
* @param {Context} context - The serverless context object.
|
|
39
|
+
* @returns None
|
|
40
|
+
*/
|
|
41
|
+
handleServerlessEvent(event, context) {
|
|
42
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
43
|
+
// init transaction
|
|
44
|
+
yield new Transaction(event, context, this.config).execute((transaction) => __awaiter(this, void 0, void 0, function* () {
|
|
45
|
+
const request = transaction.request;
|
|
46
|
+
const route = this.routeResolver.resolveRoute(request.getMethod(), request.getPath());
|
|
47
|
+
if (route) {
|
|
48
|
+
transaction.logger.log('Router accepted route:', route.method, route.path);
|
|
49
|
+
// Validate input
|
|
50
|
+
if (route.inputSchema) {
|
|
51
|
+
const validationResp = Validator.validateSchema(request.getBody(), route.inputSchema);
|
|
52
|
+
if (validationResp && validationResp instanceof Response)
|
|
53
|
+
return validationResp;
|
|
54
|
+
}
|
|
55
|
+
// Validate query
|
|
56
|
+
if (route.querySchema) {
|
|
57
|
+
const validationResp = Validator.validateSchema(request.getQueryParams(), route.querySchema);
|
|
58
|
+
if (validationResp && validationResp instanceof Response)
|
|
59
|
+
return validationResp;
|
|
60
|
+
}
|
|
61
|
+
// parse before validating
|
|
62
|
+
for (const path of Array.isArray(route.path) ? route.path : [route.path]) {
|
|
63
|
+
this.parsePathParams(request, path);
|
|
64
|
+
}
|
|
65
|
+
// Validate path
|
|
66
|
+
if (route.pathSchema) {
|
|
67
|
+
const validationResp = Validator.validateSchema(request.getPathParams(), route.pathSchema);
|
|
68
|
+
if (validationResp && validationResp instanceof Response)
|
|
69
|
+
return validationResp;
|
|
70
|
+
}
|
|
71
|
+
// Continue to route handler
|
|
72
|
+
return yield route.handler(transaction);
|
|
73
|
+
}
|
|
74
|
+
//No route found :/
|
|
75
|
+
return new Response(404, { err: 'Route not found!' });
|
|
76
|
+
}));
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Parses the path parameters from the request URL based on the given route path.
|
|
81
|
+
* @param {Request<any>} req - The request object.
|
|
82
|
+
* @param {string} routePath - The route path pattern to match against.
|
|
83
|
+
* @returns None
|
|
84
|
+
*/
|
|
85
|
+
parsePathParams(req, routePath) {
|
|
86
|
+
const path = req.getPath();
|
|
87
|
+
const regex = pathToRegexp(routePath);
|
|
88
|
+
const result = regex.regexp.exec(path);
|
|
89
|
+
if (result)
|
|
90
|
+
req.setFixedPathParams(regex.keys, result);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
//# sourceMappingURL=Server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Server.js","sourceRoot":"","sources":["../../../../src/Server/lib/Server.ts"],"names":[],"mappings":";;;;;;;;;AACA,OAAO,EAAE,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CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QACtC,IAAI,MAAM;YAAE,GAAG,CAAC,kBAAkB,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,CAAC,CAAA;IACxD,CAAC;CACF"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Request, Response } from 'express';
|
|
2
|
+
import Server from '../Server.js';
|
|
3
|
+
/**
|
|
4
|
+
* Creates an async function that handles serverless events and sends a response.
|
|
5
|
+
* @param {Server['handleServerlessEvent']} serverlessHandler - The serverless handler function.
|
|
6
|
+
* @returns {Function} - An async function that handles the request and sends a response.
|
|
7
|
+
*/
|
|
8
|
+
declare const _default: (serverlessHandler: Server["handleServerlessEvent"]) => (request: Request, res: Response) => Promise<void>;
|
|
9
|
+
export default _default;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import GenericHandlerEvent from './GenericHandlerEvent.js';
|
|
11
|
+
import Globals from '../../../Globals.js';
|
|
12
|
+
import Utils from '../../../Util/Utils.js';
|
|
13
|
+
/**
|
|
14
|
+
* Creates an async function that handles serverless events and sends a response.
|
|
15
|
+
* @param {Server['handleServerlessEvent']} serverlessHandler - The serverless handler function.
|
|
16
|
+
* @returns {Function} - An async function that handles the request and sends a response.
|
|
17
|
+
*/
|
|
18
|
+
export default (serverlessHandler) => {
|
|
19
|
+
/**
|
|
20
|
+
* Handles an HTTP request by invoking a serverless function and processing the response.
|
|
21
|
+
* @param {Request} request - The HTTP request object.
|
|
22
|
+
* @param {Response} res - The HTTP response object.
|
|
23
|
+
* @returns None
|
|
24
|
+
*/
|
|
25
|
+
return (request, res) => __awaiter(void 0, void 0, void 0, function* () {
|
|
26
|
+
const startTime = Date.now();
|
|
27
|
+
try {
|
|
28
|
+
// Generate event with request stuff (http to serverless translation)
|
|
29
|
+
const event = new GenericHandlerEvent(request, serverlessHandler);
|
|
30
|
+
// Invoke
|
|
31
|
+
const invokationResp = yield event.invoke();
|
|
32
|
+
// Respond
|
|
33
|
+
processServerlessResponse(invokationResp, res);
|
|
34
|
+
}
|
|
35
|
+
catch (e) {
|
|
36
|
+
console.error('[Proxy] - Exception during execution!', e);
|
|
37
|
+
console.error(e.stack);
|
|
38
|
+
res.status(Globals.Resp_STATUSCODE_EXCEPTION).json(Object.assign(Object.assign({}, e), { err: Globals.Resp_MSG_EXCEPTION, errCode: Globals.Resp_CODE_EXCEPTION }));
|
|
39
|
+
}
|
|
40
|
+
console.debug(`[Proxy] - Request took ${Date.now() - startTime}ms`);
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Processes the response received from a serverless function invocation and sends the appropriate response back to the client.
|
|
45
|
+
* @param {GenericHandlerEventResponse} invokation - The response object received from the serverless function invocation.
|
|
46
|
+
* @param {Response} res - The response object to send back to the client.
|
|
47
|
+
* @returns The response object to send back to the client.
|
|
48
|
+
*/
|
|
49
|
+
const processServerlessResponse = (invokation, res) => {
|
|
50
|
+
var _a, _b;
|
|
51
|
+
// translate answer to http layer
|
|
52
|
+
if (invokation && invokation.err) {
|
|
53
|
+
// err came
|
|
54
|
+
return res.json({ err: invokation.err }).status(400);
|
|
55
|
+
}
|
|
56
|
+
else if (!invokation || !invokation.data) {
|
|
57
|
+
// invalid response came
|
|
58
|
+
return res.status(Globals.Resp_STATUSCODE_INVALIDRESP).json({
|
|
59
|
+
err: Globals.Resp_MSG_INVALIDRESP,
|
|
60
|
+
errCode: Globals.Resp_CODE_INVALIDRESP,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
// Check for headers
|
|
65
|
+
if (invokation.data.headers) {
|
|
66
|
+
for (const hKey of Object.keys(invokation.data.headers)) {
|
|
67
|
+
res.header(hKey, invokation.data.headers[hKey]);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
//
|
|
71
|
+
let response = res.status(invokation.data.statusCode);
|
|
72
|
+
// check for stream
|
|
73
|
+
if ((_b = (_a = invokation.data) === null || _a === void 0 ? void 0 : _a.body) === null || _b === void 0 ? void 0 : _b.pipe) {
|
|
74
|
+
invokation.data.body.pipe(response);
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
response = response.json(Utils.parseObjectNullIfEmpty(invokation.data.body) || {});
|
|
78
|
+
}
|
|
79
|
+
return response;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
//# sourceMappingURL=GenericHandler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GenericHandler.js","sourceRoot":"","sources":["../../../../../src/Server/lib/container/GenericHandler.ts"],"names":[],"mappings":";;;;;;;;;AAEA,OAAO,mBAAoD,MAAM,0BAA0B,CAAA;AAC3F,OAAO,OAAO,MAAM,qBAAqB,CAAA;AACzC,OAAO,KAAK,MAAM,wBAAwB,CAAA;AAG1C;;;;GAIG;AACH,eAAe,CAAC,iBAAkD,EAAE,EAAE;IACpE;;;;;OAKG;IACH,OAAO,CAAO,OAAgB,EAAE,GAAa,EAAE,EAAE;QAC/C,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;QAC5B,IAAI,CAAC;YACH,qEAAqE;YACrE,MAAM,KAAK,GAAG,IAAI,mBAAmB,CAAC,OAAO,EAAE,iBAAiB,CAAC,CAAA;YACjE,SAAS;YACT,MAAM,cAAc,GAAG,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AK,MAAM,IAAI,IAAI,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC;gBACxD,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAA;YACjD,CAAC;QACH,CAAC;QACD,EAAE;QACF,IAAI,QAAQ,GAAG,GAAG,CAAC,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,UAAU,CAAC,CAAA;QACrD,mBAAmB;QACnB,IAAI,MAAA,MAAA,UAAU,CAAC,IAAI,0CAAE,IAAI,0CAAE,IAAI,EAAE,CAAC;YAChC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;QACrC,CAAC;aAAM,CAAC;YACN,QAAQ,GAAG,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,sBAAsB,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAA;QACpF,CAAC;QACD,OAAO,QAAQ,CAAA;IACjB,CAAC;AACH,CAAC,CAAA"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { Request } from 'express';
|
|
2
|
+
import Server from '../Server.js';
|
|
3
|
+
/**
|
|
4
|
+
* Represents the response object returned by a generic event handler.
|
|
5
|
+
* @typedef {Object} GenericHandlerEventResponse
|
|
6
|
+
* @property {Error | string} [err] - An optional error object or error message.
|
|
7
|
+
* @property {*} [data] - An optional data object.
|
|
8
|
+
*/
|
|
9
|
+
export type GenericHandlerEventResponse = {
|
|
10
|
+
err?: Error | string;
|
|
11
|
+
data?: any;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Represents a generic handler event for serverless functions.
|
|
15
|
+
*/
|
|
16
|
+
export default class GenericHandlerEvent {
|
|
17
|
+
/**
|
|
18
|
+
* Represents an HTTP request.
|
|
19
|
+
* @property {Request} request - The HTTP request object.
|
|
20
|
+
*/
|
|
21
|
+
request: Request;
|
|
22
|
+
/**
|
|
23
|
+
* The handler function for serverless events in a server.
|
|
24
|
+
* @param {Server['handleServerlessEvent']} serverlessHandler - The function that handles serverless events.
|
|
25
|
+
* @returns None
|
|
26
|
+
*/
|
|
27
|
+
serverlessHandler: Server['handleServerlessEvent'];
|
|
28
|
+
/**
|
|
29
|
+
* Constructs a new instance of the class.
|
|
30
|
+
* @param {Request} request - The request object.
|
|
31
|
+
* @param {Server['handleServerlessEvent']} serverlessHandler - The serverless handler function.
|
|
32
|
+
* @returns None
|
|
33
|
+
*/
|
|
34
|
+
constructor(request: Request, serverlessHandler: Server['handleServerlessEvent']);
|
|
35
|
+
/**
|
|
36
|
+
* Invokes the handler function asynchronously and returns a promise that resolves to a GenericHandlerEventResponse.
|
|
37
|
+
* @returns {Promise<GenericHandlerEventResponse>} A promise that resolves to a GenericHandlerEventResponse.
|
|
38
|
+
*/
|
|
39
|
+
invoke(): Promise<GenericHandlerEventResponse>;
|
|
40
|
+
/**
|
|
41
|
+
* Builds and returns an APIGatewayProxyEvent object based on the current request.
|
|
42
|
+
* @returns {APIGatewayProxyEvent} - The constructed APIGatewayProxyEvent object.
|
|
43
|
+
*/
|
|
44
|
+
private buildEvent;
|
|
45
|
+
/**
|
|
46
|
+
* Builds and returns a context object for an AWS Lambda function.
|
|
47
|
+
* @param {APIGatewayProxyEvent} event - The event object passed to the Lambda function.
|
|
48
|
+
* @param {(err?: Error | string, data?: any) => void} callback - The callback function to be called when the Lambda function completes.
|
|
49
|
+
* @returns {Context} - The context object for the Lambda function.
|
|
50
|
+
*/
|
|
51
|
+
private buildContext;
|
|
52
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import cuid from 'cuid';
|
|
11
|
+
import { parseMultiValueQueryStringParameters, parseQueryStringParameters } from './Utils.js';
|
|
12
|
+
import Globals from '../../../Globals.js';
|
|
13
|
+
/**
|
|
14
|
+
* Represents a generic handler event for serverless functions.
|
|
15
|
+
*/
|
|
16
|
+
export default class GenericHandlerEvent {
|
|
17
|
+
/**
|
|
18
|
+
* Constructs a new instance of the class.
|
|
19
|
+
* @param {Request} request - The request object.
|
|
20
|
+
* @param {Server['handleServerlessEvent']} serverlessHandler - The serverless handler function.
|
|
21
|
+
* @returns None
|
|
22
|
+
*/
|
|
23
|
+
constructor(request, serverlessHandler) {
|
|
24
|
+
this.request = request;
|
|
25
|
+
this.serverlessHandler = serverlessHandler;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Invokes the handler function asynchronously and returns a promise that resolves to a GenericHandlerEventResponse.
|
|
29
|
+
* @returns {Promise<GenericHandlerEventResponse>} A promise that resolves to a GenericHandlerEventResponse.
|
|
30
|
+
*/
|
|
31
|
+
invoke() {
|
|
32
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
33
|
+
// eslint-disable-next-line no-async-promise-executor
|
|
34
|
+
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
35
|
+
try {
|
|
36
|
+
// Build event and context
|
|
37
|
+
const event = this.buildEvent();
|
|
38
|
+
const context = this.buildContext(event, (err, data) => {
|
|
39
|
+
resolve({ err, data });
|
|
40
|
+
});
|
|
41
|
+
// Invoke
|
|
42
|
+
yield this.serverlessHandler(event, context);
|
|
43
|
+
}
|
|
44
|
+
catch (e) {
|
|
45
|
+
reject(e); // forward rejection
|
|
46
|
+
}
|
|
47
|
+
}));
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Builds and returns an APIGatewayProxyEvent object based on the current request.
|
|
52
|
+
* @returns {APIGatewayProxyEvent} - The constructed APIGatewayProxyEvent object.
|
|
53
|
+
*/
|
|
54
|
+
buildEvent() {
|
|
55
|
+
var _a, _b, _c, _d;
|
|
56
|
+
return {
|
|
57
|
+
body: this.request.body || null, //enforce key
|
|
58
|
+
rawBody: this.request['rawBody'],
|
|
59
|
+
headers: (this.request.headers || {}),
|
|
60
|
+
httpMethod: (_a = this.request.method) === null || _a === void 0 ? void 0 : _a.toUpperCase(),
|
|
61
|
+
isBase64Encoded: false,
|
|
62
|
+
multiValueHeaders: (this.request.headers || {}),
|
|
63
|
+
multiValueQueryStringParameters: parseMultiValueQueryStringParameters(this.request.url),
|
|
64
|
+
path: this.request.path,
|
|
65
|
+
pathParameters: null,
|
|
66
|
+
queryStringParameters: this.request.url ? parseQueryStringParameters(this.request.url) : {},
|
|
67
|
+
requestContext: {
|
|
68
|
+
accountId: process.env.AWS_ACCOUNT_ID || 'undefined',
|
|
69
|
+
apiId: '',
|
|
70
|
+
authorizer: null,
|
|
71
|
+
domainName: undefined,
|
|
72
|
+
domainPrefix: undefined,
|
|
73
|
+
extendedRequestId: cuid(),
|
|
74
|
+
httpMethod: this.request.method ? this.request.method.toUpperCase() : '',
|
|
75
|
+
identity: {
|
|
76
|
+
accessKey: null,
|
|
77
|
+
accountId: process.env.AWS_ACCOUNT_ID || null,
|
|
78
|
+
caller: null,
|
|
79
|
+
apiKey: null,
|
|
80
|
+
apiKeyId: null,
|
|
81
|
+
clientCert: null,
|
|
82
|
+
cognitoAuthenticationProvider: null,
|
|
83
|
+
cognitoAuthenticationType: null,
|
|
84
|
+
cognitoIdentityId: null,
|
|
85
|
+
cognitoIdentityPoolId: null,
|
|
86
|
+
principalOrgId: null,
|
|
87
|
+
sourceIp: ((_b = this.request.headers) === null || _b === void 0 ? void 0 : _b['x-forwarded-for']) ||
|
|
88
|
+
((_c = this.request.socket) === null || _c === void 0 ? void 0 : _c.remoteAddress) ||
|
|
89
|
+
'',
|
|
90
|
+
user: null,
|
|
91
|
+
userAgent: ((_d = this.request.headers) === null || _d === void 0 ? void 0 : _d['user-agent']) || null,
|
|
92
|
+
userArn: null,
|
|
93
|
+
},
|
|
94
|
+
path: this.request.path,
|
|
95
|
+
protocol: 'HTTP/1.1',
|
|
96
|
+
requestId: `${cuid()}-${cuid()}`,
|
|
97
|
+
requestTime: new Date().toISOString(),
|
|
98
|
+
requestTimeEpoch: Date.now(),
|
|
99
|
+
resourceId: '',
|
|
100
|
+
resourcePath: Globals.Listener_HTTP_ProxyRoute,
|
|
101
|
+
stage: process.env.STAGE || '',
|
|
102
|
+
},
|
|
103
|
+
resource: Globals.Listener_HTTP_ProxyRoute,
|
|
104
|
+
stageVariables: null,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Builds and returns a context object for an AWS Lambda function.
|
|
109
|
+
* @param {APIGatewayProxyEvent} event - The event object passed to the Lambda function.
|
|
110
|
+
* @param {(err?: Error | string, data?: any) => void} callback - The callback function to be called when the Lambda function completes.
|
|
111
|
+
* @returns {Context} - The context object for the Lambda function.
|
|
112
|
+
*/
|
|
113
|
+
buildContext(event, callback) {
|
|
114
|
+
return {
|
|
115
|
+
awsRequestId: event.requestContext.requestId,
|
|
116
|
+
callbackWaitsForEmptyEventLoop: true,
|
|
117
|
+
getRemainingTimeInMillis: () => {
|
|
118
|
+
return 0;
|
|
119
|
+
},
|
|
120
|
+
done: (err, data) => callback(err, data),
|
|
121
|
+
fail: err => callback(err),
|
|
122
|
+
succeed: res => callback(undefined, res),
|
|
123
|
+
functionName: 'container-function',
|
|
124
|
+
functionVersion: 'LATEST',
|
|
125
|
+
invokedFunctionArn: 'none',
|
|
126
|
+
memoryLimitInMB: '-1',
|
|
127
|
+
logGroupName: 'undefined',
|
|
128
|
+
logStreamName: 'undefined',
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
//# sourceMappingURL=GenericHandlerEvent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GenericHandlerEvent.js","sourceRoot":"","sources":["../../../../../src/Server/lib/container/GenericHandlerEvent.ts"],"names":[],"mappings":";;;;;;;;;AACA,OAAO,IAAI,MAAM,MAAM,CAAA;AAGvB,OAAO,EAAE,oCAAoC,EAAE,0BAA0B,EAAE,MAAM,YAAY,CAAA;AAC7F,OAAO,OAAO,MAAM,qBAAqB,CAAA;AAezC;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,mBAAmB;IAatC;;;;;OAKG;IACH,YAAY,OAAgB,EAAE,iBAAkD;QAC9E,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;QACtB,IAAI,CAAC,iBAAiB,GAAG,iBAAiB,CAAA;IAC5C,CAAC;IAED;;;OAGG;IACU,MAAM;;YACjB,qDAAqD;YACrD,OAAO,IAAI,OAAO,CAAC,CAAO,OAAO,EAAE,MAAM,EAAE,EAAE;gBAC3C,IAAI,CAAC;oBACH,0BAA0B;oBAC1B,MAAM,KAAK,GAAG,IAAI,CAAC,UAAU,EAAE,CAAA;oBAC/B,MAAM,OAAO,GAAG,IAAI,CAAC,YAAY,CAAC,KAAK,EAAE,CAAC,GAAoB,EAAE,IAAU,EAAE,EAAE;wBAC5E,OAAO,CAAC,EAAE,GAAG,EAAE,IAAI,EAAE,CAAC,CAAA;oBACxB,CAAC,CAAC,CAAA;oBACF,SAAS;oBACT,MAAM,IAAI,CAAC,iBAAiB,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;gBAC9C,CAAC;gBAAC,OAAO,CAAC,EAAE,CAAC;oBACX,MAAM,CAAC,CAAC,CAAC,CAAA,CAAC,oBAAoB;gBAChC,CAAC;YACH,CAAC,CAAA,CAAC,CAAA;QACJ,CAA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,IAAI,CAAC;YACxC,IAAI,EAAE,GAAG,CAAC,EAAE,CAAC,QAAQ,CAAC,GAAG,CAAC;YAC1B,OAAO,EAAE,GAAG,CAAC,EAAE,CAAC,QAAQ,CAAC,SAAS,EAAE,GAAG,CAAC;YACxC,YAAY,EAAE,oBAAoB;YAClC,eAAe,EAAE,QAAQ;YACzB,kBAAkB,EAAE,MAAM;YAC1B,eAAe,EAAE,IAAI;YACrB,YAAY,EAAE,WAAW;YACzB,aAAa,EAAE,WAAW;SAC3B,CAAA;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Request, Response } from 'express';
|
|
2
|
+
/**
|
|
3
|
+
* Handles a request and sends a response with the message 'Healthy!'.
|
|
4
|
+
* @param {Request} _request - The request object.
|
|
5
|
+
* @param {Response} res - The response object.
|
|
6
|
+
* @returns None
|
|
7
|
+
*/
|
|
8
|
+
declare const _default: (_request: Request, res: Response) => Promise<void>;
|
|
9
|
+
export default _default;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Handles a request and sends a response with the message 'Healthy!'.
|
|
12
|
+
* @param {Request} _request - The request object.
|
|
13
|
+
* @param {Response} res - The response object.
|
|
14
|
+
* @returns None
|
|
15
|
+
*/
|
|
16
|
+
export default (_request, res) => __awaiter(void 0, void 0, void 0, function* () {
|
|
17
|
+
res.send('Healthy!');
|
|
18
|
+
});
|
|
19
|
+
//# sourceMappingURL=HealthHandler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"HealthHandler.js","sourceRoot":"","sources":["../../../../../src/Server/lib/container/HealthHandler.ts"],"names":[],"mappings":";;;;;;;;;AAEA;;;;;GAKG;AACH,eAAe,CAAO,QAAiB,EAAE,GAAa,EAAE,EAAE;IACxD,GAAG,CAAC,IAAI,CAAC,UAAU,CAAC,CAAA;AACtB,CAAC,CAAA,CAAA"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import Server from './../Server.js';
|
|
2
|
+
import { RouterConfig } from '../../Router.js';
|
|
3
|
+
/**
|
|
4
|
+
* Represents a Proxy class that handles routing and server functionality.
|
|
5
|
+
*/
|
|
6
|
+
export default class Proxy {
|
|
7
|
+
/**
|
|
8
|
+
* A boolean flag indicating whether the process is currently stopping or not.
|
|
9
|
+
*/
|
|
10
|
+
private stopping;
|
|
11
|
+
/**
|
|
12
|
+
* The configuration object for the router.
|
|
13
|
+
*/
|
|
14
|
+
private readonly config;
|
|
15
|
+
/**
|
|
16
|
+
* The Express application instance for the server.
|
|
17
|
+
* @readonly
|
|
18
|
+
* @type {express.Express}
|
|
19
|
+
*/
|
|
20
|
+
private readonly app;
|
|
21
|
+
/**
|
|
22
|
+
* The handler function for serverless events in the Server class.
|
|
23
|
+
* @param {ServerlessEvent} event - The serverless event object.
|
|
24
|
+
* @returns None
|
|
25
|
+
*/
|
|
26
|
+
private readonly serverlessHandler;
|
|
27
|
+
/**
|
|
28
|
+
* Represents a listener for an HTTP server.
|
|
29
|
+
* @private
|
|
30
|
+
* @type {HTTPServer}
|
|
31
|
+
*/
|
|
32
|
+
private listener;
|
|
33
|
+
/**
|
|
34
|
+
* Constructs a new instance of the Router class.
|
|
35
|
+
* @param {RouterConfig} config - The configuration object for the router.
|
|
36
|
+
* @param {Server['handleServerlessEvent']} serverlessHandler - The handler function for serverless events.
|
|
37
|
+
* @returns None
|
|
38
|
+
*/
|
|
39
|
+
constructor(config: RouterConfig, serverlessHandler: Server['handleServerlessEvent']);
|
|
40
|
+
/**
|
|
41
|
+
* Loads the necessary components and initializes the application.
|
|
42
|
+
* @returns None
|
|
43
|
+
*/
|
|
44
|
+
load(): Promise<void>;
|
|
45
|
+
/**
|
|
46
|
+
* Unloads the current module, stopping any active listeners.
|
|
47
|
+
* @param {any} [err] - Optional error object to pass to the stopListeners method.
|
|
48
|
+
* @returns {Promise<void>} - A promise that resolves once the listeners have been stopped.
|
|
49
|
+
*/
|
|
50
|
+
unload(err?: any): Promise<void>;
|
|
51
|
+
/**
|
|
52
|
+
* Starts the listeners for the proxy server.
|
|
53
|
+
* @returns {Promise<void>} A promise that resolves when the listeners have started.
|
|
54
|
+
*/
|
|
55
|
+
private startListeners;
|
|
56
|
+
/**
|
|
57
|
+
* Stops the listeners and exits the process.
|
|
58
|
+
* @param {any} [err] - Optional error object.
|
|
59
|
+
* @returns {Promise<void>} - A promise that resolves when the listeners are stopped and the process is exited.
|
|
60
|
+
*/
|
|
61
|
+
private stopListeners;
|
|
62
|
+
/**
|
|
63
|
+
* Installs the routes for the proxy server.
|
|
64
|
+
* @returns None
|
|
65
|
+
*/
|
|
66
|
+
private installRoutes;
|
|
67
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import fs from 'fs';
|
|
11
|
+
import { createServer } from 'http';
|
|
12
|
+
import cors from 'cors';
|
|
13
|
+
import express from 'express';
|
|
14
|
+
import GenericHandler from './GenericHandler.js';
|
|
15
|
+
import HealthHandler from './HealthHandler.js';
|
|
16
|
+
import Globals from '../../../Globals.js';
|
|
17
|
+
import Utils from '../../../Util/Utils.js';
|
|
18
|
+
/* Get package.json version from IKApi on ESM */
|
|
19
|
+
const { version: appVersion } = JSON.parse(fs.readFileSync('package.json').toString());
|
|
20
|
+
/**
|
|
21
|
+
* Represents a Proxy class that handles routing and server functionality.
|
|
22
|
+
*/
|
|
23
|
+
export default class Proxy {
|
|
24
|
+
/**
|
|
25
|
+
* Constructs a new instance of the Router class.
|
|
26
|
+
* @param {RouterConfig} config - The configuration object for the router.
|
|
27
|
+
* @param {Server['handleServerlessEvent']} serverlessHandler - The handler function for serverless events.
|
|
28
|
+
* @returns None
|
|
29
|
+
*/
|
|
30
|
+
constructor(config, serverlessHandler) {
|
|
31
|
+
this.stopping = false;
|
|
32
|
+
this.config = config;
|
|
33
|
+
this.serverlessHandler = serverlessHandler;
|
|
34
|
+
this.app = express();
|
|
35
|
+
/* Opinionated Express configs */
|
|
36
|
+
this.app.use(express.json({
|
|
37
|
+
verify(req, res, buf) {
|
|
38
|
+
req['rawBody'] = buf;
|
|
39
|
+
},
|
|
40
|
+
}));
|
|
41
|
+
// apply cors config
|
|
42
|
+
const corsConfig = this.config.cors || Utils.parseObjectNullIfEmpty(process.env.CORS);
|
|
43
|
+
this.app.use(cors(corsConfig
|
|
44
|
+
? {
|
|
45
|
+
origin: corsConfig.origin,
|
|
46
|
+
allowedHeaders: corsConfig.headers,
|
|
47
|
+
credentials: !!corsConfig.allowCredentials,
|
|
48
|
+
}
|
|
49
|
+
: {}));
|
|
50
|
+
// //This supposedly fix some 502 codes where nodejs socket would hang during
|
|
51
|
+
// //a request and if behind ALB, it would cause 502 codes. Had experiencied this
|
|
52
|
+
// //and 502 codes reduced dramastically, but still some appearances. Maybe this
|
|
53
|
+
// //is just a palliative work-around for the real issue; TODO: need to investigate
|
|
54
|
+
// //in the future.
|
|
55
|
+
// this.listener.listener.keepAliveTimeout = 120e3
|
|
56
|
+
// this.listener.listener.headersTimeout = 120e3
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Loads the necessary components and initializes the application.
|
|
60
|
+
* @returns None
|
|
61
|
+
*/
|
|
62
|
+
load() {
|
|
63
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
64
|
+
yield this.startListeners();
|
|
65
|
+
this.installRoutes();
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Unloads the current module, stopping any active listeners.
|
|
70
|
+
* @param {any} [err] - Optional error object to pass to the stopListeners method.
|
|
71
|
+
* @returns {Promise<void>} - A promise that resolves once the listeners have been stopped.
|
|
72
|
+
*/
|
|
73
|
+
unload(err) {
|
|
74
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
75
|
+
yield this.stopListeners(err);
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Starts the listeners for the proxy server.
|
|
80
|
+
* @returns {Promise<void>} A promise that resolves when the listeners have started.
|
|
81
|
+
*/
|
|
82
|
+
startListeners() {
|
|
83
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
84
|
+
// eslint-disable-next-line no-async-promise-executor
|
|
85
|
+
return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
|
|
86
|
+
const port = this.config.port || Globals.Listener_HTTP_DefaultPort;
|
|
87
|
+
console.log(`[Proxy] - [STARTING] - v.${appVersion} - :${port}`);
|
|
88
|
+
// Create Server
|
|
89
|
+
this.listener = createServer(this.app);
|
|
90
|
+
// Set defaults
|
|
91
|
+
this.listener.setTimeout(this.config.timeout || Globals.Listener_HTTP_DefaultTimeout);
|
|
92
|
+
// Call hook if available
|
|
93
|
+
if (this.config.containerSetupHook)
|
|
94
|
+
yield this.config.containerSetupHook(this.listener, this.app);
|
|
95
|
+
// Start Server
|
|
96
|
+
this.listener.listen(port, () => {
|
|
97
|
+
console.log(`[Proxy] - [STARTED]`);
|
|
98
|
+
resolve();
|
|
99
|
+
});
|
|
100
|
+
}));
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Stops the listeners and exits the process.
|
|
105
|
+
* @param {any} [err] - Optional error object.
|
|
106
|
+
* @returns {Promise<void>} - A promise that resolves when the listeners are stopped and the process is exited.
|
|
107
|
+
*/
|
|
108
|
+
stopListeners(err) {
|
|
109
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
110
|
+
if (this.stopping)
|
|
111
|
+
return;
|
|
112
|
+
this.stopping = true;
|
|
113
|
+
console.debug('[Proxy] - [STOPPING]');
|
|
114
|
+
return new Promise(resolve => {
|
|
115
|
+
this.listener.close(_err => {
|
|
116
|
+
const err2 = err || _err;
|
|
117
|
+
if (err2)
|
|
118
|
+
console.log('[Proxy] - exit output:', err2);
|
|
119
|
+
console.log('[Proxy] - [STOPPED]');
|
|
120
|
+
process.exit(err2 ? 1 : 0);
|
|
121
|
+
resolve(null);
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Installs the routes for the proxy server.
|
|
128
|
+
* @returns None
|
|
129
|
+
*/
|
|
130
|
+
installRoutes() {
|
|
131
|
+
//Health check route -- This is a bypass route to only check if
|
|
132
|
+
//runtime proxy is working and responding to calls.
|
|
133
|
+
console.log(`[Proxy] - [HEALTH-ROUTE] - ${this.config.healthCheckRoute || Globals.Listener_HTTP_DefaultHealthCheckRoute}`);
|
|
134
|
+
this.app
|
|
135
|
+
.route(this.config.healthCheckRoute || Globals.Listener_HTTP_DefaultHealthCheckRoute)
|
|
136
|
+
.get(HealthHandler);
|
|
137
|
+
//Main route -- We use a wildcard route because is not the job of the runtime and neither
|
|
138
|
+
//the task to deny/constrain routes that invoked this task; all the job is done by the
|
|
139
|
+
//load balancer and we just foward everything we have to the function.
|
|
140
|
+
this.app.route(Globals.Listener_HTTP_ProxyRoute).all(GenericHandler(this.serverlessHandler));
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
//# sourceMappingURL=Proxy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Proxy.js","sourceRoot":"","sources":["../../../../../src/Server/lib/container/Proxy.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,MAAM,IAAI,CAAA;AACnB,OAAO,EAAwB,YAAY,EAAE,MAAM,MAAM,CAAA;AAEzD,OAAO,IAAI,MAAM,MAAM,CAAA;AACvB,OAAO,OAAO,MAAM,SAAS,CAAA;AAG7B,OAAO,cAAc,MAAM,qBAAqB,CAAA;AAChD,OAAO,aAAa,MAAM,oBAAoB,CAAA;AAC9C,OAAO,OAAO,MAAM,qBAAqB,CAAA;AACzC,OAAO,KAAK,MAAM,wBAAwB,CAAA;AAG1C,gDAAgD;AAChD,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,YAAY,CAAC,cAAc,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAA;AAEtF;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,KAAK;IA4BxB;;;;;OAKG;IACH,YAAY,MAAoB,EAAE,iBAAkD;QAClF,IAAI,CAAC,QAAQ,GAAG,KAAK,CAAA;QACrB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;QACpB,IAAI,CAAC,iBAAiB,GAAG,iBAAiB,CAAA;QAC1C,IAAI,CAAC,GAAG,GAAG,OAAO,EAAE,CAAA;QACpB,iCAAiC;QACjC,IAAI,CAAC,GAAG,CAAC,GAAG,CACV,OAAO,CAAC,IAAI,CAAC;YACX,MAAM,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG;gBAClB,GAAG,CAAC,SA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