-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));
-})
-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));
-})
-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));
-})
-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'));
+ }
+}
-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));
-})
-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));
-})
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));
-})
}
}
-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));
-})
-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));
-})
-addEventListener('fetch', event => {
- event.respondWith(new Response(crypto.randomUUID()));
-})
+export default {
+ async fetch(request) {
+ return new Response(crypto.randomUUID());
+ }
+}