11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci// https://developer.mozilla.org/en-US/docs/Web/API/PerformanceResourceTiming
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst { InternalPerformanceEntry } = require('internal/perf/performance_entry');
51cb0ef41Sopenharmony_ciconst { SymbolToStringTag } = primordials;
61cb0ef41Sopenharmony_ciconst assert = require('internal/assert');
71cb0ef41Sopenharmony_ciconst { enqueue, bufferResourceTiming } = require('internal/perf/observe');
81cb0ef41Sopenharmony_ciconst { Symbol, ObjectSetPrototypeOf } = primordials;
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst kCacheMode = Symbol('kCacheMode');
111cb0ef41Sopenharmony_ciconst kRequestedUrl = Symbol('kRequestedUrl');
121cb0ef41Sopenharmony_ciconst kTimingInfo = Symbol('kTimingInfo');
131cb0ef41Sopenharmony_ciconst kInitiatorType = Symbol('kInitiatorType');
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciconst {
161cb0ef41Sopenharmony_ci  codes: {
171cb0ef41Sopenharmony_ci    ERR_ILLEGAL_CONSTRUCTOR,
181cb0ef41Sopenharmony_ci  },
191cb0ef41Sopenharmony_ci} = require('internal/errors');
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciclass InternalPerformanceResourceTiming extends InternalPerformanceEntry {
221cb0ef41Sopenharmony_ci  constructor(requestedUrl, initiatorType, timingInfo, cacheMode = '') {
231cb0ef41Sopenharmony_ci    super(requestedUrl, 'resource');
241cb0ef41Sopenharmony_ci    this[kInitiatorType] = initiatorType;
251cb0ef41Sopenharmony_ci    this[kRequestedUrl] = requestedUrl;
261cb0ef41Sopenharmony_ci    // https://fetch.spec.whatwg.org/#fetch-timing-info
271cb0ef41Sopenharmony_ci    // This class is using timingInfo assuming it's already validated.
281cb0ef41Sopenharmony_ci    // The spec doesn't say to validate it in the class construction.
291cb0ef41Sopenharmony_ci    this[kTimingInfo] = timingInfo;
301cb0ef41Sopenharmony_ci    this[kCacheMode] = cacheMode;
311cb0ef41Sopenharmony_ci  }
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  get [SymbolToStringTag]() {
341cb0ef41Sopenharmony_ci    return 'PerformanceResourceTiming';
351cb0ef41Sopenharmony_ci  }
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  get name() {
381cb0ef41Sopenharmony_ci    return this[kRequestedUrl];
391cb0ef41Sopenharmony_ci  }
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci  get startTime() {
421cb0ef41Sopenharmony_ci    return this[kTimingInfo].startTime;
431cb0ef41Sopenharmony_ci  }
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci  get duration() {
461cb0ef41Sopenharmony_ci    return this[kTimingInfo].endTime - this[kTimingInfo].startTime;
471cb0ef41Sopenharmony_ci  }
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci  get initiatorType() {
501cb0ef41Sopenharmony_ci    return this[kInitiatorType];
511cb0ef41Sopenharmony_ci  }
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  get workerStart() {
541cb0ef41Sopenharmony_ci    return this[kTimingInfo].finalServiceWorkerStartTime;
551cb0ef41Sopenharmony_ci  }
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  get redirectStart() {
581cb0ef41Sopenharmony_ci    return this[kTimingInfo].redirectStartTime;
591cb0ef41Sopenharmony_ci  }
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  get redirectEnd() {
621cb0ef41Sopenharmony_ci    return this[kTimingInfo].redirectEndTime;
631cb0ef41Sopenharmony_ci  }
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci  get fetchStart() {
661cb0ef41Sopenharmony_ci    return this[kTimingInfo].postRedirectStartTime;
671cb0ef41Sopenharmony_ci  }
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci  get domainLookupStart() {
701cb0ef41Sopenharmony_ci    return this[kTimingInfo].finalConnectionTimingInfo?.domainLookupStartTime;
711cb0ef41Sopenharmony_ci  }
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci  get domainLookupEnd() {
741cb0ef41Sopenharmony_ci    return this[kTimingInfo].finalConnectionTimingInfo?.domainLookupEndTime;
751cb0ef41Sopenharmony_ci  }
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci  get connectStart() {
781cb0ef41Sopenharmony_ci    return this[kTimingInfo].finalConnectionTimingInfo?.connectionStartTime;
791cb0ef41Sopenharmony_ci  }
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci  get connectEnd() {
821cb0ef41Sopenharmony_ci    return this[kTimingInfo].finalConnectionTimingInfo?.connectionEndTime;
831cb0ef41Sopenharmony_ci  }
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci  get secureConnectionStart() {
861cb0ef41Sopenharmony_ci    return this[kTimingInfo]
871cb0ef41Sopenharmony_ci      .finalConnectionTimingInfo?.secureConnectionStartTime;
881cb0ef41Sopenharmony_ci  }
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci  get nextHopProtocol() {
911cb0ef41Sopenharmony_ci    return this[kTimingInfo]
921cb0ef41Sopenharmony_ci      .finalConnectionTimingInfo?.ALPNNegotiatedProtocol;
931cb0ef41Sopenharmony_ci  }
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ci  get requestStart() {
961cb0ef41Sopenharmony_ci    return this[kTimingInfo].finalNetworkRequestStartTime;
971cb0ef41Sopenharmony_ci  }
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ci  get responseStart() {
1001cb0ef41Sopenharmony_ci    return this[kTimingInfo].finalNetworkResponseStartTime;
1011cb0ef41Sopenharmony_ci  }
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ci  get responseEnd() {
1041cb0ef41Sopenharmony_ci    return this[kTimingInfo].endTime;
1051cb0ef41Sopenharmony_ci  }
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ci  get encodedBodySize() {
1081cb0ef41Sopenharmony_ci    return this[kTimingInfo].encodedBodySize;
1091cb0ef41Sopenharmony_ci  }
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_ci  get decodedBodySize() {
1121cb0ef41Sopenharmony_ci    return this[kTimingInfo].decodedBodySize;
1131cb0ef41Sopenharmony_ci  }
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_ci  get transferSize() {
1161cb0ef41Sopenharmony_ci    if (this[kCacheMode] === 'local') return 0;
1171cb0ef41Sopenharmony_ci    if (this[kCacheMode] === 'validated') return 300;
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ci    return this[kTimingInfo].encodedBodySize + 300;
1201cb0ef41Sopenharmony_ci  }
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_ci  toJSON() {
1231cb0ef41Sopenharmony_ci    return {
1241cb0ef41Sopenharmony_ci      name: this.name,
1251cb0ef41Sopenharmony_ci      entryType: this.entryType,
1261cb0ef41Sopenharmony_ci      startTime: this.startTime,
1271cb0ef41Sopenharmony_ci      duration: this.duration,
1281cb0ef41Sopenharmony_ci      initiatorType: this[kInitiatorType],
1291cb0ef41Sopenharmony_ci      nextHopProtocol: this.nextHopProtocol,
1301cb0ef41Sopenharmony_ci      workerStart: this.workerStart,
1311cb0ef41Sopenharmony_ci      redirectStart: this.redirectStart,
1321cb0ef41Sopenharmony_ci      redirectEnd: this.redirectEnd,
1331cb0ef41Sopenharmony_ci      fetchStart: this.fetchStart,
1341cb0ef41Sopenharmony_ci      domainLookupStart: this.domainLookupStart,
1351cb0ef41Sopenharmony_ci      domainLookupEnd: this.domainLookupEnd,
1361cb0ef41Sopenharmony_ci      connectStart: this.connectStart,
1371cb0ef41Sopenharmony_ci      connectEnd: this.connectEnd,
1381cb0ef41Sopenharmony_ci      secureConnectionStart: this.secureConnectionStart,
1391cb0ef41Sopenharmony_ci      requestStart: this.requestStart,
1401cb0ef41Sopenharmony_ci      responseStart: this.responseStart,
1411cb0ef41Sopenharmony_ci      responseEnd: this.responseEnd,
1421cb0ef41Sopenharmony_ci      transferSize: this.transferSize,
1431cb0ef41Sopenharmony_ci      encodedBodySize: this.encodedBodySize,
1441cb0ef41Sopenharmony_ci      decodedBodySize: this.decodedBodySize,
1451cb0ef41Sopenharmony_ci    };
1461cb0ef41Sopenharmony_ci  }
1471cb0ef41Sopenharmony_ci}
1481cb0ef41Sopenharmony_ci
1491cb0ef41Sopenharmony_ciclass PerformanceResourceTiming extends InternalPerformanceResourceTiming {
1501cb0ef41Sopenharmony_ci  constructor() {
1511cb0ef41Sopenharmony_ci    throw new ERR_ILLEGAL_CONSTRUCTOR();
1521cb0ef41Sopenharmony_ci  }
1531cb0ef41Sopenharmony_ci}
1541cb0ef41Sopenharmony_ci
1551cb0ef41Sopenharmony_ci// https://w3c.github.io/resource-timing/#dfn-mark-resource-timing
1561cb0ef41Sopenharmony_cifunction markResourceTiming(
1571cb0ef41Sopenharmony_ci  timingInfo,
1581cb0ef41Sopenharmony_ci  requestedUrl,
1591cb0ef41Sopenharmony_ci  initiatorType,
1601cb0ef41Sopenharmony_ci  global,
1611cb0ef41Sopenharmony_ci  cacheMode,
1621cb0ef41Sopenharmony_ci) {
1631cb0ef41Sopenharmony_ci  // https://w3c.github.io/resource-timing/#dfn-setup-the-resource-timing-entry
1641cb0ef41Sopenharmony_ci  assert(
1651cb0ef41Sopenharmony_ci    cacheMode === '' || cacheMode === 'local',
1661cb0ef41Sopenharmony_ci    'cache must be an empty string or \'local\'',
1671cb0ef41Sopenharmony_ci  );
1681cb0ef41Sopenharmony_ci  const resource = new InternalPerformanceResourceTiming(
1691cb0ef41Sopenharmony_ci    requestedUrl,
1701cb0ef41Sopenharmony_ci    initiatorType,
1711cb0ef41Sopenharmony_ci    timingInfo,
1721cb0ef41Sopenharmony_ci    cacheMode,
1731cb0ef41Sopenharmony_ci  );
1741cb0ef41Sopenharmony_ci
1751cb0ef41Sopenharmony_ci  ObjectSetPrototypeOf(resource, PerformanceResourceTiming.prototype);
1761cb0ef41Sopenharmony_ci  enqueue(resource);
1771cb0ef41Sopenharmony_ci  bufferResourceTiming(resource);
1781cb0ef41Sopenharmony_ci  return resource;
1791cb0ef41Sopenharmony_ci}
1801cb0ef41Sopenharmony_ci
1811cb0ef41Sopenharmony_cimodule.exports = {
1821cb0ef41Sopenharmony_ci  PerformanceResourceTiming,
1831cb0ef41Sopenharmony_ci  markResourceTiming,
1841cb0ef41Sopenharmony_ci};
185