-
Notifications
You must be signed in to change notification settings - Fork 18
/
resourcetiming-compression.js
executable file
·1394 lines (1228 loc) · 52.6 KB
/
resourcetiming-compression.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// resourcetiming-compression.js
//
// Compresses ResourceTiming data.
//
// See http://nicj.net/compressing-resourcetiming/
//
// https://github.com/nicjansma/resourcetiming-compression.js
//
(function(window) {
"use strict";
// save old ResourceTimingCompression object for noConflict()
var root;
var previousObj;
if (typeof window !== "undefined") {
root = window;
previousObj = root.ResourceTimingCompression;
}
// model
var ResourceTimingCompression = {};
//
// Constants / Config
//
/**
* Should hostnames in the compressed trie be reversed or not
*/
ResourceTimingCompression.HOSTNAMES_REVERSED = true;
/**
* Initiator type map
*/
ResourceTimingCompression.INITIATOR_TYPES = {
/** Unknown type */
"other": 0,
/** IMG element */
"img": 1,
/** LINK element (i.e. CSS) */
"link": 2,
/** SCRIPT element */
"script": 3,
/** Resource referenced in CSS */
"css": 4,
/** XMLHttpRequest */
"xmlhttprequest": 5,
/** The root HTML page itself */
"html": 6,
/** IMAGE element inside a SVG */
"image": 7,
/** [sendBeacon]{@link https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon} */
"beacon": 8,
/** [Fetch API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API} */
"fetch": 9,
/** An IFRAME */
"iframe": "a",
/** IE11 and Edge (some versions) send "subdocument" instead of "iframe" */
"subdocument": "a",
/** BODY element */
"body": "b",
/** INPUT element */
"input": "c",
/** FRAME element */
"frame": "a",
/** OBJECT element */
"object": "d",
/** VIDEO element */
"video": "e",
/** AUDIO element */
"audio": "f",
/** SOURCE element */
"source": "g",
/** TRACK element */
"track": "h",
/** EMBED element */
"embed": "i",
/** EventSource */
"eventsource": "j",
/** The root HTML page itself */
"navigation": 6,
/** Early Hints */
"early-hints": "k",
/** HTML <a> ping Attribute */
"ping": "l",
/** CSS font at-rule */
"font": "m"
};
// Words that will be broken (by ensuring the optimized trie doesn't contain
// the whole string) in URLs, to ensure NoScript doesn't think this is an XSS attack
ResourceTimingCompression.DEFAULT_XSS_BREAK_WORDS = [
/(h)(ref)/gi,
/(s)(rc)/gi,
/(a)(ction)/gi
];
// Delimiter to use to break a XSS word
ResourceTimingCompression.XSS_BREAK_DELIM = "\n";
// Maximum number of characters in a URL
ResourceTimingCompression.DEFAULT_URL_LIMIT = 500;
// Any ResourceTiming data time that starts with this character is not a time,
// but something else (like dimension data)
ResourceTimingCompression.SPECIAL_DATA_PREFIX = "*";
// Dimension data special type
ResourceTimingCompression.SPECIAL_DATA_DIMENSION_TYPE = "0";
// Dimension data special type
ResourceTimingCompression.SPECIAL_DATA_SIZE_TYPE = "1";
// Dimension data special type
ResourceTimingCompression.SPECIAL_DATA_SCRIPT_TYPE = "2";
// The following make up a bitmask
ResourceTimingCompression.SPECIAL_DATA_SCRIPT_ASYNC_ATTR = 0x1;
ResourceTimingCompression.SPECIAL_DATA_SCRIPT_DEFER_ATTR = 0x2;
// 0 => HEAD, 1 => BODY
ResourceTimingCompression.SPECIAL_DATA_SCRIPT_LOCAT_ATTR = 0x4;
// Dimension data special type
ResourceTimingCompression.SPECIAL_DATA_SERVERTIMING_TYPE = "3";
// Link attributes
ResourceTimingCompression.SPECIAL_DATA_LINK_ATTR_TYPE = "4";
// Namespaced data
ResourceTimingCompression.SPECIAL_DATA_NAMESPACED_TYPE = "5";
// Service worker type
ResourceTimingCompression.SPECIAL_DATA_SERVICE_WORKER_TYPE = "6";
// Next Hop Protocol
ResourceTimingCompression.SPECIAL_DATA_PROTOCOL = "7";
/**
* These are the only `rel` types that might be reference-able from
* ResourceTiming.
*
* https://html.spec.whatwg.org/multipage/links.html#linkTypes
*
* @enum {number}
*/
ResourceTimingCompression.REL_TYPES = {
"prefetch": 1,
"preload": 2,
"prerender": 3,
"stylesheet": 4
};
// Regular Expression to parse a URL
ResourceTimingCompression.HOSTNAME_REGEX = /^(https?:\/\/)([^/]+)(.*)/;
/**
* List of URLs (strings or regexs) to trim
*/
ResourceTimingCompression.trimUrls = [];
/**
* Words to break to avoid XSS filters
*/
ResourceTimingCompression.xssBreakWords = ResourceTimingCompression.DEFAULT_XSS_BREAK_WORDS;
//
// Functions
//
/**
* Changes the value of ResourceTimingCompression back to its original value, returning
* a reference to the ResourceTimingCompression object.
*
* @returns {object} Original ResourceTimingCompression object
*/
ResourceTimingCompression.noConflict = function() {
root.ResourceTimingCompression = previousObj;
return ResourceTimingCompression;
};
/**
* Rounds up the timing value
*
* @param {number} time Time
* @returns {number} Rounded up timestamp
*/
ResourceTimingCompression.roundUpTiming = function(time) {
if (typeof time !== "number") {
time = 0;
}
return Math.ceil(time ? time : 0);
};
/**
* Converts entries to a Trie:
* http://en.wikipedia.org/wiki/Trie
*
* Assumptions:
* 1) All entries have unique keys
* 2) Keys cannot have "|" in their name.
* 3) All key's values are strings
*
* Leaf nodes in the tree are the key's values.
*
* If key A is a prefix to key B, key A will be suffixed with "|"
*
* @param {object} entries Performance entries
* @returns {object} A trie
*/
ResourceTimingCompression.convertToTrie = function(entries) {
var trie = {}, url, urlFixed, i, value, letters, letter, cur, node;
for (url in entries) {
if (!Object.prototype.hasOwnProperty.call(entries, url)) {
continue;
}
urlFixed = url;
// find any strings to break
for (i = 0; i < this.xssBreakWords.length; i++) {
// Add a XSS_BREAK_DELIM character after the first letter. optimizeTrie will
// ensure this sequence doesn't get combined.
urlFixed = urlFixed.replace(
this.xssBreakWords[i],
"$1" + ResourceTimingCompression.XSS_BREAK_DELIM + "$2");
}
value = entries[url];
letters = urlFixed.split("");
cur = trie;
for (i = 0; i < letters.length; i++) {
letter = letters[i];
node = cur[letter];
if (typeof node === "undefined") {
// nothing exists yet, create either a leaf if this is the end of the word,
// or a branch if there are letters to go
cur = cur[letter] = (i === (letters.length - 1) ? value : {});
} else if (typeof node === "string") {
// this is a leaf, but we need to go further, so convert it into a branch
cur = cur[letter] = { "|": node };
} else if (i === (letters.length - 1)) {
// this is the end of our key, and we've hit an existing node. Add our timings.
cur[letter]["|"] = value;
} else {
// continue onwards
cur = cur[letter];
}
}
}
return trie;
};
/**
* Optimize the Trie by combining branches with no leaf
*
* @param {object} cur Current Trie branch
* @param {boolean} top Whether or not this is the root node
*
* @returns {object} Optimized Trie
*/
ResourceTimingCompression.optimizeTrie = function(cur, top) {
var num = 0, node, ret, topNode;
// capture trie keys first as we'll be modifying it
var keys = [];
for (node in cur) {
if (Object.prototype.hasOwnProperty.call(cur, node)) {
keys.push(node);
}
}
for (var i = 0; i < keys.length; i++) {
node = keys[i];
if (typeof cur[node] === "object") {
// optimize children
ret = this.optimizeTrie(cur[node], false);
if (ret) {
// swap the current leaf with compressed one
delete cur[node];
if (node === ResourceTimingCompression.XSS_BREAK_DELIM) {
// If this node is a newline, which can't be in a regular URL,
// it's due to the XSS patch. Remove the placeholder character,
// and make sure this node isn't compressed by incrementing
// num to be greater than one.
node = ret.name;
num++;
} else {
node = node + ret.name;
}
cur[node] = ret.value;
}
}
num++;
}
if (num === 1) {
// compress single leafs
if (top) {
// top node gets special treatment so we're not left with a {node:,value:} at top
topNode = {};
topNode[node] = cur[node];
return topNode;
}
// other nodes we return name and value separately
return { name: node, value: cur[node] };
} else if (top) {
// top node with more than 1 child, return it as-is
return cur;
}
// more than two nodes and not the top, we can't compress any more
return false;
};
/**
* Trims the timing, returning an offset from the startTime in ms
*
* @param {number} time Time
* @param {number} startTime Start time
*
* @returns {number} Number of ms from start time
*/
ResourceTimingCompression.trimTiming = function(time, startTime) {
if (typeof time !== "number") {
time = 0;
}
if (typeof startTime !== "number") {
startTime = 0;
}
// strip from microseconds to milliseconds only
var timeMs = Math.round(time ? time : 0),
startTimeMs = Math.round(startTime ? startTime : 0);
return timeMs === 0 ? 0 : (timeMs - startTimeMs);
};
/**
* Attempts to get the navigationStart time for a frame.
*
* @param {Frame} frame IFRAME
*
* @returns {number} navigationStart time, or 0 if not accessible
*/
ResourceTimingCompression.getNavStartTime = function(frame) {
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "frameLoc" }] */
var navStart = 0, frameLoc;
if (!frame) {
return navStart;
}
try {
// Try to access location.href first to trigger any Cross-Origin
// warnings. There's also a bug in Chrome ~48 that might cause
// the browser to crash if accessing X-O frame.performance.
// https://code.google.com/p/chromium/issues/detail?id=585871
// This variable is not otherwise used.
frameLoc = frame.location && frame.location.href;
if (("performance" in frame) &&
frame.performance &&
frame.performance.timing &&
frame.performance.timing.navigationStart) {
navStart = frame.performance.timing.navigationStart;
}
} catch (e) {
// swallow all access exceptions
}
return navStart;
};
/**
* Gets all of the performance entries for a frame and its subframes
*
* @param {Frame} frame Frame
* @param {boolean} isTopWindow This is the top window
* @param {string} offset Offset in timing from root IFRA
* @param {number} depth Recursion depth
* @returns {PerformanceEntry[]} Performance entries
*/
ResourceTimingCompression.findPerformanceEntriesForFrame = function(frame, isTopWindow, offset, depth) {
var entries = [], i, navEntries, navStart, frameNavStart, frameOffset, navEntry, t, frameLoc, rtEntry,
links = {}, scripts = {}, a;
if (typeof isTopWindow === "undefined") {
isTopWindow = true;
}
if (typeof offset === "undefined") {
offset = 0;
}
if (typeof depth === "undefined") {
depth = 0;
}
if (depth > 10) {
return entries;
}
try {
navStart = this.getNavStartTime(frame);
a = frame.document.createElement("a");
// get all scripts as an object keyed on script.src
collectResources(a, scripts, "script");
collectResources(a, links, "link");
// get sub-frames' entries first
if (frame.frames) {
for (i = 0; i < frame.frames.length; i++) {
frameNavStart = this.getNavStartTime(frame.frames[i]);
frameOffset = 0;
if (frameNavStart > navStart) {
frameOffset = offset + (frameNavStart - navStart);
}
entries = entries.concat(
this.findPerformanceEntriesForFrame(frame.frames[i], false, frameOffset, ++depth));
}
}
try {
// Try to access location.href first to trigger any Cross-Origin
// warnings. There's also a bug in Chrome ~48 that might cause
// the browser to crash if accessing X-O frame.performance.
// https://code.google.com/p/chromium/issues/detail?id=585871
// This variable is not otherwise used.
frameLoc = frame.location && frame.location.href;
if (!("performance" in frame) ||
!frame.performance ||
!frame.performance.getEntriesByType) {
return entries;
}
} catch (e) {
// NOP
return entries;
}
// add an entry for the top page
if (isTopWindow) {
navEntries = frame.performance.getEntriesByType("navigation");
if (navEntries && navEntries.length === 1) {
navEntry = navEntries[0];
// replace document with the actual URL
entries.push({
name: frame.location.href,
startTime: 0,
initiatorType: "html",
redirectStart: navEntry.redirectStart,
redirectEnd: navEntry.redirectEnd,
fetchStart: navEntry.fetchStart,
domainLookupStart: navEntry.domainLookupStart,
domainLookupEnd: navEntry.domainLookupEnd,
connectStart: navEntry.connectStart,
secureConnectionStart: navEntry.secureConnectionStart,
connectEnd: navEntry.connectEnd,
requestStart: navEntry.requestStart,
responseStart: navEntry.responseStart,
responseEnd: navEntry.responseEnd,
serverTiming: navEntry.serverTiming || [],
nextHopProtocol: navEntry.nextHopProtocol
});
} else if (frame.performance.timing) {
// add a fake entry from the timing object
t = frame.performance.timing;
//
// Avoid browser bugs:
// 1. navigationStart being 0 in some cases
// 2. responseEnd being ~2x what navigationStart is
// (ensure the end is within 60 minutes of start)
//
if (t.navigationStart !== 0 &&
t.responseEnd <= (t.navigationStart + (60 * 60 * 1000))) {
entries.push({
name: frame.location.href,
startTime: 0,
initiatorType: "html",
redirectStart: t.redirectStart ? (t.redirectStart - t.navigationStart) : 0,
redirectEnd: t.redirectEnd ? (t.redirectEnd - t.navigationStart) : 0,
fetchStart: t.fetchStart ? (t.fetchStart - t.navigationStart) : 0,
domainLookupStart: t.domainLookupStart ? (t.domainLookupStart - t.navigationStart) : 0,
domainLookupEnd: t.domainLookupEnd ? (t.domainLookupEnd - t.navigationStart) : 0,
connectStart: t.connectStart ? (t.connectStart - t.navigationStart) : 0,
secureConnectionStart: t.secureConnectionStart ?
(t.secureConnectionStart - t.navigationStart) :
0,
connectEnd: t.connectEnd ? (t.connectEnd - t.navigationStart) : 0,
requestStart: t.requestStart ? (t.requestStart - t.navigationStart) : 0,
responseStart: t.responseStart ? (t.responseStart - t.navigationStart) : 0,
responseEnd: t.responseEnd ? (t.responseEnd - t.navigationStart) : 0
});
}
}
}
// offset all of the entries by the specified offset for this frame
var frameEntries = frame.performance.getEntriesByType("resource");
var frameFixedEntries = [];
for (i = 0; frameEntries && i < frameEntries.length; i++) {
t = frameEntries[i];
rtEntry = {
name: t.name,
initiatorType: t.initiatorType,
startTime: t.startTime + offset,
redirectStart: t.redirectStart ? (t.redirectStart + offset) : 0,
redirectEnd: t.redirectEnd ? (t.redirectEnd + offset) : 0,
fetchStart: t.fetchStart ? (t.fetchStart + offset) : 0,
domainLookupStart: t.domainLookupStart ? (t.domainLookupStart + offset) : 0,
domainLookupEnd: t.domainLookupEnd ? (t.domainLookupEnd + offset) : 0,
connectStart: t.connectStart ? (t.connectStart + offset) : 0,
secureConnectionStart: t.secureConnectionStart ? (t.secureConnectionStart + offset) : 0,
connectEnd: t.connectEnd ? (t.connectEnd + offset) : 0,
requestStart: t.requestStart ? (t.requestStart + offset) : 0,
responseStart: t.responseStart ? (t.responseStart + offset) : 0,
responseEnd: t.responseEnd ? (t.responseEnd + offset) : 0,
nextHopProtocol: t.nextHopProtocol
};
if (t.encodedBodySize || t.decodedBodySize || t.transferSize) {
rtEntry.encodedBodySize = t.encodedBodySize;
rtEntry.decodedBodySize = t.decodedBodySize;
rtEntry.transferSize = t.transferSize;
}
if (t.serverTiming && t.serverTiming.length) {
rtEntry.serverTiming = t.serverTiming;
}
// If this is a script, set its flags
this.updateScriptFlags(scripts, t, rtEntry);
// Update link flags
this.updateLinkFlags(links, t, rtEntry);
frameFixedEntries.push(rtEntry);
}
entries = entries.concat(frameFixedEntries);
} catch (e) {
return entries;
}
return entries;
};
/**
* Sets .scriptAttrs flags for a compressed RT entry for <script> tags
*
* @param {object[]} scripts Scripts array
* @param {ResourceTiming} entry ResourceTiming entry from the frame
* @param {object} rtEntry Compressed RT entry
*/
ResourceTimingCompression.updateScriptFlags = function(scripts, entry, rtEntry) {
if ((entry.initiatorType === "script" || entry.initiatorType === "link") && scripts[entry.name]) {
var s = scripts[entry.name];
// Add async & defer based on attribute values
rtEntry.scriptAttrs = (s.async ? ResourceTimingCompression.SPECIAL_DATA_SCRIPT_ASYNC_ATTR : 0) |
(s.defer ? ResourceTimingCompression.SPECIAL_DATA_SCRIPT_DEFER_ATTR : 0);
while (s.nodeType === 1 && s.nodeName !== "BODY") {
s = s.parentNode;
}
// Add location by traversing up the tree until we either hit BODY or document
rtEntry.scriptAttrs |= (s.nodeName === "BODY" ?
ResourceTimingCompression.SPECIAL_DATA_SCRIPT_LOCAT_ATTR : 0);
}
};
/**
* Sets .linkAttrs flags for a compressed RT entry for <link> tags
*
* @param {object[]} links Links array
* @param {ResourceTiming} entry ResourceTiming entry from the frame
* @param {object} rtEntry Compressed RT entry
*/
ResourceTimingCompression.updateLinkFlags = function(links, entry, rtEntry) {
// If this is a link, set its flags
if (entry.initiatorType === "link" && links[entry.name]) {
// split on ASCII whitespace
// eslint-disable-next-line no-control-regex
links[entry.name].rel.split(/[\u0009\u000A\u000C\u000D\u0020]+/).find(function(rel) {
// eslint-disable-line no-loop-func
// `rel`s are case insensitive
rel = rel.toLowerCase();
// only report the `rel` if it's from the known list
if (ResourceTimingCompression.REL_TYPES[rel]) {
rtEntry.linkAttrs = ResourceTimingCompression.REL_TYPES[rel];
return true;
}
return false;
});
}
};
/**
* Converts a number to base-36.
*
* If not a number or a string, or === 0, return "". This is to facilitate
* compression in the timing array, where "blanks" or 0s show as a series
* of trailing ",,,," that can be trimmed.
*
* If a string, return a string.
*
* @param {number} n Number
* @returns {string} Base-36 number, empty string, or string
*/
ResourceTimingCompression.toBase36 = function(n) {
if (typeof n === "number" && n !== 0) {
return n.toString(36);
}
return typeof n === "string" ? n : "";
};
/**
* Collect external resources by tagName
*
* @param {Element} a an anchor element
* @param {Object} obj object of resources where the key is the url
* @param {string} tagName tag name to collect
*/
function collectResources(a, obj, tagName) {
Array.prototype
.forEach
.call(a.ownerDocument.getElementsByTagName(tagName), function(r) {
// Get canonical URL
a.href = r.currentSrc || r.src || r.getAttribute("xlink:href") || r.href;
// only get external resource
if (a.href.match(/^https?:\/\//)) {
obj[a.href] = r;
}
});
}
/**
* Finds all remote resources in the selected window that are visible, and returns an object
* keyed by the url with an array of height,width,top,left as the value
*
* @param {Window} win Window to search
* @returns {Object} Object with URLs of visible assets as keys, and Array[height, width, top, left] as value
*/
ResourceTimingCompression.getVisibleEntries = function(win) {
if (!win) {
return {};
}
// lower-case tag names should be used:
// https://developer.mozilla.org/en-US/docs/Web/API/Element/getElementsByTagName
var els = ["img", "iframe", "image"], entries = {}, x, y, doc = win.document, a = doc.createElement("A");
// https://developer.mozilla.org/en-US/docs/Web/API/Window/scrollX
// https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect
x = (win.pageXOffset !== undefined)
? win.pageXOffset
: (doc.documentElement || doc.body.parentNode || doc.body).scrollLeft;
y = (win.pageYOffset !== undefined)
? win.pageYOffset
: (doc.documentElement || doc.body.parentNode || doc.body).scrollTop;
// look at each IMG and IFRAME
els.forEach(function(elname) {
var elements = doc.getElementsByTagName(elname), el, i, rect, src;
for (i = 0; i < elements.length; i++) {
el = elements[i];
if (!el) {
continue;
}
// look at this element if it has a src attribute or xlink:href, and we haven't already looked at it
// currentSrc = IMG inside a PICTURE element or IMG srcset
// src = IMG, IFRAME
// xlink:href = svg:IMAGE
src = el.currentSrc ||
el.src ||
(typeof el.getAttribute === "function" &&
(el.getAttribute("src")) || el.getAttribute("xlink:href"));
// make src absolute
a.href = src;
src = a.href;
if (!src || entries[src]) {
continue;
}
rect = el.getBoundingClientRect();
// Require both height & width to be non-zero
// IE <= 8 does not report rect.height/rect.width so we need offsetHeight & width
if ((rect.height || el.offsetHeight) && (rect.width || el.offsetWidth)) {
entries[src] = [
rect.height || el.offsetHeight,
rect.width || el.offsetWidth,
Math.round(rect.top + y),
Math.round(rect.left + x)
];
// If this is an image, it has a naturalHeight & naturalWidth
// if these are different from its display height and width, we should report that
// because it indicates scaling in HTML
if (!el.naturalHeight && !el.naturalWidth) {
continue;
}
// If the image came from a srcset, then the naturalHeight/Width will be density corrected.
// We get the actual physical dimensions by assigning the image to an uncorrected Image object.
// This should load from in-memory cache, so there should be no extra load.
var realImg, nH, nW;
if (el.currentSrc &&
(el.srcset ||
(el.parentNode &&
el.parentNode.nodeName &&
el.parentNode.nodeName.toUpperCase() === "PICTURE"))) {
realImg = el.isConnected ? el.ownerDocument.createElement("IMG") : new window.Image();
realImg.src = src;
} else {
realImg = el;
}
nH = realImg.naturalHeight || el.naturalHeight;
nW = realImg.naturalWidth || el.naturalWidth;
if ((nH || nW) && (entries[src][0] !== nH || entries[src][1] !== nW)) {
entries[src].push(nH, nW);
}
}
}
});
return entries;
};
/**
* Determines if the value is in the specified array
*
* @param {object} val Value
* @param {object[]} ary Array
*
* @returns {boolean} True if the value is in the array
*/
ResourceTimingCompression.inArray = function(val, ary) {
var i;
if (typeof val === "undefined" || typeof ary === "undefined" || !ary.length) {
return false;
}
for (i = 0; i < ary.length; i++) {
if (ary[i] === val) {
return true;
}
}
return false;
};
/**
* Gathers a filtered list of performance entries.
* @param {Window} win The Window
* @param {number} from Only get timings from
* @param {number} to Only get timings up to
* @param {string[]} initiatorTypes Array of initiator types
* @returns {ResourceTiming[]} Matching ResourceTiming entries
*/
ResourceTimingCompression.getFilteredResourceTiming = function(win, from, to, initiatorTypes) {
var entries = this.findPerformanceEntriesForFrame(win, true, 0, 0),
i, e,
navStart = this.getNavStartTime(win), countCollector = {};
if (!entries || !entries.length) {
return {
entries: []
};
}
var filteredEntries = [];
for (i = 0; i < entries.length; i++) {
e = entries[i];
// skip non-resource URLs
if (e.name.indexOf("about:") === 0 ||
e.name.indexOf("javascript:") === 0) {
continue;
}
// TODO: skip URLs we don't want to report
// if the user specified a "from" time, skip resources that started before then
if (from && (navStart + e.startTime) < from) {
continue;
}
// if we were given a final timestamp, don't add any resources that started after it
if (to && (navStart + e.startTime) > to) {
// We can also break at this point since the array is time sorted
break;
}
// if given an array of initiatorTypes to include, skip anything else
if (typeof initiatorTypes !== "undefined" && initiatorTypes !== "*" && initiatorTypes.length) {
if (!e.initiatorType || !this.inArray(e.initiatorType, initiatorTypes)) {
continue;
}
}
ResourceTimingCompression.accumulateServerTimingEntries(countCollector, e.serverTiming);
filteredEntries.push(e);
}
var lookup = ResourceTimingCompression.compressServerTiming(countCollector);
return {
entries: filteredEntries,
serverTiming: {
lookup: lookup,
indexed: ResourceTimingCompression.indexServerTiming(lookup)
}
};
};
/**
* Gets compressed content and transfer size information, if available
*
* @param {ResourceTiming} resource ResourceTiming object
*
* @returns {string} Compressed data (or empty string, if not available)
*/
ResourceTimingCompression.compressSize = function(resource) {
var sTrans, sEnc, sDec, sizes;
// check to see if we can add content sizes
if (resource.encodedBodySize ||
resource.decodedBodySize ||
resource.transferSize) {
//
// transferSize: the size of the fetched resource ("over the wire"), including the response header fields
// and the response payload body. It can be 0 in the case of X-O, or if it was fetched from a cache.
//
// encodedBodySize: the size of the response payload body after applying encoding (e.g. gzipped size). It
// is 0 if X-O.
//
// decodedBodySize: the size of response payload body after removing encoding (e.g. the original content
// size). It is 0 if X-O.
//
// Here are the possible combinations of values: [encodedBodySize, transferSize, decodedBodySize]
//
// Cross-Origin resources w/out Timing-Allow-Origin set: [0, 0, 0] -> [0, 0, 0] -> [empty]
// 204: [0, t, 0] -> [0, t, 0] -> [e, t-e] -> [, t]
// 304: [e, t: t <=> e, d: d>=e] -> [e, t-e, d-e]
// 200 non-gzipped: [e, t: t>=e, d: d=e] -> [e, t-e]
// 200 gzipped: [e, t: t>=e, d: d>=e] -> [e, t-e, d-e]
// retrieved from cache non-gzipped: [e, 0, d: d=e] -> [e]
// retrieved from cache gzipped: [e, 0, d: d>=e] -> [e, _, d-e]
//
sTrans = resource.transferSize;
sEnc = resource.encodedBodySize;
sDec = resource.decodedBodySize;
// convert to an array
sizes = [
sEnc,
sTrans ? sTrans - sEnc : "_",
sDec - sEnc
];
// change everything to base36 and remove any trailing ,s
return sizes.map(this.toBase36).join(",").replace(/,+$/, "");
}
return "";
};
/**
* Cleans up a URL by removing the query string (if configured), and
* limits the URL to the specified size.
*
* @param {string} url URL to clean
* @param {number} urlLimit Maximum size, in characters, of the URL
*
* @returns {string} Cleaned up URL
*/
ResourceTimingCompression.cleanupURL = function(url, urlLimit) {
var qsStart;
if (!url || Object.prototype.toString.call(url) === "[object Array]") {
return "";
}
if (typeof urlLimit !== "undefined" && url && url.length > urlLimit) {
// We need to break this URL up. Try at the query string first.
qsStart = url.indexOf("?");
if (qsStart !== -1 && qsStart < urlLimit) {
url = url.substr(0, qsStart) + "?...";
} else {
// No query string, just stop at the limit
url = url.substr(0, urlLimit - 3) + "...";
}
}
return url;
};
/**
* Trims the URL according to the specified URL trim patterns,
* then applies a length limit.
*
* @param {string} url URL to trim
* @param {string} urlsToTrim List of URLs (strings or regexs) to trim
* @returns {string} Trimmed URL
*/
ResourceTimingCompression.trimUrl = function(url, urlsToTrim) {
var i, urlIdx, trim;
if (url && urlsToTrim) {
// trim the payload from any of the specified URLs
for (i = 0; i < urlsToTrim.length; i++) {
trim = urlsToTrim[i];
if (typeof trim === "string") {
urlIdx = url.indexOf(trim);
if (urlIdx !== -1) {
url = url.substr(0, urlIdx + trim.length) + "...";
break;
}
} else if (trim instanceof RegExp) {
if (trim.test(url)) {
// replace the URL with the first capture group
url = url.replace(trim, "$1") + "...";
}
}
}
}
// apply limits
return this.cleanupURL(url, ResourceTimingCompression.DEFAULT_URL_LIMIT);
};
/**
* Gathers performance entries and compresses the result.
* @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, skipDimensions) {
/* eslint no-script-url:0 */
if (typeof win === "undefined") {
win = window;
}
var ret = ResourceTimingCompression.getFilteredResourceTiming(win, from, to);
var entries = ret.entries, serverTiming = ret.serverTiming;
if (!entries || !entries.length) {
return {};
}
return ResourceTimingCompression.compressResourceTiming(win, entries, serverTiming, skipDimensions);
};
/**
* Guesses whether or not a resource is a cache hit.
*
* We can get this directly from the beacon if it has ResourceTiming2 sizing
* data, and the resource is same-origin or has TAO.
*
* For all other cases, we have to guess based on the timing
*
* @param {PerformanceResourceTiming} entry ResourceTiming entry
*
* @returns {boolean} True if we estimate it was a cache hit.
*/
ResourceTimingCompression.isCacheHit = function(entry) {
// if we transferred bytes, it must not be a cache hit