Hono — إطار العمل للويب على معايير الويب › الوسائط المدمجة 3 / 8 السابق التالي

البرمجية الوسيطة Server-Timing

الـ Server-Timing توفّر البرمجيات الوسيطة مقاييس الأداء في ترويسات الاستجابة.

ملاحظة: على Cloudflare Workers، قد لا تكون مقاييس المؤقت (timer metrics) دقيقة، منذ تُظهر المؤقتات فقط وقت آخر إدخال/إخراج (I/O).

Import

import { Hono } from 'hono'
import {
  timing,
  setMetric,
  startTime,
  endTime,
  wrapTime,
} from 'hono/timing'
import type { TimingVariables } from 'hono/timing'

الاستخدام

// Specify the variable types to infer the `c.get('metric')`:
type Variables = TimingVariables

const app = new Hono<{ Variables: Variables }>()

// add the middleware to your router
app.use(timing());

app.get('/', async (c) => {

  // add custom metrics
  setMetric(c, 'region', 'europe-west3')

  // add custom metrics with timing, must be in milliseconds
  setMetric(c, 'custom', 23.8, 'My custom Metric')

  // start a new timer
  startTime(c, 'db');
  const data = await db.findMany(...);

  // end the timer
  endTime(c, 'db');

  // ...or you can also just wrap a Promise using this function:
  const data = await wrapTime(c, 'db', db.findMany(...));

  return c.json({ response: data });
});

مُفعّلة شرطيًا

const app = new Hono()

app.use(
  '*',
  timing({
    // c: Context of the request
    enabled: (c) => c.req.method === 'POST',
  })
)

النتيجة

الخيارات

total: boolean

أظهر زمن الاستجابة الكلي. الافتراضي هو true.

enabled: boolean | (c: Context) => boolean

ما إذا كان يجب إضافة التوقيتات (timings) إلى الرؤوس (headers) أم لا. الافتراضي هو true.

totalDescription: string

وصف لإجمالي زمن الاستجابة. الافتراضي هو Total Response Time.

autoEnd: boolean

إذا startTime() يجب أن تنتهي تلقائيًا في نهاية الطلب. إذا كان معطَّلاً، فلن تظهر المؤقتات (timers) التي لم تُنهَ يدوياً.

crossOrigin: boolean | string | (c: Context) => boolean | string

الأصل الذي ينبغي أن تكون ترويسة التوقيتات هذه قابلة للقراءة منه.

  • إذا كانت false، فمن الأصل الحالي فقط.
  • إذا كانت true، من جميع الأصول.
  • إذا كانت سلسلة نصية، فمن هذه النطاقات. يجب فصل النطاقات المتعددة بفاصلة.

القيمة الافتراضية هي false. اطّلع على المزيد المستندات.