Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
language: node_js
sudo: false
node_js:
- 7
- 6
- 4
script:
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
The MIT License (MIT)
=====================

Copyright (c) 2014-2016 mqtt-packet contributors
Copyright (c) 2014-2017 mqtt-packet contributors
---------------------------------------

*mqtt-packet contributors listed at <https://github.com/mqttjs/mqtt-packet#contributors>*
Expand Down
23 changes: 14 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ Encode and Decode MQTT 3.1.1 packets the node way.
* <a href="#packets">Packets</a>
* <a href="#api">API</a>
* <a href="#contributing">Contributing</a>
* <a href="#license">Licence &amp; copyright</a>
* <a href="#license">License &amp; copyright</a>

This library is tested with node v4 and v6. The last version to support
This library is tested with node v4, v6 and v7. The last version to support
older versions of node was mqtt-packet@4.1.2.

Installation
Expand Down Expand Up @@ -44,10 +44,10 @@ console.log(mqtt.generate(object))
//
// <Buffer 30 0a 00 04 74 65 73 74 74 65 73 74>
//
// Which is the same as
// Which is the same as:
//
// new Buffer([
// 48, 10, // Header
// 48, 10, // Header (publish)
// 0, 4, // Topic length
// 116, 101, 115, 116, // Topic (test)
// 116, 101, 115, 116 // Payload (test)
Expand Down Expand Up @@ -77,7 +77,7 @@ parser.on('packet', function(packet) {
})

parser.parse(new Buffer([
48, 10, // Header
48, 10, // Header (publish)
0, 4, // Topic length
116, 101, 115, 116, // Topic (test)
116, 101, 115, 116 // Payload (test)
Expand All @@ -93,22 +93,25 @@ API
* <a href="#parser"><code>mqtt#<b>parser()</b></code></a>

<a name="generate">

### mqtt.generate(object)

Generates a `Buffer` containing an MQTT packet.
The object must be one of the ones specified by the [packets](#packets)
section. Throws an `Error` if a packet cannot be generated.

<a name="writeToStream">

### mqtt.writeToStream(object, stream)

Writes the mqtt packet defined by `object` to the given stream.
The object must be one of the ones specified by the [packets](#packets)
section. Emits an `Error` on the stream if a packet cannot be generated.
On node >= 12, this function automatically calls `cork()` on your stream,
On node >= 0.12, this function automatically calls `cork()` on your stream,
and then it calls `uncork()` on the next tick.

<a name="parser">

### mqtt.parser()

Returns a new `Parser` object. `Parser` inherits from `EventEmitter` and
Expand All @@ -119,14 +122,15 @@ will emit:
* `error`, if an error happens

<a name="parse">

#### Parser.parse(buffer)

Parse a given `Buffer` and emits synchronously all the MQTT packets that
Parses a given `Buffer` and emits synchronously all the MQTT packets that
are included. Returns the number of bytes left to parse.

If an error happens, an `error` event will be emitted, but no `packet` events
will be emitted after that. Calling `parse()` again clears the error and
previous buffer as if you created a new `Parser`.
previous buffer, as if you created a new `Parser`.

Packets
-------
Expand Down Expand Up @@ -317,6 +321,7 @@ missing.
```

<a name="contributing"></a>

Contributing
------------

Expand All @@ -333,7 +338,7 @@ mqtt-packet is only possible due to the excellent work of the following contribu
<table><tbody>
<tr><th align="left">Matteo Collina</th><td><a href="https://github.com/mcollina">GitHub/mcollina</a></td><td><a href="http://twitter.com/matteocollina">Twitter/@matteocollina</a></td></tr>
<tr><th align="left">Adam Rudd</th><td><a href="https://github.com/adamvr">GitHub/adamvr</a></td><td><a href="http://twitter.com/adam_vr">Twitter/@adam_vr</a></td></tr>
<tr><th align="left">Peter Sorowka</th><td><a href="https://github.com/adamvr">GitHub/psorowka</a></td><td><a href="http://twitter.com/psorowka">Twitter/@psorowka</a></td></tr>
<tr><th align="left">Peter Sorowka</th><td><a href="https://github.com/psorowka">GitHub/psorowka</a></td><td><a href="http://twitter.com/psorowka">Twitter/@psorowka</a></td></tr>
</tbody></table>

License
Expand Down
1 change: 0 additions & 1 deletion benchmarks/generateNet.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,5 @@ function tickWait () {

if (i >= max) {
dest.end()
return
}
}
2 changes: 1 addition & 1 deletion benchmarks/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var time

for (i = 0; i < max; i++) {
parser.parse(new Buffer([
48, 10, // Header
48, 10, // Header (publish)
0, 4, // Topic length
116, 101, 115, 116, // Topic (test)
116, 101, 115, 116 // Payload (test)
Expand Down
1 change: 0 additions & 1 deletion benchmarks/writeToStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,5 @@ function tickWait () {

if (i >= max) {
dest.end()
return
}
}
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
{
"name": "mqtt-packet",
"version": "5.2.1",
"version": "5.2.2",
"description": "Parse and generate MQTT packets like a breeze",
"main": "mqtt.js",
"contributors": [
"Matteo Collina <matteo.collina@gmail.com> (https://github.com/mcollina)",
"Adam Rudd <damvrr@gmail.com>"
"Adam Rudd <damvrr@gmail.com>",
"Peter Sorowka (https://github.com/psorowka)",
"Wouter Klijn <contact@wuhkuh.com> (https://github.com/wuhkuh)"
],
"scripts": {
"test": "tape test.js | tap-spec && standard",
Expand All @@ -31,14 +33,14 @@
"homepage": "https://github.com/mqttjs/mqtt-packet",
"devDependencies": {
"dev-null": "^0.1.1",
"pre-commit": "^1.1.3",
"readable-stream": "^2.1.5",
"standard": "^8.1.0",
"pre-commit": "^1.2.2",
"readable-stream": "^2.2.6",
"standard": "^9.0.1",
"tap-spec": "^4.1.1",
"tape": "^4.6.0"
"tape": "^4.6.3"
},
"dependencies": {
"bl": "^1.1.2",
"bl": "^1.2.0",
"inherits": "^2.0.3",
"process-nextick-args": "^1.0.7"
}
Expand Down
20 changes: 10 additions & 10 deletions parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,21 @@ Parser.prototype._parsePayload = function () {
}

Parser.prototype._parseConnect = function () {
var protocolId // constants id
var clientId // Client id
var protocolId // Protocol ID
var clientId // Client ID
var topic // Will topic
var payload // Will payload
var password // Password
var username // Username
var flags = {}
var packet = this.packet

// Parse constants id
// Parse protocolId
protocolId = this._parseString()

if (protocolId === null) return this._emitError(new Error('Cannot parse protocol id'))
if (protocolId === null) return this._emitError(new Error('Cannot parse protocolId'))
if (protocolId !== 'MQTT' && protocolId !== 'MQIsdp') {
return this._emitError(new Error('Invalid protocol id'))
return this._emitError(new Error('Invalid protocolId'))
}

packet.protocolId = protocolId
Expand Down Expand Up @@ -190,7 +190,7 @@ Parser.prototype._parseConnect = function () {
packet.keepalive = this._parseNum()
if (packet.keepalive === -1) return this._emitError(new Error('Packet too short'))

// Parse client ID
// Parse clientId
clientId = this._parseString()
if (clientId === null) return this._emitError(new Error('Packet too short'))
packet.clientId = clientId
Expand Down Expand Up @@ -241,7 +241,7 @@ Parser.prototype._parsePublish = function () {

if (packet.topic === null) return this._emitError(new Error('Cannot parse topic'))

// Parse message ID
// Parse messageId
if (packet.qos > 0) if (!this._parseMessageId()) { return }

packet.payload = this._list.slice(this._pos, packet.length)
Expand Down Expand Up @@ -288,7 +288,7 @@ Parser.prototype._parseUnsubscribe = function () {

packet.unsubscriptions = []

// Parse message ID
// Parse messageId
if (!this._parseMessageId()) { return }

while (this._pos < packet.length) {
Expand All @@ -304,7 +304,7 @@ Parser.prototype._parseUnsubscribe = function () {
}

Parser.prototype._parseUnsuback = function () {
if (!this._parseMessageId()) return this._emitError(new Error('Cannot parse message id'))
if (!this._parseMessageId()) return this._emitError(new Error('Cannot parse messageId'))
}

Parser.prototype._parseMessageId = function () {
Expand All @@ -313,7 +313,7 @@ Parser.prototype._parseMessageId = function () {
packet.messageId = this._parseNum()

if (packet.messageId === null) {
this._emitError(new Error('Cannot parse message id'))
this._emitError(new Error('Cannot parse messageId'))
return false
}

Expand Down
Loading