Comparing version 3.5.48 to 3.6.1
@@ -14,3 +14,3 @@ /* --------------------------------------------------------------------------- | ||
/* --------------------------------------------------------------------------- | ||
- Main - | ||
- Main - | ||
--------------------------------------------------------------------------- */ | ||
@@ -17,0 +17,0 @@ grant() |
@@ -8,7 +8,12 @@ /* --------------------------------------------------------------------------- | ||
var pubnub = require("./../pubnub.js").init({ | ||
var PUBNUB = require("../pubnub.js") | ||
var pubnub = PUBNUB({ | ||
publish_key : "demo", | ||
subscribe_key : "demo" | ||
//cipher_key : "demo" | ||
}); | ||
console.log(pubnub.get_version()); | ||
/* --------------------------------------------------------------------------- | ||
@@ -19,6 +24,10 @@ Listen for Messages | ||
channel : "a", | ||
windowing : 10000, | ||
callback : function(message) { | ||
console.log( " > ", message ); | ||
} | ||
}, | ||
error : function(r) { | ||
console.log(JSON.stringify(r)); | ||
}, | ||
presence : function(r) { console.log(JSON.stringify(r)) } | ||
}); |
@@ -17,3 +17,4 @@ /* --------------------------------------------------------------------------- | ||
var delivery_count = 0; | ||
var crazy = ' ~`!@#$%^&*(顶顅Ȓ)+=[]\\{}|;\':"./<>?abcd' | ||
//var crazy = ' ~`!@#$%^&*(顶顅Ȓ)+=[]\\{}|;\':"./<>?abcd' | ||
var crazy = ' ~`!@#$%^&*(顶顅Ȓ)+=[]\\{}|;\':"./<>abcd' | ||
@@ -27,2 +28,4 @@ /* --------------------------------------------------------------------------- | ||
console.log('connected'); | ||
// Publish a Message on Connect | ||
@@ -36,2 +39,5 @@ network.publish({ | ||
}, | ||
error : function(info){ | ||
console.log(info); | ||
}, | ||
callback : function(info){ | ||
@@ -38,0 +44,0 @@ if (!info[0]) console.log("Failed Message Delivery") |
{ | ||
"name": "pubnub", | ||
"preferGlobal": false, | ||
"version": "3.5.48", | ||
"version": "3.6.1", | ||
"author": "PubNub <stephen@pubnub.com>", | ||
@@ -6,0 +6,0 @@ "description": "Publish & Subscribe Real-time Messaging with PubNub", |
667
pubnub.js
@@ -1,2 +0,2 @@ | ||
// Version: 3.5.48 | ||
// Version: 3.6.1 | ||
var NOW = 1 | ||
@@ -13,2 +13,5 @@ , READY = false | ||
, PARAMSBIT = '&' | ||
, PRESENCE_HB_THRESHOLD = 5 | ||
, PRESENCE_HB_DEFAULT = 30 | ||
, SDK_VER = '3.6.1' | ||
, REPL = /{([\w\-]+)}/g; | ||
@@ -53,9 +56,9 @@ | ||
each( url_params, function( key, value ) { | ||
var value_str = (typeof value == 'object')?JSON['stringify'](value):value; | ||
(typeof value != 'undefined' && | ||
value != null && encode(value).length > 0 | ||
) && params.push(key + "=" + encode(value)); | ||
value != null && encode(value_str).length > 0 | ||
) && params.push(key + "=" + encode(value_str)); | ||
} ); | ||
url += "?" + params.join(PARAMSBIT); | ||
return url; | ||
@@ -131,2 +134,6 @@ } | ||
} | ||
function isArray(arg) { | ||
var type = Object.prototype.toString.call(arg); | ||
return ( type === "[object Array]" || type === "[object NodeList]" || type === "[object ScriptBridgingArrayProxyObject]"); | ||
} | ||
@@ -138,6 +145,6 @@ /** | ||
*/ | ||
function each( o, f ) { | ||
function each( o, f, old_logic) { | ||
if ( !o || !f ) return; | ||
if ( typeof o[0] != 'undefined' ) | ||
if ( isArray(o) || ( old_logic && typeof o[0] != 'undefined' ) ) | ||
for ( var i = 0, l = o.length; i < l; ) | ||
@@ -175,10 +182,17 @@ f.call( o[i], o[i], i++ ); | ||
*/ | ||
function generate_channel_list(channels) { | ||
function generate_channel_list(channels, nopresence) { | ||
var list = []; | ||
each( channels, function( channel, status ) { | ||
if (status.subscribed) list.push(channel); | ||
} ); | ||
if (nopresence) { | ||
if(channel.search('-pnpres') < 0) { | ||
if (status.subscribed) list.push(channel); | ||
} | ||
} else { | ||
if (status.subscribed) list.push(channel); | ||
} | ||
}); | ||
return list.sort(); | ||
} | ||
// PUBNUB READY TO CONNECT | ||
@@ -215,4 +229,11 @@ function ready() { timeout( function() { | ||
, TIMETOKEN = 0 | ||
, RESUMED = false | ||
, CHANNELS = {} | ||
, STATE = {} | ||
, PRESENCE_HB_TIMEOUT = null | ||
, PRESENCE_HB = validate_presence_heartbeat(setup['heartbeat'] || setup['pnexpires'] || 0, setup['error']) | ||
, PRESENCE_HB_INTERVAL = setup['heartbeat_interval'] || PRESENCE_HB - 3 | ||
, PRESENCE_HB_RUNNING = false | ||
, NO_WAIT_FOR_PENDING = setup['no_wait_for_pending'] | ||
, COMPATIBLE_35 = setup['compatible_3.5'] || false | ||
, xdr = setup['xdr'] | ||
@@ -223,4 +244,73 @@ , error = setup['error'] || function() {} | ||
, db = setup['db'] || {'get': function(){}, 'set': function(){}} | ||
, CIPHER_KEY = setup['cipher_key'] | ||
, UUID = setup['uuid'] || ( db && db['get'](SUBSCRIBE_KEY+'uuid') || ''); | ||
var crypto_obj = setup['crypto_obj'] || | ||
{ | ||
'encrypt' : function(a,key){ return a}, | ||
'decrypt' : function(b,key){return b} | ||
}; | ||
function validate_presence_heartbeat(heartbeat, cur_heartbeat, error) { | ||
var err = false; | ||
if (typeof heartbeat === 'number') { | ||
if (heartbeat > PRESENCE_HB_THRESHOLD || heartbeat == 0) | ||
err = false; | ||
else | ||
err = true; | ||
} else if(typeof heartbeat === 'boolean'){ | ||
if (!heartbeat) { | ||
return 0; | ||
} else { | ||
return PRESENCE_HB_DEFAULT; | ||
} | ||
} else { | ||
err = true; | ||
} | ||
if (err) { | ||
error && error("Presence Heartbeat value invalid. Valid range ( x > " + PRESENCE_HB_THRESHOLD + " or x = 0). Current Value : " + (cur_heartbeat || PRESENCE_HB_THRESHOLD)); | ||
return cur_heartbeat || PRESENCE_HB_THRESHOLD; | ||
} else return heartbeat; | ||
} | ||
function encrypt(input, key) { | ||
return crypto_obj['encrypt'](input, key || CIPHER_KEY) || input; | ||
} | ||
function decrypt(input, key) { | ||
return crypto_obj['decrypt'](input, key || CIPHER_KEY) || | ||
crypto_obj['decrypt'](input, CIPHER_KEY) || | ||
input; | ||
} | ||
function error_common(message, callback) { | ||
callback && callback({ 'error' : message || "error occurred"}); | ||
error && error(message); | ||
} | ||
function _presence_heartbeat() { | ||
clearTimeout(PRESENCE_HB_TIMEOUT); | ||
if (!PRESENCE_HB_INTERVAL || PRESENCE_HB_INTERVAL >= 500 || PRESENCE_HB_INTERVAL < 1 || !generate_channel_list(CHANNELS,true).length){ | ||
PRESENCE_HB_RUNNING = false; | ||
return; | ||
} | ||
PRESENCE_HB_RUNNING = true; | ||
SELF['presence_heartbeat']({ | ||
'callback' : function(r) { | ||
PRESENCE_HB_TIMEOUT = timeout( _presence_heartbeat, (PRESENCE_HB_INTERVAL) * SECOND ); | ||
}, | ||
'error' : function(e) { | ||
error && error("Presence Heartbeat unable to reach Pubnub servers." + JSON.stringify(e)); | ||
PRESENCE_HB_TIMEOUT = timeout( _presence_heartbeat, (PRESENCE_HB_INTERVAL) * SECOND ); | ||
} | ||
}); | ||
} | ||
function start_presence_heartbeat() { | ||
!PRESENCE_HB_RUNNING && _presence_heartbeat(); | ||
} | ||
function publish(next) { | ||
@@ -235,3 +325,3 @@ | ||
} | ||
xdr(PUB_QUEUE.shift()); | ||
@@ -254,3 +344,22 @@ } | ||
} | ||
function _invoke_callback(response, callback, err) { | ||
if (typeof response == 'object') { | ||
if (response['error'] && response['message'] && response['payload']) { | ||
err({'message' : response['message'], 'payload' : response['payload']}); | ||
return; | ||
} | ||
if (response['payload']) { | ||
callback(response['payload']); | ||
return; | ||
} | ||
} | ||
callback(response); | ||
} | ||
function _invoke_error(response,err) { | ||
if (typeof response == 'object' && response['error']) { | ||
err({'message' : response['message'], 'payload' : response['payload']}); | ||
} else err(response); | ||
} | ||
// Announce Leave Event | ||
@@ -269,6 +378,8 @@ var SELF = { | ||
// No Leave Patch (Prevent Blocking Leave if Desired) | ||
if (NOLEAVE) return false; | ||
if (!SSL) return false; | ||
if (jsonp == '0') return false; | ||
if (COMPATIBLE_35) { | ||
if (!SSL) return false; | ||
if (jsonp == '0') return false; | ||
} | ||
if (NOLEAVE) return false; | ||
@@ -283,9 +394,7 @@ if (jsonp != '0') data['callback'] = jsonp; | ||
success : function(response) { | ||
if (typeof response == 'object' && response['error']) { | ||
err(response); | ||
return; | ||
} | ||
callback(response) | ||
_invoke_callback(response, callback, err); | ||
}, | ||
fail : err, | ||
fail : function(response) { | ||
_invoke_error(response, err); | ||
}, | ||
url : [ | ||
@@ -298,2 +407,37 @@ origin, 'v2', 'presence', 'sub_key', | ||
}, | ||
'set_resumed' : function(resumed) { | ||
RESUMED = resumed; | ||
}, | ||
'get_cipher_key' : function() { | ||
return CIPHER_KEY; | ||
}, | ||
'set_cipher_key' : function(key) { | ||
CIPHER_KEY = key; | ||
}, | ||
'raw_encrypt' : function(input, key) { | ||
return encrypt(input, key); | ||
}, | ||
'raw_decrypt' : function(input, key) { | ||
return decrypt(input, key); | ||
}, | ||
'get_heartbeat' : function() { | ||
return PRESENCE_HB; | ||
}, | ||
'set_heartbeat' : function(heartbeat) { | ||
PRESENCE_HB = validate_presence_heartbeat(heartbeat, PRESENCE_HB_INTERVAL, error); | ||
PRESENCE_HB_INTERVAL = (PRESENCE_HB - 3 >= 1)?PRESENCE_HB - 3:1; | ||
CONNECT(); | ||
_presence_heartbeat(); | ||
}, | ||
'get_heartbeat_interval' : function() { | ||
return PRESENCE_HB_INTERVAL; | ||
}, | ||
'set_heartbeat_interval' : function(heartbeat_interval) { | ||
PRESENCE_HB_INTERVAL = heartbeat_interval; | ||
_presence_heartbeat(); | ||
}, | ||
'get_version' : function() { | ||
return SDK_VER; | ||
}, | ||
/* | ||
@@ -307,12 +451,14 @@ PUBNUB.history({ | ||
'history' : function( args, callback ) { | ||
var callback = args['callback'] || callback | ||
, count = args['count'] || args['limit'] || 100 | ||
, reverse = args['reverse'] || "false" | ||
, err = args['error'] || function(){} | ||
, auth_key = args['auth_key'] || AUTH_KEY | ||
, channel = args['channel'] | ||
, start = args['start'] | ||
, end = args['end'] | ||
, params = {} | ||
, jsonp = jsonp_cb(); | ||
var callback = args['callback'] || callback | ||
, count = args['count'] || args['limit'] || 100 | ||
, reverse = args['reverse'] || "false" | ||
, err = args['error'] || function(){} | ||
, auth_key = args['auth_key'] || AUTH_KEY | ||
, cipher_key = args['cipher_key'] | ||
, channel = args['channel'] | ||
, start = args['start'] | ||
, end = args['end'] | ||
, include_token = args['include_token'] | ||
, params = {} | ||
, jsonp = jsonp_cb(); | ||
@@ -329,5 +475,6 @@ // Make sure we have a Channel | ||
if (jsonp) params['callback'] = jsonp; | ||
if (start) params['start'] = start; | ||
if (end) params['end'] = end; | ||
if (jsonp) params['callback'] = jsonp; | ||
if (start) params['start'] = start; | ||
if (end) params['end'] = end; | ||
if (include_token) params['include_token'] = 'true'; | ||
@@ -340,8 +487,20 @@ // Send Message | ||
if (typeof response == 'object' && response['error']) { | ||
err(response); | ||
err({'message' : response['message'], 'payload' : response['payload']}); | ||
return; | ||
} | ||
callback(response) | ||
var messages = response[0]; | ||
var decrypted_messages = []; | ||
for (var a = 0; a < messages.length; a++) { | ||
var new_message = decrypt(messages[a],cipher_key); | ||
try { | ||
decrypted_messages['push'](JSON['parse'](new_message)); | ||
} catch (e) { | ||
decrypted_messages['push']((new_message)); | ||
} | ||
} | ||
callback([decrypted_messages, response[1], response[2]]); | ||
}, | ||
fail : err, | ||
fail : function(response) { | ||
_invoke_error(response, err); | ||
}, | ||
url : [ | ||
@@ -360,3 +519,3 @@ STD_ORIGIN, 'v2', 'history', 'sub-key', | ||
*/ | ||
'replay' : function(args) { | ||
'replay' : function(args, callback) { | ||
var callback = callback || args['callback'] || function(){} | ||
@@ -402,7 +561,3 @@ , auth_key = args['auth_key'] || AUTH_KEY | ||
success : function(response) { | ||
if (typeof response == 'object' && response['error']) { | ||
err(response); | ||
return; | ||
} | ||
callback(response) | ||
_invoke_callback(response, callback, err); | ||
}, | ||
@@ -449,2 +604,3 @@ fail : function() { callback([ 0, 'Disconnected' ]) }, | ||
, auth_key = args['auth_key'] || AUTH_KEY | ||
, cipher_key = args['cipher_key'] | ||
, err = args['error'] || function() {} | ||
@@ -463,3 +619,3 @@ , jsonp = jsonp_cb() | ||
// If trying to send Object | ||
msg = JSON['stringify'](msg); | ||
msg = JSON['stringify'](encrypt(msg, cipher_key)); | ||
@@ -480,9 +636,8 @@ // Create URL | ||
data : { 'uuid' : UUID, 'auth' : auth_key }, | ||
fail : function(response){err(response);publish(1)}, | ||
fail : function(response){ | ||
_invoke_error(response, err); | ||
publish(1); | ||
}, | ||
success : function(response) { | ||
if (typeof response == 'object' && response['error']) | ||
err(response); | ||
else | ||
callback(response) | ||
_invoke_callback(response, callback, err); | ||
publish(1); | ||
@@ -524,2 +679,3 @@ } | ||
CHANNELS[channel] = 0; | ||
if (channel in STATE) delete STATE[channel]; | ||
} ); | ||
@@ -553,2 +709,4 @@ | ||
, windowing = args['windowing'] || SUB_WINDOWING | ||
, state = args['state'] | ||
, heartbeat = args['heartbeat'] || args['pnexpires'] | ||
, restore = args['restore']; | ||
@@ -567,2 +725,6 @@ | ||
if (heartbeat || heartbeat === 0) { | ||
SELF['set_heartbeat'](heartbeat); | ||
} | ||
// Setup Channel(s) | ||
@@ -580,2 +742,3 @@ each( (channel.join ? channel.join(',') : ''+channel).split(','), | ||
callback : SUB_CALLBACK = callback, | ||
'cipher_key' : args['cipher_key'], | ||
connect : connect, | ||
@@ -585,2 +748,9 @@ disconnect : disconnect, | ||
}; | ||
if (state) { | ||
if (channel in state) { | ||
STATE[channel] = state[channel]; | ||
} else { | ||
STATE[channel] = state; | ||
} | ||
} | ||
@@ -658,2 +828,11 @@ // Presence Enabled? | ||
_reset_offline(); | ||
var data = { 'uuid' : UUID, 'auth' : auth_key }; | ||
var st = JSON.stringify(STATE); | ||
if (st.length > 2) data['state'] = JSON.stringify(STATE); | ||
if (PRESENCE_HB) data['heartbeat'] = PRESENCE_HB; | ||
start_presence_heartbeat(); | ||
SUB_RECEIVER = xdr({ | ||
@@ -663,7 +842,7 @@ timeout : sub_timeout, | ||
fail : function(response) { | ||
errcb(response); | ||
SUB_RECEIVER = null; | ||
_invoke_error(response, errcb); | ||
//SUB_RECEIVER = null; | ||
SELF['time'](_test_connection); | ||
}, | ||
data : { 'uuid' : UUID, 'auth' : auth_key }, | ||
data : data, | ||
url : [ | ||
@@ -675,3 +854,4 @@ SUB_ORIGIN, 'subscribe', | ||
success : function(messages) { | ||
SUB_RECEIVER = null; | ||
//SUB_RECEIVER = null; | ||
// Check for Errors | ||
@@ -683,3 +863,3 @@ if (!messages || ( | ||
)) { | ||
errcb(messages); | ||
errcb(messages['error']); | ||
return timeout( CONNECT, SECOND ); | ||
@@ -703,2 +883,11 @@ } | ||
if (RESUMED && !SUB_RESTORE) { | ||
TIMETOKEN = 0; | ||
RESUMED = false; | ||
// Update Saved Timetoken | ||
db['set']( SUBSCRIBE_KEY, 0 ); | ||
timeout( _connect, windowing ); | ||
return; | ||
} | ||
// Invoke Memory Catchup and Receive Up to 100 | ||
@@ -717,3 +906,3 @@ // Previous Messages from the Queue. | ||
var channels = (messages.length>2?messages[2]:map( | ||
CHANNELS, function(chan) { return map( | ||
generate_channel_list(CHANNELS), function(chan) { return map( | ||
Array(messages[0].length) | ||
@@ -738,4 +927,9 @@ .join(',').split(','), | ||
var next = next_callback(); | ||
next[0]( msg, messages, next[1], latency ); | ||
} ); | ||
var decrypted_msg = decrypt(msg,CHANNELS[next[1]]['cipher_key']); | ||
try { | ||
next[0]( JSON['parse'](decrypted_msg), messages, next[1], latency ); | ||
} catch (e) { | ||
next[0]( decrypted_msg, messages, next[1], latency ); | ||
} | ||
}); | ||
@@ -768,9 +962,20 @@ timeout( _connect, windowing ); | ||
, jsonp = jsonp_cb() | ||
, uuids = ('uuids' in args) ? args['uuids'] : true | ||
, state = args['state'] | ||
, data = { 'uuid' : UUID, 'auth' : auth_key }; | ||
if (!uuids) data['disable_uuids'] = 1; | ||
if (state) data['state'] = 1; | ||
// 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'); | ||
var url = [ | ||
STD_ORIGIN, 'v2', 'presence', | ||
'sub_key', SUBSCRIBE_KEY | ||
]; | ||
channel && url.push('channel') && url.push(encode(channel)); | ||
if (jsonp != '0') { data['callback'] = jsonp; } | ||
@@ -782,13 +987,41 @@ | ||
success : function(response) { | ||
if (typeof response == 'object' && response['error']) { | ||
err(response); | ||
return; | ||
} | ||
callback(response) | ||
_invoke_callback(response, callback, err); | ||
}, | ||
fail : err, | ||
fail : function(response) { | ||
_invoke_error(response, err); | ||
}, | ||
url : url | ||
}); | ||
}, | ||
/* | ||
PUBNUB.current_channels_by_uuid({ channel : 'my_chat', callback : fun }); | ||
*/ | ||
'where_now' : function( args, callback ) { | ||
var callback = args['callback'] || callback | ||
, err = args['error'] || function(){} | ||
, auth_key = args['auth_key'] || AUTH_KEY | ||
, jsonp = jsonp_cb() | ||
, uuid = args['uuid'] || UUID | ||
, data = { 'auth' : auth_key }; | ||
// Make sure we have a Channel | ||
if (!callback) return error('Missing Callback'); | ||
if (!SUBSCRIBE_KEY) return error('Missing Subscribe Key'); | ||
if (jsonp != '0') { data['callback'] = jsonp; } | ||
xdr({ | ||
callback : jsonp, | ||
data : data, | ||
success : function(response) { | ||
_invoke_callback(response, callback, err); | ||
}, | ||
fail : function(response) { | ||
_invoke_error(response, err); | ||
}, | ||
url : [ | ||
STD_ORIGIN, 'v2', 'presence', | ||
'sub_key', SUBSCRIBE_KEY, | ||
'channel', encode(channel) | ||
'uuid', encode(uuid) | ||
] | ||
@@ -798,2 +1031,55 @@ }); | ||
'state' : function(args, callback) { | ||
var callback = args['callback'] || callback || function(r) {} | ||
, err = args['error'] || function(){} | ||
, auth_key = args['auth_key'] || AUTH_KEY | ||
, jsonp = jsonp_cb() | ||
, state = args['state'] | ||
, uuid = args['uuid'] || UUID | ||
, channel = args['channel'] | ||
, url | ||
, data = { 'auth' : auth_key }; | ||
// Make sure we have a Channel | ||
if (!SUBSCRIBE_KEY) return error('Missing Subscribe Key'); | ||
if (!uuid) return error('Missing UUID'); | ||
if (!channel) return error('Missing Channel'); | ||
if (jsonp != '0') { data['callback'] = jsonp; } | ||
if (CHANNELS[channel] && CHANNELS[channel].subscribed) STATE[channel] = state; | ||
data['state'] = JSON.stringify(state); | ||
if (state) { | ||
url = [ | ||
STD_ORIGIN, 'v2', 'presence', | ||
'sub-key', SUBSCRIBE_KEY, | ||
'channel', encode(channel), | ||
'uuid', uuid, 'data' | ||
] | ||
} else { | ||
url = [ | ||
STD_ORIGIN, 'v2', 'presence', | ||
'sub-key', SUBSCRIBE_KEY, | ||
'channel', encode(channel), | ||
'uuid', encode(uuid) | ||
] | ||
} | ||
xdr({ | ||
callback : jsonp, | ||
data : data, | ||
success : function(response) { | ||
_invoke_callback(response, callback, err); | ||
}, | ||
fail : function(response) { | ||
_invoke_error(response, err); | ||
}, | ||
url : url | ||
}); | ||
}, | ||
/* | ||
@@ -804,3 +1090,3 @@ PUBNUB.grant({ | ||
error : fun, | ||
ttl : 60, // Seconds | ||
ttl : 24 * 60, // Minutes | ||
read : true, | ||
@@ -816,3 +1102,3 @@ write : true, | ||
, jsonp = jsonp_cb() | ||
, ttl = args['ttl'] || -1 | ||
, ttl = args['ttl'] | ||
, r = (args['read'] )?"1":"0" | ||
@@ -829,4 +1115,2 @@ , w = (args['write'])?"1":"0" | ||
if (jsonp != '0') { data['callback'] = jsonp; } | ||
var timestamp = Math.floor(new Date().getTime() / 1000) | ||
@@ -843,7 +1127,10 @@ , sign_input = SUBSCRIBE_KEY + "\n" + PUBLISH_KEY + "\n" | ||
+ "r=" + r + "&" | ||
+ "timestamp=" + encode(timestamp) | ||
+ ((ttl > -1)?"&" + "ttl=" + ttl:"") | ||
+ "&" + "w=" + w | ||
, signature = hmac_SHA256( sign_input, SECRET_KEY ); | ||
+ "timestamp=" + encode(timestamp); | ||
if (ttl || ttl === 0) sign_input += "&" + "ttl=" + ttl; | ||
sign_input += "&" + "w=" + w; | ||
var signature = hmac_SHA256( sign_input, SECRET_KEY ); | ||
signature = signature.replace( /\+/g, "-" ); | ||
@@ -856,8 +1143,9 @@ signature = signature.replace( /\//g, "_" ); | ||
'signature' : signature, | ||
'channel' : encode(channel), | ||
'channel' : channel, | ||
'timestamp' : timestamp | ||
}; | ||
if (ttl > -1) data['ttl'] = ttl; | ||
if (auth_key) data['auth'] = encode(auth_key); | ||
if (jsonp != '0') { data['callback'] = jsonp; } | ||
if (ttl || ttl === 0) data['ttl'] = ttl; | ||
if (auth_key) data['auth'] = auth_key; | ||
@@ -867,4 +1155,8 @@ xdr({ | ||
data : data, | ||
success : function(response) { callback(response) }, | ||
fail : err, | ||
success : function(response) { | ||
_invoke_callback(response, callback, err); | ||
}, | ||
fail : function(response) { | ||
_invoke_error(response, err); | ||
}, | ||
url : [ | ||
@@ -900,4 +1192,2 @@ STD_ORIGIN, 'v1', 'auth', 'grant' , | ||
if (jsonp != '0') { data['callback'] = jsonp; } | ||
var timestamp = Math.floor(new Date().getTime() / 1000) | ||
@@ -920,4 +1210,5 @@ , sign_input = SUBSCRIBE_KEY + "\n" | ||
if (channel) data['channel'] = encode(channel); | ||
if (auth_key) data['auth'] = encode(auth_key); | ||
if (jsonp != '0') { data['callback'] = jsonp; } | ||
if (channel) data['channel'] = channel; | ||
if (auth_key) data['auth'] = auth_key; | ||
@@ -927,4 +1218,8 @@ xdr({ | ||
data : data, | ||
success : function(response) { callback(response) }, | ||
fail : err, | ||
success : function(response) { | ||
_invoke_callback(response, callback, err); | ||
}, | ||
fail : function(response) { | ||
_invoke_error(response, err); | ||
}, | ||
url : [ | ||
@@ -957,3 +1252,30 @@ STD_ORIGIN, 'v1', 'auth', 'audit' , | ||
}, | ||
'presence_heartbeat' : function(args) { | ||
var callback = args['callback'] || function() {} | ||
var err = args['error'] || function() {} | ||
var jsonp = jsonp_cb(); | ||
var data = { 'uuid' : UUID, 'auth' : AUTH_KEY }; | ||
var st = JSON['stringify'](STATE); | ||
if (st.length > 2) data['state'] = JSON['stringify'](STATE); | ||
if (PRESENCE_HB > 0 && PRESENCE_HB < 320) data['heartbeat'] = PRESENCE_HB; | ||
xdr({ | ||
callback : jsonp, | ||
data : data, | ||
timeout : SECOND * 5, | ||
url : [ | ||
STD_ORIGIN, 'v2', 'presence', | ||
'sub-key', SUBSCRIBE_KEY, | ||
'channel' , encode(generate_channel_list(CHANNELS, true)['join'](',')), | ||
'heartbeat' | ||
], | ||
success : function(response) { | ||
_invoke_callback(response, callback, err); | ||
}, | ||
fail : function(response) { _invoke_error(response, err); } | ||
}); | ||
}, | ||
// Expose PUBNUB Functions | ||
@@ -1003,2 +1325,3 @@ 'xdr' : xdr, | ||
timeout( _poll_online2, KEEPALIVE ); | ||
PRESENCE_HB_TIMEOUT = timeout( start_presence_heartbeat, ( PRESENCE_HB_INTERVAL - 3 ) * SECOND ) ; | ||
@@ -1068,3 +1391,3 @@ // Detect Age of Message | ||
, SECOND = 1000 | ||
, PNSDK = 'PubNub-JS-' + 'Nodejs' + '/' + '3.5.48' | ||
, PNSDK = 'PubNub-JS-' + 'Nodejs' + '/' + '3.6.1' | ||
, crypto = require('crypto') | ||
@@ -1136,14 +1459,26 @@ , XORIGN = 1; | ||
data['pnsdk'] = PNSDK; | ||
var url = build_url(setup.url, data); | ||
var options = { | ||
hostname : setup.url[0].split("//")[1], | ||
port : ssl ? 443 : 80, | ||
path : url, | ||
method : 'GET' | ||
}; | ||
options.agent = false; | ||
var publish = setup.url[1] === 'publish'; | ||
var mode = publish ? 'POST' : 'GET'; | ||
var options = {}; | ||
var headers = {}; | ||
var payload = ''; | ||
if (publish && mode == 'POST') | ||
payload = decodeURIComponent(setup.url.pop()); | ||
var url = build_url( setup.url, data ); | ||
url = '/' + url.split('/').slice(3).join('/'); | ||
options.hostname = setup.url[0].split("//")[1]; | ||
options.port = ssl ? 443 : 80; | ||
options.path = url; | ||
options.method = mode; | ||
options.agent = false; | ||
options.body = payload; | ||
require('http').globalAgent.maxSockets = Infinity; | ||
try { | ||
request = (ssl ? https : http).request(options, function(response) { | ||
request = (ssl ? https : http)['request'](options, function(response) { | ||
response.setEncoding('utf8'); | ||
@@ -1175,7 +1510,9 @@ response.on( 'error', function(){done(1, body || { "error" : "Network Connection Error"})}); | ||
}); | ||
request.timeout = xhrtme; | ||
request.on( 'error', function() { | ||
done( 1, {"error":"Network Connection Error"} ); | ||
} ); | ||
if (mode == 'POST') request.write(payload); | ||
request.end(); | ||
request.timeout = xhrtme; | ||
@@ -1205,108 +1542,56 @@ } catch(e) { | ||
/* =-=====================================================================-= */ | ||
/* =-=====================================================================-= */ | ||
/* =-========================= PUBNUB ============================-= */ | ||
/* =-=====================================================================-= */ | ||
/* =-=====================================================================-= */ | ||
exports.init = function(setup) { | ||
var PN = {}; | ||
setup['xdr'] = xdr; | ||
setup['db'] = db; | ||
setup['error'] = error; | ||
setup['PNSDK'] = PNSDK; | ||
setup['hmac_SHA256'] = get_hmac_SHA256; | ||
PN = PN_API(setup); | ||
PN.ready(); | ||
return PN; | ||
} | ||
PUBNUB = exports.init({}); | ||
exports.secure = function(setup) { | ||
function crypto_obj() { | ||
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 get_padded_key(key) { | ||
return crypto.createHash('sha256').update(key).digest("hex").slice(0,32); | ||
} | ||
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); | ||
return { | ||
'encrypt' : function(input, key) { | ||
if (!key) return input; | ||
var plain_text = JSON['stringify'](input); | ||
var cipher = crypto.createCipheriv('aes-256-cbc', get_padded_key(key), iv); | ||
var base_64_encrypted = cipher.update(plain_text, 'utf8', 'base64') + cipher.final('base64'); | ||
return base_64_encrypted || input; | ||
}, | ||
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}); | ||
} | ||
'decrypt' : function(input, key) { | ||
if (!key) return input; | ||
var decipher = crypto.createDecipheriv('aes-256-cbc', get_padded_key(key), iv); | ||
try { | ||
var decrypted = decipher.update(input, 'base64', 'utf8') + decipher.final('utf8'); | ||
} catch (e) { | ||
return null; | ||
} | ||
return pubnub.subscribe(args); | ||
}, | ||
history : function (args) { | ||
var encrypted_messages = ""; | ||
var old_callback = args.callback; | ||
var error_callback = args.error; | ||
return decrypted; | ||
} | ||
} | ||
} | ||
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; | ||
var CREATE_PUBNUB = function(setup) { | ||
setup['xdr'] = xdr; | ||
setup['db'] = db; | ||
setup['error'] = setup['error'] || error; | ||
setup['PNSDK'] = PNSDK; | ||
setup['hmac_SHA256'] = get_hmac_SHA256; | ||
setup['crypto_obj'] = crypto_obj(); | ||
SELF = function(setup) { | ||
return CREATE_PUBNUB(setup); | ||
} | ||
var PN = PN_API(setup); | ||
for (var prop in PN) { | ||
if (PN.hasOwnProperty(prop)) { | ||
SELF[prop] = PN[prop]; | ||
} | ||
}; | ||
} | ||
SELF.init = SELF; | ||
SELF.secure = SELF; | ||
SELF.ready(); | ||
return SELF; | ||
} | ||
exports.unique = unique | ||
CREATE_PUBNUB.init = CREATE_PUBNUB; | ||
CREATE_PUBNUB.unique = unique | ||
CREATE_PUBNUB.secure = CREATE_PUBNUB; | ||
module.exports = CREATE_PUBNUB |
@@ -108,14 +108,26 @@ /* --------------------------------------------------------------------------- | ||
data['pnsdk'] = PNSDK; | ||
var url = build_url(setup.url, data); | ||
var options = { | ||
hostname : setup.url[0].split("//")[1], | ||
port : ssl ? 443 : 80, | ||
path : url, | ||
method : 'GET' | ||
}; | ||
options.agent = false; | ||
var publish = setup.url[1] === 'publish'; | ||
var mode = publish ? 'POST' : 'GET'; | ||
var options = {}; | ||
var headers = {}; | ||
var payload = ''; | ||
if (publish && mode == 'POST') | ||
payload = decodeURIComponent(setup.url.pop()); | ||
var url = build_url( setup.url, data ); | ||
url = '/' + url.split('/').slice(3).join('/'); | ||
options.hostname = setup.url[0].split("//")[1]; | ||
options.port = ssl ? 443 : 80; | ||
options.path = url; | ||
options.method = mode; | ||
options.agent = false; | ||
options.body = payload; | ||
require('http').globalAgent.maxSockets = Infinity; | ||
try { | ||
request = (ssl ? https : http).request(options, function(response) { | ||
request = (ssl ? https : http)['request'](options, function(response) { | ||
response.setEncoding('utf8'); | ||
@@ -147,7 +159,9 @@ response.on( 'error', function(){done(1, body || { "error" : "Network Connection Error"})}); | ||
}); | ||
request.timeout = xhrtme; | ||
request.on( 'error', function() { | ||
done( 1, {"error":"Network Connection Error"} ); | ||
} ); | ||
if (mode == 'POST') request.write(payload); | ||
request.end(); | ||
request.timeout = xhrtme; | ||
@@ -177,108 +191,56 @@ } catch(e) { | ||
/* =-=====================================================================-= */ | ||
/* =-=====================================================================-= */ | ||
/* =-========================= PUBNUB ============================-= */ | ||
/* =-=====================================================================-= */ | ||
/* =-=====================================================================-= */ | ||
exports.init = function(setup) { | ||
var PN = {}; | ||
setup['xdr'] = xdr; | ||
setup['db'] = db; | ||
setup['error'] = error; | ||
setup['PNSDK'] = PNSDK; | ||
setup['hmac_SHA256'] = get_hmac_SHA256; | ||
PN = PN_API(setup); | ||
PN.ready(); | ||
return PN; | ||
} | ||
PUBNUB = exports.init({}); | ||
exports.secure = function(setup) { | ||
function crypto_obj() { | ||
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 get_padded_key(key) { | ||
return crypto.createHash('sha256').update(key).digest("hex").slice(0,32); | ||
} | ||
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); | ||
return { | ||
'encrypt' : function(input, key) { | ||
if (!key) return input; | ||
var plain_text = JSON['stringify'](input); | ||
var cipher = crypto.createCipheriv('aes-256-cbc', get_padded_key(key), iv); | ||
var base_64_encrypted = cipher.update(plain_text, 'utf8', 'base64') + cipher.final('base64'); | ||
return base_64_encrypted || input; | ||
}, | ||
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}); | ||
} | ||
'decrypt' : function(input, key) { | ||
if (!key) return input; | ||
var decipher = crypto.createDecipheriv('aes-256-cbc', get_padded_key(key), iv); | ||
try { | ||
var decrypted = decipher.update(input, 'base64', 'utf8') + decipher.final('utf8'); | ||
} catch (e) { | ||
return null; | ||
} | ||
return pubnub.subscribe(args); | ||
}, | ||
history : function (args) { | ||
var encrypted_messages = ""; | ||
var old_callback = args.callback; | ||
var error_callback = args.error; | ||
return decrypted; | ||
} | ||
} | ||
} | ||
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; | ||
var CREATE_PUBNUB = function(setup) { | ||
setup['xdr'] = xdr; | ||
setup['db'] = db; | ||
setup['error'] = setup['error'] || error; | ||
setup['PNSDK'] = PNSDK; | ||
setup['hmac_SHA256'] = get_hmac_SHA256; | ||
setup['crypto_obj'] = crypto_obj(); | ||
SELF = function(setup) { | ||
return CREATE_PUBNUB(setup); | ||
} | ||
var PN = PN_API(setup); | ||
for (var prop in PN) { | ||
if (PN.hasOwnProperty(prop)) { | ||
SELF[prop] = PN[prop]; | ||
} | ||
}; | ||
} | ||
SELF.init = SELF; | ||
SELF.secure = SELF; | ||
SELF.ready(); | ||
return SELF; | ||
} | ||
exports.unique = unique | ||
CREATE_PUBNUB.init = CREATE_PUBNUB; | ||
CREATE_PUBNUB.unique = unique | ||
CREATE_PUBNUB.secure = CREATE_PUBNUB; | ||
module.exports = CREATE_PUBNUB |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
176440
27
4039
9