Skip to content

Commit

Permalink
fix types, update urls
Browse files Browse the repository at this point in the history
  • Loading branch information
shahata committed Oct 26, 2024
1 parent 0b93d97 commit ed28f76
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
2 changes: 2 additions & 0 deletions src/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
declare const forge: any;
declare const URLPattern: any;
declare const Deno: any;
1 change: 1 addition & 0 deletions src/server/deno.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* global Deno */
import worker from './worker.js';

Deno.serve(worker.fetch);
28 changes: 14 additions & 14 deletions src/server/worker.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* global URLPattern */
function cleanError(s) {
return s.match(/<h1>(.*)<\/h1>/)?.[1] || s;
}
Expand Down Expand Up @@ -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),
{
Expand All @@ -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 });
},
Expand Down
2 changes: 1 addition & 1 deletion src/utils/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() : [];
}
Expand Down

0 comments on commit ed28f76

Please sign in to comment.