Skip to content

Commit

Permalink
fix: reverse the parameter order when invoking (#71)
Browse files Browse the repository at this point in the history
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: #62

Fixes: knative/func#194 (when updated in thetemplate)

Signed-off-by: Lance Ball <[email protected]>
  • Loading branch information
lance authored Nov 3, 2020
1 parent 7317451 commit df55e8a
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 10 deletions.
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

0 comments on commit df55e8a

Please sign in to comment.