0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-30 15:30:56 +01:00
nodejs/lib/internal/http.js
Sebastien Ahkrin eac3f0adc4
lib: replace Symbol global by the primordials Symbol
PR-URL: https://github.com/nodejs/node/pull/30737
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
2019-12-08 13:38:58 +01:00

58 lines
1.1 KiB
JavaScript

'use strict';
const {
Symbol,
} = primordials;
const { setUnrefTimeout } = require('internal/timers');
const { PerformanceEntry, notify } = internalBinding('performance');
let nowCache;
let utcCache;
function nowDate() {
if (!nowCache) cache();
return nowCache;
}
function utcDate() {
if (!utcCache) cache();
return utcCache;
}
function cache() {
const d = new Date();
nowCache = d.valueOf();
utcCache = d.toUTCString();
setUnrefTimeout(resetCache, 1000 - d.getMilliseconds());
}
function resetCache() {
nowCache = undefined;
utcCache = undefined;
}
class HttpRequestTiming extends PerformanceEntry {
constructor(statistics) {
super();
this.name = 'HttpRequest';
this.entryType = 'http';
const startTime = statistics.startTime;
const diff = process.hrtime(startTime);
this.duration = diff[0] * 1000 + diff[1] / 1e6;
this.startTime = startTime[0] * 1000 + startTime[1] / 1e6;
}
}
function emitStatistics(statistics) {
notify('http', new HttpRequestTiming(statistics));
}
module.exports = {
kOutHeaders: Symbol('kOutHeaders'),
kNeedDrain: Symbol('kNeedDrain'),
nowDate,
utcDate,
emitStatistics
};