-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
core(tsc): gatherer type-checking cleanup #5019
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,39 +3,39 @@ | |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* 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. | ||
*/ | ||
// @ts-nocheck | ||
'use strict'; | ||
|
||
const Gatherer = require('./gatherer'); | ||
const manifestParser = require('../../lib/manifest-parser'); | ||
const Driver = require('../driver.js'); // eslint-disable-line no-unused-vars | ||
|
||
class StartUrl extends Gatherer { | ||
/** | ||
* Grab the manifest, extract it's start_url, attempt to `fetch()` it while offline | ||
* @param {*} options | ||
* @return {{statusCode: number, debugString?: string}} | ||
* @param {LH.Gatherer.PassContext} passContext | ||
* @return {Promise<LH.Artifacts['StartUrl']>} | ||
*/ | ||
afterPass(options) { | ||
const driver = options.driver; | ||
return driver.goOnline(options) | ||
afterPass(passContext) { | ||
const driver = passContext.driver; | ||
return driver.goOnline(passContext) | ||
.then(() => driver.getAppManifest()) | ||
.then(response => driver.goOffline(options).then(() => response)) | ||
.then(response => response && manifestParser(response.data, response.url, options.url)) | ||
.then(response => driver.goOffline().then(() => response)) | ||
.then(response => response && manifestParser(response.data, response.url, passContext.url)) | ||
.then(manifest => { | ||
const {isReadFailure, reason, startUrl} = this._readManifestStartUrl(manifest); | ||
if (isReadFailure) { | ||
return {statusCode: -1, debugString: reason}; | ||
const startUrlInfo = this._readManifestStartUrl(manifest); | ||
if (startUrlInfo.isReadFailure) { | ||
return {statusCode: -1, debugString: startUrlInfo.reason}; | ||
} | ||
|
||
return this._attemptManifestFetch(options.driver, startUrl); | ||
return this._attemptManifestFetch(passContext.driver, startUrlInfo.startUrl); | ||
}).catch(() => { | ||
return {statusCode: -1, debugString: 'Unable to fetch start URL via service worker'}; | ||
}); | ||
} | ||
|
||
/** | ||
* Read the parsed manifest and return failure reasons or the startUrl | ||
* @param {Manifest} manifest | ||
* @param {?{value?: {start_url: {value?: string, debugString?: string}}, debugString?: string}} manifest | ||
* @return {{isReadFailure: true, reason: string}|{isReadFailure: false, startUrl: string}} | ||
*/ | ||
_readManifestStartUrl(manifest) { | ||
|
@@ -55,15 +55,16 @@ class StartUrl extends Gatherer { | |
return {isReadFailure: true, reason: manifest.value.start_url.debugString}; | ||
} | ||
|
||
// @ts-ignore - TODO(bckenny): should actually be testing value above, not debugString | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a bug we let slip in after the last rewrite. Having a This means we're failing sites that use one of the fallbacks even though it's technically allowed. Above should be checking if there's not a |
||
return {isReadFailure: false, startUrl: manifest.value.start_url.value}; | ||
} | ||
|
||
/** | ||
* Try to `fetch(start_url)`, return true if fetched by SW | ||
* Resolves when we have a matched network request | ||
* @param {!Driver} driver | ||
* @param {!string} startUrl | ||
* @return {Promise<{statusCode: ?number, debugString: ?string}>} | ||
* @param {Driver} driver | ||
* @param {string} startUrl | ||
* @return {Promise<{statusCode: number, debugString: string}>} | ||
*/ | ||
_attemptManifestFetch(driver, startUrl) { | ||
// Wait up to 3s to get a matched network request from the fetch() to work | ||
|
@@ -77,7 +78,9 @@ class StartUrl extends Gatherer { | |
const fetchPromise = new Promise(resolve => { | ||
driver.on('Network.responseReceived', onResponseReceived); | ||
|
||
function onResponseReceived({response}) { | ||
/** @param {LH.Crdp.Network.ResponseReceivedEvent} responseEvent */ | ||
function onResponseReceived(responseEvent) { | ||
const {response} = responseEvent; | ||
// ignore mismatched URLs | ||
if (response.url !== startUrl) return; | ||
driver.off('Network.responseReceived', onResponseReceived); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@paulirish what's the story
.js
or not? :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i prefer without .js because that's what vscode autocompletes to but hey given the inconsistency right now, it doesn't matter until we fix it one way or the other. :)