Skip to content

Commit 3ddd647

Browse files
committed
feat: support binary data in cloudevents
Depends on: cloudevents/sdk-javascript#468 Fixes: nodeshift#64 Signed-off-by: Lance Ball <[email protected]>
1 parent 2fbe601 commit 3ddd647

File tree

8 files changed

+68
-1
lines changed

8 files changed

+68
-1
lines changed

index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ function start(func, options) {
4343
});
4444
payload.on('error', done);
4545
});
46+
47+
server.addContentTypeParser('*', { parseAs: 'buffer' }, function(req, body, done) {
48+
try {
49+
done(null, body);
50+
} catch (err) {
51+
err.statusCode = 400;
52+
done(err, undefined);
53+
}
54+
});
4655

4756
// Initialize the invocation context
4857
// This is passed as a parameter to the function when it's invoked

package-lock.json

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/cloud-event/binary.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const { CloudEvent } = require('cloudevents');
2+
3+
module.exports = function(context, event) {
4+
return new CloudEvent({
5+
source: 'image-func',
6+
type: 'response',
7+
datacontenttype: event.datacontenttype,
8+
data: event.data
9+
});
10+
};

test/fixtures/knative.gz

14.6 KB
Binary file not shown.

test/fixtures/knative.jpg

24.3 KB
Loading

test/fixtures/knative.png

7.51 KB
Loading

test/fixtures/taco.gif

166 KB
Loading

test/test-binary-data.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict';
2+
const fs = require('fs');
3+
const { start } = require('..');
4+
const test = require('tape');
5+
const request = require('supertest');
6+
const { CloudEvent, HTTP } = require('cloudevents');
7+
8+
const errHandler = t => err => {
9+
t.error(err);
10+
t.end();
11+
};
12+
13+
const testCases = [
14+
{ content: 'image/png', file: `${__dirname}/fixtures/knative.png` },
15+
{ content: 'image/jpg', file: `${__dirname}/fixtures/knative.jpg` },
16+
{ content: 'image/gif', file: `${__dirname}/fixtures/taco.gif` },
17+
];
18+
19+
for (const tc of testCases) {
20+
test(`Handles CloudEvents with ${tc.content} data`, t => {
21+
const data = fs.readFileSync(tc.file);
22+
const func = require(`${__dirname}/fixtures/cloud-event/binary.js`);
23+
const event = new CloudEvent({
24+
source: 'test',
25+
type: 'test',
26+
datacontenttype: tc.content,
27+
data
28+
});
29+
30+
const message = HTTP.binary(event);
31+
start(func)
32+
.then(server => {
33+
request(server)
34+
.post('/')
35+
.send(message.body)
36+
.set(message.headers)
37+
.expect(200)
38+
.expect('Content-Type', tc.content)
39+
.end((err, res) => {
40+
t.error(err, 'No error');
41+
t.deepEqual(res.body, data);
42+
t.end();
43+
server.close();
44+
});
45+
}, errHandler(t));
46+
});
47+
}

0 commit comments

Comments
 (0)