]> git.ardis.lu - cloudflare-workers/commitdiff
Convert to module worker syntax
authorArdis Lu <ardisglu@gmail.com>
Mon, 11 Jul 2022 04:35:27 +0000 (21:35 -0700)
committerArdis Lu <ardisglu@gmail.com>
Mon, 11 Jul 2022 04:35:27 +0000 (21:35 -0700)
It's clearer and requires less boilerplate than service worker syntax

cors/index.js
hash/index.js
instagram/index.js
ip/index.js
proxy/index.js
pwned/index.js
qr/index.js
style/index.js
unroll/index.js
uuid/index.js

index 828513c2d21417c3333afd9d04acd614cd6b7e42..454bede8f8143f78d2ec4885fd759480f00d41db 100644 (file)
@@ -1,29 +1,27 @@
-async function handleRequest(request) {
-  const url = new URL(request.url);
-  const queryString = decodeURIComponent(url.search.substring(1));
+export default {
+  async fetch(request) {
+    const url = new URL(request.url);
+    const queryString = decodeURIComponent(url.search.substring(1));
 
-  let response;
-  if (request.method === 'OPTIONS') {
-    response = new Response();
-    response.headers.set('Access-Control-Allow-Origin', request.headers.get('Origin'));
-    response.headers.set('Access-Control-Allow-Methods', request.headers.get('Access-Control-Request-Method'));
-    response.headers.set('Access-Control-Allow-Headers', request.headers.get('Access-Control-Request-Headers'));
-    response.headers.set('Access-Control-Allow-Credentials', 'true');
-    response.headers.set('Access-Control-Max-Age', '86400');
-    response.headers.append('Vary', 'Origin');
-  }
-  else {
-    response = await fetch(queryString);
-    response = new Response(response.body, response);
-    response.headers.set('Access-Control-Allow-Origin', request.headers.get('Origin'));
-    response.headers.set('Access-Control-Allow-Credentials', 'true');
-    response.headers.set('Access-Control-Expose-Headers', '*, Authorization');
-    response.headers.append('Vary', 'Origin');
+    let response;
+    if (request.method === 'OPTIONS') {
+      response = new Response();
+      response.headers.set('Access-Control-Allow-Origin', request.headers.get('Origin'));
+      response.headers.set('Access-Control-Allow-Methods', request.headers.get('Access-Control-Request-Method'));
+      response.headers.set('Access-Control-Allow-Headers', request.headers.get('Access-Control-Request-Headers'));
+      response.headers.set('Access-Control-Allow-Credentials', 'true');
+      response.headers.set('Access-Control-Max-Age', '86400');
+      response.headers.append('Vary', 'Origin');
+    }
+    else {
+      response = await fetch(queryString);
+      response = new Response(response.body, response);
+      response.headers.set('Access-Control-Allow-Origin', request.headers.get('Origin'));
+      response.headers.set('Access-Control-Allow-Credentials', 'true');
+      response.headers.set('Access-Control-Expose-Headers', '*, Authorization');
+      response.headers.append('Vary', 'Origin');
+    }
+
+    return response;
   }
-  
-  return response;
 }
-
-addEventListener('fetch', event => {
-  event.respondWith(handleRequest(event.request));
-})
index 48e1cb9d05e76b93e582c7a7d16aabb507b10eb6..de4696da34e606d3c1dd556496d54469364f6a60 100644 (file)
@@ -1,25 +1,23 @@
-async function handleRequest(request) {
-  const url = new URL(request.url);
-  let algorithm = 'SHA-256';
-  let query = 'Example string';
-  for (const [key, value] of url.searchParams) {
-    if (key.toLowerCase() === 'algorithm' && ['SHA-1', 'SHA-256', 'SHA-384', 'SHA-512'].includes(value.toUpperCase())) {
-      algorithm = value;
+export default {
+  async fetch(request) {
+    const url = new URL(request.url);
+    let algorithm = 'SHA-256';
+    let query = 'Example string';
+    for (const [key, value] of url.searchParams) {
+      if (key.toLowerCase() === 'algorithm' && ['SHA-1', 'SHA-256', 'SHA-384', 'SHA-512'].includes(value.toUpperCase())) {
+        algorithm = value;
+      }
+      else if ((key.toLowerCase() === 'q' || key.toLowerCase() === 'query') && value) { // Intentionally excluding falsy values and not just null because empty strings are no good
+        query = value;
+      }
     }
-    else if ((key.toLowerCase() === 'q' || key.toLowerCase() === 'query') && value) { // Intentionally excluding falsy values and not just null because empty strings are no good
-      query = value;
-    }
-  }
 
-  // Convert query string to byte array and hash it, then convert hashed byte array to hex string
-  const inputArray = new TextEncoder().encode(query);
-  const digest = await crypto.subtle.digest(algorithm, inputArray);
-  const outputArray = [...new Uint8Array(digest)]; // Need to spread typed array into a non-typed array to allow .map() to output strings
-  const hash = outputArray.map(v => v.toString(16).padStart(2, '0')).join('').toUpperCase();
+    // Convert query string to byte array and hash it, then convert hashed byte array to hex string
+    const inputArray = new TextEncoder().encode(query);
+    const digest = await crypto.subtle.digest(algorithm, inputArray);
+    const outputArray = [...new Uint8Array(digest)]; // Need to spread typed array into a non-typed array to allow .map() to output strings
+    const hash = outputArray.map(v => v.toString(16).padStart(2, '0')).join('').toUpperCase();
 
-  return new Response(hash);
+    return new Response(hash);
+  }
 }
-
-addEventListener('fetch', event => {
-  event.respondWith(handleRequest(event.request));
-})
index c3f460e8fa2171a606df526a150bee6652f86959..840cd942ac075a25b1bb49d8913dc368dc33ee6a 100644 (file)
@@ -1,23 +1,21 @@
-async function handleRequest(request) {
-  const url = new URL(request.url);
-  const queryString = decodeURIComponent(url.search.substring(1));
+export default {
+  async fetch(request) {
+    const url = new URL(request.url);
+    const queryString = decodeURIComponent(url.search.substring(1));
 
-  const segments = queryString.split('/');
-  const id = segments.pop() || segments.pop(); // If there's a trailing '/' character, need to pop() twice
+    const segments = queryString.split('/');
+    const id = segments.pop() || segments.pop(); // If there's a trailing '/' character, need to pop() twice
 
-  const responseBody = `<!DOCTYPE html>
-  <body>
-    <blockquote class="instagram-media" data-instgrm-captioned data-instgrm-permalink="https://www.instagram.com/p/${id}/?utm_source=ig_embed" data-instgrm-version="13"></blockquote>
-    <script async defer src="https://platform.instagram.com/en_US/embeds.js"></script>
-  </body>`;
+    const responseBody = `<!DOCTYPE html>
+    <body>
+      <blockquote class="instagram-media" data-instgrm-captioned data-instgrm-permalink="https://www.instagram.com/p/${id}/?utm_source=ig_embed" data-instgrm-version="13"></blockquote>
+      <script async defer src="https://platform.instagram.com/en_US/embeds.js"></script>
+    </body>`;
 
-  return new Response(responseBody, {
-    headers: {
-      "content-type": "text/html;charset=UTF-8",
-    },
-  });
+    return new Response(responseBody, {
+      headers: {
+        "content-type": "text/html;charset=UTF-8",
+      },
+    });
+  }
 }
-
-addEventListener('fetch', event => {
-  event.respondWith(handleRequest(event.request));
-})
index d573cb8d062e86f8be5180a9e77d9f18f7f9d035..77576575a733073a25e23240763104f8323c3ce9 100644 (file)
@@ -1,3 +1,5 @@
-addEventListener('fetch', event => {
-  event.respondWith(new Response(event.request.headers.get('CF-Connecting-IP')));
-})
+export default {
+  async fetch(request) {
+    return new Response(event.request.headers.get('CF-Connecting-IP'));
+  }
+}
index 8535b01a1aaae6a41efedada10dee1f9b3acbc75..2eb7f8dca28a321026d02b3887cf10292a0cdf82 100644 (file)
@@ -1,10 +1,7 @@
-async function handleRequest(request) {
-  const url = new URL(request.url);
-  const queryString = decodeURIComponent(url.search.substring(1));
-
-  return await fetch(queryString);
+export default {
+  async fetch(request) {
+    const url = new URL(request.url);
+    const queryString = decodeURIComponent(url.search.substring(1));
+    return await fetch(queryString);
+  }
 }
-
-addEventListener('fetch', event => {
-  event.respondWith(handleRequest(event.request));
-})
index a2db1ae3df9f909cd65b16ead76182b5dae60794..22bbba5d94435b1cf5138c72d435b812dca6819f 100644 (file)
@@ -1,25 +1,23 @@
-async function handleRequest(request) {
-  const url = new URL(request.url);
-  const queryString = decodeURIComponent(url.search.substring(1));
+export default {
+  async fetch(request) {
+    const url = new URL(request.url);
+    const queryString = decodeURIComponent(url.search.substring(1));
 
-  // Convert password string to byte array and hash it, then convert hashed byte array to hex string
-  const inputArray = new TextEncoder().encode(queryString);
-  const digest = await crypto.subtle.digest('SHA-1', inputArray);
-  const outputArray = [...new Uint8Array(digest)]; // Need to spread typed array into a non-typed array to allow .map() to output strings
-  const hash = outputArray.map(v => v.toString(16).padStart(2, '0')).join('').toUpperCase();
+    // Convert password string to byte array and hash it, then convert hashed byte array to hex string
+    const inputArray = new TextEncoder().encode(queryString);
+    const digest = await crypto.subtle.digest('SHA-1', inputArray);
+    const outputArray = [...new Uint8Array(digest)]; // Need to spread typed array into a non-typed array to allow .map() to output strings
+    const hash = outputArray.map(v => v.toString(16).padStart(2, '0')).join('').toUpperCase();
 
-  // Split the hash as required by the HIBP API (k-Anonymity model)
-  const range = hash.substring(0, 5);
-  const index = hash.substring(5);
+    // Split the hash as required by the HIBP API (k-Anonymity model)
+    const range = hash.substring(0, 5);
+    const index = hash.substring(5);
 
-  const result = fetch(`https://api.pwnedpasswords.com/range/${range}`)
-    .then(response => response.text())
-    .then(body => body.includes(index))
-    .then(bool => new Response(bool));
+    const result = fetch(`https://api.pwnedpasswords.com/range/${range}`)
+      .then(response => response.text())
+      .then(body => body.includes(index))
+      .then(bool => new Response(bool));
 
-  return result;
+    return result;
+  }
 }
-
-addEventListener('fetch', event => {
-  event.respondWith(handleRequest(event.request));
-})
index 38f55a1ef46adbcace940fa5ee95c1bbb1b53caf..91888d3b7089d9adc97d98c06580be1444381927 100644 (file)
@@ -1,17 +1,15 @@
 import QRCode from 'qrcode';
 
-async function handleRequest(request) {
-  const url = new URL(request.url);
-  const queryString = decodeURIComponent(url.search.substring(1));
-  const qrImage = await QRCode.toString(queryString || 'https://example.com', { type: 'svg' });
+export default {
+  async fetch(request) {
+    const url = new URL(request.url);
+    const queryString = decodeURIComponent(url.search.substring(1));
+    const qrImage = await QRCode.toString(queryString || 'https://example.com', { type: 'svg' });
 
-  return new Response(qrImage, {
-    headers: {
-      'Content-Type': 'image/svg+xml'
-    }
-  })
+    return new Response(qrImage, {
+      headers: {
+        'Content-Type': 'image/svg+xml'
+      }
+    })
+  }
 }
-
-addEventListener('fetch', event => {
-  event.respondWith(handleRequest(event.request));
-})
index 6747224973c7ff4985e1bd37b675ace9fe1805c3..56380196e711dd432f46ab8ca5754009f6992610 100644 (file)
@@ -4,14 +4,12 @@ class InjectStyle {
   }
 }
 
-async function handleRequest(request) {
-  const url = new URL(request.url);
-  const queryString = decodeURIComponent(url.search.substring(1));
+export default {
+  async fetch(request) {
+    const url = new URL(request.url);
+    const queryString = decodeURIComponent(url.search.substring(1));
 
-  const response = await fetch(queryString);
-  return new HTMLRewriter().on('html', new InjectStyle()).transform(response);
+    const response = await fetch(queryString);
+    return new HTMLRewriter().on('html', new InjectStyle()).transform(response);
+  }
 }
-
-addEventListener('fetch', event => {
-  event.respondWith(handleRequest(event.request));
-})
index e1a9a7f00d532f3fdea4f36f13f2ef71803b29ec..5b8d35ccbba779a5606efb5a9a9c7aaa476e856b 100644 (file)
@@ -1,11 +1,9 @@
-async function handleRequest(request) {
-  const url = new URL(request.url);
-  const queryString = decodeURIComponent(url.search.substring(1));
-  const tweet = queryString.split('/').pop();
+export default {
+  async fetch(request) {
+    const url = new URL(request.url);
+    const queryString = decodeURIComponent(url.search.substring(1));
+    const tweet = queryString.split('/').pop();
 
-  return Response.redirect(`https://threadreaderapp.com/thread/${tweet}`);
+    return Response.redirect(`https://threadreaderapp.com/thread/${tweet}`);
+  }
 }
-
-addEventListener('fetch', event => {
-  event.respondWith(handleRequest(event.request));
-})
index fa0eb515030b6822f4a731af08ae8eb6429c81d3..159bb4e1a2e6c1b112d46da86c2ce18f74efa720 100644 (file)
@@ -1,3 +1,5 @@
-addEventListener('fetch', event => {
-  event.respondWith(new Response(crypto.randomUUID()));
-})
+export default {
+  async fetch(request) {
+    return new Response(crypto.randomUUID());
+  }
+}
This page took 0.057714 seconds and 4 git commands to generate.