### Install @elysia/server-timing Source: https://github.com/elysiajs/server-timing/blob/main/README.md Install the package using bun. ```bash bun add @elysiajs/server-timing ``` -------------------------------- ### Install Elysia Server Timing Source: https://context7.com/elysiajs/server-timing/llms.txt Install the server-timing plugin using bun or npm. ```bash bun add @elysia/server-timing ``` -------------------------------- ### Full Lifecycle Tracing Example Source: https://context7.com/elysiajs/server-timing/llms.txt This example demonstrates comprehensive lifecycle tracing, capturing timing for each hook and handler with their function names. It includes simulated delays to showcase performance measurement. ```typescript import { Elysia } from 'elysia' import { serverTiming } from '@elysia/server-timing' const delay = (ms: number) => new Promise(r => setTimeout(r, ms)) const app = new Elysia() .use(serverTiming()) .onRequest(async function initRequest() { // Simulated initialization await delay(50) }) .get('/api/users', function getUsers() { return [ { id: 1, name: 'Alice' }, { id: 2, name: 'Bob' } ] }, { beforeHandle: [ async function authenticate() { // Validate auth token await delay(10) }, async function authorize() { // Check permissions await delay(5) } ], async afterHandle({ response }) { // Log response await delay(2) return response } }) .listen(3000) // Response Headers: // Server-Timing: request;dur=59.34,request.0.initRequest;dur=57.48,beforeHandle;dur=363.20,beforeHandle.0.authenticate;dur=107.59,beforeHandle.1.authorize;dur=253.64,handle.getUsers;dur=0.18,afterHandle;dur=54.28,afterHandle.0.afterHandle;dur=54.19,total;dur=479.15 ``` -------------------------------- ### Basic Elysia Server-Timing Integration Source: https://github.com/elysiajs/server-timing/blob/main/README.md Integrate the server-timing plugin into an Elysia application and start the server on port 3000. ```typescript import { Elysia } from 'elysia' import { serverTiming } from '@elysiajs/server-timing' new Elysia() .use(serverTiming()) .get('/', () => 'hello') .listen(3000) ``` -------------------------------- ### Conditional Timing with allow Option Source: https://context7.com/elysiajs/server-timing/llms.txt Use the 'allow' option to conditionally include Server-Timing headers based on request properties. It accepts a boolean, Promise, or a function that returns true to enable timing. This example disables timing for '/health' and '/no-trace' paths, and enables it only if the 'X-Debug-Timing' header is present. ```typescript import { Elysia } from 'elysia' import { serverTiming } from '@elysia/server-timing' const app = new Elysia() .use(serverTiming({ allow: async ({ request }) => { // Only report timing for specific paths or conditions const url = new URL(request.url) // Disable timing for health checks if (url.pathname === '/health') return false // Only allow timing with debug header if (request.headers.get('X-Debug-Timing')) return true // Disable for specific routes return url.pathname !== '/no-trace' } })) .get('/', () => 'With timing') .get('/no-trace', () => 'Without timing') .get('/health', () => 'OK') .listen(3000) // GET / -> Server-Timing header included // GET /no-trace -> No Server-Timing header // GET /health -> No Server-Timing header ``` -------------------------------- ### Basic Server Timing Integration Source: https://context7.com/elysiajs/server-timing/llms.txt Integrate the serverTiming plugin into an Elysia app to automatically capture and report timing metrics via the Server-Timing HTTP header. ```typescript import { Elysia } from 'elysia' import { serverTiming } from '@elysia/server-timing' const app = new Elysia() .use(serverTiming()) .get('/', () => 'Hello World') .listen(3000) // Response Headers: // Server-Timing: handle.anon;dur=0.15,total;dur=1.25 ``` -------------------------------- ### Configure Server Timing Options Source: https://context7.com/elysiajs/server-timing/llms.txt Customize server timing behavior by configuring which lifecycle events to trace and conditionally allowing timing headers based on request context. ```typescript import { Elysia } from 'elysia' import { serverTiming } from '@elysia/server-timing' const app = new Elysia() .use(serverTiming({ // Disable in production (default: NODE_ENV !== 'production') enabled: process.env.NODE_ENV === 'development', // Configure which lifecycle events to trace trace: { request: true, // Trace request lifecycle parse: true, // Trace body parsing transform: true, // Trace transforms beforeHandle: true, // Trace beforeHandle hooks handle: true, // Trace main handler afterHandle: true, // Trace afterHandle hooks error: true, // Trace error handling mapResponse: true, // Trace response mapping total: true // Include total request duration } })) .get('/', () => 'Hello World') .listen(3000) ``` -------------------------------- ### Selective Event Tracing Configuration Source: https://context7.com/elysiajs/server-timing/llms.txt Exclude specific lifecycle events from timing reports by configuring the trace options. This is useful for reducing header size or focusing on particular performance areas. ```typescript import { Elysia } from 'elysia' import { serverTiming } from '@elysia/server-timing' const app = new Elysia() .use(serverTiming({ trace: { request: false, // Disable request tracing beforeHandle: false, // Disable beforeHandle tracing total: false // Exclude total duration } })) .onRequest(async function init() { await new Promise(r => setTimeout(r, 10)) }) .get('/', function demo() { return 'Server Timing' }, { beforeHandle: [ async function a() { await new Promise(r => setTimeout(r, 5)) }, async function b() { await new Promise(r => setTimeout(r, 5)) } ], async afterHandle() { await new Promise(r => setTimeout(r, 5)) } }) .listen(3000) // Response Headers (excludes request, beforeHandle, and total): // Server-Timing: handle.demo;dur=0.18,afterHandle;dur=54.28,afterHandle.0.afterHandle;dur=54.19 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.