Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dd3004d

Browse files
authored
Updated code to ES6 (#85)
* upgraded tests to es6 * updated code, benchmarks and README to es6
1 parent c471626 commit dd3004d

File tree

13 files changed

+395
-427
lines changed

13 files changed

+395
-427
lines changed

README.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Encode and Decode MQTT 3.1.1, 5.0 packets the node way.
1212
* <a href="#contributing">Contributing</a>
1313
* <a href="#license">License &amp; copyright</a>
1414

15-
This library is tested with node v4, v6 and v7. The last version to support
15+
This library is tested with node v6, v8, v10, v12 and v14. The last version to support
1616
older versions of node was [email protected].
1717

1818
Installation
@@ -28,17 +28,17 @@ Examples
2828
### Generating
2929

3030
```js
31-
var mqtt = require('mqtt-packet')
32-
var object = {
31+
const mqtt = require('mqtt-packet');
32+
const object = {
3333
cmd: 'publish',
3434
retain: false,
3535
qos: 0,
3636
dup: false,
3737
length: 10,
3838
topic: 'test',
3939
payload: 'test' // Can also be a Buffer
40-
}
41-
var opts = { protocolVersion: 4 } // default is 4. Usually, opts is a connect packet
40+
};
41+
const opts = { protocolVersion: 4 }; // default is 4. Usually, opts is a connect packet
4242

4343
console.log(mqtt.generate(object))
4444
// Prints:
@@ -47,7 +47,7 @@ console.log(mqtt.generate(object))
4747
//
4848
// Which is the same as:
4949
//
50-
// new Buffer([
50+
// Buffer.from([
5151
// 48, 10, // Header (publish)
5252
// 0, 4, // Topic length
5353
// 116, 101, 115, 116, // Topic (test)
@@ -58,12 +58,12 @@ console.log(mqtt.generate(object))
5858
### Parsing
5959

6060
```js
61-
var mqtt = require('mqtt-packet')
62-
var opts = { protocolVersion: 4 } // default is 4. Usually, opts is a connect packet
63-
var parser = mqtt.parser(opts)
61+
const mqtt = require('mqtt-packet');
62+
const opts = { protocolVersion: 4 }; // default is 4. Usually, opts is a connect packet
63+
const parser = mqtt.parser(opts);
6464

6565
// Synchronously emits all the parsed packets
66-
parser.on('packet', function(packet) {
66+
parser.on('packet', packet => {
6767
console.log(packet)
6868
// Prints:
6969
//
@@ -78,7 +78,7 @@ parser.on('packet', function(packet) {
7878
// }
7979
})
8080

81-
parser.parse(new Buffer([
81+
parser.parse(Buffer.from([
8282
48, 10, // Header (publish)
8383
0, 4, // Topic length
8484
116, 101, 115, 116, // Topic (test)
@@ -154,10 +154,10 @@ and that you can input to `generate`.
154154
clientId: 'my-device',
155155
keepalive: 0, // Seconds which can be any positive number, with 0 as the default setting
156156
username: 'matteo',
157-
password: new Buffer('collina'), // Passwords are buffers
157+
password: Buffer.from('collina'), // Passwords are buffers
158158
will: {
159159
topic: 'mydevice/status',
160-
payload: new Buffer('dead'), // Payloads are buffers
160+
payload: Buffer.from('dead'), // Payloads are buffers
161161
properties: { // MQTT 5.0
162162
willDelayInterval: 1234,
163163
payloadFormatIndicator: false,
@@ -316,7 +316,7 @@ All properties are mandatory.
316316
qos: 2,
317317
dup: false,
318318
topic: 'test',
319-
payload: new Buffer('test'),
319+
payload: Buffer.from('test'),
320320
retain: false,
321321
properties: { // optional properties MQTT 5.0
322322
payloadFormatIndicator: true,

benchmarks/generate.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict'
2-
3-
var mqtt = require('../')
4-
var max = 100000
5-
var i
6-
var buf = Buffer.from('test')
1+
const mqtt = require('../')
2+
const max = 100000
3+
let i
4+
const buf = Buffer.from('test')
75

86
// initialize it
97
mqtt.generate({
@@ -12,8 +10,7 @@ mqtt.generate({
1210
payload: buf
1311
})
1412

15-
var start = Date.now()
16-
var time
13+
const start = Date.now()
1714

1815
for (i = 0; i < max; i++) {
1916
mqtt.generate({
@@ -23,7 +20,7 @@ for (i = 0; i < max; i++) {
2320
})
2421
}
2522

26-
time = Date.now() - start
23+
const time = Date.now() - start
2724
console.log('Total time', time)
2825
console.log('Total packets', max)
2926
console.log('Packet/s', max / time * 1000)

benchmarks/generateNet.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11

2-
var mqtt = require('../')
3-
var max = 1000000
4-
var i = 0
5-
var start = Date.now()
6-
var time
7-
var buf = Buffer.allocUnsafe(10)
8-
var net = require('net')
9-
var server = net.createServer(handle)
10-
var dest
2+
const mqtt = require('../')
3+
const max = 1000000
4+
let i = 0
5+
const start = Date.now()
6+
let time
7+
const buf = Buffer.allocUnsafe(10)
8+
const net = require('net')
9+
const server = net.createServer(handle)
10+
let dest
1111

1212
buf.fill('test')
1313

1414
function handle (sock) {
1515
sock.resume()
1616
}
1717

18-
server.listen(0, function () {
18+
server.listen(0, () => {
1919
dest = net.connect(server.address())
2020

2121
dest.on('connect', tickWait)
2222
dest.on('drain', tickWait)
23-
dest.on('finish', function () {
23+
dest.on('finish', () => {
2424
time = Date.now() - start
2525
console.log('Total time', time)
2626
console.log('Total packets', max)
@@ -31,7 +31,7 @@ server.listen(0, function () {
3131

3232
function tickWait () {
3333
// console.log('tickWait', i)
34-
var res = true
34+
let res = true
3535
// var toSend = new Buffer(5 + buf.length)
3636

3737
for (; i < max && res; i++) {

benchmarks/parse.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11

2-
var mqtt = require('../')
3-
var parser = mqtt.parser()
4-
var max = 10000000
5-
var i
6-
var start = Date.now() / 1000
7-
var time
2+
const mqtt = require('../')
3+
const parser = mqtt.parser()
4+
const max = 10000000
5+
let i
6+
const start = Date.now() / 1000
87

98
for (i = 0; i < max; i++) {
109
parser.parse(Buffer.from([
@@ -15,7 +14,7 @@ for (i = 0; i < max; i++) {
1514
]))
1615
}
1716

18-
time = Date.now() / 1000 - start
17+
const time = Date.now() / 1000 - start
1918
console.log('Total packets', max)
2019
console.log('Total time', Math.round(time * 100) / 100)
2120
console.log('Packet/s', max / time)

benchmarks/writeToStream.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11

2-
var mqtt = require('../')
3-
var max = 1000000
4-
var i = 0
5-
var start = Date.now()
6-
var time
7-
var buf = Buffer.allocUnsafe(10)
8-
var net = require('net')
9-
var server = net.createServer(handle)
10-
var dest
2+
const mqtt = require('../')
3+
const max = 1000000
4+
let i = 0
5+
const start = Date.now()
6+
let time
7+
const buf = Buffer.allocUnsafe(10)
8+
const net = require('net')
9+
const server = net.createServer(handle)
10+
let dest
1111

1212
function handle (sock) {
1313
sock.resume()
1414
}
1515

1616
buf.fill('test')
1717

18-
server.listen(0, function () {
18+
server.listen(0, () => {
1919
dest = net.connect(server.address())
2020

2121
dest.on('connect', tickWait)
2222
dest.on('drain', tickWait)
23-
dest.on('finish', function () {
23+
dest.on('finish', () => {
2424
time = Date.now() - start
2525
console.log('Total time', time)
2626
console.log('Total packets', max)
@@ -30,7 +30,7 @@ server.listen(0, function () {
3030
})
3131

3232
function tickWait () {
33-
var res = true
33+
let res = true
3434
// var toSend = new Buffer(5)
3535

3636
for (; i < max && res; i++) {

constants.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict'
2-
3-
var Buffer = require('safe-buffer').Buffer
1+
const Buffer = require('safe-buffer').Buffer
42

53
/* Protocol - protocol constants */
6-
var protocol = module.exports
4+
const protocol = module.exports
75

86
/* Command code => mnemonic */
97
protocol.types = {
@@ -27,8 +25,8 @@ protocol.types = {
2725

2826
/* Mnemonic => Command code */
2927
protocol.codes = {}
30-
for (var k in protocol.types) {
31-
var v = protocol.types[k]
28+
for (const k in protocol.types) {
29+
const v = protocol.types[k]
3230
protocol.codes[v] = k
3331
}
3432

@@ -90,8 +88,8 @@ protocol.properties = {
9088
subscriptionIdentifier: 11
9189
}
9290
protocol.propertiesCodes = {}
93-
for (var prop in protocol.properties) {
94-
var id = protocol.properties[prop]
91+
for (const prop in protocol.properties) {
92+
const id = protocol.properties[prop]
9593
protocol.propertiesCodes[id] = prop
9694
}
9795
protocol.propertiesTypes = {
@@ -125,10 +123,10 @@ protocol.propertiesTypes = {
125123
}
126124

127125
function genHeader (type) {
128-
return [0, 1, 2].map(function (qos) {
129-
return [0, 1].map(function (dup) {
130-
return [0, 1].map(function (retain) {
131-
var buf = Buffer.alloc(1)
126+
return [0, 1, 2].map(qos => {
127+
return [0, 1].map(dup => {
128+
return [0, 1].map(retain => {
129+
const buf = Buffer.alloc(1)
132130
buf.writeUInt8(
133131
protocol.codes[type] << protocol.CMD_SHIFT |
134132
(dup ? protocol.DUP_MASK : 0) |
@@ -176,7 +174,7 @@ protocol.VERSION4 = Buffer.from([4])
176174
protocol.VERSION5 = Buffer.from([5])
177175

178176
/* QoS */
179-
protocol.QOS = [0, 1, 2].map(function (qos) {
177+
protocol.QOS = [0, 1, 2].map(qos => {
180178
return Buffer.from([qos])
181179
})
182180

generate.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
'use strict'
2-
3-
var Buffer = require('safe-buffer').Buffer
4-
var writeToStream = require('./writeToStream')
5-
var EE = require('events').EventEmitter
6-
var inherits = require('inherits')
1+
const Buffer = require('safe-buffer').Buffer
2+
const writeToStream = require('./writeToStream')
3+
const EE = require('events').EventEmitter
4+
const inherits = require('inherits')
75

86
function generate (packet, opts) {
9-
var stream = new Accumulator()
7+
const stream = new Accumulator()
108
writeToStream(packet, stream, opts)
119
return stream.concat()
1210
}
@@ -24,12 +22,11 @@ Accumulator.prototype.write = function (chunk) {
2422
}
2523

2624
Accumulator.prototype.concat = function () {
27-
var length = 0
28-
var lengths = new Array(this._array.length)
29-
var list = this._array
30-
var pos = 0
31-
var i
32-
var result
25+
let length = 0
26+
const lengths = new Array(this._array.length)
27+
const list = this._array
28+
let pos = 0
29+
let i
3330

3431
for (i = 0; i < list.length && list[i] !== undefined; i++) {
3532
if (typeof list[i] !== 'string') lengths[i] = list[i].length
@@ -38,7 +35,7 @@ Accumulator.prototype.concat = function () {
3835
length += lengths[i]
3936
}
4037

41-
result = Buffer.allocUnsafe(length)
38+
const result = Buffer.allocUnsafe(length)
4239

4340
for (i = 0; i < list.length && list[i] !== undefined; i++) {
4441
if (typeof list[i] !== 'string') {

mqtt.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict'
2-
31
exports.parser = require('./parser')
42
exports.generate = require('./generate')
53
exports.writeToStream = require('./writeToStream')

0 commit comments

Comments
 (0)