From 4f2e4b37f26928f0688fbccd54c8764e817f2fbb Mon Sep 17 00:00:00 2001 From: lihao Date: Mon, 8 Jan 2024 23:07:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=AC=E5=85=B1=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/address.js | 108 + utils/animation.js | 26 + utils/api.js | 307 + utils/barcode.js | 404 + utils/card.js | 685 ++ utils/cartNum.js | 116 + utils/cartOrder.js | 95 + utils/cartUtils.js | 556 ++ utils/des-util.js | 883 +++ utils/des.js | 221 + utils/log.js | 30 + utils/loginApi.js | 211 + utils/md5.js | 214 + utils/mqMonitor.js | 38 + utils/mqtt.js | 14080 ++++++++++++++++++++++++++++++++++ utils/mqttMember.js | 141 + utils/mqttMessage.js | 156 + utils/mqttSubstribe.js | 257 + utils/msg.js | 12 + utils/myOrder.js | 1322 ++++ utils/openApi.js | 375 + utils/paho-mqtt.js | 2403 ++++++ utils/point.js | 135 + utils/qqmap-wx-jssdk.min.js | 876 +++ utils/qrcode.js | 778 ++ utils/util.js | 91 + utils/utils.js | 438 ++ utils/wcache.js | 49 + utils/wxParse/html2json.js | 308 + utils/wxParse/htmlparser.js | 192 + utils/wxParse/showdown.js | 2532 ++++++ utils/wxParse/wxDiscode.js | 207 + utils/wxParse/wxParse.js | 159 + utils/wxParse/wxParse.json | 3 + utils/wxParse/wxParse.wxml | 968 +++ utils/wxParse/wxParse.wxss | 206 + utils/wxRequest.js | 62 + utils/wxbarcode.js | 23 + 38 files changed, 29667 insertions(+) create mode 100644 utils/address.js create mode 100644 utils/animation.js create mode 100644 utils/api.js create mode 100644 utils/barcode.js create mode 100644 utils/card.js create mode 100644 utils/cartNum.js create mode 100644 utils/cartOrder.js create mode 100644 utils/cartUtils.js create mode 100644 utils/des-util.js create mode 100644 utils/des.js create mode 100644 utils/log.js create mode 100644 utils/loginApi.js create mode 100644 utils/md5.js create mode 100644 utils/mqMonitor.js create mode 100644 utils/mqtt.js create mode 100644 utils/mqttMember.js create mode 100644 utils/mqttMessage.js create mode 100644 utils/mqttSubstribe.js create mode 100644 utils/msg.js create mode 100644 utils/myOrder.js create mode 100644 utils/openApi.js create mode 100644 utils/paho-mqtt.js create mode 100644 utils/point.js create mode 100644 utils/qqmap-wx-jssdk.min.js create mode 100644 utils/qrcode.js create mode 100644 utils/util.js create mode 100644 utils/utils.js create mode 100644 utils/wcache.js create mode 100644 utils/wxParse/html2json.js create mode 100644 utils/wxParse/htmlparser.js create mode 100644 utils/wxParse/showdown.js create mode 100644 utils/wxParse/wxDiscode.js create mode 100644 utils/wxParse/wxParse.js create mode 100644 utils/wxParse/wxParse.json create mode 100644 utils/wxParse/wxParse.wxml create mode 100644 utils/wxParse/wxParse.wxss create mode 100644 utils/wxRequest.js create mode 100644 utils/wxbarcode.js diff --git a/utils/address.js b/utils/address.js new file mode 100644 index 0000000..357da2f --- /dev/null +++ b/utils/address.js @@ -0,0 +1,108 @@ +const app = getApp(); +const utils = require("utils"); +var addressInfo = { + /*--------查询收货地址列表-------*/ + queryAddressList: function(succcess, fail) { + var item = {}; + item.openId = wx.getStorageSync("openId") + var pointAddress = JSON.stringify(item) + var data = { + "pointAddress": pointAddress, + "method": "point.address.get", + }; + var ignores = ["pointAddress"]; + app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, ignores, + function(json) { + var result = json.data; + succcess(json); + }, + function(error) { + fail(error); + } + ); + }, + + /*--------添加收货地址-------*/ + addAddress: function(pointAddress, succcess, fail) { + var pointAddress = JSON.stringify(pointAddress) + var data = { + "pointAddress": pointAddress, + "method": "point.address.save", + }; + + var ignores = ["pointAddress"]; + app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, ignores, + function(json) { + var result = json.data; + succcess(json); + }, + function(error) { + fail(error); + } + ); + }, + + /*--------更新收货地址-------*/ + updateAddress: function(pointAddress, succcess, fail) { + var pointAddress = JSON.stringify(pointAddress) + var data = { + "pointAddress": pointAddress, + "method": "point.address.update", + }; + var ignores = ["pointAddress"]; + app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, ignores, + function(json) { + var result = json.data; + succcess(json); + }, + function(error) { + fail(error); + } + ); + }, + + /*--------删除收货地址-------*/ + deleteAddress: function(pointAddress, succcess, fail) { + var pointAddress = JSON.stringify(pointAddress) + var data = { + "pointAddress": pointAddress, + "method": "point.address.delete", + }; + var ignores = ["pointAddress"]; + app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, ignores, + function(json) { + var result = json.data; + succcess(json); + }, + function(error) { + fail(error); + } + ); + }, + + /*--------查询订单收货地址-------*/ + queryConfirmOrderAddress: function(succcess, fail) { + var item = {}; + item.openId = wx.getStorageSync("openId") + var pointAddress = JSON.stringify(item) + var data = { + "pointAddress": pointAddress, + "method": "get.point.confirm.order.address", + }; + var ignores = ["pointAddress"]; + app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, ignores, + function(json) { + var result = json.data; + succcess(json); + }, + function(error) { + fail(error); + } + ); + } +} + + +module.exports = { + addressInfo: addressInfo +} \ No newline at end of file diff --git a/utils/animation.js b/utils/animation.js new file mode 100644 index 0000000..c705580 --- /dev/null +++ b/utils/animation.js @@ -0,0 +1,26 @@ +const duration = 300; +const timingFun = "ease"; +const width = 0; + +var ani_width = function(duration,timingFunc,width){ + if (undefined != duration){ + this.duration = duration; + } + if (undefined != timingFun) { + this.timingFun = timingFun; + } + if (undefined != width) { + this.width = width; + } + + var animation = wx.createAnimation({ + duration: duration, + timingFunction: timingFunc + }); + animation.width(width).step(); + return animation.export(); +} + +module.exports = { + ani_width: ani_width +} \ No newline at end of file diff --git a/utils/api.js b/utils/api.js new file mode 100644 index 0000000..0cd1eb3 --- /dev/null +++ b/utils/api.js @@ -0,0 +1,307 @@ +var wxRequest = require('../utils/wxRequest'); +var app=getApp(); +var $ = { + extend: function (flag, p, c) { + var c = c || {}; + for (var i in p) { + if (!p.hasOwnProperty(i)) { + continue; + } + if (typeof p[i] === 'object') { + c[i] = (p[i].constructor === Array) ? [] : {}; + deepCopy(p[i], c[i]); + } else { + c[i] = p[i]; + } + } + return c; + }, + each: function (object, callback, args) { + var name, i = 0, + length = object.length, + isObj = length === undefined || typeof object == 'function'; + + if (args) { + if (isObj) { + for (name in object) { + if (callback.apply(object[name], args) === false) { + break; + } + } + } else { + for (; i < length;) { + if (callback.apply(object[i++], args) === false) { + break; + } + } + } + + // A special, fast, case for the most common use of each + } else { + if (isObj) { + for (name in object) { + if (callback.call(object[name], name, object[name]) === false) { + break; + } + } + } else { + for (; i < length;) { + if (callback.call(object[i], i, object[i++]) === false) { + break; + } + } + } + } + + return object; + } +} + +function sha1(x, blen) { + function add32(a, b) { + var lsw = (a & 0xFFFF) + (b & 0xFFFF); + var msw = (a >> 16) + (b >> 16) + (lsw >> 16); + return (msw << 16) | (lsw & 0xFFFF); + } + + function AA(a, b, c, d, e) { + b = (b >>> 27) | (b << 5); + var lsw = (a & 0xFFFF) + (b & 0xFFFF) + (c & 0xFFFF) + (d & 0xFFFF) + (e & 0xFFFF); + var msw = (a >> 16) + (b >> 16) + (c >> 16) + (d >> 16) + (e >> 16) + (lsw >> 16); + return (msw << 16) | (lsw & 0xFFFF); + } + + function RR(w, j) { + var n = w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16]; + return (n >>> 31) | (n << 1); + } + + var len = blen * 8; + x[len >> 5] |= 0x80 << (24 - len % 32); + x[((len + 64 >> 9) << 4) + 15] = len; + var w = new Array(80); + + var k1 = 0x5A827999; + var k2 = 0x6ED9EBA1; + var k3 = 0x8F1BBCDC; + var k4 = 0xCA62C1D6; + + var h0 = 0x67452301; + var h1 = 0xEFCDAB89; + var h2 = 0x98BADCFE; + var h3 = 0x10325476; + var h4 = 0xC3D2E1F0; + + for (var i = 0; i < x.length; i += 16) { + var j = 0; + var t; + var a = h0; + var b = h1; + var c = h2; + var d = h3; + var e = h4; + while (j < 16) { + w[j] = x[i + j]; + t = AA(e, a, d ^ (b & (c ^ d)), w[j], k1); + e = d; + d = c; + c = (b >>> 2) | (b << 30); + b = a; + a = t; + j++; + } + while (j < 20) { + w[j] = RR(w, j); + t = AA(e, a, d ^ (b & (c ^ d)), w[j], k1); + e = d; + d = c; + c = (b >>> 2) | (b << 30); + b = a; + a = t; + j++; + } + while (j < 40) { + w[j] = RR(w, j); + t = AA(e, a, b ^ c ^ d, w[j], k2); + e = d; + d = c; + c = (b >>> 2) | (b << 30); + b = a; + a = t; + j++; + } + while (j < 60) { + w[j] = RR(w, j); + t = AA(e, a, (b & c) | (d & (b | c)), w[j], k3); + e = d; + d = c; + c = (b >>> 2) | (b << 30); + b = a; + a = t; + j++; + } + while (j < 80) { + w[j] = RR(w, j); + t = AA(e, a, b ^ c ^ d, w[j], k4); + e = d; + d = c; + c = (b >>> 2) | (b << 30); + b = a; + a = t; + j++; + } + h0 = add32(h0, a); + h1 = add32(h1, b); + h2 = add32(h2, c); + h3 = add32(h3, d); + h4 = add32(h4, e); + } + return [h0, h1, h2, h3, h4]; +} + +var encoding = { + strToBe32s: function (str) { + var be = []; + var len = Math.floor(str.length / 4); + var i, j; + for (i = 0, j = 0; i < len; i++ , j += 4) { + be[i] = ((str.charCodeAt(j) & 0xff) << 24) | ((str.charCodeAt(j + 1) & 0xff) << 16) | ((str.charCodeAt(j + 2) & 0xff) << 8) | (str.charCodeAt(j + 3) & 0xff); + } + while (j < str.length) { + be[j >> 2] |= (str.charCodeAt(j) & 0xff) << (24 - (j * 8) % 32); + j++; + } + return be; + }, + + be32sToHex: function (be) { + var hex = '0123456789ABCDEF'; + var str = ''; + for (var i = 0; i < be.length * 4; i++) { + str += hex.charAt((be[i >> 2] >> ((3 - i % 4) * 8 + 4)) & 0xF) + hex.charAt((be[i >> 2] >> ((3 - i % 4) * 8)) & 0xF); + } + return str; + } +}; + +$.encoding = encoding; +var digests = { + hexSha1Str: function (str) { + return $.encoding.be32sToHex($.digests.sha1Str(str)); + }, + sha1Str: function (str) { + return sha1($.encoding.strToBe32s(str), str.length); + } +}; +$.digests = digests; + +var utils = { + inArray: function (needle, array, bool) { + if (typeof needle == "string" || typeof needle == "number") { + var len = array.length; + for (var i = 0; i < len; i++) { + if (needle === array[i]) { + if (bool) { + return i; + } + return true; + } + } + return false; + } + } +}; +$.utils = utils; + +var cardApi = { + sign: function (params, ignores) { + var timestamp = (new Date()).valueOf(); + var openAppKey = app.globalData.memberAppKey; + var openAppSecret = app.globalData.memberAppSecret; + var appSecret = openAppSecret; + var defaults = { + appKey: openAppKey, + v: '1.0', + format: 'json', + locale: 'zh-CN', + client: 'weixin', + timestamp: timestamp + }; + var options = $.extend(true, defaults, params); + var paramNames = []; + $.each(options, function (key, value) { + if (!$.utils.inArray(key, ignores, false)) { + paramNames.push(key); + } + }); + paramNames = paramNames.sort(); + var stringBuilder = []; + stringBuilder.push(appSecret); + $.each(paramNames, function (inx, value) { + stringBuilder.push(value); + stringBuilder.push(options[value]); + }); + stringBuilder.push(appSecret); + options.sign = $.digests.hexSha1Str(stringBuilder.join("")); + + return options; + }, + ajax: function (params, ignores) { + var data = this.sign(params, ignores); + var app = getApp(); + var url = app.globalData.memberUrl; + return wxRequest.postRequest(url, data); + }, + ajax2: function (params, ignores) { + var data = this.sign(params, ignores); + var app = getApp(); + var url = "http://127.0.0.1:8008/openApi/api"; + return wxRequest.postRequest(url, data); + }, + ajaxApi: function (params, ignores, success, error,urls) { + var data = this.sign(params, ignores); + var app = getApp(); + var url=""; + if(urls){ + url =urls; + }else{ + url = app.openApiParam.serverUrl + } + wx.request({ + url: url, + method: 'POST', + data: data, + dataType: 'json', + header: { + 'content-type': 'application/x-www-form-urlencoded' + }, + success: function (json) { + if (json.errorToken) { + if (typeof error == 'function') { + error.call(error, json); + } + } else { + if (typeof success == 'function') { + success.call(success, json); + } + } + }, + fail: function () { + if (typeof error == 'function') { + var json = { + "errorToken": "@@$-ERROR_TOKEN$-@@", + "code": "0", + "message": "网络好像有问题,无法访问云端服务!", + "solution": "请检查本机网络是否能正常访问互联网", + "subErrors": "" + }; + error.call(error, json); + } + } + }); + } +} + +module.exports = { + cardApi: cardApi +}; \ No newline at end of file diff --git a/utils/barcode.js b/utils/barcode.js new file mode 100644 index 0000000..8783839 --- /dev/null +++ b/utils/barcode.js @@ -0,0 +1,404 @@ +var CHAR_TILDE = 126; +var CODE_FNC1 = 102; + +var SET_STARTA = 103; +var SET_STARTB = 104; +var SET_STARTC = 105; +var SET_SHIFT = 98; +var SET_CODEA = 101; +var SET_CODEB = 100; +var SET_STOP = 106; + + +var REPLACE_CODES = { + CHAR_TILDE: CODE_FNC1 //~ corresponds to FNC1 in GS1-128 standard +} + +var CODESET = { + ANY: 1, + AB: 2, + A: 3, + B: 4, + C: 5 +}; + +function getBytes(str) { + var bytes = []; + for (var i = 0; i < str.length; i++) { + bytes.push(str.charCodeAt(i)); + } + return bytes; +} + +exports.code128 = function (ctx, text, width, height) { + + width = parseInt(width); + + height = parseInt(height); + + var codes = stringToCode128(text); + console.debug('codes', codes) + + var g = new Graphics(ctx, width, height); + + var barWeight = g.area.width / ((codes.length - 3) * 11 + 35); + + var x = g.area.left; + var y = g.area.top; + for (var i = 0; i < codes.length; i++) { + var c = codes[i]; + //two bars at a time: 1 black and 1 white + for (var bar = 0; bar < 8; bar += 2) { + var barW = PATTERNS[c][bar] * barWeight; + // var barH = height - y - this.border; + var barH = height - y; + var spcW = PATTERNS[c][bar + 1] * barWeight; + + //no need to draw if 0 width + if (barW > 0) { + g.fillFgRect(x, y, barW, barH); + } + + x += barW + spcW; + } + } + + ctx.draw(); +} + + +function stringToCode128(text) { + var barc = { + currcs: CODESET.C + }; + + var bytes = getBytes(text); + //decide starting codeset + var index = bytes[0] == CHAR_TILDE ? 1 : 0; + + var csa1 = bytes.length > 0 ? codeSetAllowedFor(bytes[index++]) : CODESET.AB; + var csa2 = bytes.length > 0 ? codeSetAllowedFor(bytes[index++]) : CODESET.AB; + barc.currcs = getBestStartSet(csa1, csa2); + barc.currcs = perhapsCodeC(bytes, barc.currcs); + + //if no codeset changes this will end up with bytes.length+3 + //start, checksum and stop + var codes = new Array(); + + switch (barc.currcs) { + case CODESET.A: + codes.push(SET_STARTA); + break; + case CODESET.B: + codes.push(SET_STARTB); + break; + default: + codes.push(SET_STARTC); + break; + } + + + for (var i = 0; i < bytes.length; i++) { + var b1 = bytes[i]; //get the first of a pair + //should we translate/replace + if (b1 in REPLACE_CODES) { + codes.push(REPLACE_CODES[b1]); + i++ //jump to next + b1 = bytes[i]; + } + + //get the next in the pair if possible + var b2 = bytes.length > (i + 1) ? bytes[i + 1] : -1; + + codes = codes.concat(codesForChar(b1, b2, barc.currcs)); + //code C takes 2 chars each time + if (barc.currcs == CODESET.C) i++; + } + + //calculate checksum according to Code 128 standards + var checksum = codes[0]; + for (var weight = 1; weight < codes.length; weight++) { + checksum += (weight * codes[weight]); + } + codes.push(checksum % 103); + + codes.push(SET_STOP); + + //encoding should now be complete + return codes; + + function getBestStartSet(csa1, csa2) { + //tries to figure out the best codeset + //to start with to get the most compact code + var vote = 0; + vote += csa1 == CODESET.A ? 1 : 0; + vote += csa1 == CODESET.B ? -1 : 0; + vote += csa2 == CODESET.A ? 1 : 0; + vote += csa2 == CODESET.B ? -1 : 0; + //tie goes to B due to my own predudices + return vote > 0 ? CODESET.A : CODESET.B; + } + + function perhapsCodeC(bytes, codeset) { + for (var i = 0; i < bytes.length; i++) { + var b = bytes[i] + if ((b < 48 || b > 57) && b != CHAR_TILDE) + return codeset; + } + if (bytes.length % 2 == 0) + return CODESET.C; + else + return CODESET.A; + } + + //chr1 is current byte + //chr2 is the next byte to process. looks ahead. + function codesForChar(chr1, chr2, currcs) { + var result = []; + var shifter = -1; + + if (charCompatible(chr1, currcs)) { + if (currcs == CODESET.C) { + if (chr2 == -1) { + shifter = SET_CODEB; + currcs = CODESET.B; + } + else if ((chr2 != -1) && !charCompatible(chr2, currcs)) { + //need to check ahead as well + if (charCompatible(chr2, CODESET.A)) { + shifter = SET_CODEA; + currcs = CODESET.A; + } + else { + shifter = SET_CODEB; + currcs = CODESET.B; + } + } + } + } + else { + //if there is a next char AND that next char is also not compatible + if ((chr2 != -1) && !charCompatible(chr2, currcs)) { + //need to switch code sets + switch (currcs) { + case CODESET.A: + shifter = SET_CODEB; + currcs = CODESET.B; + break; + case CODESET.B: + shifter = SET_CODEA; + currcs = CODESET.A; + break; + } + } + else { + //no need to shift code sets, a temporary SHIFT will suffice + shifter = SET_SHIFT; + } + } + + //ok some type of shift is nessecary + if (shifter != -1) { + result.push(shifter); + result.push(codeValue(chr2)); + } + else { + if (currcs == CODESET.C) { + //include next as well + result.push(codeValue(chr1, chr2)); + } + else { + result.push(codeValue(chr1)); + } + } + barc.currcs = currcs; + + return result; + } +} + +//reduce the ascii code to fit into the Code128 char table +function codeValue(chr1, chr2) { + if (typeof chr2 == "undefined") { + return chr1 >= 32 ? chr1 - 32 : chr1 + 64; + } + else { + return parseInt(String.fromCharCode(chr1) + String.fromCharCode(chr2)); + } +} + +function charCompatible(chr, codeset) { + var csa = codeSetAllowedFor(chr); + if (csa == CODESET.ANY) return true; + //if we need to change from current + if (csa == CODESET.AB) return true; + if (csa == CODESET.A && codeset == CODESET.A) return true; + if (csa == CODESET.B && codeset == CODESET.B) return true; + return false; +} + +function codeSetAllowedFor(chr) { + if (chr >= 48 && chr <= 57) { + //0-9 + return CODESET.ANY; + } + else if (chr >= 32 && chr <= 95) { + //0-9 A-Z + return CODESET.AB; + } + else { + //if non printable + return chr < 32 ? CODESET.A : CODESET.B; + } +} + +var Graphics = function (ctx, width, height) { + + this.width = width; + this.height = height; + this.quiet = Math.round(this.width / 40); + + this.border_size = 0; + this.padding_width = 0; + + this.area = { + width: width - this.padding_width * 2 - this.quiet * 2, + height: height - this.border_size * 2, + top: this.border_size - 4, + left: this.padding_width + this.quiet + }; + + this.ctx = ctx; + this.fg = "#000000"; + this.bg = "#ffffff"; + + // fill background + this.fillBgRect(0, 0, width, height); + + // fill center to create border + this.fillBgRect(0, this.border_size, width, height - this.border_size * 2); +} + +//use native color +Graphics.prototype._fillRect = function (x, y, width, height, color) { + this.ctx.setFillStyle(color) + this.ctx.fillRect(x, y, width, height) +} + +Graphics.prototype.fillFgRect = function (x, y, width, height) { + this._fillRect(x, y, width, height, this.fg); +} + +Graphics.prototype.fillBgRect = function (x, y, width, height) { + this._fillRect(x, y, width, height, this.bg); +} + +var PATTERNS = [ + [2, 1, 2, 2, 2, 2, 0, 0], // 0 + [2, 2, 2, 1, 2, 2, 0, 0], // 1 + [2, 2, 2, 2, 2, 1, 0, 0], // 2 + [1, 2, 1, 2, 2, 3, 0, 0], // 3 + [1, 2, 1, 3, 2, 2, 0, 0], // 4 + [1, 3, 1, 2, 2, 2, 0, 0], // 5 + [1, 2, 2, 2, 1, 3, 0, 0], // 6 + [1, 2, 2, 3, 1, 2, 0, 0], // 7 + [1, 3, 2, 2, 1, 2, 0, 0], // 8 + [2, 2, 1, 2, 1, 3, 0, 0], // 9 + [2, 2, 1, 3, 1, 2, 0, 0], // 10 + [2, 3, 1, 2, 1, 2, 0, 0], // 11 + [1, 1, 2, 2, 3, 2, 0, 0], // 12 + [1, 2, 2, 1, 3, 2, 0, 0], // 13 + [1, 2, 2, 2, 3, 1, 0, 0], // 14 + [1, 1, 3, 2, 2, 2, 0, 0], // 15 + [1, 2, 3, 1, 2, 2, 0, 0], // 16 + [1, 2, 3, 2, 2, 1, 0, 0], // 17 + [2, 2, 3, 2, 1, 1, 0, 0], // 18 + [2, 2, 1, 1, 3, 2, 0, 0], // 19 + [2, 2, 1, 2, 3, 1, 0, 0], // 20 + [2, 1, 3, 2, 1, 2, 0, 0], // 21 + [2, 2, 3, 1, 1, 2, 0, 0], // 22 + [3, 1, 2, 1, 3, 1, 0, 0], // 23 + [3, 1, 1, 2, 2, 2, 0, 0], // 24 + [3, 2, 1, 1, 2, 2, 0, 0], // 25 + [3, 2, 1, 2, 2, 1, 0, 0], // 26 + [3, 1, 2, 2, 1, 2, 0, 0], // 27 + [3, 2, 2, 1, 1, 2, 0, 0], // 28 + [3, 2, 2, 2, 1, 1, 0, 0], // 29 + [2, 1, 2, 1, 2, 3, 0, 0], // 30 + [2, 1, 2, 3, 2, 1, 0, 0], // 31 + [2, 3, 2, 1, 2, 1, 0, 0], // 32 + [1, 1, 1, 3, 2, 3, 0, 0], // 33 + [1, 3, 1, 1, 2, 3, 0, 0], // 34 + [1, 3, 1, 3, 2, 1, 0, 0], // 35 + [1, 1, 2, 3, 1, 3, 0, 0], // 36 + [1, 3, 2, 1, 1, 3, 0, 0], // 37 + [1, 3, 2, 3, 1, 1, 0, 0], // 38 + [2, 1, 1, 3, 1, 3, 0, 0], // 39 + [2, 3, 1, 1, 1, 3, 0, 0], // 40 + [2, 3, 1, 3, 1, 1, 0, 0], // 41 + [1, 1, 2, 1, 3, 3, 0, 0], // 42 + [1, 1, 2, 3, 3, 1, 0, 0], // 43 + [1, 3, 2, 1, 3, 1, 0, 0], // 44 + [1, 1, 3, 1, 2, 3, 0, 0], // 45 + [1, 1, 3, 3, 2, 1, 0, 0], // 46 + [1, 3, 3, 1, 2, 1, 0, 0], // 47 + [3, 1, 3, 1, 2, 1, 0, 0], // 48 + [2, 1, 1, 3, 3, 1, 0, 0], // 49 + [2, 3, 1, 1, 3, 1, 0, 0], // 50 + [2, 1, 3, 1, 1, 3, 0, 0], // 51 + [2, 1, 3, 3, 1, 1, 0, 0], // 52 + [2, 1, 3, 1, 3, 1, 0, 0], // 53 + [3, 1, 1, 1, 2, 3, 0, 0], // 54 + [3, 1, 1, 3, 2, 1, 0, 0], // 55 + [3, 3, 1, 1, 2, 1, 0, 0], // 56 + [3, 1, 2, 1, 1, 3, 0, 0], // 57 + [3, 1, 2, 3, 1, 1, 0, 0], // 58 + [3, 3, 2, 1, 1, 1, 0, 0], // 59 + [3, 1, 4, 1, 1, 1, 0, 0], // 60 + [2, 2, 1, 4, 1, 1, 0, 0], // 61 + [4, 3, 1, 1, 1, 1, 0, 0], // 62 + [1, 1, 1, 2, 2, 4, 0, 0], // 63 + [1, 1, 1, 4, 2, 2, 0, 0], // 64 + [1, 2, 1, 1, 2, 4, 0, 0], // 65 + [1, 2, 1, 4, 2, 1, 0, 0], // 66 + [1, 4, 1, 1, 2, 2, 0, 0], // 67 + [1, 4, 1, 2, 2, 1, 0, 0], // 68 + [1, 1, 2, 2, 1, 4, 0, 0], // 69 + [1, 1, 2, 4, 1, 2, 0, 0], // 70 + [1, 2, 2, 1, 1, 4, 0, 0], // 71 + [1, 2, 2, 4, 1, 1, 0, 0], // 72 + [1, 4, 2, 1, 1, 2, 0, 0], // 73 + [1, 4, 2, 2, 1, 1, 0, 0], // 74 + [2, 4, 1, 2, 1, 1, 0, 0], // 75 + [2, 2, 1, 1, 1, 4, 0, 0], // 76 + [4, 1, 3, 1, 1, 1, 0, 0], // 77 + [2, 4, 1, 1, 1, 2, 0, 0], // 78 + [1, 3, 4, 1, 1, 1, 0, 0], // 79 + [1, 1, 1, 2, 4, 2, 0, 0], // 80 + [1, 2, 1, 1, 4, 2, 0, 0], // 81 + [1, 2, 1, 2, 4, 1, 0, 0], // 82 + [1, 1, 4, 2, 1, 2, 0, 0], // 83 + [1, 2, 4, 1, 1, 2, 0, 0], // 84 + [1, 2, 4, 2, 1, 1, 0, 0], // 85 + [4, 1, 1, 2, 1, 2, 0, 0], // 86 + [4, 2, 1, 1, 1, 2, 0, 0], // 87 + [4, 2, 1, 2, 1, 1, 0, 0], // 88 + [2, 1, 2, 1, 4, 1, 0, 0], // 89 + [2, 1, 4, 1, 2, 1, 0, 0], // 90 + [4, 1, 2, 1, 2, 1, 0, 0], // 91 + [1, 1, 1, 1, 4, 3, 0, 0], // 92 + [1, 1, 1, 3, 4, 1, 0, 0], // 93 + [1, 3, 1, 1, 4, 1, 0, 0], // 94 + [1, 1, 4, 1, 1, 3, 0, 0], // 95 + [1, 1, 4, 3, 1, 1, 0, 0], // 96 + [4, 1, 1, 1, 1, 3, 0, 0], // 97 + [4, 1, 1, 3, 1, 1, 0, 0], // 98 + [1, 1, 3, 1, 4, 1, 0, 0], // 99 + [1, 1, 4, 1, 3, 1, 0, 0], // 100 + [3, 1, 1, 1, 4, 1, 0, 0], // 101 + [4, 1, 1, 1, 3, 1, 0, 0], // 102 + [2, 1, 1, 4, 1, 2, 0, 0], // 103 + [2, 1, 1, 2, 1, 4, 0, 0], // 104 + [2, 1, 1, 2, 3, 2, 0, 0], // 105 + [2, 3, 3, 1, 1, 1, 2, 0] // 106 +] \ No newline at end of file diff --git a/utils/card.js b/utils/card.js new file mode 100644 index 0000000..3981ad5 --- /dev/null +++ b/utils/card.js @@ -0,0 +1,685 @@ +const app = getApp(); +const utils = require("utils"); +/** + * 检测会员信息 + */ +var checkMemberInfo = function(params, ignores, succcess, fail) { + app.jsapi.memberApi(app.globalData.appMemberKey, app.globalData.appMemberSecret, app.globalData.serverMemberUrl).ajax(params, ignores, + function(json) { + var result = json.data; + succcess(json); + }, + function(error) { + fail(error); + } + ); +} + +var cardGoodsDetails = function(goods) { + var arr = []; + goods = JSON.parse(goods); + for (var i = 0; i < goods.length; i++) { + var item = goods[i]; + var map = {}; + map.goodsId = item.productNo; // 商品编码 + map.name = item.goodsName; // 商品名称 + map.spec = item.specName; // 商品规格 + map.quantity = item.count; // 数量 + map.price = item.price; // 单价 + map.discountMoney = item.discountTotal; // 优惠金额 + map.money = item.amountTotal; // 金额 + arr.push(map); + } + return JSON.stringify(arr); +} + +var cardMoney = function(money) { + return parseInt(parseFloat(money) * 100); +} + +/** + * cardInfo 会员卡信息 + * prepayment 预支付编码 + * amount 刷卡金额(精确到分) + */ +var getCardPayInfo = function(cardInfo, prepayment, amount, passwd) { + var arr = []; + var info = {}; + info.prepayment = prepayment; + info.cardNo = cardInfo.cardNo; + info.amount = amount; + info.isNoPwd = 1; + info.passwd = passwd; + arr.push(info); + console.log(info); + return JSON.stringify(arr); +} + +/** + * + */ +var getPays = function(money) { + var arr = []; + var pays = {}; + pays.payTypeNo = "02"; + pays.payType = "会员卡"; + pays.money = money; + arr.push(pays); + return JSON.stringify(arr); +} + +/** + * 充值方案 + */ +var WxRechargeScheme = function(success, fail) { + var params = { + "wid": app.globalData.memberWid, + 'method': 'weixin.recharge.scheme' + } + console.log(params); + var ignores = []; + // + app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(params, ignores, + function(json) { + var result = json.data; + success(json); + }, + function(error) { + fail(error); + } + ); +} + +/** + * 获取手机号 + */ +var wx_getPhoneNumber = function(iv, data, success, fail) { + wx.request({ + url: app.globalData.serverUrlExtend, + method: "post", + header: { + 'content-type': 'application/x-www-form-urlencoded' + }, + data: { + "type": "wxdcgetphone", + "iv": iv, + "loginKey": wx.getStorageSync("loginKey"), + "encryptedData": data + }, + success: function(res) { + success(res); + }, + fail: function(res) { + fail(res); + } + }); +} + +/** + * 查询优惠券信息 + */ +var queryStoreCouponsList = function(params, success, fail) { + params.method = "elec.coupon.query"; + params.workerNo = app.workerNo; + params.sourceSign = app.sourceSign; + var ignores = []; + app.jsapi.memberApi(app.globalData.appMemberKey, app.globalData.appMemberSecret, app.globalData.serverMemberUrl).ajax(params, ignores, + function(json) { + success(json); + }, + function(error) { + fail(error); + } + ); +} + +/** + * 查询优惠券信息 + */ +var queryGroupCouponsList = function(params, success, fail) { + params.method = "elec.coupon.query"; + params.workerNo = app.globalData.workerNo; + params.sourceSign = app.sourceSign; + var ignores = []; + // app.globalData.memberUrl + app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret,app.globalData.memberUrl).ajax(params, ignores, + function(json) { + success(json); + }, + function(error) { + fail(error); + } + ); +} + +/** + * 查询会员信息 + */ +var wx_cardInfo = function(cardNo, success, fail) { + var params = { + "method": "card.info", + "cardNo": cardNo, + "shopNo": app.globalData.shopNo, + "posNo": app.globalData.posNo, + } + var ignores = []; + app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(params, ignores, + function(json) { + success(json); + }, + function(error) { + fail(error); + } + ); +} +/** + * 优惠券名称 + */ +var getCouponsName = function(cardType) { + return cardType == "CASH" ? "代金券" : cardType == "DISCOUNT" ? "折扣券" : cardType == "GIFT" ? "兑换券" : ""; +} + +/** + * 价值 + */ +var getCardWorth = function(cardType, reduceCost, discount) { + return cardType == "CASH" ? (reduceCost / 100 + "元") : cardType == "DISCOUNT" ? discount : ""; +} + +/** + * 价值 + */ +var getCardWorthInfo = function(cardType, reduceCost, discount) { + return cardType == "CASH" ? ("¥" + reduceCost) : cardType == "DISCOUNT" ? (discount + "折") : "兑换券"; +} + + +/*--------随机字符串-------*/ +function randomString() { + var randomString = ""; + for (var i = 1; i <= 5; i++) { + randomString += parseInt(Math.random() * 10); + } + return randomString; +} +/*--------自动生成会员订单号-------*/ +function getTradeNo() { + console.log("-----------------"); + return app.globalData.shopNo + utils.getFormatTime(new Date(), 2) + this.randomString(); +} +/*--------领券中心-------*/ +var couponCenter = function couponCenter(couponType, type, success, fail) { + var data = { + 'groupNo': app.globalData.groupNo, + 'wid': app.globalData.memberWid, + 'memberId': wx.getStorageSync("memberId"), + 'type': type, + 'useFlag': 0, + 'method': 'query.coupon.exchange.schemes' + } + if (couponType) { + data.couponType = couponType + } + var ignores = []; + app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, ignores, + function(json) { + success(json); + }, + function(error) { + fail(error); + } + ); +} +/*--------免费领取优惠券接口-------*/ +var exchangeCouponScheme = function(schemeId, success, fail) { + var data = { + 'schemeId': schemeId, + 'memberId': wx.getStorageSync("memberId"), + 'number': 1, + 'openId': wx.getStorageSync("openId"), + 'shopNo': app.globalData.shopNo, + 'posNo': app.globalData.posNo, + 'workerNo': app.globalData.workerNo, + 'sourceSign': app.globalData.sourceSign, + 'method': 'elec.coupon.exchange' + } + var ignores = []; + app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, ignores, + function(json) { + success(json); + }, + function(error) { + fail(error); + } + ); +} +/*--------发送fromid后台给用户推送消息-------*/ +var sendTemPlate = function(type, fromId, couponId, success, fail) { + var data = {}; + if (couponId) { + data = { + 'openId': wx.getStorageSync('openId'), + 'formId': fromId, + 'wid': app.globalData.memberWid, + 'sendType': type, + 'method': 'get.smweixin.formId', + 'couponId': couponId, + 'memberId': wx.getStorageSync('memberId') + } + } else { + data = { + 'openId': wx.getStorageSync('openId'), + 'formId': fromId, + 'wid': app.globalData.memberWid, + 'sendType': type, + 'method': 'get.smweixin.formId', + 'memberId': wx.getStorageSync('memberId') + } + } + var ignores = []; + app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, ignores, + function(json) { + success(json); + }, + function(error) { + fail(error); + } + ); +} + +/*--------查询积分商品列表-------*/ +var pointProduct = function(type, success, fail) { + var data = { + "wid": app.globalData.memberWid, + "method": "query.point.product.list", + "pageType": type + }; + var ignores = []; + app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, ignores, + function(json) { + success(json); + }, + function(error) { + fail(error); + } + ); +} + +/*--------查询积分商品详情-------*/ +var queryPointProductDetail = function(id, schemeId, success, fail) { + var data = { + "method": "query.point.product.detail", + "productId": id, + "schemeId": schemeId + }; + app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, [], + function(json) { + success(json); + }, + function(error) { + fail(error); + } + ); +} + + +/*--------查询积分所有商品-------*/ +var pointAllTypeProduct = function(success, fail) { + var data = { + "wid": app.globalData.memberWid, + "method": "query.point.product.noPageType", + }; + var ignores = []; + app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, ignores, + function(json) { + success(json); + }, + function(error) { + fail(error); + } + ); +} + + +/** + * 小程序优惠活动弹窗 + */ +var judgeUpWindown = function(success, fail,type) { + var data = { + 'openId': wx.getStorageSync("openId"), + 'wid': app.globalData.memberWid, + 'memberId': wx.getStorageSync("memberId"), + 'method': 'query.upWindow.coupon.list', + 'type': app.temData.type + } + if (type) { + data.type = type + } + // app.globalData.memberUrl + app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, [], + function(json) { + success(json); + }, + function(error) { + fail(error); + } + ); +} +/** + * 小程序弹窗一键领取 + */ +var receive = function(schemeId, success, fail) { + var data = { + 'cardNo': wx.getStorageSync("cardInfo").cardNo, + 'wid': app.globalData.memberWid, + 'memberId': wx.getStorageSync("memberId"), + 'method': 'elec.promotion.packet.receive', + 'schemeId': schemeId, + 'sourceSign': "wechat" + } + app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, [], + function(json) { + success(json); + }, + function(error) { + fail(error); + } + ); +} + +/** + * 关闭弹窗 + */ +var closeUpWindow = function(schemeId, success, fail) { + var data = { + 'cardNo': wx.getStorageSync("cardInfo").cardNo, + 'wid': app.globalData.memberWid, + 'memberId': wx.getStorageSync("memberId"), + 'method': 'elec.promotion.packet.unreceive', + 'schemeId': schemeId, + 'sourceSign': "wechat" + } + app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, [], + function(json) { + success(json); + }, + function(error) { + fail(error); + } + ); +} + + +/** + * 会员分享规则详情 + */ +var memberShareRule = function(success, fail) { + var data = { + 'shopNo': app.globalData.shopNo, + 'posNo': app.globalData.posNo, + 'method': 'pro.share.member.info', + } + app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, [], + function(json) { + success(json); + }, + function(error) { + fail(error); + } + ); +} + +/* +会员活动 大转盘 +*/ +var memberActivity = function(success, fail) { + var data = { + 'groupId': app.globalData.groupId, + 'method': 'pro.activity.list', + } + //app.globalData.memberUrl + app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl ).ajax(data, [], + function(json) { + success(json); + }, + function(error) { + fail(error); + } + ); +} + + +/* +会员活动 大转盘 抽奖 +*/ +var memberActivityLuckyDraw = function(activityId, success, fail) { + var data = { + 'wid': app.globalData.memberWid, + "memberId": wx.getStorageSync("memberId"), + "activityId": activityId, + "shopNo": app.globalData.shopNo, + "postNo": app.globalData.posNo, + "workerNo": app.globalData.workerNo, + "sourceSign": app.sourceSign, + "mobile": wx.getStorageSync("cardInfo").mobile, + "cardNo": wx.getStorageSync("cardInfo").cardNo, + 'method': 'member.activity.lucky.draw', + } + app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, [], + function(json) { + success(json); + }, + function(error) { + fail(error); + } + ); +} + + +/** + * 会员营销活动记录 + */ +var memberActivityRecorde = function(success, fail) { + var data = { + 'memberId': wx.getStorageSync("memberId"), + 'method': 'member.activity.record', + } + app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, [], + function(json) { + success(json); + }, + function(error) { + fail(error); + } + ); +} +/** + * 会员营销活动记录 + */ +var memberActivityJoinNumber = function(activityId, success, fail) { + var data = { + 'memberId': wx.getStorageSync("memberId"), + 'method': 'pro.activity.join.number', + 'activityId': activityId + } + app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, [], + function(json) { + success(json); + }, + function(error) { + fail(error); + } + ); +} + + +/** + * 查询邀请好友活动 + */ +var getInvitationFriend = function(success, fail) { + var data = { + 'method': 'pro.share.member.info', + "shopNo": app.globalData.shopNo, + "posNo": app.globalData.posNo, + } + app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, [], + function(json) { + success(json); + }, + function(error) { + fail(error); + } + ); +} + +/** + * 邀请好友 好友注册发送邀请人奖品 + */ +var sendGift = function (success, fail) { + var data = { + 'method': 'pro.activity.share.new', + 'activityId': app.temData.activityId, + 'wid': app.globalData.memberWid, + 'memberId': app.temData.fromMemberId, + 'sourceSign': app.sourceSign, + 'toMemberId': wx.getStorageSync("memberId"), + "shopNo": app.globalData.shopNo, + "posNo": app.globalData.posNo, + 'workerNo': app.globalData.workerNo, + 'openId': app.temData.openId, + } + app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, [], + function (json) { + success(json); + }, + function (error) { + fail(error); + } + ); +} + +/** + * 查看我邀请的好友人数 + */ +var getSendGiftNumber = function (activityId,success, fail) { + var data = { + 'method': 'pro.activity.share.number', + 'activityId': activityId, + 'wid': app.globalData.memberWid, + 'memberId': wx.getStorageSync("memberId"), + } + app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(data, [], + function (json) { + success(json); + }, + function (error) { + fail(error); + } + ); +} +/** + * 根据openId查询用户信息 + */ +var getMemberInfoByOpenId = function(success, fail) { + var params = { + "method": "small.weixin.member.info.wid1", + "wid": app.globalData.memberWid, + "openId": app.openId, + }; + var ignores = ["openId", "nickName", "gender", "avatarUrl", "country", "province", "city"]; + app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(params, ignores, + function(json) { + success(json); + }, + function(error) { + fail(error); + } + ); +} + + +/** + * 查询优惠券适用门店数量 + */ +var getCouponApplyShopNum = function(couponId,longitude,latitude,success,fail) { + var params = { + 'couponId': couponId, + 'pageNum': 1, + 'pageSize': 1, + 'property': "", + 'keyword': "", + 'longitude': longitude, + 'latitude': latitude, + 'method': 'elec.coupon.shops.distance' + }; + var ignores = ['property', 'keyword']; + app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(params, ignores, + function(json) { + success(json); + }, + function(error) { + fail(error); + } + ); +} +/** + * 查询优惠券适用门店数量 + */ +var getCouponApplyShop = function(couponId,pageNum,pageSize,longitude,latitude,name,value,success,fail) { + var params = { + 'couponId': couponId, + 'pageNum': pageNum, + 'pageSize': pageSize, + 'property':name, + 'keyword': value, + 'longitude': longitude, + 'latitude': latitude, + 'method': 'elec.coupon.shops.distance' + }; + var ignores = ['property', 'keyword']; + app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(params, ignores, + function(json) { + success(json); + }, + function(error) { + fail(error); + } + ); +} +module.exports = { + checkMemberInfo: checkMemberInfo, + cardGoodsDetails: cardGoodsDetails, + cardMoney: cardMoney, + getCardPayInfo: getCardPayInfo, + getPays: getPays, + WxRechargeScheme: WxRechargeScheme, + getTradeNo: getTradeNo, + randomString: randomString, + queryGroupCouponsList: queryGroupCouponsList, + getCouponsName: getCouponsName, + getCardWorth: getCardWorth, + getCardWorthInfo: getCardWorthInfo, + wx_getPhoneNumber: wx_getPhoneNumber, + wx_cardInfo: wx_cardInfo, + couponCenter: couponCenter, + exchangeCouponScheme: exchangeCouponScheme, + sendTemPlate: sendTemPlate, + pointProduct: pointProduct, + queryPointProductDetail: queryPointProductDetail, + judgeUpWindown: judgeUpWindown, + receive: receive, + closeUpWindow: closeUpWindow, + pointAllTypeProduct: pointAllTypeProduct, + memberShareRule: memberShareRule, + memberActivity: memberActivity, + memberActivityLuckyDraw: memberActivityLuckyDraw, + memberActivityRecorde: memberActivityRecorde, + memberActivityJoinNumber: memberActivityJoinNumber, + getInvitationFriend: getInvitationFriend, + sendGift: sendGift, + getSendGiftNumber: getSendGiftNumber, + getMemberInfoByOpenId:getMemberInfoByOpenId, + getCouponApplyShop:getCouponApplyShop, + getCouponApplyShopNum:getCouponApplyShopNum + // getMemberExtend: getMemberExtend +} \ No newline at end of file diff --git a/utils/cartNum.js b/utils/cartNum.js new file mode 100644 index 0000000..cd7af03 --- /dev/null +++ b/utils/cartNum.js @@ -0,0 +1,116 @@ +var cartUtils = {}; +var app = {}; + +var init = function (app,cartUtils){ + this.app = app; + this.cartUtils = cartUtils; +} + +/** + * 加入购物车 + */ +var addToCart = function (goods, num) { + var suiltData = {}; + if (goods.suitFlag == 1){ + suiltData = processSuitData(this.app,goods, num); + } + var options = { + boxPrice: goods.boxPrice, + uniqueId: goods.uniqueId, + productId: goods.productId, + productNo: goods.productNo, + unitId: goods.unitId, + categoryId: goods.categoryId, + goodsId: goods.goodsId, + goodsName: goods.name, + specId: goods.specId, + specName: goods.specName, + makeCount: undefined == goods.makeCount ? 0 : goods.makeCount, + price: goods.price, + memberPrice: goods.memberPrice, + makeId: undefined == goods.makeId ? "" : goods.makeId, + makeName: undefined == goods.makeName ? "" : goods.makeName, + makePrice: undefined == goods.makePrice ? "" : goods.makePrice, + makeAmount: undefined == goods.makeAmount ? 0 : goods.makeAmount, + num: num, + isSuit: goods.suitFlag == 1 ?true:false, + addAmount: undefined == suiltData.addAmount ? 0 : suiltData.addAmount, + suitDetails: undefined == suiltData.suitDetails ? [] : suiltData.suitDetails + } + // 商品压入购物车 + this.cartUtils.putCartMap(options); +}; + +/** + * 处理套餐商品加入购物车 + */ +function processSuitData(app,goods,num){ + var suiltData = {}; + + var suitDetails = []; + goods.suitList.forEach(function (group, ginx) { + // 获取已选中套餐明细 + group.detail.forEach(function (item, inx) { + if (item.select) { + var suitItem = { + goodsId: item.id, + name: item.productName, + productId: item.productId, + productNo: item.productNo, + categoryId: item.categoryId, + specId: item.specId, + specName: item.specName, + unitId: item.unitId, + num: item.num, + quantity:item.quantity, + price: item.price, + addPrice: item.addPrice, + memberPrice: item.memberPrice, + makeId: item.makeId, + makeName: item.makeName, + makePrice: item.makePrice, + makeAmount: item.makeAmount, + } + suitDetails.push(suitItem); + } + }); + }); + suiltData.addAmount = goods.addAmount; + suiltData.suitDetails = suitDetails; + return suiltData; +}; + +/** + * 从购物车中减去商品数量 + */ +var addNumberByCart = function (uniqueId) { + var specMap = this.cartUtils.getSpecMapByCart(); + if (undefined != specMap[uniqueId]) { + var specGoods = specMap[uniqueId]; + var num = this.app.utils.numAdd(specGoods.num, 1); + this.addToCart(specGoods, num); + } +}; + +/** + * 从购物车中减去商品数量 + */ +var decNumberByCart = function (uniqueId) { + var specMap = this.cartUtils.getSpecMapByCart(); + if (undefined != specMap[uniqueId]) { + var specGoods = specMap[uniqueId]; + var num = 0; + if (specGoods.num > 0) { + num = this.app.utils.numSub(specGoods.num, 1); + } + this.addToCart(specGoods, num); + } +}; + + +module.exports = { + init: init, + addToCart: addToCart, + addNumberByCart: addNumberByCart, + decNumberByCart: decNumberByCart +} diff --git a/utils/cartOrder.js b/utils/cartOrder.js new file mode 100644 index 0000000..c488fea --- /dev/null +++ b/utils/cartOrder.js @@ -0,0 +1,95 @@ +/** + * 外卖计算餐盒费 + */ +var getBoxFee = function(order) { + var boxFee = 0.0; + if (order) { + for (var key in order.dishes) { + var dish = order.dishes[key]; + if (!dish.isSuit) { // 非套餐模式计算餐盒费 + if (dish.boxPrice) { + boxFee = parseFloat(boxFee + dish.boxPrice * dish.num); + } + } else { // 套餐模式计算餐盒费 + console.log(dish); + var specs = dish.specs; + for (var ckey in specs) { + var cValues = specs[ckey]; + var suitDetails = cValues.suitDetails; + for (var i = 0; i < suitDetails.length; i++) { + var tDetail = suitDetails[i]; + if (tDetail.boxPrice) { + boxFee = parseFloat(boxFee + dish.boxPrice * dish.num); + } + } + } + } + } + } + return boxFee.toFixed(2); +} + +/** + * 外卖计算配送费 + */ +var getDeliverFee = function(store) { + var deliverFee = 0.0; + if (store) { + if (store.deliverFee) { + deliverFee = store.deliverFee; + } + } + return deliverFee.toFixed(2); +} + +/** + * 订单封装增加餐盒费+配送费 + */ +var getTotal = function(order) { + var total = 0.0; + var memberAmount = 0.0; + for (var key in order.dishes) { + var dish = order.dishes[key]; + total = total + dish.amount; + console.log("订单总金额:", dish); + memberAmount = memberAmount + dish.memberAmount; + } + console.log("订单总金额:", total); + if (order.busMode == 1) { // 外带 + total = total + parseFloat(order.boxFee); + memberAmount = memberAmount + parseFloat(order.boxFee); + } else if (order.busMode == 2) { // 外卖 + total = total + parseFloat(order.boxFee) + parseFloat(order.distributionFee); + memberAmount = memberAmount + parseFloat(order.boxFee) + parseFloat(order.distributionFee); + } + + console.log("订单总金额:", total); + order.totalCash = parseFloat(total).toFixed(2); + order.totalMemberCash = parseFloat(memberAmount).toFixed(2); +} + +/** + * 规则 + */ + +var getDefaultOrderTicket = function() { + var orderTicket = {}; + orderTicket.sumCount = 0; + orderTicket.sumMoney = 0; + orderTicket.storeName = ""; + orderTicket.storeId = ""; + orderTicket.storeNo = ""; + orderTicket.people = 1; + orderTicket.ext2 = ""; + orderTicket.discount = 0; + orderTicket.openId = wx.getStorageSync("openId"); + orderTicket.orderList = []; + return orderTicket; +} + +module.exports = { + getBoxFee: getBoxFee, + getDeliverFee: getDeliverFee, + getTotal: getTotal, + getDefaultOrderTicket: getDefaultOrderTicket +} \ No newline at end of file diff --git a/utils/cartUtils.js b/utils/cartUtils.js new file mode 100644 index 0000000..97c888f --- /dev/null +++ b/utils/cartUtils.js @@ -0,0 +1,556 @@ +// /*** +// * 购物车js +// * 使用storage 存放购物车商品信息 +// */ +// const app = getApp(); +// const utils = require("utils"); + +// var digits = 2; // 小数位数 +// var storeKey = "store"; +// var cartKey = "cartKey"; +// var loadedDishKey = "currentLoadedDishs"; +// var tableKey = "tableKey"; + + +// /* 获取当前操作门店 */ +// var getStore = function() { +// return wx.getStorageSync(storeKey); +// }; + +// /* 压入当前操作门店 */ +// var setStore = function(options) { +// wx.setStorageSync(storeKey, options); +// }; + +// /* 获取当前桌号 */ +// var getTable = function() { +// return wx.getStorageSync(storeKey); +// }; + +// /* 压入当前桌号 */ +// var setTable = function(options) { +// wx.setStorageSync(tableKey, options); +// }; + +// /* 获取对应门店购物车数据 */ +// var getStoreCartMap = function(options) { +// var storeId = getStore().id; +// var cartMap = getCartMap(); +// if (null != cartMap && undefined != cartMap && cartMap != "") { +// return cartMap[storeId]; +// } else { +// cartMap = {}; +// setStoreCartMap(cartMap); +// return cartMap; +// } +// }; + +// /* 压入对应门店购物车数据 */ +// var setStoreCartMap = function(option) { +// var storeId = getStore().id; +// var storeCartMap = option.storeCartMap; + +// var cartMap = getCartMap(); +// if (cartMap == null || cartMap == undefined || cartMap == "") { +// cartMap = new Object(); +// } +// cartMap[storeId] = storeCartMap; +// setCartMap(cartMap); +// return true; +// }; + +// /* 压入购物车数据 并返回当前购物车数据*/ +// var setCartMap = function(cartMap) { +// wx.setStorageSync(cartKey, cartMap); +// return getCartMap(); +// }; + +// /* 获取购物车数据 */ +// var getCartMap = function() { +// var cartMap = wx.getStorageSync(cartKey); +// if (null == cartMap || undefined == cartMap || cartMap == "") { +// cartMap = {}; +// } +// return cartMap; +// }; + +// /* 压入购物车门店数据 */ +// var putCartMap = function(options) { +// var store = getStore(); +// var storeId = store.id; +// var boxPrice = options.boxPrice; +// var uniqueId = options.uniqueId; // 规格商品Id+做法Id +// var productId = options.productId; +// var productNo = options.productNo; +// var goodsId = options.goodsId; // 商品Id +// var goodsName = options.goodsName; +// var unitId = options.unitId; +// var categoryId = options.categoryId; +// var price = options.price; +// var memberPrice = options.memberPrice; +// var num = options.num; +// var specId = options.specId == undefined ? '' : options.specId; // 规格Id +// var specName = options.specName == undefined ? '' : options.specName; // 规格名称 +// var makeId = options.makeId == undefined ? '' : options.makeId; +// var makeName = options.makeName == undefined ? '' : options.makeName; +// var makePrice = options.makePrice == undefined ? '' : options.makePrice; +// var makeAmount = options.makeAmount == undefined ? 0 : options.makeAmount; +// var addAmount = options.addAmount == undefined ? 0 : options.addAmount; // 套餐加价 +// var isSuit = options.isSuit; // true 套餐商品、非套餐商品 +// var suitDetails = options.suitDetails; + +// var cartMap = getCartMap(); +// if (cartMap != null) { +// var storeCartMap = this.getStoreCartMap({ +// storeId: storeId +// }); +// if (utils.isBlank(storeCartMap)) { +// storeCartMap = new Object(); +// storeCartMap.totalCash = 0; +// storeCartMap.totalNum = 0; +// storeCartMap.categories = {}; +// storeCartMap.createTime = (new Date()).valueOf(); +// } + +// var dishes = storeCartMap.dishes; +// if (utils.isBlank(dishes)) { +// dishes = new Object(); +// } + +// var dishObject = dishes[productId]; +// if (utils.isNotBlank(dishObject)) { +// var specs = dishObject.specs; +// var specObject = {}; +// if (num <= 0) { +// specObject = undefined; +// } else { +// specObject = specs[uniqueId]; +// if (utils.isNotBlank(specObject)) { +// specObject.num = num; +// } else { +// specObject = new Object(); +// specObject.categoryId = categoryId; +// specObject.productId = productId; +// specObject.productNo = productNo; +// specObject.unitId = unitId; +// specObject.goodsId = goodsId; +// specObject.name = goodsName; +// specObject.specId = specId; +// specObject.specName = specName; +// // specObject.makeCount = makeCount; +// specObject.num = num; +// specObject.price = price; +// specObject.memberPrice = memberPrice; +// //做法信息 +// specObject.makeId = makeId; +// specObject.makeName = makeName; +// specObject.makePrice = makePrice; +// specObject.makeAmount = makeAmount; +// specObject.suitDetails = suitDetails; +// specObject.isSuit = isSuit; +// specObject.uniqueId = uniqueId; +// specObject.addAmount = addAmount; +// } +// } + +// specs[uniqueId] = specObject; +// dishObject.specs = specs; +// } else { +// // 购物车中不存在此菜品 新增dish和下属spec +// var amount = utils.numMulti(num, price); +// var memberAmount = utils.numMulti(num, memberPrice); + +// dishObject = new Object(); +// dishObject.name = goodsName; +// dishObject.isSuit = isSuit; +// dishObject.addAmount = addAmount; +// dishObject.totalPrice = amount; +// dishObject.totalMemberPrice = memberAmount; +// dishObject.totalMakeAmount = makeAmount; +// dishObject.amount = utils.numAdd(utils.numAdd(dishObject.totalPrice, dishObject.totalMakeAmount), addAmount); + +// var specs = new Object(); +// var specObject = new Object(); +// specObject.categoryId = categoryId; +// specObject.productId = productId; +// specObject.productNo = productNo; +// specObject.unitId = unitId; +// specObject.goodsId = goodsId; +// specObject.name = goodsName; +// specObject.specId = specId; +// specObject.specName = specName; +// // specObject.makeCount = makeCount; +// specObject.num = num; +// specObject.price = Number(price).toFixed(2); +// specObject.memberPrice = Number(memberPrice).toFixed(2); +// specObject.makeId = makeId; +// specObject.makeName = makeName; +// specObject.makePrice = makePrice; +// specObject.makeAmount = makeAmount; + +// specObject.suitDetails = suitDetails; +// specObject.isSuit = isSuit; +// specObject.uniqueId = uniqueId; +// specObject.addAmount = addAmount; + +// specObject.totalPrice = amount; +// specObject.totalMemberPrice = memberAmount; +// specObject.totalMakeAmount = makeAmount; +// /* 消费金额 = 商品消费金额 + 商品做法金额 + 套餐明细加价金额 */ +// specObject.amount = utils.numAdd(utils.numAdd(specObject.totalPrice, specObject.totalMakeAmount), addAmount); +// specObject.memberAmount = utils.numAdd(utils.numAdd(specObject.totalMemberPrice, specObject.totalMakeAmount), addAmount); + +// specs[uniqueId] = specObject; +// dishObject.specs = specs; + +// } + + +// /* 商品数量变动结束 重新计算dishObject金额,数量 */ +// dishObject.num = 0; +// dishObject.totalPrice = 0; +// dishObject.totalMemberPrice = 0; +// dishObject.totalMakeAmount = 0; +// dishObject.amount = 0; +// dishObject.memberAmount = 0; + +// var dishAddAmount = 0; +// var specs = dishObject.specs; + +// for (var key in specs) { +// var spec = specs[key]; +// if (null != spec && undefined != spec) { +// var suitAddAmount = 0.00; /* 套餐明细加价汇总 */ +// var suitMakeAmount = 0.00; /* 套餐明细做法汇总 */ +// var totalMakeAmount = 0.00; /* spec做法最终 */ +// console.log("is suit:" + spec.isSuit); + + +// if (spec.isSuit) { +// var suits = spec.suitDetails; /* 套餐明细 */ +// if (undefined != suits && suits.length > 0) { +// suits.forEach(function(suitItem, inx) { +// suitAddAmount = utils.numAdd(suitAddAmount, suitItem.addPrice); +// suitMakeAmount = utils.numAdd(suitMakeAmount, utils.numMulti(suitItem.quantity, suitItem.makeAmount)); +// }); +// } +// totalMakeAmount = utils.numMulti(spec.num, suitMakeAmount); +// } else { +// totalMakeAmount = utils.numMulti(spec.num, spec.makeAmount); +// } + +// // +// var amount = utils.numMulti(spec.num, spec.price); +// var memberAmount = utils.numMulti(spec.num, spec.memberPrice); + +// // 商品加价金额 +// spec.addAmount = utils.numMulti(spec.num, suitAddAmount); +// dishAddAmount = utils.numAdd(dishAddAmount, spec.addAmount); +// // spec.makeAmount = makeAmount; +// spec.totalMakeAmount = totalMakeAmount; +// spec.totalPrice = amount; +// spec.totalMemberPrice = memberAmount; +// //商品总金额 = 商品金额 + 商品加价总额 + 商品做法总额 +// spec.amount = utils.numAdd(utils.numAdd(amount, spec.addAmount), totalMakeAmount); +// spec.memberAmount = utils.numAdd(utils.numAdd(memberAmount, spec.addAmount), totalMakeAmount); +// specs[key] = spec; + +// dishObject.boxPrice = boxPrice; +// dishObject.num += spec.num; +// dishObject.totalPrice = utils.numAdd(dishObject.totalPrice, spec.totalPrice); +// dishObject.totalMemberPrice = utils.numAdd(dishObject.totalMemberPrice, spec.totalMemberPrice); +// dishObject.totalMakeAmount = utils.numAdd(dishObject.totalMakeAmount, spec.totalMakeAmount); +// dishObject.amount = utils.numAdd(dishObject.amount, spec.amount); +// dishObject.memberAmount = utils.numAdd(dishObject.memberAmount, spec.memberAmount); +// } +// } + +// // +// dishObject.specs = specs; +// dishObject.addAmount = dishAddAmount; +// /* dishObject数量非法 设置为undefined */ +// if (dishObject.num <= 0) { +// dishObject = undefined; +// } + +// dishes[productId] = dishObject; +// storeCartMap.dishes = dishes; + +// /*dishObj 变动结束 重新计算storeCartMap金额数量*/ +// storeCartMap.totalNum = 0; +// storeCartMap.totalCash = 0; +// storeCartMap.totalMemberCash = 0; +// storeCartMap.totalMakeAmount = 0; +// for (var key in dishes) { +// var dish = dishes[key]; +// if (null != dish && undefined != dish) { +// var cash = utils.numAdd(utils.numAdd(dish.totalPrice, dish.totalMakeAmount), dish.addAmount); +// var memberCash = utils.numAdd(utils.numAdd(dish.totalMemberPrice, dish.totalMakeAmount), dish.addAmount); +// storeCartMap.totalNum += dish.num; +// storeCartMap.totalCash = utils.numAdd(storeCartMap.totalCash, cash); +// storeCartMap.totalMemberCash = utils.numAdd(storeCartMap.totalMemberCash, memberCash); +// storeCartMap.totalMakeAmount = utils.numAdd(storeCartMap.totalMakeAmount, dish.totalMakeAmount); +// } +// } +// if (storeCartMap.totalNum <= 0) { +// storeCartMap = undefined; +// } +// cartMap[storeId] = storeCartMap; +// this.setStoreCartMap({ +// storeId: storeId, +// storeCartMap: storeCartMap +// }); +// } else { +// var amount = utils.numMulti(num, price); +// var memberAmount = utils.numMulti(num, memberPrice); + +// cartMap = new Object(); +// storeCartMap = new Object(); + +// storeCartMap.totalNum = num; +// storeCartMap.totalMakeAmount = makeAmount; +// storeCartMap.totalCash = utils.numAdd(utils.numAdd(amount, makeAmount), addAmount); +// storeCartMap.totalMemberCash = utils.numAdd(utils.numAdd(memberAmount, makeAmount), addAmount); +// /*-------菜品-------*/ +// var dishes = new Object(); +// var dishObject = new Object(); + +// dishObject.boxPrice = boxPrice; +// dishObject.name = goodsName; +// dishObject.num = num; +// dishObject.memberPrice = utils.Number(memberPrice).toFixed(2); +// dishObject.isSuit = isSuit; +// dishObject.addAmount = addAmount; + +// dishObject.totalPrice = amount; +// dishObject.totalMemberPrice = memberAmount; +// dishObject.totalMakeAmount = makeAmount; +// dishObject.amount = utils.numAdd(utils.numAdd(dishObject.totalPrice, dishObject.totalMakeAmount), addAmount); +// dishObject.memberAmount = utils.numAdd(utils.numAdd(dishObject.totalMemberPrice, dishObject.totalMakeAmount), addAmount); + +// /*-------菜品规格-------*/ +// var specs = new Object(); +// var specObject = new Object(); +// specObject.categoryId = categoryId; +// specObject.productId = productId; +// specObject.productNo = productNo; +// specObject.unitId = unitId; +// specObject.goodsId = goodsId; +// specObject.name = goodsName; +// specObject.specId = specId; +// specObject.specName = specName; +// // specObject.makeCount = makeCount; +// specObject.num = num; +// specObject.price = Number(price).toFixed(2); +// specObject.memberPrice = Number(memberPrice).toFixed(2); +// //做法信息 +// specObject.makeId = makeId; +// specObject.makeName = makeName; +// specObject.makePrice = makePrice; +// specObject.makeAmount = makeAmount; +// specObject.suitDetails = suitDetails; +// specObject.isSuit = isSuit; +// specObject.uniqueId = uniqueId; +// specObject.addAmount = addAmount; + +// specObject.totalPrice = amount; +// specObject.totalMemberPrice = memberAmount; +// specObject.totalMakeAmount = makeAmount; +// specObject.amount = utils.numAdd(utils.numAdd(specObject.totalPrice, specObject.totalMakeAmount), addAmount); +// specObject.memberAmount = utils.numAdd(utils.numAdd(specObject.totalMemberPrice, specObject.totalMakeAmount), addAmount); + +// specs[uniqueId] = specObject; +// dishObject.specs = specs; +// dishes[productId] = dishObject; +// storeCartMap.dishes = dishes; +// cartMap[storeId] = storeCartMap; +// this.setstoreCartMap({ +// storeId: storeId, +// storeCartMap: storeCartMap +// }); +// } +// return cartMap; +// }; + +// var editCartMap = function() { + +// }; + +// var addCardMap = function() { + +// }; + +// /* 清除指定门店购物车 */ +// var clearStoreCartMap = function(option) { +// var storeId = this.getStore().id; +// var cartMap = this.getCartMap(); +// if (cartMap != null) { +// cartMap[storeId] = undefined; +// } +// return this.setCartMap(cartMap); +// }; + +// /* 从购物车获取已购买商品map */ +// var getSpecMapByCart = function() { +// var specMap = {}; +// var cartStoreMap = this.getStoreCartMap(this.getStore().id); +// if (null != cartStoreMap && undefined != cartStoreMap) { +// var dishes = cartStoreMap.dishes; +// if (utils.isNotBlank(dishes)) { +// for (var productId in dishes) { +// var dishObj = dishes[productId]; +// var goodsName = dishObj.name; +// var specName = dishObj.specName; +// var specs = dishObj.specs; +// if (utils.isNotBlank(specs)) { +// for (var uniqueId in specs) { +// specs[uniqueId].name = goodsName; +// // 为了保证唯一 key=规格商品Id + 做法字符串 +// specMap[uniqueId] = specs[uniqueId]; +// } +// } +// } +// } +// } +// return specMap; +// }; + +// /* 从购物车获取已购买商品map */ +// var getDishMapByCart = function() { +// var dishMap = {}; +// var cartStoreMap = this.getStoreCartMap(this.getStore().id); +// if (null != cartStoreMap && undefined != cartStoreMap) { +// var dishes = cartStoreMap.dishes; +// if (utils.isNotBlank(dishes)) { +// for (var key in dishes) { +// var dishObj = dishes[key]; +// var goodsName = dishObj.name; +// var specName = dishObj.specName; +// var num = 0; +// // 计算商品数量和金额合计 +// var specs = dishObj.specs; +// if (utils.isNotBlank(specs)) { +// for (var jkey in specs) { +// var specObj = specs[jkey]; +// num = utils.numAdd(num, specObj.num); +// } +// } +// dishObj.num = num; + +// dishMap[key] = dishObj; +// } +// } +// } +// return dishMap; +// }; + +// /* 从购物车获取已购买商品list */ +// var getDishListByCart = function() { +// var dishList = []; + +// var cartStoreMap = this.getStoreCartMap(this.getStore().id); +// if (null != cartStoreMap && undefined != cartStoreMap) { +// var dishes = cartStoreMap.dishes; +// if (null != dishes && undefined != dishes) { +// for (var key in dishes) { +// var dishObj = dishes[key]; +// var goodsName = dishObj.name; +// var specName = dishObj.specName; +// var specs = dishObj.specs; +// if (null != specs && undefined != specs) { +// for (var jkey in specs) { +// if (undefined != specName && specName != '') { +// specs[jkey].name = "(" + specName + ") " + goodsName; +// } else { +// specs[jkey].name = goodsName; +// } +// dishList.push(specs[jkey]); +// } +// } +// } +// } +// } +// return dishList; +// }; + +// /* 获取门店菜单点菜统计 */ +// var getCartTotal = function() { +// var storeId = getStore().id; +// var storeCartMap = this.getStoreCartMap(storeId); +// // console.log(storeCartMap); +// var data = {}; +// var totalNum = 0; +// var totalCash = 0.00; +// var totalMemberCash = 0.00; +// if (utils.isNotBlank(storeCartMap)) { +// totalNum = storeCartMap.totalNum; +// totalCash = storeCartMap.totalCash; +// totalMemberCash = storeCartMap.totalMemberCash; +// } +// data.totalNum = totalNum; +// data.totalCash = Number(totalCash).toFixed(digits); +// data.totalMemberCash = Number(totalMemberCash).toFixed(digits); +// return data; +// }; + +// /* 清除购物车 */ +// var clearStoreCartMap = function(option) { +// app.takeMethod = ""; +// app.table = {}; + +// var storeId = getStore().id; +// var cartMap = this.getCartMap(); +// if (cartMap != null) { +// cartMap[storeId] = undefined; +// } +// return this.setCartMap(cartMap); +// }; + +// /** +// * 营业模式 +// */ +// var getStoreBusMode = function(index) { +// var map; +// if (index == 0) { +// map = { +// name: "堂食", +// sign: "0", +// icon: "icon-tangshi-1" +// } +// } + +// if (index == 1) { +// map = { +// name: "外带", +// sign: "1", +// icon: "icon-waidai" +// } +// } + +// if (index == 2) { +// map = { +// name: "外卖", +// sign: "2", +// icon: "icon-yunshuzhongwuliu-xianxing" +// } +// } + +// return map; +// }; + +// /**输出函数 */ +// module.exports = { +// setStore: setStore, +// getStore: getStore, +// setCartMap: setCartMap, +// getCartMap: getCartMap, +// setStoreCartMap: setStoreCartMap, +// getStoreCartMap: getStoreCartMap, +// putCartMap: putCartMap, +// getSpecMapByCart: getSpecMapByCart, +// getDishMapByCart: getDishMapByCart, +// getCartTotal: getCartTotal, +// getDishListByCart: getDishListByCart, +// clearStoreCartMap: clearStoreCartMap, +// getStoreBusMode: getStoreBusMode, +// } \ No newline at end of file diff --git a/utils/des-util.js b/utils/des-util.js new file mode 100644 index 0000000..50b81eb --- /dev/null +++ b/utils/des-util.js @@ -0,0 +1,883 @@ + +/** +* DES加密/解密 +* @Copyright Copyright (c) 2006 +* @author Guapo +* @see DESCore +*/ + +/* +* encrypt the string to string made up of hex +* return the encrypted string +*/ +function strEnc(data, firstKey, secondKey, thirdKey) { + + var leng = data.length; + var encData = ""; + var firstKeyBt, secondKeyBt, thirdKeyBt, firstLength, secondLength, thirdLength; + if (firstKey != null && firstKey != "") { + firstKeyBt = getKeyBytes(firstKey); + firstLength = firstKeyBt.length; + } + if (secondKey != null && secondKey != "") { + secondKeyBt = getKeyBytes(secondKey); + secondLength = secondKeyBt.length; + } + if (thirdKey != null && thirdKey != "") { + thirdKeyBt = getKeyBytes(thirdKey); + thirdLength = thirdKeyBt.length; + } + + if (leng > 0) { + if (leng < 4) { + var bt = strToBt(data); + var encByte; + if (firstKey != null && firstKey != "" && secondKey != null && secondKey != "" && thirdKey != null && thirdKey != "") { + var tempBt; + var x, y, z; + tempBt = bt; + for (x = 0; x < firstLength; x++) { + tempBt = enc(tempBt, firstKeyBt[x]); + } + for (y = 0; y < secondLength; y++) { + tempBt = enc(tempBt, secondKeyBt[y]); + } + for (z = 0; z < thirdLength; z++) { + tempBt = enc(tempBt, thirdKeyBt[z]); + } + encByte = tempBt; + } else { + if (firstKey != null && firstKey != "" && secondKey != null && secondKey != "") { + var tempBt; + var x, y; + tempBt = bt; + for (x = 0; x < firstLength; x++) { + tempBt = enc(tempBt, firstKeyBt[x]); + } + for (y = 0; y < secondLength; y++) { + tempBt = enc(tempBt, secondKeyBt[y]); + } + encByte = tempBt; + } else { + if (firstKey != null && firstKey != "") { + var tempBt; + var x = 0; + tempBt = bt; + for (x = 0; x < firstLength; x++) { + tempBt = enc(tempBt, firstKeyBt[x]); + } + encByte = tempBt; + } + } + } + encData = bt64ToHex(encByte); + } else { + var iterator = parseInt(leng / 4); + var remainder = leng % 4; + var i = 0; + for (i = 0; i < iterator; i++) { + var tempData = data.substring(i * 4 + 0, i * 4 + 4); + var tempByte = strToBt(tempData); + var encByte; + if (firstKey != null && firstKey != "" && secondKey != null && secondKey != "" && thirdKey != null && thirdKey != "") { + var tempBt; + var x, y, z; + tempBt = tempByte; + for (x = 0; x < firstLength; x++) { + tempBt = enc(tempBt, firstKeyBt[x]); + } + for (y = 0; y < secondLength; y++) { + tempBt = enc(tempBt, secondKeyBt[y]); + } + for (z = 0; z < thirdLength; z++) { + tempBt = enc(tempBt, thirdKeyBt[z]); + } + encByte = tempBt; + } else { + if (firstKey != null && firstKey != "" && secondKey != null && secondKey != "") { + var tempBt; + var x, y; + tempBt = tempByte; + for (x = 0; x < firstLength; x++) { + tempBt = enc(tempBt, firstKeyBt[x]); + } + for (y = 0; y < secondLength; y++) { + tempBt = enc(tempBt, secondKeyBt[y]); + } + encByte = tempBt; + } else { + if (firstKey != null && firstKey != "") { + var tempBt; + var x; + tempBt = tempByte; + for (x = 0; x < firstLength; x++) { + tempBt = enc(tempBt, firstKeyBt[x]); + } + encByte = tempBt; + } + } + } + encData += bt64ToHex(encByte); + } + if (remainder > 0) { + var remainderData = data.substring(iterator * 4 + 0, leng); + var tempByte = strToBt(remainderData); + var encByte; + if (firstKey != null && firstKey != "" && secondKey != null && secondKey != "" && thirdKey != null && thirdKey != "") { + var tempBt; + var x, y, z; + tempBt = tempByte; + for (x = 0; x < firstLength; x++) { + tempBt = enc(tempBt, firstKeyBt[x]); + } + for (y = 0; y < secondLength; y++) { + tempBt = enc(tempBt, secondKeyBt[y]); + } + for (z = 0; z < thirdLength; z++) { + tempBt = enc(tempBt, thirdKeyBt[z]); + } + encByte = tempBt; + } else { + if (firstKey != null && firstKey != "" && secondKey != null && secondKey != "") { + var tempBt; + var x, y; + tempBt = tempByte; + for (x = 0; x < firstLength; x++) { + tempBt = enc(tempBt, firstKeyBt[x]); + } + for (y = 0; y < secondLength; y++) { + tempBt = enc(tempBt, secondKeyBt[y]); + } + encByte = tempBt; + } else { + if (firstKey != null && firstKey != "") { + var tempBt; + var x; + tempBt = tempByte; + for (x = 0; x < firstLength; x++) { + tempBt = enc(tempBt, firstKeyBt[x]); + } + encByte = tempBt; + } + } + } + encData += bt64ToHex(encByte); + } + } + } + return encData; +} + +/* +* decrypt the encrypted string to the original string +* +* return the original string +*/ +function strDec(data, firstKey, secondKey, thirdKey) { + var leng = data.length; + var decStr = ""; + var firstKeyBt, secondKeyBt, thirdKeyBt, firstLength, secondLength, thirdLength; + if (firstKey != null && firstKey != "") { + firstKeyBt = getKeyBytes(firstKey); + firstLength = firstKeyBt.length; + } + if (secondKey != null && secondKey != "") { + secondKeyBt = getKeyBytes(secondKey); + secondLength = secondKeyBt.length; + } + if (thirdKey != null && thirdKey != "") { + thirdKeyBt = getKeyBytes(thirdKey); + thirdLength = thirdKeyBt.length; + } + + var iterator = parseInt(leng / 16); + var i = 0; + for (i = 0; i < iterator; i++) { + var tempData = data.substring(i * 16 + 0, i * 16 + 16); + var strByte = hexToBt64(tempData); + var intByte = new Array(64); + var j = 0; + for (j = 0; j < 64; j++) { + intByte[j] = parseInt(strByte.substring(j, j + 1)); + } + var decByte; + if (firstKey != null && firstKey != "" && secondKey != null && secondKey != "" && thirdKey != null && thirdKey != "") { + var tempBt; + var x, y, z; + tempBt = intByte; + for (x = thirdLength - 1; x >= 0; x--) { + tempBt = dec(tempBt, thirdKeyBt[x]); + } + for (y = secondLength - 1; y >= 0; y--) { + tempBt = dec(tempBt, secondKeyBt[y]); + } + for (z = firstLength - 1; z >= 0; z--) { + tempBt = dec(tempBt, firstKeyBt[z]); + } + decByte = tempBt; + } else { + if (firstKey != null && firstKey != "" && secondKey != null && secondKey != "") { + var tempBt; + var x, y, z; + tempBt = intByte; + for (x = secondLength - 1; x >= 0; x--) { + tempBt = dec(tempBt, secondKeyBt[x]); + } + for (y = firstLength - 1; y >= 0; y--) { + tempBt = dec(tempBt, firstKeyBt[y]); + } + decByte = tempBt; + } else { + if (firstKey != null && firstKey != "") { + var tempBt; + var x, y, z; + tempBt = intByte; + for (x = firstLength - 1; x >= 0; x--) { + tempBt = dec(tempBt, firstKeyBt[x]); + } + decByte = tempBt; + } + } + } + decStr += byteToString(decByte); + } + return decStr; +} +/* +* chang the string into the bit array +* +* return bit array(it's length % 64 = 0) +*/ +function getKeyBytes(key) { + var keyBytes = new Array(); + var leng = key.length; + var iterator = parseInt(leng / 4); + var remainder = leng % 4; + var i = 0; + for (i = 0; i < iterator; i++) { + keyBytes[i] = strToBt(key.substring(i * 4 + 0, i * 4 + 4)); + } + if (remainder > 0) { + keyBytes[i] = strToBt(key.substring(i * 4 + 0, leng)); + } + return keyBytes; +} + +/* +* chang the string(it's length <= 4) into the bit array +* +* return bit array(it's length = 64) +*/ +function strToBt(str) { + var leng = str.length; + var bt = new Array(64); + if (leng < 4) { + var i = 0, j = 0, p = 0, q = 0; + for (i = 0; i < leng; i++) { + var k = str.charCodeAt(i); + for (j = 0; j < 16; j++) { + var pow = 1, m = 0; + for (m = 15; m > j; m--) { + pow *= 2; + } + bt[16 * i + j] = parseInt(k / pow) % 2; + } + } + for (p = leng; p < 4; p++) { + var k = 0; + for (q = 0; q < 16; q++) { + var pow = 1, m = 0; + for (m = 15; m > q; m--) { + pow *= 2; + } + bt[16 * p + q] = parseInt(k / pow) % 2; + } + } + } else { + for (i = 0; i < 4; i++) { + var k = str.charCodeAt(i); + for (j = 0; j < 16; j++) { + var pow = 1; + for (m = 15; m > j; m--) { + pow *= 2; + } + bt[16 * i + j] = parseInt(k / pow) % 2; + } + } + } + return bt; +} + +/* +* chang the bit(it's length = 4) into the hex +* +* return hex +*/ +function bt4ToHex(binary) { + var hex; + switch (binary) { + case "0000": hex = "0"; break; + case "0001": hex = "1"; break; + case "0010": hex = "2"; break; + case "0011": hex = "3"; break; + case "0100": hex = "4"; break; + case "0101": hex = "5"; break; + case "0110": hex = "6"; break; + case "0111": hex = "7"; break; + case "1000": hex = "8"; break; + case "1001": hex = "9"; break; + case "1010": hex = "A"; break; + case "1011": hex = "B"; break; + case "1100": hex = "C"; break; + case "1101": hex = "D"; break; + case "1110": hex = "E"; break; + case "1111": hex = "F"; break; + } + return hex; +} + +/* +* chang the hex into the bit(it's length = 4) +* +* return the bit(it's length = 4) +*/ +function hexToBt4(hex) { + var binary; + switch (hex) { + case "0": binary = "0000"; break; + case "1": binary = "0001"; break; + case "2": binary = "0010"; break; + case "3": binary = "0011"; break; + case "4": binary = "0100"; break; + case "5": binary = "0101"; break; + case "6": binary = "0110"; break; + case "7": binary = "0111"; break; + case "8": binary = "1000"; break; + case "9": binary = "1001"; break; + case "A": binary = "1010"; break; + case "B": binary = "1011"; break; + case "C": binary = "1100"; break; + case "D": binary = "1101"; break; + case "E": binary = "1110"; break; + case "F": binary = "1111"; break; + } + return binary; +} + +/* +* chang the bit(it's length = 64) into the string +* +* return string +*/ +function byteToString(byteData) { + var str = ""; + for (var i = 0; i < 4; i++) { + var count = 0; + for (var j = 0; j < 16; j++) { + var pow = 1; + for (var m = 15; m > j; m--) { + pow *= 2; + } + count += byteData[16 * i + j] * pow; + } + if (count != 0) { + str += String.fromCharCode(count); + } + } + return str; +} + +function bt64ToHex(byteData) { + var hex = ""; + for (var i = 0; i < 16; i++) { + var bt = ""; + for (var j = 0; j < 4; j++) { + bt += byteData[i * 4 + j]; + } + hex += bt4ToHex(bt); + } + return hex; +} + +function hexToBt64(hex) { + var binary = ""; + for (var i = 0; i < 16; i++) { + binary += hexToBt4(hex.substring(i, i + 1)); + } + return binary; +} + +/* +* the 64 bit des core arithmetic +*/ + +function enc(dataByte, keyByte) { + var keys = generateKeys(keyByte); + var ipByte = initPermute(dataByte); + var ipLeft = new Array(32); + var ipRight = new Array(32); + var tempLeft = new Array(32); + var i = 0, j = 0, k = 0, m = 0, n = 0; + for (k = 0; k < 32; k++) { + ipLeft[k] = ipByte[k]; + ipRight[k] = ipByte[32 + k]; + } + for (i = 0; i < 16; i++) { + for (j = 0; j < 32; j++) { + tempLeft[j] = ipLeft[j]; + ipLeft[j] = ipRight[j]; + } + var key = new Array(48); + for (m = 0; m < 48; m++) { + key[m] = keys[i][m]; + } + var tempRight = xor(pPermute(sBoxPermute(xor(expandPermute(ipRight), key))), tempLeft); + for (n = 0; n < 32; n++) { + ipRight[n] = tempRight[n]; + } + + } + + + var finalData = new Array(64); + for (i = 0; i < 32; i++) { + finalData[i] = ipRight[i]; + finalData[32 + i] = ipLeft[i]; + } + return finallyPermute(finalData); +} + +function dec(dataByte, keyByte) { + var keys = generateKeys(keyByte); + var ipByte = initPermute(dataByte); + var ipLeft = new Array(32); + var ipRight = new Array(32); + var tempLeft = new Array(32); + var i = 0, j = 0, k = 0, m = 0, n = 0; + for (k = 0; k < 32; k++) { + ipLeft[k] = ipByte[k]; + ipRight[k] = ipByte[32 + k]; + } + for (i = 15; i >= 0; i--) { + for (j = 0; j < 32; j++) { + tempLeft[j] = ipLeft[j]; + ipLeft[j] = ipRight[j]; + } + var key = new Array(48); + for (m = 0; m < 48; m++) { + key[m] = keys[i][m]; + } + + var tempRight = xor(pPermute(sBoxPermute(xor(expandPermute(ipRight), key))), tempLeft); + for (n = 0; n < 32; n++) { + ipRight[n] = tempRight[n]; + } + } + + + var finalData = new Array(64); + for (i = 0; i < 32; i++) { + finalData[i] = ipRight[i]; + finalData[32 + i] = ipLeft[i]; + } + return finallyPermute(finalData); +} + +function initPermute(originalData) { + var ipByte = new Array(64); + for (var i = 0, m = 1, n = 0; i < 4; i++ , m += 2, n += 2) { + for (var j = 7, k = 0; j >= 0; j-- , k++) { + ipByte[i * 8 + k] = originalData[j * 8 + m]; + ipByte[i * 8 + k + 32] = originalData[j * 8 + n]; + } + } + return ipByte; +} + +function expandPermute(rightData) { + var epByte = new Array(48); + for (var i = 0; i < 8; i++) { + if (i == 0) { + epByte[i * 6 + 0] = rightData[31]; + } else { + epByte[i * 6 + 0] = rightData[i * 4 - 1]; + } + epByte[i * 6 + 1] = rightData[i * 4 + 0]; + epByte[i * 6 + 2] = rightData[i * 4 + 1]; + epByte[i * 6 + 3] = rightData[i * 4 + 2]; + epByte[i * 6 + 4] = rightData[i * 4 + 3]; + if (i == 7) { + epByte[i * 6 + 5] = rightData[0]; + } else { + epByte[i * 6 + 5] = rightData[i * 4 + 4]; + } + } + return epByte; +} + +function xor(byteOne, byteTwo) { + var xorByte = new Array(byteOne.length); + for (var i = 0; i < byteOne.length; i++) { + xorByte[i] = byteOne[i] ^ byteTwo[i]; + } + return xorByte; +} + +function sBoxPermute(expandByte) { + + var sBoxByte = new Array(32); + var binary = ""; + var s1 = [ + [14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7], + [0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8], + [4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0], + [15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13]]; + + /* Table - s2 */ + var s2 = [ + [15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10], + [3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5], + [0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15], + [13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9]]; + + /* Table - s3 */ + var s3 = [ + [10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8], + [13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1], + [13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7], + [1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12]]; + /* Table - s4 */ + var s4 = [ + [7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15], + [13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9], + [10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4], + [3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14]]; + + /* Table - s5 */ + var s5 = [ + [2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9], + [14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6], + [4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14], + [11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3]]; + + /* Table - s6 */ + var s6 = [ + [12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11], + [10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8], + [9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6], + [4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13]]; + + /* Table - s7 */ + var s7 = [ + [4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1], + [13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6], + [1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2], + [6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12]]; + + /* Table - s8 */ + var s8 = [ + [13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7], + [1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2], + [7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8], + [2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11]]; + + for (var m = 0; m < 8; m++) { + var i = 0, j = 0; + i = expandByte[m * 6 + 0] * 2 + expandByte[m * 6 + 5]; + j = expandByte[m * 6 + 1] * 2 * 2 * 2 + + expandByte[m * 6 + 2] * 2 * 2 + + expandByte[m * 6 + 3] * 2 + + expandByte[m * 6 + 4]; + switch (m) { + case 0: + binary = getBoxBinary(s1[i][j]); + break; + case 1: + binary = getBoxBinary(s2[i][j]); + break; + case 2: + binary = getBoxBinary(s3[i][j]); + break; + case 3: + binary = getBoxBinary(s4[i][j]); + break; + case 4: + binary = getBoxBinary(s5[i][j]); + break; + case 5: + binary = getBoxBinary(s6[i][j]); + break; + case 6: + binary = getBoxBinary(s7[i][j]); + break; + case 7: + binary = getBoxBinary(s8[i][j]); + break; + } + sBoxByte[m * 4 + 0] = parseInt(binary.substring(0, 1)); + sBoxByte[m * 4 + 1] = parseInt(binary.substring(1, 2)); + sBoxByte[m * 4 + 2] = parseInt(binary.substring(2, 3)); + sBoxByte[m * 4 + 3] = parseInt(binary.substring(3, 4)); + } + return sBoxByte; +} + +function pPermute(sBoxByte) { + var pBoxPermute = new Array(32); + pBoxPermute[0] = sBoxByte[15]; + pBoxPermute[1] = sBoxByte[6]; + pBoxPermute[2] = sBoxByte[19]; + pBoxPermute[3] = sBoxByte[20]; + pBoxPermute[4] = sBoxByte[28]; + pBoxPermute[5] = sBoxByte[11]; + pBoxPermute[6] = sBoxByte[27]; + pBoxPermute[7] = sBoxByte[16]; + pBoxPermute[8] = sBoxByte[0]; + pBoxPermute[9] = sBoxByte[14]; + pBoxPermute[10] = sBoxByte[22]; + pBoxPermute[11] = sBoxByte[25]; + pBoxPermute[12] = sBoxByte[4]; + pBoxPermute[13] = sBoxByte[17]; + pBoxPermute[14] = sBoxByte[30]; + pBoxPermute[15] = sBoxByte[9]; + pBoxPermute[16] = sBoxByte[1]; + pBoxPermute[17] = sBoxByte[7]; + pBoxPermute[18] = sBoxByte[23]; + pBoxPermute[19] = sBoxByte[13]; + pBoxPermute[20] = sBoxByte[31]; + pBoxPermute[21] = sBoxByte[26]; + pBoxPermute[22] = sBoxByte[2]; + pBoxPermute[23] = sBoxByte[8]; + pBoxPermute[24] = sBoxByte[18]; + pBoxPermute[25] = sBoxByte[12]; + pBoxPermute[26] = sBoxByte[29]; + pBoxPermute[27] = sBoxByte[5]; + pBoxPermute[28] = sBoxByte[21]; + pBoxPermute[29] = sBoxByte[10]; + pBoxPermute[30] = sBoxByte[3]; + pBoxPermute[31] = sBoxByte[24]; + return pBoxPermute; +} + +function finallyPermute(endByte) { + var fpByte = new Array(64); + fpByte[0] = endByte[39]; + fpByte[1] = endByte[7]; + fpByte[2] = endByte[47]; + fpByte[3] = endByte[15]; + fpByte[4] = endByte[55]; + fpByte[5] = endByte[23]; + fpByte[6] = endByte[63]; + fpByte[7] = endByte[31]; + fpByte[8] = endByte[38]; + fpByte[9] = endByte[6]; + fpByte[10] = endByte[46]; + fpByte[11] = endByte[14]; + fpByte[12] = endByte[54]; + fpByte[13] = endByte[22]; + fpByte[14] = endByte[62]; + fpByte[15] = endByte[30]; + fpByte[16] = endByte[37]; + fpByte[17] = endByte[5]; + fpByte[18] = endByte[45]; + fpByte[19] = endByte[13]; + fpByte[20] = endByte[53]; + fpByte[21] = endByte[21]; + fpByte[22] = endByte[61]; + fpByte[23] = endByte[29]; + fpByte[24] = endByte[36]; + fpByte[25] = endByte[4]; + fpByte[26] = endByte[44]; + fpByte[27] = endByte[12]; + fpByte[28] = endByte[52]; + fpByte[29] = endByte[20]; + fpByte[30] = endByte[60]; + fpByte[31] = endByte[28]; + fpByte[32] = endByte[35]; + fpByte[33] = endByte[3]; + fpByte[34] = endByte[43]; + fpByte[35] = endByte[11]; + fpByte[36] = endByte[51]; + fpByte[37] = endByte[19]; + fpByte[38] = endByte[59]; + fpByte[39] = endByte[27]; + fpByte[40] = endByte[34]; + fpByte[41] = endByte[2]; + fpByte[42] = endByte[42]; + fpByte[43] = endByte[10]; + fpByte[44] = endByte[50]; + fpByte[45] = endByte[18]; + fpByte[46] = endByte[58]; + fpByte[47] = endByte[26]; + fpByte[48] = endByte[33]; + fpByte[49] = endByte[1]; + fpByte[50] = endByte[41]; + fpByte[51] = endByte[9]; + fpByte[52] = endByte[49]; + fpByte[53] = endByte[17]; + fpByte[54] = endByte[57]; + fpByte[55] = endByte[25]; + fpByte[56] = endByte[32]; + fpByte[57] = endByte[0]; + fpByte[58] = endByte[40]; + fpByte[59] = endByte[8]; + fpByte[60] = endByte[48]; + fpByte[61] = endByte[16]; + fpByte[62] = endByte[56]; + fpByte[63] = endByte[24]; + return fpByte; +} + +function getBoxBinary(i) { + var binary = ""; + switch (i) { + case 0: binary = "0000"; break; + case 1: binary = "0001"; break; + case 2: binary = "0010"; break; + case 3: binary = "0011"; break; + case 4: binary = "0100"; break; + case 5: binary = "0101"; break; + case 6: binary = "0110"; break; + case 7: binary = "0111"; break; + case 8: binary = "1000"; break; + case 9: binary = "1001"; break; + case 10: binary = "1010"; break; + case 11: binary = "1011"; break; + case 12: binary = "1100"; break; + case 13: binary = "1101"; break; + case 14: binary = "1110"; break; + case 15: binary = "1111"; break; + } + return binary; +} +/* +* generate 16 keys for xor +* +*/ +function generateKeys(keyByte) { + var key = new Array(56); + var keys = new Array(); + + keys[0] = new Array(); + keys[1] = new Array(); + keys[2] = new Array(); + keys[3] = new Array(); + keys[4] = new Array(); + keys[5] = new Array(); + keys[6] = new Array(); + keys[7] = new Array(); + keys[8] = new Array(); + keys[9] = new Array(); + keys[10] = new Array(); + keys[11] = new Array(); + keys[12] = new Array(); + keys[13] = new Array(); + keys[14] = new Array(); + keys[15] = new Array(); + var loop = [1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1]; + + for (var i = 0; i < 7; i++) { + for (var j = 0, k = 7; j < 8; j++ , k--) { + key[i * 8 + j] = keyByte[8 * k + i]; + } + } + + var i = 0; + for (i = 0; i < 16; i++) { + var tempLeft = 0; + var tempRight = 0; + for (j = 0; j < loop[i]; j++) { + tempLeft = key[0]; + tempRight = key[28]; + for (k = 0; k < 27; k++) { + key[k] = key[k + 1]; + key[28 + k] = key[29 + k]; + } + key[27] = tempLeft; + key[55] = tempRight; + } + var tempKey = new Array(48); + tempKey[0] = key[13]; + tempKey[1] = key[16]; + tempKey[2] = key[10]; + tempKey[3] = key[23]; + tempKey[4] = key[0]; + tempKey[5] = key[4]; + tempKey[6] = key[2]; + tempKey[7] = key[27]; + tempKey[8] = key[14]; + tempKey[9] = key[5]; + tempKey[10] = key[20]; + tempKey[11] = key[9]; + tempKey[12] = key[22]; + tempKey[13] = key[18]; + tempKey[14] = key[11]; + tempKey[15] = key[3]; + tempKey[16] = key[25]; + tempKey[17] = key[7]; + tempKey[18] = key[15]; + tempKey[19] = key[6]; + tempKey[20] = key[26]; + tempKey[21] = key[19]; + tempKey[22] = key[12]; + tempKey[23] = key[1]; + tempKey[24] = key[40]; + tempKey[25] = key[51]; + tempKey[26] = key[30]; + tempKey[27] = key[36]; + tempKey[28] = key[46]; + tempKey[29] = key[54]; + tempKey[30] = key[29]; + tempKey[31] = key[39]; + tempKey[32] = key[50]; + tempKey[33] = key[44]; + tempKey[34] = key[32]; + tempKey[35] = key[47]; + tempKey[36] = key[43]; + tempKey[37] = key[48]; + tempKey[38] = key[38]; + tempKey[39] = key[55]; + tempKey[40] = key[33]; + tempKey[41] = key[52]; + tempKey[42] = key[45]; + tempKey[43] = key[41]; + tempKey[44] = key[49]; + tempKey[45] = key[35]; + tempKey[46] = key[28]; + tempKey[47] = key[31]; + var m = 0; + switch (i) { + case 0: for (m = 0; m < 48; m++) { keys[0][m] = tempKey[m]; } break; + case 1: for (m = 0; m < 48; m++) { keys[1][m] = tempKey[m]; } break; + case 2: for (m = 0; m < 48; m++) { keys[2][m] = tempKey[m]; } break; + case 3: for (m = 0; m < 48; m++) { keys[3][m] = tempKey[m]; } break; + case 4: for (m = 0; m < 48; m++) { keys[4][m] = tempKey[m]; } break; + case 5: for (m = 0; m < 48; m++) { keys[5][m] = tempKey[m]; } break; + case 6: for (m = 0; m < 48; m++) { keys[6][m] = tempKey[m]; } break; + case 7: for (m = 0; m < 48; m++) { keys[7][m] = tempKey[m]; } break; + case 8: for (m = 0; m < 48; m++) { keys[8][m] = tempKey[m]; } break; + case 9: for (m = 0; m < 48; m++) { keys[9][m] = tempKey[m]; } break; + case 10: for (m = 0; m < 48; m++) { keys[10][m] = tempKey[m]; } break; + case 11: for (m = 0; m < 48; m++) { keys[11][m] = tempKey[m]; } break; + case 12: for (m = 0; m < 48; m++) { keys[12][m] = tempKey[m]; } break; + case 13: for (m = 0; m < 48; m++) { keys[13][m] = tempKey[m]; } break; + case 14: for (m = 0; m < 48; m++) { keys[14][m] = tempKey[m]; } break; + case 15: for (m = 0; m < 48; m++) { keys[15][m] = tempKey[m]; } break; + } + } + return keys; +} + +function encryptString(encStr) { + return strEnc(encStr, '$QBOSSY$', '$QBOSSY$', '$QBOSSY$') +} + +function decryptString(decStr) { + return strDec(decStr, '$QBOSSY$', '$QBOSSY$', '$QBOSSY$'); +} + +module.exports = { + encryptString: encryptString, + decryptString: decryptString +} + diff --git a/utils/des.js b/utils/des.js new file mode 100644 index 0000000..4fa6205 --- /dev/null +++ b/utils/des.js @@ -0,0 +1,221 @@ +//Paul Tero, July 2001 +//http://www.tero.co.uk/des/ +// +//Optimised for performance with large blocks by Michael Hayworth, November 2001 +//http://www.netdealing.com +// +//THIS SOFTWARE IS PROVIDED "AS IS" AND +//ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +//IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +//ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +//FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +//DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +//OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +//HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +//LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +//OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +//SUCH DAMAGE. + +//des +//this takes the key, the message, and whether to encrypt or decrypt +var des = function des(key, message, encrypt, mode, iv, padding) { + //declaring this locally speeds things up a bit + var spfunction1 = new Array(0x1010400, 0, 0x10000, 0x1010404, 0x1010004, 0x10404, 0x4, 0x10000, 0x400, 0x1010400, 0x1010404, 0x400, 0x1000404, 0x1010004, 0x1000000, 0x4, 0x404, 0x1000400, 0x1000400, 0x10400, 0x10400, 0x1010000, 0x1010000, 0x1000404, 0x10004, 0x1000004, 0x1000004, 0x10004, 0, 0x404, 0x10404, 0x1000000, 0x10000, 0x1010404, 0x4, 0x1010000, 0x1010400, 0x1000000, 0x1000000, 0x400, 0x1010004, 0x10000, 0x10400, 0x1000004, 0x400, 0x4, 0x1000404, 0x10404, 0x1010404, 0x10004, 0x1010000, 0x1000404, 0x1000004, 0x404, 0x10404, 0x1010400, 0x404, 0x1000400, 0x1000400, 0, 0x10004, 0x10400, 0, 0x1010004); + var spfunction2 = new Array(-0x7fef7fe0, -0x7fff8000, 0x8000, 0x108020, 0x100000, 0x20, -0x7fefffe0, -0x7fff7fe0, -0x7fffffe0, -0x7fef7fe0, -0x7fef8000, -0x80000000, -0x7fff8000, 0x100000, 0x20, -0x7fefffe0, 0x108000, 0x100020, -0x7fff7fe0, 0, -0x80000000, 0x8000, 0x108020, -0x7ff00000, 0x100020, -0x7fffffe0, 0, 0x108000, 0x8020, -0x7fef8000, -0x7ff00000, 0x8020, 0, 0x108020, -0x7fefffe0, 0x100000, -0x7fff7fe0, -0x7ff00000, -0x7fef8000, 0x8000, -0x7ff00000, -0x7fff8000, 0x20, -0x7fef7fe0, 0x108020, 0x20, 0x8000, -0x80000000, 0x8020, -0x7fef8000, 0x100000, -0x7fffffe0, 0x100020, -0x7fff7fe0, -0x7fffffe0, 0x100020, 0x108000, 0, -0x7fff8000, 0x8020, -0x80000000, -0x7fefffe0, -0x7fef7fe0, 0x108000); + var spfunction3 = new Array(0x208, 0x8020200, 0, 0x8020008, 0x8000200, 0, 0x20208, 0x8000200, 0x20008, 0x8000008, 0x8000008, 0x20000, 0x8020208, 0x20008, 0x8020000, 0x208, 0x8000000, 0x8, 0x8020200, 0x200, 0x20200, 0x8020000, 0x8020008, 0x20208, 0x8000208, 0x20200, 0x20000, 0x8000208, 0x8, 0x8020208, 0x200, 0x8000000, 0x8020200, 0x8000000, 0x20008, 0x208, 0x20000, 0x8020200, 0x8000200, 0, 0x200, 0x20008, 0x8020208, 0x8000200, 0x8000008, 0x200, 0, 0x8020008, 0x8000208, 0x20000, 0x8000000, 0x8020208, 0x8, 0x20208, 0x20200, 0x8000008, 0x8020000, 0x8000208, 0x208, 0x8020000, 0x20208, 0x8, 0x8020008, 0x20200); + var spfunction4 = new Array(0x802001, 0x2081, 0x2081, 0x80, 0x802080, 0x800081, 0x800001, 0x2001, 0, 0x802000, 0x802000, 0x802081, 0x81, 0, 0x800080, 0x800001, 0x1, 0x2000, 0x800000, 0x802001, 0x80, 0x800000, 0x2001, 0x2080, 0x800081, 0x1, 0x2080, 0x800080, 0x2000, 0x802080, 0x802081, 0x81, 0x800080, 0x800001, 0x802000, 0x802081, 0x81, 0, 0, 0x802000, 0x2080, 0x800080, 0x800081, 0x1, 0x802001, 0x2081, 0x2081, 0x80, 0x802081, 0x81, 0x1, 0x2000, 0x800001, 0x2001, 0x802080, 0x800081, 0x2001, 0x2080, 0x800000, 0x802001, 0x80, 0x800000, 0x2000, 0x802080); + var spfunction5 = new Array(0x100, 0x2080100, 0x2080000, 0x42000100, 0x80000, 0x100, 0x40000000, 0x2080000, 0x40080100, 0x80000, 0x2000100, 0x40080100, 0x42000100, 0x42080000, 0x80100, 0x40000000, 0x2000000, 0x40080000, 0x40080000, 0, 0x40000100, 0x42080100, 0x42080100, 0x2000100, 0x42080000, 0x40000100, 0, 0x42000000, 0x2080100, 0x2000000, 0x42000000, 0x80100, 0x80000, 0x42000100, 0x100, 0x2000000, 0x40000000, 0x2080000, 0x42000100, 0x40080100, 0x2000100, 0x40000000, 0x42080000, 0x2080100, 0x40080100, 0x100, 0x2000000, 0x42080000, 0x42080100, 0x80100, 0x42000000, 0x42080100, 0x2080000, 0, 0x40080000, 0x42000000, 0x80100, 0x2000100, 0x40000100, 0x80000, 0, 0x40080000, 0x2080100, 0x40000100); + var spfunction6 = new Array(0x20000010, 0x20400000, 0x4000, 0x20404010, 0x20400000, 0x10, 0x20404010, 0x400000, 0x20004000, 0x404010, 0x400000, 0x20000010, 0x400010, 0x20004000, 0x20000000, 0x4010, 0, 0x400010, 0x20004010, 0x4000, 0x404000, 0x20004010, 0x10, 0x20400010, 0x20400010, 0, 0x404010, 0x20404000, 0x4010, 0x404000, 0x20404000, 0x20000000, 0x20004000, 0x10, 0x20400010, 0x404000, 0x20404010, 0x400000, 0x4010, 0x20000010, 0x400000, 0x20004000, 0x20000000, 0x4010, 0x20000010, 0x20404010, 0x404000, 0x20400000, 0x404010, 0x20404000, 0, 0x20400010, 0x10, 0x4000, 0x20400000, 0x404010, 0x4000, 0x400010, 0x20004010, 0, 0x20404000, 0x20000000, 0x400010, 0x20004010); + var spfunction7 = new Array(0x200000, 0x4200002, 0x4000802, 0, 0x800, 0x4000802, 0x200802, 0x4200800, 0x4200802, 0x200000, 0, 0x4000002, 0x2, 0x4000000, 0x4200002, 0x802, 0x4000800, 0x200802, 0x200002, 0x4000800, 0x4000002, 0x4200000, 0x4200800, 0x200002, 0x4200000, 0x800, 0x802, 0x4200802, 0x200800, 0x2, 0x4000000, 0x200800, 0x4000000, 0x200800, 0x200000, 0x4000802, 0x4000802, 0x4200002, 0x4200002, 0x2, 0x200002, 0x4000000, 0x4000800, 0x200000, 0x4200800, 0x802, 0x200802, 0x4200800, 0x802, 0x4000002, 0x4200802, 0x4200000, 0x200800, 0, 0x2, 0x4200802, 0, 0x200802, 0x4200000, 0x800, 0x4000002, 0x4000800, 0x800, 0x200002); + var spfunction8 = new Array(0x10001040, 0x1000, 0x40000, 0x10041040, 0x10000000, 0x10001040, 0x40, 0x10000000, 0x40040, 0x10040000, 0x10041040, 0x41000, 0x10041000, 0x41040, 0x1000, 0x40, 0x10040000, 0x10000040, 0x10001000, 0x1040, 0x41000, 0x40040, 0x10040040, 0x10041000, 0x1040, 0, 0, 0x10040040, 0x10000040, 0x10001000, 0x41040, 0x40000, 0x41040, 0x40000, 0x10041000, 0x1000, 0x40, 0x10040040, 0x1000, 0x41040, 0x10001000, 0x40, 0x10000040, 0x10040000, 0x10040040, 0x10000000, 0x40000, 0x10001040, 0, 0x10041040, 0x40040, 0x10000040, 0x10040000, 0x10001000, 0x10001040, 0, 0x10041040, 0x41000, 0x41000, 0x1040, 0x1040, 0x40040, 0x10000000, 0x10041000); + + //create the 16 or 48 subkeys we will need + var keys = des_createKeys(key); + var m = 0, i, j, temp, temp2, right1, right2, left, right, looping; + var cbcleft, cbcleft2, cbcright, cbcright2 + var endloop, loopinc; + var len = message.length; + var chunk = 0; + //set up the loops for single and triple des + var iterations = keys.length == 32 ? 3 : 9; //single or triple des + if (iterations == 3) { looping = encrypt ? new Array(0, 32, 2) : new Array(30, -2, -2); } + else { looping = encrypt ? new Array(0, 32, 2, 62, 30, -2, 64, 96, 2) : new Array(94, 62, -2, 32, 64, 2, 30, -2, -2); } + + //pad the message depending on the padding parameter + if (padding == 2) message += " "; //pad the message with spaces + else if (padding == 1) { temp = 8 - (len % 8); message += String.fromCharCode(temp, temp, temp, temp, temp, temp, temp, temp); if (temp == 8) len += 8; } //PKCS7 padding + else if (!padding) message += "\0\0\0\0\0\0\0\0"; //pad the message out with null bytes + + //store the result here + var result = ""; + var tempresult = ""; + + if (mode == 1) { //CBC mode + cbcleft = (iv.charCodeAt(m++) << 24) | (iv.charCodeAt(m++) << 16) | (iv.charCodeAt(m++) << 8) | iv.charCodeAt(m++); + cbcright = (iv.charCodeAt(m++) << 24) | (iv.charCodeAt(m++) << 16) | (iv.charCodeAt(m++) << 8) | iv.charCodeAt(m++); + m = 0; + } + + //loop through each 64 bit chunk of the message + while (m < len) { + left = (message.charCodeAt(m++) << 24) | (message.charCodeAt(m++) << 16) | (message.charCodeAt(m++) << 8) | message.charCodeAt(m++); + right = (message.charCodeAt(m++) << 24) | (message.charCodeAt(m++) << 16) | (message.charCodeAt(m++) << 8) | message.charCodeAt(m++); + + //for Cipher Block Chaining mode, xor the message with the previous result + if (mode == 1) { if (encrypt) { left ^= cbcleft; right ^= cbcright; } else { cbcleft2 = cbcleft; cbcright2 = cbcright; cbcleft = left; cbcright = right; } } + + //first each 64 but chunk of the message must be permuted according to IP + temp = ((left >>> 4) ^ right) & 0x0f0f0f0f; right ^= temp; left ^= (temp << 4); + temp = ((left >>> 16) ^ right) & 0x0000ffff; right ^= temp; left ^= (temp << 16); + temp = ((right >>> 2) ^ left) & 0x33333333; left ^= temp; right ^= (temp << 2); + temp = ((right >>> 8) ^ left) & 0x00ff00ff; left ^= temp; right ^= (temp << 8); + temp = ((left >>> 1) ^ right) & 0x55555555; right ^= temp; left ^= (temp << 1); + + left = ((left << 1) | (left >>> 31)); + right = ((right << 1) | (right >>> 31)); + + //do this either 1 or 3 times for each chunk of the message + for (j = 0; j < iterations; j += 3) { + endloop = looping[j + 1]; + loopinc = looping[j + 2]; + //now go through and perform the encryption or decryption + for (i = looping[j]; i != endloop; i += loopinc) { //for efficiency + right1 = right ^ keys[i]; + right2 = ((right >>> 4) | (right << 28)) ^ keys[i + 1]; + //the result is attained by passing these bytes through the S selection functions + temp = left; + left = right; + right = temp ^ (spfunction2[(right1 >>> 24) & 0x3f] | spfunction4[(right1 >>> 16) & 0x3f] + | spfunction6[(right1 >>> 8) & 0x3f] | spfunction8[right1 & 0x3f] + | spfunction1[(right2 >>> 24) & 0x3f] | spfunction3[(right2 >>> 16) & 0x3f] + | spfunction5[(right2 >>> 8) & 0x3f] | spfunction7[right2 & 0x3f]); + } + temp = left; left = right; right = temp; //unreverse left and right + } //for either 1 or 3 iterations + + //move then each one bit to the right + left = ((left >>> 1) | (left << 31)); + right = ((right >>> 1) | (right << 31)); + + //now perform IP-1, which is IP in the opposite direction + temp = ((left >>> 1) ^ right) & 0x55555555; right ^= temp; left ^= (temp << 1); + temp = ((right >>> 8) ^ left) & 0x00ff00ff; left ^= temp; right ^= (temp << 8); + temp = ((right >>> 2) ^ left) & 0x33333333; left ^= temp; right ^= (temp << 2); + temp = ((left >>> 16) ^ right) & 0x0000ffff; right ^= temp; left ^= (temp << 16); + temp = ((left >>> 4) ^ right) & 0x0f0f0f0f; right ^= temp; left ^= (temp << 4); + + //for Cipher Block Chaining mode, xor the message with the previous result + if (mode == 1) { if (encrypt) { cbcleft = left; cbcright = right; } else { left ^= cbcleft2; right ^= cbcright2; } } + tempresult += String.fromCharCode((left >>> 24), ((left >>> 16) & 0xff), ((left >>> 8) & 0xff), (left & 0xff), (right >>> 24), ((right >>> 16) & 0xff), ((right >>> 8) & 0xff), (right & 0xff)); + + chunk += 8; + if (chunk == 512) { result += tempresult; tempresult = ""; chunk = 0; } + } //for every 8 characters, or 64 bits in the message + + //return the result as an array + return result + tempresult; +} //end of des + + + +//des_createKeys +//this takes as input a 64 bit key (even though only 56 bits are used) +//as an array of 2 integers, and returns 16 48 bit keys +function des_createKeys(key) { + //declaring this locally speeds things up a bit + var pc2bytes0 = new Array(0, 0x4, 0x20000000, 0x20000004, 0x10000, 0x10004, 0x20010000, 0x20010004, 0x200, 0x204, 0x20000200, 0x20000204, 0x10200, 0x10204, 0x20010200, 0x20010204); + var pc2bytes1 = new Array(0, 0x1, 0x100000, 0x100001, 0x4000000, 0x4000001, 0x4100000, 0x4100001, 0x100, 0x101, 0x100100, 0x100101, 0x4000100, 0x4000101, 0x4100100, 0x4100101); + var pc2bytes2 = new Array(0, 0x8, 0x800, 0x808, 0x1000000, 0x1000008, 0x1000800, 0x1000808, 0, 0x8, 0x800, 0x808, 0x1000000, 0x1000008, 0x1000800, 0x1000808); + var pc2bytes3 = new Array(0, 0x200000, 0x8000000, 0x8200000, 0x2000, 0x202000, 0x8002000, 0x8202000, 0x20000, 0x220000, 0x8020000, 0x8220000, 0x22000, 0x222000, 0x8022000, 0x8222000); + var pc2bytes4 = new Array(0, 0x40000, 0x10, 0x40010, 0, 0x40000, 0x10, 0x40010, 0x1000, 0x41000, 0x1010, 0x41010, 0x1000, 0x41000, 0x1010, 0x41010); + var pc2bytes5 = new Array(0, 0x400, 0x20, 0x420, 0, 0x400, 0x20, 0x420, 0x2000000, 0x2000400, 0x2000020, 0x2000420, 0x2000000, 0x2000400, 0x2000020, 0x2000420); + var pc2bytes6 = new Array(0, 0x10000000, 0x80000, 0x10080000, 0x2, 0x10000002, 0x80002, 0x10080002, 0, 0x10000000, 0x80000, 0x10080000, 0x2, 0x10000002, 0x80002, 0x10080002); + var pc2bytes7 = new Array(0, 0x10000, 0x800, 0x10800, 0x20000000, 0x20010000, 0x20000800, 0x20010800, 0x20000, 0x30000, 0x20800, 0x30800, 0x20020000, 0x20030000, 0x20020800, 0x20030800); + var pc2bytes8 = new Array(0, 0x40000, 0, 0x40000, 0x2, 0x40002, 0x2, 0x40002, 0x2000000, 0x2040000, 0x2000000, 0x2040000, 0x2000002, 0x2040002, 0x2000002, 0x2040002); + var pc2bytes9 = new Array(0, 0x10000000, 0x8, 0x10000008, 0, 0x10000000, 0x8, 0x10000008, 0x400, 0x10000400, 0x408, 0x10000408, 0x400, 0x10000400, 0x408, 0x10000408); + var pc2bytes10 = new Array(0, 0x20, 0, 0x20, 0x100000, 0x100020, 0x100000, 0x100020, 0x2000, 0x2020, 0x2000, 0x2020, 0x102000, 0x102020, 0x102000, 0x102020); + var pc2bytes11 = new Array(0, 0x1000000, 0x200, 0x1000200, 0x200000, 0x1200000, 0x200200, 0x1200200, 0x4000000, 0x5000000, 0x4000200, 0x5000200, 0x4200000, 0x5200000, 0x4200200, 0x5200200); + var pc2bytes12 = new Array(0, 0x1000, 0x8000000, 0x8001000, 0x80000, 0x81000, 0x8080000, 0x8081000, 0x10, 0x1010, 0x8000010, 0x8001010, 0x80010, 0x81010, 0x8080010, 0x8081010); + var pc2bytes13 = new Array(0, 0x4, 0x100, 0x104, 0, 0x4, 0x100, 0x104, 0x1, 0x5, 0x101, 0x105, 0x1, 0x5, 0x101, 0x105); + + //how many iterations (1 for des, 3 for triple des) + var iterations = key.length > 8 ? 3 : 1; //changed by Paul 16/6/2007 to use Triple DES for 9+ byte keys + //stores the return keys + var keys = new Array(32 * iterations); + //now define the left shifts which need to be done + var shifts = new Array(0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0); + //other variables + var lefttemp, righttemp, m = 0, n = 0, temp; + + for (var j = 0; j < iterations; j++) { //either 1 or 3 iterations + var left = (key.charCodeAt(m++) << 24) | (key.charCodeAt(m++) << 16) | (key.charCodeAt(m++) << 8) | key.charCodeAt(m++); + var right = (key.charCodeAt(m++) << 24) | (key.charCodeAt(m++) << 16) | (key.charCodeAt(m++) << 8) | key.charCodeAt(m++); + + var temp = ((left >>> 4) ^ right) & 0x0f0f0f0f; right ^= temp; left ^= (temp << 4); + temp = ((right >>> -16) ^ left) & 0x0000ffff; left ^= temp; right ^= (temp << -16); + temp = ((left >>> 2) ^ right) & 0x33333333; right ^= temp; left ^= (temp << 2); + temp = ((right >>> -16) ^ left) & 0x0000ffff; left ^= temp; right ^= (temp << -16); + temp = ((left >>> 1) ^ right) & 0x55555555; right ^= temp; left ^= (temp << 1); + temp = ((right >>> 8) ^ left) & 0x00ff00ff; left ^= temp; right ^= (temp << 8); + temp = ((left >>> 1) ^ right) & 0x55555555; right ^= temp; left ^= (temp << 1); + + //the right side needs to be shifted and to get the last four bits of the left side + temp = (left << 8) | ((right >>> 20) & 0x000000f0); + //left needs to be put upside down + left = (right << 24) | ((right << 8) & 0xff0000) | ((right >>> 8) & 0xff00) | ((right >>> 24) & 0xf0); + right = temp; + + //now go through and perform these shifts on the left and right keys + for (var i = 0; i < shifts.length; i++) { + //shift the keys either one or two bits to the left + if (shifts[i]) { left = (left << 2) | (left >>> 26); right = (right << 2) | (right >>> 26); } + else { left = (left << 1) | (left >>> 27); right = (right << 1) | (right >>> 27); } + left &= -0xf; right &= -0xf; + + //now apply PC-2, in such a way that E is easier when encrypting or decrypting + //this conversion will look like PC-2 except only the last 6 bits of each byte are used + //rather than 48 consecutive bits and the order of lines will be according to + //how the S selection functions will be applied: S2, S4, S6, S8, S1, S3, S5, S7 + lefttemp = pc2bytes0[left >>> 28] | pc2bytes1[(left >>> 24) & 0xf] + | pc2bytes2[(left >>> 20) & 0xf] | pc2bytes3[(left >>> 16) & 0xf] + | pc2bytes4[(left >>> 12) & 0xf] | pc2bytes5[(left >>> 8) & 0xf] + | pc2bytes6[(left >>> 4) & 0xf]; + righttemp = pc2bytes7[right >>> 28] | pc2bytes8[(right >>> 24) & 0xf] + | pc2bytes9[(right >>> 20) & 0xf] | pc2bytes10[(right >>> 16) & 0xf] + | pc2bytes11[(right >>> 12) & 0xf] | pc2bytes12[(right >>> 8) & 0xf] + | pc2bytes13[(right >>> 4) & 0xf]; + temp = ((righttemp >>> 16) ^ lefttemp) & 0x0000ffff; + keys[n++] = lefttemp ^ temp; keys[n++] = righttemp ^ (temp << 16); + } + } //for each iterations + //return the keys we've created + return keys; +} //end of des_createKeys + + + +//////////////////////////////////////////////////////////// +function stringToHexForDES(inputS) { + var s = des('$AZKOSS$', suffix8Blank(inputS), 1, 0); + var r = ""; + var hexes = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"); + for (var i = 0; i < s.length; i++) { r += hexes[s.charCodeAt(i) >> 4] + hexes[s.charCodeAt(i) & 0xf]; } + return r; +} + +function hexToStringForDES(h) { + var r = ""; + for (var i = (h.substr(0, 2) == "0x") ? 2 : 0; i < h.length; i += 2) { r += String.fromCharCode(parseInt(h.substr(i, 2), 16)); } + return r; +} + +function suffix8Blank(str) { + for (var i = 0; i < 8; i++) { + str += " "; + } + + return str; +} + +module.exports.stringToHexForDES = stringToHexForDES; +module.exports.hexToStringForDES = hexToStringForDES; diff --git a/utils/log.js b/utils/log.js new file mode 100644 index 0000000..ac5ab42 --- /dev/null +++ b/utils/log.js @@ -0,0 +1,30 @@ +var log = wx.getRealtimeLogManager ? wx.getRealtimeLogManager() : null + +module.exports = { + debug() { + if (!log) return + log.debug.apply(log, arguments) + }, + info() { + if (!log) return + log.info.apply(log, arguments) + }, + warn() { + if (!log) return + log.warn.apply(log, arguments) + }, + error() { + if (!log) return + log.error.apply(log, arguments) + }, + setFilterMsg(msg) { // 从基础库2.7.3开始支持 + if (!log || !log.setFilterMsg) return + if (typeof msg !== 'string') return + log.setFilterMsg(msg) + }, + addFilterMsg(msg) { // 从基础库2.8.1开始支持 + if (!log || !log.addFilterMsg) return + if (typeof msg !== 'string') return + log.addFilterMsg(msg) + } +} \ No newline at end of file diff --git a/utils/loginApi.js b/utils/loginApi.js new file mode 100644 index 0000000..20ac090 --- /dev/null +++ b/utils/loginApi.js @@ -0,0 +1,211 @@ +// const app = getApp(); +// const mqUtils = require('./mqttSubstribe.js'); + + + + + + +/*********************** */ +/** + * 校验用户session信息是否有效 + */ +const checkLogin = function(userInfo) { + if (userInfo) { + wx.setStorageSync("userInfo", userInfo); + } + console.log(userInfo); + return; + var that = this; + wx.checkSession({ + success: function(res) { + console.log("检测session", res); + var openId = wx.getStorageSync("openId"); + var userId = wx.getStorageSync("userId"); + var loginKey = wx.getStorageSync("loginKey"); + if (app.utils.isBlank(openId) || app.utils.isBlank(userId) || app.utils.isBlank(loginKey)) { + that.login(); + } else { + that.checkLoginKey(userId, loginKey); + } + }, + fail: function(res) { + that.login(); + } + }); +}; + +/** + * 校验loginKey是否有效 + * 1:调用服务器接口校验loginkey是否有效 + * 1:登录有效 重置mqtt订阅topic并连接mqtt + * 0:无效 重新登录 获取loginkey + * + */ +const checkLoginKey = function(userId, loginKey) { + var that = this; + wx.request({ + url: app.globalData.serverUrlExtend, + method: "POST", + header: { + 'content-type': 'application/x-www-form-urlencoded' + }, + data: { + "type": "wxdcechecksession", + "userId": userId, + "loginKey": loginKey + }, + success: function(res) { + var result = res.data; + console.log(">>>>checkLoginKey :" + result); + if (result.status == 0) { + that.login(); + } else { + // // 刷新mqtt订阅topic + // mqUtils.reaportConnect(); + console.log("登录校验通过"); + } + }, + fail: function(res) { + console.log("校验loginKey是否有效: "); + } + }); +}; + +/** + * 登录 + * 1:调用微信登录 + * 1:调用服务器接口 获取sessionkey和登录态 放入缓存(有缓存时间) + * 1:调用微信获取用户信息并调用服务器接口解密用户信息 + */ +const login = function() { + var that = this; + wx.login({ + success: function(loginResult) { + var code = loginResult.code; + // that.getMemberSession(code); + // 调用服务器接口解密数据(并根据unionId获取/创建user) + wx.request({ + url: app.globalData.serverUrlExtend, + method: "POST", + header: { + 'content-type': 'application/x-www-form-urlencoded' + }, + data: { + "type": "wxdcjscode2session", + "code": code, + "appid": app.globalData.appid, + "secret": app.globalData.secret + }, + success: function(res) { + console.log("session", res); + var result = res.data; + if (result.status == 1) { + app.openId = result.data.openId; + app.loginKey = result.data.loginKey; + app.wc.put("openId", result.data.openId, app.validTime); + app.wc.put("loginKey", result.data.loginKey, app.validTime); + // 获取用户信息 + that.decodeUserData(); + } else { + app.msg.showMsg("提示", "请求session_key失败"); + } + }, + fail: function(res) { + app.msg.showMsg("提示", "获取/创建user信息失败"); + } + }); + } + }); +}; + +/** + * 获取运维存储用户信息 + */ +const decodeUserData = function() { + var that = this; + wx.getUserInfo({ + withCredentials: true, + success: function(res) { + var nickName = res.userInfo.nickName; + var encryptedData = res.encryptedData; + var iv = res.iv; + // 调用服务器接口解密数据(并根据unionId获取/创建user) + // 发起登录请求 + wx.request({ + url: app.globalData.serverUrlExtend, + method: "POST", + header: { + 'content-type': 'application/x-www-form-urlencoded' + }, + data: { + "type": "wxdcgetuser", + "tenantId": app.tenantId, + "wid": app.wid, + "openId": app.openId, + "loginKey": wx.getStorageSync("loginKey"), + "encryptedData": encryptedData, + "iv": iv, + "nickName": nickName, + "sex": 1 + }, + success: function(res) { + var result = res.data; + if (result.status == 1) { + + wx.setStorageSync("userId", result.data.user.id); + wx.setStorageSync("vipMobile", result.data.user.vipMobile); + wx.setStorageSync("unionid", result.data.user.unionid); + + app.userId = result.data.user.id; + app.globalData.appKey = result.data.appKey; + app.globalData.appSecret = result.data.appSecret; + app.vipMobile = result.data.user.vipMobile; + app.unionid = result.data.user.unionid; + // 刷新mqtt订阅topic + // app.topic = app.baseTopic + "/" + app.wid + "/" + app.userId; + // 重新连接mqtt + // mqUtils.reaportConnect(); + } + }, + fail: function(res) { + app.msg.showMsg("提示", "解析user信息失败"); + } + }); + }, + fail: function(res) { + app.msg.showMsg("提示", "获取user信息失败"); + } + }); +}; + +/** + * 获取会员session + */ +const getMemberSession = function(code) { + var params = { + 'appid': app.globalData.appid, + 'secret': app.globalData.secret, + 'code': code, + 'method': 'weixin.small.jscode2session' + } + var ignores = []; + + app.jsapi.memberApi(app.globalData.appMemberKey, app.globalData.appMemberSecret, app.globalData.serverMemberUrl).ajax(params, ignores, + function(json) { + console.log(json); + }, + function(error) { + + } + ); + +} + +module.exports = { + checkLogin: checkLogin, + login: login, + decodeUserData: decodeUserData, + checkLoginKey: checkLoginKey, + getMemberSession: getMemberSession +} \ No newline at end of file diff --git a/utils/md5.js b/utils/md5.js new file mode 100644 index 0000000..0371aba --- /dev/null +++ b/utils/md5.js @@ -0,0 +1,214 @@ +var rotateLeft = function (lValue, iShiftBits) { + return (lValue << iShiftBits) | (lValue >>> (32 - iShiftBits)); +} + +var addUnsigned = function (lX, lY) { + var lX4, lY4, lX8, lY8, lResult; + lX8 = (lX & 0x80000000); + lY8 = (lY & 0x80000000); + lX4 = (lX & 0x40000000); + lY4 = (lY & 0x40000000); + lResult = (lX & 0x3FFFFFFF) + (lY & 0x3FFFFFFF); + if (lX4 & lY4) return (lResult ^ 0x80000000 ^ lX8 ^ lY8); + if (lX4 | lY4) { + if (lResult & 0x40000000) return (lResult ^ 0xC0000000 ^ lX8 ^ lY8); + else return (lResult ^ 0x40000000 ^ lX8 ^ lY8); + } else { + return (lResult ^ lX8 ^ lY8); + } +} + +var F = function (x, y, z) { + return (x & y) | ((~x) & z); +} + +var G = function (x, y, z) { + return (x & z) | (y & (~z)); +} + +var H = function (x, y, z) { + return (x ^ y ^ z); +} + +var I = function (x, y, z) { + return (y ^ (x | (~z))); +} + +var FF = function (a, b, c, d, x, s, ac) { + a = addUnsigned(a, addUnsigned(addUnsigned(F(b, c, d), x), ac)); + return addUnsigned(rotateLeft(a, s), b); +}; + +var GG = function (a, b, c, d, x, s, ac) { + a = addUnsigned(a, addUnsigned(addUnsigned(G(b, c, d), x), ac)); + return addUnsigned(rotateLeft(a, s), b); +}; + +var HH = function (a, b, c, d, x, s, ac) { + a = addUnsigned(a, addUnsigned(addUnsigned(H(b, c, d), x), ac)); + return addUnsigned(rotateLeft(a, s), b); +}; + +var II = function (a, b, c, d, x, s, ac) { + a = addUnsigned(a, addUnsigned(addUnsigned(I(b, c, d), x), ac)); + return addUnsigned(rotateLeft(a, s), b); +}; + +var convertToWordArray = function (string) { + var lWordCount; + var lMessageLength = string.length; + var lNumberOfWordsTempOne = lMessageLength + 8; + var lNumberOfWordsTempTwo = (lNumberOfWordsTempOne - (lNumberOfWordsTempOne % 64)) / 64; + var lNumberOfWords = (lNumberOfWordsTempTwo + 1) * 16; + var lWordArray = Array(lNumberOfWords - 1); + var lBytePosition = 0; + var lByteCount = 0; + while (lByteCount < lMessageLength) { + lWordCount = (lByteCount - (lByteCount % 4)) / 4; + lBytePosition = (lByteCount % 4) * 8; + lWordArray[lWordCount] = (lWordArray[lWordCount] | (string.charCodeAt(lByteCount) << lBytePosition)); + lByteCount++; + } + lWordCount = (lByteCount - (lByteCount % 4)) / 4; + lBytePosition = (lByteCount % 4) * 8; + lWordArray[lWordCount] = lWordArray[lWordCount] | (0x80 << lBytePosition); + lWordArray[lNumberOfWords - 2] = lMessageLength << 3; + lWordArray[lNumberOfWords - 1] = lMessageLength >>> 29; + return lWordArray; +}; + +var wordToHex = function (lValue) { + var WordToHexValue = "", + WordToHexValueTemp = "", + lByte, lCount; + for (lCount = 0; lCount <= 3; lCount++) { + lByte = (lValue >>> (lCount * 8)) & 255; + WordToHexValueTemp = "0" + lByte.toString(16); + WordToHexValue = WordToHexValue + WordToHexValueTemp.substr(WordToHexValueTemp.length - 2, 2); + } + return WordToHexValue; +}; + +var uTF8Encode = function (string) { + string = string.replace(/\x0d\x0a/g, "\x0a"); + var output = ""; + for (var n = 0; n < string.length; n++) { + var c = string.charCodeAt(n); + if (c < 128) { + output += String.fromCharCode(c); + } else if ((c > 127) && (c < 2048)) { + output += String.fromCharCode((c >> 6) | 192); + output += String.fromCharCode((c & 63) | 128); + } else { + output += String.fromCharCode((c >> 12) | 224); + output += String.fromCharCode(((c >> 6) & 63) | 128); + output += String.fromCharCode((c & 63) | 128); + } + } + return output; +}; + +function md5(string) { + var x = Array(); + var k, AA, BB, CC, DD, a, b, c, d; + var S11 = 7, + S12 = 12, + S13 = 17, + S14 = 22; + var S21 = 5, + S22 = 9, + S23 = 14, + S24 = 20; + var S31 = 4, + S32 = 11, + S33 = 16, + S34 = 23; + var S41 = 6, + S42 = 10, + S43 = 15, + S44 = 21; + string = uTF8Encode(string); + x = convertToWordArray(string); + a = 0x67452301; + b = 0xEFCDAB89; + c = 0x98BADCFE; + d = 0x10325476; + for (k = 0; k < x.length; k += 16) { + AA = a; + BB = b; + CC = c; + DD = d; + a = FF(a, b, c, d, x[k + 0], S11, 0xD76AA478); + d = FF(d, a, b, c, x[k + 1], S12, 0xE8C7B756); + c = FF(c, d, a, b, x[k + 2], S13, 0x242070DB); + b = FF(b, c, d, a, x[k + 3], S14, 0xC1BDCEEE); + a = FF(a, b, c, d, x[k + 4], S11, 0xF57C0FAF); + d = FF(d, a, b, c, x[k + 5], S12, 0x4787C62A); + c = FF(c, d, a, b, x[k + 6], S13, 0xA8304613); + b = FF(b, c, d, a, x[k + 7], S14, 0xFD469501); + a = FF(a, b, c, d, x[k + 8], S11, 0x698098D8); + d = FF(d, a, b, c, x[k + 9], S12, 0x8B44F7AF); + c = FF(c, d, a, b, x[k + 10], S13, 0xFFFF5BB1); + b = FF(b, c, d, a, x[k + 11], S14, 0x895CD7BE); + a = FF(a, b, c, d, x[k + 12], S11, 0x6B901122); + d = FF(d, a, b, c, x[k + 13], S12, 0xFD987193); + c = FF(c, d, a, b, x[k + 14], S13, 0xA679438E); + b = FF(b, c, d, a, x[k + 15], S14, 0x49B40821); + a = GG(a, b, c, d, x[k + 1], S21, 0xF61E2562); + d = GG(d, a, b, c, x[k + 6], S22, 0xC040B340); + c = GG(c, d, a, b, x[k + 11], S23, 0x265E5A51); + b = GG(b, c, d, a, x[k + 0], S24, 0xE9B6C7AA); + a = GG(a, b, c, d, x[k + 5], S21, 0xD62F105D); + d = GG(d, a, b, c, x[k + 10], S22, 0x2441453); + c = GG(c, d, a, b, x[k + 15], S23, 0xD8A1E681); + b = GG(b, c, d, a, x[k + 4], S24, 0xE7D3FBC8); + a = GG(a, b, c, d, x[k + 9], S21, 0x21E1CDE6); + d = GG(d, a, b, c, x[k + 14], S22, 0xC33707D6); + c = GG(c, d, a, b, x[k + 3], S23, 0xF4D50D87); + b = GG(b, c, d, a, x[k + 8], S24, 0x455A14ED); + a = GG(a, b, c, d, x[k + 13], S21, 0xA9E3E905); + d = GG(d, a, b, c, x[k + 2], S22, 0xFCEFA3F8); + c = GG(c, d, a, b, x[k + 7], S23, 0x676F02D9); + b = GG(b, c, d, a, x[k + 12], S24, 0x8D2A4C8A); + a = HH(a, b, c, d, x[k + 5], S31, 0xFFFA3942); + d = HH(d, a, b, c, x[k + 8], S32, 0x8771F681); + c = HH(c, d, a, b, x[k + 11], S33, 0x6D9D6122); + b = HH(b, c, d, a, x[k + 14], S34, 0xFDE5380C); + a = HH(a, b, c, d, x[k + 1], S31, 0xA4BEEA44); + d = HH(d, a, b, c, x[k + 4], S32, 0x4BDECFA9); + c = HH(c, d, a, b, x[k + 7], S33, 0xF6BB4B60); + b = HH(b, c, d, a, x[k + 10], S34, 0xBEBFBC70); + a = HH(a, b, c, d, x[k + 13], S31, 0x289B7EC6); + d = HH(d, a, b, c, x[k + 0], S32, 0xEAA127FA); + c = HH(c, d, a, b, x[k + 3], S33, 0xD4EF3085); + b = HH(b, c, d, a, x[k + 6], S34, 0x4881D05); + a = HH(a, b, c, d, x[k + 9], S31, 0xD9D4D039); + d = HH(d, a, b, c, x[k + 12], S32, 0xE6DB99E5); + c = HH(c, d, a, b, x[k + 15], S33, 0x1FA27CF8); + b = HH(b, c, d, a, x[k + 2], S34, 0xC4AC5665); + a = II(a, b, c, d, x[k + 0], S41, 0xF4292244); + d = II(d, a, b, c, x[k + 7], S42, 0x432AFF97); + c = II(c, d, a, b, x[k + 14], S43, 0xAB9423A7); + b = II(b, c, d, a, x[k + 5], S44, 0xFC93A039); + a = II(a, b, c, d, x[k + 12], S41, 0x655B59C3); + d = II(d, a, b, c, x[k + 3], S42, 0x8F0CCC92); + c = II(c, d, a, b, x[k + 10], S43, 0xFFEFF47D); + b = II(b, c, d, a, x[k + 1], S44, 0x85845DD1); + a = II(a, b, c, d, x[k + 8], S41, 0x6FA87E4F); + d = II(d, a, b, c, x[k + 15], S42, 0xFE2CE6E0); + c = II(c, d, a, b, x[k + 6], S43, 0xA3014314); + b = II(b, c, d, a, x[k + 13], S44, 0x4E0811A1); + a = II(a, b, c, d, x[k + 4], S41, 0xF7537E82); + d = II(d, a, b, c, x[k + 11], S42, 0xBD3AF235); + c = II(c, d, a, b, x[k + 2], S43, 0x2AD7D2BB); + b = II(b, c, d, a, x[k + 9], S44, 0xEB86D391); + a = addUnsigned(a, AA); + b = addUnsigned(b, BB); + c = addUnsigned(c, CC); + d = addUnsigned(d, DD); + } + var tempValue = wordToHex(a) + wordToHex(b) + wordToHex(c) + wordToHex(d); + return tempValue.toLowerCase(); +} + +module.exports.md5 = md5 \ No newline at end of file diff --git a/utils/mqMonitor.js b/utils/mqMonitor.js new file mode 100644 index 0000000..dbef8a2 --- /dev/null +++ b/utils/mqMonitor.js @@ -0,0 +1,38 @@ +var app = getApp(); +var mqUtils = require('mqttSubstribe.js'); +var timerHolder = null; +var startMonitor = function(currentPage, type) { + var that = this; + var time = 35000; + app.page = currentPage; + // 获取c li + var client = app.client; + if (client && type == 0) { // + console.log("重新订阅主题"); + mqUtils.doSubscribe(); + } + // 线连接一次 + // mqUtils.doConnect(page); + if (!timerHolder) { + timerHolder = setInterval(function() { + var client = app.client; + if (!client || !client.isConnected()) { + console.log(client); + console.log("mqtt----------检测连接") + mqUtils.doConnect(); + } else { + console.log(" mqttMonitor 已连接...."); + } + }, 1500); + } +} + +// 取消订阅主题 +var startCancel = function() { + mqUtils.unsubscribe(); +} + +module.exports = { + startMonitor: startMonitor, + startCancel: startCancel, +} \ No newline at end of file diff --git a/utils/mqtt.js b/utils/mqtt.js new file mode 100644 index 0000000..46892f0 --- /dev/null +++ b/utils/mqtt.js @@ -0,0 +1,14080 @@ +(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' + + /** + * Module dependencies + */ + 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) { + // works in node v0.8 + process.nextTick(callback) + } + var defaultConnectOptions = { + keepalive: 60, + reschedulePings: true, + protocolId: 'MQTT', + protocolVersion: 4, + reconnectPeriod: 1000, + connectTimeout: 30 * 1000, + 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() {} + + /** + * MqttClient constructor + * + * @param {Stream} stream - stream + * @param {Object} [options] - connection options + * (see Connection#connect) + */ + function MqttClient(streamBuilder, options) { + var k + var that = this + + if (!(this instanceof MqttClient)) { + return new MqttClient(streamBuilder, options) + } + + this.options = options || {} + + // Defaults + 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 + + // Inflight message storages + this.outgoingStore = this.options.outgoingStore || new Store() + this.incomingStore = this.options.incomingStore || new Store() + + // Should QoS zero messages be queued when the connection is broken? + this.queueQoSZero = this.options.queueQoSZero === undefined ? true : this.options.queueQoSZero + + // map of subscribed topics to support reconnection + this._resubscribeTopics = {} + + // map of a subscribe messageId and a topic + this.messageIdToTopic = {} + + // Ping timer, setup in _setupPingTimer + this.pingTimer = null + // Is the client connected? + this.connected = false + // Are we disconnecting? + this.disconnecting = false + // Packet queue + this.queue = [] + // connack timer + this.connackTimer = null + // Reconnect timer + this.reconnectTimer = null + /** + * MessageIDs starting with 1 + * ensure that nextId is min. 1, see https://github.com/mqttjs/MQTT.js/issues/810 + */ + this.nextId = Math.max(1, Math.floor(Math.random() * 65535)) + + // Inflight callbacks + this.outgoing = {} + + // Mark connected on connect + 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() { + // edge case, we wrapped this twice + if (!outStore) { + return + } + + var packet = outStore.read(1) + var cb + + if (!packet) { + // read when data is available in the future + outStore.once('readable', storeDeliver) + return + } + + // Avoid unnecessary stream read operations when disconnected + if (!that.disconnecting && !that.reconnectTimer) { + cb = that.outgoing[packet.messageId] + that.outgoing[packet.messageId] = function(err, status) { + // Ensure that the original callback passed in to publish gets invoked + if (cb) { + cb(err, status) + } + + storeDeliver() + } + that._sendPacket(packet) + } else if (outStore.destroy) { + outStore.destroy() + } + } + + // start flowing + storeDeliver() + }) + + // Mark disconnected on stream close + this.on('close', function() { + this.connected = false + clearTimeout(this.connackTimer) + }) + + // Setup ping timer + this.on('connect', this._setupPingTimer) + + // Send queued packets + 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(err) + } + deliver() + } + ) + } + + deliver() + }) + + var firstConnection = true + // resubscribe + this.on('connect', function() { + if (!firstConnection && + this.options.clean && + Object.keys(this._resubscribeTopics).length > 0) { + if (this.options.resubscribe) { + this._resubscribeTopics.resubscribe = true + this.subscribe(this._resubscribeTopics) + } else { + this._resubscribeTopics = {} + } + } + + firstConnection = false + }) + + // Clear ping timer + this.on('close', function() { + if (that.pingTimer !== null) { + that.pingTimer.clear() + that.pingTimer = null + } + }) + + // Setup reconnect timer on disconnect + this.on('close', this._setupReconnect) + + events.EventEmitter.call(this) + + this._setupStream() + } + inherits(MqttClient, events.EventEmitter) + + /** + * setup the event handlers in the inner stream. + * + * @api private + */ + MqttClient.prototype._setupStream = function() { + var connectPacket + var that = this + var writable = new Writable() + var parser = mqttPacket.parser(this.options) + var completeParse = null + var packets = [] + + this._clearReconnect() + + this.stream = this.streamBuilder(this) + + parser.on('packet', function(packet) { + packets.push(packet) + }) + + function nextTickWork() { + process.nextTick(work) + } + + function work() { + var packet = packets.shift() + var done = completeParse + + if (packet) { + that._handlePacket(packet, nextTickWork) + } else { + completeParse = null + done() + } + } + + writable._write = function(buf, enc, done) { + completeParse = done + parser.parse(buf) + work() + } + + this.stream.pipe(writable) + + // Suppress connection errors + this.stream.on('error', nop) + + // Echo stream close + eos(this.stream, this.emit.bind(this, 'close')) + + // Send a connect packet + connectPacket = Object.create(this.options) + connectPacket.cmd = 'connect' + // avoid message queue + sendPacket(this, connectPacket) + + // Echo connection errors + parser.on('error', this.emit.bind(this, 'error')) + + // many drain listeners are needed for qos 1 callbacks if the connection is intermittent + this.stream.setMaxListeners(1000) + + clearTimeout(this.connackTimer) + this.connackTimer = setTimeout(function() { + that._cleanUp(true) + }, this.options.connectTimeout) + } + + MqttClient.prototype._handlePacket = function(packet, done) { + this.emit('packetreceive', packet) + + switch (packet.cmd) { + case 'publish': + this._handlePublish(packet, done) + break + case 'puback': + case 'pubrec': + case 'pubcomp': + case 'suback': + case 'unsuback': + this._handleAck(packet) + done() + break + case 'pubrel': + this._handlePubrel(packet, done) + break + case 'connack': + this._handleConnack(packet) + done() + break + case 'pingresp': + this._handlePingresp(packet) + done() + break + default: + // do nothing + // maybe we should do an error handling + // or just log it + break + } + } + + MqttClient.prototype._checkDisconnecting = function(callback) { + if (this.disconnecting) { + if (callback) { + callback(new Error('client disconnecting')) + } else { + this.emit('error', new Error('client disconnecting')) + } + } + return this.disconnecting + } + + /** + * publish - publish to + * + * @param {String} topic - topic to publish to + * @param {String, Buffer} message - message to publish + * @param {Object} [opts] - publish options, includes: + * {Number} qos - qos level to publish on + * {Boolean} retain - whether or not to retain the message + * {Boolean} dup - whether or not mark a message as duplicate + * @param {Function} [callback] - function(err){} + * called when publish succeeds or fails + * @returns {MqttClient} this - for chaining + * @api public + * + * @example client.publish('topic', 'message'); + * @example + * client.publish('topic', 'message', {qos: 1, retain: true, dup: true}); + * @example client.publish('topic', 'message', console.log); + */ + MqttClient.prototype.publish = function(topic, message, opts, callback) { + var packet + + // .publish(topic, payload, cb); + if (typeof opts === 'function') { + callback = opts + opts = null + } + + // default opts + var defaultOpts = { + qos: 0, + retain: false, + dup: false + } + opts = xtend(defaultOpts, opts) + + if (this._checkDisconnecting(callback)) { + return this + } + + packet = { + cmd: 'publish', + topic: topic, + payload: message, + qos: opts.qos, + retain: opts.retain, + messageId: this._nextId(), + dup: opts.dup + } + + switch (opts.qos) { + case 1: + case 2: + + // Add to callbacks + this.outgoing[packet.messageId] = callback || nop + this._sendPacket(packet) + break + default: + this._sendPacket(packet, callback) + break + } + + return this + } + + /** + * subscribe - subscribe to + * + * @param {String, Array, Object} topic - topic(s) to subscribe to, supports objects in the form {'topic': qos} + * @param {Object} [opts] - optional subscription options, includes: + * {Number} qos - subscribe qos level + * @param {Function} [callback] - function(err, granted){} where: + * {Error} err - subscription error (none at the moment!) + * {Array} granted - array of {topic: 't', qos: 0} + * @returns {MqttClient} this - for chaining + * @api public + * @example client.subscribe('topic'); + * @example client.subscribe('topic', {qos: 1}); + * @example client.subscribe({'topic': 0, 'topic2': 1}, console.log); + * @example client.subscribe('topic', console.log); + */ + MqttClient.prototype.subscribe = function() { + var packet + var args = Array.prototype.slice.call(arguments) + var subs = [] + var obj = args.shift() + var resubscribe = obj.resubscribe + var callback = args.pop() || nop + var opts = args.pop() + var invalidTopic + var that = this + + delete obj.resubscribe + + if (typeof obj === 'string') { + obj = [obj] + } + + if (typeof callback !== 'function') { + opts = callback + callback = nop + } + + invalidTopic = validations.validateTopics(obj) + if (invalidTopic !== null) { + setImmediate(callback, new Error('Invalid topic ' + invalidTopic)) + return this + } + + if (this._checkDisconnecting(callback)) { + return this + } + + var defaultOpts = { + qos: 0 + } + opts = xtend(defaultOpts, opts) + + if (Array.isArray(obj)) { + obj.forEach(function(topic) { + if (that._resubscribeTopics[topic] < opts.qos || + !that._resubscribeTopics.hasOwnProperty(topic) || + resubscribe) { + subs.push({ + topic: topic, + qos: opts.qos + }) + } + }) + } else { + Object + .keys(obj) + .forEach(function(k) { + if (that._resubscribeTopics[k] < obj[k] || + !that._resubscribeTopics.hasOwnProperty(k) || + resubscribe) { + subs.push({ + topic: k, + qos: obj[k] + }) + } + }) + } + + packet = { + cmd: 'subscribe', + subscriptions: subs, + qos: 1, + retain: false, + dup: false, + messageId: this._nextId() + } + + if (!subs.length) { + callback(null, []) + return + } + + // subscriptions to resubscribe to in case of disconnect + if (this.options.resubscribe) { + var topics = [] + subs.forEach(function(sub) { + if (that.options.reconnectPeriod > 0) { + that._resubscribeTopics[sub.topic] = sub.qos + topics.push(sub.topic) + } + }) + that.messageIdToTopic[packet.messageId] = topics + } + + this.outgoing[packet.messageId] = function(err, packet) { + if (!err) { + var granted = packet.granted + for (var i = 0; i < granted.length; i += 1) { + subs[i].qos = granted[i] + } + } + + callback(err, subs) + } + + this._sendPacket(packet) + + return this + } + + /** + * unsubscribe - unsubscribe from topic(s) + * + * @param {String, Array} topic - topics to unsubscribe from + * @param {Function} [callback] - callback fired on unsuback + * @returns {MqttClient} this - for chaining + * @api public + * @example client.unsubscribe('topic'); + * @example client.unsubscribe('topic', console.log); + */ + MqttClient.prototype.unsubscribe = function(topic, callback) { + var packet = { + cmd: 'unsubscribe', + qos: 1, + messageId: this._nextId() + } + var that = this + + callback = callback || nop + + if (this._checkDisconnecting(callback)) { + return this + } + + if (typeof topic === 'string') { + packet.unsubscriptions = [topic] + } else if (typeof topic === 'object' && topic.length) { + packet.unsubscriptions = topic + } + + if (this.options.resubscribe) { + packet.unsubscriptions.forEach(function(topic) { + delete that._resubscribeTopics[topic] + }) + } + + this.outgoing[packet.messageId] = callback + + this._sendPacket(packet) + + return this + } + + /** + * end - close connection + * + * @returns {MqttClient} this - for chaining + * @param {Boolean} force - do not wait for all in-flight messages to be acked + * @param {Function} cb - called when the client has been closed + * + * @api public + */ + MqttClient.prototype.end = function(force, cb) { + var that = this + + if (typeof force === 'function') { + cb = force + force = false + } + + function closeStores() { + that.disconnected = true + that.incomingStore.close(function() { + that.outgoingStore.close(function() { + if (cb) { + cb.apply(null, arguments) + } + that.emit('end') + }) + }) + if (that._deferredReconnect) { + that._deferredReconnect() + } + } + + function finish() { + // defer closesStores of an I/O cycle, + // just to make sure things are + // ok for websockets + that._cleanUp(force, setImmediate.bind(null, closeStores)) + } + + if (this.disconnecting) { + return this + } + + this._clearReconnect() + + this.disconnecting = true + + if (!force && Object.keys(this.outgoing).length > 0) { + // wait 10ms, just to be sure we received all of it + this.once('outgoingEmpty', setTimeout.bind(null, finish, 10)) + } else { + finish() + } + + return this + } + + /** + * removeOutgoingMessage - remove a message in outgoing store + * the outgoing callback will be called withe Error('Message removed') if the message is removed + * + * @param {Number} mid - messageId to remove message + * @returns {MqttClient} this - for chaining + * @api public + * + * @example client.removeOutgoingMessage(client.getLastMessageId()); + */ + MqttClient.prototype.removeOutgoingMessage = function(mid) { + var cb = this.outgoing[mid] + delete this.outgoing[mid] + this.outgoingStore.del({ + messageId: mid + }, function() { + cb(new Error('Message removed')) + }) + return this + } + + /** + * reconnect - connect again using the same options as connect() + * + * @param {Object} [opts] - optional reconnect options, includes: + * {Store} incomingStore - a store for the incoming packets + * {Store} outgoingStore - a store for the outgoing packets + * if opts is not given, current stores are used + * @returns {MqttClient} this - for chaining + * + * @api public + */ + MqttClient.prototype.reconnect = function(opts) { + var that = this + var f = function() { + if (opts) { + that.options.incomingStore = opts.incomingStore + that.options.outgoingStore = opts.outgoingStore + } else { + that.options.incomingStore = null + that.options.outgoingStore = null + } + that.incomingStore = that.options.incomingStore || new Store() + that.outgoingStore = that.options.outgoingStore || new Store() + that.disconnecting = false + that.disconnected = false + that._deferredReconnect = null + that._reconnect() + } + + if (this.disconnecting && !this.disconnected) { + this._deferredReconnect = f + } else { + f() + } + return this + } + + /** + * _reconnect - implement reconnection + * @api privateish + */ + MqttClient.prototype._reconnect = function() { + this.emit('reconnect') + this._setupStream() + } + + /** + * _setupReconnect - setup reconnect timer + */ + MqttClient.prototype._setupReconnect = function() { + var that = this + + if (!that.disconnecting && !that.reconnectTimer && (that.options.reconnectPeriod > 0)) { + if (!this.reconnecting) { + this.emit('offline') + this.reconnecting = true + } + that.reconnectTimer = setInterval(function() { + that._reconnect() + }, that.options.reconnectPeriod) + } + } + + /** + * _clearReconnect - clear the reconnect timer + */ + MqttClient.prototype._clearReconnect = function() { + if (this.reconnectTimer) { + clearInterval(this.reconnectTimer) + this.reconnectTimer = null + } + } + + /** + * _cleanUp - clean up on connection end + * @api private + */ + MqttClient.prototype._cleanUp = function(forced, done) { + if (done) { + this.stream.on('close', done) + } + + if (forced) { + if ((this.options.reconnectPeriod === 0) && this.options.clean) { + flush(this.outgoing) + } + this.stream.destroy() + } else { + this._sendPacket({ + cmd: 'disconnect' + }, + setImmediate.bind( + null, + this.stream.end.bind(this.stream) + ) + ) + } + + if (!this.disconnecting) { + this._clearReconnect() + this._setupReconnect() + } + + if (this.pingTimer !== null) { + this.pingTimer.clear() + this.pingTimer = null + } + + if (done && !this.connected) { + this.stream.removeListener('close', done) + done() + } + } + + /** + * _sendPacket - send or queue a packet + * @param {String} type - packet type (see `protocol`) + * @param {Object} packet - packet options + * @param {Function} cb - callback when the packet is sent + * @api private + */ + MqttClient.prototype._sendPacket = function(packet, cb) { + if (!this.connected) { + if (((packet.qos || 0) === 0 && this.queueQoSZero) || packet.cmd !== 'publish') { + this.queue.push({ + packet: packet, + cb: cb + }) + } else if (packet.qos > 0) { + cb = this.outgoing[packet.messageId] + this.outgoingStore.put(packet, function(err) { + if (err) { + return cb && cb(err) + } + }) + } else if (cb) { + cb(new Error('No connection to broker')) + } + + return + } + + // When sending a packet, reschedule the ping timer + this._shiftPingInterval() + + switch (packet.cmd) { + case 'publish': + break + case 'pubrel': + storeAndSend(this, packet, cb) + return + default: + sendPacket(this, packet, cb) + return + } + + switch (packet.qos) { + case 2: + case 1: + storeAndSend(this, packet, cb) + break + /** + * no need of case here since it will be caught by default + * and jshint comply that before default it must be a break + * anyway it will result in -1 evaluation + */ + case 0: + /* falls through */ + default: + sendPacket(this, packet, cb) + break + } + } + + /** + * _setupPingTimer - setup the ping timer + * + * @api private + */ + MqttClient.prototype._setupPingTimer = function() { + var that = this + + if (!this.pingTimer && this.options.keepalive) { + this.pingResp = true + this.pingTimer = reInterval(function() { + that._checkPing() + }, this.options.keepalive * 1000) + } + } + + /** + * _shiftPingInterval - reschedule the ping interval + * + * @api private + */ + MqttClient.prototype._shiftPingInterval = function() { + if (this.pingTimer && this.options.keepalive && this.options.reschedulePings) { + this.pingTimer.reschedule(this.options.keepalive * 1000) + } + } + /** + * _checkPing - check if a pingresp has come back, and ping the server again + * + * @api private + */ + MqttClient.prototype._checkPing = function() { + if (this.pingResp) { + this.pingResp = false + this._sendPacket({ + cmd: 'pingreq' + }) + } else { + // do a forced cleanup since socket will be in bad shape + this._cleanUp(true) + } + } + + /** + * _handlePingresp - handle a pingresp + * + * @api private + */ + MqttClient.prototype._handlePingresp = function() { + this.pingResp = true + } + + /** + * _handleConnack + * + * @param {Object} packet + * @api private + */ + + MqttClient.prototype._handleConnack = function(packet) { + var rc = packet.returnCode + var errors = [ + '', + 'Unacceptable protocol version', + 'Identifier rejected', + 'Server unavailable', + 'Bad username or password', + 'Not authorized' + ] + + clearTimeout(this.connackTimer) + + if (rc === 0) { + this.reconnecting = false + this.emit('connect', packet) + } else if (rc > 0) { + var err = new Error('Connection refused: ' + errors[rc]) + err.code = rc + this.emit('error', err) + } + } + + /** + * _handlePublish + * + * @param {Object} packet + * @api private + */ + /* + those late 2 case should be rewrite to comply with coding style: + + case 1: + case 0: + // do not wait sending a puback + // no callback passed + if (1 === qos) { + this._sendPacket({ + cmd: 'puback', + messageId: mid + }); + } + // emit the message event for both qos 1 and 0 + this.emit('message', topic, message, packet); + this.handleMessage(packet, done); + break; + default: + // do nothing but every switch mus have a default + // log or throw an error about unknown qos + break; + + for now i just suppressed the warnings + */ + MqttClient.prototype._handlePublish = function(packet, done) { + done = typeof done !== 'undefined' ? done : nop + var topic = packet.topic.toString() + var message = packet.payload + var qos = packet.qos + var mid = packet.messageId + var that = this + + switch (qos) { + case 2: + this.incomingStore.put(packet, function(err) { + if (err) { + return done(err) + } + that._sendPacket({ + cmd: 'pubrec', + messageId: mid + }, done) + }) + break + case 1: + // emit the message event + this.emit('message', topic, message, packet) + this.handleMessage(packet, function(err) { + if (err) { + return done(err) + } + // send 'puback' if the above 'handleMessage' method executed + // successfully. + that._sendPacket({ + cmd: 'puback', + messageId: mid + }, done) + }) + break + case 0: + // emit the message event + this.emit('message', topic, message, packet) + this.handleMessage(packet, done) + break + default: + // do nothing + // log or throw an error about unknown qos + break + } + } + + /** + * Handle messages with backpressure support, one at a time. + * Override at will. + * + * @param Packet packet the packet + * @param Function callback call when finished + * @api public + */ + MqttClient.prototype.handleMessage = function(packet, callback) { + callback() + } + + /** + * _handleAck + * + * @param {Object} packet + * @api private + */ + + MqttClient.prototype._handleAck = function(packet) { + /* eslint no-fallthrough: "off" */ + var mid = packet.messageId + var type = packet.cmd + var response = null + var cb = this.outgoing[mid] + var that = this + + if (!cb) { + // Server sent an ack in error, ignore it. + return + } + + // Process + switch (type) { + case 'pubcomp': + // same thing as puback for QoS 2 + case 'puback': + // Callback - we're done + delete this.outgoing[mid] + this.outgoingStore.del(packet, cb) + break + case 'pubrec': + response = { + cmd: 'pubrel', + qos: 2, + messageId: mid + } + + this._sendPacket(response) + break + case 'suback': + delete this.outgoing[mid] + if (packet.granted.length === 1 && (packet.granted[0] & 0x80) !== 0) { + // suback with Failure status + var topics = this.messageIdToTopic[mid] + if (topics) { + topics.forEach(function(topic) { + delete that._resubscribeTopics[topic] + }) + } + } + cb(null, packet) + break + case 'unsuback': + delete this.outgoing[mid] + cb(null) + break + default: + that.emit('error', new Error('unrecognized packet type')) + } + + if (this.disconnecting && + Object.keys(this.outgoing).length === 0) { + this.emit('outgoingEmpty') + } + } + + /** + * _handlePubrel + * + * @param {Object} packet + * @api private + */ + MqttClient.prototype._handlePubrel = function(packet, callback) { + callback = typeof callback !== 'undefined' ? callback : nop + var mid = packet.messageId + var that = this + + var comp = { + cmd: 'pubcomp', + messageId: mid + } + + that.incomingStore.get(packet, function(err, pub) { + if (!err && pub.cmd !== 'pubrel') { + that.emit('message', pub.topic, pub.payload, pub) + that.incomingStore.put(packet, function(err) { + if (err) { + return callback(err) + } + that.handleMessage(pub, function(err) { + if (err) { + return callback(err) + } + that._sendPacket(comp, callback) + }) + }) + } else { + that._sendPacket(comp, callback) + } + }) + } + + /** + * _nextId + * @return unsigned int + */ + MqttClient.prototype._nextId = function() { + // id becomes current state of this.nextId and increments afterwards + var id = this.nextId++ + // Ensure 16 bit unsigned int (max 65535, nextId got one higher) + if (this.nextId === 65536) { + this.nextId = 1 + } + return id + } + + /** + * getLastMessageId + * @return unsigned int + */ + MqttClient.prototype.getLastMessageId = function() { + return (this.nextId === 1) ? 65535 : (this.nextId - 1) + } + + module.exports = MqttClient + + }).call(this, require('_process'), typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) + }, { + "./store": 6, + "./validations": 7, + "_process": 92, + "end-of-stream": 17, + "events": 78, + "inherits": 80, + "mqtt-packet": 85, + "readable-stream": 106, + "reinterval": 107, + "xtend": 120 + }], + 2: [function(require, module, exports) { + 'use strict' + var net = require('net') + + /* + variables port and host can be removed since + you have all required information in opts object + */ + function buildBuilder(client, opts) { + var port, host + opts.port = opts.port || 1883 + opts.hostname = opts.hostname || opts.host || 'localhost' + + port = opts.port + host = opts.hostname + + return net.createConnection(port, host) + } + + module.exports = buildBuilder + + }, { + "net": 11 + }], + 3: [function(require, module, exports) { + 'use strict' + var tls = require('tls') + + function buildBuilder(mqttClient, opts) { + var connection + opts.port = opts.port || 8883 + opts.host = opts.hostname || opts.host || 'localhost' + + opts.rejectUnauthorized = opts.rejectUnauthorized !== false + + delete opts.path + + connection = tls.connect(opts) + /* eslint no-use-before-define: [2, "nofunc"] */ + connection.on('secureConnect', function() { + if (opts.rejectUnauthorized && !connection.authorized) { + connection.emit('error', new Error('TLS not authorized')) + } else { + connection.removeListener('error', handleTLSerrors) + } + }) + + function handleTLSerrors(err) { + // How can I get verify this error is a tls error? + if (opts.rejectUnauthorized) { + mqttClient.emit('error', err) + } + + // close this connection to match the behaviour of net + // otherwise all we get is an error from the connection + // and close event doesn't fire. This is a work around + // to enable the reconnect code to work the same as with + // net.createConnection + connection.end() + } + + connection.on('error', handleTLSerrors) + return connection + } + + module.exports = buildBuilder + + }, { + "tls": 11 + }], + 4: [function(require, module, exports) { + (function(process) { + 'use strict' + + var websocket = require('websocket-stream') + var urlModule = require('url') + var WSS_OPTIONS = [ + 'rejectUnauthorized', + 'ca', + 'cert', + 'key', + 'pfx', + 'passphrase' + ] + var IS_BROWSER = process.title === 'browser' + + function buildUrl(opts, client) { + var url = opts.protocol + '://' + opts.hostname + ':' + opts.port + opts.path + if (typeof(opts.transformWsUrl) === 'function') { + url = opts.transformWsUrl(url, opts, client) + } + return url + } + + function setDefaultOpts(opts) { + if (!opts.hostname) { + opts.hostname = 'localhost' + } + if (!opts.port) { + if (opts.protocol === 'wss') { + opts.port = 443 + } else { + opts.port = 80 + } + } + if (!opts.path) { + opts.path = '/' + } + + if (!opts.wsOptions) { + opts.wsOptions = {} + } + if (!IS_BROWSER && opts.protocol === 'wss') { + // Add cert/key/ca etc options + WSS_OPTIONS.forEach(function(prop) { + if (opts.hasOwnProperty(prop) && !opts.wsOptions.hasOwnProperty(prop)) { + opts.wsOptions[prop] = opts[prop] + } + }) + } + } + + function createWebSocket(client, opts) { + var websocketSubProtocol = + (opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3) ? + 'mqttv3.1' : + 'mqtt' + + setDefaultOpts(opts) + var url = buildUrl(opts, client) + return websocket(url, [websocketSubProtocol], opts.wsOptions) + } + + function buildBuilder(client, opts) { + return createWebSocket(client, opts) + } + + function buildBuilderBrowser(client, opts) { + if (!opts.hostname) { + opts.hostname = opts.host + } + + if (!opts.hostname) { + // Throwing an error in a Web Worker if no `hostname` is given, because we + // can not determine the `hostname` automatically. If connecting to + // localhost, please supply the `hostname` as an argument. + if (typeof(document) === 'undefined') { + throw new Error('Could not determine host. Specify host manually.') + } + var parsed = urlModule.parse(document.URL) + opts.hostname = parsed.hostname + + if (!opts.port) { + opts.port = parsed.port + } + } + return createWebSocket(client, opts) + } + + if (IS_BROWSER) { + module.exports = buildBuilderBrowser + } else { + module.exports = buildBuilder + } + + }).call(this, require('_process')) + }, { + "_process": 92, + "url": 112, + "websocket-stream": 117 + }], + 5: [function(require, module, exports) { + 'use strict' + + /* global wx */ + var socketOpen = false + var socketMsgQueue = [] + + function sendSocketMessage(msg) { + if (socketOpen) { + wx.sendSocketMessage({ + data: msg.buffer || msg + }) + } else { + socketMsgQueue.push(msg) + } + } + + function WebSocket(url, protocols) { + var ws = { + OPEN: 1, + CLOSING: 2, + CLOSED: 3, + readyState: socketOpen ? 1 : 0, + send: sendSocketMessage, + close: wx.closeSocket, + onopen: null, + onmessage: null, + onclose: null, + onerror: null + } + + wx.connectSocket({ + url: url, + protocols: protocols + }) + wx.onSocketOpen(function(res) { + ws.readyState = ws.OPEN + socketOpen = true + for (var i = 0; i < socketMsgQueue.length; i++) { + sendSocketMessage(socketMsgQueue[i]) + } + socketMsgQueue = [] + + ws.onopen && ws.onopen.apply(ws, arguments) + }) + wx.onSocketMessage(function(res) { + ws.onmessage && ws.onmessage.apply(ws, arguments) + }) + wx.onSocketClose(function() { + ws.onclose && ws.onclose.apply(ws, arguments) + ws.readyState = ws.CLOSED + socketOpen = false + }) + wx.onSocketError(function() { + ws.onerror && ws.onerror.apply(ws, arguments) + ws.readyState = ws.CLOSED + socketOpen = false + }) + + return ws + } + + var websocket = require('websocket-stream') + + function buildUrl(opts, client) { + var protocol = opts.protocol === 'wxs' ? 'wss' : 'ws' + var url = protocol + '://' + opts.hostname + opts.path + if (opts.port && opts.port !== 80 && opts.port !== 443) { + url = protocol + '://' + opts.hostname + ':' + opts.port + opts.path + } + if (typeof(opts.transformWsUrl) === 'function') { + url = opts.transformWsUrl(url, opts, client) + } + return url + } + + function setDefaultOpts(opts) { + if (!opts.hostname) { + opts.hostname = 'localhost' + } + if (!opts.path) { + opts.path = '/' + } + + if (!opts.wsOptions) { + opts.wsOptions = {} + } + } + + function createWebSocket(client, opts) { + var websocketSubProtocol = + (opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3) ? + 'mqttv3.1' : + 'mqtt' + + setDefaultOpts(opts) + var url = buildUrl(opts, client) + return websocket(WebSocket(url, [websocketSubProtocol])) + } + + function buildBuilder(client, opts) { + opts.hostname = opts.hostname || opts.host + + if (!opts.hostname) { + throw new Error('Could not determine host. Specify host manually.') + } + + return createWebSocket(client, opts) + } + + module.exports = buildBuilder + + }, { + "websocket-stream": 117 + }], + 6: [function(require, module, exports) { + (function(process) { + 'use strict' + + /** + * Module dependencies + */ + var xtend = require('xtend') + + var Readable = require('readable-stream').Readable + var streamsOpts = { + objectMode: true + } + var defaultStoreOptions = { + clean: true + } + + /** + * es6-map can preserve insertion order even if ES version is older. + * + * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map#Description + * It should be noted that a Map which is a map of an object, especially + * a dictionary of dictionaries, will only map to the object's insertion + * order. In ES2015 this is ordered for objects but for older versions of + * ES, this may be random and not ordered. + * + */ + var Map = require('es6-map') + + /** + * In-memory implementation of the message store + * This can actually be saved into files. + * + * @param {Object} [options] - store options + */ + function Store(options) { + if (!(this instanceof Store)) { + return new Store(options) + } + + this.options = options || {} + + // Defaults + this.options = xtend(defaultStoreOptions, options) + + this._inflights = new Map() + } + + /** + * Adds a packet to the store, a packet is + * anything that has a messageId property. + * + */ + Store.prototype.put = function(packet, cb) { + this._inflights.set(packet.messageId, packet) + + if (cb) { + cb() + } + + return this + } + + /** + * Creates a stream with all the packets in the store + * + */ + Store.prototype.createStream = function() { + var stream = new Readable(streamsOpts) + var destroyed = false + var values = [] + var i = 0 + + this._inflights.forEach(function(value, key) { + values.push(value) + }) + + stream._read = function() { + if (!destroyed && i < values.length) { + this.push(values[i++]) + } else { + this.push(null) + } + } + + stream.destroy = function() { + if (destroyed) { + return + } + + var self = this + + destroyed = true + + process.nextTick(function() { + self.emit('close') + }) + } + + return stream + } + + /** + * deletes a packet from the store. + */ + Store.prototype.del = function(packet, cb) { + packet = this._inflights.get(packet.messageId) + if (packet) { + this._inflights.delete(packet.messageId) + cb(null, packet) + } else if (cb) { + cb(new Error('missing packet')) + } + + return this + } + + /** + * get a packet from the store. + */ + Store.prototype.get = function(packet, cb) { + packet = this._inflights.get(packet.messageId) + if (packet) { + cb(null, packet) + } else if (cb) { + cb(new Error('missing packet')) + } + + return this + } + + /** + * Close the store + */ + Store.prototype.close = function(cb) { + if (this.options.clean) { + this._inflights = null + } + if (cb) { + cb() + } + } + + module.exports = Store + + }).call(this, require('_process')) + }, { + "_process": 92, + "es6-map": 66, + "readable-stream": 106, + "xtend": 120 + }], + 7: [function(require, module, exports) { + 'use strict' + + /** + * Validate a topic to see if it's valid or not. + * A topic is valid if it follow below rules: + * - Rule #1: If any part of the topic is not `+` or `#`, then it must not contain `+` and '#' + * - Rule #2: Part `#` must be located at the end of the mailbox + * + * @param {String} topic - A topic + * @returns {Boolean} If the topic is valid, returns true. Otherwise, returns false. + */ + function validateTopic(topic) { + var parts = topic.split('/') + + for (var i = 0; i < parts.length; i++) { + if (parts[i] === '+') { + continue + } + + if (parts[i] === '#') { + // for Rule #2 + return i === parts.length - 1 + } + + if (parts[i].indexOf('+') !== -1 || parts[i].indexOf('#') !== -1) { + return false + } + } + + return true + } + + /** + * Validate an array of topics to see if any of them is valid or not + * @param {Array} topics - Array of topics + * @returns {String} If the topics is valid, returns null. Otherwise, returns the invalid one + */ + function validateTopics(topics) { + if (topics.length === 0) { + return 'empty_topic_list' + } + for (var i = 0; i < topics.length; i++) { + if (!validateTopic(topics[i])) { + return topics[i] + } + } + return null + } + + module.exports = { + validateTopics: validateTopics + } + + }, {}], + 8: [function(require, module, exports) { + (function(process) { + 'use strict' + + var MqttClient = require('../client') + var Store = require('../store') + var url = require('url') + var xtend = require('xtend') + var protocols = {} + + if (process.title !== 'browser') { + protocols.mqtt = require('./tcp') + protocols.tcp = require('./tcp') + protocols.ssl = require('./tls') + protocols.tls = require('./tls') + protocols.mqtts = require('./tls') + } else { + protocols.wx = require('./wx') + protocols.wxs = require('./wx') + } + + protocols.ws = require('./ws') + protocols.wss = require('./ws') + + /** + * Parse the auth attribute and merge username and password in the options object. + * + * @param {Object} [opts] option object + */ + function parseAuthOptions(opts) { + var matches + if (opts.auth) { + matches = opts.auth.match(/^(.+):(.+)$/) + if (matches) { + opts.username = matches[1] + opts.password = matches[2] + } else { + opts.username = opts.auth + } + } + } + + /** + * connect - connect to an MQTT broker. + * + * @param {String} [brokerUrl] - url of the broker, optional + * @param {Object} opts - see MqttClient#constructor + */ + function connect(brokerUrl, opts) { + if ((typeof brokerUrl === 'object') && !opts) { + opts = brokerUrl + brokerUrl = null + } + + opts = opts || {} + + if (brokerUrl) { + var parsed = url.parse(brokerUrl, true) + if (parsed.port != null) { + parsed.port = Number(parsed.port) + } + + opts = xtend(parsed, opts) + + if (opts.protocol === null) { + throw new Error('Missing protocol') + } + opts.protocol = opts.protocol.replace(/:$/, '') + } + + // merge in the auth options if supplied + parseAuthOptions(opts) + + // support clientId passed in the query string of the url + if (opts.query && typeof opts.query.clientId === 'string') { + opts.clientId = opts.query.clientId + } + + if (opts.cert && opts.key) { + if (opts.protocol) { + if (['mqtts', 'wss', 'wxs'].indexOf(opts.protocol) === -1) { + switch (opts.protocol) { + case 'mqtt': + opts.protocol = 'mqtts' + break + case 'ws': + opts.protocol = 'wss' + break + case 'wx': + opts.protocol = 'wxs' + break + default: + throw new Error('Unknown protocol for secure connection: "' + opts.protocol + '"!') + } + } + } else { + // don't know what protocol he want to use, mqtts or wss + throw new Error('Missing secure protocol key') + } + } + + if (!protocols[opts.protocol]) { + var isSecure = ['mqtts', 'wss'].indexOf(opts.protocol) !== -1 + opts.protocol = [ + 'mqtt', + 'mqtts', + 'ws', + 'wss', + 'wx', + 'wxs' + ].filter(function(key, index) { + if (isSecure && index % 2 === 0) { + // Skip insecure protocols when requesting a secure one. + return false + } + return (typeof protocols[key] === 'function') + })[0] + } + + if (opts.clean === false && !opts.clientId) { + throw new Error('Missing clientId for unclean clients') + } + + if (opts.protocol) { + opts.defaultProtocol = opts.protocol + } + + function wrapper(client) { + if (opts.servers) { + if (!client._reconnectCount || client._reconnectCount === opts.servers.length) { + client._reconnectCount = 0 + } + + opts.host = opts.servers[client._reconnectCount].host + opts.port = opts.servers[client._reconnectCount].port + opts.protocol = (!opts.servers[client._reconnectCount].protocol ? opts.defaultProtocol : opts.servers[client._reconnectCount].protocol) + opts.hostname = opts.host + + client._reconnectCount++ + } + + return protocols[opts.protocol](client, opts) + } + + return new MqttClient(wrapper, opts) + } + + module.exports = connect + module.exports.connect = connect + module.exports.MqttClient = MqttClient + module.exports.Store = Store + + }).call(this, require('_process')) + }, { + "../client": 1, + "../store": 6, + "./tcp": 2, + "./tls": 3, + "./ws": 4, + "./wx": 5, + "_process": 92, + "url": 112, + "xtend": 120 + }], + 9: [function(require, module, exports) { + 'use strict' + + exports.byteLength = byteLength + exports.toByteArray = toByteArray + exports.fromByteArray = fromByteArray + + var lookup = [] + var revLookup = [] + var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array + + var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' + for (var i = 0, len = code.length; i < len; ++i) { + lookup[i] = code[i] + revLookup[code.charCodeAt(i)] = i + } + + // Support decoding URL-safe base64 strings, as Node.js does. + // See: https://en.wikipedia.org/wiki/Base64#URL_applications + revLookup['-'.charCodeAt(0)] = 62 + revLookup['_'.charCodeAt(0)] = 63 + + function getLens(b64) { + var len = b64.length + + if (len % 4 > 0) { + throw new Error('Invalid string. Length must be a multiple of 4') + } + + // Trim off extra bytes after placeholder bytes are found + // See: https://github.com/beatgammit/base64-js/issues/42 + var validLen = b64.indexOf('=') + if (validLen === -1) validLen = len + + var placeHoldersLen = validLen === len ? + 0 : + 4 - (validLen % 4) + + return [validLen, placeHoldersLen] + } + + // base64 is 4/3 + up to two characters of the original data + function byteLength(b64) { + var lens = getLens(b64) + var validLen = lens[0] + var placeHoldersLen = lens[1] + return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen + } + + function _byteLength(b64, validLen, placeHoldersLen) { + return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen + } + + function toByteArray(b64) { + var tmp + var lens = getLens(b64) + var validLen = lens[0] + var placeHoldersLen = lens[1] + + var arr = new Arr(_byteLength(b64, validLen, placeHoldersLen)) + + var curByte = 0 + + // if there are placeholders, only get up to the last complete 4 chars + var len = placeHoldersLen > 0 ? + validLen - 4 : + validLen + + for (var i = 0; i < len; i += 4) { + tmp = + (revLookup[b64.charCodeAt(i)] << 18) | + (revLookup[b64.charCodeAt(i + 1)] << 12) | + (revLookup[b64.charCodeAt(i + 2)] << 6) | + revLookup[b64.charCodeAt(i + 3)] + arr[curByte++] = (tmp >> 16) & 0xFF + arr[curByte++] = (tmp >> 8) & 0xFF + arr[curByte++] = tmp & 0xFF + } + + if (placeHoldersLen === 2) { + tmp = + (revLookup[b64.charCodeAt(i)] << 2) | + (revLookup[b64.charCodeAt(i + 1)] >> 4) + arr[curByte++] = tmp & 0xFF + } + + if (placeHoldersLen === 1) { + tmp = + (revLookup[b64.charCodeAt(i)] << 10) | + (revLookup[b64.charCodeAt(i + 1)] << 4) | + (revLookup[b64.charCodeAt(i + 2)] >> 2) + arr[curByte++] = (tmp >> 8) & 0xFF + arr[curByte++] = tmp & 0xFF + } + + return arr + } + + function tripletToBase64(num) { + return lookup[num >> 18 & 0x3F] + + lookup[num >> 12 & 0x3F] + + lookup[num >> 6 & 0x3F] + + lookup[num & 0x3F] + } + + function encodeChunk(uint8, start, end) { + var tmp + var output = [] + for (var i = start; i < end; i += 3) { + tmp = + ((uint8[i] << 16) & 0xFF0000) + + ((uint8[i + 1] << 8) & 0xFF00) + + (uint8[i + 2] & 0xFF) + output.push(tripletToBase64(tmp)) + } + return output.join('') + } + + function fromByteArray(uint8) { + var tmp + var len = uint8.length + var extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes + var parts = [] + var maxChunkLength = 16383 // must be multiple of 3 + + // go through the array every three bytes, we'll deal with trailing stuff later + for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) { + parts.push(encodeChunk( + uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength) + )) + } + + // pad the end with zeros, but make sure to not forget the extra bytes + if (extraBytes === 1) { + tmp = uint8[len - 1] + parts.push( + lookup[tmp >> 2] + + lookup[(tmp << 4) & 0x3F] + + '==' + ) + } else if (extraBytes === 2) { + tmp = (uint8[len - 2] << 8) + uint8[len - 1] + parts.push( + lookup[tmp >> 10] + + lookup[(tmp >> 4) & 0x3F] + + lookup[(tmp << 2) & 0x3F] + + '=' + ) + } + + return parts.join('') + } + + }, {}], + 10: [function(require, module, exports) { + var DuplexStream = require('readable-stream/duplex'), + util = require('util'), + Buffer = require('safe-buffer').Buffer + + + function BufferList(callback) { + if (!(this instanceof BufferList)) + return new BufferList(callback) + + this._bufs = [] + this.length = 0 + + if (typeof callback == 'function') { + this._callback = callback + + var piper = function piper(err) { + if (this._callback) { + this._callback(err) + this._callback = null + } + }.bind(this) + + this.on('pipe', function onPipe(src) { + src.on('error', piper) + }) + this.on('unpipe', function onUnpipe(src) { + src.removeListener('error', piper) + }) + } else { + this.append(callback) + } + + DuplexStream.call(this) + } + + + util.inherits(BufferList, DuplexStream) + + + BufferList.prototype._offset = function _offset(offset) { + var tot = 0, + i = 0, + _t + if (offset === 0) return [0, 0] + for (; i < this._bufs.length; i++) { + _t = tot + this._bufs[i].length + if (offset < _t || i == this._bufs.length - 1) + return [i, offset - tot] + tot = _t + } + } + + + BufferList.prototype.append = function append(buf) { + var i = 0 + + if (Buffer.isBuffer(buf)) { + this._appendBuffer(buf); + } else if (Array.isArray(buf)) { + for (; i < buf.length; i++) + this.append(buf[i]) + } else if (buf instanceof BufferList) { + // unwrap argument into individual BufferLists + for (; i < buf._bufs.length; i++) + this.append(buf._bufs[i]) + } else if (buf != null) { + // coerce number arguments to strings, since Buffer(number) does + // uninitialized memory allocation + if (typeof buf == 'number') + buf = buf.toString() + + this._appendBuffer(Buffer.from(buf)); + } + + return this + } + + + BufferList.prototype._appendBuffer = function appendBuffer(buf) { + this._bufs.push(buf) + this.length += buf.length + } + + + BufferList.prototype._write = function _write(buf, encoding, callback) { + this._appendBuffer(buf) + + if (typeof callback == 'function') + callback() + } + + + BufferList.prototype._read = function _read(size) { + if (!this.length) + return this.push(null) + + size = Math.min(size, this.length) + this.push(this.slice(0, size)) + this.consume(size) + } + + + BufferList.prototype.end = function end(chunk) { + DuplexStream.prototype.end.call(this, chunk) + + if (this._callback) { + this._callback(null, this.slice()) + this._callback = null + } + } + + + BufferList.prototype.get = function get(index) { + return this.slice(index, index + 1)[0] + } + + + BufferList.prototype.slice = function slice(start, end) { + if (typeof start == 'number' && start < 0) + start += this.length + if (typeof end == 'number' && end < 0) + end += this.length + return this.copy(null, 0, start, end) + } + + + BufferList.prototype.copy = function copy(dst, dstStart, srcStart, srcEnd) { + if (typeof srcStart != 'number' || srcStart < 0) + srcStart = 0 + if (typeof srcEnd != 'number' || srcEnd > this.length) + srcEnd = this.length + if (srcStart >= this.length) + return dst || Buffer.alloc(0) + if (srcEnd <= 0) + return dst || Buffer.alloc(0) + + var copy = !!dst, + off = this._offset(srcStart), + len = srcEnd - srcStart, + bytes = len, + bufoff = (copy && dstStart) || 0, + start = off[1], + l, i + + // copy/slice everything + if (srcStart === 0 && srcEnd == this.length) { + if (!copy) { // slice, but full concat if multiple buffers + return this._bufs.length === 1 ? + this._bufs[0] : + Buffer.concat(this._bufs, this.length) + } + + // copy, need to copy individual buffers + for (i = 0; i < this._bufs.length; i++) { + this._bufs[i].copy(dst, bufoff) + bufoff += this._bufs[i].length + } + + return dst + } + + // easy, cheap case where it's a subset of one of the buffers + if (bytes <= this._bufs[off[0]].length - start) { + return copy ? + this._bufs[off[0]].copy(dst, dstStart, start, start + bytes) : + this._bufs[off[0]].slice(start, start + bytes) + } + + if (!copy) // a slice, we need something to copy in to + dst = Buffer.allocUnsafe(len) + + for (i = off[0]; i < this._bufs.length; i++) { + l = this._bufs[i].length - start + + if (bytes > l) { + this._bufs[i].copy(dst, bufoff, start) + } else { + this._bufs[i].copy(dst, bufoff, start, start + bytes) + break + } + + bufoff += l + bytes -= l + + if (start) + start = 0 + } + + return dst + } + + BufferList.prototype.shallowSlice = function shallowSlice(start, end) { + start = start || 0 + end = end || this.length + + if (start < 0) + start += this.length + if (end < 0) + end += this.length + + var startOffset = this._offset(start), + endOffset = this._offset(end), + buffers = this._bufs.slice(startOffset[0], endOffset[0] + 1) + + if (endOffset[1] == 0) + buffers.pop() + else + buffers[buffers.length - 1] = buffers[buffers.length - 1].slice(0, endOffset[1]) + + if (startOffset[1] != 0) + buffers[0] = buffers[0].slice(startOffset[1]) + + return new BufferList(buffers) + } + + BufferList.prototype.toString = function toString(encoding, start, end) { + return this.slice(start, end).toString(encoding) + } + + BufferList.prototype.consume = function consume(bytes) { + while (this._bufs.length) { + if (bytes >= this._bufs[0].length) { + bytes -= this._bufs[0].length + this.length -= this._bufs[0].length + this._bufs.shift() + } else { + this._bufs[0] = this._bufs[0].slice(bytes) + this.length -= bytes + break + } + } + return this + } + + + BufferList.prototype.duplicate = function duplicate() { + var i = 0, + copy = new BufferList() + + for (; i < this._bufs.length; i++) + copy.append(this._bufs[i]) + + return copy + } + + + BufferList.prototype.destroy = function destroy() { + this._bufs.length = 0 + this.length = 0 + this.push(null) + } + + + ; + (function() { + var methods = { + 'readDoubleBE': 8, + 'readDoubleLE': 8, + 'readFloatBE': 4, + 'readFloatLE': 4, + 'readInt32BE': 4, + 'readInt32LE': 4, + 'readUInt32BE': 4, + 'readUInt32LE': 4, + 'readInt16BE': 2, + 'readInt16LE': 2, + 'readUInt16BE': 2, + 'readUInt16LE': 2, + 'readInt8': 1, + 'readUInt8': 1 + } + + for (var m in methods) { + (function(m) { + BufferList.prototype[m] = function(offset) { + return this.slice(offset, offset + methods[m])[m](0) + } + }(m)) + } + }()) + + + module.exports = BufferList + + }, { + "readable-stream/duplex": 97, + "safe-buffer": 108, + "util": 116 + }], + 11: [function(require, module, exports) { + + }, {}], + 12: [function(require, module, exports) { + /*! + * The buffer module from node.js, for the browser. + * + * @author Feross Aboukhadijeh + * @license MIT + */ + /* eslint-disable no-proto */ + + 'use strict' + + var base64 = require('base64-js') + var ieee754 = require('ieee754') + + exports.Buffer = Buffer + exports.SlowBuffer = SlowBuffer + exports.INSPECT_MAX_BYTES = 50 + + var K_MAX_LENGTH = 0x7fffffff + exports.kMaxLength = K_MAX_LENGTH + + /** + * If `Buffer.TYPED_ARRAY_SUPPORT`: + * === true Use Uint8Array implementation (fastest) + * === false Print warning and recommend using `buffer` v4.x which has an Object + * implementation (most compatible, even IE6) + * + * Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+, + * Opera 11.6+, iOS 4.2+. + * + * We report that the browser does not support typed arrays if the are not subclassable + * using __proto__. Firefox 4-29 lacks support for adding new properties to `Uint8Array` + * (See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438). IE 10 lacks support + * for __proto__ and has a buggy typed array implementation. + */ + Buffer.TYPED_ARRAY_SUPPORT = typedArraySupport() + + if (!Buffer.TYPED_ARRAY_SUPPORT && typeof console !== 'undefined' && + typeof console.error === 'function') { + console.error( + 'This browser lacks typed array (Uint8Array) support which is required by ' + + '`buffer` v5.x. Use `buffer` v4.x if you require old browser support.' + ) + } + + function typedArraySupport() { + // Can typed array instances can be augmented? + try { + var arr = new Uint8Array(1) + arr.__proto__ = { + __proto__: Uint8Array.prototype, + foo: function() { + return 42 + } + } + return arr.foo() === 42 + } catch (e) { + return false + } + } + + Object.defineProperty(Buffer.prototype, 'parent', { + enumerable: true, + get: function() { + if (!Buffer.isBuffer(this)) return undefined + return this.buffer + } + }) + + Object.defineProperty(Buffer.prototype, 'offset', { + enumerable: true, + get: function() { + if (!Buffer.isBuffer(this)) return undefined + return this.byteOffset + } + }) + + function createBuffer(length) { + if (length > K_MAX_LENGTH) { + throw new RangeError('The value "' + length + '" is invalid for option "size"') + } + // Return an augmented `Uint8Array` instance + var buf = new Uint8Array(length) + buf.__proto__ = Buffer.prototype + return buf + } + + /** + * The Buffer constructor returns instances of `Uint8Array` that have their + * prototype changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of + * `Uint8Array`, so the returned instances will have all the node `Buffer` methods + * and the `Uint8Array` methods. Square bracket notation works as expected -- it + * returns a single octet. + * + * The `Uint8Array` prototype remains unmodified. + */ + + function Buffer(arg, encodingOrOffset, length) { + // Common case. + if (typeof arg === 'number') { + if (typeof encodingOrOffset === 'string') { + throw new TypeError( + 'The "string" argument must be of type string. Received type number' + ) + } + return allocUnsafe(arg) + } + return from(arg, encodingOrOffset, length) + } + + // Fix subarray() in ES2016. See: https://github.com/feross/buffer/pull/97 + if (typeof Symbol !== 'undefined' && Symbol.species != null && + Buffer[Symbol.species] === Buffer) { + Object.defineProperty(Buffer, Symbol.species, { + value: null, + configurable: true, + enumerable: false, + writable: false + }) + } + + Buffer.poolSize = 8192 // not used by this implementation + + function from(value, encodingOrOffset, length) { + if (typeof value === 'string') { + return fromString(value, encodingOrOffset) + } + + if (ArrayBuffer.isView(value)) { + return fromArrayLike(value) + } + + if (value == null) { + throw TypeError( + 'The first argument must be one of type string, Buffer, ArrayBuffer, Array, ' + + 'or Array-like Object. Received type ' + (typeof value) + ) + } + + if (isInstance(value, ArrayBuffer) || + (value && isInstance(value.buffer, ArrayBuffer))) { + return fromArrayBuffer(value, encodingOrOffset, length) + } + + if (typeof value === 'number') { + throw new TypeError( + 'The "value" argument must not be of type number. Received type number' + ) + } + + var valueOf = value.valueOf && value.valueOf() + if (valueOf != null && valueOf !== value) { + return Buffer.from(valueOf, encodingOrOffset, length) + } + + var b = fromObject(value) + if (b) return b + + if (typeof Symbol !== 'undefined' && Symbol.toPrimitive != null && + typeof value[Symbol.toPrimitive] === 'function') { + return Buffer.from( + value[Symbol.toPrimitive]('string'), encodingOrOffset, length + ) + } + + throw new TypeError( + 'The first argument must be one of type string, Buffer, ArrayBuffer, Array, ' + + 'or Array-like Object. Received type ' + (typeof value) + ) + } + + /** + * Functionally equivalent to Buffer(arg, encoding) but throws a TypeError + * if value is a number. + * Buffer.from(str[, encoding]) + * Buffer.from(array) + * Buffer.from(buffer) + * Buffer.from(arrayBuffer[, byteOffset[, length]]) + **/ + Buffer.from = function(value, encodingOrOffset, length) { + return from(value, encodingOrOffset, length) + } + + // Note: Change prototype *after* Buffer.from is defined to workaround Chrome bug: + // https://github.com/feross/buffer/pull/148 + Buffer.prototype.__proto__ = Uint8Array.prototype + Buffer.__proto__ = Uint8Array + + function assertSize(size) { + if (typeof size !== 'number') { + throw new TypeError('"size" argument must be of type number') + } else if (size < 0) { + throw new RangeError('The value "' + size + '" is invalid for option "size"') + } + } + + function alloc(size, fill, encoding) { + assertSize(size) + if (size <= 0) { + return createBuffer(size) + } + if (fill !== undefined) { + // Only pay attention to encoding if it's a string. This + // prevents accidentally sending in a number that would + // be interpretted as a start offset. + return typeof encoding === 'string' ? + createBuffer(size).fill(fill, encoding) : + createBuffer(size).fill(fill) + } + return createBuffer(size) + } + + /** + * Creates a new filled Buffer instance. + * alloc(size[, fill[, encoding]]) + **/ + Buffer.alloc = function(size, fill, encoding) { + return alloc(size, fill, encoding) + } + + function allocUnsafe(size) { + assertSize(size) + return createBuffer(size < 0 ? 0 : checked(size) | 0) + } + + /** + * Equivalent to Buffer(num), by default creates a non-zero-filled Buffer instance. + * */ + Buffer.allocUnsafe = function(size) { + return allocUnsafe(size) + } + /** + * Equivalent to SlowBuffer(num), by default creates a non-zero-filled Buffer instance. + */ + Buffer.allocUnsafeSlow = function(size) { + return allocUnsafe(size) + } + + function fromString(string, encoding) { + if (typeof encoding !== 'string' || encoding === '') { + encoding = 'utf8' + } + + if (!Buffer.isEncoding(encoding)) { + throw new TypeError('Unknown encoding: ' + encoding) + } + + var length = byteLength(string, encoding) | 0 + var buf = createBuffer(length) + + var actual = buf.write(string, encoding) + + if (actual !== length) { + // Writing a hex string, for example, that contains invalid characters will + // cause everything after the first invalid character to be ignored. (e.g. + // 'abxxcd' will be treated as 'ab') + buf = buf.slice(0, actual) + } + + return buf + } + + function fromArrayLike(array) { + var length = array.length < 0 ? 0 : checked(array.length) | 0 + var buf = createBuffer(length) + for (var i = 0; i < length; i += 1) { + buf[i] = array[i] & 255 + } + return buf + } + + function fromArrayBuffer(array, byteOffset, length) { + if (byteOffset < 0 || array.byteLength < byteOffset) { + throw new RangeError('"offset" is outside of buffer bounds') + } + + if (array.byteLength < byteOffset + (length || 0)) { + throw new RangeError('"length" is outside of buffer bounds') + } + + var buf + if (byteOffset === undefined && length === undefined) { + buf = new Uint8Array(array) + } else if (length === undefined) { + buf = new Uint8Array(array, byteOffset) + } else { + buf = new Uint8Array(array, byteOffset, length) + } + + // Return an augmented `Uint8Array` instance + buf.__proto__ = Buffer.prototype + return buf + } + + function fromObject(obj) { + if (Buffer.isBuffer(obj)) { + var len = checked(obj.length) | 0 + var buf = createBuffer(len) + + if (buf.length === 0) { + return buf + } + + obj.copy(buf, 0, 0, len) + return buf + } + + if (obj.length !== undefined) { + if (typeof obj.length !== 'number' || numberIsNaN(obj.length)) { + return createBuffer(0) + } + return fromArrayLike(obj) + } + + if (obj.type === 'Buffer' && Array.isArray(obj.data)) { + return fromArrayLike(obj.data) + } + } + + function checked(length) { + // Note: cannot use `length < K_MAX_LENGTH` here because that fails when + // length is NaN (which is otherwise coerced to zero.) + if (length >= K_MAX_LENGTH) { + throw new RangeError('Attempt to allocate Buffer larger than maximum ' + + 'size: 0x' + K_MAX_LENGTH.toString(16) + ' bytes') + } + return length | 0 + } + + function SlowBuffer(length) { + if (+length != length) { // eslint-disable-line eqeqeq + length = 0 + } + return Buffer.alloc(+length) + } + + Buffer.isBuffer = function isBuffer(b) { + return b != null && b._isBuffer === true && + b !== Buffer.prototype // so Buffer.isBuffer(Buffer.prototype) will be false + } + + Buffer.compare = function compare(a, b) { + if (isInstance(a, Uint8Array)) a = Buffer.from(a, a.offset, a.byteLength) + if (isInstance(b, Uint8Array)) b = Buffer.from(b, b.offset, b.byteLength) + if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) { + throw new TypeError( + 'The "buf1", "buf2" arguments must be one of type Buffer or Uint8Array' + ) + } + + if (a === b) return 0 + + var x = a.length + var y = b.length + + for (var i = 0, len = Math.min(x, y); i < len; ++i) { + if (a[i] !== b[i]) { + x = a[i] + y = b[i] + break + } + } + + if (x < y) return -1 + if (y < x) return 1 + return 0 + } + + Buffer.isEncoding = function isEncoding(encoding) { + switch (String(encoding).toLowerCase()) { + case 'hex': + case 'utf8': + case 'utf-8': + case 'ascii': + case 'latin1': + case 'binary': + case 'base64': + case 'ucs2': + case 'ucs-2': + case 'utf16le': + case 'utf-16le': + return true + default: + return false + } + } + + Buffer.concat = function concat(list, length) { + if (!Array.isArray(list)) { + throw new TypeError('"list" argument must be an Array of Buffers') + } + + if (list.length === 0) { + return Buffer.alloc(0) + } + + var i + if (length === undefined) { + length = 0 + for (i = 0; i < list.length; ++i) { + length += list[i].length + } + } + + var buffer = Buffer.allocUnsafe(length) + var pos = 0 + for (i = 0; i < list.length; ++i) { + var buf = list[i] + if (isInstance(buf, Uint8Array)) { + buf = Buffer.from(buf) + } + if (!Buffer.isBuffer(buf)) { + throw new TypeError('"list" argument must be an Array of Buffers') + } + buf.copy(buffer, pos) + pos += buf.length + } + return buffer + } + + function byteLength(string, encoding) { + if (Buffer.isBuffer(string)) { + return string.length + } + if (ArrayBuffer.isView(string) || isInstance(string, ArrayBuffer)) { + return string.byteLength + } + if (typeof string !== 'string') { + throw new TypeError( + 'The "string" argument must be one of type string, Buffer, or ArrayBuffer. ' + + 'Received type ' + typeof string + ) + } + + var len = string.length + var mustMatch = (arguments.length > 2 && arguments[2] === true) + if (!mustMatch && len === 0) return 0 + + // Use a for loop to avoid recursion + var loweredCase = false + for (;;) { + switch (encoding) { + case 'ascii': + case 'latin1': + case 'binary': + return len + case 'utf8': + case 'utf-8': + return utf8ToBytes(string).length + case 'ucs2': + case 'ucs-2': + case 'utf16le': + case 'utf-16le': + return len * 2 + case 'hex': + return len >>> 1 + case 'base64': + return base64ToBytes(string).length + default: + if (loweredCase) { + return mustMatch ? -1 : utf8ToBytes(string).length // assume utf8 + } + encoding = ('' + encoding).toLowerCase() + loweredCase = true + } + } + } + Buffer.byteLength = byteLength + + function slowToString(encoding, start, end) { + var loweredCase = false + + // No need to verify that "this.length <= MAX_UINT32" since it's a read-only + // property of a typed array. + + // This behaves neither like String nor Uint8Array in that we set start/end + // to their upper/lower bounds if the value passed is out of range. + // undefined is handled specially as per ECMA-262 6th Edition, + // Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization. + if (start === undefined || start < 0) { + start = 0 + } + // Return early if start > this.length. Done here to prevent potential uint32 + // coercion fail below. + if (start > this.length) { + return '' + } + + if (end === undefined || end > this.length) { + end = this.length + } + + if (end <= 0) { + return '' + } + + // Force coersion to uint32. This will also coerce falsey/NaN values to 0. + end >>>= 0 + start >>>= 0 + + if (end <= start) { + return '' + } + + if (!encoding) encoding = 'utf8' + + while (true) { + switch (encoding) { + case 'hex': + return hexSlice(this, start, end) + + case 'utf8': + case 'utf-8': + return utf8Slice(this, start, end) + + case 'ascii': + return asciiSlice(this, start, end) + + case 'latin1': + case 'binary': + return latin1Slice(this, start, end) + + case 'base64': + return base64Slice(this, start, end) + + case 'ucs2': + case 'ucs-2': + case 'utf16le': + case 'utf-16le': + return utf16leSlice(this, start, end) + + default: + if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding) + encoding = (encoding + '').toLowerCase() + loweredCase = true + } + } + } + + // This property is used by `Buffer.isBuffer` (and the `is-buffer` npm package) + // to detect a Buffer instance. It's not possible to use `instanceof Buffer` + // reliably in a browserify context because there could be multiple different + // copies of the 'buffer' package in use. This method works even for Buffer + // instances that were created from another copy of the `buffer` package. + // See: https://github.com/feross/buffer/issues/154 + Buffer.prototype._isBuffer = true + + function swap(b, n, m) { + var i = b[n] + b[n] = b[m] + b[m] = i + } + + Buffer.prototype.swap16 = function swap16() { + var len = this.length + if (len % 2 !== 0) { + throw new RangeError('Buffer size must be a multiple of 16-bits') + } + for (var i = 0; i < len; i += 2) { + swap(this, i, i + 1) + } + return this + } + + Buffer.prototype.swap32 = function swap32() { + var len = this.length + if (len % 4 !== 0) { + throw new RangeError('Buffer size must be a multiple of 32-bits') + } + for (var i = 0; i < len; i += 4) { + swap(this, i, i + 3) + swap(this, i + 1, i + 2) + } + return this + } + + Buffer.prototype.swap64 = function swap64() { + var len = this.length + if (len % 8 !== 0) { + throw new RangeError('Buffer size must be a multiple of 64-bits') + } + for (var i = 0; i < len; i += 8) { + swap(this, i, i + 7) + swap(this, i + 1, i + 6) + swap(this, i + 2, i + 5) + swap(this, i + 3, i + 4) + } + return this + } + + Buffer.prototype.toString = function toString() { + var length = this.length + if (length === 0) return '' + if (arguments.length === 0) return utf8Slice(this, 0, length) + return slowToString.apply(this, arguments) + } + + Buffer.prototype.toLocaleString = Buffer.prototype.toString + + Buffer.prototype.equals = function equals(b) { + if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer') + if (this === b) return true + return Buffer.compare(this, b) === 0 + } + + Buffer.prototype.inspect = function inspect() { + var str = '' + var max = exports.INSPECT_MAX_BYTES + str = this.toString('hex', 0, max).replace(/(.{2})/g, '$1 ').trim() + if (this.length > max) str += ' ... ' + return '' + } + + Buffer.prototype.compare = function compare(target, start, end, thisStart, thisEnd) { + if (isInstance(target, Uint8Array)) { + target = Buffer.from(target, target.offset, target.byteLength) + } + if (!Buffer.isBuffer(target)) { + throw new TypeError( + 'The "target" argument must be one of type Buffer or Uint8Array. ' + + 'Received type ' + (typeof target) + ) + } + + if (start === undefined) { + start = 0 + } + if (end === undefined) { + end = target ? target.length : 0 + } + if (thisStart === undefined) { + thisStart = 0 + } + if (thisEnd === undefined) { + thisEnd = this.length + } + + if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) { + throw new RangeError('out of range index') + } + + if (thisStart >= thisEnd && start >= end) { + return 0 + } + if (thisStart >= thisEnd) { + return -1 + } + if (start >= end) { + return 1 + } + + start >>>= 0 + end >>>= 0 + thisStart >>>= 0 + thisEnd >>>= 0 + + if (this === target) return 0 + + var x = thisEnd - thisStart + var y = end - start + var len = Math.min(x, y) + + var thisCopy = this.slice(thisStart, thisEnd) + var targetCopy = target.slice(start, end) + + for (var i = 0; i < len; ++i) { + if (thisCopy[i] !== targetCopy[i]) { + x = thisCopy[i] + y = targetCopy[i] + break + } + } + + if (x < y) return -1 + if (y < x) return 1 + return 0 + } + + // Finds either the first index of `val` in `buffer` at offset >= `byteOffset`, + // OR the last index of `val` in `buffer` at offset <= `byteOffset`. + // + // Arguments: + // - buffer - a Buffer to search + // - val - a string, Buffer, or number + // - byteOffset - an index into `buffer`; will be clamped to an int32 + // - encoding - an optional encoding, relevant is val is a string + // - dir - true for indexOf, false for lastIndexOf + function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) { + // Empty buffer means no match + if (buffer.length === 0) return -1 + + // Normalize byteOffset + if (typeof byteOffset === 'string') { + encoding = byteOffset + byteOffset = 0 + } else if (byteOffset > 0x7fffffff) { + byteOffset = 0x7fffffff + } else if (byteOffset < -0x80000000) { + byteOffset = -0x80000000 + } + byteOffset = +byteOffset // Coerce to Number. + if (numberIsNaN(byteOffset)) { + // byteOffset: it it's undefined, null, NaN, "foo", etc, search whole buffer + byteOffset = dir ? 0 : (buffer.length - 1) + } + + // Normalize byteOffset: negative offsets start from the end of the buffer + if (byteOffset < 0) byteOffset = buffer.length + byteOffset + if (byteOffset >= buffer.length) { + if (dir) return -1 + else byteOffset = buffer.length - 1 + } else if (byteOffset < 0) { + if (dir) byteOffset = 0 + else return -1 + } + + // Normalize val + if (typeof val === 'string') { + val = Buffer.from(val, encoding) + } + + // Finally, search either indexOf (if dir is true) or lastIndexOf + if (Buffer.isBuffer(val)) { + // Special case: looking for empty string/buffer always fails + if (val.length === 0) { + return -1 + } + return arrayIndexOf(buffer, val, byteOffset, encoding, dir) + } else if (typeof val === 'number') { + val = val & 0xFF // Search for a byte value [0-255] + if (typeof Uint8Array.prototype.indexOf === 'function') { + if (dir) { + return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset) + } else { + return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset) + } + } + return arrayIndexOf(buffer, [val], byteOffset, encoding, dir) + } + + throw new TypeError('val must be string, number or Buffer') + } + + function arrayIndexOf(arr, val, byteOffset, encoding, dir) { + var indexSize = 1 + var arrLength = arr.length + var valLength = val.length + + if (encoding !== undefined) { + encoding = String(encoding).toLowerCase() + if (encoding === 'ucs2' || encoding === 'ucs-2' || + encoding === 'utf16le' || encoding === 'utf-16le') { + if (arr.length < 2 || val.length < 2) { + return -1 + } + indexSize = 2 + arrLength /= 2 + valLength /= 2 + byteOffset /= 2 + } + } + + function read(buf, i) { + if (indexSize === 1) { + return buf[i] + } else { + return buf.readUInt16BE(i * indexSize) + } + } + + var i + if (dir) { + var foundIndex = -1 + for (i = byteOffset; i < arrLength; i++) { + if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) { + if (foundIndex === -1) foundIndex = i + if (i - foundIndex + 1 === valLength) return foundIndex * indexSize + } else { + if (foundIndex !== -1) i -= i - foundIndex + foundIndex = -1 + } + } + } else { + if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength + for (i = byteOffset; i >= 0; i--) { + var found = true + for (var j = 0; j < valLength; j++) { + if (read(arr, i + j) !== read(val, j)) { + found = false + break + } + } + if (found) return i + } + } + + return -1 + } + + Buffer.prototype.includes = function includes(val, byteOffset, encoding) { + return this.indexOf(val, byteOffset, encoding) !== -1 + } + + Buffer.prototype.indexOf = function indexOf(val, byteOffset, encoding) { + return bidirectionalIndexOf(this, val, byteOffset, encoding, true) + } + + Buffer.prototype.lastIndexOf = function lastIndexOf(val, byteOffset, encoding) { + return bidirectionalIndexOf(this, val, byteOffset, encoding, false) + } + + function hexWrite(buf, string, offset, length) { + offset = Number(offset) || 0 + var remaining = buf.length - offset + if (!length) { + length = remaining + } else { + length = Number(length) + if (length > remaining) { + length = remaining + } + } + + var strLen = string.length + + if (length > strLen / 2) { + length = strLen / 2 + } + for (var i = 0; i < length; ++i) { + var parsed = parseInt(string.substr(i * 2, 2), 16) + if (numberIsNaN(parsed)) return i + buf[offset + i] = parsed + } + return i + } + + function utf8Write(buf, string, offset, length) { + return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length) + } + + function asciiWrite(buf, string, offset, length) { + return blitBuffer(asciiToBytes(string), buf, offset, length) + } + + function latin1Write(buf, string, offset, length) { + return asciiWrite(buf, string, offset, length) + } + + function base64Write(buf, string, offset, length) { + return blitBuffer(base64ToBytes(string), buf, offset, length) + } + + function ucs2Write(buf, string, offset, length) { + return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length) + } + + Buffer.prototype.write = function write(string, offset, length, encoding) { + // Buffer#write(string) + if (offset === undefined) { + encoding = 'utf8' + length = this.length + offset = 0 + // Buffer#write(string, encoding) + } else if (length === undefined && typeof offset === 'string') { + encoding = offset + length = this.length + offset = 0 + // Buffer#write(string, offset[, length][, encoding]) + } else if (isFinite(offset)) { + offset = offset >>> 0 + if (isFinite(length)) { + length = length >>> 0 + if (encoding === undefined) encoding = 'utf8' + } else { + encoding = length + length = undefined + } + } else { + throw new Error( + 'Buffer.write(string, encoding, offset[, length]) is no longer supported' + ) + } + + var remaining = this.length - offset + if (length === undefined || length > remaining) length = remaining + + if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) { + throw new RangeError('Attempt to write outside buffer bounds') + } + + if (!encoding) encoding = 'utf8' + + var loweredCase = false + for (;;) { + switch (encoding) { + case 'hex': + return hexWrite(this, string, offset, length) + + case 'utf8': + case 'utf-8': + return utf8Write(this, string, offset, length) + + case 'ascii': + return asciiWrite(this, string, offset, length) + + case 'latin1': + case 'binary': + return latin1Write(this, string, offset, length) + + case 'base64': + // Warning: maxLength not taken into account in base64Write + return base64Write(this, string, offset, length) + + case 'ucs2': + case 'ucs-2': + case 'utf16le': + case 'utf-16le': + return ucs2Write(this, string, offset, length) + + default: + if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding) + encoding = ('' + encoding).toLowerCase() + loweredCase = true + } + } + } + + Buffer.prototype.toJSON = function toJSON() { + return { + type: 'Buffer', + data: Array.prototype.slice.call(this._arr || this, 0) + } + } + + function base64Slice(buf, start, end) { + if (start === 0 && end === buf.length) { + return base64.fromByteArray(buf) + } else { + return base64.fromByteArray(buf.slice(start, end)) + } + } + + function utf8Slice(buf, start, end) { + end = Math.min(buf.length, end) + var res = [] + + var i = start + while (i < end) { + var firstByte = buf[i] + var codePoint = null + var bytesPerSequence = (firstByte > 0xEF) ? 4 : + (firstByte > 0xDF) ? 3 : + (firstByte > 0xBF) ? 2 : + 1 + + if (i + bytesPerSequence <= end) { + var secondByte, thirdByte, fourthByte, tempCodePoint + + switch (bytesPerSequence) { + case 1: + if (firstByte < 0x80) { + codePoint = firstByte + } + break + case 2: + secondByte = buf[i + 1] + if ((secondByte & 0xC0) === 0x80) { + tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F) + if (tempCodePoint > 0x7F) { + codePoint = tempCodePoint + } + } + break + case 3: + secondByte = buf[i + 1] + thirdByte = buf[i + 2] + if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) { + tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F) + if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) { + codePoint = tempCodePoint + } + } + break + case 4: + secondByte = buf[i + 1] + thirdByte = buf[i + 2] + fourthByte = buf[i + 3] + if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) { + tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F) + if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) { + codePoint = tempCodePoint + } + } + } + } + + if (codePoint === null) { + // we did not generate a valid codePoint so insert a + // replacement char (U+FFFD) and advance only 1 byte + codePoint = 0xFFFD + bytesPerSequence = 1 + } else if (codePoint > 0xFFFF) { + // encode to utf16 (surrogate pair dance) + codePoint -= 0x10000 + res.push(codePoint >>> 10 & 0x3FF | 0xD800) + codePoint = 0xDC00 | codePoint & 0x3FF + } + + res.push(codePoint) + i += bytesPerSequence + } + + return decodeCodePointsArray(res) + } + + // Based on http://stackoverflow.com/a/22747272/680742, the browser with + // the lowest limit is Chrome, with 0x10000 args. + // We go 1 magnitude less, for safety + var MAX_ARGUMENTS_LENGTH = 0x1000 + + function decodeCodePointsArray(codePoints) { + var len = codePoints.length + if (len <= MAX_ARGUMENTS_LENGTH) { + return String.fromCharCode.apply(String, codePoints) // avoid extra slice() + } + + // Decode in chunks to avoid "call stack size exceeded". + var res = '' + var i = 0 + while (i < len) { + res += String.fromCharCode.apply( + String, + codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH) + ) + } + return res + } + + function asciiSlice(buf, start, end) { + var ret = '' + end = Math.min(buf.length, end) + + for (var i = start; i < end; ++i) { + ret += String.fromCharCode(buf[i] & 0x7F) + } + return ret + } + + function latin1Slice(buf, start, end) { + var ret = '' + end = Math.min(buf.length, end) + + for (var i = start; i < end; ++i) { + ret += String.fromCharCode(buf[i]) + } + return ret + } + + function hexSlice(buf, start, end) { + var len = buf.length + + if (!start || start < 0) start = 0 + if (!end || end < 0 || end > len) end = len + + var out = '' + for (var i = start; i < end; ++i) { + out += toHex(buf[i]) + } + return out + } + + function utf16leSlice(buf, start, end) { + var bytes = buf.slice(start, end) + var res = '' + for (var i = 0; i < bytes.length; i += 2) { + res += String.fromCharCode(bytes[i] + (bytes[i + 1] * 256)) + } + return res + } + + Buffer.prototype.slice = function slice(start, end) { + var len = this.length + start = ~~start + end = end === undefined ? len : ~~end + + if (start < 0) { + start += len + if (start < 0) start = 0 + } else if (start > len) { + start = len + } + + if (end < 0) { + end += len + if (end < 0) end = 0 + } else if (end > len) { + end = len + } + + if (end < start) end = start + + var newBuf = this.subarray(start, end) + // Return an augmented `Uint8Array` instance + newBuf.__proto__ = Buffer.prototype + return newBuf + } + + /* + * Need to make sure that buffer isn't trying to write out of bounds. + */ + function checkOffset(offset, ext, length) { + if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint') + if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length') + } + + Buffer.prototype.readUIntLE = function readUIntLE(offset, byteLength, noAssert) { + offset = offset >>> 0 + byteLength = byteLength >>> 0 + if (!noAssert) checkOffset(offset, byteLength, this.length) + + var val = this[offset] + var mul = 1 + var i = 0 + while (++i < byteLength && (mul *= 0x100)) { + val += this[offset + i] * mul + } + + return val + } + + Buffer.prototype.readUIntBE = function readUIntBE(offset, byteLength, noAssert) { + offset = offset >>> 0 + byteLength = byteLength >>> 0 + if (!noAssert) { + checkOffset(offset, byteLength, this.length) + } + + var val = this[offset + --byteLength] + var mul = 1 + while (byteLength > 0 && (mul *= 0x100)) { + val += this[offset + --byteLength] * mul + } + + return val + } + + Buffer.prototype.readUInt8 = function readUInt8(offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 1, this.length) + return this[offset] + } + + Buffer.prototype.readUInt16LE = function readUInt16LE(offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 2, this.length) + return this[offset] | (this[offset + 1] << 8) + } + + Buffer.prototype.readUInt16BE = function readUInt16BE(offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 2, this.length) + return (this[offset] << 8) | this[offset + 1] + } + + Buffer.prototype.readUInt32LE = function readUInt32LE(offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 4, this.length) + + return ((this[offset]) | + (this[offset + 1] << 8) | + (this[offset + 2] << 16)) + + (this[offset + 3] * 0x1000000) + } + + Buffer.prototype.readUInt32BE = function readUInt32BE(offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 4, this.length) + + return (this[offset] * 0x1000000) + + ((this[offset + 1] << 16) | + (this[offset + 2] << 8) | + this[offset + 3]) + } + + Buffer.prototype.readIntLE = function readIntLE(offset, byteLength, noAssert) { + offset = offset >>> 0 + byteLength = byteLength >>> 0 + if (!noAssert) checkOffset(offset, byteLength, this.length) + + var val = this[offset] + var mul = 1 + var i = 0 + while (++i < byteLength && (mul *= 0x100)) { + val += this[offset + i] * mul + } + mul *= 0x80 + + if (val >= mul) val -= Math.pow(2, 8 * byteLength) + + return val + } + + Buffer.prototype.readIntBE = function readIntBE(offset, byteLength, noAssert) { + offset = offset >>> 0 + byteLength = byteLength >>> 0 + if (!noAssert) checkOffset(offset, byteLength, this.length) + + var i = byteLength + var mul = 1 + var val = this[offset + --i] + while (i > 0 && (mul *= 0x100)) { + val += this[offset + --i] * mul + } + mul *= 0x80 + + if (val >= mul) val -= Math.pow(2, 8 * byteLength) + + return val + } + + Buffer.prototype.readInt8 = function readInt8(offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 1, this.length) + if (!(this[offset] & 0x80)) return (this[offset]) + return ((0xff - this[offset] + 1) * -1) + } + + Buffer.prototype.readInt16LE = function readInt16LE(offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 2, this.length) + var val = this[offset] | (this[offset + 1] << 8) + return (val & 0x8000) ? val | 0xFFFF0000 : val + } + + Buffer.prototype.readInt16BE = function readInt16BE(offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 2, this.length) + var val = this[offset + 1] | (this[offset] << 8) + return (val & 0x8000) ? val | 0xFFFF0000 : val + } + + Buffer.prototype.readInt32LE = function readInt32LE(offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 4, this.length) + + return (this[offset]) | + (this[offset + 1] << 8) | + (this[offset + 2] << 16) | + (this[offset + 3] << 24) + } + + Buffer.prototype.readInt32BE = function readInt32BE(offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 4, this.length) + + return (this[offset] << 24) | + (this[offset + 1] << 16) | + (this[offset + 2] << 8) | + (this[offset + 3]) + } + + Buffer.prototype.readFloatLE = function readFloatLE(offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 4, this.length) + return ieee754.read(this, offset, true, 23, 4) + } + + Buffer.prototype.readFloatBE = function readFloatBE(offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 4, this.length) + return ieee754.read(this, offset, false, 23, 4) + } + + Buffer.prototype.readDoubleLE = function readDoubleLE(offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 8, this.length) + return ieee754.read(this, offset, true, 52, 8) + } + + Buffer.prototype.readDoubleBE = function readDoubleBE(offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 8, this.length) + return ieee754.read(this, offset, false, 52, 8) + } + + function checkInt(buf, value, offset, ext, max, min) { + if (!Buffer.isBuffer(buf)) throw new TypeError('"buffer" argument must be a Buffer instance') + if (value > max || value < min) throw new RangeError('"value" argument is out of bounds') + if (offset + ext > buf.length) throw new RangeError('Index out of range') + } + + Buffer.prototype.writeUIntLE = function writeUIntLE(value, offset, byteLength, noAssert) { + value = +value + offset = offset >>> 0 + byteLength = byteLength >>> 0 + if (!noAssert) { + var maxBytes = Math.pow(2, 8 * byteLength) - 1 + checkInt(this, value, offset, byteLength, maxBytes, 0) + } + + var mul = 1 + var i = 0 + this[offset] = value & 0xFF + while (++i < byteLength && (mul *= 0x100)) { + this[offset + i] = (value / mul) & 0xFF + } + + return offset + byteLength + } + + Buffer.prototype.writeUIntBE = function writeUIntBE(value, offset, byteLength, noAssert) { + value = +value + offset = offset >>> 0 + byteLength = byteLength >>> 0 + if (!noAssert) { + var maxBytes = Math.pow(2, 8 * byteLength) - 1 + checkInt(this, value, offset, byteLength, maxBytes, 0) + } + + var i = byteLength - 1 + var mul = 1 + this[offset + i] = value & 0xFF + while (--i >= 0 && (mul *= 0x100)) { + this[offset + i] = (value / mul) & 0xFF + } + + return offset + byteLength + } + + Buffer.prototype.writeUInt8 = function writeUInt8(value, offset, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0) + this[offset] = (value & 0xff) + return offset + 1 + } + + Buffer.prototype.writeUInt16LE = function writeUInt16LE(value, offset, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0) + this[offset] = (value & 0xff) + this[offset + 1] = (value >>> 8) + return offset + 2 + } + + Buffer.prototype.writeUInt16BE = function writeUInt16BE(value, offset, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0) + this[offset] = (value >>> 8) + this[offset + 1] = (value & 0xff) + return offset + 2 + } + + Buffer.prototype.writeUInt32LE = function writeUInt32LE(value, offset, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0) + this[offset + 3] = (value >>> 24) + this[offset + 2] = (value >>> 16) + this[offset + 1] = (value >>> 8) + this[offset] = (value & 0xff) + return offset + 4 + } + + Buffer.prototype.writeUInt32BE = function writeUInt32BE(value, offset, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0) + this[offset] = (value >>> 24) + this[offset + 1] = (value >>> 16) + this[offset + 2] = (value >>> 8) + this[offset + 3] = (value & 0xff) + return offset + 4 + } + + Buffer.prototype.writeIntLE = function writeIntLE(value, offset, byteLength, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) { + var limit = Math.pow(2, (8 * byteLength) - 1) + + checkInt(this, value, offset, byteLength, limit - 1, -limit) + } + + var i = 0 + var mul = 1 + var sub = 0 + this[offset] = value & 0xFF + while (++i < byteLength && (mul *= 0x100)) { + if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) { + sub = 1 + } + this[offset + i] = ((value / mul) >> 0) - sub & 0xFF + } + + return offset + byteLength + } + + Buffer.prototype.writeIntBE = function writeIntBE(value, offset, byteLength, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) { + var limit = Math.pow(2, (8 * byteLength) - 1) + + checkInt(this, value, offset, byteLength, limit - 1, -limit) + } + + var i = byteLength - 1 + var mul = 1 + var sub = 0 + this[offset + i] = value & 0xFF + while (--i >= 0 && (mul *= 0x100)) { + if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) { + sub = 1 + } + this[offset + i] = ((value / mul) >> 0) - sub & 0xFF + } + + return offset + byteLength + } + + Buffer.prototype.writeInt8 = function writeInt8(value, offset, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -0x80) + if (value < 0) value = 0xff + value + 1 + this[offset] = (value & 0xff) + return offset + 1 + } + + Buffer.prototype.writeInt16LE = function writeInt16LE(value, offset, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000) + this[offset] = (value & 0xff) + this[offset + 1] = (value >>> 8) + return offset + 2 + } + + Buffer.prototype.writeInt16BE = function writeInt16BE(value, offset, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000) + this[offset] = (value >>> 8) + this[offset + 1] = (value & 0xff) + return offset + 2 + } + + Buffer.prototype.writeInt32LE = function writeInt32LE(value, offset, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000) + this[offset] = (value & 0xff) + this[offset + 1] = (value >>> 8) + this[offset + 2] = (value >>> 16) + this[offset + 3] = (value >>> 24) + return offset + 4 + } + + Buffer.prototype.writeInt32BE = function writeInt32BE(value, offset, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000) + if (value < 0) value = 0xffffffff + value + 1 + this[offset] = (value >>> 24) + this[offset + 1] = (value >>> 16) + this[offset + 2] = (value >>> 8) + this[offset + 3] = (value & 0xff) + return offset + 4 + } + + function checkIEEE754(buf, value, offset, ext, max, min) { + if (offset + ext > buf.length) throw new RangeError('Index out of range') + if (offset < 0) throw new RangeError('Index out of range') + } + + function writeFloat(buf, value, offset, littleEndian, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) { + checkIEEE754(buf, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38) + } + ieee754.write(buf, value, offset, littleEndian, 23, 4) + return offset + 4 + } + + Buffer.prototype.writeFloatLE = function writeFloatLE(value, offset, noAssert) { + return writeFloat(this, value, offset, true, noAssert) + } + + Buffer.prototype.writeFloatBE = function writeFloatBE(value, offset, noAssert) { + return writeFloat(this, value, offset, false, noAssert) + } + + function writeDouble(buf, value, offset, littleEndian, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) { + checkIEEE754(buf, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308) + } + ieee754.write(buf, value, offset, littleEndian, 52, 8) + return offset + 8 + } + + Buffer.prototype.writeDoubleLE = function writeDoubleLE(value, offset, noAssert) { + return writeDouble(this, value, offset, true, noAssert) + } + + Buffer.prototype.writeDoubleBE = function writeDoubleBE(value, offset, noAssert) { + return writeDouble(this, value, offset, false, noAssert) + } + + // copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length) + Buffer.prototype.copy = function copy(target, targetStart, start, end) { + if (!Buffer.isBuffer(target)) throw new TypeError('argument should be a Buffer') + if (!start) start = 0 + if (!end && end !== 0) end = this.length + if (targetStart >= target.length) targetStart = target.length + if (!targetStart) targetStart = 0 + if (end > 0 && end < start) end = start + + // Copy 0 bytes; we're done + if (end === start) return 0 + if (target.length === 0 || this.length === 0) return 0 + + // Fatal error conditions + if (targetStart < 0) { + throw new RangeError('targetStart out of bounds') + } + if (start < 0 || start >= this.length) throw new RangeError('Index out of range') + if (end < 0) throw new RangeError('sourceEnd out of bounds') + + // Are we oob? + if (end > this.length) end = this.length + if (target.length - targetStart < end - start) { + end = target.length - targetStart + start + } + + var len = end - start + + if (this === target && typeof Uint8Array.prototype.copyWithin === 'function') { + // Use built-in when available, missing from IE11 + this.copyWithin(targetStart, start, end) + } else if (this === target && start < targetStart && targetStart < end) { + // descending copy from end + for (var i = len - 1; i >= 0; --i) { + target[i + targetStart] = this[i + start] + } + } else { + Uint8Array.prototype.set.call( + target, + this.subarray(start, end), + targetStart + ) + } + + return len + } + + // Usage: + // buffer.fill(number[, offset[, end]]) + // buffer.fill(buffer[, offset[, end]]) + // buffer.fill(string[, offset[, end]][, encoding]) + Buffer.prototype.fill = function fill(val, start, end, encoding) { + // Handle string cases: + if (typeof val === 'string') { + if (typeof start === 'string') { + encoding = start + start = 0 + end = this.length + } else if (typeof end === 'string') { + encoding = end + end = this.length + } + if (encoding !== undefined && typeof encoding !== 'string') { + throw new TypeError('encoding must be a string') + } + if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) { + throw new TypeError('Unknown encoding: ' + encoding) + } + if (val.length === 1) { + var code = val.charCodeAt(0) + if ((encoding === 'utf8' && code < 128) || + encoding === 'latin1') { + // Fast path: If `val` fits into a single byte, use that numeric value. + val = code + } + } + } else if (typeof val === 'number') { + val = val & 255 + } + + // Invalid ranges are not set to a default, so can range check early. + if (start < 0 || this.length < start || this.length < end) { + throw new RangeError('Out of range index') + } + + if (end <= start) { + return this + } + + start = start >>> 0 + end = end === undefined ? this.length : end >>> 0 + + if (!val) val = 0 + + var i + if (typeof val === 'number') { + for (i = start; i < end; ++i) { + this[i] = val + } + } else { + var bytes = Buffer.isBuffer(val) ? + val : + Buffer.from(val, encoding) + var len = bytes.length + if (len === 0) { + throw new TypeError('The value "' + val + + '" is invalid for argument "value"') + } + for (i = 0; i < end - start; ++i) { + this[i + start] = bytes[i % len] + } + } + + return this + } + + // HELPER FUNCTIONS + // ================ + + var INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g + + function base64clean(str) { + // Node takes equal signs as end of the Base64 encoding + str = str.split('=')[0] + // Node strips out invalid characters like \n and \t from the string, base64-js does not + str = str.trim().replace(INVALID_BASE64_RE, '') + // Node converts strings with length < 2 to '' + if (str.length < 2) return '' + // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not + while (str.length % 4 !== 0) { + str = str + '=' + } + return str + } + + function toHex(n) { + if (n < 16) return '0' + n.toString(16) + return n.toString(16) + } + + function utf8ToBytes(string, units) { + units = units || Infinity + var codePoint + var length = string.length + var leadSurrogate = null + var bytes = [] + + for (var i = 0; i < length; ++i) { + codePoint = string.charCodeAt(i) + + // is surrogate component + if (codePoint > 0xD7FF && codePoint < 0xE000) { + // last char was a lead + if (!leadSurrogate) { + // no lead yet + if (codePoint > 0xDBFF) { + // unexpected trail + if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) + continue + } else if (i + 1 === length) { + // unpaired lead + if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) + continue + } + + // valid lead + leadSurrogate = codePoint + + continue + } + + // 2 leads in a row + if (codePoint < 0xDC00) { + if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) + leadSurrogate = codePoint + continue + } + + // valid surrogate pair + codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000 + } else if (leadSurrogate) { + // valid bmp char, but last char was a lead + if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) + } + + leadSurrogate = null + + // encode utf8 + if (codePoint < 0x80) { + if ((units -= 1) < 0) break + bytes.push(codePoint) + } else if (codePoint < 0x800) { + if ((units -= 2) < 0) break + bytes.push( + codePoint >> 0x6 | 0xC0, + codePoint & 0x3F | 0x80 + ) + } else if (codePoint < 0x10000) { + if ((units -= 3) < 0) break + bytes.push( + codePoint >> 0xC | 0xE0, + codePoint >> 0x6 & 0x3F | 0x80, + codePoint & 0x3F | 0x80 + ) + } else if (codePoint < 0x110000) { + if ((units -= 4) < 0) break + bytes.push( + codePoint >> 0x12 | 0xF0, + codePoint >> 0xC & 0x3F | 0x80, + codePoint >> 0x6 & 0x3F | 0x80, + codePoint & 0x3F | 0x80 + ) + } else { + throw new Error('Invalid code point') + } + } + + return bytes + } + + function asciiToBytes(str) { + var byteArray = [] + for (var i = 0; i < str.length; ++i) { + // Node's code seems to be doing this and not & 0x7F.. + byteArray.push(str.charCodeAt(i) & 0xFF) + } + return byteArray + } + + function utf16leToBytes(str, units) { + var c, hi, lo + var byteArray = [] + for (var i = 0; i < str.length; ++i) { + if ((units -= 2) < 0) break + + c = str.charCodeAt(i) + hi = c >> 8 + lo = c % 256 + byteArray.push(lo) + byteArray.push(hi) + } + + return byteArray + } + + function base64ToBytes(str) { + return base64.toByteArray(base64clean(str)) + } + + function blitBuffer(src, dst, offset, length) { + for (var i = 0; i < length; ++i) { + if ((i + offset >= dst.length) || (i >= src.length)) break + dst[i + offset] = src[i] + } + return i + } + + // ArrayBuffer or Uint8Array objects from other contexts (i.e. iframes) do not pass + // the `instanceof` check but they should be treated as of that type. + // See: https://github.com/feross/buffer/issues/166 + function isInstance(obj, type) { + return obj instanceof type || + (obj != null && obj.constructor != null && obj.constructor.name != null && + obj.constructor.name === type.name) + } + + function numberIsNaN(obj) { + // For IE11 support + return obj !== obj // eslint-disable-line no-self-compare + } + + }, { + "base64-js": 9, + "ieee754": 79 + }], + 13: [function(require, module, exports) { + (function(Buffer) { + // Copyright Joyent, Inc. and other Node contributors. + // + // Permission is hereby granted, free of charge, to any person obtaining a + // copy of this software and associated documentation files (the + // "Software"), to deal in the Software without restriction, including + // without limitation the rights to use, copy, modify, merge, publish, + // distribute, sublicense, and/or sell copies of the Software, and to permit + // persons to whom the Software is furnished to do so, subject to the + // following conditions: + // + // The above copyright notice and this permission notice shall be included + // in all copies or substantial portions of the Software. + // + // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN + // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE + // USE OR OTHER DEALINGS IN THE SOFTWARE. + + // NOTE: These type checking functions intentionally don't use `instanceof` + // because it is fragile and can be easily faked with `Object.create()`. + + function isArray(arg) { + if (Array.isArray) { + return Array.isArray(arg); + } + return objectToString(arg) === '[object Array]'; + } + exports.isArray = isArray; + + function isBoolean(arg) { + return typeof arg === 'boolean'; + } + exports.isBoolean = isBoolean; + + function isNull(arg) { + return arg === null; + } + exports.isNull = isNull; + + function isNullOrUndefined(arg) { + return arg == null; + } + exports.isNullOrUndefined = isNullOrUndefined; + + function isNumber(arg) { + return typeof arg === 'number'; + } + exports.isNumber = isNumber; + + function isString(arg) { + return typeof arg === 'string'; + } + exports.isString = isString; + + function isSymbol(arg) { + return typeof arg === 'symbol'; + } + exports.isSymbol = isSymbol; + + function isUndefined(arg) { + return arg === void 0; + } + exports.isUndefined = isUndefined; + + function isRegExp(re) { + return objectToString(re) === '[object RegExp]'; + } + exports.isRegExp = isRegExp; + + function isObject(arg) { + return typeof arg === 'object' && arg !== null; + } + exports.isObject = isObject; + + function isDate(d) { + return objectToString(d) === '[object Date]'; + } + exports.isDate = isDate; + + function isError(e) { + return (objectToString(e) === '[object Error]' || e instanceof Error); + } + exports.isError = isError; + + function isFunction(arg) { + return typeof arg === 'function'; + } + exports.isFunction = isFunction; + + function isPrimitive(arg) { + return arg === null || + typeof arg === 'boolean' || + typeof arg === 'number' || + typeof arg === 'string' || + typeof arg === 'symbol' || // ES6 symbol + typeof arg === 'undefined'; + } + exports.isPrimitive = isPrimitive; + + exports.isBuffer = Buffer.isBuffer; + + function objectToString(o) { + return Object.prototype.toString.call(o); + } + + }).call(this, { + "isBuffer": require("../../is-buffer/index.js") + }) + }, { + "../../is-buffer/index.js": 81 + }], + 14: [function(require, module, exports) { + 'use strict'; + + var copy = require('es5-ext/object/copy'), + normalizeOptions = require('es5-ext/object/normalize-options'), + ensureCallable = require('es5-ext/object/valid-callable'), + map = require('es5-ext/object/map'), + callable = require('es5-ext/object/valid-callable'), + validValue = require('es5-ext/object/valid-value') + + , + bind = Function.prototype.bind, + defineProperty = Object.defineProperty, + hasOwnProperty = Object.prototype.hasOwnProperty, + define; + + define = function(name, desc, options) { + var value = validValue(desc) && callable(desc.value), + dgs; + dgs = copy(desc); + delete dgs.writable; + delete dgs.value; + dgs.get = function() { + if (!options.overwriteDefinition && hasOwnProperty.call(this, name)) return value; + desc.value = bind.call(value, options.resolveContext ? options.resolveContext(this) : this); + defineProperty(this, name, desc); + return this[name]; + }; + return dgs; + }; + + module.exports = function(props /*, options*/ ) { + var options = normalizeOptions(arguments[1]); + if (options.resolveContext != null) ensureCallable(options.resolveContext); + return map(props, function(desc, name) { + return define(name, desc, options); + }); + }; + + }, { + "es5-ext/object/copy": 38, + "es5-ext/object/map": 47, + "es5-ext/object/normalize-options": 48, + "es5-ext/object/valid-callable": 53, + "es5-ext/object/valid-value": 54 + }], + 15: [function(require, module, exports) { + 'use strict'; + + var assign = require('es5-ext/object/assign'), + normalizeOpts = require('es5-ext/object/normalize-options'), + isCallable = require('es5-ext/object/is-callable'), + contains = require('es5-ext/string/#/contains') + + , + d; + + d = module.exports = function(dscr, value /*, options*/ ) { + var c, e, w, options, desc; + if ((arguments.length < 2) || (typeof dscr !== 'string')) { + options = value; + value = dscr; + dscr = null; + } else { + options = arguments[2]; + } + if (dscr == null) { + c = w = true; + e = false; + } else { + c = contains.call(dscr, 'c'); + e = contains.call(dscr, 'e'); + w = contains.call(dscr, 'w'); + } + + desc = { + value: value, + configurable: c, + enumerable: e, + writable: w + }; + return !options ? desc : assign(normalizeOpts(options), desc); + }; + + d.gs = function(dscr, get, set /*, options*/ ) { + var c, e, options, desc; + if (typeof dscr !== 'string') { + options = set; + set = get; + get = dscr; + dscr = null; + } else { + options = arguments[3]; + } + if (get == null) { + get = undefined; + } else if (!isCallable(get)) { + options = get; + get = set = undefined; + } else if (set == null) { + set = undefined; + } else if (!isCallable(set)) { + options = set; + set = undefined; + } + if (dscr == null) { + c = true; + e = false; + } else { + c = contains.call(dscr, 'c'); + e = contains.call(dscr, 'e'); + } + + desc = { + get: get, + set: set, + configurable: c, + enumerable: e + }; + return !options ? desc : assign(normalizeOpts(options), desc); + }; + + }, { + "es5-ext/object/assign": 35, + "es5-ext/object/is-callable": 41, + "es5-ext/object/normalize-options": 48, + "es5-ext/string/#/contains": 55 + }], + 16: [function(require, module, exports) { + (function(process, Buffer) { + var stream = require('readable-stream') + var eos = require('end-of-stream') + var inherits = require('inherits') + var shift = require('stream-shift') + + var SIGNAL_FLUSH = (Buffer.from && Buffer.from !== Uint8Array.from) ? + Buffer.from([0]) : + new Buffer([0]) + + var onuncork = function(self, fn) { + if (self._corked) self.once('uncork', fn) + else fn() + } + + var autoDestroy = function(self, err) { + if (self._autoDestroy) self.destroy(err) + } + + var destroyer = function(self, end) { + return function(err) { + if (err) autoDestroy(self, err.message === 'premature close' ? null : err) + else if (end && !self._ended) self.end() + } + } + + var end = function(ws, fn) { + if (!ws) return fn() + if (ws._writableState && ws._writableState.finished) return fn() + if (ws._writableState) return ws.end(fn) + ws.end() + fn() + } + + var toStreams2 = function(rs) { + return new(stream.Readable)({ + objectMode: true, + highWaterMark: 16 + }).wrap(rs) + } + + var Duplexify = function(writable, readable, opts) { + if (!(this instanceof Duplexify)) return new Duplexify(writable, readable, opts) + stream.Duplex.call(this, opts) + + this._writable = null + this._readable = null + this._readable2 = null + + this._autoDestroy = !opts || opts.autoDestroy !== false + this._forwardDestroy = !opts || opts.destroy !== false + this._forwardEnd = !opts || opts.end !== false + this._corked = 1 // start corked + this._ondrain = null + this._drained = false + this._forwarding = false + this._unwrite = null + this._unread = null + this._ended = false + + this.destroyed = false + + if (writable) this.setWritable(writable) + if (readable) this.setReadable(readable) + } + + inherits(Duplexify, stream.Duplex) + + Duplexify.obj = function(writable, readable, opts) { + if (!opts) opts = {} + opts.objectMode = true + opts.highWaterMark = 16 + return new Duplexify(writable, readable, opts) + } + + Duplexify.prototype.cork = function() { + if (++this._corked === 1) this.emit('cork') + } + + Duplexify.prototype.uncork = function() { + if (this._corked && --this._corked === 0) this.emit('uncork') + } + + Duplexify.prototype.setWritable = function(writable) { + if (this._unwrite) this._unwrite() + + if (this.destroyed) { + if (writable && writable.destroy) writable.destroy() + return + } + + if (writable === null || writable === false) { + this.end() + return + } + + var self = this + var unend = eos(writable, { + writable: true, + readable: false + }, destroyer(this, this._forwardEnd)) + + var ondrain = function() { + var ondrain = self._ondrain + self._ondrain = null + if (ondrain) ondrain() + } + + var clear = function() { + self._writable.removeListener('drain', ondrain) + unend() + } + + if (this._unwrite) process.nextTick(ondrain) // force a drain on stream reset to avoid livelocks + + this._writable = writable + this._writable.on('drain', ondrain) + this._unwrite = clear + + this.uncork() // always uncork setWritable + } + + Duplexify.prototype.setReadable = function(readable) { + if (this._unread) this._unread() + + if (this.destroyed) { + if (readable && readable.destroy) readable.destroy() + return + } + + if (readable === null || readable === false) { + this.push(null) + this.resume() + return + } + + var self = this + var unend = eos(readable, { + writable: false, + readable: true + }, destroyer(this)) + + var onreadable = function() { + self._forward() + } + + var onend = function() { + self.push(null) + } + + var clear = function() { + self._readable2.removeListener('readable', onreadable) + self._readable2.removeListener('end', onend) + unend() + } + + this._drained = true + this._readable = readable + this._readable2 = readable._readableState ? readable : toStreams2(readable) + this._readable2.on('readable', onreadable) + this._readable2.on('end', onend) + this._unread = clear + + this._forward() + } + + Duplexify.prototype._read = function() { + this._drained = true + this._forward() + } + + Duplexify.prototype._forward = function() { + if (this._forwarding || !this._readable2 || !this._drained) return + this._forwarding = true + + var data + + while (this._drained && (data = shift(this._readable2)) !== null) { + if (this.destroyed) continue + this._drained = this.push(data) + } + + this._forwarding = false + } + + Duplexify.prototype.destroy = function(err) { + if (this.destroyed) return + this.destroyed = true + + var self = this + process.nextTick(function() { + self._destroy(err) + }) + } + + Duplexify.prototype._destroy = function(err) { + if (err) { + var ondrain = this._ondrain + this._ondrain = null + if (ondrain) ondrain(err) + else this.emit('error', err) + } + + if (this._forwardDestroy) { + if (this._readable && this._readable.destroy) this._readable.destroy() + if (this._writable && this._writable.destroy) this._writable.destroy() + } + + this.emit('close') + } + + Duplexify.prototype._write = function(data, enc, cb) { + if (this.destroyed) return cb() + if (this._corked) return onuncork(this, this._write.bind(this, data, enc, cb)) + if (data === SIGNAL_FLUSH) return this._finish(cb) + if (!this._writable) return cb() + + if (this._writable.write(data) === false) this._ondrain = cb + else cb() + } + + + Duplexify.prototype._finish = function(cb) { + var self = this + this.emit('preend') + onuncork(this, function() { + end(self._forwardEnd && self._writable, function() { + // haxx to not emit prefinish twice + if (self._writableState.prefinished === false) self._writableState.prefinished = true + self.emit('prefinish') + onuncork(self, cb) + }) + }) + } + + Duplexify.prototype.end = function(data, enc, cb) { + if (typeof data === 'function') return this.end(null, null, data) + if (typeof enc === 'function') return this.end(data, null, enc) + this._ended = true + if (data) this.write(data) + if (!this._writableState.ending) this.write(SIGNAL_FLUSH) + return stream.Writable.prototype.end.call(this, cb) + } + + module.exports = Duplexify + + }).call(this, require('_process'), require("buffer").Buffer) + }, { + "_process": 92, + "buffer": 12, + "end-of-stream": 17, + "inherits": 80, + "readable-stream": 106, + "stream-shift": 109 + }], + 17: [function(require, module, exports) { + var once = require('once'); + + var noop = function() {}; + + var isRequest = function(stream) { + return stream.setHeader && typeof stream.abort === 'function'; + }; + + var isChildProcess = function(stream) { + return stream.stdio && Array.isArray(stream.stdio) && stream.stdio.length === 3 + }; + + var eos = function(stream, opts, callback) { + if (typeof opts === 'function') return eos(stream, null, opts); + if (!opts) opts = {}; + + callback = once(callback || noop); + + var ws = stream._writableState; + var rs = stream._readableState; + var readable = opts.readable || (opts.readable !== false && stream.readable); + var writable = opts.writable || (opts.writable !== false && stream.writable); + + var onlegacyfinish = function() { + if (!stream.writable) onfinish(); + }; + + var onfinish = function() { + writable = false; + if (!readable) callback.call(stream); + }; + + var onend = function() { + readable = false; + if (!writable) callback.call(stream); + }; + + var onexit = function(exitCode) { + callback.call(stream, exitCode ? new Error('exited with error code: ' + exitCode) : null); + }; + + var onerror = function(err) { + callback.call(stream, err); + }; + + var onclose = function() { + if (readable && !(rs && rs.ended)) return callback.call(stream, new Error('premature close')); + if (writable && !(ws && ws.ended)) return callback.call(stream, new Error('premature close')); + }; + + var onrequest = function() { + stream.req.on('finish', onfinish); + }; + + if (isRequest(stream)) { + stream.on('complete', onfinish); + stream.on('abort', onclose); + if (stream.req) onrequest(); + else stream.on('request', onrequest); + } else if (writable && !ws) { // legacy streams + stream.on('end', onlegacyfinish); + stream.on('close', onlegacyfinish); + } + + if (isChildProcess(stream)) stream.on('exit', onexit); + + stream.on('end', onend); + stream.on('finish', onfinish); + if (opts.error !== false) stream.on('error', onerror); + stream.on('close', onclose); + + return function() { + stream.removeListener('complete', onfinish); + stream.removeListener('abort', onclose); + stream.removeListener('request', onrequest); + if (stream.req) stream.req.removeListener('finish', onfinish); + stream.removeListener('end', onlegacyfinish); + stream.removeListener('close', onlegacyfinish); + stream.removeListener('finish', onfinish); + stream.removeListener('exit', onexit); + stream.removeListener('end', onend); + stream.removeListener('error', onerror); + stream.removeListener('close', onclose); + }; + }; + + module.exports = eos; + + }, { + "once": 90 + }], + 18: [function(require, module, exports) { + // Inspired by Google Closure: + // http://closure-library.googlecode.com/svn/docs/ + // closure_goog_array_array.js.html#goog.array.clear + + "use strict"; + + var value = require("../../object/valid-value"); + + module.exports = function() { + value(this).length = 0; + return this; + }; + + }, { + "../../object/valid-value": 54 + }], + 19: [function(require, module, exports) { + "use strict"; + + var numberIsNaN = require("../../number/is-nan"), + toPosInt = require("../../number/to-pos-integer"), + value = require("../../object/valid-value"), + indexOf = Array.prototype.indexOf, + objHasOwnProperty = Object.prototype.hasOwnProperty, + abs = Math.abs, + floor = Math.floor; + + module.exports = function(searchElement /*, fromIndex*/ ) { + var i, length, fromIndex, val; + if (!numberIsNaN(searchElement)) return indexOf.apply(this, arguments); + + length = toPosInt(value(this).length); + fromIndex = arguments[1]; + if (isNaN(fromIndex)) fromIndex = 0; + else if (fromIndex >= 0) fromIndex = floor(fromIndex); + else fromIndex = toPosInt(this.length) - floor(abs(fromIndex)); + + for (i = fromIndex; i < length; ++i) { + if (objHasOwnProperty.call(this, i)) { + val = this[i]; + if (numberIsNaN(val)) return i; // Jslint: ignore + } + } + return -1; + }; + + }, { + "../../number/is-nan": 29, + "../../number/to-pos-integer": 33, + "../../object/valid-value": 54 + }], + 20: [function(require, module, exports) { + "use strict"; + + module.exports = require("./is-implemented")() ? + Array.from : + require("./shim"); + + }, { + "./is-implemented": 21, + "./shim": 22 + }], + 21: [function(require, module, exports) { + "use strict"; + + module.exports = function() { + var from = Array.from, + arr, result; + if (typeof from !== "function") return false; + arr = ["raz", "dwa"]; + result = from(arr); + return Boolean(result && (result !== arr) && (result[1] === "dwa")); + }; + + }, {}], + 22: [function(require, module, exports) { + "use strict"; + + var iteratorSymbol = require("es6-symbol").iterator, + isArguments = require("../../function/is-arguments"), + isFunction = require("../../function/is-function"), + toPosInt = require("../../number/to-pos-integer"), + callable = require("../../object/valid-callable"), + validValue = require("../../object/valid-value"), + isValue = require("../../object/is-value"), + isString = require("../../string/is-string"), + isArray = Array.isArray, + call = Function.prototype.call, + desc = { + configurable: true, + enumerable: true, + writable: true, + value: null + }, + defineProperty = Object.defineProperty; + + // eslint-disable-next-line complexity + module.exports = function(arrayLike /*, mapFn, thisArg*/ ) { + var mapFn = arguments[1], + thisArg = arguments[2], + Context, i, j, arr, length, code, iterator, result, getIterator, value; + + arrayLike = Object(validValue(arrayLike)); + + if (isValue(mapFn)) callable(mapFn); + if (!this || this === Array || !isFunction(this)) { + // Result: Plain array + if (!mapFn) { + if (isArguments(arrayLike)) { + // Source: Arguments + length = arrayLike.length; + if (length !== 1) return Array.apply(null, arrayLike); + arr = new Array(1); + arr[0] = arrayLike[0]; + return arr; + } + if (isArray(arrayLike)) { + // Source: Array + arr = new Array(length = arrayLike.length); + for (i = 0; i < length; ++i) arr[i] = arrayLike[i]; + return arr; + } + } + arr = []; + } else { + // Result: Non plain array + Context = this; + } + + if (!isArray(arrayLike)) { + if ((getIterator = arrayLike[iteratorSymbol]) !== undefined) { + // Source: Iterator + iterator = callable(getIterator).call(arrayLike); + if (Context) arr = new Context(); + result = iterator.next(); + i = 0; + while (!result.done) { + value = mapFn ? call.call(mapFn, thisArg, result.value, i) : result.value; + if (Context) { + desc.value = value; + defineProperty(arr, i, desc); + } else { + arr[i] = value; + } + result = iterator.next(); + ++i; + } + length = i; + } else if (isString(arrayLike)) { + // Source: String + length = arrayLike.length; + if (Context) arr = new Context(); + for (i = 0, j = 0; i < length; ++i) { + value = arrayLike[i]; + if (i + 1 < length) { + code = value.charCodeAt(0); + // eslint-disable-next-line max-depth + if (code >= 0xd800 && code <= 0xdbff) value += arrayLike[++i]; + } + value = mapFn ? call.call(mapFn, thisArg, value, j) : value; + if (Context) { + desc.value = value; + defineProperty(arr, j, desc); + } else { + arr[j] = value; + } + ++j; + } + length = j; + } + } + if (length === undefined) { + // Source: array or array-like + length = toPosInt(arrayLike.length); + if (Context) arr = new Context(length); + for (i = 0; i < length; ++i) { + value = mapFn ? call.call(mapFn, thisArg, arrayLike[i], i) : arrayLike[i]; + if (Context) { + desc.value = value; + defineProperty(arr, i, desc); + } else { + arr[i] = value; + } + } + } + if (Context) { + desc.value = null; + arr.length = length; + } + return arr; + }; + + }, { + "../../function/is-arguments": 23, + "../../function/is-function": 24, + "../../number/to-pos-integer": 33, + "../../object/is-value": 43, + "../../object/valid-callable": 53, + "../../object/valid-value": 54, + "../../string/is-string": 58, + "es6-symbol": 72 + }], + 23: [function(require, module, exports) { + "use strict"; + + var objToString = Object.prototype.toString, + id = objToString.call( + (function() { + return arguments; + })() + ); + + module.exports = function(value) { + return objToString.call(value) === id; + }; + + }, {}], + 24: [function(require, module, exports) { + "use strict"; + + var objToString = Object.prototype.toString, + id = objToString.call(require("./noop")); + + module.exports = function(value) { + return typeof value === "function" && objToString.call(value) === id; + }; + + }, { + "./noop": 25 + }], + 25: [function(require, module, exports) { + "use strict"; + + // eslint-disable-next-line no-empty-function + module.exports = function() {}; + + }, {}], + 26: [function(require, module, exports) { + "use strict"; + + module.exports = require("./is-implemented")() ? + Math.sign : + require("./shim"); + + }, { + "./is-implemented": 27, + "./shim": 28 + }], + 27: [function(require, module, exports) { + "use strict"; + + module.exports = function() { + var sign = Math.sign; + if (typeof sign !== "function") return false; + return (sign(10) === 1) && (sign(-20) === -1); + }; + + }, {}], + 28: [function(require, module, exports) { + "use strict"; + + module.exports = function(value) { + value = Number(value); + if (isNaN(value) || (value === 0)) return value; + return value > 0 ? 1 : -1; + }; + + }, {}], + 29: [function(require, module, exports) { + "use strict"; + + module.exports = require("./is-implemented")() ? + Number.isNaN : + require("./shim"); + + }, { + "./is-implemented": 30, + "./shim": 31 + }], + 30: [function(require, module, exports) { + "use strict"; + + module.exports = function() { + var numberIsNaN = Number.isNaN; + if (typeof numberIsNaN !== "function") return false; + return !numberIsNaN({}) && numberIsNaN(NaN) && !numberIsNaN(34); + }; + + }, {}], + 31: [function(require, module, exports) { + "use strict"; + + module.exports = function(value) { + // eslint-disable-next-line no-self-compare + return value !== value; + }; + + }, {}], + 32: [function(require, module, exports) { + "use strict"; + + var sign = require("../math/sign") + + , + abs = Math.abs, + floor = Math.floor; + + module.exports = function(value) { + if (isNaN(value)) return 0; + value = Number(value); + if ((value === 0) || !isFinite(value)) return value; + return sign(value) * floor(abs(value)); + }; + + }, { + "../math/sign": 26 + }], + 33: [function(require, module, exports) { + "use strict"; + + var toInteger = require("./to-integer") + + , + max = Math.max; + + module.exports = function(value) { + return max(0, toInteger(value)); + }; + + }, { + "./to-integer": 32 + }], + 34: [function(require, module, exports) { + // Internal method, used by iteration functions. + // Calls a function for each key-value pair found in object + // Optionally takes compareFn to iterate object in specific order + + "use strict"; + + var callable = require("./valid-callable"), + value = require("./valid-value"), + bind = Function.prototype.bind, + call = Function.prototype.call, + keys = Object.keys, + objPropertyIsEnumerable = Object.prototype.propertyIsEnumerable; + + module.exports = function(method, defVal) { + return function(obj, cb /*, thisArg, compareFn*/ ) { + var list, thisArg = arguments[2], + compareFn = arguments[3]; + obj = Object(value(obj)); + callable(cb); + + list = keys(obj); + if (compareFn) { + list.sort(typeof compareFn === "function" ? bind.call(compareFn, obj) : undefined); + } + if (typeof method !== "function") method = list[method]; + return call.call(method, list, function(key, index) { + if (!objPropertyIsEnumerable.call(obj, key)) return defVal; + return call.call(cb, thisArg, obj[key], key, obj, index); + }); + }; + }; + + }, { + "./valid-callable": 53, + "./valid-value": 54 + }], + 35: [function(require, module, exports) { + "use strict"; + + module.exports = require("./is-implemented")() ? + Object.assign : + require("./shim"); + + }, { + "./is-implemented": 36, + "./shim": 37 + }], + 36: [function(require, module, exports) { + "use strict"; + + module.exports = function() { + var assign = Object.assign, + obj; + if (typeof assign !== "function") return false; + obj = { + foo: "raz" + }; + assign(obj, { + bar: "dwa" + }, { + trzy: "trzy" + }); + return (obj.foo + obj.bar + obj.trzy) === "razdwatrzy"; + }; + + }, {}], + 37: [function(require, module, exports) { + "use strict"; + + var keys = require("../keys"), + value = require("../valid-value"), + max = Math.max; + + module.exports = function(dest, src /*, …srcn*/ ) { + var error, i, length = max(arguments.length, 2), + assign; + dest = Object(value(dest)); + assign = function(key) { + try { + dest[key] = src[key]; + } catch (e) { + if (!error) error = e; + } + }; + for (i = 1; i < length; ++i) { + src = arguments[i]; + keys(src).forEach(assign); + } + if (error !== undefined) throw error; + return dest; + }; + + }, { + "../keys": 44, + "../valid-value": 54 + }], + 38: [function(require, module, exports) { + "use strict"; + + var aFrom = require("../array/from"), + assign = require("./assign"), + value = require("./valid-value"); + + module.exports = function(obj /*, propertyNames, options*/ ) { + var copy = Object(value(obj)), + propertyNames = arguments[1], + options = Object(arguments[2]); + if (copy !== obj && !propertyNames) return copy; + var result = {}; + if (propertyNames) { + aFrom(propertyNames, function(propertyName) { + if (options.ensure || propertyName in obj) result[propertyName] = obj[propertyName]; + }); + } else { + assign(result, obj); + } + return result; + }; + + }, { + "../array/from": 20, + "./assign": 35, + "./valid-value": 54 + }], + 39: [function(require, module, exports) { + // Workaround for http://code.google.com/p/v8/issues/detail?id=2804 + + "use strict"; + + var create = Object.create, + shim; + + if (!require("./set-prototype-of/is-implemented")()) { + shim = require("./set-prototype-of/shim"); + } + + module.exports = (function() { + var nullObject, polyProps, desc; + if (!shim) return create; + if (shim.level !== 1) return create; + + nullObject = {}; + polyProps = {}; + desc = { + configurable: false, + enumerable: false, + writable: true, + value: undefined + }; + Object.getOwnPropertyNames(Object.prototype).forEach(function(name) { + if (name === "__proto__") { + polyProps[name] = { + configurable: true, + enumerable: false, + writable: true, + value: undefined + }; + return; + } + polyProps[name] = desc; + }); + Object.defineProperties(nullObject, polyProps); + + Object.defineProperty(shim, "nullPolyfill", { + configurable: false, + enumerable: false, + writable: false, + value: nullObject + }); + + return function(prototype, props) { + return create(prototype === null ? nullObject : prototype, props); + }; + }()); + + }, { + "./set-prototype-of/is-implemented": 51, + "./set-prototype-of/shim": 52 + }], + 40: [function(require, module, exports) { + "use strict"; + + module.exports = require("./_iterate")("forEach"); + + }, { + "./_iterate": 34 + }], + 41: [function(require, module, exports) { + // Deprecated + + "use strict"; + + module.exports = function(obj) { + return typeof obj === "function"; + }; + + }, {}], + 42: [function(require, module, exports) { + "use strict"; + + var isValue = require("./is-value"); + + var map = { + function: true, + object: true + }; + + module.exports = function(value) { + return (isValue(value) && map[typeof value]) || false; + }; + + }, { + "./is-value": 43 + }], + 43: [function(require, module, exports) { + "use strict"; + + var _undefined = require("../function/noop")(); // Support ES3 engines + + module.exports = function(val) { + return (val !== _undefined) && (val !== null); + }; + + }, { + "../function/noop": 25 + }], + 44: [function(require, module, exports) { + "use strict"; + + module.exports = require("./is-implemented")() ? Object.keys : require("./shim"); + + }, { + "./is-implemented": 45, + "./shim": 46 + }], + 45: [function(require, module, exports) { + "use strict"; + + module.exports = function() { + try { + Object.keys("primitive"); + return true; + } catch (e) { + return false; + } + }; + + }, {}], + 46: [function(require, module, exports) { + "use strict"; + + var isValue = require("../is-value"); + + var keys = Object.keys; + + module.exports = function(object) { + return keys(isValue(object) ? Object(object) : object); + }; + + }, { + "../is-value": 43 + }], + 47: [function(require, module, exports) { + "use strict"; + + var callable = require("./valid-callable"), + forEach = require("./for-each"), + call = Function.prototype.call; + + module.exports = function(obj, cb /*, thisArg*/ ) { + var result = {}, + thisArg = arguments[2]; + callable(cb); + forEach(obj, function(value, key, targetObj, index) { + result[key] = call.call(cb, thisArg, value, key, targetObj, index); + }); + return result; + }; + + }, { + "./for-each": 40, + "./valid-callable": 53 + }], + 48: [function(require, module, exports) { + "use strict"; + + var isValue = require("./is-value"); + + var forEach = Array.prototype.forEach, + create = Object.create; + + var process = function(src, obj) { + var key; + for (key in src) obj[key] = src[key]; + }; + + // eslint-disable-next-line no-unused-vars + module.exports = function(opts1 /*, …options*/ ) { + var result = create(null); + forEach.call(arguments, function(options) { + if (!isValue(options)) return; + process(Object(options), result); + }); + return result; + }; + + }, { + "./is-value": 43 + }], + 49: [function(require, module, exports) { + "use strict"; + + var forEach = Array.prototype.forEach, + create = Object.create; + + // eslint-disable-next-line no-unused-vars + module.exports = function(arg /*, …args*/ ) { + var set = create(null); + forEach.call(arguments, function(name) { + set[name] = true; + }); + return set; + }; + + }, {}], + 50: [function(require, module, exports) { + "use strict"; + + module.exports = require("./is-implemented")() ? + Object.setPrototypeOf : + require("./shim"); + + }, { + "./is-implemented": 51, + "./shim": 52 + }], + 51: [function(require, module, exports) { + "use strict"; + + var create = Object.create, + getPrototypeOf = Object.getPrototypeOf, + plainObject = {}; + + module.exports = function( /* CustomCreate*/ ) { + var setPrototypeOf = Object.setPrototypeOf, + customCreate = arguments[0] || create; + if (typeof setPrototypeOf !== "function") return false; + return getPrototypeOf(setPrototypeOf(customCreate(null), plainObject)) === plainObject; + }; + + }, {}], + 52: [function(require, module, exports) { + /* eslint no-proto: "off" */ + + // Big thanks to @WebReflection for sorting this out + // https://gist.github.com/WebReflection/5593554 + + "use strict"; + + var isObject = require("../is-object"), + value = require("../valid-value"), + objIsPrototypeOf = Object.prototype.isPrototypeOf, + defineProperty = Object.defineProperty, + nullDesc = { + configurable: true, + enumerable: false, + writable: true, + value: undefined + }, + validate; + + validate = function(obj, prototype) { + value(obj); + if (prototype === null || isObject(prototype)) return obj; + throw new TypeError("Prototype must be null or an object"); + }; + + module.exports = (function(status) { + var fn, set; + if (!status) return null; + if (status.level === 2) { + if (status.set) { + set = status.set; + fn = function(obj, prototype) { + set.call(validate(obj, prototype), prototype); + return obj; + }; + } else { + fn = function(obj, prototype) { + validate(obj, prototype).__proto__ = prototype; + return obj; + }; + } + } else { + fn = function self(obj, prototype) { + var isNullBase; + validate(obj, prototype); + isNullBase = objIsPrototypeOf.call(self.nullPolyfill, obj); + if (isNullBase) delete self.nullPolyfill.__proto__; + if (prototype === null) prototype = self.nullPolyfill; + obj.__proto__ = prototype; + if (isNullBase) defineProperty(self.nullPolyfill, "__proto__", nullDesc); + return obj; + }; + } + return Object.defineProperty(fn, "level", { + configurable: false, + enumerable: false, + writable: false, + value: status.level + }); + }( + (function() { + var tmpObj1 = Object.create(null), + tmpObj2 = {}, + set, desc = Object.getOwnPropertyDescriptor(Object.prototype, "__proto__"); + + if (desc) { + try { + set = desc.set; // Opera crashes at this point + set.call(tmpObj1, tmpObj2); + } catch (ignore) {} + if (Object.getPrototypeOf(tmpObj1) === tmpObj2) return { + set: set, + level: 2 + }; + } + + tmpObj1.__proto__ = tmpObj2; + if (Object.getPrototypeOf(tmpObj1) === tmpObj2) return { + level: 2 + }; + + tmpObj1 = {}; + tmpObj1.__proto__ = tmpObj2; + if (Object.getPrototypeOf(tmpObj1) === tmpObj2) return { + level: 1 + }; + + return false; + })() + )); + + require("../create"); + + }, { + "../create": 39, + "../is-object": 42, + "../valid-value": 54 + }], + 53: [function(require, module, exports) { + "use strict"; + + module.exports = function(fn) { + if (typeof fn !== "function") throw new TypeError(fn + " is not a function"); + return fn; + }; + + }, {}], + 54: [function(require, module, exports) { + "use strict"; + + var isValue = require("./is-value"); + + module.exports = function(value) { + if (!isValue(value)) throw new TypeError("Cannot use null or undefined"); + return value; + }; + + }, { + "./is-value": 43 + }], + 55: [function(require, module, exports) { + "use strict"; + + module.exports = require("./is-implemented")() ? + String.prototype.contains : + require("./shim"); + + }, { + "./is-implemented": 56, + "./shim": 57 + }], + 56: [function(require, module, exports) { + "use strict"; + + var str = "razdwatrzy"; + + module.exports = function() { + if (typeof str.contains !== "function") return false; + return (str.contains("dwa") === true) && (str.contains("foo") === false); + }; + + }, {}], + 57: [function(require, module, exports) { + "use strict"; + + var indexOf = String.prototype.indexOf; + + module.exports = function(searchString /*, position*/ ) { + return indexOf.call(this, searchString, arguments[1]) > -1; + }; + + }, {}], + 58: [function(require, module, exports) { + "use strict"; + + var objToString = Object.prototype.toString, + id = objToString.call(""); + + module.exports = function(value) { + return ( + typeof value === "string" || + (value && + typeof value === "object" && + (value instanceof String || objToString.call(value) === id)) || + false + ); + }; + + }, {}], + 59: [function(require, module, exports) { + "use strict"; + + var setPrototypeOf = require("es5-ext/object/set-prototype-of"), + contains = require("es5-ext/string/#/contains"), + d = require("d"), + Symbol = require("es6-symbol"), + Iterator = require("./"); + + var defineProperty = Object.defineProperty, + ArrayIterator; + + ArrayIterator = module.exports = function(arr, kind) { + if (!(this instanceof ArrayIterator)) throw new TypeError("Constructor requires 'new'"); + Iterator.call(this, arr); + if (!kind) kind = "value"; + else if (contains.call(kind, "key+value")) kind = "key+value"; + else if (contains.call(kind, "key")) kind = "key"; + else kind = "value"; + defineProperty(this, "__kind__", d("", kind)); + }; + if (setPrototypeOf) setPrototypeOf(ArrayIterator, Iterator); + + // Internal %ArrayIteratorPrototype% doesn't expose its constructor + delete ArrayIterator.prototype.constructor; + + ArrayIterator.prototype = Object.create(Iterator.prototype, { + _resolve: d(function(i) { + if (this.__kind__ === "value") return this.__list__[i]; + if (this.__kind__ === "key+value") return [i, this.__list__[i]]; + return i; + }) + }); + defineProperty(ArrayIterator.prototype, Symbol.toStringTag, d("c", "Array Iterator")); + + }, { + "./": 62, + "d": 15, + "es5-ext/object/set-prototype-of": 50, + "es5-ext/string/#/contains": 55, + "es6-symbol": 72 + }], + 60: [function(require, module, exports) { + "use strict"; + + var isArguments = require("es5-ext/function/is-arguments"), + callable = require("es5-ext/object/valid-callable"), + isString = require("es5-ext/string/is-string"), + get = require("./get"); + + var isArray = Array.isArray, + call = Function.prototype.call, + some = Array.prototype.some; + + module.exports = function(iterable, cb /*, thisArg*/ ) { + var mode, thisArg = arguments[2], + result, doBreak, broken, i, length, char, code; + if (isArray(iterable) || isArguments(iterable)) mode = "array"; + else if (isString(iterable)) mode = "string"; + else iterable = get(iterable); + + callable(cb); + doBreak = function() { + broken = true; + }; + if (mode === "array") { + some.call(iterable, function(value) { + call.call(cb, thisArg, value, doBreak); + return broken; + }); + return; + } + if (mode === "string") { + length = iterable.length; + for (i = 0; i < length; ++i) { + char = iterable[i]; + if (i + 1 < length) { + code = char.charCodeAt(0); + if (code >= 0xd800 && code <= 0xdbff) char += iterable[++i]; + } + call.call(cb, thisArg, char, doBreak); + if (broken) break; + } + return; + } + result = iterable.next(); + + while (!result.done) { + call.call(cb, thisArg, result.value, doBreak); + if (broken) return; + result = iterable.next(); + } + }; + + }, { + "./get": 61, + "es5-ext/function/is-arguments": 23, + "es5-ext/object/valid-callable": 53, + "es5-ext/string/is-string": 58 + }], + 61: [function(require, module, exports) { + "use strict"; + + var isArguments = require("es5-ext/function/is-arguments"), + isString = require("es5-ext/string/is-string"), + ArrayIterator = require("./array"), + StringIterator = require("./string"), + iterable = require("./valid-iterable"), + iteratorSymbol = require("es6-symbol").iterator; + + module.exports = function(obj) { + if (typeof iterable(obj)[iteratorSymbol] === "function") return obj[iteratorSymbol](); + if (isArguments(obj)) return new ArrayIterator(obj); + if (isString(obj)) return new StringIterator(obj); + return new ArrayIterator(obj); + }; + + }, { + "./array": 59, + "./string": 64, + "./valid-iterable": 65, + "es5-ext/function/is-arguments": 23, + "es5-ext/string/is-string": 58, + "es6-symbol": 72 + }], + 62: [function(require, module, exports) { + "use strict"; + + var clear = require("es5-ext/array/#/clear"), + assign = require("es5-ext/object/assign"), + callable = require("es5-ext/object/valid-callable"), + value = require("es5-ext/object/valid-value"), + d = require("d"), + autoBind = require("d/auto-bind"), + Symbol = require("es6-symbol"); + + var defineProperty = Object.defineProperty, + defineProperties = Object.defineProperties, + Iterator; + + module.exports = Iterator = function(list, context) { + if (!(this instanceof Iterator)) throw new TypeError("Constructor requires 'new'"); + defineProperties(this, { + __list__: d("w", value(list)), + __context__: d("w", context), + __nextIndex__: d("w", 0) + }); + if (!context) return; + callable(context.on); + context.on("_add", this._onAdd); + context.on("_delete", this._onDelete); + context.on("_clear", this._onClear); + }; + + // Internal %IteratorPrototype% doesn't expose its constructor + delete Iterator.prototype.constructor; + + defineProperties( + Iterator.prototype, + assign({ + _next: d(function() { + var i; + if (!this.__list__) return undefined; + if (this.__redo__) { + i = this.__redo__.shift(); + if (i !== undefined) return i; + } + if (this.__nextIndex__ < this.__list__.length) return this.__nextIndex__++; + this._unBind(); + return undefined; + }), + next: d(function() { + return this._createResult(this._next()); + }), + _createResult: d(function(i) { + if (i === undefined) return { + done: true, + value: undefined + }; + return { + done: false, + value: this._resolve(i) + }; + }), + _resolve: d(function(i) { + return this.__list__[i]; + }), + _unBind: d(function() { + this.__list__ = null; + delete this.__redo__; + if (!this.__context__) return; + this.__context__.off("_add", this._onAdd); + this.__context__.off("_delete", this._onDelete); + this.__context__.off("_clear", this._onClear); + this.__context__ = null; + }), + toString: d(function() { + return "[object " + (this[Symbol.toStringTag] || "Object") + "]"; + }) + }, + autoBind({ + _onAdd: d(function(index) { + if (index >= this.__nextIndex__) return; + ++this.__nextIndex__; + if (!this.__redo__) { + defineProperty(this, "__redo__", d("c", [index])); + return; + } + this.__redo__.forEach(function(redo, i) { + if (redo >= index) this.__redo__[i] = ++redo; + }, this); + this.__redo__.push(index); + }), + _onDelete: d(function(index) { + var i; + if (index >= this.__nextIndex__) return; + --this.__nextIndex__; + if (!this.__redo__) return; + i = this.__redo__.indexOf(index); + if (i !== -1) this.__redo__.splice(i, 1); + this.__redo__.forEach(function(redo, j) { + if (redo > index) this.__redo__[j] = --redo; + }, this); + }), + _onClear: d(function() { + if (this.__redo__) clear.call(this.__redo__); + this.__nextIndex__ = 0; + }) + }) + ) + ); + + defineProperty( + Iterator.prototype, + Symbol.iterator, + d(function() { + return this; + }) + ); + + }, { + "d": 15, + "d/auto-bind": 14, + "es5-ext/array/#/clear": 18, + "es5-ext/object/assign": 35, + "es5-ext/object/valid-callable": 53, + "es5-ext/object/valid-value": 54, + "es6-symbol": 72 + }], + 63: [function(require, module, exports) { + "use strict"; + + var isArguments = require("es5-ext/function/is-arguments"), + isValue = require("es5-ext/object/is-value"), + isString = require("es5-ext/string/is-string"); + + var iteratorSymbol = require("es6-symbol").iterator, + isArray = Array.isArray; + + module.exports = function(value) { + if (!isValue(value)) return false; + if (isArray(value)) return true; + if (isString(value)) return true; + if (isArguments(value)) return true; + return typeof value[iteratorSymbol] === "function"; + }; + + }, { + "es5-ext/function/is-arguments": 23, + "es5-ext/object/is-value": 43, + "es5-ext/string/is-string": 58, + "es6-symbol": 72 + }], + 64: [function(require, module, exports) { + // Thanks @mathiasbynens + // http://mathiasbynens.be/notes/javascript-unicode#iterating-over-symbols + + "use strict"; + + var setPrototypeOf = require("es5-ext/object/set-prototype-of"), + d = require("d"), + Symbol = require("es6-symbol"), + Iterator = require("./"); + + var defineProperty = Object.defineProperty, + StringIterator; + + StringIterator = module.exports = function(str) { + if (!(this instanceof StringIterator)) throw new TypeError("Constructor requires 'new'"); + str = String(str); + Iterator.call(this, str); + defineProperty(this, "__length__", d("", str.length)); + }; + if (setPrototypeOf) setPrototypeOf(StringIterator, Iterator); + + // Internal %ArrayIteratorPrototype% doesn't expose its constructor + delete StringIterator.prototype.constructor; + + StringIterator.prototype = Object.create(Iterator.prototype, { + _next: d(function() { + if (!this.__list__) return undefined; + if (this.__nextIndex__ < this.__length__) return this.__nextIndex__++; + this._unBind(); + return undefined; + }), + _resolve: d(function(i) { + var char = this.__list__[i], + code; + if (this.__nextIndex__ === this.__length__) return char; + code = char.charCodeAt(0); + if (code >= 0xd800 && code <= 0xdbff) return char + this.__list__[this.__nextIndex__++]; + return char; + }) + }); + defineProperty(StringIterator.prototype, Symbol.toStringTag, d("c", "String Iterator")); + + }, { + "./": 62, + "d": 15, + "es5-ext/object/set-prototype-of": 50, + "es6-symbol": 72 + }], + 65: [function(require, module, exports) { + "use strict"; + + var isIterable = require("./is-iterable"); + + module.exports = function(value) { + if (!isIterable(value)) throw new TypeError(value + " is not iterable"); + return value; + }; + + }, { + "./is-iterable": 63 + }], + 66: [function(require, module, exports) { + 'use strict'; + + module.exports = require('./is-implemented')() ? Map : require('./polyfill'); + + }, { + "./is-implemented": 67, + "./polyfill": 71 + }], + 67: [function(require, module, exports) { + 'use strict'; + + module.exports = function() { + var map, iterator, result; + if (typeof Map !== 'function') return false; + try { + // WebKit doesn't support arguments and crashes + map = new Map([ + ['raz', 'one'], + ['dwa', 'two'], + ['trzy', 'three'] + ]); + } catch (e) { + return false; + } + if (String(map) !== '[object Map]') return false; + if (map.size !== 3) return false; + if (typeof map.clear !== 'function') return false; + if (typeof map.delete !== 'function') return false; + if (typeof map.entries !== 'function') return false; + if (typeof map.forEach !== 'function') return false; + if (typeof map.get !== 'function') return false; + if (typeof map.has !== 'function') return false; + if (typeof map.keys !== 'function') return false; + if (typeof map.set !== 'function') return false; + if (typeof map.values !== 'function') return false; + + iterator = map.entries(); + result = iterator.next(); + if (result.done !== false) return false; + if (!result.value) return false; + if (result.value[0] !== 'raz') return false; + if (result.value[1] !== 'one') return false; + + return true; + }; + + }, {}], + 68: [function(require, module, exports) { + // Exports true if environment provides native `Map` implementation, + // whatever that is. + + 'use strict'; + + module.exports = (function() { + if (typeof Map === 'undefined') return false; + return (Object.prototype.toString.call(new Map()) === '[object Map]'); + }()); + + }, {}], + 69: [function(require, module, exports) { + 'use strict'; + + module.exports = require('es5-ext/object/primitive-set')('key', + 'value', 'key+value'); + + }, { + "es5-ext/object/primitive-set": 49 + }], + 70: [function(require, module, exports) { + 'use strict'; + + var setPrototypeOf = require('es5-ext/object/set-prototype-of'), + d = require('d'), + Iterator = require('es6-iterator'), + toStringTagSymbol = require('es6-symbol').toStringTag, + kinds = require('./iterator-kinds') + + , + defineProperties = Object.defineProperties, + unBind = Iterator.prototype._unBind, + MapIterator; + + MapIterator = module.exports = function(map, kind) { + if (!(this instanceof MapIterator)) return new MapIterator(map, kind); + Iterator.call(this, map.__mapKeysData__, map); + if (!kind || !kinds[kind]) kind = 'key+value'; + defineProperties(this, { + __kind__: d('', kind), + __values__: d('w', map.__mapValuesData__) + }); + }; + if (setPrototypeOf) setPrototypeOf(MapIterator, Iterator); + + MapIterator.prototype = Object.create(Iterator.prototype, { + constructor: d(MapIterator), + _resolve: d(function(i) { + if (this.__kind__ === 'value') return this.__values__[i]; + if (this.__kind__ === 'key') return this.__list__[i]; + return [this.__list__[i], this.__values__[i]]; + }), + _unBind: d(function() { + this.__values__ = null; + unBind.call(this); + }), + toString: d(function() { + return '[object Map Iterator]'; + }) + }); + Object.defineProperty(MapIterator.prototype, toStringTagSymbol, + d('c', 'Map Iterator')); + + }, { + "./iterator-kinds": 69, + "d": 15, + "es5-ext/object/set-prototype-of": 50, + "es6-iterator": 62, + "es6-symbol": 72 + }], + 71: [function(require, module, exports) { + 'use strict'; + + var clear = require('es5-ext/array/#/clear'), + eIndexOf = require('es5-ext/array/#/e-index-of'), + setPrototypeOf = require('es5-ext/object/set-prototype-of'), + callable = require('es5-ext/object/valid-callable'), + validValue = require('es5-ext/object/valid-value'), + d = require('d'), + ee = require('event-emitter'), + Symbol = require('es6-symbol'), + iterator = require('es6-iterator/valid-iterable'), + forOf = require('es6-iterator/for-of'), + Iterator = require('./lib/iterator'), + isNative = require('./is-native-implemented') + + , + call = Function.prototype.call, + defineProperties = Object.defineProperties, + getPrototypeOf = Object.getPrototypeOf, + MapPoly; + + module.exports = MapPoly = function( /*iterable*/ ) { + var iterable = arguments[0], + keys, values, self; + if (!(this instanceof MapPoly)) throw new TypeError('Constructor requires \'new\''); + if (isNative && setPrototypeOf && (Map !== MapPoly)) { + self = setPrototypeOf(new Map(), getPrototypeOf(this)); + } else { + self = this; + } + if (iterable != null) iterator(iterable); + defineProperties(self, { + __mapKeysData__: d('c', keys = []), + __mapValuesData__: d('c', values = []) + }); + if (!iterable) return self; + forOf(iterable, function(value) { + var key = validValue(value)[0]; + value = value[1]; + if (eIndexOf.call(keys, key) !== -1) return; + keys.push(key); + values.push(value); + }, self); + return self; + }; + + if (isNative) { + if (setPrototypeOf) setPrototypeOf(MapPoly, Map); + MapPoly.prototype = Object.create(Map.prototype, { + constructor: d(MapPoly) + }); + } + + ee(defineProperties(MapPoly.prototype, { + clear: d(function() { + if (!this.__mapKeysData__.length) return; + clear.call(this.__mapKeysData__); + clear.call(this.__mapValuesData__); + this.emit('_clear'); + }), + delete: d(function(key) { + var index = eIndexOf.call(this.__mapKeysData__, key); + if (index === -1) return false; + this.__mapKeysData__.splice(index, 1); + this.__mapValuesData__.splice(index, 1); + this.emit('_delete', index, key); + return true; + }), + entries: d(function() { + return new Iterator(this, 'key+value'); + }), + forEach: d(function(cb /*, thisArg*/ ) { + var thisArg = arguments[1], + iterator, result; + callable(cb); + iterator = this.entries(); + result = iterator._next(); + while (result !== undefined) { + call.call(cb, thisArg, this.__mapValuesData__[result], + this.__mapKeysData__[result], this); + result = iterator._next(); + } + }), + get: d(function(key) { + var index = eIndexOf.call(this.__mapKeysData__, key); + if (index === -1) return; + return this.__mapValuesData__[index]; + }), + has: d(function(key) { + return (eIndexOf.call(this.__mapKeysData__, key) !== -1); + }), + keys: d(function() { + return new Iterator(this, 'key'); + }), + set: d(function(key, value) { + var index = eIndexOf.call(this.__mapKeysData__, key), + emit; + if (index === -1) { + index = this.__mapKeysData__.push(key) - 1; + emit = true; + } + this.__mapValuesData__[index] = value; + if (emit) this.emit('_add', index, key); + return this; + }), + size: d.gs(function() { + return this.__mapKeysData__.length; + }), + values: d(function() { + return new Iterator(this, 'value'); + }), + toString: d(function() { + return '[object Map]'; + }) + })); + Object.defineProperty(MapPoly.prototype, Symbol.iterator, d(function() { + return this.entries(); + })); + Object.defineProperty(MapPoly.prototype, Symbol.toStringTag, d('c', 'Map')); + + }, { + "./is-native-implemented": 68, + "./lib/iterator": 70, + "d": 15, + "es5-ext/array/#/clear": 18, + "es5-ext/array/#/e-index-of": 19, + "es5-ext/object/set-prototype-of": 50, + "es5-ext/object/valid-callable": 53, + "es5-ext/object/valid-value": 54, + "es6-iterator/for-of": 60, + "es6-iterator/valid-iterable": 65, + "es6-symbol": 72, + "event-emitter": 77 + }], + 72: [function(require, module, exports) { + 'use strict'; + + module.exports = require('./is-implemented')() ? Symbol : require('./polyfill'); + + }, { + "./is-implemented": 73, + "./polyfill": 75 + }], + 73: [function(require, module, exports) { + 'use strict'; + + var validTypes = { + object: true, + symbol: true + }; + + module.exports = function() { + var symbol; + if (typeof Symbol !== 'function') return false; + symbol = Symbol('test symbol'); + try { + String(symbol); + } catch (e) { + return false; + } + + // Return 'true' also for polyfills + if (!validTypes[typeof Symbol.iterator]) return false; + if (!validTypes[typeof Symbol.toPrimitive]) return false; + if (!validTypes[typeof Symbol.toStringTag]) return false; + + return true; + }; + + }, {}], + 74: [function(require, module, exports) { + 'use strict'; + + module.exports = function(x) { + if (!x) return false; + if (typeof x === 'symbol') return true; + if (!x.constructor) return false; + if (x.constructor.name !== 'Symbol') return false; + return (x[x.constructor.toStringTag] === 'Symbol'); + }; + + }, {}], + 75: [function(require, module, exports) { + // ES2015 Symbol polyfill for environments that do not (or partially) support it + + 'use strict'; + + var d = require('d'), + validateSymbol = require('./validate-symbol') + + , + create = Object.create, + defineProperties = Object.defineProperties, + defineProperty = Object.defineProperty, + objPrototype = Object.prototype, + NativeSymbol, SymbolPolyfill, HiddenSymbol, globalSymbols = create(null), + isNativeSafe; + + if (typeof Symbol === 'function') { + NativeSymbol = Symbol; + try { + String(NativeSymbol()); + isNativeSafe = true; + } catch (ignore) {} + } + + var generateName = (function() { + var created = create(null); + return function(desc) { + var postfix = 0, + name, ie11BugWorkaround; + while (created[desc + (postfix || '')]) ++postfix; + desc += (postfix || ''); + created[desc] = true; + name = '@@' + desc; + defineProperty(objPrototype, name, d.gs(null, function(value) { + // For IE11 issue see: + // https://connect.microsoft.com/IE/feedbackdetail/view/1928508/ + // ie11-broken-getters-on-dom-objects + // https://github.com/medikoo/es6-symbol/issues/12 + if (ie11BugWorkaround) return; + ie11BugWorkaround = true; + defineProperty(this, name, d(value)); + ie11BugWorkaround = false; + })); + return name; + }; + }()); + + // Internal constructor (not one exposed) for creating Symbol instances. + // This one is used to ensure that `someSymbol instanceof Symbol` always return false + HiddenSymbol = function Symbol(description) { + if (this instanceof HiddenSymbol) throw new TypeError('Symbol is not a constructor'); + return SymbolPolyfill(description); + }; + + // Exposed `Symbol` constructor + // (returns instances of HiddenSymbol) + module.exports = SymbolPolyfill = function Symbol(description) { + var symbol; + if (this instanceof Symbol) throw new TypeError('Symbol is not a constructor'); + if (isNativeSafe) return NativeSymbol(description); + symbol = create(HiddenSymbol.prototype); + description = (description === undefined ? '' : String(description)); + return defineProperties(symbol, { + __description__: d('', description), + __name__: d('', generateName(description)) + }); + }; + defineProperties(SymbolPolyfill, { + for: d(function(key) { + if (globalSymbols[key]) return globalSymbols[key]; + return (globalSymbols[key] = SymbolPolyfill(String(key))); + }), + keyFor: d(function(s) { + var key; + validateSymbol(s); + for (key in globalSymbols) + if (globalSymbols[key] === s) return key; + }), + + // To ensure proper interoperability with other native functions (e.g. Array.from) + // fallback to eventual native implementation of given symbol + hasInstance: d('', (NativeSymbol && NativeSymbol.hasInstance) || SymbolPolyfill('hasInstance')), + isConcatSpreadable: d('', (NativeSymbol && NativeSymbol.isConcatSpreadable) || + SymbolPolyfill('isConcatSpreadable')), + iterator: d('', (NativeSymbol && NativeSymbol.iterator) || SymbolPolyfill('iterator')), + match: d('', (NativeSymbol && NativeSymbol.match) || SymbolPolyfill('match')), + replace: d('', (NativeSymbol && NativeSymbol.replace) || SymbolPolyfill('replace')), + search: d('', (NativeSymbol && NativeSymbol.search) || SymbolPolyfill('search')), + species: d('', (NativeSymbol && NativeSymbol.species) || SymbolPolyfill('species')), + split: d('', (NativeSymbol && NativeSymbol.split) || SymbolPolyfill('split')), + toPrimitive: d('', (NativeSymbol && NativeSymbol.toPrimitive) || SymbolPolyfill('toPrimitive')), + toStringTag: d('', (NativeSymbol && NativeSymbol.toStringTag) || SymbolPolyfill('toStringTag')), + unscopables: d('', (NativeSymbol && NativeSymbol.unscopables) || SymbolPolyfill('unscopables')) + }); + + // Internal tweaks for real symbol producer + defineProperties(HiddenSymbol.prototype, { + constructor: d(SymbolPolyfill), + toString: d('', function() { + return this.__name__; + }) + }); + + // Proper implementation of methods exposed on Symbol.prototype + // They won't be accessible on produced symbol instances as they derive from HiddenSymbol.prototype + defineProperties(SymbolPolyfill.prototype, { + toString: d(function() { + return 'Symbol (' + validateSymbol(this).__description__ + ')'; + }), + valueOf: d(function() { + return validateSymbol(this); + }) + }); + defineProperty(SymbolPolyfill.prototype, SymbolPolyfill.toPrimitive, d('', function() { + var symbol = validateSymbol(this); + if (typeof symbol === 'symbol') return symbol; + return symbol.toString(); + })); + defineProperty(SymbolPolyfill.prototype, SymbolPolyfill.toStringTag, d('c', 'Symbol')); + + // Proper implementaton of toPrimitive and toStringTag for returned symbol instances + defineProperty(HiddenSymbol.prototype, SymbolPolyfill.toStringTag, + d('c', SymbolPolyfill.prototype[SymbolPolyfill.toStringTag])); + + // Note: It's important to define `toPrimitive` as last one, as some implementations + // implement `toPrimitive` natively without implementing `toStringTag` (or other specified symbols) + // And that may invoke error in definition flow: + // See: https://github.com/medikoo/es6-symbol/issues/13#issuecomment-164146149 + defineProperty(HiddenSymbol.prototype, SymbolPolyfill.toPrimitive, + d('c', SymbolPolyfill.prototype[SymbolPolyfill.toPrimitive])); + + }, { + "./validate-symbol": 76, + "d": 15 + }], + 76: [function(require, module, exports) { + 'use strict'; + + var isSymbol = require('./is-symbol'); + + module.exports = function(value) { + if (!isSymbol(value)) throw new TypeError(value + " is not a symbol"); + return value; + }; + + }, { + "./is-symbol": 74 + }], + 77: [function(require, module, exports) { + 'use strict'; + + var d = require('d'), + callable = require('es5-ext/object/valid-callable') + + , + apply = Function.prototype.apply, + call = Function.prototype.call, + create = Object.create, + defineProperty = Object.defineProperty, + defineProperties = Object.defineProperties, + hasOwnProperty = Object.prototype.hasOwnProperty, + descriptor = { + configurable: true, + enumerable: false, + writable: true + } + + , + on, once, off, emit, methods, descriptors, base; + + on = function(type, listener) { + var data; + + callable(listener); + + if (!hasOwnProperty.call(this, '__ee__')) { + data = descriptor.value = create(null); + defineProperty(this, '__ee__', descriptor); + descriptor.value = null; + } else { + data = this.__ee__; + } + if (!data[type]) data[type] = listener; + else if (typeof data[type] === 'object') data[type].push(listener); + else data[type] = [data[type], listener]; + + return this; + }; + + once = function(type, listener) { + var once, self; + + callable(listener); + self = this; + on.call(this, type, once = function() { + off.call(self, type, once); + apply.call(listener, this, arguments); + }); + + once.__eeOnceListener__ = listener; + return this; + }; + + off = function(type, listener) { + var data, listeners, candidate, i; + + callable(listener); + + if (!hasOwnProperty.call(this, '__ee__')) return this; + data = this.__ee__; + if (!data[type]) return this; + listeners = data[type]; + + if (typeof listeners === 'object') { + for (i = 0; + (candidate = listeners[i]); ++i) { + if ((candidate === listener) || + (candidate.__eeOnceListener__ === listener)) { + if (listeners.length === 2) data[type] = listeners[i ? 0 : 1]; + else listeners.splice(i, 1); + } + } + } else { + if ((listeners === listener) || + (listeners.__eeOnceListener__ === listener)) { + delete data[type]; + } + } + + return this; + }; + + emit = function(type) { + var i, l, listener, listeners, args; + + if (!hasOwnProperty.call(this, '__ee__')) return; + listeners = this.__ee__[type]; + if (!listeners) return; + + if (typeof listeners === 'object') { + l = arguments.length; + args = new Array(l - 1); + for (i = 1; i < l; ++i) args[i - 1] = arguments[i]; + + listeners = listeners.slice(); + for (i = 0; + (listener = listeners[i]); ++i) { + apply.call(listener, this, args); + } + } else { + switch (arguments.length) { + case 1: + call.call(listeners, this); + break; + case 2: + call.call(listeners, this, arguments[1]); + break; + case 3: + call.call(listeners, this, arguments[1], arguments[2]); + break; + default: + l = arguments.length; + args = new Array(l - 1); + for (i = 1; i < l; ++i) { + args[i - 1] = arguments[i]; + } + apply.call(listeners, this, args); + } + } + }; + + methods = { + on: on, + once: once, + off: off, + emit: emit + }; + + descriptors = { + on: d(on), + once: d(once), + off: d(off), + emit: d(emit) + }; + + base = defineProperties({}, descriptors); + + module.exports = exports = function(o) { + return (o == null) ? create(base) : defineProperties(Object(o), descriptors); + }; + exports.methods = methods; + + }, { + "d": 15, + "es5-ext/object/valid-callable": 53 + }], + 78: [function(require, module, exports) { + // Copyright Joyent, Inc. and other Node contributors. + // + // Permission is hereby granted, free of charge, to any person obtaining a + // copy of this software and associated documentation files (the + // "Software"), to deal in the Software without restriction, including + // without limitation the rights to use, copy, modify, merge, publish, + // distribute, sublicense, and/or sell copies of the Software, and to permit + // persons to whom the Software is furnished to do so, subject to the + // following conditions: + // + // The above copyright notice and this permission notice shall be included + // in all copies or substantial portions of the Software. + // + // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN + // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE + // USE OR OTHER DEALINGS IN THE SOFTWARE. + + var objectCreate = Object.create || objectCreatePolyfill + var objectKeys = Object.keys || objectKeysPolyfill + var bind = Function.prototype.bind || functionBindPolyfill + + function EventEmitter() { + if (!this._events || !Object.prototype.hasOwnProperty.call(this, '_events')) { + this._events = objectCreate(null); + this._eventsCount = 0; + } + + this._maxListeners = this._maxListeners || undefined; + } + module.exports = EventEmitter; + + // Backwards-compat with node 0.10.x + EventEmitter.EventEmitter = EventEmitter; + + EventEmitter.prototype._events = undefined; + EventEmitter.prototype._maxListeners = undefined; + + // By default EventEmitters will print a warning if more than 10 listeners are + // added to it. This is a useful default which helps finding memory leaks. + var defaultMaxListeners = 10; + + var hasDefineProperty; + try { + var o = {}; + if (Object.defineProperty) Object.defineProperty(o, 'x', { + value: 0 + }); + hasDefineProperty = o.x === 0; + } catch (err) { + hasDefineProperty = false + } + if (hasDefineProperty) { + Object.defineProperty(EventEmitter, 'defaultMaxListeners', { + enumerable: true, + get: function() { + return defaultMaxListeners; + }, + set: function(arg) { + // check whether the input is a positive number (whose value is zero or + // greater and not a NaN). + if (typeof arg !== 'number' || arg < 0 || arg !== arg) + throw new TypeError('"defaultMaxListeners" must be a positive number'); + defaultMaxListeners = arg; + } + }); + } else { + EventEmitter.defaultMaxListeners = defaultMaxListeners; + } + + // Obviously not all Emitters should be limited to 10. This function allows + // that to be increased. Set to zero for unlimited. + EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) { + if (typeof n !== 'number' || n < 0 || isNaN(n)) + throw new TypeError('"n" argument must be a positive number'); + this._maxListeners = n; + return this; + }; + + function $getMaxListeners(that) { + if (that._maxListeners === undefined) + return EventEmitter.defaultMaxListeners; + return that._maxListeners; + } + + EventEmitter.prototype.getMaxListeners = function getMaxListeners() { + return $getMaxListeners(this); + }; + + // These standalone emit* functions are used to optimize calling of event + // handlers for fast cases because emit() itself often has a variable number of + // arguments and can be deoptimized because of that. These functions always have + // the same number of arguments and thus do not get deoptimized, so the code + // inside them can execute faster. + function emitNone(handler, isFn, self) { + if (isFn) + handler.call(self); + else { + var len = handler.length; + var listeners = arrayClone(handler, len); + for (var i = 0; i < len; ++i) + listeners[i].call(self); + } + } + + function emitOne(handler, isFn, self, arg1) { + if (isFn) + handler.call(self, arg1); + else { + var len = handler.length; + var listeners = arrayClone(handler, len); + for (var i = 0; i < len; ++i) + listeners[i].call(self, arg1); + } + } + + function emitTwo(handler, isFn, self, arg1, arg2) { + if (isFn) + handler.call(self, arg1, arg2); + else { + var len = handler.length; + var listeners = arrayClone(handler, len); + for (var i = 0; i < len; ++i) + listeners[i].call(self, arg1, arg2); + } + } + + function emitThree(handler, isFn, self, arg1, arg2, arg3) { + if (isFn) + handler.call(self, arg1, arg2, arg3); + else { + var len = handler.length; + var listeners = arrayClone(handler, len); + for (var i = 0; i < len; ++i) + listeners[i].call(self, arg1, arg2, arg3); + } + } + + function emitMany(handler, isFn, self, args) { + if (isFn) + handler.apply(self, args); + else { + var len = handler.length; + var listeners = arrayClone(handler, len); + for (var i = 0; i < len; ++i) + listeners[i].apply(self, args); + } + } + + EventEmitter.prototype.emit = function emit(type) { + var er, handler, len, args, i, events; + var doError = (type === 'error'); + + events = this._events; + if (events) + doError = (doError && events.error == null); + else if (!doError) + return false; + + // If there is no 'error' event listener then throw. + if (doError) { + if (arguments.length > 1) + er = arguments[1]; + if (er instanceof Error) { + throw er; // Unhandled 'error' event + } else { + // At least give some kind of context to the user + var err = new Error('Unhandled "error" event. (' + er + ')'); + err.context = er; + throw err; + } + return false; + } + + handler = events[type]; + + if (!handler) + return false; + + var isFn = typeof handler === 'function'; + len = arguments.length; + switch (len) { + // fast cases + case 1: + emitNone(handler, isFn, this); + break; + case 2: + emitOne(handler, isFn, this, arguments[1]); + break; + case 3: + emitTwo(handler, isFn, this, arguments[1], arguments[2]); + break; + case 4: + emitThree(handler, isFn, this, arguments[1], arguments[2], arguments[3]); + break; + // slower + default: + args = new Array(len - 1); + for (i = 1; i < len; i++) + args[i - 1] = arguments[i]; + emitMany(handler, isFn, this, args); + } + + return true; + }; + + function _addListener(target, type, listener, prepend) { + var m; + var events; + var existing; + + if (typeof listener !== 'function') + throw new TypeError('"listener" argument must be a function'); + + events = target._events; + if (!events) { + events = target._events = objectCreate(null); + target._eventsCount = 0; + } else { + // To avoid recursion in the case that type === "newListener"! Before + // adding it to the listeners, first emit "newListener". + if (events.newListener) { + target.emit('newListener', type, + listener.listener ? listener.listener : listener); + + // Re-assign `events` because a newListener handler could have caused the + // this._events to be assigned to a new object + events = target._events; + } + existing = events[type]; + } + + if (!existing) { + // Optimize the case of one listener. Don't need the extra array object. + existing = events[type] = listener; + ++target._eventsCount; + } else { + if (typeof existing === 'function') { + // Adding the second element, need to change to array. + existing = events[type] = + prepend ? [listener, existing] : [existing, listener]; + } else { + // If we've already got an array, just append. + if (prepend) { + existing.unshift(listener); + } else { + existing.push(listener); + } + } + + // Check for listener leak + if (!existing.warned) { + m = $getMaxListeners(target); + if (m && m > 0 && existing.length > m) { + existing.warned = true; + var w = new Error('Possible EventEmitter memory leak detected. ' + + existing.length + ' "' + String(type) + '" listeners ' + + 'added. Use emitter.setMaxListeners() to ' + + 'increase limit.'); + w.name = 'MaxListenersExceededWarning'; + w.emitter = target; + w.type = type; + w.count = existing.length; + if (typeof console === 'object' && console.warn) { + console.warn('%s: %s', w.name, w.message); + } + } + } + } + + return target; + } + + EventEmitter.prototype.addListener = function addListener(type, listener) { + return _addListener(this, type, listener, false); + }; + + EventEmitter.prototype.on = EventEmitter.prototype.addListener; + + EventEmitter.prototype.prependListener = + function prependListener(type, listener) { + return _addListener(this, type, listener, true); + }; + + function onceWrapper() { + if (!this.fired) { + this.target.removeListener(this.type, this.wrapFn); + this.fired = true; + switch (arguments.length) { + case 0: + return this.listener.call(this.target); + case 1: + return this.listener.call(this.target, arguments[0]); + case 2: + return this.listener.call(this.target, arguments[0], arguments[1]); + case 3: + return this.listener.call(this.target, arguments[0], arguments[1], + arguments[2]); + default: + var args = new Array(arguments.length); + for (var i = 0; i < args.length; ++i) + args[i] = arguments[i]; + this.listener.apply(this.target, args); + } + } + } + + function _onceWrap(target, type, listener) { + var state = { + fired: false, + wrapFn: undefined, + target: target, + type: type, + listener: listener + }; + var wrapped = bind.call(onceWrapper, state); + wrapped.listener = listener; + state.wrapFn = wrapped; + return wrapped; + } + + EventEmitter.prototype.once = function once(type, listener) { + if (typeof listener !== 'function') + throw new TypeError('"listener" argument must be a function'); + this.on(type, _onceWrap(this, type, listener)); + return this; + }; + + EventEmitter.prototype.prependOnceListener = + function prependOnceListener(type, listener) { + if (typeof listener !== 'function') + throw new TypeError('"listener" argument must be a function'); + this.prependListener(type, _onceWrap(this, type, listener)); + return this; + }; + + // Emits a 'removeListener' event if and only if the listener was removed. + EventEmitter.prototype.removeListener = + function removeListener(type, listener) { + var list, events, position, i, originalListener; + + if (typeof listener !== 'function') + throw new TypeError('"listener" argument must be a function'); + + events = this._events; + if (!events) + return this; + + list = events[type]; + if (!list) + return this; + + if (list === listener || list.listener === listener) { + if (--this._eventsCount === 0) + this._events = objectCreate(null); + else { + delete events[type]; + if (events.removeListener) + this.emit('removeListener', type, list.listener || listener); + } + } else if (typeof list !== 'function') { + position = -1; + + for (i = list.length - 1; i >= 0; i--) { + if (list[i] === listener || list[i].listener === listener) { + originalListener = list[i].listener; + position = i; + break; + } + } + + if (position < 0) + return this; + + if (position === 0) + list.shift(); + else + spliceOne(list, position); + + if (list.length === 1) + events[type] = list[0]; + + if (events.removeListener) + this.emit('removeListener', type, originalListener || listener); + } + + return this; + }; + + EventEmitter.prototype.removeAllListeners = + function removeAllListeners(type) { + var listeners, events, i; + + events = this._events; + if (!events) + return this; + + // not listening for removeListener, no need to emit + if (!events.removeListener) { + if (arguments.length === 0) { + this._events = objectCreate(null); + this._eventsCount = 0; + } else if (events[type]) { + if (--this._eventsCount === 0) + this._events = objectCreate(null); + else + delete events[type]; + } + return this; + } + + // emit removeListener for all listeners on all events + if (arguments.length === 0) { + var keys = objectKeys(events); + var key; + for (i = 0; i < keys.length; ++i) { + key = keys[i]; + if (key === 'removeListener') continue; + this.removeAllListeners(key); + } + this.removeAllListeners('removeListener'); + this._events = objectCreate(null); + this._eventsCount = 0; + return this; + } + + listeners = events[type]; + + if (typeof listeners === 'function') { + this.removeListener(type, listeners); + } else if (listeners) { + // LIFO order + for (i = listeners.length - 1; i >= 0; i--) { + this.removeListener(type, listeners[i]); + } + } + + return this; + }; + + function _listeners(target, type, unwrap) { + var events = target._events; + + if (!events) + return []; + + var evlistener = events[type]; + if (!evlistener) + return []; + + if (typeof evlistener === 'function') + return unwrap ? [evlistener.listener || evlistener] : [evlistener]; + + return unwrap ? unwrapListeners(evlistener) : arrayClone(evlistener, evlistener.length); + } + + EventEmitter.prototype.listeners = function listeners(type) { + return _listeners(this, type, true); + }; + + EventEmitter.prototype.rawListeners = function rawListeners(type) { + return _listeners(this, type, false); + }; + + EventEmitter.listenerCount = function(emitter, type) { + if (typeof emitter.listenerCount === 'function') { + return emitter.listenerCount(type); + } else { + return listenerCount.call(emitter, type); + } + }; + + EventEmitter.prototype.listenerCount = listenerCount; + + function listenerCount(type) { + var events = this._events; + + if (events) { + var evlistener = events[type]; + + if (typeof evlistener === 'function') { + return 1; + } else if (evlistener) { + return evlistener.length; + } + } + + return 0; + } + + EventEmitter.prototype.eventNames = function eventNames() { + return this._eventsCount > 0 ? Reflect.ownKeys(this._events) : []; + }; + + // About 1.5x faster than the two-arg version of Array#splice(). + function spliceOne(list, index) { + for (var i = index, k = i + 1, n = list.length; k < n; i += 1, k += 1) + list[i] = list[k]; + list.pop(); + } + + function arrayClone(arr, n) { + var copy = new Array(n); + for (var i = 0; i < n; ++i) + copy[i] = arr[i]; + return copy; + } + + function unwrapListeners(arr) { + var ret = new Array(arr.length); + for (var i = 0; i < ret.length; ++i) { + ret[i] = arr[i].listener || arr[i]; + } + return ret; + } + + function objectCreatePolyfill(proto) { + var F = function() {}; + F.prototype = proto; + return new F; + } + + function objectKeysPolyfill(obj) { + var keys = []; + for (var k in obj) + if (Object.prototype.hasOwnProperty.call(obj, k)) { + keys.push(k); + } + return k; + } + + function functionBindPolyfill(context) { + var fn = this; + return function() { + return fn.apply(context, arguments); + }; + } + + }, {}], + 79: [function(require, module, exports) { + exports.read = function(buffer, offset, isLE, mLen, nBytes) { + var e, m + var eLen = (nBytes * 8) - mLen - 1 + var eMax = (1 << eLen) - 1 + var eBias = eMax >> 1 + var nBits = -7 + var i = isLE ? (nBytes - 1) : 0 + var d = isLE ? -1 : 1 + var s = buffer[offset + i] + + i += d + + e = s & ((1 << (-nBits)) - 1) + s >>= (-nBits) + nBits += eLen + for (; nBits > 0; e = (e * 256) + buffer[offset + i], i += d, nBits -= 8) {} + + m = e & ((1 << (-nBits)) - 1) + e >>= (-nBits) + nBits += mLen + for (; nBits > 0; m = (m * 256) + buffer[offset + i], i += d, nBits -= 8) {} + + if (e === 0) { + e = 1 - eBias + } else if (e === eMax) { + return m ? NaN : ((s ? -1 : 1) * Infinity) + } else { + m = m + Math.pow(2, mLen) + e = e - eBias + } + return (s ? -1 : 1) * m * Math.pow(2, e - mLen) + } + + exports.write = function(buffer, value, offset, isLE, mLen, nBytes) { + var e, m, c + var eLen = (nBytes * 8) - mLen - 1 + var eMax = (1 << eLen) - 1 + var eBias = eMax >> 1 + var rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0) + var i = isLE ? 0 : (nBytes - 1) + var d = isLE ? 1 : -1 + var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0 + + value = Math.abs(value) + + if (isNaN(value) || value === Infinity) { + m = isNaN(value) ? 1 : 0 + e = eMax + } else { + e = Math.floor(Math.log(value) / Math.LN2) + if (value * (c = Math.pow(2, -e)) < 1) { + e-- + c *= 2 + } + if (e + eBias >= 1) { + value += rt / c + } else { + value += rt * Math.pow(2, 1 - eBias) + } + if (value * c >= 2) { + e++ + c /= 2 + } + + if (e + eBias >= eMax) { + m = 0 + e = eMax + } else if (e + eBias >= 1) { + m = ((value * c) - 1) * Math.pow(2, mLen) + e = e + eBias + } else { + m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen) + e = 0 + } + } + + for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {} + + e = (e << mLen) | m + eLen += mLen + for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {} + + buffer[offset + i - d] |= s * 128 + } + + }, {}], + 80: [function(require, module, exports) { + if (typeof Object.create === 'function') { + // implementation from standard node.js 'util' module + module.exports = function inherits(ctor, superCtor) { + ctor.super_ = superCtor + ctor.prototype = Object.create(superCtor.prototype, { + constructor: { + value: ctor, + enumerable: false, + writable: true, + configurable: true + } + }); + }; + } else { + // old school shim for old browsers + module.exports = function inherits(ctor, superCtor) { + ctor.super_ = superCtor + var TempCtor = function() {} + TempCtor.prototype = superCtor.prototype + ctor.prototype = new TempCtor() + ctor.prototype.constructor = ctor + } + } + + }, {}], + 81: [function(require, module, exports) { + /*! + * Determine if an object is a Buffer + * + * @author Feross Aboukhadijeh + * @license MIT + */ + + // The _isBuffer check is for Safari 5-7 support, because it's missing + // Object.prototype.constructor. Remove this eventually + module.exports = function(obj) { + return obj != null && (isBuffer(obj) || isSlowBuffer(obj) || !!obj._isBuffer) + } + + function isBuffer(obj) { + return !!obj.constructor && typeof obj.constructor.isBuffer === 'function' && obj.constructor.isBuffer(obj) + } + + // For Node v0.10 support. Remove this eventually. + function isSlowBuffer(obj) { + return typeof obj.readFloatLE === 'function' && typeof obj.slice === 'function' && isBuffer(obj.slice(0, 0)) + } + + }, {}], + 82: [function(require, module, exports) { + var toString = {}.toString; + + module.exports = Array.isArray || function(arr) { + return toString.call(arr) == '[object Array]'; + }; + + }, {}], + 83: [function(require, module, exports) { + 'use strict' + + var Buffer = require('safe-buffer').Buffer + + /* Protocol - protocol constants */ + var protocol = module.exports + + /* Command code => mnemonic */ + protocol.types = { + 0: 'reserved', + 1: 'connect', + 2: 'connack', + 3: 'publish', + 4: 'puback', + 5: 'pubrec', + 6: 'pubrel', + 7: 'pubcomp', + 8: 'subscribe', + 9: 'suback', + 10: 'unsubscribe', + 11: 'unsuback', + 12: 'pingreq', + 13: 'pingresp', + 14: 'disconnect', + 15: 'reserved' + } + + /* Mnemonic => Command code */ + protocol.codes = {} + for (var k in protocol.types) { + var v = protocol.types[k] + protocol.codes[v] = k + } + + /* Header */ + protocol.CMD_SHIFT = 4 + protocol.CMD_MASK = 0xF0 + protocol.DUP_MASK = 0x08 + protocol.QOS_MASK = 0x03 + protocol.QOS_SHIFT = 1 + protocol.RETAIN_MASK = 0x01 + + /* Length */ + protocol.LENGTH_MASK = 0x7F + protocol.LENGTH_FIN_MASK = 0x80 + + /* Connack */ + protocol.SESSIONPRESENT_MASK = 0x01 + protocol.SESSIONPRESENT_HEADER = Buffer.from([protocol.SESSIONPRESENT_MASK]) + protocol.CONNACK_HEADER = Buffer.from([protocol.codes['connack'] << protocol.CMD_SHIFT]) + + /* Connect */ + protocol.USERNAME_MASK = 0x80 + protocol.PASSWORD_MASK = 0x40 + protocol.WILL_RETAIN_MASK = 0x20 + protocol.WILL_QOS_MASK = 0x18 + protocol.WILL_QOS_SHIFT = 3 + protocol.WILL_FLAG_MASK = 0x04 + protocol.CLEAN_SESSION_MASK = 0x02 + protocol.CONNECT_HEADER = Buffer.from([protocol.codes['connect'] << protocol.CMD_SHIFT]) + + function genHeader(type) { + return [0, 1, 2].map(function(qos) { + return [0, 1].map(function(dup) { + return [0, 1].map(function(retain) { + var buf = new Buffer(1) + buf.writeUInt8( + protocol.codes[type] << protocol.CMD_SHIFT | + (dup ? protocol.DUP_MASK : 0) | + qos << protocol.QOS_SHIFT | retain, 0, true) + return buf + }) + }) + }) + } + + /* Publish */ + protocol.PUBLISH_HEADER = genHeader('publish') + + /* Subscribe */ + protocol.SUBSCRIBE_HEADER = genHeader('subscribe') + + /* Unsubscribe */ + protocol.UNSUBSCRIBE_HEADER = genHeader('unsubscribe') + + /* Confirmations */ + protocol.ACKS = { + unsuback: genHeader('unsuback'), + puback: genHeader('puback'), + pubcomp: genHeader('pubcomp'), + pubrel: genHeader('pubrel'), + pubrec: genHeader('pubrec') + } + + protocol.SUBACK_HEADER = Buffer.from([protocol.codes['suback'] << protocol.CMD_SHIFT]) + + /* Protocol versions */ + protocol.VERSION3 = Buffer.from([3]) + protocol.VERSION4 = Buffer.from([4]) + + /* QoS */ + protocol.QOS = [0, 1, 2].map(function(qos) { + return Buffer.from([qos]) + }) + + /* Empty packets */ + protocol.EMPTY = { + pingreq: Buffer.from([protocol.codes['pingreq'] << 4, 0]), + pingresp: Buffer.from([protocol.codes['pingresp'] << 4, 0]), + disconnect: Buffer.from([protocol.codes['disconnect'] << 4, 0]) + } + + }, { + "safe-buffer": 108 + }], + 84: [function(require, module, exports) { + 'use strict' + + var Buffer = require('safe-buffer').Buffer + var writeToStream = require('./writeToStream') + var EE = require('events').EventEmitter + var inherits = require('inherits') + + function generate(packet) { + var stream = new Accumulator() + writeToStream(packet, stream) + return stream.concat() + } + + function Accumulator() { + this._array = new Array(20) + this._i = 0 + } + + inherits(Accumulator, EE) + + Accumulator.prototype.write = function(chunk) { + this._array[this._i++] = chunk + return true + } + + Accumulator.prototype.concat = function() { + var length = 0 + var lengths = new Array(this._array.length) + var list = this._array + var pos = 0 + var i + var result + + for (i = 0; i < list.length && list[i] !== undefined; i++) { + if (typeof list[i] !== 'string') lengths[i] = list[i].length + else lengths[i] = Buffer.byteLength(list[i]) + + length += lengths[i] + } + + result = Buffer.allocUnsafe(length) + + for (i = 0; i < list.length && list[i] !== undefined; i++) { + if (typeof list[i] !== 'string') { + list[i].copy(result, pos) + pos += lengths[i] + } else { + result.write(list[i], pos) + pos += lengths[i] + } + } + + return result + } + + module.exports = generate + + }, { + "./writeToStream": 89, + "events": 78, + "inherits": 80, + "safe-buffer": 108 + }], + 85: [function(require, module, exports) { + 'use strict' + + exports.parser = require('./parser') + exports.generate = require('./generate') + exports.writeToStream = require('./writeToStream') + + }, { + "./generate": 84, + "./parser": 88, + "./writeToStream": 89 + }], + 86: [function(require, module, exports) { + 'use strict' + + var Buffer = require('safe-buffer').Buffer + var max = 65536 + var cache = {} + + function generateBuffer(i) { + var buffer = Buffer.allocUnsafe(2) + buffer.writeUInt8(i >> 8, 0) + buffer.writeUInt8(i & 0x00FF, 0 + 1) + + return buffer + } + + function generateCache() { + for (var i = 0; i < max; i++) { + cache[i] = generateBuffer(i) + } + } + + module.exports = { + cache: cache, + generateCache: generateCache, + generateNumber: generateBuffer + } + + }, { + "safe-buffer": 108 + }], + 87: [function(require, module, exports) { + + function Packet() { + this.cmd = null + this.retain = false + this.qos = 0 + this.dup = false + this.length = -1 + this.topic = null + this.payload = null + } + + module.exports = Packet + + }, {}], + 88: [function(require, module, exports) { + 'use strict' + + var bl = require('bl') + var inherits = require('inherits') + var EE = require('events').EventEmitter + var Packet = require('./packet') + var constants = require('./constants') + + function Parser() { + if (!(this instanceof Parser)) return new Parser() + + this._states = [ + '_parseHeader', + '_parseLength', + '_parsePayload', + '_newPacket' + ] + + this._resetState() + } + + inherits(Parser, EE) + + Parser.prototype._resetState = function() { + this.packet = new Packet() + this.error = null + this._list = bl() + this._stateCounter = 0 + } + + Parser.prototype.parse = function(buf) { + if (this.error) this._resetState() + + this._list.append(buf) + + while ((this.packet.length !== -1 || this._list.length > 0) && + this[this._states[this._stateCounter]]() && + !this.error) { + this._stateCounter++ + + if (this._stateCounter >= this._states.length) this._stateCounter = 0 + } + + return this._list.length + } + + Parser.prototype._parseHeader = function() { + // There is at least one byte in the buffer + var zero = this._list.readUInt8(0) + this.packet.cmd = constants.types[zero >> constants.CMD_SHIFT] + this.packet.retain = (zero & constants.RETAIN_MASK) !== 0 + this.packet.qos = (zero >> constants.QOS_SHIFT) & constants.QOS_MASK + this.packet.dup = (zero & constants.DUP_MASK) !== 0 + + this._list.consume(1) + + return true + } + + Parser.prototype._parseLength = function() { + // There is at least one byte in the list + var bytes = 0 + var mul = 1 + var length = 0 + var result = true + var current + + while (bytes < 5) { + current = this._list.readUInt8(bytes++) + length += mul * (current & constants.LENGTH_MASK) + mul *= 0x80 + + if ((current & constants.LENGTH_FIN_MASK) === 0) break + if (this._list.length <= bytes) { + result = false + break + } + } + + if (result) { + this.packet.length = length + this._list.consume(bytes) + } + + return result + } + + Parser.prototype._parsePayload = function() { + var result = false + + // Do we have a payload? Do we have enough data to complete the payload? + // PINGs have no payload + if (this.packet.length === 0 || this._list.length >= this.packet.length) { + this._pos = 0 + + switch (this.packet.cmd) { + case 'connect': + this._parseConnect() + break + case 'connack': + this._parseConnack() + break + case 'publish': + this._parsePublish() + break + case 'puback': + case 'pubrec': + case 'pubrel': + case 'pubcomp': + this._parseMessageId() + break + case 'subscribe': + this._parseSubscribe() + break + case 'suback': + this._parseSuback() + break + case 'unsubscribe': + this._parseUnsubscribe() + break + case 'unsuback': + this._parseUnsuback() + break + case 'pingreq': + case 'pingresp': + case 'disconnect': + // These are empty, nothing to do + break + default: + this._emitError(new Error('Not supported')) + } + + result = true + } + + return result + } + + Parser.prototype._parseConnect = function() { + var protocolId // Protocol ID + var clientId // Client ID + var topic // Will topic + var payload // Will payload + var password // Password + var username // Username + var flags = {} + var packet = this.packet + + // Parse protocolId + protocolId = this._parseString() + + if (protocolId === null) return this._emitError(new Error('Cannot parse protocolId')) + if (protocolId !== 'MQTT' && protocolId !== 'MQIsdp') { + return this._emitError(new Error('Invalid protocolId')) + } + + packet.protocolId = protocolId + + // Parse constants version number + if (this._pos >= this._list.length) return this._emitError(new Error('Packet too short')) + + packet.protocolVersion = this._list.readUInt8(this._pos) + + if (packet.protocolVersion !== 3 && packet.protocolVersion !== 4) { + return this._emitError(new Error('Invalid protocol version')) + } + + this._pos++ + + if (this._pos >= this._list.length) { + return this._emitError(new Error('Packet too short')) + } + + // Parse connect flags + flags.username = (this._list.readUInt8(this._pos) & constants.USERNAME_MASK) + flags.password = (this._list.readUInt8(this._pos) & constants.PASSWORD_MASK) + flags.will = (this._list.readUInt8(this._pos) & constants.WILL_FLAG_MASK) + + if (flags.will) { + packet.will = {} + packet.will.retain = (this._list.readUInt8(this._pos) & constants.WILL_RETAIN_MASK) !== 0 + packet.will.qos = (this._list.readUInt8(this._pos) & + constants.WILL_QOS_MASK) >> constants.WILL_QOS_SHIFT + } + + packet.clean = (this._list.readUInt8(this._pos) & constants.CLEAN_SESSION_MASK) !== 0 + this._pos++ + + // Parse keepalive + packet.keepalive = this._parseNum() + if (packet.keepalive === -1) return this._emitError(new Error('Packet too short')) + + // Parse clientId + clientId = this._parseString() + if (clientId === null) return this._emitError(new Error('Packet too short')) + packet.clientId = clientId + + if (flags.will) { + // Parse will topic + topic = this._parseString() + if (topic === null) return this._emitError(new Error('Cannot parse will topic')) + packet.will.topic = topic + + // Parse will payload + payload = this._parseBuffer() + if (payload === null) return this._emitError(new Error('Cannot parse will payload')) + packet.will.payload = payload + } + + // Parse username + if (flags.username) { + username = this._parseString() + if (username === null) return this._emitError(new Error('Cannot parse username')) + packet.username = username + } + + // Parse password + if (flags.password) { + password = this._parseBuffer() + if (password === null) return this._emitError(new Error('Cannot parse password')) + packet.password = password + } + + return packet + } + + Parser.prototype._parseConnack = function() { + var packet = this.packet + + if (this._list.length < 2) return null + + packet.sessionPresent = !!(this._list.readUInt8(this._pos++) & constants.SESSIONPRESENT_MASK) + packet.returnCode = this._list.readUInt8(this._pos) + + if (packet.returnCode === -1) return this._emitError(new Error('Cannot parse return code')) + } + + Parser.prototype._parsePublish = function() { + var packet = this.packet + packet.topic = this._parseString() + + if (packet.topic === null) return this._emitError(new Error('Cannot parse topic')) + + // Parse messageId + if (packet.qos > 0) + if (!this._parseMessageId()) { + return + } + + packet.payload = this._list.slice(this._pos, packet.length) + } + + Parser.prototype._parseSubscribe = function() { + var packet = this.packet + var topic + var qos + + if (packet.qos !== 1) { + return this._emitError(new Error('Wrong subscribe header')) + } + + packet.subscriptions = [] + + if (!this._parseMessageId()) { + return + } + + while (this._pos < packet.length) { + // Parse topic + topic = this._parseString() + if (topic === null) return this._emitError(new Error('Cannot parse topic')) + + qos = this._list.readUInt8(this._pos++) + + // Push pair to subscriptions + packet.subscriptions.push({ + topic: topic, + qos: qos + }) + } + } + + Parser.prototype._parseSuback = function() { + this.packet.granted = [] + + if (!this._parseMessageId()) { + return + } + + // Parse granted QoSes + while (this._pos < this.packet.length) { + this.packet.granted.push(this._list.readUInt8(this._pos++)) + } + } + + Parser.prototype._parseUnsubscribe = function() { + var packet = this.packet + + packet.unsubscriptions = [] + + // Parse messageId + if (!this._parseMessageId()) { + return + } + + while (this._pos < packet.length) { + var topic + + // Parse topic + topic = this._parseString() + if (topic === null) return this._emitError(new Error('Cannot parse topic')) + + // Push topic to unsubscriptions + packet.unsubscriptions.push(topic) + } + } + + Parser.prototype._parseUnsuback = function() { + if (!this._parseMessageId()) return this._emitError(new Error('Cannot parse messageId')) + } + + Parser.prototype._parseMessageId = function() { + var packet = this.packet + + packet.messageId = this._parseNum() + + if (packet.messageId === null) { + this._emitError(new Error('Cannot parse messageId')) + return false + } + + return true + } + + Parser.prototype._parseString = function(maybeBuffer) { + var length = this._parseNum() + var result + var end = length + this._pos + + if (length === -1 || end > this._list.length || end > this.packet.length) return null + + result = this._list.toString('utf8', this._pos, end) + this._pos += length + + return result + } + + Parser.prototype._parseBuffer = function() { + var length = this._parseNum() + var result + var end = length + this._pos + + if (length === -1 || end > this._list.length || end > this.packet.length) return null + + result = this._list.slice(this._pos, end) + + this._pos += length + + return result + } + + Parser.prototype._parseNum = function() { + if (this._list.length - this._pos < 2) return -1 + + var result = this._list.readUInt16BE(this._pos) + this._pos += 2 + + return result + } + + Parser.prototype._newPacket = function() { + if (this.packet) { + this._list.consume(this.packet.length) + this.emit('packet', this.packet) + } + + this.packet = new Packet() + + return true + } + + Parser.prototype._emitError = function(err) { + this.error = err + this.emit('error', err) + } + + module.exports = Parser + + }, { + "./constants": 83, + "./packet": 87, + "bl": 10, + "events": 78, + "inherits": 80 + }], + 89: [function(require, module, exports) { + 'use strict' + + var protocol = require('./constants') + var Buffer = require('safe-buffer').Buffer + var empty = Buffer.allocUnsafe(0) + var zeroBuf = Buffer.from([0]) + var numbers = require('./numbers') + var nextTick = require('process-nextick-args').nextTick + + var numCache = numbers.cache + var generateNumber = numbers.generateNumber + var generateCache = numbers.generateCache + var writeNumber = writeNumberCached + var toGenerate = true + + function generate(packet, stream) { + if (stream.cork) { + stream.cork() + nextTick(uncork, stream) + } + + if (toGenerate) { + toGenerate = false + generateCache() + } + + switch (packet.cmd) { + case 'connect': + return connect(packet, stream) + case 'connack': + return connack(packet, stream) + case 'publish': + return publish(packet, stream) + case 'puback': + case 'pubrec': + case 'pubrel': + case 'pubcomp': + case 'unsuback': + return confirmation(packet, stream) + case 'subscribe': + return subscribe(packet, stream) + case 'suback': + return suback(packet, stream) + case 'unsubscribe': + return unsubscribe(packet, stream) + case 'pingreq': + case 'pingresp': + case 'disconnect': + return emptyPacket(packet, stream) + default: + stream.emit('error', new Error('Unknown command')) + return false + } + } + /** + * Controls numbers cache. + * Set to "false" to allocate buffers on-the-flight instead of pre-generated cache + */ + Object.defineProperty(generate, 'cacheNumbers', { + get: function() { + return writeNumber === writeNumberCached + }, + set: function(value) { + if (value) { + if (!numCache || Object.keys(numCache).length === 0) toGenerate = true + writeNumber = writeNumberCached + } else { + toGenerate = false + writeNumber = writeNumberGenerated + } + } + }) + + function uncork(stream) { + stream.uncork() + } + + function connect(opts, stream) { + var settings = opts || {} + var protocolId = settings.protocolId || 'MQTT' + var protocolVersion = settings.protocolVersion || 4 + var will = settings.will + var clean = settings.clean + var keepalive = settings.keepalive || 0 + var clientId = settings.clientId || '' + var username = settings.username + var password = settings.password + + if (clean === undefined) clean = true + + var length = 0 + + // Must be a string and non-falsy + if (!protocolId || + (typeof protocolId !== 'string' && !Buffer.isBuffer(protocolId))) { + stream.emit('error', new Error('Invalid protocolId')) + return false + } else length += protocolId.length + 2 + + // Must be 3 or 4 + if (protocolVersion !== 3 && protocolVersion !== 4) { + stream.emit('error', new Error('Invalid protocol version')) + return false + } else length += 1 + + // ClientId might be omitted in 3.1.1, but only if cleanSession is set to 1 + if ((typeof clientId === 'string' || Buffer.isBuffer(clientId)) && + (clientId || protocolVersion === 4) && (clientId || clean)) { + length += clientId.length + 2 + } else { + if (protocolVersion < 4) { + stream.emit('error', new Error('clientId must be supplied before 3.1.1')) + return false + } + if ((clean * 1) === 0) { + stream.emit('error', new Error('clientId must be given if cleanSession set to 0')) + return false + } + } + + // Must be a two byte number + if (typeof keepalive !== 'number' || + keepalive < 0 || + keepalive > 65535 || + keepalive % 1 !== 0) { + stream.emit('error', new Error('Invalid keepalive')) + return false + } else length += 2 + + // Connect flags + length += 1 + + // If will exists... + if (will) { + // It must be an object + if (typeof will !== 'object') { + stream.emit('error', new Error('Invalid will')) + return false + } + // It must have topic typeof string + if (!will.topic || typeof will.topic !== 'string') { + stream.emit('error', new Error('Invalid will topic')) + return false + } else { + length += Buffer.byteLength(will.topic) + 2 + } + + // Payload + if (will.payload && will.payload) { + if (will.payload.length >= 0) { + if (typeof will.payload === 'string') { + length += Buffer.byteLength(will.payload) + 2 + } else { + length += will.payload.length + 2 + } + } else { + stream.emit('error', new Error('Invalid will payload')) + return false + } + } else { + length += 2 + } + } + + // Username + var providedUsername = false + if (username != null) { + if (isStringOrBuffer(username)) { + providedUsername = true + length += Buffer.byteLength(username) + 2 + } else { + stream.emit('error', new Error('Invalid username')) + return false + } + } + + // Password + if (password != null) { + if (!providedUsername) { + stream.emit('error', new Error('Username is required to use password')) + return false + } + + if (isStringOrBuffer(password)) { + length += byteLength(password) + 2 + } else { + stream.emit('error', new Error('Invalid password')) + return false + } + } + + // Generate header + stream.write(protocol.CONNECT_HEADER) + + // Generate length + writeLength(stream, length) + + // Generate protocol ID + writeStringOrBuffer(stream, protocolId) + stream.write( + protocolVersion === 4 ? protocol.VERSION4 : protocol.VERSION3 + ) + + // Connect flags + var flags = 0 + flags |= (username != null) ? protocol.USERNAME_MASK : 0 + flags |= (password != null) ? protocol.PASSWORD_MASK : 0 + flags |= (will && will.retain) ? protocol.WILL_RETAIN_MASK : 0 + flags |= (will && will.qos) ? will.qos << protocol.WILL_QOS_SHIFT : 0 + flags |= will ? protocol.WILL_FLAG_MASK : 0 + flags |= clean ? protocol.CLEAN_SESSION_MASK : 0 + + stream.write(Buffer.from([flags])) + + // Keepalive + writeNumber(stream, keepalive) + + // Client ID + writeStringOrBuffer(stream, clientId) + + // Will + if (will) { + writeString(stream, will.topic) + writeStringOrBuffer(stream, will.payload) + } + + // Username and password + if (username != null) { + writeStringOrBuffer(stream, username) + } + if (password != null) { + writeStringOrBuffer(stream, password) + } + // This is a small packet that happens only once on a stream + // We assume the stream is always free to receive more data after this + return true + } + + function connack(opts, stream) { + var settings = opts || {} + var rc = settings.returnCode + + // Check return code + if (typeof rc !== 'number') { + stream.emit('error', new Error('Invalid return code')) + return false + } + + stream.write(protocol.CONNACK_HEADER) + writeLength(stream, 2) + stream.write(opts.sessionPresent ? protocol.SESSIONPRESENT_HEADER : zeroBuf) + + return stream.write(Buffer.from([rc])) + } + + function publish(opts, stream) { + var settings = opts || {} + var qos = settings.qos || 0 + var retain = settings.retain ? protocol.RETAIN_MASK : 0 + var topic = settings.topic + var payload = settings.payload || empty + var id = settings.messageId + + var length = 0 + + // Topic must be a non-empty string or Buffer + if (typeof topic === 'string') length += Buffer.byteLength(topic) + 2 + else if (Buffer.isBuffer(topic)) length += topic.length + 2 + else { + stream.emit('error', new Error('Invalid topic')) + return false + } + + // Get the payload length + if (!Buffer.isBuffer(payload)) length += Buffer.byteLength(payload) + else length += payload.length + + // Message ID must a number if qos > 0 + if (qos && typeof id !== 'number') { + stream.emit('error', new Error('Invalid messageId')) + return false + } else if (qos) length += 2 + + // Header + stream.write(protocol.PUBLISH_HEADER[qos][opts.dup ? 1 : 0][retain ? 1 : 0]) + + // Remaining length + writeLength(stream, length) + + // Topic + writeNumber(stream, byteLength(topic)) + stream.write(topic) + + // Message ID + if (qos > 0) writeNumber(stream, id) + + // Payload + return stream.write(payload) + } + + /* Puback, pubrec, pubrel and pubcomp */ + function confirmation(opts, stream) { + var settings = opts || {} + var type = settings.cmd || 'puback' + var id = settings.messageId + var dup = (settings.dup && type === 'pubrel') ? protocol.DUP_MASK : 0 + var qos = 0 + + if (type === 'pubrel') qos = 1 + + // Check message ID + if (typeof id !== 'number') { + stream.emit('error', new Error('Invalid messageId')) + return false + } + + // Header + stream.write(protocol.ACKS[type][qos][dup][0]) + + // Length + writeLength(stream, 2) + + // Message ID + return writeNumber(stream, id) + } + + function subscribe(opts, stream) { + var settings = opts || {} + var dup = settings.dup ? protocol.DUP_MASK : 0 + var id = settings.messageId + var subs = settings.subscriptions + + var length = 0 + + // Check message ID + if (typeof id !== 'number') { + stream.emit('error', new Error('Invalid messageId')) + return false + } else length += 2 + + // Check subscriptions + if (typeof subs === 'object' && subs.length) { + for (var i = 0; i < subs.length; i += 1) { + var itopic = subs[i].topic + var iqos = subs[i].qos + + if (typeof itopic !== 'string') { + stream.emit('error', new Error('Invalid subscriptions - invalid topic')) + return false + } + if (typeof iqos !== 'number') { + stream.emit('error', new Error('Invalid subscriptions - invalid qos')) + return false + } + + length += Buffer.byteLength(itopic) + 2 + 1 + } + } else { + stream.emit('error', new Error('Invalid subscriptions')) + return false + } + + // Generate header + stream.write(protocol.SUBSCRIBE_HEADER[1][dup ? 1 : 0][0]) + + // Generate length + writeLength(stream, length) + + // Generate message ID + writeNumber(stream, id) + + var result = true + + // Generate subs + for (var j = 0; j < subs.length; j++) { + var sub = subs[j] + var jtopic = sub.topic + var jqos = sub.qos + + // Write topic string + writeString(stream, jtopic) + + // Write qos + result = stream.write(protocol.QOS[jqos]) + } + + return result + } + + function suback(opts, stream) { + var settings = opts || {} + var id = settings.messageId + var granted = settings.granted + + var length = 0 + + // Check message ID + if (typeof id !== 'number') { + stream.emit('error', new Error('Invalid messageId')) + return false + } else length += 2 + + // Check granted qos vector + if (typeof granted === 'object' && granted.length) { + for (var i = 0; i < granted.length; i += 1) { + if (typeof granted[i] !== 'number') { + stream.emit('error', new Error('Invalid qos vector')) + return false + } + length += 1 + } + } else { + stream.emit('error', new Error('Invalid qos vector')) + return false + } + + // header + stream.write(protocol.SUBACK_HEADER) + + // Length + writeLength(stream, length) + + // Message ID + writeNumber(stream, id) + + return stream.write(Buffer.from(granted)) + } + + function unsubscribe(opts, stream) { + var settings = opts || {} + var id = settings.messageId + var dup = settings.dup ? protocol.DUP_MASK : 0 + var unsubs = settings.unsubscriptions + + var length = 0 + + // Check message ID + if (typeof id !== 'number') { + stream.emit('error', new Error('Invalid messageId')) + return false + } else { + length += 2 + } + // Check unsubs + if (typeof unsubs === 'object' && unsubs.length) { + for (var i = 0; i < unsubs.length; i += 1) { + if (typeof unsubs[i] !== 'string') { + stream.emit('error', new Error('Invalid unsubscriptions')) + return false + } + length += Buffer.byteLength(unsubs[i]) + 2 + } + } else { + stream.emit('error', new Error('Invalid unsubscriptions')) + return false + } + + // Header + stream.write(protocol.UNSUBSCRIBE_HEADER[1][dup ? 1 : 0][0]) + + // Length + writeLength(stream, length) + + // Message ID + writeNumber(stream, id) + + // Unsubs + var result = true + for (var j = 0; j < unsubs.length; j++) { + result = writeString(stream, unsubs[j]) + } + + return result + } + + function emptyPacket(opts, stream) { + return stream.write(protocol.EMPTY[opts.cmd]) + } + + /** + * calcLengthLength - calculate the length of the remaining + * length field + * + * @api private + */ + function calcLengthLength(length) { + if (length >= 0 && length < 128) return 1 + else if (length >= 128 && length < 16384) return 2 + else if (length >= 16384 && length < 2097152) return 3 + else if (length >= 2097152 && length < 268435456) return 4 + else return 0 + } + + function genBufLength(length) { + var digit = 0 + var pos = 0 + var buffer = Buffer.allocUnsafe(calcLengthLength(length)) + + do { + digit = length % 128 | 0 + length = length / 128 | 0 + if (length > 0) digit = digit | 0x80 + + buffer.writeUInt8(digit, pos++) + } while (length > 0) + + return buffer + } + + /** + * writeLength - write an MQTT style length field to the buffer + * + * @param buffer - destination + * @param pos - offset + * @param length - length (>0) + * @returns number of bytes written + * + * @api private + */ + + var lengthCache = {} + + function writeLength(stream, length) { + var buffer = lengthCache[length] + + if (!buffer) { + buffer = genBufLength(length) + if (length < 16384) lengthCache[length] = buffer + } + + stream.write(buffer) + } + + /** + * writeString - write a utf8 string to the buffer + * + * @param buffer - destination + * @param pos - offset + * @param string - string to write + * @return number of bytes written + * + * @api private + */ + + function writeString(stream, string) { + var strlen = Buffer.byteLength(string) + writeNumber(stream, strlen) + + stream.write(string, 'utf8') + } + + /** + * writeNumber - write a two byte number to the buffer + * + * @param buffer - destination + * @param pos - offset + * @param number - number to write + * @return number of bytes written + * + * @api private + */ + function writeNumberCached(stream, number) { + return stream.write(numCache[number]) + } + + function writeNumberGenerated(stream, number) { + return stream.write(generateNumber(number)) + } + + /** + * writeStringOrBuffer - write a String or Buffer with the its length prefix + * + * @param buffer - destination + * @param pos - offset + * @param toWrite - String or Buffer + * @return number of bytes written + */ + function writeStringOrBuffer(stream, toWrite) { + if (typeof toWrite === 'string') { + writeString(stream, toWrite) + } else if (toWrite) { + writeNumber(stream, toWrite.length) + stream.write(toWrite) + } else writeNumber(stream, 0) + } + + function byteLength(bufOrString) { + if (!bufOrString) return 0 + else if (bufOrString instanceof Buffer) return bufOrString.length + else return Buffer.byteLength(bufOrString) + } + + function isStringOrBuffer(field) { + return typeof field === 'string' || field instanceof Buffer + } + + module.exports = generate + + }, { + "./constants": 83, + "./numbers": 86, + "process-nextick-args": 91, + "safe-buffer": 108 + }], + 90: [function(require, module, exports) { + var wrappy = require('wrappy') + module.exports = wrappy(once) + module.exports.strict = wrappy(onceStrict) + + once.proto = once(function() { + Object.defineProperty(Function.prototype, 'once', { + value: function() { + return once(this) + }, + configurable: true + }) + + Object.defineProperty(Function.prototype, 'onceStrict', { + value: function() { + return onceStrict(this) + }, + configurable: true + }) + }) + + function once(fn) { + var f = function() { + if (f.called) return f.value + f.called = true + return f.value = fn.apply(this, arguments) + } + f.called = false + return f + } + + function onceStrict(fn) { + var f = function() { + if (f.called) + throw new Error(f.onceError) + f.called = true + return f.value = fn.apply(this, arguments) + } + var name = fn.name || 'Function wrapped with `once`' + f.onceError = name + " shouldn't be called more than once" + f.called = false + return f + } + + }, { + "wrappy": 119 + }], + 91: [function(require, module, exports) { + (function(process) { + 'use strict'; + + if (!process.version || + process.version.indexOf('v0.') === 0 || + process.version.indexOf('v1.') === 0 && process.version.indexOf('v1.8.') !== 0) { + module.exports = { + nextTick: nextTick + }; + } else { + module.exports = process + } + + function nextTick(fn, arg1, arg2, arg3) { + if (typeof fn !== 'function') { + throw new TypeError('"callback" argument must be a function'); + } + var len = arguments.length; + var args, i; + switch (len) { + case 0: + case 1: + return process.nextTick(fn); + case 2: + return process.nextTick(function afterTickOne() { + fn.call(null, arg1); + }); + case 3: + return process.nextTick(function afterTickTwo() { + fn.call(null, arg1, arg2); + }); + case 4: + return process.nextTick(function afterTickThree() { + fn.call(null, arg1, arg2, arg3); + }); + default: + args = new Array(len - 1); + i = 0; + while (i < args.length) { + args[i++] = arguments[i]; + } + return process.nextTick(function afterTick() { + fn.apply(null, args); + }); + } + } + + + }).call(this, require('_process')) + }, { + "_process": 92 + }], + 92: [function(require, module, exports) { + // shim for using process in browser + var process = module.exports = {}; + + // cached from whatever global is present so that test runners that stub it + // don't break things. But we need to wrap it in a try catch in case it is + // wrapped in strict mode code which doesn't define any globals. It's inside a + // function because try/catches deoptimize in certain engines. + + var cachedSetTimeout; + var cachedClearTimeout; + + function defaultSetTimout() { + throw new Error('setTimeout has not been defined'); + } + + function defaultClearTimeout() { + throw new Error('clearTimeout has not been defined'); + } + (function() { + try { + if (typeof setTimeout === 'function') { + cachedSetTimeout = setTimeout; + } else { + cachedSetTimeout = defaultSetTimout; + } + } catch (e) { + cachedSetTimeout = defaultSetTimout; + } + try { + if (typeof clearTimeout === 'function') { + cachedClearTimeout = clearTimeout; + } else { + cachedClearTimeout = defaultClearTimeout; + } + } catch (e) { + cachedClearTimeout = defaultClearTimeout; + } + }()) + + function runTimeout(fun) { + if (cachedSetTimeout === setTimeout) { + //normal enviroments in sane situations + return setTimeout(fun, 0); + } + // if setTimeout wasn't available but was latter defined + if ((cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) && setTimeout) { + cachedSetTimeout = setTimeout; + return setTimeout(fun, 0); + } + try { + // when when somebody has screwed with setTimeout but no I.E. maddness + return cachedSetTimeout(fun, 0); + } catch (e) { + try { + // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally + return cachedSetTimeout.call(null, fun, 0); + } catch (e) { + // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error + return cachedSetTimeout.call(this, fun, 0); + } + } + + + } + + function runClearTimeout(marker) { + if (cachedClearTimeout === clearTimeout) { + //normal enviroments in sane situations + return clearTimeout(marker); + } + // if clearTimeout wasn't available but was latter defined + if ((cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) && clearTimeout) { + cachedClearTimeout = clearTimeout; + return clearTimeout(marker); + } + try { + // when when somebody has screwed with setTimeout but no I.E. maddness + return cachedClearTimeout(marker); + } catch (e) { + try { + // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally + return cachedClearTimeout.call(null, marker); + } catch (e) { + // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error. + // Some versions of I.E. have different rules for clearTimeout vs setTimeout + return cachedClearTimeout.call(this, marker); + } + } + + + + } + var queue = []; + var draining = false; + var currentQueue; + var queueIndex = -1; + + function cleanUpNextTick() { + if (!draining || !currentQueue) { + return; + } + draining = false; + if (currentQueue.length) { + queue = currentQueue.concat(queue); + } else { + queueIndex = -1; + } + if (queue.length) { + drainQueue(); + } + } + + function drainQueue() { + if (draining) { + return; + } + var timeout = runTimeout(cleanUpNextTick); + draining = true; + + var len = queue.length; + while (len) { + currentQueue = queue; + queue = []; + while (++queueIndex < len) { + if (currentQueue) { + currentQueue[queueIndex].run(); + } + } + queueIndex = -1; + len = queue.length; + } + currentQueue = null; + draining = false; + runClearTimeout(timeout); + } + + process.nextTick = function(fun) { + var args = new Array(arguments.length - 1); + if (arguments.length > 1) { + for (var i = 1; i < arguments.length; i++) { + args[i - 1] = arguments[i]; + } + } + queue.push(new Item(fun, args)); + if (queue.length === 1 && !draining) { + runTimeout(drainQueue); + } + }; + + // v8 likes predictible objects + function Item(fun, array) { + this.fun = fun; + this.array = array; + } + Item.prototype.run = function() { + this.fun.apply(null, this.array); + }; + process.title = 'browser'; + process.browser = true; + process.env = {}; + process.argv = []; + process.version = ''; // empty string to avoid regexp issues + process.versions = {}; + + function noop() {} + + process.on = noop; + process.addListener = noop; + process.once = noop; + process.off = noop; + process.removeListener = noop; + process.removeAllListeners = noop; + process.emit = noop; + process.prependListener = noop; + process.prependOnceListener = noop; + + process.listeners = function(name) { + return [] + } + + process.binding = function(name) { + throw new Error('process.binding is not supported'); + }; + + process.cwd = function() { + return '/' + }; + process.chdir = function(dir) { + throw new Error('process.chdir is not supported'); + }; + process.umask = function() { + return 0; + }; + + }, {}], + 93: [function(require, module, exports) { + (function(global) { + /*! https://mths.be/punycode v1.4.1 by @mathias */ + ; + (function(root) { + + /** Detect free variables */ + var freeExports = typeof exports == 'object' && exports && + !exports.nodeType && exports; + var freeModule = typeof module == 'object' && module && + !module.nodeType && module; + var freeGlobal = typeof global == 'object' && global; + if ( + freeGlobal.global === freeGlobal || + freeGlobal.window === freeGlobal || + freeGlobal.self === freeGlobal + ) { + root = freeGlobal; + } + + /** + * The `punycode` object. + * @name punycode + * @type Object + */ + var punycode, + + /** Highest positive signed 32-bit float value */ + maxInt = 2147483647, // aka. 0x7FFFFFFF or 2^31-1 + + /** Bootstring parameters */ + base = 36, + tMin = 1, + tMax = 26, + skew = 38, + damp = 700, + initialBias = 72, + initialN = 128, // 0x80 + delimiter = '-', // '\x2D' + + /** Regular expressions */ + regexPunycode = /^xn--/, + regexNonASCII = /[^\x20-\x7E]/, // unprintable ASCII chars + non-ASCII chars + regexSeparators = /[\x2E\u3002\uFF0E\uFF61]/g, // RFC 3490 separators + + /** Error messages */ + errors = { + 'overflow': 'Overflow: input needs wider integers to process', + 'not-basic': 'Illegal input >= 0x80 (not a basic code point)', + 'invalid-input': 'Invalid input' + }, + + /** Convenience shortcuts */ + baseMinusTMin = base - tMin, + floor = Math.floor, + stringFromCharCode = String.fromCharCode, + + /** Temporary variable */ + key; + + /*--------------------------------------------------------------------------*/ + + /** + * A generic error utility function. + * @private + * @param {String} type The error type. + * @returns {Error} Throws a `RangeError` with the applicable error message. + */ + function error(type) { + throw new RangeError(errors[type]); + } + + /** + * A generic `Array#map` utility function. + * @private + * @param {Array} array The array to iterate over. + * @param {Function} callback The function that gets called for every array + * item. + * @returns {Array} A new array of values returned by the callback function. + */ + function map(array, fn) { + var length = array.length; + var result = []; + while (length--) { + result[length] = fn(array[length]); + } + return result; + } + + /** + * A simple `Array#map`-like wrapper to work with domain name strings or email + * addresses. + * @private + * @param {String} domain The domain name or email address. + * @param {Function} callback The function that gets called for every + * character. + * @returns {Array} A new string of characters returned by the callback + * function. + */ + function mapDomain(string, fn) { + var parts = string.split('@'); + var result = ''; + if (parts.length > 1) { + // In email addresses, only the domain name should be punycoded. Leave + // the local part (i.e. everything up to `@`) intact. + result = parts[0] + '@'; + string = parts[1]; + } + // Avoid `split(regex)` for IE8 compatibility. See #17. + string = string.replace(regexSeparators, '\x2E'); + var labels = string.split('.'); + var encoded = map(labels, fn).join('.'); + return result + encoded; + } + + /** + * Creates an array containing the numeric code points of each Unicode + * character in the string. While JavaScript uses UCS-2 internally, + * this function will convert a pair of surrogate halves (each of which + * UCS-2 exposes as separate characters) into a single code point, + * matching UTF-16. + * @see `punycode.ucs2.encode` + * @see + * @memberOf punycode.ucs2 + * @name decode + * @param {String} string The Unicode input string (UCS-2). + * @returns {Array} The new array of code points. + */ + function ucs2decode(string) { + var output = [], + counter = 0, + length = string.length, + value, + extra; + while (counter < length) { + value = string.charCodeAt(counter++); + if (value >= 0xD800 && value <= 0xDBFF && counter < length) { + // high surrogate, and there is a next character + extra = string.charCodeAt(counter++); + if ((extra & 0xFC00) == 0xDC00) { // low surrogate + output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000); + } else { + // unmatched surrogate; only append this code unit, in case the next + // code unit is the high surrogate of a surrogate pair + output.push(value); + counter--; + } + } else { + output.push(value); + } + } + return output; + } + + /** + * Creates a string based on an array of numeric code points. + * @see `punycode.ucs2.decode` + * @memberOf punycode.ucs2 + * @name encode + * @param {Array} codePoints The array of numeric code points. + * @returns {String} The new Unicode string (UCS-2). + */ + function ucs2encode(array) { + return map(array, function(value) { + var output = ''; + if (value > 0xFFFF) { + value -= 0x10000; + output += stringFromCharCode(value >>> 10 & 0x3FF | 0xD800); + value = 0xDC00 | value & 0x3FF; + } + output += stringFromCharCode(value); + return output; + }).join(''); + } + + /** + * Converts a basic code point into a digit/integer. + * @see `digitToBasic()` + * @private + * @param {Number} codePoint The basic numeric code point value. + * @returns {Number} The numeric value of a basic code point (for use in + * representing integers) in the range `0` to `base - 1`, or `base` if + * the code point does not represent a value. + */ + function basicToDigit(codePoint) { + if (codePoint - 48 < 10) { + return codePoint - 22; + } + if (codePoint - 65 < 26) { + return codePoint - 65; + } + if (codePoint - 97 < 26) { + return codePoint - 97; + } + return base; + } + + /** + * Converts a digit/integer into a basic code point. + * @see `basicToDigit()` + * @private + * @param {Number} digit The numeric value of a basic code point. + * @returns {Number} The basic code point whose value (when used for + * representing integers) is `digit`, which needs to be in the range + * `0` to `base - 1`. If `flag` is non-zero, the uppercase form is + * used; else, the lowercase form is used. The behavior is undefined + * if `flag` is non-zero and `digit` has no uppercase form. + */ + function digitToBasic(digit, flag) { + // 0..25 map to ASCII a..z or A..Z + // 26..35 map to ASCII 0..9 + return digit + 22 + 75 * (digit < 26) - ((flag != 0) << 5); + } + + /** + * Bias adaptation function as per section 3.4 of RFC 3492. + * https://tools.ietf.org/html/rfc3492#section-3.4 + * @private + */ + function adapt(delta, numPoints, firstTime) { + var k = 0; + delta = firstTime ? floor(delta / damp) : delta >> 1; + delta += floor(delta / numPoints); + for ( /* no initialization */ ; delta > baseMinusTMin * tMax >> 1; k += base) { + delta = floor(delta / baseMinusTMin); + } + return floor(k + (baseMinusTMin + 1) * delta / (delta + skew)); + } + + /** + * Converts a Punycode string of ASCII-only symbols to a string of Unicode + * symbols. + * @memberOf punycode + * @param {String} input The Punycode string of ASCII-only symbols. + * @returns {String} The resulting string of Unicode symbols. + */ + function decode(input) { + // Don't use UCS-2 + var output = [], + inputLength = input.length, + out, + i = 0, + n = initialN, + bias = initialBias, + basic, + j, + index, + oldi, + w, + k, + digit, + t, + /** Cached calculation results */ + baseMinusT; + + // Handle the basic code points: let `basic` be the number of input code + // points before the last delimiter, or `0` if there is none, then copy + // the first basic code points to the output. + + basic = input.lastIndexOf(delimiter); + if (basic < 0) { + basic = 0; + } + + for (j = 0; j < basic; ++j) { + // if it's not a basic code point + if (input.charCodeAt(j) >= 0x80) { + error('not-basic'); + } + output.push(input.charCodeAt(j)); + } + + // Main decoding loop: start just after the last delimiter if any basic code + // points were copied; start at the beginning otherwise. + + for (index = basic > 0 ? basic + 1 : 0; index < inputLength; /* no final expression */ ) { + + // `index` is the index of the next character to be consumed. + // Decode a generalized variable-length integer into `delta`, + // which gets added to `i`. The overflow checking is easier + // if we increase `i` as we go, then subtract off its starting + // value at the end to obtain `delta`. + for (oldi = i, w = 1, k = base; /* no condition */ ; k += base) { + + if (index >= inputLength) { + error('invalid-input'); + } + + digit = basicToDigit(input.charCodeAt(index++)); + + if (digit >= base || digit > floor((maxInt - i) / w)) { + error('overflow'); + } + + i += digit * w; + t = k <= bias ? tMin : (k >= bias + tMax ? tMax : k - bias); + + if (digit < t) { + break; + } + + baseMinusT = base - t; + if (w > floor(maxInt / baseMinusT)) { + error('overflow'); + } + + w *= baseMinusT; + + } + + out = output.length + 1; + bias = adapt(i - oldi, out, oldi == 0); + + // `i` was supposed to wrap around from `out` to `0`, + // incrementing `n` each time, so we'll fix that now: + if (floor(i / out) > maxInt - n) { + error('overflow'); + } + + n += floor(i / out); + i %= out; + + // Insert `n` at position `i` of the output + output.splice(i++, 0, n); + + } + + return ucs2encode(output); + } + + /** + * Converts a string of Unicode symbols (e.g. a domain name label) to a + * Punycode string of ASCII-only symbols. + * @memberOf punycode + * @param {String} input The string of Unicode symbols. + * @returns {String} The resulting Punycode string of ASCII-only symbols. + */ + function encode(input) { + var n, + delta, + handledCPCount, + basicLength, + bias, + j, + m, + q, + k, + t, + currentValue, + output = [], + /** `inputLength` will hold the number of code points in `input`. */ + inputLength, + /** Cached calculation results */ + handledCPCountPlusOne, + baseMinusT, + qMinusT; + + // Convert the input in UCS-2 to Unicode + input = ucs2decode(input); + + // Cache the length + inputLength = input.length; + + // Initialize the state + n = initialN; + delta = 0; + bias = initialBias; + + // Handle the basic code points + for (j = 0; j < inputLength; ++j) { + currentValue = input[j]; + if (currentValue < 0x80) { + output.push(stringFromCharCode(currentValue)); + } + } + + handledCPCount = basicLength = output.length; + + // `handledCPCount` is the number of code points that have been handled; + // `basicLength` is the number of basic code points. + + // Finish the basic string - if it is not empty - with a delimiter + if (basicLength) { + output.push(delimiter); + } + + // Main encoding loop: + while (handledCPCount < inputLength) { + + // All non-basic code points < n have been handled already. Find the next + // larger one: + for (m = maxInt, j = 0; j < inputLength; ++j) { + currentValue = input[j]; + if (currentValue >= n && currentValue < m) { + m = currentValue; + } + } + + // Increase `delta` enough to advance the decoder's state to , + // but guard against overflow + handledCPCountPlusOne = handledCPCount + 1; + if (m - n > floor((maxInt - delta) / handledCPCountPlusOne)) { + error('overflow'); + } + + delta += (m - n) * handledCPCountPlusOne; + n = m; + + for (j = 0; j < inputLength; ++j) { + currentValue = input[j]; + + if (currentValue < n && ++delta > maxInt) { + error('overflow'); + } + + if (currentValue == n) { + // Represent delta as a generalized variable-length integer + for (q = delta, k = base; /* no condition */ ; k += base) { + t = k <= bias ? tMin : (k >= bias + tMax ? tMax : k - bias); + if (q < t) { + break; + } + qMinusT = q - t; + baseMinusT = base - t; + output.push( + stringFromCharCode(digitToBasic(t + qMinusT % baseMinusT, 0)) + ); + q = floor(qMinusT / baseMinusT); + } + + output.push(stringFromCharCode(digitToBasic(q, 0))); + bias = adapt(delta, handledCPCountPlusOne, handledCPCount == basicLength); + delta = 0; + ++handledCPCount; + } + } + + ++delta; + ++n; + + } + return output.join(''); + } + + /** + * Converts a Punycode string representing a domain name or an email address + * to Unicode. Only the Punycoded parts of the input will be converted, i.e. + * it doesn't matter if you call it on a string that has already been + * converted to Unicode. + * @memberOf punycode + * @param {String} input The Punycoded domain name or email address to + * convert to Unicode. + * @returns {String} The Unicode representation of the given Punycode + * string. + */ + function toUnicode(input) { + return mapDomain(input, function(string) { + return regexPunycode.test(string) ? + decode(string.slice(4).toLowerCase()) : + string; + }); + } + + /** + * Converts a Unicode string representing a domain name or an email address to + * Punycode. Only the non-ASCII parts of the domain name will be converted, + * i.e. it doesn't matter if you call it with a domain that's already in + * ASCII. + * @memberOf punycode + * @param {String} input The domain name or email address to convert, as a + * Unicode string. + * @returns {String} The Punycode representation of the given domain name or + * email address. + */ + function toASCII(input) { + return mapDomain(input, function(string) { + return regexNonASCII.test(string) ? + 'xn--' + encode(string) : + string; + }); + } + + /*--------------------------------------------------------------------------*/ + + /** Define the public API */ + punycode = { + /** + * A string representing the current Punycode.js version number. + * @memberOf punycode + * @type String + */ + 'version': '1.4.1', + /** + * An object of methods to convert from JavaScript's internal character + * representation (UCS-2) to Unicode code points, and back. + * @see + * @memberOf punycode + * @type Object + */ + 'ucs2': { + 'decode': ucs2decode, + 'encode': ucs2encode + }, + 'decode': decode, + 'encode': encode, + 'toASCII': toASCII, + 'toUnicode': toUnicode + }; + + /** Expose `punycode` */ + // Some AMD build optimizers, like r.js, check for specific condition patterns + // like the following: + if ( + typeof define == 'function' && + typeof define.amd == 'object' && + define.amd + ) { + define('punycode', function() { + return punycode; + }); + } else if (freeExports && freeModule) { + if (module.exports == freeExports) { + // in Node.js, io.js, or RingoJS v0.8.0+ + freeModule.exports = punycode; + } else { + // in Narwhal or RingoJS v0.7.0- + for (key in punycode) { + punycode.hasOwnProperty(key) && (freeExports[key] = punycode[key]); + } + } + } else { + // in Rhino or a web browser + root.punycode = punycode; + } + + }(this)); + + }).call(this, typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) + }, {}], + 94: [function(require, module, exports) { + // Copyright Joyent, Inc. and other Node contributors. + // + // Permission is hereby granted, free of charge, to any person obtaining a + // copy of this software and associated documentation files (the + // "Software"), to deal in the Software without restriction, including + // without limitation the rights to use, copy, modify, merge, publish, + // distribute, sublicense, and/or sell copies of the Software, and to permit + // persons to whom the Software is furnished to do so, subject to the + // following conditions: + // + // The above copyright notice and this permission notice shall be included + // in all copies or substantial portions of the Software. + // + // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN + // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE + // USE OR OTHER DEALINGS IN THE SOFTWARE. + + 'use strict'; + + // If obj.hasOwnProperty has been overridden, then calling + // obj.hasOwnProperty(prop) will break. + // See: https://github.com/joyent/node/issues/1707 + function hasOwnProperty(obj, prop) { + return Object.prototype.hasOwnProperty.call(obj, prop); + } + + module.exports = function(qs, sep, eq, options) { + sep = sep || '&'; + eq = eq || '='; + var obj = {}; + + if (typeof qs !== 'string' || qs.length === 0) { + return obj; + } + + var regexp = /\+/g; + qs = qs.split(sep); + + var maxKeys = 1000; + if (options && typeof options.maxKeys === 'number') { + maxKeys = options.maxKeys; + } + + var len = qs.length; + // maxKeys <= 0 means that we should not limit keys count + if (maxKeys > 0 && len > maxKeys) { + len = maxKeys; + } + + for (var i = 0; i < len; ++i) { + var x = qs[i].replace(regexp, '%20'), + idx = x.indexOf(eq), + kstr, vstr, k, v; + + if (idx >= 0) { + kstr = x.substr(0, idx); + vstr = x.substr(idx + 1); + } else { + kstr = x; + vstr = ''; + } + + k = decodeURIComponent(kstr); + v = decodeURIComponent(vstr); + + if (!hasOwnProperty(obj, k)) { + obj[k] = v; + } else if (isArray(obj[k])) { + obj[k].push(v); + } else { + obj[k] = [obj[k], v]; + } + } + + return obj; + }; + + var isArray = Array.isArray || function(xs) { + return Object.prototype.toString.call(xs) === '[object Array]'; + }; + + }, {}], + 95: [function(require, module, exports) { + // Copyright Joyent, Inc. and other Node contributors. + // + // Permission is hereby granted, free of charge, to any person obtaining a + // copy of this software and associated documentation files (the + // "Software"), to deal in the Software without restriction, including + // without limitation the rights to use, copy, modify, merge, publish, + // distribute, sublicense, and/or sell copies of the Software, and to permit + // persons to whom the Software is furnished to do so, subject to the + // following conditions: + // + // The above copyright notice and this permission notice shall be included + // in all copies or substantial portions of the Software. + // + // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN + // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE + // USE OR OTHER DEALINGS IN THE SOFTWARE. + + 'use strict'; + + var stringifyPrimitive = function(v) { + switch (typeof v) { + case 'string': + return v; + + case 'boolean': + return v ? 'true' : 'false'; + + case 'number': + return isFinite(v) ? v : ''; + + default: + return ''; + } + }; + + module.exports = function(obj, sep, eq, name) { + sep = sep || '&'; + eq = eq || '='; + if (obj === null) { + obj = undefined; + } + + if (typeof obj === 'object') { + return map(objectKeys(obj), function(k) { + var ks = encodeURIComponent(stringifyPrimitive(k)) + eq; + if (isArray(obj[k])) { + return map(obj[k], function(v) { + return ks + encodeURIComponent(stringifyPrimitive(v)); + }).join(sep); + } else { + return ks + encodeURIComponent(stringifyPrimitive(obj[k])); + } + }).join(sep); + + } + + if (!name) return ''; + return encodeURIComponent(stringifyPrimitive(name)) + eq + + encodeURIComponent(stringifyPrimitive(obj)); + }; + + var isArray = Array.isArray || function(xs) { + return Object.prototype.toString.call(xs) === '[object Array]'; + }; + + function map(xs, f) { + if (xs.map) return xs.map(f); + var res = []; + for (var i = 0; i < xs.length; i++) { + res.push(f(xs[i], i)); + } + return res; + } + + var objectKeys = Object.keys || function(obj) { + var res = []; + for (var key in obj) { + if (Object.prototype.hasOwnProperty.call(obj, key)) res.push(key); + } + return res; + }; + + }, {}], + 96: [function(require, module, exports) { + 'use strict'; + + exports.decode = exports.parse = require('./decode'); + exports.encode = exports.stringify = require('./encode'); + + }, { + "./decode": 94, + "./encode": 95 + }], + 97: [function(require, module, exports) { + module.exports = require('./lib/_stream_duplex.js'); + + }, { + "./lib/_stream_duplex.js": 98 + }], + 98: [function(require, module, exports) { + // Copyright Joyent, Inc. and other Node contributors. + // + // Permission is hereby granted, free of charge, to any person obtaining a + // copy of this software and associated documentation files (the + // "Software"), to deal in the Software without restriction, including + // without limitation the rights to use, copy, modify, merge, publish, + // distribute, sublicense, and/or sell copies of the Software, and to permit + // persons to whom the Software is furnished to do so, subject to the + // following conditions: + // + // The above copyright notice and this permission notice shall be included + // in all copies or substantial portions of the Software. + // + // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN + // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE + // USE OR OTHER DEALINGS IN THE SOFTWARE. + + // a duplex stream is just a stream that is both readable and writable. + // Since JS doesn't have multiple prototypal inheritance, this class + // prototypally inherits from Readable, and then parasitically from + // Writable. + + 'use strict'; + + /**/ + + var pna = require('process-nextick-args'); + /**/ + + /**/ + var objectKeys = Object.keys || function(obj) { + var keys = []; + for (var key in obj) { + keys.push(key); + } + return keys; + }; + /**/ + + module.exports = Duplex; + + /**/ + var util = require('core-util-is'); + util.inherits = require('inherits'); + /**/ + + var Readable = require('./_stream_readable'); + var Writable = require('./_stream_writable'); + + util.inherits(Duplex, Readable); + + { + // avoid scope creep, the keys array can then be collected + var keys = objectKeys(Writable.prototype); + for (var v = 0; v < keys.length; v++) { + var method = keys[v]; + if (!Duplex.prototype[method]) Duplex.prototype[method] = Writable.prototype[method]; + } + } + + function Duplex(options) { + if (!(this instanceof Duplex)) return new Duplex(options); + + Readable.call(this, options); + Writable.call(this, options); + + if (options && options.readable === false) this.readable = false; + + if (options && options.writable === false) this.writable = false; + + this.allowHalfOpen = true; + if (options && options.allowHalfOpen === false) this.allowHalfOpen = false; + + this.once('end', onend); + } + + Object.defineProperty(Duplex.prototype, 'writableHighWaterMark', { + // making it explicit this property is not enumerable + // because otherwise some prototype manipulation in + // userland will fail + enumerable: false, + get: function() { + return this._writableState.highWaterMark; + } + }); + + // the no-half-open enforcer + function onend() { + // if we allow half-open state, or if the writable side ended, + // then we're ok. + if (this.allowHalfOpen || this._writableState.ended) return; + + // no more data can be written. + // But allow more writes to happen in this tick. + pna.nextTick(onEndNT, this); + } + + function onEndNT(self) { + self.end(); + } + + Object.defineProperty(Duplex.prototype, 'destroyed', { + get: function() { + if (this._readableState === undefined || this._writableState === undefined) { + return false; + } + return this._readableState.destroyed && this._writableState.destroyed; + }, + set: function(value) { + // we ignore the value if the stream + // has not been initialized yet + if (this._readableState === undefined || this._writableState === undefined) { + return; + } + + // backward compatibility, the user is explicitly + // managing destroyed + this._readableState.destroyed = value; + this._writableState.destroyed = value; + } + }); + + Duplex.prototype._destroy = function(err, cb) { + this.push(null); + this.end(); + + pna.nextTick(cb, err); + }; + }, { + "./_stream_readable": 100, + "./_stream_writable": 102, + "core-util-is": 13, + "inherits": 80, + "process-nextick-args": 91 + }], + 99: [function(require, module, exports) { + // Copyright Joyent, Inc. and other Node contributors. + // + // Permission is hereby granted, free of charge, to any person obtaining a + // copy of this software and associated documentation files (the + // "Software"), to deal in the Software without restriction, including + // without limitation the rights to use, copy, modify, merge, publish, + // distribute, sublicense, and/or sell copies of the Software, and to permit + // persons to whom the Software is furnished to do so, subject to the + // following conditions: + // + // The above copyright notice and this permission notice shall be included + // in all copies or substantial portions of the Software. + // + // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN + // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE + // USE OR OTHER DEALINGS IN THE SOFTWARE. + + // a passthrough stream. + // basically just the most minimal sort of Transform stream. + // Every written chunk gets output as-is. + + 'use strict'; + + module.exports = PassThrough; + + var Transform = require('./_stream_transform'); + + /**/ + var util = require('core-util-is'); + util.inherits = require('inherits'); + /**/ + + util.inherits(PassThrough, Transform); + + function PassThrough(options) { + if (!(this instanceof PassThrough)) return new PassThrough(options); + + Transform.call(this, options); + } + + PassThrough.prototype._transform = function(chunk, encoding, cb) { + cb(null, chunk); + }; + }, { + "./_stream_transform": 101, + "core-util-is": 13, + "inherits": 80 + }], + 100: [function(require, module, exports) { + (function(process, global) { + // Copyright Joyent, Inc. and other Node contributors. + // + // Permission is hereby granted, free of charge, to any person obtaining a + // copy of this software and associated documentation files (the + // "Software"), to deal in the Software without restriction, including + // without limitation the rights to use, copy, modify, merge, publish, + // distribute, sublicense, and/or sell copies of the Software, and to permit + // persons to whom the Software is furnished to do so, subject to the + // following conditions: + // + // The above copyright notice and this permission notice shall be included + // in all copies or substantial portions of the Software. + // + // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN + // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE + // USE OR OTHER DEALINGS IN THE SOFTWARE. + + 'use strict'; + + /**/ + + var pna = require('process-nextick-args'); + /**/ + + module.exports = Readable; + + /**/ + var isArray = require('isarray'); + /**/ + + /**/ + var Duplex; + /**/ + + Readable.ReadableState = ReadableState; + + /**/ + var EE = require('events').EventEmitter; + + var EElistenerCount = function(emitter, type) { + return emitter.listeners(type).length; + }; + /**/ + + /**/ + var Stream = require('./internal/streams/stream'); + /**/ + + /**/ + + var Buffer = require('safe-buffer').Buffer; + var OurUint8Array = global.Uint8Array || function() {}; + + function _uint8ArrayToBuffer(chunk) { + return Buffer.from(chunk); + } + + function _isUint8Array(obj) { + return Buffer.isBuffer(obj) || obj instanceof OurUint8Array; + } + + /**/ + + /**/ + var util = require('core-util-is'); + util.inherits = require('inherits'); + /**/ + + /**/ + var debugUtil = require('util'); + var debug = void 0; + if (debugUtil && debugUtil.debuglog) { + debug = debugUtil.debuglog('stream'); + } else { + debug = function() {}; + } + /**/ + + var BufferList = require('./internal/streams/BufferList'); + var destroyImpl = require('./internal/streams/destroy'); + var StringDecoder; + + util.inherits(Readable, Stream); + + var kProxyEvents = ['error', 'close', 'destroy', 'pause', 'resume']; + + function prependListener(emitter, event, fn) { + // Sadly this is not cacheable as some libraries bundle their own + // event emitter implementation with them. + if (typeof emitter.prependListener === 'function') return emitter.prependListener(event, fn); + + // This is a hack to make sure that our error handler is attached before any + // userland ones. NEVER DO THIS. This is here only because this code needs + // to continue to work with older versions of Node.js that do not include + // the prependListener() method. The goal is to eventually remove this hack. + if (!emitter._events || !emitter._events[event]) emitter.on(event, fn); + else if (isArray(emitter._events[event])) emitter._events[event].unshift(fn); + else emitter._events[event] = [fn, emitter._events[event]]; + } + + function ReadableState(options, stream) { + Duplex = Duplex || require('./_stream_duplex'); + + options = options || {}; + + // Duplex streams are both readable and writable, but share + // the same options object. + // However, some cases require setting options to different + // values for the readable and the writable sides of the duplex stream. + // These options can be provided separately as readableXXX and writableXXX. + var isDuplex = stream instanceof Duplex; + + // object stream flag. Used to make read(n) ignore n and to + // make all the buffer merging and length checks go away + this.objectMode = !!options.objectMode; + + if (isDuplex) this.objectMode = this.objectMode || !!options.readableObjectMode; + + // the point at which it stops calling _read() to fill the buffer + // Note: 0 is a valid value, means "don't call _read preemptively ever" + var hwm = options.highWaterMark; + var readableHwm = options.readableHighWaterMark; + var defaultHwm = this.objectMode ? 16 : 16 * 1024; + + if (hwm || hwm === 0) this.highWaterMark = hwm; + else if (isDuplex && (readableHwm || readableHwm === 0)) this.highWaterMark = readableHwm; + else this.highWaterMark = defaultHwm; + + // cast to ints. + this.highWaterMark = Math.floor(this.highWaterMark); + + // A linked list is used to store data chunks instead of an array because the + // linked list can remove elements from the beginning faster than + // array.shift() + this.buffer = new BufferList(); + this.length = 0; + this.pipes = null; + this.pipesCount = 0; + this.flowing = null; + this.ended = false; + this.endEmitted = false; + this.reading = false; + + // a flag to be able to tell if the event 'readable'/'data' is emitted + // immediately, or on a later tick. We set this to true at first, because + // any actions that shouldn't happen until "later" should generally also + // not happen before the first read call. + this.sync = true; + + // whenever we return null, then we set a flag to say + // that we're awaiting a 'readable' event emission. + this.needReadable = false; + this.emittedReadable = false; + this.readableListening = false; + this.resumeScheduled = false; + + // has it been destroyed + this.destroyed = false; + + // Crypto is kind of old and crusty. Historically, its default string + // encoding is 'binary' so we have to make this configurable. + // Everything else in the universe uses 'utf8', though. + this.defaultEncoding = options.defaultEncoding || 'utf8'; + + // the number of writers that are awaiting a drain event in .pipe()s + this.awaitDrain = 0; + + // if true, a maybeReadMore has been scheduled + this.readingMore = false; + + this.decoder = null; + this.encoding = null; + if (options.encoding) { + if (!StringDecoder) StringDecoder = require('string_decoder/').StringDecoder; + this.decoder = new StringDecoder(options.encoding); + this.encoding = options.encoding; + } + } + + function Readable(options) { + Duplex = Duplex || require('./_stream_duplex'); + + if (!(this instanceof Readable)) return new Readable(options); + + this._readableState = new ReadableState(options, this); + + // legacy + this.readable = true; + + if (options) { + if (typeof options.read === 'function') this._read = options.read; + + if (typeof options.destroy === 'function') this._destroy = options.destroy; + } + + Stream.call(this); + } + + Object.defineProperty(Readable.prototype, 'destroyed', { + get: function() { + if (this._readableState === undefined) { + return false; + } + return this._readableState.destroyed; + }, + set: function(value) { + // we ignore the value if the stream + // has not been initialized yet + if (!this._readableState) { + return; + } + + // backward compatibility, the user is explicitly + // managing destroyed + this._readableState.destroyed = value; + } + }); + + Readable.prototype.destroy = destroyImpl.destroy; + Readable.prototype._undestroy = destroyImpl.undestroy; + Readable.prototype._destroy = function(err, cb) { + this.push(null); + cb(err); + }; + + // Manually shove something into the read() buffer. + // This returns true if the highWaterMark has not been hit yet, + // similar to how Writable.write() returns true if you should + // write() some more. + Readable.prototype.push = function(chunk, encoding) { + var state = this._readableState; + var skipChunkCheck; + + if (!state.objectMode) { + if (typeof chunk === 'string') { + encoding = encoding || state.defaultEncoding; + if (encoding !== state.encoding) { + chunk = Buffer.from(chunk, encoding); + encoding = ''; + } + skipChunkCheck = true; + } + } else { + skipChunkCheck = true; + } + + return readableAddChunk(this, chunk, encoding, false, skipChunkCheck); + }; + + // Unshift should *always* be something directly out of read() + Readable.prototype.unshift = function(chunk) { + return readableAddChunk(this, chunk, null, true, false); + }; + + function readableAddChunk(stream, chunk, encoding, addToFront, skipChunkCheck) { + var state = stream._readableState; + if (chunk === null) { + state.reading = false; + onEofChunk(stream, state); + } else { + var er; + if (!skipChunkCheck) er = chunkInvalid(state, chunk); + if (er) { + stream.emit('error', er); + } else if (state.objectMode || chunk && chunk.length > 0) { + if (typeof chunk !== 'string' && !state.objectMode && Object.getPrototypeOf(chunk) !== Buffer.prototype) { + chunk = _uint8ArrayToBuffer(chunk); + } + + if (addToFront) { + if (state.endEmitted) stream.emit('error', new Error('stream.unshift() after end event')); + else addChunk(stream, state, chunk, true); + } else if (state.ended) { + stream.emit('error', new Error('stream.push() after EOF')); + } else { + state.reading = false; + if (state.decoder && !encoding) { + chunk = state.decoder.write(chunk); + if (state.objectMode || chunk.length !== 0) addChunk(stream, state, chunk, false); + else maybeReadMore(stream, state); + } else { + addChunk(stream, state, chunk, false); + } + } + } else if (!addToFront) { + state.reading = false; + } + } + + return needMoreData(state); + } + + function addChunk(stream, state, chunk, addToFront) { + if (state.flowing && state.length === 0 && !state.sync) { + stream.emit('data', chunk); + stream.read(0); + } else { + // update the buffer info. + state.length += state.objectMode ? 1 : chunk.length; + if (addToFront) state.buffer.unshift(chunk); + else state.buffer.push(chunk); + + if (state.needReadable) emitReadable(stream); + } + maybeReadMore(stream, state); + } + + function chunkInvalid(state, chunk) { + var er; + if (!_isUint8Array(chunk) && typeof chunk !== 'string' && chunk !== undefined && !state.objectMode) { + er = new TypeError('Invalid non-string/buffer chunk'); + } + return er; + } + + // if it's past the high water mark, we can push in some more. + // Also, if we have no data yet, we can stand some + // more bytes. This is to work around cases where hwm=0, + // such as the repl. Also, if the push() triggered a + // readable event, and the user called read(largeNumber) such that + // needReadable was set, then we ought to push more, so that another + // 'readable' event will be triggered. + function needMoreData(state) { + return !state.ended && (state.needReadable || state.length < state.highWaterMark || state.length === 0); + } + + Readable.prototype.isPaused = function() { + return this._readableState.flowing === false; + }; + + // backwards compatibility. + Readable.prototype.setEncoding = function(enc) { + if (!StringDecoder) StringDecoder = require('string_decoder/').StringDecoder; + this._readableState.decoder = new StringDecoder(enc); + this._readableState.encoding = enc; + return this; + }; + + // Don't raise the hwm > 8MB + var MAX_HWM = 0x800000; + + function computeNewHighWaterMark(n) { + if (n >= MAX_HWM) { + n = MAX_HWM; + } else { + // Get the next highest power of 2 to prevent increasing hwm excessively in + // tiny amounts + n--; + n |= n >>> 1; + n |= n >>> 2; + n |= n >>> 4; + n |= n >>> 8; + n |= n >>> 16; + n++; + } + return n; + } + + // This function is designed to be inlinable, so please take care when making + // changes to the function body. + function howMuchToRead(n, state) { + if (n <= 0 || state.length === 0 && state.ended) return 0; + if (state.objectMode) return 1; + if (n !== n) { + // Only flow one buffer at a time + if (state.flowing && state.length) return state.buffer.head.data.length; + else return state.length; + } + // If we're asking for more than the current hwm, then raise the hwm. + if (n > state.highWaterMark) state.highWaterMark = computeNewHighWaterMark(n); + if (n <= state.length) return n; + // Don't have enough + if (!state.ended) { + state.needReadable = true; + return 0; + } + return state.length; + } + + // you can override either this method, or the async _read(n) below. + Readable.prototype.read = function(n) { + debug('read', n); + n = parseInt(n, 10); + var state = this._readableState; + var nOrig = n; + + if (n !== 0) state.emittedReadable = false; + + // if we're doing read(0) to trigger a readable event, but we + // already have a bunch of data in the buffer, then just trigger + // the 'readable' event and move on. + if (n === 0 && state.needReadable && (state.length >= state.highWaterMark || state.ended)) { + debug('read: emitReadable', state.length, state.ended); + if (state.length === 0 && state.ended) endReadable(this); + else emitReadable(this); + return null; + } + + n = howMuchToRead(n, state); + + // if we've ended, and we're now clear, then finish it up. + if (n === 0 && state.ended) { + if (state.length === 0) endReadable(this); + return null; + } + + // All the actual chunk generation logic needs to be + // *below* the call to _read. The reason is that in certain + // synthetic stream cases, such as passthrough streams, _read + // may be a completely synchronous operation which may change + // the state of the read buffer, providing enough data when + // before there was *not* enough. + // + // So, the steps are: + // 1. Figure out what the state of things will be after we do + // a read from the buffer. + // + // 2. If that resulting state will trigger a _read, then call _read. + // Note that this may be asynchronous, or synchronous. Yes, it is + // deeply ugly to write APIs this way, but that still doesn't mean + // that the Readable class should behave improperly, as streams are + // designed to be sync/async agnostic. + // Take note if the _read call is sync or async (ie, if the read call + // has returned yet), so that we know whether or not it's safe to emit + // 'readable' etc. + // + // 3. Actually pull the requested chunks out of the buffer and return. + + // if we need a readable event, then we need to do some reading. + var doRead = state.needReadable; + debug('need readable', doRead); + + // if we currently have less than the highWaterMark, then also read some + if (state.length === 0 || state.length - n < state.highWaterMark) { + doRead = true; + debug('length less than watermark', doRead); + } + + // however, if we've ended, then there's no point, and if we're already + // reading, then it's unnecessary. + if (state.ended || state.reading) { + doRead = false; + debug('reading or ended', doRead); + } else if (doRead) { + debug('do read'); + state.reading = true; + state.sync = true; + // if the length is currently zero, then we *need* a readable event. + if (state.length === 0) state.needReadable = true; + // call internal read method + this._read(state.highWaterMark); + state.sync = false; + // If _read pushed data synchronously, then `reading` will be false, + // and we need to re-evaluate how much data we can return to the user. + if (!state.reading) n = howMuchToRead(nOrig, state); + } + + var ret; + if (n > 0) ret = fromList(n, state); + else ret = null; + + if (ret === null) { + state.needReadable = true; + n = 0; + } else { + state.length -= n; + } + + if (state.length === 0) { + // If we have nothing in the buffer, then we want to know + // as soon as we *do* get something into the buffer. + if (!state.ended) state.needReadable = true; + + // If we tried to read() past the EOF, then emit end on the next tick. + if (nOrig !== n && state.ended) endReadable(this); + } + + if (ret !== null) this.emit('data', ret); + + return ret; + }; + + function onEofChunk(stream, state) { + if (state.ended) return; + if (state.decoder) { + var chunk = state.decoder.end(); + if (chunk && chunk.length) { + state.buffer.push(chunk); + state.length += state.objectMode ? 1 : chunk.length; + } + } + state.ended = true; + + // emit 'readable' now to make sure it gets picked up. + emitReadable(stream); + } + + // Don't emit readable right away in sync mode, because this can trigger + // another read() call => stack overflow. This way, it might trigger + // a nextTick recursion warning, but that's not so bad. + function emitReadable(stream) { + var state = stream._readableState; + state.needReadable = false; + if (!state.emittedReadable) { + debug('emitReadable', state.flowing); + state.emittedReadable = true; + if (state.sync) pna.nextTick(emitReadable_, stream); + else emitReadable_(stream); + } + } + + function emitReadable_(stream) { + debug('emit readable'); + stream.emit('readable'); + flow(stream); + } + + // at this point, the user has presumably seen the 'readable' event, + // and called read() to consume some data. that may have triggered + // in turn another _read(n) call, in which case reading = true if + // it's in progress. + // However, if we're not ended, or reading, and the length < hwm, + // then go ahead and try to read some more preemptively. + function maybeReadMore(stream, state) { + if (!state.readingMore) { + state.readingMore = true; + pna.nextTick(maybeReadMore_, stream, state); + } + } + + function maybeReadMore_(stream, state) { + var len = state.length; + while (!state.reading && !state.flowing && !state.ended && state.length < state.highWaterMark) { + debug('maybeReadMore read 0'); + stream.read(0); + if (len === state.length) + // didn't get any data, stop spinning. + break; + else len = state.length; + } + state.readingMore = false; + } + + // abstract method. to be overridden in specific implementation classes. + // call cb(er, data) where data is <= n in length. + // for virtual (non-string, non-buffer) streams, "length" is somewhat + // arbitrary, and perhaps not very meaningful. + Readable.prototype._read = function(n) { + this.emit('error', new Error('_read() is not implemented')); + }; + + Readable.prototype.pipe = function(dest, pipeOpts) { + var src = this; + var state = this._readableState; + + switch (state.pipesCount) { + case 0: + state.pipes = dest; + break; + case 1: + state.pipes = [state.pipes, dest]; + break; + default: + state.pipes.push(dest); + break; + } + state.pipesCount += 1; + debug('pipe count=%d opts=%j', state.pipesCount, pipeOpts); + + var doEnd = (!pipeOpts || pipeOpts.end !== false) && dest !== process.stdout && dest !== process.stderr; + + var endFn = doEnd ? onend : unpipe; + if (state.endEmitted) pna.nextTick(endFn); + else src.once('end', endFn); + + dest.on('unpipe', onunpipe); + + function onunpipe(readable, unpipeInfo) { + debug('onunpipe'); + if (readable === src) { + if (unpipeInfo && unpipeInfo.hasUnpiped === false) { + unpipeInfo.hasUnpiped = true; + cleanup(); + } + } + } + + function onend() { + debug('onend'); + dest.end(); + } + + // when the dest drains, it reduces the awaitDrain counter + // on the source. This would be more elegant with a .once() + // handler in flow(), but adding and removing repeatedly is + // too slow. + var ondrain = pipeOnDrain(src); + dest.on('drain', ondrain); + + var cleanedUp = false; + + function cleanup() { + debug('cleanup'); + // cleanup event handlers once the pipe is broken + dest.removeListener('close', onclose); + dest.removeListener('finish', onfinish); + dest.removeListener('drain', ondrain); + dest.removeListener('error', onerror); + dest.removeListener('unpipe', onunpipe); + src.removeListener('end', onend); + src.removeListener('end', unpipe); + src.removeListener('data', ondata); + + cleanedUp = true; + + // if the reader is waiting for a drain event from this + // specific writer, then it would cause it to never start + // flowing again. + // So, if this is awaiting a drain, then we just call it now. + // If we don't know, then assume that we are waiting for one. + if (state.awaitDrain && (!dest._writableState || dest._writableState.needDrain)) ondrain(); + } + + // If the user pushes more data while we're writing to dest then we'll end up + // in ondata again. However, we only want to increase awaitDrain once because + // dest will only emit one 'drain' event for the multiple writes. + // => Introduce a guard on increasing awaitDrain. + var increasedAwaitDrain = false; + src.on('data', ondata); + + function ondata(chunk) { + debug('ondata'); + increasedAwaitDrain = false; + var ret = dest.write(chunk); + if (false === ret && !increasedAwaitDrain) { + // If the user unpiped during `dest.write()`, it is possible + // to get stuck in a permanently paused state if that write + // also returned false. + // => Check whether `dest` is still a piping destination. + if ((state.pipesCount === 1 && state.pipes === dest || state.pipesCount > 1 && indexOf(state.pipes, dest) !== -1) && !cleanedUp) { + debug('false write response, pause', src._readableState.awaitDrain); + src._readableState.awaitDrain++; + increasedAwaitDrain = true; + } + src.pause(); + } + } + + // if the dest has an error, then stop piping into it. + // however, don't suppress the throwing behavior for this. + function onerror(er) { + debug('onerror', er); + unpipe(); + dest.removeListener('error', onerror); + if (EElistenerCount(dest, 'error') === 0) dest.emit('error', er); + } + + // Make sure our error handler is attached before userland ones. + prependListener(dest, 'error', onerror); + + // Both close and finish should trigger unpipe, but only once. + function onclose() { + dest.removeListener('finish', onfinish); + unpipe(); + } + dest.once('close', onclose); + + function onfinish() { + debug('onfinish'); + dest.removeListener('close', onclose); + unpipe(); + } + dest.once('finish', onfinish); + + function unpipe() { + debug('unpipe'); + src.unpipe(dest); + } + + // tell the dest that it's being piped to + dest.emit('pipe', src); + + // start the flow if it hasn't been started already. + if (!state.flowing) { + debug('pipe resume'); + src.resume(); + } + + return dest; + }; + + function pipeOnDrain(src) { + return function() { + var state = src._readableState; + debug('pipeOnDrain', state.awaitDrain); + if (state.awaitDrain) state.awaitDrain--; + if (state.awaitDrain === 0 && EElistenerCount(src, 'data')) { + state.flowing = true; + flow(src); + } + }; + } + + Readable.prototype.unpipe = function(dest) { + var state = this._readableState; + var unpipeInfo = { + hasUnpiped: false + }; + + // if we're not piping anywhere, then do nothing. + if (state.pipesCount === 0) return this; + + // just one destination. most common case. + if (state.pipesCount === 1) { + // passed in one, but it's not the right one. + if (dest && dest !== state.pipes) return this; + + if (!dest) dest = state.pipes; + + // got a match. + state.pipes = null; + state.pipesCount = 0; + state.flowing = false; + if (dest) dest.emit('unpipe', this, unpipeInfo); + return this; + } + + // slow case. multiple pipe destinations. + + if (!dest) { + // remove all. + var dests = state.pipes; + var len = state.pipesCount; + state.pipes = null; + state.pipesCount = 0; + state.flowing = false; + + for (var i = 0; i < len; i++) { + dests[i].emit('unpipe', this, unpipeInfo); + } + return this; + } + + // try to find the right one. + var index = indexOf(state.pipes, dest); + if (index === -1) return this; + + state.pipes.splice(index, 1); + state.pipesCount -= 1; + if (state.pipesCount === 1) state.pipes = state.pipes[0]; + + dest.emit('unpipe', this, unpipeInfo); + + return this; + }; + + // set up data events if they are asked for + // Ensure readable listeners eventually get something + Readable.prototype.on = function(ev, fn) { + var res = Stream.prototype.on.call(this, ev, fn); + + if (ev === 'data') { + // Start flowing on next tick if stream isn't explicitly paused + if (this._readableState.flowing !== false) this.resume(); + } else if (ev === 'readable') { + var state = this._readableState; + if (!state.endEmitted && !state.readableListening) { + state.readableListening = state.needReadable = true; + state.emittedReadable = false; + if (!state.reading) { + pna.nextTick(nReadingNextTick, this); + } else if (state.length) { + emitReadable(this); + } + } + } + + return res; + }; + Readable.prototype.addListener = Readable.prototype.on; + + function nReadingNextTick(self) { + debug('readable nexttick read 0'); + self.read(0); + } + + // pause() and resume() are remnants of the legacy readable stream API + // If the user uses them, then switch into old mode. + Readable.prototype.resume = function() { + var state = this._readableState; + if (!state.flowing) { + debug('resume'); + state.flowing = true; + resume(this, state); + } + return this; + }; + + function resume(stream, state) { + if (!state.resumeScheduled) { + state.resumeScheduled = true; + pna.nextTick(resume_, stream, state); + } + } + + function resume_(stream, state) { + if (!state.reading) { + debug('resume read 0'); + stream.read(0); + } + + state.resumeScheduled = false; + state.awaitDrain = 0; + stream.emit('resume'); + flow(stream); + if (state.flowing && !state.reading) stream.read(0); + } + + Readable.prototype.pause = function() { + debug('call pause flowing=%j', this._readableState.flowing); + if (false !== this._readableState.flowing) { + debug('pause'); + this._readableState.flowing = false; + this.emit('pause'); + } + return this; + }; + + function flow(stream) { + var state = stream._readableState; + debug('flow', state.flowing); + while (state.flowing && stream.read() !== null) {} + } + + // wrap an old-style stream as the async data source. + // This is *not* part of the readable stream interface. + // It is an ugly unfortunate mess of history. + Readable.prototype.wrap = function(stream) { + var _this = this; + + var state = this._readableState; + var paused = false; + + stream.on('end', function() { + debug('wrapped end'); + if (state.decoder && !state.ended) { + var chunk = state.decoder.end(); + if (chunk && chunk.length) _this.push(chunk); + } + + _this.push(null); + }); + + stream.on('data', function(chunk) { + debug('wrapped data'); + if (state.decoder) chunk = state.decoder.write(chunk); + + // don't skip over falsy values in objectMode + if (state.objectMode && (chunk === null || chunk === undefined)) return; + else if (!state.objectMode && (!chunk || !chunk.length)) return; + + var ret = _this.push(chunk); + if (!ret) { + paused = true; + stream.pause(); + } + }); + + // proxy all the other methods. + // important when wrapping filters and duplexes. + for (var i in stream) { + if (this[i] === undefined && typeof stream[i] === 'function') { + this[i] = function(method) { + return function() { + return stream[method].apply(stream, arguments); + }; + }(i); + } + } + + // proxy certain important events. + for (var n = 0; n < kProxyEvents.length; n++) { + stream.on(kProxyEvents[n], this.emit.bind(this, kProxyEvents[n])); + } + + // when we try to consume some more bytes, simply unpause the + // underlying stream. + this._read = function(n) { + debug('wrapped _read', n); + if (paused) { + paused = false; + stream.resume(); + } + }; + + return this; + }; + + Object.defineProperty(Readable.prototype, 'readableHighWaterMark', { + // making it explicit this property is not enumerable + // because otherwise some prototype manipulation in + // userland will fail + enumerable: false, + get: function() { + return this._readableState.highWaterMark; + } + }); + + // exposed for testing purposes only. + Readable._fromList = fromList; + + // Pluck off n bytes from an array of buffers. + // Length is the combined lengths of all the buffers in the list. + // This function is designed to be inlinable, so please take care when making + // changes to the function body. + function fromList(n, state) { + // nothing buffered + if (state.length === 0) return null; + + var ret; + if (state.objectMode) ret = state.buffer.shift(); + else if (!n || n >= state.length) { + // read it all, truncate the list + if (state.decoder) ret = state.buffer.join(''); + else if (state.buffer.length === 1) ret = state.buffer.head.data; + else ret = state.buffer.concat(state.length); + state.buffer.clear(); + } else { + // read part of list + ret = fromListPartial(n, state.buffer, state.decoder); + } + + return ret; + } + + // Extracts only enough buffered data to satisfy the amount requested. + // This function is designed to be inlinable, so please take care when making + // changes to the function body. + function fromListPartial(n, list, hasStrings) { + var ret; + if (n < list.head.data.length) { + // slice is the same for buffers and strings + ret = list.head.data.slice(0, n); + list.head.data = list.head.data.slice(n); + } else if (n === list.head.data.length) { + // first chunk is a perfect match + ret = list.shift(); + } else { + // result spans more than one buffer + ret = hasStrings ? copyFromBufferString(n, list) : copyFromBuffer(n, list); + } + return ret; + } + + // Copies a specified amount of characters from the list of buffered data + // chunks. + // This function is designed to be inlinable, so please take care when making + // changes to the function body. + function copyFromBufferString(n, list) { + var p = list.head; + var c = 1; + var ret = p.data; + n -= ret.length; + while (p = p.next) { + var str = p.data; + var nb = n > str.length ? str.length : n; + if (nb === str.length) ret += str; + else ret += str.slice(0, n); + n -= nb; + if (n === 0) { + if (nb === str.length) { + ++c; + if (p.next) list.head = p.next; + else list.head = list.tail = null; + } else { + list.head = p; + p.data = str.slice(nb); + } + break; + } + ++c; + } + list.length -= c; + return ret; + } + + // Copies a specified amount of bytes from the list of buffered data chunks. + // This function is designed to be inlinable, so please take care when making + // changes to the function body. + function copyFromBuffer(n, list) { + var ret = Buffer.allocUnsafe(n); + var p = list.head; + var c = 1; + p.data.copy(ret); + n -= p.data.length; + while (p = p.next) { + var buf = p.data; + var nb = n > buf.length ? buf.length : n; + buf.copy(ret, ret.length - n, 0, nb); + n -= nb; + if (n === 0) { + if (nb === buf.length) { + ++c; + if (p.next) list.head = p.next; + else list.head = list.tail = null; + } else { + list.head = p; + p.data = buf.slice(nb); + } + break; + } + ++c; + } + list.length -= c; + return ret; + } + + function endReadable(stream) { + var state = stream._readableState; + + // If we get here before consuming all the bytes, then that is a + // bug in node. Should never happen. + if (state.length > 0) throw new Error('"endReadable()" called on non-empty stream'); + + if (!state.endEmitted) { + state.ended = true; + pna.nextTick(endReadableNT, state, stream); + } + } + + function endReadableNT(state, stream) { + // Check that we didn't get one last unshift. + if (!state.endEmitted && state.length === 0) { + state.endEmitted = true; + stream.readable = false; + stream.emit('end'); + } + } + + function indexOf(xs, x) { + for (var i = 0, l = xs.length; i < l; i++) { + if (xs[i] === x) return i; + } + return -1; + } + }).call(this, require('_process'), typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) + }, { + "./_stream_duplex": 98, + "./internal/streams/BufferList": 103, + "./internal/streams/destroy": 104, + "./internal/streams/stream": 105, + "_process": 92, + "core-util-is": 13, + "events": 78, + "inherits": 80, + "isarray": 82, + "process-nextick-args": 91, + "safe-buffer": 108, + "string_decoder/": 110, + "util": 11 + }], + 101: [function(require, module, exports) { + // Copyright Joyent, Inc. and other Node contributors. + // + // Permission is hereby granted, free of charge, to any person obtaining a + // copy of this software and associated documentation files (the + // "Software"), to deal in the Software without restriction, including + // without limitation the rights to use, copy, modify, merge, publish, + // distribute, sublicense, and/or sell copies of the Software, and to permit + // persons to whom the Software is furnished to do so, subject to the + // following conditions: + // + // The above copyright notice and this permission notice shall be included + // in all copies or substantial portions of the Software. + // + // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN + // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE + // USE OR OTHER DEALINGS IN THE SOFTWARE. + + // a transform stream is a readable/writable stream where you do + // something with the data. Sometimes it's called a "filter", + // but that's not a great name for it, since that implies a thing where + // some bits pass through, and others are simply ignored. (That would + // be a valid example of a transform, of course.) + // + // While the output is causally related to the input, it's not a + // necessarily symmetric or synchronous transformation. For example, + // a zlib stream might take multiple plain-text writes(), and then + // emit a single compressed chunk some time in the future. + // + // Here's how this works: + // + // The Transform stream has all the aspects of the readable and writable + // stream classes. When you write(chunk), that calls _write(chunk,cb) + // internally, and returns false if there's a lot of pending writes + // buffered up. When you call read(), that calls _read(n) until + // there's enough pending readable data buffered up. + // + // In a transform stream, the written data is placed in a buffer. When + // _read(n) is called, it transforms the queued up data, calling the + // buffered _write cb's as it consumes chunks. If consuming a single + // written chunk would result in multiple output chunks, then the first + // outputted bit calls the readcb, and subsequent chunks just go into + // the read buffer, and will cause it to emit 'readable' if necessary. + // + // This way, back-pressure is actually determined by the reading side, + // since _read has to be called to start processing a new chunk. However, + // a pathological inflate type of transform can cause excessive buffering + // here. For example, imagine a stream where every byte of input is + // interpreted as an integer from 0-255, and then results in that many + // bytes of output. Writing the 4 bytes {ff,ff,ff,ff} would result in + // 1kb of data being output. In this case, you could write a very small + // amount of input, and end up with a very large amount of output. In + // such a pathological inflating mechanism, there'd be no way to tell + // the system to stop doing the transform. A single 4MB write could + // cause the system to run out of memory. + // + // However, even in such a pathological case, only a single written chunk + // would be consumed, and then the rest would wait (un-transformed) until + // the results of the previous transformed chunk were consumed. + + 'use strict'; + + module.exports = Transform; + + var Duplex = require('./_stream_duplex'); + + /**/ + var util = require('core-util-is'); + util.inherits = require('inherits'); + /**/ + + util.inherits(Transform, Duplex); + + function afterTransform(er, data) { + var ts = this._transformState; + ts.transforming = false; + + var cb = ts.writecb; + + if (!cb) { + return this.emit('error', new Error('write callback called multiple times')); + } + + ts.writechunk = null; + ts.writecb = null; + + if (data != null) // single equals check for both `null` and `undefined` + this.push(data); + + cb(er); + + var rs = this._readableState; + rs.reading = false; + if (rs.needReadable || rs.length < rs.highWaterMark) { + this._read(rs.highWaterMark); + } + } + + function Transform(options) { + if (!(this instanceof Transform)) return new Transform(options); + + Duplex.call(this, options); + + this._transformState = { + afterTransform: afterTransform.bind(this), + needTransform: false, + transforming: false, + writecb: null, + writechunk: null, + writeencoding: null + }; + + // start out asking for a readable event once data is transformed. + this._readableState.needReadable = true; + + // we have implemented the _read method, and done the other things + // that Readable wants before the first _read call, so unset the + // sync guard flag. + this._readableState.sync = false; + + if (options) { + if (typeof options.transform === 'function') this._transform = options.transform; + + if (typeof options.flush === 'function') this._flush = options.flush; + } + + // When the writable side finishes, then flush out anything remaining. + this.on('prefinish', prefinish); + } + + function prefinish() { + var _this = this; + + if (typeof this._flush === 'function') { + this._flush(function(er, data) { + done(_this, er, data); + }); + } else { + done(this, null, null); + } + } + + Transform.prototype.push = function(chunk, encoding) { + this._transformState.needTransform = false; + return Duplex.prototype.push.call(this, chunk, encoding); + }; + + // This is the part where you do stuff! + // override this function in implementation classes. + // 'chunk' is an input chunk. + // + // Call `push(newChunk)` to pass along transformed output + // to the readable side. You may call 'push' zero or more times. + // + // Call `cb(err)` when you are done with this chunk. If you pass + // an error, then that'll put the hurt on the whole operation. If you + // never call cb(), then you'll never get another chunk. + Transform.prototype._transform = function(chunk, encoding, cb) { + throw new Error('_transform() is not implemented'); + }; + + Transform.prototype._write = function(chunk, encoding, cb) { + var ts = this._transformState; + ts.writecb = cb; + ts.writechunk = chunk; + ts.writeencoding = encoding; + if (!ts.transforming) { + var rs = this._readableState; + if (ts.needTransform || rs.needReadable || rs.length < rs.highWaterMark) this._read(rs.highWaterMark); + } + }; + + // Doesn't matter what the args are here. + // _transform does all the work. + // That we got here means that the readable side wants more data. + Transform.prototype._read = function(n) { + var ts = this._transformState; + + if (ts.writechunk !== null && ts.writecb && !ts.transforming) { + ts.transforming = true; + this._transform(ts.writechunk, ts.writeencoding, ts.afterTransform); + } else { + // mark that we need a transform, so that any data that comes in + // will get processed, now that we've asked for it. + ts.needTransform = true; + } + }; + + Transform.prototype._destroy = function(err, cb) { + var _this2 = this; + + Duplex.prototype._destroy.call(this, err, function(err2) { + cb(err2); + _this2.emit('close'); + }); + }; + + function done(stream, er, data) { + if (er) return stream.emit('error', er); + + if (data != null) // single equals check for both `null` and `undefined` + stream.push(data); + + // if there's nothing in the write buffer, then that means + // that nothing more will ever be provided + if (stream._writableState.length) throw new Error('Calling transform done when ws.length != 0'); + + if (stream._transformState.transforming) throw new Error('Calling transform done when still transforming'); + + return stream.push(null); + } + }, { + "./_stream_duplex": 98, + "core-util-is": 13, + "inherits": 80 + }], + 102: [function(require, module, exports) { + (function(process, global, setImmediate) { + // Copyright Joyent, Inc. and other Node contributors. + // + // Permission is hereby granted, free of charge, to any person obtaining a + // copy of this software and associated documentation files (the + // "Software"), to deal in the Software without restriction, including + // without limitation the rights to use, copy, modify, merge, publish, + // distribute, sublicense, and/or sell copies of the Software, and to permit + // persons to whom the Software is furnished to do so, subject to the + // following conditions: + // + // The above copyright notice and this permission notice shall be included + // in all copies or substantial portions of the Software. + // + // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN + // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE + // USE OR OTHER DEALINGS IN THE SOFTWARE. + + // A bit simpler than readable streams. + // Implement an async ._write(chunk, encoding, cb), and it'll handle all + // the drain event emission and buffering. + + 'use strict'; + + /**/ + + var pna = require('process-nextick-args'); + /**/ + + module.exports = Writable; + + /* */ + function WriteReq(chunk, encoding, cb) { + this.chunk = chunk; + this.encoding = encoding; + this.callback = cb; + this.next = null; + } + + // It seems a linked list but it is not + // there will be only 2 of these for each stream + function CorkedRequest(state) { + var _this = this; + + this.next = null; + this.entry = null; + this.finish = function() { + onCorkedFinish(_this, state); + }; + } + /* */ + + /**/ + var asyncWrite = !process.browser && ['v0.10', 'v0.9.'].indexOf(process.version.slice(0, 5)) > -1 ? setImmediate : pna.nextTick; + /**/ + + /**/ + var Duplex; + /**/ + + Writable.WritableState = WritableState; + + /**/ + var util = require('core-util-is'); + util.inherits = require('inherits'); + /**/ + + /**/ + var internalUtil = { + deprecate: require('util-deprecate') + }; + /**/ + + /**/ + var Stream = require('./internal/streams/stream'); + /**/ + + /**/ + + var Buffer = require('safe-buffer').Buffer; + var OurUint8Array = global.Uint8Array || function() {}; + + function _uint8ArrayToBuffer(chunk) { + return Buffer.from(chunk); + } + + function _isUint8Array(obj) { + return Buffer.isBuffer(obj) || obj instanceof OurUint8Array; + } + + /**/ + + var destroyImpl = require('./internal/streams/destroy'); + + util.inherits(Writable, Stream); + + function nop() {} + + function WritableState(options, stream) { + Duplex = Duplex || require('./_stream_duplex'); + + options = options || {}; + + // Duplex streams are both readable and writable, but share + // the same options object. + // However, some cases require setting options to different + // values for the readable and the writable sides of the duplex stream. + // These options can be provided separately as readableXXX and writableXXX. + var isDuplex = stream instanceof Duplex; + + // object stream flag to indicate whether or not this stream + // contains buffers or objects. + this.objectMode = !!options.objectMode; + + if (isDuplex) this.objectMode = this.objectMode || !!options.writableObjectMode; + + // the point at which write() starts returning false + // Note: 0 is a valid value, means that we always return false if + // the entire buffer is not flushed immediately on write() + var hwm = options.highWaterMark; + var writableHwm = options.writableHighWaterMark; + var defaultHwm = this.objectMode ? 16 : 16 * 1024; + + if (hwm || hwm === 0) this.highWaterMark = hwm; + else if (isDuplex && (writableHwm || writableHwm === 0)) this.highWaterMark = writableHwm; + else this.highWaterMark = defaultHwm; + + // cast to ints. + this.highWaterMark = Math.floor(this.highWaterMark); + + // if _final has been called + this.finalCalled = false; + + // drain event flag. + this.needDrain = false; + // at the start of calling end() + this.ending = false; + // when end() has been called, and returned + this.ended = false; + // when 'finish' is emitted + this.finished = false; + + // has it been destroyed + this.destroyed = false; + + // should we decode strings into buffers before passing to _write? + // this is here so that some node-core streams can optimize string + // handling at a lower level. + var noDecode = options.decodeStrings === false; + this.decodeStrings = !noDecode; + + // Crypto is kind of old and crusty. Historically, its default string + // encoding is 'binary' so we have to make this configurable. + // Everything else in the universe uses 'utf8', though. + this.defaultEncoding = options.defaultEncoding || 'utf8'; + + // not an actual buffer we keep track of, but a measurement + // of how much we're waiting to get pushed to some underlying + // socket or file. + this.length = 0; + + // a flag to see when we're in the middle of a write. + this.writing = false; + + // when true all writes will be buffered until .uncork() call + this.corked = 0; + + // a flag to be able to tell if the onwrite cb is called immediately, + // or on a later tick. We set this to true at first, because any + // actions that shouldn't happen until "later" should generally also + // not happen before the first write call. + this.sync = true; + + // a flag to know if we're processing previously buffered items, which + // may call the _write() callback in the same tick, so that we don't + // end up in an overlapped onwrite situation. + this.bufferProcessing = false; + + // the callback that's passed to _write(chunk,cb) + this.onwrite = function(er) { + onwrite(stream, er); + }; + + // the callback that the user supplies to write(chunk,encoding,cb) + this.writecb = null; + + // the amount that is being written when _write is called. + this.writelen = 0; + + this.bufferedRequest = null; + this.lastBufferedRequest = null; + + // number of pending user-supplied write callbacks + // this must be 0 before 'finish' can be emitted + this.pendingcb = 0; + + // emit prefinish if the only thing we're waiting for is _write cbs + // This is relevant for synchronous Transform streams + this.prefinished = false; + + // True if the error was already emitted and should not be thrown again + this.errorEmitted = false; + + // count buffered requests + this.bufferedRequestCount = 0; + + // allocate the first CorkedRequest, there is always + // one allocated and free to use, and we maintain at most two + this.corkedRequestsFree = new CorkedRequest(this); + } + + WritableState.prototype.getBuffer = function getBuffer() { + var current = this.bufferedRequest; + var out = []; + while (current) { + out.push(current); + current = current.next; + } + return out; + }; + + (function() { + try { + Object.defineProperty(WritableState.prototype, 'buffer', { + get: internalUtil.deprecate(function() { + return this.getBuffer(); + }, '_writableState.buffer is deprecated. Use _writableState.getBuffer ' + 'instead.', 'DEP0003') + }); + } catch (_) {} + })(); + + // Test _writableState for inheritance to account for Duplex streams, + // whose prototype chain only points to Readable. + var realHasInstance; + if (typeof Symbol === 'function' && Symbol.hasInstance && typeof Function.prototype[Symbol.hasInstance] === 'function') { + realHasInstance = Function.prototype[Symbol.hasInstance]; + Object.defineProperty(Writable, Symbol.hasInstance, { + value: function(object) { + if (realHasInstance.call(this, object)) return true; + if (this !== Writable) return false; + + return object && object._writableState instanceof WritableState; + } + }); + } else { + realHasInstance = function(object) { + return object instanceof this; + }; + } + + function Writable(options) { + Duplex = Duplex || require('./_stream_duplex'); + + // Writable ctor is applied to Duplexes, too. + // `realHasInstance` is necessary because using plain `instanceof` + // would return false, as no `_writableState` property is attached. + + // Trying to use the custom `instanceof` for Writable here will also break the + // Node.js LazyTransform implementation, which has a non-trivial getter for + // `_writableState` that would lead to infinite recursion. + if (!realHasInstance.call(Writable, this) && !(this instanceof Duplex)) { + return new Writable(options); + } + + this._writableState = new WritableState(options, this); + + // legacy. + this.writable = true; + + if (options) { + if (typeof options.write === 'function') this._write = options.write; + + if (typeof options.writev === 'function') this._writev = options.writev; + + if (typeof options.destroy === 'function') this._destroy = options.destroy; + + if (typeof options.final === 'function') this._final = options.final; + } + + Stream.call(this); + } + + // Otherwise people can pipe Writable streams, which is just wrong. + Writable.prototype.pipe = function() { + this.emit('error', new Error('Cannot pipe, not readable')); + }; + + function writeAfterEnd(stream, cb) { + var er = new Error('write after end'); + // TODO: defer error events consistently everywhere, not just the cb + stream.emit('error', er); + pna.nextTick(cb, er); + } + + // Checks that a user-supplied chunk is valid, especially for the particular + // mode the stream is in. Currently this means that `null` is never accepted + // and undefined/non-string values are only allowed in object mode. + function validChunk(stream, state, chunk, cb) { + var valid = true; + var er = false; + + if (chunk === null) { + er = new TypeError('May not write null values to stream'); + } else if (typeof chunk !== 'string' && chunk !== undefined && !state.objectMode) { + er = new TypeError('Invalid non-string/buffer chunk'); + } + if (er) { + stream.emit('error', er); + pna.nextTick(cb, er); + valid = false; + } + return valid; + } + + Writable.prototype.write = function(chunk, encoding, cb) { + var state = this._writableState; + var ret = false; + var isBuf = !state.objectMode && _isUint8Array(chunk); + + if (isBuf && !Buffer.isBuffer(chunk)) { + chunk = _uint8ArrayToBuffer(chunk); + } + + if (typeof encoding === 'function') { + cb = encoding; + encoding = null; + } + + if (isBuf) encoding = 'buffer'; + else if (!encoding) encoding = state.defaultEncoding; + + if (typeof cb !== 'function') cb = nop; + + if (state.ended) writeAfterEnd(this, cb); + else if (isBuf || validChunk(this, state, chunk, cb)) { + state.pendingcb++; + ret = writeOrBuffer(this, state, isBuf, chunk, encoding, cb); + } + + return ret; + }; + + Writable.prototype.cork = function() { + var state = this._writableState; + + state.corked++; + }; + + Writable.prototype.uncork = function() { + var state = this._writableState; + + if (state.corked) { + state.corked--; + + if (!state.writing && !state.corked && !state.finished && !state.bufferProcessing && state.bufferedRequest) clearBuffer(this, state); + } + }; + + Writable.prototype.setDefaultEncoding = function setDefaultEncoding(encoding) { + // node::ParseEncoding() requires lower case. + if (typeof encoding === 'string') encoding = encoding.toLowerCase(); + if (!(['hex', 'utf8', 'utf-8', 'ascii', 'binary', 'base64', 'ucs2', 'ucs-2', 'utf16le', 'utf-16le', 'raw'].indexOf((encoding + '').toLowerCase()) > -1)) throw new TypeError('Unknown encoding: ' + encoding); + this._writableState.defaultEncoding = encoding; + return this; + }; + + function decodeChunk(state, chunk, encoding) { + if (!state.objectMode && state.decodeStrings !== false && typeof chunk === 'string') { + chunk = Buffer.from(chunk, encoding); + } + return chunk; + } + + Object.defineProperty(Writable.prototype, 'writableHighWaterMark', { + // making it explicit this property is not enumerable + // because otherwise some prototype manipulation in + // userland will fail + enumerable: false, + get: function() { + return this._writableState.highWaterMark; + } + }); + + // if we're already writing something, then just put this + // in the queue, and wait our turn. Otherwise, call _write + // If we return false, then we need a drain event, so set that flag. + function writeOrBuffer(stream, state, isBuf, chunk, encoding, cb) { + if (!isBuf) { + var newChunk = decodeChunk(state, chunk, encoding); + if (chunk !== newChunk) { + isBuf = true; + encoding = 'buffer'; + chunk = newChunk; + } + } + var len = state.objectMode ? 1 : chunk.length; + + state.length += len; + + var ret = state.length < state.highWaterMark; + // we must ensure that previous needDrain will not be reset to false. + if (!ret) state.needDrain = true; + + if (state.writing || state.corked) { + var last = state.lastBufferedRequest; + state.lastBufferedRequest = { + chunk: chunk, + encoding: encoding, + isBuf: isBuf, + callback: cb, + next: null + }; + if (last) { + last.next = state.lastBufferedRequest; + } else { + state.bufferedRequest = state.lastBufferedRequest; + } + state.bufferedRequestCount += 1; + } else { + doWrite(stream, state, false, len, chunk, encoding, cb); + } + + return ret; + } + + function doWrite(stream, state, writev, len, chunk, encoding, cb) { + state.writelen = len; + state.writecb = cb; + state.writing = true; + state.sync = true; + if (writev) stream._writev(chunk, state.onwrite); + else stream._write(chunk, encoding, state.onwrite); + state.sync = false; + } + + function onwriteError(stream, state, sync, er, cb) { + --state.pendingcb; + + if (sync) { + // defer the callback if we are being called synchronously + // to avoid piling up things on the stack + pna.nextTick(cb, er); + // this can emit finish, and it will always happen + // after error + pna.nextTick(finishMaybe, stream, state); + stream._writableState.errorEmitted = true; + stream.emit('error', er); + } else { + // the caller expect this to happen before if + // it is async + cb(er); + stream._writableState.errorEmitted = true; + stream.emit('error', er); + // this can emit finish, but finish must + // always follow error + finishMaybe(stream, state); + } + } + + function onwriteStateUpdate(state) { + state.writing = false; + state.writecb = null; + state.length -= state.writelen; + state.writelen = 0; + } + + function onwrite(stream, er) { + var state = stream._writableState; + var sync = state.sync; + var cb = state.writecb; + + onwriteStateUpdate(state); + + if (er) onwriteError(stream, state, sync, er, cb); + else { + // Check if we're actually ready to finish, but don't emit yet + var finished = needFinish(state); + + if (!finished && !state.corked && !state.bufferProcessing && state.bufferedRequest) { + clearBuffer(stream, state); + } + + if (sync) { + /**/ + asyncWrite(afterWrite, stream, state, finished, cb); + /**/ + } else { + afterWrite(stream, state, finished, cb); + } + } + } + + function afterWrite(stream, state, finished, cb) { + if (!finished) onwriteDrain(stream, state); + state.pendingcb--; + cb(); + finishMaybe(stream, state); + } + + // Must force callback to be called on nextTick, so that we don't + // emit 'drain' before the write() consumer gets the 'false' return + // value, and has a chance to attach a 'drain' listener. + function onwriteDrain(stream, state) { + if (state.length === 0 && state.needDrain) { + state.needDrain = false; + stream.emit('drain'); + } + } + + // if there's something in the buffer waiting, then process it + function clearBuffer(stream, state) { + state.bufferProcessing = true; + var entry = state.bufferedRequest; + + if (stream._writev && entry && entry.next) { + // Fast case, write everything using _writev() + var l = state.bufferedRequestCount; + var buffer = new Array(l); + var holder = state.corkedRequestsFree; + holder.entry = entry; + + var count = 0; + var allBuffers = true; + while (entry) { + buffer[count] = entry; + if (!entry.isBuf) allBuffers = false; + entry = entry.next; + count += 1; + } + buffer.allBuffers = allBuffers; + + doWrite(stream, state, true, state.length, buffer, '', holder.finish); + + // doWrite is almost always async, defer these to save a bit of time + // as the hot path ends with doWrite + state.pendingcb++; + state.lastBufferedRequest = null; + if (holder.next) { + state.corkedRequestsFree = holder.next; + holder.next = null; + } else { + state.corkedRequestsFree = new CorkedRequest(state); + } + state.bufferedRequestCount = 0; + } else { + // Slow case, write chunks one-by-one + while (entry) { + var chunk = entry.chunk; + var encoding = entry.encoding; + var cb = entry.callback; + var len = state.objectMode ? 1 : chunk.length; + + doWrite(stream, state, false, len, chunk, encoding, cb); + entry = entry.next; + state.bufferedRequestCount--; + // if we didn't call the onwrite immediately, then + // it means that we need to wait until it does. + // also, that means that the chunk and cb are currently + // being processed, so move the buffer counter past them. + if (state.writing) { + break; + } + } + + if (entry === null) state.lastBufferedRequest = null; + } + + state.bufferedRequest = entry; + state.bufferProcessing = false; + } + + Writable.prototype._write = function(chunk, encoding, cb) { + cb(new Error('_write() is not implemented')); + }; + + Writable.prototype._writev = null; + + Writable.prototype.end = function(chunk, encoding, cb) { + var state = this._writableState; + + if (typeof chunk === 'function') { + cb = chunk; + chunk = null; + encoding = null; + } else if (typeof encoding === 'function') { + cb = encoding; + encoding = null; + } + + if (chunk !== null && chunk !== undefined) this.write(chunk, encoding); + + // .end() fully uncorks + if (state.corked) { + state.corked = 1; + this.uncork(); + } + + // ignore unnecessary end() calls. + if (!state.ending && !state.finished) endWritable(this, state, cb); + }; + + function needFinish(state) { + return state.ending && state.length === 0 && state.bufferedRequest === null && !state.finished && !state.writing; + } + + function callFinal(stream, state) { + stream._final(function(err) { + state.pendingcb--; + if (err) { + stream.emit('error', err); + } + state.prefinished = true; + stream.emit('prefinish'); + finishMaybe(stream, state); + }); + } + + function prefinish(stream, state) { + if (!state.prefinished && !state.finalCalled) { + if (typeof stream._final === 'function') { + state.pendingcb++; + state.finalCalled = true; + pna.nextTick(callFinal, stream, state); + } else { + state.prefinished = true; + stream.emit('prefinish'); + } + } + } + + function finishMaybe(stream, state) { + var need = needFinish(state); + if (need) { + prefinish(stream, state); + if (state.pendingcb === 0) { + state.finished = true; + stream.emit('finish'); + } + } + return need; + } + + function endWritable(stream, state, cb) { + state.ending = true; + finishMaybe(stream, state); + if (cb) { + if (state.finished) pna.nextTick(cb); + else stream.once('finish', cb); + } + state.ended = true; + stream.writable = false; + } + + function onCorkedFinish(corkReq, state, err) { + var entry = corkReq.entry; + corkReq.entry = null; + while (entry) { + var cb = entry.callback; + state.pendingcb--; + cb(err); + entry = entry.next; + } + if (state.corkedRequestsFree) { + state.corkedRequestsFree.next = corkReq; + } else { + state.corkedRequestsFree = corkReq; + } + } + + Object.defineProperty(Writable.prototype, 'destroyed', { + get: function() { + if (this._writableState === undefined) { + return false; + } + return this._writableState.destroyed; + }, + set: function(value) { + // we ignore the value if the stream + // has not been initialized yet + if (!this._writableState) { + return; + } + + // backward compatibility, the user is explicitly + // managing destroyed + this._writableState.destroyed = value; + } + }); + + Writable.prototype.destroy = destroyImpl.destroy; + Writable.prototype._undestroy = destroyImpl.undestroy; + Writable.prototype._destroy = function(err, cb) { + this.end(); + cb(err); + }; + }).call(this, require('_process'), typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}, require("timers").setImmediate) + }, { + "./_stream_duplex": 98, + "./internal/streams/destroy": 104, + "./internal/streams/stream": 105, + "_process": 92, + "core-util-is": 13, + "inherits": 80, + "process-nextick-args": 91, + "safe-buffer": 108, + "timers": 111, + "util-deprecate": 114 + }], + 103: [function(require, module, exports) { + 'use strict'; + + function _classCallCheck(instance, Constructor) { + if (!(instance instanceof Constructor)) { + throw new TypeError("Cannot call a class as a function"); + } + } + + var Buffer = require('safe-buffer').Buffer; + var util = require('util'); + + function copyBuffer(src, target, offset) { + src.copy(target, offset); + } + + module.exports = function() { + function BufferList() { + _classCallCheck(this, BufferList); + + this.head = null; + this.tail = null; + this.length = 0; + } + + BufferList.prototype.push = function push(v) { + var entry = { + data: v, + next: null + }; + if (this.length > 0) this.tail.next = entry; + else this.head = entry; + this.tail = entry; + ++this.length; + }; + + BufferList.prototype.unshift = function unshift(v) { + var entry = { + data: v, + next: this.head + }; + if (this.length === 0) this.tail = entry; + this.head = entry; + ++this.length; + }; + + BufferList.prototype.shift = function shift() { + if (this.length === 0) return; + var ret = this.head.data; + if (this.length === 1) this.head = this.tail = null; + else this.head = this.head.next; + --this.length; + return ret; + }; + + BufferList.prototype.clear = function clear() { + this.head = this.tail = null; + this.length = 0; + }; + + BufferList.prototype.join = function join(s) { + if (this.length === 0) return ''; + var p = this.head; + var ret = '' + p.data; + while (p = p.next) { + ret += s + p.data; + } + return ret; + }; + + BufferList.prototype.concat = function concat(n) { + if (this.length === 0) return Buffer.alloc(0); + if (this.length === 1) return this.head.data; + var ret = Buffer.allocUnsafe(n >>> 0); + var p = this.head; + var i = 0; + while (p) { + copyBuffer(p.data, ret, i); + i += p.data.length; + p = p.next; + } + return ret; + }; + + return BufferList; + }(); + + if (util && util.inspect && util.inspect.custom) { + module.exports.prototype[util.inspect.custom] = function() { + var obj = util.inspect({ + length: this.length + }); + return this.constructor.name + ' ' + obj; + }; + } + }, { + "safe-buffer": 108, + "util": 11 + }], + 104: [function(require, module, exports) { + 'use strict'; + + /**/ + + var pna = require('process-nextick-args'); + /**/ + + // undocumented cb() API, needed for core, not for public API + function destroy(err, cb) { + var _this = this; + + var readableDestroyed = this._readableState && this._readableState.destroyed; + var writableDestroyed = this._writableState && this._writableState.destroyed; + + if (readableDestroyed || writableDestroyed) { + if (cb) { + cb(err); + } else if (err && (!this._writableState || !this._writableState.errorEmitted)) { + pna.nextTick(emitErrorNT, this, err); + } + return this; + } + + // we set destroyed to true before firing error callbacks in order + // to make it re-entrance safe in case destroy() is called within callbacks + + if (this._readableState) { + this._readableState.destroyed = true; + } + + // if this is a duplex stream mark the writable part as destroyed as well + if (this._writableState) { + this._writableState.destroyed = true; + } + + this._destroy(err || null, function(err) { + if (!cb && err) { + pna.nextTick(emitErrorNT, _this, err); + if (_this._writableState) { + _this._writableState.errorEmitted = true; + } + } else if (cb) { + cb(err); + } + }); + + return this; + } + + function undestroy() { + if (this._readableState) { + this._readableState.destroyed = false; + this._readableState.reading = false; + this._readableState.ended = false; + this._readableState.endEmitted = false; + } + + if (this._writableState) { + this._writableState.destroyed = false; + this._writableState.ended = false; + this._writableState.ending = false; + this._writableState.finished = false; + this._writableState.errorEmitted = false; + } + } + + function emitErrorNT(self, err) { + self.emit('error', err); + } + + module.exports = { + destroy: destroy, + undestroy: undestroy + }; + }, { + "process-nextick-args": 91 + }], + 105: [function(require, module, exports) { + module.exports = require('events').EventEmitter; + + }, { + "events": 78 + }], + 106: [function(require, module, exports) { + exports = module.exports = require('./lib/_stream_readable.js'); + exports.Stream = exports; + exports.Readable = exports; + exports.Writable = require('./lib/_stream_writable.js'); + exports.Duplex = require('./lib/_stream_duplex.js'); + exports.Transform = require('./lib/_stream_transform.js'); + exports.PassThrough = require('./lib/_stream_passthrough.js'); + + }, { + "./lib/_stream_duplex.js": 98, + "./lib/_stream_passthrough.js": 99, + "./lib/_stream_readable.js": 100, + "./lib/_stream_transform.js": 101, + "./lib/_stream_writable.js": 102 + }], + 107: [function(require, module, exports) { + 'use strict' + + function ReInterval(callback, interval, args) { + var self = this; + + this._callback = callback; + this._args = args; + + this._interval = setInterval(callback, interval, this._args); + + this.reschedule = function(interval) { + // if no interval entered, use the interval passed in on creation + if (!interval) + interval = self._interval; + + if (self._interval) + clearInterval(self._interval); + self._interval = setInterval(self._callback, interval, self._args); + }; + + this.clear = function() { + if (self._interval) { + clearInterval(self._interval); + self._interval = undefined; + } + }; + + this.destroy = function() { + if (self._interval) { + clearInterval(self._interval); + } + self._callback = undefined; + self._interval = undefined; + self._args = undefined; + }; + } + + function reInterval() { + if (typeof arguments[0] !== 'function') + throw new Error('callback needed'); + if (typeof arguments[1] !== 'number') + throw new Error('interval needed'); + + var args; + + if (arguments.length > 0) { + args = new Array(arguments.length - 2); + + for (var i = 0; i < args.length; i++) { + args[i] = arguments[i + 2]; + } + } + + return new ReInterval(arguments[0], arguments[1], args); + } + + module.exports = reInterval; + + }, {}], + 108: [function(require, module, exports) { + /* eslint-disable node/no-deprecated-api */ + var buffer = require('buffer') + var Buffer = buffer.Buffer + + // alternative to using Object.keys for old browsers + function copyProps(src, dst) { + for (var key in src) { + dst[key] = src[key] + } + } + if (Buffer.from && Buffer.alloc && Buffer.allocUnsafe && Buffer.allocUnsafeSlow) { + module.exports = buffer + } else { + // Copy properties from require('buffer') + copyProps(buffer, exports) + exports.Buffer = SafeBuffer + } + + function SafeBuffer(arg, encodingOrOffset, length) { + return Buffer(arg, encodingOrOffset, length) + } + + // Copy static methods from Buffer + copyProps(Buffer, SafeBuffer) + + SafeBuffer.from = function(arg, encodingOrOffset, length) { + if (typeof arg === 'number') { + throw new TypeError('Argument must not be a number') + } + return Buffer(arg, encodingOrOffset, length) + } + + SafeBuffer.alloc = function(size, fill, encoding) { + if (typeof size !== 'number') { + throw new TypeError('Argument must be a number') + } + var buf = Buffer(size) + if (fill !== undefined) { + if (typeof encoding === 'string') { + buf.fill(fill, encoding) + } else { + buf.fill(fill) + } + } else { + buf.fill(0) + } + return buf + } + + SafeBuffer.allocUnsafe = function(size) { + if (typeof size !== 'number') { + throw new TypeError('Argument must be a number') + } + return Buffer(size) + } + + SafeBuffer.allocUnsafeSlow = function(size) { + if (typeof size !== 'number') { + throw new TypeError('Argument must be a number') + } + return buffer.SlowBuffer(size) + } + + }, { + "buffer": 12 + }], + 109: [function(require, module, exports) { + module.exports = shift + + function shift(stream) { + var rs = stream._readableState + if (!rs) return null + return rs.objectMode ? stream.read() : stream.read(getStateLength(rs)) + } + + function getStateLength(state) { + if (state.buffer.length) { + // Since node 6.3.0 state.buffer is a BufferList not an array + if (state.buffer.head) { + return state.buffer.head.data.length + } + + return state.buffer[0].length + } + + return state.length + } + + }, {}], + 110: [function(require, module, exports) { + // Copyright Joyent, Inc. and other Node contributors. + // + // Permission is hereby granted, free of charge, to any person obtaining a + // copy of this software and associated documentation files (the + // "Software"), to deal in the Software without restriction, including + // without limitation the rights to use, copy, modify, merge, publish, + // distribute, sublicense, and/or sell copies of the Software, and to permit + // persons to whom the Software is furnished to do so, subject to the + // following conditions: + // + // The above copyright notice and this permission notice shall be included + // in all copies or substantial portions of the Software. + // + // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN + // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE + // USE OR OTHER DEALINGS IN THE SOFTWARE. + + 'use strict'; + + /**/ + + var Buffer = require('safe-buffer').Buffer; + /**/ + + var isEncoding = Buffer.isEncoding || function(encoding) { + encoding = '' + encoding; + switch (encoding && encoding.toLowerCase()) { + case 'hex': + case 'utf8': + case 'utf-8': + case 'ascii': + case 'binary': + case 'base64': + case 'ucs2': + case 'ucs-2': + case 'utf16le': + case 'utf-16le': + case 'raw': + return true; + default: + return false; + } + }; + + function _normalizeEncoding(enc) { + if (!enc) return 'utf8'; + var retried; + while (true) { + switch (enc) { + case 'utf8': + case 'utf-8': + return 'utf8'; + case 'ucs2': + case 'ucs-2': + case 'utf16le': + case 'utf-16le': + return 'utf16le'; + case 'latin1': + case 'binary': + return 'latin1'; + case 'base64': + case 'ascii': + case 'hex': + return enc; + default: + if (retried) return; // undefined + enc = ('' + enc).toLowerCase(); + retried = true; + } + } + }; + + // Do not cache `Buffer.isEncoding` when checking encoding names as some + // modules monkey-patch it to support additional encodings + function normalizeEncoding(enc) { + var nenc = _normalizeEncoding(enc); + if (typeof nenc !== 'string' && (Buffer.isEncoding === isEncoding || !isEncoding(enc))) throw new Error('Unknown encoding: ' + enc); + return nenc || enc; + } + + // StringDecoder provides an interface for efficiently splitting a series of + // buffers into a series of JS strings without breaking apart multi-byte + // characters. + exports.StringDecoder = StringDecoder; + + function StringDecoder(encoding) { + this.encoding = normalizeEncoding(encoding); + var nb; + switch (this.encoding) { + case 'utf16le': + this.text = utf16Text; + this.end = utf16End; + nb = 4; + break; + case 'utf8': + this.fillLast = utf8FillLast; + nb = 4; + break; + case 'base64': + this.text = base64Text; + this.end = base64End; + nb = 3; + break; + default: + this.write = simpleWrite; + this.end = simpleEnd; + return; + } + this.lastNeed = 0; + this.lastTotal = 0; + this.lastChar = Buffer.allocUnsafe(nb); + } + + StringDecoder.prototype.write = function(buf) { + if (buf.length === 0) return ''; + var r; + var i; + if (this.lastNeed) { + r = this.fillLast(buf); + if (r === undefined) return ''; + i = this.lastNeed; + this.lastNeed = 0; + } else { + i = 0; + } + if (i < buf.length) return r ? r + this.text(buf, i) : this.text(buf, i); + return r || ''; + }; + + StringDecoder.prototype.end = utf8End; + + // Returns only complete characters in a Buffer + StringDecoder.prototype.text = utf8Text; + + // Attempts to complete a partial non-UTF-8 character using bytes from a Buffer + StringDecoder.prototype.fillLast = function(buf) { + if (this.lastNeed <= buf.length) { + buf.copy(this.lastChar, this.lastTotal - this.lastNeed, 0, this.lastNeed); + return this.lastChar.toString(this.encoding, 0, this.lastTotal); + } + buf.copy(this.lastChar, this.lastTotal - this.lastNeed, 0, buf.length); + this.lastNeed -= buf.length; + }; + + // Checks the type of a UTF-8 byte, whether it's ASCII, a leading byte, or a + // continuation byte. If an invalid byte is detected, -2 is returned. + function utf8CheckByte(byte) { + if (byte <= 0x7F) return 0; + else if (byte >> 5 === 0x06) return 2; + else if (byte >> 4 === 0x0E) return 3; + else if (byte >> 3 === 0x1E) return 4; + return byte >> 6 === 0x02 ? -1 : -2; + } + + // Checks at most 3 bytes at the end of a Buffer in order to detect an + // incomplete multi-byte UTF-8 character. The total number of bytes (2, 3, or 4) + // needed to complete the UTF-8 character (if applicable) are returned. + function utf8CheckIncomplete(self, buf, i) { + var j = buf.length - 1; + if (j < i) return 0; + var nb = utf8CheckByte(buf[j]); + if (nb >= 0) { + if (nb > 0) self.lastNeed = nb - 1; + return nb; + } + if (--j < i || nb === -2) return 0; + nb = utf8CheckByte(buf[j]); + if (nb >= 0) { + if (nb > 0) self.lastNeed = nb - 2; + return nb; + } + if (--j < i || nb === -2) return 0; + nb = utf8CheckByte(buf[j]); + if (nb >= 0) { + if (nb > 0) { + if (nb === 2) nb = 0; + else self.lastNeed = nb - 3; + } + return nb; + } + return 0; + } + + // Validates as many continuation bytes for a multi-byte UTF-8 character as + // needed or are available. If we see a non-continuation byte where we expect + // one, we "replace" the validated continuation bytes we've seen so far with + // a single UTF-8 replacement character ('\ufffd'), to match v8's UTF-8 decoding + // behavior. The continuation byte check is included three times in the case + // where all of the continuation bytes for a character exist in the same buffer. + // It is also done this way as a slight performance increase instead of using a + // loop. + function utf8CheckExtraBytes(self, buf, p) { + if ((buf[0] & 0xC0) !== 0x80) { + self.lastNeed = 0; + return '\ufffd'; + } + if (self.lastNeed > 1 && buf.length > 1) { + if ((buf[1] & 0xC0) !== 0x80) { + self.lastNeed = 1; + return '\ufffd'; + } + if (self.lastNeed > 2 && buf.length > 2) { + if ((buf[2] & 0xC0) !== 0x80) { + self.lastNeed = 2; + return '\ufffd'; + } + } + } + } + + // Attempts to complete a multi-byte UTF-8 character using bytes from a Buffer. + function utf8FillLast(buf) { + var p = this.lastTotal - this.lastNeed; + var r = utf8CheckExtraBytes(this, buf, p); + if (r !== undefined) return r; + if (this.lastNeed <= buf.length) { + buf.copy(this.lastChar, p, 0, this.lastNeed); + return this.lastChar.toString(this.encoding, 0, this.lastTotal); + } + buf.copy(this.lastChar, p, 0, buf.length); + this.lastNeed -= buf.length; + } + + // Returns all complete UTF-8 characters in a Buffer. If the Buffer ended on a + // partial character, the character's bytes are buffered until the required + // number of bytes are available. + function utf8Text(buf, i) { + var total = utf8CheckIncomplete(this, buf, i); + if (!this.lastNeed) return buf.toString('utf8', i); + this.lastTotal = total; + var end = buf.length - (total - this.lastNeed); + buf.copy(this.lastChar, 0, end); + return buf.toString('utf8', i, end); + } + + // For UTF-8, a replacement character is added when ending on a partial + // character. + function utf8End(buf) { + var r = buf && buf.length ? this.write(buf) : ''; + if (this.lastNeed) return r + '\ufffd'; + return r; + } + + // UTF-16LE typically needs two bytes per character, but even if we have an even + // number of bytes available, we need to check if we end on a leading/high + // surrogate. In that case, we need to wait for the next two bytes in order to + // decode the last character properly. + function utf16Text(buf, i) { + if ((buf.length - i) % 2 === 0) { + var r = buf.toString('utf16le', i); + if (r) { + var c = r.charCodeAt(r.length - 1); + if (c >= 0xD800 && c <= 0xDBFF) { + this.lastNeed = 2; + this.lastTotal = 4; + this.lastChar[0] = buf[buf.length - 2]; + this.lastChar[1] = buf[buf.length - 1]; + return r.slice(0, -1); + } + } + return r; + } + this.lastNeed = 1; + this.lastTotal = 2; + this.lastChar[0] = buf[buf.length - 1]; + return buf.toString('utf16le', i, buf.length - 1); + } + + // For UTF-16LE we do not explicitly append special replacement characters if we + // end on a partial character, we simply let v8 handle that. + function utf16End(buf) { + var r = buf && buf.length ? this.write(buf) : ''; + if (this.lastNeed) { + var end = this.lastTotal - this.lastNeed; + return r + this.lastChar.toString('utf16le', 0, end); + } + return r; + } + + function base64Text(buf, i) { + var n = (buf.length - i) % 3; + if (n === 0) return buf.toString('base64', i); + this.lastNeed = 3 - n; + this.lastTotal = 3; + if (n === 1) { + this.lastChar[0] = buf[buf.length - 1]; + } else { + this.lastChar[0] = buf[buf.length - 2]; + this.lastChar[1] = buf[buf.length - 1]; + } + return buf.toString('base64', i, buf.length - n); + } + + function base64End(buf) { + var r = buf && buf.length ? this.write(buf) : ''; + if (this.lastNeed) return r + this.lastChar.toString('base64', 0, 3 - this.lastNeed); + return r; + } + + // Pass bytes on through for single-byte encodings (e.g. ascii, latin1, hex) + function simpleWrite(buf) { + return buf.toString(this.encoding); + } + + function simpleEnd(buf) { + return buf && buf.length ? this.write(buf) : ''; + } + }, { + "safe-buffer": 108 + }], + 111: [function(require, module, exports) { + (function(setImmediate, clearImmediate) { + var nextTick = require('process/browser.js').nextTick; + var apply = Function.prototype.apply; + var slice = Array.prototype.slice; + var immediateIds = {}; + var nextImmediateId = 0; + + // DOM APIs, for completeness + + exports.setTimeout = function() { + return new Timeout(apply.call(setTimeout, window, arguments), clearTimeout); + }; + exports.setInterval = function() { + return new Timeout(apply.call(setInterval, window, arguments), clearInterval); + }; + exports.clearTimeout = + exports.clearInterval = function(timeout) { + timeout.close(); + }; + + function Timeout(id, clearFn) { + this._id = id; + this._clearFn = clearFn; + } + Timeout.prototype.unref = Timeout.prototype.ref = function() {}; + Timeout.prototype.close = function() { + this._clearFn.call(window, this._id); + }; + + // Does not start the time, just sets up the members needed. + exports.enroll = function(item, msecs) { + clearTimeout(item._idleTimeoutId); + item._idleTimeout = msecs; + }; + + exports.unenroll = function(item) { + clearTimeout(item._idleTimeoutId); + item._idleTimeout = -1; + }; + + exports._unrefActive = exports.active = function(item) { + clearTimeout(item._idleTimeoutId); + + var msecs = item._idleTimeout; + if (msecs >= 0) { + item._idleTimeoutId = setTimeout(function onTimeout() { + if (item._onTimeout) + item._onTimeout(); + }, msecs); + } + }; + + // That's not how node.js implements it but the exposed api is the same. + exports.setImmediate = typeof setImmediate === "function" ? setImmediate : function(fn) { + var id = nextImmediateId++; + var args = arguments.length < 2 ? false : slice.call(arguments, 1); + + immediateIds[id] = true; + + nextTick(function onNextTick() { + if (immediateIds[id]) { + // fn.call() is faster so we optimize for the common use-case + // @see http://jsperf.com/call-apply-segu + if (args) { + fn.apply(null, args); + } else { + fn.call(null); + } + // Prevent ids from leaking + exports.clearImmediate(id); + } + }); + + return id; + }; + + exports.clearImmediate = typeof clearImmediate === "function" ? clearImmediate : function(id) { + delete immediateIds[id]; + }; + }).call(this, require("timers").setImmediate, require("timers").clearImmediate) + }, { + "process/browser.js": 92, + "timers": 111 + }], + 112: [function(require, module, exports) { + // Copyright Joyent, Inc. and other Node contributors. + // + // Permission is hereby granted, free of charge, to any person obtaining a + // copy of this software and associated documentation files (the + // "Software"), to deal in the Software without restriction, including + // without limitation the rights to use, copy, modify, merge, publish, + // distribute, sublicense, and/or sell copies of the Software, and to permit + // persons to whom the Software is furnished to do so, subject to the + // following conditions: + // + // The above copyright notice and this permission notice shall be included + // in all copies or substantial portions of the Software. + // + // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN + // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE + // USE OR OTHER DEALINGS IN THE SOFTWARE. + + 'use strict'; + + var punycode = require('punycode'); + var util = require('./util'); + + exports.parse = urlParse; + exports.resolve = urlResolve; + exports.resolveObject = urlResolveObject; + exports.format = urlFormat; + + exports.Url = Url; + + function Url() { + this.protocol = null; + this.slashes = null; + this.auth = null; + this.host = null; + this.port = null; + this.hostname = null; + this.hash = null; + this.search = null; + this.query = null; + this.pathname = null; + this.path = null; + this.href = null; + } + + // Reference: RFC 3986, RFC 1808, RFC 2396 + + // define these here so at least they only have to be + // compiled once on the first module load. + var protocolPattern = /^([a-z0-9.+-]+:)/i, + portPattern = /:[0-9]*$/, + + // Special case for a simple path URL + simplePathPattern = /^(\/\/?(?!\/)[^\?\s]*)(\?[^\s]*)?$/, + + // RFC 2396: characters reserved for delimiting URLs. + // We actually just auto-escape these. + delims = ['<', '>', '"', '`', ' ', '\r', '\n', '\t'], + + // RFC 2396: characters not allowed for various reasons. + unwise = ['{', '}', '|', '\\', '^', '`'].concat(delims), + + // Allowed by RFCs, but cause of XSS attacks. Always escape these. + autoEscape = ['\''].concat(unwise), + // Characters that are never ever allowed in a hostname. + // Note that any invalid chars are also handled, but these + // are the ones that are *expected* to be seen, so we fast-path + // them. + nonHostChars = ['%', '/', '?', ';', '#'].concat(autoEscape), + hostEndingChars = ['/', '?', '#'], + hostnameMaxLen = 255, + hostnamePartPattern = /^[+a-z0-9A-Z_-]{0,63}$/, + hostnamePartStart = /^([+a-z0-9A-Z_-]{0,63})(.*)$/, + // protocols that can allow "unsafe" and "unwise" chars. + unsafeProtocol = { + 'javascript': true, + 'javascript:': true + }, + // protocols that never have a hostname. + hostlessProtocol = { + 'javascript': true, + 'javascript:': true + }, + // protocols that always contain a // bit. + slashedProtocol = { + 'http': true, + 'https': true, + 'ftp': true, + 'gopher': true, + 'file': true, + 'http:': true, + 'https:': true, + 'ftp:': true, + 'gopher:': true, + 'file:': true + }, + querystring = require('querystring'); + + function urlParse(url, parseQueryString, slashesDenoteHost) { + if (url && util.isObject(url) && url instanceof Url) return url; + + var u = new Url; + u.parse(url, parseQueryString, slashesDenoteHost); + return u; + } + + Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) { + if (!util.isString(url)) { + throw new TypeError("Parameter 'url' must be a string, not " + typeof url); + } + + // Copy chrome, IE, opera backslash-handling behavior. + // Back slashes before the query string get converted to forward slashes + // See: https://code.google.com/p/chromium/issues/detail?id=25916 + var queryIndex = url.indexOf('?'), + splitter = + (queryIndex !== -1 && queryIndex < url.indexOf('#')) ? '?' : '#', + uSplit = url.split(splitter), + slashRegex = /\\/g; + uSplit[0] = uSplit[0].replace(slashRegex, '/'); + url = uSplit.join(splitter); + + var rest = url; + + // trim before proceeding. + // This is to support parse stuff like " http://foo.com \n" + rest = rest.trim(); + + if (!slashesDenoteHost && url.split('#').length === 1) { + // Try fast path regexp + var simplePath = simplePathPattern.exec(rest); + if (simplePath) { + this.path = rest; + this.href = rest; + this.pathname = simplePath[1]; + if (simplePath[2]) { + this.search = simplePath[2]; + if (parseQueryString) { + this.query = querystring.parse(this.search.substr(1)); + } else { + this.query = this.search.substr(1); + } + } else if (parseQueryString) { + this.search = ''; + this.query = {}; + } + return this; + } + } + + var proto = protocolPattern.exec(rest); + if (proto) { + proto = proto[0]; + var lowerProto = proto.toLowerCase(); + this.protocol = lowerProto; + rest = rest.substr(proto.length); + } + + // figure out if it's got a host + // user@server is *always* interpreted as a hostname, and url + // resolution will treat //foo/bar as host=foo,path=bar because that's + // how the browser resolves relative URLs. + if (slashesDenoteHost || proto || rest.match(/^\/\/[^@\/]+@[^@\/]+/)) { + var slashes = rest.substr(0, 2) === '//'; + if (slashes && !(proto && hostlessProtocol[proto])) { + rest = rest.substr(2); + this.slashes = true; + } + } + + if (!hostlessProtocol[proto] && + (slashes || (proto && !slashedProtocol[proto]))) { + + // there's a hostname. + // the first instance of /, ?, ;, or # ends the host. + // + // If there is an @ in the hostname, then non-host chars *are* allowed + // to the left of the last @ sign, unless some host-ending character + // comes *before* the @-sign. + // URLs are obnoxious. + // + // ex: + // http://a@b@c/ => user:a@b host:c + // http://a@b?@c => user:a host:c path:/?@c + + // v0.12 TODO(isaacs): This is not quite how Chrome does things. + // Review our test case against browsers more comprehensively. + + // find the first instance of any hostEndingChars + var hostEnd = -1; + for (var i = 0; i < hostEndingChars.length; i++) { + var hec = rest.indexOf(hostEndingChars[i]); + if (hec !== -1 && (hostEnd === -1 || hec < hostEnd)) + hostEnd = hec; + } + + // at this point, either we have an explicit point where the + // auth portion cannot go past, or the last @ char is the decider. + var auth, atSign; + if (hostEnd === -1) { + // atSign can be anywhere. + atSign = rest.lastIndexOf('@'); + } else { + // atSign must be in auth portion. + // http://a@b/c@d => host:b auth:a path:/c@d + atSign = rest.lastIndexOf('@', hostEnd); + } + + // Now we have a portion which is definitely the auth. + // Pull that off. + if (atSign !== -1) { + auth = rest.slice(0, atSign); + rest = rest.slice(atSign + 1); + this.auth = decodeURIComponent(auth); + } + + // the host is the remaining to the left of the first non-host char + hostEnd = -1; + for (var i = 0; i < nonHostChars.length; i++) { + var hec = rest.indexOf(nonHostChars[i]); + if (hec !== -1 && (hostEnd === -1 || hec < hostEnd)) + hostEnd = hec; + } + // if we still have not hit it, then the entire thing is a host. + if (hostEnd === -1) + hostEnd = rest.length; + + this.host = rest.slice(0, hostEnd); + rest = rest.slice(hostEnd); + + // pull out port. + this.parseHost(); + + // we've indicated that there is a hostname, + // so even if it's empty, it has to be present. + this.hostname = this.hostname || ''; + + // if hostname begins with [ and ends with ] + // assume that it's an IPv6 address. + var ipv6Hostname = this.hostname[0] === '[' && + this.hostname[this.hostname.length - 1] === ']'; + + // validate a little. + if (!ipv6Hostname) { + var hostparts = this.hostname.split(/\./); + for (var i = 0, l = hostparts.length; i < l; i++) { + var part = hostparts[i]; + if (!part) continue; + if (!part.match(hostnamePartPattern)) { + var newpart = ''; + for (var j = 0, k = part.length; j < k; j++) { + if (part.charCodeAt(j) > 127) { + // we replace non-ASCII char with a temporary placeholder + // we need this to make sure size of hostname is not + // broken by replacing non-ASCII by nothing + newpart += 'x'; + } else { + newpart += part[j]; + } + } + // we test again with ASCII char only + if (!newpart.match(hostnamePartPattern)) { + var validParts = hostparts.slice(0, i); + var notHost = hostparts.slice(i + 1); + var bit = part.match(hostnamePartStart); + if (bit) { + validParts.push(bit[1]); + notHost.unshift(bit[2]); + } + if (notHost.length) { + rest = '/' + notHost.join('.') + rest; + } + this.hostname = validParts.join('.'); + break; + } + } + } + } + + if (this.hostname.length > hostnameMaxLen) { + this.hostname = ''; + } else { + // hostnames are always lower case. + this.hostname = this.hostname.toLowerCase(); + } + + if (!ipv6Hostname) { + // IDNA Support: Returns a punycoded representation of "domain". + // It only converts parts of the domain name that + // have non-ASCII characters, i.e. it doesn't matter if + // you call it with a domain that already is ASCII-only. + this.hostname = punycode.toASCII(this.hostname); + } + + var p = this.port ? ':' + this.port : ''; + var h = this.hostname || ''; + this.host = h + p; + this.href += this.host; + + // strip [ and ] from the hostname + // the host field still retains them, though + if (ipv6Hostname) { + this.hostname = this.hostname.substr(1, this.hostname.length - 2); + if (rest[0] !== '/') { + rest = '/' + rest; + } + } + } + + // now rest is set to the post-host stuff. + // chop off any delim chars. + if (!unsafeProtocol[lowerProto]) { + + // First, make 100% sure that any "autoEscape" chars get + // escaped, even if encodeURIComponent doesn't think they + // need to be. + for (var i = 0, l = autoEscape.length; i < l; i++) { + var ae = autoEscape[i]; + if (rest.indexOf(ae) === -1) + continue; + var esc = encodeURIComponent(ae); + if (esc === ae) { + esc = escape(ae); + } + rest = rest.split(ae).join(esc); + } + } + + + // chop off from the tail first. + var hash = rest.indexOf('#'); + if (hash !== -1) { + // got a fragment string. + this.hash = rest.substr(hash); + rest = rest.slice(0, hash); + } + var qm = rest.indexOf('?'); + if (qm !== -1) { + this.search = rest.substr(qm); + this.query = rest.substr(qm + 1); + if (parseQueryString) { + this.query = querystring.parse(this.query); + } + rest = rest.slice(0, qm); + } else if (parseQueryString) { + // no query string, but parseQueryString still requested + this.search = ''; + this.query = {}; + } + if (rest) this.pathname = rest; + if (slashedProtocol[lowerProto] && + this.hostname && !this.pathname) { + this.pathname = '/'; + } + + //to support http.request + if (this.pathname || this.search) { + var p = this.pathname || ''; + var s = this.search || ''; + this.path = p + s; + } + + // finally, reconstruct the href based on what has been validated. + this.href = this.format(); + return this; + }; + + // format a parsed object into a url string + function urlFormat(obj) { + // ensure it's an object, and not a string url. + // If it's an obj, this is a no-op. + // this way, you can call url_format() on strings + // to clean up potentially wonky urls. + if (util.isString(obj)) obj = urlParse(obj); + if (!(obj instanceof Url)) return Url.prototype.format.call(obj); + return obj.format(); + } + + Url.prototype.format = function() { + var auth = this.auth || ''; + if (auth) { + auth = encodeURIComponent(auth); + auth = auth.replace(/%3A/i, ':'); + auth += '@'; + } + + var protocol = this.protocol || '', + pathname = this.pathname || '', + hash = this.hash || '', + host = false, + query = ''; + + if (this.host) { + host = auth + this.host; + } else if (this.hostname) { + host = auth + (this.hostname.indexOf(':') === -1 ? + this.hostname : + '[' + this.hostname + ']'); + if (this.port) { + host += ':' + this.port; + } + } + + if (this.query && + util.isObject(this.query) && + Object.keys(this.query).length) { + query = querystring.stringify(this.query); + } + + var search = this.search || (query && ('?' + query)) || ''; + + if (protocol && protocol.substr(-1) !== ':') protocol += ':'; + + // only the slashedProtocols get the //. Not mailto:, xmpp:, etc. + // unless they had them to begin with. + if (this.slashes || + (!protocol || slashedProtocol[protocol]) && host !== false) { + host = '//' + (host || ''); + if (pathname && pathname.charAt(0) !== '/') pathname = '/' + pathname; + } else if (!host) { + host = ''; + } + + if (hash && hash.charAt(0) !== '#') hash = '#' + hash; + if (search && search.charAt(0) !== '?') search = '?' + search; + + pathname = pathname.replace(/[?#]/g, function(match) { + return encodeURIComponent(match); + }); + search = search.replace('#', '%23'); + + return protocol + host + pathname + search + hash; + }; + + function urlResolve(source, relative) { + return urlParse(source, false, true).resolve(relative); + } + + Url.prototype.resolve = function(relative) { + return this.resolveObject(urlParse(relative, false, true)).format(); + }; + + function urlResolveObject(source, relative) { + if (!source) return relative; + return urlParse(source, false, true).resolveObject(relative); + } + + Url.prototype.resolveObject = function(relative) { + if (util.isString(relative)) { + var rel = new Url(); + rel.parse(relative, false, true); + relative = rel; + } + + var result = new Url(); + var tkeys = Object.keys(this); + for (var tk = 0; tk < tkeys.length; tk++) { + var tkey = tkeys[tk]; + result[tkey] = this[tkey]; + } + + // hash is always overridden, no matter what. + // even href="" will remove it. + result.hash = relative.hash; + + // if the relative url is empty, then there's nothing left to do here. + if (relative.href === '') { + result.href = result.format(); + return result; + } + + // hrefs like //foo/bar always cut to the protocol. + if (relative.slashes && !relative.protocol) { + // take everything except the protocol from relative + var rkeys = Object.keys(relative); + for (var rk = 0; rk < rkeys.length; rk++) { + var rkey = rkeys[rk]; + if (rkey !== 'protocol') + result[rkey] = relative[rkey]; + } + + //urlParse appends trailing / to urls like http://www.example.com + if (slashedProtocol[result.protocol] && + result.hostname && !result.pathname) { + result.path = result.pathname = '/'; + } + + result.href = result.format(); + return result; + } + + if (relative.protocol && relative.protocol !== result.protocol) { + // if it's a known url protocol, then changing + // the protocol does weird things + // first, if it's not file:, then we MUST have a host, + // and if there was a path + // to begin with, then we MUST have a path. + // if it is file:, then the host is dropped, + // because that's known to be hostless. + // anything else is assumed to be absolute. + if (!slashedProtocol[relative.protocol]) { + var keys = Object.keys(relative); + for (var v = 0; v < keys.length; v++) { + var k = keys[v]; + result[k] = relative[k]; + } + result.href = result.format(); + return result; + } + + result.protocol = relative.protocol; + if (!relative.host && !hostlessProtocol[relative.protocol]) { + var relPath = (relative.pathname || '').split('/'); + while (relPath.length && !(relative.host = relPath.shift())); + if (!relative.host) relative.host = ''; + if (!relative.hostname) relative.hostname = ''; + if (relPath[0] !== '') relPath.unshift(''); + if (relPath.length < 2) relPath.unshift(''); + result.pathname = relPath.join('/'); + } else { + result.pathname = relative.pathname; + } + result.search = relative.search; + result.query = relative.query; + result.host = relative.host || ''; + result.auth = relative.auth; + result.hostname = relative.hostname || relative.host; + result.port = relative.port; + // to support http.request + if (result.pathname || result.search) { + var p = result.pathname || ''; + var s = result.search || ''; + result.path = p + s; + } + result.slashes = result.slashes || relative.slashes; + result.href = result.format(); + return result; + } + + var isSourceAbs = (result.pathname && result.pathname.charAt(0) === '/'), + isRelAbs = ( + relative.host || + relative.pathname && relative.pathname.charAt(0) === '/' + ), + mustEndAbs = (isRelAbs || isSourceAbs || + (result.host && relative.pathname)), + removeAllDots = mustEndAbs, + srcPath = result.pathname && result.pathname.split('/') || [], + relPath = relative.pathname && relative.pathname.split('/') || [], + psychotic = result.protocol && !slashedProtocol[result.protocol]; + + // if the url is a non-slashed url, then relative + // links like ../.. should be able + // to crawl up to the hostname, as well. This is strange. + // result.protocol has already been set by now. + // Later on, put the first path part into the host field. + if (psychotic) { + result.hostname = ''; + result.port = null; + if (result.host) { + if (srcPath[0] === '') srcPath[0] = result.host; + else srcPath.unshift(result.host); + } + result.host = ''; + if (relative.protocol) { + relative.hostname = null; + relative.port = null; + if (relative.host) { + if (relPath[0] === '') relPath[0] = relative.host; + else relPath.unshift(relative.host); + } + relative.host = null; + } + mustEndAbs = mustEndAbs && (relPath[0] === '' || srcPath[0] === ''); + } + + if (isRelAbs) { + // it's absolute. + result.host = (relative.host || relative.host === '') ? + relative.host : result.host; + result.hostname = (relative.hostname || relative.hostname === '') ? + relative.hostname : result.hostname; + result.search = relative.search; + result.query = relative.query; + srcPath = relPath; + // fall through to the dot-handling below. + } else if (relPath.length) { + // it's relative + // throw away the existing file, and take the new path instead. + if (!srcPath) srcPath = []; + srcPath.pop(); + srcPath = srcPath.concat(relPath); + result.search = relative.search; + result.query = relative.query; + } else if (!util.isNullOrUndefined(relative.search)) { + // just pull out the search. + // like href='?foo'. + // Put this after the other two cases because it simplifies the booleans + if (psychotic) { + result.hostname = result.host = srcPath.shift(); + //occationaly the auth can get stuck only in host + //this especially happens in cases like + //url.resolveObject('mailto:local1@domain1', 'local2@domain2') + var authInHost = result.host && result.host.indexOf('@') > 0 ? + result.host.split('@') : false; + if (authInHost) { + result.auth = authInHost.shift(); + result.host = result.hostname = authInHost.shift(); + } + } + result.search = relative.search; + result.query = relative.query; + //to support http.request + if (!util.isNull(result.pathname) || !util.isNull(result.search)) { + result.path = (result.pathname ? result.pathname : '') + + (result.search ? result.search : ''); + } + result.href = result.format(); + return result; + } + + if (!srcPath.length) { + // no path at all. easy. + // we've already handled the other stuff above. + result.pathname = null; + //to support http.request + if (result.search) { + result.path = '/' + result.search; + } else { + result.path = null; + } + result.href = result.format(); + return result; + } + + // if a url ENDs in . or .., then it must get a trailing slash. + // however, if it ends in anything else non-slashy, + // then it must NOT get a trailing slash. + var last = srcPath.slice(-1)[0]; + var hasTrailingSlash = ( + (result.host || relative.host || srcPath.length > 1) && + (last === '.' || last === '..') || last === ''); + + // strip single dots, resolve double dots to parent dir + // if the path tries to go above the root, `up` ends up > 0 + var up = 0; + for (var i = srcPath.length; i >= 0; i--) { + last = srcPath[i]; + if (last === '.') { + srcPath.splice(i, 1); + } else if (last === '..') { + srcPath.splice(i, 1); + up++; + } else if (up) { + srcPath.splice(i, 1); + up--; + } + } + + // if the path is allowed to go above the root, restore leading ..s + if (!mustEndAbs && !removeAllDots) { + for (; up--; up) { + srcPath.unshift('..'); + } + } + + if (mustEndAbs && srcPath[0] !== '' && + (!srcPath[0] || srcPath[0].charAt(0) !== '/')) { + srcPath.unshift(''); + } + + if (hasTrailingSlash && (srcPath.join('/').substr(-1) !== '/')) { + srcPath.push(''); + } + + var isAbsolute = srcPath[0] === '' || + (srcPath[0] && srcPath[0].charAt(0) === '/'); + + // put the host back + if (psychotic) { + result.hostname = result.host = isAbsolute ? '' : + srcPath.length ? srcPath.shift() : ''; + //occationaly the auth can get stuck only in host + //this especially happens in cases like + //url.resolveObject('mailto:local1@domain1', 'local2@domain2') + var authInHost = result.host && result.host.indexOf('@') > 0 ? + result.host.split('@') : false; + if (authInHost) { + result.auth = authInHost.shift(); + result.host = result.hostname = authInHost.shift(); + } + } + + mustEndAbs = mustEndAbs || (result.host && srcPath.length); + + if (mustEndAbs && !isAbsolute) { + srcPath.unshift(''); + } + + if (!srcPath.length) { + result.pathname = null; + result.path = null; + } else { + result.pathname = srcPath.join('/'); + } + + //to support request.http + if (!util.isNull(result.pathname) || !util.isNull(result.search)) { + result.path = (result.pathname ? result.pathname : '') + + (result.search ? result.search : ''); + } + result.auth = relative.auth || result.auth; + result.slashes = result.slashes || relative.slashes; + result.href = result.format(); + return result; + }; + + Url.prototype.parseHost = function() { + var host = this.host; + var port = portPattern.exec(host); + if (port) { + port = port[0]; + if (port !== ':') { + this.port = port.substr(1); + } + host = host.substr(0, host.length - port.length); + } + if (host) this.hostname = host; + }; + + }, { + "./util": 113, + "punycode": 93, + "querystring": 96 + }], + 113: [function(require, module, exports) { + 'use strict'; + + module.exports = { + isString: function(arg) { + return typeof(arg) === 'string'; + }, + isObject: function(arg) { + return typeof(arg) === 'object' && arg !== null; + }, + isNull: function(arg) { + return arg === null; + }, + isNullOrUndefined: function(arg) { + return arg == null; + } + }; + + }, {}], + 114: [function(require, module, exports) { + (function(global) { + + /** + * Module exports. + */ + + module.exports = deprecate; + + /** + * Mark that a method should not be used. + * Returns a modified function which warns once by default. + * + * If `localStorage.noDeprecation = true` is set, then it is a no-op. + * + * If `localStorage.throwDeprecation = true` is set, then deprecated functions + * will throw an Error when invoked. + * + * If `localStorage.traceDeprecation = true` is set, then deprecated functions + * will invoke `console.trace()` instead of `console.error()`. + * + * @param {Function} fn - the function to deprecate + * @param {String} msg - the string to print to the console when `fn` is invoked + * @returns {Function} a new "deprecated" version of `fn` + * @api public + */ + + function deprecate(fn, msg) { + if (config('noDeprecation')) { + return fn; + } + + var warned = false; + + function deprecated() { + if (!warned) { + if (config('throwDeprecation')) { + throw new Error(msg); + } else if (config('traceDeprecation')) { + console.trace(msg); + } else { + console.warn(msg); + } + warned = true; + } + return fn.apply(this, arguments); + } + + return deprecated; + } + + /** + * Checks `localStorage` for boolean values for the given `name`. + * + * @param {String} name + * @returns {Boolean} + * @api private + */ + + function config(name) { + // accessing global.localStorage can trigger a DOMException in sandboxed iframes + try { + if (!global.localStorage) return false; + } catch (_) { + return false; + } + var val = global.localStorage[name]; + if (null == val) return false; + return String(val).toLowerCase() === 'true'; + } + + }).call(this, typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) + }, {}], + 115: [function(require, module, exports) { + module.exports = function isBuffer(arg) { + return arg && typeof arg === 'object' && + typeof arg.copy === 'function' && + typeof arg.fill === 'function' && + typeof arg.readUInt8 === 'function'; + } + }, {}], + 116: [function(require, module, exports) { + (function(process, global) { + // Copyright Joyent, Inc. and other Node contributors. + // + // Permission is hereby granted, free of charge, to any person obtaining a + // copy of this software and associated documentation files (the + // "Software"), to deal in the Software without restriction, including + // without limitation the rights to use, copy, modify, merge, publish, + // distribute, sublicense, and/or sell copies of the Software, and to permit + // persons to whom the Software is furnished to do so, subject to the + // following conditions: + // + // The above copyright notice and this permission notice shall be included + // in all copies or substantial portions of the Software. + // + // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN + // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE + // USE OR OTHER DEALINGS IN THE SOFTWARE. + + var formatRegExp = /%[sdj%]/g; + exports.format = function(f) { + if (!isString(f)) { + var objects = []; + for (var i = 0; i < arguments.length; i++) { + objects.push(inspect(arguments[i])); + } + return objects.join(' '); + } + + var i = 1; + var args = arguments; + var len = args.length; + var str = String(f).replace(formatRegExp, function(x) { + if (x === '%%') return '%'; + if (i >= len) return x; + switch (x) { + case '%s': + return String(args[i++]); + case '%d': + return Number(args[i++]); + case '%j': + try { + return JSON.stringify(args[i++]); + } catch (_) { + return '[Circular]'; + } + default: + return x; + } + }); + for (var x = args[i]; i < len; x = args[++i]) { + if (isNull(x) || !isObject(x)) { + str += ' ' + x; + } else { + str += ' ' + inspect(x); + } + } + return str; + }; + + + // Mark that a method should not be used. + // Returns a modified function which warns once by default. + // If --no-deprecation is set, then it is a no-op. + exports.deprecate = function(fn, msg) { + // Allow for deprecating things in the process of starting up. + if (isUndefined(global.process)) { + return function() { + return exports.deprecate(fn, msg).apply(this, arguments); + }; + } + + if (process.noDeprecation === true) { + return fn; + } + + var warned = false; + + function deprecated() { + if (!warned) { + if (process.throwDeprecation) { + throw new Error(msg); + } else if (process.traceDeprecation) { + console.trace(msg); + } else { + console.error(msg); + } + warned = true; + } + return fn.apply(this, arguments); + } + + return deprecated; + }; + + + var debugs = {}; + var debugEnviron; + exports.debuglog = function(set) { + if (isUndefined(debugEnviron)) + debugEnviron = process.env.NODE_DEBUG || ''; + set = set.toUpperCase(); + if (!debugs[set]) { + if (new RegExp('\\b' + set + '\\b', 'i').test(debugEnviron)) { + var pid = process.pid; + debugs[set] = function() { + var msg = exports.format.apply(exports, arguments); + console.error('%s %d: %s', set, pid, msg); + }; + } else { + debugs[set] = function() {}; + } + } + return debugs[set]; + }; + + + /** + * Echos the value of a value. Trys to print the value out + * in the best way possible given the different types. + * + * @param {Object} obj The object to print out. + * @param {Object} opts Optional options object that alters the output. + */ + /* legacy: obj, showHidden, depth, colors*/ + function inspect(obj, opts) { + // default options + var ctx = { + seen: [], + stylize: stylizeNoColor + }; + // legacy... + if (arguments.length >= 3) ctx.depth = arguments[2]; + if (arguments.length >= 4) ctx.colors = arguments[3]; + if (isBoolean(opts)) { + // legacy... + ctx.showHidden = opts; + } else if (opts) { + // got an "options" object + exports._extend(ctx, opts); + } + // set default options + if (isUndefined(ctx.showHidden)) ctx.showHidden = false; + if (isUndefined(ctx.depth)) ctx.depth = 2; + if (isUndefined(ctx.colors)) ctx.colors = false; + if (isUndefined(ctx.customInspect)) ctx.customInspect = true; + if (ctx.colors) ctx.stylize = stylizeWithColor; + return formatValue(ctx, obj, ctx.depth); + } + exports.inspect = inspect; + + + // http://en.wikipedia.org/wiki/ANSI_escape_code#graphics + inspect.colors = { + 'bold': [1, 22], + 'italic': [3, 23], + 'underline': [4, 24], + 'inverse': [7, 27], + 'white': [37, 39], + 'grey': [90, 39], + 'black': [30, 39], + 'blue': [34, 39], + 'cyan': [36, 39], + 'green': [32, 39], + 'magenta': [35, 39], + 'red': [31, 39], + 'yellow': [33, 39] + }; + + // Don't use 'blue' not visible on cmd.exe + inspect.styles = { + 'special': 'cyan', + 'number': 'yellow', + 'boolean': 'yellow', + 'undefined': 'grey', + 'null': 'bold', + 'string': 'green', + 'date': 'magenta', + // "name": intentionally not styling + 'regexp': 'red' + }; + + + function stylizeWithColor(str, styleType) { + var style = inspect.styles[styleType]; + + if (style) { + return '\u001b[' + inspect.colors[style][0] + 'm' + str + + '\u001b[' + inspect.colors[style][1] + 'm'; + } else { + return str; + } + } + + + function stylizeNoColor(str, styleType) { + return str; + } + + + function arrayToHash(array) { + var hash = {}; + + array.forEach(function(val, idx) { + hash[val] = true; + }); + + return hash; + } + + + function formatValue(ctx, value, recurseTimes) { + // Provide a hook for user-specified inspect functions. + // Check that value is an object with an inspect function on it + if (ctx.customInspect && + value && + isFunction(value.inspect) && + // Filter out the util module, it's inspect function is special + value.inspect !== exports.inspect && + // Also filter out any prototype objects using the circular check. + !(value.constructor && value.constructor.prototype === value)) { + var ret = value.inspect(recurseTimes, ctx); + if (!isString(ret)) { + ret = formatValue(ctx, ret, recurseTimes); + } + return ret; + } + + // Primitive types cannot have properties + var primitive = formatPrimitive(ctx, value); + if (primitive) { + return primitive; + } + + // Look up the keys of the object. + var keys = Object.keys(value); + var visibleKeys = arrayToHash(keys); + + if (ctx.showHidden) { + keys = Object.getOwnPropertyNames(value); + } + + // IE doesn't make error fields non-enumerable + // http://msdn.microsoft.com/en-us/library/ie/dww52sbt(v=vs.94).aspx + if (isError(value) && + (keys.indexOf('message') >= 0 || keys.indexOf('description') >= 0)) { + return formatError(value); + } + + // Some type of object without properties can be shortcutted. + if (keys.length === 0) { + if (isFunction(value)) { + var name = value.name ? ': ' + value.name : ''; + return ctx.stylize('[Function' + name + ']', 'special'); + } + if (isRegExp(value)) { + return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp'); + } + if (isDate(value)) { + return ctx.stylize(Date.prototype.toString.call(value), 'date'); + } + if (isError(value)) { + return formatError(value); + } + } + + var base = '', + array = false, + braces = ['{', '}']; + + // Make Array say that they are Array + if (isArray(value)) { + array = true; + braces = ['[', ']']; + } + + // Make functions say that they are functions + if (isFunction(value)) { + var n = value.name ? ': ' + value.name : ''; + base = ' [Function' + n + ']'; + } + + // Make RegExps say that they are RegExps + if (isRegExp(value)) { + base = ' ' + RegExp.prototype.toString.call(value); + } + + // Make dates with properties first say the date + if (isDate(value)) { + base = ' ' + Date.prototype.toUTCString.call(value); + } + + // Make error with message first say the error + if (isError(value)) { + base = ' ' + formatError(value); + } + + if (keys.length === 0 && (!array || value.length == 0)) { + return braces[0] + base + braces[1]; + } + + if (recurseTimes < 0) { + if (isRegExp(value)) { + return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp'); + } else { + return ctx.stylize('[Object]', 'special'); + } + } + + ctx.seen.push(value); + + var output; + if (array) { + output = formatArray(ctx, value, recurseTimes, visibleKeys, keys); + } else { + output = keys.map(function(key) { + return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array); + }); + } + + ctx.seen.pop(); + + return reduceToSingleString(output, base, braces); + } + + + function formatPrimitive(ctx, value) { + if (isUndefined(value)) + return ctx.stylize('undefined', 'undefined'); + if (isString(value)) { + var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '') + .replace(/'/g, "\\'") + .replace(/\\"/g, '"') + '\''; + return ctx.stylize(simple, 'string'); + } + if (isNumber(value)) + return ctx.stylize('' + value, 'number'); + if (isBoolean(value)) + return ctx.stylize('' + value, 'boolean'); + // For some reason typeof null is "object", so special case here. + if (isNull(value)) + return ctx.stylize('null', 'null'); + } + + + function formatError(value) { + return '[' + Error.prototype.toString.call(value) + ']'; + } + + + function formatArray(ctx, value, recurseTimes, visibleKeys, keys) { + var output = []; + for (var i = 0, l = value.length; i < l; ++i) { + if (hasOwnProperty(value, String(i))) { + output.push(formatProperty(ctx, value, recurseTimes, visibleKeys, + String(i), true)); + } else { + output.push(''); + } + } + keys.forEach(function(key) { + if (!key.match(/^\d+$/)) { + output.push(formatProperty(ctx, value, recurseTimes, visibleKeys, + key, true)); + } + }); + return output; + } + + + function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) { + var name, str, desc; + desc = Object.getOwnPropertyDescriptor(value, key) || { + value: value[key] + }; + if (desc.get) { + if (desc.set) { + str = ctx.stylize('[Getter/Setter]', 'special'); + } else { + str = ctx.stylize('[Getter]', 'special'); + } + } else { + if (desc.set) { + str = ctx.stylize('[Setter]', 'special'); + } + } + if (!hasOwnProperty(visibleKeys, key)) { + name = '[' + key + ']'; + } + if (!str) { + if (ctx.seen.indexOf(desc.value) < 0) { + if (isNull(recurseTimes)) { + str = formatValue(ctx, desc.value, null); + } else { + str = formatValue(ctx, desc.value, recurseTimes - 1); + } + if (str.indexOf('\n') > -1) { + if (array) { + str = str.split('\n').map(function(line) { + return ' ' + line; + }).join('\n').substr(2); + } else { + str = '\n' + str.split('\n').map(function(line) { + return ' ' + line; + }).join('\n'); + } + } + } else { + str = ctx.stylize('[Circular]', 'special'); + } + } + if (isUndefined(name)) { + if (array && key.match(/^\d+$/)) { + return str; + } + name = JSON.stringify('' + key); + if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) { + name = name.substr(1, name.length - 2); + name = ctx.stylize(name, 'name'); + } else { + name = name.replace(/'/g, "\\'") + .replace(/\\"/g, '"') + .replace(/(^"|"$)/g, "'"); + name = ctx.stylize(name, 'string'); + } + } + + return name + ': ' + str; + } + + + function reduceToSingleString(output, base, braces) { + var numLinesEst = 0; + var length = output.reduce(function(prev, cur) { + numLinesEst++; + if (cur.indexOf('\n') >= 0) numLinesEst++; + return prev + cur.replace(/\u001b\[\d\d?m/g, '').length + 1; + }, 0); + + if (length > 60) { + return braces[0] + + (base === '' ? '' : base + '\n ') + + ' ' + + output.join(',\n ') + + ' ' + + braces[1]; + } + + return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1]; + } + + + // NOTE: These type checking functions intentionally don't use `instanceof` + // because it is fragile and can be easily faked with `Object.create()`. + function isArray(ar) { + return Array.isArray(ar); + } + exports.isArray = isArray; + + function isBoolean(arg) { + return typeof arg === 'boolean'; + } + exports.isBoolean = isBoolean; + + function isNull(arg) { + return arg === null; + } + exports.isNull = isNull; + + function isNullOrUndefined(arg) { + return arg == null; + } + exports.isNullOrUndefined = isNullOrUndefined; + + function isNumber(arg) { + return typeof arg === 'number'; + } + exports.isNumber = isNumber; + + function isString(arg) { + return typeof arg === 'string'; + } + exports.isString = isString; + + function isSymbol(arg) { + return typeof arg === 'symbol'; + } + exports.isSymbol = isSymbol; + + function isUndefined(arg) { + return arg === void 0; + } + exports.isUndefined = isUndefined; + + function isRegExp(re) { + return isObject(re) && objectToString(re) === '[object RegExp]'; + } + exports.isRegExp = isRegExp; + + function isObject(arg) { + return typeof arg === 'object' && arg !== null; + } + exports.isObject = isObject; + + function isDate(d) { + return isObject(d) && objectToString(d) === '[object Date]'; + } + exports.isDate = isDate; + + function isError(e) { + return isObject(e) && + (objectToString(e) === '[object Error]' || e instanceof Error); + } + exports.isError = isError; + + function isFunction(arg) { + return typeof arg === 'function'; + } + exports.isFunction = isFunction; + + function isPrimitive(arg) { + return arg === null || + typeof arg === 'boolean' || + typeof arg === 'number' || + typeof arg === 'string' || + typeof arg === 'symbol' || // ES6 symbol + typeof arg === 'undefined'; + } + exports.isPrimitive = isPrimitive; + + exports.isBuffer = require('./support/isBuffer'); + + function objectToString(o) { + return Object.prototype.toString.call(o); + } + + + function pad(n) { + return n < 10 ? '0' + n.toString(10) : n.toString(10); + } + + + var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', + 'Oct', 'Nov', 'Dec' + ]; + + // 26 Feb 16:19:34 + function timestamp() { + var d = new Date(); + var time = [pad(d.getHours()), + pad(d.getMinutes()), + pad(d.getSeconds()) + ].join(':'); + return [d.getDate(), months[d.getMonth()], time].join(' '); + } + + + // log is just a thin wrapper to console.log that prepends a timestamp + exports.log = function() { + console.log('%s - %s', timestamp(), exports.format.apply(exports, arguments)); + }; + + + /** + * Inherit the prototype methods from one constructor into another. + * + * The Function.prototype.inherits from lang.js rewritten as a standalone + * function (not on Function.prototype). NOTE: If this file is to be loaded + * during bootstrapping this function needs to be rewritten using some native + * functions as prototype setup using normal JavaScript does not work as + * expected during bootstrapping (see mirror.js in r114903). + * + * @param {function} ctor Constructor function which needs to inherit the + * prototype. + * @param {function} superCtor Constructor function to inherit prototype from. + */ + exports.inherits = require('inherits'); + + exports._extend = function(origin, add) { + // Don't do anything if add isn't an object + if (!add || !isObject(add)) return origin; + + var keys = Object.keys(add); + var i = keys.length; + while (i--) { + origin[keys[i]] = add[keys[i]]; + } + return origin; + }; + + function hasOwnProperty(obj, prop) { + return Object.prototype.hasOwnProperty.call(obj, prop); + } + + }).call(this, require('_process'), typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) + }, { + "./support/isBuffer": 115, + "_process": 92, + "inherits": 80 + }], + 117: [function(require, module, exports) { + (function(process, global) { + 'use strict' + + var Transform = require('readable-stream').Transform + var duplexify = require('duplexify') + var WS = require('ws') + var Buffer = require('safe-buffer').Buffer + + module.exports = WebSocketStream + + function buildProxy(options, socketWrite, socketEnd) { + var proxy = new Transform({ + objectMode: options.objectMode + }) + + proxy._write = socketWrite + proxy._flush = socketEnd + + return proxy + } + + function WebSocketStream(target, protocols, options) { + var stream, socket + + var isBrowser = process.title === 'browser' + var isNative = !!global.WebSocket + var socketWrite = isBrowser ? socketWriteBrowser : socketWriteNode + + if (protocols && !Array.isArray(protocols) && 'object' === typeof protocols) { + // accept the "options" Object as the 2nd argument + options = protocols + protocols = null + + if (typeof options.protocol === 'string' || Array.isArray(options.protocol)) { + protocols = options.protocol; + } + } + + if (!options) options = {} + + if (options.objectMode === undefined) { + options.objectMode = !(options.binary === true || options.binary === undefined) + } + + var proxy = buildProxy(options, socketWrite, socketEnd) + + if (!options.objectMode) { + proxy._writev = writev + } + + // browser only: sets the maximum socket buffer size before throttling + var bufferSize = options.browserBufferSize || 1024 * 512 + + // browser only: how long to wait when throttling + var bufferTimeout = options.browserBufferTimeout || 1000 + + // use existing WebSocket object that was passed in + if (typeof target === 'object') { + socket = target + // otherwise make a new one + } else { + // special constructor treatment for native websockets in browsers, see + // https://github.com/maxogden/websocket-stream/issues/82 + if (isNative && isBrowser) { + socket = new WS(target, protocols) + } else { + socket = new WS(target, protocols, options) + } + + socket.binaryType = 'arraybuffer' + } + + // was already open when passed in + if (socket.readyState === socket.OPEN) { + stream = proxy + } else { + stream = duplexify.obj() + socket.onopen = onopen + } + + stream.socket = socket + + socket.onclose = onclose + socket.onerror = onerror + socket.onmessage = onmessage + + proxy.on('close', destroy) + + var coerceToBuffer = !options.objectMode + + function socketWriteNode(chunk, enc, next) { + // avoid errors, this never happens unless + // destroy() is called + if (socket.readyState !== socket.OPEN) { + next() + return + } + + if (coerceToBuffer && typeof chunk === 'string') { + chunk = Buffer.from(chunk, 'utf8') + } + socket.send(chunk, next) + } + + function socketWriteBrowser(chunk, enc, next) { + if (socket.bufferedAmount > bufferSize) { + setTimeout(socketWriteBrowser, bufferTimeout, chunk, enc, next) + return + } + + if (coerceToBuffer && typeof chunk === 'string') { + chunk = Buffer.from(chunk, 'utf8') + } + + try { + socket.send(chunk) + } catch (err) { + return next(err) + } + + next() + } + + function socketEnd(done) { + socket.close() + done() + } + + function onopen() { + stream.setReadable(proxy) + stream.setWritable(proxy) + stream.emit('connect') + } + + function onclose() { + stream.end() + stream.destroy() + } + + function onerror(err) { + stream.destroy(err) + } + + function onmessage(event) { + var data = event.data + if (data instanceof ArrayBuffer) data = Buffer.from(data) + else data = Buffer.from(data, 'utf8') + proxy.push(data) + } + + function destroy() { + socket.close() + } + + // this is to be enabled only if objectMode is false + function writev(chunks, cb) { + var buffers = new Array(chunks.length) + for (var i = 0; i < chunks.length; i++) { + if (typeof chunks[i].chunk === 'string') { + buffers[i] = Buffer.from(chunks[i], 'utf8') + } else { + buffers[i] = chunks[i].chunk + } + } + + this._write(Buffer.concat(buffers), 'binary', cb) + } + + return stream + } + + }).call(this, require('_process'), typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) + }, { + "_process": 92, + "duplexify": 16, + "readable-stream": 106, + "safe-buffer": 108, + "ws": 118 + }], + 118: [function(require, module, exports) { + + var ws = null + + if (typeof WebSocket !== 'undefined') { + ws = WebSocket + } else if (typeof MozWebSocket !== 'undefined') { + ws = MozWebSocket + } else if (typeof window !== 'undefined') { + ws = window.WebSocket || window.MozWebSocket + } + + module.exports = ws + + }, {}], + 119: [function(require, module, exports) { + // Returns a wrapper function that returns a wrapped callback + // The wrapper function should do some stuff, and return a + // presumably different callback function. + // This makes sure that own properties are retained, so that + // decorations and such are not lost along the way. + module.exports = wrappy + + function wrappy(fn, cb) { + if (fn && cb) return wrappy(fn)(cb) + + if (typeof fn !== 'function') + throw new TypeError('need wrapper function') + + Object.keys(fn).forEach(function(k) { + wrapper[k] = fn[k] + }) + + return wrapper + + function wrapper() { + var args = new Array(arguments.length) + for (var i = 0; i < args.length; i++) { + args[i] = arguments[i] + } + var ret = fn.apply(this, args) + var cb = args[args.length - 1] + if (typeof ret === 'function' && ret !== cb) { + Object.keys(cb).forEach(function(k) { + ret[k] = cb[k] + }) + } + return ret + } + } + + }, {}], + 120: [function(require, module, exports) { + module.exports = extend + + var hasOwnProperty = Object.prototype.hasOwnProperty; + + function extend() { + var target = {} + + for (var i = 0; i < arguments.length; i++) { + var source = arguments[i] + + for (var key in source) { + if (hasOwnProperty.call(source, key)) { + target[key] = source[key] + } + } + } + + return target + } + + }, {}] + }, {}, [8])(8) +}); \ No newline at end of file diff --git a/utils/mqttMember.js b/utils/mqttMember.js new file mode 100644 index 0000000..963589f --- /dev/null +++ b/utils/mqttMember.js @@ -0,0 +1,141 @@ +var app = getApp(); +const Paho = require('./paho-mqtt.js'); + +var mqttApi = { + /*---- 获取连接 ----*/ + connect: function (cb) { + var that = this; + var client = app.mqttParam.client; + if (client && client.isConnected()) { + return; + } + var userInfo = wx.getStorageSync("userInfo"); + var randomString = ""; + for (var i = 1; i <= 5; i++) { + randomString += parseInt(Math.random() * 10); + } + var clientId = "swxclientId-member-" + app.globalData.tenantId + "-" + app.globalData.shopNo + "-" + app.globalData.posNo + randomString; + client = new Paho.Client(app.mqttParam.wsServer, 0, clientId); + client.connect({ + useSSL: false, + cleanSession: false, + keepAliveInterval: 2, + onSuccess: function () { + app.mqttParam.client = client; + client.onMessageArrived = function (msg) { + console.log("----------订阅消息", msg); + var title = msg.destinationName; + if (title.indexOf('pay') > -1) { + if (typeof app.globalFunction.scanPayCodeCallBack === 'function') { + app.globalFunction.scanPayCodeCallBack(msg.payloadString); + } + } else if (title.indexOf('couponconsume') > -1) { + app.globalFunction.consumeCouponCallBack(msg.payloadString); + } + } + + client.onConnectionLost = function (responseObject) { + if (typeof app.globalFunction.onConnectionLost === 'function') { + return app.globalData.onConnectionLost(responseObject) + } + if (responseObject.errorCode !== 0) { + } + } + + if (typeof cb === 'function') { + cb(); + } + } + }); + }, + + /*----订阅 ----*/ + subscribe: function (filter, subscribeOptions) { + var that = this; + var client = app.mqttParam.client; + if (client && client.isConnected()) { + client.subscribe(filter, { + qos: 2 + }); + console.log("mqtt订阅成功") + } else { + that.connect(function () { + client = app.mqttParam.client; + if (client && client.isConnected()) { + client.subscribe(filter, { + qos: 2 + }); + console.log("mqtt订阅成功") + } + }) + } + }, + + /*----废弃订阅 ----*/ + unsubscribe: function (filter, subscribeOptions) { + var client = app.mqttParam.client; + if (client && client.isConnected()) { + client.unsubscribe(filter); + } + }, + + /*----发布消息----*/ + publish: function (topic, message, qos = 0, retained = false) { + // 发布 + var that = this; + var client = app.mqttParam.client; + if (client && client.isConnected()) { + var message = new Paho.Message(message); + message.destinationName = topic; + message.qos = qos; + message.retained = retained; + return client.send(message); + } else { + that.connect(function () { + client = app.mqttParam.client; + if (client && client.isConnected()) { + var message = new Paho.Message(message); + message.destinationName = topic; + message.qos = qos; + message.retained = retained; + return client.send(message); + return; + } + }); + wx.showToast({ + title: '发送失败', + icon: 'success', + duration: 2000 + }); + } + }, + + setOnMessageArrived: function (onMessageArrived) { + if (typeof onMessageArrived === 'function') { + onMessageArrived(); + } + }, + + startReconnect: function () { + // 重连机制 + var that = this; + var interval = app.interval; + clearInterval(interval); + interval = setInterval(function () { + var client = app.mqttParam.client; + if (client != null) { + that.connect(); + } + }, 10000); + }, + + setOnConnectionLost: function (onConnectionLost) { + if (typeof onConnectionLost === 'function') { + onConnectionLost(); + } + } +} + +module.exports = { + api: mqttApi +} \ No newline at end of file diff --git a/utils/mqttMessage.js b/utils/mqttMessage.js new file mode 100644 index 0000000..577f20c --- /dev/null +++ b/utils/mqttMessage.js @@ -0,0 +1,156 @@ +const app = getApp() +const mqtt = require("mqtt.js") +/** + * + */ +var mqttClient = function() { + app.client = mqtt.connect('wxs://iotv2.ffcygl.com/mqtt', { + // 心跳请求,单位s + keepalive: 30, + clientId: app.openId, + protocolId: 'MQTT', + protocolVersion: 4, + username: 'admin', + password: 'admin', + reconnectPeriod: 5000, + }); + + app.client.on('message', function(topic, message, packet) { + console.log(message.toString()); + if (app.that) app.that.onMessage(topic, JSON.parse(message.toString())); + }); + + app.client.on('connect', function(err) { + console.log("监听连接状态", err); + }); + + app.client.on('close', function(res) { + // console.log("监听连接关闭"); + if (!app.client._activeEndFlag) { + // 重新建立连接 + app.client.reconnect() + } + }); + + app.client.on('error', function(err) { + console.log("连接失败", err); + }); +} + +/** + * mqtt 重新连接 + */ +var mqttConnect = function() { + if (app.client == null) { + mqttClient(); + } + app.client.reconnect() +} + +/** + * mqtt 断开连接 + */ +var mqttClose = function() { + if (app.client == null) { + mqttClient(); + } + app.client.end(true, function(err) { + console.log("断开连接", err); + }); +} + +/** + * 订阅主题 + */ +var mqttSubscribe = function(top, page) { + if (app.client == null) { + mqttClient(); + } + if (top == 1 && app.appTop1) { + mqttunSubscribe(app.appTop1); + } + if (top == 2 && app.appTop2) { + mqttunSubscribe(app.appTop2); + } + + console.log("开始订阅信息" + top, mqttTop(top)); + app.client.subscribe(mqttTop(top), function(err) { + console.log("订阅主题" + mqttTop(top), err); + }) +} + +/** + * 订阅主题 + */ +var mqttSubscribe1 = function(top) { + app.client.subscribe(top, function(err) { + console.log("订阅主题", err); + }) +} + +/** + * 取消订阅 + */ +var mqttunSubscribe = function(top) { + app.client.unsubscribe(top, function(err) { + console.log("取消订阅", err,top); + }); +} + +/** + * 发布订阅 + */ +var mqttPubish = function(top) { + if (app.client == null) { + mqttClient(); + } + app.client.publish(top, "hello"); +} + +/** + * 接收消息 + */ +var mqttOn = function(that) { + if (app.client == null) { + mqttClient(); + } + app.that = that; +} + +/** + * 订单主题 + */ +var mqttTop = function(type) { + var store = wx.getStorageSync("store"); + var tenantId = app.globalData.tenantId; + var storeId = store.storeId; + if (type == 1) { // 接单主题 + return app.appTop1 = app.baseTopic + "/" + app.openId; + } + + if (type == 2) { // 售罄主题 + return app.appTop2 = app.baseTopic2 + "/" + tenantId + "/" + storeId; + } + + if (type == 3) { + return app.getPayCode(); + } + if (type == 4) { + return app.getCouponConsumeCode(app.globalData.oldCode); + } + if (type == 5) { + return app.appTop3 = app.baseTopic3+ "/" + tenantId + "/" + storeId; + } + if(type==6) { //骑手接单 + var orderNo=app.globalData.orderNo; + return app.baseTopic4 + "/" + tenantId + "/" + orderNo; + } +} + +module.exports = { + mqttunSubscribe: mqttunSubscribe, + mqttSubscribe: mqttSubscribe, + mqttPubish: mqttPubish, + mqttOn: mqttOn, + mqttTop: mqttTop +} \ No newline at end of file diff --git a/utils/mqttSubstribe.js b/utils/mqttSubstribe.js new file mode 100644 index 0000000..205ad8e --- /dev/null +++ b/utils/mqttSubstribe.js @@ -0,0 +1,257 @@ +const app = getApp(); +const { + Client, + Message +} = require('paho-mqtt.js') + +var page = app.page; +// 初始化订阅信息 +var init = function() { + + if (undefined == app.wid || app.wid.length == 0) { + console.error("mqtt wid: 值等于undefined或者长度等于0"); + return false; + } + if (undefined == app.userId || app.userId.length == 0) { + console.error("mqtt userId: 值等于undefined或者长度等于0 "); + return false; + } + + var store = wx.getStorageSync("store"); + app.topic = []; + var tenantId = store.tenantId; + var storeId = store.id; + var topic1 = app.baseTopic + "/" + app.wid + "/" + app.userId; + app.topic.push(topic1); + var topic2 = app.baseTopic2 + "/" + tenantId + "/" + storeId; + app.topic.push(topic2); + return true; +}; + +var randomString = function(len) { + len = len || 32; + var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; + var maxPos = $chars.length; + var pwd = ''; + for (let i = 0; i < len; i++) { + pwd += $chars.charAt(Math.floor(Math.random() * maxPos)); + } + return pwd; +}; +// 订阅 +var subscribe = function(filter, subscribeOptions) { + var client = app.client; + if (client && client.isConnected()) { + for (var i = 0; i < filter.length; i++) { + var topic = filter[i]; + client.subscribe(topic, subscribeOptions); + } + return; + } +}; + +// 发布 +var publish = function(topic, message, qos = 2, retained = false) { + var client = app.client; + if (client && client.isConnected()) { + var message = new Message(message); + message.destinationName = topic; + message.qos = qos; + message.retained = retained; + console.log("发布成功"); + return client.send(message); + } + console.log("发布失败"); +}; + +var setOnMessageArrived = function(onMessageArrived) { + if (typeof onMessageArrived === 'function') { + this.data.onMessageArrived = onMessageArrived + } +}; + +var setOnConnectionLost = function(onConnectionLost) { + if (typeof onConnectionLost === 'function') { + this.data.onConnectionLost = onConnectionLost + } +}; + +var doSubscribe = function() { + this.subscribe(app.topic, { + qos: 2 + }) +}; + +var doPublish = function() { + var topic = app.topic[1]; + var content = { + "type": 1, //(1售罄2开卖) + "isAll": false, + "productIds": "644721258512125952", //,逗号分隔 + "date": "2018-08-16 00:00:00", + "storeId": "" + } + this.publish(topic, JSON.stringify(content), 1, false) +}; + +/** + * 链接 + */ +var doConnect = function() { + var that = this; + if (app.client && app.client.isConnected()) { + // 取消连接 + // app.client.disconnect(); + console.log("mqtt 已处于连接中"); + return; + } + + var initFlag = that.init(); + if (initFlag) { + var client = app.client; + if (!client) { + client = new Client('wss://iotv2.ffcygl.com/mqtt', app.openId); + } + + client.connect({ + useSSL: true, + cleanSession: true, + keepAliveInterval: 30, + onSuccess: function() { + + app.client = client; + console.log("mqtt 连接成功 ++++++++++ 发布订阅消息"); + // console.log(app.topic); + // 订阅消息 + that.doSubscribe(); + // 接收到消息 + client.onMessageArrived = function(msg) { + console.log("++++++++++++++++++++++++++ mqtt 消息通知"); + var result = JSON.parse(msg.payloadString); + var topic = msg.topic; + var reg = RegExp(/wxdc\/sell/); + if (topic.match(reg)) { + if (app.page) { + app.page.doHandleMqttMessage(result); + } + } else { + console.log("订单") + that.doSomeThing(result); + } + }; + + // 链接丢失 + client.onConnectionLost = function(responseObject) { + console.error("mqtt 链接丢失"); + console.log(responseObject); + }; + + + // setInterval(function() { + // that.doPublish(); + // }, 5000); + + } + }); + } else { + console.error("mqtt 检测到userId或者wid异常 暂不连接"); + } +}; + +var reaportConnect = function() { + if (null != app.client && app.client && app.client.isConnected()) { + //this.doSubscribe(); + } else { + app.client = null; + this.doConnect(); + } +} + +/** + * 取消订阅主题 + */ +var unsubscribe = function() { + console.log("============取消订阅主题"); + console.log(app.topic); + var client = app.client; + if (!client) { + // 取消连接 + return; + } + + // 取消订阅 + if (client && client.isConnected()) { + var topic1 = app.topic[1]; + client.unsubscribe(topic1, 2); + + // 重新发布订阅消息 + var initFlag = this.init(); + var topic2 = app.topic[1]; + + // console.log("重新发布订阅消息============" + JSON.stringify(topic)); + client.subscribe(topic2, { + qos: 2 + }); + } + +} + +/** + * 收到通知后业务处理 + */ +var doSomeThing = function(result) { + console.log("----------", result); + // 如果当前是在订单详情页面 重新加载订单数据 + var pages = getCurrentPages() + var currentPage = pages[pages.length - 1] + var url = currentPage.route + if (url.indexOf("detail/detail") > -1) { + currentPage.loadOrderInfo(); + return; + } + if (undefined != result) { + var orderNo = result.orderNo; + orderNo = orderNo.substring(orderNo.length - 6, orderNo.length); + var msg = ""; + switch (result.type) { + case 3: + msg = "抱歉,商家取消了订单 订单尾号:" + orderNo; + break; + case 4: + msg = "商家已接单啦 取餐码:" + result.takeNo; + break; + case 5: + msg = "商家已退单 订单尾号:" + orderNo; + break; + case 6: + msg = "抱歉,商家拒绝了您的退款请求 订单尾号:" + orderNo; + break; + default: + } + wx.showModal({ + title: "提示", + content: msg, + showCancel: false, + success: function(res) { + if (res.confirm) { + app.page.doCancelOrder(); + } + } + }); + } +} + +module.exports = { + init: init, + randomString: randomString, + subscribe: subscribe, + unsubscribe: unsubscribe, + publish: publish, + setOnMessageArrived: setOnMessageArrived, + setOnConnectionLost: setOnConnectionLost, + doSubscribe: doSubscribe, + doPublish: doPublish, + doConnect: doConnect, + reaportConnect: reaportConnect, + doSomeThing: doSomeThing +} \ No newline at end of file diff --git a/utils/msg.js b/utils/msg.js new file mode 100644 index 0000000..896d7df --- /dev/null +++ b/utils/msg.js @@ -0,0 +1,12 @@ +const showMsg = function(title, msg) { + wx.showModal({ + title: title, + content: msg, + showCancel: false, + }); +} + + +module.exports = { + showMsg: showMsg +} \ No newline at end of file diff --git a/utils/myOrder.js b/utils/myOrder.js new file mode 100644 index 0000000..6d1bd92 --- /dev/null +++ b/utils/myOrder.js @@ -0,0 +1,1322 @@ +var app = getApp(); +var utils = require('utils.js'); +/** + * + */ +var cy_addItem = function (product) { + + var tempSpec = []; // 规格 + var makeList = []; // 做法 + var ids = ""; + var idsName = ""; + var isContain = false; + var price=0; + var member + // 遍历商品规格 + if (product.specList) { + for (var spec of product.specList) { + if (spec.checked || product.specList.length == 1) { + tempSpec.push(spec); + ids = spec.specId; + idsName = spec.specName; + + break; + } + } + } + + // 遍历商品的做法 + if (product.makeList) { + for (var make of product.makeList) { + for (var detail of make.list) { + if (detail.checked) { + makeList.push(detail); + ids = ids + "|" + detail.makeId; + idsName = idsName + "、" + detail.showName; + } + } + } + } + + + // + for (var item of app.orderTicket.orderList) { + var tempIds = item.ids; // 规格id + if (tempIds == ids) { + item.count = item.count + 1; + isContain = true; + } + } + + + // + if (!isContain) { + product.ids = ids; + var tempProduct = JSON.parse(JSON.stringify(product)); + tempProduct.ids = ids; + tempProduct.idsName = idsName; + tempProduct.showName = idsName; + tempProduct.isSuit = 1; + tempProduct.count = 1; + tempProduct.specList = tempSpec; + tempProduct.makeList = makeList; + app.orderTicket.orderList.push(tempProduct); + } +} + +/** + * + */ +var cy_subItem = function (product) { + var tempIds = product.ids; + // 遍历商品规格 + if (product.specList) { + for (var spec of product.specList) { + if (spec.checked || product.specList.length == 1) { + tempIds = spec.specId; + break; + } + } + } + // 遍历商品的做法 + if (product.makeList) { + for (var make of product.makeList) { + for (var detail of make.list) { + if (detail.checked) { + tempIds = tempIds + "|" + detail.makeId; + } + } + } + } + for (var i = 0; i < app.orderTicket.orderList.length; i++) { + var item = app.orderTicket.orderList[i]; + var ids = item.ids; // 规格id + if (tempIds == ids) { + item.count = item.count - 1; + if (item.count == 0) { + app.orderTicket.orderList.remove(i); + break; + } + } + } +} + +/** + * + */ +var cy_getProductList = function (list) { + for (var product of list) { + product.sumCount = 0; + for (var item of app.orderTicket.orderList) { + if (item.isSuit == 3) { + continue; + } + if (item.productId == product.productId) { + product.sumCount = product.sumCount + item.count; + } + } + } +} + +/** + * 多规格商品数量 + */ +var cy_getProductCount = function (product) { + + var ids = ""; + for (var i = 0; i < product.specList.length; i++) { + var spec = product.specList[i]; + if (spec.checked) { + ids = spec.specId; + } + } + + for (var i = 0; i < product.makeList.length; i++) { + var makeListTemp = product.makeList[i]; + for (var j = 0; j < makeListTemp.list.length; j++) { + var make = makeListTemp.list[j]; + if (make.checked) { + ids = ids + "|" + make.makeId; + } + } + } + + var productCount = 0; + for (var item of app.orderTicket.orderList) { + if (item.ids == ids) { + if (item.isSuit != 3) { + productCount = item.count; + } + } + } + return productCount; +} + + +/** + * 默认规格和做法ids + */ +var cy_getIds = function (product) { + var ids = ""; + if (product.specList) { // 选择 + + for (var i = 0; i < product.specList.length; i++) { + var spec = product.specList[i]; + spec.checked = false; + // if (i == 0) { + // spec.checked = true; + // } + if (spec.stock > 0) { + spec.checked = true; + } + if (spec.checked) { + ids = spec.specId; + break; + } + } + } + + + if (product.makeList) { + for (var make of product.makeList) { + for (var i = 0; i < make.list.length; i++) { + var detail = make.list[i]; + if (detail.checked) { + ids = ids + "|" + detail.makeId + } + } + } + } + return ids; +} + +/** + * 默认规格做法名称idsName + */ +var cy_getIdsName = function (product) { + var idsName = ""; + if (product.specList) { + // idsName = product.specList[0].specName; + for (var i = 0; i < product.specList.length; i++) { + var spec = product.specList[i]; + spec.checked = false; + // if (i == 0) { + // spec.checked = true; + // } + if (spec.stock > 0) { + spec.checked = true; + idsName = spec.specName; + break; + } + } + } + + if (product.makeList) { + for (var make of product.makeList) { + for (var i = 0; i < make.list.length; i++) { + var detail = make.list[i]; + var showName = detail.makeName + (detail.addPrice == 0 ? "" : ("+" + detail.addPrice + "元")); + detail.showName = showName; + } + } + } + return idsName; +} + +/** + * 商品销售订单 (小程序订单的组合) + */ +var getOrder = function () { + var date = app.utils.getFormatTime(new Date(), 1); + var order = {}; + order.id = getDaySeo(1); // 主单id + order.memberId = ""; // 会员id + order.openId = wx.getStorageSync("openId"); // 会员openId + order.sourceType = 0; // 订单来源 + order.sourceType = ""; // 用户头像 + order.memberName = ""; // 会员姓名 + order.mobile = app.orderTicket.phone; // 手机号 + order.storeId = app.orderTicket.storeId; // 门店id + order.storeNo = app.orderTicket.storeNo; // 门店编号 + order.storeName = app.orderTicket.storeName; // 门店名称 + order.saleDate = date; // 销售时间 + order.tableNo = app.orderTicket.tableNo ? app.orderTicket.tableNo : ""; // 餐桌号 + order.tableName = app.orderTicket.tableName == "选择餐桌" ? "" : app.orderTicket.tableName; // 桌台名称 + order.people = app.orderTicket.people; // 人数 + order.busMode = wx.getStorageSync('mode') || app.orderTicket.busMode; // 营业模式(0堂食1外带2预定3外卖) + order.reserveTime = app.orderTicket.reserveTime; // 预定时间 + order.deliverFee = 0; // 配送费 默认为零下面根据营业模式(busMode = 3 外卖)进行计算 + order.packageFee = 0; // 打包费 默认为零下面根据营业模式(busMode = 1 或者 busMode = 3)进行计算 + order.amount = 0; // 消费金额 = 商品的总金额 + 做法的总金额 + order.discount = 1; // 优惠率 + order.discountTotal = 0; // 优惠总金额 + order.receivable = 0; // 应收金额 = 商品消费金额 - 优惠金额 (不含餐盒费和配送费) + order.maling = 0; // 抹零金额 + order.paid = 0; // 实收金额 = 应收金额 (不含餐盒费和配送费) + order.noOrg = ""; // 原单号 + order.backCause = ""; // 退单原因 + order.weeker = app.utils.getWeeker(); // 周几(星期二) + order.ext2 = app.orderTicket.ext2; // 订单备注 + // order.ticket_deliver ={} ; //配送信息 + order.ticket_info = []; // 订单优惠明细 同一类商品优惠的合计 + order.order_product = []; // 订单产品明细 + + if (order.busMode == 0) { // 堂食模式 + order.packageFee = 0; // 餐盒费 + order.deliverFee = 0; // 配送费 + } + + if (order.busMode == 1) { // 打包外带 + order.deliverFee = 0; + if (app.orderTicket.boxFee) { + if (app.globalData.userIsFee == 1 || app.globalData.takeOutBoxFeeFlag == 1) { + order.packageFee = app.orderTicket.boxFee; // 全部的餐盒费 + } + } + } + + if (order.busMode == 2) { // 预订 + + } + + if (order.busMode == 3) { // 外卖 + order.deliverFee = app.orderTicket.deliverFee; // 配送费根据门店后台配送获取 + order.packageFee = app.orderTicket.boxFee; // 餐盒非 + order.ticket_deliver = app.orderTicket.ticket_deliver; // 配送费 + order.ticket_deliver.id = order.id; + } + + var tempInfo = null; + var memberTempInfo = []; + var last = false; + var couponTotal = 0; + var memberPriceTemInfoItem = {}; + var discountMemberMoney = 0; + var giftProductTempInfo = ""; + var promotionDiscountTemInfo = []; + for (var i = 0; i < app.orderTicket.orderList.length; i++) { + if (i == app.orderTicket.orderList.length - 1) { + last = true; + } + var product = app.orderTicket.orderList[i]; + + var tempProduct = getOrderItem(product, last, app.orderTicket.isMember); + + tempProduct.parentId = order.id; //父记录id + tempProduct.saleDate = date; //销售时间 + tempProduct.lineNo = i; // 行号 + order.order_product.push(tempProduct); + + console.log("----------------------1", order); + + if (app.orderTicket.coupon) { // 使用优惠券 + // 电子代金券 = 80, 电子折扣券 = 81, 电子兑换券 = 82, 电子商品券 = 83, 会员价优惠 = 8 + //兑换券不在这里处理 + if (app.orderTicket.coupon.cardType != 'GIFT') { + if (tempProduct.isSuit != 3) { // 套菜明细不参与优惠券的优惠券分摊 + useCoupon(tempProduct, last, couponTotal); + couponTotal += parseFloat(tempProduct.discountTotal); // 优惠的合计金额 + + console.log("couponTotal" + couponTotal); + + // 统计优惠券的优惠 + for (var product_info of tempProduct.product_info) { + for (var ticket_info of order.ticket_info) { + if (ticket_info.type == product_info.type) { + tempInfo = ticket_info; + } + } + + if (tempInfo == null) { + tempInfo = {}; + tempInfo.id = getDaySeo(3); + tempInfo.type = product_info.type; + tempInfo.info = product_info.info; + tempInfo.discountMoney = product_info.discountMoney; + } else { + tempInfo.discountMoney = couponTotal; + // if (tempInfo.type >= 80) { + tempInfo.info = "优惠券优惠" + couponTotal + "元"; + // } + } + + } + } + } + } + + + /** + * 会员价优惠(折扣券禁止使用有会员优惠) + */ + + var discountFlag = true; + if (app.orderTicket.coupon) { + if (app.orderTicket.coupon.cardType == "DISCOUNT") { + discountFlag = false; + } + } + + if (discountFlag && tempProduct.isSuit != 3) { + if (app.orderTicket.isMember == 1) { + //判断促销是否支持折上折 + if (tempProduct.isRepeatDiscount == 1 | !tempProduct.discountRule) { + if (app.globalData.memberPriceEnabled == 1 && tempProduct.giftProduct != "1") { + var isMemberPrice = 0; + if(app.globalData.cardInfo){ + isMemberPrice = app.globalData.cardInfo.memberPriceFlag; + } + if(isMemberPrice == 1){ + useMemberPrice(tempProduct); + } + + var productInfoLength = tempProduct.product_info.length; + for (var product_info of tempProduct.product_info) { + if (product_info.type == 3) { + discountMemberMoney += parseFloat(product_info.discountMoney); + memberPriceTemInfoItem.id = getDaySeo(3); + memberPriceTemInfoItem.type = product_info.type; + } + } + } + } + } + } + + + + // 计算套餐的做法加价 + // if (tempProduct.isSuit == 3 && tempProduct.giftProduct != '1') { + // order.amount += parseFloat(tempProduct.addPriceTotal); + // order.paid += parseFloat(tempProduct.addPriceTotal); + // order.receivable += parseFloat(tempProduct.addPriceTotal); + // } + // if (tempProduct.isSuit ==2 && tempProduct.giftProduct != '1') { + // order.amount += parseFloat(tempProduct.amount)-parseFloat(tempProduct.price); + // order.paid += parseFloat(tempProduct.amount); + // order.receivable += parseFloat(tempProduct.amount); + // } + //兑换券 + if (tempProduct.giftProduct == '1') { + giftProductTempInfo = giftCoupon(order, tempProduct, giftProductTempInfo); + } + + //计算餐饮促销活动 + if (tempProduct.isSuit != 3 && tempProduct.isSuit != 2) { + var tempProductInfo = promotionCompute(tempProduct); + if (tempProductInfo) { + if (promotionDiscountTemInfo.length > 0) { + var promotionSize = promotionDiscountTemInfo.length; + for (var i; i < promotionSize; i++) { + var item = promotionDiscountTemInfo[i]; + if (item.type == tempProductInfo.type) { + item.discountMoney = parseFloat(item.discountMoney) + parseFloat(tempProductInfo.discountMoney); + if (item.type == 1) { + item.info = "促销折扣优惠 " + item.discountMoney + " 元 "; + } + if (item.type == 9) { + item.info = "促销立减优惠 " + item.discountMoney + " 元 "; + } + } else { + promotionDiscountTemInfo.push(tempProductInfo); + } + } + } else { + promotionDiscountTemInfo.push(tempProductInfo); + } + } + } + + if (tempProduct.isSuit != 3 && tempProduct.giftProduct != '1') { + order.discountTotal += parseFloat(tempProduct.discountTotal); + order.amount += parseFloat(tempProduct.amountTotal); + order.paid += parseFloat(tempProduct.receivableTotal); + order.receivable += parseFloat(tempProduct.receivableTotal); + + //重新定义商品的折后价 + tempProduct.discountPrice = (parseFloat(tempProduct.price) - (parseFloat(tempProduct.discountTotal) / tempProduct.count)).toFixed(2); + //重新定义商品的优惠率 + tempProduct.discount = tempProduct.discountTotal / tempProduct.amountTotal; + } + } + + + // 订单优惠信息 + if (tempInfo) { + order.ticket_info.push(tempInfo); + // order.discountTotal=parseFloat(order.discountTotal) +parseFloat(tempInfo.discountMoney); + } + + //促销优惠信息 + if (promotionDiscountTemInfo.length > 0) { + for (var item of promotionDiscountTemInfo) { + order.ticket_info.push(item); + } + } + + if (giftProductTempInfo) { + order.ticket_info.push(giftProductTempInfo); + } + + if (discountMemberMoney > 0) { + // order.discountTotal=parseFloat(order.discountTotal) + parseFloat(discountMemberMoney); + memberPriceTemInfoItem.discountMoney = discountMemberMoney; + memberPriceTemInfoItem.info = "会员优惠" + discountMemberMoney + "元"; + order.ticket_info.push(memberPriceTemInfoItem); + } + + + if (order.receivable + order.discountTotal != 0) { + order.discount = (order.discountTotal / (order.receivable + order.discountTotal) * 100).toFixed(2); + } + order.discountTotal = order.discountTotal.toFixed(2); + + order.amount = parseFloat(order.amount.toFixed(2)) + order.paid = parseFloat(order.paid.toFixed(2)) + order.receivable = order.receivable.toFixed(2) + + if(wx.getStorageSync("user")){ + order.memberName =wx.getStorageSync("user").nickName; + } + + if(wx.getStorageSync("cardInfo")){ + order.memberId=wx.getStorageSync("cardInfo").memberId; + order.mobile=wx.getStorageSync("cardInfo").mobile; + order.memberName =wx.getStorageSync("cardInfo").name; + } + return order; +} + + +/** + * 商品销售订单 (小程序订单的组合) + */ +var getMemberCreateOrder = function () { + var date = app.utils.getFormatTime(new Date(), 1); + var order = {}; + order.id = getDaySeo(1); // 主单id + order.memberId = ""; // 会员id + order.openId = wx.getStorageSync("openId"); // 会员openId + order.sourceType = 0; // 订单来源 + order.sourceType = ""; // 用户头像 + order.memberName = ""; // 会员姓名 + order.mobile = app.orderTicket.phone; // 手机号 + order.storeId = app.orderTicket.storeId; // 门店id + order.storeNo = app.orderTicket.storeNo; // 门店编号 + order.storeName = app.orderTicket.storeName; // 门店名称 + order.saleDate = date; // 销售时间 + order.tableNo = app.orderTicket.tableNo ? app.orderTicket.tableNo : ""; // 餐桌号 + order.tableName = app.orderTicket.tableName == "选择餐桌" ? "" : app.orderTicket.tableName; // 桌台名称 + order.people = app.orderTicket.people; // 人数 + order.busMode = app.orderTicket.busMode // 营业模式(0堂食1外带2预定3外卖) + order.reserveTime = app.orderTicket.reserveTime; // 预定时间 + order.deliverFee = 0; // 配送费 默认为零下面根据营业模式(busMode = 3 外卖)进行计算 + order.packageFee = 0; // 打包费 默认为零下面根据营业模式(busMode = 1 或者 busMode = 3)进行计算 + order.amount = 0; // 消费金额 = 商品的总金额 + 做法的总金额 + order.discount = 1; // 优惠率 + order.discountTotal = 0; // 优惠总金额 + order.receivable = 0; // 应收金额 = 商品消费金额 - 优惠金额 (不含餐盒费和配送费) + order.maling = 0; // 抹零金额 + order.paid = 0; // 实收金额 = 应收金额 (不含餐盒费和配送费) + order.noOrg = ""; // 原单号 + order.backCause = ""; // 退单原因 + order.weeker = app.utils.getWeeker(); // 周几(星期二) + order.ext2 = app.orderTicket.ext2; // 订单备注 + // order.ticket_deliver ={} ; //配送信息 + order.ticket_info = []; // 订单优惠明细 同一类商品优惠的合计 + order.order_product = []; // 订单产品明细 + + if (order.busMode == 0) { // 堂食模式 + order.packageFee = 0; // 餐盒费 + order.deliverFee = 0; // 配送费 + } + if (order.busMode == 1) { // 打包外带 + order.deliverFee = 0; + if (app.orderTicket.boxFee) { + if (app.globalData.userIsFee == 1 || app.globalData.takeOutBoxFeeFlag == 1) { + order.packageFee = app.orderTicket.boxFee; // 全部的餐盒费 + } + } + } + if (order.busMode == 3) { // 外卖 + order.deliverFee = app.orderTicket.deliverFee; // 配送费根据门店后台配送获取 + order.packageFee = app.orderTicket.boxFee; // 餐盒非 + order.ticket_deliver = app.orderTicket.ticket_deliver; // 配送费 + order.ticket_deliver.id = order.id; + } + var tempInfo = null; + var memberTempInfo = []; + var last = false; + var couponTotal = 0; + var memberPriceTemInfoItem = {}; + var discountMemberMoney = 0; + var giftProductTempInfo = ""; + var promotionDiscountTemInfo = []; + for (var i = 0; i < app.orderTicket.orderList.length; i++) { + if (i == app.orderTicket.orderList.length - 1) { + last = true; + } + var product = app.orderTicket.orderList[i]; + var tempProduct = getOrderItem(product, last, app.orderTicket.isMember); + tempProduct.parentId = order.id; //父记录id + tempProduct.saleDate = date; //销售时间 + tempProduct.lineNo = i; // 行号 + order.order_product.push(tempProduct); + if (app.orderTicket.coupon) { // 使用优惠券 + // 电子代金券 = 80, 电子折扣券 = 81, 电子兑换券 = 82, 电子商品券 = 83, 会员价优惠 = 8 + //兑换券不在这里处理 + if (app.orderTicket.coupon.cardType != 'GIFT') { + if (tempProduct.isSuit != 3) { // 套菜明细不参与优惠券的优惠券分摊 + useCoupon(tempProduct, last, couponTotal); + couponTotal += parseFloat(tempProduct.discountTotal); // 优惠的合计金额 + // 统计优惠券的优惠 + for (var product_info of tempProduct.product_info) { + for (var ticket_info of order.ticket_info) { + if (ticket_info.type == product_info.type) { + tempInfo = ticket_info; + } + } + if (tempInfo == null) { + tempInfo = {}; + tempInfo.id = getDaySeo(3); + tempInfo.type = product_info.type; + tempInfo.info = product_info.info; + tempInfo.discountMoney = product_info.discountMoney; + } else { + tempInfo.discountMoney = couponTotal; + tempInfo.info = "优惠券优惠" + couponTotal + "元"; + } + + } + } + } + } + /** + * 会员价优惠(折扣券禁止使用有会员优惠) + */ + + var discountFlag = true; + if (app.orderTicket.coupon) { + if (app.orderTicket.coupon.cardType == "DISCOUNT") { + discountFlag = false; + } + } + + if (discountFlag && tempProduct.isSuit != 3) { + //判断促销是否支持折上折 + if (tempProduct.isRepeatDiscount == 1 | !tempProduct.discountRule) { + + /** + * 根据会员信息判断是否使用会员价 + */ + console.log("会员卡信息", app.globalData.cardInfo); + var isMemberPrice = 0; + if(app.globalData.cardInfo){ + isMemberPrice = app.globalData.cardInfo.memberPriceFlag; + } + if(isMemberPrice == 1){ + useMemberPrice(tempProduct); + } + for (var product_info of tempProduct.product_info) { + if (product_info.type == 3) { + discountMemberMoney += parseFloat(product_info.discountMoney); + memberPriceTemInfoItem.id = getDaySeo(3); + memberPriceTemInfoItem.type = product_info.type; + } + } + } + } + + //兑换券 + if (tempProduct.giftProduct == '1') { + giftProductTempInfo = giftCoupon(order, tempProduct, giftProductTempInfo); + } + + //计算餐饮促销活动 + if (tempProduct.isSuit != 3 && tempProduct.isSuit != 2) { + var tempProductInfo = promotionCompute(tempProduct); + if (tempProductInfo) { + if (promotionDiscountTemInfo.length > 0) { + var promotionSize = promotionDiscountTemInfo.length; + for (var i; i < promotionSize; i++) { + var item = promotionDiscountTemInfo[i]; + if (item.type == tempProductInfo.type) { + item.discountMoney = parseFloat(item.discountMoney) + parseFloat(tempProductInfo.discountMoney); + if (item.type == 1) { + item.info = "促销折扣优惠 " + item.discountMoney + " 元 "; + } + if (item.type == 9) { + item.info = "促销立减优惠 " + item.discountMoney + " 元 "; + } + } else { + promotionDiscountTemInfo.push(tempProductInfo); + } + } + } else { + promotionDiscountTemInfo.push(tempProductInfo); + } + } + } + + + if (tempProduct.isSuit != 3 && tempProduct.giftProduct != '1') { + order.discountTotal += parseFloat(tempProduct.discountTotal); + order.amount += parseFloat(tempProduct.amountTotal); + order.paid += parseFloat(tempProduct.receivableTotal); + order.receivable += parseFloat(tempProduct.receivableTotal); + + //重新定义商品的折后价 + tempProduct.discountPrice = (parseFloat(tempProduct.price) - (parseFloat(tempProduct.discountTotal) / tempProduct.count)).toFixed(2); + //重新定义商品的优惠率 + tempProduct.discount = tempProduct.discountTotal / tempProduct.amountTotal; + } + } + + // 订单优惠信息 + if (tempInfo) { + order.ticket_info.push(tempInfo); + } + + //促销优惠信息 + if (promotionDiscountTemInfo.length > 0) { + for (var item of promotionDiscountTemInfo) { + order.ticket_info.push(item); + } + } + + if (giftProductTempInfo) { + order.ticket_info.push(giftProductTempInfo); + } + console.log(discountMemberMoney); + if (discountMemberMoney > 0) { + memberPriceTemInfoItem.discountMoney = discountMemberMoney; + memberPriceTemInfoItem.info = "会员优惠" + discountMemberMoney + "元"; + order.ticket_info.push(memberPriceTemInfoItem); + } + + + if (order.receivable + order.discountTotal != 0) { + order.discount = (order.discountTotal / (order.receivable + order.discountTotal) * 100).toFixed(2); + } + order.discountTotal = order.discountTotal.toFixed(2); + + order.amount = parseFloat(order.amount.toFixed(2)) + order.paid = parseFloat(order.paid.toFixed(2)) + order.receivable = order.receivable.toFixed(2) + + + if(wx.getStorageSync("user")){ + order.memberName =wx.getStorageSync("user").nickName; + } + + if(wx.getStorageSync("cardInfo")){ + order.memberId=wx.getStorageSync("cardInfo").memberId; + order.mobile=wx.getStorageSync("cardInfo").mobile; + order.memberName =wx.getStorageSync("cardInfo").name; + } + + return order; +} + + +//计算餐饮促销活动 +function promotionCompute(tempProduct) { + //此商品是促销商品 + var tempProductInfo = ""; + if (tempProduct.promotionPrice > 0) { + //促销价优惠金额 + var discountPromotion = parseFloat(((parseFloat(tempProduct.price) - parseFloat(tempProduct.promotionPrice)) * tempProduct.count).toFixed(2)); + //设置折后价 + tempProduct.discountPrice = parseFloat((parseFloat(tempProduct.discountPrice) - parseFloat(discountPromotion)).toFixed(2)); + //设置总优惠 + tempProduct.discountTotal = parseFloat((parseFloat(tempProduct.discountTotal) + discountPromotion).toFixed(2)); + if (tempProduct.discountTotal > tempProduct.amountTotal) { + tempProduct.discountTotal = tempProduct.amountTotal; + } + //如果优惠券优惠会员价优惠 等优惠后的总实收 小于促销优惠让促销优惠等于现在的实收 + // var promotionDiscount = ((tempProduct.price - tempProduct.promotionPrice) * tempProduct.count).toFixed(2); + if (discountPromotion > tempProduct.receivableTotal) { + discountPromotion = tempProduct.receivableTotal; + } + tempProduct.receivableTotal = parseFloat((tempProduct.receivableTotal - discountPromotion).toFixed(2)); + tempProduct.receivable =parseFloat(( tempProduct.receivable - discountPromotion).toFixed(2)); + tempProductInfo = {}; + var discountRule = JSON.parse(tempProduct.discountRule); + + tempProductInfo.id = getDaySeo(3); + tempProductInfo.orderItemId = tempProduct.id; + tempProductInfo.discountMoney = discountPromotion; + if (discountRule.discountType == 0) { + tempProductInfo.type = 1; + tempProductInfo.info = "促销折扣优惠 " + discountPromotion + " 元 "; + tempProduct.product_info.push(tempProductInfo); + } else if (discountRule.discountType == 1) { + tempProductInfo.type = 9; + tempProductInfo.info = "促销立减优惠 " + discountPromotion + " 元 "; + tempProduct.product_info.push(tempProductInfo); + } + } + // var receivable = (parseFloat(tempProduct.receivable) - parseFloat(tempProduct.discountTotal)); + // if (!receivable > 0) { + // tempProduct.receivable = 0; + // } + // var receivableTotal = (parseFloat(tempProduct.receivableTotal) - parseFloat(tempProduct.discountTotal)); + // if (!receivableTotal > 0) { + // tempProduct.receivableTotal = 0; + // } + return tempProductInfo; +} + +//计算商品促销价 +function promotionComputePrice(product) { + var promotionDiscount = 0; //优惠金额 + if (product.discountRule) { + var discountRule = JSON.parse(product.discountRule); + var discount = discountRule.discount; + if (discount > 0) { + if (discountRule.discountType == 0) { + promotionDiscount = product.price * (100 - discount) * product.count / 100; + product.promotionPrice = (product.price - promotionDiscount).toFixed(2); + } + if (discountRule.discountType == 1) { + promotionDiscount = discount * product.count; + product.promotionPrice = product.price - discount; + } + } + } + return promotionDiscount; +} + +// 使用优惠券 +var useCoupon = function (tempProduct, isLast, couponTotal) { + var coupon = app.orderTicket.coupon; + var cardType = coupon.cardType; + if (cardType == "CASH") { // 代金券 + cashCoupon(coupon, tempProduct, isLast, couponTotal); + } + + if (cardType == "DISCOUNT") { // 折扣券 + discountCoupon(coupon, tempProduct); + } + +} + +/** + * 代金券 (做法不参与优惠计算) + * 1.代金券优惠计算方法:进行整单分摊计算, 单个商品的总价占整单总计的比例乘以优惠金额,计算出来本单的优惠金额, + * 2.最后一个商品的优惠等于优惠总金额减去前面所有商品的和 + * 3.coupon 优惠券信息 + * 4.tempProduct 单品信息 + */ +var cashCoupon = function (coupon, tempProduct, isLast, couponTotal) { + + console.log("==================", tempProduct); + + //重新找到订单的金额不包括餐盒费配送 主要用于促销之后分摊不干净问题 + var receivable = 0; + for (var item of app.orderTicket.orderList) { + if (item.isSuit != 3) { + if (item.promotionTotalPrice && item.promotionTotalPrice > 0) { //在上个页面计算的促销总价包括加价等 + receivable += item.promotionTotalPrice; + } else { + receivable += item.amount; + } + } + } + + // 计算单品所占的比例, + // var scale = parseFloat(tempProduct.receivableTotal) / parseFloat(app.orderTicket.receivable); + var scale = parseFloat(tempProduct.receivableTotal) / parseFloat(receivable); + // 优惠金额 + var discountMoney = parseFloat(coupon.amount) * scale; + // 商品优惠后价格 前面封装 + tempProduct.discountPrice = (tempProduct.discountPrice - (discountMoney / tempProduct.count)).toFixed(2); + // 优惠率 + tempProduct.discount = 0; + if (tempProduct.receivableTotal > 0) { + tempProduct.discount = (discountMoney / tempProduct.receivableTotal).toFixed(2); + } + // 优惠券 + var tempProductInfo = {}; + tempProductInfo.id = getDaySeo(3); // + tempProductInfo.orderItemId = tempProduct.id; + tempProductInfo.type = 2; + tempProductInfo.discountMoney = discountMoney.toFixed(2); + + if (isLast) { // + tempProductInfo.discountMoney = (parseFloat(coupon.amount) - couponTotal).toFixed(2); + // 优惠总金额 + tempProduct.discountTotal = (parseFloat(tempProduct.discountTotal) + parseFloat(tempProductInfo.discountMoney)).toFixed(2); + tempProduct.receivableTotal = parseFloat((tempProduct.receivableTotal - tempProductInfo.discountMoney).toFixed(2)); + tempProduct.receivable = parseFloat((tempProduct.receivable - tempProductInfo.discountMoney).toFixed(2)); + } else { + couponTotal += discountMoney; + // 优惠总金额 + tempProduct.discountTotal = (parseFloat(tempProduct.discountTotal) + parseFloat(discountMoney)).toFixed(2); + tempProduct.receivableTotal = parseFloat((tempProduct.receivableTotal - tempProduct.discountTotal).toFixed(2)); + tempProduct.receivable = parseFloat((tempProduct.receivable - tempProduct.discountTotal).toFixed(2)); + } + tempProductInfo.info = "优惠券优惠" + tempProductInfo.discountMoney + "元"; + tempProduct.product_info.push(tempProductInfo); +} + +/** + * 折扣券 (做法不参与优惠券) + * + */ +var discountCoupon = function (coupon, tempProduct) { + console.log(tempProduct); + // 计算优惠金额 + var discountMoney = (1 - parseFloat(coupon.amount / 10)) * parseFloat(tempProduct.receivableTotal); + console.error("-------------------discountMoney", discountMoney) + coupon.reduceCost = parseInt(discountMoney.toFixed(2) * 100); + // 单品优惠后的价格 + tempProduct.discountPrice = (tempProduct.discountPrice - (discountMoney / tempProduct.count)).toFixed(2); + // 优惠的总金额 + tempProduct.discountTotal = discountMoney.toFixed(2); + + tempProduct.discount = 0; + if (tempProduct.amountTotal > 0) { + tempProduct.discount = (discountMoney / tempProduct.amountTotal).toFixed(2); + } + + tempProduct.receivableTotal = parseFloat((tempProduct.amountTotal - discountMoney).toFixed(2)); + tempProduct.receivable = parseFloat((tempProduct.receivable - discountMoney).toFixed(2)); + var tempProductInfo = {}; + tempProductInfo.id = getDaySeo(3); // + tempProductInfo.orderItemId = tempProduct.id; + tempProductInfo.type = 2; + tempProductInfo.discountMoney = discountMoney.toFixed(2); + tempProductInfo.info = "优惠券优惠" + tempProductInfo.discountMoney + "元"; + + console.log("---------------", tempProductInfo); + tempProduct.product_info.push(tempProductInfo); +} + +/** + * 兑换券 + */ +var giftCoupon = function (order, tempProduct, giftProductTempInfo) { + order.amount += parseFloat(tempProduct.amountTotal); + var giftProductTempInfoItem = {}; + giftProductTempInfoItem.id = getDaySeo(3); + giftProductTempInfoItem.type = 7; + giftProductTempInfoItem.orderItemId = tempProduct.id; + var discountAmount = tempProduct.price * tempProduct.count; + giftProductTempInfoItem.info = "商品兑换券优惠" + discountAmount + "元"; + giftProductTempInfoItem.discountMoney = discountAmount; + giftProductTempInfoItem.count = tempProduct.count; + order.discountTotal = parseFloat(order.discountTotal) + parseFloat(discountAmount); + tempProduct.product_info.push(giftProductTempInfoItem); + if (giftProductTempInfo) { + giftProductTempInfo.discountMoney = parseFloat(giftProductTempInfo.discountMoney) + parseFloat(discountAmount); + giftProductTempInfo.info = "商品兑换券总优惠 " + giftProductTempInfo.discountMoney + " 元 "; + } else { + if (discountAmount >= 0) { + giftProductTempInfo = {}; + giftProductTempInfo.id = getDaySeo(3); + giftProductTempInfo.type = 7; + giftProductTempInfo.info = "商品兑换券总优惠 " + discountAmount + " 元 "; + giftProductTempInfo.discountMoney = discountAmount; + } + } + return giftProductTempInfo; +} + +// 计算会员价 +var useMemberPrice = function (tempProduct) { + var tempProductInfo = {}; + // 会员优惠价格 + var disPrice = 0; + //判断是否是套餐 + if (tempProduct.isSuit == 2) { + console.error(" tempProduct ",tempProduct) + if(tempProduct.addPriceTotalConsumer){ + disPrice = parseFloat(tempProduct.addPriceOldPrice) - parseFloat(tempProduct.memberPrice)-parseFloat(tempProduct.addPriceTotalConsumer); + }else{ + disPrice = parseFloat(tempProduct.addPriceOldPrice) - parseFloat(tempProduct.memberPrice); + } + } else { + disPrice = parseFloat(tempProduct.price) - parseFloat(tempProduct.memberPrice); + } + if (disPrice > 0) { + // 会员优惠后的价格 + // if (tempProduct.promotionPrice > 0) { + // tempProduct.discountPrice = tempProduct.promotionPrice - disPrice; + // } else tempProduct.discountPrice = tempProduct.price - disPrice; + if ((tempProduct.discountPrice - disPrice) < 0) { + disPrice = tempProduct.discountPrice; + } + tempProduct.discountPrice = tempProduct.discountPrice - disPrice; + // 计算优惠金额 + var discountMoney = (disPrice * tempProduct.count).toFixed(2); + //计算前优惠总金额 + var oldDiscountTotal = tempProduct.discountTotal; + // 优惠总金额 + tempProduct.discountTotal = parseFloat(tempProduct.discountTotal) + parseFloat(discountMoney); + //商品总价 + var amountTotal = tempProduct.amountTotal; + if (tempProduct.discountTotal >= amountTotal) { + var newDiscountTotal = tempProduct.discountTotal; + tempProduct.discountTotal = amountTotal; + + tempProduct.discount = 0; + // 优惠率 + if (tempProduct.receivableTotal > 0) { + tempProduct.discount = (discountMoney / tempProduct.receivableTotal).toFixed(2); + } + if (oldDiscountTotal != amountTotal) { + discountMoney = parseFloat(newDiscountTotal) - parseFloat(oldDiscountTotal); + if (discountMoney > 0) { + tempProductInfo.id = getDaySeo(3); + tempProductInfo.type = 3; + tempProductInfo.orderItemId = tempProduct.id; + tempProductInfo.discountMoney = discountMoney; + tempProductInfo.info = "会员价优惠 " + discountMoney + " 元 "; + tempProduct.product_info.push(tempProductInfo); + } + // tempProduct.discount = (discountMoney / tempProduct.receivableTotal).toFixed(2); + } else { + tempProduct.discount = 0; + } + tempProduct.receivable = 0; + tempProduct.receivableTotal = 0; + } else { + tempProduct.discount = 0; + // 优惠率 + if (tempProduct.receivableTotal > 0) { + tempProduct.discount = (discountMoney / tempProduct.receivableTotal).toFixed(2); + } + // + tempProduct.receivableTotal = parseFloat((tempProduct.receivableTotal - discountMoney).toFixed(2)); + tempProduct.receivable = parseFloat((tempProduct.receivable - discountMoney).toFixed(2)); + if (discountMoney > 0) { + tempProductInfo.id = getDaySeo(3); + tempProductInfo.type = 3; + tempProductInfo.orderItemId = tempProduct.id; + tempProductInfo.discountMoney = discountMoney; + tempProductInfo.info = "会员价优惠 " + discountMoney + " 元 "; + tempProduct.product_info.push(tempProductInfo); + } + } + } +} + +// 订单明细信息 +/** + * product 商品信息 + * last 是否是最后一个 + * isMember 是否是会员 + */ +var getOrderItem = function (product, last, isMember) { + var productSpec = product.specList[0]; + var item = {}; + item.id = getDaySeo(2); + item.giftProduct = product.giftProduct; + // item.parentId = ""; // 父记录ID + item.productId = product.productId; // 菜品id + item.productNo = product.productNo; // 菜品编号 + item.productName = product.productName; //菜品名称 + item.productUnitId = product.productUnitId; // 菜品单位 + item.productUnitName = product.productUnitName; // 菜品名称 + item.productImageUrl = product.linkUrl; // 菜品图片URL + item.typePath = product.typePath; // 分类券路径 + item.seriesId = product.seriesId; // 顶级分类ID + item.seriesName = product.seriesName; // 顶级分类名称 + item.typeId = product.typeId; // 类别ID + item.typeName = product.typeName; // 类别名 + item.specId = productSpec.specId; // 规格ID + item.specName = productSpec.specName; // 规格名称 + item.count = product.count; // 数量 + item.rcount = 0; // 退菜数量 + item.price = 0; // 销售价格 + item.discountPrice = 0; // 折后价格 + item.priceOrg = 0; // 菜品原价 + item.isSuit = product.isSuit; // 是否套菜(1普通菜 2 主菜 3 明细菜) + item.suitId = ""; // 套菜ID + // item.saleDate = ""; // 销售时间 + item.amount = 0; // 消费金额 + item.discountTotal = 0; // 优惠额 + item.discount = 0; // 优惠率 + item.receivable = 0; //应收金额 + item.addPriceTotal = 0; //加价金额 + item.discountAddTotal = 0; // 加价优惠金额 + item.amountAddTotal = 0; // 加价消费金额 + item.amountTotal = 0; // 消费总额(消费金额 + 加价总额) + item.receivableTotal = 0; // 应收总额(应收金额 + 加价应收总额) + item.packageFee = 0; // 餐盒费 + // item.lineNo = 0; // 行号 + item.product_info = []; // 商品优惠明细 + item.product_make = []; // 商品做法明细 + item.memberPrice = productSpec.memberPrice; + item.isRepeatDiscount = product.isRepeatDiscount; // + item.addPriceTotalConsumer=product.addPriceTotalConsumer //套餐的总加价 + + //如果是套餐的商品给 设置价格方便计算会员价 + if(item.isSuit==2){ + item.addPriceOldPrice=product.addPriceOldPrice; //套餐价 + } + + if (product.giftProduct == '1') { //兑换的商品 + item.price = product.price; // 销售价格 + item.priceOrg = product.price; // 菜品原价 + item.discount = product.discount; // 优惠率 + item.amountTotal = product.amountTotal; // 消费总额(消费金额 + 加价总额) + } + + if (app.orderTicket.busMode == 0) { // 堂食 + item.packageFee = 0; + } + + if (app.orderTicket.busMode == 1 && app.globalData.userIsFee == 1) { // 外带 + item.packageFee = product.count * product.boxPrice; + } + + if (app.orderTicket.busMode == 2) { // 预订 + + } + + if (app.orderTicket.busMode == 3) { // 外卖和配送费 + item.packageFee = product.count * product.boxPrice; + } + if (item.isSuit == 3) { // 套餐明细 + item.suitId=product.suitId; + item.suitProductId=product.suitProductId; + var product_info = product.product_info[0]; + if(product_info){ + product_info.id = getDaySeo(3); + product_info.orderItemId = item.id; + item.product_info = product.product_info; + } + } + + + item.discountAddTotal = 0; + if (product.makeList) { + for (var make of product.makeList) { + var tempMake = getOrderItemMake(make); + tempMake.orderItemId = item.id; + item.product_make.push(tempMake); + + if (product.isSuit != 3) { + item.addPriceTotal += make.count * make.addPrice; + item.amountAddTotal += make.count * make.addPrice; + } + } + } + + /**计算套餐的做法加价 */ + if (product.isSuit == 2) { // 套餐主菜 + item.addPriceTotal = product.addPriceTotal; + item.amountAddTotal = product.addPriceTotal; + } + //如果有促销价 + if (productSpec.promotionPrice > 0) { + item.price = productSpec.price; + item.discountPrice = productSpec.price; + //主要计算会员价还有券的时候判断是否存在存在不参与会员价跟券的优惠 + item.promotionPrice = productSpec.promotionPrice; + item.discountRule = productSpec.discountRule; + } else { + //没有促销价 判断是不是套餐主菜 + if(product.isSuit == 2){ + item.price =item.addPriceOldPrice; + item.discountPrice=item.addPriceOldPrice; + }else{ + item.price = productSpec.price; + } + if (product.giftProduct != "1" && product.isSuit != 2) { + item.discountPrice = productSpec.price; + } + } + + if (isMember == 0) { // 微信支付 + if( product.isSuit==2){ + item.priceOrg = product.addPriceOldPrice; + }else{ + item.priceOrg = productSpec.price; + } + } + + if (isMember == 1) { // 会员卡支付 + if( product.isSuit==2){ + item.priceOrg = product.addPriceOldPrice; + }else{ + item.priceOrg = productSpec.price; + } + } + if( product.isSuit==2){ + item.amount = productSpec.price * item.count; + item.amountTotal = productSpec.price * item.count + item.amountAddTotal; + item.receivable = productSpec.price * item.count; + item.receivableTotal = item.receivable + item.addPriceTotal; + }else{ + item.amount = item.price * item.count; + item.amountTotal = item.price * item.count + item.amountAddTotal; + item.receivable = item.discountPrice * item.count; + item.receivableTotal = item.receivable + item.addPriceTotal; + } + + item.discountTotal = (item.price - item.discountPrice) * item.count; + + + if (product.isSuit == 3) { + var product_info = product.product_info[0]; + if(product_info){ + var discountPrice = item.price - product_info.discountMoney / product.count; + item.amount = parseFloat((item.price * item.count).toFixed(2)); + item.discountPrice = parseFloat(discountPrice.toFixed(2)); + item.receivable = parseFloat((item.discountPrice * item.count).toFixed(2)); + item.amountTotal = parseFloat((item.price * item.count + item.amountAddTotal).toFixed(2)); + item.receivableTotal = parseFloat((item.receivable + item.addPriceTotal).toFixed(2)); + item.discountTotal = product_info.discountMoney; + } + } + return item; +} + +/** + * 做法明细 + */ +var getOrderItemMake = function (make) { + var order_item_make = {}; + order_item_make.id = getDaySeo(4); // String 前台系统ID + order_item_make.orderItemId = ""; // String 商品明细ID + order_item_make.makeId = make.makeId; // String 做法ID + order_item_make.makeName = make.makeName; // String 做法名称 + order_item_make.addPrice = make.addPrice; // Double 做法加价 + order_item_make.discountPrice = make.addPrice; // Double 折后单价 + order_item_make.count = make.count; // Double 做法数量 + order_item_make.rcount = 0; // Double 做法退数量 + order_item_make.addTotal = make.addPrice * make.count; // Double 加价总额 + order_item_make.discountAddTotal = make.addPrice * make.count; // Double 折后总额 + order_item_make.discount = 0; // Double 折后率 + order_item_make.qtyFlag = make.qtyFlag; // Integer 做法管理数量标识(0否1是) + return order_item_make; +} + +/** + * 优惠明细 + */ +var getOrderItemDiscount = function (product, type) { + var product_info = {}; + product_info.id = getDaySeo(3); // 前台系统ID + product_info.orderItemId = ""; // 产品明细ID + product_info.type = type; // Integer 优惠类型 0赠送 1折扣 2优惠券 3会员卡折扣 4满减 5满送 6议价 7兑换 8会员价 9立减 + product_info.info = ""; // info String 优惠说明 + product_info.discountMoney = product.discountTotal; // 优惠金额 + return product_info; +} + +/** + * 获取当前订单号 + * orderNum:1 主单id + * orderNum:2 商品id + * orderNum:3 优惠明细id + * orderNum:4 做法明细id + */ +var getDaySeo = function (orderNum) { + var store = wx.getStorageSync("store"); + return orderNum + store.storeNo + new Date().getTime() + app.globalData.globalNumber++; +} + +/** + * 订单上传信息 + * + */ +var getMemberOrder = function () { + var memberOrder = {}; + memberOrder.id = getDaySeo(1); // 前台系统ID + memberOrder.payNo = ""; // 付款单号 + memberOrder.payTypeNo = "02"; // 付款方式编号 + memberOrder.payType = "会员卡支付"; //会员卡支付 + memberOrder.paid = ""; // 实收金额 + memberOrder.rchange = 0; // 找零金额 + memberOrder.money = ""; // 已收金额 + memberOrder.overAmount = 0; // 溢出金额 + memberOrder.voucherNo = ""; // 凭证号 + memberOrder.payDate = ""; // + memberOrder.cardno = ""; // 付款卡号 + // memberOrder.cardYe = ""; // 充值卡支付前余额 + // memberOrder.cardJf = ""; // 充值卡支付前积分 + memberOrder.incomeFlag = 1; // 是否实收(0否1是) + // memberOrder.otherRateType = ""; // 第三方扣费类型(0不扣费1按次数2按比例) + // memberOrder.otherRateValue = ""; // 第三方扣费值 + // memberOrder.otherRate = ""; // 第三方扣费 + // memberOrder.payChannel = ""; // 支付渠道(-1无) + memberOrder.memo = "微信小程序点餐会员卡支付上传订单信息"; + return memberOrder; +} + +var giftProductAddOrderProduct = function (gift, list, coupon) { + var reduceCost = 0; + if (gift && gift.length > 0) { + for (var giftItem of gift) { + var productItem = {}; + productItem.productName = giftItem.productName; + productItem.linkUrl = giftItem.linkUrl; + productItem.giftProduct = 1; + productItem.count = giftItem.num; + productItem.amount = giftItem.price; + reduceCost += giftItem.price * giftItem.num; + productItem.receivable = 0.0; + productItem.productId = giftItem.productId; + productItem.productNo = giftItem.productNo; + productItem.productUnitId = giftItem.unitId; + productItem.productUnitName = giftItem.unitName; + productItem.typeName = giftItem.typeName; + productItem.specId = giftItem.specId; + productItem.specName = giftItem.specName; + productItem.price = giftItem.price; + productItem.discountPrice = 0; + productItem.discountTotal = giftItem.price * giftItem.num; + productItem.priceOrg = giftItem.price; + productItem.amountTotal = giftItem.price * giftItem.num; + productItem.discount = 100; + productItem.receivableTotal = 0; + productItem.isSuit = parseFloat(giftItem.suitFlag) + 1; + productItem.boxPrice = giftItem.boxPrice; + var specList = []; + var specListItem = {}; + specListItem.specId = giftItem.specId; + specListItem.price = giftItem.price; + specListItem.specName = giftItem.specName; + specList.push(specListItem); + productItem.specList = specList; + list.push(productItem); + } + app.orderTicket.orderList = list; + coupon.reduceCost = reduceCost * 100; + return list; + } +} + + +var checkPromotionProduct = function () { + //判断删掉商品后时候还有促销商品 没有去掉标识 + var promotionFlag = false; + for (var item of app.orderTicket.orderList) { + if (item.discountRule) { + promotionFlag = true; + } + } + if (promotionFlag) { + app.orderTicket.promotionFlag = 1; + } else app.orderTicket.promotionFlag = 0; +} + +module.exports = { + getDaySeo: getDaySeo, + cy_addItem: cy_addItem, + cy_subItem: cy_subItem, + cy_getIds: cy_getIds, + cy_getIdsName: cy_getIdsName, + cy_getProductCount: cy_getProductCount, + cy_getProductList: cy_getProductList, + getOrder: getOrder, + getMemberOrder: getMemberOrder, + giftProductAddOrderProduct: giftProductAddOrderProduct, + promotionComputePrice: promotionComputePrice, + checkPromotionProduct: checkPromotionProduct, + getMemberCreateOrder: getMemberCreateOrder +}; \ No newline at end of file diff --git a/utils/openApi.js b/utils/openApi.js new file mode 100644 index 0000000..3705cb4 --- /dev/null +++ b/utils/openApi.js @@ -0,0 +1,375 @@ +var $ = { + extend: function(flag, p, c) { + var c = c || {}; + for (var i in p) { + if (!p.hasOwnProperty(i)) { + continue; + } + if (typeof p[i] === 'object') { + c[i] = (p[i].constructor === Array) ? [] : {}; + deepCopy(p[i], c[i]); + } else { + c[i] = p[i]; + } + } + return c; + }, + each: function(object, callback, args) { + var name, i = 0, + length = object.length, + isObj = length === undefined || typeof object == 'function'; + + if (args) { + if (isObj) { + for (name in object) { + if (callback.apply(object[name], args) === false) { + break; + } + } + } else { + for (; i < length;) { + if (callback.apply(object[i++], args) === false) { + break; + } + } + } + + // A special, fast, case for the most common use of each + } else { + if (isObj) { + for (name in object) { + if (callback.call(object[name], name, object[name]) === false) { + break; + } + } + } else { + for (; i < length;) { + if (callback.call(object[i], i, object[i++]) === false) { + break; + } + } + } + } + + return object; + } +} + +function sha1(x, blen) { + function add32(a, b) { + var lsw = (a & 0xFFFF) + (b & 0xFFFF); + var msw = (a >> 16) + (b >> 16) + (lsw >> 16); + return (msw << 16) | (lsw & 0xFFFF); + } + + function AA(a, b, c, d, e) { + b = (b >>> 27) | (b << 5); + var lsw = (a & 0xFFFF) + (b & 0xFFFF) + (c & 0xFFFF) + (d & 0xFFFF) + (e & 0xFFFF); + var msw = (a >> 16) + (b >> 16) + (c >> 16) + (d >> 16) + (e >> 16) + (lsw >> 16); + return (msw << 16) | (lsw & 0xFFFF); + } + + function RR(w, j) { + var n = w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16]; + return (n >>> 31) | (n << 1); + } + + var len = blen * 8; + x[len >> 5] |= 0x80 << (24 - len % 32); + x[((len + 64 >> 9) << 4) + 15] = len; + var w = new Array(80); + + var k1 = 0x5A827999; + var k2 = 0x6ED9EBA1; + var k3 = 0x8F1BBCDC; + var k4 = 0xCA62C1D6; + + var h0 = 0x67452301; + var h1 = 0xEFCDAB89; + var h2 = 0x98BADCFE; + var h3 = 0x10325476; + var h4 = 0xC3D2E1F0; + + for (var i = 0; i < x.length; i += 16) { + var j = 0; + var t; + var a = h0; + var b = h1; + var c = h2; + var d = h3; + var e = h4; + while (j < 16) { + w[j] = x[i + j]; + t = AA(e, a, d ^ (b & (c ^ d)), w[j], k1); + e = d; + d = c; + c = (b >>> 2) | (b << 30); + b = a; + a = t; + j++; + } + while (j < 20) { + w[j] = RR(w, j); + t = AA(e, a, d ^ (b & (c ^ d)), w[j], k1); + e = d; + d = c; + c = (b >>> 2) | (b << 30); + b = a; + a = t; + j++; + } + while (j < 40) { + w[j] = RR(w, j); + t = AA(e, a, b ^ c ^ d, w[j], k2); + e = d; + d = c; + c = (b >>> 2) | (b << 30); + b = a; + a = t; + j++; + } + while (j < 60) { + w[j] = RR(w, j); + t = AA(e, a, (b & c) | (d & (b | c)), w[j], k3); + e = d; + d = c; + c = (b >>> 2) | (b << 30); + b = a; + a = t; + j++; + } + while (j < 80) { + w[j] = RR(w, j); + t = AA(e, a, b ^ c ^ d, w[j], k4); + e = d; + d = c; + c = (b >>> 2) | (b << 30); + b = a; + a = t; + j++; + } + h0 = add32(h0, a); + h1 = add32(h1, b); + h2 = add32(h2, c); + h3 = add32(h3, d); + h4 = add32(h4, e); + } + return [h0, h1, h2, h3, h4]; +} + +var encoding = { + strToBe32s: function(str) { + var be = []; + var len = Math.floor(str.length / 4); + var i, j; + for (i = 0, j = 0; i < len; i++, j += 4) { + be[i] = ((str.charCodeAt(j) & 0xff) << 24) | ((str.charCodeAt(j + 1) & 0xff) << 16) | ((str.charCodeAt(j + 2) & 0xff) << 8) | (str.charCodeAt(j + 3) & 0xff); + } + while (j < str.length) { + be[j >> 2] |= (str.charCodeAt(j) & 0xff) << (24 - (j * 8) % 32); + j++; + } + return be; + }, + + be32sToHex: function(be) { + var hex = '0123456789ABCDEF'; + var str = ''; + for (var i = 0; i < be.length * 4; i++) { + str += hex.charAt((be[i >> 2] >> ((3 - i % 4) * 8 + 4)) & 0xF) + hex.charAt((be[i >> 2] >> ((3 - i % 4) * 8)) & 0xF); + } + return str; + } +}; + +$.encoding = encoding; +var digests = { + hexSha1Str: function(str) { + return $.encoding.be32sToHex($.digests.sha1Str(str)); + }, + sha1Str: function(str) { + return sha1($.encoding.strToBe32s(str), str.length); + } +}; +$.digests = digests; + +var utils = { + inArray: function(needle, array, bool) { + if (typeof needle == "string" || typeof needle == "number") { + var len = array.length; + for (var i = 0; i < len; i++) { + if (needle === array[i]) { + if (bool) { + return i; + } + return true; + } + } + return false; + } + } +}; +$.utils = utils; + +var api = function(openAppKey, openAppSecret, url) { + return { + test1: function() { + console.log("test1"); + }, + sign: function(params, ignores) { + var timestamp = (new Date()).valueOf(); + var appSecret = openAppSecret; + var defaults = { + appKey: openAppKey, + v: '1.0', + format: 'json', + locale: 'zh-CN', + client: 'weixin', + timestamp: timestamp + }; + var options = $.extend(true, defaults, params); + var paramNames = []; + $.each(options, function(key, value) { + if (!$.utils.inArray(key, ignores, false)) { + paramNames.push(key); + } + }); + + paramNames = paramNames.sort(); + + var stringBuilder = []; + stringBuilder.push(appSecret); + + $.each(paramNames, function(inx, value) { + stringBuilder.push(value); + stringBuilder.push(options[value]); + }); + + stringBuilder.push(appSecret); + options.sign = $.digests.hexSha1Str(stringBuilder.join("")); + + return options; + }, + ajax: function(params, ignores, success, error) { + var data = this.sign(params, ignores); + wx.request({ + url: url, + method: 'POST', + data: data, + dataType: 'json', + header: { + 'content-type': 'application/x-www-form-urlencoded' + }, + success: function(json) { + if (json.errorToken) { + if (typeof error == 'function') { + error.call(error, json); + } + } else { + if (typeof success == 'function') { + success.call(success, json); + } + } + }, + fail: function() { + if (typeof error == 'function') { + var json = { + "errorToken": "@@$-ERROR_TOKEN$-@@", + "code": "0", + "message": "网络好像有问题,无法访问云端服务!", + "solution": "请检查本机网络是否能正常访问互联网", + "subErrors": "" + }; + error.call(error, json); + } + } + }); + } + } +} + +/** + * 会员授权 + */ +var memberApi = function (openAppKey, openAppSecret, url) { + return { + test1: function () { + console.log("test1"); + }, + sign: function (params, ignores) { + var timestamp = (new Date()).valueOf(); + var appSecret = openAppSecret; + var defaults = { + appKey: openAppKey, + v: '1.0', + format: 'json', + locale: 'zh-CN', + client: 'web', + timestamp: timestamp + }; + var options = $.extend(true, defaults, params); + var paramNames = []; + $.each(options, function (key, value) { + if (!$.utils.inArray(key, ignores, false)) { + paramNames.push(key); + } + }); + + paramNames = paramNames.sort(); + + var stringBuilder = []; + stringBuilder.push(appSecret); + + $.each(paramNames, function (inx, value) { + stringBuilder.push(value); + stringBuilder.push(options[value]); + }); + + stringBuilder.push(appSecret); + options.sign = $.digests.hexSha1Str(stringBuilder.join("")); + + return options; + }, + ajax: function (params, ignores, success, error) { + var data = this.sign(params, ignores); + wx.request({ + url: url, + method: 'POST', + data: data, + dataType: 'json', + header: { + 'content-type': 'application/x-www-form-urlencoded' + }, + success: function (json) { + if (json.errorToken) { + if (typeof error == 'function') { + error.call(error, json); + } + } else { + if (typeof success == 'function') { + success.call(success, json); + } + } + }, + fail: function () { + if (typeof error == 'function') { + var json = { + "errorToken": "@@$-ERROR_TOKEN$-@@", + "code": "0", + "message": "网络好像有问题,无法访问云端服务!", + "solution": "请检查本机网络是否能正常访问互联网", + "subErrors": "" + }; + error.call(error, json); + } + } + }); + } + } + +} + +module.exports = { + api: api, + memberApi: memberApi +}; \ No newline at end of file diff --git a/utils/paho-mqtt.js b/utils/paho-mqtt.js new file mode 100644 index 0000000..3c9077a --- /dev/null +++ b/utils/paho-mqtt.js @@ -0,0 +1,2403 @@ +/******************************************************************************* + * Copyright (c) 2013 IBM Corp. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Andrew Banks - initial API and implementation and initial documentation + *******************************************************************************/ + + +// Only expose a single object name in the global namespace. +// Everything must go through this module. Global Paho.MQTT module +// only has a single public function, client, which returns +// a Paho.MQTT client object given connection details. + +/** + * Send and receive messages using web browsers. + *

+ * This programming interface lets a JavaScript client application use the MQTT V3.1 or + * V3.1.1 protocol to connect to an MQTT-supporting messaging server. + * + * The function supported includes: + *

    + *
  1. Connecting to and disconnecting from a server. The server is identified by its host name and port number. + *
  2. Specifying options that relate to the communications link with the server, + * for example the frequency of keep-alive heartbeats, and whether SSL/TLS is required. + *
  3. Subscribing to and receiving messages from MQTT Topics. + *
  4. Publishing messages to MQTT Topics. + *
+ *

+ * The API consists of two main objects: + *

+ *
{@link Paho.MQTT.Client}
+ *
This contains methods that provide the functionality of the API, + * including provision of callbacks that notify the application when a message + * arrives from or is delivered to the messaging server, + * or when the status of its connection to the messaging server changes.
+ *
{@link Paho.MQTT.Message}
+ *
This encapsulates the payload of the message along with various attributes + * associated with its delivery, in particular the destination to which it has + * been (or is about to be) sent.
+ *
+ *

+ * The programming interface validates parameters passed to it, and will throw + * an Error containing an error message intended for developer use, if it detects + * an error with any parameter. + *

+ * Example: + * + *

+client = new Paho.MQTT.Client(location.hostname, Number(location.port), "clientId");
+client.onConnectionLost = onConnectionLost;
+client.onMessageArrived = onMessageArrived;
+client.connect({onSuccess:onConnect});
+
+function onConnect() {
+  // Once a connection has been made, make a subscription and send a message.
+  console.log("onConnect");
+  client.subscribe("/World");
+  message = new Paho.MQTT.Message("Hello");
+  message.destinationName = "/World";
+  client.send(message);
+};
+function onConnectionLost(responseObject) {
+  if (responseObject.errorCode !== 0)
+	console.log("onConnectionLost:"+responseObject.errorMessage);
+};
+function onMessageArrived(message) {
+  console.log("onMessageArrived:"+message.payloadString);
+  client.disconnect();
+};
+ * 
+ * @namespace Paho.MQTT + */ + +/* jshint shadow:true */ +(function ExportLibrary(root, factory) { + if (typeof exports === 'object' && typeof module === 'object') { + module.exports = factory(); + } else if (typeof define === 'function' && define.amd) { + define(factory); + } else if (typeof exports === 'object') { + exports = factory(); + } else { + if (typeof root.Paho === 'undefined') { + root.Paho = {}; + } + root.Paho.MQTT = factory(); + } +})(global, function LibraryFactory() { + + + var PahoMQTT = (function(wx) { + + // Private variables below, these are only visible inside the function closure + // which is used to define the module. + + var version = "@VERSION@"; + var buildLevel = "@BUILDLEVEL@"; + + /** + * Unique message type identifiers, with associated + * associated integer values. + * @private + */ + var MESSAGE_TYPE = { + CONNECT: 1, + CONNACK: 2, + PUBLISH: 3, + PUBACK: 4, + PUBREC: 5, + PUBREL: 6, + PUBCOMP: 7, + SUBSCRIBE: 8, + SUBACK: 9, + UNSUBSCRIBE: 10, + UNSUBACK: 11, + PINGREQ: 12, + PINGRESP: 13, + DISCONNECT: 14 + }; + + // Collection of utility methods used to simplify module code + // and promote the DRY pattern. + + /** + * Validate an object's parameter names to ensure they + * match a list of expected variables name for this option + * type. Used to ensure option object passed into the API don't + * contain erroneous parameters. + * @param {Object} obj - User options object + * @param {Object} keys - valid keys and types that may exist in obj. + * @throws {Error} Invalid option parameter found. + * @private + */ + var validate = function(obj, keys) { + for (var key in obj) { + if (obj.hasOwnProperty(key)) { + if (keys.hasOwnProperty(key)) { + if (typeof obj[key] !== keys[key]) + throw new Error(format(ERROR.INVALID_TYPE, [typeof obj[key], key])); + } else { + var errorStr = "Unknown property, " + key + ". Valid properties are:"; + for (var validKey in keys) + if (keys.hasOwnProperty(validKey)) + errorStr = errorStr + " " + validKey; + throw new Error(errorStr); + } + } + } + }; + + /** + * Return a new function which runs the user function bound + * to a fixed scope. + * @param {function} User function + * @param {object} Function scope + * @return {function} User function bound to another scope + * @private + */ + var scope = function(f, scope) { + return function() { + return f.apply(scope, arguments); + }; + }; + + /** + * Unique message type identifiers, with associated + * associated integer values. + * @private + */ + var ERROR = { + OK: { code: 0, text: "AMQJSC0000I OK." }, + CONNECT_TIMEOUT: { code: 1, text: "AMQJSC0001E Connect timed out." }, + SUBSCRIBE_TIMEOUT: { code: 2, text: "AMQJS0002E Subscribe timed out." }, + UNSUBSCRIBE_TIMEOUT: { code: 3, text: "AMQJS0003E Unsubscribe timed out." }, + PING_TIMEOUT: { code: 4, text: "AMQJS0004E Ping timed out." }, + INTERNAL_ERROR: { code: 5, text: "AMQJS0005E Internal error. Error Message: {0}, Stack trace: {1}" }, + CONNACK_RETURNCODE: { code: 6, text: "AMQJS0006E Bad Connack return code:{0} {1}." }, + SOCKET_ERROR: { code: 7, text: "AMQJS0007E Socket error:{0}." }, + SOCKET_CLOSE: { code: 8, text: "AMQJS0008I Socket closed." }, + MALFORMED_UTF: { code: 9, text: "AMQJS0009E Malformed UTF data:{0} {1} {2}." }, + UNSUPPORTED: { code: 10, text: "AMQJS0010E {0} is not supported by this browser." }, + INVALID_STATE: { code: 11, text: "AMQJS0011E Invalid state {0}." }, + INVALID_TYPE: { code: 12, text: "AMQJS0012E Invalid type {0} for {1}." }, + INVALID_ARGUMENT: { code: 13, text: "AMQJS0013E Invalid argument {0} for {1}." }, + UNSUPPORTED_OPERATION: { code: 14, text: "AMQJS0014E Unsupported operation." }, + INVALID_STORED_DATA: { code: 15, text: "AMQJS0015E Invalid data in local storage key={0} value={1}." }, + INVALID_MQTT_MESSAGE_TYPE: { code: 16, text: "AMQJS0016E Invalid MQTT message type {0}." }, + MALFORMED_UNICODE: { code: 17, text: "AMQJS0017E Malformed Unicode string:{0} {1}." }, + BUFFER_FULL: { code: 18, text: "AMQJS0018E Message buffer is full, maximum buffer size: {0}." }, + }; + + /** CONNACK RC Meaning. */ + var CONNACK_RC = { + 0: "Connection Accepted", + 1: "Connection Refused: unacceptable protocol version", + 2: "Connection Refused: identifier rejected", + 3: "Connection Refused: server unavailable", + 4: "Connection Refused: bad user name or password", + 5: "Connection Refused: not authorized" + }; + + /** + * Format an error message text. + * @private + * @param {error} ERROR.KEY value above. + * @param {substitutions} [array] substituted into the text. + * @return the text with the substitutions made. + */ + var format = function(error, substitutions) { + var text = error.text; + if (substitutions) { + var field, start; + for (var i = 0; i < substitutions.length; i++) { + field = "{" + i + "}"; + start = text.indexOf(field); + if (start > 0) { + var part1 = text.substring(0, start); + var part2 = text.substring(start + field.length); + text = part1 + substitutions[i] + part2; + } + } + } + return text; + }; + + //MQTT protocol and version 6 M Q I s d p 3 + var MqttProtoIdentifierv3 = [0x00, 0x06, 0x4d, 0x51, 0x49, 0x73, 0x64, 0x70, 0x03]; + //MQTT proto/version for 311 4 M Q T T 4 + var MqttProtoIdentifierv4 = [0x00, 0x04, 0x4d, 0x51, 0x54, 0x54, 0x04]; + + /** + * Construct an MQTT wire protocol message. + * @param type MQTT packet type. + * @param options optional wire message attributes. + * + * Optional properties + * + * messageIdentifier: message ID in the range [0..65535] + * payloadMessage: Application Message - PUBLISH only + * connectStrings: array of 0 or more Strings to be put into the CONNECT payload + * topics: array of strings (SUBSCRIBE, UNSUBSCRIBE) + * requestQoS: array of QoS values [0..2] + * + * "Flag" properties + * cleanSession: true if present / false if absent (CONNECT) + * willMessage: true if present / false if absent (CONNECT) + * isRetained: true if present / false if absent (CONNECT) + * userName: true if present / false if absent (CONNECT) + * password: true if present / false if absent (CONNECT) + * keepAliveInterval: integer [0..65535] (CONNECT) + * + * @private + * @ignore + */ + var WireMessage = function(type, options) { + this.type = type; + for (var name in options) { + if (options.hasOwnProperty(name)) { + this[name] = options[name]; + } + } + }; + + WireMessage.prototype.encode = function() { + // Compute the first byte of the fixed header + var first = ((this.type & 0x0f) << 4); + + /* + * Now calculate the length of the variable header + payload by adding up the lengths + * of all the component parts + */ + + var remLength = 0; + var topicStrLength = []; + var destinationNameLength = 0; + var willMessagePayloadBytes; + + // if the message contains a messageIdentifier then we need two bytes for that + if (this.messageIdentifier !== undefined) + remLength += 2; + + switch (this.type) { + // If this a Connect then we need to include 12 bytes for its header + case MESSAGE_TYPE.CONNECT: + switch (this.mqttVersion) { + case 3: + remLength += MqttProtoIdentifierv3.length + 3; + break; + case 4: + remLength += MqttProtoIdentifierv4.length + 3; + break; + } + + remLength += UTF8Length(this.clientId) + 2; + if (this.willMessage !== undefined) { + remLength += UTF8Length(this.willMessage.destinationName) + 2; + // Will message is always a string, sent as UTF-8 characters with a preceding length. + willMessagePayloadBytes = this.willMessage.payloadBytes; + if (!(willMessagePayloadBytes instanceof Uint8Array)) + willMessagePayloadBytes = new Uint8Array(payloadBytes); + remLength += willMessagePayloadBytes.byteLength + 2; + } + if (this.userName !== undefined) + remLength += UTF8Length(this.userName) + 2; + if (this.password !== undefined) + remLength += UTF8Length(this.password) + 2; + break; + + // Subscribe, Unsubscribe can both contain topic strings + case MESSAGE_TYPE.SUBSCRIBE: + first |= 0x02; // Qos = 1; + for (var i = 0; i < this.topics.length; i++) { + topicStrLength[i] = UTF8Length(this.topics[i]); + remLength += topicStrLength[i] + 2; + } + remLength += this.requestedQos.length; // 1 byte for each topic's Qos + // QoS on Subscribe only + break; + + case MESSAGE_TYPE.UNSUBSCRIBE: + first |= 0x02; // Qos = 1; + for (var i = 0; i < this.topics.length; i++) { + topicStrLength[i] = UTF8Length(this.topics[i]); + remLength += topicStrLength[i] + 2; + } + break; + + case MESSAGE_TYPE.PUBREL: + first |= 0x02; // Qos = 1; + break; + + case MESSAGE_TYPE.PUBLISH: + if (this.payloadMessage.duplicate) first |= 0x08; + first = first |= (this.payloadMessage.qos << 1); + if (this.payloadMessage.retained) first |= 0x01; + destinationNameLength = UTF8Length(this.payloadMessage.destinationName); + remLength += destinationNameLength + 2; + var payloadBytes = this.payloadMessage.payloadBytes; + remLength += payloadBytes.byteLength; + if (payloadBytes instanceof ArrayBuffer) + payloadBytes = new Uint8Array(payloadBytes); + else if (!(payloadBytes instanceof Uint8Array)) + payloadBytes = new Uint8Array(payloadBytes.buffer); + break; + + case MESSAGE_TYPE.DISCONNECT: + break; + + default: + break; + } + + // Now we can allocate a buffer for the message + + var mbi = encodeMBI(remLength); // Convert the length to MQTT MBI format + var pos = mbi.length + 1; // Offset of start of variable header + var buffer = new ArrayBuffer(remLength + pos); + var byteStream = new Uint8Array(buffer); // view it as a sequence of bytes + + //Write the fixed header into the buffer + byteStream[0] = first; + byteStream.set(mbi, 1); + + // If this is a PUBLISH then the variable header starts with a topic + if (this.type == MESSAGE_TYPE.PUBLISH) + pos = writeString(this.payloadMessage.destinationName, destinationNameLength, byteStream, pos); + // If this is a CONNECT then the variable header contains the protocol name/version, flags and keepalive time + + else if (this.type == MESSAGE_TYPE.CONNECT) { + switch (this.mqttVersion) { + case 3: + byteStream.set(MqttProtoIdentifierv3, pos); + pos += MqttProtoIdentifierv3.length; + break; + case 4: + byteStream.set(MqttProtoIdentifierv4, pos); + pos += MqttProtoIdentifierv4.length; + break; + } + var connectFlags = 0; + if (this.cleanSession) + connectFlags = 0x02; + if (this.willMessage !== undefined) { + connectFlags |= 0x04; + connectFlags |= (this.willMessage.qos << 3); + if (this.willMessage.retained) { + connectFlags |= 0x20; + } + } + if (this.userName !== undefined) + connectFlags |= 0x80; + if (this.password !== undefined) + connectFlags |= 0x40; + byteStream[pos++] = connectFlags; + pos = writeUint16(this.keepAliveInterval, byteStream, pos); + } + + // Output the messageIdentifier - if there is one + if (this.messageIdentifier !== undefined) + pos = writeUint16(this.messageIdentifier, byteStream, pos); + + switch (this.type) { + case MESSAGE_TYPE.CONNECT: + pos = writeString(this.clientId, UTF8Length(this.clientId), byteStream, pos); + if (this.willMessage !== undefined) { + pos = writeString(this.willMessage.destinationName, UTF8Length(this.willMessage.destinationName), byteStream, pos); + pos = writeUint16(willMessagePayloadBytes.byteLength, byteStream, pos); + byteStream.set(willMessagePayloadBytes, pos); + pos += willMessagePayloadBytes.byteLength; + + } + if (this.userName !== undefined) + pos = writeString(this.userName, UTF8Length(this.userName), byteStream, pos); + if (this.password !== undefined) + pos = writeString(this.password, UTF8Length(this.password), byteStream, pos); + break; + + case MESSAGE_TYPE.PUBLISH: + // PUBLISH has a text or binary payload, if text do not add a 2 byte length field, just the UTF characters. + byteStream.set(payloadBytes, pos); + + break; + + // case MESSAGE_TYPE.PUBREC: + // case MESSAGE_TYPE.PUBREL: + // case MESSAGE_TYPE.PUBCOMP: + // break; + + case MESSAGE_TYPE.SUBSCRIBE: + // SUBSCRIBE has a list of topic strings and request QoS + for (var i = 0; i < this.topics.length; i++) { + pos = writeString(this.topics[i], topicStrLength[i], byteStream, pos); + byteStream[pos++] = this.requestedQos[i]; + } + break; + + case MESSAGE_TYPE.UNSUBSCRIBE: + // UNSUBSCRIBE has a list of topic strings + for (var i = 0; i < this.topics.length; i++) + pos = writeString(this.topics[i], topicStrLength[i], byteStream, pos); + break; + + default: + // Do nothing. + } + + return buffer; + }; + + function decodeMessage(input, pos) { + var startingPos = pos; + var first = input[pos]; + var type = first >> 4; + var messageInfo = first &= 0x0f; + pos += 1; + + + // Decode the remaining length (MBI format) + + var digit; + var remLength = 0; + var multiplier = 1; + do { + if (pos == input.length) { + return [null, startingPos]; + } + digit = input[pos++]; + remLength += ((digit & 0x7F) * multiplier); + multiplier *= 128; + } while ((digit & 0x80) !== 0); + + var endPos = pos + remLength; + if (endPos > input.length) { + return [null, startingPos]; + } + + var wireMessage = new WireMessage(type); + switch (type) { + case MESSAGE_TYPE.CONNACK: + var connectAcknowledgeFlags = input[pos++]; + if (connectAcknowledgeFlags & 0x01) + wireMessage.sessionPresent = true; + wireMessage.returnCode = input[pos++]; + break; + + case MESSAGE_TYPE.PUBLISH: + var qos = (messageInfo >> 1) & 0x03; + + var len = readUint16(input, pos); + pos += 2; + var topicName = parseUTF8(input, pos, len); + pos += len; + // If QoS 1 or 2 there will be a messageIdentifier + if (qos > 0) { + wireMessage.messageIdentifier = readUint16(input, pos); + pos += 2; + } + + var message = new Message(input.subarray(pos, endPos)); + if ((messageInfo & 0x01) == 0x01) + message.retained = true; + if ((messageInfo & 0x08) == 0x08) + message.duplicate = true; + message.qos = qos; + message.destinationName = topicName; + wireMessage.payloadMessage = message; + break; + + case MESSAGE_TYPE.PUBACK: + case MESSAGE_TYPE.PUBREC: + case MESSAGE_TYPE.PUBREL: + case MESSAGE_TYPE.PUBCOMP: + case MESSAGE_TYPE.UNSUBACK: + wireMessage.messageIdentifier = readUint16(input, pos); + break; + + case MESSAGE_TYPE.SUBACK: + wireMessage.messageIdentifier = readUint16(input, pos); + pos += 2; + wireMessage.returnCode = input.subarray(pos, endPos); + break; + + default: + break; + } + + return [wireMessage, endPos]; + } + + function writeUint16(input, buffer, offset) { + buffer[offset++] = input >> 8; //MSB + buffer[offset++] = input % 256; //LSB + return offset; + } + + function writeString(input, utf8Length, buffer, offset) { + offset = writeUint16(utf8Length, buffer, offset); + stringToUTF8(input, buffer, offset); + return offset + utf8Length; + } + + function readUint16(buffer, offset) { + return 256 * buffer[offset] + buffer[offset + 1]; + } + + /** + * Encodes an MQTT Multi-Byte Integer + * @private + */ + function encodeMBI(number) { + var output = new Array(1); + var numBytes = 0; + + do { + var digit = number % 128; + number = number >> 7; + if (number > 0) { + digit |= 0x80; + } + output[numBytes++] = digit; + } while ((number > 0) && (numBytes < 4)); + + return output; + } + + /** + * Takes a String and calculates its length in bytes when encoded in UTF8. + * @private + */ + function UTF8Length(input) { + var output = 0; + for (var i = 0; i < input.length; i++) { + var charCode = input.charCodeAt(i); + if (charCode > 0x7FF) { + // Surrogate pair means its a 4 byte character + if (0xD800 <= charCode && charCode <= 0xDBFF) { + i++; + output++; + } + output += 3; + } else if (charCode > 0x7F) + output += 2; + else + output++; + } + return output; + } + + /** + * Takes a String and writes it into an array as UTF8 encoded bytes. + * @private + */ + function stringToUTF8(input, output, start) { + var pos = start; + for (var i = 0; i < input.length; i++) { + var charCode = input.charCodeAt(i); + + // Check for a surrogate pair. + if (0xD800 <= charCode && charCode <= 0xDBFF) { + var lowCharCode = input.charCodeAt(++i); + if (isNaN(lowCharCode)) { + throw new Error(format(ERROR.MALFORMED_UNICODE, [charCode, lowCharCode])); + } + charCode = ((charCode - 0xD800) << 10) + (lowCharCode - 0xDC00) + 0x10000; + + } + + if (charCode <= 0x7F) { + output[pos++] = charCode; + } else if (charCode <= 0x7FF) { + output[pos++] = charCode >> 6 & 0x1F | 0xC0; + output[pos++] = charCode & 0x3F | 0x80; + } else if (charCode <= 0xFFFF) { + output[pos++] = charCode >> 12 & 0x0F | 0xE0; + output[pos++] = charCode >> 6 & 0x3F | 0x80; + output[pos++] = charCode & 0x3F | 0x80; + } else { + output[pos++] = charCode >> 18 & 0x07 | 0xF0; + output[pos++] = charCode >> 12 & 0x3F | 0x80; + output[pos++] = charCode >> 6 & 0x3F | 0x80; + output[pos++] = charCode & 0x3F | 0x80; + } + } + return output; + } + + function parseUTF8(input, offset, length) { + var output = ""; + var utf16; + var pos = offset; + + while (pos < offset + length) { + var byte1 = input[pos++]; + if (byte1 < 128) + utf16 = byte1; + else { + var byte2 = input[pos++] - 128; + if (byte2 < 0) + throw new Error(format(ERROR.MALFORMED_UTF, [byte1.toString(16), byte2.toString(16), ""])); + if (byte1 < 0xE0) // 2 byte character + utf16 = 64 * (byte1 - 0xC0) + byte2; + else { + var byte3 = input[pos++] - 128; + if (byte3 < 0) + throw new Error(format(ERROR.MALFORMED_UTF, [byte1.toString(16), byte2.toString(16), byte3.toString(16)])); + if (byte1 < 0xF0) // 3 byte character + utf16 = 4096 * (byte1 - 0xE0) + 64 * byte2 + byte3; + else { + var byte4 = input[pos++] - 128; + if (byte4 < 0) + throw new Error(format(ERROR.MALFORMED_UTF, [byte1.toString(16), byte2.toString(16), byte3.toString(16), byte4.toString(16)])); + if (byte1 < 0xF8) // 4 byte character + utf16 = 262144 * (byte1 - 0xF0) + 4096 * byte2 + 64 * byte3 + byte4; + else // longer encodings are not supported + throw new Error(format(ERROR.MALFORMED_UTF, [byte1.toString(16), byte2.toString(16), byte3.toString(16), byte4.toString(16)])); + } + } + } + + if (utf16 > 0xFFFF) // 4 byte character - express as a surrogate pair + { + utf16 -= 0x10000; + output += String.fromCharCode(0xD800 + (utf16 >> 10)); // lead character + utf16 = 0xDC00 + (utf16 & 0x3FF); // trail character + } + output += String.fromCharCode(utf16); + } + return output; + } + + /** + * Repeat keepalive requests, monitor responses. + * @ignore + */ + var Pinger = function(client, keepAliveInterval) { + this._client = client; + this._keepAliveInterval = keepAliveInterval * 1000; + this.isReset = false; + + var pingReq = new WireMessage(MESSAGE_TYPE.PINGREQ).encode(); + + var doTimeout = function(pinger) { + return function() { + return doPing.apply(pinger); + }; + }; + + /** @ignore */ + var doPing = function() { + if (!this.isReset) { + this._client._trace("Pinger.doPing", "Timed out"); + this._client._disconnected(ERROR.PING_TIMEOUT.code, format(ERROR.PING_TIMEOUT)); + } else { + this.isReset = false; + this._client._trace("Pinger.doPing", "send PINGREQ"); + wx.sendSocketMessage({ + data: pingReq, + success: function() { + // + }, + fail: function() { + // + }, + complete: function() { + // + } + }) + this.timeout = setTimeout(doTimeout(this), this._keepAliveInterval); + } + }; + + this.reset = function() { + this.isReset = true; + clearTimeout(this.timeout); + if (this._keepAliveInterval > 0) + this.timeout = setTimeout(doTimeout(this), this._keepAliveInterval); + }; + + this.cancel = function() { + clearTimeout(this.timeout); + }; + }; + + /** + * Monitor request completion. + * @ignore + */ + var Timeout = function(client, timeoutSeconds, action, args) { + if (!timeoutSeconds) + timeoutSeconds = 30; + + var doTimeout = function(action, client, args) { + return function() { + return action.apply(client, args); + }; + }; + this.timeout = setTimeout(doTimeout(action, client, args), timeoutSeconds * 1000); + + this.cancel = function() { + clearTimeout(this.timeout); + }; + }; + + /* + * Internal implementation of the Websockets MQTT V3.1 client. + * + * @name Paho.MQTT.ClientImpl @constructor + * @param {String} host the DNS nameof the webSocket host. + * @param {Number} port the port number for that host. + * @param {String} clientId the MQ client identifier. + */ + var ClientImpl = function(uri, host, port, path, clientId) { + this._trace("Paho.MQTT.Client", uri, host, port, path, clientId); + + this.host = host; + this.port = port; + this.path = path; + this.uri = uri; + this.clientId = clientId; + this._wsuri = null; + + // Local storagekeys are qualified with the following string. + // The conditional inclusion of path in the key is for backward + // compatibility to when the path was not configurable and assumed to + // be /mqtt + // this._localKey = host + ":" + port + (path != "/mqtt" ? ":" + path : "") + ":" + clientId + ":"; + this._localKey = host + (path != "/mqtt" ? ":" + path : "") + ":" + clientId + ":"; + + // Create private instance-only message queue + // Internal queue of messages to be sent, in sending order. + this._msg_queue = []; + this._buffered_msg_queue = []; + + // Messages we have sent and are expecting a response for, indexed by their respective message ids. + this._sentMessages = {}; + + // Messages we have received and acknowleged and are expecting a confirm message for + // indexed by their respective message ids. + this._receivedMessages = {}; + + // Internal list of callbacks to be executed when messages + // have been successfully sent over web socket, e.g. disconnect + // when it doesn't have to wait for ACK, just message is dispatched. + this._notify_msg_sent = {}; + + // Unique identifier for SEND messages, incrementing + // counter as messages are sent. + this._message_identifier = 1; + + // Used to determine the transmission sequence of stored sent messages. + this._sequence = 0; + + + // Load the local state, if any, from the saved version, only restore state relevant to this client. + for (var key in wx.getStorageInfoSync().keys) + if (key.indexOf("Sent:" + this._localKey) === 0 || key.indexOf("Received:" + this._localKey) === 0) + this.restore(key); + }; + + // Messaging Client public instance members. + ClientImpl.prototype.host = null; + ClientImpl.prototype.port = null; + ClientImpl.prototype.path = null; + ClientImpl.prototype.uri = null; + ClientImpl.prototype.clientId = null; + + // Messaging Client private instance members. + ClientImpl.prototype.socket = null; + /* true once we have received an acknowledgement to a CONNECT packet. */ + ClientImpl.prototype.connected = false; + /* The largest message identifier allowed, may not be larger than 2**16 but + * if set smaller reduces the maximum number of outbound messages allowed. + */ + ClientImpl.prototype.maxMessageIdentifier = 65536; + ClientImpl.prototype.connectOptions = null; + ClientImpl.prototype.hostIndex = null; + ClientImpl.prototype.onConnected = null; + ClientImpl.prototype.onConnectionLost = null; + ClientImpl.prototype.onMessageDelivered = null; + ClientImpl.prototype.onMessageArrived = null; + ClientImpl.prototype.traceFunction = null; + ClientImpl.prototype._msg_queue = null; + ClientImpl.prototype._buffered_msg_queue = null; + ClientImpl.prototype._connectTimeout = null; + /* The sendPinger monitors how long we allow before we send data to prove to the server that we are alive. */ + ClientImpl.prototype.sendPinger = null; + /* The receivePinger monitors how long we allow before we require evidence that the server is alive. */ + ClientImpl.prototype.receivePinger = null; + ClientImpl.prototype._reconnectInterval = 1; // Reconnect Delay, starts at 1 second + ClientImpl.prototype._reconnecting = false; + ClientImpl.prototype._reconnectTimeout = null; + ClientImpl.prototype.disconnectedPublishing = false; + ClientImpl.prototype.disconnectedBufferSize = 5000; + + ClientImpl.prototype.receiveBuffer = null; + + ClientImpl.prototype._traceBuffer = null; + ClientImpl.prototype._MAX_TRACE_ENTRIES = 100; + + ClientImpl.prototype.connect = function(connectOptions) { + var connectOptionsMasked = this._traceMask(connectOptions, "password"); + this._trace("Client.connect", connectOptionsMasked, null, this.connected); + + if (this.connected) + throw new Error(format(ERROR.INVALID_STATE, ["already connected"])); + + if (this._reconnecting) { + // connect() function is called while reconnect is in progress. + // Terminate the auto reconnect process to use new connect options. + this._reconnectTimeout.cancel(); + this._reconnectTimeout = null; + this._reconnecting = false; + } + + this.connectOptions = connectOptions; + this._reconnectInterval = 1; + this._reconnecting = false; + if (connectOptions.uris) { + this.hostIndex = 0; + this._doConnect(connectOptions.uris[0]); + } else { + this._doConnect(this.uri); + } + + }; + + ClientImpl.prototype.subscribe = function(filter, subscribeOptions) { + this._trace("Client.subscribe", filter, subscribeOptions); + + if (!this.connected) + throw new Error(format(ERROR.INVALID_STATE, ["not connected"])); + + var wireMessage = new WireMessage(MESSAGE_TYPE.SUBSCRIBE); + wireMessage.topics = [filter]; + if (subscribeOptions.qos !== undefined) + wireMessage.requestedQos = [subscribeOptions.qos]; + else + wireMessage.requestedQos = [0]; + + if (subscribeOptions.onSuccess) { + wireMessage.onSuccess = function(grantedQos) { subscribeOptions.onSuccess({ invocationContext: subscribeOptions.invocationContext, grantedQos: grantedQos }); }; + } + + if (subscribeOptions.onFailure) { + wireMessage.onFailure = function(errorCode) { subscribeOptions.onFailure({ invocationContext: subscribeOptions.invocationContext, errorCode: errorCode, errorMessage: format(errorCode) }); }; + } + + if (subscribeOptions.timeout) { + wireMessage.timeOut = new Timeout(this, subscribeOptions.timeout, subscribeOptions.onFailure, [{ + invocationContext: subscribeOptions.invocationContext, + errorCode: ERROR.SUBSCRIBE_TIMEOUT.code, + errorMessage: format(ERROR.SUBSCRIBE_TIMEOUT) + }]); + } + + // All subscriptions return a SUBACK. + this._requires_ack(wireMessage); + this._schedule_message(wireMessage); + }; + + /** @ignore */ + ClientImpl.prototype.unsubscribe = function(filter, unsubscribeOptions) { + this._trace("Client.unsubscribe", filter, unsubscribeOptions); + + if (!this.connected) + throw new Error(format(ERROR.INVALID_STATE, ["not connected"])); + + var wireMessage = new WireMessage(MESSAGE_TYPE.UNSUBSCRIBE); + wireMessage.topics = [filter]; + + if (unsubscribeOptions.onSuccess) { + wireMessage.callback = function() { unsubscribeOptions.onSuccess({ invocationContext: unsubscribeOptions.invocationContext }); }; + } + if (unsubscribeOptions.timeout) { + wireMessage.timeOut = new Timeout(this, unsubscribeOptions.timeout, unsubscribeOptions.onFailure, [{ + invocationContext: unsubscribeOptions.invocationContext, + errorCode: ERROR.UNSUBSCRIBE_TIMEOUT.code, + errorMessage: format(ERROR.UNSUBSCRIBE_TIMEOUT) + }]); + } + + // All unsubscribes return a SUBACK. + this._requires_ack(wireMessage); + this._schedule_message(wireMessage); + }; + + ClientImpl.prototype.send = function(message) { + this._trace("Client.send", message); + + var wireMessage = new WireMessage(MESSAGE_TYPE.PUBLISH); + wireMessage.payloadMessage = message; + + if (this.connected) { + // Mark qos 1 & 2 message as "ACK required" + // For qos 0 message, invoke onMessageDelivered callback if there is one. + // Then schedule the message. + if (message.qos > 0) { + this._requires_ack(wireMessage); + } else if (this.onMessageDelivered) { + this._notify_msg_sent[wireMessage] = this.onMessageDelivered(wireMessage.payloadMessage); + } + this._schedule_message(wireMessage); + } else { + // Currently disconnected, will not schedule this message + // Check if reconnecting is in progress and disconnected publish is enabled. + if (this._reconnecting && this.disconnectedPublishing) { + // Check the limit which include the "required ACK" messages + var messageCount = Object.keys(this._sentMessages).length + this._buffered_msg_queue.length; + if (messageCount > this.disconnectedBufferSize) { + throw new Error(format(ERROR.BUFFER_FULL, [this.disconnectedBufferSize])); + } else { + if (message.qos > 0) { + // Mark this message as "ACK required" + this._requires_ack(wireMessage); + } else { + wireMessage.sequence = ++this._sequence; + this._buffered_msg_queue.push(wireMessage); + } + } + } else { + throw new Error(format(ERROR.INVALID_STATE, ["not connected"])); + } + } + }; + + ClientImpl.prototype.disconnect = function() { + this._trace("Client.disconnect"); + + if (this._reconnecting) { + // disconnect() function is called while reconnect is in progress. + // Terminate the auto reconnect process. + this._reconnectTimeout.cancel(); + this._reconnectTimeout = null; + this._reconnecting = false; + } + + if (!this.connected) + throw new Error(format(ERROR.INVALID_STATE, ["not connecting or connected"])); + + var wireMessage = new WireMessage(MESSAGE_TYPE.DISCONNECT); + + // Run the disconnected call back as soon as the message has been sent, + // in case of a failure later on in the disconnect processing. + // as a consequence, the _disconected call back may be run several times. + this._notify_msg_sent[wireMessage] = scope(this._disconnected, this); + + this._schedule_message(wireMessage); + }; + + ClientImpl.prototype.getTraceLog = function() { + if (this._traceBuffer !== null) { + this._trace("Client.getTraceLog", new Date()); + this._trace("Client.getTraceLog in flight messages", this._sentMessages.length); + for (var key in this._sentMessages) + this._trace("_sentMessages ", key, this._sentMessages[key]); + for (var key in this._receivedMessages) + this._trace("_receivedMessages ", key, this._receivedMessages[key]); + + return this._traceBuffer; + } + }; + + ClientImpl.prototype.startTrace = function() { + if (this._traceBuffer === null) { + this._traceBuffer = []; + } + this._trace("Client.startTrace", new Date(), version); + }; + + ClientImpl.prototype.stopTrace = function() { + delete this._traceBuffer; + }; + + ClientImpl.prototype._doConnect = function(wsurl) { + // When the socket is open, this client will send the CONNECT WireMessage using the saved parameters. + if (this.connectOptions.useSSL) { + var uriParts = wsurl.split(":"); + uriParts[0] = "wss"; + wsurl = uriParts.join(":"); + } + this._wsuri = wsurl; + this.connected = false; + + wx.connectSocket({ + url: wsurl + }); + + wx.onSocketOpen(scope(this._on_socket_open, this)) + wx.onSocketMessage(scope(this._on_socket_message, this)) + wx.onSocketError(scope(this._on_socket_error, this)) + wx.onSocketClose(scope(this._on_socket_close, this)) + + this.sendPinger = new Pinger(this, this.connectOptions.keepAliveInterval); + this.receivePinger = new Pinger(this, this.connectOptions.keepAliveInterval); + if (this._connectTimeout) { + this._connectTimeout.cancel(); + this._connectTimeout = null; + } + this._connectTimeout = new Timeout(this, this.connectOptions.timeout, this._disconnected, [ERROR.CONNECT_TIMEOUT.code, format(ERROR.CONNECT_TIMEOUT)]); + }; + + + // Schedule a new message to be sent over the WebSockets + // connection. CONNECT messages cause WebSocket connection + // to be started. All other messages are queued internally + // until this has happened. When WS connection starts, process + // all outstanding messages. + ClientImpl.prototype._schedule_message = function(message) { + this._msg_queue.push(message); + // Process outstanding messages in the queue if we have an open socket, and have received CONNACK. + if (this.connected) { + this._process_queue(); + } + }; + + ClientImpl.prototype.store = function(prefix, wireMessage) { + var storedMessage = { type: wireMessage.type, messageIdentifier: wireMessage.messageIdentifier, version: 1 }; + + switch (wireMessage.type) { + case MESSAGE_TYPE.PUBLISH: + if (wireMessage.pubRecReceived) + storedMessage.pubRecReceived = true; + + // Convert the payload to a hex string. + storedMessage.payloadMessage = {}; + var hex = ""; + var messageBytes = wireMessage.payloadMessage.payloadBytes; + for (var i = 0; i < messageBytes.length; i++) { + if (messageBytes[i] <= 0xF) + hex = hex + "0" + messageBytes[i].toString(16); + else + hex = hex + messageBytes[i].toString(16); + } + storedMessage.payloadMessage.payloadHex = hex; + + storedMessage.payloadMessage.qos = wireMessage.payloadMessage.qos; + storedMessage.payloadMessage.destinationName = wireMessage.payloadMessage.destinationName; + if (wireMessage.payloadMessage.duplicate) + storedMessage.payloadMessage.duplicate = true; + if (wireMessage.payloadMessage.retained) + storedMessage.payloadMessage.retained = true; + + // Add a sequence number to sent messages. + if (prefix.indexOf("Sent:") === 0) { + if (wireMessage.sequence === undefined) + wireMessage.sequence = ++this._sequence; + storedMessage.sequence = wireMessage.sequence; + } + break; + + default: + throw Error(format(ERROR.INVALID_STORED_DATA, [key, storedMessage])); + } + wx.setStorageSync(prefix + this._localKey + wireMessage.messageIdentifier, JSON.stringify(storedMessage)); + }; + + ClientImpl.prototype.restore = function(key) { + var value = wx.getStorageSync(key); + var storedMessage = JSON.parse(value); + + var wireMessage = new WireMessage(storedMessage.type, storedMessage); + + switch (storedMessage.type) { + case MESSAGE_TYPE.PUBLISH: + // Replace the payload message with a Message object. + var hex = storedMessage.payloadMessage.payloadHex; + var buffer = new ArrayBuffer((hex.length) / 2); + var byteStream = new Uint8Array(buffer); + var i = 0; + while (hex.length >= 2) { + var x = parseInt(hex.substring(0, 2), 16); + hex = hex.substring(2, hex.length); + byteStream[i++] = x; + } + var payloadMessage = new Message(byteStream); + + payloadMessage.qos = storedMessage.payloadMessage.qos; + payloadMessage.destinationName = storedMessage.payloadMessage.destinationName; + if (storedMessage.payloadMessage.duplicate) + payloadMessage.duplicate = true; + if (storedMessage.payloadMessage.retained) + payloadMessage.retained = true; + wireMessage.payloadMessage = payloadMessage; + + break; + + default: + throw Error(format(ERROR.INVALID_STORED_DATA, [key, value])); + } + + if (key.indexOf("Sent:" + this._localKey) === 0) { + wireMessage.payloadMessage.duplicate = true; + this._sentMessages[wireMessage.messageIdentifier] = wireMessage; + } else if (key.indexOf("Received:" + this._localKey) === 0) { + this._receivedMessages[wireMessage.messageIdentifier] = wireMessage; + } + }; + + ClientImpl.prototype._process_queue = function() { + var message = null; + // Process messages in order they were added + var fifo = this._msg_queue.reverse(); + + // Send all queued messages down socket connection + while ((message = fifo.pop())) { + this._socket_send(message); + // Notify listeners that message was successfully sent + if (this._notify_msg_sent[message]) { + this._notify_msg_sent[message](); + delete this._notify_msg_sent[message]; + } + } + }; + + /** + * Expect an ACK response for this message. Add message to the set of in progress + * messages and set an unused identifier in this message. + * @ignore + */ + ClientImpl.prototype._requires_ack = function(wireMessage) { + var messageCount = Object.keys(this._sentMessages).length; + if (messageCount > this.maxMessageIdentifier) + throw Error("Too many messages:" + messageCount); + + while (this._sentMessages[this._message_identifier] !== undefined) { + this._message_identifier++; + } + wireMessage.messageIdentifier = this._message_identifier; + this._sentMessages[wireMessage.messageIdentifier] = wireMessage; + if (wireMessage.type === MESSAGE_TYPE.PUBLISH) { + this.store("Sent:", wireMessage); + } + if (this._message_identifier === this.maxMessageIdentifier) { + this._message_identifier = 1; + } + }; + + /** + * Called when the underlying websocket has been opened. + * @ignore + */ + ClientImpl.prototype._on_socket_open = function(res) { + // Create the CONNECT message object. + var wireMessage = new WireMessage(MESSAGE_TYPE.CONNECT, this.connectOptions); + wireMessage.clientId = this.clientId; + this._socket_send(wireMessage); + }; + + /** + * Called when the underlying websocket has received a complete packet. + * @ignore + */ + ClientImpl.prototype._on_socket_message = function(event) { + this._trace("Client._on_socket_message", event.data); + var messages = this._deframeMessages(event.data); + for (var i = 0; i < messages.length; i += 1) { + this._handleMessage(messages[i]); + } + }; + + ClientImpl.prototype._deframeMessages = function(data) { + var byteArray = new Uint8Array(data); + var messages = []; + if (this.receiveBuffer) { + var newData = new Uint8Array(this.receiveBuffer.length + byteArray.length); + newData.set(this.receiveBuffer); + newData.set(byteArray, this.receiveBuffer.length); + byteArray = newData; + delete this.receiveBuffer; + } + try { + var offset = 0; + while (offset < byteArray.length) { + var result = decodeMessage(byteArray, offset); + var wireMessage = result[0]; + offset = result[1]; + if (wireMessage !== null) { + messages.push(wireMessage); + } else { + break; + } + } + if (offset < byteArray.length) { + this.receiveBuffer = byteArray.subarray(offset); + } + } catch (error) { + var errorStack = ((error.hasOwnProperty('stack') == 'undefined') ? error.stack.toString() : "No Error Stack Available"); + this._disconnected(ERROR.INTERNAL_ERROR.code, format(ERROR.INTERNAL_ERROR, [error.message, errorStack])); + return; + } + return messages; + }; + + ClientImpl.prototype._handleMessage = function(wireMessage) { + + this._trace("Client._handleMessage", wireMessage); + + try { + switch (wireMessage.type) { + case MESSAGE_TYPE.CONNACK: + this._connectTimeout.cancel(); + if (this._reconnectTimeout) + this._reconnectTimeout.cancel(); + + // If we have started using clean session then clear up the local state. + if (this.connectOptions.cleanSession) { + for (var key in this._sentMessages) { + var sentMessage = this._sentMessages[key]; + wx.removeStorageSync("Sent:" + this._localKey + sentMessage.messageIdentifier); + } + this._sentMessages = {}; + + for (var key in this._receivedMessages) { + var receivedMessage = this._receivedMessages[key]; + wx.removeStorageSync("Received:" + this._localKey + receivedMessage.messageIdentifier); + } + this._receivedMessages = {}; + } + // Client connected and ready for business. + if (wireMessage.returnCode === 0) { + + this.connected = true; + // Jump to the end of the list of uris and stop looking for a good host. + + if (this.connectOptions.uris) + this.hostIndex = this.connectOptions.uris.length; + + } else { + this._disconnected(ERROR.CONNACK_RETURNCODE.code, format(ERROR.CONNACK_RETURNCODE, [wireMessage.returnCode, CONNACK_RC[wireMessage.returnCode]])); + break; + } + + // Resend messages. + var sequencedMessages = []; + for (var msgId in this._sentMessages) { + if (this._sentMessages.hasOwnProperty(msgId)) + sequencedMessages.push(this._sentMessages[msgId]); + } + + // Also schedule qos 0 buffered messages if any + if (this._buffered_msg_queue.length > 0) { + var msg = null; + var fifo = this._buffered_msg_queue.reverse(); + while ((msg = fifo.pop())) { + sequencedMessages.push(msg); + if (this.onMessageDelivered) + this._notify_msg_sent[msg] = this.onMessageDelivered(msg.payloadMessage); + } + } + + // Sort sentMessages into the original sent order. + var sequencedMessages = sequencedMessages.sort(function(a, b) { return a.sequence - b.sequence; }); + for (var i = 0, len = sequencedMessages.length; i < len; i++) { + var sentMessage = sequencedMessages[i]; + if (sentMessage.type == MESSAGE_TYPE.PUBLISH && sentMessage.pubRecReceived) { + var pubRelMessage = new WireMessage(MESSAGE_TYPE.PUBREL, { messageIdentifier: sentMessage.messageIdentifier }); + this._schedule_message(pubRelMessage); + } else { + this._schedule_message(sentMessage); + } + } + + // Execute the connectOptions.onSuccess callback if there is one. + // Will also now return if this connection was the result of an automatic + // reconnect and which URI was successfully connected to. + if (this.connectOptions.onSuccess) { + this.connectOptions.onSuccess({ invocationContext: this.connectOptions.invocationContext }); + } + + var reconnected = false; + if (this._reconnecting) { + reconnected = true; + this._reconnectInterval = 1; + this._reconnecting = false; + } + + // Execute the onConnected callback if there is one. + this._connected(reconnected, this._wsuri); + + // Process all queued messages now that the connection is established. + this._process_queue(); + break; + + case MESSAGE_TYPE.PUBLISH: + this._receivePublish(wireMessage); + break; + + case MESSAGE_TYPE.PUBACK: + var sentMessage = this._sentMessages[wireMessage.messageIdentifier]; + // If this is a re flow of a PUBACK after we have restarted receivedMessage will not exist. + if (sentMessage) { + delete this._sentMessages[wireMessage.messageIdentifier]; + wx.removeStorageSync("Sent:" + this._localKey + wireMessage.messageIdentifier); + if (this.onMessageDelivered) + this.onMessageDelivered(sentMessage.payloadMessage); + } + break; + + case MESSAGE_TYPE.PUBREC: + var sentMessage = this._sentMessages[wireMessage.messageIdentifier]; + // If this is a re flow of a PUBREC after we have restarted receivedMessage will not exist. + if (sentMessage) { + sentMessage.pubRecReceived = true; + var pubRelMessage = new WireMessage(MESSAGE_TYPE.PUBREL, { messageIdentifier: wireMessage.messageIdentifier }); + this.store("Sent:", sentMessage); + this._schedule_message(pubRelMessage); + } + break; + + case MESSAGE_TYPE.PUBREL: + var receivedMessage = this._receivedMessages[wireMessage.messageIdentifier]; + wx.removeStorageSync("Received:" + this._localKey + wireMessage.messageIdentifier); + // If this is a re flow of a PUBREL after we have restarted receivedMessage will not exist. + if (receivedMessage) { + this._receiveMessage(receivedMessage); + delete this._receivedMessages[wireMessage.messageIdentifier]; + } + // Always flow PubComp, we may have previously flowed PubComp but the server lost it and restarted. + var pubCompMessage = new WireMessage(MESSAGE_TYPE.PUBCOMP, { messageIdentifier: wireMessage.messageIdentifier }); + this._schedule_message(pubCompMessage); + + + break; + + case MESSAGE_TYPE.PUBCOMP: + var sentMessage = this._sentMessages[wireMessage.messageIdentifier]; + delete this._sentMessages[wireMessage.messageIdentifier]; + wx.removeStorageSync("Sent:" + this._localKey + wireMessage.messageIdentifier); + if (this.onMessageDelivered) + this.onMessageDelivered(sentMessage.payloadMessage); + break; + + case MESSAGE_TYPE.SUBACK: + var sentMessage = this._sentMessages[wireMessage.messageIdentifier]; + if (sentMessage) { + if (sentMessage.timeOut) + sentMessage.timeOut.cancel(); + // This will need to be fixed when we add multiple topic support + if (wireMessage.returnCode[0] === 0x80) { + if (sentMessage.onFailure) { + sentMessage.onFailure(wireMessage.returnCode); + } + } else if (sentMessage.onSuccess) { + sentMessage.onSuccess(wireMessage.returnCode); + } + delete this._sentMessages[wireMessage.messageIdentifier]; + } + break; + + case MESSAGE_TYPE.UNSUBACK: + var sentMessage = this._sentMessages[wireMessage.messageIdentifier]; + if (sentMessage) { + if (sentMessage.timeOut) + sentMessage.timeOut.cancel(); + if (sentMessage.callback) { + sentMessage.callback(); + } + delete this._sentMessages[wireMessage.messageIdentifier]; + } + + break; + + case MESSAGE_TYPE.PINGRESP: + /* The sendPinger or receivePinger may have sent a ping, the receivePinger has already been reset. */ + this.sendPinger.reset(); + break; + + case MESSAGE_TYPE.DISCONNECT: + // Clients do not expect to receive disconnect packets. + this._disconnected(ERROR.INVALID_MQTT_MESSAGE_TYPE.code, format(ERROR.INVALID_MQTT_MESSAGE_TYPE, [wireMessage.type])); + break; + + default: + this._disconnected(ERROR.INVALID_MQTT_MESSAGE_TYPE.code, format(ERROR.INVALID_MQTT_MESSAGE_TYPE, [wireMessage.type])); + } + } catch (error) { + var errorStack = ((error.hasOwnProperty('stack') == 'undefined') ? error.stack.toString() : "No Error Stack Available"); + this._disconnected(ERROR.INTERNAL_ERROR.code, format(ERROR.INTERNAL_ERROR, [error.message, errorStack])); + return; + } + }; + + /** @ignore */ + ClientImpl.prototype._on_socket_error = function(error) { + if (!this._reconnecting) { + this._disconnected(ERROR.SOCKET_ERROR.code, format(ERROR.SOCKET_ERROR, [error.data])); + } + }; + + /** @ignore */ + ClientImpl.prototype._on_socket_close = function() { + if (!this._reconnecting) { + this._disconnected(ERROR.SOCKET_CLOSE.code, format(ERROR.SOCKET_CLOSE)); + } + }; + + /** @ignore */ + ClientImpl.prototype._socket_send = function(wireMessage) { + + if (wireMessage.type == 1) { + var wireMessageMasked = this._traceMask(wireMessage, "password"); + this._trace("Client._socket_send", wireMessageMasked); + } else this._trace("Client._socket_send", wireMessage); + + wx.sendSocketMessage({ + data: wireMessage.encode(), + success: function() { + // + }, + fail: function() { + // + }, + complete: function() { + // + } + }) + /* We have proved to the server we are alive. */ + this.sendPinger.reset(); + }; + + /** @ignore */ + ClientImpl.prototype._receivePublish = function(wireMessage) { + switch (wireMessage.payloadMessage.qos) { + case "undefined": + case 0: + this._receiveMessage(wireMessage); + break; + + case 1: + var pubAckMessage = new WireMessage(MESSAGE_TYPE.PUBACK, { messageIdentifier: wireMessage.messageIdentifier }); + this._schedule_message(pubAckMessage); + this._receiveMessage(wireMessage); + break; + + case 2: + this._receivedMessages[wireMessage.messageIdentifier] = wireMessage; + this.store("Received:", wireMessage); + var pubRecMessage = new WireMessage(MESSAGE_TYPE.PUBREC, { messageIdentifier: wireMessage.messageIdentifier }); + this._schedule_message(pubRecMessage); + + break; + + default: + throw Error("Invaild qos=" + wireMmessage.payloadMessage.qos); + } + }; + + /** @ignore */ + ClientImpl.prototype._receiveMessage = function(wireMessage) { + if (this.onMessageArrived) { + this.onMessageArrived(wireMessage.payloadMessage); + } + }; + + /** + * Client has connected. + * @param {reconnect} [boolean] indicate if this was a result of reconnect operation. + * @param {uri} [string] fully qualified WebSocket URI of the server. + */ + ClientImpl.prototype._connected = function(reconnect, uri) { + // Execute the onConnected callback if there is one. + if (this.onConnected) + this.onConnected(reconnect, uri); + }; + + /** + * Attempts to reconnect the client to the server. + * For each reconnect attempt, will double the reconnect interval + * up to 128 seconds. + */ + ClientImpl.prototype._reconnect = function() { + this._trace("Client._reconnect"); + if (!this.connected) { + this._reconnecting = true; + this.sendPinger.cancel(); + this.receivePinger.cancel(); + if (this._reconnectInterval < 128) + this._reconnectInterval = this._reconnectInterval * 2; + if (this.connectOptions.uris) { + this.hostIndex = 0; + this._doConnect(this.connectOptions.uris[0]); + } else { + this._doConnect(this.uri); + } + } + }; + + /** + * Client has disconnected either at its own request or because the server + * or network disconnected it. Remove all non-durable state. + * @param {errorCode} [number] the error number. + * @param {errorText} [string] the error text. + * @ignore + */ + ClientImpl.prototype._disconnected = function(errorCode, errorText) { + this._trace("Client._disconnected", errorCode, errorText); + + if (errorCode !== undefined && this._reconnecting) { + //Continue automatic reconnect process + this._reconnectTimeout = new Timeout(this, this._reconnectInterval, this._reconnect); + return; + } + + this.sendPinger.cancel(); + this.receivePinger.cancel(); + if (this._connectTimeout) { + this._connectTimeout.cancel(); + this._connectTimeout = null; + } + + // Clear message buffers. + this._msg_queue = []; + this._buffered_msg_queue = []; + this._notify_msg_sent = {}; + + if (this.connectOptions.uris && this.hostIndex < this.connectOptions.uris.length - 1) { + // Try the next host. + this.hostIndex++; + this._doConnect(this.connectOptions.uris[this.hostIndex]); + } else { + + if (errorCode === undefined) { + errorCode = ERROR.OK.code; + errorText = format(ERROR.OK); + } + + // Run any application callbacks last as they may attempt to reconnect and hence create a new socket. + if (this.connected) { + this.connected = false; + // Execute the connectionLostCallback if there is one, and we were connected. + if (this.onConnectionLost) { + this.onConnectionLost({ errorCode: errorCode, errorMessage: errorText, reconnect: this.connectOptions.reconnect, uri: this._wsuri }); + } + if (errorCode !== ERROR.OK.code && this.connectOptions.reconnect) { + // Start automatic reconnect process for the very first time since last successful connect. + this._reconnectInterval = 1; + this._reconnect(); + return; + } + } else { + // Otherwise we never had a connection, so indicate that the connect has failed. + if (this.connectOptions.mqttVersion === 4 && this.connectOptions.mqttVersionExplicit === false) { + this._trace("Failed to connect V4, dropping back to V3"); + this.connectOptions.mqttVersion = 3; + if (this.connectOptions.uris) { + this.hostIndex = 0; + this._doConnect(this.connectOptions.uris[0]); + } else { + this._doConnect(this.uri); + } + } else if (this.connectOptions.onFailure) { + this.connectOptions.onFailure({ invocationContext: this.connectOptions.invocationContext, errorCode: errorCode, errorMessage: errorText }); + } + } + } + }; + + /** @ignore */ + ClientImpl.prototype._trace = function() { + // Pass trace message back to client's callback function + if (this.traceFunction) { + for (var i in arguments) { + if (typeof arguments[i] !== "undefined") + arguments.splice(i, 1, JSON.stringify(arguments[i])); + } + var record = Array.prototype.slice.call(arguments).join(""); + this.traceFunction({ severity: "Debug", message: record }); + } + + //buffer style trace + if (this._traceBuffer !== null) { + for (var i = 0, max = arguments.length; i < max; i++) { + if (this._traceBuffer.length == this._MAX_TRACE_ENTRIES) { + this._traceBuffer.shift(); + } + if (i === 0) this._traceBuffer.push(arguments[i]); + else if (typeof arguments[i] === "undefined") this._traceBuffer.push(arguments[i]); + else this._traceBuffer.push(" " + JSON.stringify(arguments[i])); + } + } + }; + + /** @ignore */ + ClientImpl.prototype._traceMask = function(traceObject, masked) { + var traceObjectMasked = {}; + for (var attr in traceObject) { + if (traceObject.hasOwnProperty(attr)) { + if (attr == masked) + traceObjectMasked[attr] = "******"; + else + traceObjectMasked[attr] = traceObject[attr]; + } + } + return traceObjectMasked; + }; + + // ------------------------------------------------------------------------ + // Public Programming interface. + // ------------------------------------------------------------------------ + + /** + * The JavaScript application communicates to the server using a {@link Paho.MQTT.Client} object. + *

+ * Most applications will create just one Client object and then call its connect() method, + * however applications can create more than one Client object if they wish. + * In this case the combination of host, port and clientId attributes must be different for each Client object. + *

+ * The send, subscribe and unsubscribe methods are implemented as asynchronous JavaScript methods + * (even though the underlying protocol exchange might be synchronous in nature). + * This means they signal their completion by calling back to the application, + * via Success or Failure callback functions provided by the application on the method in question. + * Such callbacks are called at most once per method invocation and do not persist beyond the lifetime + * of the script that made the invocation. + *

+ * In contrast there are some callback functions, most notably onMessageArrived, + * that are defined on the {@link Paho.MQTT.Client} object. + * These may get called multiple times, and aren't directly related to specific method invocations made by the client. + * + * @name Paho.MQTT.Client + * + * @constructor + * + * @param {string} host - the address of the messaging server, as a fully qualified WebSocket URI, as a DNS name or dotted decimal IP address. + * @param {number} port - the port number to connect to - only required if host is not a URI + * @param {string} path - the path on the host to connect to - only used if host is not a URI. Default: '/mqtt'. + * @param {string} clientId - the Messaging client identifier, between 1 and 23 characters in length. + * + * @property {string} host - read only the server's DNS hostname or dotted decimal IP address. + * @property {number} port - read only the server's port. + * @property {string} path - read only the server's path. + * @property {string} clientId - read only used when connecting to the server. + * @property {function} onConnectionLost - called when a connection has been lost. + * after a connect() method has succeeded. + * Establish the call back used when a connection has been lost. The connection may be + * lost because the client initiates a disconnect or because the server or network + * cause the client to be disconnected. The disconnect call back may be called without + * the connectionComplete call back being invoked if, for example the client fails to + * connect. + * A single response object parameter is passed to the onConnectionLost callback containing the following fields: + *

    + *
  1. errorCode + *
  2. errorMessage + *
+ * @property {function} onMessageDelivered - called when a message has been delivered. + * All processing that this Client will ever do has been completed. So, for example, + * in the case of a Qos=2 message sent by this client, the PubComp flow has been received from the server + * and the message has been removed from persistent storage before this callback is invoked. + * Parameters passed to the onMessageDelivered callback are: + *
    + *
  1. {@link Paho.MQTT.Message} that was delivered. + *
+ * @property {function} onMessageArrived - called when a message has arrived in this Paho.MQTT.client. + * Parameters passed to the onMessageArrived callback are: + *
    + *
  1. {@link Paho.MQTT.Message} that has arrived. + *
+ * @property {function} onConnected - called when a connection is successfully made to the server. + * after a connect() method. + * Parameters passed to the onConnected callback are: + *
    + *
  1. reconnect (boolean) - If true, the connection was the result of a reconnect.
  2. + *
  3. URI (string) - The URI used to connect to the server.
  4. + *
+ * @property {boolean} disconnectedPublishing - if set, will enable disconnected publishing in + * in the event that the connection to the server is lost. + * @property {number} disconnectedBufferSize - Used to set the maximum number of messages that the disconnected + * buffer will hold before rejecting new messages. Default size: 5000 messages + * @property {function} trace - called whenever trace is called. TODO + */ + var Client = function(host, port, path, clientId) { + + var uri; + + if (typeof host !== "string") + throw new Error(format(ERROR.INVALID_TYPE, [typeof host, "host"])); + if (arguments.length == 2) { + // host: must be full ws:// uri + // port: clientId + clientId = port; + uri = host; + var match = uri.match(/^(wss?):\/\/((\[(.+)\])|([^\/]+?))(:(\d+))?(\/.*)$/); + if (match) { + host = match[4] || match[2]; + port = parseInt(match[7]); + path = match[8]; + } else { + throw new Error(format(ERROR.INVALID_ARGUMENT, [host, "host"])); + } + } else { + if (arguments.length == 3) { + clientId = path; + path = "/mqtt"; + } + // if (typeof port !== "number" || port < 0) + // throw new Error(format(ERROR.INVALID_TYPE, [typeof port, "port"])); + // if (typeof path !== "string") + // throw new Error(format(ERROR.INVALID_TYPE, [typeof path, "path"])); + + var ipv6AddSBracket = (host.indexOf(":") !== -1 && host.slice(0, 1) !== "[" && host.slice(-1) !== "]"); + uri = "wss://" + (ipv6AddSBracket ? "[" + host + "]" : host) + ":" + port + path; + if(port == 0) { + uri = "wss://" + (ipv6AddSBracket ? "[" + host + "]" : host) + path; + } + } + + var clientIdLength = 0; + for (var i = 0; i < clientId.length; i++) { + var charCode = clientId.charCodeAt(i); + if (0xD800 <= charCode && charCode <= 0xDBFF) { + i++; // Surrogate pair. + } + clientIdLength++; + } + if (typeof clientId !== "string" || clientIdLength > 65535) + throw new Error(format(ERROR.INVALID_ARGUMENT, [clientId, "clientId"])); + + var client = new ClientImpl(uri, host, port, path, clientId); + this._getHost = function() { return host; }; + this._setHost = function() { throw new Error(format(ERROR.UNSUPPORTED_OPERATION)); }; + + this._getPort = function() { return port; }; + this._setPort = function() { throw new Error(format(ERROR.UNSUPPORTED_OPERATION)); }; + + this._getPath = function() { return path; }; + this._setPath = function() { throw new Error(format(ERROR.UNSUPPORTED_OPERATION)); }; + + this._getURI = function() { return uri; }; + this._setURI = function() { throw new Error(format(ERROR.UNSUPPORTED_OPERATION)); }; + + this._getClientId = function() { return client.clientId; }; + this._setClientId = function() { throw new Error(format(ERROR.UNSUPPORTED_OPERATION)); }; + + this._getOnConnected = function() { return client.onConnected; }; + this._setOnConnected = function(newOnConnected) { + if (typeof newOnConnected === "function") + client.onConnected = newOnConnected; + else + throw new Error(format(ERROR.INVALID_TYPE, [typeof newOnConnected, "onConnected"])); + }; + + this._getDisconnectedPublishing = function() { return client.disconnectedPublishing; }; + this._setDisconnectedPublishing = function(newDisconnectedPublishing) { + client.disconnectedPublishing = newDisconnectedPublishing; + }; + + this._getDisconnectedBufferSize = function() { return client.disconnectedBufferSize; }; + this._setDisconnectedBufferSize = function(newDisconnectedBufferSize) { + client.disconnectedBufferSize = newDisconnectedBufferSize; + }; + + this._getOnConnectionLost = function() { return client.onConnectionLost; }; + this._setOnConnectionLost = function(newOnConnectionLost) { + if (typeof newOnConnectionLost === "function") + client.onConnectionLost = newOnConnectionLost; + else + throw new Error(format(ERROR.INVALID_TYPE, [typeof newOnConnectionLost, "onConnectionLost"])); + }; + + this._getOnMessageDelivered = function() { return client.onMessageDelivered; }; + this._setOnMessageDelivered = function(newOnMessageDelivered) { + if (typeof newOnMessageDelivered === "function") + client.onMessageDelivered = newOnMessageDelivered; + else + throw new Error(format(ERROR.INVALID_TYPE, [typeof newOnMessageDelivered, "onMessageDelivered"])); + }; + + this._getOnMessageArrived = function() { return client.onMessageArrived; }; + this._setOnMessageArrived = function(newOnMessageArrived) { + if (typeof newOnMessageArrived === "function") + client.onMessageArrived = newOnMessageArrived; + else + throw new Error(format(ERROR.INVALID_TYPE, [typeof newOnMessageArrived, "onMessageArrived"])); + }; + + this._getTrace = function() { return client.traceFunction; }; + this._setTrace = function(trace) { + if (typeof trace === "function") { + client.traceFunction = trace; + } else { + throw new Error(format(ERROR.INVALID_TYPE, [typeof trace, "onTrace"])); + } + }; + + /** + * Connect this Messaging client to its server. + * + * @name Paho.MQTT.Client#connect + * @function + * @param {object} connectOptions - Attributes used with the connection. + * @param {number} connectOptions.timeout - If the connect has not succeeded within this + * number of seconds, it is deemed to have failed. + * The default is 30 seconds. + * @param {string} connectOptions.userName - Authentication username for this connection. + * @param {string} connectOptions.password - Authentication password for this connection. + * @param {Paho.MQTT.Message} connectOptions.willMessage - sent by the server when the client + * disconnects abnormally. + * @param {number} connectOptions.keepAliveInterval - the server disconnects this client if + * there is no activity for this number of seconds. + * The default value of 60 seconds is assumed if not set. + * @param {boolean} connectOptions.cleanSession - if true(default) the client and server + * persistent state is deleted on successful connect. + * @param {boolean} connectOptions.useSSL - if present and true, use an SSL Websocket connection. + * @param {object} connectOptions.invocationContext - passed to the onSuccess callback or onFailure callback. + * @param {function} connectOptions.onSuccess - called when the connect acknowledgement + * has been received from the server. + * A single response object parameter is passed to the onSuccess callback containing the following fields: + *
    + *
  1. invocationContext as passed in to the onSuccess method in the connectOptions. + *
+ * @param {function} connectOptions.onFailure - called when the connect request has failed or timed out. + * A single response object parameter is passed to the onFailure callback containing the following fields: + *
    + *
  1. invocationContext as passed in to the onFailure method in the connectOptions. + *
  2. errorCode a number indicating the nature of the error. + *
  3. errorMessage text describing the error. + *
+ * @param {array} connectOptions.hosts - If present this contains either a set of hostnames or fully qualified + * WebSocket URIs (ws://iot.eclipse.org:80/ws), that are tried in order in place + * of the host and port paramater on the construtor. The hosts are tried one at at time in order until + * one of then succeeds. + * @param {array} connectOptions.ports - If present the set of ports matching the hosts. If hosts contains URIs, this property + * is not used. + * @param {boolean} connectOptions.reconnect - Sets whether the client will automatically attempt to reconnect + * to the server if the connection is lost. + *
    + *
  • If set to false, the client will not attempt to automatically reconnect to the server in the event that the + * connection is lost.
  • + *
  • If set to true, in the event that the connection is lost, the client will attempt to reconnect to the server. + * It will initially wait 1 second before it attempts to reconnect, for every failed reconnect attempt, the delay + * will double until it is at 2 minutes at which point the delay will stay at 2 minutes.
  • + *
+ * @param {number} connectOptions.mqttVersion - The version of MQTT to use to connect to the MQTT Broker. + *
    + *
  • 3 - MQTT V3.1
  • + *
  • 4 - MQTT V3.1.1
  • + *
+ * @param {boolean} connectOptions.mqttVersionExplicit - If set to true, will force the connection to use the + * selected MQTT Version or will fail to connect. + * @param {array} connectOptions.uris - If present, should contain a list of fully qualified WebSocket uris + * (e.g. ws://iot.eclipse.org:80/ws), that are tried in order in place of the host and port parameter of the construtor. + * The uris are tried one at a time in order until one of them succeeds. Do not use this in conjunction with hosts as + * the hosts array will be converted to uris and will overwrite this property. + * @throws {InvalidState} If the client is not in disconnected state. The client must have received connectionLost + * or disconnected before calling connect for a second or subsequent time. + */ + this.connect = function(connectOptions) { + connectOptions = connectOptions || {}; + validate(connectOptions, { + timeout: "number", + userName: "string", + password: "string", + willMessage: "object", + keepAliveInterval: "number", + cleanSession: "boolean", + useSSL: "boolean", + invocationContext: "object", + onSuccess: "function", + onFailure: "function", + hosts: "object", + ports: "object", + reconnect: "boolean", + mqttVersion: "number", + mqttVersionExplicit: "boolean", + uris: "object" + }); + + // If no keep alive interval is set, assume 60 seconds. + if (connectOptions.keepAliveInterval === undefined) + connectOptions.keepAliveInterval = 60; + + if (connectOptions.mqttVersion > 4 || connectOptions.mqttVersion < 3) { + throw new Error(format(ERROR.INVALID_ARGUMENT, [connectOptions.mqttVersion, "connectOptions.mqttVersion"])); + } + + if (connectOptions.mqttVersion === undefined) { + connectOptions.mqttVersionExplicit = false; + connectOptions.mqttVersion = 4; + } else { + connectOptions.mqttVersionExplicit = true; + } + + //Check that if password is set, so is username + if (connectOptions.password !== undefined && connectOptions.userName === undefined) + throw new Error(format(ERROR.INVALID_ARGUMENT, [connectOptions.password, "connectOptions.password"])); + + if (connectOptions.willMessage) { + if (!(connectOptions.willMessage instanceof Message)) + throw new Error(format(ERROR.INVALID_TYPE, [connectOptions.willMessage, "connectOptions.willMessage"])); + // The will message must have a payload that can be represented as a string. + // Cause the willMessage to throw an exception if this is not the case. + connectOptions.willMessage.stringPayload = null; + + if (typeof connectOptions.willMessage.destinationName === "undefined") + throw new Error(format(ERROR.INVALID_TYPE, [typeof connectOptions.willMessage.destinationName, "connectOptions.willMessage.destinationName"])); + } + if (typeof connectOptions.cleanSession === "undefined") + connectOptions.cleanSession = true; + if (connectOptions.hosts) { + + if (!(connectOptions.hosts instanceof Array)) + throw new Error(format(ERROR.INVALID_ARGUMENT, [connectOptions.hosts, "connectOptions.hosts"])); + if (connectOptions.hosts.length < 1) + throw new Error(format(ERROR.INVALID_ARGUMENT, [connectOptions.hosts, "connectOptions.hosts"])); + + var usingURIs = false; + for (var i = 0; i < connectOptions.hosts.length; i++) { + if (typeof connectOptions.hosts[i] !== "string") + throw new Error(format(ERROR.INVALID_TYPE, [typeof connectOptions.hosts[i], "connectOptions.hosts[" + i + "]"])); + if (/^(wss?):\/\/((\[(.+)\])|([^\/]+?))(:(\d+))?(\/.*)$/.test(connectOptions.hosts[i])) { + if (i === 0) { + usingURIs = true; + } else if (!usingURIs) { + throw new Error(format(ERROR.INVALID_ARGUMENT, [connectOptions.hosts[i], "connectOptions.hosts[" + i + "]"])); + } + } else if (usingURIs) { + throw new Error(format(ERROR.INVALID_ARGUMENT, [connectOptions.hosts[i], "connectOptions.hosts[" + i + "]"])); + } + } + + if (!usingURIs) { + if (!connectOptions.ports) + throw new Error(format(ERROR.INVALID_ARGUMENT, [connectOptions.ports, "connectOptions.ports"])); + if (!(connectOptions.ports instanceof Array)) + throw new Error(format(ERROR.INVALID_ARGUMENT, [connectOptions.ports, "connectOptions.ports"])); + if (connectOptions.hosts.length !== connectOptions.ports.length) + throw new Error(format(ERROR.INVALID_ARGUMENT, [connectOptions.ports, "connectOptions.ports"])); + + connectOptions.uris = []; + + for (var i = 0; i < connectOptions.hosts.length; i++) { + if (typeof connectOptions.ports[i] !== "number" || connectOptions.ports[i] < 0) + throw new Error(format(ERROR.INVALID_TYPE, [typeof connectOptions.ports[i], "connectOptions.ports[" + i + "]"])); + var host = connectOptions.hosts[i]; + var port = connectOptions.ports[i]; + + var ipv6 = (host.indexOf(":") !== -1); + uri = "ws://" + (ipv6 ? "[" + host + "]" : host) + ":" + port + path; + connectOptions.uris.push(uri); + } + } else { + connectOptions.uris = connectOptions.hosts; + } + } + + client.connect(connectOptions); + }; + + /** + * Subscribe for messages, request receipt of a copy of messages sent to the destinations described by the filter. + * + * @name Paho.MQTT.Client#subscribe + * @function + * @param {string} filter describing the destinations to receive messages from. + *
+ * @param {object} subscribeOptions - used to control the subscription + * + * @param {number} subscribeOptions.qos - the maiximum qos of any publications sent + * as a result of making this subscription. + * @param {object} subscribeOptions.invocationContext - passed to the onSuccess callback + * or onFailure callback. + * @param {function} subscribeOptions.onSuccess - called when the subscribe acknowledgement + * has been received from the server. + * A single response object parameter is passed to the onSuccess callback containing the following fields: + *
    + *
  1. invocationContext if set in the subscribeOptions. + *
+ * @param {function} subscribeOptions.onFailure - called when the subscribe request has failed or timed out. + * A single response object parameter is passed to the onFailure callback containing the following fields: + *
    + *
  1. invocationContext - if set in the subscribeOptions. + *
  2. errorCode - a number indicating the nature of the error. + *
  3. errorMessage - text describing the error. + *
+ * @param {number} subscribeOptions.timeout - which, if present, determines the number of + * seconds after which the onFailure calback is called. + * The presence of a timeout does not prevent the onSuccess + * callback from being called when the subscribe completes. + * @throws {InvalidState} if the client is not in connected state. + */ + this.subscribe = function(filter, subscribeOptions) { + if (typeof filter !== "string") + throw new Error("Invalid argument:" + filter); + subscribeOptions = subscribeOptions || {}; + validate(subscribeOptions, { + qos: "number", + invocationContext: "object", + onSuccess: "function", + onFailure: "function", + timeout: "number" + }); + if (subscribeOptions.timeout && !subscribeOptions.onFailure) + throw new Error("subscribeOptions.timeout specified with no onFailure callback."); + if (typeof subscribeOptions.qos !== "undefined" && !(subscribeOptions.qos === 0 || subscribeOptions.qos === 1 || subscribeOptions.qos === 2)) + throw new Error(format(ERROR.INVALID_ARGUMENT, [subscribeOptions.qos, "subscribeOptions.qos"])); + client.subscribe(filter, subscribeOptions); + }; + + /** + * Unsubscribe for messages, stop receiving messages sent to destinations described by the filter. + * + * @name Paho.MQTT.Client#unsubscribe + * @function + * @param {string} filter - describing the destinations to receive messages from. + * @param {object} unsubscribeOptions - used to control the subscription + * @param {object} unsubscribeOptions.invocationContext - passed to the onSuccess callback + or onFailure callback. + * @param {function} unsubscribeOptions.onSuccess - called when the unsubscribe acknowledgement has been received from the server. + * A single response object parameter is passed to the + * onSuccess callback containing the following fields: + *
    + *
  1. invocationContext - if set in the unsubscribeOptions. + *
+ * @param {function} unsubscribeOptions.onFailure called when the unsubscribe request has failed or timed out. + * A single response object parameter is passed to the onFailure callback containing the following fields: + *
    + *
  1. invocationContext - if set in the unsubscribeOptions. + *
  2. errorCode - a number indicating the nature of the error. + *
  3. errorMessage - text describing the error. + *
+ * @param {number} unsubscribeOptions.timeout - which, if present, determines the number of seconds + * after which the onFailure callback is called. The presence of + * a timeout does not prevent the onSuccess callback from being + * called when the unsubscribe completes + * @throws {InvalidState} if the client is not in connected state. + */ + this.unsubscribe = function(filter, unsubscribeOptions) { + if (typeof filter !== "string") + throw new Error("Invalid argument:" + filter); + unsubscribeOptions = unsubscribeOptions || {}; + validate(unsubscribeOptions, { + invocationContext: "object", + onSuccess: "function", + onFailure: "function", + timeout: "number" + }); + if (unsubscribeOptions.timeout && !unsubscribeOptions.onFailure) + throw new Error("unsubscribeOptions.timeout specified with no onFailure callback."); + client.unsubscribe(filter, unsubscribeOptions); + }; + + /** + * Send a message to the consumers of the destination in the Message. + * + * @name Paho.MQTT.Client#send + * @function + * @param {string|Paho.MQTT.Message} topic - mandatory The name of the destination to which the message is to be sent. + * - If it is the only parameter, used as Paho.MQTT.Message object. + * @param {String|ArrayBuffer} payload - The message data to be sent. + * @param {number} qos The Quality of Service used to deliver the message. + *
+ *
0 Best effort (default). + *
1 At least once. + *
2 Exactly once. + *
+ * @param {Boolean} retained If true, the message is to be retained by the server and delivered + * to both current and future subscriptions. + * If false the server only delivers the message to current subscribers, this is the default for new Messages. + * A received message has the retained boolean set to true if the message was published + * with the retained boolean set to true + * and the subscrption was made after the message has been published. + * @throws {InvalidState} if the client is not connected. + */ + this.send = function(topic, payload, qos, retained) { + var message; + + if (arguments.length === 0) { + throw new Error("Invalid argument." + "length"); + + } else if (arguments.length == 1) { + + if (!(topic instanceof Message) && (typeof topic !== "string")) + throw new Error("Invalid argument:" + typeof topic); + + message = topic; + if (typeof message.destinationName === "undefined") + throw new Error(format(ERROR.INVALID_ARGUMENT, [message.destinationName, "Message.destinationName"])); + client.send(message); + + } else { + //parameter checking in Message object + message = new Message(payload); + message.destinationName = topic; + if (arguments.length >= 3) + message.qos = qos; + if (arguments.length >= 4) + message.retained = retained; + client.send(message); + } + }; + + /** + * Publish a message to the consumers of the destination in the Message. + * Synonym for Paho.Mqtt.Client#send + * + * @name Paho.MQTT.Client#publish + * @function + * @param {string|Paho.MQTT.Message} topic - mandatory The name of the topic to which the message is to be published. + * - If it is the only parameter, used as Paho.MQTT.Message object. + * @param {String|ArrayBuffer} payload - The message data to be published. + * @param {number} qos The Quality of Service used to deliver the message. + *
+ *
0 Best effort (default). + *
1 At least once. + *
2 Exactly once. + *
+ * @param {Boolean} retained If true, the message is to be retained by the server and delivered + * to both current and future subscriptions. + * If false the server only delivers the message to current subscribers, this is the default for new Messages. + * A received message has the retained boolean set to true if the message was published + * with the retained boolean set to true + * and the subscrption was made after the message has been published. + * @throws {InvalidState} if the client is not connected. + */ + this.publish = function(topic, payload, qos, retained) { + var message; + if (arguments.length === 0) { + throw new Error("Invalid argument." + "length"); + + } else if (arguments.length == 1) { + + if (!(topic instanceof Message) && (typeof topic !== "string")) + throw new Error("Invalid argument:" + typeof topic); + + message = topic; + if (typeof message.destinationName === "undefined") + throw new Error(format(ERROR.INVALID_ARGUMENT, [message.destinationName, "Message.destinationName"])); + client.send(message); + + } else { + //parameter checking in Message object + message = new Message(payload); + message.destinationName = topic; + if (arguments.length >= 3) + message.qos = qos; + if (arguments.length >= 4) + message.retained = retained; + client.send(message); + } + }; + + /** + * Normal disconnect of this Messaging client from its server. + * + * @name Paho.MQTT.Client#disconnect + * @function + * @throws {InvalidState} if the client is already disconnected. + */ + this.disconnect = function() { + client.disconnect(); + }; + + /** + * Get the contents of the trace log. + * + * @name Paho.MQTT.Client#getTraceLog + * @function + * @return {Object[]} tracebuffer containing the time ordered trace records. + */ + this.getTraceLog = function() { + return client.getTraceLog(); + }; + + /** + * Start tracing. + * + * @name Paho.MQTT.Client#startTrace + * @function + */ + this.startTrace = function() { + client.startTrace(); + }; + + /** + * Stop tracing. + * + * @name Paho.MQTT.Client#stopTrace + * @function + */ + this.stopTrace = function() { + client.stopTrace(); + }; + + this.isConnected = function() { + return client.connected; + }; + }; + + Client.prototype = { + get host() { return this._getHost(); }, + set host(newHost) { this._setHost(newHost); }, + + get port() { return this._getPort(); }, + set port(newPort) { this._setPort(newPort); }, + + get path() { return this._getPath(); }, + set path(newPath) { this._setPath(newPath); }, + + get clientId() { return this._getClientId(); }, + set clientId(newClientId) { this._setClientId(newClientId); }, + + get onConnected() { return this._getOnConnected(); }, + set onConnected(newOnConnected) { this._setOnConnected(newOnConnected); }, + + get disconnectedPublishing() { return this._getDisconnectedPublishing(); }, + set disconnectedPublishing(newDisconnectedPublishing) { this._setDisconnectedPublishing(newDisconnectedPublishing); }, + + get disconnectedBufferSize() { return this._getDisconnectedBufferSize(); }, + set disconnectedBufferSize(newDisconnectedBufferSize) { this._setDisconnectedBufferSize(newDisconnectedBufferSize); }, + + get onConnectionLost() { return this._getOnConnectionLost(); }, + set onConnectionLost(newOnConnectionLost) { this._setOnConnectionLost(newOnConnectionLost); }, + + get onMessageDelivered() { return this._getOnMessageDelivered(); }, + set onMessageDelivered(newOnMessageDelivered) { this._setOnMessageDelivered(newOnMessageDelivered); }, + + get onMessageArrived() { return this._getOnMessageArrived(); }, + set onMessageArrived(newOnMessageArrived) { this._setOnMessageArrived(newOnMessageArrived); }, + + get trace() { return this._getTrace(); }, + set trace(newTraceFunction) { this._setTrace(newTraceFunction); } + + }; + + /** + * An application message, sent or received. + *

+ * All attributes may be null, which implies the default values. + * + * @name Paho.MQTT.Message + * @constructor + * @param {String|ArrayBuffer} payload The message data to be sent. + *

+ * @property {string} payloadString read only The payload as a string if the payload consists of valid UTF-8 characters. + * @property {ArrayBuffer} payloadBytes read only The payload as an ArrayBuffer. + *

+ * @property {string} destinationName mandatory The name of the destination to which the message is to be sent + * (for messages about to be sent) or the name of the destination from which the message has been received. + * (for messages received by the onMessage function). + *

+ * @property {number} qos The Quality of Service used to deliver the message. + *

+ *
0 Best effort (default). + *
1 At least once. + *
2 Exactly once. + *
+ *

+ * @property {Boolean} retained If true, the message is to be retained by the server and delivered + * to both current and future subscriptions. + * If false the server only delivers the message to current subscribers, this is the default for new Messages. + * A received message has the retained boolean set to true if the message was published + * with the retained boolean set to true + * and the subscrption was made after the message has been published. + *

+ * @property {Boolean} duplicate read only If true, this message might be a duplicate of one which has already been received. + * This is only set on messages received from the server. + * + */ + var Message = function(newPayload) { + var payload; + if (typeof newPayload === "string" || + newPayload instanceof ArrayBuffer || + newPayload instanceof Int8Array || + newPayload instanceof Uint8Array || + newPayload instanceof Int16Array || + newPayload instanceof Uint16Array || + newPayload instanceof Int32Array || + newPayload instanceof Uint32Array || + newPayload instanceof Float32Array || + newPayload instanceof Float64Array + ) { + payload = newPayload; + } else { + throw (format(ERROR.INVALID_ARGUMENT, [newPayload, "newPayload"])); + } + + this._getPayloadString = function() { + if (typeof payload === "string") + return payload; + else + return parseUTF8(payload, 0, payload.length); + }; + + this._getPayloadBytes = function() { + if (typeof payload === "string") { + var buffer = new ArrayBuffer(UTF8Length(payload)); + var byteStream = new Uint8Array(buffer); + stringToUTF8(payload, byteStream, 0); + + return byteStream; + } else { + return payload; + } + }; + + var destinationName; + this._getDestinationName = function() { return destinationName; }; + this._setDestinationName = function(newDestinationName) { + if (typeof newDestinationName === "string") + destinationName = newDestinationName; + else + throw new Error(format(ERROR.INVALID_ARGUMENT, [newDestinationName, "newDestinationName"])); + }; + + var qos = 0; + this._getQos = function() { return qos; }; + this._setQos = function(newQos) { + if (newQos === 0 || newQos === 1 || newQos === 2) + qos = newQos; + else + throw new Error("Invalid argument:" + newQos); + }; + + var retained = false; + this._getRetained = function() { return retained; }; + this._setRetained = function(newRetained) { + if (typeof newRetained === "boolean") + retained = newRetained; + else + throw new Error(format(ERROR.INVALID_ARGUMENT, [newRetained, "newRetained"])); + }; + + var duplicate = false; + this._getDuplicate = function() { return duplicate; }; + this._setDuplicate = function(newDuplicate) { duplicate = newDuplicate; }; + }; + + Message.prototype = { + get payloadString() { return this._getPayloadString(); }, + get payloadBytes() { return this._getPayloadBytes(); }, + + get destinationName() { return this._getDestinationName(); }, + set destinationName(newDestinationName) { this._setDestinationName(newDestinationName); }, + + get topic() { return this._getDestinationName(); }, + set topic(newTopic) { this._setDestinationName(newTopic); }, + + get qos() { return this._getQos(); }, + set qos(newQos) { this._setQos(newQos); }, + + get retained() { return this._getRetained(); }, + set retained(newRetained) { this._setRetained(newRetained); }, + + get duplicate() { return this._getDuplicate(); }, + set duplicate(newDuplicate) { this._setDuplicate(newDuplicate); } + }; + + // Module contents. + return { + Client: Client, + Message: Message + }; + })(wx); + return PahoMQTT; +}); \ No newline at end of file diff --git a/utils/point.js b/utils/point.js new file mode 100644 index 0000000..466a7f1 --- /dev/null +++ b/utils/point.js @@ -0,0 +1,135 @@ +var jsapi = require('api.js'); +var util = require('util.js'); +var app = getApp(); + +var pointInfo = { + /*--------随机字符串-------*/ + randomString: function() { + var randomString = ""; + for (var i = 1; i <= 5; i++) { + randomString += parseInt(Math.random() * 10); + } + return randomString; + }, + /*--------自动生成订单号-------*/ + generateTradeNo: function() { + return util.formatTimeV(new Date(), 'yyMMddhhmmss') + app.globalData.shopNo + this.randomString(); + }, + + /*--------查询积分商品列表-------*/ + queryPointProductList: function(type) { + var data = { + "wid": app.globalData.memberWid, + "method": "query.point.product.list", + "pageType": type + }; + return jsapi.cardApi.ajax(data, []); + }, + /*--------查询积分商品详情-------*/ + queryPointProductDetail: function (id, schemeId) { + var data = { + "method": "query.point.product.detail", + "productId": id, + "schemeId": schemeId + }; + return jsapi.cardApi.ajax(data, []); + }, + + /*--------创建积分商城订单-------*/ + createPointProductOrder: function (payAmount, payPoint, schemeId, payType, addressList, num, imageUrl, remark) { + var data = { + "method": "weixin.point.product.order.create", + "wid": app.globalData.memberWid, + "cardNo": wx.getStorageSync("cardNo"), + "mobile": wx.getStorageSync("cardInfo").mobile, + "openid": wx.getStorageSync("openId"), + "outTradeNo": this.generateTradeNo(), + "payAmount": payAmount, + "payPoint": payPoint, + "createTime": util.formatTimeV(new Date(), 'yyyy-MM-dd hh:mm:ss'), + "schemeId": schemeId, + "payType": payType, + "num": num, + "imageUrl": imageUrl, + "groupNo": app.globalData.groupNo, + "remark":remark + }; + var ignores = ["mobile", "payType", "remark", "imageUrl"]; + if (addressList){ + if (addressList.receiveMobile){ + ignores.push("receiveMobile"); + data.receiveMobile = addressList.receiveMobile + } + if (addressList.receiveAddress) { + ignores.push("receiveAddress"); + data.receiveAddress = addressList.receiveAddress + } + if (addressList.receiveDoor) { + ignores.push("receiveDoor"); + data.receiveDoor = addressList.receiveDoor + } + if (addressList.receiveName) { + ignores.push("receiveName"); + data.receiveName = addressList.receiveName + } + } + return jsapi.cardApi.ajax(data, ignores); + }, + /*--------拉取微信预支付参数-------*/ + pullOrderParam: function (outTradeNo, payAmount, type, schemeId, point, num) { + var data = { + "method": "weixin.unifiedorder.point", + "wid": app.globalData.memberWid, + "outTradeNo": outTradeNo, + "openid": wx.getStorageSync("openId"), + "spbillCreateIp": "127.0.0.1", + "type": type, + "groupNo": app.globalData.groupNo, + "sourceSign": "wechat", + "posNo": app.globalData.posNo, + "shopNo": app.globalData.shopNo, + "cardNo": wx.getStorageSync("cardNo"), + "workerNo": app.globalData.workerNo, + "memberId": wx.getStorageSync("memberId"), + "schemeId": schemeId, + "body": "积分商城下单" + }; + var ignores = ["body", "payType"]; + return jsapi.cardApi.ajax(data, ignores); + }, + /*--------查询订单-------*/ + queryPointOrder: function(orderNo) { + var data = { + "method": "query.point.product.order", + "wid": app.globalData.memberWid, + "orderNo": orderNo + }; + var ignores = []; + return jsapi.cardApi.ajax(data, ignores); + }, + /*--------查询积分商城订单列表-------*/ + queryPointOrderList: function (pageSize, pageNum) { + var data = { + "method": "query.point.product.order.list", + "cardNo": wx.getStorageSync("cardNo"), + "pageSize": pageSize, + "pageNum": pageNum + }; + var ignores = []; + return jsapi.cardApi.ajax(data, ignores); + }, + /*--------查询积分商城订单详情-------*/ + queryPointOrderDetail: function (orderNo) { + var data = { + "method": "query.point.product.order.detail", + "orderNo": orderNo + }; + var ignores = []; + return jsapi.cardApi.ajax(data, ignores); + } +} + + +module.exports = { + pointInfo: pointInfo +} \ No newline at end of file diff --git a/utils/qqmap-wx-jssdk.min.js b/utils/qqmap-wx-jssdk.min.js new file mode 100644 index 0000000..3ab824e --- /dev/null +++ b/utils/qqmap-wx-jssdk.min.js @@ -0,0 +1,876 @@ +var ERROR_CONF = { + KEY_ERR: 311, + KEY_ERR_MSG: 'key格式错误', + PARAM_ERR: 310, + PARAM_ERR_MSG: '请求参数信息有误', + SYSTEM_ERR: 600, + SYSTEM_ERR_MSG: '系统错误', + WX_ERR_CODE: 1000, + WX_OK_CODE: 200 +}; +var BASE_URL = 'https://apis.map.qq.com/ws/'; +var URL_SEARCH = BASE_URL + 'place/v1/search'; +var URL_SUGGESTION = BASE_URL + 'place/v1/suggestion'; +var URL_GET_GEOCODER = BASE_URL + 'geocoder/v1/'; +var URL_CITY_LIST = BASE_URL + 'district/v1/list'; +var URL_AREA_LIST = BASE_URL + 'district/v1/getchildren'; +var URL_DISTANCE = BASE_URL + 'distance/v1/'; +var URL_DIRECTION = BASE_URL + 'direction/v1/'; +var MODE = { + driving: 'driving', + transit: 'transit' +}; +var EARTH_RADIUS = 6378136.49; +var Utils = { + safeAdd(x, y) { + var lsw = (x & 0xffff) + (y & 0xffff); + var msw = (x >> 16) + (y >> 16) + (lsw >> 16); + return (msw << 16) | (lsw & 0xffff) + }, + bitRotateLeft(num, cnt) { + return (num << cnt) | (num >>> (32 - cnt)) + }, + md5cmn(q, a, b, x, s, t) { + return this.safeAdd(this.bitRotateLeft(this.safeAdd(this.safeAdd(a, q), this.safeAdd(x, t)), s), b) + }, + md5ff(a, b, c, d, x, s, t) { + return this.md5cmn((b & c) | (~b & d), a, b, x, s, t) + }, + md5gg(a, b, c, d, x, s, t) { + return this.md5cmn((b & d) | (c & ~d), a, b, x, s, t) + }, + md5hh(a, b, c, d, x, s, t) { + return this.md5cmn(b ^ c ^ d, a, b, x, s, t) + }, + md5ii(a, b, c, d, x, s, t) { + return this.md5cmn(c ^ (b | ~d), a, b, x, s, t) + }, + binlMD5(x, len) { + x[len >> 5] |= 0x80 << (len % 32); + x[((len + 64) >>> 9 << 4) + 14] = len; + var i; + var olda; + var oldb; + var oldc; + var oldd; + var a = 1732584193; + var b = -271733879; + var c = -1732584194; + var d = 271733878; + for (i = 0; i < x.length; i += 16) { + olda = a; + oldb = b; + oldc = c; + oldd = d; + a = this.md5ff(a, b, c, d, x[i], 7, -680876936); + d = this.md5ff(d, a, b, c, x[i + 1], 12, -389564586); + c = this.md5ff(c, d, a, b, x[i + 2], 17, 606105819); + b = this.md5ff(b, c, d, a, x[i + 3], 22, -1044525330); + a = this.md5ff(a, b, c, d, x[i + 4], 7, -176418897); + d = this.md5ff(d, a, b, c, x[i + 5], 12, 1200080426); + c = this.md5ff(c, d, a, b, x[i + 6], 17, -1473231341); + b = this.md5ff(b, c, d, a, x[i + 7], 22, -45705983); + a = this.md5ff(a, b, c, d, x[i + 8], 7, 1770035416); + d = this.md5ff(d, a, b, c, x[i + 9], 12, -1958414417); + c = this.md5ff(c, d, a, b, x[i + 10], 17, -42063); + b = this.md5ff(b, c, d, a, x[i + 11], 22, -1990404162); + a = this.md5ff(a, b, c, d, x[i + 12], 7, 1804603682); + d = this.md5ff(d, a, b, c, x[i + 13], 12, -40341101); + c = this.md5ff(c, d, a, b, x[i + 14], 17, -1502002290); + b = this.md5ff(b, c, d, a, x[i + 15], 22, 1236535329); + a = this.md5gg(a, b, c, d, x[i + 1], 5, -165796510); + d = this.md5gg(d, a, b, c, x[i + 6], 9, -1069501632); + c = this.md5gg(c, d, a, b, x[i + 11], 14, 643717713); + b = this.md5gg(b, c, d, a, x[i], 20, -373897302); + a = this.md5gg(a, b, c, d, x[i + 5], 5, -701558691); + d = this.md5gg(d, a, b, c, x[i + 10], 9, 38016083); + c = this.md5gg(c, d, a, b, x[i + 15], 14, -660478335); + b = this.md5gg(b, c, d, a, x[i + 4], 20, -405537848); + a = this.md5gg(a, b, c, d, x[i + 9], 5, 568446438); + d = this.md5gg(d, a, b, c, x[i + 14], 9, -1019803690); + c = this.md5gg(c, d, a, b, x[i + 3], 14, -187363961); + b = this.md5gg(b, c, d, a, x[i + 8], 20, 1163531501); + a = this.md5gg(a, b, c, d, x[i + 13], 5, -1444681467); + d = this.md5gg(d, a, b, c, x[i + 2], 9, -51403784); + c = this.md5gg(c, d, a, b, x[i + 7], 14, 1735328473); + b = this.md5gg(b, c, d, a, x[i + 12], 20, -1926607734); + a = this.md5hh(a, b, c, d, x[i + 5], 4, -378558); + d = this.md5hh(d, a, b, c, x[i + 8], 11, -2022574463); + c = this.md5hh(c, d, a, b, x[i + 11], 16, 1839030562); + b = this.md5hh(b, c, d, a, x[i + 14], 23, -35309556); + a = this.md5hh(a, b, c, d, x[i + 1], 4, -1530992060); + d = this.md5hh(d, a, b, c, x[i + 4], 11, 1272893353); + c = this.md5hh(c, d, a, b, x[i + 7], 16, -155497632); + b = this.md5hh(b, c, d, a, x[i + 10], 23, -1094730640); + a = this.md5hh(a, b, c, d, x[i + 13], 4, 681279174); + d = this.md5hh(d, a, b, c, x[i], 11, -358537222); + c = this.md5hh(c, d, a, b, x[i + 3], 16, -722521979); + b = this.md5hh(b, c, d, a, x[i + 6], 23, 76029189); + a = this.md5hh(a, b, c, d, x[i + 9], 4, -640364487); + d = this.md5hh(d, a, b, c, x[i + 12], 11, -421815835); + c = this.md5hh(c, d, a, b, x[i + 15], 16, 530742520); + b = this.md5hh(b, c, d, a, x[i + 2], 23, -995338651); + a = this.md5ii(a, b, c, d, x[i], 6, -198630844); + d = this.md5ii(d, a, b, c, x[i + 7], 10, 1126891415); + c = this.md5ii(c, d, a, b, x[i + 14], 15, -1416354905); + b = this.md5ii(b, c, d, a, x[i + 5], 21, -57434055); + a = this.md5ii(a, b, c, d, x[i + 12], 6, 1700485571); + d = this.md5ii(d, a, b, c, x[i + 3], 10, -1894986606); + c = this.md5ii(c, d, a, b, x[i + 10], 15, -1051523); + b = this.md5ii(b, c, d, a, x[i + 1], 21, -2054922799); + a = this.md5ii(a, b, c, d, x[i + 8], 6, 1873313359); + d = this.md5ii(d, a, b, c, x[i + 15], 10, -30611744); + c = this.md5ii(c, d, a, b, x[i + 6], 15, -1560198380); + b = this.md5ii(b, c, d, a, x[i + 13], 21, 1309151649); + a = this.md5ii(a, b, c, d, x[i + 4], 6, -145523070); + d = this.md5ii(d, a, b, c, x[i + 11], 10, -1120210379); + c = this.md5ii(c, d, a, b, x[i + 2], 15, 718787259); + b = this.md5ii(b, c, d, a, x[i + 9], 21, -343485551); + a = this.safeAdd(a, olda); + b = this.safeAdd(b, oldb); + c = this.safeAdd(c, oldc); + d = this.safeAdd(d, oldd) + } + return [a, b, c, d] + }, + binl2rstr(input) { + var i; + var output = ''; + var length32 = input.length * 32; + for (i = 0; i < length32; i += 8) { + output += String.fromCharCode((input[i >> 5] >>> (i % 32)) & 0xff) + } + return output + }, + rstr2binl(input) { + var i; + var output = []; + output[(input.length >> 2) - 1] = undefined; + for (i = 0; i < output.length; i += 1) { + output[i] = 0 + } + var length8 = input.length * 8; + for (i = 0; i < length8; i += 8) { + output[i >> 5] |= (input.charCodeAt(i / 8) & 0xff) << (i % 32) + } + return output + }, + rstrMD5(s) { + return this.binl2rstr(this.binlMD5(this.rstr2binl(s), s.length * 8)) + }, + rstrHMACMD5(key, data) { + var i; + var bkey = this.rstr2binl(key); + var ipad = []; + var opad = []; + var hash; + ipad[15] = opad[15] = undefined; + if (bkey.length > 16) { + bkey = this.binlMD5(bkey, key.length * 8) + } + for (i = 0; i < 16; i += 1) { + ipad[i] = bkey[i] ^ 0x36363636; + opad[i] = bkey[i] ^ 0x5c5c5c5c + } + hash = this.binlMD5(ipad.concat(this.rstr2binl(data)), 512 + data.length * 8); + return this.binl2rstr(this.binlMD5(opad.concat(hash), 512 + 128)) + }, + rstr2hex(input) { + var hexTab = '0123456789abcdef'; + var output = ''; + var x; + var i; + for (i = 0; i < input.length; i += 1) { + x = input.charCodeAt(i); + output += hexTab.charAt((x >>> 4) & 0x0f) + hexTab.charAt(x & 0x0f) + } + return output + }, + str2rstrUTF8(input) { + return unescape(encodeURIComponent(input)) + }, + rawMD5(s) { + return this.rstrMD5(this.str2rstrUTF8(s)) + }, + hexMD5(s) { + return this.rstr2hex(this.rawMD5(s)) + }, + rawHMACMD5(k, d) { + return this.rstrHMACMD5(this.str2rstrUTF8(k), str2rstrUTF8(d)) + }, + hexHMACMD5(k, d) { + return this.rstr2hex(this.rawHMACMD5(k, d)) + }, + md5(string, key, raw) { + if (!key) { + if (!raw) { + return this.hexMD5(string) + } + return this.rawMD5(string) + } + if (!raw) { + return this.hexHMACMD5(key, string) + } + return this.rawHMACMD5(key, string) + }, + getSig(requestParam, sk, feature, mode) { + var sig = null; + var requestArr = []; + Object.keys(requestParam).sort().forEach(function (key) { + requestArr.push(key + '=' + requestParam[key]) + }); + if (feature == 'search') { + sig = '/ws/place/v1/search?' + requestArr.join('&') + sk + } + if (feature == 'suggest') { + sig = '/ws/place/v1/suggestion?' + requestArr.join('&') + sk + } + if (feature == 'reverseGeocoder') { + sig = '/ws/geocoder/v1/?' + requestArr.join('&') + sk + } + if (feature == 'geocoder') { + sig = '/ws/geocoder/v1/?' + requestArr.join('&') + sk + } + if (feature == 'getCityList') { + sig = '/ws/district/v1/list?' + requestArr.join('&') + sk + } + if (feature == 'getDistrictByCityId') { + sig = '/ws/district/v1/getchildren?' + requestArr.join('&') + sk + } + if (feature == 'calculateDistance') { + sig = '/ws/distance/v1/?' + requestArr.join('&') + sk + } + if (feature == 'direction') { + sig = '/ws/direction/v1/' + mode + '?' + requestArr.join('&') + sk + } + sig = this.md5(sig); + return sig + }, + location2query(data) { + if (typeof data == 'string') { + return data + } + var query = ''; + for (var i = 0; i < data.length; i++) { + var d = data[i]; + if (!!query) { + query += ';' + } + if (d.location) { + query = query + d.location.lat + ',' + d.location.lng + } + if (d.latitude && d.longitude) { + query = query + d.latitude + ',' + d.longitude + } + } + return query + }, + rad(d) { + return d * Math.PI / 180.0 + }, + getEndLocation(location) { + var to = location.split(';'); + var endLocation = []; + for (var i = 0; i < to.length; i++) { + endLocation.push({ + lat: parseFloat(to[i].split(',')[0]), + lng: parseFloat(to[i].split(',')[1]) + }) + } + return endLocation + }, + getDistance(latFrom, lngFrom, latTo, lngTo) { + var radLatFrom = this.rad(latFrom); + var radLatTo = this.rad(latTo); + var a = radLatFrom - radLatTo; + var b = this.rad(lngFrom) - this.rad(lngTo); + var distance = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLatFrom) * Math.cos(radLatTo) * Math.pow(Math.sin(b / 2), 2))); + distance = distance * EARTH_RADIUS; + distance = Math.round(distance * 10000) / 10000; + return parseFloat(distance.toFixed(0)) + }, + getWXLocation(success, fail, complete) { + wx.getLocation({ + type: 'gcj02', + success: success, + fail: fail, + complete: complete + }) + }, + getLocationParam(location) { + if (typeof location == 'string') { + var locationArr = location.split(','); + if (locationArr.length === 2) { + location = { + latitude: location.split(',')[0], + longitude: location.split(',')[1] + } + } else { + location = {} + } + } + return location + }, + polyfillParam(param) { + param.success = param.success || function () {}; + param.fail = param.fail || function () {}; + param.complete = param.complete || function () {} + }, + checkParamKeyEmpty(param, key) { + if (!param[key]) { + var errconf = this.buildErrorConfig(ERROR_CONF.PARAM_ERR, ERROR_CONF.PARAM_ERR_MSG + key + '参数格式有误'); + param.fail(errconf); + param.complete(errconf); + return true + } + return false + }, + checkKeyword(param) { + return !this.checkParamKeyEmpty(param, 'keyword') + }, + checkLocation(param) { + var location = this.getLocationParam(param.location); + if (!location || !location.latitude || !location.longitude) { + var errconf = this.buildErrorConfig(ERROR_CONF.PARAM_ERR, ERROR_CONF.PARAM_ERR_MSG + ' location参数格式有误'); + param.fail(errconf); + param.complete(errconf); + return false + } + return true + }, + buildErrorConfig(errCode, errMsg) { + return { + status: errCode, + message: errMsg + } + }, + handleData(param, data, feature) { + if (feature == 'search') { + var searchResult = data.data; + var searchSimplify = []; + for (var i = 0; i < searchResult.length; i++) { + searchSimplify.push({ + id: searchResult[i].id || null, + title: searchResult[i].title || null, + latitude: searchResult[i].location && searchResult[i].location.lat || null, + longitude: searchResult[i].location && searchResult[i].location.lng || null, + address: searchResult[i].address || null, + category: searchResult[i].category || null, + tel: searchResult[i].tel || null, + adcode: searchResult[i].ad_info && searchResult[i].ad_info.adcode || null, + city: searchResult[i].ad_info && searchResult[i].ad_info.city || null, + district: searchResult[i].ad_info && searchResult[i].ad_info.district || null, + province: searchResult[i].ad_info && searchResult[i].ad_info.province || null + }) + } + param.success(data, { + searchResult: searchResult, + searchSimplify: searchSimplify + }) + } else if (feature == 'suggest') { + var suggestResult = data.data; + var suggestSimplify = []; + for (var i = 0; i < suggestResult.length; i++) { + suggestSimplify.push({ + adcode: suggestResult[i].adcode || null, + address: suggestResult[i].address || null, + category: suggestResult[i].category || null, + city: suggestResult[i].city || null, + district: suggestResult[i].district || null, + id: suggestResult[i].id || null, + latitude: suggestResult[i].location && suggestResult[i].location.lat || null, + longitude: suggestResult[i].location && suggestResult[i].location.lng || null, + province: suggestResult[i].province || null, + title: suggestResult[i].title || null, + type: suggestResult[i].type || null + }) + } + param.success(data, { + suggestResult: suggestResult, + suggestSimplify: suggestSimplify + }) + } else if (feature == 'reverseGeocoder') { + var reverseGeocoderResult = data.result; + var reverseGeocoderSimplify = { + address: reverseGeocoderResult.address || null, + latitude: reverseGeocoderResult.location && reverseGeocoderResult.location.lat || null, + longitude: reverseGeocoderResult.location && reverseGeocoderResult.location.lng || null, + adcode: reverseGeocoderResult.ad_info && reverseGeocoderResult.ad_info.adcode || null, + city: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component.city || null, + district: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component.district || null, + nation: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component.nation || null, + province: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component.province || null, + street: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component.street || null, + street_number: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component.street_number || null, + recommend: reverseGeocoderResult.formatted_addresses && reverseGeocoderResult.formatted_addresses.recommend || null, + rough: reverseGeocoderResult.formatted_addresses && reverseGeocoderResult.formatted_addresses.rough || null + }; + if (reverseGeocoderResult.pois) { + var pois = reverseGeocoderResult.pois; + var poisSimplify = []; + for (var i = 0; i < pois.length; i++) { + poisSimplify.push({ + id: pois[i].id || null, + title: pois[i].title || null, + latitude: pois[i].location && pois[i].location.lat || null, + longitude: pois[i].location && pois[i].location.lng || null, + address: pois[i].address || null, + category: pois[i].category || null, + adcode: pois[i].ad_info && pois[i].ad_info.adcode || null, + city: pois[i].ad_info && pois[i].ad_info.city || null, + district: pois[i].ad_info && pois[i].ad_info.district || null, + province: pois[i].ad_info && pois[i].ad_info.province || null + }) + } + param.success(data, { + reverseGeocoderResult: reverseGeocoderResult, + reverseGeocoderSimplify: reverseGeocoderSimplify, + pois: pois, + poisSimplify: poisSimplify + }) + } else { + param.success(data, { + reverseGeocoderResult: reverseGeocoderResult, + reverseGeocoderSimplify: reverseGeocoderSimplify + }) + } + } else if (feature == 'geocoder') { + var geocoderResult = data.result; + var geocoderSimplify = { + title: geocoderResult.title || null, + latitude: geocoderResult.location && geocoderResult.location.lat || null, + longitude: geocoderResult.location && geocoderResult.location.lng || null, + adcode: geocoderResult.ad_info && geocoderResult.ad_info.adcode || null, + province: geocoderResult.address_components && geocoderResult.address_components.province || null, + city: geocoderResult.address_components && geocoderResult.address_components.city || null, + district: geocoderResult.address_components && geocoderResult.address_components.district || null, + street: geocoderResult.address_components && geocoderResult.address_components.street || null, + street_number: geocoderResult.address_components && geocoderResult.address_components.street_number || null, + level: geocoderResult.level || null + }; + param.success(data, { + geocoderResult: geocoderResult, + geocoderSimplify: geocoderSimplify + }) + } else if (feature == 'getCityList') { + var provinceResult = data.result[0]; + var cityResult = data.result[1]; + var districtResult = data.result[2]; + param.success(data, { + provinceResult: provinceResult, + cityResult: cityResult, + districtResult: districtResult + }) + } else if (feature == 'getDistrictByCityId') { + var districtByCity = data.result[0]; + param.success(data, districtByCity) + } else if (feature == 'calculateDistance') { + var calculateDistanceResult = data.result.elements; + var distance = []; + for (var i = 0; i < calculateDistanceResult.length; i++) { + distance.push(calculateDistanceResult[i].distance) + } + param.success(data, { + calculateDistanceResult: calculateDistanceResult, + distance: distance + }) + } else if (feature == 'direction') { + var direction = data.result.routes; + param.success(data, direction) + } else { + param.success(data) + } + }, + buildWxRequestConfig(param, options, feature) { + var that = this; + options.header = { + "content-type": "application/json" + }; + options.method = 'GET'; + options.success = function (res) { + var data = res.data; + if (data.status === 0) { + that.handleData(param, data, feature) + } else { + param.fail(data) + } + }; + options.fail = function (res) { + res.statusCode = ERROR_CONF.WX_ERR_CODE; + param.fail(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg)) + }; + options.complete = function (res) { + var statusCode = +res.statusCode; + switch (statusCode) { + case ERROR_CONF.WX_ERR_CODE: { + param.complete(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg)); + break + } + case ERROR_CONF.WX_OK_CODE: { + var data = res.data; + if (data.status === 0) { + param.complete(data) + } else { + param.complete(that.buildErrorConfig(data.status, data.message)) + } + break + } + default: { + param.complete(that.buildErrorConfig(ERROR_CONF.SYSTEM_ERR, ERROR_CONF.SYSTEM_ERR_MSG)) + } + } + }; + return options + }, + locationProcess(param, locationsuccess, locationfail, locationcomplete) { + var that = this; + locationfail = locationfail || function (res) { + res.statusCode = ERROR_CONF.WX_ERR_CODE; + param.fail(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg)) + }; + locationcomplete = locationcomplete || function (res) { + if (res.statusCode == ERROR_CONF.WX_ERR_CODE) { + param.complete(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg)) + } + }; + if (!param.location) { + that.getWXLocation(locationsuccess, locationfail, locationcomplete) + } else if (that.checkLocation(param)) { + var location = Utils.getLocationParam(param.location); + locationsuccess(location) + } + } +}; +class QQMapWX { + constructor(options) { + if (!options.key) { + throw Error('key值不能为空') + } + this.key = options.key + }; + search(options) { + var that = this; + options = options || {}; + Utils.polyfillParam(options); + if (!Utils.checkKeyword(options)) { + return + } + var requestParam = { + keyword: options.keyword, + orderby: options.orderby || '_distance', + page_size: options.page_size || 10, + page_index: options.page_index || 1, + output: 'json', + key: that.key + }; + if (options.address_format) { + requestParam.address_format = options.address_format + } + if (options.filter) { + requestParam.filter = options.filter + } + var distance = options.distance || "1000"; + var auto_extend = options.auto_extend || 1; + var region = null; + var rectangle = null; + if (options.region) { + region = options.region + } + if (options.rectangle) { + rectangle = options.rectangle + } + var locationsuccess = function (result) { + if (region && !rectangle) { + requestParam.boundary = "region(" + region + "," + auto_extend + "," + result.latitude + "," + result.longitude + ")"; + if (options.sig) { + requestParam.sig = Utils.getSig(requestParam, options.sig, 'search') + } + } else if (rectangle && !region) { + requestParam.boundary = "rectangle(" + rectangle + ")"; + if (options.sig) { + requestParam.sig = Utils.getSig(requestParam, options.sig, 'search') + } + } else { + requestParam.boundary = "nearby(" + result.latitude + "," + result.longitude + "," + distance + "," + auto_extend + ")"; + if (options.sig) { + requestParam.sig = Utils.getSig(requestParam, options.sig, 'search') + } + } + wx.request(Utils.buildWxRequestConfig(options, { + url: URL_SEARCH, + data: requestParam + }, 'search')) + }; + Utils.locationProcess(options, locationsuccess) + }; + getSuggestion(options) { + var that = this; + options = options || {}; + Utils.polyfillParam(options); + if (!Utils.checkKeyword(options)) { + return + } + var requestParam = { + keyword: options.keyword, + region: options.region || '全国', + region_fix: options.region_fix || 0, + policy: options.policy || 0, + page_size: options.page_size || 10, + page_index: options.page_index || 1, + get_subpois: options.get_subpois || 0, + output: 'json', + key: that.key + }; + if (options.address_format) { + requestParam.address_format = options.address_format + } + if (options.filter) { + requestParam.filter = options.filter + } + if (options.location) { + var locationsuccess = function (result) { + requestParam.location = result.latitude + ',' + result.longitude; + if (options.sig) { + requestParam.sig = Utils.getSig(requestParam, options.sig, 'suggest') + } + wx.request(Utils.buildWxRequestConfig(options, { + url: URL_SUGGESTION, + data: requestParam + }, "suggest")) + }; + Utils.locationProcess(options, locationsuccess) + } else { + if (options.sig) { + requestParam.sig = Utils.getSig(requestParam, options.sig, 'suggest') + } + wx.request(Utils.buildWxRequestConfig(options, { + url: URL_SUGGESTION, + data: requestParam + }, "suggest")) + } + }; + reverseGeocoder(options) { + var that = this; + options = options || {}; + Utils.polyfillParam(options); + var requestParam = { + coord_type: options.coord_type || 5, + get_poi: options.get_poi || 0, + output: 'json', + key: that.key + }; + if (options.poi_options) { + requestParam.poi_options = options.poi_options + } + var locationsuccess = function (result) { + requestParam.location = result.latitude + ',' + result.longitude; + if (options.sig) { + requestParam.sig = Utils.getSig(requestParam, options.sig, 'reverseGeocoder') + } + wx.request(Utils.buildWxRequestConfig(options, { + url: URL_GET_GEOCODER, + data: requestParam + }, 'reverseGeocoder')) + }; + Utils.locationProcess(options, locationsuccess) + }; + geocoder(options) { + var that = this; + options = options || {}; + Utils.polyfillParam(options); + if (Utils.checkParamKeyEmpty(options, 'address')) { + return + } + var requestParam = { + address: options.address, + output: 'json', + key: that.key + }; + if (options.region) { + requestParam.region = options.region + } + if (options.sig) { + requestParam.sig = Utils.getSig(requestParam, options.sig, 'geocoder') + } + wx.request(Utils.buildWxRequestConfig(options, { + url: URL_GET_GEOCODER, + data: requestParam + }, 'geocoder')) + }; + getCityList(options) { + var that = this; + options = options || {}; + Utils.polyfillParam(options); + var requestParam = { + output: 'json', + key: that.key + }; + if (options.sig) { + requestParam.sig = Utils.getSig(requestParam, options.sig, 'getCityList') + } + wx.request(Utils.buildWxRequestConfig(options, { + url: URL_CITY_LIST, + data: requestParam + }, 'getCityList')) + }; + getDistrictByCityId(options) { + var that = this; + options = options || {}; + Utils.polyfillParam(options); + if (Utils.checkParamKeyEmpty(options, 'id')) { + return + } + var requestParam = { + id: options.id || '', + output: 'json', + key: that.key + }; + if (options.sig) { + requestParam.sig = Utils.getSig(requestParam, options.sig, 'getDistrictByCityId') + } + wx.request(Utils.buildWxRequestConfig(options, { + url: URL_AREA_LIST, + data: requestParam + }, 'getDistrictByCityId')) + }; + calculateDistance(options) { + var that = this; + options = options || {}; + Utils.polyfillParam(options); + if (Utils.checkParamKeyEmpty(options, 'to')) { + return + } + var requestParam = { + mode: options.mode || 'walking', + to: Utils.location2query(options.to), + output: 'json', + key: that.key + }; + if (options.from) { + options.location = options.from + } + if (requestParam.mode == 'straight') { + var locationsuccess = function (result) { + var locationTo = Utils.getEndLocation(requestParam.to); + var data = { + message: "query ok", + result: { + elements: [] + }, + status: 0 + }; + for (var i = 0; i < locationTo.length; i++) { + data.result.elements.push({ + distance: Utils.getDistance(result.latitude, result.longitude, locationTo[i].lat, locationTo[i].lng), + duration: 0, + from: { + lat: result.latitude, + lng: result.longitude + }, + to: { + lat: locationTo[i].lat, + lng: locationTo[i].lng + } + }) + } + var calculateResult = data.result.elements; + var distanceResult = []; + for (var i = 0; i < calculateResult.length; i++) { + distanceResult.push(calculateResult[i].distance) + } + return options.success(data, { + calculateResult: calculateResult, + distanceResult: distanceResult + }) + }; + Utils.locationProcess(options, locationsuccess) + } else { + var locationsuccess = function (result) { + requestParam.from = result.latitude + ',' + result.longitude; + if (options.sig) { + requestParam.sig = Utils.getSig(requestParam, options.sig, 'calculateDistance') + } + wx.request(Utils.buildWxRequestConfig(options, { + url: URL_DISTANCE, + data: requestParam + }, 'calculateDistance')) + }; + Utils.locationProcess(options, locationsuccess) + } + }; + direction(options) { + var that = this; + options = options || {}; + Utils.polyfillParam(options); + if (Utils.checkParamKeyEmpty(options, 'to')) { + return + } + var requestParam = { + output: 'json', + key: that.key + }; + if (typeof options.to == 'string') { + requestParam.to = options.to + } else { + requestParam.to = options.to.latitude + ',' + options.to.longitude + } + var SET_URL_DIRECTION = null; + options.mode = options.mode || MODE.driving; + SET_URL_DIRECTION = URL_DIRECTION + options.mode; + if (options.from) { + options.location = options.from + } + if (options.mode == MODE.driving) { + if (options.from_poi) { + requestParam.from_poi = options.from_poi + } + if (options.heading) { + requestParam.heading = options.heading + } + if (options.speed) { + requestParam.speed = options.speed + } + if (options.accuracy) { + requestParam.accuracy = options.accuracy + } + if (options.road_type) { + requestParam.road_type = options.road_type + } + if (options.to_poi) { + requestParam.to_poi = options.to_poi + } + if (options.from_track) { + requestParam.from_track = options.from_track + } + if (options.waypoints) { + requestParam.waypoints = options.waypoints + } + if (options.policy) { + requestParam.policy = options.policy + } + if (options.plate_number) { + requestParam.plate_number = options.plate_number + } + } + if (options.mode == MODE.transit) { + if (options.departure_time) { + requestParam.departure_time = options.departure_time + } + if (options.policy) { + requestParam.policy = options.policy + } + } + var locationsuccess = function (result) { + requestParam.from = result.latitude + ',' + result.longitude; + if (options.sig) { + requestParam.sig = Utils.getSig(requestParam, options.sig, 'direction', options.mode) + } + wx.request(Utils.buildWxRequestConfig(options, { + url: SET_URL_DIRECTION, + data: requestParam + }, 'direction')) + }; + Utils.locationProcess(options, locationsuccess) + } +}; +module.exports = QQMapWX; \ No newline at end of file diff --git a/utils/qrcode.js b/utils/qrcode.js new file mode 100644 index 0000000..e4c994e --- /dev/null +++ b/utils/qrcode.js @@ -0,0 +1,778 @@ +var QR = (function () { + + // alignment pattern + var adelta = [ + 0, 11, 15, 19, 23, 27, 31, // force 1 pat + 16, 18, 20, 22, 24, 26, 28, 20, 22, 24, 24, 26, 28, 28, 22, 24, 24, + 26, 26, 28, 28, 24, 24, 26, 26, 26, 28, 28, 24, 26, 26, 26, 28, 28 + ]; + + // version block + var vpat = [ + 0xc94, 0x5bc, 0xa99, 0x4d3, 0xbf6, 0x762, 0x847, 0x60d, + 0x928, 0xb78, 0x45d, 0xa17, 0x532, 0x9a6, 0x683, 0x8c9, + 0x7ec, 0xec4, 0x1e1, 0xfab, 0x08e, 0xc1a, 0x33f, 0xd75, + 0x250, 0x9d5, 0x6f0, 0x8ba, 0x79f, 0xb0b, 0x42e, 0xa64, + 0x541, 0xc69 + ]; + + // final format bits with mask: level << 3 | mask + var fmtword = [ + 0x77c4, 0x72f3, 0x7daa, 0x789d, 0x662f, 0x6318, 0x6c41, 0x6976, //L + 0x5412, 0x5125, 0x5e7c, 0x5b4b, 0x45f9, 0x40ce, 0x4f97, 0x4aa0, //M + 0x355f, 0x3068, 0x3f31, 0x3a06, 0x24b4, 0x2183, 0x2eda, 0x2bed, //Q + 0x1689, 0x13be, 0x1ce7, 0x19d0, 0x0762, 0x0255, 0x0d0c, 0x083b //H + ]; + + // 4 per version: number of blocks 1,2; data width; ecc width + var eccblocks = [ + 1, 0, 19, 7, 1, 0, 16, 10, 1, 0, 13, 13, 1, 0, 9, 17, + 1, 0, 34, 10, 1, 0, 28, 16, 1, 0, 22, 22, 1, 0, 16, 28, + 1, 0, 55, 15, 1, 0, 44, 26, 2, 0, 17, 18, 2, 0, 13, 22, + 1, 0, 80, 20, 2, 0, 32, 18, 2, 0, 24, 26, 4, 0, 9, 16, + 1, 0, 108, 26, 2, 0, 43, 24, 2, 2, 15, 18, 2, 2, 11, 22, + 2, 0, 68, 18, 4, 0, 27, 16, 4, 0, 19, 24, 4, 0, 15, 28, + 2, 0, 78, 20, 4, 0, 31, 18, 2, 4, 14, 18, 4, 1, 13, 26, + 2, 0, 97, 24, 2, 2, 38, 22, 4, 2, 18, 22, 4, 2, 14, 26, + 2, 0, 116, 30, 3, 2, 36, 22, 4, 4, 16, 20, 4, 4, 12, 24, + 2, 2, 68, 18, 4, 1, 43, 26, 6, 2, 19, 24, 6, 2, 15, 28, + 4, 0, 81, 20, 1, 4, 50, 30, 4, 4, 22, 28, 3, 8, 12, 24, + 2, 2, 92, 24, 6, 2, 36, 22, 4, 6, 20, 26, 7, 4, 14, 28, + 4, 0, 107, 26, 8, 1, 37, 22, 8, 4, 20, 24, 12, 4, 11, 22, + 3, 1, 115, 30, 4, 5, 40, 24, 11, 5, 16, 20, 11, 5, 12, 24, + 5, 1, 87, 22, 5, 5, 41, 24, 5, 7, 24, 30, 11, 7, 12, 24, + 5, 1, 98, 24, 7, 3, 45, 28, 15, 2, 19, 24, 3, 13, 15, 30, + 1, 5, 107, 28, 10, 1, 46, 28, 1, 15, 22, 28, 2, 17, 14, 28, + 5, 1, 120, 30, 9, 4, 43, 26, 17, 1, 22, 28, 2, 19, 14, 28, + 3, 4, 113, 28, 3, 11, 44, 26, 17, 4, 21, 26, 9, 16, 13, 26, + 3, 5, 107, 28, 3, 13, 41, 26, 15, 5, 24, 30, 15, 10, 15, 28, + 4, 4, 116, 28, 17, 0, 42, 26, 17, 6, 22, 28, 19, 6, 16, 30, + 2, 7, 111, 28, 17, 0, 46, 28, 7, 16, 24, 30, 34, 0, 13, 24, + 4, 5, 121, 30, 4, 14, 47, 28, 11, 14, 24, 30, 16, 14, 15, 30, + 6, 4, 117, 30, 6, 14, 45, 28, 11, 16, 24, 30, 30, 2, 16, 30, + 8, 4, 106, 26, 8, 13, 47, 28, 7, 22, 24, 30, 22, 13, 15, 30, + 10, 2, 114, 28, 19, 4, 46, 28, 28, 6, 22, 28, 33, 4, 16, 30, + 8, 4, 122, 30, 22, 3, 45, 28, 8, 26, 23, 30, 12, 28, 15, 30, + 3, 10, 117, 30, 3, 23, 45, 28, 4, 31, 24, 30, 11, 31, 15, 30, + 7, 7, 116, 30, 21, 7, 45, 28, 1, 37, 23, 30, 19, 26, 15, 30, + 5, 10, 115, 30, 19, 10, 47, 28, 15, 25, 24, 30, 23, 25, 15, 30, + 13, 3, 115, 30, 2, 29, 46, 28, 42, 1, 24, 30, 23, 28, 15, 30, + 17, 0, 115, 30, 10, 23, 46, 28, 10, 35, 24, 30, 19, 35, 15, 30, + 17, 1, 115, 30, 14, 21, 46, 28, 29, 19, 24, 30, 11, 46, 15, 30, + 13, 6, 115, 30, 14, 23, 46, 28, 44, 7, 24, 30, 59, 1, 16, 30, + 12, 7, 121, 30, 12, 26, 47, 28, 39, 14, 24, 30, 22, 41, 15, 30, + 6, 14, 121, 30, 6, 34, 47, 28, 46, 10, 24, 30, 2, 64, 15, 30, + 17, 4, 122, 30, 29, 14, 46, 28, 49, 10, 24, 30, 24, 46, 15, 30, + 4, 18, 122, 30, 13, 32, 46, 28, 48, 14, 24, 30, 42, 32, 15, 30, + 20, 4, 117, 30, 40, 7, 47, 28, 43, 22, 24, 30, 10, 67, 15, 30, + 19, 6, 118, 30, 18, 31, 47, 28, 34, 34, 24, 30, 20, 61, 15, 30 + ]; + + // Galois field log table + var glog = [ + 0xff, 0x00, 0x01, 0x19, 0x02, 0x32, 0x1a, 0xc6, 0x03, 0xdf, 0x33, 0xee, 0x1b, 0x68, 0xc7, 0x4b, + 0x04, 0x64, 0xe0, 0x0e, 0x34, 0x8d, 0xef, 0x81, 0x1c, 0xc1, 0x69, 0xf8, 0xc8, 0x08, 0x4c, 0x71, + 0x05, 0x8a, 0x65, 0x2f, 0xe1, 0x24, 0x0f, 0x21, 0x35, 0x93, 0x8e, 0xda, 0xf0, 0x12, 0x82, 0x45, + 0x1d, 0xb5, 0xc2, 0x7d, 0x6a, 0x27, 0xf9, 0xb9, 0xc9, 0x9a, 0x09, 0x78, 0x4d, 0xe4, 0x72, 0xa6, + 0x06, 0xbf, 0x8b, 0x62, 0x66, 0xdd, 0x30, 0xfd, 0xe2, 0x98, 0x25, 0xb3, 0x10, 0x91, 0x22, 0x88, + 0x36, 0xd0, 0x94, 0xce, 0x8f, 0x96, 0xdb, 0xbd, 0xf1, 0xd2, 0x13, 0x5c, 0x83, 0x38, 0x46, 0x40, + 0x1e, 0x42, 0xb6, 0xa3, 0xc3, 0x48, 0x7e, 0x6e, 0x6b, 0x3a, 0x28, 0x54, 0xfa, 0x85, 0xba, 0x3d, + 0xca, 0x5e, 0x9b, 0x9f, 0x0a, 0x15, 0x79, 0x2b, 0x4e, 0xd4, 0xe5, 0xac, 0x73, 0xf3, 0xa7, 0x57, + 0x07, 0x70, 0xc0, 0xf7, 0x8c, 0x80, 0x63, 0x0d, 0x67, 0x4a, 0xde, 0xed, 0x31, 0xc5, 0xfe, 0x18, + 0xe3, 0xa5, 0x99, 0x77, 0x26, 0xb8, 0xb4, 0x7c, 0x11, 0x44, 0x92, 0xd9, 0x23, 0x20, 0x89, 0x2e, + 0x37, 0x3f, 0xd1, 0x5b, 0x95, 0xbc, 0xcf, 0xcd, 0x90, 0x87, 0x97, 0xb2, 0xdc, 0xfc, 0xbe, 0x61, + 0xf2, 0x56, 0xd3, 0xab, 0x14, 0x2a, 0x5d, 0x9e, 0x84, 0x3c, 0x39, 0x53, 0x47, 0x6d, 0x41, 0xa2, + 0x1f, 0x2d, 0x43, 0xd8, 0xb7, 0x7b, 0xa4, 0x76, 0xc4, 0x17, 0x49, 0xec, 0x7f, 0x0c, 0x6f, 0xf6, + 0x6c, 0xa1, 0x3b, 0x52, 0x29, 0x9d, 0x55, 0xaa, 0xfb, 0x60, 0x86, 0xb1, 0xbb, 0xcc, 0x3e, 0x5a, + 0xcb, 0x59, 0x5f, 0xb0, 0x9c, 0xa9, 0xa0, 0x51, 0x0b, 0xf5, 0x16, 0xeb, 0x7a, 0x75, 0x2c, 0xd7, + 0x4f, 0xae, 0xd5, 0xe9, 0xe6, 0xe7, 0xad, 0xe8, 0x74, 0xd6, 0xf4, 0xea, 0xa8, 0x50, 0x58, 0xaf + ]; + + // Galios field exponent table + var gexp = [ + 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1d, 0x3a, 0x74, 0xe8, 0xcd, 0x87, 0x13, 0x26, + 0x4c, 0x98, 0x2d, 0x5a, 0xb4, 0x75, 0xea, 0xc9, 0x8f, 0x03, 0x06, 0x0c, 0x18, 0x30, 0x60, 0xc0, + 0x9d, 0x27, 0x4e, 0x9c, 0x25, 0x4a, 0x94, 0x35, 0x6a, 0xd4, 0xb5, 0x77, 0xee, 0xc1, 0x9f, 0x23, + 0x46, 0x8c, 0x05, 0x0a, 0x14, 0x28, 0x50, 0xa0, 0x5d, 0xba, 0x69, 0xd2, 0xb9, 0x6f, 0xde, 0xa1, + 0x5f, 0xbe, 0x61, 0xc2, 0x99, 0x2f, 0x5e, 0xbc, 0x65, 0xca, 0x89, 0x0f, 0x1e, 0x3c, 0x78, 0xf0, + 0xfd, 0xe7, 0xd3, 0xbb, 0x6b, 0xd6, 0xb1, 0x7f, 0xfe, 0xe1, 0xdf, 0xa3, 0x5b, 0xb6, 0x71, 0xe2, + 0xd9, 0xaf, 0x43, 0x86, 0x11, 0x22, 0x44, 0x88, 0x0d, 0x1a, 0x34, 0x68, 0xd0, 0xbd, 0x67, 0xce, + 0x81, 0x1f, 0x3e, 0x7c, 0xf8, 0xed, 0xc7, 0x93, 0x3b, 0x76, 0xec, 0xc5, 0x97, 0x33, 0x66, 0xcc, + 0x85, 0x17, 0x2e, 0x5c, 0xb8, 0x6d, 0xda, 0xa9, 0x4f, 0x9e, 0x21, 0x42, 0x84, 0x15, 0x2a, 0x54, + 0xa8, 0x4d, 0x9a, 0x29, 0x52, 0xa4, 0x55, 0xaa, 0x49, 0x92, 0x39, 0x72, 0xe4, 0xd5, 0xb7, 0x73, + 0xe6, 0xd1, 0xbf, 0x63, 0xc6, 0x91, 0x3f, 0x7e, 0xfc, 0xe5, 0xd7, 0xb3, 0x7b, 0xf6, 0xf1, 0xff, + 0xe3, 0xdb, 0xab, 0x4b, 0x96, 0x31, 0x62, 0xc4, 0x95, 0x37, 0x6e, 0xdc, 0xa5, 0x57, 0xae, 0x41, + 0x82, 0x19, 0x32, 0x64, 0xc8, 0x8d, 0x07, 0x0e, 0x1c, 0x38, 0x70, 0xe0, 0xdd, 0xa7, 0x53, 0xa6, + 0x51, 0xa2, 0x59, 0xb2, 0x79, 0xf2, 0xf9, 0xef, 0xc3, 0x9b, 0x2b, 0x56, 0xac, 0x45, 0x8a, 0x09, + 0x12, 0x24, 0x48, 0x90, 0x3d, 0x7a, 0xf4, 0xf5, 0xf7, 0xf3, 0xfb, 0xeb, 0xcb, 0x8b, 0x0b, 0x16, + 0x2c, 0x58, 0xb0, 0x7d, 0xfa, 0xe9, 0xcf, 0x83, 0x1b, 0x36, 0x6c, 0xd8, 0xad, 0x47, 0x8e, 0x00 + ]; + + // Working buffers: + // data input and ecc append, image working buffer, fixed part of image, run lengths for badness + var strinbuf=[], eccbuf=[], qrframe=[], framask=[], rlens=[]; + // Control values - width is based on version, last 4 are from table. + var version, width, neccblk1, neccblk2, datablkw, eccblkwid; + var ecclevel = 2; + // set bit to indicate cell in qrframe is immutable. symmetric around diagonal + function setmask(x, y) + { + var bt; + if (x > y) { + bt = x; + x = y; + y = bt; + } + // y*y = 1+3+5... + bt = y; + bt *= y; + bt += y; + bt >>= 1; + bt += x; + framask[bt] = 1; + } + + // enter alignment pattern - black to qrframe, white to mask (later black frame merged to mask) + function putalign(x, y) + { + var j; + + qrframe[x + width * y] = 1; + for (j = -2; j < 2; j++) { + qrframe[(x + j) + width * (y - 2)] = 1; + qrframe[(x - 2) + width * (y + j + 1)] = 1; + qrframe[(x + 2) + width * (y + j)] = 1; + qrframe[(x + j + 1) + width * (y + 2)] = 1; + } + for (j = 0; j < 2; j++) { + setmask(x - 1, y + j); + setmask(x + 1, y - j); + setmask(x - j, y - 1); + setmask(x + j, y + 1); + } + } + + //======================================================================== + // Reed Solomon error correction + // exponentiation mod N + function modnn(x) + { + while (x >= 255) { + x -= 255; + x = (x >> 8) + (x & 255); + } + return x; + } + + var genpoly = []; + + // Calculate and append ECC data to data block. Block is in strinbuf, indexes to buffers given. + function appendrs(data, dlen, ecbuf, eclen) + { + var i, j, fb; + + for (i = 0; i < eclen; i++) + strinbuf[ecbuf + i] = 0; + for (i = 0; i < dlen; i++) { + fb = glog[strinbuf[data + i] ^ strinbuf[ecbuf]]; + if (fb != 255) /* fb term is non-zero */ + for (j = 1; j < eclen; j++) + strinbuf[ecbuf + j - 1] = strinbuf[ecbuf + j] ^ gexp[modnn(fb + genpoly[eclen - j])]; + else + for( j = ecbuf ; j < ecbuf + eclen; j++ ) + strinbuf[j] = strinbuf[j + 1]; + strinbuf[ ecbuf + eclen - 1] = fb == 255 ? 0 : gexp[modnn(fb + genpoly[0])]; + } + } + + //======================================================================== + // Frame data insert following the path rules + + // check mask - since symmetrical use half. + function ismasked(x, y) + { + var bt; + if (x > y) { + bt = x; + x = y; + y = bt; + } + bt = y; + bt += y * y; + bt >>= 1; + bt += x; + return framask[bt]; + } + + //======================================================================== + // Apply the selected mask out of the 8. + function applymask(m) + { + var x, y, r3x, r3y; + + switch (m) { + case 0: + for (y = 0; y < width; y++) + for (x = 0; x < width; x++) + if (!((x + y) & 1) && !ismasked(x, y)) + qrframe[x + y * width] ^= 1; + break; + case 1: + for (y = 0; y < width; y++) + for (x = 0; x < width; x++) + if (!(y & 1) && !ismasked(x, y)) + qrframe[x + y * width] ^= 1; + break; + case 2: + for (y = 0; y < width; y++) + for (r3x = 0, x = 0; x < width; x++, r3x++) { + if (r3x == 3) + r3x = 0; + if (!r3x && !ismasked(x, y)) + qrframe[x + y * width] ^= 1; + } + break; + case 3: + for (r3y = 0, y = 0; y < width; y++, r3y++) { + if (r3y == 3) + r3y = 0; + for (r3x = r3y, x = 0; x < width; x++, r3x++) { + if (r3x == 3) + r3x = 0; + if (!r3x && !ismasked(x, y)) + qrframe[x + y * width] ^= 1; + } + } + break; + case 4: + for (y = 0; y < width; y++) + for (r3x = 0, r3y = ((y >> 1) & 1), x = 0; x < width; x++, r3x++) { + if (r3x == 3) { + r3x = 0; + r3y = !r3y; + } + if (!r3y && !ismasked(x, y)) + qrframe[x + y * width] ^= 1; + } + break; + case 5: + for (r3y = 0, y = 0; y < width; y++, r3y++) { + if (r3y == 3) + r3y = 0; + for (r3x = 0, x = 0; x < width; x++, r3x++) { + if (r3x == 3) + r3x = 0; + if (!((x & y & 1) + !(!r3x | !r3y)) && !ismasked(x, y)) + qrframe[x + y * width] ^= 1; + } + } + break; + case 6: + for (r3y = 0, y = 0; y < width; y++, r3y++) { + if (r3y == 3) + r3y = 0; + for (r3x = 0, x = 0; x < width; x++, r3x++) { + if (r3x == 3) + r3x = 0; + if (!(((x & y & 1) + (r3x && (r3x == r3y))) & 1) && !ismasked(x, y)) + qrframe[x + y * width] ^= 1; + } + } + break; + case 7: + for (r3y = 0, y = 0; y < width; y++, r3y++) { + if (r3y == 3) + r3y = 0; + for (r3x = 0, x = 0; x < width; x++, r3x++) { + if (r3x == 3) + r3x = 0; + if (!(((r3x && (r3x == r3y)) + ((x + y) & 1)) & 1) && !ismasked(x, y)) + qrframe[x + y * width] ^= 1; + } + } + break; + } + return; + } + + // Badness coefficients. + var N1 = 3, N2 = 3, N3 = 40, N4 = 10; + + // Using the table of the length of each run, calculate the amount of bad image + // - long runs or those that look like finders; called twice, once each for X and Y + function badruns(length) + { + var i; + var runsbad = 0; + for (i = 0; i <= length; i++) + if (rlens[i] >= 5) + runsbad += N1 + rlens[i] - 5; + // BwBBBwB as in finder + for (i = 3; i < length - 1; i += 2) + if (rlens[i - 2] == rlens[i + 2] + && rlens[i + 2] == rlens[i - 1] + && rlens[i - 1] == rlens[i + 1] + && rlens[i - 1] * 3 == rlens[i] + // white around the black pattern? Not part of spec + && (rlens[i - 3] == 0 // beginning + || i + 3 > length // end + || rlens[i - 3] * 3 >= rlens[i] * 4 || rlens[i + 3] * 3 >= rlens[i] * 4) + ) + runsbad += N3; + return runsbad; + } + + // Calculate how bad the masked image is - blocks, imbalance, runs, or finders. + function badcheck() + { + var x, y, h, b, b1; + var thisbad = 0; + var bw = 0; + + // blocks of same color. + for (y = 0; y < width - 1; y++) + for (x = 0; x < width - 1; x++) + if ((qrframe[x + width * y] && qrframe[(x + 1) + width * y] + && qrframe[x + width * (y + 1)] && qrframe[(x + 1) + width * (y + 1)]) // all black + || !(qrframe[x + width * y] || qrframe[(x + 1) + width * y] + || qrframe[x + width * (y + 1)] || qrframe[(x + 1) + width * (y + 1)])) // all white + thisbad += N2; + + // X runs + for (y = 0; y < width; y++) { + rlens[0] = 0; + for (h = b = x = 0; x < width; x++) { + if ((b1 = qrframe[x + width * y]) == b) + rlens[h]++; + else + rlens[++h] = 1; + b = b1; + bw += b ? 1 : -1; + } + thisbad += badruns(h); + } + + // black/white imbalance + if (bw < 0) + bw = -bw; + + var big = bw; + var count = 0; + big += big << 2; + big <<= 1; + while (big > width * width) + big -= width * width, count++; + thisbad += count * N4; + + // Y runs + for (x = 0; x < width; x++) { + rlens[0] = 0; + for (h = b = y = 0; y < width; y++) { + if ((b1 = qrframe[x + width * y]) == b) + rlens[h]++; + else + rlens[++h] = 1; + b = b1; + } + thisbad += badruns(h); + } + return thisbad; + } + + function genframe(instring) + { + var x, y, k, t, v, i, j, m; + + // find the smallest version that fits the string + t = instring.length; + version = 0; + do { + version++; + k = (ecclevel - 1) * 4 + (version - 1) * 16; + neccblk1 = eccblocks[k++]; + neccblk2 = eccblocks[k++]; + datablkw = eccblocks[k++]; + eccblkwid = eccblocks[k]; + k = datablkw * (neccblk1 + neccblk2) + neccblk2 - 3 + (version <= 9); + if (t <= k) + break; + } while (version < 40); + + // FIXME - insure that it fits insted of being truncated + width = 17 + 4 * version; + + // allocate, clear and setup data structures + v = datablkw + (datablkw + eccblkwid) * (neccblk1 + neccblk2) + neccblk2; + for( t = 0; t < v; t++ ) + eccbuf[t] = 0; + strinbuf = instring.slice(0); + + for( t = 0; t < width * width; t++ ) + qrframe[t] = 0; + + for( t = 0 ; t < (width * (width + 1) + 1) / 2; t++) + framask[t] = 0; + + // insert finders - black to frame, white to mask + for (t = 0; t < 3; t++) { + k = 0; + y = 0; + if (t == 1) + k = (width - 7); + if (t == 2) + y = (width - 7); + qrframe[(y + 3) + width * (k + 3)] = 1; + for (x = 0; x < 6; x++) { + qrframe[(y + x) + width * k] = 1; + qrframe[y + width * (k + x + 1)] = 1; + qrframe[(y + 6) + width * (k + x)] = 1; + qrframe[(y + x + 1) + width * (k + 6)] = 1; + } + for (x = 1; x < 5; x++) { + setmask(y + x, k + 1); + setmask(y + 1, k + x + 1); + setmask(y + 5, k + x); + setmask(y + x + 1, k + 5); + } + for (x = 2; x < 4; x++) { + qrframe[(y + x) + width * (k + 2)] = 1; + qrframe[(y + 2) + width * (k + x + 1)] = 1; + qrframe[(y + 4) + width * (k + x)] = 1; + qrframe[(y + x + 1) + width * (k + 4)] = 1; + } + } + + // alignment blocks + if (version > 1) { + t = adelta[version]; + y = width - 7; + for (;;) { + x = width - 7; + while (x > t - 3) { + putalign(x, y); + if (x < t) + break; + x -= t; + } + if (y <= t + 9) + break; + y -= t; + putalign(6, y); + putalign(y, 6); + } + } + + // single black + qrframe[8 + width * (width - 8)] = 1; + + // timing gap - mask only + for (y = 0; y < 7; y++) { + setmask(7, y); + setmask(width - 8, y); + setmask(7, y + width - 7); + } + for (x = 0; x < 8; x++) { + setmask(x, 7); + setmask(x + width - 8, 7); + setmask(x, width - 8); + } + + // reserve mask-format area + for (x = 0; x < 9; x++) + setmask(x, 8); + for (x = 0; x < 8; x++) { + setmask(x + width - 8, 8); + setmask(8, x); + } + for (y = 0; y < 7; y++) + setmask(8, y + width - 7); + + // timing row/col + for (x = 0; x < width - 14; x++) + if (x & 1) { + setmask(8 + x, 6); + setmask(6, 8 + x); + } + else { + qrframe[(8 + x) + width * 6] = 1; + qrframe[6 + width * (8 + x)] = 1; + } + + // version block + if (version > 6) { + t = vpat[version - 7]; + k = 17; + for (x = 0; x < 6; x++) + for (y = 0; y < 3; y++, k--) + if (1 & (k > 11 ? version >> (k - 12) : t >> k)) { + qrframe[(5 - x) + width * (2 - y + width - 11)] = 1; + qrframe[(2 - y + width - 11) + width * (5 - x)] = 1; + } + else { + setmask(5 - x, 2 - y + width - 11); + setmask(2 - y + width - 11, 5 - x); + } + } + + // sync mask bits - only set above for white spaces, so add in black bits + for (y = 0; y < width; y++) + for (x = 0; x <= y; x++) + if (qrframe[x + width * y]) + setmask(x, y); + + // convert string to bitstream + // 8 bit data to QR-coded 8 bit data (numeric or alphanum, or kanji not supported) + v = strinbuf.length; + + // string to array + for( i = 0 ; i < v; i++ ) + eccbuf[i] = strinbuf.charCodeAt(i); + strinbuf = eccbuf.slice(0); + + // calculate max string length + x = datablkw * (neccblk1 + neccblk2) + neccblk2; + if (v >= x - 2) { + v = x - 2; + if (version > 9) + v--; + } + + // shift and repack to insert length prefix + i = v; + if (version > 9) { + strinbuf[i + 2] = 0; + strinbuf[i + 3] = 0; + while (i--) { + t = strinbuf[i]; + strinbuf[i + 3] |= 255 & (t << 4); + strinbuf[i + 2] = t >> 4; + } + strinbuf[2] |= 255 & (v << 4); + strinbuf[1] = v >> 4; + strinbuf[0] = 0x40 | (v >> 12); + } + else { + strinbuf[i + 1] = 0; + strinbuf[i + 2] = 0; + while (i--) { + t = strinbuf[i]; + strinbuf[i + 2] |= 255 & (t << 4); + strinbuf[i + 1] = t >> 4; + } + strinbuf[1] |= 255 & (v << 4); + strinbuf[0] = 0x40 | (v >> 4); + } + // fill to end with pad pattern + i = v + 3 - (version < 10); + while (i < x) { + strinbuf[i++] = 0xec; + // buffer has room if (i == x) break; + strinbuf[i++] = 0x11; + } + + // calculate and append ECC + + // calculate generator polynomial + genpoly[0] = 1; + for (i = 0; i < eccblkwid; i++) { + genpoly[i + 1] = 1; + for (j = i; j > 0; j--) + genpoly[j] = genpoly[j] + ? genpoly[j - 1] ^ gexp[modnn(glog[genpoly[j]] + i)] : genpoly[j - 1]; + genpoly[0] = gexp[modnn(glog[genpoly[0]] + i)]; + } + for (i = 0; i <= eccblkwid; i++) + genpoly[i] = glog[genpoly[i]]; // use logs for genpoly[] to save calc step + + // append ecc to data buffer + k = x; + y = 0; + for (i = 0; i < neccblk1; i++) { + appendrs(y, datablkw, k, eccblkwid); + y += datablkw; + k += eccblkwid; + } + for (i = 0; i < neccblk2; i++) { + appendrs(y, datablkw + 1, k, eccblkwid); + y += datablkw + 1; + k += eccblkwid; + } + // interleave blocks + y = 0; + for (i = 0; i < datablkw; i++) { + for (j = 0; j < neccblk1; j++) + eccbuf[y++] = strinbuf[i + j * datablkw]; + for (j = 0; j < neccblk2; j++) + eccbuf[y++] = strinbuf[(neccblk1 * datablkw) + i + (j * (datablkw + 1))]; + } + for (j = 0; j < neccblk2; j++) + eccbuf[y++] = strinbuf[(neccblk1 * datablkw) + i + (j * (datablkw + 1))]; + for (i = 0; i < eccblkwid; i++) + for (j = 0; j < neccblk1 + neccblk2; j++) + eccbuf[y++] = strinbuf[x + i + j * eccblkwid]; + strinbuf = eccbuf; + + // pack bits into frame avoiding masked area. + x = y = width - 1; + k = v = 1; // up, minus + /* inteleaved data and ecc codes */ + m = (datablkw + eccblkwid) * (neccblk1 + neccblk2) + neccblk2; + for (i = 0; i < m; i++) { + t = strinbuf[i]; + for (j = 0; j < 8; j++, t <<= 1) { + if (0x80 & t) + qrframe[x + width * y] = 1; + do { // find next fill position + if (v) + x--; + else { + x++; + if (k) { + if (y != 0) + y--; + else { + x -= 2; + k = !k; + if (x == 6) { + x--; + y = 9; + } + } + } + else { + if (y != width - 1) + y++; + else { + x -= 2; + k = !k; + if (x == 6) { + x--; + y -= 8; + } + } + } + } + v = !v; + } while (ismasked(x, y)); + } + } + + // save pre-mask copy of frame + strinbuf = qrframe.slice(0); + t = 0; // best + y = 30000; // demerit + // for instead of while since in original arduino code + // if an early mask was "good enough" it wouldn't try for a better one + // since they get more complex and take longer. + for (k = 0; k < 8; k++) { + applymask(k); // returns black-white imbalance + x = badcheck(); + if (x < y) { // current mask better than previous best? + y = x; + t = k; + } + if (t == 7) + break; // don't increment i to a void redoing mask + qrframe = strinbuf.slice(0); // reset for next pass + } + if (t != k) // redo best mask - none good enough, last wasn't t + applymask(t); + + // add in final mask/ecclevel bytes + y = fmtword[t + ((ecclevel - 1) << 3)]; + // low byte + for (k = 0; k < 8; k++, y >>= 1) + if (y & 1) { + qrframe[(width - 1 - k) + width * 8] = 1; + if (k < 6) + qrframe[8 + width * k] = 1; + else + qrframe[8 + width * (k + 1)] = 1; + } + // high byte + for (k = 0; k < 7; k++, y >>= 1) + if (y & 1) { + qrframe[8 + width * (width - 7 + k)] = 1; + if (k) + qrframe[(6 - k) + width * 8] = 1; + else + qrframe[7 + width * 8] = 1; + } + + // return image + return qrframe; + } + + var _canvas = null, + _size = null; + + var api = { + + get ecclevel () { + return ecclevel; + }, + + set ecclevel (val) { + ecclevel = val; + }, + + get size () { + return _size; + }, + + set size (val) { + _size = val + }, + + get canvas () { + return _canvas; + }, + + set canvas (el) { + _canvas = el; + }, + + getFrame: function (string) { + return genframe(string); + }, + + draw: function (string, canvas, size, ecc) { + + ecclevel = ecc || ecclevel; + canvas = canvas || _canvas; + + if (!canvas) { + console.warn('No canvas provided to draw QR code in!') + return; + } + + size = size || _size || Math.min(canvas.width, canvas.height); + + var frame = genframe(string), + ctx = canvas.ctx, + px = Math.round(size / (width + 8)); + + var roundedSize = px * (width + 8), + offset = Math.floor((size - roundedSize) / 2); + + size = roundedSize; + + ctx.clearRect(0, 0, canvas.width, canvas.height); + ctx.setFillStyle('#000000'); + + for (var i = 0; i < width; i++) { + for (var j = 0; j < width; j++) { + if (frame[j * width + i]) { + ctx.fillRect(px * (4 + i) + offset, px * (4 + j) + offset, px, px); + } + } + } + ctx.draw(); + } + } + + module.exports = { + api: api + } + +})() \ No newline at end of file diff --git a/utils/util.js b/utils/util.js new file mode 100644 index 0000000..befe9fe --- /dev/null +++ b/utils/util.js @@ -0,0 +1,91 @@ +const formatTime = date => { + const year = date.getFullYear() + const month = date.getMonth() + 1 + const day = date.getDate() + const hour = date.getHours() + const minute = date.getMinutes() + const second = date.getSeconds() + + return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':') +} + +const formatNumber = n => { + n = n.toString() + return n[1] ? n : '0' + n +} + +function formatTimeV(date, fmt) { + var o = { + "M+": date.getMonth() + 1, // 月份 + "d+": date.getDate(), // 日 + "h+": date.getHours(), // 小时 + "m+": date.getMinutes(), // 分 + "s+": date.getSeconds(), // 秒 + "q+": Math.floor((date.getMonth() + 3) / 3), // 季度 + "S": date.getMilliseconds() + // 毫秒 + }; + if (/(y+)/.test(fmt)) + fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length)); + for (var k in o) + if (new RegExp("(" + k + ")").test(fmt)) + fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); + return fmt; +} + +function setNavTitle4Color(title, color,bgColor) { + var bc = '#000000'; + if (bgColor){ + bc = bgColor; + } + wx.setNavigationBarColor({ + frontColor: bc, + backgroundColor: color, + }) + wx.setNavigationBarTitle({ + title: title + }) +} + +function showFailureMsg(error) { + var title = "系统繁忙!!!"; + if (error.errMessage) { + title = error.errMessage + } else + if (error.message) { + title = error.message + } + wx.showToast({ + title: title, + icon: "none" + }) +} + +function showSuccessMsg(msg) { + var title = "操作成功"; + if (msg){ + title = msg; + } + wx.showToast({ + title: title + }) +} +function showWaring(msg) { + var title = "警告"; + if (msg) { + title = msg; + } + wx.showToast({ + title: title, + icon: "none" + }) +} + +module.exports = { + formatTime: formatTime, + formatTimeV: formatTimeV, + setNavTitle4Color: setNavTitle4Color, + showFailureMsg: showFailureMsg, + showSuccessMsg: showSuccessMsg, + showWaring: showWaring +} \ No newline at end of file diff --git a/utils/utils.js b/utils/utils.js new file mode 100644 index 0000000..733dafb --- /dev/null +++ b/utils/utils.js @@ -0,0 +1,438 @@ +const formatTime = date => { + const year = date.getFullYear() + const month = date.getMonth() + 1 + const day = date.getDate() + const hour = date.getHours() + const minute = date.getMinutes() + const second = date.getSeconds() + + return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':') +} + +const formatNumber = n => { + n = n.toString() + return n[1] ? n : '0' + n +} + +/** + * 获取今天星期 + */ +function getWeeker() { + var a = ["日", "一", "二", "三", "四", "五", "六"]; + var week = new Date().getDay(); + return "星期" + a[week]; + +} + +/** + * 比较两个日期的大小 + */ +function compareDate(d1, d2) { + var date = new Date(d1); + var tempDate = new Date(d2); + return date.getTime() > tempDate.getTime(); +} + +/*** + * data 时间 + * + * sign 标识(0,1,2) + * 0:yyyy/MM/dd HH:mm:ss:ms + * 1:yyyy-MM-dd HH:mm:ss:ms(default) + * 2:yyyyMMddHHmmss + * 3:yyyy-MM-dd + */ +function getFormatTime(date, sign) { + if (undefined == sign || null == sign || sign == "" || sign > 5) { + sign = 0; + } + var year = date.getFullYear() + var month = date.getMonth() + 1 + var day = date.getDate() + var hour = date.getHours() + var minute = date.getMinutes() + var second = date.getSeconds() + var milliseconds = date.getMilliseconds() + var value = ""; + switch (sign) { + case 0: + value = [hour, minute].map(formatNumber).join(':'); + break; + case 1: + value = [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':'); + break; + case 2: + value = [year, month, day].map(formatNumber).join('') + [hour, minute, second].map(formatNumber).join(''); + break; + case 3: + value = [year, month, day].map(formatNumber).join('-'); + break; + case 4: + value = [year, month].map(formatNumber).join('-') + "-01"; + break; + case 5: + value = [hour, minute, second].map(formatNumber).join(':'); + break; + } + return value; +} + + +// 对象转string +const parseToString = function(options) { + var content = ""; + var keys = Object.keys(options); + for (var i = 0; i < keys.length; i++) { + var key = keys[i]; + var value = ""; + if (undefined != options[key]) { + value = options[key].toString(); + } + if (content.length > 0) { + content += "&" + encodeURI(key, "utf-8") + "=" + encodeURI(value, "utf-8"); + } else { + content += encodeURI(key, "utf-8") + "=" + encodeURI(value, "utf-8"); + + } + } + return content; +} + +/**校验不为空 */ +const isNotBlank = function(value) { + if (null != value && undefined != value && JSON.stringify(value) != "") { + return true; + } else { + return false; + } +}; + +/**校验为空 */ +const isBlank = function(value) { + if (null == value || undefined == value || value == "" || JSON.stringify(value) == "") { + return true; + } else { + return false; + } +}; + +// 替换全部 +function replaceAll(source, oldStr, newStr) { + while (source.indexOf(oldStr) >= 0) { + source = source.replace(oldStr, newStr); + } + return source; +} + +/** + * 加法运算,避免数据相加小数点后产生多位数和计算精度损失。 + * + * @param num1加数1 | + * num2加数2 + */ +var numAdd = function(num1, num2, decimal) { + var baseNum, baseNum1, baseNum2; + try { + baseNum1 = num1.toString().split(".")[1].length; + } catch (e) { + baseNum1 = 0; + } + try { + baseNum2 = num2.toString().split(".")[1].length; + } catch (e) { + baseNum2 = 0; + } + baseNum = Math.pow(10, Math.max(baseNum1, baseNum2)); + var value = (num1 * baseNum + num2 * baseNum) / baseNum + if (undefined == decimal || null == decimal || "" == decimal) { + return value; + } else { + if (isNaN(decimal)) { + decimal = 2 + } + return value.toFixed(2); + } +}; +/** + * 减法运算,避免数据相减小数点后产生多位数和计算精度损失。 + * + * @param num1被减数 | + * num2减数 + */ +var numSub = function(num1, num2, decimal) { + var baseNum, baseNum1, baseNum2; + var precision; /*** 精度***/ + try { + baseNum1 = num1.toString().split(".")[1].length; + } catch (e) { + baseNum1 = 0; + } + try { + baseNum2 = num2.toString().split(".")[1].length; + } catch (e) { + baseNum2 = 0; + } + baseNum = Math.pow(10, Math.max(baseNum1, baseNum2)); + precision = (baseNum1 >= baseNum2) ? baseNum1 : baseNum2; + var value = Number(((num1 * baseNum - num2 * baseNum) / baseNum).toFixed(precision)); + if (undefined == decimal || null == decimal || "" == decimal) { + return value; + } else { + if (isNaN(decimal)) { + decimal = 2 + } + return value.toFixed(2); + } +}; +/** + * 乘法运算,避免数据相乘小数点后产生多位数和计算精度损失。 + * + * @param num1被乘数 | + * num2乘数 + */ +var numMulti = function(num1, num2, decimal) { + var baseNum = 0; + try { + baseNum += num1.toString().split(".")[1].length; + } catch (e) {} + try { + baseNum += num2.toString().split(".")[1].length; + } catch (e) {} + var value = Number(num1.toString().replace(".", "")) * + Number(num2.toString().replace(".", "")) / Math.pow(10, baseNum); + + if (undefined == decimal || null == decimal || "" == decimal) { + return value; + } else { + if (isNaN(decimal)) { + decimal = 2 + } + return value.toFixed(2); + } +}; +/** + * 除法运算,避免数据相除小数点后产生多位数和计算精度损失。 + * + * @param num1被除数 | + * num2除数 + */ +var numDiv = function(num1, num2, decimal) { + var baseNum1 = 0, + baseNum2 = 0; + var baseNum3, baseNum4; + try { + baseNum1 = num1.toString().split(".")[1].length; + } catch (e) { + baseNum1 = 0; + } + try { + baseNum2 = num2.toString().split(".")[1].length; + } catch (e) { + baseNum2 = 0; + } + + baseNum3 = Number(num1.toString().replace(".", "")); + baseNum4 = Number(num2.toString().replace(".", "")); + var value = (baseNum3 / baseNum4) * pow(10, baseNum2 - baseNum1); + + if (undefined == decimal || null == decimal || "" == decimal) { + return value; + } else { + if (isNaN(decimal)) { + decimal = 2 + } + return value.toFixed(2); + } +}; + +// 仿js each循环 +var each = function(object, callback, args) { + var name, i = 0, + length = object.length, + isObj = length === undefined || typeof object == 'function'; + + if (args) { + if (isObj) { + for (name in object) { + if (callback.apply(object[name], args) === false) { + break; + } + } + } else { + for (; i < length;) { + if (callback.apply(object[i++], args) === false) { + break; + } + } + } + + // A special, fast, case for the most common use of each + } else { + if (isObj) { + for (name in object) { + if (callback.call(object[name], name, object[name]) === false) { + break; + } + } + } else { + for (; i < length;) { + if (callback.call(object[i], i, object[i++]) === false) { + break; + } + } + } + } + + return object; +}; + + +// 显示繁忙提示 +var showBusy = text => wx.showToast({ + title: text, + icon: 'loading', + duration: 10000 +}) + +// 显示成功提示 +var showSuccess = text => wx.showToast({ + title: text, + icon: 'success' +}) + +function alertErrorMsg(title, content) { + wx.showModal({ + title: title, + content: content, + showCancel: false + }); +} + +// 显示失败提示 +var showModel = (title, content) => { + wx.hideToast(); + wx.showModal({ + title, + content: JSON.stringify(content), + showCancel: false + }) +} + +// 根据下标删除对应的元素 +Array.prototype.remove = function(dx) { + if (isNaN(dx) || dx > this.length) { + return false; + } + + for (var i = 0, n = 0; i < this.length; i++) { + if (this[i] != this[dx]) { + this[n++] = this[i] + } + } + this.length -= 1 +} + +// +var deviceInfo = function() { + var model = "iPhone X"; + var result = false; + wx.getSystemInfo({ + success: function(res) { + console.log(res); + if (res.errMsg == "getSystemInfo:ok") { + console.log(model.indexOf(res.model)); + if (res.model.indexOf(model) != -1) { + result = true; + } + wx.setStorageSync("model", result); + } + }, + }); +} + +var getZero = function(num) { + return parseFloat(num).toFixed(0); +} + +var getOne = function(num) { + return parseFloat(num).toFixed(1); +} + +var getTwo = function(num) { + return parseFloat(num).toFixed(2); +} + +var isOpen = function(dateTime, time) { + var result = false; + if (!time) { + return true; + } + + var tempTimes = time.split(","); + for (var item of tempTimes) { + var temps = item.split("-"); + + var startTime = temps[0].split(":"); + var time1 = startTime[0] * 3600 + startTime[1] * 60 + startTime[2]; + + var endTime = temps[1].split(":"); + var time2 = endTime[0] * 3600 + endTime[1] * 60 + endTime[2]; + + var nowTime = dateTime.split(":"); + var time3 = nowTime[0] * 3600 + nowTime[1] * 60 + nowTime[2]; + + if (time1 < time3 && time3 < time2) { + result = true; + break; + } + } + return result; +} +var getDistance = function(lat1, lng1, lat2, lng2) { + lat1 = lat1 || 0; + lng1 = lng1 || 0; + lat2 = lat2 || 0; + lng2 = lng2 || 0; + + var rad1 = lat1 * Math.PI / 180.0; + var rad2 = lat2 * Math.PI / 180.0; + var a = rad1 - rad2; + var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0; + + var r = 6378137; + var distance = r * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(b / 2), 2))); + + // if (distance > 1000){ + distance = Math.round(distance / 1000); + // } + + return distance; +} + + +module.exports = { + getWeeker: getWeeker, + compareDate: compareDate, + each: each, + numAdd: numAdd, + numSub: numSub, + numMulti: numMulti, + numDiv: numDiv, + formatTime: formatTime, + replaceAll: replaceAll, + parseToString: parseToString, + isBlank: isBlank, + isNotBlank: isNotBlank, + formatNumber: formatNumber, + showBusy: showBusy, + showSuccess: showSuccess, + showModel: showModel, + alertErrorMsg: alertErrorMsg, + deviceInfo: deviceInfo, + getFormatTime: getFormatTime, + getZero: getZero, + getOne: getOne, + getTwo: getTwo, + isOpen: isOpen, + getDistance: getDistance +} \ No newline at end of file diff --git a/utils/wcache.js b/utils/wcache.js new file mode 100644 index 0000000..f62df96 --- /dev/null +++ b/utils/wcache.js @@ -0,0 +1,49 @@ +var postfix = '_deadtime'; + +/**存储数据 并指定有效时间 */ +function put(k, v, t) { + // console.log(k); + wx.setStorageSync(k, v) + var seconds = parseInt(t); + if (seconds > 0) { + var timestamp = Date.parse(new Date()); + timestamp = timestamp / 1000 + seconds; + // console.log(timestamp); + wx.setStorageSync(k + postfix, timestamp + "") + } else { + wx.removeStorageSync(k + postfix) + } +} +/**获取数据 1:校验是否在有效期 2:true 返回指定keyvalue 否则返回 def*/ +function get(k, def) { + var deadtime = parseInt(wx.getStorageSync(k + postfix)) + if (deadtime) { + if (parseInt(deadtime) < Date.parse(new Date()) / 1000) { + if (def) { return def; } else { return; } + } + } + var res = wx.getStorageSync(k); + if (res) { + return res; + } else { + return def; + } +} + +/**删除指定key 以及指定key有效时间缓存 */ +function remove(k) { + wx.removeStorageSync(k); + wx.removeStorageSync(k + postfix); +} + +/**清空缓存 */ +function clear() { + wx.clearStorageSync(); +} + +module.exports = { + put: put, + get: get, + remove: remove, + clear: clear, +} diff --git a/utils/wxParse/html2json.js b/utils/wxParse/html2json.js new file mode 100644 index 0000000..69c535c --- /dev/null +++ b/utils/wxParse/html2json.js @@ -0,0 +1,308 @@ +/** + * html2Json 改造来自: https://github.com/Jxck/html2json + * + * + * author: Di (微信小程序开发工程师) + * organization: WeAppDev(微信小程序开发论坛)(http://weappdev.com) + * 垂直微信小程序开发交流社区 + * + * github地址: https://github.com/icindy/wxParse + * + * for: 微信小程序富文本解析 + * detail : http://weappdev.com/t/wxparse-alpha0-1-html-markdown/184 + */ + +var __placeImgeUrlHttps = "https"; +var __emojisReg = ''; +var __emojisBaseSrc = ''; +var __emojis = {}; +var wxDiscode = require('./wxDiscode.js'); +var HTMLParser = require('./htmlparser.js'); +// Empty Elements - HTML 5 +var empty = makeMap("area,base,basefont,br,col,frame,hr,img,input,link,meta,param,embed,command,keygen,source,track,wbr"); +// Block Elements - HTML 5 +var block = makeMap("br,a,code,address,article,applet,aside,audio,blockquote,button,canvas,center,dd,del,dir,div,dl,dt,fieldset,figcaption,figure,footer,form,frameset,h1,h2,h3,h4,h5,h6,header,hgroup,hr,iframe,ins,isindex,li,map,menu,noframes,noscript,object,ol,output,p,pre,section,script,table,tbody,td,tfoot,th,thead,tr,ul,video"); + +// Inline Elements - HTML 5 +var inline = makeMap("abbr,acronym,applet,b,basefont,bdo,big,button,cite,del,dfn,em,font,i,iframe,img,input,ins,kbd,label,map,object,q,s,samp,script,select,small,span,strike,strong,sub,sup,textarea,tt,u,var"); + +// Elements that you can, intentionally, leave open +// (and which close themselves) +var closeSelf = makeMap("colgroup,dd,dt,li,options,p,td,tfoot,th,thead,tr"); + +// Attributes that have their values filled in disabled="disabled" +var fillAttrs = makeMap("checked,compact,declare,defer,disabled,ismap,multiple,nohref,noresize,noshade,nowrap,readonly,selected"); + +// Special Elements (can contain anything) +var special = makeMap("wxxxcode-style,script,style,view,scroll-view,block"); +function makeMap(str) { + var obj = {}, items = str.split(","); + for (var i = 0; i < items.length; i++) + obj[items[i]] = true; + return obj; +} + +function q(v) { + return '"' + v + '"'; +} + +function removeDOCTYPE(html) { + return html + .replace(/<\?xml.*\?>\n/, '') + .replace(/<.*!doctype.*\>\n/, '') + .replace(/<.*!DOCTYPE.*\>\n/, ''); +} + +function trimHtml(html) { + return html + .replace(/\r?\n+/g, '') + .replace(//ig, '') + .replace(/\/\*.*?\*\//ig, '') + .replace(/[ ]+ + // add to parents + var parent = bufArray[0] || results; + if (parent.nodes === undefined) { + parent.nodes = []; + } + parent.nodes.push(node); + } else { + bufArray.unshift(node); + } + }, + end: function (tag) { + //debug(tag); + // merge into parent tag + var node = bufArray.shift(); + if (node.tag !== tag) console.error('invalid state: mismatch end tag'); + + //当有缓存source资源时于于video补上src资源 + if(node.tag === 'video' && results.source){ + node.attr.src = results.source; + delete results.source; + } + + if (bufArray.length === 0) { + results.nodes.push(node); + } else { + var parent = bufArray[0]; + if (parent.nodes === undefined) { + parent.nodes = []; + } + parent.nodes.push(node); + } + }, + chars: function (text) { + //debug(text); + var node = { + node: 'text', + text: text, + textArray:transEmojiStr(text) + }; + + if (bufArray.length === 0) { + node.index = index.toString() + index += 1 + results.nodes.push(node); + } else { + var parent = bufArray[0]; + if (parent.nodes === undefined) { + parent.nodes = []; + } + node.index = parent.index + '.' + parent.nodes.length + parent.nodes.push(node); + } + }, + comment: function (text) { + //debug(text); + // var node = { + // node: 'comment', + // text: text, + // }; + // var parent = bufArray[0]; + // if (parent.nodes === undefined) { + // parent.nodes = []; + // } + // parent.nodes.push(node); + }, + }); + return results; +}; + +function transEmojiStr(str){ + // var eReg = new RegExp("["+__reg+' '+"]"); +// str = str.replace(/\[([^\[\]]+)\]/g,':$1:') + + var emojiObjs = []; + //如果正则表达式为空 + if(__emojisReg.length == 0 || !__emojis){ + var emojiObj = {} + emojiObj.node = "text"; + emojiObj.text = str; + array = [emojiObj]; + return array; + } + //这个地方需要调整 + str = str.replace(/\[([^\[\]]+)\]/g,':$1:') + var eReg = new RegExp("[:]"); + var array = str.split(eReg); + for(var i = 0; i < array.length; i++){ + var ele = array[i]; + var emojiObj = {}; + if(__emojis[ele]){ + emojiObj.node = "element"; + emojiObj.tag = "emoji"; + emojiObj.text = __emojis[ele]; + emojiObj.baseSrc= __emojisBaseSrc; + }else{ + emojiObj.node = "text"; + emojiObj.text = ele; + } + emojiObjs.push(emojiObj); + } + + return emojiObjs; +} + +function emojisInit(reg='',baseSrc="/wxParse/emojis/",emojis){ + __emojisReg = reg; + __emojisBaseSrc=baseSrc; + __emojis=emojis; +} + +module.exports = { + html2json: html2json, + emojisInit:emojisInit +}; + diff --git a/utils/wxParse/htmlparser.js b/utils/wxParse/htmlparser.js new file mode 100644 index 0000000..75e33cd --- /dev/null +++ b/utils/wxParse/htmlparser.js @@ -0,0 +1,192 @@ +/** + * + * htmlParser改造自: https://github.com/blowsie/Pure-JavaScript-HTML5-Parser + * + * author: Di (微信小程序开发工程师) + * organization: WeAppDev(微信小程序开发论坛)(http://weappdev.com) + * 垂直微信小程序开发交流社区 + * + * github地址: https://github.com/icindy/wxParse + * + * for: 微信小程序富文本解析 + * detail : http://weappdev.com/t/wxparse-alpha0-1-html-markdown/184 + */ +// Regular Expressions for parsing tags and attributes +var startTag = /^<([-A-Za-z0-9_]+)((?:\s+[a-zA-Z_:][-a-zA-Z0-9_:.]*(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)>/, + endTag = /^<\/([-A-Za-z0-9_]+)[^>]*>/, + attr = /([a-zA-Z_:][-a-zA-Z0-9_:.]*)(?:\s*=\s*(?:(?:"((?:\\.|[^"])*)")|(?:'((?:\\.|[^'])*)')|([^>\s]+)))?/g; + +// Empty Elements - HTML 5 +var empty = makeMap("area,base,basefont,br,col,frame,hr,img,input,link,meta,param,embed,command,keygen,source,track,wbr"); + +// Block Elements - HTML 5 +var block = makeMap("a,address,code,article,applet,aside,audio,blockquote,button,canvas,center,dd,del,dir,div,dl,dt,fieldset,figcaption,figure,footer,form,frameset,h1,h2,h3,h4,h5,h6,header,hgroup,hr,iframe,ins,isindex,li,map,menu,noframes,noscript,object,ol,output,p,pre,section,script,table,tbody,td,tfoot,th,thead,tr,ul,video"); + +// Inline Elements - HTML 5 +var inline = makeMap("abbr,acronym,applet,b,basefont,bdo,big,br,button,cite,del,dfn,em,font,i,iframe,img,input,ins,kbd,label,map,object,q,s,samp,script,select,small,span,strike,strong,sub,sup,textarea,tt,u,var"); + +// Elements that you can, intentionally, leave open +// (and which close themselves) +var closeSelf = makeMap("colgroup,dd,dt,li,options,p,td,tfoot,th,thead,tr"); + +// Attributes that have their values filled in disabled="disabled" +var fillAttrs = makeMap("checked,compact,declare,defer,disabled,ismap,multiple,nohref,noresize,noshade,nowrap,readonly,selected"); + +// Special Elements (can contain anything) +var special = makeMap("wxxxcode-style,script,style,view,scroll-view,block"); + +function HTMLParser(html, handler) { + var index, chars, match, stack = [], last = html; + stack.last = function () { + return this[this.length - 1]; + }; + + while (html) { + chars = true; + + // Make sure we're not in a script or style element + if (!stack.last() || !special[stack.last()]) { + + // Comment + if (html.indexOf(""); + + if (index >= 0) { + if (handler.comment) + handler.comment(html.substring(4, index)); + html = html.substring(index + 3); + chars = false; + } + + // end tag + } else if (html.indexOf("]*>"), function (all, text) { + text = text.replace(/|/g, "$1$2"); + if (handler.chars) + handler.chars(text); + + return ""; + }); + + + parseEndTag("", stack.last()); + } + + if (html == last) + throw "Parse Error: " + html; + last = html; + } + + // Clean up any remaining tags + parseEndTag(); + + function parseStartTag(tag, tagName, rest, unary) { + tagName = tagName.toLowerCase(); + + if (block[tagName]) { + while (stack.last() && inline[stack.last()]) { + parseEndTag("", stack.last()); + } + } + + if (closeSelf[tagName] && stack.last() == tagName) { + parseEndTag("", tagName); + } + + unary = empty[tagName] || !!unary; + + if (!unary) + stack.push(tagName); + + if (handler.start) { + var attrs = []; + + rest.replace(attr, function (match, name) { + var value = arguments[2] ? arguments[2] : + arguments[3] ? arguments[3] : + arguments[4] ? arguments[4] : + fillAttrs[name] ? name : ""; + + attrs.push({ + name: name, + value: value, + escaped: value.replace(/(^|[^\\])"/g, '$1\\\"') //" + }); + }); + + if (handler.start) { + handler.start(tagName, attrs, unary); + } + + } + } + + function parseEndTag(tag, tagName) { + // If no tag name is provided, clean shop + if (!tagName) + var pos = 0; + + // Find the closest opened tag of the same type + else { + tagName = tagName.toLowerCase(); + for (var pos = stack.length - 1; pos >= 0; pos--) + if (stack[pos] == tagName) + break; + } + if (pos >= 0) { + // Close all the open elements, up the stack + for (var i = stack.length - 1; i >= pos; i--) + if (handler.end) + handler.end(stack[i]); + + // Remove the open elements from the stack + stack.length = pos; + } + } +}; + + +function makeMap(str) { + var obj = {}, items = str.split(","); + for (var i = 0; i < items.length; i++) + obj[items[i]] = true; + return obj; +} + +module.exports = HTMLParser; diff --git a/utils/wxParse/showdown.js b/utils/wxParse/showdown.js new file mode 100644 index 0000000..d29206d --- /dev/null +++ b/utils/wxParse/showdown.js @@ -0,0 +1,2532 @@ +/** + * + * showdown: https://github.com/showdownjs/showdown + * + * author: Di (微信小程序开发工程师) + * organization: WeAppDev(微信小程序开发论坛)(http://weappdev.com) + * 垂直微信小程序开发交流社区 + * + * github地址: https://github.com/icindy/wxParse + * + * for: 微信小程序富文本解析 + * detail : http://weappdev.com/t/wxparse-alpha0-1-html-markdown/184 + */ + +function getDefaultOpts(simple) { + 'use strict'; + + var defaultOptions = { + omitExtraWLInCodeBlocks: { + defaultValue: false, + describe: 'Omit the default extra whiteline added to code blocks', + type: 'boolean' + }, + noHeaderId: { + defaultValue: false, + describe: 'Turn on/off generated header id', + type: 'boolean' + }, + prefixHeaderId: { + defaultValue: false, + describe: 'Specify a prefix to generated header ids', + type: 'string' + }, + headerLevelStart: { + defaultValue: false, + describe: 'The header blocks level start', + type: 'integer' + }, + parseImgDimensions: { + defaultValue: false, + describe: 'Turn on/off image dimension parsing', + type: 'boolean' + }, + simplifiedAutoLink: { + defaultValue: false, + describe: 'Turn on/off GFM autolink style', + type: 'boolean' + }, + literalMidWordUnderscores: { + defaultValue: false, + describe: 'Parse midword underscores as literal underscores', + type: 'boolean' + }, + strikethrough: { + defaultValue: false, + describe: 'Turn on/off strikethrough support', + type: 'boolean' + }, + tables: { + defaultValue: false, + describe: 'Turn on/off tables support', + type: 'boolean' + }, + tablesHeaderId: { + defaultValue: false, + describe: 'Add an id to table headers', + type: 'boolean' + }, + ghCodeBlocks: { + defaultValue: true, + describe: 'Turn on/off GFM fenced code blocks support', + type: 'boolean' + }, + tasklists: { + defaultValue: false, + describe: 'Turn on/off GFM tasklist support', + type: 'boolean' + }, + smoothLivePreview: { + defaultValue: false, + describe: 'Prevents weird effects in live previews due to incomplete input', + type: 'boolean' + }, + smartIndentationFix: { + defaultValue: false, + description: 'Tries to smartly fix identation in es6 strings', + type: 'boolean' + } + }; + if (simple === false) { + return JSON.parse(JSON.stringify(defaultOptions)); + } + var ret = {}; + for (var opt in defaultOptions) { + if (defaultOptions.hasOwnProperty(opt)) { + ret[opt] = defaultOptions[opt].defaultValue; + } + } + return ret; +} + +/** + * Created by Tivie on 06-01-2015. + */ + +// Private properties +var showdown = {}, + parsers = {}, + extensions = {}, + globalOptions = getDefaultOpts(true), + flavor = { + github: { + omitExtraWLInCodeBlocks: true, + prefixHeaderId: 'user-content-', + simplifiedAutoLink: true, + literalMidWordUnderscores: true, + strikethrough: true, + tables: true, + tablesHeaderId: true, + ghCodeBlocks: true, + tasklists: true + }, + vanilla: getDefaultOpts(true) + }; + +/** + * helper namespace + * @type {{}} + */ +showdown.helper = {}; + +/** + * TODO LEGACY SUPPORT CODE + * @type {{}} + */ +showdown.extensions = {}; + +/** + * Set a global option + * @static + * @param {string} key + * @param {*} value + * @returns {showdown} + */ +showdown.setOption = function (key, value) { + 'use strict'; + globalOptions[key] = value; + return this; +}; + +/** + * Get a global option + * @static + * @param {string} key + * @returns {*} + */ +showdown.getOption = function (key) { + 'use strict'; + return globalOptions[key]; +}; + +/** + * Get the global options + * @static + * @returns {{}} + */ +showdown.getOptions = function () { + 'use strict'; + return globalOptions; +}; + +/** + * Reset global options to the default values + * @static + */ +showdown.resetOptions = function () { + 'use strict'; + globalOptions = getDefaultOpts(true); +}; + +/** + * Set the flavor showdown should use as default + * @param {string} name + */ +showdown.setFlavor = function (name) { + 'use strict'; + if (flavor.hasOwnProperty(name)) { + var preset = flavor[name]; + for (var option in preset) { + if (preset.hasOwnProperty(option)) { + globalOptions[option] = preset[option]; + } + } + } +}; + +/** + * Get the default options + * @static + * @param {boolean} [simple=true] + * @returns {{}} + */ +showdown.getDefaultOptions = function (simple) { + 'use strict'; + return getDefaultOpts(simple); +}; + +/** + * Get or set a subParser + * + * subParser(name) - Get a registered subParser + * subParser(name, func) - Register a subParser + * @static + * @param {string} name + * @param {function} [func] + * @returns {*} + */ +showdown.subParser = function (name, func) { + 'use strict'; + if (showdown.helper.isString(name)) { + if (typeof func !== 'undefined') { + parsers[name] = func; + } else { + if (parsers.hasOwnProperty(name)) { + return parsers[name]; + } else { + throw Error('SubParser named ' + name + ' not registered!'); + } + } + } +}; + +/** + * Gets or registers an extension + * @static + * @param {string} name + * @param {object|function=} ext + * @returns {*} + */ +showdown.extension = function (name, ext) { + 'use strict'; + + if (!showdown.helper.isString(name)) { + throw Error('Extension \'name\' must be a string'); + } + + name = showdown.helper.stdExtName(name); + + // Getter + if (showdown.helper.isUndefined(ext)) { + if (!extensions.hasOwnProperty(name)) { + throw Error('Extension named ' + name + ' is not registered!'); + } + return extensions[name]; + + // Setter + } else { + // Expand extension if it's wrapped in a function + if (typeof ext === 'function') { + ext = ext(); + } + + // Ensure extension is an array + if (!showdown.helper.isArray(ext)) { + ext = [ext]; + } + + var validExtension = validate(ext, name); + + if (validExtension.valid) { + extensions[name] = ext; + } else { + throw Error(validExtension.error); + } + } +}; + +/** + * Gets all extensions registered + * @returns {{}} + */ +showdown.getAllExtensions = function () { + 'use strict'; + return extensions; +}; + +/** + * Remove an extension + * @param {string} name + */ +showdown.removeExtension = function (name) { + 'use strict'; + delete extensions[name]; +}; + +/** + * Removes all extensions + */ +showdown.resetExtensions = function () { + 'use strict'; + extensions = {}; +}; + +/** + * Validate extension + * @param {array} extension + * @param {string} name + * @returns {{valid: boolean, error: string}} + */ +function validate(extension, name) { + 'use strict'; + + var errMsg = (name) ? 'Error in ' + name + ' extension->' : 'Error in unnamed extension', + ret = { + valid: true, + error: '' + }; + + if (!showdown.helper.isArray(extension)) { + extension = [extension]; + } + + for (var i = 0; i < extension.length; ++i) { + var baseMsg = errMsg + ' sub-extension ' + i + ': ', + ext = extension[i]; + if (typeof ext !== 'object') { + ret.valid = false; + ret.error = baseMsg + 'must be an object, but ' + typeof ext + ' given'; + return ret; + } + + if (!showdown.helper.isString(ext.type)) { + ret.valid = false; + ret.error = baseMsg + 'property "type" must be a string, but ' + typeof ext.type + ' given'; + return ret; + } + + var type = ext.type = ext.type.toLowerCase(); + + // normalize extension type + if (type === 'language') { + type = ext.type = 'lang'; + } + + if (type === 'html') { + type = ext.type = 'output'; + } + + if (type !== 'lang' && type !== 'output' && type !== 'listener') { + ret.valid = false; + ret.error = baseMsg + 'type ' + type + ' is not recognized. Valid values: "lang/language", "output/html" or "listener"'; + return ret; + } + + if (type === 'listener') { + if (showdown.helper.isUndefined(ext.listeners)) { + ret.valid = false; + ret.error = baseMsg + '. Extensions of type "listener" must have a property called "listeners"'; + return ret; + } + } else { + if (showdown.helper.isUndefined(ext.filter) && showdown.helper.isUndefined(ext.regex)) { + ret.valid = false; + ret.error = baseMsg + type + ' extensions must define either a "regex" property or a "filter" method'; + return ret; + } + } + + if (ext.listeners) { + if (typeof ext.listeners !== 'object') { + ret.valid = false; + ret.error = baseMsg + '"listeners" property must be an object but ' + typeof ext.listeners + ' given'; + return ret; + } + for (var ln in ext.listeners) { + if (ext.listeners.hasOwnProperty(ln)) { + if (typeof ext.listeners[ln] !== 'function') { + ret.valid = false; + ret.error = baseMsg + '"listeners" property must be an hash of [event name]: [callback]. listeners.' + ln + + ' must be a function but ' + typeof ext.listeners[ln] + ' given'; + return ret; + } + } + } + } + + if (ext.filter) { + if (typeof ext.filter !== 'function') { + ret.valid = false; + ret.error = baseMsg + '"filter" must be a function, but ' + typeof ext.filter + ' given'; + return ret; + } + } else if (ext.regex) { + if (showdown.helper.isString(ext.regex)) { + ext.regex = new RegExp(ext.regex, 'g'); + } + if (!ext.regex instanceof RegExp) { + ret.valid = false; + ret.error = baseMsg + '"regex" property must either be a string or a RegExp object, but ' + typeof ext.regex + ' given'; + return ret; + } + if (showdown.helper.isUndefined(ext.replace)) { + ret.valid = false; + ret.error = baseMsg + '"regex" extensions must implement a replace string or function'; + return ret; + } + } + } + return ret; +} + +/** + * Validate extension + * @param {object} ext + * @returns {boolean} + */ +showdown.validateExtension = function (ext) { + 'use strict'; + + var validateExtension = validate(ext, null); + if (!validateExtension.valid) { + console.warn(validateExtension.error); + return false; + } + return true; +}; + +/** + * showdownjs helper functions + */ + +if (!showdown.hasOwnProperty('helper')) { + showdown.helper = {}; +} + +/** + * Check if var is string + * @static + * @param {string} a + * @returns {boolean} + */ +showdown.helper.isString = function isString(a) { + 'use strict'; + return (typeof a === 'string' || a instanceof String); +}; + +/** + * Check if var is a function + * @static + * @param {string} a + * @returns {boolean} + */ +showdown.helper.isFunction = function isFunction(a) { + 'use strict'; + var getType = {}; + return a && getType.toString.call(a) === '[object Function]'; +}; + +/** + * ForEach helper function + * @static + * @param {*} obj + * @param {function} callback + */ +showdown.helper.forEach = function forEach(obj, callback) { + 'use strict'; + if (typeof obj.forEach === 'function') { + obj.forEach(callback); + } else { + for (var i = 0; i < obj.length; i++) { + callback(obj[i], i, obj); + } + } +}; + +/** + * isArray helper function + * @static + * @param {*} a + * @returns {boolean} + */ +showdown.helper.isArray = function isArray(a) { + 'use strict'; + return a.constructor === Array; +}; + +/** + * Check if value is undefined + * @static + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is `undefined`, else `false`. + */ +showdown.helper.isUndefined = function isUndefined(value) { + 'use strict'; + return typeof value === 'undefined'; +}; + +/** + * Standardidize extension name + * @static + * @param {string} s extension name + * @returns {string} + */ +showdown.helper.stdExtName = function (s) { + 'use strict'; + return s.replace(/[_-]||\s/g, '').toLowerCase(); +}; + +function escapeCharactersCallback(wholeMatch, m1) { + 'use strict'; + var charCodeToEscape = m1.charCodeAt(0); + return '~E' + charCodeToEscape + 'E'; +} + +/** + * Callback used to escape characters when passing through String.replace + * @static + * @param {string} wholeMatch + * @param {string} m1 + * @returns {string} + */ +showdown.helper.escapeCharactersCallback = escapeCharactersCallback; + +/** + * Escape characters in a string + * @static + * @param {string} text + * @param {string} charsToEscape + * @param {boolean} afterBackslash + * @returns {XML|string|void|*} + */ +showdown.helper.escapeCharacters = function escapeCharacters(text, charsToEscape, afterBackslash) { + 'use strict'; + // First we have to escape the escape characters so that + // we can build a character class out of them + var regexString = '([' + charsToEscape.replace(/([\[\]\\])/g, '\\$1') + '])'; + + if (afterBackslash) { + regexString = '\\\\' + regexString; + } + + var regex = new RegExp(regexString, 'g'); + text = text.replace(regex, escapeCharactersCallback); + + return text; +}; + +var rgxFindMatchPos = function (str, left, right, flags) { + 'use strict'; + var f = flags || '', + g = f.indexOf('g') > -1, + x = new RegExp(left + '|' + right, 'g' + f.replace(/g/g, '')), + l = new RegExp(left, f.replace(/g/g, '')), + pos = [], + t, s, m, start, end; + + do { + t = 0; + while ((m = x.exec(str))) { + if (l.test(m[0])) { + if (!(t++)) { + s = x.lastIndex; + start = s - m[0].length; + } + } else if (t) { + if (!--t) { + end = m.index + m[0].length; + var obj = { + left: {start: start, end: s}, + match: {start: s, end: m.index}, + right: {start: m.index, end: end}, + wholeMatch: {start: start, end: end} + }; + pos.push(obj); + if (!g) { + return pos; + } + } + } + } + } while (t && (x.lastIndex = s)); + + return pos; +}; + +/** + * matchRecursiveRegExp + * + * (c) 2007 Steven Levithan + * MIT License + * + * Accepts a string to search, a left and right format delimiter + * as regex patterns, and optional regex flags. Returns an array + * of matches, allowing nested instances of left/right delimiters. + * Use the "g" flag to return all matches, otherwise only the + * first is returned. Be careful to ensure that the left and + * right format delimiters produce mutually exclusive matches. + * Backreferences are not supported within the right delimiter + * due to how it is internally combined with the left delimiter. + * When matching strings whose format delimiters are unbalanced + * to the left or right, the output is intentionally as a + * conventional regex library with recursion support would + * produce, e.g. "<" and ">" both produce ["x"] when using + * "<" and ">" as the delimiters (both strings contain a single, + * balanced instance of ""). + * + * examples: + * matchRecursiveRegExp("test", "\\(", "\\)") + * returns: [] + * matchRecursiveRegExp(">>t<>", "<", ">", "g") + * returns: ["t<>", ""] + * matchRecursiveRegExp("

test
", "]*>", "", "gi") + * returns: ["test"] + */ +showdown.helper.matchRecursiveRegExp = function (str, left, right, flags) { + 'use strict'; + + var matchPos = rgxFindMatchPos (str, left, right, flags), + results = []; + + for (var i = 0; i < matchPos.length; ++i) { + results.push([ + str.slice(matchPos[i].wholeMatch.start, matchPos[i].wholeMatch.end), + str.slice(matchPos[i].match.start, matchPos[i].match.end), + str.slice(matchPos[i].left.start, matchPos[i].left.end), + str.slice(matchPos[i].right.start, matchPos[i].right.end) + ]); + } + return results; +}; + +/** + * + * @param {string} str + * @param {string|function} replacement + * @param {string} left + * @param {string} right + * @param {string} flags + * @returns {string} + */ +showdown.helper.replaceRecursiveRegExp = function (str, replacement, left, right, flags) { + 'use strict'; + + if (!showdown.helper.isFunction(replacement)) { + var repStr = replacement; + replacement = function () { + return repStr; + }; + } + + var matchPos = rgxFindMatchPos(str, left, right, flags), + finalStr = str, + lng = matchPos.length; + + if (lng > 0) { + var bits = []; + if (matchPos[0].wholeMatch.start !== 0) { + bits.push(str.slice(0, matchPos[0].wholeMatch.start)); + } + for (var i = 0; i < lng; ++i) { + bits.push( + replacement( + str.slice(matchPos[i].wholeMatch.start, matchPos[i].wholeMatch.end), + str.slice(matchPos[i].match.start, matchPos[i].match.end), + str.slice(matchPos[i].left.start, matchPos[i].left.end), + str.slice(matchPos[i].right.start, matchPos[i].right.end) + ) + ); + if (i < lng - 1) { + bits.push(str.slice(matchPos[i].wholeMatch.end, matchPos[i + 1].wholeMatch.start)); + } + } + if (matchPos[lng - 1].wholeMatch.end < str.length) { + bits.push(str.slice(matchPos[lng - 1].wholeMatch.end)); + } + finalStr = bits.join(''); + } + return finalStr; +}; + +/** + * POLYFILLS + */ +if (showdown.helper.isUndefined(console)) { + console = { + warn: function (msg) { + 'use strict'; + alert(msg); + }, + log: function (msg) { + 'use strict'; + alert(msg); + }, + error: function (msg) { + 'use strict'; + throw msg; + } + }; +} + +/** + * Created by Estevao on 31-05-2015. + */ + +/** + * Showdown Converter class + * @class + * @param {object} [converterOptions] + * @returns {Converter} + */ +showdown.Converter = function (converterOptions) { + 'use strict'; + + var + /** + * Options used by this converter + * @private + * @type {{}} + */ + options = {}, + + /** + * Language extensions used by this converter + * @private + * @type {Array} + */ + langExtensions = [], + + /** + * Output modifiers extensions used by this converter + * @private + * @type {Array} + */ + outputModifiers = [], + + /** + * Event listeners + * @private + * @type {{}} + */ + listeners = {}; + + _constructor(); + + /** + * Converter constructor + * @private + */ + function _constructor() { + converterOptions = converterOptions || {}; + + for (var gOpt in globalOptions) { + if (globalOptions.hasOwnProperty(gOpt)) { + options[gOpt] = globalOptions[gOpt]; + } + } + + // Merge options + if (typeof converterOptions === 'object') { + for (var opt in converterOptions) { + if (converterOptions.hasOwnProperty(opt)) { + options[opt] = converterOptions[opt]; + } + } + } else { + throw Error('Converter expects the passed parameter to be an object, but ' + typeof converterOptions + + ' was passed instead.'); + } + + if (options.extensions) { + showdown.helper.forEach(options.extensions, _parseExtension); + } + } + + /** + * Parse extension + * @param {*} ext + * @param {string} [name=''] + * @private + */ + function _parseExtension(ext, name) { + + name = name || null; + // If it's a string, the extension was previously loaded + if (showdown.helper.isString(ext)) { + ext = showdown.helper.stdExtName(ext); + name = ext; + + // LEGACY_SUPPORT CODE + if (showdown.extensions[ext]) { + console.warn('DEPRECATION WARNING: ' + ext + ' is an old extension that uses a deprecated loading method.' + + 'Please inform the developer that the extension should be updated!'); + legacyExtensionLoading(showdown.extensions[ext], ext); + return; + // END LEGACY SUPPORT CODE + + } else if (!showdown.helper.isUndefined(extensions[ext])) { + ext = extensions[ext]; + + } else { + throw Error('Extension "' + ext + '" could not be loaded. It was either not found or is not a valid extension.'); + } + } + + if (typeof ext === 'function') { + ext = ext(); + } + + if (!showdown.helper.isArray(ext)) { + ext = [ext]; + } + + var validExt = validate(ext, name); + if (!validExt.valid) { + throw Error(validExt.error); + } + + for (var i = 0; i < ext.length; ++i) { + switch (ext[i].type) { + + case 'lang': + langExtensions.push(ext[i]); + break; + + case 'output': + outputModifiers.push(ext[i]); + break; + } + if (ext[i].hasOwnProperty(listeners)) { + for (var ln in ext[i].listeners) { + if (ext[i].listeners.hasOwnProperty(ln)) { + listen(ln, ext[i].listeners[ln]); + } + } + } + } + + } + + /** + * LEGACY_SUPPORT + * @param {*} ext + * @param {string} name + */ + function legacyExtensionLoading(ext, name) { + if (typeof ext === 'function') { + ext = ext(new showdown.Converter()); + } + if (!showdown.helper.isArray(ext)) { + ext = [ext]; + } + var valid = validate(ext, name); + + if (!valid.valid) { + throw Error(valid.error); + } + + for (var i = 0; i < ext.length; ++i) { + switch (ext[i].type) { + case 'lang': + langExtensions.push(ext[i]); + break; + case 'output': + outputModifiers.push(ext[i]); + break; + default:// should never reach here + throw Error('Extension loader error: Type unrecognized!!!'); + } + } + } + + /** + * Listen to an event + * @param {string} name + * @param {function} callback + */ + function listen(name, callback) { + if (!showdown.helper.isString(name)) { + throw Error('Invalid argument in converter.listen() method: name must be a string, but ' + typeof name + ' given'); + } + + if (typeof callback !== 'function') { + throw Error('Invalid argument in converter.listen() method: callback must be a function, but ' + typeof callback + ' given'); + } + + if (!listeners.hasOwnProperty(name)) { + listeners[name] = []; + } + listeners[name].push(callback); + } + + function rTrimInputText(text) { + var rsp = text.match(/^\s*/)[0].length, + rgx = new RegExp('^\\s{0,' + rsp + '}', 'gm'); + return text.replace(rgx, ''); + } + + /** + * Dispatch an event + * @private + * @param {string} evtName Event name + * @param {string} text Text + * @param {{}} options Converter Options + * @param {{}} globals + * @returns {string} + */ + this._dispatch = function dispatch (evtName, text, options, globals) { + if (listeners.hasOwnProperty(evtName)) { + for (var ei = 0; ei < listeners[evtName].length; ++ei) { + var nText = listeners[evtName][ei](evtName, text, this, options, globals); + if (nText && typeof nText !== 'undefined') { + text = nText; + } + } + } + return text; + }; + + /** + * Listen to an event + * @param {string} name + * @param {function} callback + * @returns {showdown.Converter} + */ + this.listen = function (name, callback) { + listen(name, callback); + return this; + }; + + /** + * Converts a markdown string into HTML + * @param {string} text + * @returns {*} + */ + this.makeHtml = function (text) { + //check if text is not falsy + if (!text) { + return text; + } + + var globals = { + gHtmlBlocks: [], + gHtmlMdBlocks: [], + gHtmlSpans: [], + gUrls: {}, + gTitles: {}, + gDimensions: {}, + gListLevel: 0, + hashLinkCounts: {}, + langExtensions: langExtensions, + outputModifiers: outputModifiers, + converter: this, + ghCodeBlocks: [] + }; + + // attacklab: Replace ~ with ~T + // This lets us use tilde as an escape char to avoid md5 hashes + // The choice of character is arbitrary; anything that isn't + // magic in Markdown will work. + text = text.replace(/~/g, '~T'); + + // attacklab: Replace $ with ~D + // RegExp interprets $ as a special character + // when it's in a replacement string + text = text.replace(/\$/g, '~D'); + + // Standardize line endings + text = text.replace(/\r\n/g, '\n'); // DOS to Unix + text = text.replace(/\r/g, '\n'); // Mac to Unix + + if (options.smartIndentationFix) { + text = rTrimInputText(text); + } + + // Make sure text begins and ends with a couple of newlines: + //text = '\n\n' + text + '\n\n'; + text = text; + // detab + text = showdown.subParser('detab')(text, options, globals); + + // stripBlankLines + text = showdown.subParser('stripBlankLines')(text, options, globals); + + //run languageExtensions + showdown.helper.forEach(langExtensions, function (ext) { + text = showdown.subParser('runExtension')(ext, text, options, globals); + }); + + // run the sub parsers + text = showdown.subParser('hashPreCodeTags')(text, options, globals); + text = showdown.subParser('githubCodeBlocks')(text, options, globals); + text = showdown.subParser('hashHTMLBlocks')(text, options, globals); + text = showdown.subParser('hashHTMLSpans')(text, options, globals); + text = showdown.subParser('stripLinkDefinitions')(text, options, globals); + text = showdown.subParser('blockGamut')(text, options, globals); + text = showdown.subParser('unhashHTMLSpans')(text, options, globals); + text = showdown.subParser('unescapeSpecialChars')(text, options, globals); + + // attacklab: Restore dollar signs + text = text.replace(/~D/g, '$$'); + + // attacklab: Restore tildes + text = text.replace(/~T/g, '~'); + + // Run output modifiers + showdown.helper.forEach(outputModifiers, function (ext) { + text = showdown.subParser('runExtension')(ext, text, options, globals); + }); + return text; + }; + + /** + * Set an option of this Converter instance + * @param {string} key + * @param {*} value + */ + this.setOption = function (key, value) { + options[key] = value; + }; + + /** + * Get the option of this Converter instance + * @param {string} key + * @returns {*} + */ + this.getOption = function (key) { + return options[key]; + }; + + /** + * Get the options of this Converter instance + * @returns {{}} + */ + this.getOptions = function () { + return options; + }; + + /** + * Add extension to THIS converter + * @param {{}} extension + * @param {string} [name=null] + */ + this.addExtension = function (extension, name) { + name = name || null; + _parseExtension(extension, name); + }; + + /** + * Use a global registered extension with THIS converter + * @param {string} extensionName Name of the previously registered extension + */ + this.useExtension = function (extensionName) { + _parseExtension(extensionName); + }; + + /** + * Set the flavor THIS converter should use + * @param {string} name + */ + this.setFlavor = function (name) { + if (flavor.hasOwnProperty(name)) { + var preset = flavor[name]; + for (var option in preset) { + if (preset.hasOwnProperty(option)) { + options[option] = preset[option]; + } + } + } + }; + + /** + * Remove an extension from THIS converter. + * Note: This is a costly operation. It's better to initialize a new converter + * and specify the extensions you wish to use + * @param {Array} extension + */ + this.removeExtension = function (extension) { + if (!showdown.helper.isArray(extension)) { + extension = [extension]; + } + for (var a = 0; a < extension.length; ++a) { + var ext = extension[a]; + for (var i = 0; i < langExtensions.length; ++i) { + if (langExtensions[i] === ext) { + langExtensions[i].splice(i, 1); + } + } + for (var ii = 0; ii < outputModifiers.length; ++i) { + if (outputModifiers[ii] === ext) { + outputModifiers[ii].splice(i, 1); + } + } + } + }; + + /** + * Get all extension of THIS converter + * @returns {{language: Array, output: Array}} + */ + this.getAllExtensions = function () { + return { + language: langExtensions, + output: outputModifiers + }; + }; +}; + +/** + * Turn Markdown link shortcuts into XHTML tags. + */ +showdown.subParser('anchors', function (text, options, globals) { + 'use strict'; + + text = globals.converter._dispatch('anchors.before', text, options, globals); + + var writeAnchorTag = function (wholeMatch, m1, m2, m3, m4, m5, m6, m7) { + if (showdown.helper.isUndefined(m7)) { + m7 = ''; + } + wholeMatch = m1; + var linkText = m2, + linkId = m3.toLowerCase(), + url = m4, + title = m7; + + if (!url) { + if (!linkId) { + // lower-case and turn embedded newlines into spaces + linkId = linkText.toLowerCase().replace(/ ?\n/g, ' '); + } + url = '#' + linkId; + + if (!showdown.helper.isUndefined(globals.gUrls[linkId])) { + url = globals.gUrls[linkId]; + if (!showdown.helper.isUndefined(globals.gTitles[linkId])) { + title = globals.gTitles[linkId]; + } + } else { + if (wholeMatch.search(/\(\s*\)$/m) > -1) { + // Special case for explicit empty url + url = ''; + } else { + return wholeMatch; + } + } + } + + url = showdown.helper.escapeCharacters(url, '*_', false); + var result = ''; + + return result; + }; + + // First, handle reference-style links: [link text] [id] + /* + text = text.replace(/ + ( // wrap whole match in $1 + \[ + ( + (?: + \[[^\]]*\] // allow brackets nested one level + | + [^\[] // or anything else + )* + ) + \] + + [ ]? // one optional space + (?:\n[ ]*)? // one optional newline followed by spaces + + \[ + (.*?) // id = $3 + \] + )()()()() // pad remaining backreferences + /g,_DoAnchors_callback); + */ + text = text.replace(/(\[((?:\[[^\]]*]|[^\[\]])*)][ ]?(?:\n[ ]*)?\[(.*?)])()()()()/g, writeAnchorTag); + + // + // Next, inline-style links: [link text](url "optional title") + // + + /* + text = text.replace(/ + ( // wrap whole match in $1 + \[ + ( + (?: + \[[^\]]*\] // allow brackets nested one level + | + [^\[\]] // or anything else + ) + ) + \] + \( // literal paren + [ \t]* + () // no id, so leave $3 empty + ? // href = $4 + [ \t]* + ( // $5 + (['"]) // quote char = $6 + (.*?) // Title = $7 + \6 // matching quote + [ \t]* // ignore any spaces/tabs between closing quote and ) + )? // title is optional + \) + ) + /g,writeAnchorTag); + */ + text = text.replace(/(\[((?:\[[^\]]*]|[^\[\]])*)]\([ \t]*()?[ \t]*((['"])(.*?)\6[ \t]*)?\))/g, + writeAnchorTag); + + // + // Last, handle reference-style shortcuts: [link text] + // These must come last in case you've also got [link test][1] + // or [link test](/foo) + // + + /* + text = text.replace(/ + ( // wrap whole match in $1 + \[ + ([^\[\]]+) // link text = $2; can't contain '[' or ']' + \] + )()()()()() // pad rest of backreferences + /g, writeAnchorTag); + */ + text = text.replace(/(\[([^\[\]]+)])()()()()()/g, writeAnchorTag); + + text = globals.converter._dispatch('anchors.after', text, options, globals); + return text; +}); + +showdown.subParser('autoLinks', function (text, options, globals) { + 'use strict'; + + text = globals.converter._dispatch('autoLinks.before', text, options, globals); + + var simpleURLRegex = /\b(((https?|ftp|dict):\/\/|www\.)[^'">\s]+\.[^'">\s]+)(?=\s|$)(?!["<>])/gi, + delimUrlRegex = /<(((https?|ftp|dict):\/\/|www\.)[^'">\s]+)>/gi, + simpleMailRegex = /(?:^|[ \n\t])([A-Za-z0-9!#$%&'*+-/=?^_`\{|}~\.]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)(?:$|[ \n\t])/gi, + delimMailRegex = /<(?:mailto:)?([-.\w]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)>/gi; + + text = text.replace(delimUrlRegex, replaceLink); + text = text.replace(delimMailRegex, replaceMail); + // simpleURLRegex = /\b(((https?|ftp|dict):\/\/|www\.)[-.+~:?#@!$&'()*,;=[\]\w]+)\b/gi, + // Email addresses: + + if (options.simplifiedAutoLink) { + text = text.replace(simpleURLRegex, replaceLink); + text = text.replace(simpleMailRegex, replaceMail); + } + + function replaceLink(wm, link) { + var lnkTxt = link; + if (/^www\./i.test(link)) { + link = link.replace(/^www\./i, 'http://www.'); + } + return '' + lnkTxt + ''; + } + + function replaceMail(wholeMatch, m1) { + var unescapedStr = showdown.subParser('unescapeSpecialChars')(m1); + return showdown.subParser('encodeEmailAddress')(unescapedStr); + } + + text = globals.converter._dispatch('autoLinks.after', text, options, globals); + + return text; +}); + +/** + * These are all the transformations that form block-level + * tags like paragraphs, headers, and list items. + */ +showdown.subParser('blockGamut', function (text, options, globals) { + 'use strict'; + + text = globals.converter._dispatch('blockGamut.before', text, options, globals); + + // we parse blockquotes first so that we can have headings and hrs + // inside blockquotes + text = showdown.subParser('blockQuotes')(text, options, globals); + text = showdown.subParser('headers')(text, options, globals); + + // Do Horizontal Rules: + var key = showdown.subParser('hashBlock')('
', options, globals); + text = text.replace(/^[ ]{0,2}([ ]?\*[ ]?){3,}[ \t]*$/gm, key); + text = text.replace(/^[ ]{0,2}([ ]?\-[ ]?){3,}[ \t]*$/gm, key); + text = text.replace(/^[ ]{0,2}([ ]?_[ ]?){3,}[ \t]*$/gm, key); + + text = showdown.subParser('lists')(text, options, globals); + text = showdown.subParser('codeBlocks')(text, options, globals); + text = showdown.subParser('tables')(text, options, globals); + + // We already ran _HashHTMLBlocks() before, in Markdown(), but that + // was to escape raw HTML in the original Markdown source. This time, + // we're escaping the markup we've just created, so that we don't wrap + //

tags around block-level tags. + text = showdown.subParser('hashHTMLBlocks')(text, options, globals); + text = showdown.subParser('paragraphs')(text, options, globals); + + text = globals.converter._dispatch('blockGamut.after', text, options, globals); + + return text; +}); + +showdown.subParser('blockQuotes', function (text, options, globals) { + 'use strict'; + + text = globals.converter._dispatch('blockQuotes.before', text, options, globals); + /* + text = text.replace(/ + ( // Wrap whole match in $1 + ( + ^[ \t]*>[ \t]? // '>' at the start of a line + .+\n // rest of the first line + (.+\n)* // subsequent consecutive lines + \n* // blanks + )+ + ) + /gm, function(){...}); + */ + + text = text.replace(/((^[ \t]{0,3}>[ \t]?.+\n(.+\n)*\n*)+)/gm, function (wholeMatch, m1) { + var bq = m1; + + // attacklab: hack around Konqueror 3.5.4 bug: + // "----------bug".replace(/^-/g,"") == "bug" + bq = bq.replace(/^[ \t]*>[ \t]?/gm, '~0'); // trim one level of quoting + + // attacklab: clean up hack + bq = bq.replace(/~0/g, ''); + + bq = bq.replace(/^[ \t]+$/gm, ''); // trim whitespace-only lines + bq = showdown.subParser('githubCodeBlocks')(bq, options, globals); + bq = showdown.subParser('blockGamut')(bq, options, globals); // recurse + + bq = bq.replace(/(^|\n)/g, '$1 '); + // These leading spaces screw with

 content, so we need to fix that:
+    bq = bq.replace(/(\s*
[^\r]+?<\/pre>)/gm, function (wholeMatch, m1) {
+      var pre = m1;
+      // attacklab: hack around Konqueror 3.5.4 bug:
+      pre = pre.replace(/^  /mg, '~0');
+      pre = pre.replace(/~0/g, '');
+      return pre;
+    });
+
+    return showdown.subParser('hashBlock')('
\n' + bq + '\n
', options, globals); + }); + + text = globals.converter._dispatch('blockQuotes.after', text, options, globals); + return text; +}); + +/** + * Process Markdown `
` blocks.
+ */
+showdown.subParser('codeBlocks', function (text, options, globals) {
+  'use strict';
+
+  text = globals.converter._dispatch('codeBlocks.before', text, options, globals);
+  /*
+   text = text.replace(text,
+   /(?:\n\n|^)
+   (								// $1 = the code block -- one or more lines, starting with a space/tab
+   (?:
+   (?:[ ]{4}|\t)			// Lines must start with a tab or a tab-width of spaces - attacklab: g_tab_width
+   .*\n+
+   )+
+   )
+   (\n*[ ]{0,3}[^ \t\n]|(?=~0))	// attacklab: g_tab_width
+   /g,function(){...});
+   */
+
+  // attacklab: sentinel workarounds for lack of \A and \Z, safari\khtml bug
+  text += '~0';
+
+  var pattern = /(?:\n\n|^)((?:(?:[ ]{4}|\t).*\n+)+)(\n*[ ]{0,3}[^ \t\n]|(?=~0))/g;
+  text = text.replace(pattern, function (wholeMatch, m1, m2) {
+    var codeblock = m1,
+        nextChar = m2,
+        end = '\n';
+
+    codeblock = showdown.subParser('outdent')(codeblock);
+    codeblock = showdown.subParser('encodeCode')(codeblock);
+    codeblock = showdown.subParser('detab')(codeblock);
+    codeblock = codeblock.replace(/^\n+/g, ''); // trim leading newlines
+    codeblock = codeblock.replace(/\n+$/g, ''); // trim trailing newlines
+
+    if (options.omitExtraWLInCodeBlocks) {
+      end = '';
+    }
+
+    codeblock = '
' + codeblock + end + '
'; + + return showdown.subParser('hashBlock')(codeblock, options, globals) + nextChar; + }); + + // attacklab: strip sentinel + text = text.replace(/~0/, ''); + + text = globals.converter._dispatch('codeBlocks.after', text, options, globals); + return text; +}); + +/** + * + * * Backtick quotes are used for spans. + * + * * You can use multiple backticks as the delimiters if you want to + * include literal backticks in the code span. So, this input: + * + * Just type ``foo `bar` baz`` at the prompt. + * + * Will translate to: + * + *

Just type foo `bar` baz at the prompt.

+ * + * There's no arbitrary limit to the number of backticks you + * can use as delimters. If you need three consecutive backticks + * in your code, use four for delimiters, etc. + * + * * You can use spaces to get literal backticks at the edges: + * + * ... type `` `bar` `` ... + * + * Turns to: + * + * ... type `bar` ... + */ +showdown.subParser('codeSpans', function (text, options, globals) { + 'use strict'; + + text = globals.converter._dispatch('codeSpans.before', text, options, globals); + + /* + text = text.replace(/ + (^|[^\\]) // Character before opening ` can't be a backslash + (`+) // $2 = Opening run of ` + ( // $3 = The code block + [^\r]*? + [^`] // attacklab: work around lack of lookbehind + ) + \2 // Matching closer + (?!`) + /gm, function(){...}); + */ + + if (typeof(text) === 'undefined') { + text = ''; + } + text = text.replace(/(^|[^\\])(`+)([^\r]*?[^`])\2(?!`)/gm, + function (wholeMatch, m1, m2, m3) { + var c = m3; + c = c.replace(/^([ \t]*)/g, ''); // leading whitespace + c = c.replace(/[ \t]*$/g, ''); // trailing whitespace + c = showdown.subParser('encodeCode')(c); + return m1 + '' + c + ''; + } + ); + + text = globals.converter._dispatch('codeSpans.after', text, options, globals); + return text; +}); + +/** + * Convert all tabs to spaces + */ +showdown.subParser('detab', function (text) { + 'use strict'; + + // expand first n-1 tabs + text = text.replace(/\t(?=\t)/g, ' '); // g_tab_width + + // replace the nth with two sentinels + text = text.replace(/\t/g, '~A~B'); + + // use the sentinel to anchor our regex so it doesn't explode + text = text.replace(/~B(.+?)~A/g, function (wholeMatch, m1) { + var leadingText = m1, + numSpaces = 4 - leadingText.length % 4; // g_tab_width + + // there *must* be a better way to do this: + for (var i = 0; i < numSpaces; i++) { + leadingText += ' '; + } + + return leadingText; + }); + + // clean up sentinels + text = text.replace(/~A/g, ' '); // g_tab_width + text = text.replace(/~B/g, ''); + + return text; + +}); + +/** + * Smart processing for ampersands and angle brackets that need to be encoded. + */ +showdown.subParser('encodeAmpsAndAngles', function (text) { + 'use strict'; + // Ampersand-encoding based entirely on Nat Irons's Amputator MT plugin: + // http://bumppo.net/projects/amputator/ + text = text.replace(/&(?!#?[xX]?(?:[0-9a-fA-F]+|\w+);)/g, '&'); + + // Encode naked <'s + text = text.replace(/<(?![a-z\/?\$!])/gi, '<'); + + return text; +}); + +/** + * Returns the string, with after processing the following backslash escape sequences. + * + * attacklab: The polite way to do this is with the new escapeCharacters() function: + * + * text = escapeCharacters(text,"\\",true); + * text = escapeCharacters(text,"`*_{}[]()>#+-.!",true); + * + * ...but we're sidestepping its use of the (slow) RegExp constructor + * as an optimization for Firefox. This function gets called a LOT. + */ +showdown.subParser('encodeBackslashEscapes', function (text) { + 'use strict'; + text = text.replace(/\\(\\)/g, showdown.helper.escapeCharactersCallback); + text = text.replace(/\\([`*_{}\[\]()>#+-.!])/g, showdown.helper.escapeCharactersCallback); + return text; +}); + +/** + * Encode/escape certain characters inside Markdown code runs. + * The point is that in code, these characters are literals, + * and lose their special Markdown meanings. + */ +showdown.subParser('encodeCode', function (text) { + 'use strict'; + + // Encode all ampersands; HTML entities are not + // entities within a Markdown code span. + text = text.replace(/&/g, '&'); + + // Do the angle bracket song and dance: + text = text.replace(//g, '>'); + + // Now, escape characters that are magic in Markdown: + text = showdown.helper.escapeCharacters(text, '*_{}[]\\', false); + + // jj the line above breaks this: + //--- + //* Item + // 1. Subitem + // special char: * + // --- + + return text; +}); + +/** + * Input: an email address, e.g. "foo@example.com" + * + * Output: the email address as a mailto link, with each character + * of the address encoded as either a decimal or hex entity, in + * the hopes of foiling most address harvesting spam bots. E.g.: + * + * foo + * @example.com + * + * Based on a filter by Matthew Wickline, posted to the BBEdit-Talk + * mailing list: + * + */ +showdown.subParser('encodeEmailAddress', function (addr) { + 'use strict'; + + var encode = [ + function (ch) { + return '&#' + ch.charCodeAt(0) + ';'; + }, + function (ch) { + return '&#x' + ch.charCodeAt(0).toString(16) + ';'; + }, + function (ch) { + return ch; + } + ]; + + addr = 'mailto:' + addr; + + addr = addr.replace(/./g, function (ch) { + if (ch === '@') { + // this *must* be encoded. I insist. + ch = encode[Math.floor(Math.random() * 2)](ch); + } else if (ch !== ':') { + // leave ':' alone (to spot mailto: later) + var r = Math.random(); + // roughly 10% raw, 45% hex, 45% dec + ch = ( + r > 0.9 ? encode[2](ch) : r > 0.45 ? encode[1](ch) : encode[0](ch) + ); + } + return ch; + }); + + addr = '' + addr + ''; + addr = addr.replace(/">.+:/g, '">'); // strip the mailto: from the visible part + + return addr; +}); + +/** + * Within tags -- meaning between < and > -- encode [\ ` * _] so they + * don't conflict with their use in Markdown for code, italics and strong. + */ +showdown.subParser('escapeSpecialCharsWithinTagAttributes', function (text) { + 'use strict'; + + // Build a regex to find HTML tags and comments. See Friedl's + // "Mastering Regular Expressions", 2nd Ed., pp. 200-201. + var regex = /(<[a-z\/!$]("[^"]*"|'[^']*'|[^'">])*>|)/gi; + + text = text.replace(regex, function (wholeMatch) { + var tag = wholeMatch.replace(/(.)<\/?code>(?=.)/g, '$1`'); + tag = showdown.helper.escapeCharacters(tag, '\\`*_', false); + return tag; + }); + + return text; +}); + +/** + * Handle github codeblocks prior to running HashHTML so that + * HTML contained within the codeblock gets escaped properly + * Example: + * ```ruby + * def hello_world(x) + * puts "Hello, #{x}" + * end + * ``` + */ +showdown.subParser('githubCodeBlocks', function (text, options, globals) { + 'use strict'; + + // early exit if option is not enabled + if (!options.ghCodeBlocks) { + return text; + } + + text = globals.converter._dispatch('githubCodeBlocks.before', text, options, globals); + + text += '~0'; + + text = text.replace(/(?:^|\n)```(.*)\n([\s\S]*?)\n```/g, function (wholeMatch, language, codeblock) { + var end = (options.omitExtraWLInCodeBlocks) ? '' : '\n'; + + // First parse the github code block + codeblock = showdown.subParser('encodeCode')(codeblock); + codeblock = showdown.subParser('detab')(codeblock); + codeblock = codeblock.replace(/^\n+/g, ''); // trim leading newlines + codeblock = codeblock.replace(/\n+$/g, ''); // trim trailing whitespace + + codeblock = '
' + codeblock + end + '
'; + + codeblock = showdown.subParser('hashBlock')(codeblock, options, globals); + + // Since GHCodeblocks can be false positives, we need to + // store the primitive text and the parsed text in a global var, + // and then return a token + return '\n\n~G' + (globals.ghCodeBlocks.push({text: wholeMatch, codeblock: codeblock}) - 1) + 'G\n\n'; + }); + + // attacklab: strip sentinel + text = text.replace(/~0/, ''); + + return globals.converter._dispatch('githubCodeBlocks.after', text, options, globals); +}); + +showdown.subParser('hashBlock', function (text, options, globals) { + 'use strict'; + text = text.replace(/(^\n+|\n+$)/g, ''); + return '\n\n~K' + (globals.gHtmlBlocks.push(text) - 1) + 'K\n\n'; +}); + +showdown.subParser('hashElement', function (text, options, globals) { + 'use strict'; + + return function (wholeMatch, m1) { + var blockText = m1; + + // Undo double lines + blockText = blockText.replace(/\n\n/g, '\n'); + blockText = blockText.replace(/^\n/, ''); + + // strip trailing blank lines + blockText = blockText.replace(/\n+$/g, ''); + + // Replace the element text with a marker ("~KxK" where x is its key) + blockText = '\n\n~K' + (globals.gHtmlBlocks.push(blockText) - 1) + 'K\n\n'; + + return blockText; + }; +}); + +showdown.subParser('hashHTMLBlocks', function (text, options, globals) { + 'use strict'; + + var blockTags = [ + 'pre', + 'div', + 'h1', + 'h2', + 'h3', + 'h4', + 'h5', + 'h6', + 'blockquote', + 'table', + 'dl', + 'ol', + 'ul', + 'script', + 'noscript', + 'form', + 'fieldset', + 'iframe', + 'math', + 'style', + 'section', + 'header', + 'footer', + 'nav', + 'article', + 'aside', + 'address', + 'audio', + 'canvas', + 'figure', + 'hgroup', + 'output', + 'video', + 'p' + ], + repFunc = function (wholeMatch, match, left, right) { + var txt = wholeMatch; + // check if this html element is marked as markdown + // if so, it's contents should be parsed as markdown + if (left.search(/\bmarkdown\b/) !== -1) { + txt = left + globals.converter.makeHtml(match) + right; + } + return '\n\n~K' + (globals.gHtmlBlocks.push(txt) - 1) + 'K\n\n'; + }; + + for (var i = 0; i < blockTags.length; ++i) { + text = showdown.helper.replaceRecursiveRegExp(text, repFunc, '^(?: |\\t){0,3}<' + blockTags[i] + '\\b[^>]*>', '', 'gim'); + } + + // HR SPECIAL CASE + text = text.replace(/(\n[ ]{0,3}(<(hr)\b([^<>])*?\/?>)[ \t]*(?=\n{2,}))/g, + showdown.subParser('hashElement')(text, options, globals)); + + // Special case for standalone HTML comments: + text = text.replace(/()/g, + showdown.subParser('hashElement')(text, options, globals)); + + // PHP and ASP-style processor instructions ( and <%...%>) + text = text.replace(/(?:\n\n)([ ]{0,3}(?:<([?%])[^\r]*?\2>)[ \t]*(?=\n{2,}))/g, + showdown.subParser('hashElement')(text, options, globals)); + return text; +}); + +/** + * Hash span elements that should not be parsed as markdown + */ +showdown.subParser('hashHTMLSpans', function (text, config, globals) { + 'use strict'; + + var matches = showdown.helper.matchRecursiveRegExp(text, ']*>', '', 'gi'); + + for (var i = 0; i < matches.length; ++i) { + text = text.replace(matches[i][0], '~L' + (globals.gHtmlSpans.push(matches[i][0]) - 1) + 'L'); + } + return text; +}); + +/** + * Unhash HTML spans + */ +showdown.subParser('unhashHTMLSpans', function (text, config, globals) { + 'use strict'; + + for (var i = 0; i < globals.gHtmlSpans.length; ++i) { + text = text.replace('~L' + i + 'L', globals.gHtmlSpans[i]); + } + + return text; +}); + +/** + * Hash span elements that should not be parsed as markdown + */ +showdown.subParser('hashPreCodeTags', function (text, config, globals) { + 'use strict'; + + var repFunc = function (wholeMatch, match, left, right) { + // encode html entities + var codeblock = left + showdown.subParser('encodeCode')(match) + right; + return '\n\n~G' + (globals.ghCodeBlocks.push({text: wholeMatch, codeblock: codeblock}) - 1) + 'G\n\n'; + }; + + text = showdown.helper.replaceRecursiveRegExp(text, repFunc, '^(?: |\\t){0,3}]*>\\s*]*>', '^(?: |\\t){0,3}\\s*
', 'gim'); + return text; +}); + +showdown.subParser('headers', function (text, options, globals) { + 'use strict'; + + text = globals.converter._dispatch('headers.before', text, options, globals); + + var prefixHeader = options.prefixHeaderId, + headerLevelStart = (isNaN(parseInt(options.headerLevelStart))) ? 1 : parseInt(options.headerLevelStart), + + // Set text-style headers: + // Header 1 + // ======== + // + // Header 2 + // -------- + // + setextRegexH1 = (options.smoothLivePreview) ? /^(.+)[ \t]*\n={2,}[ \t]*\n+/gm : /^(.+)[ \t]*\n=+[ \t]*\n+/gm, + setextRegexH2 = (options.smoothLivePreview) ? /^(.+)[ \t]*\n-{2,}[ \t]*\n+/gm : /^(.+)[ \t]*\n-+[ \t]*\n+/gm; + + text = text.replace(setextRegexH1, function (wholeMatch, m1) { + + var spanGamut = showdown.subParser('spanGamut')(m1, options, globals), + hID = (options.noHeaderId) ? '' : ' id="' + headerId(m1) + '"', + hLevel = headerLevelStart, + hashBlock = '' + spanGamut + ''; + return showdown.subParser('hashBlock')(hashBlock, options, globals); + }); + + text = text.replace(setextRegexH2, function (matchFound, m1) { + var spanGamut = showdown.subParser('spanGamut')(m1, options, globals), + hID = (options.noHeaderId) ? '' : ' id="' + headerId(m1) + '"', + hLevel = headerLevelStart + 1, + hashBlock = '' + spanGamut + ''; + return showdown.subParser('hashBlock')(hashBlock, options, globals); + }); + + // atx-style headers: + // # Header 1 + // ## Header 2 + // ## Header 2 with closing hashes ## + // ... + // ###### Header 6 + // + text = text.replace(/^(#{1,6})[ \t]*(.+?)[ \t]*#*\n+/gm, function (wholeMatch, m1, m2) { + var span = showdown.subParser('spanGamut')(m2, options, globals), + hID = (options.noHeaderId) ? '' : ' id="' + headerId(m2) + '"', + hLevel = headerLevelStart - 1 + m1.length, + header = '' + span + ''; + + return showdown.subParser('hashBlock')(header, options, globals); + }); + + function headerId(m) { + var title, escapedId = m.replace(/[^\w]/g, '').toLowerCase(); + + if (globals.hashLinkCounts[escapedId]) { + title = escapedId + '-' + (globals.hashLinkCounts[escapedId]++); + } else { + title = escapedId; + globals.hashLinkCounts[escapedId] = 1; + } + + // Prefix id to prevent causing inadvertent pre-existing style matches. + if (prefixHeader === true) { + prefixHeader = 'section'; + } + + if (showdown.helper.isString(prefixHeader)) { + return prefixHeader + title; + } + return title; + } + + text = globals.converter._dispatch('headers.after', text, options, globals); + return text; +}); + +/** + * Turn Markdown image shortcuts into tags. + */ +showdown.subParser('images', function (text, options, globals) { + 'use strict'; + + text = globals.converter._dispatch('images.before', text, options, globals); + + var inlineRegExp = /!\[(.*?)]\s?\([ \t]*()?(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*(?:(['"])(.*?)\6[ \t]*)?\)/g, + referenceRegExp = /!\[([^\]]*?)] ?(?:\n *)?\[(.*?)]()()()()()/g; + + function writeImageTag (wholeMatch, altText, linkId, url, width, height, m5, title) { + + var gUrls = globals.gUrls, + gTitles = globals.gTitles, + gDims = globals.gDimensions; + + linkId = linkId.toLowerCase(); + + if (!title) { + title = ''; + } + + if (url === '' || url === null) { + if (linkId === '' || linkId === null) { + // lower-case and turn embedded newlines into spaces + linkId = altText.toLowerCase().replace(/ ?\n/g, ' '); + } + url = '#' + linkId; + + if (!showdown.helper.isUndefined(gUrls[linkId])) { + url = gUrls[linkId]; + if (!showdown.helper.isUndefined(gTitles[linkId])) { + title = gTitles[linkId]; + } + if (!showdown.helper.isUndefined(gDims[linkId])) { + width = gDims[linkId].width; + height = gDims[linkId].height; + } + } else { + return wholeMatch; + } + } + + altText = altText.replace(/"/g, '"'); + altText = showdown.helper.escapeCharacters(altText, '*_', false); + url = showdown.helper.escapeCharacters(url, '*_', false); + var result = '' + altText + 'x "optional title") + text = text.replace(inlineRegExp, writeImageTag); + + text = globals.converter._dispatch('images.after', text, options, globals); + return text; +}); + +showdown.subParser('italicsAndBold', function (text, options, globals) { + 'use strict'; + + text = globals.converter._dispatch('italicsAndBold.before', text, options, globals); + + if (options.literalMidWordUnderscores) { + //underscores + // Since we are consuming a \s character, we need to add it + text = text.replace(/(^|\s|>|\b)__(?=\S)([\s\S]+?)__(?=\b|<|\s|$)/gm, '$1$2'); + text = text.replace(/(^|\s|>|\b)_(?=\S)([\s\S]+?)_(?=\b|<|\s|$)/gm, '$1$2'); + //asterisks + text = text.replace(/(\*\*)(?=\S)([^\r]*?\S[*]*)\1/g, '$2'); + text = text.replace(/(\*)(?=\S)([^\r]*?\S)\1/g, '$2'); + + } else { + // must go first: + text = text.replace(/(\*\*|__)(?=\S)([^\r]*?\S[*_]*)\1/g, '$2'); + text = text.replace(/(\*|_)(?=\S)([^\r]*?\S)\1/g, '$2'); + } + + text = globals.converter._dispatch('italicsAndBold.after', text, options, globals); + return text; +}); + +/** + * Form HTML ordered (numbered) and unordered (bulleted) lists. + */ +showdown.subParser('lists', function (text, options, globals) { + 'use strict'; + + text = globals.converter._dispatch('lists.before', text, options, globals); + /** + * Process the contents of a single ordered or unordered list, splitting it + * into individual list items. + * @param {string} listStr + * @param {boolean} trimTrailing + * @returns {string} + */ + function processListItems (listStr, trimTrailing) { + // The $g_list_level global keeps track of when we're inside a list. + // Each time we enter a list, we increment it; when we leave a list, + // we decrement. If it's zero, we're not in a list anymore. + // + // We do this because when we're not inside a list, we want to treat + // something like this: + // + // I recommend upgrading to version + // 8. Oops, now this line is treated + // as a sub-list. + // + // As a single paragraph, despite the fact that the second line starts + // with a digit-period-space sequence. + // + // Whereas when we're inside a list (or sub-list), that line will be + // treated as the start of a sub-list. What a kludge, huh? This is + // an aspect of Markdown's syntax that's hard to parse perfectly + // without resorting to mind-reading. Perhaps the solution is to + // change the syntax rules such that sub-lists must start with a + // starting cardinal number; e.g. "1." or "a.". + globals.gListLevel++; + + // trim trailing blank lines: + listStr = listStr.replace(/\n{2,}$/, '\n'); + + // attacklab: add sentinel to emulate \z + listStr += '~0'; + + var rgx = /(\n)?(^[ \t]*)([*+-]|\d+[.])[ \t]+((\[(x|X| )?])?[ \t]*[^\r]+?(\n{1,2}))(?=\n*(~0|\2([*+-]|\d+[.])[ \t]+))/gm, + isParagraphed = (/\n[ \t]*\n(?!~0)/.test(listStr)); + + listStr = listStr.replace(rgx, function (wholeMatch, m1, m2, m3, m4, taskbtn, checked) { + checked = (checked && checked.trim() !== ''); + var item = showdown.subParser('outdent')(m4, options, globals), + bulletStyle = ''; + + // Support for github tasklists + if (taskbtn && options.tasklists) { + bulletStyle = ' class="task-list-item" style="list-style-type: none;"'; + item = item.replace(/^[ \t]*\[(x|X| )?]/m, function () { + var otp = ' -1)) { + item = showdown.subParser('githubCodeBlocks')(item, options, globals); + item = showdown.subParser('blockGamut')(item, options, globals); + } else { + // Recursion for sub-lists: + item = showdown.subParser('lists')(item, options, globals); + item = item.replace(/\n$/, ''); // chomp(item) + if (isParagraphed) { + item = showdown.subParser('paragraphs')(item, options, globals); + } else { + item = showdown.subParser('spanGamut')(item, options, globals); + } + } + item = '\n' + item + '\n'; + return item; + }); + + // attacklab: strip sentinel + listStr = listStr.replace(/~0/g, ''); + + globals.gListLevel--; + + if (trimTrailing) { + listStr = listStr.replace(/\s+$/, ''); + } + + return listStr; + } + + /** + * Check and parse consecutive lists (better fix for issue #142) + * @param {string} list + * @param {string} listType + * @param {boolean} trimTrailing + * @returns {string} + */ + function parseConsecutiveLists(list, listType, trimTrailing) { + // check if we caught 2 or more consecutive lists by mistake + // we use the counterRgx, meaning if listType is UL we look for UL and vice versa + var counterRxg = (listType === 'ul') ? /^ {0,2}\d+\.[ \t]/gm : /^ {0,2}[*+-][ \t]/gm, + subLists = [], + result = ''; + + if (list.search(counterRxg) !== -1) { + (function parseCL(txt) { + var pos = txt.search(counterRxg); + if (pos !== -1) { + // slice + result += '\n\n<' + listType + '>' + processListItems(txt.slice(0, pos), !!trimTrailing) + '\n\n'; + + // invert counterType and listType + listType = (listType === 'ul') ? 'ol' : 'ul'; + counterRxg = (listType === 'ul') ? /^ {0,2}\d+\.[ \t]/gm : /^ {0,2}[*+-][ \t]/gm; + + //recurse + parseCL(txt.slice(pos)); + } else { + result += '\n\n<' + listType + '>' + processListItems(txt, !!trimTrailing) + '\n\n'; + } + })(list); + for (var i = 0; i < subLists.length; ++i) { + + } + } else { + result = '\n\n<' + listType + '>' + processListItems(list, !!trimTrailing) + '\n\n'; + } + + return result; + } + + // attacklab: add sentinel to hack around khtml/safari bug: + // http://bugs.webkit.org/show_bug.cgi?id=11231 + text += '~0'; + + // Re-usable pattern to match any entire ul or ol list: + var wholeList = /^(([ ]{0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(~0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/gm; + + if (globals.gListLevel) { + text = text.replace(wholeList, function (wholeMatch, list, m2) { + var listType = (m2.search(/[*+-]/g) > -1) ? 'ul' : 'ol'; + return parseConsecutiveLists(list, listType, true); + }); + } else { + wholeList = /(\n\n|^\n?)(([ ]{0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(~0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/gm; + //wholeList = /(\n\n|^\n?)( {0,3}([*+-]|\d+\.)[ \t]+[\s\S]+?)(?=(~0)|(\n\n(?!\t| {2,}| {0,3}([*+-]|\d+\.)[ \t])))/g; + text = text.replace(wholeList, function (wholeMatch, m1, list, m3) { + + var listType = (m3.search(/[*+-]/g) > -1) ? 'ul' : 'ol'; + return parseConsecutiveLists(list, listType); + }); + } + + // attacklab: strip sentinel + text = text.replace(/~0/, ''); + + text = globals.converter._dispatch('lists.after', text, options, globals); + return text; +}); + +/** + * Remove one level of line-leading tabs or spaces + */ +showdown.subParser('outdent', function (text) { + 'use strict'; + + // attacklab: hack around Konqueror 3.5.4 bug: + // "----------bug".replace(/^-/g,"") == "bug" + text = text.replace(/^(\t|[ ]{1,4})/gm, '~0'); // attacklab: g_tab_width + + // attacklab: clean up hack + text = text.replace(/~0/g, ''); + + return text; +}); + +/** + * + */ +showdown.subParser('paragraphs', function (text, options, globals) { + 'use strict'; + + text = globals.converter._dispatch('paragraphs.before', text, options, globals); + // Strip leading and trailing lines: + text = text.replace(/^\n+/g, ''); + text = text.replace(/\n+$/g, ''); + + var grafs = text.split(/\n{2,}/g), + grafsOut = [], + end = grafs.length; // Wrap

tags + + for (var i = 0; i < end; i++) { + var str = grafs[i]; + // if this is an HTML marker, copy it + if (str.search(/~(K|G)(\d+)\1/g) >= 0) { + grafsOut.push(str); + } else { + str = showdown.subParser('spanGamut')(str, options, globals); + str = str.replace(/^([ \t]*)/g, '

'); + str += '

'; + grafsOut.push(str); + } + } + + /** Unhashify HTML blocks */ + end = grafsOut.length; + for (i = 0; i < end; i++) { + var blockText = '', + grafsOutIt = grafsOut[i], + codeFlag = false; + // if this is a marker for an html block... + while (grafsOutIt.search(/~(K|G)(\d+)\1/) >= 0) { + var delim = RegExp.$1, + num = RegExp.$2; + + if (delim === 'K') { + blockText = globals.gHtmlBlocks[num]; + } else { + // we need to check if ghBlock is a false positive + if (codeFlag) { + // use encoded version of all text + blockText = showdown.subParser('encodeCode')(globals.ghCodeBlocks[num].text); + } else { + blockText = globals.ghCodeBlocks[num].codeblock; + } + } + blockText = blockText.replace(/\$/g, '$$$$'); // Escape any dollar signs + + grafsOutIt = grafsOutIt.replace(/(\n\n)?~(K|G)\d+\2(\n\n)?/, blockText); + // Check if grafsOutIt is a pre->code + if (/^]*>\s*]*>/.test(grafsOutIt)) { + codeFlag = true; + } + } + grafsOut[i] = grafsOutIt; + } + text = grafsOut.join('\n\n'); + // Strip leading and trailing lines: + text = text.replace(/^\n+/g, ''); + text = text.replace(/\n+$/g, ''); + return globals.converter._dispatch('paragraphs.after', text, options, globals); +}); + +/** + * Run extension + */ +showdown.subParser('runExtension', function (ext, text, options, globals) { + 'use strict'; + + if (ext.filter) { + text = ext.filter(text, globals.converter, options); + + } else if (ext.regex) { + // TODO remove this when old extension loading mechanism is deprecated + var re = ext.regex; + if (!re instanceof RegExp) { + re = new RegExp(re, 'g'); + } + text = text.replace(re, ext.replace); + } + + return text; +}); + +/** + * These are all the transformations that occur *within* block-level + * tags like paragraphs, headers, and list items. + */ +showdown.subParser('spanGamut', function (text, options, globals) { + 'use strict'; + + text = globals.converter._dispatch('spanGamut.before', text, options, globals); + text = showdown.subParser('codeSpans')(text, options, globals); + text = showdown.subParser('escapeSpecialCharsWithinTagAttributes')(text, options, globals); + text = showdown.subParser('encodeBackslashEscapes')(text, options, globals); + + // Process anchor and image tags. Images must come first, + // because ![foo][f] looks like an anchor. + text = showdown.subParser('images')(text, options, globals); + text = showdown.subParser('anchors')(text, options, globals); + + // Make links out of things like `` + // Must come after _DoAnchors(), because you can use < and > + // delimiters in inline links like [this](). + text = showdown.subParser('autoLinks')(text, options, globals); + text = showdown.subParser('encodeAmpsAndAngles')(text, options, globals); + text = showdown.subParser('italicsAndBold')(text, options, globals); + text = showdown.subParser('strikethrough')(text, options, globals); + + // Do hard breaks: + text = text.replace(/ +\n/g, '
\n'); + + text = globals.converter._dispatch('spanGamut.after', text, options, globals); + return text; +}); + +showdown.subParser('strikethrough', function (text, options, globals) { + 'use strict'; + + if (options.strikethrough) { + text = globals.converter._dispatch('strikethrough.before', text, options, globals); + text = text.replace(/(?:~T){2}([\s\S]+?)(?:~T){2}/g, '$1'); + text = globals.converter._dispatch('strikethrough.after', text, options, globals); + } + + return text; +}); + +/** + * Strip any lines consisting only of spaces and tabs. + * This makes subsequent regexs easier to write, because we can + * match consecutive blank lines with /\n+/ instead of something + * contorted like /[ \t]*\n+/ + */ +showdown.subParser('stripBlankLines', function (text) { + 'use strict'; + return text.replace(/^[ \t]+$/mg, ''); +}); + +/** + * Strips link definitions from text, stores the URLs and titles in + * hash references. + * Link defs are in the form: ^[id]: url "optional title" + * + * ^[ ]{0,3}\[(.+)\]: // id = $1 attacklab: g_tab_width - 1 + * [ \t]* + * \n? // maybe *one* newline + * [ \t]* + * ? // url = $2 + * [ \t]* + * \n? // maybe one newline + * [ \t]* + * (?: + * (\n*) // any lines skipped = $3 attacklab: lookbehind removed + * ["(] + * (.+?) // title = $4 + * [")] + * [ \t]* + * )? // title is optional + * (?:\n+|$) + * /gm, + * function(){...}); + * + */ +showdown.subParser('stripLinkDefinitions', function (text, options, globals) { + 'use strict'; + + var regex = /^ {0,3}\[(.+)]:[ \t]*\n?[ \t]*?(?: =([*\d]+[A-Za-z%]{0,4})x([*\d]+[A-Za-z%]{0,4}))?[ \t]*\n?[ \t]*(?:(\n*)["|'(](.+?)["|')][ \t]*)?(?:\n+|(?=~0))/gm; + + // attacklab: sentinel workarounds for lack of \A and \Z, safari\khtml bug + text += '~0'; + + text = text.replace(regex, function (wholeMatch, linkId, url, width, height, blankLines, title) { + linkId = linkId.toLowerCase(); + globals.gUrls[linkId] = showdown.subParser('encodeAmpsAndAngles')(url); // Link IDs are case-insensitive + + if (blankLines) { + // Oops, found blank lines, so it's not a title. + // Put back the parenthetical statement we stole. + return blankLines + title; + + } else { + if (title) { + globals.gTitles[linkId] = title.replace(/"|'/g, '"'); + } + if (options.parseImgDimensions && width && height) { + globals.gDimensions[linkId] = { + width: width, + height: height + }; + } + } + // Completely remove the definition from the text + return ''; + }); + + // attacklab: strip sentinel + text = text.replace(/~0/, ''); + + return text; +}); + +showdown.subParser('tables', function (text, options, globals) { + 'use strict'; + + if (!options.tables) { + return text; + } + + var tableRgx = /^[ \t]{0,3}\|?.+\|.+\n[ \t]{0,3}\|?[ \t]*:?[ \t]*(?:-|=){2,}[ \t]*:?[ \t]*\|[ \t]*:?[ \t]*(?:-|=){2,}[\s\S]+?(?:\n\n|~0)/gm; + + function parseStyles(sLine) { + if (/^:[ \t]*--*$/.test(sLine)) { + return ' style="text-align:left;"'; + } else if (/^--*[ \t]*:[ \t]*$/.test(sLine)) { + return ' style="text-align:right;"'; + } else if (/^:[ \t]*--*[ \t]*:$/.test(sLine)) { + return ' style="text-align:center;"'; + } else { + return ''; + } + } + + function parseHeaders(header, style) { + var id = ''; + header = header.trim(); + if (options.tableHeaderId) { + id = ' id="' + header.replace(/ /g, '_').toLowerCase() + '"'; + } + header = showdown.subParser('spanGamut')(header, options, globals); + + return '' + header + '\n'; + } + + function parseCells(cell, style) { + var subText = showdown.subParser('spanGamut')(cell, options, globals); + return '' + subText + '\n'; + } + + function buildTable(headers, cells) { + var tb = '\n\n\n', + tblLgn = headers.length; + + for (var i = 0; i < tblLgn; ++i) { + tb += headers[i]; + } + tb += '\n\n\n'; + + for (i = 0; i < cells.length; ++i) { + tb += '\n'; + for (var ii = 0; ii < tblLgn; ++ii) { + tb += cells[i][ii]; + } + tb += '\n'; + } + tb += '\n
\n'; + return tb; + } + + text = globals.converter._dispatch('tables.before', text, options, globals); + + text = text.replace(tableRgx, function (rawTable) { + + var i, tableLines = rawTable.split('\n'); + + // strip wrong first and last column if wrapped tables are used + for (i = 0; i < tableLines.length; ++i) { + if (/^[ \t]{0,3}\|/.test(tableLines[i])) { + tableLines[i] = tableLines[i].replace(/^[ \t]{0,3}\|/, ''); + } + if (/\|[ \t]*$/.test(tableLines[i])) { + tableLines[i] = tableLines[i].replace(/\|[ \t]*$/, ''); + } + } + + var rawHeaders = tableLines[0].split('|').map(function (s) { return s.trim();}), + rawStyles = tableLines[1].split('|').map(function (s) { return s.trim();}), + rawCells = [], + headers = [], + styles = [], + cells = []; + + tableLines.shift(); + tableLines.shift(); + + for (i = 0; i < tableLines.length; ++i) { + if (tableLines[i].trim() === '') { + continue; + } + rawCells.push( + tableLines[i] + .split('|') + .map(function (s) { + return s.trim(); + }) + ); + } + + if (rawHeaders.length < rawStyles.length) { + return rawTable; + } + + for (i = 0; i < rawStyles.length; ++i) { + styles.push(parseStyles(rawStyles[i])); + } + + for (i = 0; i < rawHeaders.length; ++i) { + if (showdown.helper.isUndefined(styles[i])) { + styles[i] = ''; + } + headers.push(parseHeaders(rawHeaders[i], styles[i])); + } + + for (i = 0; i < rawCells.length; ++i) { + var row = []; + for (var ii = 0; ii < headers.length; ++ii) { + if (showdown.helper.isUndefined(rawCells[i][ii])) { + + } + row.push(parseCells(rawCells[i][ii], styles[ii])); + } + cells.push(row); + } + + return buildTable(headers, cells); + }); + + text = globals.converter._dispatch('tables.after', text, options, globals); + + return text; +}); + +/** + * Swap back in all the special characters we've hidden. + */ +showdown.subParser('unescapeSpecialChars', function (text) { + 'use strict'; + + text = text.replace(/~E(\d+)E/g, function (wholeMatch, m1) { + var charCodeToReplace = parseInt(m1); + return String.fromCharCode(charCodeToReplace); + }); + return text; +}); +module.exports = showdown; diff --git a/utils/wxParse/wxDiscode.js b/utils/wxParse/wxDiscode.js new file mode 100644 index 0000000..fca29bb --- /dev/null +++ b/utils/wxParse/wxDiscode.js @@ -0,0 +1,207 @@ +// HTML 支持的数学符号 +function strNumDiscode(str){ + str = str.replace(/∀/g, '∀'); + str = str.replace(/∂/g, '∂'); + str = str.replace(/&exists;/g, '∃'); + str = str.replace(/∅/g, '∅'); + str = str.replace(/∇/g, '∇'); + str = str.replace(/∈/g, '∈'); + str = str.replace(/∉/g, '∉'); + str = str.replace(/∋/g, '∋'); + str = str.replace(/∏/g, '∏'); + str = str.replace(/∑/g, '∑'); + str = str.replace(/−/g, '−'); + str = str.replace(/∗/g, '∗'); + str = str.replace(/√/g, '√'); + str = str.replace(/∝/g, '∝'); + str = str.replace(/∞/g, '∞'); + str = str.replace(/∠/g, '∠'); + str = str.replace(/∧/g, '∧'); + str = str.replace(/∨/g, '∨'); + str = str.replace(/∩/g, '∩'); + str = str.replace(/∩/g, '∪'); + str = str.replace(/∫/g, '∫'); + str = str.replace(/∴/g, '∴'); + str = str.replace(/∼/g, '∼'); + str = str.replace(/≅/g, '≅'); + str = str.replace(/≈/g, '≈'); + str = str.replace(/≠/g, '≠'); + str = str.replace(/≤/g, '≤'); + str = str.replace(/≥/g, '≥'); + str = str.replace(/⊂/g, '⊂'); + str = str.replace(/⊃/g, '⊃'); + str = str.replace(/⊄/g, '⊄'); + str = str.replace(/⊆/g, '⊆'); + str = str.replace(/⊇/g, '⊇'); + str = str.replace(/⊕/g, '⊕'); + str = str.replace(/⊗/g, '⊗'); + str = str.replace(/⊥/g, '⊥'); + str = str.replace(/⋅/g, '⋅'); + return str; +} + +//HTML 支持的希腊字母 +function strGreeceDiscode(str){ + str = str.replace(/Α/g, 'Α'); + str = str.replace(/Β/g, 'Β'); + str = str.replace(/Γ/g, 'Γ'); + str = str.replace(/Δ/g, 'Δ'); + str = str.replace(/Ε/g, 'Ε'); + str = str.replace(/Ζ/g, 'Ζ'); + str = str.replace(/Η/g, 'Η'); + str = str.replace(/Θ/g, 'Θ'); + str = str.replace(/Ι/g, 'Ι'); + str = str.replace(/Κ/g, 'Κ'); + str = str.replace(/Λ/g, 'Λ'); + str = str.replace(/Μ/g, 'Μ'); + str = str.replace(/Ν/g, 'Ν'); + str = str.replace(/Ξ/g, 'Ν'); + str = str.replace(/Ο/g, 'Ο'); + str = str.replace(/Π/g, 'Π'); + str = str.replace(/Ρ/g, 'Ρ'); + str = str.replace(/Σ/g, 'Σ'); + str = str.replace(/Τ/g, 'Τ'); + str = str.replace(/Υ/g, 'Υ'); + str = str.replace(/Φ/g, 'Φ'); + str = str.replace(/Χ/g, 'Χ'); + str = str.replace(/Ψ/g, 'Ψ'); + str = str.replace(/Ω/g, 'Ω'); + + str = str.replace(/α/g, 'α'); + str = str.replace(/β/g, 'β'); + str = str.replace(/γ/g, 'γ'); + str = str.replace(/δ/g, 'δ'); + str = str.replace(/ε/g, 'ε'); + str = str.replace(/ζ/g, 'ζ'); + str = str.replace(/η/g, 'η'); + str = str.replace(/θ/g, 'θ'); + str = str.replace(/ι/g, 'ι'); + str = str.replace(/κ/g, 'κ'); + str = str.replace(/λ/g, 'λ'); + str = str.replace(/μ/g, 'μ'); + str = str.replace(/ν/g, 'ν'); + str = str.replace(/ξ/g, 'ξ'); + str = str.replace(/ο/g, 'ο'); + str = str.replace(/π/g, 'π'); + str = str.replace(/ρ/g, 'ρ'); + str = str.replace(/ς/g, 'ς'); + str = str.replace(/σ/g, 'σ'); + str = str.replace(/τ/g, 'τ'); + str = str.replace(/υ/g, 'υ'); + str = str.replace(/φ/g, 'φ'); + str = str.replace(/χ/g, 'χ'); + str = str.replace(/ψ/g, 'ψ'); + str = str.replace(/ω/g, 'ω'); + str = str.replace(/ϑ/g, 'ϑ'); + str = str.replace(/ϒ/g, 'ϒ'); + str = str.replace(/ϖ/g, 'ϖ'); + str = str.replace(/·/g, '·'); + return str; +} + +// + +function strcharacterDiscode(str){ + // 加入常用解析 + str = str.replace(/ /g, ' '); + str = str.replace(/"/g, "'"); + str = str.replace(/&/g, '&'); + // str = str.replace(/</g, '‹'); + // str = str.replace(/>/g, '›'); + + str = str.replace(/</g, '<'); + str = str.replace(/>/g, '>'); + str = str.replace(/•/g, '•'); + + return str; +} + +// HTML 支持的其他实体 +function strOtherDiscode(str){ + str = str.replace(/Œ/g, 'Œ'); + str = str.replace(/œ/g, 'œ'); + str = str.replace(/Š/g, 'Š'); + str = str.replace(/š/g, 'š'); + str = str.replace(/Ÿ/g, 'Ÿ'); + str = str.replace(/ƒ/g, 'ƒ'); + str = str.replace(/ˆ/g, 'ˆ'); + str = str.replace(/˜/g, '˜'); + str = str.replace(/ /g, ''); + str = str.replace(/ /g, ''); + str = str.replace(/ /g, ''); + str = str.replace(/‌/g, ''); + str = str.replace(/‍/g, ''); + str = str.replace(/‎/g, ''); + str = str.replace(/‏/g, ''); + str = str.replace(/–/g, '–'); + str = str.replace(/—/g, '—'); + str = str.replace(/‘/g, '‘'); + str = str.replace(/’/g, '’'); + str = str.replace(/‚/g, '‚'); + str = str.replace(/“/g, '“'); + str = str.replace(/”/g, '”'); + str = str.replace(/„/g, '„'); + str = str.replace(/†/g, '†'); + str = str.replace(/‡/g, '‡'); + str = str.replace(/•/g, '•'); + str = str.replace(/…/g, '…'); + str = str.replace(/‰/g, '‰'); + str = str.replace(/′/g, '′'); + str = str.replace(/″/g, '″'); + str = str.replace(/‹/g, '‹'); + str = str.replace(/›/g, '›'); + str = str.replace(/‾/g, '‾'); + str = str.replace(/€/g, '€'); + str = str.replace(/™/g, '™'); + + str = str.replace(/←/g, '←'); + str = str.replace(/↑/g, '↑'); + str = str.replace(/→/g, '→'); + str = str.replace(/↓/g, '↓'); + str = str.replace(/↔/g, '↔'); + str = str.replace(/↵/g, '↵'); + str = str.replace(/⌈/g, '⌈'); + str = str.replace(/⌉/g, '⌉'); + + str = str.replace(/⌊/g, '⌊'); + str = str.replace(/⌋/g, '⌋'); + str = str.replace(/◊/g, '◊'); + str = str.replace(/♠/g, '♠'); + str = str.replace(/♣/g, '♣'); + str = str.replace(/♥/g, '♥'); + + str = str.replace(/♦/g, '♦'); + str = str.replace(/'/g, '\''); + return str; +} + +function strMoreDiscode(str){ + str = str.replace(/\r\n/g,""); + str = str.replace(/\n/g,""); + + str = str.replace(/code/g,"wxxxcode-style"); + return str; +} + +function strDiscode(str){ + str = strNumDiscode(str); + str = strGreeceDiscode(str); + str = strcharacterDiscode(str); + str = strOtherDiscode(str); + str = strMoreDiscode(str); + return str; +} +function urlToHttpUrl(url,rep){ + + var patt1 = new RegExp("^//"); + var result = patt1.test(url); + if(result){ + url = rep+":"+url; + } + return url; +} + +module.exports = { + strDiscode:strDiscode, + urlToHttpUrl:urlToHttpUrl +} \ No newline at end of file diff --git a/utils/wxParse/wxParse.js b/utils/wxParse/wxParse.js new file mode 100644 index 0000000..5b9f5d9 --- /dev/null +++ b/utils/wxParse/wxParse.js @@ -0,0 +1,159 @@ +/** + * author: Di (微信小程序开发工程师) + * organization: WeAppDev(微信小程序开发论坛)(http://weappdev.com) + * 垂直微信小程序开发交流社区 + * + * github地址: https://github.com/icindy/wxParse + * + * for: 微信小程序富文本解析 + * detail : http://weappdev.com/t/wxparse-alpha0-1-html-markdown/184 + */ + +/** + * utils函数引入 + **/ +import showdown from './showdown.js'; +import HtmlToJson from './html2json.js'; +/** + * 配置及公有属性 + **/ +var realWindowWidth = 0; +var realWindowHeight = 0; +wx.getSystemInfo({ + success: function (res) { + realWindowWidth = res.windowWidth + realWindowHeight = res.windowHeight + } +}) +/** + * 主函数入口区 + **/ +function wxParse(bindName = 'wxParseData', type='html', data='
数据不能为空
', target,imagePadding) { + var that = target; + var transData = {};//存放转化后的数据 + if (type == 'html') { + transData = HtmlToJson.html2json(data, bindName); + // console.log(JSON.stringify(transData, ' ', ' ')); + } else if (type == 'md' || type == 'markdown') { + var converter = new showdown.Converter(); + var html = converter.makeHtml(data); + transData = HtmlToJson.html2json(html, bindName); + // console.log(JSON.stringify(transData, ' ', ' ')); + } + transData.view = {}; + transData.view.imagePadding = 0; + if(typeof(imagePadding) != 'undefined'){ + transData.view.imagePadding = imagePadding + } + var bindData = {}; + bindData[bindName] = transData; + that.setData(bindData) + that.wxParseImgLoad = wxParseImgLoad; + that.wxParseImgTap = wxParseImgTap; +} +// 图片点击事件 +function wxParseImgTap(e) { + var that = this; + var nowImgUrl = e.target.dataset.src; + var tagFrom = e.target.dataset.from; + if (typeof (tagFrom) != 'undefined' && tagFrom.length > 0) { + wx.previewImage({ + current: nowImgUrl, // 当前显示图片的http链接 + urls: that.data[tagFrom].imageUrls // 需要预览的图片http链接列表 + }) + } +} + +/** + * 图片视觉宽高计算函数区 + **/ +function wxParseImgLoad(e) { + var that = this; + var tagFrom = e.target.dataset.from; + var idx = e.target.dataset.idx; + if (typeof (tagFrom) != 'undefined' && tagFrom.length > 0) { + calMoreImageInfo(e, idx, that, tagFrom) + } +} +// 假循环获取计算图片视觉最佳宽高 +function calMoreImageInfo(e, idx, that, bindName) { + var temData = that.data[bindName]; + if (!temData || temData.images.length == 0) { + return; + } + var temImages = temData.images; + //因为无法获取view宽度 需要自定义padding进行计算,稍后处理 + var recal = wxAutoImageCal(e.detail.width, e.detail.height,that,bindName); + // temImages[idx].width = recal.imageWidth; + // temImages[idx].height = recal.imageheight; + // temData.images = temImages; + // var bindData = {}; + // bindData[bindName] = temData; + // that.setData(bindData); + var index = temImages[idx].index + var key = `${bindName}` + for (var i of index.split('.')) key+=`.nodes[${i}]` + var keyW = key + '.width' + var keyH = key + '.height' + that.setData({ + [keyW]: recal.imageWidth, + [keyH]: recal.imageheight, + }) +} + +// 计算视觉优先的图片宽高 +function wxAutoImageCal(originalWidth, originalHeight,that,bindName) { + //获取图片的原始长宽 + var windowWidth = 0, windowHeight = 0; + var autoWidth = 0, autoHeight = 0; + var results = {}; + var padding = that.data[bindName].view.imagePadding; + windowWidth = realWindowWidth-2*padding; + windowHeight = realWindowHeight; + //判断按照那种方式进行缩放 + // console.log("windowWidth" + windowWidth); + if (originalWidth > windowWidth) {//在图片width大于手机屏幕width时候 + autoWidth = windowWidth; + // console.log("autoWidth" + autoWidth); + autoHeight = (autoWidth * originalHeight) / originalWidth; + // console.log("autoHeight" + autoHeight); + results.imageWidth = autoWidth; + results.imageheight = autoHeight; + } else {//否则展示原来的数据 + results.imageWidth = originalWidth; + results.imageheight = originalHeight; + } + return results; +} + +function wxParseTemArray(temArrayName,bindNameReg,total,that){ + var array = []; + var temData = that.data; + var obj = null; + for(var i = 0; i < total; i++){ + var simArr = temData[bindNameReg+i].nodes; + array.push(simArr); + } + + temArrayName = temArrayName || 'wxParseTemArray'; + obj = JSON.parse('{"'+ temArrayName +'":""}'); + obj[temArrayName] = array; + that.setData(obj); +} + +/** + * 配置emojis + * + */ + +function emojisInit(reg='',baseSrc="/wxParse/emojis/",emojis){ + HtmlToJson.emojisInit(reg,baseSrc,emojis); +} + +module.exports = { + wxParse: wxParse, + wxParseTemArray:wxParseTemArray, + emojisInit:emojisInit +} + + diff --git a/utils/wxParse/wxParse.json b/utils/wxParse/wxParse.json new file mode 100644 index 0000000..8835af0 --- /dev/null +++ b/utils/wxParse/wxParse.json @@ -0,0 +1,3 @@ +{ + "usingComponents": {} +} \ No newline at end of file diff --git a/utils/wxParse/wxParse.wxml b/utils/wxParse/wxParse.wxml new file mode 100644 index 0000000..5101f5c --- /dev/null +++ b/utils/wxParse/wxParse.wxml @@ -0,0 +1,968 @@ + + + + + + + + + + + + +