@mantajs/host-nitro 0.2.0-beta.6 → 0.2.0-beta.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mantajs/host-nitro",
3
- "version": "0.2.0-beta.6",
3
+ "version": "0.2.0-beta.9",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -6,8 +6,29 @@
6
6
  import { defineEventHandler, getRequestHeaders, getRequestURL, readRawBody } from '@mantajs/adapter-h3'
7
7
  import { getMantaAdapter } from '../manta-bootstrap.js'
8
8
 
9
+ function nowMs() {
10
+ return typeof performance !== 'undefined' ? performance.now() : Date.now()
11
+ }
12
+
13
+ function withServerTiming(response: Response, timings: Record<string, number>) {
14
+ const headers = new Headers(response.headers)
15
+ const existing = headers.get('server-timing')
16
+ const value = Object.entries(timings)
17
+ .map(([name, dur]) => `${name};dur=${dur.toFixed(1)}`)
18
+ .join(', ')
19
+ headers.set('server-timing', existing ? `${existing}, ${value}` : value)
20
+ return new Response(response.body, {
21
+ status: response.status,
22
+ statusText: response.statusText,
23
+ headers,
24
+ })
25
+ }
26
+
9
27
  export default defineEventHandler(async (event) => {
28
+ const startedAt = nowMs()
29
+ const bootStartedAt = nowMs()
10
30
  const adapter = await getMantaAdapter()
31
+ const bootMs = nowMs() - bootStartedAt
11
32
 
12
33
  const url = getRequestURL(event)
13
34
  const method = event.method ?? 'GET'
@@ -23,7 +44,9 @@ export default defineEventHandler(async (event) => {
23
44
  headers,
24
45
  body: (body || undefined) as BodyInit | undefined,
25
46
  })
47
+ const dispatchStartedAt = nowMs()
26
48
  const response = await adapter.handleRequest(request)
49
+ const dispatchMs = nowMs() - dispatchStartedAt
27
50
 
28
51
  // SPA fallback for dev mode: if the adapter returned 404 for a GET on a SPA path,
29
52
  // fetch the SPA index.html from Vite. In prod, Vercel rewrites handle this.
@@ -33,13 +56,16 @@ export default defineEventHandler(async (event) => {
33
56
  if (vitePort && pathname.startsWith('/admin') && !pathname.startsWith('/api/') && !pathname.match(/\.\w+$/)) {
34
57
  const viteRes = await fetch(`http://localhost:${vitePort}/admin/`)
35
58
  if (viteRes.ok) {
36
- return new Response(viteRes.body, {
37
- status: 200,
38
- headers: { 'content-type': 'text/html; charset=utf-8' },
39
- })
59
+ return withServerTiming(
60
+ new Response(viteRes.body, {
61
+ status: 200,
62
+ headers: { 'content-type': 'text/html; charset=utf-8' },
63
+ }),
64
+ { manta: nowMs() - startedAt, bootstrap: bootMs, dispatch: dispatchMs },
65
+ )
40
66
  }
41
67
  }
42
68
  }
43
69
 
44
- return response
70
+ return withServerTiming(response, { manta: nowMs() - startedAt, bootstrap: bootMs, dispatch: dispatchMs })
45
71
  })