0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-30 07:27:22 +01:00
nodejs/lib/internal/http.js
Robert Nagy bdf07f4317 http: simplify drain()
Simplify and slightly optimize draining outgoing http streams. Avoid
extra event listener and inline with rest of the drain logic.

PR-URL: https://github.com/nodejs/node/pull/29081
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2019-08-19 12:56:08 -07:00

54 lines
1.1 KiB
JavaScript

'use strict';
const { setUnrefTimeout } = require('internal/timers');
const { PerformanceEntry, notify } = internalBinding('performance');
var nowCache;
var 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
};