diff --git a/src/global.d.ts b/src/global.d.ts index a3df999f..85f75816 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -1,2 +1,4 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ declare const forge: any; +declare const URLPattern: any; +declare const Deno: any; diff --git a/src/server/deno.js b/src/server/deno.js index 5c4729ae..ea22ecc0 100644 --- a/src/server/deno.js +++ b/src/server/deno.js @@ -1,3 +1,4 @@ +/* global Deno */ import worker from './worker.js'; Deno.serve(worker.fetch); diff --git a/src/server/worker.js b/src/server/worker.js index 871a99b3..d5b04e11 100644 --- a/src/server/worker.js +++ b/src/server/worker.js @@ -1,3 +1,4 @@ +/* global URLPattern */ function cleanError(s) { return s.match(/

(.*)<\/h1>/)?.[1] || s; } @@ -31,28 +32,28 @@ async function downloadContent(url, session, postPayload) { ].join('\n'), ); } - return await response.text(); + return response.text(); } -async function getDayAnswer(year, day, session) { +function getDayAnswer(year, day, session) { const url = `https://adventofcode.com/${+year}/day/${+day}`; - return cleanQuestion(await downloadContent(url, session)); + return downloadContent(url, session).then(cleanQuestion); } -async function getDayInput(year, day, session) { +function getDayInput(year, day, session) { const url = `https://adventofcode.com/${+year}/day/${+day}/input`; - return await downloadContent(url, session); + return downloadContent(url, session); } -async function submitDayAnswer(year, day, session, level, answer) { +function submitDayAnswer(year, day, session, level, answer) { const url = `https://adventofcode.com/${+year}/day/${+day}/answer`; const postPayload = `level=${level}&answer=${encodeURIComponent(answer)}`; - return cleanAnswer(await downloadContent(url, session, postPayload)); + return downloadContent(url, session, postPayload).then(cleanAnswer); } -async function respond(fn) { +async function respond(promise) { try { - const body = await fn(); + const body = await promise; return new Response( typeof body === 'string' ? body : JSON.stringify(body), { @@ -75,20 +76,19 @@ export default { match = new URLPattern({ pathname: '/input/:year/:day' }).exec(req.url); if (req.method === 'GET' && match) { const { year, day } = match.pathname.groups; - return respond(() => getDayInput(year, day, session)); + return respond(getDayInput(year, day, session)); } - match = new URLPattern({ pathname: '/question/:year/:day' }).exec(req.url); + match = new URLPattern({ pathname: '/answer/:year/:day' }).exec(req.url); if (req.method === 'GET' && match) { const { year, day } = match.pathname.groups; - return respond(() => getDayAnswer(year, day, session)); + return respond(getDayAnswer(year, day, session)); } - match = new URLPattern({ pathname: '/answer/:year/:day' }).exec(req.url); if (req.method === 'POST' && match) { const { year, day } = match.pathname.groups; const formData = await req.formData(); const level = formData.get('level'); const answer = formData.get('answer'); - return respond(() => submitDayAnswer(year, day, session, level, answer)); + return respond(submitDayAnswer(year, day, session, level, answer)); } return new Response('Not found', { status: 404 }); }, diff --git a/src/utils/worker.js b/src/utils/worker.js index 490f7a98..c6cf7863 100644 --- a/src/utils/worker.js +++ b/src/utils/worker.js @@ -24,7 +24,7 @@ async function readInput(session, year, day) { } async function readAnswers(session, year, day) { - const url = `${aocSolverServer}/question/${year}/${day}?session=${session}`; + const url = `${aocSolverServer}/answer/${year}/${day}?session=${session}`; const result = await fetch(url); return result.status === 200 ? await result.json() : []; }