diff --git a/package-lock.json b/package-lock.json index 5009fa0..ac64110 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1108,9 +1108,9 @@ } }, "node_modules/@sveltejs/kit": { - "version": "2.5.24", - "resolved": "https://registry.npmjs.org/@sveltejs/kit/-/kit-2.5.24.tgz", - "integrity": "sha512-Nr2oxsCsDfEkdS/zzQQQbsPYTbu692Qs3/iE3L7VHzCVjG2+WujF9oMUozWI7GuX98KxYSoPMlAsfmDLSg44hQ==", + "version": "2.5.25", + "resolved": "https://registry.npmjs.org/@sveltejs/kit/-/kit-2.5.25.tgz", + "integrity": "sha512-5hBSEN8XEjDZ5+2bHkFh8Z0QyOk0C187cyb12aANe1c8aeKbfu5ZD5XaC2vEH4h0alJFDXPdUkXQBmeeXeMr1A==", "dev": true, "hasInstallScript": true, "license": "MIT", diff --git a/src/hooks.server.ts b/src/hooks.server.ts index 17372c1..dc43d27 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -24,9 +24,9 @@ export const handle: Handle = async ({ event, resolve }) => { // make requestId available to handlers event.locals.requestId = requestId; - const { executionTime, result: response } = await timedExecution(async () => { - return await resolve(event); - }); + const { executionTime, result: response } = await timedExecution( + async () => await resolve(event) + ); response.headers.set(requestIdHeader, requestId); log.info( diff --git a/src/index.test.ts b/src/lib/index.test.ts similarity index 55% rename from src/index.test.ts rename to src/lib/index.test.ts index dbc9154..39127ea 100644 --- a/src/index.test.ts +++ b/src/lib/index.test.ts @@ -1,4 +1,4 @@ -import safePath from '$lib'; +import safePath, { timedExecution } from '$lib'; import { describe, it, expect } from 'vitest'; describe('safe path', () => { @@ -30,3 +30,21 @@ describe('safe path', () => { expect(safePath('./uplodas', '..foobar..')).toBe(true); }); }); + +describe('timedExecution', () => { + const asyncIdentity = async (v: T): Promise => v; + const identity = (v: T): T => v; + + it('works with async', async () => { + const { executionTime, result } = await timedExecution(() => asyncIdentity(5)); + expect(result).toBe(5); + // execution time is always positive + expect(executionTime).toBeGreaterThanOrEqual(0); + }); + it('works with sync', async () => { + const { executionTime, result } = await timedExecution(() => identity(5)); + expect(result).toBe(5); + // execution time is always positive + expect(executionTime).toBeGreaterThanOrEqual(0); + }); +}); diff --git a/src/lib/index.ts b/src/lib/index.ts index c2c405a..cee56ae 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -1,7 +1,6 @@ // place files you want to import through the `$lib` alias in this folder. import path from 'path'; import bunyan from 'bunyan'; -import type { MaybePromise } from '@sveltejs/kit'; export const log = bunyan.createLogger({ name: 'fotochallenge', @@ -9,7 +8,7 @@ export const log = bunyan.createLogger({ src: true, }); -function safePath(basePath: string, name: string): boolean { +const safePath = (basePath: string, name: string): boolean => { const fullPath = `${basePath}/${name}`; const relative = path.relative(basePath, fullPath); return ( @@ -21,7 +20,9 @@ function safePath(basePath: string, name: string): boolean { // result is not an absolute path !path.isAbsolute(relative) ); -} +}; + +export default safePath; const defaultPath: string = './uploads'; if (!('STORAGE_PATH' in process.env)) { @@ -31,7 +32,7 @@ export const storagePath: string = process.env.STORAGE_PATH ?? defaultPath; export const requestIdHeader = 'x-request-id'; -export default safePath; +export type MaybePromise = T | Promise; export async function timedExecution( fn: () => MaybePromise