From 6af733b321f0135a2fdd2f018ba27f578e0b3436 Mon Sep 17 00:00:00 2001 From: Nic Jansma Date: Tue, 16 Jul 2019 09:18:38 -0400 Subject: [PATCH] v1.2.4: Added skipDimensions option --- README.md | 4 +++- bower.json | 2 +- dist/resourcetiming-compression.min.js | 2 +- package-lock.json | 2 +- package.json | 2 +- src/resourcetiming-compression.js | 16 +++++++++------ test/test-resourcetiming-compression.js | 26 ++++++++++++++++++++++++- 7 files changed, 42 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 6f24972..3e8583f 100755 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # resourcetiming-compression.js -v1.2.3 +v1.2.4 [http://nicj.net](http://nicj.net) @@ -253,6 +253,8 @@ Or via ``gulp``: ## Version History +* v1.2.4 - 2019-07-16 + * Optional `skipDimensions` for `getResourceTiming()` and `compressResourceTiming()` * v1.2.3 - 2019-05-09 * Decode `initiatorType` above 9 properly * v1.2.2 - 2019-05-09 diff --git a/bower.json b/bower.json index 8313f36..24480d8 100755 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "resourcetiming-compression", - "version": "1.2.3", + "version": "1.2.4", "homepage": "https://github.com/nicjansma/resourcetiming-compression.js", "authors": [ "Nic Jansma ", diff --git a/dist/resourcetiming-compression.min.js b/dist/resourcetiming-compression.min.js index 1b29820..c911ad6 100755 --- a/dist/resourcetiming-compression.min.js +++ b/dist/resourcetiming-compression.min.js @@ -1 +1 @@ -!function(e){"use strict";function t(e,t,r){Array.prototype.forEach.call(e.ownerDocument.getElementsByTagName(r),function(r){e.href=r.currentSrc||r.src||r.getAttribute("xlink:href")||r.href,e.href.match(/^https?:\/\//)&&(t[e.href]=r)})}var r,n;"undefined"!=typeof e&&(r=e,n=r.ResourceTimingCompression);var i={};i.HOSTNAMES_REVERSED=!0,i.INITIATOR_TYPES={other:0,img:1,link:2,script:3,css:4,xmlhttprequest:5,html:6,image:7,beacon:8,fetch:9,iframe:"a",subdocument:"a",body:"b",input:"c",frame:"a",object:"d",video:"e",audio:"f",source:"g",track:"h",embed:"i",eventsource:"j",navigation:6},i.DEFAULT_XSS_BREAK_WORDS=[/(h)(ref)/gi,/(s)(rc)/gi,/(a)(ction)/gi],i.XSS_BREAK_DELIM="\n",i.DEFAULT_URL_LIMIT=500,i.SPECIAL_DATA_PREFIX="*",i.SPECIAL_DATA_DIMENSION_TYPE="0",i.SPECIAL_DATA_SIZE_TYPE="1",i.SPECIAL_DATA_SCRIPT_TYPE="2",i.SPECIAL_DATA_SCRIPT_ASYNC_ATTR=1,i.SPECIAL_DATA_SCRIPT_DEFER_ATTR=2,i.SPECIAL_DATA_SCRIPT_LOCAT_ATTR=4,i.SPECIAL_DATA_SERVERTIMING_TYPE="3",i.SPECIAL_DATA_LINK_ATTR_TYPE="4",i.REL_TYPES={prefetch:1,preload:2,prerender:3,stylesheet:4},i.HOSTNAME_REGEX=/^(https?:\/\/)([^\/]+)(.*)/,i.trimUrls=[],i.xssBreakWords=i.DEFAULT_XSS_BREAK_WORDS,i.noConflict=function(){return r.ResourceTimingCompression=n,i},i.convertToTrie=function(e){var t,r,n,o,a,s,c,d,u={};for(t in e)if(e.hasOwnProperty(t)){for(r=t,n=0;n10)return T;try{if(s=this.getNavStartTime(e),p=e.document.createElement("a"),t(p,E,"script"),t(p,g,"link"),e.frames)for(o=0;os&&(d=n+(c-s)),T=T.concat(this.findPerformanceEntriesForFrame(e.frames[o],!1,d,++i));try{if(f=e.location&&e.location.href,!("performance"in e&&e.performance&&e.performance.getEntriesByType))return T}catch(e){return T}r&&(a=e.performance.getEntriesByType("navigation"),a&&1===a.length?(u=a[0],T.push({name:e.location.href,startTime:0,initiatorType:"html",redirectStart:u.redirectStart,redirectEnd:u.redirectEnd,fetchStart:u.fetchStart,domainLookupStart:u.domainLookupStart,domainLookupEnd:u.domainLookupEnd,connectStart:u.connectStart,secureConnectionStart:u.secureConnectionStart,connectEnd:u.connectEnd,requestStart:u.requestStart,responseStart:u.responseStart,responseEnd:u.responseEnd,serverTiming:u.serverTiming||[]})):e.performance.timing&&(m=e.performance.timing,0!==m.navigationStart&&m.responseEnd<=m.navigationStart+36e5&&T.push({name:e.location.href,startTime:0,initiatorType:"html",redirectStart:m.redirectStart?m.redirectStart-m.navigationStart:0,redirectEnd:m.redirectEnd?m.redirectEnd-m.navigationStart:0,fetchStart:m.fetchStart?m.fetchStart-m.navigationStart:0,domainLookupStart:m.domainLookupStart?m.domainLookupStart-m.navigationStart:0,domainLookupEnd:m.domainLookupEnd?m.domainLookupEnd-m.navigationStart:0,connectStart:m.connectStart?m.connectStart-m.navigationStart:0,secureConnectionStart:m.secureConnectionStart?m.secureConnectionStart-m.navigationStart:0,connectEnd:m.connectEnd?m.connectEnd-m.navigationStart:0,requestStart:m.requestStart?m.requestStart-m.navigationStart:0,responseStart:m.responseStart?m.responseStart-m.navigationStart:0,responseEnd:m.responseEnd?m.responseEnd-m.navigationStart:0})));var h=e.performance.getEntriesByType("resource"),A=[];for(o=0;h&&or)break;("undefined"==typeof n||"*"===n||!n.length||a.initiatorType&&this.inArray(a.initiatorType,n))&&(i.accumulateServerTimingEntries(d,a.serverTiming),u.push(a))}var m=i.compressServerTiming(d);return{entries:u,serverTiming:{lookup:m,indexed:i.indexServerTiming(m)}}},i.compressSize=function(e){var t,r,n,i;return e.encodedBodySize||e.decodedBodySize||e.transferSize?(t=e.transferSize,r=e.encodedBodySize,n=e.decodedBodySize,i=[r,t?t-r:"_",n-r],i.map(this.toBase36).join(",").replace(/,+$/,"")):""},i.cleanupURL=function(e,t){var r;return e&&"[object Array]"!==Object.prototype.toString.call(e)?("undefined"!=typeof t&&e&&e.length>t&&(r=e.indexOf("?"),e=r!==-1&&r0?",":"")+o+a},"")),c=s=this.trimUrl(o.name,this.trimUrls),i.HOSTNAMES_REVERSED&&(c=this.reverseHostname(s)),void 0!==u[c]?u[c]+="|"+d:void 0!==m[s]?u[c]=i.SPECIAL_DATA_PREFIX+i.SPECIAL_DATA_DIMENSION_TYPE+m[s].map(this.toBase36).join(",").replace(/,+$/,"")+"|"+d:u[c]=d}return{restiming:this.optimizeTrie(this.convertToTrie(u),!0),servertiming:r.lookup}},i.reverseHostname=function(e){return e.replace(i.HOSTNAME_REGEX,function(e,t,r,n){return t+i.reverseString(r)+n})},i.reverseString=function(e){for(var t=e.length,r="";t--;)r+=e[t];return r},i.accumulateServerTimingEntries=function(e,t){(t||[]).forEach(function(t){"undefined"==typeof e[t.name]&&(e[t.name]={count:0,counts:{}});var r=e[t.name];r.counts[t.description]=r.counts[t.description]||0,r.counts[t.description]++,r.count++})},i.compressServerTiming=function(e){return Object.keys(e).sort(function(t,r){return e[r].count-e[t].count}).reduce(function(t,r){var n=Object.keys(e[r].counts).sort(function(t,n){return e[r].counts[n]-e[r].counts[t]});return t.push(1===n.length&&""===n[0]?r:[r].concat(n)),t},[])},i.indexServerTiming=function(e){return e.reduce(function(e,t,r){var n,i;return Array.isArray(t)?(n=t[0],i=t.slice(1).reduce(function(e,t,r){return e[t]=r,e},{})):(n=t,i={"":0}),e[n]={index:r,descriptions:i},e},{})},i.identifyServerTimingEntry=function(e,t){var r="";return e&&(r+=e),t&&(r+="."+t),r.length&&(r=":"+r),r},"function"==typeof define&&define.amd?define([],function(){return i}):"undefined"!=typeof module&&module.exports?module.exports=i:"undefined"!=typeof r&&(r.ResourceTimingCompression=i)}("undefined"!=typeof window?window:void 0); \ No newline at end of file +!function(e){"use strict";function t(e,t,r){Array.prototype.forEach.call(e.ownerDocument.getElementsByTagName(r),function(r){e.href=r.currentSrc||r.src||r.getAttribute("xlink:href")||r.href,e.href.match(/^https?:\/\//)&&(t[e.href]=r)})}var r,n;"undefined"!=typeof e&&(r=e,n=r.ResourceTimingCompression);var i={};i.HOSTNAMES_REVERSED=!0,i.INITIATOR_TYPES={other:0,img:1,link:2,script:3,css:4,xmlhttprequest:5,html:6,image:7,beacon:8,fetch:9,iframe:"a",subdocument:"a",body:"b",input:"c",frame:"a",object:"d",video:"e",audio:"f",source:"g",track:"h",embed:"i",eventsource:"j",navigation:6},i.DEFAULT_XSS_BREAK_WORDS=[/(h)(ref)/gi,/(s)(rc)/gi,/(a)(ction)/gi],i.XSS_BREAK_DELIM="\n",i.DEFAULT_URL_LIMIT=500,i.SPECIAL_DATA_PREFIX="*",i.SPECIAL_DATA_DIMENSION_TYPE="0",i.SPECIAL_DATA_SIZE_TYPE="1",i.SPECIAL_DATA_SCRIPT_TYPE="2",i.SPECIAL_DATA_SCRIPT_ASYNC_ATTR=1,i.SPECIAL_DATA_SCRIPT_DEFER_ATTR=2,i.SPECIAL_DATA_SCRIPT_LOCAT_ATTR=4,i.SPECIAL_DATA_SERVERTIMING_TYPE="3",i.SPECIAL_DATA_LINK_ATTR_TYPE="4",i.REL_TYPES={prefetch:1,preload:2,prerender:3,stylesheet:4},i.HOSTNAME_REGEX=/^(https?:\/\/)([^\/]+)(.*)/,i.trimUrls=[],i.xssBreakWords=i.DEFAULT_XSS_BREAK_WORDS,i.noConflict=function(){return r.ResourceTimingCompression=n,i},i.convertToTrie=function(e){var t,r,n,o,a,s,c,d,u={};for(t in e)if(e.hasOwnProperty(t)){for(r=t,n=0;n10)return T;try{if(s=this.getNavStartTime(e),p=e.document.createElement("a"),t(p,E,"script"),t(p,g,"link"),e.frames)for(o=0;os&&(d=n+(c-s)),T=T.concat(this.findPerformanceEntriesForFrame(e.frames[o],!1,d,++i));try{if(f=e.location&&e.location.href,!("performance"in e&&e.performance&&e.performance.getEntriesByType))return T}catch(e){return T}r&&(a=e.performance.getEntriesByType("navigation"),a&&1===a.length?(u=a[0],T.push({name:e.location.href,startTime:0,initiatorType:"html",redirectStart:u.redirectStart,redirectEnd:u.redirectEnd,fetchStart:u.fetchStart,domainLookupStart:u.domainLookupStart,domainLookupEnd:u.domainLookupEnd,connectStart:u.connectStart,secureConnectionStart:u.secureConnectionStart,connectEnd:u.connectEnd,requestStart:u.requestStart,responseStart:u.responseStart,responseEnd:u.responseEnd,serverTiming:u.serverTiming||[]})):e.performance.timing&&(m=e.performance.timing,0!==m.navigationStart&&m.responseEnd<=m.navigationStart+36e5&&T.push({name:e.location.href,startTime:0,initiatorType:"html",redirectStart:m.redirectStart?m.redirectStart-m.navigationStart:0,redirectEnd:m.redirectEnd?m.redirectEnd-m.navigationStart:0,fetchStart:m.fetchStart?m.fetchStart-m.navigationStart:0,domainLookupStart:m.domainLookupStart?m.domainLookupStart-m.navigationStart:0,domainLookupEnd:m.domainLookupEnd?m.domainLookupEnd-m.navigationStart:0,connectStart:m.connectStart?m.connectStart-m.navigationStart:0,secureConnectionStart:m.secureConnectionStart?m.secureConnectionStart-m.navigationStart:0,connectEnd:m.connectEnd?m.connectEnd-m.navigationStart:0,requestStart:m.requestStart?m.requestStart-m.navigationStart:0,responseStart:m.responseStart?m.responseStart-m.navigationStart:0,responseEnd:m.responseEnd?m.responseEnd-m.navigationStart:0})));var h=e.performance.getEntriesByType("resource"),A=[];for(o=0;h&&or)break;("undefined"==typeof n||"*"===n||!n.length||a.initiatorType&&this.inArray(a.initiatorType,n))&&(i.accumulateServerTimingEntries(d,a.serverTiming),u.push(a))}var m=i.compressServerTiming(d);return{entries:u,serverTiming:{lookup:m,indexed:i.indexServerTiming(m)}}},i.compressSize=function(e){var t,r,n,i;return e.encodedBodySize||e.decodedBodySize||e.transferSize?(t=e.transferSize,r=e.encodedBodySize,n=e.decodedBodySize,i=[r,t?t-r:"_",n-r],i.map(this.toBase36).join(",").replace(/,+$/,"")):""},i.cleanupURL=function(e,t){var r;return e&&"[object Array]"!==Object.prototype.toString.call(e)?("undefined"!=typeof t&&e&&e.length>t&&(r=e.indexOf("?"),e=r!==-1&&r0?",":"")+o+a},"")),d=c=this.trimUrl(a.name,this.trimUrls),i.HOSTNAMES_REVERSED&&(d=this.reverseHostname(c)),void 0!==m[d]?m[d]+="|"+u:void 0!==f[c]?m[d]=i.SPECIAL_DATA_PREFIX+i.SPECIAL_DATA_DIMENSION_TYPE+f[c].map(this.toBase36).join(",").replace(/,+$/,"")+"|"+u:m[d]=u}return{restiming:this.optimizeTrie(this.convertToTrie(m),!0),servertiming:r.lookup}},i.reverseHostname=function(e){return e.replace(i.HOSTNAME_REGEX,function(e,t,r,n){return t+i.reverseString(r)+n})},i.reverseString=function(e){for(var t=e.length,r="";t--;)r+=e[t];return r},i.accumulateServerTimingEntries=function(e,t){(t||[]).forEach(function(t){"undefined"==typeof e[t.name]&&(e[t.name]={count:0,counts:{}});var r=e[t.name];r.counts[t.description]=r.counts[t.description]||0,r.counts[t.description]++,r.count++})},i.compressServerTiming=function(e){return Object.keys(e).sort(function(t,r){return e[r].count-e[t].count}).reduce(function(t,r){var n=Object.keys(e[r].counts).sort(function(t,n){return e[r].counts[n]-e[r].counts[t]});return t.push(1===n.length&&""===n[0]?r:[r].concat(n)),t},[])},i.indexServerTiming=function(e){return e.reduce(function(e,t,r){var n,i;return Array.isArray(t)?(n=t[0],i=t.slice(1).reduce(function(e,t,r){return e[t]=r,e},{})):(n=t,i={"":0}),e[n]={index:r,descriptions:i},e},{})},i.identifyServerTimingEntry=function(e,t){var r="";return e&&(r+=e),t&&(r+="."+t),r.length&&(r=":"+r),r},"function"==typeof define&&define.amd?define([],function(){return i}):"undefined"!=typeof module&&module.exports?module.exports=i:"undefined"!=typeof r&&(r.ResourceTimingCompression=i)}("undefined"!=typeof window?window:void 0); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 92f7165..4a72acf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "resourcetiming-compression", - "version": "1.2.3", + "version": "1.2.4", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 2f0a23f..6970c84 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "description": "ResourceTiming compression and decompression", "main": "./index.js", "author": "Nic Jansma", - "version": "1.2.3", + "version": "1.2.4", "repository": { "type": "git", "url": "http://github.com/nicjansma/resourcetiming-compression.js.git" diff --git a/src/resourcetiming-compression.js b/src/resourcetiming-compression.js index a510a00..8a3856c 100755 --- a/src/resourcetiming-compression.js +++ b/src/resourcetiming-compression.js @@ -932,9 +932,10 @@ * @param {Window} [win] The Window * @param {number} [from] Only get timings from * @param {number} [to] Only get timings up to + * @param {boolean} skipDimensions Skip gathering resource dimensions * @returns {object} Optimized performance entries trie */ - ResourceTimingCompression.getResourceTiming = function(win, from, to) { + ResourceTimingCompression.getResourceTiming = function(win, from, to, skipDimensions) { /* eslint no-script-url:0 */ if (typeof win === "undefined") { win = window; @@ -947,22 +948,25 @@ return {}; } - return ResourceTimingCompression.compressResourceTiming(win, entries, serverTiming); + return ResourceTimingCompression.compressResourceTiming(win, entries, serverTiming, skipDimensions); }; /** * Optimizes the specified set of performance entries. * @param {Window} win The Window * @param {object} entries Performance entries - * @param {object} serverTiming object containing `lookup` and `indexed` + * @param {object} serverTiming Object containing `lookup` and `indexed` + * @param {boolean} skipDimensions Skip gathering resource dimensions * @returns {object} Optimized performance entries trie */ - ResourceTimingCompression.compressResourceTiming = function(win, entries, serverTiming) { + ResourceTimingCompression.compressResourceTiming = function(win, entries, serverTiming, skipDimensions) { /* eslint no-script-url:0 */ var i, e, results = {}, initiatorType, url, finalUrl, data, visibleEntries = {}; - // gather visible entries on the page - visibleEntries = this.getVisibleEntries(win); + if (!skipDimensions) { + // gather visible entries on the page + visibleEntries = this.getVisibleEntries(win); + } for (i = 0; i < entries.length; i++) { e = entries[i]; diff --git a/test/test-resourcetiming-compression.js b/test/test-resourcetiming-compression.js index 55b4238..8555827 100755 --- a/test/test-resourcetiming-compression.js +++ b/test/test-resourcetiming-compression.js @@ -793,7 +793,7 @@ }); // - // .compressResourceTiming + // .getResourceTiming // describe(".getResourceTiming()", function() { it("Should get an object with .restiming and .servertiming from the page", function() { @@ -872,6 +872,30 @@ expect(scaledImage.naturalHeight).to.equal(1000); expect(scaledImage.naturalWidth).to.equal(1000); }); + + it("Should contain the scaled PNG image without dimensions if skipDimensions was set", function() { + if (!canGetResourceTiming()) { + return this.skip(); + } + + var results = ResourceTimingCompression.getResourceTiming(window, undefined, undefined, true); + var resources = ResourceTimingDecompression.decompressResources(results.restiming, results.servertiming); + var scaledImage = resources.find(function(r) { + return r.name.indexOf("?scaled") !== -1; + }); + + if (!scaledImage) { + // Karma won't load the image + return this.skip(); + } + + expect(scaledImage.height).to.be.undefined; + expect(scaledImage.width).to.be.undefined; + expect(scaledImage.x).to.be.undefined; + expect(scaledImage.y).to.be.undefined; + expect(scaledImage.naturalHeight).to.be.undefined; + expect(scaledImage.naturalWidth).to.be.undefined; + }); }); }); }(typeof window !== "undefined" ? window : undefined));