-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpusher.js
219 lines (186 loc) · 6.07 KB
/
pusher.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
module.exports = (function() {
var jssha = require('cloud/jssha256.js');
var md5 = require('cloud/md5.js');
var Pusher = function(options) {
this.options = options;
// support appKey being provided instead of key for legacy support
if( this.options.appKey ) {
this.options.key = this.options.appKey;
delete this.options.appKey;
}
return this;
};
/**
* @private
* Used for testing so that modules can be overidden.
*/
Pusher.modules = {
md5: md5,
jssha: jssha,
};
Pusher.prototype = {
domain: 'api.pusherapp.com',
scheme: 'http',
port: 80
};
Pusher.prototype.auth = function(socketId, channel, channelData) {
var returnHash = {}
var channelDataStr = ''
if (channelData) {
channelData = JSON.stringify(channelData);
channelDataStr = ':' + channelData;
returnHash['channel_data'] = channelData;
}
var stringToSign = socketId + ':' + channel + channelDataStr;
returnHash['auth'] = this.options.key + ':' + jssha.HMAC_SHA256_MAC(this.options.secret, stringToSign);
return(returnHash);
};
/**
* Legacy supporting function for fetching a channel object.
*/
Pusher.prototype.channel = function( channelName ) {
return new Channel( channelName, this );
};
Pusher.prototype.trigger = function(channels, event, message, socketId, callback) {
if( ( channels instanceof Array ) === false ) {
// add single channel to array for multi trigger compatibility
channels = [ channels ];
}
var eventData = {
"name": event,
"data": ( typeof message === 'object' ? JSON.stringify( message ) : message ),
"channels": channels
};
if( socketId ) {
eventData.socket_id = socketId;
}
return this.post( { path: '/events', body: eventData }, callback );
};
/**
* Make a post request to Pusher. Authentication is handled for you.
*
* @param {{
* path: String, // the path for the request
* body: String // the body of the request
* }} options
* @param {Function} callback called when the post has completed
*/
Pusher.prototype.post = function( options, callback ) {
var path = '/apps/' + this.options.appId + options.path;
var requestBody = JSON.stringify( options.body );
var queryString = this.createSignedQueryString( { method: 'POST', path: path, body: requestBody } );
var url = this.scheme + '://' + this.domain + ( this.port === 80? '' : ':' + this.port ) + path;
url += '?' + queryString
Parse.Cloud.httpRequest({
method: 'POST',
url: url,
headers: {
'content-type': 'application/json'
},
body: requestBody,
success: function(httpResponse) {
if( typeof callback === 'function' ) {
callback(null, this, httpResponse);
}
},
error: function(httpResponse) {
console.log('Pusher Error! Response Code: ' + httpResponse.status);
console.log(httpResponse.text);
if( typeof callback === 'function' ) {
callback(httpResponse.status, this, httpResponse);
}
}
});
return this;
};
/**
* Make a get request to Pusher. Authentication is handled for you.
*
* @param {{
* path: String, // the path for the request
* }} options
*/
Pusher.prototype.get = function( options, callback ) {
var path = '/apps/' + this.options.appId + options.path;
var queryString = this.createSignedQueryString( { method: 'GET', path: path, params: options.params } );
var url = this.scheme + '://' + this.domain + ( this.port === 80? '' : ':' + this.port ) + path;
url += '?' + queryString
Parse.Cloud.httpRequest({
method: 'GET',
url: url,
success: function(httpResponse) {
if( typeof callback === 'function' ) {
callback(null, this, httpResponse);
}
},
error: function(httpResponse) {
console.log('Pusher Error! Response Code: ' + httpResponse.status);
console.log(httpResponse.text);
if( typeof callback === 'function' ) {
callback(httpResponse.status, this, httpResponse);
}
}
});
return this;
};
/**
* Create a query string which has been signed and can be used when making a request to Pusher.
*
* @param {{
* method: String, // GET or POST
* body: String, // body, required if making a POST
* params: Object // key value pairs for parameters
* }} options
*/
Pusher.prototype.createSignedQueryString = function( options ) {
var timestamp = parseInt(new Date().getTime() / 1000);
var params = {};
params[ 'auth_key' ] = this.options.key;
params[ 'auth_timestamp' ] = timestamp;
params[ 'auth_version' ] = '1.0';
if( options.body ) {
var hash = md5.md5(options.body);
params[ 'body_md5' ] = hash;
}
if( options.params ) {
for( var name in options.params ) {
if( params[ name ] !== undefined ) {
throw Error( name + ' is a required parameter and cannot be overidden' );
}
params[ name ] = options.params[ name ];
}
}
var sortedKeyVal = toOrderedArray( params );
var queryString = sortedKeyVal.join( '&' );
var signData = [ options.method, options.path, queryString ].join('\n');
var signature = Pusher.modules.jssha.HMAC_SHA256_MAC(this.options.secret, signData);
queryString += '&auth_signature=' + signature;
return queryString;
};
/**
* Channel object used for legacy support. Should not be extended.
*/
function Channel( channelName, pusher ) {
this._channelName = channelName;
this._pusher = pusher;
}
/**
* Trigger an event on a channel.
*/
Channel.prototype.trigger = function( event, message, callback ) {
var socketId = null;
this._pusher.trigger( this._channelName, event, message, socketId, callback );
};
/* Utility */
function toOrderedArray( map ) {
var items = [];
for( var name in map ) {
items.push( name + '=' + map[ name ] );
}
items.slice(0).sort(function(a, b) {
return a - b;
});
return items;
}
return Pusher;
})();