From ce5ed7d0dbf95a72427636a62670b0ece36bfa83 Mon Sep 17 00:00:00 2001 From: Shahar Talmi Date: Sat, 23 Nov 2024 21:34:28 +0200 Subject: [PATCH] added local server --- src/global.d.ts | 2 -- src/server/deno.lock | 48 ++++++++++++++++++++++++++++++++++++++ src/server/local.js | 15 ++++++++++++ src/utils/create-worker.js | 2 ++ src/utils/urls.js | 15 +++++++++--- tsconfig.json | 1 + 6 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 src/server/deno.lock create mode 100644 src/server/local.js diff --git a/src/global.d.ts b/src/global.d.ts index 85f75816..a3df999f 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -1,4 +1,2 @@ /* 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.lock b/src/server/deno.lock new file mode 100644 index 00000000..a6962a0e --- /dev/null +++ b/src/server/deno.lock @@ -0,0 +1,48 @@ +{ + "version": "4", + "specifiers": { + "jsr:@std/cli@^1.0.6": "1.0.6", + "jsr:@std/encoding@^1.0.5": "1.0.5", + "jsr:@std/fmt@^1.0.3": "1.0.3", + "jsr:@std/http@*": "1.0.9", + "jsr:@std/media-types@^1.0.3": "1.0.3", + "jsr:@std/net@^1.0.4": "1.0.4", + "jsr:@std/path@^1.0.7": "1.0.7", + "jsr:@std/streams@^1.0.7": "1.0.7" + }, + "jsr": { + "@std/cli@1.0.6": { + "integrity": "d22d8b38c66c666d7ad1f2a66c5b122da1704f985d3c47f01129f05abb6c5d3d" + }, + "@std/encoding@1.0.5": { + "integrity": "ecf363d4fc25bd85bd915ff6733a7e79b67e0e7806334af15f4645c569fefc04" + }, + "@std/fmt@1.0.3": { + "integrity": "97765c16aa32245ff4e2204ecf7d8562496a3cb8592340a80e7e554e0bb9149f" + }, + "@std/http@1.0.9": { + "integrity": "d409fc319a5e8d4a154e576c758752e9700282d74f31357a12fec6420f9ecb6c", + "dependencies": [ + "jsr:@std/cli", + "jsr:@std/encoding", + "jsr:@std/fmt", + "jsr:@std/media-types", + "jsr:@std/net", + "jsr:@std/path", + "jsr:@std/streams" + ] + }, + "@std/media-types@1.0.3": { + "integrity": "b12d30a7852f7578f4d210622df713bbfd1cbdd9b4ec2eaf5c1845ab70bab159" + }, + "@std/net@1.0.4": { + "integrity": "2f403b455ebbccf83d8a027d29c5a9e3a2452fea39bb2da7f2c04af09c8bc852" + }, + "@std/path@1.0.7": { + "integrity": "76a689e07f0e15dcc6002ec39d0866797e7156629212b28f27179b8a5c3b33a1" + }, + "@std/streams@1.0.7": { + "integrity": "1a93917ca0c58c01b2bfb93647189229b1702677f169b6fb61ad6241cd2e499b" + } + } +} diff --git a/src/server/local.js b/src/server/local.js new file mode 100644 index 00000000..12cd8241 --- /dev/null +++ b/src/server/local.js @@ -0,0 +1,15 @@ +/* global Deno */ +import worker from "./worker.js"; +import { serveDir } from "jsr:@std/http/file-server"; + +const options = { + onListen: () => console.log("Listening on https://localhost"), + cert: await Deno.readTextFile("./static/http-cert.pem"), + key: await Deno.readTextFile("./static/http-key.pem"), + port: 443, +}; + +Deno.serve(options, async function (req) { + const response = await worker.fetch(req); + return response.status === 404 ? serveDir(req) : response; +}); diff --git a/src/utils/create-worker.js b/src/utils/create-worker.js index a53d6679..0f3373c2 100644 --- a/src/utils/create-worker.js +++ b/src/utils/create-worker.js @@ -64,6 +64,8 @@ function cleanResult(str, year) { str = str.match(/
([^]*)<\/main>/)[1].trim(); str = str.replace(/
[^]*<\/article>/, ""); return `[Go Check on Your Calendar] ${str}`; + } else if (str.includes("too recently")) { + return ''; } else { return str; } diff --git a/src/utils/urls.js b/src/utils/urls.js index 28e43148..d77d5b8b 100644 --- a/src/utils/urls.js +++ b/src/utils/urls.js @@ -22,6 +22,15 @@ export const imports = { ...unpkg("es-module-shims"), }; -// export const aocSolverServer = 'https://www.wix.com/_serverless/adventofcode'; -// export const aocSolverServer = 'https://aoc.shahar-talmi.workers.dev'; -export const aocSolverServer = "https://aoc.deno.dev"; +function server() { + if ( + typeof location !== "undefined" && + location.origin === "https://localhost" + ) { + return "https://localhost"; + } else { + return "https://aoc.deno.dev"; + } +} + +export const aocSolverServer = server(); diff --git a/tsconfig.json b/tsconfig.json index 9d941f2f..7349b185 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -9,6 +9,7 @@ "allowSyntheticDefaultImports": true }, "exclude": [ + "src/server", "templates" ] }