Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
prepare() {
const platform = getPlatform() as SupportedPlatforms;
if (!_SUPPORTED_PLATFORMS.has(platform)) {
throw new UnsupportedPlatformError();
}
this.userDataDir = this.userDataDir || this.makeTmpDir();
this.outFile = this.fs.openSync(`${this.userDataDir}/chrome-out.log`, 'a');
this.errFile = this.fs.openSync(`${this.userDataDir}/chrome-err.log`, 'a');
// fix for Node4
// you can't pass a fd to fs.writeFileSync
this.pidFile = `${this.userDataDir}/chrome.pid`;
log.verbose('ChromeLauncher', `created ${this.userDataDir}`);
this.tmpDirandPidFileReady = true;
}
prepare() {
const platform = utils_1.getPlatform();
if (!_SUPPORTED_PLATFORMS.has(platform)) {
throw new utils_1.UnsupportedPlatformError();
}
this.userDataDir = this.userDataDir || this.makeTmpDir();
this.outFile = this.fs.openSync(`${this.userDataDir}/chrome-out.log`, 'a');
this.errFile = this.fs.openSync(`${this.userDataDir}/chrome-err.log`, 'a');
// fix for Node4
// you can't pass a fd to fs.writeFileSync
this.pidFile = `${this.userDataDir}/chrome.pid`;
log.verbose('ChromeLauncher', `created ${this.userDataDir}`);
this.tmpDirandPidFileReady = true;
}
launch() {
let firstMeaningfulPaint = frameEvents.find(
e => e.name === 'firstMeaningfulPaint' && e.ts > navigationStart.ts
);
let fmpFellBack = false;
// If there was no firstMeaningfulPaint event found in the trace, the network idle detection
// may have not been triggered before Lighthouse finished tracing.
// In this case, we'll use the last firstMeaningfulPaintCandidate we can find.
// However, if no candidates were found (a bogus trace, likely), we fail.
if (!firstMeaningfulPaint) {
const fmpCand = 'firstMeaningfulPaintCandidate';
fmpFellBack = true;
log.verbose('trace-of-tab', `No firstMeaningfulPaint found, falling back to last ${fmpCand}`);
const lastCandidate = frameEvents.filter(e => e.name === fmpCand).pop();
if (!lastCandidate) {
log.verbose('trace-of-tab', 'No `firstMeaningfulPaintCandidate` events found in trace');
}
firstMeaningfulPaint = lastCandidate;
}
// LCP's trace event was first introduced in m78. We can't surface an LCP for older Chrome versions
// LCP comes from the latest `largestContentfulPaint::Candidate`, but it can be invalidated
// by a `largestContentfulPaint::Invalidate` event. In the case that the last candidate is
// invalidated, the value will be undefined.
let largestContentfulPaint;
let lcpInvalidated = false;
// Iterate the events backwards.
for (let i = frameEvents.length - 1; i >= 0; i--) {
const e = frameEvents[i];
// If the event's timestamp is before the navigation start, stop.
if (e.ts <= navigationStart.ts) break;
// If the last lcp event in the trace is 'Invalidate', there is inconclusive data to determine LCP.
let firstMeaningfulPaint = frameEvents.find(
e => e.name === 'firstMeaningfulPaint' && e.ts > navigationStart.ts
);
let fmpFellBack = false;
// If there was no firstMeaningfulPaint event found in the trace, the network idle detection
// may have not been triggered before Lighthouse finished tracing.
// In this case, we'll use the last firstMeaningfulPaintCandidate we can find.
// However, if no candidates were found (a bogus trace, likely), we fail.
if (!firstMeaningfulPaint) {
const fmpCand = 'firstMeaningfulPaintCandidate';
fmpFellBack = true;
log.verbose('trace-of-tab', `No firstMeaningfulPaint found, falling back to last ${fmpCand}`);
const lastCandidate = frameEvents.filter(e => e.name === fmpCand).pop();
if (!lastCandidate) {
log.verbose('trace-of-tab', 'No `firstMeaningfulPaintCandidate` events found in trace');
}
firstMeaningfulPaint = lastCandidate;
}
// LCP comes from the latest `largestContentfulPaint::Candidate`, but it can be invalidated
// by a `largestContentfulPaint::Invalidate` event. In the case that the last candidate is
// invalidated, the value will be undefined.
let largestContentfulPaint;
for (let i = frameEvents.length - 1; i >= 0; i--) {
const e = frameEvents[i];
if (e.ts <= navigationStart.ts) break;
if (e.name === 'largestContentfulPaint::Invalidate') break;
if (e.name !== 'largestContentfulPaint::Candidate') continue;
largestContentfulPaint = e;
break;
}
_shouldToggleDomain(domain, sessionId, enable) {
const key = domain + (sessionId || '');
const enabledCount = this._domainEnabledCounts.get(key) || 0;
const newCount = enabledCount + (enable ? 1 : -1);
this._domainEnabledCounts.set(key, Math.max(0, newCount));
// Switching to enabled or disabled, respectively.
if ((enable && newCount === 1) || (!enable && newCount === 0)) {
log.verbose('Driver', `${domain}.${enable ? 'enable' : 'disable'}`);
return true;
} else {
if (newCount < 0) {
log.error('Driver', `Attempted to disable domain '${domain}' when already disabled.`);
}
return false;
}
}
_emitNetworkStatus() {
const zeroQuiet = this.isIdle();
const twoQuiet = this.is2Idle();
if (twoQuiet && zeroQuiet) {
log.verbose('NetworkRecorder', 'network fully-quiet');
this.emit('network-2-idle');
this.emit('networkidle');
} else if (twoQuiet && !zeroQuiet) {
log.verbose('NetworkRecorder', 'network semi-quiet');
this.emit('network-2-idle');
this.emit('networkbusy');
} else {
log.verbose('NetworkRecorder', 'network busy');
this.emit('network-2-busy');
this.emit('networkbusy');
}
}
_emitNetworkStatus() {
const zeroQuiet = this._getActiveIdlePeriod(0);
const twoQuiet = this._getActiveIdlePeriod(2);
if (twoQuiet && zeroQuiet) {
log.verbose('NetworkRecorder', 'network fully-quiet');
this.emit('network-2-idle');
this.emit('networkidle');
} else if (twoQuiet && !zeroQuiet) {
log.verbose('NetworkRecorder', 'network semi-quiet');
this.emit('network-2-idle');
this.emit('networkbusy');
} else {
log.verbose('NetworkRecorder', 'network busy');
this.emit('network-2-busy');
this.emit('networkbusy');
}
}
_emitNetworkStatus() {
const zeroQuiet = this._getActiveIdlePeriod(0);
const twoQuiet = this._getActiveIdlePeriod(2);
if (twoQuiet && zeroQuiet) {
log.verbose('NetworkRecorder', 'network fully-quiet');
this.emit('network-2-idle');
this.emit('networkidle');
} else if (twoQuiet && !zeroQuiet) {
log.verbose('NetworkRecorder', 'network semi-quiet');
this.emit('network-2-idle');
this.emit('networkbusy');
} else {
log.verbose('NetworkRecorder', 'network busy');
this.emit('network-2-busy');
this.emit('networkbusy');
}
}
_emitNetworkStatus() {
const zeroQuiet = this.isIdle();
const twoQuiet = this.is2Idle();
if (twoQuiet && zeroQuiet) {
log.verbose('NetworkRecorder', 'network fully-quiet');
this.emit('network-2-idle');
this.emit('networkidle');
} else if (twoQuiet && !zeroQuiet) {
log.verbose('NetworkRecorder', 'network semi-quiet');
this.emit('network-2-idle');
this.emit('networkbusy');
} else {
log.verbose('NetworkRecorder', 'network busy');
this.emit('network-2-busy');
this.emit('networkbusy');
}
}