Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

Commit

Permalink
Enabling the binary encoding.
Browse files Browse the repository at this point in the history
 - If the transmitted data contains a binary object it's cbor encoded and and the application/cbor content type is used.
 - Added cbor npm package into package.json file

Signed-off-by: Jamal El Youssefi <[email protected]>
Signed-off-by: Marcel Wagner <[email protected]>
  • Loading branch information
jelyoussefi authored and srware committed Mar 21, 2019
1 parent 425d4b8 commit 68737ae
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
12 changes: 11 additions & 1 deletion api/rest/data.def.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"use strict";

var cbor = require('cbor');

module.exports = function(config) {
var common = require('../../lib/common');
var api = require('./api');
Expand All @@ -32,11 +34,18 @@ module.exports = function(config) {
var module = {};

function SubmitDataOption(data) {
var isBinary = common.isBinary(data.body);
this.pathname = common.buildPath(api.data.SEND, data.deviceId);
this.token = data.userToken;
ConnectionOptions.call(this);
this.method = 'POST';
this.body = JSON.stringify(data.body);
if ( isBinary ) {
this.body = cbor.encode(data.body);
this.headers["Content-type"] = "application/cbor";
} else {
this.body = JSON.stringify(data.body);
this.headers["Content-type"] = "application/json";
}
}
SubmitDataOption.prototype = new ConnectionOptions();
SubmitDataOption.prototype.constructor = SubmitDataOption;
Expand All @@ -48,6 +57,7 @@ module.exports = function(config) {
this.token = data.userToken;
ConnectionOptions.call(this);
this.method = 'POST';
this.encoding = null;
this.body = JSON.stringify(data.body);
}
SearchDataOption.prototype = new ConnectionOptions();
Expand Down
15 changes: 13 additions & 2 deletions api/rest/devices.def.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"use strict";

var cbor = require('cbor');

module.exports = function(config) {
var common = require('../../lib/common');
var api = require('./api');
Expand Down Expand Up @@ -185,18 +187,27 @@ module.exports = function(config) {
* @constructor
*/
function DeviceSubmitDataOption (data) {
var isBinary = common.isBinary(data.body);
this.pathname = common.buildPath(api.submit.DATA, data.deviceId);
ConnectionOptions.call(this);
var contentType = "application/json";
if ( isBinary == true ) {
contentType = "application/cbor";
}
this.method = 'POST';
this.headers = {
"Content-type" : "application/json",
"Content-type" : contentType,
"Authorization" : "Bearer " + data.deviceToken
};
if (data.forwarded) {
this.headers["forwarded"] = true;
delete data.forwarded;
}
this.body = JSON.stringify(data.body);
if ( isBinary ) {
this.body = cbor.encode(data.body);
} else {
this.body = JSON.stringify(data.body);
}
}
DeviceSubmitDataOption.prototype = new ConnectionOptions();
DeviceSubmitDataOption.prototype.constructor = DeviceSubmitDataOption;
Expand Down
16 changes: 16 additions & 0 deletions lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,19 @@ module.exports.isAbsolutePath = function(location) {
return path.resolve(location) === path.normalize(location);
};

module.exports.isBinary = function(object) {
if ( Buffer.isBuffer(object) ) {
return true;
}
var keys = Object.keys(object);
for(var index in keys) {
var key = keys[index];
if(typeof object[key] == 'object') {
if( this.isBinary(object[key]) ) {
return true;
}
}
}
return false;
};

7 changes: 7 additions & 0 deletions lib/httpClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* HTTPS request
*/
var request = require('request');
var cbor = require('cbor');

function processResponse(res, body, callback) {
var data = null;
Expand All @@ -42,6 +43,12 @@ function processResponse(res, body, callback) {
} catch (e) {
data = null;
}
} else if (res.headers['content-type'] && res.headers['content-type'].indexOf('application/cbor') > -1) {
try {
data = cbor.decode(body);
} catch (e) {
data = null;
}
} else {
data = null;
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"grunt-mocha-istanbul": "^5.0.2",
"istanbul": "^0.4.5",
"mocha": "^4.0.1",
"rewire": "^2.5.2"
"rewire": "^2.5.2",
"cbor": "^4.1.5"
},
"dependencies": {
"async": "^2.5.0",
Expand Down

0 comments on commit 68737ae

Please sign in to comment.