commit 230feecf5e0c1ea59a1b09eafbc64d11efa42807 Author: Riley Smith Date: Fri Jan 21 23:59:24 2022 -0800 first commit diff --git a/.cargo-ok b/.cargo-ok new file mode 100644 index 0000000..e69de29 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..53c37a1 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +dist \ No newline at end of file diff --git a/public/404.html b/public/404.html new file mode 100644 index 0000000..5642852 --- /dev/null +++ b/public/404.html @@ -0,0 +1,40 @@ + + + + + + + + + +
+

404 Not Found

+

Oh dang! We couldn't find that page.

+ a sad crab is unable to unable to lasso a paper airplane. 404 not found. +
+ + diff --git a/public/favicon.ico b/public/favicon.ico new file mode 100644 index 0000000..cc6c23b Binary files /dev/null and b/public/favicon.ico differ diff --git a/public/img/200-wrangler-ferris.gif b/public/img/200-wrangler-ferris.gif new file mode 100644 index 0000000..8853751 Binary files /dev/null and b/public/img/200-wrangler-ferris.gif differ diff --git a/public/img/404-wrangler-ferris.gif b/public/img/404-wrangler-ferris.gif new file mode 100644 index 0000000..0ac1479 Binary files /dev/null and b/public/img/404-wrangler-ferris.gif differ diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..d07cf62 --- /dev/null +++ b/public/index.html @@ -0,0 +1,40 @@ + + + + + + + + + +
+

200 Success

+

Hello World! Welcome to your Workers Site.

+ a happy crab is wearing a cowboy hat and holding a lasso. 200 success. +
+ + diff --git a/workers-site/.gitignore b/workers-site/.gitignore new file mode 100644 index 0000000..7915249 --- /dev/null +++ b/workers-site/.gitignore @@ -0,0 +1,3 @@ +node_modules +dist +worker diff --git a/workers-site/index.js b/workers-site/index.js new file mode 100644 index 0000000..806f91a --- /dev/null +++ b/workers-site/index.js @@ -0,0 +1,81 @@ +import { getAssetFromKV, mapRequestToAsset } from '@cloudflare/kv-asset-handler' + +/** + * The DEBUG flag will do two things that help during development: + * 1. we will skip caching on the edge, which makes it easier to + * debug. + * 2. we will return an error message on exception in your Response rather + * than the default 404.html page. + */ +const DEBUG = false + +addEventListener('fetch', event => { + event.respondWith(handleEvent(event)) +}) + +async function handleEvent(event) { + let options = {} + + /** + * You can add custom logic to how we fetch your assets + * by configuring the function `mapRequestToAsset` + */ + // options.mapRequestToAsset = handlePrefix(/^\/docs/) + + try { + if (DEBUG) { + // customize caching + options.cacheControl = { + bypassCache: true, + } + } + + const page = await getAssetFromKV(event, options) + + // allow headers to be altered + const response = new Response(page.body, page) + + response.headers.set('X-XSS-Protection', '1; mode=block') + response.headers.set('X-Content-Type-Options', 'nosniff') + response.headers.set('X-Frame-Options', 'DENY') + response.headers.set('Referrer-Policy', 'unsafe-url') + response.headers.set('Feature-Policy', 'none') + + return response + + } catch (e) { + // if an error is thrown try to serve the asset at 404.html + if (!DEBUG) { + try { + let notFoundResponse = await getAssetFromKV(event, { + mapRequestToAsset: req => new Request(`${new URL(req.url).origin}/404.html`, req), + }) + + return new Response(notFoundResponse.body, { ...notFoundResponse, status: 404 }) + } catch (e) {} + } + + return new Response(e.message || e.toString(), { status: 500 }) + } +} + +/** + * Here's one example of how to modify a request to + * remove a specific prefix, in this case `/docs` from + * the url. This can be useful if you are deploying to a + * route on a zone, or if you only want your static content + * to exist at a specific path. + */ +function handlePrefix(prefix) { + return request => { + // compute the default (e.g. / -> index.html) + let defaultAssetKey = mapRequestToAsset(request) + let url = new URL(defaultAssetKey.url) + + // strip the prefix from the path for lookup + url.pathname = url.pathname.replace(prefix, '/') + + // inherit all other props from the default request + return new Request(url.toString(), defaultAssetKey) + } +} diff --git a/workers-site/package-lock.json b/workers-site/package-lock.json new file mode 100644 index 0000000..8c7b88b --- /dev/null +++ b/workers-site/package-lock.json @@ -0,0 +1,20 @@ +{ + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "@cloudflare/kv-asset-handler": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/@cloudflare/kv-asset-handler/-/kv-asset-handler-0.1.2.tgz", + "integrity": "sha512-otES1gV5mEhNh82p/sJERPMMrC7UOLV2JyfKf4e3EX1TmMkZ3N8IDGAqRNsoRU8UYTO7wc83I7pH1p4ozAdgMQ==", + "requires": { + "mime": "^2.5.2" + } + }, + "mime": { + "version": "2.5.2", + "resolved": "https://registry.npmjs.org/mime/-/mime-2.5.2.tgz", + "integrity": "sha512-tqkh47FzKeCPD2PUiPB6pkbMzsCasjxAfC62/Wap5qrUWcb+sFasXUC5I3gYM5iBM8v/Qpn4UK0x+j0iHyFPDg==" + } + } +} diff --git a/workers-site/package.json b/workers-site/package.json new file mode 100644 index 0000000..2b4250e --- /dev/null +++ b/workers-site/package.json @@ -0,0 +1,10 @@ +{ + "private": true, + "version": "1.0.0", + "description": "A template for kick starting a Cloudflare Workers project", + "main": "index.js", + "license": "MIT", + "dependencies": { + "@cloudflare/kv-asset-handler": "~0.1.2" + } +} diff --git a/wrangler.toml b/wrangler.toml new file mode 100644 index 0000000..921c6a6 --- /dev/null +++ b/wrangler.toml @@ -0,0 +1,6 @@ +account_id = "" +name = "velvox-dev" +type = "webpack" +workers_dev = true +site = { bucket = "./public" } +compatibility_date = "2022-01-22"