Socket
Socket
Sign inDemoInstall

pubnub

Package Overview
Dependencies
Maintainers
1
Versions
224
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pubnub - npm Package Compare versions

Comparing version 3.5.3 to 3.5.4

LICENSE

2

package.json
{
"name": "pubnub",
"preferGlobal": false,
"version": "3.5.3",
"version": "3.5.4",
"author": "PubNub <stephen@pubnub.com>",

@@ -6,0 +6,0 @@ "description": "Publish & Subscribe Real-time Messaging with PubNub",

@@ -1,2 +0,2 @@

// Version: 3.5.3
// Version: 3.5.4
var NOW = 1

@@ -9,3 +9,3 @@ , READY = false

, DEF_SUB_TIMEOUT = 310 // SECONDS.
, DEF_KEEPALIVE = 60 // SECONDS.
, DEF_KEEPALIVE = 60 // SECONDS (FOR TIMESYNC).
, SECOND = 1000 // A THOUSAND MILLISECONDS.

@@ -53,3 +53,3 @@ , URLBIT = '/'

each( url_params, function( key, value ) {
params.push(key + "=" + encode(value));
(typeof value != 'undefined' && value != null && encode_param(value).length > 0) && params.push(key + "=" + encode_param(value));
} );

@@ -164,5 +164,5 @@

*/
function encode(path) {
function encode_base(path, chars) {
return map( (encodeURIComponent(path)).split(''), function(chr) {
return "-_.!~*'()".indexOf(chr) < 0 ? chr :
return chars.indexOf(chr) < 0 ? chr :
"%"+chr.charCodeAt(0).toString(16).toUpperCase()

@@ -172,2 +172,10 @@ } ).join('');

function encode(path) {
return encode_base(path,"-_.!~*'()");
}
function encode_param(value) {
return encode_base(value,".!~*'()");
}
/**

@@ -197,5 +205,9 @@ * Generate Subscription Channel List

, KEEPALIVE = (+setup['keepalive'] || DEF_KEEPALIVE) * SECOND
, NOLEAVE = setup['noleave'] || 0
, PUBLISH_KEY = setup['publish_key'] || ''
, SUBSCRIBE_KEY = setup['subscribe_key'] || ''
, AUTH_KEY = setup['auth_key'] || ''
, SECRET_KEY = setup['secret_key'] || ''
, PNSDK = setup['PNSDK'] || ''
, hmac_SHA256 = setup['hmac_SHA256']
, SSL = setup['ssl'] ? 's' : ''

@@ -207,2 +219,3 @@ , ORIGIN = 'http'+SSL+'://'+(setup['origin']||'pubsub.pubnub.com')

, PUB_QUEUE = []
, TIME_DRIFT = 0
, SUB_CALLBACK = 0

@@ -254,2 +267,5 @@ , SUB_CHANNEL = 0

// No Leave Patch (Prevent Blocking Leave if Desired)
if (NOLEAVE) return;
if (jsonp != '0') data['callback'] = jsonp;

@@ -280,2 +296,3 @@

, err = args['error'] || function(){}
, auth_key = args['auth_key'] || AUTH_KEY
, channel = args['channel']

@@ -295,3 +312,3 @@ , start = args['start']

params['reverse'] = reverse;
params['auth'] = AUTH_KEY;
params['auth'] = auth_key;

@@ -306,3 +323,9 @@ if (jsonp) params['callback'] = jsonp;

data : params,
success : function(response) { callback(response) },
success : function(response) {
if (typeof response == 'object' && response['error']) {
err(response);
return;
}
callback(response)
},
fail : err,

@@ -324,2 +347,3 @@ url : [

var callback = callback || args['callback'] || function(){}
, auth_key = args['auth_key'] || AUTH_KEY
, source = args['source']

@@ -350,3 +374,3 @@ , destination = args['destination']

data['auth'] = AUTH_KEY;
data['auth'] = auth_key;

@@ -363,3 +387,9 @@ // Compose URL Parts

callback : jsonp,
success : function(response) { callback(response) },
success : function(response) {
if (typeof response == 'object' && response['error']) {
err(response);
return;
}
callback(response)
},
fail : function() { callback([ 0, 'Disconnected' ]) },

@@ -404,2 +434,4 @@ url : url,

, channel = args['channel']
, auth_key = args['auth_key'] || AUTH_KEY
, err = args['error'] || function() {}
, jsonp = jsonp_cb()

@@ -429,5 +461,12 @@ , url;

url : url,
data : { 'uuid' : UUID, 'auth' : AUTH_KEY },
success : function(response){callback(response);publish(1)},
fail : function(){callback([0,'Failed',msg]);publish(1)}
data : { 'uuid' : UUID, 'auth' : auth_key },
success : function(response) {
if (typeof response == 'object' && response['error']) {
err(response);
} else {
callback(response);
}
publish(1);
},
fail : function(response){err(response);publish(1)}
});

@@ -476,2 +515,3 @@

, callback = callback || args['message']
, auth_key = args['auth_key'] || AUTH_KEY
, connect = args['connect'] || function(){}

@@ -481,2 +521,3 @@ , reconnect = args['reconnect'] || function(){}

, errcb = args['error'] || function(){}
, idlecb = args['idle'] || function(){}
, presence = args['presence'] || 0

@@ -592,7 +633,8 @@ , noheresync = args['noheresync'] || 0

callback : jsonp,
fail : function() {
fail : function(response) {
errcb(response);
SUB_RECEIVER = null;
SELF['time'](_test_connection);
},
data : { 'uuid' : UUID, 'auth' : AUTH_KEY },
data : { 'uuid' : UUID, 'auth' : auth_key },
url : [

@@ -605,3 +647,2 @@ SUB_ORIGIN, 'subscribe',

SUB_RECEIVER = null;
// Check for Errors

@@ -611,8 +652,11 @@ if (!messages || (

'error' in messages &&
!messages['error'])
) {
messages['error']
)) {
errcb(messages);
return timeout( CONNECT, windowing );
return timeout( CONNECT, SECOND );
}
// User Idle Callback
idlecb(messages[1]);
// Restore Previous Connection Point if Needed

@@ -660,5 +704,6 @@ TIMETOKEN = !TIMETOKEN &&

var latency = detect_latency(+messages[1]);
each( messages[0], function(msg) {
var next = next_callback();
next[0]( msg, messages, next[1] );
next[0]( msg, messages, next[1], latency );
} );

@@ -686,5 +731,6 @@

, err = args['error'] || function(){}
, auth_key = args['auth_key'] || AUTH_KEY
, channel = args['channel']
, jsonp = jsonp_cb()
, data = { 'uuid' : UUID, 'auth' : AUTH_KEY };
, data = { 'uuid' : UUID, 'auth' : auth_key };

@@ -701,3 +747,9 @@ // Make sure we have a Channel

data : data,
success : function(response) { callback(response,channel) },
success : function(response) {
if (typeof response == 'object' && response['error']) {
err(response);
return;
}
callback(response)
},
fail : err,

@@ -711,3 +763,119 @@ url : [

},
'grant' : function( args, callback ) {
var callback = args['callback'] || callback
, err = args['error'] || function(){}
, channel = args['channel']
, jsonp = jsonp_cb()
, ttl = args['ttl'] || -1
, r = (args['read'] )?"1":"0"
, w = (args['write'])?"1":"0"
, auth_key = args['auth_key'];
function replaceAll(find, replace, str) {
return str.replace(new RegExp(find, 'g'), replace);
}
// Make sure we have a Channel
if (!channel) return error('Missing Channel');
if (!callback) return error('Missing Callback');
if (!SUBSCRIBE_KEY) return error('Missing Subscribe Key');
if (!PUBLISH_KEY) return error('Missing Publish Key');
if (!SECRET_KEY) return error('Missing Secret Key');
if (jsonp != '0') { data['callback'] = jsonp; }
var timestamp = Math.floor(new Date().getTime() / 1000);
var sign_input = SUBSCRIBE_KEY + "\n" + PUBLISH_KEY + "\n"
+ "grant" + "\n"
+ (((auth_key && encodeURIComponent(auth_key).length > 0)?"auth=" + encodeURIComponent(auth_key) + "&":""))
+ "channel=" + encodeURIComponent(channel) + "&" + "pnsdk=" + encodeURIComponent(PNSDK) + "&"
+ "r=" + r + "&" + "timestamp=" + encodeURIComponent(timestamp)
+ ((ttl > -1)?"&" + "ttl=" + ttl:"")
+ "&" + "w=" + w;
var signature = hmac_SHA256( sign_input, SECRET_KEY );
signature = replaceAll("\\+","-",signature);
signature = replaceAll("\\/","_",signature);
var data = {
'w' : w,
'r' : r,
'signature' : signature,
'channel' : encodeURIComponent(channel),
'timestamp' : timestamp
};
if (ttl > -1) data['ttl'] = ttl
if (auth_key) data['auth'] = encodeURIComponent(auth_key);
xdr({
callback : jsonp,
data : data,
success : function(response) { callback(response) },
fail : err,
url : [
STD_ORIGIN, 'v1', 'auth', 'grant' ,
'sub-key', SUBSCRIBE_KEY
]
});
},
'audit' : function( args, callback ) {
var callback = args['callback'] || callback
, err = args['error'] || function(){}
, channel = args['channel']
, jsonp = jsonp_cb()
, ttl = args['ttl'] || -1
, auth_key = args['auth_key'];
function replaceAll(find, replace, str) {
return str.replace(new RegExp(find, 'g'), replace);
}
// Make sure we have a Channel
if (!callback) return error('Missing Callback');
if (!SUBSCRIBE_KEY) return error('Missing Subscribe Key');
if (!PUBLISH_KEY) return error('Missing Publish Key');
if (!SECRET_KEY) return error('Missing Secret Key');
if (jsonp != '0') { data['callback'] = jsonp; }
var timestamp = Math.floor(new Date().getTime() / 1000);
var sign_input = SUBSCRIBE_KEY + "\n" + PUBLISH_KEY + "\n" + "audit" + "\n";
if (auth_key) sign_input += ("auth=" + encodeURIComponent(auth_key) + "&");
if (channel) sign_input += ("channel=" + encodeURIComponent(channel) + "&") ;
sign_input += "pnsdk=" + encodeURIComponent(PNSDK) + "&" + "timestamp=" + timestamp;
var signature = hmac_SHA256( sign_input, SECRET_KEY );
signature = replaceAll("\\+","-",signature);
signature = replaceAll("\\/","_",signature);
var data = {
'signature' : signature,
'timestamp' : timestamp
};
if (channel) data['channel'] = encodeURIComponent(channel)
if (auth_key) data['auth'] = encodeURIComponent(auth_key)
xdr({
callback : jsonp,
data : data,
success : function(response) { callback(response) },
fail : err,
url : [
STD_ORIGIN, 'v1', 'auth', 'audit' ,
'sub-key', SUBSCRIBE_KEY
]
});
},
'revoke' : function( args, callback ) {
args['read'] = false;
args['write'] = false;
SELF['grant'](args,callback);
},
// Expose PUBNUB Functions

@@ -730,3 +898,3 @@ 'xdr' : xdr,

function _poll_online() {
_is_online() || _reset_offline(1);
_is_online() || _reset_offline(1 , {"error" : "Offline. Please check your network settings. "});
timeout( _poll_online, SECOND );

@@ -737,9 +905,10 @@ }

SELF['time'](function(success){
success || _reset_offline(1);
detect_time_detla( function(){}, success );
success || _reset_offline(1, {"error" : "Heartbeat failed to connect to Pubnub Servers. Please check your network settings."});
timeout( _poll_online2, KEEPALIVE );
})
});
}
function _reset_offline(err) {
SUB_RECEIVER && SUB_RECEIVER(err);
function _reset_offline(err, msg) {
SUB_RECEIVER && SUB_RECEIVER(err, msg);
SUB_RECEIVER = null;

@@ -754,4 +923,26 @@ }

SELF['time'](function() {});
// Detect Age of Message
function detect_latency(tt) {
var adjusted_time = rnow() - TIME_DRIFT;
return adjusted_time - tt / 10000;
}
detect_time_detla();
function detect_time_detla( cb, time ) {
var stime = rnow();
time && calculate(time) || SELF['time'](calculate);
function calculate(time) {
if (!time) return;
var ptime = time / 10000
, latency = (rnow() - stime) / 2;
TIME_DRIFT = rnow() - (ptime + latency);
cb && cb(TIME_DRIFT);
}
}
return SELF;

@@ -793,12 +984,19 @@ }

*/
var NOW = 1
, http = require('http')
, https = require('https')
, XHRTME = 310000
var NOW = 1
, http = require('http')
, https = require('https')
, XHRTME = 310000
, DEF_TIMEOUT = 10000
, SECOND = 1000
, PNSDK = 'PubNub-JS-' + 'Nodejs' + '/' + '3.5.3'
, XORIGN = 1;
, PNSDK = 'PubNub-JS-' + 'Nodejs' + '/' + '3.5.4'
, crypto = require('crypto')
, XORIGN = 1;
function get_hmac_SHA256(data, key) {
return crypto.createHmac('sha256',
new Buffer(key, 'utf8')).update(data).digest('base64');
}
/**

@@ -842,3 +1040,3 @@ * ERROR

}
, done = function(failed) {
, done = function(failed, response) {
if (complete) return;

@@ -856,3 +1054,3 @@ complete = 1;

}
failed && fail();
failed && fail(response);
}

@@ -865,3 +1063,3 @@ , timer = timeout( function(){done(1);} , xhrtme );

var options = {
hostname : origin,
hostname : setup.url[0].split("//")[1],
port : ssl ? 443 : 80,

@@ -874,8 +1072,26 @@ path : url,

response.setEncoding('utf8');
response.on( 'error', function(){done(1)});
response.on( 'abort', function(){done(1)});
response.on( 'error', function(){done(1, body || { "error" : "Network Connection Error"})});
response.on( 'abort', function(){done(1, body || { "error" : "Network Connection Error"})});
response.on( 'data', function (chunk) {
if (chunk) body += chunk;
} );
response.on( 'end', function(){finished();});
response.on( 'end', function(){
var statusCode = response.statusCode;
switch(statusCode) {
case 401:
case 402:
case 403:
try {
response = JSON['parse'](body);
done(1,response);
}
catch (r) { return done(1, body); }
return;
default:
break;
}
finished();
});
});

@@ -919,2 +1135,4 @@ request.end();

setup['error'] = error;
setup['PNSDK'] = PNSDK;
setup['hmac_SHA256'] = get_hmac_SHA256;
PN = PN_API(setup);

@@ -925,2 +1143,90 @@ PN.ready();

PUBNUB = exports.init({});
exports.secure = function(setup) {
var iv = "0123456789012345";
var cipher_key = setup['cipher_key'];
var padded_cipher_key = crypto.createHash('sha256').update(cipher_key).digest("hex").slice(0,32);
var pubnub = exports.init(setup);
function encrypt(data) {
var plain_text = JSON.stringify(data);
var cipher = crypto.createCipheriv('aes-256-cbc', padded_cipher_key, iv);
var base_64_encrypted = cipher.update(plain_text, 'utf8', 'base64') + cipher.final('base64');
return base_64_encrypted || data;
}
function decrypt(data) {
var decipher = crypto.createDecipheriv('aes-256-cbc', padded_cipher_key, iv);
try {
var decrypted = decipher.update(data, 'base64', 'utf8') + decipher.final('utf8');
} catch (e) {
return null;
}
return JSON.parse(decrypted);
}
SELF =
{
raw_encrypt : encrypt,
raw_decrypt : decrypt,
ready : pubnub.ready,
time : PUBNUB.time,
publish : function (args) {
args.message = encrypt(args.message);
return pubnub.publish(args);
},
unsubscribe : function (args) {
return pubnub.unsubscribe(args);
},
subscribe : function (args) {
var callback = args.callback || args.message;
args.callback = function (message, envelope, channel) {
var decrypted = decrypt(message);
if(decrypted) {
callback(decrypted, envelope, channel);
} else {
args.error && args.error({"error":"DECRYPT_ERROR", "message" : message});
}
}
return pubnub.subscribe(args);
},
history : function (args) {
var encrypted_messages = "";
var old_callback = args.callback;
var error_callback = args.error;
function new_callback(response) {
encrypted_messages = response[0];
var decrypted_messages = [];
var decrypted_error_messages = [];
var a;
for (a = 0; a < encrypted_messages.length; a++) {
var new_message = decrypt( encrypted_messages[a]);
if(new_message) {
decrypted_messages.push((new_message));
} else {
decrypted_error_messages.push({"error":"DECRYPT_ERROR", "message" : encrypted_messages[a]});
}
}
old_callback([
decrypted_messages,
response[1],
response[2]
]);
error_callback && error_callback([
decrypted_error_messages,
response[1],
response[2]
]);
}
args.callback = new_callback;
pubnub.history(args);
return true;
}
};
return SELF;
}
exports.unique = unique

@@ -1,74 +0,329 @@

var assert = require('assert');
var PUBNUB = require('../pubnub.js');
var assert = require('assert');
var PUBNUB = require('../pubnub.js');
var pubnub = PUBNUB.init({
publish_key : 'demo',
subscribe_key : 'demo'
});
var pubnub = PUBNUB.init({
publish_key : 'demo',
subscribe_key : 'demo'
});
var channel = 'javascript-test-channel-' + Date.now();
var count = 0;
var pubnub_enc = PUBNUB.secure({
publish_key : 'demo',
subscribe_key : 'demo',
cipher_key : 'enigma'
});
var message_string = 'Hi from Javascript';
var message_jsono = {'message': 'Hi Hi from Javascript'};
var message_jsona = ['message' , 'Hi Hi from javascript'];
var channel = 'javascript-test-channel-' + Date.now();
var count = 0;
describe('Pubnub', function() {
this.timeout(40000);
describe('#publish()', function(){
it('should publish strings without error', function(done){
var ch = channel + '-' + ++count;
pubnub.subscribe({channel : ch ,
connect : function(response) {
pubnub.publish({channel: ch , message : message_string,
callback : function(response) {
assert.deepEqual(response[0],1);
}
});
},
var message_string = "Hi from Javascript";
var message_jsono = {'message': 'Hi from Javascript'};
var message_jsona = ['message' , 'Hi from javascript'];
describe('Pubnub', function() {
this.timeout(180000);
describe('#subscribe()', function(){
it('should call error callback on decryption error', function(done){
var ch = channel + '-' + ++count;
pubnub_enc.subscribe({channel : ch ,
connect : function(response) {
pubnub.publish({channel: ch , message : message_string,
callback : function(response) {
assert.deepEqual(response[0],1);
}
});
},
callback : function(response) {
assert.ok(false);
pubnub_enc.unsubscribe({channel : ch});
done();
},
error : function(response) {
assert.deepEqual(response['error'],"DECRYPT_ERROR");
pubnub_enc.unsubscribe({channel : ch});
done();
}
})
})
})
describe('#publish()', function(){
it('should publish strings without error', function(done){
var ch = channel + '-' + ++count;
pubnub.subscribe({channel : ch ,
connect : function(response) {
pubnub.publish({channel: ch , message : message_string,
callback : function(response) {
assert.deepEqual(response[0],1);
}
});
},
callback : function(response) {
assert.deepEqual(response,message_string);
pubnub.unsubscribe({channel : ch});
done();
}
})
})
it('should publish strings without error when encryption is enabled', function(done){
var ch = channel + '-' + ++count;
pubnub_enc.subscribe({channel : ch ,
connect : function(response) {
pubnub_enc.publish({channel: ch , message : message_string,
callback : function(response) {
assert.deepEqual(response[0],1);
}
});
},
callback : function(response) {
assert.deepEqual(response,message_string);
pubnub_enc.unsubscribe({channel : ch});
done();
}
})
})
it('should publish json objects without error', function(done){
var ch = channel + '-' + ++count;
pubnub.subscribe({channel : ch ,
connect : function(response) {
pubnub.publish({channel: ch , message : message_jsono,
callback : function(response) {
assert.deepEqual(response[0],1);
}
});
},
callback : function(response) {
assert.deepEqual(response,message_jsono);
pubnub.unsubscribe({channel : ch});
done();
}
})
})
it('should publish json objects without error when encryption is enabled', function(done){
var ch = channel + '-' + ++count;
pubnub_enc.subscribe({channel : ch ,
connect : function(response) {
pubnub_enc.publish({channel: ch , message : message_jsono,
callback : function(response) {
assert.deepEqual(response[0],1);
}
});
},
callback : function(response) {
assert.deepEqual(response,message_jsono);
pubnub_enc.unsubscribe({channel : ch});
done();
}
})
})
it('should publish json arrays without error', function(done){
var ch = channel + '-' + ++count ;
pubnub.subscribe({channel : ch ,
connect : function(response) {
pubnub.publish({channel: ch , message : message_jsona,
callback : function(response) {
assert.deepEqual(response[0],1);
}
});
},
callback : function(response) {
assert.deepEqual(response,message_jsona);
pubnub.unsubscribe({channel : ch});
done();
}
})
})
it('should publish json arrays without error when encryption is enabled', function(done){
var ch = channel + '-' + ++count ;
pubnub_enc.subscribe({channel : ch ,
connect : function(response) {
pubnub_enc.publish({channel: ch , message : message_jsona,
callback : function(response) {
assert.deepEqual(response[0],1);
}
});
},
callback : function(response) {
assert.deepEqual(response,message_jsona);
pubnub_enc.unsubscribe({channel : ch});
done();
}
})
})
})
describe('#history()', function(){
var history_channel = channel + '-history';
before(function(done){
this.timeout(40000);
var x;
pubnub.publish({channel: history_channel,
message : message_string + '-1',
callback : function(response){
assert.deepEqual(response[0], 1);
pubnub.publish({channel: history_channel,
message : message_string + '-2',
callback : function(response){
assert.deepEqual(response[0], 1);
pubnub_enc.publish({channel: history_channel,
message : message_string + '-1',
callback : function(response){
assert.deepEqual(response[0], 1);
pubnub_enc.publish({channel: history_channel,
message : message_string + '-2',
callback : function(response){
assert.deepEqual(response[0], 1);
pubnub.publish({channel: history_channel,
message : message_string + '-1',
callback : function(response){
assert.deepEqual(response[0], 1);
pubnub.publish({channel: history_channel,
message : message_string + '-2',
callback : function(response){
assert.deepEqual(response[0], 1);
done();
}
});
}
});
}
});
}
});
}
});
}
});
})
it('should return 6 messages when 6 messages were published on channel', function(done) {
this.timeout(40000);
setTimeout(function() {
pubnub.history({channel : history_channel,
callback : function(response) {
assert.deepEqual(response,message_string);
pubnub.unsubscribe({channel : ch});
assert.deepEqual(response[0].length, 6);
assert.deepEqual(response[0][0], message_string + '-1');
assert.deepEqual(response[0][5], message_string + '-2');
done();
}
})
})
it('should publish json objects without error', function(done){
var ch = channel + '-' + ++count;
pubnub.subscribe({channel : ch ,
connect : function(response) {
pubnub.publish({channel: ch , message : message_jsono,
callback : function(response) {
assert.deepEqual(response[0],1);
}
});
},
},5000);
})
it('should return 1 message when 6 messages were published on channel and count is 1', function(done) {
this.timeout(40000);
setTimeout(function() {
pubnub.history({channel : history_channel,
count : 1,
callback : function(response) {
assert.deepEqual(response,message_jsono);
pubnub.unsubscribe({channel : ch});
assert.deepEqual(response[0].length, 1);
assert.deepEqual(response[0][0], message_string + '-2');
done();
}
})
})
it('should publish json arrays without error', function(done){
var ch = channel + '-' + ++count ;
pubnub.subscribe({channel : ch ,
connect : function(response) {
pubnub.publish({channel: ch , message : message_jsona,
callback : function(response) {
assert.deepEqual(response[0],1);
}
});
},5000);
})
it('should return 1 message from reverse when 6 messages were published on channel and count is 1', function(done) {
this.timeout(40000);
setTimeout(function() {
pubnub.history({channel : history_channel,
count : 1,
reverse : true,
callback : function(response) {
assert.deepEqual(response[0].length, 1);
assert.deepEqual(response[0][0], message_string + '-1');
done();
}
})
},5000);
})
it('should call error callbacks for messages which could not be decrypted when encryption is enabled', function(done) {
this.timeout(40000);
setTimeout(function() {
pubnub_enc.history({channel : history_channel,
callback : function(response) {
assert.deepEqual(response[0].length, 2);
},
error : function(response) {
assert.deepEqual(response[0].length, 4);
done();
}
})
},5000);
})
})
describe('#history() with encryption', function(){
var history_channel = channel + '-history-enc';
before(function(done){
this.timeout(40000);
var x;
pubnub_enc.publish({channel: history_channel,
message : message_string + '-1',
callback : function(response){
assert.deepEqual(response[0], 1);
pubnub_enc.publish({channel: history_channel,
message : message_string + '-2',
callback : function(response){
assert.deepEqual(response[0], 1);
done();
}
});
}
});
})
it('should return 2 messages when 2 messages were published on channel', function(done) {
this.timeout(40000);
setTimeout(function() {
pubnub_enc.history({channel : history_channel,
callback : function(response) {
assert.deepEqual(response,message_jsona);
pubnub.unsubscribe({channel : ch});
assert.deepEqual(response[0].length, 2);
assert.deepEqual(response[0][0], message_string + '-1');
assert.deepEqual(response[0][1], message_string + '-2');
done();
}
})
})
},5000);
})
it('should return 1 message when 2 messages were published on channel and count is 1', function(done) {
this.timeout(40000);
setTimeout(function() {
pubnub_enc.history({channel : history_channel,
count : 1,
callback : function(response) {
assert.deepEqual(response[0].length, 1);
assert.deepEqual(response[0][0], message_string + '-2');
done();
}
})
},5000);
})
it('should return 1 message from reverse when 2 messages were published on channel and count is 1', function(done) {
this.timeout(40000);
setTimeout(function() {
pubnub_enc.history({channel : history_channel,
count : 1,
reverse : true,
callback : function(response) {
assert.deepEqual(response[0].length, 1);
assert.deepEqual(response[0][0], message_string + '-1');
done();
}
})
},5000);
})
})
describe('#time()', function() {

@@ -120,26 +375,131 @@ it('should return time successfully when called', function(done){

})
describe('#history()', function(){
var history_channel = channel + '-history';
this.timeout(60000);
before(function(done){
pubnub.publish({channel: history_channel,
message : message_string,
callback : function(response){
assert.deepEqual(response[0],1);}
});
pubnub.publish({channel: history_channel,
message : message_string,
callback : function(response){
assert.deepEqual(response[0],1);
done();
}
});
describe('#grant()', function(){
var grant_channel = channel + '-grant';
var auth_key = "abcd";
var sub_key = 'sub-c-a478dd2a-c33d-11e2-883f-02ee2ddab7fe';
var pubnub = PUBNUB.init({
origin : 'pam-beta.pubnub.com',
publish_key : 'pub-c-a2650a22-deb1-44f5-aa87-1517049411d5',
subscribe_key : sub_key,
secret_key : 'sec-c-YjFmNzYzMGMtYmI3NC00NzJkLTlkYzYtY2MwMzI4YTJhNDVh'
});
it('should be able to grant read write access', function(done) {
var grant_channel_1 = grant_channel + '-1';
setTimeout(function() {
pubnub.grant({
channel : grant_channel_1,
auth_key : auth_key,
read : true,
write : true,
callback : function(response) {
assert.deepEqual(response.status,200);
pubnub.audit({
channel : grant_channel_1,
auth_key : auth_key,
callback : function(response) {
assert.deepEqual(response.status,200);
assert.deepEqual(response.payload.auths.abcd.r,1);
assert.deepEqual(response.payload.auths.abcd.w,1);
pubnub.history({
'channel' : grant_channel_1,
'auth_key' : auth_key,
'callback' : function(response) {
assert.ok(true);
pubnub.publish({
'channel' : grant_channel_1,
'auth_key' : auth_key,
'message' : 'Test',
'callback': function(response) {
assert.ok(true);
done();
},
'error' : function(response) {
assert.ok(false);
}
})
},
'error' : function(response) {
assert.ok(false);
pubnub.publish({
'channel' : grant_channel_1,
'message' : 'Test',
'auth_key' : auth_key,
'callback': function(response) {
assert.ok(true);
done();
},
'error' : function(response) {
assert.ok(false);
done();
}
})
}
});
}
});
}
})
},5000);
})
it('should return 2 messages when 2 messages were published on channel', function(done) {
it('should be able to grant read write access without auth key', function(done) {
var grant_channel_5 = grant_channel + '-5';
setTimeout(function() {
pubnub.history({channel : history_channel,
pubnub.grant({
channel : grant_channel_5,
read : true,
write : true,
callback : function(response) {
assert.deepEqual(response[0].length,2);
done();
assert.deepEqual(response.status,200);
pubnub.audit({
channel : grant_channel_5,
callback : function(response) {
assert.deepEqual(response.status,200);
assert.deepEqual(response.payload.channels[grant_channel_5].r,1);
assert.deepEqual(response.payload.channels[grant_channel_5].w,1);
assert.deepEqual(response.payload.subscribe_key,sub_key);
pubnub.history({
'channel' : grant_channel_5,
'auth_key' : "",
'callback' : function(response) {
assert.ok(true);
pubnub.publish({
'channel' : grant_channel_5,
'auth_key' : "",
'message' : 'Test',
'callback': function(response) {
assert.ok(true);
done();
},
'error' : function(response) {
assert.ok(false);
}
})
},
'error' : function(response) {
assert.ok(false);
pubnub.publish({
'channel' : grant_channel_5,
'message' : 'Test',
'auth_key' : "",
'callback': function(response) {
assert.ok(true);
done();
},
'error' : function(response) {
assert.ok(false);
done();
}
})
}
});
}
});
}

@@ -149,10 +509,63 @@ })

})
it('should return 1 message when 2 messages were published on channel and count is 1', function(done) {
it('should be able to grant read access revoke write access', function(done) {
var grant_channel_2 = grant_channel + '-2';
setTimeout(function() {
pubnub.history({channel : history_channel,
count : 1,
pubnub.grant({
channel : grant_channel_2,
auth_key : auth_key,
read : true,
write : false,
callback : function(response) {
assert.deepEqual(response[0].length,1);
done();
assert.deepEqual(response.status,200);
pubnub.audit({
channel : grant_channel_2,
auth_key : auth_key,
callback : function(response) {
assert.deepEqual(response.status,200);
assert.deepEqual(response.payload.auths.abcd.r,1);
assert.deepEqual(response.payload.auths.abcd.w,0);
pubnub.history({
'channel' : grant_channel_2,
'auth_key' : auth_key,
'callback' : function(response) {
assert.ok(true)
pubnub.publish({
'channel' : grant_channel_2,
'auth_key' : auth_key,
'message' : 'Test',
'callback': function(response) {
assert.ok(false);
done();
},
'error' : function(response) {
assert.ok(true);
assert.deepEqual(response.status, 403);
done();
}
})
},
'error' : function(response) {
assert.ok(false);
pubnub.publish({
'channel' : grant_channel_2,
'message' : 'Test',
'auth_key' : auth_key,
'callback': function(response) {
assert.ok(false);
done();
},
'error' : function(response) {
assert.ok(true);
assert.deepEqual(response.status, 403);
done();
}
})
}
});
}
});
}

@@ -162,4 +575,274 @@ })

})
it('should be able to revoke read access grant write access', function(done) {
var grant_channel_3 = grant_channel + '-3';
setTimeout(function() {
pubnub.grant({
channel : grant_channel_3,
auth_key : auth_key,
read : false,
write : true,
callback : function(response) {
assert.deepEqual(response.status,200);
pubnub.audit({
channel : grant_channel_3,
auth_key : auth_key,
callback : function(response) {
assert.deepEqual(response.status,200);
assert.deepEqual(response.payload.auths.abcd.r,0);
assert.deepEqual(response.payload.auths.abcd.w,1);
pubnub.history({
'channel' : grant_channel_3,
'auth_key' : auth_key,
'callback' : function(response) {
assert.ok(false);
pubnub.publish({
'channel' : grant_channel_3,
'auth_key' : auth_key,
'message' : 'Test',
'callback': function(response) {
assert.ok(true);
done();
},
'error' : function(response) {
assert.ok(false);
done()
}
})
},
'error' : function(response) {
assert.ok(true);
assert.deepEqual(response.status, 403);
pubnub.publish({
'channel' : grant_channel_3,
'message' : 'Test',
'auth_key' : auth_key,
'callback': function(response) {
assert.ok(true)
done();
},
'error' : function(response) {
assert.ok(false);
done();
}
})
}
});
}
});
}
})
},5000);
})
it('should be able to revoke read and write access', function(done) {
var grant_channel_4 = grant_channel + '-4';
setTimeout(function() {
pubnub.grant({
channel : grant_channel_4,
auth_key : auth_key,
read : false,
write : false,
callback : function(response) {
assert.deepEqual(response.status,200);
pubnub.audit({
channel : grant_channel_4,
auth_key : auth_key,
callback : function(response) {
assert.deepEqual(response.status,200);
assert.deepEqual(response.payload.auths.abcd.r,0);
assert.deepEqual(response.payload.auths.abcd.w,0);
pubnub.history({
'channel' : grant_channel_4,
'auth_key' : auth_key,
'callback' : function(response) {
assert.ok(false);
pubnub.publish({
'channel' : grant_channel_4,
'auth_key' : auth_key,
'message' : 'Test',
'callback': function(response) {
assert.ok(false);
done();
},
'error' : function(response) {
assert.ok(true);
assert.deepEqual(response.status, 403);
done();
}
})
},
'error' : function(response) {
assert.ok(true);
assert.deepEqual(response.status, 403);
pubnub.publish({
'channel' : grant_channel_4,
'message' : 'Test',
'auth_key' : auth_key,
'callback': function(response) {
assert.ok(false);
done();
},
'error' : function(response) {
assert.ok(true);
assert.deepEqual(response.status, 403);
done();
}
})
}
});
}
});
}
})
},5000);
})
it('should be able to revoke read and write access without auth key', function(done) {
var grant_channel_6 = grant_channel + '-6';
setTimeout(function() {
pubnub.grant({
channel : grant_channel_6,
read : false,
write : false,
callback : function(response) {
assert.deepEqual(response.status,200);
pubnub.audit({
channel : grant_channel_6,
callback : function(response) {
assert.deepEqual(response.status,200);
assert.deepEqual(response.payload.channels[grant_channel_6].r,0);
assert.deepEqual(response.payload.channels[grant_channel_6].w,0);
assert.deepEqual(response.payload.subscribe_key,sub_key);
pubnub.history({
'channel' : grant_channel_6,
'auth_key' : "",
'callback' : function(response) {
assert.ok(false);
pubnub.publish({
'channel' : grant_channel_6,
'auth_key' : "",
'message' : 'Test',
'callback': function(response) {
assert.ok(false);
done();
},
'error' : function(response) {
assert.ok(true);
assert.deepEqual(response.status, 403);
done();
}
})
},
'error' : function(response) {
assert.ok(true);
assert.deepEqual(response.status, 403);
pubnub.publish({
'channel' : grant_channel_6,
'message' : 'Test',
'auth_key' : "",
'callback': function(response) {
assert.ok(false);
done();
},
'error' : function(response) {
assert.ok(true);
assert.deepEqual(response.status, 403);
done();
}
})
}
});
}
});
}
})
},5000);
})
})
describe('#revoke()', function(){
var revoke_channel = channel + '-revoke';
var auth_key = "abcd";
var pubnub = PUBNUB.init({
origin : 'pam-beta.pubnub.com',
publish_key : 'pub-c-a2650a22-deb1-44f5-aa87-1517049411d5',
subscribe_key : 'sub-c-a478dd2a-c33d-11e2-883f-02ee2ddab7fe',
secret_key : 'sec-c-YjFmNzYzMGMtYmI3NC00NzJkLTlkYzYtY2MwMzI4YTJhNDVh'
});
it('should be able to revoke access', function(done) {
setTimeout(function() {
pubnub.revoke({
channel : revoke_channel,
auth_key : auth_key,
callback : function(response) {
assert.deepEqual(response.status,200);
pubnub.audit({
channel : revoke_channel,
auth_key : auth_key,
callback : function(response) {
assert.deepEqual(response.status,200);
assert.deepEqual(response.payload.auths.abcd.r,0);
assert.deepEqual(response.payload.auths.abcd.w,0);
pubnub.history({
'channel' : revoke_channel,
'auth_key' : auth_key,
'callback' : function(response) {
assert.ok(false);
pubnub.publish({
'channel' : revoke_channel,
'auth_key' : auth_key,
'message' : 'Test',
'callback': function(response) {
assert.ok(false);
done();
},
'error' : function(response) {
assert.ok(true);
assert.deepEqual(response.status, 403);
done();
}
})
},
'error' : function(response) {
assert.ok(true)
assert.deepEqual(response.status, 403);
pubnub.publish({
'channel' : revoke_channel,
'message' : 'Test',
'auth_key' : auth_key,
'callback': function(response) {
assert.ok(false);
done();
},
'error' : function(response) {
assert.ok(true);
assert.deepEqual(response.status, 403);
done();
}
})
}
});
}
});
}
})
},5000);
})
})
})

@@ -20,3 +20,3 @@ var assert = require('assert');

callback : function(response) {
done()
done()
}

@@ -29,3 +29,3 @@ });

callback : function(response) {
done()
done()
}

@@ -38,3 +38,3 @@ });

callback : function(response) {
done()
done()
}

@@ -47,3 +47,3 @@ });

callback : function(response) {
done()
done()
}

@@ -56,3 +56,3 @@ });

callback : function(response) {
done()
done()
}

@@ -65,3 +65,3 @@ });

callback : function(response) {
done()
done()
}

@@ -74,3 +74,3 @@ });

callback : function(response) {
done()
done()
}

@@ -83,3 +83,3 @@ });

callback : function(response) {
done()
done()
}

@@ -90,2 +90,2 @@ });

})
})
})

@@ -35,12 +35,19 @@ /* ---------------------------------------------------------------------------

*/
var NOW = 1
, http = require('http')
, https = require('https')
, XHRTME = 310000
var NOW = 1
, http = require('http')
, https = require('https')
, XHRTME = 310000
, DEF_TIMEOUT = 10000
, SECOND = 1000
, PNSDK = 'PubNub-JS-' + PLATFORM + '/' + VERSION
, XORIGN = 1;
, PNSDK = 'PubNub-JS-' + PLATFORM + '/' + VERSION
, crypto = require('crypto')
, XORIGN = 1;
function get_hmac_SHA256(data, key) {
return crypto.createHmac('sha256',
new Buffer(key, 'utf8')).update(data).digest('base64');
}
/**

@@ -84,3 +91,3 @@ * ERROR

}
, done = function(failed) {
, done = function(failed, response) {
if (complete) return;

@@ -98,3 +105,3 @@ complete = 1;

}
failed && fail();
failed && fail(response);
}

@@ -107,3 +114,3 @@ , timer = timeout( function(){done(1);} , xhrtme );

var options = {
hostname : origin,
hostname : setup.url[0].split("//")[1],
port : ssl ? 443 : 80,

@@ -116,8 +123,26 @@ path : url,

response.setEncoding('utf8');
response.on( 'error', function(){done(1)});
response.on( 'abort', function(){done(1)});
response.on( 'error', function(){done(1, body || { "error" : "Network Connection Error"})});
response.on( 'abort', function(){done(1, body || { "error" : "Network Connection Error"})});
response.on( 'data', function (chunk) {
if (chunk) body += chunk;
} );
response.on( 'end', function(){finished();});
response.on( 'end', function(){
var statusCode = response.statusCode;
switch(statusCode) {
case 401:
case 402:
case 403:
try {
response = JSON['parse'](body);
done(1,response);
}
catch (r) { return done(1, body); }
return;
default:
break;
}
finished();
});
});

@@ -161,2 +186,4 @@ request.end();

setup['error'] = error;
setup['PNSDK'] = PNSDK;
setup['hmac_SHA256'] = get_hmac_SHA256;
PN = PN_API(setup);

@@ -167,2 +194,90 @@ PN.ready();

PUBNUB = exports.init({});
exports.secure = function(setup) {
var iv = "0123456789012345";
var cipher_key = setup['cipher_key'];
var padded_cipher_key = crypto.createHash('sha256').update(cipher_key).digest("hex").slice(0,32);
var pubnub = exports.init(setup);
function encrypt(data) {
var plain_text = JSON.stringify(data);
var cipher = crypto.createCipheriv('aes-256-cbc', padded_cipher_key, iv);
var base_64_encrypted = cipher.update(plain_text, 'utf8', 'base64') + cipher.final('base64');
return base_64_encrypted || data;
}
function decrypt(data) {
var decipher = crypto.createDecipheriv('aes-256-cbc', padded_cipher_key, iv);
try {
var decrypted = decipher.update(data, 'base64', 'utf8') + decipher.final('utf8');
} catch (e) {
return null;
}
return JSON.parse(decrypted);
}
SELF =
{
raw_encrypt : encrypt,
raw_decrypt : decrypt,
ready : pubnub.ready,
time : PUBNUB.time,
publish : function (args) {
args.message = encrypt(args.message);
return pubnub.publish(args);
},
unsubscribe : function (args) {
return pubnub.unsubscribe(args);
},
subscribe : function (args) {
var callback = args.callback || args.message;
args.callback = function (message, envelope, channel) {
var decrypted = decrypt(message);
if(decrypted) {
callback(decrypted, envelope, channel);
} else {
args.error && args.error({"error":"DECRYPT_ERROR", "message" : message});
}
}
return pubnub.subscribe(args);
},
history : function (args) {
var encrypted_messages = "";
var old_callback = args.callback;
var error_callback = args.error;
function new_callback(response) {
encrypted_messages = response[0];
var decrypted_messages = [];
var decrypted_error_messages = [];
var a;
for (a = 0; a < encrypted_messages.length; a++) {
var new_message = decrypt( encrypted_messages[a]);
if(new_message) {
decrypted_messages.push((new_message));
} else {
decrypted_error_messages.push({"error":"DECRYPT_ERROR", "message" : encrypted_messages[a]});
}
}
old_callback([
decrypted_messages,
response[1],
response[2]
]);
error_callback && error_callback([
decrypted_error_messages,
response[1],
response[2]
]);
}
args.callback = new_callback;
pubnub.history(args);
return true;
}
};
return SELF;
}
exports.unique = unique
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc