mqtt-packet
Advanced tools
Comparing version 6.6.0 to 6.7.0
@@ -42,2 +42,3 @@ /* Protocol - protocol constants */ | ||
protocol.VARBYTEINT_FIN_MASK = 0x80 | ||
protocol.VARBYTEINT_MAX = 268435455 | ||
@@ -44,0 +45,0 @@ /* Connack */ |
{ | ||
"name": "mqtt-packet", | ||
"version": "6.6.0", | ||
"version": "6.7.0", | ||
"description": "Parse and generate MQTT packets like a breeze", | ||
@@ -5,0 +5,0 @@ "main": "mqtt.js", |
@@ -75,4 +75,2 @@ const bl = require('bl') | ||
this._list.consume(result.bytes) | ||
} else { | ||
this._emitError(new Error('Invalid length')) | ||
} | ||
@@ -546,2 +544,3 @@ debug('_parseLength %d', result.value) | ||
debug('_parseVarByteNum') | ||
const maxBytes = 4 | ||
let bytes = 0 | ||
@@ -554,3 +553,3 @@ let mul = 1 | ||
while (bytes < 5) { | ||
while (bytes < maxBytes) { | ||
current = this._list.readUInt8(padding + bytes++) | ||
@@ -569,2 +568,6 @@ value += mul * (current & constants.VARBYTEINT_MASK) | ||
if (!result && bytes === maxBytes && this._list.length >= bytes) { | ||
this._emitError(new Error('Invalid variable byte integer')) | ||
} | ||
if (padding) { | ||
@@ -571,0 +574,0 @@ this._pos += bytes |
157
test.js
@@ -0,1 +1,3 @@ | ||
const util = require('util') | ||
const test = require('tape') | ||
@@ -35,3 +37,20 @@ const mqtt = require('./') | ||
test(`${name} generate`, t => { | ||
t.equal(mqtt.generate(object, opts).toString('hex'), buffer.toString('hex')) | ||
// For really large buffers, the expanded hex string can be so long as to | ||
// generate an error in nodejs 14.x, so only do the test with extra output | ||
// for relatively small buffers. | ||
const bigLength = 10000 | ||
const generatedBuffer = mqtt.generate(object, opts) | ||
if (generatedBuffer.length < bigLength && buffer.length < bigLength) { | ||
t.equal(generatedBuffer.toString('hex'), buffer.toString('hex')) | ||
} else { | ||
const bufferOkay = generatedBuffer.equals(buffer) | ||
if (bufferOkay) { | ||
t.pass() | ||
} else { | ||
// Output abbreviated representations of the buffers. | ||
t.comment('Expected:\n' + util.inspect(buffer)) | ||
t.comment('Got:\n' + util.inspect(generatedBuffer)) | ||
t.fail('Large buffers not equal') | ||
} | ||
} | ||
t.end() | ||
@@ -209,5 +228,22 @@ }) | ||
testParseError('Not supported', Buffer.from([0, 1, 0]), {}) | ||
testParseError('Invalid length', Buffer.from( | ||
// Length header field | ||
testParseError('Invalid variable byte integer', Buffer.from( | ||
[16, 255, 255, 255, 255] | ||
), {}) | ||
testParseError('Invalid variable byte integer', Buffer.from( | ||
[16, 255, 255, 255, 128] | ||
), {}) | ||
testParseError('Invalid variable byte integer', Buffer.from( | ||
[16, 255, 255, 255, 255, 1] | ||
), {}) | ||
testParseError('Invalid variable byte integer', Buffer.from( | ||
[16, 255, 255, 255, 255, 127] | ||
), {}) | ||
testParseError('Invalid variable byte integer', Buffer.from( | ||
[16, 255, 255, 255, 255, 128] | ||
), {}) | ||
testParseError('Invalid variable byte integer', Buffer.from( | ||
[16, 255, 255, 255, 255, 255, 1] | ||
), {}) | ||
@@ -1139,2 +1175,27 @@ testParseGenerate('minimal connect', { | ||
testParseGenerate('publish MQTT 5 properties with max value varbyte', { | ||
cmd: 'publish', | ||
retain: true, | ||
qos: 2, | ||
dup: true, | ||
length: 22, | ||
topic: 'test', | ||
payload: Buffer.from('test'), | ||
messageId: 10, | ||
properties: { | ||
payloadFormatIndicator: false, | ||
subscriptionIdentifier: [1, 268435455] | ||
} | ||
}, Buffer.from([ | ||
61, 22, // Header | ||
0, 4, // Topic length | ||
116, 101, 115, 116, // Topic (test) | ||
0, 10, // Message ID | ||
9, // properties length | ||
1, 0, // payloadFormatIndicator | ||
11, 1, // subscriptionIdentifier | ||
11, 255, 255, 255, 127, // subscriptionIdentifier (max value) | ||
116, 101, 115, 116 // Payload (test) | ||
]), { protocolVersion: 5 }) | ||
; (() => { | ||
@@ -1155,5 +1216,8 @@ const buffer = Buffer.alloc(2048) | ||
]), buffer])) | ||
})(); (() => { | ||
const buffer = Buffer.alloc(2 * 1024 * 1024) | ||
testParseGenerate('2MB publish packet', { | ||
})() | ||
; (() => { | ||
const maxLength = 268435455 | ||
const buffer = Buffer.alloc(maxLength - 6) | ||
testParseGenerate('Max payload publish packet', { | ||
cmd: 'publish', | ||
@@ -1163,7 +1227,7 @@ retain: false, | ||
dup: false, | ||
length: 6 + 2 * 1024 * 1024, | ||
length: maxLength, | ||
topic: 'test', | ||
payload: buffer | ||
}, Buffer.concat([Buffer.from([ | ||
48, 134, 128, 128, 1, // Header | ||
48, 255, 255, 255, 127, // Header | ||
0, 4, // Topic length | ||
@@ -1258,2 +1322,81 @@ 116, 101, 115, 116 // Topic (test) | ||
test('split publish longer', t => { | ||
t.plan(3) | ||
const length = 255 | ||
const topic = 'test' | ||
// Minus two bytes for the topic length specifier | ||
const payloadLength = length - topic.length - 2 | ||
const parser = mqtt.parser() | ||
const expected = { | ||
cmd: 'publish', | ||
retain: false, | ||
qos: 0, | ||
dup: false, | ||
length: length, | ||
topic: topic, | ||
payload: Buffer.from('a'.repeat(payloadLength)) | ||
} | ||
parser.on('packet', packet => { | ||
t.deepLooseEqual(packet, expected, 'expected packet') | ||
}) | ||
t.equal(parser.parse(Buffer.from([ | ||
48, 255, 1, // Header | ||
0, topic.length, // Topic length | ||
116, 101, 115, 116 // Topic (test) | ||
])), 6, 'remaining bytes') | ||
t.equal(parser.parse(Buffer.from(Array(payloadLength).fill(97))), | ||
0, 'remaining bytes') | ||
}) | ||
test('split length parse', t => { | ||
t.plan(4) | ||
const length = 255 | ||
const topic = 'test' | ||
const payloadLength = length - topic.length - 2 | ||
const parser = mqtt.parser() | ||
const expected = { | ||
cmd: 'publish', | ||
retain: false, | ||
qos: 0, | ||
dup: false, | ||
length: length, | ||
topic: topic, | ||
payload: Buffer.from('a'.repeat(payloadLength)) | ||
} | ||
parser.on('packet', packet => { | ||
t.deepLooseEqual(packet, expected, 'expected packet') | ||
}) | ||
t.equal(parser.parse(Buffer.from([ | ||
48, 255 // Header (partial length) | ||
])), 1, 'remaining bytes') | ||
t.equal(parser.parse(Buffer.from([ | ||
1, // Rest of header length | ||
0, topic.length, // Topic length | ||
116, 101, 115, 116 // Topic (test) | ||
])), 6, 'remaining bytes') | ||
t.equal(parser.parse(Buffer.from(Array(payloadLength).fill(97))), | ||
0, 'remaining bytes') | ||
}) | ||
testGenerateError('Invalid variable byte integer: 268435456', { | ||
cmd: 'publish', | ||
retain: false, | ||
qos: 0, | ||
dup: false, | ||
length: (268435455 + 1), | ||
topic: 'test', | ||
payload: Buffer.alloc(268435455 + 1 - 6) | ||
}, {}, 'Length var byte integer over max allowed value throws error') | ||
testGenerateError('Invalid subscriptionIdentifier: 268435456', { | ||
@@ -1260,0 +1403,0 @@ cmd: 'publish', |
@@ -6,3 +6,4 @@ import EventEmitter = NodeJS.EventEmitter | ||
export declare type PacketCmd = 'connack' | | ||
export declare type PacketCmd = 'auth' | | ||
'connack' | | ||
'connect' | | ||
@@ -28,2 +29,13 @@ 'disconnect' | | ||
export interface IAuthPacket extends IPacket { | ||
cmd: 'auth' | ||
reasonCode: number, | ||
properties?: { | ||
authenticationMethod?: string, | ||
authenticationData?: Buffer, | ||
reasonString?: string, | ||
userProperties?: Object, | ||
} | ||
} | ||
export interface IConnectPacket extends IPacket { | ||
@@ -216,3 +228,4 @@ cmd: 'connect' | ||
IDisconnectPacket | | ||
IPubrecPacket | ||
IPubrecPacket | | ||
IAuthPacket | ||
@@ -219,0 +232,0 @@ export interface Parser extends EventEmitter { |
@@ -783,2 +783,7 @@ const protocol = require('./constants') | ||
function writeVarByteInt (stream, num) { | ||
if (num > protocol.VARBYTEINT_MAX) { | ||
stream.emit('error', new Error(`Invalid variable byte integer: ${num}`)) | ||
return false | ||
} | ||
let buffer = varByteIntCache[num] | ||
@@ -791,3 +796,3 @@ | ||
debug('writeVarByteInt: writing to stream: %o', buffer) | ||
stream.write(buffer) | ||
return stream.write(buffer) | ||
} | ||
@@ -794,0 +799,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
143636
4571