Comparing version 2.0.0-alpha3 to 2.0.0-alpha4
{ | ||
"name": "network-js", | ||
"version": "2.0.0-alpha3", | ||
"version": "2.0.0-alpha4", | ||
"homepage": "https://github.com/nesk/network.js", | ||
@@ -5,0 +5,0 @@ "authors": [ |
@@ -24,2 +24,3 @@ (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.Network = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
var getGlobalObject = _utilsHelpers.getGlobalObject; | ||
var isObject = _utilsHelpers.isObject; | ||
@@ -50,5 +51,5 @@ var assign = _utilsHelpers.assign; | ||
* @param {Network~settingsObject} [settings={}] A set of custom settings. | ||
* @property {LatencyModule} latency The latency module. | ||
* @property {BandwidthModule} upload The upload module. | ||
* @property {BandwidthModule} download The download module. | ||
* @member {LatencyModule} latency The latency module. | ||
* @member {BandwidthModule} upload The upload module. | ||
* @member {BandwidthModule} download The download module. | ||
*/ | ||
@@ -235,6 +236,7 @@ | ||
value: function _exposeInternalClasses() { | ||
var classes = { EventDispatcher: EventDispatcher, HttpModule: HttpModule, LatencyModule: LatencyModule, BandwidthModule: BandwidthModule, Timing: Timing }; | ||
var global = getGlobalObject(), | ||
classes = { EventDispatcher: EventDispatcher, HttpModule: HttpModule, LatencyModule: LatencyModule, BandwidthModule: BandwidthModule, Timing: Timing }; | ||
Object.keys(classes).forEach(function (name) { | ||
window[name] = classes[name]; | ||
global[name] = classes[name]; | ||
}); | ||
@@ -1323,2 +1325,4 @@ | ||
var getGlobalObject = require("../utils/helpers").getGlobalObject; | ||
/** | ||
@@ -1333,11 +1337,31 @@ * @private | ||
this._marks = {}; | ||
this._measures = {}; | ||
var global = getGlobalObject(); | ||
// Does the browser support the following APIs? | ||
/** | ||
* Defines if the current browser supports some specific Timing APIs. | ||
* @private | ||
* @member {Object} _support | ||
* @property {boolean} performance `true` if the Performance API is available. | ||
* @property {boolean} userTiming `true` if the User Timing API is available. | ||
* @property {boolean} resourceTiming `true` if the Resource Timing API is available. | ||
*/ | ||
this._support = { | ||
performance: !!window.performance, | ||
userTiming: window.performance && performance.mark, | ||
resourceTiming: window.performance && typeof performance.getEntriesByType == "function" && performance.timing | ||
performance: !!global.performance, | ||
userTiming: global.performance && performance.mark, | ||
resourceTiming: global.performance && typeof performance.getEntriesByType == "function" && performance.timing | ||
}; | ||
/** | ||
* Contains all the marks created by the `mark` method. | ||
* @private | ||
* @member {Object} _marks | ||
*/ | ||
this._marks = {}; | ||
/** | ||
* Contains all the measures created by the `measure` method. | ||
* @private | ||
* @member {Object} _measures | ||
*/ | ||
this._measures = {}; | ||
} | ||
@@ -1362,3 +1386,5 @@ | ||
performance.mark(label); | ||
} else if (support.performance) { | ||
} | ||
if (support.performance) { | ||
marks[label] = performance.now(); | ||
@@ -1390,7 +1416,13 @@ } else { | ||
if (typeof measures[measureLabel] == "undefined") { | ||
var measureWithoutUserTiming = marks[markLabelB] - marks[markLabelA]; | ||
if (support.userTiming) { | ||
performance.measure(measureLabel, markLabelA, markLabelB); | ||
measures[measureLabel] = performance.getEntriesByName(measureLabel)[0].duration; | ||
var entriesByName = performance.getEntriesByName(measureLabel); | ||
// The performance API could return no corresponding entries in Firefox so we must use a fallback. | ||
// See: https://github.com/nesk/network.js/issues/32#issuecomment-118434305 | ||
measures[measureLabel] = entriesByName.length ? entriesByName[0].duration : measureWithoutUserTiming; | ||
} else { | ||
measures[measureLabel] = marks[markLabelB] - marks[markLabelA]; | ||
measures[measureLabel] = measureWithoutUserTiming; | ||
} | ||
@@ -1422,3 +1454,4 @@ } | ||
},{}],7:[function(require,module,exports){ | ||
},{"../utils/helpers":7}],7:[function(require,module,exports){ | ||
(function (global){ | ||
"use strict"; | ||
@@ -1431,2 +1464,11 @@ | ||
/** | ||
* Return the global object. | ||
* @private | ||
* @function getGlobalObject | ||
* @return {Object} | ||
* @see https://gist.github.com/rauschma/1bff02da66472f555c75 | ||
*/ | ||
exports.getGlobalObject = getGlobalObject; | ||
/** | ||
* Determine if the provided value is an object. | ||
@@ -1491,2 +1533,16 @@ * @private | ||
function getGlobalObject() { | ||
// Workers don’t have `window`, only `self`. | ||
if (typeof self !== "undefined") { | ||
return self; | ||
} | ||
if (typeof global !== "undefined") { | ||
return global; | ||
} | ||
// Not all environments allow `eval` and `Function`, use only as a last resort. | ||
return new Function("return this")(); | ||
} | ||
function isObject(obj) { | ||
@@ -1593,2 +1649,4 @@ return obj != undefined && obj != null && typeof obj.valueOf() == "object"; | ||
}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) | ||
},{}]},{},[1])(1) | ||
@@ -1595,0 +1653,0 @@ }); |
@@ -1,2 +0,2 @@ | ||
!function(t){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var e;e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,e.Network=t()}}(function(){return function t(e,n,r){function i(u,o){if(!n[u]){if(!e[u]){var a="function"==typeof require&&require;if(!o&&a)return a(u,!0);if(s)return s(u,!0);var l=new Error("Cannot find module '"+u+"'");throw l.code="MODULE_NOT_FOUND",l}var c=n[u]={exports:{}};e[u][0].call(c.exports,function(t){var n=e[u][1][t];return i(n?n:t)},c,c.exports,t,e,n,r)}return n[u].exports}for(var s="function"==typeof require&&require,u=0;u<r.length;u++)i(r[u]);return i}({1:[function(t,e){"use strict";var n=function(t){return t&&t.__esModule?t["default"]:t},r=function(t,e,n){return Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0})},i=function(){function t(t,e){for(var n in e){var r=e[n];r.configurable=!0,r.value&&(r.writable=!0)}Object.defineProperties(t,e)}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),s=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},u=n(t("./EventDispatcher")),o=n(t("./Http/HttpModule")),a=n(t("./Http/LatencyModule")),l=n(t("./Http/BandwidthModule")),c=n(t("./Timing")),f=t("../utils/helpers"),d=f.isObject,h=f.assign,p=f.except,g=function(){function t(){var e=void 0===arguments[0]?{}:arguments[0];s(this,t),this._modules={},this._modulesInitialized=!1,this._pendingSettings={},this._registerModule("latency",function(t){return new a(t)})._registerModule("upload",function(t){return new l("upload",t)})._registerModule("download",function(t){return new l("download",t)}),this._initModules(this.settings(e))}return i(t,{settings:{value:function(t){var e=function(){return t.apply(this,arguments)};return e.toString=function(){return t.toString()},e}(function(){var t=this,e=void 0===arguments[0]?null:arguments[0],n=Object.keys(this._modules);if(!d(e))return n.reduce(function(e,n){return h(e,r({},n,t._modules[n].settings()))},{});var i=function(){var i=p(e,n),s=p(e,Object.keys(i));return e=n.reduce(function(t,e){return h(t,r({},e,i))},{}),e=h(e,s),t._modulesInitialized?Object.keys(t._modules).forEach(function(n){t._modules[n].settings(e[n])}):t._pendingSettings=e,{v:t}}();return"object"==typeof i?i.v:void 0})},isRequesting:{value:function(){var t=!1;for(var e in this._modules)this._modules.hasOwnProperty(e)&&(t=t||this._modules[e].isRequesting());return t}},_registerModule:{value:function(t,e){return this._modules[t]=e,this}},_initModules:{value:function(){var t=this;return this._modulesInitialized||(Object.keys(this._modules).forEach(function(e){t._modules[e]=t._modules[e](t._pendingSettings[e]).on("_newRequest",function(){return!t.isRequesting()}),t[e]=t._modules[e]}),this._modulesInitialized=!0),this}}},{_exposeInternalClasses:{value:function(){var t={EventDispatcher:u,HttpModule:o,LatencyModule:a,BandwidthModule:l,Timing:c};return Object.keys(t).forEach(function(e){window[e]=t[e]}),this}}}),t}();e.exports=g},{"../utils/helpers":7,"./EventDispatcher":2,"./Http/BandwidthModule":3,"./Http/HttpModule":4,"./Http/LatencyModule":5,"./Timing":6}],2:[function(t,e){"use strict";var n=function(){function t(t,e){for(var n in e){var r=e[n];r.configurable=!0,r.value&&(r.writable=!0)}Object.defineProperties(t,e)}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),r=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},i=function(){function t(){r(this,t),this._eventCallbacks={}}return n(t,{on:{value:function(t,e){var n=this;return t=Array.isArray(t)?t:[t],t.forEach(function(t){var r=n._eventCallbacks[t]=n._eventCallbacks[t]||[];~r.indexOf(e)||r.push(e)}),this}},off:{value:function(t){var e=this,n=void 0===arguments[1]?null:arguments[1];return t=Array.isArray(t)?t:[t],t.forEach(function(t){var r=e._eventCallbacks[t];if(!n&&r)delete e._eventCallbacks[t];else{var i=r?r.indexOf(n):-1;-1!=i&&r.splice(i,1)}}),this}},trigger:{value:function(t){for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];var i=this._eventCallbacks[t]||[],s=!0;return i.forEach(function(t){var e=t.apply(void 0,n);e=e!==!1?!0:!1,s=s&&e}),s}}}),t}();e.exports=i},{}],3:[function(t,e){"use strict";var n=function(t){return t&&t.__esModule?t["default"]:t},r=function(){function t(t,e){for(var n in e){var r=e[n];r.configurable=!0,r.value&&(r.writable=!0)}Object.defineProperties(t,e)}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),i=function f(t,e,n){var r=Object.getOwnPropertyDescriptor(t,e);if(void 0===r){var i=Object.getPrototypeOf(t);return null===i?void 0:f(i,e,n)}if("value"in r&&r.writable)return r.value;var s=r.get;return void 0===s?void 0:s.call(n)},s=function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(t.__proto__=e)},u=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},o=n(t("./HttpModule")),a=n(t("../Timing")),l=t("../../utils/helpers").defer,c=function(t){function e(t){var n=this,r=void 0===arguments[1]?{}:arguments[1];u(this,e),t=~["upload","download"].indexOf(t)?t:"download",this._extendDefaultSettings({data:{size:"upload"==t?2097152:10485760,multiplier:2}}),i(Object.getPrototypeOf(e.prototype),"constructor",this).call(this,t,r),this._loadingType=t,this._intendedEnd=!1,this._isRestarting=!1,this._lastLoadedValue=null,this._speedRecords=[],this._avgSpeed=null,this._requestID=0,this._progressID=0,this._started=!1,this._firstProgress=!0,this._deferredProgress,this._timingLabels={start:null,progress:null,end:null,measure:null},this.on("xhr-upload-loadstart",function(){return a.mark(n._timingLabels.start)}),this.on("xhr-readystatechange",function(t){n._started||t.readyState!=XMLHttpRequest.LOADING||(a.mark(n._timingLabels.start),n._started=!0)});var s="upload"==t?"xhr-upload":"xhr";this.on(""+s+"-progress",function(t,e){return n._progress(e)}),this.on(""+s+"-timeout",function(){return n._timeout()}),this.on(""+s+"-loadend",function(){return n._end()})}return s(e,t),r(e,{start:{value:function(){var t=this._loadingType,e=this.settings().data,n=this._requestID++;this._intendedEnd=!1,this._lastLoadedValue=null,this._speedRecords=[],this._started=!1,this._firstProgress=!0,this._deferredProgress=l(),this._isRestarting||this.trigger("start",e.size);var r=this._timingLabels;r.start=""+t+"-"+n+"-start",r.progress=""+t+"-"+n+"-progress",r.end=""+t+"-"+n+"-end",r.measure=""+t+"-"+n+"-measure";var i="upload"==t?new Blob([new ArrayBuffer(e.size)]):null,s="download"==t?"GET":"POST";return this._newRequest(s,{size:e.size})._sendRequest(i),this}},abort:{value:function(){return this._intendedEnd=!0,this._abort()}},_progress:{value:function(t){var e=this;if(this._firstProgress)return this._firstProgress=!1;this._deferredProgress.run();var n=this._timingLabels,r=this._progressID++,i=""+n.progress+"-"+r,s=t.loaded;a.mark(i);var u,o=a.measure(""+n.measure+"-avg-"+r,n.start,i),c=s/o*1e3;if(this._lastLoadedValue){var f=a.measure(""+n.measure+"-instant-"+r,""+n.progress+"-"+(r-1),i);u=(s-this._lastLoadedValue)/f*1e3}else u=c;return this._lastLoadedValue=s,this._deferredProgress=l(function(){e._avgSpeed=c,e._speedRecords.push(u),e.trigger("progress",c,u)}),this}},_timeout:{value:function(){return this._intendedEnd=!0,this}},_end:{value:function(){if(this._intendedEnd)this._isRestarting=!1,this.trigger("end",this._avgSpeed,this._speedRecords);else{var t=(this._loadingType,this.settings().data);t.size*=t.multiplier,this.trigger("restart",t.size),this._isRestarting=!0,this.start()}return this}}}),e}(o);e.exports=c},{"../../utils/helpers":7,"../Timing":6,"./HttpModule":4}],4:[function(t,e){"use strict";var n=function(t){return t&&t.__esModule?t["default"]:t},r=Array.prototype.slice,i=function(){function t(t,e){for(var n in e){var r=e[n];r.configurable=!0,r.value&&(r.writable=!0)}Object.defineProperties(t,e)}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),s=function p(t,e,n){var r=Object.getOwnPropertyDescriptor(t,e);if(void 0===r){var i=Object.getPrototypeOf(t);return null===i?void 0:p(i,e,n)}if("value"in r&&r.writable)return r.value;var s=r.get;return void 0===s?void 0:s.call(n)},u=function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(t.__proto__=e)},o=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},a=n(t("../EventDispatcher")),l=t("../../utils/helpers"),c=l.isObject,f=l.assign,d=l.assignStrict,h=function(t){function e(t){var n=this,r=void 0===arguments[1]?{}:arguments[1];o(this,e),s(Object.getPrototypeOf(e.prototype),"constructor",this).call(this),this._extendDefaultSettings({endpoint:"./network.php",delay:8e3}),this.settings(r),this._moduleName=t,this._xhr=null,this._lastURLToken=null,this._requestingOverridden=!1,this._requesting=!1,this.on(["xhr-loadstart","xhr-upload-loadstart"],function(){n._requestingOverridden||(n._requesting=!0)}),this.on(["xhr-loadend","xhr-upload-loadend"],function(){n._requestingOverridden||(n._requesting=!1)})}return u(e,t),i(e,{settings:{value:function(t){var e=function(){return t.apply(this,arguments)};return e.toString=function(){return t.toString()},e}(function(){var t=void 0===arguments[0]?null:arguments[0];return c(t)?(this._settings=d(this._defaultSettings||{},this._settings||{},t),this):this._settings||this._defaultSettings||{}})},isRequesting:{value:function(){return this._requesting}},_extendDefaultSettings:{value:function(t){return this._defaultSettings=f(this._defaultSettings||{},t),this}},_newRequest:{value:function(t,e){if(!this.trigger("_newRequest")&&!this._requestingOverridden)return console.warn("To ensure accurate measures, you can only make one request at a time."),this;var n=this.settings(),i=new XMLHttpRequest,s=["GET","POST"];if(!~s.indexOf(t))return console.warn("The HTTP method must be GET or POST."),this;e=e||{};var u=(new Date).getTime();this._lastURLToken="network-"+u;var o=n.endpoint;o+=~o.indexOf("?")?"&":"?",o+="module="+this._moduleName,Object.keys(e).forEach(function(t){var n=encodeURIComponent(e[t]);o+="&"+t+"="+n}),o+="&"+this._lastURLToken,i.open(t,o),i.timeout=n.delay,this._xhr&&this._xhr.readyState==XMLHttpRequest.OPENED&&this._xhr.abort(),this._xhr=i;var a=this,l=["loadstart","progress","abort","error","load","timeout","loadend","readystatechange"];return l.forEach(function(t){i.addEventListener(t,function(){("progress"!=t||a._requesting)&&a.trigger.apply(a,["xhr-"+t,i].concat(r.call(arguments)))}),"readystatechange"!=t&&i.upload.addEventListener(t,function(){a.trigger.apply(a,["xhr-upload-"+t,i].concat(r.call(arguments)))})}),this}},_sendRequest:{value:function(){var t=void 0===arguments[0]?null:arguments[0];return this._xhr&&this._xhr.readyState==XMLHttpRequest.OPENED?this._xhr.send(t):console.warn("A request must have been created before sending any data."),this}},_abort:{value:function(){return this._xhr&&this._xhr.abort(),this}},_getTimingEntry:{value:function(t){return setTimeout(function(e){return function(){var n=performance.getEntriesByType("resource").filter(function(t){return~t.name.indexOf(e)});t(n.length?n[0]:null)}}(this._lastURLToken),0),this}},_setRequesting:{value:function(t){return this._requestingOverridden=!0,this._requesting=t,this}}}),e}(a);e.exports=h},{"../../utils/helpers":7,"../EventDispatcher":2}],5:[function(t,e){"use strict";var n=function(t){return t&&t.__esModule?t["default"]:t},r=function(){function t(t,e){for(var n in e){var r=e[n];r.configurable=!0,r.value&&(r.writable=!0)}Object.defineProperties(t,e)}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),i=function p(t,e,n){var r=Object.getOwnPropertyDescriptor(t,e);if(void 0===r){var i=Object.getPrototypeOf(t);return null===i?void 0:p(i,e,n)}if("value"in r&&r.writable)return r.value;var s=r.get;return void 0===s?void 0:s.call(n)},s=function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(t.__proto__=e)},u=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},o=n(t("./HttpModule")),a=n(t("../Timing")),l=t("../../utils/helpers"),c=l.isObject,f=l.assignStrict,d=l.except,h=function(t){function e(){var t=this,n=void 0===arguments[0]?{}:arguments[0];u(this,e),this._extendDefaultSettings({measures:5,attempts:3}),i(Object.getPrototypeOf(e.prototype),"constructor",this).call(this,"latency"),this.settings(n),this._requestsLeft=0,this._attemptsLeft=0,this._latencies=[],this._requestID=0,this._timingLabels={start:null,end:null,measure:null},a.supportsResourceTiming()?this.on("xhr-load",function(){return t._measure()}):(this.on("xhr-loadstart",function(){return a.mark(t._timingLabels.start)}),this.on("xhr-readystatechange",function(e){return t._measure(e)}))}return s(e,t),r(e,{settings:{value:function(t){var e=function(){return t.apply(this,arguments)};return e.toString=function(){return t.toString()},e}(function(){var t=void 0===arguments[0]?null:arguments[0];return c(t)?i(Object.getPrototypeOf(e.prototype),"settings",this).call(this,f(t,{delay:0})):d(i(Object.getPrototypeOf(e.prototype),"settings",this).call(this),["delay"])})},start:{value:function(){var t=this.settings(),e=t.measures,n=t.attempts;return this._requestsLeft=e,this._attemptsLeft=n*e,a.supportsResourceTiming()||(this._requestsLeft++,this._attemptsLeft++),this._setRequesting(!0),this._latencies=[],this._nextRequest(),this}},_nextRequest:{value:function(){var t=this,e=void 0===arguments[0]?!1:arguments[0],n=this._requestID++,r=e?this._requestsLeft:this._requestsLeft--;if(this._attemptsLeft--&&(r||e)){var i=this._timingLabels;i.start="latency-"+n+"-start",i.end="latency-"+n+"-end",i.measure="latency-"+n+"-measure",this._newRequest("GET")._sendRequest()}else this._setRequesting(!1),setTimeout(function(){return t._end()},0);return this}},_measure:{value:function(){var t=this,e=void 0===arguments[0]?null:arguments[0];if(e)if(this._requestsLeft<this.settings().measures){if(e.readyState==XMLHttpRequest.HEADERS_RECEIVED){var n=this._timingLabels;a.mark(n.end);var r=a.measure(n.measure,n.start,n.end);r&&this._latencies.push(r),this._abort(),this._nextRequest(!r)}}else this._nextRequest();else this._getTimingEntry(function(e){var n=e.secureConnectionStart?e.secureConnectionStart-e.connectStart:e.connectEnd-e.connectStart;n&&t._latencies.push(n),t._nextRequest(!n)});return this}},_end:{value:function(){var t=this._latencies,e=t.reduce(function(t,e){return t+e},0)/(t.length||1);if(e=e||null,t.length<this.settings().measures){var n=this.settings(),r=n.measures,i=n.attempts;console.warn(["An insufficient number of measures have been processed, this could be due to your web server using","persistant connections or to your client settings (measures: "+r+", attempts: "+i+")"].join(" "))}return this.trigger("end",e,t),this}}}),e}(o);e.exports=h},{"../../utils/helpers":7,"../Timing":6,"./HttpModule":4}],6:[function(t,e){"use strict";var n=function(){function t(t,e){for(var n in e){var r=e[n];r.configurable=!0,r.value&&(r.writable=!0)}Object.defineProperties(t,e)}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),r=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},i=function(){function t(){r(this,t),this._marks={},this._measures={},this._support={performance:!!window.performance,userTiming:window.performance&&performance.mark,resourceTiming:window.performance&&"function"==typeof performance.getEntriesByType&&performance.timing}}return n(t,{mark:{value:function(t){var e=this._support,n=this._marks;return e.userTiming?performance.mark(t):n[t]=e.performance?performance.now():(new Date).getTime(),this}},measure:{value:function(t,e,n){var r=this._support,i=this._marks,s=this._measures;return"undefined"==typeof s[t]&&(r.userTiming?(performance.measure(t,e,n),s[t]=performance.getEntriesByName(t)[0].duration):s[t]=i[n]-i[e]),s[t]}},supportsResourceTiming:{value:function(){return this._support.resourceTiming}}}),t}();e.exports=new i},{}],7:[function(t,e,n){"use strict";function r(t){return void 0!=t&&null!=t&&"object"==typeof t.valueOf()}function i(t){return JSON.parse(JSON.stringify(t))}function s(t){for(var e=arguments.length,n=Array(e>2?e-2:0),u=2;e>u;u++)n[u-2]=arguments[u];var o=void 0===arguments[1]?{}:arguments[1];return o=i(o),n.forEach(function(e){Object.keys(e).forEach(function(n){if(!t||o.hasOwnProperty(n)){var i=e[n];o[n]=r(i)?s(t,o[n],i):i}})}),o}function u(){for(var t=arguments.length,e=Array(t>1?t-1:0),n=1;t>n;n++)e[n-1]=arguments[n];var r=void 0===arguments[0]?{}:arguments[0];return s.apply(void 0,[!1,r].concat(e))}function o(){for(var t=arguments.length,e=Array(t>1?t-1:0),n=1;t>n;n++)e[n-1]=arguments[n];var r=void 0===arguments[0]?{}:arguments[0];return s.apply(void 0,[!0,r].concat(e))}function a(t,e){var n=i(t);return e.forEach(function(t){return delete n[t]}),n}function l(){var t=void 0===arguments[0]?function(){}:arguments[0];return new(function(){var e=function(){f(this,e),this.func=t};return c(e,{run:{value:function(){this.func&&this.func(),delete this.func}}}),e}())}var c=function(){function t(t,e){for(var n in e){var r=e[n];r.configurable=!0,r.value&&(r.writable=!0)}Object.defineProperties(t,e)}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),f=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")};n.isObject=r,n.copy=i,n.assign=u,n.assignStrict=o,n.except=a,n.defer=l,Object.defineProperty(n,"__esModule",{value:!0})},{}]},{},[1])(1)}); | ||
!function(t){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var e;e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,e.Network=t()}}(function(){return function t(e,n,r){function i(u,o){if(!n[u]){if(!e[u]){var a="function"==typeof require&&require;if(!o&&a)return a(u,!0);if(s)return s(u,!0);var l=new Error("Cannot find module '"+u+"'");throw l.code="MODULE_NOT_FOUND",l}var c=n[u]={exports:{}};e[u][0].call(c.exports,function(t){var n=e[u][1][t];return i(n?n:t)},c,c.exports,t,e,n,r)}return n[u].exports}for(var s="function"==typeof require&&require,u=0;u<r.length;u++)i(r[u]);return i}({1:[function(t,e){"use strict";var n=function(t){return t&&t.__esModule?t["default"]:t},r=function(t,e,n){return Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0})},i=function(){function t(t,e){for(var n in e){var r=e[n];r.configurable=!0,r.value&&(r.writable=!0)}Object.defineProperties(t,e)}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),s=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},u=n(t("./EventDispatcher")),o=n(t("./Http/HttpModule")),a=n(t("./Http/LatencyModule")),l=n(t("./Http/BandwidthModule")),c=n(t("./Timing")),f=t("../utils/helpers"),d=f.getGlobalObject,h=f.isObject,p=f.assign,g=f.except,v=function(){function t(){var e=void 0===arguments[0]?{}:arguments[0];s(this,t),this._modules={},this._modulesInitialized=!1,this._pendingSettings={},this._registerModule("latency",function(t){return new a(t)})._registerModule("upload",function(t){return new l("upload",t)})._registerModule("download",function(t){return new l("download",t)}),this._initModules(this.settings(e))}return i(t,{settings:{value:function(t){var e=function(){return t.apply(this,arguments)};return e.toString=function(){return t.toString()},e}(function(){var t=this,e=void 0===arguments[0]?null:arguments[0],n=Object.keys(this._modules);if(!h(e))return n.reduce(function(e,n){return p(e,r({},n,t._modules[n].settings()))},{});var i=function(){var i=g(e,n),s=g(e,Object.keys(i));return e=n.reduce(function(t,e){return p(t,r({},e,i))},{}),e=p(e,s),t._modulesInitialized?Object.keys(t._modules).forEach(function(n){t._modules[n].settings(e[n])}):t._pendingSettings=e,{v:t}}();return"object"==typeof i?i.v:void 0})},isRequesting:{value:function(){var t=!1;for(var e in this._modules)this._modules.hasOwnProperty(e)&&(t=t||this._modules[e].isRequesting());return t}},_registerModule:{value:function(t,e){return this._modules[t]=e,this}},_initModules:{value:function(){var t=this;return this._modulesInitialized||(Object.keys(this._modules).forEach(function(e){t._modules[e]=t._modules[e](t._pendingSettings[e]).on("_newRequest",function(){return!t.isRequesting()}),t[e]=t._modules[e]}),this._modulesInitialized=!0),this}}},{_exposeInternalClasses:{value:function(){var t=d(),e={EventDispatcher:u,HttpModule:o,LatencyModule:a,BandwidthModule:l,Timing:c};return Object.keys(e).forEach(function(n){t[n]=e[n]}),this}}}),t}();e.exports=v},{"../utils/helpers":7,"./EventDispatcher":2,"./Http/BandwidthModule":3,"./Http/HttpModule":4,"./Http/LatencyModule":5,"./Timing":6}],2:[function(t,e){"use strict";var n=function(){function t(t,e){for(var n in e){var r=e[n];r.configurable=!0,r.value&&(r.writable=!0)}Object.defineProperties(t,e)}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),r=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},i=function(){function t(){r(this,t),this._eventCallbacks={}}return n(t,{on:{value:function(t,e){var n=this;return t=Array.isArray(t)?t:[t],t.forEach(function(t){var r=n._eventCallbacks[t]=n._eventCallbacks[t]||[];~r.indexOf(e)||r.push(e)}),this}},off:{value:function(t){var e=this,n=void 0===arguments[1]?null:arguments[1];return t=Array.isArray(t)?t:[t],t.forEach(function(t){var r=e._eventCallbacks[t];if(!n&&r)delete e._eventCallbacks[t];else{var i=r?r.indexOf(n):-1;-1!=i&&r.splice(i,1)}}),this}},trigger:{value:function(t){for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;r++)n[r-1]=arguments[r];var i=this._eventCallbacks[t]||[],s=!0;return i.forEach(function(t){var e=t.apply(void 0,n);e=e!==!1?!0:!1,s=s&&e}),s}}}),t}();e.exports=i},{}],3:[function(t,e){"use strict";var n=function(t){return t&&t.__esModule?t["default"]:t},r=function(){function t(t,e){for(var n in e){var r=e[n];r.configurable=!0,r.value&&(r.writable=!0)}Object.defineProperties(t,e)}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),i=function f(t,e,n){var r=Object.getOwnPropertyDescriptor(t,e);if(void 0===r){var i=Object.getPrototypeOf(t);return null===i?void 0:f(i,e,n)}if("value"in r&&r.writable)return r.value;var s=r.get;return void 0===s?void 0:s.call(n)},s=function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(t.__proto__=e)},u=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},o=n(t("./HttpModule")),a=n(t("../Timing")),l=t("../../utils/helpers").defer,c=function(t){function e(t){var n=this,r=void 0===arguments[1]?{}:arguments[1];u(this,e),t=~["upload","download"].indexOf(t)?t:"download",this._extendDefaultSettings({data:{size:"upload"==t?2097152:10485760,multiplier:2}}),i(Object.getPrototypeOf(e.prototype),"constructor",this).call(this,t,r),this._loadingType=t,this._intendedEnd=!1,this._isRestarting=!1,this._lastLoadedValue=null,this._speedRecords=[],this._avgSpeed=null,this._requestID=0,this._progressID=0,this._started=!1,this._firstProgress=!0,this._deferredProgress,this._timingLabels={start:null,progress:null,end:null,measure:null},this.on("xhr-upload-loadstart",function(){return a.mark(n._timingLabels.start)}),this.on("xhr-readystatechange",function(t){n._started||t.readyState!=XMLHttpRequest.LOADING||(a.mark(n._timingLabels.start),n._started=!0)});var s="upload"==t?"xhr-upload":"xhr";this.on(""+s+"-progress",function(t,e){return n._progress(e)}),this.on(""+s+"-timeout",function(){return n._timeout()}),this.on(""+s+"-loadend",function(){return n._end()})}return s(e,t),r(e,{start:{value:function(){var t=this._loadingType,e=this.settings().data,n=this._requestID++;this._intendedEnd=!1,this._lastLoadedValue=null,this._speedRecords=[],this._started=!1,this._firstProgress=!0,this._deferredProgress=l(),this._isRestarting||this.trigger("start",e.size);var r=this._timingLabels;r.start=""+t+"-"+n+"-start",r.progress=""+t+"-"+n+"-progress",r.end=""+t+"-"+n+"-end",r.measure=""+t+"-"+n+"-measure";var i="upload"==t?new Blob([new ArrayBuffer(e.size)]):null,s="download"==t?"GET":"POST";return this._newRequest(s,{size:e.size})._sendRequest(i),this}},abort:{value:function(){return this._intendedEnd=!0,this._abort()}},_progress:{value:function(t){var e=this;if(this._firstProgress)return this._firstProgress=!1;this._deferredProgress.run();var n=this._timingLabels,r=this._progressID++,i=""+n.progress+"-"+r,s=t.loaded;a.mark(i);var u,o=a.measure(""+n.measure+"-avg-"+r,n.start,i),c=s/o*1e3;if(this._lastLoadedValue){var f=a.measure(""+n.measure+"-instant-"+r,""+n.progress+"-"+(r-1),i);u=(s-this._lastLoadedValue)/f*1e3}else u=c;return this._lastLoadedValue=s,this._deferredProgress=l(function(){e._avgSpeed=c,e._speedRecords.push(u),e.trigger("progress",c,u)}),this}},_timeout:{value:function(){return this._intendedEnd=!0,this}},_end:{value:function(){if(this._intendedEnd)this._isRestarting=!1,this.trigger("end",this._avgSpeed,this._speedRecords);else{var t=(this._loadingType,this.settings().data);t.size*=t.multiplier,this.trigger("restart",t.size),this._isRestarting=!0,this.start()}return this}}}),e}(o);e.exports=c},{"../../utils/helpers":7,"../Timing":6,"./HttpModule":4}],4:[function(t,e){"use strict";var n=function(t){return t&&t.__esModule?t["default"]:t},r=Array.prototype.slice,i=function(){function t(t,e){for(var n in e){var r=e[n];r.configurable=!0,r.value&&(r.writable=!0)}Object.defineProperties(t,e)}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),s=function p(t,e,n){var r=Object.getOwnPropertyDescriptor(t,e);if(void 0===r){var i=Object.getPrototypeOf(t);return null===i?void 0:p(i,e,n)}if("value"in r&&r.writable)return r.value;var s=r.get;return void 0===s?void 0:s.call(n)},u=function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(t.__proto__=e)},o=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},a=n(t("../EventDispatcher")),l=t("../../utils/helpers"),c=l.isObject,f=l.assign,d=l.assignStrict,h=function(t){function e(t){var n=this,r=void 0===arguments[1]?{}:arguments[1];o(this,e),s(Object.getPrototypeOf(e.prototype),"constructor",this).call(this),this._extendDefaultSettings({endpoint:"./network.php",delay:8e3}),this.settings(r),this._moduleName=t,this._xhr=null,this._lastURLToken=null,this._requestingOverridden=!1,this._requesting=!1,this.on(["xhr-loadstart","xhr-upload-loadstart"],function(){n._requestingOverridden||(n._requesting=!0)}),this.on(["xhr-loadend","xhr-upload-loadend"],function(){n._requestingOverridden||(n._requesting=!1)})}return u(e,t),i(e,{settings:{value:function(t){var e=function(){return t.apply(this,arguments)};return e.toString=function(){return t.toString()},e}(function(){var t=void 0===arguments[0]?null:arguments[0];return c(t)?(this._settings=d(this._defaultSettings||{},this._settings||{},t),this):this._settings||this._defaultSettings||{}})},isRequesting:{value:function(){return this._requesting}},_extendDefaultSettings:{value:function(t){return this._defaultSettings=f(this._defaultSettings||{},t),this}},_newRequest:{value:function(t,e){if(!this.trigger("_newRequest")&&!this._requestingOverridden)return console.warn("To ensure accurate measures, you can only make one request at a time."),this;var n=this.settings(),i=new XMLHttpRequest,s=["GET","POST"];if(!~s.indexOf(t))return console.warn("The HTTP method must be GET or POST."),this;e=e||{};var u=(new Date).getTime();this._lastURLToken="network-"+u;var o=n.endpoint;o+=~o.indexOf("?")?"&":"?",o+="module="+this._moduleName,Object.keys(e).forEach(function(t){var n=encodeURIComponent(e[t]);o+="&"+t+"="+n}),o+="&"+this._lastURLToken,i.open(t,o),i.timeout=n.delay,this._xhr&&this._xhr.readyState==XMLHttpRequest.OPENED&&this._xhr.abort(),this._xhr=i;var a=this,l=["loadstart","progress","abort","error","load","timeout","loadend","readystatechange"];return l.forEach(function(t){i.addEventListener(t,function(){("progress"!=t||a._requesting)&&a.trigger.apply(a,["xhr-"+t,i].concat(r.call(arguments)))}),"readystatechange"!=t&&i.upload.addEventListener(t,function(){a.trigger.apply(a,["xhr-upload-"+t,i].concat(r.call(arguments)))})}),this}},_sendRequest:{value:function(){var t=void 0===arguments[0]?null:arguments[0];return this._xhr&&this._xhr.readyState==XMLHttpRequest.OPENED?this._xhr.send(t):console.warn("A request must have been created before sending any data."),this}},_abort:{value:function(){return this._xhr&&this._xhr.abort(),this}},_getTimingEntry:{value:function(t){return setTimeout(function(e){return function(){var n=performance.getEntriesByType("resource").filter(function(t){return~t.name.indexOf(e)});t(n.length?n[0]:null)}}(this._lastURLToken),0),this}},_setRequesting:{value:function(t){return this._requestingOverridden=!0,this._requesting=t,this}}}),e}(a);e.exports=h},{"../../utils/helpers":7,"../EventDispatcher":2}],5:[function(t,e){"use strict";var n=function(t){return t&&t.__esModule?t["default"]:t},r=function(){function t(t,e){for(var n in e){var r=e[n];r.configurable=!0,r.value&&(r.writable=!0)}Object.defineProperties(t,e)}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),i=function p(t,e,n){var r=Object.getOwnPropertyDescriptor(t,e);if(void 0===r){var i=Object.getPrototypeOf(t);return null===i?void 0:p(i,e,n)}if("value"in r&&r.writable)return r.value;var s=r.get;return void 0===s?void 0:s.call(n)},s=function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(t.__proto__=e)},u=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},o=n(t("./HttpModule")),a=n(t("../Timing")),l=t("../../utils/helpers"),c=l.isObject,f=l.assignStrict,d=l.except,h=function(t){function e(){var t=this,n=void 0===arguments[0]?{}:arguments[0];u(this,e),this._extendDefaultSettings({measures:5,attempts:3}),i(Object.getPrototypeOf(e.prototype),"constructor",this).call(this,"latency"),this.settings(n),this._requestsLeft=0,this._attemptsLeft=0,this._latencies=[],this._requestID=0,this._timingLabels={start:null,end:null,measure:null},a.supportsResourceTiming()?this.on("xhr-load",function(){return t._measure()}):(this.on("xhr-loadstart",function(){return a.mark(t._timingLabels.start)}),this.on("xhr-readystatechange",function(e){return t._measure(e)}))}return s(e,t),r(e,{settings:{value:function(t){var e=function(){return t.apply(this,arguments)};return e.toString=function(){return t.toString()},e}(function(){var t=void 0===arguments[0]?null:arguments[0];return c(t)?i(Object.getPrototypeOf(e.prototype),"settings",this).call(this,f(t,{delay:0})):d(i(Object.getPrototypeOf(e.prototype),"settings",this).call(this),["delay"])})},start:{value:function(){var t=this.settings(),e=t.measures,n=t.attempts;return this._requestsLeft=e,this._attemptsLeft=n*e,a.supportsResourceTiming()||(this._requestsLeft++,this._attemptsLeft++),this._setRequesting(!0),this._latencies=[],this._nextRequest(),this}},_nextRequest:{value:function(){var t=this,e=void 0===arguments[0]?!1:arguments[0],n=this._requestID++,r=e?this._requestsLeft:this._requestsLeft--;if(this._attemptsLeft--&&(r||e)){var i=this._timingLabels;i.start="latency-"+n+"-start",i.end="latency-"+n+"-end",i.measure="latency-"+n+"-measure",this._newRequest("GET")._sendRequest()}else this._setRequesting(!1),setTimeout(function(){return t._end()},0);return this}},_measure:{value:function(){var t=this,e=void 0===arguments[0]?null:arguments[0];if(e)if(this._requestsLeft<this.settings().measures){if(e.readyState==XMLHttpRequest.HEADERS_RECEIVED){var n=this._timingLabels;a.mark(n.end);var r=a.measure(n.measure,n.start,n.end);r&&this._latencies.push(r),this._abort(),this._nextRequest(!r)}}else this._nextRequest();else this._getTimingEntry(function(e){var n=e.secureConnectionStart?e.secureConnectionStart-e.connectStart:e.connectEnd-e.connectStart;n&&t._latencies.push(n),t._nextRequest(!n)});return this}},_end:{value:function(){var t=this._latencies,e=t.reduce(function(t,e){return t+e},0)/(t.length||1);if(e=e||null,t.length<this.settings().measures){var n=this.settings(),r=n.measures,i=n.attempts;console.warn(["An insufficient number of measures have been processed, this could be due to your web server using","persistant connections or to your client settings (measures: "+r+", attempts: "+i+")"].join(" "))}return this.trigger("end",e,t),this}}}),e}(o);e.exports=h},{"../../utils/helpers":7,"../Timing":6,"./HttpModule":4}],6:[function(t,e){"use strict";var n=function(){function t(t,e){for(var n in e){var r=e[n];r.configurable=!0,r.value&&(r.writable=!0)}Object.defineProperties(t,e)}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),r=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},i=t("../utils/helpers").getGlobalObject,s=function(){function t(){r(this,t);var e=i();this._support={performance:!!e.performance,userTiming:e.performance&&performance.mark,resourceTiming:e.performance&&"function"==typeof performance.getEntriesByType&&performance.timing},this._marks={},this._measures={}}return n(t,{mark:{value:function(t){var e=this._support,n=this._marks;return e.userTiming&&performance.mark(t),n[t]=e.performance?performance.now():(new Date).getTime(),this}},measure:{value:function(t,e,n){var r=this._support,i=this._marks,s=this._measures;if("undefined"==typeof s[t]){var u=i[n]-i[e];if(r.userTiming){performance.measure(t,e,n);var o=performance.getEntriesByName(t);s[t]=o.length?o[0].duration:u}else s[t]=u}return s[t]}},supportsResourceTiming:{value:function(){return this._support.resourceTiming}}}),t}();e.exports=new s},{"../utils/helpers":7}],7:[function(t,e,n){(function(t){"use strict";function e(){return"undefined"!=typeof self?self:"undefined"!=typeof t?t:new Function("return this")()}function r(t){return void 0!=t&&null!=t&&"object"==typeof t.valueOf()}function i(t){return JSON.parse(JSON.stringify(t))}function s(t){for(var e=arguments.length,n=Array(e>2?e-2:0),u=2;e>u;u++)n[u-2]=arguments[u];var o=void 0===arguments[1]?{}:arguments[1];return o=i(o),n.forEach(function(e){Object.keys(e).forEach(function(n){if(!t||o.hasOwnProperty(n)){var i=e[n];o[n]=r(i)?s(t,o[n],i):i}})}),o}function u(){for(var t=arguments.length,e=Array(t>1?t-1:0),n=1;t>n;n++)e[n-1]=arguments[n];var r=void 0===arguments[0]?{}:arguments[0];return s.apply(void 0,[!1,r].concat(e))}function o(){for(var t=arguments.length,e=Array(t>1?t-1:0),n=1;t>n;n++)e[n-1]=arguments[n];var r=void 0===arguments[0]?{}:arguments[0];return s.apply(void 0,[!0,r].concat(e))}function a(t,e){var n=i(t);return e.forEach(function(t){return delete n[t]}),n}function l(){var t=void 0===arguments[0]?function(){}:arguments[0];return new(function(){var e=function(){f(this,e),this.func=t};return c(e,{run:{value:function(){this.func&&this.func(),delete this.func}}}),e}())}var c=function(){function t(t,e){for(var n in e){var r=e[n];r.configurable=!0,r.value&&(r.writable=!0)}Object.defineProperties(t,e)}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),f=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")};n.getGlobalObject=e,n.isObject=r,n.copy=i,n.assign=u,n.assignStrict=o,n.except=a,n.defer=l,Object.defineProperty(n,"__esModule",{value:!0})}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{}]},{},[1])(1)}); | ||
//# sourceMappingURL=network.min.js.map |
@@ -6,3 +6,3 @@ import EventDispatcher from './EventDispatcher'; | ||
import Timing from './Timing'; | ||
import {isObject, assign, except} from '../utils/helpers'; | ||
import {getGlobalObject, isObject, assign, except} from '../utils/helpers'; | ||
@@ -30,5 +30,5 @@ /** | ||
* @param {Network~settingsObject} [settings={}] A set of custom settings. | ||
* @property {LatencyModule} latency The latency module. | ||
* @property {BandwidthModule} upload The upload module. | ||
* @property {BandwidthModule} download The download module. | ||
* @member {LatencyModule} latency The latency module. | ||
* @member {BandwidthModule} upload The upload module. | ||
* @member {BandwidthModule} download The download module. | ||
*/ | ||
@@ -45,6 +45,7 @@ export default class Network { | ||
{ | ||
var classes = {EventDispatcher, HttpModule, LatencyModule, BandwidthModule, Timing}; | ||
let global = getGlobalObject(), | ||
classes = {EventDispatcher, HttpModule, LatencyModule, BandwidthModule, Timing}; | ||
Object.keys(classes).forEach(name => { | ||
window[name] = classes[name]; | ||
global[name] = classes[name]; | ||
}); | ||
@@ -51,0 +52,0 @@ |
@@ -0,1 +1,3 @@ | ||
import {getGlobalObject} from '../utils/helpers'; | ||
/** | ||
@@ -9,13 +11,33 @@ * @private | ||
{ | ||
this._marks = {}; | ||
this._measures = {}; | ||
const global = getGlobalObject(); | ||
// Does the browser support the following APIs? | ||
/** | ||
* Defines if the current browser supports some specific Timing APIs. | ||
* @private | ||
* @member {Object} _support | ||
* @property {boolean} performance `true` if the Performance API is available. | ||
* @property {boolean} userTiming `true` if the User Timing API is available. | ||
* @property {boolean} resourceTiming `true` if the Resource Timing API is available. | ||
*/ | ||
this._support = { | ||
performance: !!window.performance, | ||
userTiming: window.performance && performance.mark, | ||
resourceTiming: window.performance | ||
performance: !!global.performance, | ||
userTiming: global.performance && performance.mark, | ||
resourceTiming: global.performance | ||
&& (typeof performance.getEntriesByType == "function") | ||
&& performance.timing | ||
}; | ||
/** | ||
* Contains all the marks created by the `mark` method. | ||
* @private | ||
* @member {Object} _marks | ||
*/ | ||
this._marks = {}; | ||
/** | ||
* Contains all the measures created by the `measure` method. | ||
* @private | ||
* @member {Object} _measures | ||
*/ | ||
this._measures = {}; | ||
} | ||
@@ -32,8 +54,10 @@ | ||
{ | ||
var support = this._support, | ||
marks = this._marks; | ||
const support = this._support, | ||
marks = this._marks; | ||
if (support.userTiming) { | ||
performance.mark(label); | ||
} else if (support.performance) { | ||
} | ||
if (support.performance) { | ||
marks[label] = performance.now(); | ||
@@ -58,12 +82,18 @@ } else { | ||
{ | ||
var support = this._support, | ||
marks = this._marks, | ||
measures = this._measures; | ||
const support = this._support, | ||
marks = this._marks, | ||
measures = this._measures; | ||
if (typeof measures[measureLabel] == 'undefined') { | ||
const measureWithoutUserTiming = marks[markLabelB] - marks[markLabelA]; | ||
if (support.userTiming) { | ||
performance.measure(measureLabel, markLabelA, markLabelB); | ||
measures[measureLabel] = performance.getEntriesByName(measureLabel)[0].duration; | ||
const entriesByName = performance.getEntriesByName(measureLabel); | ||
// The performance API could return no corresponding entries in Firefox so we must use a fallback. | ||
// See: https://github.com/nesk/network.js/issues/32#issuecomment-118434305 | ||
measures[measureLabel] = entriesByName.length ? entriesByName[0].duration : measureWithoutUserTiming; | ||
} else { | ||
measures[measureLabel] = marks[markLabelB] - marks[markLabelA]; | ||
measures[measureLabel] = measureWithoutUserTiming; | ||
} | ||
@@ -70,0 +100,0 @@ } |
{ | ||
"name": "network-js", | ||
"version": "2.0.0-alpha3", | ||
"version": "2.0.0-alpha4", | ||
"description": "Make accurate network measures in JavaScript", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -10,3 +10,3 @@ # Network.js [![Build Status](https://travis-ci.org/nesk/network.js.svg?branch=master)](https://travis-ci.org/nesk/network.js) | ||
```shell | ||
bower install --save network-js#2.0.0-alpha2 | ||
bower install --save network-js#master | ||
``` | ||
@@ -13,0 +13,0 @@ |
/** | ||
* Return the global object. | ||
* @private | ||
* @function getGlobalObject | ||
* @return {Object} | ||
* @see https://gist.github.com/rauschma/1bff02da66472f555c75 | ||
*/ | ||
export function getGlobalObject() { | ||
// Workers don’t have `window`, only `self`. | ||
if (typeof self !== 'undefined') { | ||
return self; | ||
} | ||
if (typeof global !== 'undefined') { | ||
return global; | ||
} | ||
// Not all environments allow `eval` and `Function`, use only as a last resort. | ||
return new Function('return this')(); | ||
} | ||
/** | ||
* Determine if the provided value is an object. | ||
@@ -3,0 +24,0 @@ * @private |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
353352
2913
3