Skip to content

Commit

Permalink
protocol 1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Bishop authored and Simon Bishop committed Sep 20, 2016
1 parent 2558eda commit 5387def
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 32 deletions.
17 changes: 10 additions & 7 deletions lib/client/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@
var Logger;
var crypto;

var PROTOCOL = "1.0.0";
var PROTOCOL = "{{protocol}}";

if (typeof window !== 'undefined' && typeof document !== 'undefined') browser = true;

// allow require when module is defined (needed for NW.js)
if (typeof module !== 'undefined') module.exports = HappnClient;

if (!browser) {

Promise = require('bluebird');
Logger = require('happn-logger');
PROTOCOL = require('../../package.json').protocol;//we can access our package

} else {

window.HappnClient = HappnClient;
if (!Promise || typeof Promise.promisify !== 'function') {
Promise = Promise || {};
Expand Down Expand Up @@ -646,13 +649,13 @@

if (this.session) message.sessionId = this.session.id;

if (!options) options = {};

if (!options.timeout) options.timeout = 20000;
if (!options){
options = {};//skip sending up the options
} else message.options = options;

message.options = options;
if (!options.timeout) options.timeout = 20000;//this is not used on the server side

message.headers = {protocol:PROTOCOL};
message.protocol = PROTOCOL;

if (['login', 'describe', 'request-nonce'].indexOf(action) == -1 && this.serverInfo.encryptPayloads) message = this.__encryptPayload(message);

Expand Down Expand Up @@ -941,7 +944,7 @@

HappnClient.prototype._remoteOff = function (channel, refCount, callback) {

this.performRequest(channel, 'off', this.session, {'refCount': refCount}, function (e, response) {
this.performRequest(channel, 'off', null, {'refCount': refCount}, function (e, response) {

if (e)
return callback(e);
Expand Down
9 changes: 8 additions & 1 deletion lib/middleware/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,16 @@ ClientMiddleware.prototype.process = function (req, res, next) {
if (_this.cached) return res.end(_this.cached);

var path = require('path');
var protocol = require('../../package.json').protocol;

fs.readFile(path.resolve(__dirname, '../client/base.js'), function (e, buf) {
_this.cached = '\/\/happn client v' + require('../../package.json').version + '\r\n' + buf.toString();

var clientScript = buf.toString().replace('{{protocol}}', protocol);//set the protocol here

_this.cached = '\/\/happn client v' + require('../../package.json').version + '\r\n' +
'protocol v' + protocol + '\r\n' +
clientScript;

res.end(_this.cached);
});
};
Expand Down
28 changes: 11 additions & 17 deletions lib/services/pubsub/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ PubSubService.prototype.createResponse = function (e, message, response, local)
_meta.published = false;
_meta.eventId = message.eventId;

delete _meta._id;

//we need these passed in case we are encrypting the resulting payload
if (['login', 'describe'].indexOf(message.action) > -1) {
_meta.action = message.action;
Expand All @@ -243,14 +245,14 @@ PubSubService.prototype.createResponse = function (e, message, response, local)
_meta.action = message.action;
}

if (response.paths)
response = response.paths;
if (response.paths) response = response.paths;

response._meta = _meta;

if (e) {

response._meta.status = 'error';

response._meta.error = {
name: e.toString()
};
Expand Down Expand Up @@ -292,21 +294,6 @@ PubSubService.prototype.handleDataResponseSocket = function (e, message, respons
return socket.write(this.createResponse(e, message, response, false));
};

PubSubService.prototype.decodeArrayResponse = function (response) {

var decoded = response.map(function (item) {
var obj = item.data;
obj._meta = item._meta;

if (item._id) obj._meta.id = item._id;

return obj;
});

decoded._meta = response._meta;
return decoded;
};

PubSubService.prototype.handleDataResponseLocal = function (e, message, response, handler, client) {

if (e){
Expand Down Expand Up @@ -388,6 +375,8 @@ PubSubService.prototype.handle_message = function (message, socketInstance) {

_this.authorizeRequest(socketInstance, message, function () {

if (!message.options) message.options = {};

if (message.action == 'get') {
_this.dataService.get(message.path, message.options,
function (e, response) {
Expand Down Expand Up @@ -738,6 +727,11 @@ PubSubService.prototype.emitToAudience = function (publication, channel, opts) {
decoupledPublication._meta.channel = channel.toString();
decoupledPublication._meta.sessionId = this.__sessions[sessionIndex].session.id;

delete decoupledPublication._meta.status;
delete decoupledPublication._meta.published;
delete decoupledPublication._meta.eventId;
delete decoupledPublication._meta._id;

this.__sessions[sessionIndex].write(decoupledPublication);
}
}
Expand Down
6 changes: 1 addition & 5 deletions lib/services/pubsub/transform-message-protocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,12 @@ TransformProtocol.prototype.initialize = function(config, callback){

TransformProtocol.prototype.incoming = function(packet, next){
//backwards compatibility issues on incoming packets could be dealt with here

if (!packet.data.headers) packet.data.headers = {};

next();
};

TransformProtocol.prototype.outgoing = function(packet, next){

if (!packet.data.headers) packet.data.headers = {};
packet.data.headers.protocol = this.protocolVersion;
packet.data.protocol = this.protocolVersion;
next();
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "pub/sub api as a service using primus and mongo & redis or nedb, can work as cluster, single process or embedded using nedb, use in production at your own risk",
"version": "2.15.0",
"main": "./lib/index",
"protocol":"1.0.1",
"protocol":"1.1.0",
"scripts": {
"test": "mocha --expose-gc silence.js test",
"test-cover": "istanbul cover _mocha -- silence.js test",
Expand Down
2 changes: 1 addition & 1 deletion test/e1_pubsub_middleware_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe('e1_pubsub_middleware_unit', function () {
protocolTransformer.outgoing(packet, function(e){

if (e) return done(e);
expect(packet.data.headers.protocol).to.be('1.0.0');
expect(packet.data.protocol).to.be(require('../package.json').protocol);

done();

Expand Down

0 comments on commit 5387def

Please sign in to comment.