@@ -12,26 +12,26 @@ module.exports.header = function(buf, packet) {
12
12
module . exports . connect = function ( buf , packet ) {
13
13
var pos = 0
14
14
, len = buf . length
15
- , protocol_and_len
16
- , topic_and_len
17
- , username_and_len
18
- , client_and_len
19
- , payload_and_len
20
- , password_and_len
15
+ , protocolId // Protocol id
16
+ , clientId // Client id
17
+ , topic // Will topic
18
+ , payload // Will payload
19
+ , password // Password
20
+ , username // Username
21
21
, flags = { } ;
22
22
23
- /* Parse protocol id */
24
- protocol_and_len = parse_string ( buf , len , pos ) ;
25
- packet . protocolId = protocol_and_len [ 0 ] ;
26
- if ( packet . protocolId === null ) return null ;
27
- pos += protocol_and_len [ 1 ] + 2 ;
23
+ // Parse protocol id
24
+ protocolId = parse_string ( buf , len , pos ) ;
25
+ if ( protocolId === null ) return new Error ( 'Parse error - cannot parse protocol id' ) ;
26
+ packet . protocolId = protocolId [ 0 ] ;
27
+ pos += protocolId [ 1 ] + 2 ;
28
28
29
- /* Parse protocol version number */
29
+ // Parse protocol version number
30
30
if ( pos > len ) return null ;
31
31
packet . protocolVersion = buf [ pos ] ;
32
32
pos += 1 ;
33
33
34
- /* Parse connect flags */
34
+ // Parse connect flags
35
35
flags . username = ( buf [ pos ] & protocol . USERNAME_MASK ) ;
36
36
flags . password = ( buf [ pos ] & protocol . PASSWORD_MASK ) ;
37
37
flags . will = ( buf [ pos ] & protocol . WILL_FLAG_MASK ) ;
@@ -45,49 +45,47 @@ module.exports.connect = function(buf, packet) {
45
45
packet . clean = ( buf [ pos ] & protocol . CLEAN_SESSION_MASK ) !== 0 ;
46
46
pos += 1 ;
47
47
48
- /* Parse keepalive */
48
+ // Parse keepalive
49
49
packet . keepalive = parse_num ( buf , len , pos ) ;
50
50
if ( packet . keepalive === null ) return null ;
51
51
pos += 2 ;
52
52
53
- /* Parse client ID */
54
- client_and_len = parse_string ( buf , len , pos ) ;
55
- packet . clientId = client_and_len [ 0 ] ;
56
- if ( packet . clientId === null ) return null ;
57
- pos += client_and_len [ 1 ] + 2 ;
53
+ // Parse client ID
54
+ clientId = parse_string ( buf , len , pos ) ;
55
+ if ( clientId === null ) return new Error ( 'Parse error - cannot parse client id' ) ;
56
+ packet . clientId = clientId [ 0 ] ;
57
+ pos += clientId [ 1 ] + 2 ;
58
58
59
59
if ( flags . will ) {
60
- /* Parse will topic */
61
- topic_and_len = parse_string ( buf , len , pos ) ;
62
- packet . will . topic = topic_and_len [ 0 ] ;
63
- if ( packet . will . topic === null ) return null ;
64
- pos += topic_and_len [ 1 ] + 2 ;
65
-
66
- /* Parse will payload */
67
- payload_and_len = parse_string ( buf , len , pos ) ;
68
- packet . will . payload = payload_and_len [ 0 ] ;
69
- if ( packet . will . payload === null ) return null ;
70
- pos += payload_and_len [ 1 ] + 2 ;
60
+ // Parse will topic
61
+ topic = parse_string ( buf , len , pos ) ;
62
+ if ( topic === null ) return new Error ( 'Parse error - cannot parse will topic' ) ;
63
+ packet . will . topic = topic [ 0 ] ;
64
+ pos += topic [ 1 ] + 2 ;
65
+
66
+ // Parse will payload
67
+ payload = parse_string ( buf , len , pos ) ;
68
+ if ( payload === null ) return new Error ( 'Parse error - cannot parse will payload' ) ;
69
+ packet . will . payload = payload [ 0 ] ;
70
+ pos += payload [ 1 ] + 2 ;
71
71
}
72
72
73
- /* Parse username */
73
+ // Parse username
74
74
if ( flags . username ) {
75
- username_and_len = parse_string ( buf , len , pos ) ;
76
- packet . username = username_and_len [ 0 ] ;
77
- if ( packet . username === null ) return null ;
78
- pos += username_and_len [ 1 ] + 2 ;
75
+ username = parse_string ( buf , len , pos ) ;
76
+ if ( username === null ) return new Error ( 'Parse error - cannot parse username' ) ;
77
+ packet . username = username [ 0 ] ;
78
+ pos += username [ 1 ] + 2 ;
79
79
}
80
80
81
- /* Parse password */
81
+ // Parse password
82
82
if ( flags . password ) {
83
- password_and_len = parse_string ( buf , len , pos ) ;
84
- packet . password = password_and_len [ 0 ] ;
85
- if ( packet . password === null ) return null ;
86
- pos += password_and_len [ 1 ] + 2 ;
83
+ password = parse_string ( buf , len , pos ) ;
84
+ if ( password === null ) return ;
85
+ packet . password = password [ 0 ] ;
86
+ pos += password [ 1 ] + 2 ;
87
87
}
88
88
89
- pos = len = protocol_and_len = topic_and_len = username_and_len = client_and_len = payload_and_len = password_and_len = flags = buf = null ;
90
-
91
89
return packet ;
92
90
} ;
93
91
@@ -96,85 +94,78 @@ module.exports.connack = function(buf, packet) {
96
94
, pos = 0 ;
97
95
98
96
packet . returnCode = parse_num ( buf , len , pos ) ;
99
- if ( packet . returnCode === null ) return null ;
100
-
101
- buf = len = pos = null ;
97
+ if ( packet . returnCode === null ) return new Error ( 'Parse error - cannot parse return code' ) ;
102
98
103
99
return packet ;
104
100
} ;
105
101
106
102
module . exports . publish = function ( buf , packet ) {
107
103
var len = buf . length
108
104
, pos = 0
109
- , topic_and_len ;
105
+ , topic ;
110
106
111
- /* Parse topic name */
112
- topic_and_len = parse_string ( buf , len , pos ) ;
113
- packet . topic = topic_and_len [ 0 ] ;
114
- if ( packet . topic === null ) return null ;
115
- pos += topic_and_len [ 1 ] + 2 ;
107
+ // Parse topic name
108
+ topic = parse_string ( buf , len , pos ) ;
109
+ if ( topic === null ) return null ;
110
+ packet . topic = topic [ 0 ] ;
111
+ pos += topic [ 1 ] + 2 ;
116
112
117
- /* Parse message ID */
113
+ // Parse message ID
118
114
if ( packet . qos > 0 ) {
119
115
packet . messageId = parse_num ( buf , len , pos ) ;
120
- if ( packet . messageId === null ) return null ;
116
+ if ( packet . messageId === null ) return new Error ( 'Parse error - cannot parse message id' ) ;
121
117
pos += 2 ;
122
118
}
123
119
124
- /* Parse the payload */
120
+ // Parse the payload
125
121
/* No checks - whatever remains in the packet is the payload */
126
122
packet . payload = buf . toString ( 'utf8' , pos , len ) ;
127
123
128
- buf = len = pos = topic_and_len = null ;
129
-
130
124
return packet ;
131
125
}
132
126
133
- var pubs = [ 'puback' , 'pubrec' , 'pubrel' , 'pubcomp' , 'unsuback' ] ;
134
-
135
- for ( var i = 0 ; i < pubs . length ; i ++ ) {
136
- module . exports [ pubs [ i ] ] = function ( buf , packet ) {
137
- var len = buf . length
138
- , pos = 0 ;
127
+ // Parse puback, pubrec, pubrel, pubcomp and suback
128
+ module . exports . puback =
129
+ module . exports . pubrec =
130
+ module . exports . pubrel =
131
+ module . exports . pubcomp =
132
+ module . exports . unsuback = function ( buf , packet ) {
133
+ var len = buf . length
134
+ , pos = 0 ;
139
135
140
- packet . messageId = parse_num ( buf , len , pos ) ;
141
- if ( packet . messageId === null ) return null ;
142
-
143
- buf = len = pos = null ;
136
+ packet . messageId = parse_num ( buf , len , pos ) ;
137
+ if ( packet . messageId === null ) return new Error ( 'Parse error - cannot parse message id' ) ;
144
138
145
- return packet ;
146
- } ;
147
- }
139
+ return packet ;
140
+ } ;
148
141
149
142
module . exports . subscribe = function ( buf , packet ) {
150
143
var len = buf . length
151
144
, pos = 0 ;
152
145
153
146
packet . subscriptions = [ ] ;
154
147
155
- /* Parse message ID */
148
+ // Parse message ID
156
149
packet . messageId = parse_num ( buf , len , pos ) ;
157
- if ( packet . messageId === null ) return null ;
150
+ if ( packet . messageId === null ) return new Error ( 'Parse error - cannot parse message id' ) ;
158
151
pos += 2 ;
159
152
160
153
while ( pos < len ) {
161
- var topic , qos , topic_and_len ;
154
+ var topic
155
+ , qos ;
162
156
163
- /* Parse topic */
164
- topic_and_len = parse_string ( buf , len , pos ) ;
165
- topic = topic_and_len [ 0 ] ;
166
- if ( topic === null ) return null ;
167
- pos += topic_and_len [ 1 ] + 2 ;
157
+ // Parse topic
158
+ topic = parse_string ( buf , len , pos ) ;
159
+ if ( topic === null ) return new Error ( 'Parse error - cannot parse topic' ) ;
160
+ pos += topic [ 1 ] + 2 ;
168
161
169
- /* Parse QoS */
162
+ // Parse QoS
163
+ // TODO: possible failure location
170
164
qos = buf [ pos ++ ] ;
171
165
172
- /* Push pair to subscriptions */
173
- packet . subscriptions . push ( { topic : topic , qos : qos } ) ;
166
+ // Push pair to subscriptions
167
+ packet . subscriptions . push ( { topic : topic [ 0 ] , qos : qos } ) ;
174
168
}
175
-
176
- pos = len = buf = null ;
177
-
178
169
return packet ;
179
170
} ;
180
171
@@ -184,17 +175,15 @@ module.exports.suback = function(buf, packet) {
184
175
185
176
packet . granted = [ ] ;
186
177
187
- /* Parse message ID */
178
+ // Parse message ID
188
179
packet . messageId = parse_num ( buf , len , pos ) ;
189
- if ( packet . messageId === null ) return null ;
180
+ if ( packet . messageId === null ) return new Error ( 'Parse error - cannot parse message id' ) ;
190
181
pos += 2 ;
191
182
183
+ // Parse granted QoSes
192
184
while ( pos < len ) {
193
185
packet . granted . push ( buf [ pos ++ ] ) ;
194
186
}
195
-
196
- pos = len = buf = null ;
197
-
198
187
return packet ;
199
188
} ;
200
189
@@ -204,26 +193,22 @@ module.exports.unsubscribe = function(buf, packet) {
204
193
205
194
packet . unsubscriptions = [ ] ;
206
195
207
- /* Parse message ID */
196
+ // Parse message ID
208
197
packet . messageId = parse_num ( buf , len , pos ) ;
209
- if ( packet . messageId === null ) return null ;
198
+ if ( packet . messageId === null ) return new Error ( 'Parse error - cannot parse message id' ) ;
210
199
pos += 2 ;
211
200
212
201
while ( pos < len ) {
213
- var topic , topic_and_len ;
202
+ var topic ;
214
203
215
- /* Parse topic */
216
- topic_and_len = parse_string ( buf , len , pos ) ;
217
- topic = topic_and_len [ 0 ] ;
218
- if ( topic === null ) return null ;
219
- pos += topic_and_len [ 1 ] + 2 ;
220
-
221
- /* Push topic to unsubscriptions */
222
- packet . unsubscriptions . push ( topic ) ;
204
+ // Parse topic
205
+ topic = parse_string ( buf , len , pos ) ;
206
+ if ( topic === null ) return new Error ( 'Parse error - cannot parse topic' ) ;
207
+ pos += topic [ 1 ] + 2 ;
208
+
209
+ // Push topic to unsubscriptions
210
+ packet . unsubscriptions . push ( topic [ 0 ] ) ;
223
211
}
224
-
225
- pos = len = buf = null ;
226
-
227
212
return packet ;
228
213
} ;
229
214
@@ -233,12 +218,11 @@ module.exports.reserved = function(buf, packet) {
233
218
234
219
var empties = [ 'pingreq' , 'pingresp' , 'disconnect' ] ;
235
220
236
- for ( var i = 0 ; i < empties . length ; i ++ ) {
237
- module . exports [ empties [ i ] ] = function ( buf , packet ) {
238
- buf = null ;
239
- return packet ;
240
- } ;
241
- }
221
+ module . exports . pingreq =
222
+ module . exports . pingresp =
223
+ module . exports . disconnect = function ( buf , packet ) {
224
+ return packet ;
225
+ } ;
242
226
243
227
var parse_num = function ( buf , len , pos ) {
244
228
if ( 2 > pos + len ) return null ;
0 commit comments