]> git.ardis.lu - cloudflare-workers/commitdiff
Add CORS worker
authorArdis Lu <ardisglu@gmail.com>
Thu, 27 May 2021 09:34:22 +0000 (02:34 -0700)
committerArdis Lu <ardisglu@gmail.com>
Thu, 27 May 2021 09:34:22 +0000 (02:34 -0700)
README.md
cors/index.js [new file with mode: 0644]
cors/package.json [new file with mode: 0644]
package.json

index c8bcc6b70a5665ce55908d780d275591c8c4c7ea..e0f06358ad7dca76b1efcc257d06fe51953c9252 100644 (file)
--- a/README.md
+++ b/README.md
@@ -83,3 +83,11 @@ This reverse proxy embeds Instagram posts so they can be viewed without logging
 ### Usage
 
 Same as `proxy`, but it will only work on instagram URLs in the format: `https://www.instagram.com/p/{POST_ID}/`.
+
+## cors
+
+This reverse proxy injects permissive CORS response headers to a response. Useful to use APIs that have not enabled CORS.
+
+### Usage
+
+Same as `proxy`.
diff --git a/cors/index.js b/cors/index.js
new file mode 100644 (file)
index 0000000..a358d1c
--- /dev/null
@@ -0,0 +1,18 @@
+async function handleRequest(request) {
+  const url = new URL(request.url);
+  const queryString = decodeURIComponent(url.search.substring(1));
+
+  let response = await fetch(queryString);
+  response = new Response(response.body, response);
+  response.headers.set('Access-Control-Allow-Origin', '*');
+  response.headers.set('Access-Control-Allow-Headers', '*');
+  response.headers.set('Access-Control-Allow-Methods', '*');
+  response.headers.set('Access-Control-Expose-Headers', '*');
+  response.headers.set('Access-Control-Max-Age', '86400');
+  
+  return response;
+}
+
+addEventListener('fetch', event => {
+  event.respondWith(handleRequest(event.request));
+})
diff --git a/cors/package.json b/cors/package.json
new file mode 100644 (file)
index 0000000..6809586
--- /dev/null
@@ -0,0 +1,22 @@
+{
+  "name": "cors",
+  "version": "0.1.0",
+  "description": "This reverse proxy injects permissive CORS response headers to a response.",
+  "private": true,
+  "main": "index.js",
+  "scripts": {
+    "login": "wrangler login",
+    "preview": "wrangler preview",
+    "build": "wrangler build",
+    "publish": "wrangler publish"
+  },
+  "author": "Ardis Lu",
+  "license": "ISC",
+  "devDependencies": {
+    "@cloudflare/wrangler": "^1.16.1"
+  },
+  "repository": {
+    "type": "git",
+    "url": "https://github.com/ardislu/cloudflare-workers.git"
+  }
+}
index 989467efbea4f0f33ddb0f1fa42cfd883a902c41..1ed753784e74aba1f998b1f3d89fdbdd4a2dcf0c 100644 (file)
@@ -20,6 +20,7 @@
   },
   "workspaces": [
     "proxy",
-    "instagram"
+    "instagram",
+    "cors"
   ]
 }
This page took 0.040735 seconds and 4 git commands to generate.