Skip to content

Commit df55e8a

Browse files
authored
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: #62 Fixes: knative/func#194 (when updated in thetemplate) Signed-off-by: Lance Ball <[email protected]>
1 parent 7317451 commit df55e8a

File tree

5 files changed

+12
-10
lines changed

5 files changed

+12
-10
lines changed

lib/event-handler.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { Receiver } = require('cloudevents');
1+
const { HTTP } = require('cloudevents');
22
const Spec = require('./ce-constants.js').Spec;
33

44
function use(fastify, opts, done) {
@@ -14,8 +14,10 @@ function use(fastify, opts, done) {
1414
fastify.addHook('preHandler', function(request, reply, done) {
1515
if (request.isCloudEvent()) {
1616
try {
17-
const event = Receiver.accept(request.headers, request.body);
18-
request.fcontext.cloudevent = event;
17+
request.fcontext.cloudevent = HTTP.toEvent({
18+
headers: request.headers,
19+
body: request.body
20+
});
1921
} catch (err) {
2022
if (err.message.startsWith('invalid spec version')) {
2123
reply.code(406);

lib/invoker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = function invoker(func) {
1919
if (context.cloudevent) {
2020
// If there is a cloud event, provide the data
2121
// as the first parameter
22-
payload.response = await func(context.cloudevent.data, context);
22+
payload.response = await func(context, context.cloudevent.data);
2323
} else {
2424
// Invoke with context
2525
// TODO: Should this actually just get the Node.js request object?

test/fixtures/cloud-event/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = function testFunc(data, context) {
1+
module.exports = function testFunc(context, data) {
22
if (context.cloudevent) return { message: data.message };
33
else return new Error('No cloud event received');
44
};

test/fixtures/cloud-event/with-response.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = function testFunc(data, context) {
1+
module.exports = function testFunc(context, data) {
22
if (context.cloudevent) {
33
const response = {
44
message: data.message

test/test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,12 @@ test('Handles 1.0 CloudEvent Message responses', t => {
229229
{ log: false });
230230
});
231231

232-
test('Extracts event data as the first parameter to a function', t => {
232+
test('Extracts event data as the second parameter to a function', t => {
233233
const data = {
234234
lunch: "tacos"
235235
};
236236

237-
framework(menu => {
237+
framework((context, menu) => {
238238
t.equal(menu.lunch, data.lunch);
239239
return menu;
240240
}, server => {
@@ -260,7 +260,7 @@ test('Extracts event data as the first parameter to a function', t => {
260260
});
261261

262262
test('Successfully handles events with no data', t => {
263-
framework((data, context) => {
263+
framework((context, data) => {
264264
t.equal(data, undefined);
265265
t.true(context.cloudevent instanceof CloudEvent);
266266
return { status: 'done' }
@@ -279,7 +279,7 @@ test('Successfully handles events with no data', t => {
279279
t.end();
280280
server.close();
281281
});
282-
});
282+
}, { log: false });
283283
});
284284

285285
test('Responds with 406 Not Acceptable to unknown cloud event versions', t => {

0 commit comments

Comments
 (0)