All files / Scripts/table Received.ts

100% Statements 131/131
88.57% Branches 62/70
100% Functions 16/16
100% Lines 125/125

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 2554x   4x 4x     4x   4x 29x 29x 29x 29x             67x   67x   64x     64x     64x       64x   7x 7x     64x   5x 5x     64x 61x 61x 61x   61x 61x 61x             64x 64x 1x 1x         64x 64x 1x 1x       64x     64x     64x 832x 7787x 124x                       80x   64x   124x 80x   44x     124x       67x     29x     20x 1x   19x 19x     20x 1x     20x 14x 14x 10x 10x 8x       27x   14x 13x 13x     1x           40x   40x 24x   24x   24x 5x     19x 4x 4x     19x 13x 13x 13x 2x   11x     13x 13x     19x 8x     19x 14x 14x 14x 5x   9x       19x         16x     16x 16x 16x 16x   16x 26x 26x 26x 8x     26x 26x 26x       16x   16x 26x 26x 26x   26x 10x     10x 8x       26x 26x         16x     247x   14x 13x 13x 13x 14x   13x 13x      
import { ITable } from "./ITable";
import { DateWithNum } from "../DateWithNum";
import { MHADates } from "../MHADates";
import { mhaStrings } from "../mhaStrings";
import { Header } from "../row/Header";
import { Match } from "../row/Match";
import { ReceivedRow } from "../row/ReceivedRow";
 
export class Received extends ITable {
    private receivedRows: ReceivedRow[] = [];
    protected sortColumnInternal = "hop";
    protected sortOrderInternal = 1;
    public readonly tableName: string = "receivedHeaders";
 
    // Builds array of values for each header in receivedHeaderNames.
    // This algorithm should work regardless of the order of the headers, given:
    //  - The date, if present, is always at the end, separated by a ";".
    // Values not attached to a header will not be reflected in output.
    public parseHeader(receivedHeader: string | null): ReceivedRow {
        const receivedFields: ReceivedRow = new ReceivedRow(receivedHeader);
 
        if (receivedHeader) {
            // Strip linefeeds first
            receivedHeader = receivedHeader.replace(/\r|\n|\r\n/g, " ");
 
            // Some bad dates don't wrap UTC in paren - fix that first
            receivedHeader = receivedHeader.replace(/UTC|\(UTC\)/gi, "(UTC)");
 
            // Read out the date first, then clear it from the string
            let iDate = receivedHeader.lastIndexOf(";");
 
            // No semicolon means no date - or maybe there's one there?
            // Sendgrid is bad about this
            if (iDate === -1) {
                // First try to find a day of the week
                receivedHeader = receivedHeader.replace(/\s*(Mon|Tue|Wed|Thu|Fri|Sat|Sun)/g, "; $1");
                iDate = receivedHeader.lastIndexOf(";");
            }
 
            if (iDate === -1) {
                // Next we look for year-month-day, 4-1/2-1/2
                receivedHeader = receivedHeader.replace(/\s*(\d{4}-\d{1,2}-\d{1,2})/g, "; $1");
                iDate = receivedHeader.lastIndexOf(";");
            }
 
            if (iDate !== -1 && receivedHeader.length !== iDate + 1) {
                const dateField = receivedHeader.substring(iDate + 1);
                receivedHeader = receivedHeader.substring(0, iDate);
                const parsedDate: DateWithNum = MHADates.parseDate(dateField);
 
                if (parsedDate) {
                    receivedFields["date"].value = parsedDate.date;
                    receivedFields["dateNum"].value = parsedDate.dateNum;
                }
            }
 
            // Scan for malformed postFix headers
            // Received: by example.com (Postfix, from userid 1001)
            //   id 1234ABCD; Thu, 21 Aug 2014 12:12:48 +0200 (CEST)
            const postFix = receivedHeader.match(/(.*)by (.*? \(Postfix, from userid .*?\))(.*)/mi);
            if (postFix) {
                receivedFields.setField("by", postFix[2] ?? "");
                receivedHeader = `${postFix[1] ?? ""}${postFix[3] ?? ""}`;
            }
 
            // Scan for malformed qmail headers
            // Received: (qmail 10876 invoked from network); 24 Aug 2014 16:13:38 -0000
            const qmail = receivedHeader.match(/(.*)\((qmail .*? invoked from .*?)\)(.*)/mi);
            if (qmail) {
                receivedFields.setField("by", qmail[2] ?? "");
                receivedHeader = `${qmail[1] ?? ""}${qmail[3] ?? ""}`;
            }
 
            // Split up the string now so we can look for our headers
            const tokens = receivedHeader.split(/\s+/);
 
            // Build array of header locations
            const headerMatches: Match[] = [];
 
            let fieldName: string;
            for (fieldName in receivedFields) {
                tokens.some(function (token, iToken) {
                    if (fieldName.toLowerCase() === token.toLowerCase()) {
                        headerMatches.push(<Match>{ fieldName: fieldName, iToken: iToken });
                        // We don't return true so we can match any duplicate headers
                        // In doing this, we risk failing to parse a string where a header
                        // keyword appears as the value for another header
                        // Both situations are invalid input
                        // We're just picking which one we'd prefer to handle
                    }
                });
            }
 
            // Next bit assumes headerMatches[fieldName,iToken] is increasing on iToken.
            // Sort it so it is.
            headerMatches.sort(function (a, b) { return a.iToken - b.iToken; });
 
            headerMatches.forEach(function (headerMatch, iMatch) {
                let iNextTokenHeader;
                if (iMatch + 1 < headerMatches.length) {
                    iNextTokenHeader = headerMatches[iMatch + 1]?.iToken;
                } else {
                    iNextTokenHeader = tokens.length;
                }
 
                receivedFields.setField(headerMatch.fieldName, tokens.slice(headerMatch.iToken + 1, iNextTokenHeader).join(" ").trim());
            });
        }
 
        return receivedFields;
    }
 
    public exists(): boolean { return this.rows.length > 0; }
 
    public doSort(col: string): void {
        if (this.sortColumn === col) {
            this.sortOrderInternal *= -1;
        } else {
            this.sortColumnInternal = col;
            this.sortOrderInternal = 1;
        }
 
        if (this.rows[0] && this.sortColumn + "Sort" in this.rows[0]) {
            col = col + "Sort";
        }
 
        this.rows.sort((a: ReceivedRow, b: ReceivedRow) => {
            const acol = a[col]?.toString();
            if (!acol) return 1;
            const bcol = b[col]?.toString();
            if (!bcol) return -1;
            return this.sortOrder * (acol < bcol ? -1 : 1);
        });
    }
 
    public addInternal(receivedHeader: string): void { this.rows.push(this.parseHeader(receivedHeader)); }
    public add(header: Header): boolean {
        if (header.header.toUpperCase() === "Received".toUpperCase()) {
            this.rows.push(this.parseHeader(header.value));
            return true;
        }
 
        return false;
    }
 
    // Computes min/sec from the diff of current and last.
    // Returns nothing if last or current is NaN.
    public static computeTime(current: number, last: number): string {
        const time: string[] = [];
 
        if (isNaN(current) || isNaN(last)) { return ""; }
        let diff = current - last;
        let iDelay;
        let printedMinutes = false;
 
        if (Math.abs(diff) < 1000) {
            return "0 " + mhaStrings.mhaSeconds;
        }
 
        if (diff < 0) {
            time.push(mhaStrings.mhaNegative);
            diff = -diff;
        }
 
        if (diff >= 1000 * 60) {
            iDelay = Math.floor(diff / 1000 / 60);
            time.push(iDelay.toString(), " ");
            if (iDelay === 1) {
                time.push(mhaStrings.mhaMinute);
            } else {
                time.push(mhaStrings.mhaMinutes);
            }
 
            diff -= iDelay * 1000 * 60;
            printedMinutes = true;
        }
 
        if (printedMinutes && diff) {
            time.push(" ");
        }
 
        if (!printedMinutes || diff) {
            iDelay = Math.floor(diff / 1000);
            time.push(iDelay.toString(), " ");
            if (iDelay === 1) {
                time.push(mhaStrings.mhaSecond);
            } else {
                time.push(mhaStrings.mhaSeconds);
            }
        }
 
        return time.join("");
    }
 
    public computeDeltas(): string {
        // Process received headers in reverse order
        this.rows.reverse();
 
        // Parse rows and compute values needed for the "Delay" column
        let iStartTime = 0;
        let iEndTime = 0;
        let iLastTime = NaN;
        let iDelta = 0; // This will be the sum of our positive deltas
 
        this.rows.forEach(function (row: ReceivedRow) {
            const dateNum: number = typeof row.dateNum.value === "number" ? row.dateNum.value : NaN;
            if (!isNaN(dateNum)) {
                if (!isNaN(iLastTime) && iLastTime < dateNum) {
                    iDelta += dateNum - iLastTime;
                }
 
                iStartTime = iStartTime || dateNum;
                iEndTime = dateNum;
                iLastTime = dateNum;
            }
        });
 
        iLastTime = NaN;
 
        this.rows.forEach(function (row: ReceivedRow, index: number) {
            row.hop.value = index + 1;
            const dateNum: number = typeof row.dateNum.value === "number" ? row.dateNum.value : NaN;
            row.delay.value = Received.computeTime(dateNum, iLastTime);
 
            if (!isNaN(dateNum) && !isNaN(iLastTime) && iDelta !== 0) {
                row.delaySort.value = dateNum - iLastTime;
 
                // Only positive delays will get percentage bars. Negative delays will be color coded at render time.
                if (row.delaySort.value > 0) {
                    row.percent.value = 100 * row.delaySort.value / iDelta;
                }
            }
 
            if (!isNaN(dateNum)) {
                iLastTime = dateNum;
            }
        });
 
        // Total time is still last minus first, even if negative.
        return iEndTime !== iStartTime ? Received.computeTime(iEndTime, iStartTime) : "";
    }
 
    public get rows(): ReceivedRow[] { return this.receivedRows; }
    public toString(): string {
        if (!this.exists()) return "";
        const ret: string[] = ["Received"];
        const rows: ReceivedRow[] = [];
        this.rows.forEach(function (row: ReceivedRow): void {
            rows.push(row);
        });
        if (rows.length) ret.push(rows.join("\n\n"));
        return ret.join("\n");
    }
}