mqtt 2.18.6 → 3.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.
@@ -0,0 +1,130 @@
1
+ 'use strict'
2
+
3
+ var Transform = require('readable-stream').Transform
4
+ var duplexify = require('duplexify')
5
+ var base64 = require('base64-js')
6
+
7
+ /* global FileReader */
8
+ var my
9
+ var proxy
10
+ var stream
11
+ var isInitialized = false
12
+
13
+ function buildProxy () {
14
+ var proxy = new Transform()
15
+ proxy._write = function (chunk, encoding, next) {
16
+ my.sendSocketMessage({
17
+ data: chunk.buffer,
18
+ success: function () {
19
+ next()
20
+ },
21
+ fail: function () {
22
+ next(new Error())
23
+ }
24
+ })
25
+ }
26
+ proxy._flush = function socketEnd (done) {
27
+ my.closeSocket({
28
+ success: function () {
29
+ done()
30
+ }
31
+ })
32
+ }
33
+
34
+ return proxy
35
+ }
36
+
37
+ function setDefaultOpts (opts) {
38
+ if (!opts.hostname) {
39
+ opts.hostname = 'localhost'
40
+ }
41
+ if (!opts.path) {
42
+ opts.path = '/'
43
+ }
44
+
45
+ if (!opts.wsOptions) {
46
+ opts.wsOptions = {}
47
+ }
48
+ }
49
+
50
+ function buildUrl (opts, client) {
51
+ var protocol = opts.protocol === 'alis' ? 'wss' : 'ws'
52
+ var url = protocol + '://' + opts.hostname + opts.path
53
+ if (opts.port && opts.port !== 80 && opts.port !== 443) {
54
+ url = protocol + '://' + opts.hostname + ':' + opts.port + opts.path
55
+ }
56
+ if (typeof (opts.transformWsUrl) === 'function') {
57
+ url = opts.transformWsUrl(url, opts, client)
58
+ }
59
+ return url
60
+ }
61
+
62
+ function bindEventHandler () {
63
+ if (isInitialized) return
64
+
65
+ isInitialized = true
66
+
67
+ my.onSocketOpen(function () {
68
+ stream.setReadable(proxy)
69
+ stream.setWritable(proxy)
70
+ stream.emit('connect')
71
+ })
72
+
73
+ my.onSocketMessage(function (res) {
74
+ if (typeof res.data === 'string') {
75
+ var array = base64.toByteArray(res.data)
76
+ var buffer = Buffer.from(array)
77
+ proxy.push(buffer)
78
+ } else {
79
+ var reader = new FileReader()
80
+ reader.addEventListener('load', function () {
81
+ var data = reader.result
82
+
83
+ if (data instanceof ArrayBuffer) data = Buffer.from(data)
84
+ else data = Buffer.from(data, 'utf8')
85
+ proxy.push(data)
86
+ })
87
+ reader.readAsArrayBuffer(res.data)
88
+ }
89
+ })
90
+
91
+ my.onSocketClose(function () {
92
+ stream.end()
93
+ stream.destroy()
94
+ })
95
+
96
+ my.onSocketError(function (res) {
97
+ stream.destroy(res)
98
+ })
99
+ }
100
+
101
+ function buildStream (client, opts) {
102
+ opts.hostname = opts.hostname || opts.host
103
+
104
+ if (!opts.hostname) {
105
+ throw new Error('Could not determine host. Specify host manually.')
106
+ }
107
+
108
+ var websocketSubProtocol =
109
+ (opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
110
+ ? 'mqttv3.1'
111
+ : 'mqtt'
112
+
113
+ setDefaultOpts(opts)
114
+
115
+ var url = buildUrl(opts, client)
116
+ my = opts.my
117
+ my.connectSocket({
118
+ url: url,
119
+ protocols: websocketSubProtocol
120
+ })
121
+
122
+ proxy = buildProxy()
123
+ stream = duplexify.obj()
124
+
125
+ bindEventHandler()
126
+
127
+ return stream
128
+ }
129
+
130
+ module.exports = buildStream
@@ -15,6 +15,9 @@ if (process.title !== 'browser') {
15
15
  } else {
16
16
  protocols.wx = require('./wx')
17
17
  protocols.wxs = require('./wx')
18
+
19
+ protocols.ali = require('./ali')
20
+ protocols.alis = require('./ali')
18
21
  }
19
22
 
20
23
  protocols.ws = require('./ws')
@@ -76,7 +79,7 @@ function connect (brokerUrl, opts) {
76
79
 
77
80
  if (opts.cert && opts.key) {
78
81
  if (opts.protocol) {
79
- if (['mqtts', 'wss', 'wxs'].indexOf(opts.protocol) === -1) {
82
+ if (['mqtts', 'wss', 'wxs', 'alis'].indexOf(opts.protocol) === -1) {
80
83
  switch (opts.protocol) {
81
84
  case 'mqtt':
82
85
  opts.protocol = 'mqtts'
@@ -87,6 +90,9 @@ function connect (brokerUrl, opts) {
87
90
  case 'wx':
88
91
  opts.protocol = 'wxs'
89
92
  break
93
+ case 'ali':
94
+ opts.protocol = 'alis'
95
+ break
90
96
  default:
91
97
  throw new Error('Unknown protocol for secure connection: "' + opts.protocol + '"!')
92
98
  }
@@ -105,7 +111,9 @@ function connect (brokerUrl, opts) {
105
111
  'ws',
106
112
  'wss',
107
113
  'wx',
108
- 'wxs'
114
+ 'wxs',
115
+ 'ali',
116
+ 'alis'
109
117
  ].filter(function (key, index) {
110
118
  if (isSecure && index % 2 === 0) {
111
119
  // Skip insecure protocols when requesting a secure one.
package/lib/connect/wx.js CHANGED
@@ -1,66 +1,50 @@
1
1
  'use strict'
2
2
 
3
- /* global wx */
4
- var socketOpen = false
5
- var socketMsgQueue = []
3
+ var Transform = require('readable-stream').Transform
4
+ var duplexify = require('duplexify')
6
5
 
7
- function sendSocketMessage (msg) {
8
- if (socketOpen) {
9
- wx.sendSocketMessage({
10
- data: msg.buffer || msg
6
+ /* global wx */
7
+ var socketTask
8
+ var proxy
9
+ var stream
10
+
11
+ function buildProxy () {
12
+ var proxy = new Transform()
13
+ proxy._write = function (chunk, encoding, next) {
14
+ socketTask.send({
15
+ data: chunk.buffer,
16
+ success: function () {
17
+ next()
18
+ },
19
+ fail: function (errMsg) {
20
+ next(new Error(errMsg))
21
+ }
22
+ })
23
+ }
24
+ proxy._flush = function socketEnd (done) {
25
+ socketTask.close({
26
+ success: function () {
27
+ done()
28
+ }
11
29
  })
12
- } else {
13
- socketMsgQueue.push(msg)
14
30
  }
31
+
32
+ return proxy
15
33
  }
16
34
 
17
- function WebSocket (url, protocols) {
18
- var ws = {
19
- OPEN: 1,
20
- CLOSING: 2,
21
- CLOSED: 3,
22
- readyState: socketOpen ? 1 : 0,
23
- send: sendSocketMessage,
24
- close: wx.closeSocket,
25
- onopen: null,
26
- onmessage: null,
27
- onclose: null,
28
- onerror: null
35
+ function setDefaultOpts (opts) {
36
+ if (!opts.hostname) {
37
+ opts.hostname = 'localhost'
38
+ }
39
+ if (!opts.path) {
40
+ opts.path = '/'
29
41
  }
30
42
 
31
- wx.connectSocket({
32
- url: url,
33
- protocols: protocols
34
- })
35
- wx.onSocketOpen(function (res) {
36
- ws.readyState = ws.OPEN
37
- socketOpen = true
38
- for (var i = 0; i < socketMsgQueue.length; i++) {
39
- sendSocketMessage(socketMsgQueue[i])
40
- }
41
- socketMsgQueue = []
42
-
43
- ws.onopen && ws.onopen.apply(ws, arguments)
44
- })
45
- wx.onSocketMessage(function (res) {
46
- ws.onmessage && ws.onmessage.apply(ws, arguments)
47
- })
48
- wx.onSocketClose(function () {
49
- ws.onclose && ws.onclose.apply(ws, arguments)
50
- ws.readyState = ws.CLOSED
51
- socketOpen = false
52
- })
53
- wx.onSocketError(function () {
54
- ws.onerror && ws.onerror.apply(ws, arguments)
55
- ws.readyState = ws.CLOSED
56
- socketOpen = false
57
- })
58
-
59
- return ws
43
+ if (!opts.wsOptions) {
44
+ opts.wsOptions = {}
45
+ }
60
46
  }
61
47
 
62
- var websocket = require('websocket-stream')
63
-
64
48
  function buildUrl (opts, client) {
65
49
  var protocol = opts.protocol === 'wxs' ? 'wss' : 'ws'
66
50
  var url = protocol + '://' + opts.hostname + opts.path
@@ -73,38 +57,78 @@ function buildUrl (opts, client) {
73
57
  return url
74
58
  }
75
59
 
76
- function setDefaultOpts (opts) {
77
- if (!opts.hostname) {
78
- opts.hostname = 'localhost'
79
- }
80
- if (!opts.path) {
81
- opts.path = '/'
82
- }
60
+ function bindEventHandler () {
61
+ socketTask.onOpen(function () {
62
+ stream.setReadable(proxy)
63
+ stream.setWritable(proxy)
64
+ stream.emit('connect')
65
+ })
83
66
 
84
- if (!opts.wsOptions) {
85
- opts.wsOptions = {}
86
- }
67
+ socketTask.onMessage(function (res) {
68
+ var data = res.data
69
+
70
+ if (data instanceof ArrayBuffer) data = Buffer.from(data)
71
+ else data = Buffer.from(data, 'utf8')
72
+ proxy.push(data)
73
+ })
74
+
75
+ socketTask.onClose(function () {
76
+ stream.end()
77
+ stream.destroy()
78
+ })
79
+
80
+ socketTask.onError(function (res) {
81
+ stream.destroy(new Error(res.errMsg))
82
+ })
87
83
  }
88
84
 
89
- function createWebSocket (client, opts) {
85
+ function buildStream (client, opts) {
86
+ opts.hostname = opts.hostname || opts.host
87
+
88
+ if (!opts.hostname) {
89
+ throw new Error('Could not determine host. Specify host manually.')
90
+ }
91
+
90
92
  var websocketSubProtocol =
91
93
  (opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
92
94
  ? 'mqttv3.1'
93
95
  : 'mqtt'
94
96
 
95
97
  setDefaultOpts(opts)
96
- var url = buildUrl(opts, client)
97
- return websocket(WebSocket(url, [websocketSubProtocol]))
98
- }
99
98
 
100
- function buildBuilder (client, opts) {
101
- opts.hostname = opts.hostname || opts.host
99
+ var url = buildUrl(opts, client)
100
+ socketTask = wx.connectSocket({
101
+ url: url,
102
+ protocols: websocketSubProtocol
103
+ })
102
104
 
103
- if (!opts.hostname) {
104
- throw new Error('Could not determine host. Specify host manually.')
105
+ proxy = buildProxy()
106
+ stream = duplexify.obj()
107
+ stream._destroy = function (err, cb) {
108
+ socketTask.close({
109
+ success: function () {
110
+ cb && cb(err)
111
+ }
112
+ })
105
113
  }
106
114
 
107
- return createWebSocket(client, opts)
115
+ var destroyRef = stream.destroy
116
+ stream.destroy = function () {
117
+ stream.destroy = destroyRef
118
+
119
+ var self = this
120
+ process.nextTick(function () {
121
+ socketTask.close({
122
+ fail: function () {
123
+ self._destroy(new Error())
124
+ }
125
+ })
126
+ })
127
+ }.bind(stream)
128
+
129
+ bindEventHandler()
130
+
131
+ return stream
108
132
  }
109
133
 
110
- module.exports = buildBuilder
134
+ module.exports = buildStream
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "mqtt",
3
3
  "description": "A library for the MQTT protocol",
4
- "version": "2.18.6",
4
+ "version": "3.0.0",
5
5
  "contributors": [
6
6
  "Adam Rudd <adamvrr@gmail.com>",
7
- "Matteo Collina <matteo.collina@gmail.com> (https://github.com/mcollina)"
7
+ "Matteo Collina <matteo.collina@gmail.com> (https://github.com/mcollina)",
8
+ "Siarhei Buntsevich <scarry0506@gmail.com> (https://github.com/scarry1992)"
8
9
  ],
9
10
  "keywords": [
10
11
  "mqtt",
@@ -29,9 +30,8 @@
29
30
  "prepare": "npm run browser-build",
30
31
  "browser-build": "rimraf dist/ && mkdirp dist/ && browserify mqtt.js -s mqtt > dist/mqtt.js && uglifyjs < dist/mqtt.js > dist/mqtt.min.js",
31
32
  "browser-test": "zuul --server test/browser/server.js --local --open test/browser/test.js",
32
- "weapp-test": "zuul --server test/browser/server.js --local --open test/browser/wx.js",
33
33
  "sauce-test": "zuul --server test/browser/server.js --tunnel ngrok -- test/browser/test.js",
34
- "ci": "npm run tslint && npm run typescript-test && npm run test && codecov"
34
+ "ci": "npm run tslint && npm run typescript-compile-test && npm run test && codecov"
35
35
  },
36
36
  "pre-commit": [
37
37
  "test",
@@ -63,6 +63,7 @@
63
63
  "net": false
64
64
  },
65
65
  "dependencies": {
66
+ "base64-js": "^1.3.0",
66
67
  "commist": "^1.0.0",
67
68
  "concat-stream": "^1.6.2",
68
69
  "end-of-stream": "^1.4.1",
@@ -70,34 +71,34 @@
70
71
  "help-me": "^1.0.1",
71
72
  "inherits": "^2.0.3",
72
73
  "minimist": "^1.2.0",
73
- "mqtt-packet": "^5.6.0",
74
+ "mqtt-packet": "^6.0.0",
74
75
  "pump": "^3.0.0",
75
76
  "readable-stream": "^2.3.6",
76
77
  "reinterval": "^1.1.0",
77
- "split2": "^2.1.1",
78
+ "split2": "^3.1.0",
78
79
  "websocket-stream": "^5.1.2",
79
80
  "xtend": "^4.0.1"
80
81
  },
81
82
  "devDependencies": {
82
- "@types/node": "^8.10.21",
83
+ "@types/node": "^10.0.0",
83
84
  "browserify": "^16.2.2",
84
85
  "codecov": "^3.0.4",
85
86
  "global": "^4.3.2",
86
87
  "istanbul": "^0.4.5",
87
88
  "mkdirp": "^0.5.1",
88
89
  "mocha": "^4.1.0",
89
- "mqtt-connection": "^3.0.0",
90
+ "mqtt-connection": "^4.0.0",
90
91
  "pre-commit": "^1.2.2",
91
92
  "rimraf": "^2.6.2",
92
93
  "safe-buffer": "^5.1.2",
93
94
  "should": "^13.2.1",
94
95
  "sinon": "~1.17.7",
95
- "snazzy": "^7.1.1",
96
+ "snazzy": "^8.0.0",
96
97
  "standard": "^11.0.1",
97
- "through2": "^2.0.3",
98
+ "through2": "^3.0.0",
98
99
  "tslint": "^5.11.0",
99
- "tslint-config-standard": "^7.1.0",
100
- "typescript": "^2.9.2",
100
+ "tslint-config-standard": "^8.0.1",
101
+ "typescript": "^3.2.2",
101
102
  "uglify-js": "^3.4.5",
102
103
  "ws": "^3.3.3",
103
104
  "zuul": "^3.12.0",