Skip to content

Commit

Permalink
core(lantern): move types to lib/lantern (#15859)
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark authored Mar 7, 2024
1 parent 045f706 commit 3f7f26f
Show file tree
Hide file tree
Showing 15 changed files with 190 additions and 195 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ module.exports = {
vars: 'all',
args: 'after-used',
argsIgnorePattern: '(^reject$|^_+$)',
varsIgnorePattern: '(^_$|^LH$)',
varsIgnorePattern: '(^_$|^LH$|^Lantern$)',
}],
'no-cond-assign': 2,
'space-infix-ops': 2,
Expand Down
5 changes: 2 additions & 3 deletions core/computed/load-simulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
* SPDX-License-Identifier: Apache-2.0
*/

import * as Lantern from '../lib/lantern/types/lantern.js';
import {makeComputedArtifact} from './computed-artifact.js';
import * as constants from '../config/constants.js';
import {Simulator} from '../lib/lantern/simulator/simulator.js';
import {NetworkAnalysis} from './network-analysis.js';

/** @typedef {import('../../types/internal/lantern.js').Lantern.Simulation.Options} SimulationOptions */

class LoadSimulator {
/**
* @param {{devtoolsLog: LH.DevtoolsLog, settings: LH.Audit.Context['settings']}} data
Expand All @@ -21,7 +20,7 @@ class LoadSimulator {
const {throttlingMethod, throttling, precomputedLanternData} = data.settings;
const networkAnalysis = await NetworkAnalysis.request(data.devtoolsLog, context);

/** @type {SimulationOptions} */
/** @type {Lantern.Simulation.Options} */
const options = {
additionalRttByOrigin: networkAnalysis.additionalRttByOrigin,
serverResponseTimeByOrigin: networkAnalysis.serverResponseTimeByOrigin,
Expand Down
12 changes: 6 additions & 6 deletions core/lib/lantern/metric.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
* SPDX-License-Identifier: Apache-2.0
*/

import * as Lantern from './types/lantern.js';
import {BaseNode} from '../../lib/lantern/base-node.js';
import {NetworkRequest} from '../../lib/network-request.js';

/** @typedef {import('./base-node.js').Node} Node */
/** @typedef {import('./network-node.js').NetworkNode} NetworkNode */
/** @typedef {import('./simulator/simulator.js').Simulator} Simulator */
/** @typedef {import('../../../types/internal/lantern.js').Lantern.Simulation.MetricComputationDataInput} MetricComputationDataInput */

/**
* @typedef Extras
Expand Down Expand Up @@ -43,7 +43,7 @@ class Metric {
}

/**
* @return {LH.Gatherer.Simulation.MetricCoefficients}
* @return {Lantern.Simulation.MetricCoefficients}
*/
static get COEFFICIENTS() {
throw new Error('COEFFICIENTS unimplemented!');
Expand All @@ -56,7 +56,7 @@ class Metric {
* settings change.
*
* @param {number} rttMs
* @return {LH.Gatherer.Simulation.MetricCoefficients}
* @return {Lantern.Simulation.MetricCoefficients}
*/
static getScaledCoefficients(rttMs) { // eslint-disable-line no-unused-vars
return this.COEFFICIENTS;
Expand All @@ -81,16 +81,16 @@ class Metric {
}

/**
* @param {LH.Gatherer.Simulation.Result} simulationResult
* @param {Lantern.Simulation.Result} simulationResult
* @param {Extras} extras
* @return {LH.Gatherer.Simulation.Result}
* @return {Lantern.Simulation.Result}
*/
static getEstimateFromSimulation(simulationResult, extras) { // eslint-disable-line no-unused-vars
return simulationResult;
}

/**
* @param {MetricComputationDataInput} data
* @param {Lantern.Simulation.MetricComputationDataInput} data
* @param {Omit<Extras, 'optimistic'>=} extras
* @return {Promise<LH.Artifacts.LanternMetric>}
*/
Expand Down
7 changes: 3 additions & 4 deletions core/lib/lantern/network-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

/** @template T @typedef {import('../../../types/internal/lantern.js').Lantern.NetworkRequest<T>} NetworkRequest */

import * as Lantern from './types/lantern.js';
import {NetworkRequestTypes} from './lantern.js';
import {BaseNode} from './base-node.js';
// TODO(15841): bring impl of isNonNetworkRequest inside lantern and remove this.
Expand All @@ -17,7 +16,7 @@ import UrlUtils from '../url-utils.js';
*/
class NetworkNode extends BaseNode {
/**
* @param {NetworkRequest<T>} networkRequest
* @param {Lantern.NetworkRequest<T>} networkRequest
*/
constructor(networkRequest) {
super(networkRequest.requestId);
Expand Down Expand Up @@ -51,7 +50,7 @@ class NetworkNode extends BaseNode {
}

/**
* @return {NetworkRequest<T>}
* @return {Lantern.NetworkRequest<T>}
*/
get request() {
return this._request;
Expand Down
8 changes: 4 additions & 4 deletions core/lib/lantern/page-dependency-graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
* SPDX-License-Identifier: Apache-2.0
*/

import * as Lantern from './types/lantern.js';
import {NetworkRequestTypes} from './lantern.js';
import {NetworkNode} from './network-node.js';
import {CPUNode} from './cpu-node.js';
import {TraceProcessor} from '../tracehouse/trace-processor.js';
import {NetworkAnalyzer} from './simulator/network-analyzer.js';

/** @typedef {import('../../../types/internal/lantern.js').Lantern.NetworkRequest} NetworkRequest */
/** @typedef {import('./base-node.js').Node} Node */
/** @typedef {Omit<LH.Artifacts['URL'], 'finalDisplayedUrl'>} URLArtifact */

Expand All @@ -31,7 +31,7 @@ const IGNORED_MIME_TYPES_REGEX = /^video/;

class PageDependencyGraph {
/**
* @param {NetworkRequest} record
* @param {Lantern.NetworkRequest} record
* @return {Array<string>}
*/
static getNetworkInitiators(record) {
Expand Down Expand Up @@ -60,7 +60,7 @@ class PageDependencyGraph {
}

/**
* @param {Array<NetworkRequest>} networkRecords
* @param {Array<Lantern.NetworkRequest>} networkRecords
* @return {NetworkNodeOutput}
*/
static getNetworkNodeOutput(networkRecords) {
Expand Down Expand Up @@ -394,7 +394,7 @@ class PageDependencyGraph {

/**
* @param {LH.Artifacts.ProcessedTrace} processedTrace
* @param {Array<NetworkRequest>} networkRecords
* @param {Array<Lantern.NetworkRequest>} networkRecords
* @param {URLArtifact} URL
* @return {Node}
*/
Expand Down
16 changes: 7 additions & 9 deletions core/lib/lantern/simulator/connection-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

/** @typedef {import('../../../../types/internal/lantern.js').Lantern.NetworkRequest} NetworkRequest */
/** @typedef {import('../../../../types/internal/lantern.js').Lantern.Simulation.Options} SimulationOptions */

import * as Lantern from '../types/lantern.js';
import {NetworkAnalyzer} from './network-analyzer.js';
import {TcpConnection} from './tcp-connection.js';

Expand All @@ -19,16 +17,16 @@ const CONNECTIONS_PER_ORIGIN = 6;

export class ConnectionPool {
/**
* @param {NetworkRequest[]} records
* @param {Required<SimulationOptions>} options
* @param {Lantern.NetworkRequest[]} records
* @param {Required<Lantern.Simulation.Options>} options
*/
constructor(records, options) {
this._options = options;

this._records = records;
/** @type {Map<string, TcpConnection[]>} */
this._connectionsByOrigin = new Map();
/** @type {Map<NetworkRequest, TcpConnection>} */
/** @type {Map<Lantern.NetworkRequest, TcpConnection>} */
this._connectionsByRecord = new Map();
this._connectionsInUse = new Set();
this._connectionReusedByRequestId = NetworkAnalyzer.estimateIfConnectionWasReused(records, {
Expand Down Expand Up @@ -126,7 +124,7 @@ export class ConnectionPool {
* If ignoreConnectionReused is true, acquire will consider all connections not in use as available.
* Otherwise, only connections that have matching "warmth" are considered available.
*
* @param {NetworkRequest} record
* @param {Lantern.NetworkRequest} record
* @param {{ignoreConnectionReused?: boolean}} options
* @return {?TcpConnection}
*/
Expand All @@ -152,7 +150,7 @@ export class ConnectionPool {
* Return the connection currently being used to fetch a record. If no connection
* currently being used for this record, an error will be thrown.
*
* @param {NetworkRequest} record
* @param {Lantern.NetworkRequest} record
* @return {TcpConnection}
*/
acquireActiveConnectionFromRecord(record) {
Expand All @@ -163,7 +161,7 @@ export class ConnectionPool {
}

/**
* @param {NetworkRequest} record
* @param {Lantern.NetworkRequest} record
*/
release(record) {
const connection = this._connectionsByRecord.get(record);
Expand Down
6 changes: 3 additions & 3 deletions core/lib/lantern/simulator/dns-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

/** @typedef {import('../../../../types/internal/lantern.js').Lantern.NetworkRequest} NetworkRequest */
import * as Lantern from '../types/lantern.js';

// A DNS lookup will usually take ~1-2 roundtrips of connection latency plus the extra DNS routing time.
// Example: https://www.webpagetest.org/result/180703_3A_e33ec79747c002ed4d7bcbfc81462203/1/details/#waterfall_view_step1
Expand All @@ -25,7 +25,7 @@ class DNSCache {
}

/**
* @param {NetworkRequest} request
* @param {Lantern.NetworkRequest} request
* @param {{requestedAt: number, shouldUpdateCache: boolean}=} options
* @return {number}
*/
Expand All @@ -47,7 +47,7 @@ class DNSCache {
}

/**
* @param {NetworkRequest} request
* @param {Lantern.NetworkRequest} request
* @param {number} resolvedAt
*/
_updateCacheResolvedAtIfNeeded(request, resolvedAt) {
Expand Down
29 changes: 14 additions & 15 deletions core/lib/lantern/simulator/network-analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

/** @typedef {import('../../../../types/internal/lantern.js').Lantern.NetworkRequest} NetworkRequest */

import * as Lantern from '../types/lantern.js';
import UrlUtils from '../../url-utils.js';

const INITIAL_CWD = 14 * 1024;
Expand Down Expand Up @@ -33,8 +32,8 @@ class NetworkAnalyzer {
}

/**
* @param {NetworkRequest[]} records
* @return {Map<string, NetworkRequest[]>}
* @param {Lantern.NetworkRequest[]} records
* @return {Map<string, Lantern.NetworkRequest[]>}
*/
static groupByOrigin(records) {
const grouped = new Map();
Expand Down Expand Up @@ -89,10 +88,10 @@ class NetworkAnalyzer {
return summaryByKey;
}

/** @typedef {{record: NetworkRequest, timing: LH.Crdp.Network.ResourceTiming, connectionReused?: boolean}} RequestInfo */
/** @typedef {{record: Lantern.NetworkRequest, timing: LH.Crdp.Network.ResourceTiming, connectionReused?: boolean}} RequestInfo */

/**
* @param {NetworkRequest[]} records
* @param {Lantern.NetworkRequest[]} records
* @param {(e: RequestInfo) => number | number[] | undefined} iteratee
* @return {Map<string, number[]>}
*/
Expand Down Expand Up @@ -251,7 +250,7 @@ class NetworkAnalyzer {
/**
* Given the RTT to each origin, estimates the observed server response times.
*
* @param {NetworkRequest[]} records
* @param {Lantern.NetworkRequest[]} records
* @param {Map<string, number>} rttByOrigin
* @return {Map<string, number[]>}
*/
Expand All @@ -272,7 +271,7 @@ class NetworkAnalyzer {
}

/**
* @param {NetworkRequest[]} records
* @param {Lantern.NetworkRequest[]} records
* @return {boolean}
*/
static canTrustConnectionInformation(records) {
Expand All @@ -292,7 +291,7 @@ class NetworkAnalyzer {
* Returns a map of requestId -> connectionReused, estimating the information if the information
* available in the records themselves appears untrustworthy.
*
* @param {NetworkRequest[]} records
* @param {Lantern.NetworkRequest[]} records
* @param {{forceCoarseEstimates: boolean}} [options]
* @return {Map<string, boolean>}
*/
Expand Down Expand Up @@ -336,7 +335,7 @@ class NetworkAnalyzer {
* Attempts to use the most accurate information first and falls back to coarser estimates when it
* is unavailable.
*
* @param {NetworkRequest[]} records
* @param {Lantern.NetworkRequest[]} records
* @param {RTTEstimateOptions} [options]
* @return {Map<string, Summary>}
*/
Expand Down Expand Up @@ -417,7 +416,7 @@ class NetworkAnalyzer {
* Estimates the server response time of each origin. RTT times can be passed in or will be
* estimated automatically if not provided.
*
* @param {NetworkRequest[]} records
* @param {Lantern.NetworkRequest[]} records
* @param {RTTEstimateOptions & {rttByOrigin?: Map<string, number>}} [options]
* @return {Map<string, Summary>}
*/
Expand All @@ -443,7 +442,7 @@ class NetworkAnalyzer {
* Excludes data URI, failed or otherwise incomplete, and cached requests.
* Returns Infinity if there were no analyzable network records.
*
* @param {Array<NetworkRequest>} networkRecords
* @param {Array<Lantern.NetworkRequest>} networkRecords
* @return {number}
*/
static estimateThroughput(networkRecords) {
Expand Down Expand Up @@ -496,7 +495,7 @@ class NetworkAnalyzer {
}

/**
* @template {NetworkRequest} T
* @template {Lantern.NetworkRequest} T
* @param {Array<T>} records
* @param {string} resourceUrl
* @return {T|undefined}
Expand All @@ -510,7 +509,7 @@ class NetworkAnalyzer {
}

/**
* @template {NetworkRequest} T
* @template {Lantern.NetworkRequest} T
* @param {Array<T>} records
* @param {string} resourceUrl
* @return {T|undefined}
Expand All @@ -530,7 +529,7 @@ class NetworkAnalyzer {
* Resolves redirect chain given a main document.
* See: {@link NetworkAnalyzer.findLastDocumentForUrl}) for how to retrieve main document.
*
* @template {NetworkRequest} T
* @template {Lantern.NetworkRequest} T
* @param {T} request
* @return {T}
*/
Expand Down
Loading

0 comments on commit 3f7f26f

Please sign in to comment.