From 3849dfa92ce3d53c818491780b25ca75130b17de Mon Sep 17 00:00:00 2001 From: Gordon Pedersen Date: Tue, 14 May 2024 10:31:45 +1000 Subject: [PATCH] full first commit --- package-lock.json | 35 ++++++++++++++ package.json | 7 ++- src/proxy.js | 23 --------- src/redirect.js | 13 ----- src/router.js | 90 ---------------------------------- src/worker.js | 121 ++++++++++++++++++++++++++++++++++++++-------- 6 files changed, 141 insertions(+), 148 deletions(-) delete mode 100644 src/proxy.js delete mode 100644 src/redirect.js delete mode 100644 src/router.js diff --git a/package-lock.json b/package-lock.json index 6ad36e2..b768c83 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,6 +7,11 @@ "": { "name": "status", "version": "0.0.0", + "dependencies": { + "moment": "^2.30.1", + "moment-timezone": "^0.5.45", + "yaml": "^2.4.2" + }, "devDependencies": { "wrangler": "^3.0.0" } @@ -923,6 +928,25 @@ "node": ">=16.13" } }, + "node_modules/moment": { + "version": "2.30.1", + "resolved": "https://registry.npmjs.org/moment/-/moment-2.30.1.tgz", + "integrity": "sha512-uEmtNhbDOrWPFS+hdjFCBfy9f2YoyzRpwcl+DqpC6taX21FzsTLQVbMV/W7PzNSX6x/bhC1zA3c2UQ5NzH6how==", + "engines": { + "node": "*" + } + }, + "node_modules/moment-timezone": { + "version": "0.5.45", + "resolved": "https://registry.npmjs.org/moment-timezone/-/moment-timezone-0.5.45.tgz", + "integrity": "sha512-HIWmqA86KcmCAhnMAN0wuDOARV/525R2+lOLotuGFzn4HO+FH+/645z2wx0Dt3iDv6/p61SIvKnDstISainhLQ==", + "dependencies": { + "moment": "^2.29.4" + }, + "engines": { + "node": "*" + } + }, "node_modules/ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", @@ -1256,6 +1280,17 @@ "integrity": "sha512-ibF0Or+FivM9lNrg+HGJfVX8WJqgo+kCLDc4vx6xMeTce7Aj+DLttKbxxRR/gNLSAelRc1omAPlJ77N/Jem07A==", "dev": true }, + "node_modules/yaml": { + "version": "2.4.2", + "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.4.2.tgz", + "integrity": "sha512-B3VqDZ+JAg1nZpaEmWtTXUlBneoGx6CPM9b0TENK6aoSu5t73dItudwdgmi6tHlIZZId4dZ9skcAQ2UbcyAeVA==", + "bin": { + "yaml": "bin.mjs" + }, + "engines": { + "node": ">= 14" + } + }, "node_modules/youch": { "version": "3.3.3", "resolved": "https://registry.npmjs.org/youch/-/youch-3.3.3.tgz", diff --git a/package.json b/package.json index 5f72db5..0c1e499 100644 --- a/package.json +++ b/package.json @@ -9,5 +9,10 @@ }, "devDependencies": { "wrangler": "^3.0.0" + }, + "dependencies": { + "moment": "^2.30.1", + "moment-timezone": "^0.5.45", + "yaml": "^2.4.2" } -} \ No newline at end of file +} diff --git a/src/proxy.js b/src/proxy.js deleted file mode 100644 index 9eb6675..0000000 --- a/src/proxy.js +++ /dev/null @@ -1,23 +0,0 @@ -export default { - async fetch(request, env, ctx) { - const url = new URL(request.url); - - const proxyUrl = url.searchParams.get('proxyUrl'); // get a query param value (?proxyUrl=...) - const modify = url.searchParams.has('modify'); // check if a query param is set (?proxyUrl=...&modify) - - if (!proxyUrl) { - return new Response('Bad request: Missing `proxyUrl` query param', { status: 400 }); - } - - // make subrequests with the global `fetch()` function - let res = await fetch(proxyUrl, request); - - // optionally, modify the respone - if (modify) { - res = new Response(res.body, res) - res.headers.set('X-My-Header', 'My Header Value'); - } - - return res; - }, -}; \ No newline at end of file diff --git a/src/redirect.js b/src/redirect.js deleted file mode 100644 index 8237740..0000000 --- a/src/redirect.js +++ /dev/null @@ -1,13 +0,0 @@ -export default { - async fetch(request, env, ctx) { - const url = new URL(request.url); - const redirectUrl = url.searchParams.get('redirectUrl'); // get a query param value (?redirectUrl=...) - - if (!redirectUrl) { - return new Response('Bad request: Missing `redirectUrl` query param', { status: 400 }); - } - - // The Response class has static methods to create common Response objects as a convenience - return Response.redirect(redirectUrl); - }, -}; \ No newline at end of file diff --git a/src/router.js b/src/router.js deleted file mode 100644 index 8ddbef1..0000000 --- a/src/router.js +++ /dev/null @@ -1,90 +0,0 @@ -// An example 'toy' implementation of a router using URLPattern: https://developer.mozilla.org/en-US/docs/Web/API/URL_Pattern_API -// If you're interested in more production-ready routing, you may want to check out one of the following: - -// itty-router: https://www.npmjs.com/package/itty-router -// Hono: https://www.npmjs.com/package/hono - -class Router { - routes = []; - - handle(request) { - for (const route of this.routes) { - const match = route[0](request); - if (match) { - return route[1]({ ...match, request }); - } - } - const match = this.routes.find(([matcher]) => matcher(request)); - if (match) { - return match[1](request); - } - } - - register(handler, path, method) { - const urlPattern = new URLPattern({ pathname: path }); - this.routes.push([ - (request) => { - if (method === undefined || request.method.toLowerCase() === method) { - const match = urlPattern.exec({ - pathname: new URL(request.url).pathname, - }); - if (match) { - return { params: match.pathname.groups }; - } - } - }, - (args) => handler(args), - ]); - } - - options(path, handler) { - this.register(handler, path, "options"); - } - head(path, handler) { - this.register(handler, path, "head"); - } - get(path, handler) { - this.register(handler, path, "get"); - } - post(path, handler) { - this.register(handler, path, "post"); - } - put(path, handler) { - this.register(handler, path, "put"); - } - patch(path, handler) { - this.register(handler, path, "patch"); - } - delete(path, handler) { - this.register(handler, path, "delete"); - } - - all(path, handler) { - this.register(handler, path); - } -} - -// Setting up our application: - -const router = new Router(); - -// GET collection index -router.get("/api/todos", () => new Response("Todos Index!")); - -// GET item -router.get( - "/api/todos/:id", - ({ params }) => new Response(`Todo #${params.id}`) -); - -// POST to the collection (we'll use async here) -router.post("/api/todos", async ({ request }) => { - const content = await request.json(); - - return new Response("Creating Todo: " + JSON.stringify(content)); -}); - -// 404 for everything else -router.all("*", () => new Response("Not Found.", { status: 404 })); - -export default router; \ No newline at end of file diff --git a/src/worker.js b/src/worker.js index 543f0da..f7959ba 100644 --- a/src/worker.js +++ b/src/worker.js @@ -8,40 +8,119 @@ * Learn more at https://developers.cloudflare.com/workers/ */ -import handleProxy from './proxy.js'; -import handleRedirect from './redirect.js'; -import apiRouter from './router.js'; +import YAML from 'yaml' +import moment from 'moment-timezone' + // Export a default object containing event handlers export default { // The fetch handler is invoked when this worker receives a HTTP(S) request // and should return a Response (optionally wrapped in a Promise) - async fetch(request, env, ctx) { + async fetch(req, env, ctx) { // You'll find it helpful to parse the request.url string into a URL object. Learn more at https://developer.mozilla.org/en-US/docs/Web/API/URL - const url = new URL(request.url); + const url = new URL(req.url); - // You can get pretty far with simple logic like if/switch-statements - switch (url.pathname) { - case '/redirect': - return handleRedirect.fetch(request, env, ctx); + // log the incoming request info + console.info(`${new Date().toISOString()} 📥 ${req.method} ${req.url}`) - case '/proxy': - return handleProxy.fetch(request, env, ctx); + // CORS route (for now, any domain has access) + if(req.method === "OPTIONS") { + const headers = { + 'Access-Control-Allow-Origin': req.headers.get('Origin') || '*', + 'Access-Control-Allow-Methods': 'POST, GET, OPTIONS, DELETE', + 'Access-Control-Max-Age': '86400', + 'Access-Control-Allow-Credentials': true + } + let h + if (h = req.headers.get('Access-Control-Request-Headers')) + headers['Access-Control-Allow-Headers'] = 'Accept, Content-Type, Authorization, Signature, Digest, Date, Host' + return new Response('', { status: 204, headers }) } - - if (url.pathname.startsWith('/api/')) { - // You can also use more robust routing - return apiRouter.handle(request); + else if(req.method === "POST" && url.pathname.startsWith("/status.lol")) { + const body = await req.json() + return handleLol(body, env) } + else if(req.method === "GET" && url.pathname.startsWith("/apget")) { + const href = url.searchParams.get('url') + if(!href) return new Response('"url" parameter required', { status: 400, headers: { "Content-Type": "text/plain" }}) + return fetch(url, { + method: "GET", + headers: { + 'Accept': 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"' + } + }) + } return new Response( - `Try making requests to: -