-
Notifications
You must be signed in to change notification settings - Fork 18
/
resourcetiming-decompression.js
executable file
·1047 lines (901 loc) · 33.9 KB
/
resourcetiming-decompression.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-decompression.js
//
// Decompresses ResourceTiming data compressed via resourcetiming-compression.js.
//
// See http://nicj.net/compressing-resourcetiming/
//
// https://github.com/nicjansma/resourcetiming-compression.js
//
(function(window) {
"use strict";
// save old ResourceTimingDecompression object for noConflict()
var root;
var previousObj;
if (typeof window !== "undefined") {
root = window;
previousObj = root.ResourceTimingDecompression;
}
// model
var ResourceTimingDecompression = {};
//
// Constants / Config
//
/**
* Are hostnames in the compressed trie reversed or not
*/
ResourceTimingDecompression.HOSTNAMES_REVERSED = true;
/**
* Initiator type map
*/
ResourceTimingDecompression.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 */
"navigation": 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,
/** FRAME element */
"frame": "a",
/** BODY element */
"body": "b",
/** INPUT element */
"input": "c",
/** 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",
/** Early Hints */
"early-hints": "k",
/** HTML <a> ping Attribute */
"ping": "l",
/** CSS font at-rule */
"font": "m"
};
/**
* Dimension name map
*/
ResourceTimingDecompression.DIMENSION_NAMES = {
"height": 0,
"width": 1,
"y": 2,
"x": 3,
"naturalHeight": 4,
"naturalWidth": 5
};
/**
* Script mask map
*/
ResourceTimingDecompression.SCRIPT_ATTRIBUTES = {
"scriptAsync": 0x1,
"scriptDefer": 0x2,
"scriptBody": 0x4
};
/**
* These are the only `rel` types that might be reference-able from
* ResourceTiming.
*
* https://html.spec.whatwg.org/multipage/links.html#linkTypes
*
* @enum {number}
*/
ResourceTimingDecompression.REL_TYPES = {
"prefetch": 1,
"preload": 2,
"prerender": 3,
"stylesheet": 4
};
/**
* Returns a map with key/value pairs reversed.
*
* @param {object} origMap Map we want to reverse.
*
* @returns {object} New map with reversed mappings.
*/
ResourceTimingDecompression.getRevMap = function(origMap) {
var revMap = {};
for (var key in origMap) {
if (Object.prototype.hasOwnProperty.call(origMap, key)) {
revMap[origMap[key]] = key;
}
}
return revMap;
};
/**
* Reverse initiator type map
*/
ResourceTimingDecompression.REV_INITIATOR_TYPES = ResourceTimingDecompression.
getRevMap(ResourceTimingDecompression.INITIATOR_TYPES);
/**
* Reverse dimension name map
*/
ResourceTimingDecompression.REV_DIMENSION_NAMES = ResourceTimingDecompression.
getRevMap(ResourceTimingDecompression.DIMENSION_NAMES);
/**
* Reverse script attribute map
*/
ResourceTimingDecompression.REV_SCRIPT_ATTRIBUTES = ResourceTimingDecompression.
getRevMap(ResourceTimingDecompression.SCRIPT_ATTRIBUTES);
/**
* Reverse link rel attribute map
*/
ResourceTimingDecompression.REV_REL_TYPES = ResourceTimingDecompression.
getRevMap(ResourceTimingDecompression.REL_TYPES);
// Any ResourceTiming data time that starts with this character is not a time,
// but something else (like dimension data)
ResourceTimingDecompression.SPECIAL_DATA_PREFIX = "*";
// Dimension data special type
ResourceTimingDecompression.SPECIAL_DATA_DIMENSION_TYPE = "0";
ResourceTimingDecompression.SPECIAL_DATA_DIMENSION_PREFIX = ResourceTimingDecompression.SPECIAL_DATA_PREFIX +
ResourceTimingDecompression.SPECIAL_DATA_DIMENSION_TYPE;
// Dimension data special type
ResourceTimingDecompression.SPECIAL_DATA_SIZE_TYPE = "1";
// Dimension data special type
ResourceTimingDecompression.SPECIAL_DATA_SCRIPT_TYPE = "2";
// Dimension data special type
ResourceTimingDecompression.SPECIAL_DATA_SERVERTIMING_TYPE = "3";
// Link attributes
ResourceTimingDecompression.SPECIAL_DATA_LINK_ATTR_TYPE = "4";
// Namespaced data
ResourceTimingDecompression.SPECIAL_DATA_NAMESPACED_TYPE = "5";
// Service worker type
ResourceTimingDecompression.SPECIAL_DATA_SERVICE_WORKER_TYPE = "6";
// Regular Expression to parse a URL
ResourceTimingDecompression.HOSTNAME_REGEX = /^(https?:\/\/)([^/]+)(.*)/;
// Next Hop Protocol
ResourceTimingDecompression.SPECIAL_DATA_PROTOCOL = "7";
//
// Functions
//
/**
* Returns the index of the first value in the array such that it is
* greater or equal to x.
* The search is performed using binary search and the array is assumed
* to be sorted in ascending order.
*
* @param {array} arr haystack
* @param {any} x needle
* @param {function} by transform function (optional)
*
* @returns {number} the desired index or arr.length if x is more than all values.
*/
ResourceTimingDecompression.searchSortedFirst = function(arr, x, by) {
if (!arr || arr.length === 0) {
return -1;
}
function ident(a) {
return a;
}
by = by || ident;
x = by(x);
var min = -1;
var max = arr.length;
var m = 0;
while (min < (max - 1)) {
m = (min + max) >>> 1;
if (by(arr[m]) < x) {
min = m;
} else {
max = m;
}
}
return max;
};
/**
* Returns the index of the last value in the array such that is it less
* than or equal to x.
* The search is performed using binary search and the array is assumed
* to be sorted in ascending order.
*
* @param {array} arr haystack
* @param {any} x needle
* @param {function} by transform function (optional)
*
* @returns {number} the desired index or -1 if x is less than all values.
*/
ResourceTimingDecompression.searchSortedLast = function(arr, x, by) {
if (!arr || arr.length === 0) {
return -1;
}
function ident(a) {
return a;
}
by = by || ident;
x = by(x);
var min = -1;
var max = arr.length;
var m = 0;
while (min < (max - 1)) {
m = (min + max) >>> 1;
if (x < by(arr[m])) {
max = m;
} else {
min = m;
}
}
return min;
};
/**
* Changes the value of ResourceTimingDecompression back to its original value, returning
* a reference to the ResourceTimingDecompression object.
*
* @returns {object} Original ResourceTimingDecompression object
*/
ResourceTimingDecompression.noConflict = function() {
root.ResourceTimingDecompression = previousObj;
return ResourceTimingDecompression;
};
/**
* Decompresses a compressed ResourceTiming trie
*
* @param {object} rt ResourceTiming trie
* @param {array} st server timing entries lookup
* @param {string} prefix URL prefix for the current node
*
* @returns {ResourceTiming[]} ResourceTiming array
*/
ResourceTimingDecompression.decompressResources = function(rt, st, prefix) {
var resources = [];
// Dimension data for resources.
var dimensionData;
prefix = prefix || "";
for (var key in rt) {
// skip over inherited properties
if (!Object.prototype.hasOwnProperty.call(rt, key)) {
continue;
}
var node = rt[key];
var nodeKey = prefix + key;
// strip trailing pipe, which is used to designate a node that is a prefix for
// other nodes but has resTiming data
if (nodeKey.indexOf("|", nodeKey.length - 1) !== -1) {
nodeKey = nodeKey.substring(0, nodeKey.length - 1);
}
if (typeof node === "string") {
// add all occurences
var timings = node.split("|");
if (timings.length === 0) {
continue;
}
// Make sure we reset the dimensions before each new resource.
dimensionData = undefined;
if (this.isDimensionData(timings[0])) {
dimensionData = this.decompressDimension(timings[0]);
// Remove the dimension data from our timings array
timings = timings.splice(1);
}
// end-node
for (var i = 0; i < timings.length; i++) {
var resourceData = timings[i];
if (resourceData.length > 0 &&
resourceData[0] === ResourceTimingDecompression.SPECIAL_DATA_PREFIX) {
// dimensions or sizes for this resource
continue;
}
// Decode resource and add dimension data to it.
resources.push(
this.addDimension(
this.decodeCompressedResource(resourceData, nodeKey, st),
dimensionData
)
);
}
} else {
// continue down
var nodeResources = this.decompressResources(node, st, nodeKey);
resources = resources.concat(nodeResources);
}
}
return resources;
};
/**
* Checks that the input contains dimension information.
*
* @param {string} resourceData The string we want to check.
*
* @returns {boolean} True if resourceData starts with SPECIAL_DATA_DIMENSION_PREFIX, false otherwise.
*/
ResourceTimingDecompression.isDimensionData = function(resourceData) {
return resourceData &&
resourceData.substring(0, ResourceTimingDecompression.SPECIAL_DATA_DIMENSION_PREFIX.length)
=== ResourceTimingDecompression.SPECIAL_DATA_DIMENSION_PREFIX;
};
/**
* Extract height, width, y and x from a string.
*
* @param {string} resourceData A string containing dimension data.
*
* @returns {object} Dimension data with keys defined by DIMENSION_NAMES.
*/
ResourceTimingDecompression.decompressDimension = function(resourceData) {
var dimensions, i;
var dimensionData = {};
// If the string does not contain dimension information, do nothing.
if (!this.isDimensionData(resourceData)) {
return dimensionData;
}
// Remove special prefix
resourceData = resourceData.substring(ResourceTimingDecompression.SPECIAL_DATA_DIMENSION_PREFIX.length);
dimensions = resourceData.split(",");
// The data should contain at least height/width.
if (dimensions.length < 2) {
return dimensionData;
}
// If x is 0, and the last dimension, then it will be excluded, so initialize to 0
// If x & y are 0, and the last dimensions, then both will be excluded, so initialize to 0
dimensionData.y = 0;
dimensionData.x = 0;
// Base 36 decode and assign to correct keys of dimensionData.
for (i = 0; i < dimensions.length; i++) {
if (dimensions[i] === "") {
dimensionData[this.REV_DIMENSION_NAMES[i]] = 0;
} else {
dimensionData[this.REV_DIMENSION_NAMES[i]] = parseInt(dimensions[i], 36);
}
}
// If naturalHeight and naturalWidth are missing, then they are the same as height and width
if (!Object.prototype.hasOwnProperty.call(dimensionData, "naturalHeight")) {
dimensionData.naturalHeight = dimensionData.height;
}
if (!Object.prototype.hasOwnProperty.call(dimensionData, "naturalWidth")) {
dimensionData.naturalWidth = dimensionData.width;
}
return dimensionData;
};
/**
* Adds dimension data to the given resource.
*
* @param {object} resource The resource we want to edit.
* @param {object} dimensionData The dimension data we want to add.
*
* @returns {object} The resource with added dimensions.
*/
ResourceTimingDecompression.addDimension = function(resource, dimensionData) {
// If the resource or data are not defined, do nothing.
if (!resource || !dimensionData) {
return resource;
}
// Add all the dimensions to our resource.
for (var key in this.DIMENSION_NAMES) {
if (Object.prototype.hasOwnProperty.call(this.DIMENSION_NAMES, key) &&
Object.prototype.hasOwnProperty.call(dimensionData, key)) {
resource[key] = dimensionData[key];
}
}
return resource;
};
/**
* Compute a list of cells based on the start/end times of the
* given array of resources.
* The returned list of cells is sorted in chronological order.
*
* @param {array} rts array of resource timings.
*
* @returns {array} Array of cells.
*/
ResourceTimingDecompression.getSortedCells = function(rts) {
// We have exactly 2 events per resource (start and end).
// var cells = new Array(rts.length * 2);
var cells = [];
for (var i = 0; i < rts.length; i++) {
// Ignore resources with duration <= 0
if (rts[i].responseEnd <= rts[i].startTime) {
continue;
}
// Increment on resource start
cells.push({
ts: rts[i].startTime,
val: 1.0
});
// Decrement on resource end
cells.push({
ts: rts[i].responseEnd,
val: -1.0
});
}
// Sort in chronological order
cells.sort(function(x, y) {
return x.ts - y.ts;
});
return cells;
};
/**
* Add contributions to the array of cells.
*
* @param {array} cells array of cells that need contributions.
*
* @returns {array} Array of cells with their contributions.
*/
ResourceTimingDecompression.addCellContributions = function(cells) {
var tot = 0.0;
var incr = 0.0;
var deleteIdx = [];
var currentSt = cells[0].ts;
var cellLen = cells.length;
var c = {};
for (var i = 0; i < cellLen; i++) {
c = cells[i];
// The next timestamp is the same.
// We don't want to have cells of duration 0, so
// we aggregate them.
if ((i < (cellLen - 1)) && (cells[i + 1].ts === c.ts)) {
cells[i + 1].val += c.val;
deleteIdx.push(i);
continue;
}
incr = c.val;
if (tot > 0) {
// divide time delta by number of active resources.
c.val = (c.ts - currentSt) / tot;
}
currentSt = c.ts;
tot += incr;
}
// Delete timestamps that don't delimit cells.
for (i = deleteIdx.length - 1; i >= 0; i--) {
cells.splice(deleteIdx[i], 1);
}
return cells;
};
/**
* Sum the contributions of a single resource based on an array of cells.
*
* @param {array} cells Array of cells with their contributions.
* @param {ResourceTiming} rt a single resource timing object.
*
* @returns {number} The total contribution for that resource.
*/
ResourceTimingDecompression.sumContributions = function(cells, rt) {
if (!rt || typeof rt.startTime === "undefined" ||
typeof rt.responseEnd === "undefined") {
return 0.0;
}
var startTime = rt.startTime + 1;
var responseEnd = rt.responseEnd;
function getTs(x) {
return x.ts;
}
// Find indices of cells that were affected by our resource.
var low = this.searchSortedFirst(cells, { ts: startTime }, getTs);
var up = this.searchSortedLast(cells, { ts: responseEnd }, getTs);
var tot = 0.0;
// Sum contributions across all those cells
for (var i = low; i <= up; i++) {
tot += cells[i].val;
}
return tot;
};
/**
* Adds contribution scores to all resources in the array.
*
* @param {array} rts array of resource timings.
*
* @returns {array} Array of resource timings with their contributions.
*/
ResourceTimingDecompression.addContribution = function(rts) {
if (!rts || rts.length === 0) {
return rts;
}
// Get cells in chronological order.
var cells = this.getSortedCells(rts);
// We need at least two cells and they need to begin
// with a start event. Furthermore, the last timestamp
// should be > 0.
if (cells.length < 2 ||
cells[0].val < 1.0 ||
cells[cells.length - 1].ts <= 0
) {
return rts;
}
// Compute each cell's contribution.
this.addCellContributions(cells);
// Total load time for this batch of resources.
var loadTime = cells[cells.length - 1].ts;
for (var i = 0; i < rts.length; i++) {
// Compute the contribution of each resource.
// Normalize by total load time.
rts[i].contribution = this.sumContributions(cells, rts[i]) / loadTime;
}
return rts;
};
/**
* Determines the initiatorType from a lookup
*
* @param {number} index Initiator type index
*
* @returns {string} initiatorType, or "other" if not known
*/
ResourceTimingDecompression.getInitiatorTypeFromIndex = function(index) {
if (Object.prototype.hasOwnProperty.call(this.REV_INITIATOR_TYPES, index)) {
return this.REV_INITIATOR_TYPES[index];
}
return "other";
};
/**
* Decodes a compressed ResourceTiming data string
*
* @param {string} data Compressed timing data
* @param {string} url URL
* @param {array} st server timing entries lookup
*
* @returns {ResourceTiming} ResourceTiming pseudo-object (containing all of the properties of a
* ResourceTiming object)
*/
ResourceTimingDecompression.decodeCompressedResource = function(data, url, st) {
if (!data || !url) {
return {};
}
if (ResourceTimingDecompression.HOSTNAMES_REVERSED) {
url = ResourceTimingDecompression.reverseHostname(url);
}
var initiatorType = isNaN(parseInt(data[0], 10)) ? data[0] : parseInt(data[0], 10);
data = data.length > 1 ? data.split(ResourceTimingDecompression.SPECIAL_DATA_PREFIX) : [];
var timings = data.length > 0 && data[0].length > 1 ? data[0].substring(1).split(",") : [];
var specialData = data.length > 1 ? data.slice(1) : [];
// convert all timings from base36
for (var i = 0; i < timings.length; i++) {
if (timings[i] === "") {
// startTime being 0
timings[i] = 0;
} else {
// de-base36
timings[i] = parseInt(timings[i], 36);
}
}
// special case timestamps
var startTime = timings.length >= 1 ? timings[0] : 0;
// fetchStart is either the redirectEnd time, or startTime
// NOTE: This may be later modified by Service Worker special data, which has the real timestamp if needed
var fetchStart = timings.length < 10 ?
startTime :
this.decodeCompressedResourceTimeStamp(timings, 9, startTime);
// all others are offset from startTime
var res = {
name: url,
initiatorType: this.getInitiatorTypeFromIndex(initiatorType),
startTime: startTime,
redirectStart: this.decodeCompressedResourceTimeStamp(timings, 9, startTime) > 0 ? startTime : 0,
redirectEnd: this.decodeCompressedResourceTimeStamp(timings, 9, startTime),
fetchStart: fetchStart,
domainLookupStart: this.decodeCompressedResourceTimeStamp(timings, 8, startTime),
domainLookupEnd: this.decodeCompressedResourceTimeStamp(timings, 7, startTime),
connectStart: this.decodeCompressedResourceTimeStamp(timings, 6, startTime),
secureConnectionStart: this.decodeCompressedResourceTimeStamp(timings, 5, startTime),
connectEnd: this.decodeCompressedResourceTimeStamp(timings, 4, startTime),
requestStart: this.decodeCompressedResourceTimeStamp(timings, 3, startTime),
responseStart: this.decodeCompressedResourceTimeStamp(timings, 2, startTime),
responseEnd: this.decodeCompressedResourceTimeStamp(timings, 1, startTime)
};
res.duration = res.responseEnd > 0 ? (res.responseEnd - res.startTime) : 0;
// decompress resource size data
for (i = 0; i < specialData.length; i++) {
this.decompressSpecialData(specialData[i], res, st);
}
return res;
};
/**
* Decodes a timestamp from a compressed RT array
*
* @param {number[]} timings ResourceTiming timings
* @param {number} idx Index into array
* @param {number} startTime NavigationTiming The Resource's startTime
*
* @returns {number} Timestamp, or 0 if unknown or missing
*/
ResourceTimingDecompression.decodeCompressedResourceTimeStamp = function(timings, idx, startTime) {
if (timings && timings.length >= (idx + 1)) {
if (timings[idx] !== 0) {
return timings[idx] + startTime;
}
}
return 0;
};
/**
* Decompresses script load type into the specified resource.
*
* @param {string} compressed String with a single integer.
* @param {ResourceTiming} resource ResourceTiming object.
* @returns {ResourceTiming} ResourceTiming object with decompressed script type.
*/
ResourceTimingDecompression.decompressScriptType = function(compressed, resource) {
var data = parseInt(compressed, 10);
if (!resource) {
resource = {};
}
for (var key in this.SCRIPT_ATTRIBUTES) {
if (Object.prototype.hasOwnProperty.call(this.SCRIPT_ATTRIBUTES, key)) {
resource[key] = (data & this.SCRIPT_ATTRIBUTES[key]) === this.SCRIPT_ATTRIBUTES[key];
}
}
return resource;
};
/**
* Decompresses link attributes
*
* @param {string} compressed String with a single integer.
* @param {ResourceTiming} resource ResourceTiming object.
* @returns {ResourceTiming} ResourceTiming object with decompressed link attribute type.
*/
ResourceTimingDecompression.decompressLinkAttrType = function(compressed, resource) {
var data = parseInt(compressed, 10);
if (!resource) {
resource = {};
}
if (Object.prototype.hasOwnProperty.call(this.REV_REL_TYPES, data)) {
resource.rel = this.REV_REL_TYPES[data];
}
return resource;
};
/**
* Decompresses size information back into the specified resource
*
* @param {string} compressed Compressed string
* @param {ResourceTiming} resource ResourceTiming object
* @returns {ResourceTiming} ResourceTiming object with decompressed sizes
*/
ResourceTimingDecompression.decompressSize = function(compressed, resource) {
var split, i;
if (typeof resource === "undefined") {
resource = {};
}
split = compressed.split(",");
for (i = 0; i < split.length; i++) {
if (split[i] === "_") {
// special non-delta value
split[i] = 0;
} else {
// fill in missing numbers
if (split[i] === "") {
split[i] = 0;
}
// convert back from Base36
split[i] = parseInt(split[i], 36);
if (i > 0) {
// delta against first number
split[i] += split[0];
}
}
}
// fill in missing
if (split.length === 1) {
// transferSize is a delta from encodedSize
split.push(split[0]);
}
if (split.length === 2) {
// decodedSize is a delta from encodedSize
split.push(split[0]);
}
// re-add attributes to the resource
resource.encodedBodySize = split[0];
resource.transferSize = split[1];
resource.decodedBodySize = split[2];
return resource;
};
/**
* Decompresses namespaced data back into the specified resource
*
* @param {string} compressed Compressed string
* @param {ResourceTiming} resource ResourceTiming object
* @returns {ResourceTiming} ResourceTiming object with namespaced data
*/
ResourceTimingDecompression.decompressNamespacedData = function(compressed, resource) {
resource = resource || {};
if (typeof compressed === "string") {
var delimiter = ":";
var colon = compressed.indexOf(delimiter);
if (colon > 0) {
var key = compressed.substring(0, colon);
var value = compressed.substring(colon + delimiter.length);
resource._data = resource._data || {};
if (Object.prototype.hasOwnProperty.call(resource._data, key)) {
// we are adding our 2nd or nth value (n > 2) for this key
if (!Array.isArray(resource._data[key])) {
// we are adding our 2nd value for this key, convert to array before pushing
resource._data[key] = [resource._data[key]];
}
// push it onto the array
resource._data[key].push(value);
} else {
// we are adding our 1st value for this key
resource._data[key] = value;
}
}
}
return resource;
};
/**
* Decompresses service worker data
*
* @param {string} compressed Compressed string
* @param {ResourceTiming} resource ResourceTiming object
* @returns {ResourceTiming} ResourceTiming object with decompressed special data
*/
ResourceTimingDecompression.decompressServiceWorkerData = function(compressed, resource) {
resource = resource || {};
if (typeof compressed === "string") {
var splitCompressed = compressed.split(",");
var offset = parseInt(splitCompressed[0], 36);
resource.workerStart = resource.startTime + offset;
// if fetchStart is set, also use that instead of the inferred startTime/redirectEnd
if (splitCompressed[1]) {
resource.fetchStart = resource.startTime + parseInt(splitCompressed[1], 36);
}
}
return resource;
};
/**
* Decompresses special data such as resource size or script type into the given resource.
*
* @param {string} compressed Compressed string
* @param {ResourceTiming} resource ResourceTiming object
* @param {array} st server timing entries lookup
* @returns {ResourceTiming} ResourceTiming object with decompressed special data
*/
ResourceTimingDecompression.decompressSpecialData = function(compressed, resource, st) {
var dataType;
if (!compressed || compressed.length === 0) {
return resource;
}
dataType = compressed[0];
compressed = compressed.substring(1);
if (dataType === ResourceTimingDecompression.SPECIAL_DATA_SIZE_TYPE) {
resource = this.decompressSize(compressed, resource);
} else if (dataType === ResourceTimingDecompression.SPECIAL_DATA_SCRIPT_TYPE) {
resource = this.decompressScriptType(compressed, resource);
} else if (dataType === ResourceTimingDecompression.SPECIAL_DATA_SERVERTIMING_TYPE) {
resource = this.decompressServerTimingEntries(st, compressed, resource);
} else if (dataType === ResourceTimingDecompression.SPECIAL_DATA_LINK_ATTR_TYPE) {
resource = this.decompressLinkAttrType(compressed, resource);
} else if (dataType === ResourceTimingDecompression.SPECIAL_DATA_NAMESPACED_TYPE) {
resource = this.decompressNamespacedData(compressed, resource);
} else if (dataType === ResourceTimingDecompression.SPECIAL_DATA_SERVICE_WORKER_TYPE) {
resource = this.decompressServiceWorkerData(compressed, resource);
} else if (dataType === ResourceTimingDecompression.SPECIAL_DATA_PROTOCOL) {
resource = this.decompressNextHopProtocol(compressed, resource);
}
return resource;
};
/**
* Reverse the hostname portion of a URL
*
* @param {string} url a fully-qualified URL
* @returns {string} the input URL with the hostname portion reversed, if it can be found
*/
ResourceTimingDecompression.reverseHostname = function(url) {
return url.replace(ResourceTimingDecompression.HOSTNAME_REGEX, function(m, p1, p2, p3) {
// p2 is everything after the first `://` and before the next `/`
// which includes `<username>:<password>@` and `:<port-number>`, if present
return p1 + ResourceTimingDecompression.reverseString(p2) + p3;
});
};
/**
* Reverse a string
*
* @param {string} i a string
* @returns {string} the reversed string
*/
ResourceTimingDecompression.reverseString = function(i) {
var l = i.length, o = "";
while (l--) {
o += i[l];
}
return o;
};
/**
* Decompress a list of compressed server timing entries for a resource
*
* @param {array} lookup server timing entries lookup
* @param {string} compressedList server timing entries for a resource
* @param {ResourceTiming} resource ResourceTiming object.
* @returns {ResourceTiming} ResourceTiming object with decompressed server timing entries.
*/
ResourceTimingDecompression.decompressServerTimingEntries = function(lookup, compressedList, resource) {
if (typeof resource === "undefined") {
resource = {};
}
if (lookup && compressedList) {
resource.serverTiming = compressedList.split(",").map(function(compressedEntry) {
return this.decompressServerTiming(lookup, compressedEntry);
}, this);
}
return resource;
};
/**
* Decompress a compressed server timing entry for a resource
*
* @param {array} lookup server timing entries lookup
* @param {string} key key into the lookup for one server timing entry
* @returns {object} server timing entry
*/
ResourceTimingDecompression.decompressServerTiming = function(lookup, key) {
var split = key.split(":");
var duration = Number(split[0]);
var entryIndex = 0, descriptionIndex = 0;
if (split.length > 1) {
var identity = split[1].split(".");
if (identity[0] !== "") {
entryIndex = Number(identity[0]);
}
if (identity.length > 1) {
descriptionIndex = Number(identity[1]);
}
}
var name, description = "";
if (Array.isArray(lookup[entryIndex])) {
name = lookup[entryIndex][0];
description = lookup[entryIndex][1 + descriptionIndex] || "";
} else {
name = lookup[entryIndex];
}
return {
name: name,
duration: duration,
description: description
};
};