lemon-core
Version:
Lemon Serverless Micro-Service Platform
166 lines • 6.87 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LambdaALBHandler = exports.ALBHttpHeaderTool = void 0;
/**
* `lambda-alb-handler.ts`
* - lambda handler to process ALB event.
*
*
* @author Steve Jung <steve@lemoncloud.io>
* @date 2025-05-14 initial version via backbone
*
* @copyright (C) 2025 LemonCloud Co Ltd. - All Rights Reserved.
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const engine_1 = require("../../engine/");
const lambda_handler_1 = require("./lambda-handler");
const lambda_web_handler_1 = require("./lambda-web-handler");
const NS = engine_1.$U.NS('HCRN', 'yellow'); // NAMESPACE TO BE PRINTED.
/**
* ALB Http Header Tool
*/
class ALBHttpHeaderTool {
constructor(headers, options) {
this.tool = new lambda_web_handler_1.MyHttpHeaderTool(headers, options);
}
hello() {
return this.tool.hello();
}
getHeaders(name) {
return this.tool.getHeaders(name);
}
getHeader(name) {
return this.tool.getHeader(name);
}
parseIdentityHeader(name) {
return this.tool.parseIdentityHeader(name);
}
parseLanguageHeader(name) {
return this.tool.parseLanguageHeader(name);
}
parseCookiesHeader(name) {
return this.tool.parseCookiesHeader(name);
}
prepareContext($org, reqContext) {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
const errScope = `alb.prepareContext(${(_a = $org === null || $org === void 0 ? void 0 : $org.requestId) !== null && _a !== void 0 ? _a : ''})`;
if (typeof ((_b = reqContext === null || reqContext === void 0 ? void 0 : reqContext.elb) === null || _b === void 0 ? void 0 : _b.targetGroupArn) !== 'string')
throw new Error(`.targetGroupArn is invalid - ${errScope}`);
return Object.assign({}, $org);
});
}
onlyDefined(obj) {
return this.tool.onlyDefined(obj);
}
}
exports.ALBHttpHeaderTool = ALBHttpHeaderTool;
/**
* class: LambdaALBHandler
* - default ALB Handler w/ event-listeners.
*/
class LambdaALBHandler extends lambda_handler_1.LambdaSubHandler {
/**
* default constructor w/ registering self.
*/
constructor(lambda, register) {
super(lambda, register ? 'alb' : undefined);
/**
* Default ALB Handler.
*/
this.handle = (event, context) => __awaiter(this, void 0, void 0, function* () {
var _a, _b;
(0, engine_1._log)(NS, `handle()...`);
if (!this.handler)
return this.buildResponse(403, '403 NOT SUPPORTED - no handler for alb');
const arn = (_b = (_a = event === null || event === void 0 ? void 0 : event.requestContext) === null || _a === void 0 ? void 0 : _a.elb) === null || _b === void 0 ? void 0 : _b.targetGroupArn;
const $res = yield this.handler(arn, this, event, context);
return $res;
});
/**
* builder of tools for http-headers
* - extracting header content, and parse.
*/
this.tools = (event) => new ALBHttpHeaderTool(event === null || event === void 0 ? void 0 : event.headers);
// _log(NS, `LambdaALBHandler()..`);
}
/** say hello */
hello() {
return `lambda-alb-handler`;
}
/**
* set handler of alb-event.
* @param handler
*/
setHandler(handler) {
const prev = this.handler;
this.handler = handler;
return prev;
}
/**
* build response for ALB.
*
* @param statusCode status code like 200, 404, etc.
* @param body any body to be returned.
* @param options (optional) options for content-type and origin.
* @returns ALBResult
*/
buildResponse(statusCode, body, options) {
var _a, _b;
const origin = (_a = options === null || options === void 0 ? void 0 : options.origin) !== null && _a !== void 0 ? _a : null;
const credentials = (_b = options === null || options === void 0 ? void 0 : options.credentials) !== null && _b !== void 0 ? _b : null;
return (0, lambda_web_handler_1.buildResponse)(statusCode, body, Object.assign(Object.assign({}, options), { origin, credentials }));
}
/**
* pack the request context for Http request.
*
* @param event origin Event.
* @param orgContext (optional) original lambda.Context
*/
packContext(event, orgContext) {
return __awaiter(this, void 0, void 0, function* () {
(0, engine_1._log)(NS, `packContext(${event ? '' : 'null'})..`);
if (!event)
return null;
//* prepare chain object.
const reqContext = event === null || event === void 0 ? void 0 : event.requestContext;
orgContext && (0, engine_1._log)(NS, `> orgContext =`, engine_1.$U.S(orgContext, 256, 32));
reqContext && (0, engine_1._log)(NS, `> reqContext =`, engine_1.$U.S(reqContext, 256, 32));
// STEP.1 extract the key headers.
const $tool = this.tools(event);
const identity = yield $tool.parseIdentityHeader();
const _prepare = () => {
const cookie = $tool.parseCookiesHeader();
const domain = $tool.getHeader('host');
const referer = $tool.getHeader('referer');
const origin = $tool.getHeader('origin');
const userAgent = $tool.getHeader('user-agent');
const authorization = $tool.getHeader('authorization');
return $tool.onlyDefined({
identity,
cookie,
domain,
referer,
origin,
userAgent,
authorization,
});
};
// STEP.3. prepare the final `next-context`.
const $ctx = yield $tool.prepareContext(_prepare(), reqContext);
// FINIAL. returns
return $ctx;
});
}
}
exports.LambdaALBHandler = LambdaALBHandler;
//# sourceMappingURL=lambda-alb-handler.js.map
;