1 line
17 KiB
JavaScript
1 line
17 KiB
JavaScript
!function(t,e,n){"undefined"!=typeof module&&module.exports?module.exports=n():"function"==typeof define&&define.amd?define(n):e[t]=n()}("reqwest",this,function(){function succeed(t){var e=protocolRe.exec(t.url);return e=e&&e[1]||window.location.protocol,httpsRe.test(e)?twoHundo.test(t.request.status):!!t.request.response}function handleReadyState(t,e,n){return function(){return t._aborted?n(t.request):t._timedOut?n(t.request,"Request is aborted: timeout"):(t.request&&4==t.request[readyState]&&(t.request.onreadystatechange=noop,succeed(t)?e(t.request):n(t.request)),void 0)}}function setHeaders(t,e){var n,r=e.headers||{};r.Accept=r.Accept||defaultHeaders.accept[e.type]||defaultHeaders.accept["*"];var i="function"==typeof FormData&&e.data instanceof FormData;e.crossOrigin||r[requestedWith]||(r[requestedWith]=defaultHeaders.requestedWith),r[contentType]||i||(r[contentType]=e.contentType||defaultHeaders.contentType);for(n in r)r.hasOwnProperty(n)&&"setRequestHeader"in t&&t.setRequestHeader(n,r[n])}function setCredentials(t,e){"undefined"!=typeof e.withCredentials&&"undefined"!=typeof t.withCredentials&&(t.withCredentials=!!e.withCredentials)}function generalCallback(t){lastValue=t}function urlappend(t,e){return t+(/\?/.test(t)?"&":"?")+e}function handleJsonp(t,e,n,r){var i=uniqid++,o=t.jsonpCallback||"callback",a=t.jsonpCallbackName||reqwest.getcallbackPrefix(i),u=new RegExp("((^|\\?|&)"+o+")=([^&]+)"),s=r.match(u),c=doc.createElement("script"),l=0,f=-1!==navigator.userAgent.indexOf("MSIE 10.0");return s?"?"===s[3]?r=r.replace(u,"$1="+a):a=s[3]:r=urlappend(r,o+"="+a),win[a]=generalCallback,c.type="text/javascript",c.src=r,c.async=!0,"undefined"==typeof c.onreadystatechange||f||(c.htmlFor=c.id="_reqwest_"+i),c.onload=c.onreadystatechange=function(){return c[readyState]&&"complete"!==c[readyState]&&"loaded"!==c[readyState]||l?!1:(c.onload=c.onreadystatechange=null,c.onclick&&c.onclick(),e(lastValue),lastValue=void 0,head.removeChild(c),l=1,void 0)},head.appendChild(c),{abort:function(){c.onload=c.onreadystatechange=null,n({},"Request is aborted: timeout",{}),lastValue=void 0,head.removeChild(c),l=1}}}function getRequest(t,e){var n,r=this.o,i=(r.method||"GET").toUpperCase(),o="string"==typeof r?r:r.url,a=r.processData!==!1&&r.data&&"string"!=typeof r.data?reqwest.toQueryString(r.data):r.data||null,u=!1;return"jsonp"!=r.type&&"GET"!=i||!a||(o=urlappend(o,a),a=null),"jsonp"==r.type?handleJsonp(r,t,e,o):(n=r.xhr&&r.xhr(r)||xhr(r),n.open(i,o,r.async===!1?!1:!0),setHeaders(n,r),setCredentials(n,r),win[xDomainRequest]&&n instanceof win[xDomainRequest]?(n.onload=t,n.onerror=e,n.onprogress=function(){},u=!0):n.onreadystatechange=handleReadyState(this,t,e),r.before&&r.before(n),u?setTimeout(function(){n.send(a)},200):n.send(a),n)}function Reqwest(t,e){this.o=t,this.fn=e,init.apply(this,arguments)}function setType(t){return t.match("json")?"json":t.match("javascript")?"js":t.match("text")?"html":t.match("xml")?"xml":void 0}function init(o,fn){function complete(t){for(o.timeout&&clearTimeout(self.timeout),self.timeout=null;self._completeHandlers.length>0;)self._completeHandlers.shift()(t)}function success(resp){var type=o.type||resp&&setType(resp.getResponseHeader("Content-Type"));resp="jsonp"!==type?self.request:resp;var filteredResponse=globalSetupOptions.dataFilter(resp.responseText,type),r=filteredResponse;try{resp.responseText=r}catch(e){}if(r)switch(type){case"json":try{resp=win.JSON?win.JSON.parse(r):eval("("+r+")")}catch(err){return error(resp,"Could not parse JSON in response",err)}break;case"js":resp=eval(r);break;case"html":resp=r;break;case"xml":resp=resp.responseXML&&resp.responseXML.parseError&&resp.responseXML.parseError.errorCode&&resp.responseXML.parseError.reason?null:resp.responseXML}for(self._responseArgs.resp=resp,self._fulfilled=!0,fn(resp),self._successHandler(resp);self._fulfillmentHandlers.length>0;)resp=self._fulfillmentHandlers.shift()(resp);complete(resp)}function timedOut(){self._timedOut=!0,self.request.abort()}function error(t,e,n){for(t=self.request,self._responseArgs.resp=t,self._responseArgs.msg=e,self._responseArgs.t=n,self._erred=!0;self._errorHandlers.length>0;)self._errorHandlers.shift()(t,e,n);complete(t)}this.url="string"==typeof o?o:o.url,this.timeout=null,this._fulfilled=!1,this._successHandler=function(){},this._fulfillmentHandlers=[],this._errorHandlers=[],this._completeHandlers=[],this._erred=!1,this._responseArgs={};var self=this;fn=fn||function(){},o.timeout&&(this.timeout=setTimeout(function(){timedOut()},o.timeout)),o.success&&(this._successHandler=function(){o.success.apply(o,arguments)}),o.error&&this._errorHandlers.push(function(){o.error.apply(o,arguments)}),o.complete&&this._completeHandlers.push(function(){o.complete.apply(o,arguments)}),this.request=getRequest.call(this,success,error)}function reqwest(t,e){return new Reqwest(t,e)}function normalize(t){return t?t.replace(/\r?\n/g,"\r\n"):""}function serial(t,e){var n,r,i,o,a=t.name,u=t.tagName.toLowerCase(),s=function(t){t&&!t.disabled&&e(a,normalize(t.attributes.value&&t.attributes.value.specified?t.value:t.text))};if(!t.disabled&&a)switch(u){case"input":/reset|button|image|file/i.test(t.type)||(n=/checkbox/i.test(t.type),r=/radio/i.test(t.type),i=t.value,(!(n||r)||t.checked)&&e(a,normalize(n&&""===i?"on":i)));break;case"textarea":e(a,normalize(t.value));break;case"select":if("select-one"===t.type.toLowerCase())s(t.selectedIndex>=0?t.options[t.selectedIndex]:null);else for(o=0;t.length&&o<t.length;o++)t.options[o].selected&&s(t.options[o])}}function eachFormElement(){var t,e,n=this,r=function(t,e){var r,i,o;for(r=0;r<e.length;r++)for(o=t[byTag](e[r]),i=0;i<o.length;i++)serial(o[i],n)};for(e=0;e<arguments.length;e++)t=arguments[e],/input|select|textarea/i.test(t.tagName)&&serial(t,n),r(t,["input","select","textarea"])}function serializeQueryString(){return reqwest.toQueryString(reqwest.serializeArray.apply(null,arguments))}function serializeHash(){var t={};return eachFormElement.apply(function(e,n){e in t?(t[e]&&!isArray(t[e])&&(t[e]=[t[e]]),t[e].push(n)):t[e]=n},arguments),t}function buildParams(t,e,n,r){var i,o,a,u=/\[\]$/;if(isArray(e))for(o=0;e&&o<e.length;o++)a=e[o],n||u.test(t)?r(t,a):buildParams(t+"["+("object"==typeof a?o:"")+"]",a,n,r);else if(e&&"[object Object]"===e.toString())for(i in e)buildParams(t+"["+i+"]",e[i],n,r);else r(t,e)}var win=window,doc=document,httpsRe=/^http/,protocolRe=/(^\w+):\/\//,twoHundo=/^(20\d|1223)$/,byTag="getElementsByTagName",readyState="readyState",contentType="Content-Type",requestedWith="X-Requested-With",head=doc[byTag]("head")[0],uniqid=0,callbackPrefix="reqwest_"+ +new Date,lastValue,xmlHttpRequest="XMLHttpRequest",xDomainRequest="XDomainRequest",noop=function(){},isArray="function"==typeof Array.isArray?Array.isArray:function(t){return t instanceof Array},defaultHeaders={contentType:"application/x-www-form-urlencoded",requestedWith:xmlHttpRequest,accept:{"*":"text/javascript, text/html, application/xml, text/xml, */*",xml:"application/xml, text/xml",html:"text/html",text:"text/plain",json:"application/json, text/javascript",js:"application/javascript, text/javascript"}},xhr=function(t){if(t.crossOrigin===!0){var e=win[xmlHttpRequest]?new XMLHttpRequest:null;if(e&&"withCredentials"in e)return e;if(win[xDomainRequest])return new XDomainRequest;throw new Error("Browser does not support cross-origin requests")}return win[xmlHttpRequest]?new XMLHttpRequest:new ActiveXObject("Microsoft.XMLHTTP")},globalSetupOptions={dataFilter:function(t){return t}};return Reqwest.prototype={abort:function(){this._aborted=!0,this.request.abort()},retry:function(){init.call(this,this.o,this.fn)},then:function(t,e){return t=t||function(){},e=e||function(){},this._fulfilled?this._responseArgs.resp=t(this._responseArgs.resp):this._erred?e(this._responseArgs.resp,this._responseArgs.msg,this._responseArgs.t):(this._fulfillmentHandlers.push(t),this._errorHandlers.push(e)),this},always:function(t){return this._fulfilled||this._erred?t(this._responseArgs.resp):this._completeHandlers.push(t),this},fail:function(t){return this._erred?t(this._responseArgs.resp,this._responseArgs.msg,this._responseArgs.t):this._errorHandlers.push(t),this},"catch":function(t){return this.fail(t)}},reqwest.serializeArray=function(){var t=[];return eachFormElement.apply(function(e,n){t.push({name:e,value:n})},arguments),t},reqwest.serialize=function(){if(0===arguments.length)return"";var t,e,n=Array.prototype.slice.call(arguments,0);return t=n.pop(),t&&t.nodeType&&n.push(t)&&(t=null),t&&(t=t.type),e="map"==t?serializeHash:"array"==t?reqwest.serializeArray:serializeQueryString,e.apply(null,n)},reqwest.toQueryString=function(t,e){var n,r,i=e||!1,o=[],a=encodeURIComponent,u=function(t,e){e="function"==typeof e?e():null==e?"":e,o[o.length]=a(t)+"="+a(e)};if(isArray(t))for(r=0;t&&r<t.length;r++)u(t[r].name,t[r].value);else for(n in t)t.hasOwnProperty(n)&&buildParams(n,t[n],i,u);return o.join("&").replace(/%20/g,"+")},reqwest.getcallbackPrefix=function(){return callbackPrefix},reqwest.compat=function(t,e){return t&&(t.type&&(t.method=t.type)&&delete t.type,t.dataType&&(t.type=t.dataType),t.jsonpCallback&&(t.jsonpCallbackName=t.jsonpCallback)&&delete t.jsonpCallback,t.jsonp&&(t.jsonpCallback=t.jsonp)),new Reqwest(t,e)},reqwest.ajaxSetup=function(t){t=t||{};for(var e in t)globalSetupOptions[e]=t[e]},reqwest}),function(t){if("function"==typeof bootstrap)bootstrap("promise",t);else if("object"==typeof exports)module.exports=t();else if("function"==typeof define&&define.amd)define(t);else if("undefined"!=typeof ses){if(!ses.ok())return;ses.makePromise=t}else"undefined"!=typeof window?window.Promise=t():global.Promise=t()}(function(){return function(t,e,n){function r(n,o){if(!e[n]){if(!t[n]){var a="function"==typeof require&&require;if(!o&&a)return a(n,!0);if(i)return i(n,!0);throw new Error("Cannot find module '"+n+"'")}var u=e[n]={exports:{}};t[n][0].call(u.exports,function(e){var i=t[n][1][e];return r(i?i:e)},u,u.exports)}return e[n].exports}for(var i="function"==typeof require&&require,o=0;o<n.length;o++)r(n[o]);return r}({1:[function(t,e){var n=e.exports={};n.nextTick=function(){var t="undefined"!=typeof window&&window.setImmediate,e="undefined"!=typeof window&&window.postMessage&&window.addEventListener;if(t)return function(t){return window.setImmediate(t)};if(e){var n=[];return window.addEventListener("message",function(t){if(t.source===window&&"process-tick"===t.data&&(t.stopPropagation(),n.length>0)){var e=n.shift();e()}},!0),function(t){n.push(t),window.postMessage("process-tick","*")}}return function(t){setTimeout(t,0)}}(),n.title="browser",n.browser=!0,n.env={},n.argv=[],n.binding=function(){throw new Error("process.binding is not supported")},n.cwd=function(){return"/"},n.chdir=function(){throw new Error("process.chdir is not supported")}},{}],2:[function(t,e){"use strict";function n(t){function e(t){return null===l?(h.push(t),void 0):(i(function(){var e=l?t.onFulfilled:t.onRejected;if(null===e)return(l?t.resolve:t.reject)(p),void 0;var n;try{n=e(p)}catch(r){return t.reject(r),void 0}t.resolve(n)}),void 0)}function o(t){f||a(t)}function a(t){if(null===l)try{if(t===d)throw new TypeError("A promise cannot be resolved with itself.");if(t&&("object"==typeof t||"function"==typeof t)){var e=t.then;if("function"==typeof e)return f=!0,e.call(t,a,s),void 0}l=!0,p=t,c()}catch(n){s(n)}}function u(t){f||s(t)}function s(t){null===l&&(l=!1,p=t,c())}function c(){for(var t=0,n=h.length;n>t;t++)e(h[t]);h=null}if(!(this instanceof n))return new n(t);if("function"!=typeof t)throw new TypeError("not a function");var l=null,f=!1,p=null,h=[],d=this;this.then=function(t,i){return new n(function(n,o){e(new r(t,i,n,o))})};try{t(o,u)}catch(g){u(g)}}function r(t,e,n,r){this.onFulfilled="function"==typeof t?t:null,this.onRejected="function"==typeof e?e:null,this.resolve=n,this.reject=r}var i=t("./lib/next-tick");e.exports=n},{"./lib/next-tick":4}],3:[function(t,e){"use strict";var n=t("./core.js"),r=t("./lib/next-tick");e.exports=n,n.from=function(t){return t instanceof n?t:new n(function(e){e(t)})},n.denodeify=function(t){return function(){var e=this,r=Array.prototype.slice.call(arguments);return new n(function(n,i){r.push(function(t,e){t?i(t):n(e)}),t.apply(e,r)})}},n.nodeify=function(t){return function(){var e=Array.prototype.slice.call(arguments),i="function"==typeof e[e.length-1]?e.pop():null;try{return t.apply(this,arguments).nodeify(i)}catch(o){if(null==i)return new n(function(t,e){e(o)});r(function(){i(o)})}}},n.all=function(){var t=Array.prototype.slice.call(1===arguments.length&&Array.isArray(arguments[0])?arguments[0]:arguments);return new n(function(e,n){function r(o,a){try{if(a&&("object"==typeof a||"function"==typeof a)){var u=a.then;if("function"==typeof u)return u.call(a,function(t){r(o,t)},n),void 0}t[o]=a,0===--i&&e(t)}catch(s){n(s)}}if(0===t.length)return e([]);for(var i=t.length,o=0;o<t.length;o++)r(o,t[o])})},n.prototype.done=function(){var t=arguments.length?this.then.apply(this,arguments):this;t.then(null,function(t){r(function(){throw t})})},n.prototype.nodeify=function(t){return null==t?this:(this.then(function(e){r(function(){t(null,e)})},function(e){r(function(){t(e)})}),void 0)}},{"./core.js":2,"./lib/next-tick":4}],4:[function(t,e){!function(t){"use strict";e.exports="function"==typeof setImmediate?function(t){setImmediate(t)}:"undefined"!=typeof t&&t&&"function"==typeof t.nextTick?function(e){t.nextTick(e)}:function(t){setTimeout(t,0)}}(t("__browserify_process"))},{__browserify_process:1}]},{},[3])(3)}),function(){var t,e=[].indexOf||function(t){for(var e=0,n=this.length;n>e;e++)if(e in this&&this[e]===t)return e;return-1};window.InfluxDB=t=function(){function t(t){var n;t||(t={}),this.host=t.host||"localhost",this.hosts=t.hosts||[this.host],this.port=t.port||8086,this.username=t.username||"root",this.password=t.password||"root",this.database=t.database,this.ssl=t.ssl||!1,this.isCrossOrigin=(n=window.location.host,e.call(this.hosts,n)<0),this.username=encodeURIComponent(this.username),this.password=encodeURIComponent(this.password)}return t.prototype.showDatabases=function(){return this.query("SHOW DATABASES")},t.prototype.createDatabase=function(t){return this.query("CREATE DATABASE "+t)},t.prototype.dropDatabase=function(t){return this.query("DROP DATABASE "+t)},t.prototype.showUsers=function(){return this.query("SHOW USERS")},t.prototype.createUser=function(t,e,n){return this.query("CREATE USER "+e+" WITH PASSWORD '"+n+"'")},t.prototype.dropUser=function(t,e){return this.query("DROP USER "+e)},t.prototype.showRetentionPolicies=function(t){return this.queryDatabase("SHOW RETENTION POLICIES "+t)},t.prototype.createRetentionPolicy=function(t,e,n,r,i){return this.query("CREATE RETENTION POLICY "+e+" ON "+t+" DURATION "+n+" REPLICATION "+r+(i?" DEFAULT":""))},t.prototype.deleteRetentionPolicy=function(t){return this.queryDatabase("DROP RETENTION POLICY "+t)},t.prototype.showContinuousQueries=function(){return this.query("SHOW CONTINUOUS QUERIES")},t.prototype.deleteContinuousQuery=function(t,e){return this.query("DROP CONTINUOUS QUERY "+e)},t.prototype.query=function(t,e){return this.get(this.path("query",{q:t}),e)},t.prototype.queryDatabase=function(t,e){return this.get(this.path("query",{q:t,db:this.database}),e)},t.prototype.get=function(t,e){return new Promise(function(n){return function(r,i){return reqwest({method:"get",type:"json",url:n.url(t),crossOrigin:n.isCrossOrigin,success:function(t){return r(t),e?e(n.formatPoints(t)):void 0},error:function(t){return i(t)}})}}(this))},t.prototype.post=function(t,e){return new Promise(function(n){return function(r,i){return reqwest({method:"post",type:"json",url:n.url(t),crossOrigin:n.isCrossOrigin,contentType:"application/json",data:"string"==typeof e?e:JSON.stringify(e),success:function(t){return r(t)},error:function(t){return i(t)}})}}(this))},t.prototype.formatPoints=function(t){return t.map(function(t){var e;return e={name:t.name,points:t.points.map(function(e){var n,r;return n={},t.columns.forEach(function(t,r){return n[t]=e[r]}),r=new Date(0),r.setUTCSeconds(Math.round(n.time/1e3)),n.time=r,n})}})},t.prototype.read=function(){return this.queryDatabase("SELECT value FROM cpu","foo")},t.prototype.write=function(t,e,n,r){var i,o,a,u,s;null==n&&(n={}),o={points:[],name:t,columns:[]},u=[];for(a in e)s=e[a],u.push(s),o.columns.push(a);return o.points.push(u),i=[o],this.post(this.path("db/"+this.database+"/series"),i,r)},t.prototype.writeJSON=function(t,e){return this.post(this.path("write",{db:this.database}),t,e)},t.prototype.writePoint=function(t,e,n,r){var i,o,a,u,s;null==n&&(n={}),o={points:[],name:t,columns:[]},u=[];for(a in e)s=e[a],u.push(s),o.columns.push(a);return o.points.push(u),i=[o],this.post(this.path("db/"+this.database+"/series"),i,r)},t.prototype.writeSeries=function(t,e){return this.post(this.path("db/"+this.database+"/series"),t,e)},t.prototype.path=function(t,e){var n;return n=""+t+"?u="+this.username+"&p="+this.password,null!=e&&e.q&&(n+="&q="+encodeURIComponent(e.q)),null!=e&&e.db&&(n+="&db="+encodeURIComponent(e.db)),n},t.prototype.url=function(t){var e;return e=this.hosts.shift(),this.hosts.push(e),""+(this.ssl?"https":"http")+"://"+e+":"+this.port+"/"+t},t}()}.call(this),window.InfluxDB.VERSION="0.0.16",function(){}.call(this); |