UNPKG

lemon-core

Version:
127 lines 7.07 kB
"use strict"; 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.LambdaSQSHandler = void 0; /** * `lambda-sqs-handler.ts` * - lambda handler to process SQS event. * * * @author Steve Jung <steve@lemoncloud.io> * @date 2019-11-20 initial version via backbone * * @copyright (C) 2019 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 protocol_1 = __importDefault(require("../protocol/")); const NS = engine_1.$U.NS('HSQS', 'yellow'); // NAMESPACE TO BE PRINTED. /** * class: LambdaSQSHandler * - default SQS Handler w/ event-listeners. */ class LambdaSQSHandler extends lambda_handler_1.LambdaSubHandler { /** * default constructor w/ registering self. */ constructor(lambda, register) { super(lambda, register ? 'sqs' : undefined); this.listeners = []; //* for debugging. save last result this.$lastResult = null; /** * Default SQS Handler. */ this.handle = (event) => __awaiter(this, void 0, void 0, function* () { //* for each records. const records = event.Records || []; (0, engine_1._log)(NS, `handle(len=${records.length})...`); // _log(NS, '> event =', $U.json(event)); const $doReportError = (0, lambda_handler_1.buildReportError)(LambdaSQSHandler.REPORT_ERROR); //* handle sqs record data. const onSQSRecord = (record, index) => __awaiter(this, void 0, void 0, function* () { (0, engine_1._log)(NS, `onSQSRecord(${(record && record.messageId) || ''}, ${index})...`); //* retrieve message-attributes as `param` const param = Object.keys(record.messageAttributes || {}).reduce((O, key) => { const V = record.messageAttributes[key]; if (!V) return O; const type = V.dataType || ''; const val = V.stringValue || ''; O[key] = type == 'Number' ? Number(val) : val; return O; }, {}); //* check if via protocol-service. if (param['Subject'] && param['Subject'] == 'x-protocol-service') { const param = protocol_1.default.service.asTransformer('sqs').transformToParam(record); const context = param.context; const callback = param.callback || ''; const result = yield this.lambda .handleProtocol(param) .then(body => { // _log(NS, `! res[${index}] =`, $U.json(body)); callback && (0, engine_1._log)(NS, `> callback[${index}] =`, callback); // ex) api://lemon-queue-api-dev/batch/test11/callback#2.2.1 context && (0, engine_1._log)(NS, `> context[${index}] =`, engine_1.$U.json(context)); // ex) {"source":"express","domain":"localhost"} //* report call back. const proto = callback ? protocol_1.default.service.fromURL(context, callback, null, body || {}) : null; proto && (0, engine_1._log)(NS, `> protocol[${index}] =`, engine_1.$U.json(proto)); (0, engine_1._log)(NS, `> config.service =`, this.lambda.config && this.lambda.config.getService()); //* check if service is in same.. if (proto && this.lambda.config && proto.service == this.lambda.config.getService()) { proto.context.depth = engine_1.$U.N(proto.context.depth, 1) + 1; proto.body = body; (0, engine_1._log)(NS, `! body[${index}] =`, engine_1.$U.json(body)); return this.lambda.handleProtocol(proto).then(body => { (0, engine_1._log)(NS, `>> body[${index}].callback =`, engine_1.$U.json(body)); return body; }); } //* call the remote service if callback. return proto ? protocol_1.default.service.execute(proto) : body; }) .catch(e => $doReportError(e, param.context, null, { protocol: param })); (0, engine_1._log)(NS, `> sqs[${index}].res =`, engine_1.$U.json(result)); return typeof result == 'string' ? result : engine_1.$U.json(result); } else { //* load data as `body` const body = typeof record.body == 'string' && record.body.startsWith('{') && record.body.endsWith('}') ? JSON.parse(record.body) : { data: record.body }; (0, engine_1._log)(NS, `> sqs[${index}].param =`, engine_1.$U.json(param)); (0, engine_1._log)(NS, `> sqs[${index}].body =`, engine_1.$U.json(body)); //* call all listeners in parrallel. const asyncNext = (fn, j) => new Promise(resolve => { resolve(fn('SQS', param, body, null)); }).catch(e => $doReportError(e, null, null, { param, body, i: index, j })); const res = yield Promise.all(this.listeners.map(asyncNext)); return res.join(','); } }); //* serialize each records. this.$lastResult = yield (0, engine_1.do_parrallel)(records, (record, i) => onSQSRecord(record, i).catch(e => $doReportError(e, null, null, { record, i })), 1); //* returns void. return; }); // _log(NS, `LambdaSQSHandler()..`); } addListener(handler) { this.listeners.push(handler); } } exports.LambdaSQSHandler = LambdaSQSHandler; //* shared config. LambdaSQSHandler.REPORT_ERROR = lambda_handler_1.LambdaHandler.REPORT_ERROR; //# sourceMappingURL=lambda-sqs-handler.js.map