You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2 lines
222 KiB

2 years ago
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.mqtt=f()}})(function(){var define,module,exports;return function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r}()({1:[function(require,module,exports){(function(process,global){"use strict";var events=require("events");var Store=require("./store");var eos=require("end-of-stream");var mqttPacket=require("mqtt-packet");var Writable=require("readable-stream").Writable;var inherits=require("inherits");var reInterval=require("reinterval");var validations=require("./validations");var xtend=require("xtend");var setImmediate=global.setImmediate||function(callback){process.nextTick(callback)};var defaultConnectOptions={keepalive:60,reschedulePings:true,protocolId:"MQTT",protocolVersion:4,reconnectPeriod:1e3,connectTimeout:30*1e3,clean:true,resubscribe:true};function defaultId(){return"mqttjs_"+Math.random().toString(16).substr(2,8)}function sendPacket(client,packet,cb){client.emit("packetsend",packet);var result=mqttPacket.writeToStream(packet,client.stream);if(!result&&cb){client.stream.once("drain",cb)}else if(cb){cb()}}function flush(queue){if(queue){Object.keys(queue).forEach(function(messageId){if(typeof queue[messageId]==="function"){queue[messageId](new Error("Connection closed"));delete queue[messageId]}})}}function storeAndSend(client,packet,cb){client.outgoingStore.put(packet,function storedPacket(err){if(err){return cb&&cb(err)}sendPacket(client,packet,cb)})}function nop(){}function MqttClient(streamBuilder,options){var k;var that=this;if(!(this instanceof MqttClient)){return new MqttClient(streamBuilder,options)}this.options=options||{};for(k in defaultConnectOptions){if(typeof this.options[k]==="undefined"){this.options[k]=defaultConnectOptions[k]}else{this.options[k]=options[k]}}this.options.clientId=typeof this.options.clientId==="string"?this.options.clientId:defaultId();this.streamBuilder=streamBuilder;this.outgoingStore=this.options.outgoingStore||new Store;this.incomingStore=this.options.incomingStore||new Store;this.queueQoSZero=this.options.queueQoSZero===undefined?true:this.options.queueQoSZero;this._resubscribeTopics={};this.messageIdToTopic={};this.pingTimer=null;this.connected=false;this.disconnecting=false;this.queue=[];this.connackTimer=null;this.reconnectTimer=null;this.nextId=Math.max(1,Math.floor(Math.random()*65535));this.outgoing={};this.on("connect",function(){if(this.disconnected){return}this.connected=true;var outStore=this.outgoingStore.createStream();this.once("close",remove);outStore.on("end",function(){that.removeListener("close",remove)});outStore.on("error",function(err){that.removeListener("close",remove);that.emit("error",err)});function remove(){outStore.destroy();outStore=null}function storeDeliver(){if(!outStore){return}var packet=outStore.read(1);var cb;if(!packet){outStore.once("readable",storeDeliver);return}if(!that.disconnecting&&!that.reconnectTimer){cb=that.outgoing[packet.messageId];that.outgoing[packet.messageId]=function(err,status){if(cb){cb(err,status)}storeDeliver()};that._sendPacket(packet)}else if(outStore.destroy){outStore.destroy()}}storeDeliver()});this.on("close",function(){this.connected=false;clearTimeout(this.connackTimer)});this.on("connect",this._setupPingTimer);this.on("connect",function(){var queue=this.queue;function deliver(){var entry=queue.shift();var packet=null;if(!entry){return}packet=entry.packet;that._sendPacket(packet,function(err){if(entry.cb){entry.cb(er