sjs_timings-test-server.sjs (1299B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 "use strict"; 4 5 const trailerServerTiming = [ 6 { metric: "metric3", duration: "99789.11", description: "time3" }, 7 { metric: "metric4", duration: "1112.13", description: "time4" }, 8 ]; 9 10 const responseServerTiming = [ 11 { metric: "metric1", duration: "123.4", description: "time1" }, 12 { metric: "metric2", duration: "0", description: "time2" }, 13 ]; 14 15 function handleRequest(request, response) { 16 const body = "c\r\ndata reached\r\n3\r\nhej\r\n0\r\n"; 17 18 response.seizePower(); 19 response.write("HTTP/1.1 200 OK\r\n"); 20 response.write("Content-Type: text/plain\r\n"); 21 response.write(createServerTimingHeader(responseServerTiming)); 22 response.write("Transfer-Encoding: chunked\r\n"); 23 response.write("\r\n"); 24 response.write(body); 25 response.write(createServerTimingHeader(trailerServerTiming)); 26 response.write("\r\n"); 27 response.finish(); 28 } 29 30 function createServerTimingHeader(headerData) { 31 let header = ""; 32 for (let i = 0; i < headerData.length; i++) { 33 header += 34 "Server-Timing: " + 35 headerData[i].metric + 36 ";" + 37 "dur=" + 38 headerData[i].duration + 39 ";" + 40 "desc=" + 41 headerData[i].description + 42 "\r\n"; 43 } 44 return header; 45 }