Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.
Open
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
23 changes: 18 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,30 @@ module.exports = exports = (app) => (args) => {

app.action_params = args;

// use buffer to have custom parer that stores data chunks flagged as binary data
req
.buffer(true)
.parse((res, cb) => {
res.setEncoding('binary');
res.data = '';
res.on('data', (chunk) => res.data += chunk);
res.on('end', () => cb(null, Buffer.from(res.data, 'binary')));
});

req.end((err, res) => {
if (err)
if (err || !res.body)
return resolve({
statusCode: 500,
body: JSON.stringify(err)
});

let body = res.text || res.body;

let body;
let contentType = res.headers['content-type'] || 'text/plain';
contentType = contentType.split(';')[0];
if (isBinary(contentType))
body = new Buffer(body).toString('base64');
body = res.body.toString('base64');
else
body = res.body.toString('utf-8');

// Convert response back to a OpenWhisk response object.
return resolve({
Expand All @@ -77,6 +88,7 @@ const mediaTypes = {
"application/base64": true,
"application/excel": true,
"application/font-woff": true,
"application/font-woff2": true, // backward compatibility: woff2 had been proposed as this
"application/gnutar": true,
"application/java-archive": true,
"application/javascript": false,
Expand Down Expand Up @@ -148,7 +160,8 @@ const mediaTypes = {
function isBinary(contentType) {
if (contentType.startsWith('text/') || contentType.startsWith('message/'))
return false;
if (contentType.startsWith('audio/') || contentType.startsWith('image/') || contentType.startsWith('video/'))
if (contentType.startsWith('audio/') || contentType.startsWith('image/') ||
contentType.startsWith('video/') || contentType.startsWith('font/'))
return true;
return mediaTypes[contentType];
}
26 changes: 26 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ app.patch('/', (req, res) => {
res.send(req.body);
});

app.get('/font', (req, res) => {
res.set('Content-Type', 'font/woff');
res.send(sampleFont);
});

const forward = require('../index.js')(app);

describe('get', () => {
Expand Down Expand Up @@ -99,3 +104,24 @@ describe('put', () => {
});
});
});

describe('get', () => {
describe('font', () => {
it('should return encoded font', (done) => {
forward({
__ow_method: 'get',
__ow_path: '/font',
}).then(res => {
assert.equal(wantEncodedFont, res.body);
done();
});
});
});
});

let sampleFont = '';
for (i = 0; i < 50000; i++) {
sampleFont += Math.random().toString(36).substring(2, 15);
}
let wantEncodedFont = Buffer.from(sampleFont, 'binary').toString('base64');