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 => {