Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'on-headers' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
return (req, res, next) => {
let jsMeter: JsMeter;
if (this.env.DEBUG) {
jsMeter = new JsMeter({ isPrint: false, isMs: true, isKb: true });
}
const startAt = Date.now();
onHeaders(res, () => {
const diff = Date.now();
const time = diff - startAt;
this.logger.info(
`${req.method} ${req.originalUrl} ${res.statusCode} +${time}ms`,
);
if (this.env.DEBUG) {
const {
diffCPU,
diffHeapTotal,
diffHeapUsed,
diffExternal,
diffRAM,
} = jsMeter.stop();
this.logger.debug(
`CPU: ${diffCPU}ms RAM: ${diffRAM}kb HeapTotal: ${diffHeapTotal}kb HeapUsed: ${diffHeapUsed}kb External: ${diffExternal}kb`,
);
statusCode: null,
queries: [],
debug: {}
};
res.set({
'X-Service-Name': serviceName,
'X-Requested-At': timestamp,
'X-B3-SpanId': spanId,
'X-B3-TraceId': traceId,
'X-B3-ParentSpanId': parentId,
'X-B3-Sampled': enabled ? 1 : 0
});
if (sampled < 1) {
onHeaders(res, () => {
const [seconds, nanoseconds] = process.hrtime(startedAt);
const duration = ((seconds * 1e3) + (nanoseconds * 1e-6));
res.set('X-Response-Milliseconds', parseInt(duration, 10));
});
next();
return;
}
onHeaders(res, () => {
const [seconds, nanoseconds] = process.hrtime(startedAt);
const duration = ((seconds * 1e3) + (nanoseconds * 1e-6)) * 1000;
tracer.duration = parseInt(duration, 10);
tracer.statusCode = res.statusCode;
res.set('X-Response-Milliseconds', tracer.duration / 1000);
const useHeader = config.get('trace.header');
export default options => (req, res, next) => {
if (res.timing) {
consola.warn('server-timing is already registered.')
}
res.timing = new ServerTiming()
if (options && options.total) {
res.timing.start('total', 'Nuxt Server Time')
}
onHeaders(res, () => {
res.timing.end('total')
if (res.timing.headers.length > 0) {
res.setHeader(
'Server-Timing',
[]
.concat(res.getHeader('Server-Timing') || [])
.concat(res.timing.headers)
.join(', ')
)
}
res.timing.clear()
})
next()
}