From a18a9ca34d5dc149f0db7af49daa22cd89d118a3 Mon Sep 17 00:00:00 2001 From: Collin Duncan <3679940+cgduncan7@users.noreply.github.com> Date: Fri, 20 Jan 2023 09:52:28 +0100 Subject: [PATCH] Prettiering :sparkles: --- src/common/password.ts | 4 +- src/common/runner.ts | 12 ++++-- src/server/cron.ts | 23 ++++++---- src/server/http.ts | 95 ++++++++++++++++++++++-------------------- 4 files changed, 75 insertions(+), 59 deletions(-) diff --git a/src/common/password.ts b/src/common/password.ts index 84b664e..6924d5c 100644 --- a/src/common/password.ts +++ b/src/common/password.ts @@ -42,7 +42,9 @@ export const hashPassword = async (password: string) => { const hash = await generateHash(password, saltBuffer) return hash } catch (err: any) { - asyncLocalStorage.getStore()?.error('Error hashing and salting password', { message: err.message }) + asyncLocalStorage + .getStore() + ?.error('Error hashing and salting password', { message: err.message }) throw err } } diff --git a/src/common/runner.ts b/src/common/runner.ts index 695b1fd..6fd2512 100644 --- a/src/common/runner.ts +++ b/src/common/runner.ts @@ -54,7 +54,9 @@ export class Runner { reservation.booked = true return true } catch (err) { - asyncLocalStorage.getStore()?.error('Error making reservation', reservation.format()) + asyncLocalStorage + .getStore() + ?.error('Error making reservation', reservation.format()) return false } } @@ -77,7 +79,9 @@ export class Runner { asyncLocalStorage.getStore()?.debug(`Navigating to ${date.format()}`) if (this.getLastVisibleDay().isBefore(date)) { - asyncLocalStorage.getStore()?.debug('Date is on different page, increase month') + asyncLocalStorage + .getStore() + ?.debug('Date is on different page, increase month') await this.page?.waitForSelector('td.month.next').then((d) => d?.click()) } @@ -96,7 +100,9 @@ export class Runner { } private async selectAvailableTime(res: Reservation): Promise { - asyncLocalStorage.getStore()?.debug('Selecting available time', res.format()) + asyncLocalStorage + .getStore() + ?.debug('Selecting available time', res.format()) let freeCourt: ElementHandle | null | undefined let i = 0 while (i < res.possibleDates.length && !freeCourt) { diff --git a/src/server/cron.ts b/src/server/cron.ts index 3884944..c8a2906 100644 --- a/src/server/cron.ts +++ b/src/server/cron.ts @@ -18,16 +18,21 @@ export const startTasks = () => { const task = schedule( '0 * * * * *', async (timestamp) => { - asyncLocalStorage.run(new Logger('cron', v4(), LogLevel.DEBUG), async () => { - const childLogger = asyncLocalStorage.getStore() - childLogger?.info('Running cron job', { timestamp }) - try { - await reserve() - childLogger?.info('Completed running cron job') - } catch (error: any) { - childLogger?.error('Error running cron job', { error: error.message }) + asyncLocalStorage.run( + new Logger('cron', v4(), LogLevel.DEBUG), + async () => { + const childLogger = asyncLocalStorage.getStore() + childLogger?.info('Running cron job', { timestamp }) + try { + await reserve() + childLogger?.info('Completed running cron job') + } catch (error: any) { + childLogger?.error('Error running cron job', { + error: error.message, + }) + } } - }) + ) }, getTaskConfig('reserver cron') ) diff --git a/src/server/http.ts b/src/server/http.ts index dd2808b..419a182 100644 --- a/src/server/http.ts +++ b/src/server/http.ts @@ -6,54 +6,57 @@ import { parseJson } from './utils' // Handles POST requests to /reservations const server = http.createServer(async (req, res) => { - await asyncLocalStorage.run(new Logger('request', v4(), LogLevel.DEBUG), async () => { - const logger = asyncLocalStorage.getStore() - logger?.debug('Incoming request') - const { url, method } = req - - if ( - !url || - !method || - !/^\/reservations$/.test(url) || - method.toLowerCase() !== 'post' - ) { - logger?.info('Not found') - res.writeHead(404, 'Not found') + await asyncLocalStorage.run( + new Logger('request', v4(), LogLevel.DEBUG), + async () => { + const logger = asyncLocalStorage.getStore() + logger?.debug('Incoming request') + const { url, method } = req + + if ( + !url || + !method || + !/^\/reservations$/.test(url) || + method.toLowerCase() !== 'post' + ) { + logger?.info('Not found') + res.writeHead(404, 'Not found') + res.end() + return + } + + let jsonBody: Record + const contentType = req.headers['content-type'] || 'application/json' + if (contentType !== 'application/json') { + logger?.error('Invalid content type', { contentType }) + res.writeHead(406, 'Unsupported content type') + res.end() + return + } + + try { + const length = Number.parseInt(req.headers['content-length'] || '0') + const encoding = req.readableEncoding || 'utf8' + jsonBody = await parseJson(length, encoding, req) + } catch (error: any) { + logger?.error('Failed to parse body', { error: error.message }) + res.writeHead(400, 'Bad request') + res.end() + return + } + + try { + await schedule(jsonBody) + } catch (error: any) { + logger?.error('Failed to schedule request', { error }) + res.writeHead(400, 'Bad request') + res.end() + return + } + res.end() - return } - - let jsonBody: Record - const contentType = req.headers['content-type'] || 'application/json' - if (contentType !== 'application/json') { - logger?.error('Invalid content type', { contentType }) - res.writeHead(406, 'Unsupported content type') - res.end() - return - } - - try { - const length = Number.parseInt(req.headers['content-length'] || '0') - const encoding = req.readableEncoding || 'utf8' - jsonBody = await parseJson(length, encoding, req) - } catch (error: any) { - logger?.error('Failed to parse body', { error: error.message }) - res.writeHead(400, 'Bad request') - res.end() - return - } - - try { - await schedule(jsonBody) - } catch (error: any) { - logger?.error('Failed to schedule request', { error }) - res.writeHead(400, 'Bad request') - res.end() - return - } - - res.end() - }) + ) }) export default server