lighthouse 12.0.0-dev.20240609 → 12.0.0-dev.20240610

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,7 +10,6 @@ import {NetworkNode} from './network-node.js';
10
10
  import {CPUNode} from './cpu-node.js';
11
11
  import {TraceProcessor} from '../tracehouse/trace-processor.js';
12
12
  import {NetworkAnalyzer} from './simulator/network-analyzer.js';
13
- import {RESOURCE_TYPES} from '../network-request.js';
14
13
 
15
14
  /** @typedef {import('./base-node.js').Node} Node */
16
15
  /** @typedef {Omit<LH.Artifacts['URL'], 'finalDisplayedUrl'>} URLArtifact */
@@ -543,347 +542,6 @@ class PageDependencyGraph {
543
542
  }
544
543
 
545
544
  /**
546
- * @param {Lantern.NetworkRequest} request The request to find the initiator of
547
- * @param {Map<string, Lantern.NetworkRequest[]>} requestsByURL
548
- * @return {Lantern.NetworkRequest|null}
549
- */
550
- static chooseInitiatorRequest(request, requestsByURL) {
551
- if (request.redirectSource) {
552
- return request.redirectSource;
553
- }
554
-
555
- const initiatorURL = PageDependencyGraph.getNetworkInitiators(request)[0];
556
- let candidates = requestsByURL.get(initiatorURL) || [];
557
- // The (valid) initiator must come before the initiated request.
558
- candidates = candidates.filter(c => {
559
- return c.responseHeadersEndTime <= request.rendererStartTime &&
560
- c.finished && !c.failed;
561
- });
562
- if (candidates.length > 1) {
563
- // Disambiguate based on prefetch. Prefetch requests have type 'Other' and cannot
564
- // initiate requests, so we drop them here.
565
- const nonPrefetchCandidates = candidates.filter(
566
- cand => cand.resourceType !== RESOURCE_TYPES.Other);
567
- if (nonPrefetchCandidates.length) {
568
- candidates = nonPrefetchCandidates;
569
- }
570
- }
571
- if (candidates.length > 1) {
572
- // Disambiguate based on frame. It's likely that the initiator comes from the same frame.
573
- const sameFrameCandidates = candidates.filter(cand => cand.frameId === request.frameId);
574
- if (sameFrameCandidates.length) {
575
- candidates = sameFrameCandidates;
576
- }
577
- }
578
- if (candidates.length > 1 && request.initiator.type === 'parser') {
579
- // Filter to just Documents when initiator type is parser.
580
- const documentCandidates = candidates.filter(cand =>
581
- cand.resourceType === RESOURCE_TYPES.Document);
582
- if (documentCandidates.length) {
583
- candidates = documentCandidates;
584
- }
585
- }
586
- if (candidates.length > 1) {
587
- // If all real loads came from successful preloads (url preloaded and
588
- // loads came from the cache), filter to link rel=preload request(s).
589
- const linkPreloadCandidates = candidates.filter(c => c.isLinkPreload);
590
- if (linkPreloadCandidates.length) {
591
- const nonPreloadCandidates = candidates.filter(c => !c.isLinkPreload);
592
- const allPreloaded = nonPreloadCandidates.every(c => c.fromDiskCache || c.fromMemoryCache);
593
- if (nonPreloadCandidates.length && allPreloaded) {
594
- candidates = linkPreloadCandidates;
595
- }
596
- }
597
- }
598
-
599
- // Only return an initiator if the result is unambiguous.
600
- return candidates.length === 1 ? candidates[0] : null;
601
- }
602
-
603
- /**
604
- * Returns a map of `pid` -> `tid[]`.
605
- * @param {LH.Trace} trace
606
- * @return {Map<number, number[]>}
607
- */
608
- static _findWorkerThreads(trace) {
609
- // TODO: WorkersHandler in TraceEngine needs to be updated to also include `pid` (only had `tid`).
610
- const workerThreads = new Map();
611
- const workerCreationEvents = ['ServiceWorker thread', 'DedicatedWorker thread'];
612
-
613
- for (const event of trace.traceEvents) {
614
- if (event.name !== 'thread_name' || !event.args.name) {
615
- continue;
616
- }
617
- if (!workerCreationEvents.includes(event.args.name)) {
618
- continue;
619
- }
620
-
621
- const tids = workerThreads.get(event.pid);
622
- if (tids) {
623
- tids.push(event.tid);
624
- } else {
625
- workerThreads.set(event.pid, [event.tid]);
626
- }
627
- }
628
-
629
- return workerThreads;
630
- }
631
-
632
- /**
633
- * @param {URL|string} url
634
- */
635
- static _createParsedUrl(url) {
636
- if (typeof url === 'string') {
637
- url = new URL(url);
638
- }
639
- return {
640
- scheme: url.protocol.split(':')[0],
641
- // Intentional, DevTools uses different terminology
642
- host: url.hostname,
643
- securityOrigin: url.origin,
644
- };
645
- }
646
-
647
- /**
648
- * @param {LH.Artifacts.TraceEngineResult} traceEngineResult
649
- * @param {Map<number, number[]>} workerThreads
650
- * @param {import('@paulirish/trace_engine/models/trace/types/TraceEvents.js').SyntheticNetworkRequest} request
651
- * @return {Lantern.NetworkRequest=}
652
- */
653
- static _createLanternRequest(traceEngineResult, workerThreads, request) {
654
- if (request.args.data.connectionId === undefined ||
655
- request.args.data.connectionReused === undefined) {
656
- throw new Error('Trace is too old');
657
- }
658
-
659
- let url;
660
- try {
661
- url = new URL(request.args.data.url);
662
- } catch (e) {
663
- return;
664
- }
665
-
666
- const timing = request.args.data.timing ? {
667
- // These two timings are not included in the trace.
668
- workerFetchStart: -1,
669
- workerRespondWithSettled: -1,
670
- ...request.args.data.timing,
671
- } : undefined;
672
-
673
- const networkRequestTime = timing ?
674
- timing.requestTime * 1000 :
675
- request.args.data.syntheticData.downloadStart / 1000;
676
-
677
- let fromWorker = false;
678
- const tids = workerThreads.get(request.pid);
679
- if (tids?.includes(request.tid)) {
680
- fromWorker = true;
681
- }
682
-
683
- // TraceEngine collects worker thread ids in a different manner than `workerThreads` does.
684
- // AFAIK these should be equivalent, but in case they are not let's also check this for now.
685
- if (traceEngineResult.data.Workers.workerIdByThread.has(request.tid)) {
686
- fromWorker = true;
687
- }
688
-
689
- // `initiator` in the trace does not contain the stack trace for JS-initiated
690
- // requests. Instead, that is stored in the `stackTrace` property of the SyntheticNetworkRequest.
691
- // There are some minor differences in the fields, accounted for here.
692
- // Most importantly, there seems to be fewer frames in the trace than the equivalent
693
- // events over the CDP. This results in less accuracy in determining the initiator request,
694
- // which means less edges in the graph, which mean worse results.
695
- // TODO: Should fix in Chromium.
696
- /** @type {Lantern.NetworkRequest['initiator']} */
697
- const initiator = request.args.data.initiator ?? {type: 'other'};
698
- if (request.args.data.stackTrace) {
699
- const callFrames = request.args.data.stackTrace.map(f => {
700
- return {
701
- scriptId: String(f.scriptId),
702
- url: f.url,
703
- lineNumber: f.lineNumber - 1,
704
- columnNumber: f.columnNumber - 1,
705
- functionName: f.functionName,
706
- };
707
- });
708
- initiator.stack = {callFrames};
709
- }
710
-
711
- let resourceType = request.args.data.resourceType;
712
- if (request.args.data.initiator?.fetchType === 'xmlhttprequest') {
713
- // @ts-expect-error yes XHR is a valid ResourceType. TypeScript const enums are so unhelpful.
714
- resourceType = 'XHR';
715
- } else if (request.args.data.initiator?.fetchType === 'fetch') {
716
- // @ts-expect-error yes Fetch is a valid ResourceType. TypeScript const enums are so unhelpful.
717
- resourceType = 'Fetch';
718
- }
719
-
720
- // TODO: set decodedBodyLength for data urls in Trace Engine.
721
- let resourceSize = request.args.data.decodedBodyLength ?? 0;
722
- if (url.protocol === 'data:' && resourceSize === 0) {
723
- const needle = 'base64,';
724
- const index = url.pathname.indexOf(needle);
725
- if (index !== -1) {
726
- resourceSize = atob(url.pathname.substring(index + needle.length)).length;
727
- }
728
- }
729
-
730
- return {
731
- rawRequest: request,
732
- requestId: request.args.data.requestId,
733
- connectionId: request.args.data.connectionId,
734
- connectionReused: request.args.data.connectionReused,
735
- url: request.args.data.url,
736
- protocol: request.args.data.protocol,
737
- parsedURL: this._createParsedUrl(url),
738
- documentURL: request.args.data.requestingFrameUrl,
739
- rendererStartTime: request.ts / 1000,
740
- networkRequestTime,
741
- responseHeadersEndTime: request.args.data.syntheticData.downloadStart / 1000,
742
- networkEndTime: request.args.data.syntheticData.finishTime / 1000,
743
- transferSize: request.args.data.encodedDataLength,
744
- resourceSize,
745
- fromDiskCache: request.args.data.syntheticData.isDiskCached,
746
- fromMemoryCache: request.args.data.syntheticData.isMemoryCached,
747
- isLinkPreload: request.args.data.isLinkPreload,
748
- finished: request.args.data.finished,
749
- failed: request.args.data.failed,
750
- statusCode: request.args.data.statusCode,
751
- initiator,
752
- timing,
753
- resourceType,
754
- mimeType: request.args.data.mimeType,
755
- priority: request.args.data.priority,
756
- frameId: request.args.data.frame,
757
- fromWorker,
758
- // Set later.
759
- redirects: undefined,
760
- redirectSource: undefined,
761
- redirectDestination: undefined,
762
- initiatorRequest: undefined,
763
- };
764
- }
765
-
766
- /**
767
- *
768
- * @param {Lantern.NetworkRequest[]} lanternRequests
769
- */
770
- static _linkInitiators(lanternRequests) {
771
- /** @type {Map<string, Lantern.NetworkRequest[]>} */
772
- const requestsByURL = new Map();
773
- for (const request of lanternRequests) {
774
- const requests = requestsByURL.get(request.url) || [];
775
- requests.push(request);
776
- requestsByURL.set(request.url, requests);
777
- }
778
-
779
- for (const request of lanternRequests) {
780
- const initiatorRequest = PageDependencyGraph.chooseInitiatorRequest(request, requestsByURL);
781
- if (initiatorRequest) {
782
- request.initiatorRequest = initiatorRequest;
783
- }
784
- }
785
- }
786
-
787
- /**
788
- * @param {LH.TraceEvent[]} mainThreadEvents
789
- * @param {LH.Trace} trace
790
- * @param {LH.Artifacts.TraceEngineResult} traceEngineResult
791
- * @param {LH.Artifacts.URL} URL
792
- */
793
- static async createGraphFromTrace(mainThreadEvents, trace, traceEngineResult, URL) {
794
- const workerThreads = this._findWorkerThreads(trace);
795
-
796
- /** @type {Lantern.NetworkRequest[]} */
797
- const lanternRequests = [];
798
- for (const request of traceEngineResult.data.NetworkRequests.byTime) {
799
- const lanternRequest = this._createLanternRequest(traceEngineResult, workerThreads, request);
800
- if (lanternRequest) {
801
- lanternRequests.push(lanternRequest);
802
- }
803
- }
804
-
805
- // TraceEngine consolidates all redirects into a single request object, but lantern needs
806
- // an entry for each redirected request.
807
- for (const request of [...lanternRequests]) {
808
- if (!request.rawRequest) continue;
809
-
810
- const redirects = request.rawRequest.args.data.redirects;
811
- if (!redirects.length) continue;
812
-
813
- const requestChain = [];
814
- for (const redirect of redirects) {
815
- const redirectedRequest = structuredClone(request);
816
-
817
- redirectedRequest.networkRequestTime = redirect.ts / 1000;
818
- redirectedRequest.rendererStartTime = redirectedRequest.networkRequestTime;
819
-
820
- redirectedRequest.networkEndTime = (redirect.ts + redirect.dur) / 1000;
821
- redirectedRequest.responseHeadersEndTime = redirectedRequest.networkEndTime;
822
-
823
- redirectedRequest.timing = {
824
- requestTime: redirectedRequest.networkRequestTime / 1000,
825
- receiveHeadersStart: redirectedRequest.responseHeadersEndTime,
826
- receiveHeadersEnd: redirectedRequest.responseHeadersEndTime,
827
- proxyStart: -1,
828
- proxyEnd: -1,
829
- dnsStart: -1,
830
- dnsEnd: -1,
831
- connectStart: -1,
832
- connectEnd: -1,
833
- sslStart: -1,
834
- sslEnd: -1,
835
- sendStart: -1,
836
- sendEnd: -1,
837
- workerStart: -1,
838
- workerReady: -1,
839
- workerFetchStart: -1,
840
- workerRespondWithSettled: -1,
841
- pushStart: -1,
842
- pushEnd: -1,
843
- };
844
-
845
- redirectedRequest.url = redirect.url;
846
- redirectedRequest.parsedURL = this._createParsedUrl(redirect.url);
847
- // TODO: TraceEngine is not retaining the actual status code.
848
- redirectedRequest.statusCode = 302;
849
- redirectedRequest.resourceType = undefined;
850
- // TODO: TraceEngine is not retaining transfer size of redirected request.
851
- redirectedRequest.transferSize = 400;
852
- requestChain.push(redirectedRequest);
853
- lanternRequests.push(redirectedRequest);
854
- }
855
- requestChain.push(request);
856
-
857
- for (let i = 0; i < requestChain.length; i++) {
858
- const request = requestChain[i];
859
- if (i > 0) {
860
- request.redirectSource = requestChain[i - 1];
861
- request.redirects = requestChain.slice(0, i);
862
- }
863
- if (i !== requestChain.length - 1) {
864
- request.redirectDestination = requestChain[i + 1];
865
- }
866
- }
867
-
868
- // Apply the `:redirect` requestId convention: only redirects[0].requestId is the actual
869
- // requestId, all the rest have n occurences of `:redirect` as a suffix.
870
- for (let i = 1; i < requestChain.length; i++) {
871
- requestChain[i].requestId = `${requestChain[i - 1].requestId}:redirect`;
872
- }
873
- }
874
-
875
- this._linkInitiators(lanternRequests);
876
-
877
- // This would already be sorted by rendererStartTime, if not for the redirect unwrapping done
878
- // above.
879
- lanternRequests.sort((a, b) => a.rendererStartTime - b.rendererStartTime);
880
-
881
- const graph = PageDependencyGraph.createGraph(mainThreadEvents, lanternRequests, URL);
882
- return {graph, records: lanternRequests};
883
- }
884
-
885
- /**
886
- *
887
545
  * @param {Node} rootNode
888
546
  */
889
547
  static printGraph(rootNode, widthInCharacters = 100) {
@@ -0,0 +1,25 @@
1
+ export type MetricName = import('@paulirish/trace_engine/models/trace/handlers/PageLoadMetricsHandler.js').MetricName;
2
+ export type MetricScore = import('@paulirish/trace_engine/models/trace/handlers/PageLoadMetricsHandler.js').MetricScore;
3
+ /** @typedef {import('@paulirish/trace_engine/models/trace/handlers/PageLoadMetricsHandler.js').MetricName} MetricName */
4
+ /** @typedef {import('@paulirish/trace_engine/models/trace/handlers/PageLoadMetricsHandler.js').MetricScore} MetricScore */
5
+ /**
6
+ * @param {TraceEngine.Handlers.Types.TraceParseData} traceEngineData
7
+ * @return {Lantern.Simulation.ProcessedNavigation}
8
+ */
9
+ export function createProcessedNavigation(traceEngineData: TraceEngine.Handlers.Types.TraceParseData): Lantern.Simulation.ProcessedNavigation;
10
+ /**
11
+ * @param {LH.Trace} trace
12
+ * @param {TraceEngine.Handlers.Types.TraceParseData} traceEngineData
13
+ * @return {Lantern.NetworkRequest[]}
14
+ */
15
+ export function createNetworkRequests(trace: LH.Trace, traceEngineData: TraceEngine.Handlers.Types.TraceParseData): Lantern.NetworkRequest[];
16
+ /**
17
+ * @param {Lantern.NetworkRequest[]} requests
18
+ * @param {LH.Trace} trace
19
+ * @param {TraceEngine.Handlers.Types.TraceParseData} traceEngineData
20
+ * @param {LH.Artifacts.URL=} URL
21
+ */
22
+ export function createGraph(requests: Lantern.NetworkRequest[], trace: LH.Trace, traceEngineData: TraceEngine.Handlers.Types.TraceParseData, URL?: LH.Artifacts.URL | undefined): import("./page-dependency-graph.js").Node;
23
+ import * as TraceEngine from '@paulirish/trace_engine';
24
+ import * as Lantern from './types/lantern.js';
25
+ //# sourceMappingURL=trace-engine-computation-data.d.ts.map