Skip to content

Convert to ES modules #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module.exports = {
// See https://github.com/torchbox/eslint-config-torchbox for rules.
extends: 'torchbox',
rules: {
"no-restricted-globals": 0,
"no-use-before-define": 0
},
globals: {
Expand Down
26 changes: 12 additions & 14 deletions cf-2fa-verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,17 @@ const TOKENS = {
"example.com": "deadbeef5",
};

async function handleRequest(request) {
const url = new URL(request.url);
export default {
handleRequest(request) {
const url = new URL(request.url);

const verificationToken = TOKENS[url.host];
if (verificationToken) {
// If the domain is known and we have a token, return it.
return new Response(verificationToken);
}
const verificationToken = TOKENS[url.host];
if (verificationToken) {
// If the domain is known and we have a token, return it.
return new Response(verificationToken);
}

// Return something nonsensical for an invalid domain
return new Response("", { status: 404 });
}

addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});
// Return something nonsensical for an invalid domain
return new Response("", { status: 404 });
},
};
54 changes: 25 additions & 29 deletions common-caching.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,40 +97,36 @@ const STRIP_VALUELESS_QUERYSTRING_KEYS = false;
// (from https://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.4)
const CACHABLE_HTTP_STATUS_CODES = [200, 203, 206, 300, 301, 410];

addEventListener("fetch", (event) => {
event.respondWith(main(event));
});

async function main(event) {
const cache = caches.default;
let { request } = event;
let strippedParams;
// eslint-disable-next-line prefer-const
[request, strippedParams] = stripQuerystring(request);

if (!requestIsCachable(request)) {
// If the request isn't cacheable, return a Response directly from the origin.
return fetch(request);
}
export default {
async fetch(originalRequest, env, ctx) {
const cache = caches.default;
// eslint-disable-next-line prefer-const
const [request, strippedParams] = stripQuerystring(originalRequest);

if (!requestIsCachable(request)) {
// If the request isn't cacheable, return a Response directly from the origin.
return fetch(request);
}

const cachingRequest = getCachingRequest(request);
let response = await cache.match(cachingRequest);
const cachingRequest = getCachingRequest(request);
let response = await cache.match(cachingRequest);

if (!response) {
// If we didn't get a response from the cache, fetch one from the origin
// and put it in the cache.
response = await fetch(request);
if (responseIsCachable(response)) {
event.waitUntil(cache.put(cachingRequest, response.clone()));
if (!response) {
// If we didn't get a response from the cache, fetch one from the origin
// and put it in the cache.
response = await fetch(request);
if (responseIsCachable(response)) {
ctx.waitUntil(cache.put(cachingRequest, response.clone()));
}
}
}

if (REPLACE_STRIPPED_QUERYSTRING_ON_REDIRECT_LOCATION) {
response = replaceStrippedQsOnRedirectResponse(response, strippedParams);
}
if (REPLACE_STRIPPED_QUERYSTRING_ON_REDIRECT_LOCATION) {
response = replaceStrippedQsOnRedirectResponse(response, strippedParams);
}

return response;
}
return response;
},
};

/*
* Cacheability Utilities
Expand Down
28 changes: 13 additions & 15 deletions holding-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,19 @@
// Keep it simple, small and self-contained.
const HOLDING_PAGE_URL = "https://example.com";

addEventListener("fetch", (event) => {
event.respondWith(fetchAndReplace());
});
export default {
async fetch() {
const modifiedHeaders = new Headers();

async function fetchAndReplace() {
const modifiedHeaders = new Headers();
modifiedHeaders.set("Content-Type", "text/html");
modifiedHeaders.append("Pragma", "no-cache");

modifiedHeaders.set("Content-Type", "text/html");
modifiedHeaders.append("Pragma", "no-cache");
const holdingPage = await fetch(HOLDING_PAGE_URL);
const content = await holdingPage.text();

const holdingPage = await fetch(HOLDING_PAGE_URL);
const content = await holdingPage.text();

// Return modified response.
return new Response(content, {
headers: modifiedHeaders,
});
}
// Return modified response.
return new Response(content, {
headers: modifiedHeaders,
});
},
};
22 changes: 8 additions & 14 deletions insert-banner.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Inject some HTML before a given element in the response
// Uses the HTMLRewriter API https://developers.cloudflare.com/workers/runtime-apis/html-rewriter

addEventListener("fetch", (event) => {
event.respondWith(main(event));
});

async function main(event) {
const response = await fetch(event.request);
return addBanner(response);
}
export default {
async fetch(request) {
const response = await fetch(request);
return new HTMLRewriter()
.on("*", new BannerElementHandler())
.transform(response);
},
};

class BannerElementHandler {
static async element(element) {
Expand All @@ -20,9 +20,3 @@ class BannerElementHandler {
);
}
}

function addBanner(response) {
return new HTMLRewriter()
.on("*", new BannerElementHandler())
.transform(response);
}
22 changes: 8 additions & 14 deletions insert-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
// e.g.
// <!-- include https://www.google.co.uk -->

addEventListener("fetch", (event) => {
event.respondWith(main(event));
});

async function main(event) {
const response = await fetch(event.request);
return insertContent(response);
}
export default {
async fetch(request) {
const response = await fetch(request);
return new HTMLRewriter()
.onDocument(new DocumentHandler(response))
.transform(response);
},
};

class DocumentHandler {
constructor(response) {
Expand Down Expand Up @@ -37,9 +37,3 @@ class DocumentHandler {
}
}
}

function insertContent(response) {
return new HTMLRewriter()
.onDocument(new DocumentHandler(response))
.transform(response);
}
Loading