// 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', level: process.env.NODE_ENV === 'production' ? 'info' : 'debug', src: true, }); function safePath(basePath: string, name: string): boolean { const fullPath = `${basePath}/${name}`; const relative = path.relative(basePath, fullPath); return ( !!relative && // does move out of `basePath` !relative.startsWith(`..${path.sep}`) && // exactly one layer deep, e.g. no `./uplodas/foo/bar` !relative.includes(path.sep) && // result is not an absolute path !path.isAbsolute(relative) ); } const defaultPath: string = './uploads'; if (!('STORAGE_PATH' in process.env)) { log.warn(`'STORAGE_PATH' environment variable is not set. Defaulting to ${defaultPath}`); } export const storagePath: string = process.env.STORAGE_PATH ?? defaultPath; export const requestIdHeader = 'x-request-id'; export default safePath; export async function timedExecution( fn: () => MaybePromise ): Promise<{ executionTime: number; result: T }> { const start = process.hrtime(); const result = await fn(); const end = process.hrtime(start); const executionTime = (end[0] * 1e6 + end[1]) / 1e6; return { executionTime, result }; }