From df55e8a9fe4e3f97cb1e82f2d6da6f5d82ec936b Mon Sep 17 00:00:00 2001 From: Lance Ball Date: Tue, 3 Nov 2020 14:04:56 -0500 Subject: [PATCH] fix: reverse the parameter order when invoking (#71) To make it possible for functions to easily act as both HTTP and CloudEvent handler it's better to have any potential data as the second parameter. This change also makes use of a new bit of the cloudevents API to receive an incoming event, eliminating a transitive dependency on axios. Fixes: https://github.com/boson-project/faas-js-runtime/issues/62 Fixes: https://github.com/boson-project/faas/issues/194 (when updated in thetemplate) Signed-off-by: Lance Ball --- lib/event-handler.js | 8 +++++--- lib/invoker.js | 2 +- test/fixtures/cloud-event/index.js | 2 +- test/fixtures/cloud-event/with-response.js | 2 +- test/test.js | 8 ++++---- 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/event-handler.js b/lib/event-handler.js index 980368a..0ff489f 100644 --- a/lib/event-handler.js +++ b/lib/event-handler.js @@ -1,4 +1,4 @@ -const { Receiver } = require('cloudevents'); +const { HTTP } = require('cloudevents'); const Spec = require('./ce-constants.js').Spec; function use(fastify, opts, done) { @@ -14,8 +14,10 @@ function use(fastify, opts, done) { fastify.addHook('preHandler', function(request, reply, done) { if (request.isCloudEvent()) { try { - const event = Receiver.accept(request.headers, request.body); - request.fcontext.cloudevent = event; + request.fcontext.cloudevent = HTTP.toEvent({ + headers: request.headers, + body: request.body + }); } catch (err) { if (err.message.startsWith('invalid spec version')) { reply.code(406); diff --git a/lib/invoker.js b/lib/invoker.js index 4c9d4ff..770cdbf 100644 --- a/lib/invoker.js +++ b/lib/invoker.js @@ -19,7 +19,7 @@ module.exports = function invoker(func) { if (context.cloudevent) { // If there is a cloud event, provide the data // as the first parameter - payload.response = await func(context.cloudevent.data, context); + payload.response = await func(context, context.cloudevent.data); } else { // Invoke with context // TODO: Should this actually just get the Node.js request object? diff --git a/test/fixtures/cloud-event/index.js b/test/fixtures/cloud-event/index.js index 884661e..a89eb16 100644 --- a/test/fixtures/cloud-event/index.js +++ b/test/fixtures/cloud-event/index.js @@ -1,4 +1,4 @@ -module.exports = function testFunc(data, context) { +module.exports = function testFunc(context, data) { if (context.cloudevent) return { message: data.message }; else return new Error('No cloud event received'); }; diff --git a/test/fixtures/cloud-event/with-response.js b/test/fixtures/cloud-event/with-response.js index ee7eef5..280bbac 100644 --- a/test/fixtures/cloud-event/with-response.js +++ b/test/fixtures/cloud-event/with-response.js @@ -1,4 +1,4 @@ -module.exports = function testFunc(data, context) { +module.exports = function testFunc(context, data) { if (context.cloudevent) { const response = { message: data.message diff --git a/test/test.js b/test/test.js index 53d17b1..759ea30 100644 --- a/test/test.js +++ b/test/test.js @@ -229,12 +229,12 @@ test('Handles 1.0 CloudEvent Message responses', t => { { log: false }); }); -test('Extracts event data as the first parameter to a function', t => { +test('Extracts event data as the second parameter to a function', t => { const data = { lunch: "tacos" }; - framework(menu => { + framework((context, menu) => { t.equal(menu.lunch, data.lunch); return menu; }, server => { @@ -260,7 +260,7 @@ test('Extracts event data as the first parameter to a function', t => { }); test('Successfully handles events with no data', t => { - framework((data, context) => { + framework((context, data) => { t.equal(data, undefined); t.true(context.cloudevent instanceof CloudEvent); return { status: 'done' } @@ -279,7 +279,7 @@ test('Successfully handles events with no data', t => { t.end(); server.close(); }); - }); + }, { log: false }); }); test('Responds with 406 Not Acceptable to unknown cloud event versions', t => {