All files / lib/internal/perf resource_timing.js

98.88% Statements 177/179
93.33% Branches 28/30
100% Functions 23/23
98.88% Lines 177/179

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 18025x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 4x 4x 4x 4x 4x 4x 4x 4x 4x 25x 25x     25x 25x 6x 6x 25x 25x 3x 3x 25x 25x 3x 3x 25x 25x 2x 2x 25x 25x 2x 2x 25x 25x 2x 2x 25x 25x 2x 2x 25x 25x 2x 2x 25x 25x 2x 2x 25x 25x 2x 2x 25x 25x 2x 2x 25x 25x 2x 2x 2x 25x 25x 2x 2x 2x 25x 25x 2x 2x 25x 25x 2x 2x 25x 25x 2x 2x 25x 25x 2x 2x 25x 25x 2x 2x 25x 25x 3x 3x 1x 1x 3x 25x 25x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 25x 25x 25x 25x 1x 1x 25x 25x 25x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 25x 25x 25x 25x 25x  
'use strict';
// https://developer.mozilla.org/en-US/docs/Web/API/PerformanceResourceTiming
 
const { InternalPerformanceEntry } = require('internal/perf/performance_entry');
const { SymbolToStringTag } = primordials;
const assert = require('internal/assert');
const { enqueue } = require('internal/perf/observe');
const { Symbol, ObjectSetPrototypeOf } = primordials;
 
const kCacheMode = Symbol('kCacheMode');
const kRequestedUrl = Symbol('kRequestedUrl');
const kTimingInfo = Symbol('kTimingInfo');
const kInitiatorType = Symbol('kInitiatorType');
 
const {
  codes: {
    ERR_ILLEGAL_CONSTRUCTOR,
  }
} = require('internal/errors');
 
class InternalPerformanceResourceTiming extends InternalPerformanceEntry {
  constructor(requestedUrl, initiatorType, timingInfo, cacheMode = '') {
    super(requestedUrl, 'resource');
    this[kInitiatorType] = initiatorType;
    this[kRequestedUrl] = requestedUrl;
    // https://fetch.spec.whatwg.org/#fetch-timing-info
    // This class is using timingInfo assuming it's already validated.
    // The spec doesn't say to validate it in the class construction.
    this[kTimingInfo] = timingInfo;
    this[kCacheMode] = cacheMode;
  }
 
  get [SymbolToStringTag]() {
    return 'PerformanceResourceTiming';
  }
 
  get name() {
    return this[kRequestedUrl];
  }
 
  get startTime() {
    return this[kTimingInfo].startTime;
  }
 
  get duration() {
    return this[kTimingInfo].endTime - this[kTimingInfo].startTime;
  }
 
  get workerStart() {
    return this[kTimingInfo].finalServiceWorkerStartTime;
  }
 
  get redirectStart() {
    return this[kTimingInfo].redirectStartTime;
  }
 
  get redirectEnd() {
    return this[kTimingInfo].redirectEndTime;
  }
 
  get fetchStart() {
    return this[kTimingInfo].postRedirectStartTime;
  }
 
  get domainLookupStart() {
    return this[kTimingInfo].finalConnectionTimingInfo?.domainLookupStartTime;
  }
 
  get domainLookupEnd() {
    return this[kTimingInfo].finalConnectionTimingInfo?.domainLookupEndTime;
  }
 
  get connectStart() {
    return this[kTimingInfo].finalConnectionTimingInfo?.connectionStartTime;
  }
 
  get connectEnd() {
    return this[kTimingInfo].finalConnectionTimingInfo?.connectionEndTime;
  }
 
  get secureConnectionStart() {
    return this[kTimingInfo]
      .finalConnectionTimingInfo?.secureConnectionStartTime;
  }
 
  get nextHopProtocol() {
    return this[kTimingInfo]
      .finalConnectionTimingInfo?.ALPNNegotiatedProtocol;
  }
 
  get requestStart() {
    return this[kTimingInfo].finalNetworkRequestStartTime;
  }
 
  get responseStart() {
    return this[kTimingInfo].finalNetworkResponseStartTime;
  }
 
  get responseEnd() {
    return this[kTimingInfo].endTime;
  }
 
  get encodedBodySize() {
    return this[kTimingInfo].encodedBodySize;
  }
 
  get decodedBodySize() {
    return this[kTimingInfo].decodedBodySize;
  }
 
  get transferSize() {
    if (this[kCacheMode] === 'local') return 0;
    if (this[kCacheMode] === 'validated') return 300;
 
    return this[kTimingInfo].encodedBodySize + 300;
  }
 
  toJSON() {
    return {
      name: this.name,
      entryType: this.entryType,
      startTime: this.startTime,
      duration: this.duration,
      initiatorType: this[kInitiatorType],
      nextHopProtocol: this.nextHopProtocol,
      workerStart: this.workerStart,
      redirectStart: this.redirectStart,
      redirectEnd: this.redirectEnd,
      fetchStart: this.fetchStart,
      domainLookupStart: this.domainLookupStart,
      domainLookupEnd: this.domainLookupEnd,
      connectStart: this.connectStart,
      connectEnd: this.connectEnd,
      secureConnectionStart: this.secureConnectionStart,
      requestStart: this.requestStart,
      responseStart: this.responseStart,
      responseEnd: this.responseEnd,
      transferSize: this.transferSize,
      encodedBodySize: this.encodedBodySize,
      decodedBodySize: this.decodedBodySize,
    };
  }
}
 
class PerformanceResourceTiming extends InternalPerformanceResourceTiming {
  constructor() {
    throw new ERR_ILLEGAL_CONSTRUCTOR();
  }
}
 
// https://w3c.github.io/resource-timing/#dfn-mark-resource-timing
function markResourceTiming(
  timingInfo,
  requestedUrl,
  initiatorType,
  global,
  cacheMode,
) {
  // https://w3c.github.io/resource-timing/#dfn-setup-the-resource-timing-entry
  assert(
    cacheMode === '' || cacheMode === 'local',
    'cache must be an empty string or \'local\'',
  );
  const resource = new InternalPerformanceResourceTiming(
    requestedUrl,
    initiatorType,
    timingInfo,
    cacheMode,
  );
 
  ObjectSetPrototypeOf(resource, PerformanceResourceTiming.prototype);
  enqueue(resource);
  return resource;
}
 
module.exports = {
  PerformanceResourceTiming,
  markResourceTiming,
};