UNPKG

1.78 kBJavaScriptView Raw
1/*
2 * Copyright 2019 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12const readline = require('readline');
13
14const rl = readline.createInterface({
15 input: process.stdin,
16 output: process.stdout,
17 terminal: false,
18});
19
20const rows = [];
21
22rl.on('line', (line) => {
23 const idx = line.indexOf(':');
24 if (idx < 0) {
25 return;
26 }
27 const name = line.substring(0, idx).toLowerCase();
28 if (name !== 'server-timing') {
29 return;
30 }
31 const timings = line.substring(idx + 1).trim().split(',');
32 let row = null;
33 timings.forEach((t) => {
34 t = t.trim().split(';');
35 const [, time] = t[1].split('=');
36 if (t[0] === 'total' && rows.length > 0) {
37 rows[rows.length - 1].total = time;
38 } else {
39 const [, desc] = t[2].split('=');
40 if (!row) {
41 row = {};
42 rows.push(row);
43 }
44 row[desc] = time;
45 }
46 });
47});
48
49rl.on('close', () => {
50 const out = process.stdout;
51 let headers;
52 rows.forEach((row, idx) => {
53 if (idx === 0) {
54 headers = Object.keys(row);
55 headers.forEach((name) => {
56 out.write(`${name}, `);
57 });
58 out.write('\n');
59 }
60 Object.values(row).forEach((value) => {
61 out.write(`${value}, `);
62 });
63 out.write('\n');
64 });
65});