@mapbox/instrumentile-gl
Version:
tileload metrics for Mapbox GL
2 lines (1 loc) • 12.3 kB
JavaScript
!function(t){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var e;"undefined"!=typeof window?e=window:"undefined"!=typeof global?e=global:"undefined"!=typeof self&&(e=self),e.instrumentile=t()}}(function(){return function i(s,a,u){function c(n,t){if(!a[n]){if(!s[n]){var e="function"==typeof require&&require;if(!t&&e)return e(n,!0);if(p)return p(n,!0);var o=new Error("Cannot find module '"+n+"'");throw o.code="MODULE_NOT_FOUND",o}var r=a[n]={exports:{}};s[n][0].call(r.exports,function(t){var e=s[n][1][t];return c(e||t)},r,r.exports,i,s,a,u)}return a[n].exports}for(var p="function"==typeof require&&require,t=0;t<u.length;t++)c(u[t]);return c}({1:[function(t,e,n){"use strict";var o=t("@mapbox/mapbox-events");function u(t){this.url=t,this.search=1<t.split("?").length?t.split("?")[1]:"",this.hostname=this.url.replace(/https?:\/\/([^/]+)\/.*$/,"$1")}function r(t,e){if(this.map=t,this.options=e||{},this.id=null,this.source=this.options.source||"",!this.map)throw new TypeError("You must provide a valid Mapbox GL Map object");if(!this.options.token)throw new TypeError("You must provide a valid Mapbox token");this.options.stub&&this.options.stub.events?this._events=this.options.stub.events:this._events=o({api:this.options.api,token:this.options.token,flushAt:this.options.flushAt,flushAfter:this.options.flushAfter,version:2}),this.events={push:function(t){t.schema=t.event+"-2.1",this._events.push(t)}.bind(this)},this.options.stub&&this.options.stub.performance?this.performance=this.options.stub.performance:window&&void 0!==window.performance&&window.performance?this.performance=window.performance:this.performance=!1,this.map._collectResourceTiming&&t.on("data",function(t){t.tile&&t.tile.resourceTiming&&0<t.tile.resourceTiming.length?this._dataLoadEvent("instrumentile.source.vt",t.tile.resourceTiming[t.tile.resourceTiming.length-1]):t.source&&"geojson"===t.source.type&&t.source.data&&"string"==typeof t.source.data&&t.resourceTiming&&0<t.resourceTiming.length&&this._dataLoadEvent("instrumentile.source.geojson",t.resourceTiming[t.resourceTiming.length-1])}.bind(this)),t.on("load",this._mapLoadEvent.bind(this)),t.on("click",this._interactionEvent.bind(this,"instrumentile.map.click")),t.on("dragend",this._interactionEvent.bind(this,"instrumentile.map.dragend"))}u.prototype.toString=function(){return this.url.split("?")[0]+(this.search?"?"+this.search:"")},r.prototype._dataLoadEvent=function(t,e){var n,o,r,i,s,a=new u(e.name);a.search=a.search.split("&").filter(function(t){return!/(^|\?)access_token=.*/.test(t)}).join("&"),"resource"===e.entryType&&(n=e.domainLookupEnd-e.domainLookupStart,o=e.connectEnd-e.connectStart,r=!isNaN(parseFloat(e.secureConnectionStart))&&isFinite(e.secureConnectionStart)?e.connectEnd-e.secureConnectionStart:void 0,i=e.responseStart-e.requestStart,s=e.responseEnd-e.responseStart),this.events.push({id:this.id,source:this.source,event:t,url:a.toString(),timeTaken:e.duration,host:a.hostname,DNS:n,TCP:o,SSL:r,request:i,response:s,transferSize:e.transferSize,decodedBodySize:e.decodedBodySize,encodedBodySize:e.encodedBodySize})},r.prototype._interactionEvent=function(t,e){var n=this.map.getZoom(),o=e.lngLat||this.map.getCenter();this.events.push({id:this.id,source:this.source,event:t,lat:o.lat,lng:o.lng,zoom:n})},r.prototype._mapLoadEvent=function(){var t,e,n,o,r,i,s,a=[];this.map.style&&this.map.style.stylesheet&&(this.map.style.stylesheet.owner&&a.push(this.map.style.stylesheet.owner||""),this.map.style.stylesheet.id&&a.push(this.map.style.stylesheet.id||""),this.id=a.join("/"));var u=this.map.getCenter(),c=this.map.getZoom(),p={};this.performance&&(t=(p=this.performance.timing).domainLookupEnd-p.domainLookupStart,e=p.connectEnd-p.connectStart,n=p.secureConnectionStart?p.connectEnd-p.secureConnectionStart:void 0,o=+new Date-p.navigationStart,r=p.responseStart-p.requestStart,i=p.responseEnd-p.responseStart,s=p.domainLookupStart-p.fetchStart),this.events.push({id:this.id,source:this.source,event:"instrumentile.map.load",lat:u.lat,lng:u.lng,zoom:c,DNS:t,TCP:e,SSL:n,loadtime:o,request:r,response:i,appCache:s})},r.supportsWebWorkerPerformanceCollection=function(e){if(window.URL=window.URL||window.webkitURL,!window.Worker||!window.URL||"undefined"==typeof Blob||"undefined"!=typeof MSBlobBuilder)return e(null,!1);var n,o="self.onmessage = function(e) { postMessage(typeof performance === 'undefined' ? 'missing' : 'found'); }";try{n=new Blob([o],{type:"application/javascript"})}catch(t){if(window.BlobBuilder=window.BlobBuilder||window.WebKitBlobBuilder||window.MozBlobBuilder,!window.BlobBuilder)return e("no blobbuilder fallback",!1);(n=new BlobBuilder).append(o),n=n.getBlob()}try{var t=new Worker(URL.createObjectURL(n));t.onmessage=function(t){return e(null,"found"===t.data)},t.postMessage(!0)}catch(t){return e(t,!1)}return null},e.exports=r},{"@mapbox/mapbox-events":3}],2:[function(t,e,n){e.exports=function(t,e){e=e||function(){};var n=this.api.match(/^(https?:)?/);if("undefined"!=typeof document&&document.location.protocol!=n[0])return e();xdr=new this._xdr;var o=this.api+"/events/v"+this.version.toString()+"?access_token="+this.token;xdr.onload=function(){e(xdr)},xdr.onerror=function(){},xdr.onprogress=function(){},xdr.open("post",o),xdr.send(JSON.stringify(t))}},{}],3:[function(t,e,n){var o=t("xhr"),r=t("hat");function i(t){if(!(this instanceof i))return new i(t);this.queue=[],this.flushAt=Math.max(t.flushAt,1)||20,this.flushAfter=Math.max(t.flushAfter,0)||1e4,this.version=t.version||1,this.api=t.api||"https://api.tiles.mapbox.com",this.token=t.token,this._xhr=o,this._xdr="undefined"==typeof window||"withCredentials"in new window.XMLHttpRequest?null:XDomainRequest,this.instance=r(),this.anonid=function(){try{var t=window.localStorage.getItem("anonid")||r();return window.localStorage.setItem("anonid",t),t}catch(t){return this.anonid||(this.anonid=r()),this.anonid}}()}(e.exports=i).prototype.push=function(t){(t=JSON.parse(JSON.stringify(t))).version=this.version,t.created=+new Date,t.instance=this.instance,t.anonid=this.anonid,this.queue.push(t),this.queue.length>=this.flushAt&&this.flush(),this.timer&&clearTimeout(this.timer),this.flushAfter&&(this.timer=setTimeout(this.flush.bind(this),this.flushAfter))},i.prototype.flush=function(){this.queue.length&&(this._xdr?this._compatibilityPost(this.queue.splice(0,this.flushAt)):this._post(this.queue.splice(0,this.flushAt)))},i.prototype._post=t("./post.js"),i.prototype._compatibilityPost=t("./compatibility_post.js")},{"./compatibility_post.js":2,"./post.js":4,hat:7,xhr:11}],4:[function(t,e,n){e.exports=function(t,e){e=e||function(t,e){},this._xhr({method:"POST",body:JSON.stringify(t),uri:this.api+"/events/v"+this.version.toString()+"?access_token="+this.token,headers:{"Content-Type":"text/plain"}},e)}},{}],5:[function(t,e,n){var o=t("is-function");e.exports=function(t,e,n){if(!o(e))throw new TypeError("iterator must be a function");arguments.length<3&&(n=this);"[object Array]"===r.call(t)?function(t,e,n){for(var o=0,r=t.length;o<r;o++)i.call(t,o)&&e.call(n,t[o],o,t)}(t,e,n):"string"==typeof t?function(t,e,n){for(var o=0,r=t.length;o<r;o++)e.call(n,t.charAt(o),o,t)}(t,e,n):function(t,e,n){for(var o in t)i.call(t,o)&&e.call(n,t[o],o,t)}(t,e,n)};var r=Object.prototype.toString,i=Object.prototype.hasOwnProperty},{"is-function":8}],6:[function(t,n,e){(function(t){var e;e="undefined"!=typeof window?window:void 0!==t?t:"undefined"!=typeof self?self:{},n.exports=e}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{}],7:[function(t,e,n){var u=e.exports=function(t,e){if(e||(e=16),void 0===t&&(t=128),t<=0)return"0";for(var n=Math.log(Math.pow(2,t))/Math.log(e),o=2;n===1/0;o*=2)n=Math.log(Math.pow(2,t/o))/Math.log(e)*o;var r=n-Math.floor(n),i="";for(o=0;o<Math.floor(n);o++){i=Math.floor(Math.random()*e).toString(e)+i}if(r){var s=Math.pow(e,r);i=Math.floor(Math.random()*s).toString(e)+i}var a=parseInt(i,e);return a!==1/0&&a>=Math.pow(2,t)?u(t,e):i};u.rack=function(o,r,i){var n=function(t){var e=0;do{if(10<e++){if(!i)throw new Error("too many ID collisions, use more bits");o+=i}var n=u(o,r)}while(Object.hasOwnProperty.call(s,n));return s[n]=t,n},s=n.hats={};return n.get=function(t){return n.hats[t]},n.set=function(t,e){return n.hats[t]=e,n},n.bits=o||128,n.base=r||16,n}},{}],8:[function(t,e,n){e.exports=function(t){var e=o.call(t);return"[object Function]"===e||"function"==typeof t&&"[object RegExp]"!==e||"undefined"!=typeof window&&(t===window.setTimeout||t===window.alert||t===window.confirm||t===window.prompt)};var o=Object.prototype.toString},{}],9:[function(t,e,n){var s=t("trim"),o=t("for-each");e.exports=function(t){if(!t)return{};var i={};return o(s(t).split("\n"),function(t){var e,n=t.indexOf(":"),o=s(t.slice(0,n)).toLowerCase(),r=s(t.slice(n+1));void 0===i[o]?i[o]=r:(e=i[o],"[object Array]"===Object.prototype.toString.call(e)?i[o].push(r):i[o]=[i[o],r])}),i}},{"for-each":5,trim:10}],10:[function(t,e,n){(n=e.exports=function(t){return t.replace(/^\s*|\s*$/g,"")}).left=function(t){return t.replace(/^\s*/,"")},n.right=function(t){return t.replace(/\s*$/,"")}},{}],11:[function(t,e,n){"use strict";var o=t("global/window"),r=t("is-function"),w=t("parse-headers"),i=t("xtend");function s(t,e,n){var o=t;return r(e)?(n=e,"string"==typeof t&&(o={uri:t})):o=i(e,{uri:t}),o.callback=n,o}function v(t,e,n){return a(e=s(t,e,n))}function a(o){if(void 0===o.callback)throw new Error("callback argument missing");var r=!1,i=function(t,e,n){r||(r=!0,o.callback(t,e,n))};function e(t){return clearTimeout(u),t instanceof Error||(t=new Error(""+(t||"Unknown XMLHttpRequest Error"))),t.statusCode=0,i(t,m)}function t(){if(!s){var t;clearTimeout(u),t=o.useXDR&&void 0===a.status?200:1223===a.status?204:a.status;var e=m,n=null;return 0!==t?(e={body:function(){var t=void 0;if(t=a.response?a.response:a.responseText||function(t){try{if("document"===t.responseType)return t.responseXML;var e=t.responseXML&&"parsererror"===t.responseXML.documentElement.nodeName;if(""===t.responseType&&!e)return t.responseXML}catch(t){}return null}(a),f)try{t=JSON.parse(t)}catch(t){}return t}(),statusCode:t,method:p,headers:{},url:c,rawRequest:a},a.getAllResponseHeaders&&(e.headers=w(a.getAllResponseHeaders()))):n=new Error("Internal XMLHttpRequest Error"),i(n,e,e.body)}}var n,s,a=o.xhr||null;a||(a=o.cors||o.useXDR?new v.XDomainRequest:new v.XMLHttpRequest);var u,c=a.url=o.uri||o.url,p=a.method=o.method||"GET",h=o.body||o.data,d=a.headers=o.headers||{},l=!!o.sync,f=!1,m={body:void 0,headers:{},statusCode:0,method:p,url:c,rawRequest:a};if("json"in o&&!1!==o.json&&(f=!0,d.accept||d.Accept||(d.Accept="application/json"),"GET"!==p&&"HEAD"!==p&&(d["content-type"]||d["Content-Type"]||(d["Content-Type"]="application/json"),h=JSON.stringify(!0===o.json?h:o.json))),a.onreadystatechange=function(){4===a.readyState&&setTimeout(t,0)},a.onload=t,a.onerror=e,a.onprogress=function(){},a.onabort=function(){s=!0},a.ontimeout=e,a.open(p,c,!l,o.username,o.password),l||(a.withCredentials=!!o.withCredentials),!l&&0<o.timeout&&(u=setTimeout(function(){if(!s){s=!0,a.abort("timeout");var t=new Error("XMLHttpRequest timeout");t.code="ETIMEDOUT",e(t)}},o.timeout)),a.setRequestHeader)for(n in d)d.hasOwnProperty(n)&&a.setRequestHeader(n,d[n]);else if(o.headers&&!function(t){for(var e in t)if(t.hasOwnProperty(e))return!1;return!0}(o.headers))throw new Error("Headers cannot be set on an XDomainRequest object");return"responseType"in o&&(a.responseType=o.responseType),"beforeSend"in o&&"function"==typeof o.beforeSend&&o.beforeSend(a),a.send(h||null),a}(e.exports=v).XMLHttpRequest=o.XMLHttpRequest||function(){},v.XDomainRequest="withCredentials"in new v.XMLHttpRequest?v.XMLHttpRequest:o.XDomainRequest,function(t,e){for(var n=0;n<t.length;n++)e(t[n])}(["get","put","post","patch","head","delete"],function(o){v["delete"===o?"del":o]=function(t,e,n){return(e=s(t,e,n)).method=o.toUpperCase(),a(e)}})},{"global/window":6,"is-function":8,"parse-headers":9,xtend:12}],12:[function(t,e,n){e.exports=function(){for(var t={},e=0;e<arguments.length;e++){var n=arguments[e];for(var o in n)r.call(n,o)&&(t[o]=n[o])}return t};var r=Object.prototype.hasOwnProperty},{}]},{},[1])(1)});