|
| 1 | +'use strict'; |
| 2 | +// https://developer.mozilla.org/en-US/docs/Web/API/PerformanceResourceTiming |
| 3 | + |
| 4 | +const { InternalPerformanceEntry } = require('internal/perf/performance_entry'); |
| 5 | +const { SymbolToStringTag } = primordials; |
| 6 | +const assert = require('internal/assert'); |
| 7 | +const { enqueue } = require('internal/perf/observe'); |
| 8 | +const { Symbol } = primordials; |
| 9 | + |
| 10 | +const kCacheMode = Symbol('kCacheMode'); |
| 11 | +const kRequestedUrl = Symbol('kRequestedUrl'); |
| 12 | +const kTimingInfo = Symbol('kTimingInfo'); |
| 13 | +const kInitiatorType = Symbol('kInitiatorType'); |
| 14 | + |
| 15 | +class PerformanceResourceTiming extends InternalPerformanceEntry { |
| 16 | + constructor(requestedUrl, initiatorType, timingInfo, cacheMode = '') { |
| 17 | + super(requestedUrl, 'resource'); |
| 18 | + this[kInitiatorType] = initiatorType; |
| 19 | + this[kRequestedUrl] = requestedUrl; |
| 20 | + // https://fetch.spec.whatwg.org/#fetch-timing-info |
| 21 | + // This class is using timingInfo assuming it's already validated. |
| 22 | + // The spec doesn't say to validate it in the class construction. |
| 23 | + this[kTimingInfo] = timingInfo; |
| 24 | + this[kCacheMode] = cacheMode; |
| 25 | + } |
| 26 | + |
| 27 | + get [SymbolToStringTag]() { |
| 28 | + return 'PerformanceResourceTiming'; |
| 29 | + } |
| 30 | + |
| 31 | + get name() { |
| 32 | + return this[kRequestedUrl]; |
| 33 | + } |
| 34 | + |
| 35 | + get startTime() { |
| 36 | + return this[kTimingInfo].startTime; |
| 37 | + } |
| 38 | + |
| 39 | + get duration() { |
| 40 | + return this[kTimingInfo].endTime - this[kTimingInfo].startTime; |
| 41 | + } |
| 42 | + |
| 43 | + get workerStart() { |
| 44 | + return this[kTimingInfo].finalServiceWorkerStartTime; |
| 45 | + } |
| 46 | + |
| 47 | + get redirectStart() { |
| 48 | + return this[kTimingInfo].redirectStartTime; |
| 49 | + } |
| 50 | + |
| 51 | + get redirectEnd() { |
| 52 | + return this[kTimingInfo].redirectEndTime; |
| 53 | + } |
| 54 | + |
| 55 | + get fetchStart() { |
| 56 | + return this[kTimingInfo].postRedirectStartTime; |
| 57 | + } |
| 58 | + |
| 59 | + get domainLookupStart() { |
| 60 | + return this[kTimingInfo].finalConnectionTimingInfo?.domainLookupStartTime; |
| 61 | + } |
| 62 | + |
| 63 | + get domainLookupEnd() { |
| 64 | + return this[kTimingInfo].finalConnectionTimingInfo?.domainLookupEndTime; |
| 65 | + } |
| 66 | + |
| 67 | + get connectStart() { |
| 68 | + return this[kTimingInfo].finalConnectionTimingInfo?.connectionStartTime; |
| 69 | + } |
| 70 | + |
| 71 | + get connectEnd() { |
| 72 | + return this[kTimingInfo].finalConnectionTimingInfo?.connectionEndTime; |
| 73 | + } |
| 74 | + |
| 75 | + get secureConnectionStart() { |
| 76 | + return this[kTimingInfo] |
| 77 | + .finalConnectionTimingInfo?.secureConnectionStartTime; |
| 78 | + } |
| 79 | + |
| 80 | + get nextHopProtocol() { |
| 81 | + return this[kTimingInfo] |
| 82 | + .finalConnectionTimingInfo?.ALPNNegotiatedProtocol; |
| 83 | + } |
| 84 | + |
| 85 | + get requestStart() { |
| 86 | + return this[kTimingInfo].finalNetworkRequestStartTime; |
| 87 | + } |
| 88 | + |
| 89 | + get responseStart() { |
| 90 | + return this[kTimingInfo].finalNetworkResponseStartTime; |
| 91 | + } |
| 92 | + |
| 93 | + get responseEnd() { |
| 94 | + return this[kTimingInfo].endTime; |
| 95 | + } |
| 96 | + |
| 97 | + get encodedBodySize() { |
| 98 | + return this[kTimingInfo].encodedBodySize; |
| 99 | + } |
| 100 | + |
| 101 | + get decodedBodySize() { |
| 102 | + return this[kTimingInfo].decodedBodySize; |
| 103 | + } |
| 104 | + |
| 105 | + get transferSize() { |
| 106 | + if (this[kCacheMode] === 'local') return 0; |
| 107 | + if (this[kCacheMode] === 'validated') return 300; |
| 108 | + |
| 109 | + return this[kTimingInfo].encodedBodySize + 300; |
| 110 | + } |
| 111 | + |
| 112 | + toJSON() { |
| 113 | + return { |
| 114 | + name: this.name, |
| 115 | + entryType: this.entryType, |
| 116 | + startTime: this.startTime, |
| 117 | + duration: this.duration, |
| 118 | + initiatorType: this[kInitiatorType], |
| 119 | + nextHopProtocol: this.nextHopProtocol, |
| 120 | + workerStart: this.workerStart, |
| 121 | + redirectStart: this.redirectStart, |
| 122 | + redirectEnd: this.redirectEnd, |
| 123 | + fetchStart: this.fetchStart, |
| 124 | + domainLookupStart: this.domainLookupStart, |
| 125 | + domainLookupEnd: this.domainLookupEnd, |
| 126 | + connectStart: this.connectStart, |
| 127 | + connectEnd: this.connectEnd, |
| 128 | + secureConnectionStart: this.secureConnectionStart, |
| 129 | + requestStart: this.requestStart, |
| 130 | + responseStart: this.responseStart, |
| 131 | + responseEnd: this.responseEnd, |
| 132 | + transferSize: this.transferSize, |
| 133 | + encodedBodySize: this.encodedBodySize, |
| 134 | + decodedBodySize: this.decodedBodySize, |
| 135 | + }; |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +// https://w3c.github.io/resource-timing/#dfn-mark-resource-timing |
| 140 | +function markResourceTiming( |
| 141 | + timingInfo, |
| 142 | + requestedUrl, |
| 143 | + initiatorType, |
| 144 | + global, |
| 145 | + cacheMode, |
| 146 | +) { |
| 147 | + // https://w3c.github.io/resource-timing/#dfn-setup-the-resource-timing-entry |
| 148 | + assert(cacheMode === '' || cacheMode === 'local'); |
| 149 | + const resource = new PerformanceResourceTiming( |
| 150 | + requestedUrl, |
| 151 | + initiatorType, |
| 152 | + timingInfo, |
| 153 | + cacheMode, |
| 154 | + ); |
| 155 | + enqueue(resource); |
| 156 | + return resource; |
| 157 | +} |
| 158 | + |
| 159 | +module.exports = { |
| 160 | + PerformanceResourceTiming, |
| 161 | + markResourceTiming, |
| 162 | +}; |
0 commit comments