aedes 0.51.3 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/.github/actions/sticky-pr-comment/action.yml +55 -0
  2. package/.github/workflows/benchmark-compare-serial.yml +60 -0
  3. package/.github/workflows/ci.yml +12 -17
  4. package/.release-it.json +18 -0
  5. package/.taprc +15 -6
  6. package/README.md +6 -4
  7. package/aedes.d.ts +0 -6
  8. package/aedes.js +270 -242
  9. package/benchmarks/README.md +33 -0
  10. package/benchmarks/pingpong.js +94 -25
  11. package/benchmarks/receiver.js +77 -0
  12. package/benchmarks/report.js +150 -0
  13. package/benchmarks/runBenchmarks.js +118 -0
  14. package/benchmarks/sender.js +86 -0
  15. package/benchmarks/server.js +19 -18
  16. package/checkVersion.js +20 -0
  17. package/docs/Aedes.md +66 -8
  18. package/docs/Client.md +3 -4
  19. package/docs/Examples.md +39 -22
  20. package/docs/MIGRATION.md +50 -0
  21. package/eslint.config.js +8 -0
  22. package/example.js +51 -40
  23. package/examples/clusters/index.js +28 -23
  24. package/examples/clusters/package.json +10 -6
  25. package/lib/client.js +405 -306
  26. package/lib/handlers/connect.js +42 -38
  27. package/lib/handlers/index.js +9 -11
  28. package/lib/handlers/ping.js +2 -3
  29. package/lib/handlers/puback.js +5 -5
  30. package/lib/handlers/publish.js +29 -14
  31. package/lib/handlers/pubrec.js +9 -17
  32. package/lib/handlers/pubrel.js +34 -25
  33. package/lib/handlers/subscribe.js +47 -43
  34. package/lib/handlers/unsubscribe.js +16 -19
  35. package/lib/qos-packet.js +14 -17
  36. package/lib/utils.js +5 -12
  37. package/lib/write.js +4 -5
  38. package/package.json +134 -136
  39. package/test/auth.js +468 -804
  40. package/test/basic.js +613 -575
  41. package/test/bridge.js +44 -40
  42. package/test/client-pub-sub.js +531 -504
  43. package/test/close_socket_by_other_party.js +137 -102
  44. package/test/connect.js +487 -484
  45. package/test/drain-timeout.js +593 -0
  46. package/test/drain-toxiproxy.js +620 -0
  47. package/test/events.js +173 -145
  48. package/test/helper.js +351 -73
  49. package/test/keep-alive.js +40 -67
  50. package/test/meta.js +257 -210
  51. package/test/not-blocking.js +93 -197
  52. package/test/qos1.js +464 -554
  53. package/test/qos2.js +308 -393
  54. package/test/regr-21.js +39 -21
  55. package/test/require.cjs +22 -0
  56. package/test/retain.js +349 -398
  57. package/test/topics.js +176 -183
  58. package/test/types/aedes.test-d.ts +4 -8
  59. package/test/will.js +310 -428
  60. package/types/instance.d.ts +40 -35
  61. package/types/packet.d.ts +10 -10
  62. package/.coveralls.yml +0 -1
  63. package/benchmarks/bombing.js +0 -34
  64. package/benchmarks/bombingQoS1.js +0 -36
  65. package/benchmarks/throughputCounter.js +0 -23
  66. package/benchmarks/throughputCounterQoS1.js +0 -33
  67. package/types/.eslintrc.json +0 -47
@@ -1,22 +1,26 @@
1
- 'use strict'
1
+ import retimer from 'retimer'
2
+ import { pipeline } from 'stream'
3
+ import write from '../write.js'
4
+ import QoSPacket from '../qos-packet.js'
5
+ import { through } from '../utils.js'
6
+ import handleSubscribe from './subscribe.js'
7
+ import hyperid from 'hyperid'
2
8
 
3
- const retimer = require('retimer')
4
- const { pipeline } = require('stream')
5
- const write = require('../write')
6
- const QoSPacket = require('../qos-packet')
7
- const { through } = require('../utils')
8
- const handleSubscribe = require('./subscribe')
9
- const uniqueId = require('hyperid')()
9
+ const uniqueId = hyperid()
10
10
 
11
- function Connack (arg) {
12
- this.cmd = 'connack'
13
- this.returnCode = arg.returnCode
14
- this.sessionPresent = arg.sessionPresent
11
+ class Connack {
12
+ constructor (arg) {
13
+ this.cmd = 'connack'
14
+ this.returnCode = arg.returnCode
15
+ this.sessionPresent = arg.sessionPresent
16
+ }
15
17
  }
16
18
 
17
- function ClientPacketStatus (client, packet) {
18
- this.client = client
19
- this.packet = packet
19
+ class ClientPacketStatus {
20
+ constructor (client, packet) {
21
+ this.client = client
22
+ this.packet = packet
23
+ }
20
24
  }
21
25
 
22
26
  const connectActions = [
@@ -159,24 +163,22 @@ function setKeepAlive (arg, done) {
159
163
  function fetchSubs (arg, done) {
160
164
  const client = this.client
161
165
  if (!this.packet.clean) {
162
- client.broker.persistence.subscriptionsByClient({
166
+ const subsClient = {
163
167
  id: client.id,
164
168
  done,
165
169
  arg
166
- }, gotSubs)
170
+ }
171
+ client.broker.persistence.subscriptionsByClient({ id: client.id })
172
+ .then(subs => gotSubs(subs, subsClient), subsClient.done)
167
173
  return
168
174
  }
169
175
  arg.sessionPresent = false // [MQTT-3.2.2-1]
170
- client.broker.persistence.cleanSubscriptions(
171
- client,
172
- done)
176
+ client.broker.persistence.cleanSubscriptions(client)
177
+ .then(() => done(null), done)
173
178
  }
174
179
 
175
- function gotSubs (err, subs, client) {
176
- if (err) {
177
- return client.done(err)
178
- }
179
- client.arg.subs = subs
180
+ function gotSubs (subs, client) {
181
+ client.arg.subs = subs.length > 0 ? subs : null
180
182
  client.done()
181
183
  }
182
184
 
@@ -194,16 +196,15 @@ function storeWill (arg, done) {
194
196
  const client = this.client
195
197
  client.will = client._will
196
198
  // delete any existing will messages from persistence
197
- client.broker.persistence.delWill(client, function () {
198
- if (client.will) {
199
- client.broker.persistence.putWill(
200
- client,
201
- client.will,
202
- done)
203
- } else {
204
- done()
205
- }
206
- })
199
+ client.broker.persistence.delWill(client)
200
+ .finally(() => {
201
+ if (client.will) {
202
+ client.broker.persistence.putWill(client, client.will)
203
+ .then(() => done(null, client), done)
204
+ } else {
205
+ done()
206
+ }
207
+ })
207
208
  }
208
209
 
209
210
  function registerClient (arg, done) {
@@ -242,7 +243,9 @@ function emptyQueue (arg, done) {
242
243
  // Object filled the buffer up to the highWaterMark preventing stored messages
243
244
  // being sent
244
245
  packet.writeCallback = (error, _client) => next(error)
245
- persistence.outgoingUpdate(client, packet, emptyQueueFilter)
246
+ const filter = (err) => emptyQueueFilter(err, client, packet)
247
+ persistence.outgoingUpdate(client, packet)
248
+ .then(() => filter(null, client, packet), err => filter(err, client, packet))
246
249
  }),
247
250
  done
248
251
  )
@@ -263,10 +266,11 @@ function emptyQueueFilter (err, client, packet) {
263
266
  const persistence = client.broker.persistence
264
267
 
265
268
  if (client.clean || !authorized) {
266
- persistence.outgoingClearMessageId(client, packet, next)
269
+ persistence.outgoingClearMessageId(client, packet)
270
+ .then(packet => next(null, packet), next)
267
271
  } else {
268
272
  write(client, packet, next)
269
273
  }
270
274
  }
271
275
 
272
- module.exports = handleConnect
276
+ export default handleConnect
@@ -1,13 +1,11 @@
1
- 'use strict'
2
-
3
- const handleConnect = require('./connect')
4
- const handleSubscribe = require('./subscribe')
5
- const handleUnsubscribe = require('./unsubscribe')
6
- const handlePublish = require('./publish')
7
- const handlePuback = require('./puback')
8
- const handlePubrel = require('./pubrel')
9
- const handlePubrec = require('./pubrec')
10
- const handlePing = require('./ping')
1
+ import handleConnect from './connect.js'
2
+ import handleSubscribe from './subscribe.js'
3
+ import handleUnsubscribe from './unsubscribe.js'
4
+ import handlePublish from './publish.js'
5
+ import handlePuback from './puback.js'
6
+ import handlePubrel from './pubrel.js'
7
+ import handlePubrec from './pubrec.js'
8
+ import handlePing from './ping.js'
11
9
 
12
10
  function handle (client, packet, done) {
13
11
  if (packet.cmd === 'connect') {
@@ -74,4 +72,4 @@ function finish (conn, packet, done) {
74
72
  done(error)
75
73
  }
76
74
 
77
- module.exports = handle
75
+ export default handle
@@ -1,6 +1,5 @@
1
- 'use strict'
1
+ import write from '../write.js'
2
2
 
3
- const write = require('../write')
4
3
  const pingResp = {
5
4
  cmd: 'pingresp'
6
5
  }
@@ -10,4 +9,4 @@ function handlePing (client, packet, done) {
10
9
  write(client, pingResp, done)
11
10
  }
12
11
 
13
- module.exports = handlePing
12
+ export default handlePing
@@ -1,11 +1,11 @@
1
- 'use strict'
2
-
3
1
  function handlePuback (client, packet, done) {
4
2
  const persistence = client.broker.persistence
5
- persistence.outgoingClearMessageId(client, packet, function (err, origPacket) {
3
+ const emitAck = (err, origPacket) => {
6
4
  client.broker.emit('ack', origPacket, client)
7
5
  done(err)
8
- })
6
+ }
7
+ persistence.outgoingClearMessageId(client, packet)
8
+ .then((packet) => emitAck(null, packet), emitAck)
9
9
  }
10
10
 
11
- module.exports = handlePuback
11
+ export default handlePuback
@@ -1,15 +1,17 @@
1
- 'use strict'
1
+ import write from '../write.js'
2
2
 
3
- const write = require('../write')
4
-
5
- function PubAck (packet) {
6
- this.cmd = 'puback'
7
- this.messageId = packet.messageId
3
+ class PubAck {
4
+ constructor (packet) {
5
+ this.cmd = 'puback'
6
+ this.messageId = packet.messageId
7
+ }
8
8
  }
9
9
 
10
- function PubRec (packet) {
11
- this.cmd = 'pubrec'
12
- this.messageId = packet.messageId
10
+ class PubRec {
11
+ constructor (packet) {
12
+ this.cmd = 'pubrec'
13
+ this.messageId = packet.messageId
14
+ }
13
15
  }
14
16
 
15
17
  const publishActions = [
@@ -39,10 +41,23 @@ function enqueuePublish (packet, done) {
39
41
 
40
42
  switch (packet.qos) {
41
43
  case 2:
42
- client.broker.persistence.incomingStorePacket(client, packet, function (err) {
43
- if (err) { return done(err) }
44
- write(client, new PubRec(packet), done)
45
- })
44
+ // MQTT-4.3.3-2: Check if we already have this packet by messageId
45
+ client.broker.persistence.incomingGetPacket(client, packet)
46
+ .then(() => {
47
+ // Duplicate packet: just send PUBREC, don't publish again
48
+ write(client, new PubRec(packet), done)
49
+ }, () => {
50
+ // New packet (not found in store): store first, then publish
51
+ // This ensures if storage fails, message hasn't been delivered yet
52
+ // preventing duplicate delivery on retransmission
53
+ client.broker.persistence.incomingStorePacket(client, packet)
54
+ .then(() => {
55
+ client.broker.publish(packet, client, (err) => {
56
+ if (err) { return done(err) }
57
+ write(client, new PubRec(packet), done)
58
+ })
59
+ }, done)
60
+ })
46
61
  break
47
62
  case 1:
48
63
  write(client, new PubAck(packet), function (err) {
@@ -62,4 +77,4 @@ function authorizePublish (packet, done) {
62
77
  this.broker.authorizePublish(this, packet, done)
63
78
  }
64
79
 
65
- module.exports = handlePublish
80
+ export default handlePublish
@@ -1,10 +1,10 @@
1
- 'use strict'
1
+ import write from '../write.js'
2
2
 
3
- const write = require('../write')
4
-
5
- function PubRel (packet) {
6
- this.cmd = 'pubrel'
7
- this.messageId = packet.messageId
3
+ class PubRel {
4
+ constructor (packet) {
5
+ this.cmd = 'pubrel'
6
+ this.messageId = packet.messageId
7
+ }
8
8
  }
9
9
 
10
10
  function handlePubrec (client, packet, done) {
@@ -15,16 +15,8 @@ function handlePubrec (client, packet, done) {
15
15
  return
16
16
  }
17
17
 
18
- client.broker.persistence.outgoingUpdate(
19
- client, pubrel, reply)
20
-
21
- function reply (err) {
22
- if (err) {
23
- done(err)
24
- } else {
25
- write(client, pubrel, done)
26
- }
27
- }
18
+ client.broker.persistence.outgoingUpdate(client, pubrel)
19
+ .then(() => write(client, pubrel, done), done)
28
20
  }
29
21
 
30
- module.exports = handlePubrec
22
+ export default handlePubrec
@@ -1,22 +1,23 @@
1
- 'use strict'
1
+ import write from '../write.js'
2
2
 
3
- const write = require('../write')
4
-
5
- function ClientPacketStatus (client, packet) {
6
- this.client = client
7
- this.packet = packet
3
+ class ClientPacketStatus {
4
+ constructor (client, packet) {
5
+ this.client = client
6
+ this.packet = packet
7
+ }
8
8
  }
9
9
 
10
- function PubComp (packet) {
11
- this.cmd = 'pubcomp'
12
- this.messageId = packet.messageId
10
+ class PubComp {
11
+ constructor (packet) {
12
+ this.cmd = 'pubcomp'
13
+ this.messageId = packet.messageId
14
+ }
13
15
  }
14
16
 
15
17
  const pubrelActions = [
16
18
  pubrelGet,
17
- pubrelPublish,
18
- pubrelWrite,
19
- pubrelDel
19
+ pubrelDelete,
20
+ pubrelWrite
20
21
  ]
21
22
  function handlePubrel (client, packet, done) {
22
23
  client.broker._series(
@@ -25,26 +26,34 @@ function handlePubrel (client, packet, done) {
25
26
  }
26
27
 
27
28
  function pubrelGet (arg, done) {
29
+ // MQTT-4.3.3-2: Check if we have this packet in persistence
28
30
  const persistence = this.client.broker.persistence
29
- persistence.incomingGetPacket(this.client, this.packet, reply)
31
+ persistence.incomingGetPacket(this.client, this.packet)
32
+ .then((packet) => {
33
+ arg.packet = this.packet
34
+ arg.foundInStore = !!packet
35
+ done(null, arg)
36
+ }, () => {
37
+ // Even if incomingGetPacket fails, continue to send PUBCOMP
38
+ arg.packet = this.packet
39
+ arg.foundInStore = false
40
+ done(null, arg)
41
+ })
42
+ }
30
43
 
31
- function reply (err, packet) {
32
- arg.packet = packet
33
- done(err)
44
+ function pubrelDelete (arg, done) {
45
+ // Only delete if we found the packet in the store
46
+ if (!arg.foundInStore) {
47
+ return done(null, arg)
34
48
  }
35
- }
36
49
 
37
- function pubrelPublish (arg, done) {
38
- this.client.broker.publish(arg.packet, this.client, done)
50
+ const persistence = this.client.broker.persistence
51
+ persistence.incomingDelPacket(this.client, arg.packet).finally(() => done(null, arg))
39
52
  }
40
53
 
41
54
  function pubrelWrite (arg, done) {
55
+ // Always send PUBCOMP, even if packet was not found in store
42
56
  write(this.client, new PubComp(arg.packet), done)
43
57
  }
44
58
 
45
- function pubrelDel (arg, done) {
46
- const persistence = this.client.broker.persistence
47
- persistence.incomingDelPacket(this.client, arg.packet, done)
48
- }
49
-
50
- module.exports = handlePubrel
59
+ export default handlePubrel
@@ -1,10 +1,7 @@
1
- 'use strict'
2
-
3
- const fastfall = require('fastfall')
4
- const Packet = require('aedes-packet')
5
- const { through } = require('../utils')
6
- const { validateTopic, $SYS_PREFIX } = require('../utils')
7
- const write = require('../write')
1
+ import fastfall from 'fastfall'
2
+ import Packet from 'aedes-packet'
3
+ import { through, validateTopic, $SYS_PREFIX } from '../utils.js'
4
+ import write from '../write.js'
8
5
 
9
6
  const subscribeTopicActions = fastfall([
10
7
  authorize,
@@ -16,47 +13,55 @@ const restoreTopicActions = fastfall([
16
13
  addSubs
17
14
  ])
18
15
 
19
- function SubAck (packet, granted) {
20
- this.cmd = 'suback'
21
- this.messageId = packet.messageId
22
- // the qos granted
23
- this.granted = granted
16
+ class SubAck {
17
+ constructor (packet, granted) {
18
+ this.cmd = 'suback'
19
+ this.messageId = packet.messageId
20
+ // the qos granted
21
+ this.granted = granted
22
+ }
24
23
  }
25
24
 
26
- function Subscription (qos, func, rh, rap, nl) {
27
- this.qos = qos
28
- this.func = func
25
+ class Subscription {
26
+ constructor (qos, func, rh, rap, nl) {
27
+ this.qos = qos
28
+ this.func = func
29
29
 
30
- // retain-handling indicates how retained messages should be
31
- // handled when a new subscription is created
32
- // (see [MQTT-3.3.1-9] through [MQTT-3.3.1-11])
33
- this.rh = rh
30
+ // retain-handling indicates how retained messages should be
31
+ // handled when a new subscription is created
32
+ // (see [MQTT-3.3.1-9] through [MQTT-3.3.1-11])
33
+ this.rh = rh
34
34
 
35
- // retain-as-published indicates whether to leave the retain flag as-is (true)
36
- // or to clear it before sending to subscriptions (false) default false
37
- // (see [MQTT-3.3.1-12] through [MQTT-3.3.1-13])
38
- this.rap = rap
35
+ // retain-as-published indicates whether to leave the retain flag as-is (true)
36
+ // or to clear it before sending to subscriptions (false) default false
37
+ // (see [MQTT-3.3.1-12] through [MQTT-3.3.1-13])
38
+ this.rap = rap
39
39
 
40
- // no-local indicates that a client should not receive its own
41
- // messages (see [MQTT-3.8.3-3])
42
- this.nl = nl
40
+ // no-local indicates that a client should not receive its own
41
+ // messages (see [MQTT-3.8.3-3])
42
+ this.nl = nl
43
+ }
43
44
  }
44
45
 
45
- function SubscribeState (client, packet, restore, finish) {
46
- this.client = client
47
- this.packet = packet
48
- this.actions = restore ? restoreTopicActions : subscribeTopicActions
49
- this.finish = finish
50
- this.subState = []
46
+ class SubscribeState {
47
+ constructor (client, packet, restore, finish) {
48
+ this.client = client
49
+ this.packet = packet
50
+ this.actions = restore ? restoreTopicActions : subscribeTopicActions
51
+ this.finish = finish
52
+ this.subState = []
53
+ }
51
54
  }
52
55
 
53
- function SubState (client, packet, granted, rh, rap, nl) {
54
- this.client = client
55
- this.packet = packet
56
- this.granted = granted
57
- this.rh = rh
58
- this.rap = rap
59
- this.nl = nl
56
+ class SubState {
57
+ constructor (client, packet, granted, rh, rap, nl) {
58
+ this.client = client
59
+ this.packet = packet
60
+ this.granted = granted
61
+ this.rh = rh
62
+ this.rap = rap
63
+ this.nl = nl
64
+ }
60
65
  }
61
66
 
62
67
  // if same subscribed topic in subs array, we pick up the last one
@@ -123,9 +128,8 @@ function storeSubscriptions (sub, done) {
123
128
  return done(null, sub)
124
129
  }
125
130
 
126
- client.broker.persistence.addSubscriptions(client, packet.subscriptions, function addSub (err) {
127
- done(err, sub)
128
- })
131
+ client.broker.persistence.addSubscriptions(client, packet.subscriptions)
132
+ .then(() => done(null, sub), err => done(err, sub))
129
133
  }
130
134
 
131
135
  function addSubs (sub, done) {
@@ -252,4 +256,4 @@ function completeSubscribe (err) {
252
256
 
253
257
  function noop () { }
254
258
 
255
- module.exports = handleSubscribe
259
+ export default handleSubscribe
@@ -1,17 +1,19 @@
1
- 'use strict'
1
+ import write from '../write.js'
2
+ import { validateTopic, $SYS_PREFIX } from '../utils.js'
2
3
 
3
- const write = require('../write')
4
- const { validateTopic, $SYS_PREFIX } = require('../utils')
5
-
6
- function UnSubAck (packet) {
7
- this.cmd = 'unsuback'
8
- this.messageId = packet.messageId // [MQTT-3.10.4-4]
4
+ class UnSubAck {
5
+ constructor (packet) {
6
+ this.cmd = 'unsuback'
7
+ this.messageId = packet.messageId // [MQTT-3.10.4-4]
8
+ }
9
9
  }
10
10
 
11
- function UnsubscribeState (client, packet, finish) {
12
- this.client = client
13
- this.packet = packet
14
- this.finish = finish
11
+ class UnsubscribeState {
12
+ constructor (client, packet, finish) {
13
+ this.client = client
14
+ this.packet = packet
15
+ this.finish = finish
16
+ }
15
17
  }
16
18
 
17
19
  function handleUnsubscribe (client, packet, done) {
@@ -31,13 +33,8 @@ function handleUnsubscribe (client, packet, done) {
31
33
  return actualUnsubscribe(client, packet, done)
32
34
  }
33
35
 
34
- broker.persistence.removeSubscriptions(client, unsubscriptions, function (err) {
35
- if (err) {
36
- return done(err)
37
- }
38
-
39
- actualUnsubscribe(client, packet, done)
40
- })
36
+ broker.persistence.removeSubscriptions(client, unsubscriptions)
37
+ .then(() => actualUnsubscribe(client, packet, done), done)
41
38
  } else {
42
39
  actualUnsubscribe(client, packet, done)
43
40
  }
@@ -101,4 +98,4 @@ function completeUnsubscribe (err) {
101
98
 
102
99
  function noop () { }
103
100
 
104
- module.exports = handleUnsubscribe
101
+ export default handleUnsubscribe
package/lib/qos-packet.js CHANGED
@@ -1,25 +1,22 @@
1
- 'use strict'
1
+ import Packet from 'aedes-packet'
2
2
 
3
- const Packet = require('aedes-packet')
4
- const util = require('util')
3
+ class QoSPacket extends Packet {
4
+ constructor (original, client) {
5
+ super(original, client.broker)
5
6
 
6
- function QoSPacket (original, client) {
7
- Packet.call(this, original, client.broker)
7
+ this.writeCallback = client._onError.bind(client)
8
8
 
9
- this.writeCallback = client._onError.bind(client)
10
-
11
- if (!original.messageId) {
12
- this.messageId = client._nextId
13
- if (client._nextId >= 65535) {
14
- client._nextId = 1
9
+ if (!original.messageId) {
10
+ this.messageId = client._nextId
11
+ if (client._nextId >= 65535) {
12
+ client._nextId = 1
13
+ } else {
14
+ client._nextId++
15
+ }
15
16
  } else {
16
- client._nextId++
17
+ this.messageId = original.messageId
17
18
  }
18
- } else {
19
- this.messageId = original.messageId
20
19
  }
21
20
  }
22
21
 
23
- util.inherits(QoSPacket, Packet)
24
-
25
- module.exports = QoSPacket
22
+ export default QoSPacket
package/lib/utils.js CHANGED
@@ -1,8 +1,6 @@
1
- 'use strict'
1
+ import { Transform, Writable } from 'stream'
2
2
 
3
- const { Transform, Writable } = require('stream')
4
-
5
- function validateTopic (topic, message) {
3
+ export function validateTopic (topic, message) {
6
4
  if (!topic || topic.length === 0) { // [MQTT-3.8.3-3]
7
5
  return new Error('impossible to ' + message + ' to an empty topic')
8
6
  }
@@ -32,14 +30,14 @@ function validateTopic (topic, message) {
32
30
  }
33
31
  }
34
32
 
35
- function through (transform) {
33
+ export function through (transform) {
36
34
  return new Transform({
37
35
  objectMode: true,
38
36
  transform
39
37
  })
40
38
  }
41
39
 
42
- function bulk (fn) {
40
+ export function bulk (fn) {
43
41
  return new Writable({
44
42
  objectMode: true,
45
43
  writev: function (chunks, cb) {
@@ -48,9 +46,4 @@ function bulk (fn) {
48
46
  })
49
47
  }
50
48
 
51
- module.exports = {
52
- validateTopic,
53
- through,
54
- bulk,
55
- $SYS_PREFIX: '$SYS/'
56
- }
49
+ export const $SYS_PREFIX = '$SYS/'
package/lib/write.js CHANGED
@@ -1,6 +1,4 @@
1
- 'use strict'
2
-
3
- const mqtt = require('mqtt-packet')
1
+ import mqtt from 'mqtt-packet'
4
2
 
5
3
  function write (client, packet, done) {
6
4
  let error = null
@@ -8,7 +6,8 @@ function write (client, packet, done) {
8
6
  try {
9
7
  const result = mqtt.writeToStream(packet, client.conn)
10
8
  if (!result && !client.errored) {
11
- client.conn.once('drain', done)
9
+ // Socket buffer is full - wait for drain
10
+ client.waitForDrain(done)
12
11
  return
13
12
  }
14
13
  } catch (e) {
@@ -21,4 +20,4 @@ function write (client, packet, done) {
21
20
  setImmediate(done, error, client)
22
21
  }
23
22
 
24
- module.exports = write
23
+ export default write