mqtt-packet
Advanced tools
Comparing version 2.0.1 to 3.0.0
@@ -107,4 +107,4 @@ | ||
// Payload | ||
if (will.payload) { | ||
if (will.payload.length) { | ||
if (will.payload && will.payload) { | ||
if (will.payload.length >= 0) { | ||
if ('string' === typeof will.payload) { | ||
@@ -111,0 +111,0 @@ length += Buffer.byteLength(will.payload) + 2 |
{ | ||
"name": "mqtt-packet", | ||
"version": "2.0.1", | ||
"version": "3.0.0", | ||
"description": "Parse and generate MQTT packets like a breeze", | ||
@@ -5,0 +5,0 @@ "main": "mqtt.js", |
@@ -7,12 +7,9 @@ | ||
function Parser(opts) { | ||
function Parser() { | ||
if (!(this instanceof Parser)) { | ||
return new Parser(opts) | ||
return new Parser() | ||
} | ||
opts = opts || {} | ||
this._list = bl() | ||
this._newPacket() | ||
this.encoding = opts.encoding || "utf8" | ||
@@ -214,3 +211,3 @@ this._states = [ | ||
// Parse will payload | ||
payload = this._parseString() | ||
payload = this._parseBuffer() | ||
if (payload === null) | ||
@@ -231,3 +228,3 @@ return this.emit('error', new Error('cannot parse will payload')) | ||
if(flags.password) { | ||
password = this._parseString() | ||
password = this._parseBuffer() | ||
if(password === null) | ||
@@ -261,9 +258,3 @@ return this.emit('error', new Error('cannot parse username')) | ||
// Parse the payload | ||
/* No checks - whatever remains in the packet is the payload */ | ||
if (this.encoding !== 'binary') { | ||
packet.payload = this._list.toString(this.encoding, this._pos, packet.length) | ||
} else { | ||
packet.payload = this._list.slice(this._pos, packet.length) | ||
} | ||
packet.payload = this._list.slice(this._pos, packet.length) | ||
} | ||
@@ -348,3 +339,3 @@ | ||
Parser.prototype._parseString = function () { | ||
Parser.prototype._parseString = function(maybeBuffer) { | ||
var length = this._parseNum() | ||
@@ -356,7 +347,3 @@ , result | ||
if (this.encoding !== 'binary') { | ||
result = this._list.toString(this.encoding || 'utf8', this._pos, this._pos + length) | ||
} else { | ||
result = this._list.slice(this._pos, this._pos + length) | ||
} | ||
result = this._list.toString('utf8', this._pos, this._pos + length) | ||
@@ -368,2 +355,16 @@ this._pos += length | ||
Parser.prototype._parseBuffer = function() { | ||
var length = this._parseNum() | ||
, result | ||
if(length === -1 || length + this._pos > this._list.length) | ||
return null | ||
result = this._list.slice(this._pos, this._pos + length) | ||
this._pos += length | ||
return result | ||
} | ||
Parser.prototype._parseNum = function() { | ||
@@ -370,0 +371,0 @@ if(2 > this._pos + this._list.length) return -1 |
mqtt-packet [![Build Status](https://travis-ci.org/mqttjs/mqtt-packet.png)](https://travis-ci.org/mqttjs/mqtt-packet) | ||
=========== | ||
Encode and Decode MQTT packets the node way. | ||
Encode and Decode MQTT 3.1.1 packets the node way. | ||
@@ -38,3 +38,3 @@ * <a href="#install">Install</a> | ||
, topic: 'test' | ||
, payload: 'test' | ||
, payload: 'test' // can also be a Buffer | ||
} | ||
@@ -74,3 +74,3 @@ | ||
// , topic: 'test' | ||
// , payload: 'test' | ||
// , payload: <Buffer 74 65 73 74> | ||
// } | ||
@@ -102,3 +102,3 @@ }) | ||
<a name="parser"> | ||
### mqtt.parser(opts) | ||
### mqtt.parser() | ||
@@ -112,6 +112,2 @@ Returns a new `Parser` object. `Parser` inherits from `EventEmitter` and | ||
The only allowed option is `{ encoding: 'binary' }` which will block the | ||
automatic parsing of all the strings in the package. Instead, the | ||
strings will remain 'raw', i.e. a `Buffer`. | ||
<a name="parse"> | ||
@@ -140,6 +136,6 @@ #### Parser.parse(buffer) | ||
, username: 'matteo' | ||
, password: 'collina' | ||
, password: new Buffer('collina') // passwords are buffers | ||
, will: { | ||
topic: 'mydevice/status' | ||
, payload: 'dead' | ||
, payload: new Buffer('dead') // payloads are buffers | ||
} | ||
@@ -152,2 +148,5 @@ } | ||
If `password` or `will.payload` are passed as strings, they will | ||
automatically be converted into a `Buffer`. | ||
### Connack | ||
@@ -228,3 +227,3 @@ | ||
, topic: 'test' | ||
, payload: 'test' | ||
, payload: new Buffer('test') | ||
, retain: false | ||
@@ -238,2 +237,5 @@ } | ||
If `payload` is passed to `generate(packet)` as a string, it will be | ||
automatically converted into a `Buffer`. | ||
### Puback | ||
@@ -240,0 +242,0 @@ |
117
test.js
@@ -133,3 +133,3 @@ | ||
, topic: 'topic' | ||
, payload: '' | ||
, payload: new Buffer(0) | ||
} | ||
@@ -140,3 +140,3 @@ , clean: true | ||
, username: 'username' | ||
, password: 'password' | ||
, password: new Buffer('password') | ||
}, new Buffer([ | ||
@@ -173,3 +173,3 @@ 16, 47, // Header | ||
, topic: 'topic' | ||
, payload: 'payload' | ||
, payload: new Buffer('payload') | ||
} | ||
@@ -180,3 +180,3 @@ , clean: true | ||
, username: 'username' | ||
, password: 'password' | ||
, password: new Buffer('password') | ||
}, new Buffer([ | ||
@@ -201,30 +201,44 @@ 16, 54, // Header | ||
testParseGenerate('binary username/password', { | ||
cmd: 'connect' | ||
, retain: false | ||
, qos: 0 | ||
, dup: false | ||
, length: 28 | ||
, protocolId: new Buffer([77, 81, 73, 115, 100, 112]) | ||
, protocolVersion: 3 | ||
, clean: false | ||
, keepalive: 30 | ||
, clientId: new Buffer([116, 101, 115, 116]) | ||
, username: new Buffer([12, 13, 14]) | ||
, password: new Buffer([15, 16, 17]) | ||
}, new Buffer([ | ||
16, 28, // Header | ||
0, 6, // Protocol id length | ||
77, 81, 73, 115, 100, 112, // Protocol id | ||
3, // Protocol version | ||
0x80 | 0x40, // Connect flags | ||
0, 30, // Keepalive | ||
0, 4, //Client id length | ||
116, 101, 115, 116, // Client id | ||
0, 3, // username length | ||
12, 13, 14, // username | ||
0, 3, // password length | ||
15, 16, 17 //password | ||
]), { | ||
encoding: 'binary' | ||
test('connect all strings generate', function(t) { | ||
var message = { | ||
cmd: 'connect' | ||
, retain: false | ||
, qos: 0 | ||
, dup: false | ||
, length: 54 | ||
, protocolId: 'MQIsdp' | ||
, protocolVersion: 3 | ||
, will: { | ||
retain: true | ||
, qos: 2 | ||
, topic: 'topic' | ||
, payload: 'payload' | ||
} | ||
, clean: true | ||
, keepalive: 30 | ||
, clientId: 'test' | ||
, username: 'username' | ||
, password: 'password' | ||
} | ||
, expected = new Buffer([ | ||
16, 54, // Header | ||
0, 6, // Protocol id length | ||
77, 81, 73, 115, 100, 112, // Protocol id | ||
3, // Protocol version | ||
246, // Connect flags | ||
0, 30, // Keepalive | ||
0, 4, // Client id length | ||
116, 101, 115, 116, // Client id | ||
0, 5, // will topic length | ||
116, 111, 112, 105, 99, // will topic | ||
0, 7, // will payload length | ||
112, 97, 121, 108, 111, 97, 100, // will payload | ||
0, 8, // username length | ||
117, 115, 101, 114, 110, 97, 109, 101, // username | ||
0, 8, // password length | ||
112, 97, 115, 115, 119, 111, 114, 100 //password | ||
]) | ||
t.equal(mqtt.generate(message).toString('hex'), expected.toString('hex')) | ||
t.end() | ||
}) | ||
@@ -267,3 +281,3 @@ | ||
, topic: 'test' | ||
, payload: 'test' | ||
, payload: new Buffer('test') | ||
}, new Buffer([ | ||
@@ -284,3 +298,3 @@ 48, 10, // Header | ||
, length: 2054 | ||
, topic: new Buffer('test') | ||
, topic: 'test' | ||
, payload: buffer | ||
@@ -291,3 +305,3 @@ }, Buffer.concat([new Buffer([ | ||
116, 101, 115, 116, // Topic (test) | ||
]), buffer]), { encoding: 'binary' }) | ||
]), buffer])) | ||
})() | ||
@@ -303,3 +317,3 @@ | ||
, length: 6 + 2 * 1024 * 1024 | ||
, topic: new Buffer('test') | ||
, topic: 'test' | ||
, payload: buffer | ||
@@ -310,3 +324,3 @@ }, Buffer.concat([new Buffer([ | ||
116, 101, 115, 116, // Topic (test) | ||
]), buffer]), { encoding: 'binary' }) | ||
]), buffer])) | ||
})() | ||
@@ -322,3 +336,3 @@ | ||
, messageId: 10 | ||
, payload: 'test' | ||
, payload: new Buffer('test') | ||
}, new Buffer([ | ||
@@ -332,2 +346,25 @@ 61, 12, // Header | ||
test('publish all strings generate', function(t) { | ||
var message = { | ||
cmd:'publish' | ||
, retain: true | ||
, qos: 2 | ||
, length: 12 | ||
, dup: true | ||
, topic: 'test' | ||
, messageId: 10 | ||
, payload: new Buffer('test') | ||
} | ||
, expected = new Buffer([ | ||
61, 12, // Header | ||
0, 4, // Topic length | ||
116, 101, 115, 116, // Topic | ||
0, 10, // Message id | ||
116, 101, 115, 116 // Payload | ||
]) | ||
t.equal(mqtt.generate(message).toString('hex'), expected.toString('hex')) | ||
t.end() | ||
}) | ||
testParseGenerate('empty publish', { | ||
@@ -340,3 +377,3 @@ cmd: 'publish' | ||
, topic: 'test' | ||
, payload: '' | ||
, payload: new Buffer(0) | ||
}, new Buffer([ | ||
@@ -362,3 +399,3 @@ 48, 6, // Header | ||
, topic: 'test' | ||
, payload: 'test' | ||
, payload: new Buffer('test') | ||
}; | ||
@@ -365,0 +402,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
49782
1545
328