Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/event-handler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { Receiver } = require('cloudevents');
const { HTTP } = require('cloudevents');
const Spec = require('./ce-constants.js').Spec;

function use(fastify, opts, done) {
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion lib/invoker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/cloud-event/index.js
Original file line number Diff line number Diff line change
@@ -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');
};
2 changes: 1 addition & 1 deletion test/fixtures/cloud-event/with-response.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = function testFunc(data, context) {
module.exports = function testFunc(context, data) {
if (context.cloudevent) {
const response = {
message: data.message
Expand Down
8 changes: 4 additions & 4 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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' }
Expand All @@ -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 => {
Expand Down