From e6d9d4f2c5f306e89203ed61cf57bdd22e4b3ed7 Mon Sep 17 00:00:00 2001 From: Jake Jarvis Date: Tue, 20 Feb 2024 09:38:28 -0500 Subject: [PATCH] use newer fetch handler w/ typescript --- .github/dependabot.yml | 10 ---------- README.md | 4 +++- index.js | 31 ------------------------------- package-lock.json | 21 +++++++++++++++++++++ package.json | 4 +++- src/index.ts | 31 +++++++++++++++++++++++++++++++ tsconfig.json | 12 ++++++++++++ wrangler.toml | 5 ++--- 8 files changed, 72 insertions(+), 46 deletions(-) delete mode 100644 .github/dependabot.yml delete mode 100644 index.js create mode 100644 src/index.ts create mode 100644 tsconfig.json diff --git a/.github/dependabot.yml b/.github/dependabot.yml deleted file mode 100644 index 38e5f26..0000000 --- a/.github/dependabot.yml +++ /dev/null @@ -1,10 +0,0 @@ -version: 2 - -updates: -- package-ecosystem: "npm" - directory: "/" - versioning-strategy: increase - schedule: - interval: "daily" - commit-message: - prefix: "📦 npm:" diff --git a/README.md b/README.md index 632b3fa..012d5d4 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # 🌎 [simpip](https://simpip.com/) -**⚡ Now powered purely by [Cloudflare Workers](https://www.cloudflare.com/products/cloudflare-workers/), making it _blazing_ fast from anywhere in the world — and even simpler!** [Try this code on the playground.](https://cloudflareworkers.com/#ad749bc793a44b32186fc5d10fddf163:https://simpip.com/) +**⚡ Now powered purely by [Cloudflare Workers](https://www.cloudflare.com/products/cloudflare-workers/), making it _blazing_ fast from anywhere in the world — and even simpler!** [Try this code on the playground.][playground] [![Deploy Cloudflare Worker](https://github.com/jakejarvis/simpip/actions/workflows/deploy.yml/badge.svg)](https://github.com/jakejarvis/simpip/actions/workflows/deploy.yml) @@ -41,3 +41,5 @@ jake@macbook:~$ ip ## License MIT + +[playground]: https://workers.cloudflare.com/playground#LYVwNgLglgDghgJwgegGYHsHALQBM4RwDcABAEbogB2+CAngLzbPYDqApmQNJQQBimYACFKNRHSEBjAFYBGAJqyAWgA0AigFYAktI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newline at end of file diff --git a/index.js b/index.js deleted file mode 100644 index c0321ab..0000000 --- a/index.js +++ /dev/null @@ -1,31 +0,0 @@ -const handle = async (request) => { - const opts = { - headers: { - "Content-Type": "text/plain", - "Cache-Control": "no-cache, no-store, must-revalidate", - "Content-Security-Policy": "default-src 'none'", - "X-Did-You-Know": "You can use \"curl -4\" or \"curl -6\" to get either IP address!", - "X-View-Source": "https://github.com/jakejarvis/simpip", - }, - }; - - if (new URL(request.url).pathname === "/") { - const ip = request.headers.get("cf-connecting-ip") || request.headers.get("x-forwarded-for"); - - return new Response(ip + "\n", { - status: 200, - statusText: "OK", - ...opts, - }); - } - - return new Response("404 Not Found\n", { - status: 404, - statusText: "Not Found", - ...opts, - }); -}; - -addEventListener("fetch", (event) => { - event.respondWith(handle(event.request)); -}); diff --git a/package-lock.json b/package-lock.json index f693b5a..882dc37 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,8 @@ "version": "0.0.0", "license": "MIT", "devDependencies": { + "@cloudflare/workers-types": "^4.20240208.0", + "typescript": "^5.3.3", "wrangler": "^3.27.0" } }, @@ -101,6 +103,12 @@ "node": ">=16" } }, + "node_modules/@cloudflare/workers-types": { + "version": "4.20240208.0", + "resolved": "https://registry.npmjs.org/@cloudflare/workers-types/-/workers-types-4.20240208.0.tgz", + "integrity": "sha512-MVGTTjZpJu4kJONvai5SdJzWIhOJbuweVZ3goI7FNyG+JdoQH41OoB+nMhLsX626vPLZVWGPIWsiSo/WZHzgQw==", + "dev": true + }, "node_modules/@cspotcode/source-map-support": { "version": "0.8.1", "resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz", @@ -1146,6 +1154,19 @@ "integrity": "sha512-AEYxH93jGFPn/a2iVAwW87VuUIkR1FVUKB77NwMF7nBTDkDrrT/Hpt/IrCJ0QXhW27jTBDcf5ZY7w6RiqTMw2Q==", "dev": true }, + "node_modules/typescript": { + "version": "5.3.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.3.3.tgz", + "integrity": "sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw==", + "dev": true, + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + }, "node_modules/undici": { "version": "5.28.3", "resolved": "https://registry.npmjs.org/undici/-/undici-5.28.3.tgz", diff --git a/package.json b/package.json index e1cb230..0f9782d 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,15 @@ { "name": "simpip", "version": "0.0.0", + "private": true, "license": "MIT", - "main": "index.js", "scripts": { "start": "wrangler dev", "deploy": "wrangler publish" }, "devDependencies": { + "@cloudflare/workers-types": "^4.20240208.0", + "typescript": "^5.3.3", "wrangler": "^3.27.0" } } diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..431e0a8 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,31 @@ +export default { + async fetch(request: Request) { + const opts = { + headers: { + "Content-Type": "text/plain", + "Cache-Control": "no-cache, no-store, must-revalidate", + "Expires": "0", + "Pragma": "no-cache", + "Content-Security-Policy": "default-src 'none'", + "X-Did-You-Know": "You can use \"curl -4\" or \"curl -6\" to get either IP address!", + "X-View-Source": "https://github.com/jakejarvis/simpip", + }, + }; + + if (new URL(request.url).pathname === "/") { + const ip = request.headers.get("cf-connecting-ip") || request.headers.get("x-forwarded-for"); + + return new Response(ip + "\n", { + status: 200, + statusText: "OK", + ...opts, + }); + } + + return new Response("404 Not Found\n", { + status: 404, + statusText: "Not Found", + ...opts, + }); + }, +} satisfies ExportedHandler; diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..926324a --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,12 @@ +{ + "compilerOptions": { + "noEmit": true, + "module": "esnext", + "target": "esnext", + "lib": ["esnext"], + "strict": true, + "moduleResolution": "node", + "types": ["@cloudflare/workers-types"] + }, + "exclude": ["node_modules"] +} diff --git a/wrangler.toml b/wrangler.toml index c0880d8..bc93453 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -1,7 +1,6 @@ name = "simpip" compatibility_date = "2024-02-08" -main = "index.js" +main = "./src/index.ts" routes = [ - { pattern = "simpip.com", custom_domain = true }, - { pattern = "www.simpip.com", custom_domain = true } + { pattern = "simpip.com", custom_domain = true } ]