From e289b515b13b376aaa66f12d8d630d607cd1e060 Mon Sep 17 00:00:00 2001 From: Le Vivilet Date: Fri, 27 Dec 2024 12:47:10 +0100 Subject: [PATCH] feature: add server timing response header --- .../src/parts/GetResponse/GetResponse.ts | 34 +++++++++++++------ .../test/WebViewProtocol.test.ts | 8 +++++ 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/packages/preview-process/src/parts/GetResponse/GetResponse.ts b/packages/preview-process/src/parts/GetResponse/GetResponse.ts index d3ed3dcb..4fed8df0 100644 --- a/packages/preview-process/src/parts/GetResponse/GetResponse.ts +++ b/packages/preview-process/src/parts/GetResponse/GetResponse.ts @@ -8,16 +8,28 @@ import { NotFoundResponse } from '../Responses/NotFoundResponse.ts' import * as Routes from '../Routes/Routes.ts' export const getResponse = async (request: RequestOptions, options: HandlerOptions): Promise => { - if (request.method !== HttpMethod.Get && request.method !== HttpMethod.Head) { - return new MethodNotAllowedResponse() - } - const matchedRoute = FindMatchingRoute.findMatchingRoute(request.path, Routes.routes) - if (!matchedRoute) { - return new NotFoundResponse() - } - const response = await matchedRoute.handler(request, options) - if (request.method === HttpMethod.Head) { - return new HeadResponse(response.status, response.headers) + const start = performance.now() + + try { + if (request.method !== HttpMethod.Get && request.method !== HttpMethod.Head) { + return new MethodNotAllowedResponse() + } + const matchedRoute = FindMatchingRoute.findMatchingRoute(request.path, Routes.routes) + if (!matchedRoute) { + return new NotFoundResponse() + } + const response = await matchedRoute.handler(request, options) + if (request.method === HttpMethod.Head) { + return new HeadResponse(response.status, response.headers) + } + + // Add Server-Timing header + const duration = Math.round(performance.now() - start) + response.headers.set('Server-Timing', `total;dur=${duration}`) + + return response + } catch (error) { + const response = new NotFoundResponse() + return response } - return response } diff --git a/packages/preview-process/test/WebViewProtocol.test.ts b/packages/preview-process/test/WebViewProtocol.test.ts index b3c713c3..94c6a6fe 100644 --- a/packages/preview-process/test/WebViewProtocol.test.ts +++ b/packages/preview-process/test/WebViewProtocol.test.ts @@ -54,6 +54,7 @@ test('get - css file', async () => { headers: { 'Content-Type': 'text/css', 'Cross-Origin-Resource-Policy': 'same-origin', + 'Server-Timing': expect.any(String), }, }) expect(response.body.toString()).toBe('a') @@ -69,6 +70,7 @@ test('get - javascript file', async () => { headers: { 'Content-Type': 'text/javascript', 'Cross-Origin-Resource-Policy': 'same-origin', + 'Server-Timing': expect.any(String), }, }) expect(response.body.toString()).toBe('console.log("test")') @@ -102,6 +104,7 @@ test('get - preview injected', async () => { headers: { 'Content-Type': 'text/javascript', 'Cross-Origin-Resource-Policy': 'same-origin', + 'Server-Timing': expect.any(String), }, }) expect(response.body).toBeDefined() @@ -118,6 +121,7 @@ test('get - index.html', async () => { 'Content-Type': 'text/html', 'Cross-Origin-Resource-Policy': 'cross-origin', 'Cross-Origin-Embedder-Policy': 'require-corp', + 'Server-Timing': expect.any(String), }, }) expect(response.body.toString()).toBe('

hello world

') @@ -162,6 +166,7 @@ test('get - png image file', async () => { headers: { 'Content-Type': 'image/png', 'Cross-Origin-Resource-Policy': 'same-origin', + 'Server-Timing': expect.any(String), }, }, }) @@ -179,6 +184,7 @@ test('get - svg file', async () => { headers: { 'Content-Type': 'image/svg+xml', 'Cross-Origin-Resource-Policy': 'same-origin', + 'Server-Timing': expect.any(String), }, }, }) @@ -196,6 +202,7 @@ test('get - json file', async () => { headers: { 'Content-Type': 'application/json', 'Cross-Origin-Resource-Policy': 'same-origin', + 'Server-Timing': expect.any(String), }, }, }) @@ -213,6 +220,7 @@ test('get - unknown file type', async () => { headers: { 'Content-Type': 'text/plain', 'Cross-Origin-Resource-Policy': 'same-origin', + 'Server-Timing': expect.any(String), }, }, })