Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow data parameter to be optional for presence#enter and channell#subscribe (2) #50

Merged
merged 8 commits into from
May 12, 2015
Merged
17 changes: 8 additions & 9 deletions browser/static/ably.js
Original file line number Diff line number Diff line change
Expand Up @@ -4423,11 +4423,11 @@ var Message = (function() {
encoding: this.encoding
};

/* encode to base64 if we're returning real JSON;
/* encode data to base64 if present and we're returning real JSON;
* although msgpack calls toJSON(), we know it is a stringify()
* call if it has a non-empty arguments list */
var data = this.data;
if(arguments.length > 0 && BufferUtils.isBuffer(data)) {
if(data && arguments.length > 0 && BufferUtils.isBuffer(data)) {
var encoding = this.encoding;
result.encoding = encoding ? (encoding + '/base64') : 'base64';
data = BufferUtils.base64Encode(data);
Expand Down Expand Up @@ -4478,7 +4478,7 @@ var Message = (function() {

Message.encode = function(msg, options) {
var data = msg.data, encoding;
if(typeof(data) != 'string' && !BufferUtils.isBuffer(data)) {
if(data && typeof(data) != 'string' && !BufferUtils.isBuffer(data)) {
msg.data = JSON.stringify(data);
msg.encoding = (encoding = msg.encoding) ? (encoding + '/json') : 'json';
}
Expand Down Expand Up @@ -4592,7 +4592,6 @@ var PresenceMessage = (function() {
*/
PresenceMessage.prototype.toJSON = function() {
var result = {
name: this.name,
clientId: this.clientId,
connectionId: this.connectionId,
timestamp: this.timestamp,
Expand All @@ -4604,7 +4603,7 @@ var PresenceMessage = (function() {
* although msgpack calls toJSON(), we know it is a stringify()
* call if it passes on the stringify arguments */
var data = this.data;
if(arguments.length > 0 && BufferUtils.isBuffer(data)) {
if(data && arguments.length > 0 && BufferUtils.isBuffer(data)) {
var encoding = this.encoding;
result.encoding = encoding ? (encoding + '/base64') : 'base64';
data = data.toString('base64');
Expand Down Expand Up @@ -7652,16 +7651,16 @@ var Presence = (function() {
Utils.inherits(Presence, EventEmitter);

Presence.prototype.enter = function(data, callback) {
if (!callback && (typeof(data)==='function')) {
callback = data;
data = '';
}
if(!this.clientId)
throw new Error('clientId must be specified to enter a presence channel');
this.enterClient(this.clientId, data, callback);
};

Presence.prototype.enterClient = function(clientId, data, callback) {
if (!callback && (typeof(data)==='function')) {
callback = data;
data = null;
}
Logger.logAction(Logger.LOG_MICRO, 'Presence.enterClient()', 'entering; channel = ' + this.channel.name + ', client = ' + clientId);
var presence = PresenceMessage.fromValues({
action : presenceAction.ENTER,
Expand Down
17 changes: 8 additions & 9 deletions browser/static/ably.noencryption.js
Original file line number Diff line number Diff line change
Expand Up @@ -3075,11 +3075,11 @@ var Message = (function() {
encoding: this.encoding
};

/* encode to base64 if we're returning real JSON;
/* encode data to base64 if present and we're returning real JSON;
* although msgpack calls toJSON(), we know it is a stringify()
* call if it has a non-empty arguments list */
var data = this.data;
if(arguments.length > 0 && BufferUtils.isBuffer(data)) {
if(data && arguments.length > 0 && BufferUtils.isBuffer(data)) {
var encoding = this.encoding;
result.encoding = encoding ? (encoding + '/base64') : 'base64';
data = BufferUtils.base64Encode(data);
Expand Down Expand Up @@ -3130,7 +3130,7 @@ var Message = (function() {

Message.encode = function(msg, options) {
var data = msg.data, encoding;
if(typeof(data) != 'string' && !BufferUtils.isBuffer(data)) {
if(data && typeof(data) != 'string' && !BufferUtils.isBuffer(data)) {
msg.data = JSON.stringify(data);
msg.encoding = (encoding = msg.encoding) ? (encoding + '/json') : 'json';
}
Expand Down Expand Up @@ -3244,7 +3244,6 @@ var PresenceMessage = (function() {
*/
PresenceMessage.prototype.toJSON = function() {
var result = {
name: this.name,
clientId: this.clientId,
connectionId: this.connectionId,
timestamp: this.timestamp,
Expand All @@ -3256,7 +3255,7 @@ var PresenceMessage = (function() {
* although msgpack calls toJSON(), we know it is a stringify()
* call if it passes on the stringify arguments */
var data = this.data;
if(arguments.length > 0 && BufferUtils.isBuffer(data)) {
if(data && arguments.length > 0 && BufferUtils.isBuffer(data)) {
var encoding = this.encoding;
result.encoding = encoding ? (encoding + '/base64') : 'base64';
data = data.toString('base64');
Expand Down Expand Up @@ -6304,16 +6303,16 @@ var Presence = (function() {
Utils.inherits(Presence, EventEmitter);

Presence.prototype.enter = function(data, callback) {
if (!callback && (typeof(data)==='function')) {
callback = data;
data = '';
}
if(!this.clientId)
throw new Error('clientId must be specified to enter a presence channel');
this.enterClient(this.clientId, data, callback);
};

Presence.prototype.enterClient = function(clientId, data, callback) {
if (!callback && (typeof(data)==='function')) {
callback = data;
data = null;
}
Logger.logAction(Logger.LOG_MICRO, 'Presence.enterClient()', 'entering; channel = ' + this.channel.name + ', client = ' + clientId);
var presence = PresenceMessage.fromValues({
action : presenceAction.ENTER,
Expand Down
8 changes: 4 additions & 4 deletions common/lib/client/presence.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ var Presence = (function() {
Utils.inherits(Presence, EventEmitter);

Presence.prototype.enter = function(data, callback) {
if (!callback && (typeof(data)==='function')) {
callback = data;
data = '';
}
if(!this.clientId)
throw new Error('clientId must be specified to enter a presence channel');
this.enterClient(this.clientId, data, callback);
};

Presence.prototype.enterClient = function(clientId, data, callback) {
if (!callback && (typeof(data)==='function')) {
callback = data;
data = null;
}
Logger.logAction(Logger.LOG_MICRO, 'Presence.enterClient()', 'entering; channel = ' + this.channel.name + ', client = ' + clientId);
var presence = PresenceMessage.fromValues({
action : presenceAction.ENTER,
Expand Down
6 changes: 3 additions & 3 deletions common/lib/types/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ var Message = (function() {
encoding: this.encoding
};

/* encode to base64 if we're returning real JSON;
/* encode data to base64 if present and we're returning real JSON;
* although msgpack calls toJSON(), we know it is a stringify()
* call if it has a non-empty arguments list */
var data = this.data;
if(arguments.length > 0 && BufferUtils.isBuffer(data)) {
if(data && arguments.length > 0 && BufferUtils.isBuffer(data)) {
var encoding = this.encoding;
result.encoding = encoding ? (encoding + '/base64') : 'base64';
data = BufferUtils.base64Encode(data);
Expand Down Expand Up @@ -79,7 +79,7 @@ var Message = (function() {

Message.encode = function(msg, options) {
var data = msg.data, encoding;
if(typeof(data) != 'string' && !BufferUtils.isBuffer(data)) {
if(data && typeof(data) != 'string' && !BufferUtils.isBuffer(data)) {
msg.data = JSON.stringify(data);
msg.encoding = (encoding = msg.encoding) ? (encoding + '/json') : 'json';
}
Expand Down
2 changes: 1 addition & 1 deletion common/lib/types/presencemessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var PresenceMessage = (function() {
* although msgpack calls toJSON(), we know it is a stringify()
* call if it passes on the stringify arguments */
var data = this.data;
if(arguments.length > 0 && BufferUtils.isBuffer(data)) {
if(data && arguments.length > 0 && BufferUtils.isBuffer(data)) {
var encoding = this.encoding;
result.encoding = encoding ? (encoding + '/base64') : 'base64';
data = data.toString('base64');
Expand Down
118 changes: 118 additions & 0 deletions spec/realtime/message.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,124 @@ define(['ably', 'shared_helper'], function(Ably, helper) {
}
};

exports.publishVariations = function(test) {
var transport = 'binary';
var testData = 'Some data'
var errorCallback = function(err){
if(err) {
test.ok(false, 'Error received by publish callback ' + err);
test.done();
realtime.close();
return;
}
};
var testArguments = [
[{name: 'objectWithName'}],
[{name: 'objectWithNameAndCallback'}, errorCallback],
[{name: 'objectWithNameAndNullData', data: null}],
[{name: 'objectWithNameAndUndefinedData', data: undefined}],
[{name: 'objectWithNameAndEmptyStringData', data: ''}],
['nameAndNullData', null],
['nameAndUndefinedData', undefined],
['nameAndEmptyStringData', ''],
['nameAndData', testData],
['nameAndDataAndCallback', testData, errorCallback],
[{name: 'objectWithNameAndData', data: testData}],
[{name: 'objectWithNameAndDataAndCallback', data: testData}, errorCallback],
// 6 messages with null name,
[null, testData],
[null, testData, errorCallback],
[{name: null, data: testData}],
[null, null],
[{name: null}],
[{name: null, data: null}]
];

test.expect(testArguments.length * 2);
try {
/* set up realtime */
var realtime = helper.AblyRealtime();
var rest = helper.AblyRest();

/* connect and attach */
realtime.connection.on('connected', function() {
var rtChannel = realtime.channels.get('publishVariations');
rtChannel.attach(function(err) {
if(err) {
test.ok(false, 'Attach failed with error: ' + err);
test.done();
realtime.close();
return;
}

/* subscribe to different message types */
var messagesReceived = 0
rtChannel.subscribe(function(msg) {
test.ok(true, 'Received ' + msg.name);
++messagesReceived;
switch(msg.name) {
case 'objectWithName':
case 'objectWithNameAndCallback':
case 'objectWithNameAndNullData':
case 'objectWithNameAndUndefinedData':
case 'nameAndNullData':
case 'nameAndUndefinedData':
test.equal(typeof(msg.data), 'undefined', 'Msg data was received where none expected');
break;
case 'nameAndEmptyStringData':
case 'objectWithNameAndEmptyStringData':
test.strictEqual(msg.data, '', 'Msg data received was a ' + typeof(msg.data) + ' when should have been an empty string');
break;
case 'nameAndData':
case 'nameAndDataAndCallback':
case 'objectWithNameAndData':
case 'objectWithNameAndDataAndCallback':
test.equal(msg.data, testData, 'Msg data ' + msg.data + 'Unexpected message data received');
break;
case undefined:
if (msg.data) {
// 3 messages: null name and data, null name and data and callback, object with null name and data
test.equal(msg.data, testData, 'Msg data ' + msg.data + 'Unexpected message data received');
} else {
// 3 messages: null name and null data, object with null name and no data, object with null name and null data
test.equal(typeof(msg.data), 'undefined', 'Msg data was received where none expected');
}
break;
default:
test.ok(false, 'Unexpected message ' + msg.name + 'received');
test.done();
realtime.close();
}

if (messagesReceived == testArguments.length) {
test.done();
realtime.close();
}
});

/* publish events */
var restChannel = rest.channels.get('publishVariations');
for(var i = 0; i < testArguments.length; i++) {
restChannel.publish.apply(restChannel, testArguments[i]);
}
});
});
var exitOnState = function(state) {
realtime.connection.on(state, function () {
test.ok(false, transport + ' connection to server failed');
test.done();
realtime.close();
});
};
exitOnState('failed');
exitOnState('suspended');
} catch(e) {
test.ok(false, 'Channel attach failed with exception: ' + e.stack);
test.done();
realtime.close();
}
};

exports.restpublish = function(test) {
var count = 10;
var rest = helper.AblyRest();
Expand Down
Loading