mqtt stuff added

This commit is contained in:
2018-05-16 10:44:10 +02:00
parent 74584cdbbe
commit c7eb46b346
499 changed files with 55775 additions and 19 deletions

2323
node_modules/mqtt/test/abstract_client.js generated vendored Normal file

File diff suppressed because it is too large Load Diff

133
node_modules/mqtt/test/abstract_store.js generated vendored Normal file
View File

@ -0,0 +1,133 @@
'use strict'
require('should')
module.exports = function abstractStoreTest (build) {
var store
beforeEach(function (done) {
build(function (err, _store) {
store = _store
done(err)
})
})
afterEach(function (done) {
store.close(done)
})
it('should put and stream in-flight packets', function (done) {
var packet = {
topic: 'hello',
payload: 'world',
qos: 1,
messageId: 42
}
store.put(packet, function () {
store
.createStream()
.on('data', function (data) {
data.should.eql(packet)
done()
})
})
})
it('should support destroying the stream', function (done) {
var packet = {
topic: 'hello',
payload: 'world',
qos: 1,
messageId: 42
}
store.put(packet, function () {
var stream = store.createStream()
stream.on('close', done)
stream.destroy()
})
})
it('should add and del in-flight packets', function (done) {
var packet = {
topic: 'hello',
payload: 'world',
qos: 1,
messageId: 42
}
store.put(packet, function () {
store.del(packet, function () {
store
.createStream()
.on('data', function () {
done(new Error('this should never happen'))
})
.on('end', done)
})
})
})
it('should replace a packet when doing put with the same messageId', function (done) {
var packet1 = {
topic: 'hello',
payload: 'world',
qos: 2,
messageId: 42
}
var packet2 = {
qos: 2,
messageId: 42
}
store.put(packet1, function () {
store.put(packet2, function () {
store
.createStream()
.on('data', function (data) {
data.should.eql(packet2)
done()
})
})
})
})
it('should return the original packet on del', function (done) {
var packet = {
topic: 'hello',
payload: 'world',
qos: 1,
messageId: 42
}
store.put(packet, function () {
store.del({ messageId: 42 }, function (err, deleted) {
if (err) {
throw err
}
deleted.should.eql(packet)
done()
})
})
})
it('should get a packet with the same messageId', function (done) {
var packet = {
topic: 'hello',
payload: 'world',
qos: 1,
messageId: 42
}
store.put(packet, function () {
store.get({ messageId: 42 }, function (err, fromDb) {
if (err) {
throw err
}
fromDb.should.eql(packet)
done()
})
})
})
}

132
node_modules/mqtt/test/browser/server.js generated vendored Normal file
View File

@ -0,0 +1,132 @@
'use strict'
var handleClient
var websocket = require('websocket-stream')
var WebSocketServer = require('ws').Server
var Connection = require('mqtt-connection')
var http = require('http')
handleClient = function (client) {
var self = this
if (!self.clients) {
self.clients = {}
}
client.on('connect', function (packet) {
if (packet.clientId === 'invalid') {
client.connack({returnCode: 2})
} else {
client.connack({returnCode: 0})
}
self.clients[packet.clientId] = client
client.subscriptions = []
})
client.on('publish', function (packet) {
var i, k, c, s, publish
switch (packet.qos) {
case 0:
break
case 1:
client.puback(packet)
break
case 2:
client.pubrec(packet)
break
}
for (k in self.clients) {
c = self.clients[k]
publish = false
for (i = 0; i < c.subscriptions.length; i++) {
s = c.subscriptions[i]
if (s.test(packet.topic)) {
publish = true
}
}
if (publish) {
try {
c.publish({topic: packet.topic, payload: packet.payload})
} catch (error) {
delete self.clients[k]
}
}
}
})
client.on('pubrel', function (packet) {
client.pubcomp(packet)
})
client.on('pubrec', function (packet) {
client.pubrel(packet)
})
client.on('pubcomp', function () {
// Nothing to be done
})
client.on('subscribe', function (packet) {
var qos
var topic
var reg
var granted = []
for (var i = 0; i < packet.subscriptions.length; i++) {
qos = packet.subscriptions[i].qos
topic = packet.subscriptions[i].topic
reg = new RegExp(topic.replace('+', '[^/]+').replace('#', '.+') + '$')
granted.push(qos)
client.subscriptions.push(reg)
}
client.suback({messageId: packet.messageId, granted: granted})
})
client.on('unsubscribe', function (packet) {
client.unsuback(packet)
})
client.on('pingreq', function () {
client.pingresp()
})
}
function start (startPort, done) {
var server = http.createServer()
var wss = new WebSocketServer({server: server})
wss.on('connection', function (ws) {
var stream, connection
if (!(ws.protocol === 'mqtt' ||
ws.protocol === 'mqttv3.1')) {
return ws.close()
}
stream = websocket(ws)
connection = new Connection(stream)
handleClient.call(server, connection)
})
server.listen(startPort, done)
server.on('request', function (req, res) {
res.statusCode = 404
res.end('Not Found')
})
return server
}
if (require.main === module) {
start(process.env.PORT || process.env.ZUUL_PORT, function (err) {
if (err) {
console.error(err)
return
}
console.log('tunnelled server started on port', process.env.PORT || process.env.ZUUL_PORT)
})
}

92
node_modules/mqtt/test/browser/test.js generated vendored Normal file
View File

@ -0,0 +1,92 @@
'use strict'
var mqtt = require('../../lib/connect')
var _URL = require('url')
var xtend = require('xtend')
var parsed = _URL.parse(document.URL)
var isHttps = parsed.protocol === 'https:'
var port = parsed.port || (isHttps ? 443 : 80)
var host = parsed.hostname
var protocol = isHttps ? 'wss' : 'ws'
function clientTests (buildClient) {
var client
beforeEach(function () {
client = buildClient()
client.on('offline', function () {
console.log('client offline')
})
client.on('connect', function () {
console.log('client connect')
})
client.on('reconnect', function () {
console.log('client reconnect')
})
})
afterEach(function (done) {
client.once('close', function () {
done()
})
client.end()
})
it('should connect', function (done) {
client.on('connect', function () {
done()
})
})
it('should publish and subscribe', function (done) {
client.subscribe('hello', function () {
done()
}).publish('hello', 'world')
})
}
function suiteFactory (configName, opts) {
function setVersion (base) {
return xtend(base || {}, opts)
}
var suiteName = 'MqttClient(' + configName + '=' + JSON.stringify(opts) + ')'
describe(suiteName, function () {
this.timeout(10000)
describe('specifying nothing', function () {
clientTests(function () {
return mqtt.connect(setVersion())
})
})
if (parsed.hostname === 'localhost') {
describe('specifying a port', function () {
clientTests(function () {
return mqtt.connect(setVersion({ protocol: protocol, port: port }))
})
})
}
describe('specifying a port and host', function () {
clientTests(function () {
return mqtt.connect(setVersion({ protocol: protocol, port: port, host: host }))
})
})
describe('specifying a URL', function () {
clientTests(function () {
return mqtt.connect(protocol + '://' + host + ':' + port, setVersion())
})
})
describe('specifying a URL with a path', function () {
clientTests(function () {
return mqtt.connect(protocol + '://' + host + ':' + port + '/mqtt', setVersion())
})
})
})
}
suiteFactory('v3', {protocolId: 'MQIsdp', protocolVersion: 3})
suiteFactory('default', {})

93
node_modules/mqtt/test/browser/wx.js generated vendored Normal file
View File

@ -0,0 +1,93 @@
'use strict'
var mqtt = require('../../lib/connect')
var _URL = require('url')
var xtend = require('xtend')
var parsed = _URL.parse(document.URL)
var isHttps = parsed.protocol === 'https:'
var port = parsed.port || (isHttps ? 443 : 80)
var host = parsed.hostname
var protocol = isHttps ? 'wxs' : 'wx'
require('../helpers/wx')
function clientTests (buildClient) {
var client
beforeEach(function () {
client = buildClient()
client.on('offline', function () {
console.log('client offline')
})
client.on('connect', function () {
console.log('client connect')
})
client.on('reconnect', function () {
console.log('client reconnect')
})
})
afterEach(function (done) {
client.once('close', function () {
done()
})
client.end()
})
it('should connect', function (done) {
client.on('connect', function () {
done()
})
})
it('should publish and subscribe', function (done) {
client.subscribe('hello', function () {
done()
}).publish('hello', 'world')
})
}
function suiteFactory (configName, opts) {
function setVersion (base) {
return xtend(base || {}, opts)
}
var suiteName = 'MqttClient(' + configName + '=' + JSON.stringify(opts) + ')'
describe(suiteName, function () {
this.timeout(10000)
describe('specifying nothing', function () {
clientTests(function () {
return mqtt.connect(setVersion())
})
})
if (parsed.hostname === 'localhost') {
describe('specifying a port', function () {
clientTests(function () {
return mqtt.connect(setVersion({ protocol: protocol, port: port }))
})
})
}
describe('specifying a port and host', function () {
clientTests(function () {
return mqtt.connect(setVersion({ protocol: protocol, port: port, host: host }))
})
})
describe('specifying a URL', function () {
clientTests(function () {
return mqtt.connect(protocol + '://' + host + ':' + port, setVersion())
})
})
describe('specifying a URL with a path', function () {
clientTests(function () {
return mqtt.connect(protocol + '://' + host + ':' + port + '/mqtt', setVersion())
})
})
})
}
suiteFactory('v3', {protocolId: 'MQIsdp', protocolVersion: 3})
suiteFactory('default', {})

542
node_modules/mqtt/test/client.js generated vendored Normal file
View File

@ -0,0 +1,542 @@
'use strict'
var mqtt = require('..')
var should = require('should')
var fork = require('child_process').fork
var path = require('path')
var abstractClientTests = require('./abstract_client')
var net = require('net')
var eos = require('end-of-stream')
var mqttPacket = require('mqtt-packet')
var Buffer = require('safe-buffer').Buffer
var Duplex = require('readable-stream').Duplex
var Connection = require('mqtt-connection')
var Server = require('./server')
var port = 9876
var server
function connOnlyServer () {
return new Server(function (client) {
client.on('connect', function (packet) {
client.connack({returnCode: 0})
})
})
}
/**
* Test server
*/
function buildServer () {
return new Server(function (client) {
client.on('connect', function (packet) {
if (packet.clientId === 'invalid') {
client.connack({returnCode: 2})
} else {
client.connack({returnCode: 0})
}
})
client.on('publish', function (packet) {
setImmediate(function () {
switch (packet.qos) {
case 0:
break
case 1:
client.puback(packet)
break
case 2:
client.pubrec(packet)
break
}
})
})
client.on('pubrel', function (packet) {
client.pubcomp(packet)
})
client.on('pubrec', function (packet) {
client.pubrel(packet)
})
client.on('pubcomp', function () {
// Nothing to be done
})
client.on('subscribe', function (packet) {
client.suback({
messageId: packet.messageId,
granted: packet.subscriptions.map(function (e) {
return e.qos
})
})
})
client.on('unsubscribe', function (packet) {
client.unsuback(packet)
})
client.on('pingreq', function () {
client.pingresp()
})
})
}
server = buildServer().listen(port)
describe('MqttClient', function () {
describe('creating', function () {
it('should allow instantiation of MqttClient without the \'new\' operator', function (done) {
should(function () {
var client
try {
client = mqtt.MqttClient(function () {
throw Error('break')
}, {})
client.end()
} catch (err) {
if (err.message !== 'break') {
throw err
}
done()
}
}).not.throw('Object #<Object> has no method \'_setupStream\'')
})
})
var config = { protocol: 'mqtt', port: port }
abstractClientTests(server, config)
describe('message ids', function () {
it('should increment the message id', function () {
var client = mqtt.connect(config)
var currentId = client._nextId()
client._nextId().should.equal(currentId + 1)
client.end()
})
it('should return 1 once the internal counter reached limit', function () {
var client = mqtt.connect(config)
client.nextId = 65535
client._nextId().should.equal(65535)
client._nextId().should.equal(1)
client.end()
})
it('should return 65535 for last message id once the internal counter reached limit', function () {
var client = mqtt.connect(config)
client.nextId = 65535
client._nextId().should.equal(65535)
client.getLastMessageId().should.equal(65535)
client._nextId().should.equal(1)
client.getLastMessageId().should.equal(1)
client.end()
})
it('should not throw an error if packet\'s messageId is not found when receiving a pubrel packet', function (done) {
var server2 = new Server(function (c) {
c.on('connect', function (packet) {
c.connack({returnCode: 0})
c.pubrel({ messageId: Math.floor(Math.random() * 9000) + 1000 })
})
})
server2.listen(port + 49, function () {
var client = mqtt.connect({
port: port + 49,
host: 'localhost'
})
client.on('packetsend', function (packet) {
if (packet.cmd === 'pubcomp') {
client.end()
server2.close()
done()
}
})
})
})
it('should not go overflow if the TCP frame contains a lot of PUBLISH packets', function (done) {
var parser = mqttPacket.parser()
var count = 0
var max = 1000
var duplex = new Duplex({
read: function (n) {},
write: function (chunk, enc, cb) {
parser.parse(chunk)
cb() // nothing to do
}
})
var client = new mqtt.MqttClient(function () {
return duplex
}, {})
client.on('message', function (t, p, packet) {
if (++count === max) {
done()
}
})
parser.on('packet', function (packet) {
var packets = []
if (packet.cmd === 'connect') {
duplex.push(mqttPacket.generate({
cmd: 'connack',
sessionPresent: false,
returnCode: 0
}))
for (var i = 0; i < max; i++) {
packets.push(mqttPacket.generate({
cmd: 'publish',
topic: Buffer.from('hello'),
payload: Buffer.from('world'),
retain: false,
dup: false,
messageId: i + 1,
qos: 1
}))
}
duplex.push(Buffer.concat(packets))
}
})
})
})
describe('flushing', function () {
it('should attempt to complete pending unsub and send on ping timeout', function (done) {
this.timeout(10000)
var server3 = connOnlyServer().listen(port + 72)
var pubCallbackCalled = false
var unsubscribeCallbackCalled = false
var client = mqtt.connect({
port: port + 72,
host: 'localhost',
keepalive: 1,
connectTimeout: 350,
reconnectPeriod: 0
})
client.once('connect', () => {
client.publish('fakeTopic', 'fakeMessage', {qos: 1}, (err, result) => {
should.exist(err)
pubCallbackCalled = true
})
client.unsubscribe('fakeTopic', (err, result) => {
should.exist(err)
unsubscribeCallbackCalled = true
})
setTimeout(() => {
client.end(() => {
should.equal(pubCallbackCalled && unsubscribeCallbackCalled, true, 'callbacks not invoked')
server3.close()
done()
})
}, 5000)
})
})
})
describe('reconnecting', function () {
it('should attempt to reconnect once server is down', function (done) {
this.timeout(15000)
var innerServer = fork(path.join(__dirname, 'helpers', 'server_process.js'))
var client = mqtt.connect({ port: 3000, host: 'localhost', keepalive: 1 })
client.once('connect', function () {
innerServer.kill('SIGINT') // mocks server shutdown
client.once('close', function () {
should.exist(client.reconnectTimer)
client.end()
done()
})
})
})
it('should reconnect to multiple host-ports combination if servers is passed', function (done) {
this.timeout(15000)
var server2 = buildServer().listen(port + 42)
server2.on('client', function (c) {
c.stream.destroy()
server2.close()
})
server2.on('listening', function () {
var client = mqtt.connect({
servers: [
{ port: port + 42, host: 'localhost' },
{ port: port, host: 'localhost' }
],
keepalive: 50
})
server.once('client', function () {
client.end()
done()
})
client.once('connect', function () {
client.stream.destroy()
})
})
})
it('should reconnect if a connack is not received in an interval', function (done) {
this.timeout(2000)
var server2 = net.createServer().listen(port + 43)
server2.on('connection', function (c) {
eos(c, function () {
server2.close()
})
})
server2.on('listening', function () {
var client = mqtt.connect({
servers: [
{ port: port + 43, host: 'localhost_fake' },
{ port: port, host: 'localhost' }
],
connectTimeout: 500
})
server.once('client', function () {
client.end()
done()
})
client.once('connect', function () {
client.stream.destroy()
})
})
})
it('should not be cleared by the connack timer', function (done) {
this.timeout(4000)
var server2 = net.createServer().listen(port + 44)
server2.on('connection', function (c) {
c.destroy()
})
server2.once('listening', function () {
var reconnects = 0
var connectTimeout = 1000
var reconnectPeriod = 100
var expectedReconnects = Math.floor(connectTimeout / reconnectPeriod)
var client = mqtt.connect({
port: port + 44,
host: 'localhost',
connectTimeout: connectTimeout,
reconnectPeriod: reconnectPeriod
})
client.on('reconnect', function () {
reconnects++
if (reconnects >= expectedReconnects) {
client.end()
done()
}
})
})
})
it('should not keep requeueing the first message when offline', function (done) {
this.timeout(2500)
var server2 = buildServer().listen(port + 45)
var client = mqtt.connect({
port: port + 45,
host: 'localhost',
connectTimeout: 350,
reconnectPeriod: 300
})
server2.on('client', function (c) {
client.publish('hello', 'world', { qos: 1 }, function () {
c.destroy()
server2.close()
client.publish('hello', 'world', { qos: 1 })
})
})
setTimeout(function () {
if (client.queue.length === 0) {
client.end(true)
done()
} else {
client.end(true)
}
}, 2000)
})
it('should not send the same subscribe multiple times on a flaky connection', function (done) {
this.timeout(3500)
var KILL_COUNT = 4
var killedConnections = 0
var subIds = {}
var client = mqtt.connect({
port: port + 46,
host: 'localhost',
connectTimeout: 350,
reconnectPeriod: 300
})
var server2 = new Server(function (client) {
client.on('error', function () {})
client.on('connect', function (packet) {
if (packet.clientId === 'invalid') {
client.connack({returnCode: 2})
} else {
client.connack({returnCode: 0})
}
})
}).listen(port + 46)
server2.on('client', function (c) {
client.subscribe('topic', function () {
done()
client.end()
c.destroy()
server2.close()
})
c.on('subscribe', function (packet) {
if (killedConnections < KILL_COUNT) {
// Kill the first few sub attempts to simulate a flaky connection
killedConnections++
c.destroy()
} else {
// Keep track of acks
if (!subIds[packet.messageId]) {
subIds[packet.messageId] = 0
}
subIds[packet.messageId]++
if (subIds[packet.messageId] > 1) {
done(new Error('Multiple duplicate acked subscriptions received for messageId ' + packet.messageId))
client.end(true)
c.destroy()
server2.destroy()
}
c.suback({
messageId: packet.messageId,
granted: packet.subscriptions.map(function (e) {
return e.qos
})
})
}
})
})
})
it('should not fill the queue of subscribes if it cannot connect', function (done) {
this.timeout(2500)
var port2 = port + 48
var server2 = net.createServer(function (stream) {
var client = new Connection(stream)
client.on('error', function () {})
client.on('connect', function (packet) {
client.connack({returnCode: 0})
client.destroy()
})
})
server2.listen(port2, function () {
var client = mqtt.connect({
port: port2,
host: 'localhost',
connectTimeout: 350,
reconnectPeriod: 300
})
client.subscribe('hello')
setTimeout(function () {
client.queue.length.should.equal(1)
client.end()
done()
}, 1000)
})
})
it('should not send the same publish multiple times on a flaky connection', function (done) {
this.timeout(3500)
var KILL_COUNT = 4
var killedConnections = 0
var pubIds = {}
var client = mqtt.connect({
port: port + 47,
host: 'localhost',
connectTimeout: 350,
reconnectPeriod: 300
})
var server2 = net.createServer(function (stream) {
var client = new Connection(stream)
client.on('error', function () {})
client.on('connect', function (packet) {
if (packet.clientId === 'invalid') {
client.connack({returnCode: 2})
} else {
client.connack({returnCode: 0})
}
})
this.emit('client', client)
}).listen(port + 47)
server2.on('client', function (c) {
client.publish('topic', 'data', { qos: 1 }, function () {
done()
client.end()
c.destroy()
server2.destroy()
})
c.on('publish', function onPublish (packet) {
if (killedConnections < KILL_COUNT) {
// Kill the first few pub attempts to simulate a flaky connection
killedConnections++
c.destroy()
// to avoid receiving inflight messages
c.removeListener('publish', onPublish)
} else {
// Keep track of acks
if (!pubIds[packet.messageId]) {
pubIds[packet.messageId] = 0
}
pubIds[packet.messageId]++
if (pubIds[packet.messageId] > 1) {
done(new Error('Multiple duplicate acked publishes received for messageId ' + packet.messageId))
client.end(true)
c.destroy()
server2.destroy()
}
c.puback(packet)
}
})
})
})
})
})

16
node_modules/mqtt/test/helpers/private-csr.pem generated vendored Normal file
View File

@ -0,0 +1,16 @@
-----BEGIN CERTIFICATE REQUEST-----
MIICijCCAXICAQAwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUx
ITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcN
AQEBBQADggEPADCCAQoCggEBANtzIZmqf7h3axO9mzo2VhiF/BF3Y4E/fDTkFy27
PgssS/ipFOMe/IxyM/hA/o/bQb0BY+sH5s1020kNH79umtabWMaDmOi8bvmHWtVC
cYhn3mhbRFWcORdTnfQ8uRYXZGeoupjlhfrKkQCoSAFKh1OzU7aNx4CjMAjSa4py
trMAVNJ37RryhsfMuHAeG8+0Eo3qmYyaplpurtr8A3HWV65R2VFCwZ5hKG8I9X2F
3UrYKHr4xlxOgjD8j2OfYZxpGHI6YexJ28aR0xlsWfzS+TKKFVxy8ntgPGL0ZXL3
vss80mAcBl9FfsJzufn4IHOYspX1OEM0M7plMmQw/yNT9B8CAwEAAaAAMA0GCSqG
SIb3DQEBBQUAA4IBAQBsONiE5HTjfR1pDrWPIhbLqMO3AqmuB5AwpQm8kAaM2Oz1
DI/a8bHYyODMiyWUPTtwLMQWcJpAG2ZhE18gLqFwXZR1XSOxY1yF+uZ7Ls3hwzbq
9A6O254B5wXBnXkVbzZwFshV5HWiZwVivF5GDyLRsMoS2EtUHoDEP4YIRK0kPL9H
m3BB334KlWTc8NNXFFG62OL7q2fa8xRHlN8SYfeUjy79eEoBdHv5wL/ZN/YBCDNJ
2zrYUvbOmfoq1e+6AczZ6xAHHeneUQuaOF225aMwHHZTiP2TlIeFXwBvzV1BWIJv
dOaHX/f3NamKoGvwYyIR1FrI2FpXTJLRE/eu7TFD
-----END CERTIFICATE REQUEST-----

27
node_modules/mqtt/test/helpers/private-key.pem generated vendored Normal file
View File

@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA23Mhmap/uHdrE72bOjZWGIX8EXdjgT98NOQXLbs+CyxL+KkU
4x78jHIz+ED+j9tBvQFj6wfmzXTbSQ0fv26a1ptYxoOY6Lxu+Yda1UJxiGfeaFtE
VZw5F1Od9Dy5FhdkZ6i6mOWF+sqRAKhIAUqHU7NTto3HgKMwCNJrinK2swBU0nft
GvKGx8y4cB4bz7QSjeqZjJqmWm6u2vwDcdZXrlHZUULBnmEobwj1fYXdStgoevjG
XE6CMPyPY59hnGkYcjph7EnbxpHTGWxZ/NL5MooVXHLye2A8YvRlcve+yzzSYBwG
X0V+wnO5+fggc5iylfU4QzQzumUyZDD/I1P0HwIDAQABAoIBAQDNgNdqS5wnZs1D
Qz/mF5QwiugugxsPoh/yd9as4LeNRwIt7ki9F/twmlHInTTGCpFZKcAkDNY6eMAR
fNTKNA2UAw3zeLDs4ekai4KoSvx+vKYuG6m2cgGUsp0sZuD8qxM/b2auX+JDpQZ9
Exm6+8wWucwfHE5DTI5i9In4sMweeuiEUYnndTzElkvnP/44h1fGSU1iGUKn/ftc
P4X+3SU68KMT3kUsEBavtmSdyeG/lSFEjm73FwVIRZ+PfbQX2hDD+mmseAXGFKi1
HudtQkEzTvYR+QAgvtjNgt/0qxFtPdj7Y+iRkCZQSJToAw8z6vwUn1qNCADauGMI
X6KIm8XBAoGBAPiwMLYpIqp1rksINbqpbVqjtqsoejQuPYeEF7OXHbH9il7pWrQF
wLbogo3YXX+a66RreVMhsUeq7+pIf/sK2lT73gDpFfvZnJG1ww94QkHBEPso0bN9
pcGgceIK7KRRAiAl5Mjw6pZZNnIBxlIFaSbBqQau74NfdaalMBF2wi+3AoGBAOHm
3ttFtVjVlb2fHoiGNZCZDv3gnsQXZlCxS+rQ4XEmEWKHAH4T3+Kzmo8jWoX+DGGD
6UkxWHv7e+KrYIZDi7Dd2HFV0gHN6d1SNdPix3vN114bNOrbfqxuEVT5PdFHSuel
5d3ix+3U+tpHamwb88eyeq6Q3t5Lcl3gIRGLzo7ZAoGBAKVuLzk+K/1Qw1zOXU+K
nWAKP92j04caq3uWd13UTMC2dHGmsdvHZ+dEzHQnVisol1CM3exbIV8XavliuR/6
nDqkQY5Bf4pFvE2Bp/yGdyzejblF8hmAn98qKBfCRKEZ8lwIWSUCfkr9laZJX+/4
AXbypMn5XQL7YXw1rsAvTAYJAoGAV4ZL8kkf6jtWuRFdkyfsuQmUdWkCGpe2XK1U
7LXhoyVMtw/3cOHibMOJrsvT1vaHdYDWcjVcQy084qXj0CF7jhtmMQM/StOtOMMR
d/b1s1Idj6ia6CQDAGvk6zdmbB9jNj1gwoeLTuqmBsyEvz5VRZoxTlFzCE3TEew0
48d3UIECgYBMxnLByVQA3pQWWIZZyqt+HgJAphYPdpnPalblQAbuCksKTZ/QKDkW
dzih1PQROVrYrX7VwJ3/I8gXIuvKVtN1NKOS3a0JtbJQhpH4YbRwyQskXWYP8oYa
MjBGPymNDhZh0zoGWzst5uR3NpdNV+7yNYPvyxzVNjlPjtAUqIxjBg==
-----END RSA PRIVATE KEY-----

19
node_modules/mqtt/test/helpers/public-cert.pem generated vendored Normal file
View File

@ -0,0 +1,19 @@
-----BEGIN CERTIFICATE-----
MIIDBjCCAe4CCQDkrq1PMPtmfzANBgkqhkiG9w0BAQUFADBFMQswCQYDVQQGEwJB
VTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0
cyBQdHkgTHRkMB4XDTEzMDEyNTEwMzEyOVoXDTEzMDIyNDEwMzEyOVowRTELMAkG
A1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0
IFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB
ANtzIZmqf7h3axO9mzo2VhiF/BF3Y4E/fDTkFy27PgssS/ipFOMe/IxyM/hA/o/b
Qb0BY+sH5s1020kNH79umtabWMaDmOi8bvmHWtVCcYhn3mhbRFWcORdTnfQ8uRYX
ZGeoupjlhfrKkQCoSAFKh1OzU7aNx4CjMAjSa4pytrMAVNJ37RryhsfMuHAeG8+0
Eo3qmYyaplpurtr8A3HWV65R2VFCwZ5hKG8I9X2F3UrYKHr4xlxOgjD8j2OfYZxp
GHI6YexJ28aR0xlsWfzS+TKKFVxy8ntgPGL0ZXL3vss80mAcBl9FfsJzufn4IHOY
spX1OEM0M7plMmQw/yNT9B8CAwEAATANBgkqhkiG9w0BAQUFAAOCAQEAeHAwoKYl
6g9lUEwBDqm6ZxjgoYQi6V3loCjBcTr5OrMkLvvZrA55xsse0NRH40I/pvCaAZAZ
EEna0fr5GPYi+y+I8EoU2W/+ehSqRAU8Fkdm0eR5MjyLWYOwd3ClUND8EpUNNSKH
Xw9k9EQmyKsDxVsKWoJoO9rfFkUjooz07jGPCud18QCBs5i5ThbnQ9UP+26D8z5k
1Dii69LIcLXA3Vtm6R5fT57zNusfx8bqA9yy7UThYaXIazNMWNxiJRXfv0J4zFdD
RQ+SFdJ3p5jurPkc3oRWWPbn/Lpf0E5XlYTJImXT1WmWnQSaNtME4P+3kEL5x+v/
u8zTLbobG4x0rQ==
-----END CERTIFICATE-----

9
node_modules/mqtt/test/helpers/public-key.pem generated vendored Normal file
View File

@ -0,0 +1,9 @@
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA23Mhmap/uHdrE72bOjZW
GIX8EXdjgT98NOQXLbs+CyxL+KkU4x78jHIz+ED+j9tBvQFj6wfmzXTbSQ0fv26a
1ptYxoOY6Lxu+Yda1UJxiGfeaFtEVZw5F1Od9Dy5FhdkZ6i6mOWF+sqRAKhIAUqH
U7NTto3HgKMwCNJrinK2swBU0nftGvKGx8y4cB4bz7QSjeqZjJqmWm6u2vwDcdZX
rlHZUULBnmEobwj1fYXdStgoevjGXE6CMPyPY59hnGkYcjph7EnbxpHTGWxZ/NL5
MooVXHLye2A8YvRlcve+yzzSYBwGX0V+wnO5+fggc5iylfU4QzQzumUyZDD/I1P0
HwIDAQAB
-----END PUBLIC KEY-----

52
node_modules/mqtt/test/helpers/server.js generated vendored Normal file
View File

@ -0,0 +1,52 @@
'use strict'
var Server = require('../server')
var fs = require('fs')
module.exports.init_server = function (PORT) {
var server = new Server(function (client) {
client.on('connect', function () {
client.connack(0)
})
client.on('publish', function (packet) {
switch (packet.qos) {
case 1:
client.puback({messageId: packet.messageId})
break
case 2:
client.pubrec({messageId: packet.messageId})
break
default:
break
}
})
client.on('pubrel', function (packet) {
client.pubcomp({messageId: packet.messageId})
})
client.on('pingreq', function () {
client.pingresp()
})
client.on('disconnect', function () {
client.stream.end()
})
})
server.listen(PORT)
return server
}
module.exports.init_secure_server = function (port, key, cert) {
var server = new Server.SecureServer({
key: fs.readFileSync(key),
cert: fs.readFileSync(cert)
}, function (client) {
client.on('connect', function () {
client.connack({returnCode: 0})
})
})
server.listen(port)
return server
}

9
node_modules/mqtt/test/helpers/server_process.js generated vendored Normal file
View File

@ -0,0 +1,9 @@
'use strict'
var Server = require('../server')
new Server(function (client) {
client.on('connect', function () {
client.connack({ returnCode: 0 })
})
}).listen(3000, 'localhost')

14
node_modules/mqtt/test/helpers/tls-cert.pem generated vendored Normal file
View File

@ -0,0 +1,14 @@
-----BEGIN CERTIFICATE-----
MIICKTCCAZICCQDRSYqWgZyJmjANBgkqhkiG9w0BAQUFADBZMQswCQYDVQQGEwJB
VTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0
cyBQdHkgTHRkMRIwEAYDVQQDEwlsb2NhbGhvc3QwHhcNMTQwNjEzMTAwMzAzWhcN
MjQwNjEwMTAwMzAzWjBZMQswCQYDVQQGEwJBVTETMBEGA1UECBMKU29tZS1TdGF0
ZTEhMB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMRIwEAYDVQQDEwls
b2NhbGhvc3QwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMzFv8+9EBb1sG07
TjdtbRksRwF7/CZsOWe+ef4ZYPolC5lzvNVYXsBIjL+ilhyKopBbwnOuX9+6FmYO
G/N1lDZRssolGoOVM+1ma3Whmxz8C1g+xi95nP2OqtwP5Du6xhvOM265CiMaf8DH
n63ZFxyi3d1CdNGamNQvrybCzJn7AgMBAAEwDQYJKoZIhvcNAQEFBQADgYEABmyp
3wyBGjb2zSHK5pF9c9GXyHRL4/FkP6qzU5NWrfVAowdOczctJbc3hxPh34Encbr6
KijYnbdP7/f8aZrStLGqgFYL3SHZY3zvgLTzOmGr9reHUkubHtN+mWHeYy1wVe3D
qEOI8ygT4olVZmWAD+VLKgAb0J07rA/PKf82fBI=
-----END CERTIFICATE-----

11
node_modules/mqtt/test/helpers/tls-csr.pem generated vendored Normal file
View File

@ -0,0 +1,11 @@
-----BEGIN CERTIFICATE REQUEST-----
MIIBmTCCAQICAQAwWTELMAkGA1UEBhMCQVUxEzARBgNVBAgTClNvbWUtU3RhdGUx
ITAfBgNVBAoTGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDESMBAGA1UEAxMJbG9j
YWxob3N0MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDMxb/PvRAW9bBtO043
bW0ZLEcBe/wmbDlnvnn+GWD6JQuZc7zVWF7ASIy/opYciqKQW8Jzrl/fuhZmDhvz
dZQ2UbLKJRqDlTPtZmt1oZsc/AtYPsYveZz9jqrcD+Q7usYbzjNuuQojGn/Ax5+t
2Rccot3dQnTRmpjUL68mwsyZ+wIDAQABoAAwDQYJKoZIhvcNAQEFBQADgYEALjPb
zOEL8ahD+UFxwVCXTq4MsKwMlyZCcEVY0CksAgWpCkWr54JUp832p3nEylPRj/gx
8fKWzz5DiO3RER8fzmkb+Kwa+JvXVHmTFzemxYGnxS/HRlF0ZoeAIgvq6ouIrqm9
1P9gsuYmA5vtfc6Y/NVlSrcSYFH4ADF5DcRTi2Q=
-----END CERTIFICATE REQUEST-----

15
node_modules/mqtt/test/helpers/tls-key.pem generated vendored Normal file
View File

@ -0,0 +1,15 @@
-----BEGIN RSA PRIVATE KEY-----
MIICWwIBAAKBgQDMxb/PvRAW9bBtO043bW0ZLEcBe/wmbDlnvnn+GWD6JQuZc7zV
WF7ASIy/opYciqKQW8Jzrl/fuhZmDhvzdZQ2UbLKJRqDlTPtZmt1oZsc/AtYPsYv
eZz9jqrcD+Q7usYbzjNuuQojGn/Ax5+t2Rccot3dQnTRmpjUL68mwsyZ+wIDAQAB
AoGARg7p/xL6LEDGqbh+nCwOBWzGplVbAXJJeZsLdcoNCcge3dNhKcTgNf0cWnwv
y3gLAkTClH12Q78Q5r2xBmyV1hqyEb9lrIqAlSS5GjnTWWhyzspcjKZWR5PAjOYo
LlxNpCegWEjOUpD4Lwf9yjEu+xrDGVmsLF0PPRkAM32qh9ECQQD1vzyFr/hSn7Rh
6IFFbLAVkIvsy+1Ca7tF6/7byHCdwqS5oUKaY+9DAr0TE+br87N2IzUCU5X7Cv74
m+YiqhBlAkEA1VDfpq8puyIq2F6Ftx0xpYMv6XKhuRyAziT/DzIBdFVeOMIgUuk0
7E4W0N/gDmUmEQFl3HYzUfdZrTUKzjzq3wJAZflsKOGDfu2skXBErEVUsC4iEinx
Ez3XIUWzpQoAyUYqyqjDFYPglgL96Hu6uDCRSLWFWqjKtLi0Yv92OO4vDQJASuAk
YQHDCCiqGWC0Vt4sewhdXPgbxDo5DCL4VIEc+ZStiga6CeBJ71hJse+jWeovPnDb
LFNhGDhWhfHEZTgEyQJAXNuypDS5l73LPvc+yduPZiNEtwae9KbWaZUwC683a81s
mkT7uroNYyK9ptZrz/LMJJotkqCjigXaA3kuzuNUCQ==
-----END RSA PRIVATE KEY-----

13
node_modules/mqtt/test/helpers/wrong-cert.pem generated vendored Normal file
View File

@ -0,0 +1,13 @@
-----BEGIN CERTIFICATE-----
MIICATCCAWoCCQDEVSSDKkcTdjANBgkqhkiG9w0BAQUFADBFMQswCQYDVQQGEwJB
VTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0
cyBQdHkgTHRkMB4XDTE0MDUxMTE2MzMxMVoXDTE0MDYxMDE2MzMxMVowRTELMAkG
A1UEBhMCQVUxEzARBgNVBAgTClNvbWUtU3RhdGUxITAfBgNVBAoTGEludGVybmV0
IFdpZGdpdHMgUHR5IEx0ZDCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAyDMI
VS2XSizZT8KeFYFQfKt7CcT5/Pkzw2BDJoMVLmrkfHdddjsTgswqHfzhO8Fmfg6B
MxgsEz2aKi24hJxQFuQ1DGhyfKHnjxM5PqSLiOkQDKllnAOgqOBDXpca0jXypCk1
IVhMspM2ylrnBXps3nTBLJxFBkZSBov/JDkkL+cCAwEAATANBgkqhkiG9w0BAQUF
AAOBgQA8k93U0VDIpQ8lpScxrCtEu5jLZgB1fw0fdCUtDHaaM1v+LWr1xfCmFKyT
kUMcJl4e1pkcSNfXcI7LdNt8EJqMabOi2UpW1+VZJn206D0f3XmNSmZbk8oozGrl
qg2wSTZYlZClCTpWO2Y+iYzojY8kmLaQ2xbTxBz1XlshC8HvsA==
-----END CERTIFICATE-----

11
node_modules/mqtt/test/helpers/wrong-csr.pem generated vendored Normal file
View File

@ -0,0 +1,11 @@
-----BEGIN CERTIFICATE REQUEST-----
MIIBhDCB7gIBADBFMQswCQYDVQQGEwJBVTETMBEGA1UECBMKU29tZS1TdGF0ZTEh
MB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIGfMA0GCSqGSIb3DQEB
AQUAA4GNADCBiQKBgQDIMwhVLZdKLNlPwp4VgVB8q3sJxPn8+TPDYEMmgxUuauR8
d112OxOCzCod/OE7wWZ+DoEzGCwTPZoqLbiEnFAW5DUMaHJ8oeePEzk+pIuI6RAM
qWWcA6Co4ENelxrSNfKkKTUhWEyykzbKWucFemzedMEsnEUGRlIGi/8kOSQv5wID
AQABoAAwDQYJKoZIhvcNAQEFBQADgYEAFXqd8jhW+2hRvkRB1CCVBK5e6AQHq1rF
s3B36O64hRHIr1KC+dWr8vv1t9Rkud+7E3ELHtxWCORIYpqQ2Ddldt4PP+MTNj2C
qgwOpxM0VDxeeWml8fqx2uzfPhVduyHGm0yff2JS2KRVmnIPLTUuz/+udukIFDVO
Sc4/W3qY7f8=
-----END CERTIFICATE REQUEST-----

15
node_modules/mqtt/test/helpers/wrong-key.pem generated vendored Normal file
View File

@ -0,0 +1,15 @@
-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQDIMwhVLZdKLNlPwp4VgVB8q3sJxPn8+TPDYEMmgxUuauR8d112
OxOCzCod/OE7wWZ+DoEzGCwTPZoqLbiEnFAW5DUMaHJ8oeePEzk+pIuI6RAMqWWc
A6Co4ENelxrSNfKkKTUhWEyykzbKWucFemzedMEsnEUGRlIGi/8kOSQv5wIDAQAB
AoGBALOzszgaG2I2jb4dmJ7/G4s8tc2YJTlhS4iFgOEx6rJmur/KuXcmIiZXMzsF
wftMZ76hMHH3saB3vEk+DxHh6bR6cW/I82Vxts9suz2fRnd2mh5JHI+opXE53LVn
hJcQ4k6LJ9MNVxlHwCTrSuJvtikDOOrCARHRvYzFRL4wXvmpAkEA+DFzXGDg8PxX
RFp6RLLbqaUT6YXNi+E5ERuumru+rgRj+OF/dxNK3d1lcIJZjqVMDAgOsZ66/bkh
GfCzJPREUwJBAM5/HeHmTEM5K5B0X8b6XEHTgWFUNTu4K36Ee5ySd8RYI8zjQ9wS
NM1nXnx12npL7DSkShz9xgnTe0f8YmQnc50CQQCgdE/RXCxwf6LnZNsBCOSsIzXh
VgiRsxSSs+PI0zGuDNaY8yfV0ponH1fSSeMeLk0gxiDBwg2/tGzq+UrHzEdTAkB1
/U5O0K+MzbLlxIkhgdaLSlYoDdyo9e/sR7j12v8SMqaqIMWajtCa+VCU3yZqMM2T
urgaXqr03GEZ3c0+mwhFAkAwWkczV1iwuedmWLKc36iQhoj+FRMUoxWe/fBixQls
g0lDvwWiZ3M6hjCsBRckmt8eU2mUh79Odrj5fRWIwXaX
-----END RSA PRIVATE KEY-----

4
node_modules/mqtt/test/mocha.opts generated vendored Normal file
View File

@ -0,0 +1,4 @@
--check-leaks
--timeout 5000
--exit

230
node_modules/mqtt/test/mqtt.js generated vendored Normal file
View File

@ -0,0 +1,230 @@
'use strict'
var fs = require('fs')
var path = require('path')
var mqtt = require('../')
describe('mqtt', function () {
describe('#connect', function () {
var sslOpts, sslOpts2
it('should return an MqttClient when connect is called with mqtt:/ url', function () {
var c = mqtt.connect('mqtt://localhost:1883')
c.should.be.instanceOf(mqtt.MqttClient)
c.end()
})
it('should throw an error when called with no protocol specified', function () {
(function () {
var c = mqtt.connect('foo.bar.com')
c.end()
}).should.throw('Missing protocol')
})
it('should throw an error when called with no protocol specified - with options', function () {
(function () {
var c = mqtt.connect('tcp://foo.bar.com', { protocol: null })
c.end()
}).should.throw('Missing protocol')
})
it('should return an MqttClient with username option set', function () {
var c = mqtt.connect('mqtt://user:pass@localhost:1883')
c.should.be.instanceOf(mqtt.MqttClient)
c.options.should.have.property('username', 'user')
c.options.should.have.property('password', 'pass')
c.end()
})
it('should return an MqttClient with username and password options set', function () {
var c = mqtt.connect('mqtt://user@localhost:1883')
c.should.be.instanceOf(mqtt.MqttClient)
c.options.should.have.property('username', 'user')
c.end()
})
it('should return an MqttClient with the clientid with random value', function () {
var c = mqtt.connect('mqtt://user@localhost:1883')
c.should.be.instanceOf(mqtt.MqttClient)
c.options.should.have.property('clientId')
c.end()
})
it('should return an MqttClient with the clientid with empty string', function () {
var c = mqtt.connect('mqtt://user@localhost:1883?clientId=')
c.should.be.instanceOf(mqtt.MqttClient)
c.options.should.have.property('clientId', '')
c.end()
})
it('should return an MqttClient with the clientid option set', function () {
var c = mqtt.connect('mqtt://user@localhost:1883?clientId=123')
c.should.be.instanceOf(mqtt.MqttClient)
c.options.should.have.property('clientId', '123')
c.end()
})
it('should return an MqttClient when connect is called with tcp:/ url', function () {
var c = mqtt.connect('tcp://localhost')
c.should.be.instanceOf(mqtt.MqttClient)
c.end()
})
it('should return an MqttClient with correct host when called with a host and port', function () {
var c = mqtt.connect('tcp://user:pass@localhost:1883')
c.options.should.have.property('hostname', 'localhost')
c.options.should.have.property('port', 1883)
c.end()
})
sslOpts = {
keyPath: path.join(__dirname, 'helpers', 'private-key.pem'),
certPath: path.join(__dirname, 'helpers', 'public-cert.pem'),
caPaths: [path.join(__dirname, 'helpers', 'public-cert.pem')]
}
it('should return an MqttClient when connect is called with mqtts:/ url', function () {
var c = mqtt.connect('mqtts://localhost', sslOpts)
c.options.should.have.property('protocol', 'mqtts')
c.on('error', function () {})
c.should.be.instanceOf(mqtt.MqttClient)
c.end()
})
it('should return an MqttClient when connect is called with ssl:/ url', function () {
var c = mqtt.connect('ssl://localhost', sslOpts)
c.options.should.have.property('protocol', 'ssl')
c.on('error', function () {})
c.should.be.instanceOf(mqtt.MqttClient)
c.end()
})
it('should return an MqttClient when connect is called with ws:/ url', function () {
var c = mqtt.connect('ws://localhost', sslOpts)
c.options.should.have.property('protocol', 'ws')
c.on('error', function () {})
c.should.be.instanceOf(mqtt.MqttClient)
c.end()
})
it('should return an MqttClient when connect is called with wss:/ url', function () {
var c = mqtt.connect('wss://localhost', sslOpts)
c.options.should.have.property('protocol', 'wss')
c.on('error', function () {})
c.should.be.instanceOf(mqtt.MqttClient)
c.end()
})
sslOpts2 = {
key: fs.readFileSync(path.join(__dirname, 'helpers', 'private-key.pem')),
cert: fs.readFileSync(path.join(__dirname, 'helpers', 'public-cert.pem')),
ca: [fs.readFileSync(path.join(__dirname, 'helpers', 'public-cert.pem'))]
}
it('should throw an error when it is called with cert and key set but no protocol specified', function () {
// to do rewrite wrap function
(function () {
var c = mqtt.connect(sslOpts2)
c.end()
}).should.throw('Missing secure protocol key')
})
it('should throw an error when it is called with cert and key set and protocol other than allowed: mqtt,mqtts,ws,wss,wxs', function () {
(function () {
sslOpts2.protocol = 'UNKNOWNPROTOCOL'
var c = mqtt.connect(sslOpts2)
c.end()
}).should.throw()
})
it('should return a MqttClient with mqtts set when connect is called key and cert set and protocol mqtt', function () {
sslOpts2.protocol = 'mqtt'
var c = mqtt.connect(sslOpts2)
c.options.should.have.property('protocol', 'mqtts')
c.on('error', function () {})
c.should.be.instanceOf(mqtt.MqttClient)
})
it('should return a MqttClient with mqtts set when connect is called key and cert set and protocol mqtts', function () {
sslOpts2.protocol = 'mqtts'
var c = mqtt.connect(sslOpts2)
c.options.should.have.property('protocol', 'mqtts')
c.on('error', function () {})
c.should.be.instanceOf(mqtt.MqttClient)
})
it('should return a MqttClient with wss set when connect is called key and cert set and protocol ws', function () {
sslOpts2.protocol = 'ws'
var c = mqtt.connect(sslOpts2)
c.options.should.have.property('protocol', 'wss')
c.on('error', function () {})
c.should.be.instanceOf(mqtt.MqttClient)
})
it('should return a MqttClient with wss set when connect is called key and cert set and protocol wss', function () {
sslOpts2.protocol = 'wss'
var c = mqtt.connect(sslOpts2)
c.options.should.have.property('protocol', 'wss')
c.on('error', function () {})
c.should.be.instanceOf(mqtt.MqttClient)
})
it('should return an MqttClient with the clientid with option of clientId as empty string', function () {
var c = mqtt.connect('mqtt://localhost:1883', {
clientId: ''
})
c.should.be.instanceOf(mqtt.MqttClient)
c.options.should.have.property('clientId', '')
})
it('should return an MqttClient with the clientid with option of clientId empty', function () {
var c = mqtt.connect('mqtt://localhost:1883')
c.should.be.instanceOf(mqtt.MqttClient)
c.options.should.have.property('clientId')
c.end()
})
it('should return an MqttClient with the clientid with option of with specific clientId', function () {
var c = mqtt.connect('mqtt://localhost:1883', {
clientId: '123'
})
c.should.be.instanceOf(mqtt.MqttClient)
c.options.should.have.property('clientId', '123')
c.end()
})
})
})

9
node_modules/mqtt/test/mqtt_store.js generated vendored Normal file
View File

@ -0,0 +1,9 @@
'use strict'
var mqtt = require('../lib/connect')
describe('store in lib/connect/index.js (webpack entry point)', function () {
it('should create store', function (done) {
done(null, new mqtt.Store())
})
})

157
node_modules/mqtt/test/secure_client.js generated vendored Normal file
View File

@ -0,0 +1,157 @@
'use strict'
var mqtt = require('..')
var path = require('path')
var abstractClientTests = require('./abstract_client')
var fs = require('fs')
var port = 9899
var KEY = path.join(__dirname, 'helpers', 'tls-key.pem')
var CERT = path.join(__dirname, 'helpers', 'tls-cert.pem')
var WRONG_CERT = path.join(__dirname, 'helpers', 'wrong-cert.pem')
var Server = require('./server')
var server = new Server.SecureServer({
key: fs.readFileSync(KEY),
cert: fs.readFileSync(CERT)
}, function (client) {
client.on('connect', function (packet) {
if (packet.clientId === 'invalid') {
client.connack({returnCode: 2})
} else {
server.emit('connect', client)
client.connack({returnCode: 0})
}
})
client.on('publish', function (packet) {
setImmediate(function () {
/* jshint -W027 */
/* eslint default-case:0 */
switch (packet.qos) {
case 0:
break
case 1:
client.puback(packet)
break
case 2:
client.pubrec(packet)
break
}
/* jshint +W027 */
})
})
client.on('pubrel', function (packet) {
client.pubcomp(packet)
})
client.on('pubrec', function (packet) {
client.pubrel(packet)
})
client.on('pubcomp', function () {
// Nothing to be done
})
client.on('subscribe', function (packet) {
client.suback({
messageId: packet.messageId,
granted: packet.subscriptions.map(function (e) {
return e.qos
})
})
})
client.on('unsubscribe', function (packet) {
client.unsuback(packet)
})
client.on('pingreq', function () {
client.pingresp()
})
}).listen(port)
describe('MqttSecureClient', function () {
var config = { protocol: 'mqtts', port: port, rejectUnauthorized: false }
abstractClientTests(server, config)
describe('with secure parameters', function () {
it('should validate successfully the CA', function (done) {
var client = mqtt.connect({
protocol: 'mqtts',
port: port,
ca: [fs.readFileSync(CERT)],
rejectUnauthorized: true
})
client.on('error', function (err) {
done(err)
})
server.once('connect', function () {
done()
})
})
it('should validate successfully the CA using URI', function (done) {
var client = mqtt.connect('mqtts://localhost:' + port, {
ca: [fs.readFileSync(CERT)],
rejectUnauthorized: true
})
client.on('error', function (err) {
done(err)
})
server.once('connect', function () {
done()
})
})
it('should validate successfully the CA using URI with path', function (done) {
var client = mqtt.connect('mqtts://localhost:' + port + '/', {
ca: [fs.readFileSync(CERT)],
rejectUnauthorized: true
})
client.on('error', function (err) {
done(err)
})
server.once('connect', function () {
done()
})
})
it('should validate unsuccessfully the CA', function (done) {
var client = mqtt.connect({
protocol: 'mqtts',
port: port,
ca: [fs.readFileSync(WRONG_CERT)],
rejectUnauthorized: true
})
client.once('error', function () {
done()
client.end()
client.on('error', function () {})
})
})
it('should emit close on TLS error', function (done) {
var client = mqtt.connect({
protocol: 'mqtts',
port: port,
ca: [fs.readFileSync(WRONG_CERT)],
rejectUnauthorized: true
})
client.on('error', function () {})
// TODO node v0.8.x emits multiple close events
client.once('close', function () {
done()
})
})
})
})

65
node_modules/mqtt/test/server.js generated vendored Normal file
View File

@ -0,0 +1,65 @@
'use strict'
var net = require('net')
var tls = require('tls')
var inherits = require('inherits')
var Connection = require('mqtt-connection')
var MqttServer
var MqttSecureServer
function setupConnection (duplex) {
var connection = new Connection(duplex)
this.emit('client', connection)
}
/*
* MqttServer
*
* @param {Function} listener - fired on client connection
*/
MqttServer = module.exports = function Server (listener) {
if (!(this instanceof Server)) {
return new Server(listener)
}
net.Server.call(this)
this.on('connection', setupConnection)
if (listener) {
this.on('client', listener)
}
return this
}
inherits(MqttServer, net.Server)
/**
* MqttSecureServer
*
* @param {Object} opts - server options
* @param {Function} listener
*/
MqttSecureServer = module.exports.SecureServer =
function SecureServer (opts, listener) {
if (!(this instanceof SecureServer)) {
return new SecureServer(opts, listener)
}
// new MqttSecureServer(function(){})
if (typeof opts === 'function') {
listener = opts
opts = {}
}
tls.Server.call(this, opts)
if (listener) {
this.on('client', listener)
}
this.on('secureConnection', setupConnection)
return this
}
inherits(MqttSecureServer, tls.Server)

10
node_modules/mqtt/test/store.js generated vendored Normal file
View File

@ -0,0 +1,10 @@
'use strict'
var Store = require('../lib/store')
var abstractTest = require('../test/abstract_store')
describe('in-memory store', function () {
abstractTest(function (done) {
done(null, new Store())
})
})

View File

@ -0,0 +1,24 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// relative path uses package.json {"types":"types/index.d.ts", ...}
var __1 = require("../..");
var BROKER = 'test.mosquitto.org';
var PAYLOAD = 'hello from TS';
var TOPIC = 'typescript-test-' + Math.random().toString(16).substr(2);
var opts = {};
console.log("connect(" + JSON.stringify(BROKER) + ")");
var client = __1.connect("mqtt://" + BROKER, opts);
client.subscribe((_a = {}, _a[TOPIC] = 2, _a), function (err, granted) {
granted.forEach(function (_a) {
var topic = _a.topic, qos = _a.qos;
console.log("subscribed to " + topic + " with qos=" + qos);
});
client.publish(TOPIC, PAYLOAD, { qos: 2 });
}).on('message', function (topic, payload) {
console.log("message from " + topic + ": " + payload);
client.end();
}).on('connect', function (packet) {
console.log('connected!', JSON.stringify(packet));
});
var _a;
//# sourceMappingURL=broker-connect-subscribe-and-publish.js.map

View File

@ -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"}

View File

@ -0,0 +1,22 @@
// relative path uses package.json {"types":"types/index.d.ts", ...}
import {IClientOptions, Client, connect, IConnackPacket} from '../..'
const BROKER = 'test.mosquitto.org'
const PAYLOAD = 'hello from TS'
const TOPIC = 'typescript-test-' + Math.random().toString(16).substr(2)
const opts: IClientOptions = {}
console.log(`connect(${JSON.stringify(BROKER)})`)
const client:Client = connect(`mqtt://${BROKER}`, opts)
client.subscribe({[TOPIC]: 2}, (err, granted) => {
granted.forEach(({topic, qos}) => {
console.log(`subscribed to ${topic} with qos=${qos}`)
})
client.publish(TOPIC, PAYLOAD, {qos: 2})
}).on('message', (topic: string, payload: Buffer) => {
console.log(`message from ${topic}: ${payload}`)
client.end()
}).on('connect', (packet: IConnackPacket) => {
console.log('connected!', JSON.stringify(packet))
})

14
node_modules/mqtt/test/typescript/tsconfig.json generated vendored Normal file
View File

@ -0,0 +1,14 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"moduleResolution": "node",
"noEmitOnError": true,
"noImplicitAny": true,
"alwaysStrict": true,
"strictNullChecks": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"sourceMap": true
}
}

13
node_modules/mqtt/test/util.js generated vendored Normal file
View File

@ -0,0 +1,13 @@
'use strict'
var through = require('through2')
module.exports.testStream = function () {
return through(function (buf, enc, cb) {
var that = this
setImmediate(function () {
that.push(buf)
cb()
})
})
}

144
node_modules/mqtt/test/websocket_client.js generated vendored Normal file
View File

@ -0,0 +1,144 @@
'use strict'
var http = require('http')
var websocket = require('websocket-stream')
var WebSocketServer = require('ws').Server
var Connection = require('mqtt-connection')
var abstractClientTests = require('./abstract_client')
var mqtt = require('../')
var xtend = require('xtend')
var assert = require('assert')
var port = 9999
var server = http.createServer()
function attachWebsocketServer (wsServer) {
var wss = new WebSocketServer({server: wsServer, perMessageDeflate: false})
wss.on('connection', function (ws) {
var stream = websocket(ws)
var connection = new Connection(stream)
wsServer.emit('client', connection)
stream.on('error', function () {})
connection.on('error', function () {})
})
return wsServer
}
attachWebsocketServer(server)
server.on('client', function (client) {
client.on('connect', function (packet) {
if (packet.clientId === 'invalid') {
client.connack({ returnCode: 2 })
} else {
server.emit('connect', client)
client.connack({returnCode: 0})
}
})
client.on('publish', function (packet) {
setImmediate(function () {
switch (packet.qos) {
case 0:
break
case 1:
client.puback(packet)
break
case 2:
client.pubrec(packet)
break
}
})
})
client.on('pubrel', function (packet) {
client.pubcomp(packet)
})
client.on('pubrec', function (packet) {
client.pubrel(packet)
})
client.on('pubcomp', function () {
// Nothing to be done
})
client.on('subscribe', function (packet) {
client.suback({
messageId: packet.messageId,
granted: packet.subscriptions.map(function (e) {
return e.qos
})
})
})
client.on('unsubscribe', function (packet) {
client.unsuback(packet)
})
client.on('pingreq', function () {
client.pingresp()
})
}).listen(port)
describe('Websocket Client', function () {
var baseConfig = { protocol: 'ws', port: port }
function makeOptions (custom) {
// xtend returns a new object. Does not mutate arguments
return xtend(baseConfig, custom || {})
}
it('should use mqtt as the protocol by default', function (done) {
server.once('client', function (client) {
client.stream.socket.protocol.should.equal('mqtt')
})
mqtt.connect(makeOptions()).on('connect', function () {
this.end(true, done)
})
})
it('should be able transform the url (for e.g. to sign it)', function (done) {
var baseUrl = 'ws://localhost:9999/mqtt'
var sig = '?AUTH=token'
var expected = baseUrl + sig
var actual
var opts = makeOptions({
path: '/mqtt',
transformWsUrl: function (url, opt, client) {
assert.equal(url, baseUrl)
assert.strictEqual(opt, opts)
assert.strictEqual(client.options, opts)
assert.strictEqual(typeof opt.transformWsUrl, 'function')
assert(client instanceof mqtt.MqttClient)
url += sig
actual = url
return url
}})
mqtt.connect(opts)
.on('connect', function () {
assert.equal(this.stream.socket.url, expected)
assert.equal(actual, expected)
this.end(true, done)
})
})
it('should use mqttv3.1 as the protocol if using v3.1', function (done) {
server.once('client', function (client) {
client.stream.socket.protocol.should.equal('mqttv3.1')
})
var opts = makeOptions({
protocolId: 'MQIsdp',
protocolVersion: 3
})
mqtt.connect(opts).on('connect', function () {
this.end(true, done)
})
})
abstractClientTests(server, makeOptions())
})