From 6fe35a78276828a0db005b37ee25c9691c1b532a Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Sun, 10 Jul 2022 21:35:27 -0700 Subject: [PATCH] Convert to module worker syntax It's clearer and requires less boilerplate than service worker syntax --- cors/index.js | 50 ++++++++++++++++++++++------------------------ hash/index.js | 42 +++++++++++++++++++------------------- instagram/index.js | 36 ++++++++++++++++----------------- ip/index.js | 8 +++++--- proxy/index.js | 15 ++++++-------- pwned/index.js | 38 +++++++++++++++++------------------ qr/index.js | 24 ++++++++++------------ style/index.js | 16 +++++++-------- unroll/index.js | 16 +++++++-------- uuid/index.js | 8 +++++--- 10 files changed, 120 insertions(+), 133 deletions(-) diff --git a/cors/index.js b/cors/index.js index 828513c..454bede 100644 --- a/cors/index.js +++ b/cors/index.js @@ -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'); + 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; } - 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; } - -addEventListener('fetch', event => { - event.respondWith(handleRequest(event.request)); -}) diff --git a/hash/index.js b/hash/index.js index 48e1cb9..de4696d 100644 --- a/hash/index.js +++ b/hash/index.js @@ -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; - } - 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; +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; + } } + + // 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); } - - // 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); } - -addEventListener('fetch', event => { - event.respondWith(handleRequest(event.request)); -}) diff --git a/instagram/index.js b/instagram/index.js index c3f460e..840cd94 100644 --- a/instagram/index.js +++ b/instagram/index.js @@ -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 = ` - -
- - `; + const responseBody = ` + +
+ + `; - 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)); -}) diff --git a/ip/index.js b/ip/index.js index d573cb8..7757657 100644 --- a/ip/index.js +++ b/ip/index.js @@ -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')); + } +} diff --git a/proxy/index.js b/proxy/index.js index 8535b01..2eb7f8d 100644 --- a/proxy/index.js +++ b/proxy/index.js @@ -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)); -}) diff --git a/pwned/index.js b/pwned/index.js index a2db1ae..22bbba5 100644 --- a/pwned/index.js +++ b/pwned/index.js @@ -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)); -}) diff --git a/qr/index.js b/qr/index.js index 38f55a1..91888d3 100644 --- a/qr/index.js +++ b/qr/index.js @@ -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)); -}) diff --git a/style/index.js b/style/index.js index 6747224..5638019 100644 --- a/style/index.js +++ b/style/index.js @@ -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)); -}) diff --git a/unroll/index.js b/unroll/index.js index e1a9a7f..5b8d35c 100644 --- a/unroll/index.js +++ b/unroll/index.js @@ -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)); -}) diff --git a/uuid/index.js b/uuid/index.js index fa0eb51..159bb4e 100644 --- a/uuid/index.js +++ b/uuid/index.js @@ -1,3 +1,5 @@ -addEventListener('fetch', event => { - event.respondWith(new Response(crypto.randomUUID())); -}) +export default { + async fetch(request) { + return new Response(crypto.randomUUID()); + } +} -- 2.47.3