lemon-core
Version:
Lemon Serverless Micro-Service Platform
93 lines • 3.79 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.LambdaWSSHandler = exports.failure = exports.notfound = exports.success = exports.buildResponse = void 0;
/**
* `lambda-wss-handler.ts`
* - lambda handler to process WSS 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 NS = engine_1.$U.NS('HWSS', 'yellow'); // NAMESPACE TO BE PRINTED.
const buildResponse = (statusCode, body) => {
// @0612 - body 가 string일 경우, 응답형식을 텍스트로 바꿔서 출력한다.
return {
statusCode,
headers: {
'Content-Type': typeof body === 'string'
? body.startsWith('<') && body.endsWith('>')
? 'text/html; charset=utf-8'
: 'text/plain; charset=utf-8'
: 'application/json; charset=utf-8',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true, // Required for cookies, authorization headers with HTTPS
},
body: typeof body === 'string' ? body : JSON.stringify(body),
};
};
exports.buildResponse = buildResponse;
const success = (body) => {
return (0, exports.buildResponse)(200, body);
};
exports.success = success;
const notfound = (body) => {
return (0, exports.buildResponse)(404, body);
};
exports.notfound = notfound;
const failure = (body) => {
return (0, exports.buildResponse)(503, body);
};
exports.failure = failure;
/**
* class: LambdaWSSHandler
* - default WSS Handler w/ event-listeners.
*/
class LambdaWSSHandler extends lambda_handler_1.LambdaSubHandler {
/**
* default constructor w/ registering self.
*/
constructor(lambda, register) {
super(lambda, register ? 'wss' : undefined);
/**
* Default WSS Handler.
*
* example:
* ```js
* $ npm install -g wscat
* $ wscat -c wss://6ye6t5py3i.execute-api.ap-northeast-2.amazonaws.com/dev
* > {"action":"echo"}
* ```
*/
this.handle = (event) => __awaiter(this, void 0, void 0, function* () {
//* for each records.
(0, engine_1._log)(NS, `handle()...`);
(0, engine_1._log)(NS, '> event =', engine_1.$U.json(event));
const $req = event.requestContext;
const EVENT_TYPE = $req.eventType || '';
const ROUTE_KEY = $req.routeKey || '';
(0, engine_1._log)(NS, `> route(${ROUTE_KEY}/${EVENT_TYPE})...`);
return (0, exports.success)('ok');
});
// _log(NS, `LambdaWSSHandler()..`);
}
addListener() { }
}
exports.LambdaWSSHandler = LambdaWSSHandler;
//* shared config.
LambdaWSSHandler.REPORT_ERROR = lambda_handler_1.LambdaHandler.REPORT_ERROR;
//# sourceMappingURL=lambda-wss-handler.js.map
;