next-rum
Version:
RUM Component for Next.js
156 lines (132 loc) • 4.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.prefix = prefix;
exports.timeOrigin = timeOrigin;
exports.find = find;
exports.entries = entries;
exports.default = purrformance;
/**
* Checks if a method needs to be vendor prefixed.
*
* @param {Object} where Object where we check if prop exists.
* @param {String} prop Name of the property to check.
* @returns {String|Undefined} Correct name of the property to use.
* @private
*/
function prefix() {
var where = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var prop = arguments[1];
if (prop in where) return prop;
var prefixes = ['webkit', 'ms', 'moz'];
for (var i = 0; i < prefixes.length; i++) {
var vendor = prefixes[i];
var prefixed = vendor + prop.slice(0, 1).toUpperCase() + prop.slice(1);
if (prefixed in where) return prefixed;
}
}
/**
* In order to correctly calculate the when a resource in the ResourceAPI
* was made we need to know when the high resolution timer started at 0.
* New browsers support the `timeOrigin` property that contains the EPOCH
* of when the timer started. For older browsers we default to the first
* known timing event, `navigationStart`.
*
* @returns {Number} Epoch of when the high resolution timers were inititlized
* @public
*/
function timeOrigin() {
var origin = purrformance('timeOrigin');
return origin || (purrformance('timing') || {}).navigationStart;
}
/**
* Find an resource entry that matches a given Regular Expression for the entry
* name.
*
* @param {Array} resources All resources.
* @param {RegExp} regexp The regexp that needs to match.
* @returns {Object|Undefined} The entry that is found.
*/
function find() {
var resources = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
var regexp = arguments[1];
for (var i = 0; i < resources.length; i++) {
var entry = resources[i];
if (entry && entry.name && regexp.test(entry.name)) {
return entry;
}
}
}
/**
* Get all entries that happend during the navigation cycle.
*
* @param {Object} between Start and end range where the request is made.
* @returns {Resources} Resources that were gathered.
* @public
*/
function entries(_ref) {
var start = _ref.start,
end = _ref.end;
var resources = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : purrformance('getEntriesByType', 'resource');
var origin = timeOrigin();
var contains = ['Start', // transform keys like: responseStart, redirectStart etc
'Time', // transform the startTime
'End' // and all responseEnd
];
return (resources || []).sort(function (a, b) {
if (a.fetchStart !== b.fetchStart) return a.fetchStart - b.fetchStart;
return (a.responseStart || a.responseEnd) - (b.responseStart || b.responseEnd);
}).map(function (entry) {
var data = {};
var _loop = function _loop(key) {
data[key] = entry[key];
//
// Normalize the high precision timers to EPOCH values as that is what
// the navigation timing is using.
//
if (contains.some(function (check) {
return !!~key.indexOf(check);
})) {
data[key] = origin + data[key];
}
};
for (var key in entry) {
_loop(key);
}
return data;
}).filter(function (_ref2) {
var startTime = _ref2.startTime;
return startTime >= start && startTime <= end;
});
}
/**
* Small helper function that allows us to safely interact with the
* performance API that is exposed in browsers.
*
* @param {String} method Name of the method we want to invoke.
* @param {Arguments} args Rest of the arguments that should be applied.
* @returns {Mixed} What ever the API returns.
* @private
*/
function purrformance(method) {
var perf = global[prefix(global, 'performance')];
if (perf) {
var name = prefix(perf, method);
if (typeof perf[name] === 'function') {
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
return perf[name].apply(perf, args);
} else {
return perf[name];
}
}
}
//
// Expose all methods on the purrformance method as well for easier exports.
//
purrformance.find = find;
purrformance.prefix = prefix;
purrformance.entries = entries;
purrformance.timeOrigin = timeOrigin;