lighthouse-core/lib/network-recorder.js
/**
   * Construct network records from a log of devtools protocol messages.
   * @param {LH.DevtoolsLog} devtoolsLog
   * @return {Array<LH.Artifacts.NetworkRequest>}
   */
Construct network records from a log of devtools protocol messages.
@param {LH.DevtoolsLog} devtoolsLog
@return {Array}
Construct network records from a log of devtools protocol messages . @param { LH . DevtoolsLog } devtoolsLog @return { Array }
static recordsFromLogs(devtoolsLog) {
    const networkRecorder = new NetworkRecorder();
    devtoolsLog.forEach(message => networkRecorder.dispatch(message));
    const records = networkRecorder.getRecords().filter(record => record.isValid);
    const recordsByURL = new Map();
    for (const record of records) {
      if (recordsByURL.has(record.url)) continue;
      recordsByURL.set(record.url, record);
    }
    for (const record of records) {
      const stackFrames = (record.initiator.stack && record.initiator.stack.callFrames) || [];
      const initiatorURL = record.initiator.url || (stackFrames[0] && stackFrames[0].url);
      const initiator = record.redirectSource || recordsByURL.get(initiatorURL);
      if (initiator) {
        record.setInitiatorRequest(initiator);
      }
      let finalRecord = record;
      while (finalRecord.redirectDestination) finalRecord = finalRecord.redirectDestination;
      if (finalRecord === record || finalRecord.redirects) continue;
      const redirects = [];
      for (
        let redirect = finalRecord.redirectSource;
        redirect;
        redirect = redirect.redirectSource
      ) {
        redirects.unshift(redirect);
      }
      finalRecord.redirects = redirects;
    }
    return records;
  }
static recordsFromLogs ( devtoolsLog ) { const networkRecorder = new NetworkRecorder ( ) ; devtoolsLog . forEach ( message => networkRecorder . dispatch ( message ) ) ; const records = networkRecorder . getRecords ( ) . filter ( record => record . isValid ) ; const recordsByURL = new Map ( ) ; for ( const record of records ) { if ( recordsByURL . has ( record . url ) ) continue ; recordsByURL . set ( record . url , record ) ; } for ( const record of records ) { const stackFrames = ( record . initiator . stack && record . initiator . stack . callFrames ) || [ ] ; const initiatorURL = record . initiator . url || ( stackFrames [ 0 ] && stackFrames [ 0 ] . url ) ; const initiator = record . redirectSource || recordsByURL . get ( initiatorURL ) ; if ( initiator ) { record . setInitiatorRequest ( initiator ) ; } let finalRecord = record ; while ( finalRecord . redirectDestination ) finalRecord = finalRecord . redirectDestination ; if ( finalRecord === record || finalRecord . redirects ) continue ; const redirects = [ ] ; for ( let redirect = finalRecord . redirectSource ; redirect ; redirect = redirect . redirectSource ) { redirects . unshift ( redirect ) ; } finalRecord . redirects = redirects ; } return records ; }
Construct network records from a log of devtools protocol messages.
Construct network records from a log of devtools protocol messages .
// playback all the devtools messages to recreate network records // get out the list of records & filter out invalid records // create a map of all the records by URL to link up initiator // set the initiator and redirects array // If we were redirected to this request, our initiator is that redirect, otherwise, it's the // initiator provided by the protocol. See https://github.com/GoogleChrome/lighthouse/pull/7352/