@@ -10,6 +10,21 @@ var Config = require('./config');
10
10
var Token = require ( './token' ) ;
11
11
var WebHook = require ( './webhook' ) ;
12
12
13
+ var validateChannel = function ( channel ) {
14
+ if ( typeof channel !== "string" || channel === "" || channel . match ( / [ ^ A - Z a - z 0 - 9 _ \- = @ , . ; ] / ) ) {
15
+ throw new Error ( "Invalid channel name: '" + channel + "'" ) ;
16
+ }
17
+ if ( channel . length > 200 ) {
18
+ throw new Error ( "Channel name too long: '" + channel + "'" ) ;
19
+ }
20
+ }
21
+
22
+ var validateSocketId = function ( socketId ) {
23
+ if ( typeof socketId !== "string" || socketId === "" || ! socketId . match ( / ^ \d + \. \d + $ / ) ) {
24
+ throw new Error ( "Invalid socket id: '" + socketId + "'" ) ;
25
+ }
26
+ }
27
+
13
28
/** Callback passed to all REST API methods.
14
29
*
15
30
* @callback requestCallback
@@ -79,12 +94,9 @@ Pusher.forCluster = function(cluster, options) {
79
94
* @returns {String } authentication signature
80
95
*/
81
96
Pusher . prototype . authenticate = function ( socketId , channel , data ) {
82
- if ( typeof socketId !== "string" || socketId === "" ) {
83
- throw new Error ( "Invalid socket id: '" + socketId + "'" ) ;
84
- }
85
- if ( typeof channel !== "string" || channel === "" ) {
86
- throw new Error ( "Invalid channel name: '" + channel + "'" ) ;
87
- }
97
+ validateSocketId ( socketId ) ;
98
+ validateChannel ( channel ) ;
99
+
88
100
return auth . getSocketSignature ( this . config . token , channel , socketId , data ) ;
89
101
} ;
90
102
@@ -107,6 +119,9 @@ Pusher.prototype.authenticate = function(socketId, channel, data) {
107
119
* @see RequestError
108
120
*/
109
121
Pusher . prototype . trigger = function ( channels , event , data , socketId , callback ) {
122
+ if ( socketId ) {
123
+ validateSocketId ( socketId ) ;
124
+ }
110
125
if ( ! ( channels instanceof Array ) ) {
111
126
// add single channel to array for multi trigger compatibility
112
127
channels = [ channels ] ;
@@ -118,12 +133,7 @@ Pusher.prototype.trigger = function(channels, event, data, socketId, callback) {
118
133
throw new Error ( "Can't trigger a message to more than 10 channels" ) ;
119
134
}
120
135
for ( var i = 0 ; i < channels . length ; i ++ ) {
121
- if ( channels [ i ] . length > 200 ) {
122
- throw new Error ( "Too long channel name: '" + channels [ i ] + "'" ) ;
123
- }
124
- if ( ! channels [ i ] . match ( / ^ [ a - z A - Z 0 - 9 _ \- = @ , . ; ] + $ / ) ) {
125
- throw new Error ( "Invalid channel name: '" + channels [ i ] + "'" ) ;
126
- }
136
+ validateChannel ( channels [ i ] )
127
137
}
128
138
events . trigger ( this , channels , event , data , socketId , callback ) ;
129
139
} ;
0 commit comments