@befaas/lib
Advanced tools
Comparing version 8.22.3 to 8.23.0
@@ -1,6 +0,6 @@ | ||
## 8.22.3 (2022-07-01) | ||
## 8.23.0 (2022-07-05) | ||
#### Bug Fixes | ||
#### Feature | ||
* parse lambda req body as well (8ac4ecbb) | ||
* add logging for router (21aba78b) | ||
@@ -50,3 +50,3 @@ { | ||
}, | ||
"version": "8.22.3" | ||
"version": "8.23.0" | ||
} |
@@ -62,3 +62,3 @@ const _ = require('lodash') | ||
} catch (e) { | ||
console.log(e.toString()) | ||
console.log(e.toString()) | ||
ctx.body = { error: e.toString() } | ||
@@ -70,2 +70,3 @@ ctx.status = 502 | ||
function hybridBodyParser () { | ||
console.log('in body parser') | ||
const bp = bodyParser() | ||
@@ -78,2 +79,3 @@ return async (ctx, next) => { | ||
) { | ||
console.log('will set req.body') | ||
ctx.req.body = qs.parse(ctx.req.body, { allowDots: true }) | ||
@@ -85,3 +87,2 @@ } | ||
helper.isAzure || | ||
helper.isLambda || | ||
helper.isTinyfaas || | ||
@@ -91,2 +92,4 @@ helper.isOpenfaas | ||
: ctx.request.body | ||
console.log('ctx.request.body is ' + JSON.stringify(ctx.request.body)) | ||
return bp(ctx, next) | ||
@@ -97,2 +100,4 @@ } | ||
function serverlessRouter (options, routerFn) { | ||
console.log('router: ' + JSON.stringify(routerFn)) | ||
console.log('options: ' + JSON.stringify(options)) | ||
if (_.isFunction(options) && _.isUndefined(routerFn)) { | ||
@@ -102,3 +107,3 @@ routerFn = options | ||
} | ||
const app = new Koa() | ||
@@ -108,2 +113,3 @@ const router = new Router({ | ||
}) | ||
console.log('app and router initialized.') | ||
@@ -113,3 +119,5 @@ let dbBindToMeasure = () => undefined | ||
console.log('will use hybrifBodyParser.') | ||
router.use(handleErrors, hybridBodyParser()) | ||
console.log('done.') | ||
@@ -120,2 +128,6 @@ const wrapHandler = (m, r, h) => | ||
const end = ctx.lib.measure(`${m}:${r}`) | ||
console.log('will call handler with:') | ||
console.log('r: ' + JSON.stringify(r)) | ||
console.log('m: ' + JSON.stringify(m)) | ||
console.log('h: ' + JSON.stringify(h)) | ||
await h(ctx, next) | ||
@@ -141,2 +153,3 @@ end() | ||
console.log('will use routes and methods.') | ||
app.use(router.routes()) | ||
@@ -163,59 +176,64 @@ app.use(router.allowedMethods()) | ||
module.exports.msgHandler = (options, handler) => { | ||
if (_.isFunction(options) && _.isUndefined(handler)) { | ||
handler = options | ||
options = {} | ||
} | ||
let dbBindToMeasure = () => undefined | ||
if (options.db) dbBindToMeasure = db.connect(options.db) | ||
return { | ||
lambdaHandler: async (event, ctx) => { | ||
console.log("ctxEntry: " + JSON.stringify(ctx)); | ||
console.log("eventEntry: " + JSON.stringify(event)); | ||
const contextId = event.Records[0].Sns.MessageAttributes.contextId.Value || helper.generateRandomID() | ||
const xPair = event.Records[0].Sns.MessageAttributes.xPair.Value || 'undefined-x-pair' | ||
ctx.contextId = contextId | ||
ctx.xPair = xPair | ||
ctx.lib = createContext(contextId, xPair, dbBindToMeasure) | ||
const end = ctx.lib.measure(`msg`) | ||
await handler(JSON.parse(event.Records[0].Sns.Message), ctx.lib) | ||
end() | ||
}, | ||
googleHandler: async (event, ctx) => { | ||
console.log("ctxEntry: " + JSON.stringify(ctx)); | ||
console.log("eventEntry: " + JSON.stringify(event)); | ||
const msg = event.data | ||
? Buffer.from(event.data, 'base64').toString() | ||
: 'no data'; | ||
console.log("Message: " + msg); | ||
const contextId = event.attributes.contextId || helper.generateRandomID() | ||
const xPair = event.attributes.xPair || 'undefined-x-pair' | ||
ctx.contextId = contextId | ||
ctx.xPair = xPair | ||
ctx.lib = createContext(contextId, xPair, dbBindToMeasure) | ||
const end = ctx.lib.measure(`msg`) | ||
await handler(JSON.parse(msg), ctx.lib) | ||
end() | ||
}, | ||
azureHandler: async (ctx, event) => { | ||
console.log("ctxEntry: " + JSON.stringify(ctx)); | ||
console.log("eventEntry: " + JSON.stringify(event)); | ||
const contextId = event.data.contextId || helper.generateRandomID() | ||
const xPair = event.data.xPair || 'undefined-x-pair' | ||
ctx.contextId = contextId | ||
ctx.xPair = xPair | ||
ctx.lib = createContext(contextId, xPair, dbBindToMeasure) | ||
const end = ctx.lib.measure(`msg`) | ||
await handler(JSON.parse(event.data.event), ctx.lib) | ||
end() | ||
}, | ||
tinyfaasHandler: _.isUndefined(handler) ? serverlessRouter(r => r.addRpcHandler(options)).tinyfaasHandler : serverlessRouter(options, r => r.addRpcHandler(handler)).tinyfaasHandler, | ||
} | ||
if (_.isFunction(options) && _.isUndefined(handler)) { | ||
handler = options | ||
options = {} | ||
} | ||
let dbBindToMeasure = () => undefined | ||
if (options.db) dbBindToMeasure = db.connect(options.db) | ||
return { | ||
lambdaHandler: async (event, ctx) => { | ||
console.log('ctxEntry: ' + JSON.stringify(ctx)) | ||
console.log('eventEntry: ' + JSON.stringify(event)) | ||
const contextId = | ||
event.Records[0].Sns.MessageAttributes.contextId.Value || | ||
helper.generateRandomID() | ||
const xPair = | ||
event.Records[0].Sns.MessageAttributes.xPair.Value || 'undefined-x-pair' | ||
ctx.contextId = contextId | ||
ctx.xPair = xPair | ||
ctx.lib = createContext(contextId, xPair, dbBindToMeasure) | ||
const end = ctx.lib.measure(`msg`) | ||
await handler(JSON.parse(event.Records[0].Sns.Message), ctx.lib) | ||
end() | ||
}, | ||
googleHandler: async (event, ctx) => { | ||
console.log('ctxEntry: ' + JSON.stringify(ctx)) | ||
console.log('eventEntry: ' + JSON.stringify(event)) | ||
const msg = event.data | ||
? Buffer.from(event.data, 'base64').toString() | ||
: 'no data' | ||
console.log('Message: ' + msg) | ||
const contextId = event.attributes.contextId || helper.generateRandomID() | ||
const xPair = event.attributes.xPair || 'undefined-x-pair' | ||
ctx.contextId = contextId | ||
ctx.xPair = xPair | ||
ctx.lib = createContext(contextId, xPair, dbBindToMeasure) | ||
const end = ctx.lib.measure(`msg`) | ||
await handler(JSON.parse(msg), ctx.lib) | ||
end() | ||
}, | ||
azureHandler: async (ctx, event) => { | ||
console.log('ctxEntry: ' + JSON.stringify(ctx)) | ||
console.log('eventEntry: ' + JSON.stringify(event)) | ||
const contextId = event.data.contextId || helper.generateRandomID() | ||
const xPair = event.data.xPair || 'undefined-x-pair' | ||
ctx.contextId = contextId | ||
ctx.xPair = xPair | ||
ctx.lib = createContext(contextId, xPair, dbBindToMeasure) | ||
const end = ctx.lib.measure(`msg`) | ||
await handler(JSON.parse(event.data.event), ctx.lib) | ||
end() | ||
}, | ||
tinyfaasHandler: _.isUndefined(handler) | ||
? serverlessRouter(r => r.addRpcHandler(options)).tinyfaasHandler | ||
: serverlessRouter(options, r => r.addRpcHandler(handler)).tinyfaasHandler | ||
} | ||
} |
59491586
503