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

Skip to content

Commit 81d401b

Browse files
authored
convert to ES6 classes and cleanup dependencies (#87)
1 parent dd3004d commit 81d401b

File tree

8 files changed

+577
-579
lines changed

8 files changed

+577
-579
lines changed

constants.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const Buffer = require('safe-buffer').Buffer
2-
31
/* Protocol - protocol constants */
42
const protocol = module.exports
53

generate.js

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,52 @@
1-
const Buffer = require('safe-buffer').Buffer
21
const writeToStream = require('./writeToStream')
3-
const EE = require('events').EventEmitter
4-
const inherits = require('inherits')
2+
const EventEmitter = require('events')
53

64
function generate (packet, opts) {
75
const stream = new Accumulator()
86
writeToStream(packet, stream, opts)
97
return stream.concat()
108
}
119

12-
function Accumulator () {
13-
this._array = new Array(20)
14-
this._i = 0
15-
}
16-
17-
inherits(Accumulator, EE)
10+
class Accumulator extends EventEmitter {
11+
constructor () {
12+
super()
13+
this._array = new Array(20)
14+
this._i = 0
15+
}
1816

19-
Accumulator.prototype.write = function (chunk) {
20-
this._array[this._i++] = chunk
21-
return true
22-
}
17+
write (chunk) {
18+
this._array[this._i++] = chunk
19+
return true
20+
}
2321

24-
Accumulator.prototype.concat = function () {
25-
let length = 0
26-
const lengths = new Array(this._array.length)
27-
const list = this._array
28-
let pos = 0
29-
let i
22+
concat () {
23+
let length = 0
24+
const lengths = new Array(this._array.length)
25+
const list = this._array
26+
let pos = 0
27+
let i
3028

31-
for (i = 0; i < list.length && list[i] !== undefined; i++) {
32-
if (typeof list[i] !== 'string') lengths[i] = list[i].length
33-
else lengths[i] = Buffer.byteLength(list[i])
29+
for (i = 0; i < list.length && list[i] !== undefined; i++) {
30+
if (typeof list[i] !== 'string') lengths[i] = list[i].length
31+
else lengths[i] = Buffer.byteLength(list[i])
3432

35-
length += lengths[i]
36-
}
33+
length += lengths[i]
34+
}
3735

38-
const result = Buffer.allocUnsafe(length)
36+
const result = Buffer.allocUnsafe(length)
3937

40-
for (i = 0; i < list.length && list[i] !== undefined; i++) {
41-
if (typeof list[i] !== 'string') {
42-
list[i].copy(result, pos)
43-
pos += lengths[i]
44-
} else {
45-
result.write(list[i], pos)
46-
pos += lengths[i]
38+
for (i = 0; i < list.length && list[i] !== undefined; i++) {
39+
if (typeof list[i] !== 'string') {
40+
list[i].copy(result, pos)
41+
pos += lengths[i]
42+
} else {
43+
result.write(list[i], pos)
44+
pos += lengths[i]
45+
}
4746
}
48-
}
4947

50-
return result
48+
return result
49+
}
5150
}
5251

5352
module.exports = generate

mqtt.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
exports.parser = require('./parser')
1+
exports.parser = require('./parser').parser
22
exports.generate = require('./generate')
33
exports.writeToStream = require('./writeToStream')

numbers.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const Buffer = require('safe-buffer').Buffer
21
const max = 65536
32
const cache = {}
43

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
},
3535
"homepage": "https://github.com/mqttjs/mqtt-packet",
3636
"devDependencies": {
37-
"dev-null": "^0.1.1",
3837
"pre-commit": "^1.2.2",
3938
"readable-stream": "^3.6.0",
4039
"standard": "^14.3.4",
@@ -44,8 +43,6 @@
4443
"dependencies": {
4544
"bl": "^4.0.2",
4645
"debug": "^4.1.1",
47-
"inherits": "^2.0.4",
48-
"process-nextick-args": "^2.0.1",
49-
"safe-buffer": "^5.2.1"
46+
"process-nextick-args": "^2.0.1"
5047
}
5148
}

packet.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
2-
function Packet () {
3-
this.cmd = null
4-
this.retain = false
5-
this.qos = 0
6-
this.dup = false
7-
this.length = -1
8-
this.topic = null
9-
this.payload = null
1+
class Packet {
2+
constructor () {
3+
this.cmd = null
4+
this.retain = false
5+
this.qos = 0
6+
this.dup = false
7+
this.length = -1
8+
this.topic = null
9+
this.payload = null
10+
}
1011
}
1112

1213
module.exports = Packet

0 commit comments

Comments
 (0)