|
| 1 | +function cleanError(s) { |
| 2 | + return s.match(/<h1>(.*)<\/h1>/)?.[1] || s; |
| 3 | +} |
| 4 | + |
| 5 | +function cleanAnswer(s) { |
| 6 | + return s.match(/<article><p>([^.]*\.)/)?.[1] || s; |
| 7 | +} |
| 8 | + |
| 9 | +function cleanQuestion(s) { |
| 10 | + return s.match(/(?<=<p>Your puzzle answer was <code>).*?(?=<\/code>)/g) || []; |
| 11 | +} |
| 12 | + |
| 13 | +async function downloadContent(url, session, postPayload) { |
| 14 | + const headers = { Cookie: `session=${session}` }; |
| 15 | + const options = { headers }; |
| 16 | + if (postPayload) { |
| 17 | + Object.assign(options, { |
| 18 | + body: postPayload, |
| 19 | + method: 'POST', |
| 20 | + }); |
| 21 | + Object.assign(options.headers, { |
| 22 | + 'content-type': 'application/x-www-form-urlencoded', |
| 23 | + }); |
| 24 | + } |
| 25 | + const response = await fetch(url, options); |
| 26 | + if (response.status >= 400) { |
| 27 | + throw new Error( |
| 28 | + [ |
| 29 | + `Failed to download from ${url} (${response.status})`, |
| 30 | + `Description: ${cleanError(await response.text())}`, |
| 31 | + ].join('\n'), |
| 32 | + ); |
| 33 | + } |
| 34 | + return await response.text(); |
| 35 | +} |
| 36 | + |
| 37 | +async function getDayAnswer(year, day, session) { |
| 38 | + const url = `https://adventofcode.com/${+year}/day/${+day}`; |
| 39 | + return cleanQuestion(await downloadContent(url, session)); |
| 40 | +} |
| 41 | + |
| 42 | +async function getDayInput(year, day, session) { |
| 43 | + const url = `https://adventofcode.com/${+year}/day/${+day}/input`; |
| 44 | + return await downloadContent(url, session); |
| 45 | +} |
| 46 | + |
| 47 | +async function submitDayAnswer(year, day, session, level, answer) { |
| 48 | + const url = `https://adventofcode.com/${+year}/day/${+day}/answer`; |
| 49 | + const postPayload = `level=${level}&answer=${encodeURIComponent(answer)}`; |
| 50 | + return cleanAnswer(await downloadContent(url, session, postPayload)); |
| 51 | +} |
| 52 | + |
| 53 | +async function respond(fn) { |
| 54 | + try { |
| 55 | + const body = await fn(); |
| 56 | + return new Response( |
| 57 | + typeof body === 'string' ? body : JSON.stringify(body), |
| 58 | + { |
| 59 | + status: 200, |
| 60 | + headers: { 'Access-Control-Allow-Origin': '*' }, |
| 61 | + }, |
| 62 | + ); |
| 63 | + } catch (e) { |
| 64 | + return new Response(e.toString(), { |
| 65 | + status: 500, |
| 66 | + headers: { 'Access-Control-Allow-Origin': '*' }, |
| 67 | + }); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +export default { |
| 72 | + async fetch(req) { |
| 73 | + let match; |
| 74 | + const session = new URL(req.url).searchParams.get('session'); |
| 75 | + match = new URLPattern({ pathname: '/input/:year/:day' }).exec(req.url); |
| 76 | + if (req.method === 'GET' && match) { |
| 77 | + const { year, day } = match.pathname.groups; |
| 78 | + return respond(() => getDayInput(year, day, session)); |
| 79 | + } |
| 80 | + match = new URLPattern({ pathname: '/question/:year/:day' }).exec(req.url); |
| 81 | + if (req.method === 'GET' && match) { |
| 82 | + const { year, day } = match.pathname.groups; |
| 83 | + return respond(() => getDayAnswer(year, day, session)); |
| 84 | + } |
| 85 | + match = new URLPattern({ pathname: '/answer/:year/:day' }).exec(req.url); |
| 86 | + if (req.method === 'POST' && match) { |
| 87 | + const { year, day } = match.pathname.groups; |
| 88 | + const formData = await req.formData(); |
| 89 | + const level = formData.get('level'); |
| 90 | + const answer = formData.get('answer'); |
| 91 | + return respond(() => submitDayAnswer(year, day, session, level, answer)); |
| 92 | + } |
| 93 | + return new Response('Not found', { status: 404 }); |
| 94 | + }, |
| 95 | +}; |
0 commit comments