mqtt 4.0.0 → 4.2.1

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 (59) hide show
  1. package/README.md +58 -24
  2. package/bin/mqtt.js +27 -0
  3. package/bin/pub.js +0 -0
  4. package/bin/sub.js +0 -0
  5. package/dist/mqtt.js +2794 -2512
  6. package/dist/mqtt.min.js +1 -1
  7. package/lib/client.js +72 -59
  8. package/lib/connect/index.js +7 -2
  9. package/lib/connect/tcp.js +2 -0
  10. package/lib/connect/tls.js +3 -0
  11. package/lib/connect/ws.js +194 -34
  12. package/lib/connect/wx.js +3 -3
  13. package/lib/store.js +2 -2
  14. package/mqtt.js +0 -24
  15. package/package.json +9 -10
  16. package/types/lib/client-options.d.ts +14 -1
  17. package/types/lib/client.d.ts +4 -3
  18. package/examples/client/secure-client.js +0 -24
  19. package/examples/client/simple-both.js +0 -13
  20. package/examples/client/simple-publish.js +0 -7
  21. package/examples/client/simple-subscribe.js +0 -9
  22. package/examples/tls client/crt.ca.cg.pem +0 -34
  23. package/examples/tls client/mqttclient.js +0 -48
  24. package/examples/tls client/tls-cert.pem +0 -13
  25. package/examples/tls client/tls-key.pem +0 -15
  26. package/examples/wss/client.js +0 -49
  27. package/examples/wss/client_with_proxy.js +0 -58
  28. package/test/abstract_client.js +0 -3139
  29. package/test/abstract_store.js +0 -135
  30. package/test/browser/server.js +0 -132
  31. package/test/browser/test.js +0 -92
  32. package/test/client.js +0 -540
  33. package/test/client_mqtt5.js +0 -537
  34. package/test/helpers/port_list.js +0 -45
  35. package/test/helpers/private-csr.pem +0 -16
  36. package/test/helpers/private-key.pem +0 -27
  37. package/test/helpers/public-cert.pem +0 -19
  38. package/test/helpers/public-key.pem +0 -9
  39. package/test/helpers/server.js +0 -53
  40. package/test/helpers/server_process.js +0 -9
  41. package/test/helpers/tls-cert.pem +0 -14
  42. package/test/helpers/tls-csr.pem +0 -11
  43. package/test/helpers/tls-key.pem +0 -15
  44. package/test/helpers/wrong-cert.pem +0 -13
  45. package/test/helpers/wrong-csr.pem +0 -11
  46. package/test/helpers/wrong-key.pem +0 -15
  47. package/test/mocha.opts +0 -4
  48. package/test/mqtt.js +0 -230
  49. package/test/mqtt_store.js +0 -9
  50. package/test/secure_client.js +0 -188
  51. package/test/server.js +0 -94
  52. package/test/server_helpers_for_client_tests.js +0 -100
  53. package/test/store.js +0 -10
  54. package/test/typescript/broker-connect-subscribe-and-publish.js +0 -24
  55. package/test/typescript/broker-connect-subscribe-and-publish.js.map +0 -1
  56. package/test/typescript/broker-connect-subscribe-and-publish.ts +0 -25
  57. package/test/typescript/tsconfig.json +0 -14
  58. package/test/util.js +0 -13
  59. package/test/websocket_client.js +0 -146
@@ -1,100 +0,0 @@
1
- 'use strict'
2
-
3
- var MqttServer = require('./server').MqttServer
4
- var MqttServerNoWait = require('./server').MqttServerNoWait
5
- var debug = require('debug')('TEST:server_helpers')
6
-
7
- /**
8
- * This will build the client for the server to use during testing, and set up the
9
- * server side client based on mqtt-connection for handling MQTT messages.
10
- * @param {boolean} fastFlag
11
- */
12
- function serverBuilder (fastFlag) {
13
- var handler = function (serverClient) {
14
- serverClient.on('auth', function (packet) {
15
- var rc = 'reasonCode'
16
- var connack = {}
17
- connack[rc] = 0
18
- serverClient.connack(connack)
19
- })
20
- serverClient.on('connect', function (packet) {
21
- var rc = 'returnCode'
22
- var connack = {}
23
- if (serverClient.options && serverClient.options.protocolVersion === 5) {
24
- rc = 'reasonCode'
25
- if (packet.clientId === 'invalid') {
26
- connack[rc] = 128
27
- } else {
28
- connack[rc] = 0
29
- }
30
- } else {
31
- if (packet.clientId === 'invalid') {
32
- connack[rc] = 2
33
- } else {
34
- connack[rc] = 0
35
- }
36
- }
37
- if (packet.properties && packet.properties.authenticationMethod) {
38
- return false
39
- } else {
40
- serverClient.connack(connack)
41
- }
42
- })
43
-
44
- serverClient.on('publish', function (packet) {
45
- setImmediate(function () {
46
- switch (packet.qos) {
47
- case 0:
48
- break
49
- case 1:
50
- serverClient.puback(packet)
51
- break
52
- case 2:
53
- serverClient.pubrec(packet)
54
- break
55
- }
56
- })
57
- })
58
-
59
- serverClient.on('pubrel', function (packet) {
60
- serverClient.pubcomp(packet)
61
- })
62
-
63
- serverClient.on('pubrec', function (packet) {
64
- serverClient.pubrel(packet)
65
- })
66
-
67
- serverClient.on('pubcomp', function () {
68
- // Nothing to be done
69
- })
70
-
71
- serverClient.on('subscribe', function (packet) {
72
- serverClient.suback({
73
- messageId: packet.messageId,
74
- granted: packet.subscriptions.map(function (e) {
75
- return e.qos
76
- })
77
- })
78
- })
79
-
80
- serverClient.on('unsubscribe', function (packet) {
81
- packet.granted = packet.unsubscriptions.map(function () { return 0 })
82
- serverClient.unsuback(packet)
83
- })
84
-
85
- serverClient.on('pingreq', function () {
86
- serverClient.pingresp()
87
- })
88
-
89
- serverClient.on('end', function () {
90
- debug('disconnected from server')
91
- })
92
- }
93
- if (fastFlag) {
94
- return new MqttServerNoWait(handler)
95
- } else {
96
- return new MqttServer(handler)
97
- }
98
- }
99
-
100
- exports.serverBuilder = serverBuilder
package/test/store.js DELETED
@@ -1,10 +0,0 @@
1
- 'use strict'
2
-
3
- var Store = require('../lib/store')
4
- var abstractTest = require('../test/abstract_store')
5
-
6
- describe('in-memory store', function () {
7
- abstractTest(function (done) {
8
- done(null, new Store())
9
- })
10
- })
@@ -1,24 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- // relative path uses package.json {"types":"types/index.d.ts", ...}
4
- var __1 = require("../..");
5
- var BROKER = 'test.mosquitto.org';
6
- var PAYLOAD = 'hello from TS';
7
- var TOPIC = 'typescript-test-' + Math.random().toString(16).substr(2);
8
- var opts = {};
9
- console.log("connect(" + JSON.stringify(BROKER) + ")");
10
- var client = __1.connect("mqtt://" + BROKER, opts);
11
- client.subscribe((_a = {}, _a[TOPIC] = 2, _a), function (err, granted) {
12
- granted.forEach(function (_a) {
13
- var topic = _a.topic, qos = _a.qos;
14
- console.log("subscribed to " + topic + " with qos=" + qos);
15
- });
16
- client.publish(TOPIC, PAYLOAD, { qos: 2 });
17
- }).on('message', function (topic, payload) {
18
- console.log("message from " + topic + ": " + payload);
19
- client.end();
20
- }).on('connect', function (packet) {
21
- console.log('connected!', JSON.stringify(packet));
22
- });
23
- var _a;
24
- //# sourceMappingURL=broker-connect-subscribe-and-publish.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"broker-connect-subscribe-and-publish.js","sourceRoot":"","sources":["broker-connect-subscribe-and-publish.ts"],"names":[],"mappings":";;AAAA,oEAAoE;AACpE,2BAAqE;AACrE,IAAM,MAAM,GAAG,oBAAoB,CAAA;AAEnC,IAAM,OAAO,GAAG,eAAe,CAAA;AAC/B,IAAM,KAAK,GAAG,kBAAkB,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAA;AACvE,IAAM,IAAI,GAAmB,EAAE,CAAA;AAE/B,OAAO,CAAC,GAAG,CAAC,aAAW,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,MAAG,CAAC,CAAA;AACjD,IAAM,MAAM,GAAU,WAAO,CAAC,YAAU,MAAQ,EAAE,IAAI,CAAC,CAAA;AAEvD,MAAM,CAAC,SAAS,WAAE,GAAC,KAAK,IAAG,CAAC,OAAG,UAAC,GAAG,EAAE,OAAO;IACxC,OAAO,CAAC,OAAO,CAAC,UAAC,EAAY;YAAX,gBAAK,EAAE,YAAG;QACxB,OAAO,CAAC,GAAG,CAAC,mBAAiB,KAAK,kBAAa,GAAK,CAAC,CAAA;IACzD,CAAC,CAAC,CAAA;IACF,MAAM,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,EAAE,EAAC,GAAG,EAAE,CAAC,EAAC,CAAC,CAAA;AAC5C,CAAC,CAAC,CAAC,EAAE,CAAC,SAAS,EAAE,UAAC,KAAa,EAAE,OAAe;IAC5C,OAAO,CAAC,GAAG,CAAC,kBAAgB,KAAK,UAAK,OAAS,CAAC,CAAA;IAChD,MAAM,CAAC,GAAG,EAAE,CAAA;AAChB,CAAC,CAAC,CAAC,EAAE,CAAC,SAAS,EAAE,UAAC,MAAsB;IACpC,OAAO,CAAC,GAAG,CAAC,YAAY,EAAE,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAA;AACrD,CAAC,CAAC,CAAA"}
@@ -1,25 +0,0 @@
1
- // relative path uses package.json {"types":"types/index.d.ts", ...}
2
- import {IClientOptions, Client, connect, IConnackPacket} from '../..'
3
- const BROKER = 'test.mosquitto.org'
4
-
5
- const PAYLOAD_WILL = Buffer.from('bye from TS')
6
- const PAYLOAD_QOS = Buffer.from('hello from TS (with qos=2)')
7
- const PAYLOAD_RETAIN = 'hello from TS (with retain=true)'
8
- const TOPIC = 'typescript-test-' + Math.random().toString(16).substr(2)
9
- const opts: IClientOptions = {will: {topic: TOPIC, payload: PAYLOAD_WILL, qos: 0, retain: false}}
10
-
11
- console.log(`connect(${JSON.stringify(BROKER)})`)
12
- const client:Client = connect(`mqtt://${BROKER}`, opts)
13
-
14
- client.subscribe({[TOPIC]: {qos: 2}}, (err, granted) => {
15
- granted.forEach(({topic, qos}) => {
16
- console.log(`subscribed to ${topic} with qos=${qos}`)
17
- })
18
- client.publish(TOPIC, PAYLOAD_QOS, {qos: 2})
19
- client.publish(TOPIC, PAYLOAD_RETAIN, {retain: true})
20
- }).on('message', (topic: string, payload: Buffer) => {
21
- console.log(`message from ${topic}: ${payload}`)
22
- client.end()
23
- }).on('connect', (packet: IConnackPacket) => {
24
- console.log('connected!', JSON.stringify(packet))
25
- })
@@ -1,14 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "module": "commonjs",
4
- "target": "es5",
5
- "moduleResolution": "node",
6
- "noEmitOnError": true,
7
- "noImplicitAny": true,
8
- "alwaysStrict": true,
9
- "strictNullChecks": true,
10
- "noImplicitReturns": true,
11
- "noImplicitThis": true,
12
- "sourceMap": true
13
- }
14
- }
package/test/util.js DELETED
@@ -1,13 +0,0 @@
1
- 'use strict'
2
-
3
- var through = require('through2')
4
-
5
- module.exports.testStream = function () {
6
- return through(function (buf, enc, cb) {
7
- var that = this
8
- setImmediate(function () {
9
- that.push(buf)
10
- cb()
11
- })
12
- })
13
- }
@@ -1,146 +0,0 @@
1
- 'use strict'
2
-
3
- var http = require('http')
4
- var websocket = require('websocket-stream')
5
- var WebSocketServer = require('ws').Server
6
- var Connection = require('mqtt-connection')
7
- var abstractClientTests = require('./abstract_client')
8
- var mqtt = require('../')
9
- var xtend = require('xtend')
10
- var assert = require('assert')
11
- var port = 9999
12
- var server = http.createServer()
13
-
14
- function attachWebsocketServer (wsServer) {
15
- var wss = new WebSocketServer({server: wsServer, perMessageDeflate: false})
16
-
17
- wss.on('connection', function (ws) {
18
- var stream = websocket(ws)
19
- var connection = new Connection(stream)
20
-
21
- wsServer.emit('client', connection)
22
- stream.on('error', function () {})
23
- connection.on('error', function () {})
24
- })
25
-
26
- return wsServer
27
- }
28
-
29
- function attachClientEventHandlers (client) {
30
- client.on('connect', function (packet) {
31
- if (packet.clientId === 'invalid') {
32
- client.connack({ returnCode: 2 })
33
- } else {
34
- server.emit('connect', client)
35
- client.connack({returnCode: 0})
36
- }
37
- })
38
-
39
- client.on('publish', function (packet) {
40
- setImmediate(function () {
41
- switch (packet.qos) {
42
- case 0:
43
- break
44
- case 1:
45
- client.puback(packet)
46
- break
47
- case 2:
48
- client.pubrec(packet)
49
- break
50
- }
51
- })
52
- })
53
-
54
- client.on('pubrel', function (packet) {
55
- client.pubcomp(packet)
56
- })
57
-
58
- client.on('pubrec', function (packet) {
59
- client.pubrel(packet)
60
- })
61
-
62
- client.on('pubcomp', function () {
63
- // Nothing to be done
64
- })
65
-
66
- client.on('subscribe', function (packet) {
67
- client.suback({
68
- messageId: packet.messageId,
69
- granted: packet.subscriptions.map(function (e) {
70
- return e.qos
71
- })
72
- })
73
- })
74
-
75
- client.on('unsubscribe', function (packet) {
76
- client.unsuback(packet)
77
- })
78
-
79
- client.on('pingreq', function () {
80
- client.pingresp()
81
- })
82
- }
83
-
84
- attachWebsocketServer(server)
85
-
86
- server.on('client', attachClientEventHandlers).listen(port)
87
-
88
- describe('Websocket Client', function () {
89
- var baseConfig = { protocol: 'ws', port: port }
90
-
91
- function makeOptions (custom) {
92
- // xtend returns a new object. Does not mutate arguments
93
- return xtend(baseConfig, custom || {})
94
- }
95
-
96
- it('should use mqtt as the protocol by default', function (done) {
97
- server.once('client', function (client) {
98
- assert.strictEqual(client.stream.socket.protocol, 'mqtt')
99
- })
100
- mqtt.connect(makeOptions()).on('connect', function () {
101
- this.end(true, done)
102
- })
103
- })
104
-
105
- it('should be able transform the url (for e.g. to sign it)', function (done) {
106
- var baseUrl = 'ws://localhost:9999/mqtt'
107
- var sig = '?AUTH=token'
108
- var expected = baseUrl + sig
109
- var actual
110
- var opts = makeOptions({
111
- path: '/mqtt',
112
- transformWsUrl: function (url, opt, client) {
113
- assert.equal(url, baseUrl)
114
- assert.strictEqual(opt, opts)
115
- assert.strictEqual(client.options, opts)
116
- assert.strictEqual(typeof opt.transformWsUrl, 'function')
117
- assert(client instanceof mqtt.MqttClient)
118
- url += sig
119
- actual = url
120
- return url
121
- }})
122
- mqtt.connect(opts)
123
- .on('connect', function () {
124
- assert.equal(this.stream.socket.url, expected)
125
- assert.equal(actual, expected)
126
- this.end(true, done)
127
- })
128
- })
129
-
130
- it('should use mqttv3.1 as the protocol if using v3.1', function (done) {
131
- server.once('client', function (client) {
132
- assert.strictEqual(client.stream.socket.protocol, 'mqttv3.1')
133
- })
134
-
135
- var opts = makeOptions({
136
- protocolId: 'MQIsdp',
137
- protocolVersion: 3
138
- })
139
-
140
- mqtt.connect(opts).on('connect', function () {
141
- this.end(true, done)
142
- })
143
- })
144
-
145
- abstractClientTests(server, makeOptions())
146
- })