/** * @license * Copyright 2018 Google LLC * SPDX-License-Identifier: Apache-2.0 */ import {Protocol as Crdp} from 'devtools-protocol/types/protocol.js'; import {ProtocolMapping as CrdpMappings} from 'devtools-protocol/types/protocol-mapping.js'; import {ExecutionContext} from '../core/gather/driver/execution-context.js'; import {NetworkMonitor} from '../core/gather/driver/network-monitor.js'; import {Fetcher} from '../core/gather/fetcher.js'; import {ArbitraryEqualityMap} from '../core/lib/arbitrary-equality-map.js'; import {Artifacts, BaseArtifacts, GathererArtifacts, DevtoolsLog, Trace} from './artifacts.js'; import Config from './config.js'; import Result from './lhr/lhr.js'; import Protocol from './protocol.js'; import Puppeteer from './puppeteer.js'; import * as Lantern from '../core/lib/lantern/lantern.js'; type CrdpEvents = CrdpMappings.Events; type CrdpCommands = CrdpMappings.Commands; declare module Gatherer { /** The Lighthouse wrapper around a raw CDP session. */ interface ProtocolSession { setTargetInfo(targetInfo: Crdp.Target.TargetInfo): void; hasNextProtocolTimeout(): boolean; getNextProtocolTimeout(): number; setNextProtocolTimeout(ms: number): void; on(event: TEvent, callback: (...args: CrdpEvents[TEvent]) => void): void; once(event: TEvent, callback: (...args: CrdpEvents[TEvent]) => void): void; off(event: TEvent, callback: (...args: CrdpEvents[TEvent]) => void): void; sendCommand(method: TMethod, ...params: CrdpCommands[TMethod]['paramsType']): Promise; sendCommandAndIgnore(method: TMethod, ...params: CrdpCommands[TMethod]['paramsType']): Promise; dispose(): Promise; onCrashPromise(): Promise; } interface Driver { defaultSession: ProtocolSession; executionContext: ExecutionContext; fetcher: Fetcher; url: () => Promise; targetManager: { rootSession(): ProtocolSession; mainFrameExecutionContexts(): Array; on(event: 'protocolevent', callback: (payload: Protocol.RawEventMessage) => void): void off(event: 'protocolevent', callback: (payload: Protocol.RawEventMessage) => void): void }; networkMonitor: NetworkMonitor; } interface Context { /** The gather mode Lighthouse is currently in. */ gatherMode: GatherMode; /** The connection to the page being analyzed. */ driver: Driver; /** The Puppeteer page handle. */ page: Puppeteer.Page; /** The set of base artifacts that are always collected. */ baseArtifacts: BaseArtifacts; /** The cached results of computed artifacts. */ computedCache: Map; /** The set of available dependencies requested by the current gatherer. */ dependencies: Pick>; /** The settings used for gathering. */ settings: Config.Settings; } interface GatherResult { artifacts: Artifacts; runnerOptions: { resolvedConfig: Config.ResolvedConfig; computedCache: Map } } interface LoadData { networkRecords: Array; devtoolsLog: DevtoolsLog; trace?: Trace; } type PhaseResultNonPromise = void | GathererArtifacts[keyof GathererArtifacts]; type PhaseResult = PhaseResultNonPromise | Promise type GatherMode = Result.GatherMode; type DefaultDependenciesKey = '__none__' type DependencyKey = keyof GathererArtifacts | DefaultDependenciesKey interface GathererMetaNoDependencies { /** * Used to validate the dependency requirements of gatherers. * If this property is not defined, this gatherer cannot be the dependency of another. */ symbol?: Symbol; /** Lists the modes in which this gatherer can run. */ supportedModes: Array; } interface GathererMetaWithDependencies< TDependencies extends Exclude > extends GathererMetaNoDependencies { /** * The set of required dependencies that this gatherer needs before it can compute its results. */ dependencies: Record; } type GathererMeta = [TDependencies] extends [DefaultDependenciesKey] ? GathererMetaNoDependencies : GathererMetaWithDependencies>; type GatherPhase = keyof Omit interface GathererInstance { meta: GathererMeta; startInstrumentation(context: Context): Promise|void; startSensitiveInstrumentation(context: Context): Promise|void; stopSensitiveInstrumentation(context: Context): Promise|void; stopInstrumentation(context: Context): Promise|void; getArtifact(context: Context): PhaseResult; } type GathererInstanceExpander = // Lack of brackets intentional here to convert to the union of all individual dependencies. TDependencies extends Gatherer.DefaultDependenciesKey ? GathererInstance : GathererInstance> type AnyGathererInstance = GathererInstanceExpander namespace Simulation { type GraphNode = Lantern.Graph.Node; type GraphNetworkNode = Lantern.Graph.NetworkNode; type GraphCPUNode = Lantern.Graph.CPUNode; type Simulator = Lantern.Simulation.Simulator; type NodeTiming = Lantern.Types.Simulation.NodeTiming; type Result = Lantern.Simulation.Result; } } export default Gatherer;