tinyserve's access log can weigh gigabytes. fs.readFile would load all of it into memory;
a stream reads it in chunks of 64 KB, and
[`readline`](https://nodejs.org/api/readline.html#example-read-file-stream-line-by-line) turns
those chunks into lines. Memory stays flat whatever the size of the file.
Write async summarize(file). Each line looks like:
10.0.0.1 GET /index.html 200 512(ip, method in capitals, path, 3-digit status, bytes sent) — the LINE regex matches exactly
this. Return:
| Field | Meaning |
|---|---|
requests | number of valid lines |
bytes | sum of the bytes column |
byStatus | count per status, keys are the status strings: { '200': 2 } |
topPath | most requested path; on a tie, the smallest in string order; null if none |
malformed | non-blank lines that do not match LINE |
Blank lines are skipped. Lines may end with \r\n (Windows): they still count as valid.
The test driver writes the log (one test makes 60,000 lines), then calls your function.
const rl = readline.createInterface({ input: fs.createReadStream(file), crlfDelay: Infinity });
for await (const line of rl) {
// one line at a time, without its line break
}Sample tests