diff --git a/.travis.yml b/.travis.yml index 8f2539a..c345b68 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,7 @@ language: node_js sudo: false node_js: + - 7 - 6 - 4 script: diff --git a/LICENSE.md b/LICENSE.md index ab9ff99..833b757 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -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 * diff --git a/README.md b/README.md index d1e0924..18e2151 100644 --- a/README.md +++ b/README.md @@ -10,9 +10,9 @@ Encode and Decode MQTT 3.1.1 packets the node way. * Packets * API * Contributing - * Licence & copyright + * License & copyright -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 @@ -44,10 +44,10 @@ console.log(mqtt.generate(object)) // // // -// 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) @@ -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) @@ -93,6 +93,7 @@ API * mqtt#parser() + ### mqtt.generate(object) Generates a `Buffer` containing an MQTT packet. @@ -100,15 +101,17 @@ The object must be one of the ones specified by the [packets](#packets) section. Throws an `Error` if a packet cannot be generated. + ### 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. + ### mqtt.parser() Returns a new `Parser` object. `Parser` inherits from `EventEmitter` and @@ -119,14 +122,15 @@ will emit: * `error`, if an error happens + #### 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 ------- @@ -317,6 +321,7 @@ missing. ``` + Contributing ------------ @@ -333,7 +338,7 @@ mqtt-packet is only possible due to the excellent work of the following contribu - +
Matteo CollinaGitHub/mcollinaTwitter/@matteocollina
Adam RuddGitHub/adamvrTwitter/@adam_vr
Peter SorowkaGitHub/psorowkaTwitter/@psorowka
Peter SorowkaGitHub/psorowkaTwitter/@psorowka
License diff --git a/benchmarks/generateNet.js b/benchmarks/generateNet.js index 89bbf6e..b635a5b 100644 --- a/benchmarks/generateNet.js +++ b/benchmarks/generateNet.js @@ -47,6 +47,5 @@ function tickWait () { if (i >= max) { dest.end() - return } } diff --git a/benchmarks/parse.js b/benchmarks/parse.js index c8afb1e..b8c7623 100644 --- a/benchmarks/parse.js +++ b/benchmarks/parse.js @@ -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) diff --git a/benchmarks/writeToStream.js b/benchmarks/writeToStream.js index e8b4a21..6213744 100644 --- a/benchmarks/writeToStream.js +++ b/benchmarks/writeToStream.js @@ -45,6 +45,5 @@ function tickWait () { if (i >= max) { dest.end() - return } } diff --git a/package.json b/package.json index 3b35fcf..579d1cf 100644 --- a/package.json +++ b/package.json @@ -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 (https://github.com/mcollina)", - "Adam Rudd " + "Adam Rudd ", + "Peter Sorowka (https://github.com/psorowka)", + "Wouter Klijn (https://github.com/wuhkuh)" ], "scripts": { "test": "tape test.js | tap-spec && standard", @@ -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" } diff --git a/parser.js b/parser.js index 3ecd246..366ba27 100644 --- a/parser.js +++ b/parser.js @@ -137,8 +137,8 @@ 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 @@ -146,12 +146,12 @@ Parser.prototype._parseConnect = function () { 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 @@ -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 @@ -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) @@ -288,7 +288,7 @@ Parser.prototype._parseUnsubscribe = function () { packet.unsubscriptions = [] - // Parse message ID + // Parse messageId if (!this._parseMessageId()) { return } while (this._pos < packet.length) { @@ -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 () { @@ -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 } diff --git a/test.js b/test.js index dfdfeba..c9a3424 100644 --- a/test.js +++ b/test.js @@ -127,13 +127,13 @@ testParseGenerate('minimal connect', { clientId: 'test' }, new Buffer([ 16, 18, // Header - 0, 6, // Protocol id length - 77, 81, 73, 115, 100, 112, // Protocol id + 0, 6, // Protocol ID length + 77, 81, 73, 115, 100, 112, // Protocol ID 3, // Protocol version 0, // Connect flags 0, 30, // Keepalive - 0, 4, // Client id length - 116, 101, 115, 116 // Client id + 0, 4, // Client ID length + 116, 101, 115, 116 // Client ID ])) testParseGenerate('no clientId with 3.1.1', { @@ -149,12 +149,12 @@ testParseGenerate('no clientId with 3.1.1', { clientId: '' }, new Buffer([ 16, 12, // Header - 0, 4, // Protocol id length - 77, 81, 84, 84, // Protocol id + 0, 4, // Protocol ID length + 77, 81, 84, 84, // Protocol ID 4, // Protocol version 2, // Connect flags 0, 30, // Keepalive - 0, 0 // Client id length + 0, 0 // Client ID length ])) testParseGenerateDefaults('default connect', { @@ -187,13 +187,13 @@ testParseGenerate('empty will payload', { password: new Buffer('password') }, new Buffer([ 16, 47, // Header - 0, 6, // Protocol id length - 77, 81, 73, 115, 100, 112, // Protocol id + 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, 4, // Client ID length + 116, 101, 115, 116, // Client ID 0, 5, // Will topic length 116, 111, 112, 105, 99, // Will topic 0, 0, // Will payload length @@ -225,13 +225,13 @@ testParseGenerate('maximal connect', { password: new Buffer('password') }, new Buffer([ 16, 54, // Header - 0, 6, // Protocol id length - 77, 81, 73, 115, 100, 112, // Protocol id + 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, 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 @@ -263,13 +263,13 @@ testParseGenerate('max connect with special chars', { password: new Buffer('p4$$w0£d') }, new Buffer([ 16, 57, // Header - 0, 6, // Protocol id length - 77, 81, 73, 115, 100, 112, // Protocol id + 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, 36, 116, // Client id + 0, 4, // Client ID length + 116, 101, 36, 116, // Client ID 0, 6, // Will topic length 116, 195, 178, 112, 105, 99, // Will topic 0, 8, // Will payload length @@ -303,13 +303,13 @@ test('connect all strings generate', function (t) { } var expected = new Buffer([ 16, 54, // Header - 0, 6, // Protocol id length - 77, 81, 73, 115, 100, 112, // Protocol id + 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, 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 @@ -324,7 +324,7 @@ test('connect all strings generate', function (t) { t.end() }) -testParseError('Cannot parse protocol id', new Buffer([ +testParseError('Cannot parse protocolId', new Buffer([ 16, 4, 0, 6, 77, 81 @@ -428,7 +428,7 @@ testParseGenerate('maximal publish', { 61, 12, // Header 0, 4, // Topic length 116, 101, 115, 116, // Topic - 0, 10, // Message id + 0, 10, // Message ID 116, 101, 115, 116 // Payload ])) @@ -447,7 +447,7 @@ test('publish all strings generate', function (t) { 61, 12, // Header 0, 4, // Topic length 116, 101, 115, 116, // Topic - 0, 10, // Message id + 0, 10, // Message ID 116, 101, 115, 116 // Payload ]) @@ -508,7 +508,7 @@ testParseGenerate('puback', { messageId: 2 }, new Buffer([ 64, 2, // Header - 0, 2 // Message id + 0, 2 // Message ID ])) testParseGenerate('pubrec', { @@ -520,7 +520,7 @@ testParseGenerate('pubrec', { messageId: 2 }, new Buffer([ 80, 2, // Header - 0, 2 // Message id + 0, 2 // Message ID ])) testParseGenerate('pubrel', { @@ -532,7 +532,7 @@ testParseGenerate('pubrel', { messageId: 2 }, new Buffer([ 98, 2, // Header - 0, 2 // Message id + 0, 2 // Message ID ])) testParseGenerate('pubcomp', { @@ -544,12 +544,12 @@ testParseGenerate('pubcomp', { messageId: 2 }, new Buffer([ 112, 2, // Header - 0, 2 // Message id + 0, 2 // Message ID ])) testParseError('Wrong subscribe header', new Buffer([ 128, 9, // Header (subscribeqos=0length=9) - 0, 6, // Message id (6) + 0, 6, // Message ID (6) 0, 4, // Topic length, 116, 101, 115, 116, // Topic (test) 0 // Qos (0) @@ -570,7 +570,7 @@ testParseGenerate('subscribe to one topic', { messageId: 6 }, new Buffer([ 130, 9, // Header (subscribeqos=1length=9) - 0, 6, // Message id (6) + 0, 6, // Message ID (6) 0, 4, // Topic length, 116, 101, 115, 116, // Topic (test) 0 // Qos (0) @@ -597,7 +597,7 @@ testParseGenerate('subscribe to three topics', { messageId: 6 }, new Buffer([ 130, 23, // Header (publishqos=1length=9) - 0, 6, // Message id (6) + 0, 6, // Message ID (6) 0, 4, // Topic length, 116, 101, 115, 116, // Topic (test) 0, // Qos (0) @@ -619,7 +619,7 @@ testParseGenerate('suback', { messageId: 6 }, new Buffer([ 144, 6, // Header - 0, 6, // Message id + 0, 6, // Message ID 0, 1, 2, 128 // Granted qos (0, 1, 2) and a rejected being 0x80 ])) @@ -636,7 +636,7 @@ testParseGenerate('unsubscribe', { messageId: 7 }, new Buffer([ 162, 14, - 0, 7, // Message id (7) + 0, 7, // Message ID (7) 0, 4, // Topic length 116, 102, 115, 116, // Topic (tfst) 0, 4, // Topic length, @@ -652,7 +652,7 @@ testParseGenerate('unsuback', { messageId: 8 }, new Buffer([ 176, 2, // Header - 0, 8 // Message id + 0, 8 // Message ID ])) testParseGenerate('pingreq', { @@ -687,7 +687,7 @@ testParseGenerate('disconnect', { testGenerateError('Unknown command', {}) -testGenerateError('Invalid protocol id', { +testGenerateError('Invalid protocolId', { cmd: 'connect', retain: false, qos: 0, @@ -917,18 +917,18 @@ testParseError('Packet too short', new Buffer([ 3 ])) -// CONNECT Packets that show other protocol ids than +// CONNECT Packets that show other protocol IDs than // the valid values MQTT and MQIsdp should cause an error // those packets are a hint that this is not a mqtt connection -testParseError('Invalid protocol id', new Buffer([ +testParseError('Invalid protocolId', new Buffer([ 16, 18, 0, 6, 65, 65, 65, 65, 65, 65, // AAAAAA 3, // Protocol version 0, // Connect flags 0, 10, // Keepalive - 0, 4, // Client id length - 116, 101, 115, 116 // Client id + 0, 4, // Client ID length + 116, 101, 115, 116 // Client ID ])) // CONNECT Packets that contain an unsupported protocol version @@ -936,22 +936,22 @@ testParseError('Invalid protocol id', new Buffer([ testParseError('Invalid protocol version', new Buffer([ 16, 18, 0, 6, - 77, 81, 73, 115, 100, 112, // Protocol id + 77, 81, 73, 115, 100, 112, // Protocol ID 1, // Protocol version 0, // Connect flags 0, 10, // Keepalive - 0, 4, // Client id length - 116, 101, 115, 116 // Client id + 0, 4, // Client ID length + 116, 101, 115, 116 // Client ID ])) // When a packet contains a string in the variable header and the // given string length of this exceeds the overall length of the packet that // was specified in the fixed header, parsing must fail. -// this case simulates this behavior with the protocol id string of the +// this case simulates this behavior with the protocol ID string of the // CONNECT packet. The fixed header suggests a remaining length of 8 bytes // which would be exceeded by the string length of 15 -// in this case, a protocol id parse error is expected -testParseError('Cannot parse protocol id', new Buffer([ +// in this case, a protocol ID parse error is expected +testParseError('Cannot parse protocolId', new Buffer([ 16, 8, // Fixed header 0, 15, // string length 15 --> 15 > 8 --> error! 77, 81, 73, 115, 100, 112, @@ -986,17 +986,17 @@ test('stops parsing after first error', function (t) { // First, a valid connect packet: 16, 12, // Header - 0, 4, // Protocol id length - 77, 81, 84, 84, // Protocol id + 0, 4, // Protocol ID length + 77, 81, 84, 84, // Protocol ID 4, // Protocol version 2, // Connect flags 0, 30, // Keepalive - 0, 0, // Client id length + 0, 0, // Client ID length // Then an invalid subscribe packet: 128, 9, // Header (subscribeqos=0length=9) - 0, 6, // Message id (6) + 0, 6, // Message ID (6) 0, 4, // Topic length, 116, 101, 115, 116, // Topic (test) 0, // Qos (0) @@ -1004,7 +1004,7 @@ test('stops parsing after first error', function (t) { // And another invalid subscribe packet: 128, 9, // Header (subscribeqos=0length=9) - 0, 6, // Message id (6) + 0, 6, // Message ID (6) 0, 4, // Topic length, 116, 101, 115, 116, // Topic (test) 0, // Qos (0) @@ -1024,12 +1024,12 @@ test('stops parsing after first error', function (t) { // Connect: 16, 12, // Header - 0, 4, // Protocol id length - 77, 81, 84, 84, // Protocol id + 0, 4, // Protocol ID length + 77, 81, 84, 84, // Protocol ID 4, // Protocol version 2, // Connect flags 0, 30, // Keepalive - 0, 0, // Client id length + 0, 0, // Client ID length // Disconnect: @@ -1037,7 +1037,7 @@ test('stops parsing after first error', function (t) { ])) }) -testWriteToStreamError('Invalid protocol id', { +testWriteToStreamError('Invalid protocolId', { cmd: 'connect', protocolId: {} }) @@ -1047,7 +1047,7 @@ testWriteToStreamError('Invalid topic', { topic: {} }) -testWriteToStreamError('Invalid message id', { +testWriteToStreamError('Invalid messageId', { cmd: 'subscribe', mid: {} }) diff --git a/writeToStream.js b/writeToStream.js index 15db0ce..8155b8f 100644 --- a/writeToStream.js +++ b/writeToStream.js @@ -63,7 +63,7 @@ function connect (opts, stream) { // Must be a string and non-falsy if (!protocolId || (typeof protocolId !== 'string' && !Buffer.isBuffer(protocolId))) { - stream.emit('error', new Error('Invalid protocol id')) + stream.emit('error', new Error('Invalid protocolId')) return false } else length += protocolId.length + 2 @@ -235,9 +235,9 @@ function publish (opts, stream) { if (!Buffer.isBuffer(payload)) length += Buffer.byteLength(payload) else length += payload.length - // Message id must a number if qos > 0 + // Message ID must a number if qos > 0 if (qos && typeof id !== 'number') { - stream.emit('error', new Error('Invalid message id')) + stream.emit('error', new Error('Invalid messageId')) return false } else if (qos) length += 2 @@ -270,7 +270,7 @@ function confirmation (opts, stream) { // Check message ID if (typeof id !== 'number') { - stream.emit('error', new Error('Invalid message id')) + stream.emit('error', new Error('Invalid messageId')) return false } @@ -292,9 +292,9 @@ function subscribe (opts, stream) { var length = 0 - // Check mid + // Check message ID if (typeof id !== 'number') { - stream.emit('error', new Error('Invalid message id')) + stream.emit('error', new Error('Invalid messageId')) return false } else length += 2 @@ -354,9 +354,9 @@ function suback (opts, stream) { var length = 0 - // Check message id + // Check message ID if (typeof id !== 'number') { - stream.emit('error', new Error('Invalid message id')) + stream.emit('error', new Error('Invalid messageId')) return false } else length += 2 @@ -394,9 +394,9 @@ function unsubscribe (opts, stream) { var length = 0 - // Check message id + // Check message ID if (typeof id !== 'number') { - stream.emit('error', new Error('Invalid message id')) + stream.emit('error', new Error('Invalid messageId')) return false } else { length += 2