@heathmont/lab-flask
Advanced tools
Comparing version 0.3.22 to 0.3.23-1
@@ -6,3 +6,3 @@ /// <reference types="node" /> | ||
export declare const privateImplemenSubstance: (labConfig: LabConfig, substance: string, implementations: Implementations) => void; | ||
export declare const implement: <REQ extends import("./types/types").PartialBy<import("./types/types").ProtobufMessageWithToJSON<REQ>, "$type">, RES extends import("./types/types").PartialBy<import("./types/types").ProtobufMessageWithToJSON<RES>, "$type">>(requestClass: new () => REQ, processor: (req: REQ, context: Context, rawReq: express.Request) => RES) => (reqData: Buffer, context: Context, rawReq: express.Request) => RES; | ||
export declare const implement: <REQ extends import("./types/types").PartialBy<import("./types/types").ProtobufMessageWithToJSON<REQ>, "$type">, RES extends import("./types/types").PartialBy<import("./types/types").ProtobufMessageWithToJSON<RES>, "$type">>(requestClass: new () => REQ, processor: (req: REQ, context: Context, rawReq: express.Request) => RES | Promise<RES>) => (reqData: Buffer, context: Context, rawReq: express.Request) => Promise<RES>; | ||
export interface ReactionImplementations { | ||
@@ -9,0 +9,0 @@ [reactionEnum: string]: (reqDataR: Buffer, context: Context, rawReq: express.Request) => any; |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
var _this = this; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -12,28 +48,39 @@ var createUrls_1 = require("./createUrls"); | ||
node_logger_1.logger.debug("Attaching binding " + labConfig.flaskPathnameProto + "/" + actPath + " with async function"); | ||
expressServer.post(labConfig.flaskPathnameProto + "/" + actPath, function (req, res) { | ||
node_logger_1.logger.debug('Handling POST for url', { url: labConfig.flaskPathnameProto + "/" + actPath, | ||
body: req.body, | ||
queryC: req.query.c, | ||
expressServer.post(labConfig.flaskPathnameProto + "/" + actPath, function (req, res) { return __awaiter(_this, void 0, void 0, function () { | ||
var isSignatureValid, context, responseProtobuf, e_1, responseData; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
node_logger_1.logger.debug('Handling POST for url', { url: labConfig.flaskPathnameProto + "/" + actPath, | ||
body: req.body, | ||
queryC: req.query.c, | ||
}); | ||
isSignatureValid = labConfig.hmCrypto.isValid(req.body, req.headers['x-lab-signature']); | ||
context = createUrls_1.getContextFromQueryString(req.query.c, actKindEnum); | ||
if (!isSignatureValid) { | ||
node_logger_1.logger.warn('Lab signature invalid', { actPath: actPath, context: context, requestHeaders: req.headers }); | ||
return [2 /*return*/, res.sendStatus(400)]; | ||
} | ||
_a.label = 1; | ||
case 1: | ||
_a.trys.push([1, 3, , 4]); | ||
node_logger_1.logger.debug('Before executing implementation', { SOMETHING: responseProtobuf }); | ||
return [4 /*yield*/, implementation(req.body, context, req)]; | ||
case 2: | ||
responseProtobuf = _a.sent(); | ||
node_logger_1.logger.info('After executing implementation', responseProtobuf); | ||
return [3 /*break*/, 4]; | ||
case 3: | ||
e_1 = _a.sent(); | ||
node_logger_1.logger.error('Got error on executing implementation', e_1); | ||
res.sendStatus(400, 'Impossible to process'); | ||
return [2 /*return*/]; | ||
case 4: | ||
responseData = responseProtobuf.constructor.encode(responseProtobuf).finish(); | ||
res.set('content-type', 'application/x-protobuf'); | ||
res.send(responseData); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
var isSignatureValid = labConfig.hmCrypto.isValid(req.body, req.headers['x-lab-signature']); | ||
var context = createUrls_1.getContextFromQueryString(req.query.c, actKindEnum); | ||
if (!isSignatureValid) { | ||
node_logger_1.logger.warn('Lab signature invalid', { actPath: actPath, context: context, requestHeaders: req.headers }); | ||
return res.sendStatus(400); | ||
} | ||
var responseProtobuf; | ||
try { | ||
node_logger_1.logger.debug('Before executing implementation', { SOMETHING: responseProtobuf }); | ||
responseProtobuf = implementation(req.body, context, req); | ||
node_logger_1.logger.info('After executing implementation', responseProtobuf); | ||
} | ||
catch (e) { | ||
node_logger_1.logger.error('Got error on executing implementation', e); | ||
res.sendStatus(400, 'Impossible to process'); | ||
return; | ||
} | ||
var responseData = responseProtobuf.constructor.encode(responseProtobuf).finish(); | ||
res.set('content-type', 'application/x-protobuf'); | ||
res.send(responseData); | ||
}); | ||
}); }); | ||
}; | ||
@@ -54,13 +101,19 @@ for (var _i = 0, implementationKeys_1 = implementationKeys; _i < implementationKeys_1.length; _i++) { | ||
exports.implement = function (requestClass, processor) { | ||
return function (reqData, context, rawReq) { | ||
return function (reqData, context, rawReq) { return __awaiter(_this, void 0, void 0, function () { | ||
var req; | ||
try { | ||
req = decodeRequestBody(requestClass, reqData); | ||
} | ||
catch (e) { | ||
node_logger_1.logger.error('Error decoding body', { reqBodyLength: rawReq.body.toString().length }); | ||
throw e; | ||
} | ||
return processor(req, context, rawReq); | ||
}; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
try { | ||
req = decodeRequestBody(requestClass, reqData); | ||
} | ||
catch (e) { | ||
node_logger_1.logger.error('Error decoding body', { reqBodyLength: rawReq.body.toString().length }); | ||
throw e; | ||
} | ||
return [4 /*yield*/, processor(req, context, rawReq)]; | ||
case 1: return [2 /*return*/, _a.sent()]; | ||
} | ||
}); | ||
}); }; | ||
}; | ||
@@ -67,0 +120,0 @@ var decodeRequestBody = function (requestClass, reqData) { |
{ | ||
"name": "@heathmont/lab-flask", | ||
"version": "0.3.22", | ||
"version": "0.3.23-1", | ||
"author": "Margus Lamp", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -19,3 +19,3 @@ import { Proto } from '@heathmont/coingaming-protobuf'; | ||
logger.debug(`Attaching binding ${labConfig.flaskPathnameProto}/${actPath} with async function`); | ||
expressServer.post(`${labConfig.flaskPathnameProto}/${actPath}`, (req: express.Request, res) => { | ||
expressServer.post(`${labConfig.flaskPathnameProto}/${actPath}`, async (req: express.Request, res) => { | ||
logger.debug('Handling POST for url', { url: `${labConfig.flaskPathnameProto}/${actPath}`, | ||
@@ -34,3 +34,3 @@ body: req.body, | ||
logger.debug('Before executing implementation', { SOMETHING: responseProtobuf }); | ||
responseProtobuf = implementation(req.body, context, req); | ||
responseProtobuf = await implementation(req.body, context, req); | ||
logger.info('After executing implementation', responseProtobuf); | ||
@@ -61,5 +61,4 @@ } catch (e) { | ||
requestClass: { new (): REQ; }, | ||
processor: (req: REQ, context: Context, rawReq: express.Request) => RES): | ||
(reqData: Buffer, context: Context, rawReq: express.Request) => RES => { | ||
return (reqData: Buffer, context: Context, rawReq: express.Request) => { | ||
processor: (req: REQ, context: Context, rawReq: express.Request) => RES | Promise<RES>) => { | ||
return async (reqData: Buffer, context: Context, rawReq: express.Request) => { | ||
let req; | ||
@@ -72,3 +71,3 @@ try { | ||
} | ||
return processor(req, context, rawReq); | ||
return await processor(req, context, rawReq); | ||
}; | ||
@@ -75,0 +74,0 @@ }; |
@@ -208,3 +208,3 @@ import axios from 'axios'; | ||
}; | ||
expressServer.post.mock.calls[2][1](req, res); | ||
await expressServer.post.mock.calls[2][1](req, res); | ||
expect(res.sendStatus.mock.calls.length).toBe(0); | ||
@@ -211,0 +211,0 @@ expect(res.set.mock.calls[0][0]).toBe('content-type'); |
Sorry, the diff of this file is not supported yet
123474
2180