From 2dce93640d6c4e16493feed5aad4afb0409d82b7 Mon Sep 17 00:00:00 2001 From: Renovate Bot Date: Tue, 27 Aug 2024 22:01:59 +0000 Subject: [PATCH 1/5] chore(deps): update dependency @sveltejs/kit to v2.5.25 --- package-lock.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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", -- 2.45.2 From c9990e9d3096d88355ff22a66083a3997e93f1f7 Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Wed, 28 Aug 2024 16:12:38 +0200 Subject: [PATCH 2/5] Simplify --- src/hooks.server.ts | 6 +++--- src/{ => lib}/index.test.ts | 0 2 files changed, 3 insertions(+), 3 deletions(-) rename src/{ => lib}/index.test.ts (100%) 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 100% rename from src/index.test.ts rename to src/lib/index.test.ts -- 2.45.2 From 29e06fdf915af1b4ff46050013cf2685a59f7020 Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Wed, 28 Aug 2024 16:13:11 +0200 Subject: [PATCH 3/5] Our own `MaybePromise` --- src/lib/index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib/index.ts b/src/lib/index.ts index c2c405a..84ebbc6 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', @@ -33,6 +32,8 @@ export const requestIdHeader = 'x-request-id'; export default safePath; +export type MaybePromise = T | Promise; + export async function timedExecution( fn: () => MaybePromise ): Promise<{ executionTime: number; result: T }> { -- 2.45.2 From b177a348b7b3de09c8e813791df4b78dedbd8b0a Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Wed, 28 Aug 2024 16:13:26 +0200 Subject: [PATCH 4/5] Simple tests for timedExecution --- src/lib/index.test.ts | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/lib/index.test.ts b/src/lib/index.test.ts index dbc9154..b6aa671 100644 --- a/src/lib/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) => v; + const identity = (v) => 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); + }); +}); -- 2.45.2 From 15de87432b29c045885874c4867bee967e30f0c2 Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Thu, 29 Aug 2024 10:37:38 +0200 Subject: [PATCH 5/5] Typing --- src/lib/index.test.ts | 6 +++--- src/lib/index.ts | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/lib/index.test.ts b/src/lib/index.test.ts index b6aa671..39127ea 100644 --- a/src/lib/index.test.ts +++ b/src/lib/index.test.ts @@ -1,4 +1,4 @@ -import { safePath, timedExecution } from '$lib'; +import safePath, { timedExecution } from '$lib'; import { describe, it, expect } from 'vitest'; describe('safe path', () => { @@ -32,8 +32,8 @@ describe('safe path', () => { }); describe('timedExecution', () => { - const asyncIdentity = async (v) => v; - const identity = (v) => v; + 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)); diff --git a/src/lib/index.ts b/src/lib/index.ts index 84ebbc6..cee56ae 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -8,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 ( @@ -20,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)) { @@ -30,8 +32,6 @@ 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( -- 2.45.2