1'use strict'; 2 3const { 4 Symbol, 5} = primordials; 6 7const { setUnrefTimeout } = require('internal/timers'); 8const { PerformanceEntry, notify } = internalBinding('performance'); 9 10let nowCache; 11let utcCache; 12 13function nowDate() { 14 if (!nowCache) cache(); 15 return nowCache; 16} 17 18function utcDate() { 19 if (!utcCache) cache(); 20 return utcCache; 21} 22 23function cache() { 24 const d = new Date(); 25 nowCache = d.valueOf(); 26 utcCache = d.toUTCString(); 27 setUnrefTimeout(resetCache, 1000 - d.getMilliseconds()); 28} 29 30function resetCache() { 31 nowCache = undefined; 32 utcCache = undefined; 33} 34 35class HttpRequestTiming extends PerformanceEntry { 36 constructor(statistics) { 37 super(); 38 this.name = 'HttpRequest'; 39 this.entryType = 'http'; 40 const startTime = statistics.startTime; 41 const diff = process.hrtime(startTime); 42 this.duration = diff[0] * 1000 + diff[1] / 1e6; 43 this.startTime = startTime[0] * 1000 + startTime[1] / 1e6; 44 } 45} 46 47function emitStatistics(statistics) { 48 notify('http', new HttpRequestTiming(statistics)); 49} 50 51module.exports = { 52 kOutHeaders: Symbol('kOutHeaders'), 53 kNeedDrain: Symbol('kNeedDrain'), 54 nowDate, 55 utcDate, 56 emitStatistics 57}; 58