Skip to content

Commit e5dfcb2

Browse files
authored
core: merge api.js into index.js, new report generator api (#14531)
1 parent 1b843de commit e5dfcb2

File tree

10 files changed

+133
-103
lines changed

10 files changed

+133
-103
lines changed

clients/devtools/devtools-entry.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import {Buffer} from 'buffer';
1010

1111
import log from 'lighthouse-logger';
1212

13-
import lighthouse, {legacyNavigation} from '../../core/index.js';
14-
import {navigation, startTimespan, snapshot} from '../../core/api.js';
13+
import lighthouse, {legacyNavigation, navigation, startTimespan, snapshot} from '../../core/index.js';
1514
import {RawConnection} from '../../core/legacy/gather/connections/raw.js';
1615
import {lookupLocale} from '../../core/lib/i18n/i18n.js';
1716
import {registerLocaleData, getCanonicalLocales} from '../../shared/localization/format.js';

core/api.js

Lines changed: 0 additions & 80 deletions
This file was deleted.

core/index.js

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ import {Runner} from './runner.js';
1010
import {CriConnection} from './legacy/gather/connections/cri.js';
1111
import {Config} from './legacy/config/config.js';
1212
import UrlUtils from './lib/url-utils.js';
13-
import * as fraggleRock from './api.js';
1413
import {Driver} from './legacy/gather/driver.js';
1514
import {initializeConfig} from './config/config.js';
15+
import {UserFlow, auditGatherSteps} from './user-flow.js';
16+
import {ReportGenerator} from '../report/generator/report-generator.js';
17+
import {startTimespanGather} from './gather/timespan-runner.js';
18+
import {snapshotGather} from './gather/snapshot-runner.js';
19+
import {navigationGather} from './gather/navigation-runner.js';
1620

1721
/** @typedef {import('./legacy/gather/connections/connection.js').Connection} Connection */
1822

@@ -40,7 +44,7 @@ import {initializeConfig} from './config/config.js';
4044
* @return {Promise<LH.RunnerResult|undefined>}
4145
*/
4246
async function lighthouse(url, flags = {}, configJSON, page) {
43-
return fraggleRock.navigation(page, url, {config: configJSON, flags});
47+
return navigation(page, url, {config: configJSON, flags});
4448
}
4549

4650
/**
@@ -73,6 +77,75 @@ async function legacyNavigation(url, flags = {}, configJSON, userConnection) {
7377
return Runner.audit(artifacts, options);
7478
}
7579

80+
/**
81+
* @param {LH.Puppeteer.Page} page
82+
* @param {LH.UserFlow.Options} [options]
83+
*/
84+
async function startFlow(page, options) {
85+
return new UserFlow(page, options);
86+
}
87+
88+
/**
89+
* @param {LH.Puppeteer.Page|undefined} page
90+
* @param {LH.NavigationRequestor|undefined} requestor
91+
* @param {{config?: LH.Config.Json, flags?: LH.Flags}} [options]
92+
* @return {Promise<LH.RunnerResult|undefined>}
93+
*/
94+
async function navigation(page, requestor, options) {
95+
const gatherResult = await navigationGather(page, requestor, options);
96+
return Runner.audit(gatherResult.artifacts, gatherResult.runnerOptions);
97+
}
98+
99+
/**
100+
* @param {LH.Puppeteer.Page} page
101+
* @param {{config?: LH.Config.Json, flags?: LH.Flags}} [options]
102+
* @return {Promise<LH.RunnerResult|undefined>}
103+
*/
104+
async function snapshot(page, options) {
105+
const gatherResult = await snapshotGather(page, options);
106+
return Runner.audit(gatherResult.artifacts, gatherResult.runnerOptions);
107+
}
108+
109+
/**
110+
* @param {LH.Puppeteer.Page} page
111+
* @param {{config?: LH.Config.Json, flags?: LH.Flags}} [options]
112+
* @return {Promise<{endTimespan: () => Promise<LH.RunnerResult|undefined>}>}
113+
*/
114+
async function startTimespan(page, options) {
115+
const {endTimespanGather} = await startTimespanGather(page, options);
116+
const endTimespan = async () => {
117+
const gatherResult = await endTimespanGather();
118+
return Runner.audit(gatherResult.artifacts, gatherResult.runnerOptions);
119+
};
120+
return {endTimespan};
121+
}
122+
123+
/**
124+
* @template {LH.Result|LH.FlowResult} R
125+
* @param {R} result
126+
* @param {[R] extends [LH.Result] ? LH.OutputMode : Exclude<LH.OutputMode, 'csv'>} [format]
127+
* @return {string}
128+
*/
129+
function generateReport(result, format = 'html') {
130+
const reportOutput = ReportGenerator.generateReport(result, format);
131+
if (Array.isArray(reportOutput)) {
132+
// In theory the output should never be an array.
133+
// This is mostly for type checking.
134+
return reportOutput[0];
135+
} else {
136+
return reportOutput;
137+
}
138+
}
139+
140+
/**
141+
* @param {LH.UserFlow.FlowArtifacts} flowArtifacts
142+
* @param {LH.Config.Json} [config]
143+
*/
144+
async function auditFlowArtifacts(flowArtifacts, config) {
145+
const {gatherSteps, name} = flowArtifacts;
146+
return await auditGatherSteps(gatherSteps, {name, config});
147+
}
148+
76149
/**
77150
* Generate a Lighthouse Config.
78151
* @param {LH.Config.Json=} configJson Configuration for the Lighthouse run. If
@@ -113,6 +186,12 @@ export {default as Gatherer} from './gather/base-gatherer.js';
113186
export {NetworkRecords} from './computed/network-records.js';
114187
export {
115188
legacyNavigation,
189+
startFlow,
190+
navigation,
191+
startTimespan,
192+
snapshot,
193+
generateReport,
194+
auditFlowArtifacts,
116195
generateConfig,
117196
generateLegacyConfig,
118197
getAuditList,

core/scripts/update-flow-fixtures.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import yargs from 'yargs';
1616
import {getChromePath} from 'chrome-launcher';
1717

1818
import {LH_ROOT} from '../../root.js';
19-
import * as api from '../api.js';
19+
import * as api from '../index.js';
2020
import * as assetSaver from '../lib/asset-saver.js';
2121

2222
const ARTIFACTS_PATH =
@@ -153,7 +153,7 @@ async function generateFlowResult() {
153153
fs.writeFileSync(args.outputPath, JSON.stringify(flowResult, null, 2));
154154

155155
if (args.view) {
156-
const htmlReport = await api.generateFlowReport(flowResult);
156+
const htmlReport = await api.generateReport(flowResult);
157157
fs.writeFileSync(FLOW_REPORT_PATH, htmlReport);
158158
open(FLOW_REPORT_PATH);
159159
}

core/test/scenarios/api-test-pptr.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import jestMock from 'jest-mock';
88

9-
import * as lighthouse from '../../api.js';
9+
import * as api from '../../index.js';
1010
import {createTestState, getAuditsBreakdown} from './pptr-test-utils.js';
1111
import {LH_ROOT} from '../../../root.js';
1212

@@ -36,7 +36,7 @@ describe('Fraggle Rock API', function() {
3636
it('should compute accessibility results on the page as-is', async () => {
3737
await setupTestPage();
3838

39-
const result = await lighthouse.snapshot(state.page);
39+
const result = await api.snapshot(state.page);
4040
if (!result) throw new Error('Lighthouse failed to produce a result');
4141

4242
const {lhr, artifacts} = result;
@@ -63,7 +63,7 @@ describe('Fraggle Rock API', function() {
6363
});
6464

6565
it('should compute ConsoleMessage results across a span of time', async () => {
66-
const run = await lighthouse.startTimespan(state.page);
66+
const run = await api.startTimespan(state.page);
6767

6868
await setupTestPage();
6969

@@ -120,7 +120,7 @@ describe('Fraggle Rock API', function() {
120120
await page.goto(`${serverBaseUrl}/onclick.html`);
121121
await page.waitForSelector('button');
122122

123-
const run = await lighthouse.startTimespan(state.page);
123+
const run = await api.startTimespan(state.page);
124124

125125
await page.click('button');
126126
await page.waitForSelector('input');
@@ -155,7 +155,7 @@ describe('Fraggle Rock API', function() {
155155
it('should compute both snapshot & timespan results', async () => {
156156
const {page, serverBaseUrl} = state;
157157
const url = `${serverBaseUrl}/index.html`;
158-
const result = await lighthouse.navigation(page, url);
158+
const result = await api.navigation(page, url);
159159
if (!result) throw new Error('Lighthouse failed to produce a result');
160160

161161
const {lhr, artifacts} = result;
@@ -194,7 +194,7 @@ describe('Fraggle Rock API', function() {
194194
await page.click('a');
195195
});
196196

197-
const result = await lighthouse.navigation(page, requestor);
197+
const result = await api.navigation(page, requestor);
198198
if (!result) throw new Error('Lighthouse failed to produce a result');
199199

200200
expect(requestor).toHaveBeenCalled();

core/test/scenarios/cross-origin-test-pptr.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
55
*/
66

7-
import * as lighthouse from '../../api.js';
7+
import * as api from '../../index.js';
88
import {createTestState} from './pptr-test-utils.js';
99
import {LH_ROOT} from '../../../root.js';
1010

@@ -26,7 +26,7 @@ describe('Cross origin timespan', function() {
2626
it('should resolve all stylesheets', async () => {
2727
await state.page.goto(`${state.serverBaseUrl}/start.html`, {waitUntil: ['networkidle0']});
2828

29-
const timespan = await lighthouse.startTimespan(state.page);
29+
const timespan = await api.startTimespan(state.page);
3030
await state.page.goto(`${state.secondaryServerBaseUrl}/end.html`);
3131
const result = await timespan.endTimespan();
3232
if (!result) throw new Error('Lighthouse did not return a result');

core/test/scenarios/disconnect-test-pptr.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
/* eslint-env browser */
88

9-
import * as lighthouse from '../../api.js';
9+
import * as api from '../../index.js';
1010
import {createTestState} from './pptr-test-utils.js';
1111
import {LH_ROOT} from '../../../root.js';
1212

@@ -29,7 +29,7 @@ describe('Disconnect', function() {
2929
const session = await state.page.target().createCDPSession();
3030
await session.send('Network.enable');
3131

32-
const timespan = await lighthouse.startTimespan(state.page, {
32+
const timespan = await api.startTimespan(state.page, {
3333
flags: {
3434
blockedUrlPatterns: ['*'],
3535
},

core/test/scenarios/start-end-navigation-test-pptr.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
55
*/
66

7-
import * as lighthouse from '../../api.js';
7+
import * as api from '../../index.js';
88
import {createTestState, getAuditsBreakdown} from './pptr-test-utils.js';
99
import {LH_ROOT} from '../../../root.js';
1010

@@ -26,7 +26,7 @@ describe('Start/End navigation', function() {
2626
const pageUrl = `${state.serverBaseUrl}/links-to-index.html`;
2727
await state.page.goto(pageUrl, {waitUntil: ['networkidle0']});
2828

29-
const flow = await lighthouse.startFlow(state.page);
29+
const flow = await api.startFlow(state.page);
3030

3131
await flow.startNavigation();
3232
await state.page.click('a');

report/generator/report-generator.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,28 +146,42 @@ class ReportGenerator {
146146
.join(CRLF);
147147
}
148148

149+
/**
150+
* @param {LHResult|FlowResult} result
151+
* @return {result is FlowResult}
152+
*/
153+
static isFlowResult(result) {
154+
return 'steps' in result;
155+
}
156+
149157
/**
150158
* Creates the results output in a format based on the `mode`.
151-
* @param {LHResult} lhr
159+
* @param {LHResult|FlowResult} result
152160
* @param {LHResult['configSettings']['output']} outputModes
153161
* @return {string|string[]}
154162
*/
155-
static generateReport(lhr, outputModes) {
163+
static generateReport(result, outputModes) {
156164
const outputAsArray = Array.isArray(outputModes);
157165
if (typeof outputModes === 'string') outputModes = [outputModes];
158166

159167
const output = outputModes.map(outputMode => {
160168
// HTML report.
161169
if (outputMode === 'html') {
162-
return ReportGenerator.generateReportHtml(lhr);
170+
if (ReportGenerator.isFlowResult(result)) {
171+
return ReportGenerator.generateFlowReportHtml(result);
172+
}
173+
return ReportGenerator.generateReportHtml(result);
163174
}
164175
// CSV report.
165176
if (outputMode === 'csv') {
166-
return ReportGenerator.generateReportCSV(lhr);
177+
if (ReportGenerator.isFlowResult(result)) {
178+
throw new Error('CSV output is not support for user flows');
179+
}
180+
return ReportGenerator.generateReportCSV(result);
167181
}
168182
// JSON report.
169183
if (outputMode === 'json') {
170-
return JSON.stringify(lhr, null, 2);
184+
return JSON.stringify(result, null, 2);
171185
}
172186

173187
throw new Error('Invalid output mode: ' + outputMode);

0 commit comments

Comments
 (0)