@@ -54,7 +54,7 @@ function generate (packet, stream, opts) {
5454 case 'auth' :
5555 return auth ( packet , stream , opts )
5656 default :
57- stream . emit ( 'error' , new Error ( 'Unknown command' ) )
57+ stream . destroy ( new Error ( 'Unknown command' ) )
5858 return false
5959 }
6060}
@@ -101,13 +101,13 @@ function connect (packet, stream, opts) {
101101 // Must be a string and non-falsy
102102 if ( ! protocolId ||
103103 ( typeof protocolId !== 'string' && ! Buffer . isBuffer ( protocolId ) ) ) {
104- stream . emit ( 'error' , new Error ( 'Invalid protocolId' ) )
104+ stream . destroy ( new Error ( 'Invalid protocolId' ) )
105105 return false
106106 } else length += protocolId . length + 2
107107
108108 // Must be 3 or 4 or 5
109109 if ( protocolVersion !== 3 && protocolVersion !== 4 && protocolVersion !== 5 ) {
110- stream . emit ( 'error' , new Error ( 'Invalid protocol version' ) )
110+ stream . destroy ( new Error ( 'Invalid protocol version' ) )
111111 return false
112112 } else length += 1
113113
@@ -117,11 +117,11 @@ function connect (packet, stream, opts) {
117117 length += Buffer . byteLength ( clientId ) + 2
118118 } else {
119119 if ( protocolVersion < 4 ) {
120- stream . emit ( 'error' , new Error ( 'clientId must be supplied before 3.1.1' ) )
120+ stream . destroy ( new Error ( 'clientId must be supplied before 3.1.1' ) )
121121 return false
122122 }
123123 if ( ( clean * 1 ) === 0 ) {
124- stream . emit ( 'error' , new Error ( 'clientId must be given if cleanSession set to 0' ) )
124+ stream . destroy ( new Error ( 'clientId must be given if cleanSession set to 0' ) )
125125 return false
126126 }
127127 }
@@ -131,7 +131,7 @@ function connect (packet, stream, opts) {
131131 keepalive < 0 ||
132132 keepalive > 65535 ||
133133 keepalive % 1 !== 0 ) {
134- stream . emit ( 'error' , new Error ( 'Invalid keepalive' ) )
134+ stream . destroy ( new Error ( 'Invalid keepalive' ) )
135135 return false
136136 } else length += 2
137137
@@ -152,12 +152,12 @@ function connect (packet, stream, opts) {
152152 if ( will ) {
153153 // It must be an object
154154 if ( typeof will !== 'object' ) {
155- stream . emit ( 'error' , new Error ( 'Invalid will' ) )
155+ stream . destroy ( new Error ( 'Invalid will' ) )
156156 return false
157157 }
158158 // It must have topic typeof string
159159 if ( ! will . topic || typeof will . topic !== 'string' ) {
160- stream . emit ( 'error' , new Error ( 'Invalid will topic' ) )
160+ stream . destroy ( new Error ( 'Invalid will topic' ) )
161161 return false
162162 } else {
163163 length += Buffer . byteLength ( will . topic ) + 2
@@ -173,7 +173,7 @@ function connect (packet, stream, opts) {
173173 length += will . payload . length
174174 }
175175 } else {
176- stream . emit ( 'error' , new Error ( 'Invalid will payload' ) )
176+ stream . destroy ( new Error ( 'Invalid will payload' ) )
177177 return false
178178 }
179179 }
@@ -193,22 +193,22 @@ function connect (packet, stream, opts) {
193193 providedUsername = true
194194 length += Buffer . byteLength ( username ) + 2
195195 } else {
196- stream . emit ( 'error' , new Error ( 'Invalid username' ) )
196+ stream . destroy ( new Error ( 'Invalid username' ) )
197197 return false
198198 }
199199 }
200200
201201 // Password
202202 if ( password != null ) {
203203 if ( ! providedUsername ) {
204- stream . emit ( 'error' , new Error ( 'Username is required to use password' ) )
204+ stream . destroy ( new Error ( 'Username is required to use password' ) )
205205 return false
206206 }
207207
208208 if ( isStringOrBuffer ( password ) ) {
209209 length += byteLength ( password ) + 2
210210 } else {
211- stream . emit ( 'error' , new Error ( 'Invalid password' ) )
211+ stream . destroy ( new Error ( 'Invalid password' ) )
212212 return false
213213 }
214214 }
@@ -290,7 +290,7 @@ function connack (packet, stream, opts) {
290290
291291 // Check return code
292292 if ( typeof rc !== 'number' ) {
293- stream . emit ( 'error' , new Error ( 'Invalid return code' ) )
293+ stream . destroy ( new Error ( 'Invalid return code' ) )
294294 return false
295295 }
296296 // mqtt5 properties
@@ -330,7 +330,7 @@ function publish (packet, stream, opts) {
330330 if ( typeof topic === 'string' ) length += Buffer . byteLength ( topic ) + 2
331331 else if ( Buffer . isBuffer ( topic ) ) length += topic . length + 2
332332 else {
333- stream . emit ( 'error' , new Error ( 'Invalid topic' ) )
333+ stream . destroy ( new Error ( 'Invalid topic' ) )
334334 return false
335335 }
336336
@@ -340,7 +340,7 @@ function publish (packet, stream, opts) {
340340
341341 // Message ID must a number if qos > 0
342342 if ( qos && typeof id !== 'number' ) {
343- stream . emit ( 'error' , new Error ( 'Invalid messageId' ) )
343+ stream . destroy ( new Error ( 'Invalid messageId' ) )
344344 return false
345345 } else if ( qos ) length += 2
346346
@@ -391,7 +391,7 @@ function confirmation (packet, stream, opts) {
391391
392392 // Check message ID
393393 if ( typeof id !== 'number' ) {
394- stream . emit ( 'error' , new Error ( 'Invalid messageId' ) )
394+ stream . destroy ( new Error ( 'Invalid messageId' ) )
395395 return false
396396 }
397397
@@ -440,7 +440,7 @@ function subscribe (packet, stream, opts) {
440440
441441 // Check message ID
442442 if ( typeof id !== 'number' ) {
443- stream . emit ( 'error' , new Error ( 'Invalid messageId' ) )
443+ stream . destroy ( new Error ( 'Invalid messageId' ) )
444444 return false
445445 } else length += 2
446446
@@ -459,36 +459,36 @@ function subscribe (packet, stream, opts) {
459459 const iqos = subs [ i ] . qos
460460
461461 if ( typeof itopic !== 'string' ) {
462- stream . emit ( 'error' , new Error ( 'Invalid subscriptions - invalid topic' ) )
462+ stream . destroy ( new Error ( 'Invalid subscriptions - invalid topic' ) )
463463 return false
464464 }
465465 if ( typeof iqos !== 'number' ) {
466- stream . emit ( 'error' , new Error ( 'Invalid subscriptions - invalid qos' ) )
466+ stream . destroy ( new Error ( 'Invalid subscriptions - invalid qos' ) )
467467 return false
468468 }
469469
470470 if ( version === 5 ) {
471471 const nl = subs [ i ] . nl || false
472472 if ( typeof nl !== 'boolean' ) {
473- stream . emit ( 'error' , new Error ( 'Invalid subscriptions - invalid No Local' ) )
473+ stream . destroy ( new Error ( 'Invalid subscriptions - invalid No Local' ) )
474474 return false
475475 }
476476 const rap = subs [ i ] . rap || false
477477 if ( typeof rap !== 'boolean' ) {
478- stream . emit ( 'error' , new Error ( 'Invalid subscriptions - invalid Retain as Published' ) )
478+ stream . destroy ( new Error ( 'Invalid subscriptions - invalid Retain as Published' ) )
479479 return false
480480 }
481481 const rh = subs [ i ] . rh || 0
482482 if ( typeof rh !== 'number' || rh > 2 ) {
483- stream . emit ( 'error' , new Error ( 'Invalid subscriptions - invalid Retain Handling' ) )
483+ stream . destroy ( new Error ( 'Invalid subscriptions - invalid Retain Handling' ) )
484484 return false
485485 }
486486 }
487487
488488 length += Buffer . byteLength ( itopic ) + 2 + 1
489489 }
490490 } else {
491- stream . emit ( 'error' , new Error ( 'Invalid subscriptions' ) )
491+ stream . destroy ( new Error ( 'Invalid subscriptions' ) )
492492 return false
493493 }
494494
@@ -545,21 +545,21 @@ function suback (packet, stream, opts) {
545545
546546 // Check message ID
547547 if ( typeof id !== 'number' ) {
548- stream . emit ( 'error' , new Error ( 'Invalid messageId' ) )
548+ stream . destroy ( new Error ( 'Invalid messageId' ) )
549549 return false
550550 } else length += 2
551551
552552 // Check granted qos vector
553553 if ( typeof granted === 'object' && granted . length ) {
554554 for ( let i = 0 ; i < granted . length ; i += 1 ) {
555555 if ( typeof granted [ i ] !== 'number' ) {
556- stream . emit ( 'error' , new Error ( 'Invalid qos vector' ) )
556+ stream . destroy ( new Error ( 'Invalid qos vector' ) )
557557 return false
558558 }
559559 length += 1
560560 }
561561 } else {
562- stream . emit ( 'error' , new Error ( 'Invalid qos vector' ) )
562+ stream . destroy ( new Error ( 'Invalid qos vector' ) )
563563 return false
564564 }
565565
@@ -600,7 +600,7 @@ function unsubscribe (packet, stream, opts) {
600600
601601 // Check message ID
602602 if ( typeof id !== 'number' ) {
603- stream . emit ( 'error' , new Error ( 'Invalid messageId' ) )
603+ stream . destroy ( new Error ( 'Invalid messageId' ) )
604604 return false
605605 } else {
606606 length += 2
@@ -609,13 +609,13 @@ function unsubscribe (packet, stream, opts) {
609609 if ( typeof unsubs === 'object' && unsubs . length ) {
610610 for ( let i = 0 ; i < unsubs . length ; i += 1 ) {
611611 if ( typeof unsubs [ i ] !== 'string' ) {
612- stream . emit ( 'error' , new Error ( 'Invalid unsubscriptions' ) )
612+ stream . destroy ( new Error ( 'Invalid unsubscriptions' ) )
613613 return false
614614 }
615615 length += Buffer . byteLength ( unsubs [ i ] ) + 2
616616 }
617617 } else {
618- stream . emit ( 'error' , new Error ( 'Invalid unsubscriptions' ) )
618+ stream . destroy ( new Error ( 'Invalid unsubscriptions' ) )
619619 return false
620620 }
621621 // properies mqtt 5
@@ -663,7 +663,7 @@ function unsuback (packet, stream, opts) {
663663
664664 // Check message ID
665665 if ( typeof id !== 'number' ) {
666- stream . emit ( 'error' , new Error ( 'Invalid messageId' ) )
666+ stream . destroy ( new Error ( 'Invalid messageId' ) )
667667 return false
668668 }
669669
@@ -672,13 +672,13 @@ function unsuback (packet, stream, opts) {
672672 if ( typeof granted === 'object' && granted . length ) {
673673 for ( let i = 0 ; i < granted . length ; i += 1 ) {
674674 if ( typeof granted [ i ] !== 'number' ) {
675- stream . emit ( 'error' , new Error ( 'Invalid qos vector' ) )
675+ stream . destroy ( new Error ( 'Invalid qos vector' ) )
676676 return false
677677 }
678678 length += 1
679679 }
680680 } else {
681- stream . emit ( 'error' , new Error ( 'Invalid qos vector' ) )
681+ stream . destroy ( new Error ( 'Invalid qos vector' ) )
682682 return false
683683 }
684684 }
@@ -757,7 +757,7 @@ function auth (packet, stream, opts) {
757757 const properties = settings . properties
758758 let length = version === 5 ? 1 : 0
759759
760- if ( version !== 5 ) stream . emit ( 'error' , new Error ( 'Invalid mqtt version for auth packet' ) )
760+ if ( version !== 5 ) stream . destroy ( new Error ( 'Invalid mqtt version for auth packet' ) )
761761
762762 // properies mqtt 5
763763 const propertiesData = getPropertiesByMaximumPacketSize ( stream , properties , opts , length )
@@ -794,7 +794,7 @@ function auth (packet, stream, opts) {
794794const varByteIntCache = { }
795795function writeVarByteInt ( stream , num ) {
796796 if ( num > protocol . VARBYTEINT_MAX ) {
797- stream . emit ( 'error' , new Error ( `Invalid variable byte integer: ${ num } ` ) )
797+ stream . destroy ( new Error ( `Invalid variable byte integer: ${ num } ` ) )
798798 return false
799799 }
800800
@@ -901,39 +901,39 @@ function getProperties (stream, properties) {
901901 switch ( type ) {
902902 case 'byte' : {
903903 if ( typeof value !== 'boolean' ) {
904- stream . emit ( 'error' , new Error ( `Invalid ${ name } : ${ value } ` ) )
904+ stream . destroy ( new Error ( `Invalid ${ name } : ${ value } ` ) )
905905 return false
906906 }
907907 length += 1 + 1
908908 break
909909 }
910910 case 'int8' : {
911911 if ( typeof value !== 'number' || value < 0 || value > 0xff ) {
912- stream . emit ( 'error' , new Error ( `Invalid ${ name } : ${ value } ` ) )
912+ stream . destroy ( new Error ( `Invalid ${ name } : ${ value } ` ) )
913913 return false
914914 }
915915 length += 1 + 1
916916 break
917917 }
918918 case 'binary' : {
919919 if ( value && value === null ) {
920- stream . emit ( 'error' , new Error ( `Invalid ${ name } : ${ value } ` ) )
920+ stream . destroy ( new Error ( `Invalid ${ name } : ${ value } ` ) )
921921 return false
922922 }
923923 length += 1 + Buffer . byteLength ( value ) + 2
924924 break
925925 }
926926 case 'int16' : {
927927 if ( typeof value !== 'number' || value < 0 || value > 0xffff ) {
928- stream . emit ( 'error' , new Error ( `Invalid ${ name } : ${ value } ` ) )
928+ stream . destroy ( new Error ( `Invalid ${ name } : ${ value } ` ) )
929929 return false
930930 }
931931 length += 1 + 2
932932 break
933933 }
934934 case 'int32' : {
935935 if ( typeof value !== 'number' || value < 0 || value > 0xffffffff ) {
936- stream . emit ( 'error' , new Error ( `Invalid ${ name } : ${ value } ` ) )
936+ stream . destroy ( new Error ( `Invalid ${ name } : ${ value } ` ) )
937937 return false
938938 }
939939 length += 1 + 4
@@ -942,23 +942,23 @@ function getProperties (stream, properties) {
942942 case 'var' : {
943943 // var byte integer is max 24 bits packed in 32 bits
944944 if ( typeof value !== 'number' || value < 0 || value > 0x0fffffff ) {
945- stream . emit ( 'error' , new Error ( `Invalid ${ name } : ${ value } ` ) )
945+ stream . destroy ( new Error ( `Invalid ${ name } : ${ value } ` ) )
946946 return false
947947 }
948948 length += 1 + Buffer . byteLength ( genBufVariableByteInt ( value ) )
949949 break
950950 }
951951 case 'string' : {
952952 if ( typeof value !== 'string' ) {
953- stream . emit ( 'error' , new Error ( `Invalid ${ name } : ${ value } ` ) )
953+ stream . destroy ( new Error ( `Invalid ${ name } : ${ value } ` ) )
954954 return false
955955 }
956956 length += 1 + 2 + Buffer . byteLength ( value . toString ( ) )
957957 break
958958 }
959959 case 'pair' : {
960960 if ( typeof value !== 'object' ) {
961- stream . emit ( 'error' , new Error ( `Invalid ${ name } : ${ value } ` ) )
961+ stream . destroy ( new Error ( `Invalid ${ name } : ${ value } ` ) )
962962 return false
963963 }
964964 length += Object . getOwnPropertyNames ( value ) . reduce ( ( result , name ) => {
@@ -976,7 +976,7 @@ function getProperties (stream, properties) {
976976 break
977977 }
978978 default : {
979- stream . emit ( 'error' , new Error ( `Invalid property ${ name } : ${ value } ` ) )
979+ stream . destroy ( new Error ( `Invalid property ${ name } : ${ value } ` ) )
980980 return false
981981 }
982982 }
@@ -1085,7 +1085,7 @@ function writeProperty (stream, propName, value) {
10851085 break
10861086 }
10871087 default : {
1088- stream . emit ( 'error' , new Error ( `Invalid property ${ propName } value: ${ value } ` ) )
1088+ stream . destroy ( new Error ( `Invalid property ${ propName } value: ${ value } ` ) )
10891089 return false
10901090 }
10911091 }
0 commit comments