1
1
2
2
var Emitter = require ( 'component-emitter' ) ;
3
3
4
+ exports . protocol = 5 ;
5
+
4
6
/**
5
7
* Packet types (see https://github.com/socketio/socket.io-protocol)
6
8
*/
7
9
8
- exports . CONNECT = 0 ;
9
- exports . DISCONNECT = 1 ;
10
- exports . EVENT = 2 ;
11
- exports . ACK = 3 ;
12
- exports . ERROR = 4 ;
13
- exports . BINARY_EVENT = 5 ;
14
- exports . BINARY_ACK = 6 ;
10
+ var PacketType = ( exports . PacketType = {
11
+ CONNECT : 0 ,
12
+ DISCONNECT : 1 ,
13
+ EVENT : 2 ,
14
+ ACK : 3 ,
15
+ CONNECT_ERROR : 4
16
+ } ) ;
15
17
16
18
var isInteger = Number . isInteger || function ( value ) {
17
19
return typeof value === 'number' &&
@@ -21,10 +23,14 @@ var isInteger = Number.isInteger || function (value) {
21
23
22
24
var isString = function ( value ) { return typeof value === 'string' ; } ;
23
25
26
+ var isObject = function ( value ) {
27
+ return Object . prototype . toString . call ( value ) === '[object Object]' ;
28
+ } ;
29
+
24
30
function Encoder ( ) { }
25
31
26
- Encoder . prototype . encode = function ( packet , callback ) {
27
- return callback ( [ JSON . stringify ( packet ) ] ) ;
32
+ Encoder . prototype . encode = function ( packet ) {
33
+ return [ JSON . stringify ( packet ) ] ;
28
34
} ;
29
35
30
36
function Decoder ( ) { }
@@ -33,11 +39,12 @@ Emitter(Decoder.prototype);
33
39
34
40
function isDataValid ( decoded ) {
35
41
switch ( decoded . type ) {
36
- case exports . CONNECT :
37
- case exports . DISCONNECT :
42
+ case PacketType . CONNECT :
43
+ return decoded . data === undefined || isObject ( decoded . data ) ;
44
+ case PacketType . DISCONNECT :
38
45
return decoded . data === undefined ;
39
- case exports . ERROR :
40
- return isString ( decoded . data ) ;
46
+ case PacketType . CONNECT_ERROR :
47
+ return isObject ( decoded . data ) ;
41
48
default :
42
49
return Array . isArray ( decoded . data ) ;
43
50
}
@@ -46,7 +53,7 @@ function isDataValid (decoded) {
46
53
Decoder . prototype . add = function ( obj ) {
47
54
var decoded = JSON . parse ( obj ) ;
48
55
49
- var isTypeValid = isInteger ( decoded . type ) && decoded . type >= exports . CONNECT && decoded . type <= exports . BINARY_ACK ;
56
+ var isTypeValid = isInteger ( decoded . type ) && decoded . type >= PacketType . CONNECT && decoded . type <= PacketType . CONNECT_ERROR ;
50
57
if ( ! isTypeValid ) {
51
58
throw new Error ( 'invalid packet type' ) ;
52
59
}
0 commit comments